Move some declarations to source.h
[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
95554aad
TT
2188static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2189 enum language pretend_language);
2190
ed2dc618 2191static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 2192
b303c6f6
AB
2193/* Class, the destructor of which frees all allocated queue entries. This
2194 will only have work to do if an error was thrown while processing the
2195 dwarf. If no error was thrown then the queue entries should have all
2196 been processed, and freed, as we went along. */
2197
2198class dwarf2_queue_guard
2199{
2200public:
2201 dwarf2_queue_guard () = default;
2202
2203 /* Free any entries remaining on the queue. There should only be
2204 entries left if we hit an error while processing the dwarf. */
2205 ~dwarf2_queue_guard ()
2206 {
2207 struct dwarf2_queue_item *item, *last;
2208
2209 item = dwarf2_queue;
2210 while (item)
2211 {
2212 /* Anything still marked queued is likely to be in an
2213 inconsistent state, so discard it. */
2214 if (item->per_cu->queued)
2215 {
2216 if (item->per_cu->cu != NULL)
2217 free_one_cached_comp_unit (item->per_cu);
2218 item->per_cu->queued = 0;
2219 }
2220
2221 last = item;
2222 item = item->next;
2223 xfree (last);
2224 }
2225
2226 dwarf2_queue = dwarf2_queue_tail = NULL;
2227 }
2228};
2229
d721ba37
PA
2230/* The return type of find_file_and_directory. Note, the enclosed
2231 string pointers are only valid while this object is valid. */
2232
2233struct file_and_directory
2234{
2235 /* The filename. This is never NULL. */
2236 const char *name;
2237
2238 /* The compilation directory. NULL if not known. If we needed to
2239 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2240 points directly to the DW_AT_comp_dir string attribute owned by
2241 the obstack that owns the DIE. */
2242 const char *comp_dir;
2243
2244 /* If we needed to build a new string for comp_dir, this is what
2245 owns the storage. */
2246 std::string comp_dir_storage;
2247};
2248
2249static file_and_directory find_file_and_directory (struct die_info *die,
2250 struct dwarf2_cu *cu);
9291a0cd
TT
2251
2252static char *file_full_name (int file, struct line_header *lh,
2253 const char *comp_dir);
2254
43988095
JK
2255/* Expected enum dwarf_unit_type for read_comp_unit_head. */
2256enum class rcuh_kind { COMPILE, TYPE };
2257
d521ce57 2258static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
2259 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2260 struct comp_unit_head *header,
36586728 2261 struct dwarf2_section_info *section,
d521ce57 2262 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 2263 rcuh_kind section_kind);
36586728 2264
fd820528 2265static void init_cutu_and_read_dies
f4dc4d17
DE
2266 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2267 int use_existing_cu, int keep,
3019eac3
DE
2268 die_reader_func_ftype *die_reader_func, void *data);
2269
dee91e82
DE
2270static void init_cutu_and_read_dies_simple
2271 (struct dwarf2_per_cu_data *this_cu,
2272 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 2273
673bfd45 2274static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2275
3019eac3
DE
2276static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2277
57d63ce2 2278static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
2279 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2280 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 2281 ULONGEST signature, int is_debug_types);
a2ce51a0 2282
ed2dc618
SM
2283static struct dwp_file *get_dwp_file
2284 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 2285
3019eac3 2286static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2287 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2288
2289static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2290 (struct signatured_type *, const char *, const char *);
3019eac3 2291
89e63ee4
DE
2292static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2293
3019eac3
DE
2294static void free_dwo_file_cleanup (void *);
2295
ed2dc618
SM
2296struct free_dwo_file_cleanup_data
2297{
2298 struct dwo_file *dwo_file;
2299 struct dwarf2_per_objfile *dwarf2_per_objfile;
2300};
2301
2302static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 2303
1b80a9fa 2304static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2305
2306static void free_line_header_voidp (void *arg);
4390d890
DE
2307\f
2308/* Various complaints about symbol reading that don't abort the process. */
2309
2310static void
2311dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2312{
2313 complaint (&symfile_complaints,
2314 _("statement list doesn't fit in .debug_line section"));
2315}
2316
2317static void
2318dwarf2_debug_line_missing_file_complaint (void)
2319{
2320 complaint (&symfile_complaints,
2321 _(".debug_line section has line data without a file"));
2322}
2323
2324static void
2325dwarf2_debug_line_missing_end_sequence_complaint (void)
2326{
2327 complaint (&symfile_complaints,
2328 _(".debug_line section has line "
2329 "program sequence without an end"));
2330}
2331
2332static void
2333dwarf2_complex_location_expr_complaint (void)
2334{
2335 complaint (&symfile_complaints, _("location expression too complex"));
2336}
2337
2338static void
2339dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2340 int arg3)
2341{
2342 complaint (&symfile_complaints,
2343 _("const value length mismatch for '%s', got %d, expected %d"),
2344 arg1, arg2, arg3);
2345}
2346
2347static void
2348dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2349{
2350 complaint (&symfile_complaints,
2351 _("debug info runs off end of %s section"
2352 " [in module %s]"),
a32a8923
DE
2353 get_section_name (section),
2354 get_section_file_name (section));
4390d890 2355}
1b80a9fa 2356
4390d890
DE
2357static void
2358dwarf2_macro_malformed_definition_complaint (const char *arg1)
2359{
2360 complaint (&symfile_complaints,
2361 _("macro debug info contains a "
2362 "malformed macro definition:\n`%s'"),
2363 arg1);
2364}
2365
2366static void
2367dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2368{
2369 complaint (&symfile_complaints,
2370 _("invalid attribute class or form for '%s' in '%s'"),
2371 arg1, arg2);
2372}
527f3840
JK
2373
2374/* Hash function for line_header_hash. */
2375
2376static hashval_t
2377line_header_hash (const struct line_header *ofs)
2378{
9c541725 2379 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2380}
2381
2382/* Hash function for htab_create_alloc_ex for line_header_hash. */
2383
2384static hashval_t
2385line_header_hash_voidp (const void *item)
2386{
9a3c8263 2387 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2388
2389 return line_header_hash (ofs);
2390}
2391
2392/* Equality function for line_header_hash. */
2393
2394static int
2395line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2396{
9a3c8263
SM
2397 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2398 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2399
9c541725 2400 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2401 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2402}
2403
4390d890 2404\f
9291a0cd 2405
31aa7e4e
JB
2406/* Read the given attribute value as an address, taking the attribute's
2407 form into account. */
2408
2409static CORE_ADDR
2410attr_value_as_address (struct attribute *attr)
2411{
2412 CORE_ADDR addr;
2413
2414 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2415 {
2416 /* Aside from a few clearly defined exceptions, attributes that
2417 contain an address must always be in DW_FORM_addr form.
2418 Unfortunately, some compilers happen to be violating this
2419 requirement by encoding addresses using other forms, such
2420 as DW_FORM_data4 for example. For those broken compilers,
2421 we try to do our best, without any guarantee of success,
2422 to interpret the address correctly. It would also be nice
2423 to generate a complaint, but that would require us to maintain
2424 a list of legitimate cases where a non-address form is allowed,
2425 as well as update callers to pass in at least the CU's DWARF
2426 version. This is more overhead than what we're willing to
2427 expand for a pretty rare case. */
2428 addr = DW_UNSND (attr);
2429 }
2430 else
2431 addr = DW_ADDR (attr);
2432
2433 return addr;
2434}
2435
9291a0cd 2436/* The suffix for an index file. */
437afbb8
JK
2437#define INDEX4_SUFFIX ".gdb-index"
2438#define INDEX5_SUFFIX ".debug_names"
2439#define DEBUG_STR_SUFFIX ".debug_str"
9291a0cd 2440
330cdd98
PA
2441/* See declaration. */
2442
2443dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2444 const dwarf2_debug_sections *names)
2445 : objfile (objfile_)
2446{
2447 if (names == NULL)
2448 names = &dwarf2_elf_names;
2449
2450 bfd *obfd = objfile->obfd;
2451
2452 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2453 locate_sections (obfd, sec, *names);
2454}
2455
fc8e7e75
SM
2456static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2457
330cdd98
PA
2458dwarf2_per_objfile::~dwarf2_per_objfile ()
2459{
2460 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2461 free_cached_comp_units ();
2462
2463 if (quick_file_names_table)
2464 htab_delete (quick_file_names_table);
2465
2466 if (line_header_hash)
2467 htab_delete (line_header_hash);
2468
fc8e7e75
SM
2469 for (int ix = 0; ix < n_comp_units; ++ix)
2470 VEC_free (dwarf2_per_cu_ptr, all_comp_units[ix]->imported_symtabs);
2471
2472 for (int ix = 0; ix < n_type_units; ++ix)
2473 VEC_free (dwarf2_per_cu_ptr,
2474 all_type_units[ix]->per_cu.imported_symtabs);
2475 xfree (all_type_units);
2476
2477 VEC_free (dwarf2_section_info_def, types);
2478
2479 if (dwo_files != NULL)
2480 free_dwo_files (dwo_files, objfile);
2481 if (dwp_file != NULL)
2482 gdb_bfd_unref (dwp_file->dbfd);
2483
2484 if (dwz_file != NULL && dwz_file->dwz_bfd)
2485 gdb_bfd_unref (dwz_file->dwz_bfd);
2486
2487 if (index_table != NULL)
2488 index_table->~mapped_index ();
2489
330cdd98
PA
2490 /* Everything else should be on the objfile obstack. */
2491}
2492
2493/* See declaration. */
2494
2495void
2496dwarf2_per_objfile::free_cached_comp_units ()
2497{
2498 dwarf2_per_cu_data *per_cu = read_in_chain;
2499 dwarf2_per_cu_data **last_chain = &read_in_chain;
2500 while (per_cu != NULL)
2501 {
2502 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2503
fcd3b13d 2504 delete per_cu->cu;
330cdd98
PA
2505 *last_chain = next_cu;
2506 per_cu = next_cu;
2507 }
2508}
2509
c906108c 2510/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2511 information and return true if we have enough to do something.
2512 NAMES points to the dwarf2 section names, or is NULL if the standard
2513 ELF names are used. */
c906108c
SS
2514
2515int
251d32d9
TG
2516dwarf2_has_info (struct objfile *objfile,
2517 const struct dwarf2_debug_sections *names)
c906108c 2518{
97cbe998
SDJ
2519 if (objfile->flags & OBJF_READNEVER)
2520 return 0;
2521
ed2dc618
SM
2522 struct dwarf2_per_objfile *dwarf2_per_objfile
2523 = get_dwarf2_per_objfile (objfile);
2524
2525 if (dwarf2_per_objfile == NULL)
be391dca
TT
2526 {
2527 /* Initialize per-objfile state. */
2528 struct dwarf2_per_objfile *data
8d749320 2529 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
9a619af0 2530
330cdd98 2531 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
ed2dc618 2532 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
be391dca 2533 }
73869dc2 2534 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2535 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2536 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2537 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2538}
2539
2540/* Return the containing section of virtual section SECTION. */
2541
2542static struct dwarf2_section_info *
2543get_containing_section (const struct dwarf2_section_info *section)
2544{
2545 gdb_assert (section->is_virtual);
2546 return section->s.containing_section;
c906108c
SS
2547}
2548
a32a8923
DE
2549/* Return the bfd owner of SECTION. */
2550
2551static struct bfd *
2552get_section_bfd_owner (const struct dwarf2_section_info *section)
2553{
73869dc2
DE
2554 if (section->is_virtual)
2555 {
2556 section = get_containing_section (section);
2557 gdb_assert (!section->is_virtual);
2558 }
049412e3 2559 return section->s.section->owner;
a32a8923
DE
2560}
2561
2562/* Return the bfd section of SECTION.
2563 Returns NULL if the section is not present. */
2564
2565static asection *
2566get_section_bfd_section (const struct dwarf2_section_info *section)
2567{
73869dc2
DE
2568 if (section->is_virtual)
2569 {
2570 section = get_containing_section (section);
2571 gdb_assert (!section->is_virtual);
2572 }
049412e3 2573 return section->s.section;
a32a8923
DE
2574}
2575
2576/* Return the name of SECTION. */
2577
2578static const char *
2579get_section_name (const struct dwarf2_section_info *section)
2580{
2581 asection *sectp = get_section_bfd_section (section);
2582
2583 gdb_assert (sectp != NULL);
2584 return bfd_section_name (get_section_bfd_owner (section), sectp);
2585}
2586
2587/* Return the name of the file SECTION is in. */
2588
2589static const char *
2590get_section_file_name (const struct dwarf2_section_info *section)
2591{
2592 bfd *abfd = get_section_bfd_owner (section);
2593
2594 return bfd_get_filename (abfd);
2595}
2596
2597/* Return the id of SECTION.
2598 Returns 0 if SECTION doesn't exist. */
2599
2600static int
2601get_section_id (const struct dwarf2_section_info *section)
2602{
2603 asection *sectp = get_section_bfd_section (section);
2604
2605 if (sectp == NULL)
2606 return 0;
2607 return sectp->id;
2608}
2609
2610/* Return the flags of SECTION.
73869dc2 2611 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2612
2613static int
2614get_section_flags (const struct dwarf2_section_info *section)
2615{
2616 asection *sectp = get_section_bfd_section (section);
2617
2618 gdb_assert (sectp != NULL);
2619 return bfd_get_section_flags (sectp->owner, sectp);
2620}
2621
251d32d9
TG
2622/* When loading sections, we look either for uncompressed section or for
2623 compressed section names. */
233a11ab
CS
2624
2625static int
251d32d9
TG
2626section_is_p (const char *section_name,
2627 const struct dwarf2_section_names *names)
233a11ab 2628{
251d32d9
TG
2629 if (names->normal != NULL
2630 && strcmp (section_name, names->normal) == 0)
2631 return 1;
2632 if (names->compressed != NULL
2633 && strcmp (section_name, names->compressed) == 0)
2634 return 1;
2635 return 0;
233a11ab
CS
2636}
2637
330cdd98 2638/* See declaration. */
c906108c 2639
330cdd98
PA
2640void
2641dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2642 const dwarf2_debug_sections &names)
c906108c 2643{
dc7650b8 2644 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2645
dc7650b8
JK
2646 if ((aflag & SEC_HAS_CONTENTS) == 0)
2647 {
2648 }
330cdd98 2649 else if (section_is_p (sectp->name, &names.info))
c906108c 2650 {
330cdd98
PA
2651 this->info.s.section = sectp;
2652 this->info.size = bfd_get_section_size (sectp);
c906108c 2653 }
330cdd98 2654 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2655 {
330cdd98
PA
2656 this->abbrev.s.section = sectp;
2657 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2658 }
330cdd98 2659 else if (section_is_p (sectp->name, &names.line))
c906108c 2660 {
330cdd98
PA
2661 this->line.s.section = sectp;
2662 this->line.size = bfd_get_section_size (sectp);
c906108c 2663 }
330cdd98 2664 else if (section_is_p (sectp->name, &names.loc))
c906108c 2665 {
330cdd98
PA
2666 this->loc.s.section = sectp;
2667 this->loc.size = bfd_get_section_size (sectp);
c906108c 2668 }
330cdd98 2669 else if (section_is_p (sectp->name, &names.loclists))
43988095 2670 {
330cdd98
PA
2671 this->loclists.s.section = sectp;
2672 this->loclists.size = bfd_get_section_size (sectp);
43988095 2673 }
330cdd98 2674 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2675 {
330cdd98
PA
2676 this->macinfo.s.section = sectp;
2677 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2678 }
330cdd98 2679 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2680 {
330cdd98
PA
2681 this->macro.s.section = sectp;
2682 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2683 }
330cdd98 2684 else if (section_is_p (sectp->name, &names.str))
c906108c 2685 {
330cdd98
PA
2686 this->str.s.section = sectp;
2687 this->str.size = bfd_get_section_size (sectp);
c906108c 2688 }
330cdd98 2689 else if (section_is_p (sectp->name, &names.line_str))
43988095 2690 {
330cdd98
PA
2691 this->line_str.s.section = sectp;
2692 this->line_str.size = bfd_get_section_size (sectp);
43988095 2693 }
330cdd98 2694 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2695 {
330cdd98
PA
2696 this->addr.s.section = sectp;
2697 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2698 }
330cdd98 2699 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2700 {
330cdd98
PA
2701 this->frame.s.section = sectp;
2702 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2703 }
330cdd98 2704 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2705 {
330cdd98
PA
2706 this->eh_frame.s.section = sectp;
2707 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2708 }
330cdd98 2709 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2710 {
330cdd98
PA
2711 this->ranges.s.section = sectp;
2712 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2713 }
330cdd98 2714 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2715 {
330cdd98
PA
2716 this->rnglists.s.section = sectp;
2717 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2718 }
330cdd98 2719 else if (section_is_p (sectp->name, &names.types))
348e048f 2720 {
8b70b953
TT
2721 struct dwarf2_section_info type_section;
2722
2723 memset (&type_section, 0, sizeof (type_section));
049412e3 2724 type_section.s.section = sectp;
8b70b953
TT
2725 type_section.size = bfd_get_section_size (sectp);
2726
330cdd98 2727 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2728 &type_section);
348e048f 2729 }
330cdd98 2730 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2731 {
330cdd98
PA
2732 this->gdb_index.s.section = sectp;
2733 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2734 }
927aa2e7
JK
2735 else if (section_is_p (sectp->name, &names.debug_names))
2736 {
2737 this->debug_names.s.section = sectp;
2738 this->debug_names.size = bfd_get_section_size (sectp);
2739 }
2740 else if (section_is_p (sectp->name, &names.debug_aranges))
2741 {
2742 this->debug_aranges.s.section = sectp;
2743 this->debug_aranges.size = bfd_get_section_size (sectp);
2744 }
dce234bc 2745
b4e1fd61 2746 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2747 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2748 this->has_section_at_zero = true;
c906108c
SS
2749}
2750
fceca515
DE
2751/* A helper function that decides whether a section is empty,
2752 or not present. */
9e0ac564
TT
2753
2754static int
19ac8c2e 2755dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2756{
73869dc2
DE
2757 if (section->is_virtual)
2758 return section->size == 0;
049412e3 2759 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2760}
2761
3019eac3
DE
2762/* Read the contents of the section INFO.
2763 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2764 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2765 of the DWO file.
dce234bc 2766 If the section is compressed, uncompress it before returning. */
c906108c 2767
dce234bc
PP
2768static void
2769dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2770{
a32a8923 2771 asection *sectp;
3019eac3 2772 bfd *abfd;
dce234bc 2773 gdb_byte *buf, *retbuf;
c906108c 2774
be391dca
TT
2775 if (info->readin)
2776 return;
dce234bc 2777 info->buffer = NULL;
be391dca 2778 info->readin = 1;
188dd5d6 2779
9e0ac564 2780 if (dwarf2_section_empty_p (info))
dce234bc 2781 return;
c906108c 2782
a32a8923 2783 sectp = get_section_bfd_section (info);
3019eac3 2784
73869dc2
DE
2785 /* If this is a virtual section we need to read in the real one first. */
2786 if (info->is_virtual)
2787 {
2788 struct dwarf2_section_info *containing_section =
2789 get_containing_section (info);
2790
2791 gdb_assert (sectp != NULL);
2792 if ((sectp->flags & SEC_RELOC) != 0)
2793 {
2794 error (_("Dwarf Error: DWP format V2 with relocations is not"
2795 " supported in section %s [in module %s]"),
2796 get_section_name (info), get_section_file_name (info));
2797 }
2798 dwarf2_read_section (objfile, containing_section);
2799 /* Other code should have already caught virtual sections that don't
2800 fit. */
2801 gdb_assert (info->virtual_offset + info->size
2802 <= containing_section->size);
2803 /* If the real section is empty or there was a problem reading the
2804 section we shouldn't get here. */
2805 gdb_assert (containing_section->buffer != NULL);
2806 info->buffer = containing_section->buffer + info->virtual_offset;
2807 return;
2808 }
2809
4bf44c1c
TT
2810 /* If the section has relocations, we must read it ourselves.
2811 Otherwise we attach it to the BFD. */
2812 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2813 {
d521ce57 2814 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2815 return;
dce234bc 2816 }
dce234bc 2817
224c3ddb 2818 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2819 info->buffer = buf;
dce234bc
PP
2820
2821 /* When debugging .o files, we may need to apply relocations; see
2822 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2823 We never compress sections in .o files, so we only need to
2824 try this when the section is not compressed. */
ac8035ab 2825 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2826 if (retbuf != NULL)
2827 {
2828 info->buffer = retbuf;
2829 return;
2830 }
2831
a32a8923
DE
2832 abfd = get_section_bfd_owner (info);
2833 gdb_assert (abfd != NULL);
2834
dce234bc
PP
2835 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2836 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2837 {
2838 error (_("Dwarf Error: Can't read DWARF data"
2839 " in section %s [in module %s]"),
2840 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2841 }
dce234bc
PP
2842}
2843
9e0ac564
TT
2844/* A helper function that returns the size of a section in a safe way.
2845 If you are positive that the section has been read before using the
2846 size, then it is safe to refer to the dwarf2_section_info object's
2847 "size" field directly. In other cases, you must call this
2848 function, because for compressed sections the size field is not set
2849 correctly until the section has been read. */
2850
2851static bfd_size_type
2852dwarf2_section_size (struct objfile *objfile,
2853 struct dwarf2_section_info *info)
2854{
2855 if (!info->readin)
2856 dwarf2_read_section (objfile, info);
2857 return info->size;
2858}
2859
dce234bc 2860/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2861 SECTION_NAME. */
af34e669 2862
dce234bc 2863void
3017a003
TG
2864dwarf2_get_section_info (struct objfile *objfile,
2865 enum dwarf2_section_enum sect,
d521ce57 2866 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2867 bfd_size_type *sizep)
2868{
2869 struct dwarf2_per_objfile *data
9a3c8263
SM
2870 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2871 dwarf2_objfile_data_key);
dce234bc 2872 struct dwarf2_section_info *info;
a3b2a86b
TT
2873
2874 /* We may see an objfile without any DWARF, in which case we just
2875 return nothing. */
2876 if (data == NULL)
2877 {
2878 *sectp = NULL;
2879 *bufp = NULL;
2880 *sizep = 0;
2881 return;
2882 }
3017a003
TG
2883 switch (sect)
2884 {
2885 case DWARF2_DEBUG_FRAME:
2886 info = &data->frame;
2887 break;
2888 case DWARF2_EH_FRAME:
2889 info = &data->eh_frame;
2890 break;
2891 default:
2892 gdb_assert_not_reached ("unexpected section");
2893 }
dce234bc 2894
9e0ac564 2895 dwarf2_read_section (objfile, info);
dce234bc 2896
a32a8923 2897 *sectp = get_section_bfd_section (info);
dce234bc
PP
2898 *bufp = info->buffer;
2899 *sizep = info->size;
2900}
2901
36586728
TT
2902/* A helper function to find the sections for a .dwz file. */
2903
2904static void
2905locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2906{
9a3c8263 2907 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2908
2909 /* Note that we only support the standard ELF names, because .dwz
2910 is ELF-only (at the time of writing). */
2911 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2912 {
049412e3 2913 dwz_file->abbrev.s.section = sectp;
36586728
TT
2914 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2915 }
2916 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2917 {
049412e3 2918 dwz_file->info.s.section = sectp;
36586728
TT
2919 dwz_file->info.size = bfd_get_section_size (sectp);
2920 }
2921 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2922 {
049412e3 2923 dwz_file->str.s.section = sectp;
36586728
TT
2924 dwz_file->str.size = bfd_get_section_size (sectp);
2925 }
2926 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2927 {
049412e3 2928 dwz_file->line.s.section = sectp;
36586728
TT
2929 dwz_file->line.size = bfd_get_section_size (sectp);
2930 }
2931 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2932 {
049412e3 2933 dwz_file->macro.s.section = sectp;
36586728
TT
2934 dwz_file->macro.size = bfd_get_section_size (sectp);
2935 }
2ec9a5e0
TT
2936 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2937 {
049412e3 2938 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2939 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2940 }
927aa2e7
JK
2941 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2942 {
2943 dwz_file->debug_names.s.section = sectp;
2944 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2945 }
36586728
TT
2946}
2947
4db1a1dc
TT
2948/* Open the separate '.dwz' debug file, if needed. Return NULL if
2949 there is no .gnu_debugaltlink section in the file. Error if there
2950 is such a section but the file cannot be found. */
36586728
TT
2951
2952static struct dwz_file *
ed2dc618 2953dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2954{
36586728
TT
2955 const char *filename;
2956 struct dwz_file *result;
acd13123 2957 bfd_size_type buildid_len_arg;
dc294be5
TT
2958 size_t buildid_len;
2959 bfd_byte *buildid;
36586728
TT
2960
2961 if (dwarf2_per_objfile->dwz_file != NULL)
2962 return dwarf2_per_objfile->dwz_file;
2963
4db1a1dc 2964 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2965 gdb::unique_xmalloc_ptr<char> data
2966 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2967 &buildid_len_arg, &buildid));
4db1a1dc
TT
2968 if (data == NULL)
2969 {
2970 if (bfd_get_error () == bfd_error_no_error)
2971 return NULL;
2972 error (_("could not read '.gnu_debugaltlink' section: %s"),
2973 bfd_errmsg (bfd_get_error ()));
2974 }
791afaa2
TT
2975
2976 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2977
acd13123
TT
2978 buildid_len = (size_t) buildid_len_arg;
2979
791afaa2 2980 filename = data.get ();
d721ba37
PA
2981
2982 std::string abs_storage;
36586728
TT
2983 if (!IS_ABSOLUTE_PATH (filename))
2984 {
14278e1f
TT
2985 gdb::unique_xmalloc_ptr<char> abs
2986 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2987
14278e1f 2988 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2989 filename = abs_storage.c_str ();
36586728
TT
2990 }
2991
dc294be5
TT
2992 /* First try the file name given in the section. If that doesn't
2993 work, try to use the build-id instead. */
192b62ce 2994 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2995 if (dwz_bfd != NULL)
36586728 2996 {
192b62ce
TT
2997 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2998 dwz_bfd.release ();
36586728
TT
2999 }
3000
dc294be5
TT
3001 if (dwz_bfd == NULL)
3002 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
3003
3004 if (dwz_bfd == NULL)
3005 error (_("could not find '.gnu_debugaltlink' file for %s"),
3006 objfile_name (dwarf2_per_objfile->objfile));
3007
36586728
TT
3008 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
3009 struct dwz_file);
192b62ce 3010 result->dwz_bfd = dwz_bfd.release ();
36586728 3011
192b62ce 3012 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728 3013
192b62ce 3014 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 3015 dwarf2_per_objfile->dwz_file = result;
36586728
TT
3016 return result;
3017}
9291a0cd 3018\f
7b9f3c50
DE
3019/* DWARF quick_symbols_functions support. */
3020
3021/* TUs can share .debug_line entries, and there can be a lot more TUs than
3022 unique line tables, so we maintain a separate table of all .debug_line
3023 derived entries to support the sharing.
3024 All the quick functions need is the list of file names. We discard the
3025 line_header when we're done and don't need to record it here. */
3026struct quick_file_names
3027{
094b34ac
DE
3028 /* The data used to construct the hash key. */
3029 struct stmt_list_hash hash;
7b9f3c50
DE
3030
3031 /* The number of entries in file_names, real_names. */
3032 unsigned int num_file_names;
3033
3034 /* The file names from the line table, after being run through
3035 file_full_name. */
3036 const char **file_names;
3037
3038 /* The file names from the line table after being run through
3039 gdb_realpath. These are computed lazily. */
3040 const char **real_names;
3041};
3042
3043/* When using the index (and thus not using psymtabs), each CU has an
3044 object of this type. This is used to hold information needed by
3045 the various "quick" methods. */
3046struct dwarf2_per_cu_quick_data
3047{
3048 /* The file table. This can be NULL if there was no file table
3049 or it's currently not read in.
3050 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
3051 struct quick_file_names *file_names;
3052
3053 /* The corresponding symbol table. This is NULL if symbols for this
3054 CU have not yet been read. */
43f3e411 3055 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
3056
3057 /* A temporary mark bit used when iterating over all CUs in
3058 expand_symtabs_matching. */
3059 unsigned int mark : 1;
3060
3061 /* True if we've tried to read the file table and found there isn't one.
3062 There will be no point in trying to read it again next time. */
3063 unsigned int no_file_data : 1;
3064};
3065
094b34ac
DE
3066/* Utility hash function for a stmt_list_hash. */
3067
3068static hashval_t
3069hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
3070{
3071 hashval_t v = 0;
3072
3073 if (stmt_list_hash->dwo_unit != NULL)
3074 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 3075 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
3076 return v;
3077}
3078
3079/* Utility equality function for a stmt_list_hash. */
3080
3081static int
3082eq_stmt_list_entry (const struct stmt_list_hash *lhs,
3083 const struct stmt_list_hash *rhs)
3084{
3085 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
3086 return 0;
3087 if (lhs->dwo_unit != NULL
3088 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
3089 return 0;
3090
9c541725 3091 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
3092}
3093
7b9f3c50
DE
3094/* Hash function for a quick_file_names. */
3095
3096static hashval_t
3097hash_file_name_entry (const void *e)
3098{
9a3c8263
SM
3099 const struct quick_file_names *file_data
3100 = (const struct quick_file_names *) e;
7b9f3c50 3101
094b34ac 3102 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
3103}
3104
3105/* Equality function for a quick_file_names. */
3106
3107static int
3108eq_file_name_entry (const void *a, const void *b)
3109{
9a3c8263
SM
3110 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3111 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 3112
094b34ac 3113 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
3114}
3115
3116/* Delete function for a quick_file_names. */
3117
3118static void
3119delete_file_name_entry (void *e)
3120{
9a3c8263 3121 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
3122 int i;
3123
3124 for (i = 0; i < file_data->num_file_names; ++i)
3125 {
3126 xfree ((void*) file_data->file_names[i]);
3127 if (file_data->real_names)
3128 xfree ((void*) file_data->real_names[i]);
3129 }
3130
3131 /* The space for the struct itself lives on objfile_obstack,
3132 so we don't free it here. */
3133}
3134
3135/* Create a quick_file_names hash table. */
3136
3137static htab_t
3138create_quick_file_names_table (unsigned int nr_initial_entries)
3139{
3140 return htab_create_alloc (nr_initial_entries,
3141 hash_file_name_entry, eq_file_name_entry,
3142 delete_file_name_entry, xcalloc, xfree);
3143}
9291a0cd 3144
918dd910
JK
3145/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3146 have to be created afterwards. You should call age_cached_comp_units after
3147 processing PER_CU->CU. dw2_setup must have been already called. */
3148
3149static void
3150load_cu (struct dwarf2_per_cu_data *per_cu)
3151{
3019eac3 3152 if (per_cu->is_debug_types)
e5fe5e75 3153 load_full_type_unit (per_cu);
918dd910 3154 else
95554aad 3155 load_full_comp_unit (per_cu, language_minimal);
918dd910 3156
cc12ce38
DE
3157 if (per_cu->cu == NULL)
3158 return; /* Dummy CU. */
2dc860c0
DE
3159
3160 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
3161}
3162
a0f42c21 3163/* Read in the symbols for PER_CU. */
2fdf6df6 3164
9291a0cd 3165static void
a0f42c21 3166dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 3167{
ed2dc618 3168 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 3169
f4dc4d17
DE
3170 /* Skip type_unit_groups, reading the type units they contain
3171 is handled elsewhere. */
3172 if (IS_TYPE_UNIT_GROUP (per_cu))
3173 return;
3174
b303c6f6
AB
3175 /* The destructor of dwarf2_queue_guard frees any entries left on
3176 the queue. After this point we're guaranteed to leave this function
3177 with the dwarf queue empty. */
3178 dwarf2_queue_guard q_guard;
9291a0cd 3179
95554aad 3180 if (dwarf2_per_objfile->using_index
43f3e411 3181 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
3182 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3183 {
3184 queue_comp_unit (per_cu, language_minimal);
3185 load_cu (per_cu);
89e63ee4
DE
3186
3187 /* If we just loaded a CU from a DWO, and we're working with an index
3188 that may badly handle TUs, load all the TUs in that DWO as well.
3189 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3190 if (!per_cu->is_debug_types
cc12ce38 3191 && per_cu->cu != NULL
89e63ee4
DE
3192 && per_cu->cu->dwo_unit != NULL
3193 && dwarf2_per_objfile->index_table != NULL
3194 && dwarf2_per_objfile->index_table->version <= 7
3195 /* DWP files aren't supported yet. */
ed2dc618 3196 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 3197 queue_and_load_all_dwo_tus (per_cu);
95554aad 3198 }
9291a0cd 3199
ed2dc618 3200 process_queue (dwarf2_per_objfile);
9291a0cd
TT
3201
3202 /* Age the cache, releasing compilation units that have not
3203 been used recently. */
ed2dc618 3204 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
3205}
3206
3207/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3208 the objfile from which this CU came. Returns the resulting symbol
3209 table. */
2fdf6df6 3210
43f3e411 3211static struct compunit_symtab *
a0f42c21 3212dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 3213{
ed2dc618
SM
3214 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3215
95554aad 3216 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 3217 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 3218 {
ed2dc618
SM
3219 struct cleanup *back_to = make_cleanup (free_cached_comp_units,
3220 dwarf2_per_objfile);
c83dd867 3221 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 3222 dw2_do_instantiate_symtab (per_cu);
ed2dc618 3223 process_cu_includes (dwarf2_per_objfile);
9291a0cd
TT
3224 do_cleanups (back_to);
3225 }
f194fefb 3226
43f3e411 3227 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
3228}
3229
8832e7e3 3230/* Return the CU/TU given its index.
f4dc4d17
DE
3231
3232 This is intended for loops like:
3233
3234 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3235 + dwarf2_per_objfile->n_type_units); ++i)
3236 {
8832e7e3 3237 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
3238
3239 ...;
3240 }
3241*/
2fdf6df6 3242
1fd400ff 3243static struct dwarf2_per_cu_data *
ed2dc618
SM
3244dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
3245 int index)
1fd400ff
TT
3246{
3247 if (index >= dwarf2_per_objfile->n_comp_units)
3248 {
f4dc4d17 3249 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
3250 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3251 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
3252 }
3253
3254 return dwarf2_per_objfile->all_comp_units[index];
3255}
3256
8832e7e3
DE
3257/* Return the CU given its index.
3258 This differs from dw2_get_cutu in that it's for when you know INDEX
3259 refers to a CU. */
f4dc4d17
DE
3260
3261static struct dwarf2_per_cu_data *
ed2dc618 3262dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
f4dc4d17 3263{
8832e7e3 3264 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 3265
1fd400ff
TT
3266 return dwarf2_per_objfile->all_comp_units[index];
3267}
3268
4b514bc8
JK
3269/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3270 objfile_obstack, and constructed with the specified field
3271 values. */
3272
3273static dwarf2_per_cu_data *
ed2dc618 3274create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
3275 struct dwarf2_section_info *section,
3276 int is_dwz,
3277 sect_offset sect_off, ULONGEST length)
3278{
ed2dc618 3279 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
3280 dwarf2_per_cu_data *the_cu
3281 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3282 struct dwarf2_per_cu_data);
3283 the_cu->sect_off = sect_off;
3284 the_cu->length = length;
e3b94546 3285 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
3286 the_cu->section = section;
3287 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3288 struct dwarf2_per_cu_quick_data);
3289 the_cu->is_dwz = is_dwz;
3290 return the_cu;
3291}
3292
2ec9a5e0
TT
3293/* A helper for create_cus_from_index that handles a given list of
3294 CUs. */
2fdf6df6 3295
74a0d9f6 3296static void
2ec9a5e0
TT
3297create_cus_from_index_list (struct objfile *objfile,
3298 const gdb_byte *cu_list, offset_type n_elements,
3299 struct dwarf2_section_info *section,
3300 int is_dwz,
3301 int base_offset)
9291a0cd
TT
3302{
3303 offset_type i;
ed2dc618
SM
3304 struct dwarf2_per_objfile *dwarf2_per_objfile
3305 = get_dwarf2_per_objfile (objfile);
9291a0cd 3306
2ec9a5e0 3307 for (i = 0; i < n_elements; i += 2)
9291a0cd 3308 {
74a0d9f6 3309 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3310
3311 sect_offset sect_off
3312 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3313 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3314 cu_list += 2 * 8;
3315
4b514bc8 3316 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
ed2dc618
SM
3317 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3318 sect_off, length);
9291a0cd 3319 }
9291a0cd
TT
3320}
3321
2ec9a5e0 3322/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3323 the CU objects for this objfile. */
2ec9a5e0 3324
74a0d9f6 3325static void
2ec9a5e0
TT
3326create_cus_from_index (struct objfile *objfile,
3327 const gdb_byte *cu_list, offset_type cu_list_elements,
3328 const gdb_byte *dwz_list, offset_type dwz_elements)
3329{
3330 struct dwz_file *dwz;
ed2dc618
SM
3331 struct dwarf2_per_objfile *dwarf2_per_objfile
3332 = get_dwarf2_per_objfile (objfile);
2ec9a5e0
TT
3333
3334 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
3335 dwarf2_per_objfile->all_comp_units =
3336 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3337 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 3338
74a0d9f6
JK
3339 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3340 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
3341
3342 if (dwz_elements == 0)
74a0d9f6 3343 return;
2ec9a5e0 3344
ed2dc618 3345 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
74a0d9f6
JK
3346 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3347 cu_list_elements / 2);
2ec9a5e0
TT
3348}
3349
1fd400ff 3350/* Create the signatured type hash table from the index. */
673bfd45 3351
74a0d9f6 3352static void
673bfd45 3353create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3354 struct dwarf2_section_info *section,
673bfd45
DE
3355 const gdb_byte *bytes,
3356 offset_type elements)
1fd400ff
TT
3357{
3358 offset_type i;
673bfd45 3359 htab_t sig_types_hash;
ed2dc618
SM
3360 struct dwarf2_per_objfile *dwarf2_per_objfile
3361 = get_dwarf2_per_objfile (objfile);
1fd400ff 3362
6aa5f3a6
DE
3363 dwarf2_per_objfile->n_type_units
3364 = dwarf2_per_objfile->n_allocated_type_units
3365 = elements / 3;
8d749320
SM
3366 dwarf2_per_objfile->all_type_units =
3367 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3368
673bfd45 3369 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3370
3371 for (i = 0; i < elements; i += 3)
3372 {
52dc124a 3373 struct signatured_type *sig_type;
9c541725 3374 ULONGEST signature;
1fd400ff 3375 void **slot;
9c541725 3376 cu_offset type_offset_in_tu;
1fd400ff 3377
74a0d9f6 3378 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3379 sect_offset sect_off
3380 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3381 type_offset_in_tu
3382 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3383 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3384 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3385 bytes += 3 * 8;
3386
52dc124a 3387 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3388 struct signatured_type);
52dc124a 3389 sig_type->signature = signature;
9c541725 3390 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3391 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3392 sig_type->per_cu.section = section;
9c541725 3393 sig_type->per_cu.sect_off = sect_off;
e3b94546 3394 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3395 sig_type->per_cu.v.quick
1fd400ff
TT
3396 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3397 struct dwarf2_per_cu_quick_data);
3398
52dc124a
DE
3399 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3400 *slot = sig_type;
1fd400ff 3401
b4dd5633 3402 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3403 }
3404
673bfd45 3405 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3406}
3407
927aa2e7
JK
3408/* Create the signatured type hash table from .debug_names. */
3409
3410static void
3411create_signatured_type_table_from_debug_names
ed2dc618 3412 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3413 const mapped_debug_names &map,
3414 struct dwarf2_section_info *section,
3415 struct dwarf2_section_info *abbrev_section)
3416{
ed2dc618
SM
3417 struct objfile *objfile = dwarf2_per_objfile->objfile;
3418
927aa2e7
JK
3419 dwarf2_read_section (objfile, section);
3420 dwarf2_read_section (objfile, abbrev_section);
3421
3422 dwarf2_per_objfile->n_type_units
3423 = dwarf2_per_objfile->n_allocated_type_units
3424 = map.tu_count;
3425 dwarf2_per_objfile->all_type_units
3426 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3427
3428 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3429
3430 for (uint32_t i = 0; i < map.tu_count; ++i)
3431 {
3432 struct signatured_type *sig_type;
3433 ULONGEST signature;
3434 void **slot;
3435 cu_offset type_offset_in_tu;
3436
3437 sect_offset sect_off
3438 = (sect_offset) (extract_unsigned_integer
3439 (map.tu_table_reordered + i * map.offset_size,
3440 map.offset_size,
3441 map.dwarf5_byte_order));
3442
3443 comp_unit_head cu_header;
ed2dc618
SM
3444 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3445 abbrev_section,
927aa2e7
JK
3446 section->buffer + to_underlying (sect_off),
3447 rcuh_kind::TYPE);
3448
3449 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3450 struct signatured_type);
3451 sig_type->signature = cu_header.signature;
3452 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3453 sig_type->per_cu.is_debug_types = 1;
3454 sig_type->per_cu.section = section;
3455 sig_type->per_cu.sect_off = sect_off;
e3b94546 3456 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3457 sig_type->per_cu.v.quick
3458 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3459 struct dwarf2_per_cu_quick_data);
3460
3461 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3462 *slot = sig_type;
3463
3464 dwarf2_per_objfile->all_type_units[i] = sig_type;
3465 }
3466
3467 dwarf2_per_objfile->signatured_types = sig_types_hash;
3468}
3469
9291a0cd
TT
3470/* Read the address map data from the mapped index, and use it to
3471 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3472
9291a0cd 3473static void
ed2dc618
SM
3474create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3475 struct mapped_index *index)
9291a0cd 3476{
ed2dc618 3477 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3478 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3479 const gdb_byte *iter, *end;
9291a0cd 3480 struct addrmap *mutable_map;
9291a0cd
TT
3481 CORE_ADDR baseaddr;
3482
8268c778
PA
3483 auto_obstack temp_obstack;
3484
9291a0cd
TT
3485 mutable_map = addrmap_create_mutable (&temp_obstack);
3486
f00a2de2
PA
3487 iter = index->address_table.data ();
3488 end = iter + index->address_table.size ();
9291a0cd
TT
3489
3490 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3491
3492 while (iter < end)
3493 {
3494 ULONGEST hi, lo, cu_index;
3495 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3496 iter += 8;
3497 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3498 iter += 8;
3499 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3500 iter += 4;
f652bce2 3501
24a55014 3502 if (lo > hi)
f652bce2 3503 {
24a55014
DE
3504 complaint (&symfile_complaints,
3505 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3506 hex_string (lo), hex_string (hi));
24a55014 3507 continue;
f652bce2 3508 }
24a55014
DE
3509
3510 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3511 {
3512 complaint (&symfile_complaints,
3513 _(".gdb_index address table has invalid CU number %u"),
3514 (unsigned) cu_index);
24a55014 3515 continue;
f652bce2 3516 }
24a55014 3517
3e29f34a
MR
3518 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3519 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
ed2dc618
SM
3520 addrmap_set_empty (mutable_map, lo, hi - 1,
3521 dw2_get_cutu (dwarf2_per_objfile, cu_index));
9291a0cd
TT
3522 }
3523
3524 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3525 &objfile->objfile_obstack);
9291a0cd
TT
3526}
3527
927aa2e7
JK
3528/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3529 populate the objfile's psymtabs_addrmap. */
3530
3531static void
ed2dc618 3532create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3533 struct dwarf2_section_info *section)
3534{
ed2dc618 3535 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3536 bfd *abfd = objfile->obfd;
3537 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3538 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3539 SECT_OFF_TEXT (objfile));
3540
3541 auto_obstack temp_obstack;
3542 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3543
3544 std::unordered_map<sect_offset,
3545 dwarf2_per_cu_data *,
3546 gdb::hash_enum<sect_offset>>
3547 debug_info_offset_to_per_cu;
3548 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3549 {
ed2dc618 3550 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
927aa2e7
JK
3551 const auto insertpair
3552 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3553 if (!insertpair.second)
3554 {
3555 warning (_("Section .debug_aranges in %s has duplicate "
3556 "debug_info_offset %u, ignoring .debug_aranges."),
3557 objfile_name (objfile), to_underlying (per_cu->sect_off));
3558 return;
3559 }
3560 }
3561
3562 dwarf2_read_section (objfile, section);
3563
3564 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3565
3566 const gdb_byte *addr = section->buffer;
3567
3568 while (addr < section->buffer + section->size)
3569 {
3570 const gdb_byte *const entry_addr = addr;
3571 unsigned int bytes_read;
3572
3573 const LONGEST entry_length = read_initial_length (abfd, addr,
3574 &bytes_read);
3575 addr += bytes_read;
3576
3577 const gdb_byte *const entry_end = addr + entry_length;
3578 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3579 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3580 if (addr + entry_length > section->buffer + section->size)
3581 {
3582 warning (_("Section .debug_aranges in %s entry at offset %zu "
3583 "length %s exceeds section length %s, "
3584 "ignoring .debug_aranges."),
3585 objfile_name (objfile), entry_addr - section->buffer,
3586 plongest (bytes_read + entry_length),
3587 pulongest (section->size));
3588 return;
3589 }
3590
3591 /* The version number. */
3592 const uint16_t version = read_2_bytes (abfd, addr);
3593 addr += 2;
3594 if (version != 2)
3595 {
3596 warning (_("Section .debug_aranges in %s entry at offset %zu "
3597 "has unsupported version %d, ignoring .debug_aranges."),
3598 objfile_name (objfile), entry_addr - section->buffer,
3599 version);
3600 return;
3601 }
3602
3603 const uint64_t debug_info_offset
3604 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3605 addr += offset_size;
3606 const auto per_cu_it
3607 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3608 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3609 {
3610 warning (_("Section .debug_aranges in %s entry at offset %zu "
3611 "debug_info_offset %s does not exists, "
3612 "ignoring .debug_aranges."),
3613 objfile_name (objfile), entry_addr - section->buffer,
3614 pulongest (debug_info_offset));
3615 return;
3616 }
3617 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3618
3619 const uint8_t address_size = *addr++;
3620 if (address_size < 1 || address_size > 8)
3621 {
3622 warning (_("Section .debug_aranges in %s entry at offset %zu "
3623 "address_size %u is invalid, ignoring .debug_aranges."),
3624 objfile_name (objfile), entry_addr - section->buffer,
3625 address_size);
3626 return;
3627 }
3628
3629 const uint8_t segment_selector_size = *addr++;
3630 if (segment_selector_size != 0)
3631 {
3632 warning (_("Section .debug_aranges in %s entry at offset %zu "
3633 "segment_selector_size %u is not supported, "
3634 "ignoring .debug_aranges."),
3635 objfile_name (objfile), entry_addr - section->buffer,
3636 segment_selector_size);
3637 return;
3638 }
3639
3640 /* Must pad to an alignment boundary that is twice the address
3641 size. It is undocumented by the DWARF standard but GCC does
3642 use it. */
3643 for (size_t padding = ((-(addr - section->buffer))
3644 & (2 * address_size - 1));
3645 padding > 0; padding--)
3646 if (*addr++ != 0)
3647 {
3648 warning (_("Section .debug_aranges in %s entry at offset %zu "
3649 "padding is not zero, ignoring .debug_aranges."),
3650 objfile_name (objfile), entry_addr - section->buffer);
3651 return;
3652 }
3653
3654 for (;;)
3655 {
3656 if (addr + 2 * address_size > entry_end)
3657 {
3658 warning (_("Section .debug_aranges in %s entry at offset %zu "
3659 "address list is not properly terminated, "
3660 "ignoring .debug_aranges."),
3661 objfile_name (objfile), entry_addr - section->buffer);
3662 return;
3663 }
3664 ULONGEST start = extract_unsigned_integer (addr, address_size,
3665 dwarf5_byte_order);
3666 addr += address_size;
3667 ULONGEST length = extract_unsigned_integer (addr, address_size,
3668 dwarf5_byte_order);
3669 addr += address_size;
3670 if (start == 0 && length == 0)
3671 break;
3672 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3673 {
3674 /* Symbol was eliminated due to a COMDAT group. */
3675 continue;
3676 }
3677 ULONGEST end = start + length;
3678 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3679 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3680 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3681 }
3682 }
3683
3684 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3685 &objfile->objfile_obstack);
3686}
3687
59d7bcaf
JK
3688/* The hash function for strings in the mapped index. This is the same as
3689 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3690 implementation. This is necessary because the hash function is tied to the
3691 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3692 SYMBOL_HASH_NEXT.
3693
3694 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3695
9291a0cd 3696static hashval_t
559a7a62 3697mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3698{
3699 const unsigned char *str = (const unsigned char *) p;
3700 hashval_t r = 0;
3701 unsigned char c;
3702
3703 while ((c = *str++) != 0)
559a7a62
JK
3704 {
3705 if (index_version >= 5)
3706 c = tolower (c);
3707 r = r * 67 + c - 113;
3708 }
9291a0cd
TT
3709
3710 return r;
3711}
3712
3713/* Find a slot in the mapped index INDEX for the object named NAME.
3714 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3715 constant pool and return true. If NAME cannot be found, return
3716 false. */
2fdf6df6 3717
109483d9 3718static bool
9291a0cd
TT
3719find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3720 offset_type **vec_out)
3721{
0cf03b49 3722 offset_type hash;
9291a0cd 3723 offset_type slot, step;
559a7a62 3724 int (*cmp) (const char *, const char *);
9291a0cd 3725
791afaa2 3726 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3727 if (current_language->la_language == language_cplus
45280282
IB
3728 || current_language->la_language == language_fortran
3729 || current_language->la_language == language_d)
0cf03b49
JK
3730 {
3731 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3732 not contain any. */
a8719064 3733
72998fb3 3734 if (strchr (name, '(') != NULL)
0cf03b49 3735 {
109483d9 3736 without_params = cp_remove_params (name);
0cf03b49 3737
72998fb3 3738 if (without_params != NULL)
791afaa2 3739 name = without_params.get ();
0cf03b49
JK
3740 }
3741 }
3742
559a7a62 3743 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3744 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3745 simulate our NAME being searched is also lowercased. */
3746 hash = mapped_index_string_hash ((index->version == 4
3747 && case_sensitivity == case_sensitive_off
3748 ? 5 : index->version),
3749 name);
3750
f00a2de2
PA
3751 slot = hash & (index->symbol_table.size () - 1);
3752 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3753 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3754
3755 for (;;)
3756 {
9291a0cd 3757 const char *str;
f00a2de2
PA
3758
3759 const auto &bucket = index->symbol_table[slot];
3760 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3761 return false;
9291a0cd 3762
f00a2de2 3763 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3764 if (!cmp (name, str))
9291a0cd
TT
3765 {
3766 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3767 + MAYBE_SWAP (bucket.vec));
109483d9 3768 return true;
9291a0cd
TT
3769 }
3770
f00a2de2 3771 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3772 }
3773}
3774
2ec9a5e0
TT
3775/* A helper function that reads the .gdb_index from SECTION and fills
3776 in MAP. FILENAME is the name of the file containing the section;
3777 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3778 ok to use deprecated sections.
3779
3780 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3781 out parameters that are filled in with information about the CU and
3782 TU lists in the section.
3783
3784 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3785
9291a0cd 3786static int
2ec9a5e0
TT
3787read_index_from_section (struct objfile *objfile,
3788 const char *filename,
3789 int deprecated_ok,
3790 struct dwarf2_section_info *section,
3791 struct mapped_index *map,
3792 const gdb_byte **cu_list,
3793 offset_type *cu_list_elements,
3794 const gdb_byte **types_list,
3795 offset_type *types_list_elements)
9291a0cd 3796{
948f8e3d 3797 const gdb_byte *addr;
2ec9a5e0 3798 offset_type version;
b3b272e1 3799 offset_type *metadata;
1fd400ff 3800 int i;
9291a0cd 3801
2ec9a5e0 3802 if (dwarf2_section_empty_p (section))
9291a0cd 3803 return 0;
82430852
JK
3804
3805 /* Older elfutils strip versions could keep the section in the main
3806 executable while splitting it for the separate debug info file. */
a32a8923 3807 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3808 return 0;
3809
2ec9a5e0 3810 dwarf2_read_section (objfile, section);
9291a0cd 3811
2ec9a5e0 3812 addr = section->buffer;
9291a0cd 3813 /* Version check. */
1fd400ff 3814 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3815 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3816 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3817 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3818 indices. */
831adc1f 3819 if (version < 4)
481860b3
GB
3820 {
3821 static int warning_printed = 0;
3822 if (!warning_printed)
3823 {
3824 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3825 filename);
481860b3
GB
3826 warning_printed = 1;
3827 }
3828 return 0;
3829 }
3830 /* Index version 4 uses a different hash function than index version
3831 5 and later.
3832
3833 Versions earlier than 6 did not emit psymbols for inlined
3834 functions. Using these files will cause GDB not to be able to
3835 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3836 indices unless the user has done
3837 "set use-deprecated-index-sections on". */
2ec9a5e0 3838 if (version < 6 && !deprecated_ok)
481860b3
GB
3839 {
3840 static int warning_printed = 0;
3841 if (!warning_printed)
3842 {
e615022a
DE
3843 warning (_("\
3844Skipping deprecated .gdb_index section in %s.\n\
3845Do \"set use-deprecated-index-sections on\" before the file is read\n\
3846to use the section anyway."),
2ec9a5e0 3847 filename);
481860b3
GB
3848 warning_printed = 1;
3849 }
3850 return 0;
3851 }
796a7ff8 3852 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3853 of the TU (for symbols coming from TUs),
3854 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3855 Plus gold-generated indices can have duplicate entries for global symbols,
3856 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3857 These are just performance bugs, and we can't distinguish gdb-generated
3858 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3859
481860b3 3860 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3861 longer backward compatible. */
796a7ff8 3862 if (version > 8)
594e8718 3863 return 0;
9291a0cd 3864
559a7a62 3865 map->version = version;
2ec9a5e0 3866 map->total_size = section->size;
9291a0cd
TT
3867
3868 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3869
3870 i = 0;
2ec9a5e0
TT
3871 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3872 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3873 / 8);
1fd400ff
TT
3874 ++i;
3875
2ec9a5e0
TT
3876 *types_list = addr + MAYBE_SWAP (metadata[i]);
3877 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3878 - MAYBE_SWAP (metadata[i]))
3879 / 8);
987d643c 3880 ++i;
1fd400ff 3881
f00a2de2
PA
3882 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3883 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3884 map->address_table
3885 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3886 ++i;
3887
f00a2de2
PA
3888 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3889 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3890 map->symbol_table
3891 = gdb::array_view<mapped_index::symbol_table_slot>
3892 ((mapped_index::symbol_table_slot *) symbol_table,
3893 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3894
f00a2de2 3895 ++i;
f9d83a0b 3896 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3897
2ec9a5e0
TT
3898 return 1;
3899}
3900
927aa2e7 3901/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3902 elements of all the CUs and return 1. Otherwise, return 0. */
3903
3904static int
3905dwarf2_read_index (struct objfile *objfile)
3906{
3907 struct mapped_index local_map, *map;
3908 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3909 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3910 struct dwz_file *dwz;
ed2dc618
SM
3911 struct dwarf2_per_objfile *dwarf2_per_objfile
3912 = get_dwarf2_per_objfile (objfile);
2ec9a5e0 3913
4262abfb 3914 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3915 use_deprecated_index_sections,
3916 &dwarf2_per_objfile->gdb_index, &local_map,
3917 &cu_list, &cu_list_elements,
3918 &types_list, &types_list_elements))
3919 return 0;
3920
0fefef59 3921 /* Don't use the index if it's empty. */
f00a2de2 3922 if (local_map.symbol_table.empty ())
0fefef59
DE
3923 return 0;
3924
2ec9a5e0
TT
3925 /* If there is a .dwz file, read it so we can get its CU list as
3926 well. */
ed2dc618 3927 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3928 if (dwz != NULL)
2ec9a5e0 3929 {
2ec9a5e0
TT
3930 struct mapped_index dwz_map;
3931 const gdb_byte *dwz_types_ignore;
3932 offset_type dwz_types_elements_ignore;
3933
3934 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3935 1,
3936 &dwz->gdb_index, &dwz_map,
3937 &dwz_list, &dwz_list_elements,
3938 &dwz_types_ignore,
3939 &dwz_types_elements_ignore))
3940 {
3941 warning (_("could not read '.gdb_index' section from %s; skipping"),
3942 bfd_get_filename (dwz->dwz_bfd));
3943 return 0;
3944 }
3945 }
3946
74a0d9f6
JK
3947 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3948 dwz_list_elements);
1fd400ff 3949
8b70b953
TT
3950 if (types_list_elements)
3951 {
3952 struct dwarf2_section_info *section;
3953
3954 /* We can only handle a single .debug_types when we have an
3955 index. */
3956 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3957 return 0;
3958
3959 section = VEC_index (dwarf2_section_info_def,
3960 dwarf2_per_objfile->types, 0);
3961
74a0d9f6
JK
3962 create_signatured_type_table_from_index (objfile, section, types_list,
3963 types_list_elements);
8b70b953 3964 }
9291a0cd 3965
ed2dc618 3966 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
2ec9a5e0 3967
8d749320 3968 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3f563c84 3969 map = new (map) mapped_index ();
2ec9a5e0 3970 *map = local_map;
9291a0cd
TT
3971
3972 dwarf2_per_objfile->index_table = map;
3973 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3974 dwarf2_per_objfile->quick_file_names_table =
3975 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3976
3977 return 1;
3978}
3979
dee91e82 3980/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3981
dee91e82
DE
3982static void
3983dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3984 const gdb_byte *info_ptr,
dee91e82
DE
3985 struct die_info *comp_unit_die,
3986 int has_children,
3987 void *data)
9291a0cd 3988{
dee91e82 3989 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3990 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3991 struct dwarf2_per_objfile *dwarf2_per_objfile
3992 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3993 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3994 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3995 struct attribute *attr;
dee91e82 3996 int i;
7b9f3c50
DE
3997 void **slot;
3998 struct quick_file_names *qfn;
9291a0cd 3999
0186c6a7
DE
4000 gdb_assert (! this_cu->is_debug_types);
4001
07261596
TT
4002 /* Our callers never want to match partial units -- instead they
4003 will match the enclosing full CU. */
4004 if (comp_unit_die->tag == DW_TAG_partial_unit)
4005 {
4006 this_cu->v.quick->no_file_data = 1;
4007 return;
4008 }
4009
0186c6a7 4010 lh_cu = this_cu;
7b9f3c50 4011 slot = NULL;
dee91e82 4012
fff8551c 4013 line_header_up lh;
9c541725 4014 sect_offset line_offset {};
fff8551c 4015
dee91e82 4016 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
4017 if (attr)
4018 {
7b9f3c50
DE
4019 struct quick_file_names find_entry;
4020
9c541725 4021 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
4022
4023 /* We may have already read in this line header (TU line header sharing).
4024 If we have we're done. */
094b34ac 4025 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 4026 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
4027 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
4028 &find_entry, INSERT);
4029 if (*slot != NULL)
4030 {
9a3c8263 4031 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 4032 return;
7b9f3c50
DE
4033 }
4034
3019eac3 4035 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
4036 }
4037 if (lh == NULL)
4038 {
094b34ac 4039 lh_cu->v.quick->no_file_data = 1;
dee91e82 4040 return;
9291a0cd
TT
4041 }
4042
8d749320 4043 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 4044 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 4045 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
4046 gdb_assert (slot != NULL);
4047 *slot = qfn;
9291a0cd 4048
d721ba37 4049 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 4050
fff8551c 4051 qfn->num_file_names = lh->file_names.size ();
8d749320 4052 qfn->file_names =
fff8551c
PA
4053 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
4054 for (i = 0; i < lh->file_names.size (); ++i)
4055 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 4056 qfn->real_names = NULL;
9291a0cd 4057
094b34ac 4058 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
4059}
4060
4061/* A helper for the "quick" functions which attempts to read the line
4062 table for THIS_CU. */
4063
4064static struct quick_file_names *
e4a48d9d 4065dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 4066{
0186c6a7
DE
4067 /* This should never be called for TUs. */
4068 gdb_assert (! this_cu->is_debug_types);
4069 /* Nor type unit groups. */
4070 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 4071
dee91e82
DE
4072 if (this_cu->v.quick->file_names != NULL)
4073 return this_cu->v.quick->file_names;
4074 /* If we know there is no line data, no point in looking again. */
4075 if (this_cu->v.quick->no_file_data)
4076 return NULL;
4077
0186c6a7 4078 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
4079
4080 if (this_cu->v.quick->no_file_data)
4081 return NULL;
4082 return this_cu->v.quick->file_names;
9291a0cd
TT
4083}
4084
4085/* A helper for the "quick" functions which computes and caches the
7b9f3c50 4086 real path for a given file name from the line table. */
2fdf6df6 4087
9291a0cd 4088static const char *
7b9f3c50
DE
4089dw2_get_real_path (struct objfile *objfile,
4090 struct quick_file_names *qfn, int index)
9291a0cd 4091{
7b9f3c50
DE
4092 if (qfn->real_names == NULL)
4093 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 4094 qfn->num_file_names, const char *);
9291a0cd 4095
7b9f3c50 4096 if (qfn->real_names[index] == NULL)
14278e1f 4097 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 4098
7b9f3c50 4099 return qfn->real_names[index];
9291a0cd
TT
4100}
4101
4102static struct symtab *
4103dw2_find_last_source_symtab (struct objfile *objfile)
4104{
ed2dc618
SM
4105 struct dwarf2_per_objfile *dwarf2_per_objfile
4106 = get_dwarf2_per_objfile (objfile);
4107 int index = dwarf2_per_objfile->n_comp_units - 1;
4108 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
4109 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
ae2de4f8 4110
43f3e411
DE
4111 if (cust == NULL)
4112 return NULL;
ed2dc618 4113
43f3e411 4114 return compunit_primary_filetab (cust);
9291a0cd
TT
4115}
4116
7b9f3c50
DE
4117/* Traversal function for dw2_forget_cached_source_info. */
4118
4119static int
4120dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 4121{
7b9f3c50 4122 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 4123
7b9f3c50 4124 if (file_data->real_names)
9291a0cd 4125 {
7b9f3c50 4126 int i;
9291a0cd 4127
7b9f3c50 4128 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 4129 {
7b9f3c50
DE
4130 xfree ((void*) file_data->real_names[i]);
4131 file_data->real_names[i] = NULL;
9291a0cd
TT
4132 }
4133 }
7b9f3c50
DE
4134
4135 return 1;
4136}
4137
4138static void
4139dw2_forget_cached_source_info (struct objfile *objfile)
4140{
ed2dc618
SM
4141 struct dwarf2_per_objfile *dwarf2_per_objfile
4142 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
4143
4144 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4145 dw2_free_cached_file_names, NULL);
9291a0cd
TT
4146}
4147
f8eba3c6
TT
4148/* Helper function for dw2_map_symtabs_matching_filename that expands
4149 the symtabs and calls the iterator. */
4150
4151static int
4152dw2_map_expand_apply (struct objfile *objfile,
4153 struct dwarf2_per_cu_data *per_cu,
f5b95b50 4154 const char *name, const char *real_path,
14bc53a8 4155 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 4156{
43f3e411 4157 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
4158
4159 /* Don't visit already-expanded CUs. */
43f3e411 4160 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
4161 return 0;
4162
4163 /* This may expand more than one symtab, and we want to iterate over
4164 all of them. */
a0f42c21 4165 dw2_instantiate_symtab (per_cu);
f8eba3c6 4166
14bc53a8
PA
4167 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4168 last_made, callback);
f8eba3c6
TT
4169}
4170
4171/* Implementation of the map_symtabs_matching_filename method. */
4172
14bc53a8
PA
4173static bool
4174dw2_map_symtabs_matching_filename
4175 (struct objfile *objfile, const char *name, const char *real_path,
4176 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
4177{
4178 int i;
c011a4f4 4179 const char *name_basename = lbasename (name);
ed2dc618
SM
4180 struct dwarf2_per_objfile *dwarf2_per_objfile
4181 = get_dwarf2_per_objfile (objfile);
ae2de4f8 4182
848e3e78
DE
4183 /* The rule is CUs specify all the files, including those used by
4184 any TU, so there's no need to scan TUs here. */
f4dc4d17 4185
ed2dc618 4186 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4187 {
4188 int j;
ed2dc618 4189 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
7b9f3c50 4190 struct quick_file_names *file_data;
9291a0cd 4191
3d7bb9d9 4192 /* We only need to look at symtabs not already expanded. */
43f3e411 4193 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4194 continue;
4195
e4a48d9d 4196 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4197 if (file_data == NULL)
9291a0cd
TT
4198 continue;
4199
7b9f3c50 4200 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4201 {
7b9f3c50 4202 const char *this_name = file_data->file_names[j];
da235a7c 4203 const char *this_real_name;
9291a0cd 4204
af529f8f 4205 if (compare_filenames_for_search (this_name, name))
9291a0cd 4206 {
f5b95b50 4207 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4208 callback))
4209 return true;
288e77a7 4210 continue;
4aac40c8 4211 }
9291a0cd 4212
c011a4f4
DE
4213 /* Before we invoke realpath, which can get expensive when many
4214 files are involved, do a quick comparison of the basenames. */
4215 if (! basenames_may_differ
4216 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4217 continue;
4218
da235a7c
JK
4219 this_real_name = dw2_get_real_path (objfile, file_data, j);
4220 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 4221 {
da235a7c 4222 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4223 callback))
4224 return true;
288e77a7 4225 continue;
da235a7c 4226 }
9291a0cd 4227
da235a7c
JK
4228 if (real_path != NULL)
4229 {
af529f8f
JK
4230 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4231 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 4232 if (this_real_name != NULL
af529f8f 4233 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 4234 {
f5b95b50 4235 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4236 callback))
4237 return true;
288e77a7 4238 continue;
9291a0cd
TT
4239 }
4240 }
4241 }
4242 }
4243
14bc53a8 4244 return false;
9291a0cd
TT
4245}
4246
da51c347
DE
4247/* Struct used to manage iterating over all CUs looking for a symbol. */
4248
4249struct dw2_symtab_iterator
9291a0cd 4250{
ed2dc618
SM
4251 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
4252 struct dwarf2_per_objfile *dwarf2_per_objfile;
da51c347
DE
4253 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4254 int want_specific_block;
4255 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4256 Unused if !WANT_SPECIFIC_BLOCK. */
4257 int block_index;
4258 /* The kind of symbol we're looking for. */
4259 domain_enum domain;
4260 /* The list of CUs from the index entry of the symbol,
4261 or NULL if not found. */
4262 offset_type *vec;
4263 /* The next element in VEC to look at. */
4264 int next;
4265 /* The number of elements in VEC, or zero if there is no match. */
4266 int length;
8943b874
DE
4267 /* Have we seen a global version of the symbol?
4268 If so we can ignore all further global instances.
4269 This is to work around gold/15646, inefficient gold-generated
4270 indices. */
4271 int global_seen;
da51c347 4272};
9291a0cd 4273
da51c347
DE
4274/* Initialize the index symtab iterator ITER.
4275 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4276 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 4277
9291a0cd 4278static void
da51c347 4279dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 4280 struct dwarf2_per_objfile *dwarf2_per_objfile,
da51c347
DE
4281 int want_specific_block,
4282 int block_index,
4283 domain_enum domain,
4284 const char *name)
4285{
ed2dc618 4286 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
4287 iter->want_specific_block = want_specific_block;
4288 iter->block_index = block_index;
4289 iter->domain = domain;
4290 iter->next = 0;
8943b874 4291 iter->global_seen = 0;
da51c347 4292
ed2dc618
SM
4293 mapped_index *index = dwarf2_per_objfile->index_table;
4294
4295 /* index is NULL if OBJF_READNOW. */
4296 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
4297 iter->length = MAYBE_SWAP (*iter->vec);
4298 else
4299 {
4300 iter->vec = NULL;
4301 iter->length = 0;
4302 }
4303}
4304
4305/* Return the next matching CU or NULL if there are no more. */
4306
4307static struct dwarf2_per_cu_data *
4308dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4309{
ed2dc618
SM
4310 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4311
da51c347
DE
4312 for ( ; iter->next < iter->length; ++iter->next)
4313 {
4314 offset_type cu_index_and_attrs =
4315 MAYBE_SWAP (iter->vec[iter->next + 1]);
4316 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 4317 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
4318 int want_static = iter->block_index != GLOBAL_BLOCK;
4319 /* This value is only valid for index versions >= 7. */
4320 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4321 gdb_index_symbol_kind symbol_kind =
4322 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4323 /* Only check the symbol attributes if they're present.
4324 Indices prior to version 7 don't record them,
4325 and indices >= 7 may elide them for certain symbols
4326 (gold does this). */
4327 int attrs_valid =
ed2dc618 4328 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
4329 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4330
3190f0c6
DE
4331 /* Don't crash on bad data. */
4332 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4333 + dwarf2_per_objfile->n_type_units))
4334 {
4335 complaint (&symfile_complaints,
4336 _(".gdb_index entry has bad CU index"
4262abfb
JK
4337 " [in module %s]"),
4338 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
4339 continue;
4340 }
4341
ed2dc618 4342 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
3190f0c6 4343
da51c347 4344 /* Skip if already read in. */
43f3e411 4345 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
4346 continue;
4347
8943b874
DE
4348 /* Check static vs global. */
4349 if (attrs_valid)
4350 {
4351 if (iter->want_specific_block
4352 && want_static != is_static)
4353 continue;
4354 /* Work around gold/15646. */
4355 if (!is_static && iter->global_seen)
4356 continue;
4357 if (!is_static)
4358 iter->global_seen = 1;
4359 }
da51c347
DE
4360
4361 /* Only check the symbol's kind if it has one. */
4362 if (attrs_valid)
4363 {
4364 switch (iter->domain)
4365 {
4366 case VAR_DOMAIN:
4367 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4368 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4369 /* Some types are also in VAR_DOMAIN. */
4370 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4371 continue;
4372 break;
4373 case STRUCT_DOMAIN:
4374 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4375 continue;
4376 break;
4377 case LABEL_DOMAIN:
4378 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4379 continue;
4380 break;
4381 default:
4382 break;
4383 }
4384 }
4385
4386 ++iter->next;
4387 return per_cu;
4388 }
4389
4390 return NULL;
4391}
4392
43f3e411 4393static struct compunit_symtab *
da51c347
DE
4394dw2_lookup_symbol (struct objfile *objfile, int block_index,
4395 const char *name, domain_enum domain)
9291a0cd 4396{
43f3e411 4397 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4398 struct dwarf2_per_objfile *dwarf2_per_objfile
4399 = get_dwarf2_per_objfile (objfile);
9291a0cd 4400
b5ec771e
PA
4401 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4402
ed2dc618
SM
4403 struct dw2_symtab_iterator iter;
4404 struct dwarf2_per_cu_data *per_cu;
da51c347 4405
ed2dc618 4406 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
9291a0cd 4407
ed2dc618
SM
4408 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4409 {
4410 struct symbol *sym, *with_opaque = NULL;
4411 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4412 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4413 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4414
ed2dc618
SM
4415 sym = block_find_symbol (block, name, domain,
4416 block_find_non_opaque_type_preferred,
4417 &with_opaque);
b2e2f908 4418
ed2dc618
SM
4419 /* Some caution must be observed with overloaded functions
4420 and methods, since the index will not contain any overload
4421 information (but NAME might contain it). */
da51c347 4422
ed2dc618
SM
4423 if (sym != NULL
4424 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4425 return stab;
4426 if (with_opaque != NULL
4427 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4428 stab_best = stab;
da51c347 4429
ed2dc618 4430 /* Keep looking through other CUs. */
9291a0cd 4431 }
9291a0cd 4432
da51c347 4433 return stab_best;
9291a0cd
TT
4434}
4435
4436static void
4437dw2_print_stats (struct objfile *objfile)
4438{
ed2dc618
SM
4439 struct dwarf2_per_objfile *dwarf2_per_objfile
4440 = get_dwarf2_per_objfile (objfile);
4441 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4442 int count = 0;
9291a0cd 4443
ed2dc618 4444 for (int i = 0; i < total; ++i)
9291a0cd 4445 {
ed2dc618 4446 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 4447
43f3e411 4448 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4449 ++count;
4450 }
e4a48d9d 4451 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4452 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4453}
4454
779bd270
DE
4455/* This dumps minimal information about the index.
4456 It is called via "mt print objfiles".
4457 One use is to verify .gdb_index has been loaded by the
4458 gdb.dwarf2/gdb-index.exp testcase. */
4459
9291a0cd
TT
4460static void
4461dw2_dump (struct objfile *objfile)
4462{
ed2dc618
SM
4463 struct dwarf2_per_objfile *dwarf2_per_objfile
4464 = get_dwarf2_per_objfile (objfile);
4465
779bd270
DE
4466 gdb_assert (dwarf2_per_objfile->using_index);
4467 printf_filtered (".gdb_index:");
4468 if (dwarf2_per_objfile->index_table != NULL)
4469 {
4470 printf_filtered (" version %d\n",
4471 dwarf2_per_objfile->index_table->version);
4472 }
4473 else
4474 printf_filtered (" faked for \"readnow\"\n");
4475 printf_filtered ("\n");
9291a0cd
TT
4476}
4477
4478static void
3189cb12
DE
4479dw2_relocate (struct objfile *objfile,
4480 const struct section_offsets *new_offsets,
4481 const struct section_offsets *delta)
9291a0cd
TT
4482{
4483 /* There's nothing to relocate here. */
4484}
4485
4486static void
4487dw2_expand_symtabs_for_function (struct objfile *objfile,
4488 const char *func_name)
4489{
ed2dc618
SM
4490 struct dwarf2_per_objfile *dwarf2_per_objfile
4491 = get_dwarf2_per_objfile (objfile);
da51c347 4492
ed2dc618
SM
4493 struct dw2_symtab_iterator iter;
4494 struct dwarf2_per_cu_data *per_cu;
da51c347 4495
ed2dc618
SM
4496 /* Note: It doesn't matter what we pass for block_index here. */
4497 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4498 func_name);
da51c347 4499
ed2dc618
SM
4500 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4501 dw2_instantiate_symtab (per_cu);
da51c347 4502
9291a0cd
TT
4503}
4504
4505static void
4506dw2_expand_all_symtabs (struct objfile *objfile)
4507{
ed2dc618
SM
4508 struct dwarf2_per_objfile *dwarf2_per_objfile
4509 = get_dwarf2_per_objfile (objfile);
4510 int total_units = (dwarf2_per_objfile->n_comp_units
4511 + dwarf2_per_objfile->n_type_units);
9291a0cd 4512
ed2dc618 4513 for (int i = 0; i < total_units; ++i)
9291a0cd 4514 {
ed2dc618
SM
4515 struct dwarf2_per_cu_data *per_cu
4516 = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 4517
a0f42c21 4518 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4519 }
4520}
4521
4522static void
652a8996
JK
4523dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4524 const char *fullname)
9291a0cd 4525{
ed2dc618
SM
4526 struct dwarf2_per_objfile *dwarf2_per_objfile
4527 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4528
4529 /* We don't need to consider type units here.
4530 This is only called for examining code, e.g. expand_line_sal.
4531 There can be an order of magnitude (or more) more type units
4532 than comp units, and we avoid them if we can. */
4533
ed2dc618 4534 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4535 {
4536 int j;
ed2dc618 4537 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
7b9f3c50 4538 struct quick_file_names *file_data;
9291a0cd 4539
3d7bb9d9 4540 /* We only need to look at symtabs not already expanded. */
43f3e411 4541 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4542 continue;
4543
e4a48d9d 4544 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4545 if (file_data == NULL)
9291a0cd
TT
4546 continue;
4547
7b9f3c50 4548 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4549 {
652a8996
JK
4550 const char *this_fullname = file_data->file_names[j];
4551
4552 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4553 {
a0f42c21 4554 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4555 break;
4556 }
4557 }
4558 }
4559}
4560
9291a0cd 4561static void
ade7ed9e 4562dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4563 const char * name, domain_enum domain,
ade7ed9e 4564 int global,
40658b94
PH
4565 int (*callback) (struct block *,
4566 struct symbol *, void *),
b5ec771e 4567 void *data, symbol_name_match_type match,
2edb89d3 4568 symbol_compare_ftype *ordered_compare)
9291a0cd 4569{
40658b94 4570 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4571 current language is Ada for a non-Ada objfile using GNU index. As Ada
4572 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4573}
4574
b5ec771e
PA
4575/* Symbol name matcher for .gdb_index names.
4576
4577 Symbol names in .gdb_index have a few particularities:
4578
4579 - There's no indication of which is the language of each symbol.
4580
4581 Since each language has its own symbol name matching algorithm,
4582 and we don't know which language is the right one, we must match
3f563c84
PA
4583 each symbol against all languages. This would be a potential
4584 performance problem if it were not mitigated by the
4585 mapped_index::name_components lookup table, which significantly
4586 reduces the number of times we need to call into this matcher,
4587 making it a non-issue.
b5ec771e
PA
4588
4589 - Symbol names in the index have no overload (parameter)
4590 information. I.e., in C++, "foo(int)" and "foo(long)" both
4591 appear as "foo" in the index, for example.
4592
4593 This means that the lookup names passed to the symbol name
4594 matcher functions must have no parameter information either
4595 because (e.g.) symbol search name "foo" does not match
4596 lookup-name "foo(int)" [while swapping search name for lookup
4597 name would match].
4598*/
4599class gdb_index_symbol_name_matcher
4600{
4601public:
4602 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4603 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4604
4605 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4606 Returns true if any matcher matches. */
4607 bool matches (const char *symbol_name);
4608
4609private:
4610 /* A reference to the lookup name we're matching against. */
4611 const lookup_name_info &m_lookup_name;
4612
4613 /* A vector holding all the different symbol name matchers, for all
4614 languages. */
4615 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4616};
4617
4618gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4619 (const lookup_name_info &lookup_name)
4620 : m_lookup_name (lookup_name)
4621{
4622 /* Prepare the vector of comparison functions upfront, to avoid
4623 doing the same work for each symbol. Care is taken to avoid
4624 matching with the same matcher more than once if/when multiple
4625 languages use the same matcher function. */
4626 auto &matchers = m_symbol_name_matcher_funcs;
4627 matchers.reserve (nr_languages);
4628
4629 matchers.push_back (default_symbol_name_matcher);
4630
4631 for (int i = 0; i < nr_languages; i++)
4632 {
4633 const language_defn *lang = language_def ((enum language) i);
c63d3e8d 4634 symbol_name_matcher_ftype *name_matcher
618daa93 4635 = get_symbol_name_matcher (lang, m_lookup_name);
c63d3e8d
PA
4636
4637 /* Don't insert the same comparison routine more than once.
4638 Note that we do this linear walk instead of a seemingly
4639 cheaper sorted insert, or use a std::set or something like
4640 that, because relative order of function addresses is not
4641 stable. This is not a problem in practice because the number
4642 of supported languages is low, and the cost here is tiny
4643 compared to the number of searches we'll do afterwards using
4644 this object. */
4645 if (name_matcher != default_symbol_name_matcher
4646 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4647 == matchers.end ()))
4648 matchers.push_back (name_matcher);
b5ec771e
PA
4649 }
4650}
4651
4652bool
4653gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4654{
4655 for (auto matches_name : m_symbol_name_matcher_funcs)
4656 if (matches_name (symbol_name, m_lookup_name, NULL))
4657 return true;
4658
4659 return false;
4660}
4661
e1ef7d7a
PA
4662/* Starting from a search name, return the string that finds the upper
4663 bound of all strings that start with SEARCH_NAME in a sorted name
4664 list. Returns the empty string to indicate that the upper bound is
4665 the end of the list. */
4666
4667static std::string
4668make_sort_after_prefix_name (const char *search_name)
4669{
4670 /* When looking to complete "func", we find the upper bound of all
4671 symbols that start with "func" by looking for where we'd insert
4672 the closest string that would follow "func" in lexicographical
4673 order. Usually, that's "func"-with-last-character-incremented,
4674 i.e. "fund". Mind non-ASCII characters, though. Usually those
4675 will be UTF-8 multi-byte sequences, but we can't be certain.
4676 Especially mind the 0xff character, which is a valid character in
4677 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4678 rule out compilers allowing it in identifiers. Note that
4679 conveniently, strcmp/strcasecmp are specified to compare
4680 characters interpreted as unsigned char. So what we do is treat
4681 the whole string as a base 256 number composed of a sequence of
4682 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4683 to 0, and carries 1 to the following more-significant position.
4684 If the very first character in SEARCH_NAME ends up incremented
4685 and carries/overflows, then the upper bound is the end of the
4686 list. The string after the empty string is also the empty
4687 string.
4688
4689 Some examples of this operation:
4690
4691 SEARCH_NAME => "+1" RESULT
4692
4693 "abc" => "abd"
4694 "ab\xff" => "ac"
4695 "\xff" "a" "\xff" => "\xff" "b"
4696 "\xff" => ""
4697 "\xff\xff" => ""
4698 "" => ""
4699
4700 Then, with these symbols for example:
4701
4702 func
4703 func1
4704 fund
4705
4706 completing "func" looks for symbols between "func" and
4707 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4708 which finds "func" and "func1", but not "fund".
4709
4710 And with:
4711
4712 funcÿ (Latin1 'ÿ' [0xff])
4713 funcÿ1
4714 fund
4715
4716 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4717 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4718
4719 And with:
4720
4721 ÿÿ (Latin1 'ÿ' [0xff])
4722 ÿÿ1
4723
4724 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4725 the end of the list.
4726 */
4727 std::string after = search_name;
4728 while (!after.empty () && (unsigned char) after.back () == 0xff)
4729 after.pop_back ();
4730 if (!after.empty ())
4731 after.back () = (unsigned char) after.back () + 1;
4732 return after;
4733}
4734
5c58de74 4735/* See declaration. */
61d96d7e 4736
5c58de74
PA
4737std::pair<std::vector<name_component>::const_iterator,
4738 std::vector<name_component>::const_iterator>
44ed8f3e 4739mapped_index_base::find_name_components_bounds
5c58de74 4740 (const lookup_name_info &lookup_name_without_params) const
3f563c84 4741{
5c58de74
PA
4742 auto *name_cmp
4743 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84
PA
4744
4745 const char *cplus
c62446b1 4746 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4747
3f563c84
PA
4748 /* Comparison function object for lower_bound that matches against a
4749 given symbol name. */
4750 auto lookup_compare_lower = [&] (const name_component &elem,
4751 const char *name)
4752 {
5c58de74 4753 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4754 const char *elem_name = elem_qualified + elem.name_offset;
4755 return name_cmp (elem_name, name) < 0;
4756 };
4757
4758 /* Comparison function object for upper_bound that matches against a
4759 given symbol name. */
4760 auto lookup_compare_upper = [&] (const char *name,
4761 const name_component &elem)
4762 {
5c58de74 4763 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4764 const char *elem_name = elem_qualified + elem.name_offset;
4765 return name_cmp (name, elem_name) < 0;
4766 };
4767
5c58de74
PA
4768 auto begin = this->name_components.begin ();
4769 auto end = this->name_components.end ();
3f563c84
PA
4770
4771 /* Find the lower bound. */
4772 auto lower = [&] ()
4773 {
5c58de74 4774 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4775 return begin;
4776 else
4777 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4778 } ();
4779
4780 /* Find the upper bound. */
4781 auto upper = [&] ()
4782 {
5c58de74 4783 if (lookup_name_without_params.completion_mode ())
3f563c84 4784 {
e1ef7d7a
PA
4785 /* In completion mode, we want UPPER to point past all
4786 symbols names that have the same prefix. I.e., with
4787 these symbols, and completing "func":
4788
4789 function << lower bound
4790 function1
4791 other_function << upper bound
4792
4793 We find the upper bound by looking for the insertion
4794 point of "func"-with-last-character-incremented,
4795 i.e. "fund". */
4796 std::string after = make_sort_after_prefix_name (cplus);
4797 if (after.empty ())
3f563c84 4798 return end;
e6b2f5ef
PA
4799 return std::lower_bound (lower, end, after.c_str (),
4800 lookup_compare_lower);
3f563c84
PA
4801 }
4802 else
4803 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4804 } ();
4805
5c58de74
PA
4806 return {lower, upper};
4807}
4808
4809/* See declaration. */
4810
4811void
44ed8f3e 4812mapped_index_base::build_name_components ()
5c58de74
PA
4813{
4814 if (!this->name_components.empty ())
4815 return;
4816
4817 this->name_components_casing = case_sensitivity;
4818 auto *name_cmp
4819 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4820
4821 /* The code below only knows how to break apart components of C++
4822 symbol names (and other languages that use '::' as
4823 namespace/module separator). If we add support for wild matching
4824 to some language that uses some other operator (E.g., Ada, Go and
4825 D use '.'), then we'll need to try splitting the symbol name
4826 according to that language too. Note that Ada does support wild
4827 matching, but doesn't currently support .gdb_index. */
44ed8f3e
PA
4828 auto count = this->symbol_name_count ();
4829 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4830 {
44ed8f3e 4831 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4832 continue;
4833
4834 const char *name = this->symbol_name_at (idx);
4835
4836 /* Add each name component to the name component table. */
4837 unsigned int previous_len = 0;
4838 for (unsigned int current_len = cp_find_first_component (name);
4839 name[current_len] != '\0';
4840 current_len += cp_find_first_component (name + current_len))
4841 {
4842 gdb_assert (name[current_len] == ':');
4843 this->name_components.push_back ({previous_len, idx});
4844 /* Skip the '::'. */
4845 current_len += 2;
4846 previous_len = current_len;
4847 }
4848 this->name_components.push_back ({previous_len, idx});
4849 }
4850
4851 /* Sort name_components elements by name. */
4852 auto name_comp_compare = [&] (const name_component &left,
4853 const name_component &right)
4854 {
4855 const char *left_qualified = this->symbol_name_at (left.idx);
4856 const char *right_qualified = this->symbol_name_at (right.idx);
4857
4858 const char *left_name = left_qualified + left.name_offset;
4859 const char *right_name = right_qualified + right.name_offset;
4860
4861 return name_cmp (left_name, right_name) < 0;
4862 };
4863
4864 std::sort (this->name_components.begin (),
4865 this->name_components.end (),
4866 name_comp_compare);
4867}
4868
4869/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4870 mapped_index_base instead of the containing objfile. This is split
4871 to a separate function in order to be able to unit test the
4872 name_components matching using a mock mapped_index_base. For each
5c58de74 4873 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4874 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4875
4876static void
4877dw2_expand_symtabs_matching_symbol
44ed8f3e 4878 (mapped_index_base &index,
5c58de74
PA
4879 const lookup_name_info &lookup_name_in,
4880 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4881 enum search_domain kind,
4882 gdb::function_view<void (offset_type)> match_callback)
4883{
4884 lookup_name_info lookup_name_without_params
4885 = lookup_name_in.make_ignore_params ();
4886 gdb_index_symbol_name_matcher lookup_name_matcher
4887 (lookup_name_without_params);
4888
4889 /* Build the symbol name component sorted vector, if we haven't
4890 yet. */
4891 index.build_name_components ();
4892
4893 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4894
3f563c84
PA
4895 /* Now for each symbol name in range, check to see if we have a name
4896 match, and if so, call the MATCH_CALLBACK callback. */
4897
4898 /* The same symbol may appear more than once in the range though.
4899 E.g., if we're looking for symbols that complete "w", and we have
4900 a symbol named "w1::w2", we'll find the two name components for
4901 that same symbol in the range. To be sure we only call the
4902 callback once per symbol, we first collect the symbol name
4903 indexes that matched in a temporary vector and ignore
4904 duplicates. */
4905 std::vector<offset_type> matches;
5c58de74 4906 matches.reserve (std::distance (bounds.first, bounds.second));
3f563c84 4907
5c58de74 4908 for (; bounds.first != bounds.second; ++bounds.first)
3f563c84 4909 {
5c58de74 4910 const char *qualified = index.symbol_name_at (bounds.first->idx);
3f563c84
PA
4911
4912 if (!lookup_name_matcher.matches (qualified)
4913 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4914 continue;
4915
5c58de74 4916 matches.push_back (bounds.first->idx);
3f563c84
PA
4917 }
4918
4919 std::sort (matches.begin (), matches.end ());
4920
4921 /* Finally call the callback, once per match. */
4922 ULONGEST prev = -1;
4923 for (offset_type idx : matches)
4924 {
4925 if (prev != idx)
4926 {
4927 match_callback (idx);
4928 prev = idx;
4929 }
4930 }
4931
4932 /* Above we use a type wider than idx's for 'prev', since 0 and
4933 (offset_type)-1 are both possible values. */
4934 static_assert (sizeof (prev) > sizeof (offset_type), "");
4935}
4936
c62446b1
PA
4937#if GDB_SELF_TEST
4938
4939namespace selftests { namespace dw2_expand_symtabs_matching {
4940
a3c5fafd
PA
4941/* A mock .gdb_index/.debug_names-like name index table, enough to
4942 exercise dw2_expand_symtabs_matching_symbol, which works with the
4943 mapped_index_base interface. Builds an index from the symbol list
4944 passed as parameter to the constructor. */
4945class mock_mapped_index : public mapped_index_base
c62446b1
PA
4946{
4947public:
a3c5fafd
PA
4948 mock_mapped_index (gdb::array_view<const char *> symbols)
4949 : m_symbol_table (symbols)
c62446b1
PA
4950 {}
4951
a3c5fafd 4952 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4953
a3c5fafd
PA
4954 /* Return the number of names in the symbol table. */
4955 virtual size_t symbol_name_count () const
c62446b1 4956 {
a3c5fafd 4957 return m_symbol_table.size ();
c62446b1
PA
4958 }
4959
a3c5fafd
PA
4960 /* Get the name of the symbol at IDX in the symbol table. */
4961 virtual const char *symbol_name_at (offset_type idx) const
4962 {
4963 return m_symbol_table[idx];
4964 }
c62446b1 4965
a3c5fafd
PA
4966private:
4967 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4968};
4969
4970/* Convenience function that converts a NULL pointer to a "<null>"
4971 string, to pass to print routines. */
4972
4973static const char *
4974string_or_null (const char *str)
4975{
4976 return str != NULL ? str : "<null>";
4977}
4978
4979/* Check if a lookup_name_info built from
4980 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4981 index. EXPECTED_LIST is the list of expected matches, in expected
4982 matching order. If no match expected, then an empty list is
4983 specified. Returns true on success. On failure prints a warning
4984 indicating the file:line that failed, and returns false. */
4985
4986static bool
4987check_match (const char *file, int line,
4988 mock_mapped_index &mock_index,
4989 const char *name, symbol_name_match_type match_type,
4990 bool completion_mode,
4991 std::initializer_list<const char *> expected_list)
4992{
4993 lookup_name_info lookup_name (name, match_type, completion_mode);
4994
4995 bool matched = true;
4996
4997 auto mismatch = [&] (const char *expected_str,
4998 const char *got)
4999 {
5000 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
5001 "expected=\"%s\", got=\"%s\"\n"),
5002 file, line,
5003 (match_type == symbol_name_match_type::FULL
5004 ? "FULL" : "WILD"),
5005 name, string_or_null (expected_str), string_or_null (got));
5006 matched = false;
5007 };
5008
5009 auto expected_it = expected_list.begin ();
5010 auto expected_end = expected_list.end ();
5011
a3c5fafd 5012 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
5013 NULL, ALL_DOMAIN,
5014 [&] (offset_type idx)
5015 {
a3c5fafd 5016 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
5017 const char *expected_str
5018 = expected_it == expected_end ? NULL : *expected_it++;
5019
5020 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
5021 mismatch (expected_str, matched_name);
5022 });
5023
5024 const char *expected_str
5025 = expected_it == expected_end ? NULL : *expected_it++;
5026 if (expected_str != NULL)
5027 mismatch (expected_str, NULL);
5028
5029 return matched;
5030}
5031
5032/* The symbols added to the mock mapped_index for testing (in
5033 canonical form). */
5034static const char *test_symbols[] = {
5035 "function",
5036 "std::bar",
5037 "std::zfunction",
5038 "std::zfunction2",
5039 "w1::w2",
5040 "ns::foo<char*>",
5041 "ns::foo<int>",
5042 "ns::foo<long>",
a20714ff
PA
5043 "ns2::tmpl<int>::foo2",
5044 "(anonymous namespace)::A::B::C",
c62446b1 5045
e1ef7d7a
PA
5046 /* These are used to check that the increment-last-char in the
5047 matching algorithm for completion doesn't match "t1_fund" when
5048 completing "t1_func". */
5049 "t1_func",
5050 "t1_func1",
5051 "t1_fund",
5052 "t1_fund1",
5053
5054 /* A UTF-8 name with multi-byte sequences to make sure that
5055 cp-name-parser understands this as a single identifier ("função"
5056 is "function" in PT). */
5057 u8"u8função",
5058
5059 /* \377 (0xff) is Latin1 'ÿ'. */
5060 "yfunc\377",
5061
5062 /* \377 (0xff) is Latin1 'ÿ'. */
5063 "\377",
5064 "\377\377123",
5065
c62446b1
PA
5066 /* A name with all sorts of complications. Starts with "z" to make
5067 it easier for the completion tests below. */
5068#define Z_SYM_NAME \
5069 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
5070 "::tuple<(anonymous namespace)::ui*, " \
5071 "std::default_delete<(anonymous namespace)::ui>, void>"
5072
5073 Z_SYM_NAME
5074};
5075
a3c5fafd
PA
5076/* Returns true if the mapped_index_base::find_name_component_bounds
5077 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
5078 in completion mode. */
5c58de74
PA
5079
5080static bool
a3c5fafd 5081check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
5082 const char *search_name,
5083 gdb::array_view<const char *> expected_syms)
5084{
5085 lookup_name_info lookup_name (search_name,
5086 symbol_name_match_type::FULL, true);
5087
5088 auto bounds = index.find_name_components_bounds (lookup_name);
5089
5090 size_t distance = std::distance (bounds.first, bounds.second);
5091 if (distance != expected_syms.size ())
5092 return false;
5093
5094 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
5095 {
5096 auto nc_elem = bounds.first + exp_elem;
5097 const char *qualified = index.symbol_name_at (nc_elem->idx);
5098 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
5099 return false;
5100 }
5101
5102 return true;
5103}
5104
5105/* Test the lower-level mapped_index::find_name_component_bounds
5106 method. */
5107
c62446b1 5108static void
5c58de74
PA
5109test_mapped_index_find_name_component_bounds ()
5110{
5111 mock_mapped_index mock_index (test_symbols);
5112
a3c5fafd 5113 mock_index.build_name_components ();
5c58de74
PA
5114
5115 /* Test the lower-level mapped_index::find_name_component_bounds
5116 method in completion mode. */
5117 {
5118 static const char *expected_syms[] = {
5119 "t1_func",
5120 "t1_func1",
5c58de74
PA
5121 };
5122
a3c5fafd 5123 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
5124 "t1_func", expected_syms));
5125 }
5126
5127 /* Check that the increment-last-char in the name matching algorithm
5128 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5129 {
5130 static const char *expected_syms1[] = {
5131 "\377",
5132 "\377\377123",
5133 };
a3c5fafd 5134 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
5135 "\377", expected_syms1));
5136
5137 static const char *expected_syms2[] = {
5138 "\377\377123",
5139 };
a3c5fafd 5140 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
5141 "\377\377", expected_syms2));
5142 }
5143}
5144
5145/* Test dw2_expand_symtabs_matching_symbol. */
5146
5147static void
5148test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
5149{
5150 mock_mapped_index mock_index (test_symbols);
5151
5152 /* We let all tests run until the end even if some fails, for debug
5153 convenience. */
5154 bool any_mismatch = false;
5155
5156 /* Create the expected symbols list (an initializer_list). Needed
5157 because lists have commas, and we need to pass them to CHECK,
5158 which is a macro. */
5159#define EXPECT(...) { __VA_ARGS__ }
5160
5161 /* Wrapper for check_match that passes down the current
5162 __FILE__/__LINE__. */
5163#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5164 any_mismatch |= !check_match (__FILE__, __LINE__, \
5165 mock_index, \
5166 NAME, MATCH_TYPE, COMPLETION_MODE, \
5167 EXPECTED_LIST)
5168
5169 /* Identity checks. */
5170 for (const char *sym : test_symbols)
5171 {
5172 /* Should be able to match all existing symbols. */
5173 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5174 EXPECT (sym));
5175
5176 /* Should be able to match all existing symbols with
5177 parameters. */
5178 std::string with_params = std::string (sym) + "(int)";
5179 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5180 EXPECT (sym));
5181
5182 /* Should be able to match all existing symbols with
5183 parameters and qualifiers. */
5184 with_params = std::string (sym) + " ( int ) const";
5185 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5186 EXPECT (sym));
5187
5188 /* This should really find sym, but cp-name-parser.y doesn't
5189 know about lvalue/rvalue qualifiers yet. */
5190 with_params = std::string (sym) + " ( int ) &&";
5191 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5192 {});
5193 }
5194
e1ef7d7a
PA
5195 /* Check that the name matching algorithm for completion doesn't get
5196 confused with Latin1 'ÿ' / 0xff. */
5197 {
5198 static const char str[] = "\377";
5199 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5200 EXPECT ("\377", "\377\377123"));
5201 }
5202
5203 /* Check that the increment-last-char in the matching algorithm for
5204 completion doesn't match "t1_fund" when completing "t1_func". */
5205 {
5206 static const char str[] = "t1_func";
5207 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5208 EXPECT ("t1_func", "t1_func1"));
5209 }
5210
c62446b1
PA
5211 /* Check that completion mode works at each prefix of the expected
5212 symbol name. */
5213 {
5214 static const char str[] = "function(int)";
5215 size_t len = strlen (str);
5216 std::string lookup;
5217
5218 for (size_t i = 1; i < len; i++)
5219 {
5220 lookup.assign (str, i);
5221 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5222 EXPECT ("function"));
5223 }
5224 }
5225
5226 /* While "w" is a prefix of both components, the match function
5227 should still only be called once. */
5228 {
5229 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5230 EXPECT ("w1::w2"));
a20714ff
PA
5231 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5232 EXPECT ("w1::w2"));
c62446b1
PA
5233 }
5234
5235 /* Same, with a "complicated" symbol. */
5236 {
5237 static const char str[] = Z_SYM_NAME;
5238 size_t len = strlen (str);
5239 std::string lookup;
5240
5241 for (size_t i = 1; i < len; i++)
5242 {
5243 lookup.assign (str, i);
5244 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5245 EXPECT (Z_SYM_NAME));
5246 }
5247 }
5248
5249 /* In FULL mode, an incomplete symbol doesn't match. */
5250 {
5251 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5252 {});
5253 }
5254
5255 /* A complete symbol with parameters matches any overload, since the
5256 index has no overload info. */
5257 {
5258 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5259 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
5260 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5261 EXPECT ("std::zfunction", "std::zfunction2"));
5262 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5263 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
5264 }
5265
5266 /* Check that whitespace is ignored appropriately. A symbol with a
5267 template argument list. */
5268 {
5269 static const char expected[] = "ns::foo<int>";
5270 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5271 EXPECT (expected));
a20714ff
PA
5272 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5273 EXPECT (expected));
c62446b1
PA
5274 }
5275
5276 /* Check that whitespace is ignored appropriately. A symbol with a
5277 template argument list that includes a pointer. */
5278 {
5279 static const char expected[] = "ns::foo<char*>";
5280 /* Try both completion and non-completion modes. */
5281 static const bool completion_mode[2] = {false, true};
5282 for (size_t i = 0; i < 2; i++)
5283 {
5284 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5285 completion_mode[i], EXPECT (expected));
a20714ff
PA
5286 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5287 completion_mode[i], EXPECT (expected));
c62446b1
PA
5288
5289 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5290 completion_mode[i], EXPECT (expected));
a20714ff
PA
5291 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5292 completion_mode[i], EXPECT (expected));
c62446b1
PA
5293 }
5294 }
5295
5296 {
5297 /* Check method qualifiers are ignored. */
5298 static const char expected[] = "ns::foo<char*>";
5299 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5300 symbol_name_match_type::FULL, true, EXPECT (expected));
5301 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5302 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
5303 CHECK_MATCH ("foo < char * > ( int ) const",
5304 symbol_name_match_type::WILD, true, EXPECT (expected));
5305 CHECK_MATCH ("foo < char * > ( int ) &&",
5306 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
5307 }
5308
5309 /* Test lookup names that don't match anything. */
5310 {
a20714ff
PA
5311 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5312 {});
5313
c62446b1
PA
5314 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5315 {});
5316 }
5317
a20714ff
PA
5318 /* Some wild matching tests, exercising "(anonymous namespace)",
5319 which should not be confused with a parameter list. */
5320 {
5321 static const char *syms[] = {
5322 "A::B::C",
5323 "B::C",
5324 "C",
5325 "A :: B :: C ( int )",
5326 "B :: C ( int )",
5327 "C ( int )",
5328 };
5329
5330 for (const char *s : syms)
5331 {
5332 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5333 EXPECT ("(anonymous namespace)::A::B::C"));
5334 }
5335 }
5336
5337 {
5338 static const char expected[] = "ns2::tmpl<int>::foo2";
5339 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5340 EXPECT (expected));
5341 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5342 EXPECT (expected));
5343 }
5344
c62446b1
PA
5345 SELF_CHECK (!any_mismatch);
5346
5347#undef EXPECT
5348#undef CHECK_MATCH
5349}
5350
5c58de74
PA
5351static void
5352run_test ()
5353{
5354 test_mapped_index_find_name_component_bounds ();
5355 test_dw2_expand_symtabs_matching_symbol ();
5356}
5357
c62446b1
PA
5358}} // namespace selftests::dw2_expand_symtabs_matching
5359
5360#endif /* GDB_SELF_TEST */
5361
4b514bc8
JK
5362/* If FILE_MATCHER is NULL or if PER_CU has
5363 dwarf2_per_cu_quick_data::MARK set (see
5364 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5365 EXPANSION_NOTIFY on it. */
5366
5367static void
5368dw2_expand_symtabs_matching_one
5369 (struct dwarf2_per_cu_data *per_cu,
5370 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5371 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5372{
5373 if (file_matcher == NULL || per_cu->v.quick->mark)
5374 {
5375 bool symtab_was_null
5376 = (per_cu->v.quick->compunit_symtab == NULL);
5377
5378 dw2_instantiate_symtab (per_cu);
5379
5380 if (expansion_notify != NULL
5381 && symtab_was_null
5382 && per_cu->v.quick->compunit_symtab != NULL)
5383 expansion_notify (per_cu->v.quick->compunit_symtab);
5384 }
5385}
5386
3f563c84
PA
5387/* Helper for dw2_expand_matching symtabs. Called on each symbol
5388 matched, to expand corresponding CUs that were marked. IDX is the
5389 index of the symbol name that matched. */
5390
5391static void
5392dw2_expand_marked_cus
ed2dc618 5393 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5394 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5395 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5396 search_domain kind)
5397{
3f563c84
PA
5398 offset_type *vec, vec_len, vec_idx;
5399 bool global_seen = false;
ed2dc618 5400 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5401
61920122 5402 vec = (offset_type *) (index.constant_pool
f00a2de2 5403 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5404 vec_len = MAYBE_SWAP (vec[0]);
5405 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5406 {
5407 struct dwarf2_per_cu_data *per_cu;
5408 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5409 /* This value is only valid for index versions >= 7. */
5410 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5411 gdb_index_symbol_kind symbol_kind =
5412 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5413 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5414 /* Only check the symbol attributes if they're present.
5415 Indices prior to version 7 don't record them,
5416 and indices >= 7 may elide them for certain symbols
5417 (gold does this). */
5418 int attrs_valid =
5419 (index.version >= 7
5420 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5421
5422 /* Work around gold/15646. */
5423 if (attrs_valid)
9291a0cd 5424 {
61920122
PA
5425 if (!is_static && global_seen)
5426 continue;
5427 if (!is_static)
5428 global_seen = true;
5429 }
3190f0c6 5430
61920122
PA
5431 /* Only check the symbol's kind if it has one. */
5432 if (attrs_valid)
5433 {
5434 switch (kind)
8943b874 5435 {
61920122
PA
5436 case VARIABLES_DOMAIN:
5437 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5438 continue;
5439 break;
5440 case FUNCTIONS_DOMAIN:
5441 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5442 continue;
61920122
PA
5443 break;
5444 case TYPES_DOMAIN:
5445 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5446 continue;
5447 break;
5448 default:
5449 break;
8943b874 5450 }
61920122 5451 }
8943b874 5452
61920122
PA
5453 /* Don't crash on bad data. */
5454 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5455 + dwarf2_per_objfile->n_type_units))
5456 {
5457 complaint (&symfile_complaints,
5458 _(".gdb_index entry has bad CU index"
ed2dc618
SM
5459 " [in module %s]"),
5460 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5461 continue;
5462 }
5463
ed2dc618 5464 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4b514bc8
JK
5465 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5466 expansion_notify);
61920122
PA
5467 }
5468}
5469
4b514bc8
JK
5470/* If FILE_MATCHER is non-NULL, set all the
5471 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5472 that match FILE_MATCHER. */
5473
61920122 5474static void
4b514bc8 5475dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5476 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5477 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5478{
4b514bc8 5479 if (file_matcher == NULL)
61920122
PA
5480 return;
5481
4b514bc8
JK
5482 objfile *const objfile = dwarf2_per_objfile->objfile;
5483
5484 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5485 htab_eq_pointer,
5486 NULL, xcalloc, xfree));
5487 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5488 htab_eq_pointer,
5489 NULL, xcalloc, xfree));
61920122 5490
4b514bc8
JK
5491 /* The rule is CUs specify all the files, including those used by
5492 any TU, so there's no need to scan TUs here. */
61920122 5493
927aa2e7
JK
5494 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5495 {
5496 int j;
ed2dc618 5497 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
927aa2e7
JK
5498 struct quick_file_names *file_data;
5499 void **slot;
5500
5501 QUIT;
5502
5503 per_cu->v.quick->mark = 0;
5504
5505 /* We only need to look at symtabs not already expanded. */
5506 if (per_cu->v.quick->compunit_symtab)
5507 continue;
5508
5509 file_data = dw2_get_file_names (per_cu);
5510 if (file_data == NULL)
5511 continue;
5512
5513 if (htab_find (visited_not_found.get (), file_data) != NULL)
5514 continue;
5515 else if (htab_find (visited_found.get (), file_data) != NULL)
5516 {
5517 per_cu->v.quick->mark = 1;
5518 continue;
5519 }
5520
5521 for (j = 0; j < file_data->num_file_names; ++j)
5522 {
5523 const char *this_real_name;
5524
5525 if (file_matcher (file_data->file_names[j], false))
5526 {
5527 per_cu->v.quick->mark = 1;
5528 break;
5529 }
5530
5531 /* Before we invoke realpath, which can get expensive when many
5532 files are involved, do a quick comparison of the basenames. */
5533 if (!basenames_may_differ
5534 && !file_matcher (lbasename (file_data->file_names[j]),
5535 true))
5536 continue;
5537
5538 this_real_name = dw2_get_real_path (objfile, file_data, j);
5539 if (file_matcher (this_real_name, false))
5540 {
5541 per_cu->v.quick->mark = 1;
5542 break;
5543 }
5544 }
5545
5546 slot = htab_find_slot (per_cu->v.quick->mark
5547 ? visited_found.get ()
5548 : visited_not_found.get (),
5549 file_data, INSERT);
5550 *slot = file_data;
5551 }
5552}
5553
5554static void
5555dw2_expand_symtabs_matching
5556 (struct objfile *objfile,
5557 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5558 const lookup_name_info &lookup_name,
5559 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5560 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5561 enum search_domain kind)
5562{
ed2dc618
SM
5563 struct dwarf2_per_objfile *dwarf2_per_objfile
5564 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5565
5566 /* index_table is NULL if OBJF_READNOW. */
5567 if (!dwarf2_per_objfile->index_table)
5568 return;
5569
ed2dc618 5570 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5571
5572 mapped_index &index = *dwarf2_per_objfile->index_table;
5573
5574 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5575 symbol_matcher,
5576 kind, [&] (offset_type idx)
5577 {
ed2dc618 5578 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7
JK
5579 expansion_notify, kind);
5580 });
5581}
5582
5583/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5584 symtab. */
5585
5586static struct compunit_symtab *
5587recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5588 CORE_ADDR pc)
5589{
5590 int i;
5591
5592 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5593 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5594 return cust;
5595
5596 if (cust->includes == NULL)
5597 return NULL;
5598
5599 for (i = 0; cust->includes[i]; ++i)
5600 {
5601 struct compunit_symtab *s = cust->includes[i];
5602
5603 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5604 if (s != NULL)
5605 return s;
5606 }
5607
5608 return NULL;
5609}
5610
5611static struct compunit_symtab *
5612dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5613 struct bound_minimal_symbol msymbol,
5614 CORE_ADDR pc,
5615 struct obj_section *section,
5616 int warn_if_readin)
5617{
5618 struct dwarf2_per_cu_data *data;
5619 struct compunit_symtab *result;
5620
927aa2e7
JK
5621 if (!objfile->psymtabs_addrmap)
5622 return NULL;
5623
5624 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5625 pc);
5626 if (!data)
5627 return NULL;
5628
5629 if (warn_if_readin && data->v.quick->compunit_symtab)
5630 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5631 paddress (get_objfile_arch (objfile), pc));
5632
5633 result
5634 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5635 pc);
5636 gdb_assert (result != NULL);
5637 return result;
5638}
5639
5640static void
5641dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5642 void *data, int need_fullname)
5643{
ed2dc618
SM
5644 struct dwarf2_per_objfile *dwarf2_per_objfile
5645 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5646
5647 if (!dwarf2_per_objfile->filenames_cache)
5648 {
5649 dwarf2_per_objfile->filenames_cache.emplace ();
5650
5651 htab_up visited (htab_create_alloc (10,
5652 htab_hash_pointer, htab_eq_pointer,
5653 NULL, xcalloc, xfree));
5654
5655 /* The rule is CUs specify all the files, including those used
5656 by any TU, so there's no need to scan TUs here. We can
5657 ignore file names coming from already-expanded CUs. */
5658
5659 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5660 {
ed2dc618 5661 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
927aa2e7
JK
5662
5663 if (per_cu->v.quick->compunit_symtab)
5664 {
5665 void **slot = htab_find_slot (visited.get (),
5666 per_cu->v.quick->file_names,
5667 INSERT);
5668
5669 *slot = per_cu->v.quick->file_names;
5670 }
5671 }
5672
5673 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5674 {
ed2dc618 5675 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
927aa2e7
JK
5676 struct quick_file_names *file_data;
5677 void **slot;
5678
5679 /* We only need to look at symtabs not already expanded. */
5680 if (per_cu->v.quick->compunit_symtab)
5681 continue;
5682
5683 file_data = dw2_get_file_names (per_cu);
5684 if (file_data == NULL)
5685 continue;
5686
5687 slot = htab_find_slot (visited.get (), file_data, INSERT);
5688 if (*slot)
5689 {
5690 /* Already visited. */
5691 continue;
5692 }
5693 *slot = file_data;
5694
5695 for (int j = 0; j < file_data->num_file_names; ++j)
5696 {
5697 const char *filename = file_data->file_names[j];
5698 dwarf2_per_objfile->filenames_cache->seen (filename);
5699 }
5700 }
5701 }
5702
5703 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5704 {
5705 gdb::unique_xmalloc_ptr<char> this_real_name;
5706
5707 if (need_fullname)
5708 this_real_name = gdb_realpath (filename);
5709 (*fun) (filename, this_real_name.get (), data);
5710 });
5711}
5712
5713static int
5714dw2_has_symbols (struct objfile *objfile)
5715{
5716 return 1;
5717}
5718
5719const struct quick_symbol_functions dwarf2_gdb_index_functions =
5720{
5721 dw2_has_symbols,
5722 dw2_find_last_source_symtab,
5723 dw2_forget_cached_source_info,
5724 dw2_map_symtabs_matching_filename,
5725 dw2_lookup_symbol,
5726 dw2_print_stats,
5727 dw2_dump,
5728 dw2_relocate,
5729 dw2_expand_symtabs_for_function,
5730 dw2_expand_all_symtabs,
5731 dw2_expand_symtabs_with_fullname,
5732 dw2_map_matching_symbols,
5733 dw2_expand_symtabs_matching,
5734 dw2_find_pc_sect_compunit_symtab,
5735 NULL,
5736 dw2_map_symbol_filenames
5737};
5738
5739/* DWARF-5 debug_names reader. */
5740
5741/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5742static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5743
5744/* A helper function that reads the .debug_names section in SECTION
5745 and fills in MAP. FILENAME is the name of the file containing the
5746 section; it is used for error reporting.
5747
5748 Returns true if all went well, false otherwise. */
5749
5750static bool
5751read_debug_names_from_section (struct objfile *objfile,
5752 const char *filename,
5753 struct dwarf2_section_info *section,
5754 mapped_debug_names &map)
5755{
5756 if (dwarf2_section_empty_p (section))
5757 return false;
5758
5759 /* Older elfutils strip versions could keep the section in the main
5760 executable while splitting it for the separate debug info file. */
5761 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5762 return false;
5763
5764 dwarf2_read_section (objfile, section);
5765
5766 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5767
5768 const gdb_byte *addr = section->buffer;
5769
5770 bfd *const abfd = get_section_bfd_owner (section);
5771
5772 unsigned int bytes_read;
5773 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5774 addr += bytes_read;
5775
5776 map.dwarf5_is_dwarf64 = bytes_read != 4;
5777 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5778 if (bytes_read + length != section->size)
5779 {
5780 /* There may be multiple per-CU indices. */
5781 warning (_("Section .debug_names in %s length %s does not match "
5782 "section length %s, ignoring .debug_names."),
5783 filename, plongest (bytes_read + length),
5784 pulongest (section->size));
5785 return false;
5786 }
5787
5788 /* The version number. */
5789 uint16_t version = read_2_bytes (abfd, addr);
5790 addr += 2;
5791 if (version != 5)
5792 {
5793 warning (_("Section .debug_names in %s has unsupported version %d, "
5794 "ignoring .debug_names."),
5795 filename, version);
5796 return false;
5797 }
5798
5799 /* Padding. */
5800 uint16_t padding = read_2_bytes (abfd, addr);
5801 addr += 2;
5802 if (padding != 0)
5803 {
5804 warning (_("Section .debug_names in %s has unsupported padding %d, "
5805 "ignoring .debug_names."),
5806 filename, padding);
5807 return false;
5808 }
5809
5810 /* comp_unit_count - The number of CUs in the CU list. */
5811 map.cu_count = read_4_bytes (abfd, addr);
5812 addr += 4;
5813
5814 /* local_type_unit_count - The number of TUs in the local TU
5815 list. */
5816 map.tu_count = read_4_bytes (abfd, addr);
5817 addr += 4;
5818
5819 /* foreign_type_unit_count - The number of TUs in the foreign TU
5820 list. */
5821 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5822 addr += 4;
5823 if (foreign_tu_count != 0)
5824 {
5825 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5826 "ignoring .debug_names."),
5827 filename, static_cast<unsigned long> (foreign_tu_count));
5828 return false;
5829 }
5830
5831 /* bucket_count - The number of hash buckets in the hash lookup
5832 table. */
5833 map.bucket_count = read_4_bytes (abfd, addr);
5834 addr += 4;
5835
5836 /* name_count - The number of unique names in the index. */
5837 map.name_count = read_4_bytes (abfd, addr);
5838 addr += 4;
5839
5840 /* abbrev_table_size - The size in bytes of the abbreviations
5841 table. */
5842 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5843 addr += 4;
5844
5845 /* augmentation_string_size - The size in bytes of the augmentation
5846 string. This value is rounded up to a multiple of 4. */
5847 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5848 addr += 4;
5849 map.augmentation_is_gdb = ((augmentation_string_size
5850 == sizeof (dwarf5_augmentation))
5851 && memcmp (addr, dwarf5_augmentation,
5852 sizeof (dwarf5_augmentation)) == 0);
5853 augmentation_string_size += (-augmentation_string_size) & 3;
5854 addr += augmentation_string_size;
5855
5856 /* List of CUs */
5857 map.cu_table_reordered = addr;
5858 addr += map.cu_count * map.offset_size;
5859
5860 /* List of Local TUs */
5861 map.tu_table_reordered = addr;
5862 addr += map.tu_count * map.offset_size;
5863
5864 /* Hash Lookup Table */
5865 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5866 addr += map.bucket_count * 4;
5867 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5868 addr += map.name_count * 4;
5869
5870 /* Name Table */
5871 map.name_table_string_offs_reordered = addr;
5872 addr += map.name_count * map.offset_size;
5873 map.name_table_entry_offs_reordered = addr;
5874 addr += map.name_count * map.offset_size;
5875
5876 const gdb_byte *abbrev_table_start = addr;
5877 for (;;)
5878 {
5879 unsigned int bytes_read;
5880 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5881 addr += bytes_read;
5882 if (index_num == 0)
5883 break;
5884
5885 const auto insertpair
5886 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5887 if (!insertpair.second)
5888 {
5889 warning (_("Section .debug_names in %s has duplicate index %s, "
5890 "ignoring .debug_names."),
5891 filename, pulongest (index_num));
5892 return false;
5893 }
5894 mapped_debug_names::index_val &indexval = insertpair.first->second;
5895 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5896 addr += bytes_read;
5897
5898 for (;;)
5899 {
5900 mapped_debug_names::index_val::attr attr;
5901 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5902 addr += bytes_read;
5903 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5904 addr += bytes_read;
5905 if (attr.form == DW_FORM_implicit_const)
5906 {
5907 attr.implicit_const = read_signed_leb128 (abfd, addr,
5908 &bytes_read);
5909 addr += bytes_read;
5910 }
5911 if (attr.dw_idx == 0 && attr.form == 0)
5912 break;
5913 indexval.attr_vec.push_back (std::move (attr));
5914 }
5915 }
5916 if (addr != abbrev_table_start + abbrev_table_size)
5917 {
5918 warning (_("Section .debug_names in %s has abbreviation_table "
5919 "of size %zu vs. written as %u, ignoring .debug_names."),
5920 filename, addr - abbrev_table_start, abbrev_table_size);
5921 return false;
5922 }
5923 map.entry_pool = addr;
5924
5925 return true;
5926}
5927
5928/* A helper for create_cus_from_debug_names that handles the MAP's CU
5929 list. */
5930
5931static void
ed2dc618 5932create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5933 const mapped_debug_names &map,
5934 dwarf2_section_info &section,
5935 bool is_dwz, int base_offset)
5936{
5937 sect_offset sect_off_prev;
5938 for (uint32_t i = 0; i <= map.cu_count; ++i)
5939 {
5940 sect_offset sect_off_next;
5941 if (i < map.cu_count)
5942 {
5943 sect_off_next
5944 = (sect_offset) (extract_unsigned_integer
5945 (map.cu_table_reordered + i * map.offset_size,
5946 map.offset_size,
5947 map.dwarf5_byte_order));
5948 }
5949 else
5950 sect_off_next = (sect_offset) section.size;
5951 if (i >= 1)
5952 {
5953 const ULONGEST length = sect_off_next - sect_off_prev;
5954 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
ed2dc618 5955 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7
JK
5956 sect_off_prev, length);
5957 }
5958 sect_off_prev = sect_off_next;
5959 }
5960}
5961
5962/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5963 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5964
5965static void
ed2dc618 5966create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5967 const mapped_debug_names &map,
5968 const mapped_debug_names &dwz_map)
5969{
ed2dc618 5970 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5971
5972 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
5973 dwarf2_per_objfile->all_comp_units
5974 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
5975 dwarf2_per_objfile->n_comp_units);
5976
ed2dc618
SM
5977 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5978 dwarf2_per_objfile->info,
927aa2e7
JK
5979 false /* is_dwz */,
5980 0 /* base_offset */);
5981
5982 if (dwz_map.cu_count == 0)
5983 return;
5984
ed2dc618
SM
5985 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5986 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
927aa2e7
JK
5987 true /* is_dwz */,
5988 map.cu_count /* base_offset */);
5989}
5990
5991/* Read .debug_names. If everything went ok, initialize the "quick"
5992 elements of all the CUs and return true. Otherwise, return false. */
5993
5994static bool
ed2dc618 5995dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5996{
ed2dc618
SM
5997 mapped_debug_names local_map (dwarf2_per_objfile);
5998 mapped_debug_names dwz_map (dwarf2_per_objfile);
5999 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
6000
6001 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
6002 &dwarf2_per_objfile->debug_names,
6003 local_map))
6004 return false;
6005
6006 /* Don't use the index if it's empty. */
6007 if (local_map.name_count == 0)
6008 return false;
6009
6010 /* If there is a .dwz file, read it so we can get its CU list as
6011 well. */
ed2dc618 6012 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
6013 if (dwz != NULL)
6014 {
6015 if (!read_debug_names_from_section (objfile,
6016 bfd_get_filename (dwz->dwz_bfd),
6017 &dwz->debug_names, dwz_map))
6018 {
6019 warning (_("could not read '.debug_names' section from %s; skipping"),
6020 bfd_get_filename (dwz->dwz_bfd));
6021 return false;
6022 }
6023 }
6024
ed2dc618 6025 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
927aa2e7
JK
6026
6027 if (local_map.tu_count != 0)
6028 {
6029 /* We can only handle a single .debug_types when we have an
6030 index. */
6031 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
6032 return false;
6033
6034 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
6035 dwarf2_per_objfile->types, 0);
6036
6037 create_signatured_type_table_from_debug_names
ed2dc618 6038 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
6039 }
6040
ed2dc618
SM
6041 create_addrmap_from_aranges (dwarf2_per_objfile,
6042 &dwarf2_per_objfile->debug_aranges);
927aa2e7 6043
ed2dc618
SM
6044 dwarf2_per_objfile->debug_names_table.reset
6045 (new mapped_debug_names (dwarf2_per_objfile));
927aa2e7
JK
6046 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
6047 dwarf2_per_objfile->using_index = 1;
6048 dwarf2_per_objfile->quick_file_names_table =
6049 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6050
6051 return true;
6052}
6053
6054/* Symbol name hashing function as specified by DWARF-5. */
6055
6056static uint32_t
6057dwarf5_djb_hash (const char *str_)
6058{
6059 const unsigned char *str = (const unsigned char *) str_;
6060
6061 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
6062 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
6063
6064 uint32_t hash = 5381;
6065 while (int c = *str++)
6066 hash = hash * 33 + tolower (c);
6067 return hash;
6068}
6069
6070/* Type used to manage iterating over all CUs looking for a symbol for
6071 .debug_names. */
6072
6073class dw2_debug_names_iterator
6074{
6075public:
6076 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
6077 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
6078 dw2_debug_names_iterator (const mapped_debug_names &map,
6079 bool want_specific_block,
6080 block_enum block_index, domain_enum domain,
6081 const char *name)
6082 : m_map (map), m_want_specific_block (want_specific_block),
6083 m_block_index (block_index), m_domain (domain),
6084 m_addr (find_vec_in_debug_names (map, name))
6085 {}
6086
6087 dw2_debug_names_iterator (const mapped_debug_names &map,
6088 search_domain search, uint32_t namei)
6089 : m_map (map),
6090 m_search (search),
6091 m_addr (find_vec_in_debug_names (map, namei))
6092 {}
6093
6094 /* Return the next matching CU or NULL if there are no more. */
6095 dwarf2_per_cu_data *next ();
6096
6097private:
6098 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6099 const char *name);
6100 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6101 uint32_t namei);
6102
6103 /* The internalized form of .debug_names. */
6104 const mapped_debug_names &m_map;
6105
6106 /* If true, only look for symbols that match BLOCK_INDEX. */
6107 const bool m_want_specific_block = false;
6108
6109 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6110 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6111 value. */
6112 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6113
6114 /* The kind of symbol we're looking for. */
6115 const domain_enum m_domain = UNDEF_DOMAIN;
6116 const search_domain m_search = ALL_DOMAIN;
6117
6118 /* The list of CUs from the index entry of the symbol, or NULL if
6119 not found. */
6120 const gdb_byte *m_addr;
6121};
6122
6123const char *
6124mapped_debug_names::namei_to_name (uint32_t namei) const
6125{
6126 const ULONGEST namei_string_offs
6127 = extract_unsigned_integer ((name_table_string_offs_reordered
6128 + namei * offset_size),
6129 offset_size,
6130 dwarf5_byte_order);
6131 return read_indirect_string_at_offset
ed2dc618 6132 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
6133}
6134
6135/* Find a slot in .debug_names for the object named NAME. If NAME is
6136 found, return pointer to its pool data. If NAME cannot be found,
6137 return NULL. */
6138
6139const gdb_byte *
6140dw2_debug_names_iterator::find_vec_in_debug_names
6141 (const mapped_debug_names &map, const char *name)
6142{
6143 int (*cmp) (const char *, const char *);
6144
6145 if (current_language->la_language == language_cplus
6146 || current_language->la_language == language_fortran
6147 || current_language->la_language == language_d)
6148 {
6149 /* NAME is already canonical. Drop any qualifiers as
6150 .debug_names does not contain any. */
6151
6152 if (strchr (name, '(') != NULL)
6153 {
6154 gdb::unique_xmalloc_ptr<char> without_params
6155 = cp_remove_params (name);
6156
6157 if (without_params != NULL)
6158 {
6159 name = without_params.get();
6160 }
6161 }
6162 }
6163
6164 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6165
6166 const uint32_t full_hash = dwarf5_djb_hash (name);
6167 uint32_t namei
6168 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6169 (map.bucket_table_reordered
6170 + (full_hash % map.bucket_count)), 4,
6171 map.dwarf5_byte_order);
6172 if (namei == 0)
6173 return NULL;
6174 --namei;
6175 if (namei >= map.name_count)
6176 {
6177 complaint (&symfile_complaints,
6178 _("Wrong .debug_names with name index %u but name_count=%u "
6179 "[in module %s]"),
6180 namei, map.name_count,
ed2dc618 6181 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
6182 return NULL;
6183 }
6184
6185 for (;;)
6186 {
6187 const uint32_t namei_full_hash
6188 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6189 (map.hash_table_reordered + namei), 4,
6190 map.dwarf5_byte_order);
6191 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6192 return NULL;
6193
6194 if (full_hash == namei_full_hash)
6195 {
6196 const char *const namei_string = map.namei_to_name (namei);
6197
6198#if 0 /* An expensive sanity check. */
6199 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6200 {
6201 complaint (&symfile_complaints,
6202 _("Wrong .debug_names hash for string at index %u "
6203 "[in module %s]"),
6204 namei, objfile_name (dwarf2_per_objfile->objfile));
6205 return NULL;
6206 }
6207#endif
6208
6209 if (cmp (namei_string, name) == 0)
6210 {
6211 const ULONGEST namei_entry_offs
6212 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6213 + namei * map.offset_size),
6214 map.offset_size, map.dwarf5_byte_order);
6215 return map.entry_pool + namei_entry_offs;
6216 }
6217 }
6218
6219 ++namei;
6220 if (namei >= map.name_count)
6221 return NULL;
6222 }
6223}
6224
6225const gdb_byte *
6226dw2_debug_names_iterator::find_vec_in_debug_names
6227 (const mapped_debug_names &map, uint32_t namei)
6228{
6229 if (namei >= map.name_count)
6230 {
6231 complaint (&symfile_complaints,
6232 _("Wrong .debug_names with name index %u but name_count=%u "
6233 "[in module %s]"),
6234 namei, map.name_count,
ed2dc618 6235 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
6236 return NULL;
6237 }
6238
6239 const ULONGEST namei_entry_offs
6240 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6241 + namei * map.offset_size),
6242 map.offset_size, map.dwarf5_byte_order);
6243 return map.entry_pool + namei_entry_offs;
6244}
6245
6246/* See dw2_debug_names_iterator. */
6247
6248dwarf2_per_cu_data *
6249dw2_debug_names_iterator::next ()
6250{
6251 if (m_addr == NULL)
6252 return NULL;
6253
ed2dc618
SM
6254 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
6255 struct objfile *objfile = dwarf2_per_objfile->objfile;
6256 bfd *const abfd = objfile->obfd;
927aa2e7
JK
6257
6258 again:
6259
6260 unsigned int bytes_read;
6261 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6262 m_addr += bytes_read;
6263 if (abbrev == 0)
6264 return NULL;
6265
6266 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6267 if (indexval_it == m_map.abbrev_map.cend ())
6268 {
6269 complaint (&symfile_complaints,
6270 _("Wrong .debug_names undefined abbrev code %s "
6271 "[in module %s]"),
ed2dc618 6272 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
6273 return NULL;
6274 }
6275 const mapped_debug_names::index_val &indexval = indexval_it->second;
6276 bool have_is_static = false;
6277 bool is_static;
6278 dwarf2_per_cu_data *per_cu = NULL;
6279 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6280 {
6281 ULONGEST ull;
6282 switch (attr.form)
6283 {
6284 case DW_FORM_implicit_const:
6285 ull = attr.implicit_const;
6286 break;
6287 case DW_FORM_flag_present:
6288 ull = 1;
6289 break;
6290 case DW_FORM_udata:
6291 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6292 m_addr += bytes_read;
6293 break;
6294 default:
6295 complaint (&symfile_complaints,
6296 _("Unsupported .debug_names form %s [in module %s]"),
6297 dwarf_form_name (attr.form),
ed2dc618 6298 objfile_name (objfile));
927aa2e7
JK
6299 return NULL;
6300 }
6301 switch (attr.dw_idx)
6302 {
6303 case DW_IDX_compile_unit:
6304 /* Don't crash on bad data. */
8af5c486 6305 if (ull >= dwarf2_per_objfile->n_comp_units)
927aa2e7
JK
6306 {
6307 complaint (&symfile_complaints,
6308 _(".debug_names entry has bad CU index %s"
6309 " [in module %s]"),
6310 pulongest (ull),
6311 objfile_name (dwarf2_per_objfile->objfile));
6312 continue;
6313 }
ed2dc618 6314 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
927aa2e7 6315 break;
8af5c486
JK
6316 case DW_IDX_type_unit:
6317 /* Don't crash on bad data. */
6318 if (ull >= dwarf2_per_objfile->n_type_units)
6319 {
6320 complaint (&symfile_complaints,
6321 _(".debug_names entry has bad TU index %s"
6322 " [in module %s]"),
6323 pulongest (ull),
6324 objfile_name (dwarf2_per_objfile->objfile));
6325 continue;
6326 }
ed2dc618
SM
6327 per_cu = dw2_get_cutu (dwarf2_per_objfile,
6328 dwarf2_per_objfile->n_comp_units + ull);
8af5c486 6329 break;
927aa2e7
JK
6330 case DW_IDX_GNU_internal:
6331 if (!m_map.augmentation_is_gdb)
6332 break;
6333 have_is_static = true;
6334 is_static = true;
6335 break;
6336 case DW_IDX_GNU_external:
6337 if (!m_map.augmentation_is_gdb)
6338 break;
6339 have_is_static = true;
6340 is_static = false;
6341 break;
6342 }
6343 }
6344
6345 /* Skip if already read in. */
6346 if (per_cu->v.quick->compunit_symtab)
6347 goto again;
6348
6349 /* Check static vs global. */
6350 if (have_is_static)
6351 {
6352 const bool want_static = m_block_index != GLOBAL_BLOCK;
6353 if (m_want_specific_block && want_static != is_static)
6354 goto again;
6355 }
6356
6357 /* Match dw2_symtab_iter_next, symbol_kind
6358 and debug_names::psymbol_tag. */
6359 switch (m_domain)
6360 {
6361 case VAR_DOMAIN:
6362 switch (indexval.dwarf_tag)
6363 {
6364 case DW_TAG_variable:
6365 case DW_TAG_subprogram:
6366 /* Some types are also in VAR_DOMAIN. */
6367 case DW_TAG_typedef:
6368 case DW_TAG_structure_type:
6369 break;
6370 default:
6371 goto again;
6372 }
6373 break;
6374 case STRUCT_DOMAIN:
6375 switch (indexval.dwarf_tag)
6376 {
6377 case DW_TAG_typedef:
6378 case DW_TAG_structure_type:
6379 break;
6380 default:
6381 goto again;
6382 }
6383 break;
6384 case LABEL_DOMAIN:
6385 switch (indexval.dwarf_tag)
6386 {
6387 case 0:
6388 case DW_TAG_variable:
6389 break;
6390 default:
6391 goto again;
6392 }
6393 break;
6394 default:
6395 break;
6396 }
6397
6398 /* Match dw2_expand_symtabs_matching, symbol_kind and
6399 debug_names::psymbol_tag. */
6400 switch (m_search)
4b514bc8 6401 {
927aa2e7
JK
6402 case VARIABLES_DOMAIN:
6403 switch (indexval.dwarf_tag)
4b514bc8 6404 {
927aa2e7
JK
6405 case DW_TAG_variable:
6406 break;
6407 default:
6408 goto again;
4b514bc8 6409 }
927aa2e7
JK
6410 break;
6411 case FUNCTIONS_DOMAIN:
6412 switch (indexval.dwarf_tag)
4b514bc8 6413 {
927aa2e7
JK
6414 case DW_TAG_subprogram:
6415 break;
6416 default:
6417 goto again;
4b514bc8 6418 }
927aa2e7
JK
6419 break;
6420 case TYPES_DOMAIN:
6421 switch (indexval.dwarf_tag)
6422 {
6423 case DW_TAG_typedef:
6424 case DW_TAG_structure_type:
6425 break;
6426 default:
6427 goto again;
6428 }
6429 break;
6430 default:
6431 break;
4b514bc8 6432 }
927aa2e7
JK
6433
6434 return per_cu;
4b514bc8 6435}
61920122 6436
927aa2e7
JK
6437static struct compunit_symtab *
6438dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6439 const char *name, domain_enum domain)
4b514bc8 6440{
927aa2e7 6441 const block_enum block_index = static_cast<block_enum> (block_index_int);
ed2dc618
SM
6442 struct dwarf2_per_objfile *dwarf2_per_objfile
6443 = get_dwarf2_per_objfile (objfile);
61920122 6444
927aa2e7
JK
6445 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6446 if (!mapp)
61920122 6447 {
927aa2e7
JK
6448 /* index is NULL if OBJF_READNOW. */
6449 return NULL;
6450 }
6451 const auto &map = *mapp;
9291a0cd 6452
927aa2e7
JK
6453 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6454 block_index, domain, name);
9703b513 6455
927aa2e7
JK
6456 struct compunit_symtab *stab_best = NULL;
6457 struct dwarf2_per_cu_data *per_cu;
6458 while ((per_cu = iter.next ()) != NULL)
6459 {
6460 struct symbol *sym, *with_opaque = NULL;
6461 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6462 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6463 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6464
927aa2e7
JK
6465 sym = block_find_symbol (block, name, domain,
6466 block_find_non_opaque_type_preferred,
6467 &with_opaque);
9703b513 6468
927aa2e7
JK
6469 /* Some caution must be observed with overloaded functions and
6470 methods, since the index will not contain any overload
6471 information (but NAME might contain it). */
a3ec0bb1 6472
927aa2e7
JK
6473 if (sym != NULL
6474 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6475 return stab;
6476 if (with_opaque != NULL
6477 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6478 stab_best = stab;
9703b513 6479
927aa2e7 6480 /* Keep looking through other CUs. */
9703b513
TT
6481 }
6482
927aa2e7 6483 return stab_best;
9703b513
TT
6484}
6485
927aa2e7
JK
6486/* This dumps minimal information about .debug_names. It is called
6487 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6488 uses this to verify that .debug_names has been loaded. */
9291a0cd 6489
927aa2e7
JK
6490static void
6491dw2_debug_names_dump (struct objfile *objfile)
6492{
ed2dc618
SM
6493 struct dwarf2_per_objfile *dwarf2_per_objfile
6494 = get_dwarf2_per_objfile (objfile);
6495
927aa2e7
JK
6496 gdb_assert (dwarf2_per_objfile->using_index);
6497 printf_filtered (".debug_names:");
6498 if (dwarf2_per_objfile->debug_names_table)
6499 printf_filtered (" exists\n");
6500 else
6501 printf_filtered (" faked for \"readnow\"\n");
6502 printf_filtered ("\n");
9291a0cd
TT
6503}
6504
9291a0cd 6505static void
927aa2e7
JK
6506dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6507 const char *func_name)
9291a0cd 6508{
ed2dc618
SM
6509 struct dwarf2_per_objfile *dwarf2_per_objfile
6510 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6511
927aa2e7
JK
6512 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6513 if (dwarf2_per_objfile->debug_names_table)
24c79950 6514 {
927aa2e7 6515 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6516
927aa2e7
JK
6517 /* Note: It doesn't matter what we pass for block_index here. */
6518 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6519 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
24c79950 6520
927aa2e7
JK
6521 struct dwarf2_per_cu_data *per_cu;
6522 while ((per_cu = iter.next ()) != NULL)
6523 dw2_instantiate_symtab (per_cu);
6524 }
6525}
24c79950 6526
927aa2e7
JK
6527static void
6528dw2_debug_names_expand_symtabs_matching
6529 (struct objfile *objfile,
6530 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6531 const lookup_name_info &lookup_name,
6532 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6533 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6534 enum search_domain kind)
6535{
ed2dc618
SM
6536 struct dwarf2_per_objfile *dwarf2_per_objfile
6537 = get_dwarf2_per_objfile (objfile);
9291a0cd 6538
927aa2e7
JK
6539 /* debug_names_table is NULL if OBJF_READNOW. */
6540 if (!dwarf2_per_objfile->debug_names_table)
6541 return;
9291a0cd 6542
ed2dc618 6543 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6544
44ed8f3e 6545 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6546
44ed8f3e
PA
6547 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6548 symbol_matcher,
6549 kind, [&] (offset_type namei)
927aa2e7 6550 {
927aa2e7
JK
6551 /* The name was matched, now expand corresponding CUs that were
6552 marked. */
6553 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6554
927aa2e7
JK
6555 struct dwarf2_per_cu_data *per_cu;
6556 while ((per_cu = iter.next ()) != NULL)
6557 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6558 expansion_notify);
44ed8f3e 6559 });
9291a0cd
TT
6560}
6561
927aa2e7 6562const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6563{
6564 dw2_has_symbols,
6565 dw2_find_last_source_symtab,
6566 dw2_forget_cached_source_info,
f8eba3c6 6567 dw2_map_symtabs_matching_filename,
927aa2e7 6568 dw2_debug_names_lookup_symbol,
9291a0cd 6569 dw2_print_stats,
927aa2e7 6570 dw2_debug_names_dump,
9291a0cd 6571 dw2_relocate,
927aa2e7 6572 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6573 dw2_expand_all_symtabs,
652a8996 6574 dw2_expand_symtabs_with_fullname,
40658b94 6575 dw2_map_matching_symbols,
927aa2e7 6576 dw2_debug_names_expand_symtabs_matching,
43f3e411 6577 dw2_find_pc_sect_compunit_symtab,
71a3c369 6578 NULL,
9291a0cd
TT
6579 dw2_map_symbol_filenames
6580};
6581
3c0aa29a 6582/* See symfile.h. */
9291a0cd 6583
3c0aa29a
PA
6584bool
6585dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6586{
ed2dc618
SM
6587 struct dwarf2_per_objfile *dwarf2_per_objfile
6588 = get_dwarf2_per_objfile (objfile);
6589
9291a0cd
TT
6590 /* If we're about to read full symbols, don't bother with the
6591 indices. In this case we also don't care if some other debug
6592 format is making psymtabs, because they are all about to be
6593 expanded anyway. */
6594 if ((objfile->flags & OBJF_READNOW))
6595 {
6596 int i;
6597
6598 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6599 create_all_comp_units (dwarf2_per_objfile);
6600 create_all_type_units (dwarf2_per_objfile);
7b9f3c50
DE
6601 dwarf2_per_objfile->quick_file_names_table =
6602 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 6603
1fd400ff 6604 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 6605 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 6606 {
ed2dc618 6607 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 6608
e254ef6a
DE
6609 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6610 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6611 }
6612
6613 /* Return 1 so that gdb sees the "quick" functions. However,
6614 these functions will be no-ops because we will have expanded
6615 all symtabs. */
3c0aa29a
PA
6616 *index_kind = dw_index_kind::GDB_INDEX;
6617 return true;
9291a0cd
TT
6618 }
6619
ed2dc618 6620 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6621 {
6622 *index_kind = dw_index_kind::DEBUG_NAMES;
6623 return true;
6624 }
927aa2e7 6625
9291a0cd 6626 if (dwarf2_read_index (objfile))
3c0aa29a
PA
6627 {
6628 *index_kind = dw_index_kind::GDB_INDEX;
6629 return true;
6630 }
9291a0cd 6631
3c0aa29a 6632 return false;
9291a0cd
TT
6633}
6634
6635\f
6636
dce234bc
PP
6637/* Build a partial symbol table. */
6638
6639void
f29dff0a 6640dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6641{
ed2dc618
SM
6642 struct dwarf2_per_objfile *dwarf2_per_objfile
6643 = get_dwarf2_per_objfile (objfile);
c9bf0622 6644
af5bf4ad
SM
6645 if (objfile->global_psymbols.capacity () == 0
6646 && objfile->static_psymbols.capacity () == 0)
6647 init_psymbol_list (objfile, 1024);
c906108c 6648
492d29ea 6649 TRY
c9bf0622
TT
6650 {
6651 /* This isn't really ideal: all the data we allocate on the
6652 objfile's obstack is still uselessly kept around. However,
6653 freeing it seems unsafe. */
906768f9 6654 psymtab_discarder psymtabs (objfile);
ed2dc618 6655 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6656 psymtabs.keep ();
c9bf0622 6657 }
492d29ea
PA
6658 CATCH (except, RETURN_MASK_ERROR)
6659 {
6660 exception_print (gdb_stderr, except);
6661 }
6662 END_CATCH
c906108c 6663}
c906108c 6664
1ce1cefd
DE
6665/* Return the total length of the CU described by HEADER. */
6666
6667static unsigned int
6668get_cu_length (const struct comp_unit_head *header)
6669{
6670 return header->initial_length_size + header->length;
6671}
6672
9c541725 6673/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6674
9c541725
PA
6675static inline bool
6676offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6677{
9c541725
PA
6678 sect_offset bottom = cu_header->sect_off;
6679 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6680
9c541725 6681 return sect_off >= bottom && sect_off < top;
45452591
DE
6682}
6683
3b80fe9b
DE
6684/* Find the base address of the compilation unit for range lists and
6685 location lists. It will normally be specified by DW_AT_low_pc.
6686 In DWARF-3 draft 4, the base address could be overridden by
6687 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6688 compilation units with discontinuous ranges. */
6689
6690static void
6691dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6692{
6693 struct attribute *attr;
6694
6695 cu->base_known = 0;
6696 cu->base_address = 0;
6697
6698 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6699 if (attr)
6700 {
31aa7e4e 6701 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6702 cu->base_known = 1;
6703 }
6704 else
6705 {
6706 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6707 if (attr)
6708 {
31aa7e4e 6709 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6710 cu->base_known = 1;
6711 }
6712 }
6713}
6714
93311388 6715/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6716 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6717 NOTE: This leaves members offset, first_die_offset to be filled in
6718 by the caller. */
107d2387 6719
d521ce57 6720static const gdb_byte *
107d2387 6721read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6722 const gdb_byte *info_ptr,
6723 struct dwarf2_section_info *section,
6724 rcuh_kind section_kind)
107d2387
AC
6725{
6726 int signed_addr;
891d2f0b 6727 unsigned int bytes_read;
43988095
JK
6728 const char *filename = get_section_file_name (section);
6729 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6730
6731 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6732 cu_header->initial_length_size = bytes_read;
6733 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6734 info_ptr += bytes_read;
107d2387
AC
6735 cu_header->version = read_2_bytes (abfd, info_ptr);
6736 info_ptr += 2;
43988095
JK
6737 if (cu_header->version < 5)
6738 switch (section_kind)
6739 {
6740 case rcuh_kind::COMPILE:
6741 cu_header->unit_type = DW_UT_compile;
6742 break;
6743 case rcuh_kind::TYPE:
6744 cu_header->unit_type = DW_UT_type;
6745 break;
6746 default:
6747 internal_error (__FILE__, __LINE__,
6748 _("read_comp_unit_head: invalid section_kind"));
6749 }
6750 else
6751 {
6752 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6753 (read_1_byte (abfd, info_ptr));
6754 info_ptr += 1;
6755 switch (cu_header->unit_type)
6756 {
6757 case DW_UT_compile:
6758 if (section_kind != rcuh_kind::COMPILE)
6759 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6760 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6761 filename);
6762 break;
6763 case DW_UT_type:
6764 section_kind = rcuh_kind::TYPE;
6765 break;
6766 default:
6767 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6768 "(is %d, should be %d or %d) [in module %s]"),
6769 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6770 }
6771
6772 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6773 info_ptr += 1;
6774 }
9c541725
PA
6775 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6776 cu_header,
6777 &bytes_read);
613e1657 6778 info_ptr += bytes_read;
43988095
JK
6779 if (cu_header->version < 5)
6780 {
6781 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6782 info_ptr += 1;
6783 }
107d2387
AC
6784 signed_addr = bfd_get_sign_extend_vma (abfd);
6785 if (signed_addr < 0)
8e65ff28 6786 internal_error (__FILE__, __LINE__,
e2e0b3e5 6787 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6788 cu_header->signed_addr_p = signed_addr;
c764a876 6789
43988095
JK
6790 if (section_kind == rcuh_kind::TYPE)
6791 {
6792 LONGEST type_offset;
6793
6794 cu_header->signature = read_8_bytes (abfd, info_ptr);
6795 info_ptr += 8;
6796
6797 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6798 info_ptr += bytes_read;
9c541725
PA
6799 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6800 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6801 error (_("Dwarf Error: Too big type_offset in compilation unit "
6802 "header (is %s) [in module %s]"), plongest (type_offset),
6803 filename);
6804 }
6805
107d2387
AC
6806 return info_ptr;
6807}
6808
36586728
TT
6809/* Helper function that returns the proper abbrev section for
6810 THIS_CU. */
6811
6812static struct dwarf2_section_info *
6813get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6814{
6815 struct dwarf2_section_info *abbrev;
ed2dc618 6816 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6817
6818 if (this_cu->is_dwz)
ed2dc618 6819 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6820 else
6821 abbrev = &dwarf2_per_objfile->abbrev;
6822
6823 return abbrev;
6824}
6825
9ff913ba
DE
6826/* Subroutine of read_and_check_comp_unit_head and
6827 read_and_check_type_unit_head to simplify them.
6828 Perform various error checking on the header. */
6829
6830static void
ed2dc618
SM
6831error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6832 struct comp_unit_head *header,
4bdcc0c1
DE
6833 struct dwarf2_section_info *section,
6834 struct dwarf2_section_info *abbrev_section)
9ff913ba 6835{
a32a8923 6836 const char *filename = get_section_file_name (section);
9ff913ba 6837
43988095 6838 if (header->version < 2 || header->version > 5)
9ff913ba 6839 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 6840 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
6841 filename);
6842
9c541725 6843 if (to_underlying (header->abbrev_sect_off)
36586728 6844 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9c541725
PA
6845 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
6846 "(offset 0x%x + 6) [in module %s]"),
6847 to_underlying (header->abbrev_sect_off),
6848 to_underlying (header->sect_off),
9ff913ba
DE
6849 filename);
6850
9c541725 6851 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6852 avoid potential 32-bit overflow. */
9c541725 6853 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6854 > section->size)
9c541725
PA
6855 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6856 "(offset 0x%x + 0) [in module %s]"),
6857 header->length, to_underlying (header->sect_off),
9ff913ba
DE
6858 filename);
6859}
6860
6861/* Read in a CU/TU header and perform some basic error checking.
6862 The contents of the header are stored in HEADER.
6863 The result is a pointer to the start of the first DIE. */
adabb602 6864
d521ce57 6865static const gdb_byte *
ed2dc618
SM
6866read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6867 struct comp_unit_head *header,
9ff913ba 6868 struct dwarf2_section_info *section,
4bdcc0c1 6869 struct dwarf2_section_info *abbrev_section,
d521ce57 6870 const gdb_byte *info_ptr,
43988095 6871 rcuh_kind section_kind)
72bf9492 6872{
d521ce57 6873 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6874
9c541725 6875 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6876
43988095 6877 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6878
9c541725 6879 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6880
ed2dc618
SM
6881 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6882 abbrev_section);
9ff913ba
DE
6883
6884 return info_ptr;
348e048f
DE
6885}
6886
f4dc4d17
DE
6887/* Fetch the abbreviation table offset from a comp or type unit header. */
6888
6889static sect_offset
ed2dc618
SM
6890read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6891 struct dwarf2_section_info *section,
9c541725 6892 sect_offset sect_off)
f4dc4d17 6893{
a32a8923 6894 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6895 const gdb_byte *info_ptr;
ac298888 6896 unsigned int initial_length_size, offset_size;
43988095 6897 uint16_t version;
f4dc4d17
DE
6898
6899 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6900 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6901 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6902 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6903 info_ptr += initial_length_size;
6904
6905 version = read_2_bytes (abfd, info_ptr);
6906 info_ptr += 2;
6907 if (version >= 5)
6908 {
6909 /* Skip unit type and address size. */
6910 info_ptr += 2;
6911 }
6912
9c541725 6913 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6914}
6915
aaa75496
JB
6916/* Allocate a new partial symtab for file named NAME and mark this new
6917 partial symtab as being an include of PST. */
6918
6919static void
d521ce57 6920dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6921 struct objfile *objfile)
6922{
6923 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6924
fbd9ab74
JK
6925 if (!IS_ABSOLUTE_PATH (subpst->filename))
6926 {
6927 /* It shares objfile->objfile_obstack. */
6928 subpst->dirname = pst->dirname;
6929 }
6930
aaa75496
JB
6931 subpst->textlow = 0;
6932 subpst->texthigh = 0;
6933
8d749320
SM
6934 subpst->dependencies
6935 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
6936 subpst->dependencies[0] = pst;
6937 subpst->number_of_dependencies = 1;
6938
6939 subpst->globals_offset = 0;
6940 subpst->n_global_syms = 0;
6941 subpst->statics_offset = 0;
6942 subpst->n_static_syms = 0;
43f3e411 6943 subpst->compunit_symtab = NULL;
aaa75496
JB
6944 subpst->read_symtab = pst->read_symtab;
6945 subpst->readin = 0;
6946
6947 /* No private part is necessary for include psymtabs. This property
6948 can be used to differentiate between such include psymtabs and
10b3939b 6949 the regular ones. */
58a9656e 6950 subpst->read_symtab_private = NULL;
aaa75496
JB
6951}
6952
6953/* Read the Line Number Program data and extract the list of files
6954 included by the source file represented by PST. Build an include
d85a05f0 6955 partial symtab for each of these included files. */
aaa75496
JB
6956
6957static void
6958dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6959 struct die_info *die,
6960 struct partial_symtab *pst)
aaa75496 6961{
fff8551c 6962 line_header_up lh;
d85a05f0 6963 struct attribute *attr;
aaa75496 6964
d85a05f0
DJ
6965 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6966 if (attr)
9c541725 6967 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6968 if (lh == NULL)
6969 return; /* No linetable, so no includes. */
6970
c6da4cef 6971 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 6972 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
6973}
6974
348e048f 6975static hashval_t
52dc124a 6976hash_signatured_type (const void *item)
348e048f 6977{
9a3c8263
SM
6978 const struct signatured_type *sig_type
6979 = (const struct signatured_type *) item;
9a619af0 6980
348e048f 6981 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6982 return sig_type->signature;
348e048f
DE
6983}
6984
6985static int
52dc124a 6986eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6987{
9a3c8263
SM
6988 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6989 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6990
348e048f
DE
6991 return lhs->signature == rhs->signature;
6992}
6993
1fd400ff
TT
6994/* Allocate a hash table for signatured types. */
6995
6996static htab_t
673bfd45 6997allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6998{
6999 return htab_create_alloc_ex (41,
52dc124a
DE
7000 hash_signatured_type,
7001 eq_signatured_type,
1fd400ff
TT
7002 NULL,
7003 &objfile->objfile_obstack,
7004 hashtab_obstack_allocate,
7005 dummy_obstack_deallocate);
7006}
7007
d467dd73 7008/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
7009
7010static int
d467dd73 7011add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 7012{
9a3c8263
SM
7013 struct signatured_type *sigt = (struct signatured_type *) *slot;
7014 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 7015
b4dd5633 7016 **datap = sigt;
1fd400ff
TT
7017 ++*datap;
7018
7019 return 1;
7020}
7021
78d4d2c5 7022/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
7023 and fill them into TYPES_HTAB. It will process only type units,
7024 therefore DW_UT_type. */
c88ee1f0 7025
78d4d2c5 7026static void
ed2dc618
SM
7027create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7028 struct dwo_file *dwo_file,
43988095
JK
7029 dwarf2_section_info *section, htab_t &types_htab,
7030 rcuh_kind section_kind)
348e048f 7031{
3019eac3 7032 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 7033 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
7034 bfd *abfd;
7035 const gdb_byte *info_ptr, *end_ptr;
348e048f 7036
4bdcc0c1
DE
7037 abbrev_section = (dwo_file != NULL
7038 ? &dwo_file->sections.abbrev
7039 : &dwarf2_per_objfile->abbrev);
7040
b4f54984 7041 if (dwarf_read_debug)
43988095
JK
7042 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
7043 get_section_name (section),
a32a8923 7044 get_section_file_name (abbrev_section));
09406207 7045
78d4d2c5
JK
7046 dwarf2_read_section (objfile, section);
7047 info_ptr = section->buffer;
348e048f 7048
78d4d2c5
JK
7049 if (info_ptr == NULL)
7050 return;
348e048f 7051
78d4d2c5
JK
7052 /* We can't set abfd until now because the section may be empty or
7053 not present, in which case the bfd is unknown. */
7054 abfd = get_section_bfd_owner (section);
348e048f 7055
78d4d2c5
JK
7056 /* We don't use init_cutu_and_read_dies_simple, or some such, here
7057 because we don't need to read any dies: the signature is in the
7058 header. */
3019eac3 7059
78d4d2c5
JK
7060 end_ptr = info_ptr + section->size;
7061 while (info_ptr < end_ptr)
7062 {
78d4d2c5
JK
7063 struct signatured_type *sig_type;
7064 struct dwo_unit *dwo_tu;
7065 void **slot;
7066 const gdb_byte *ptr = info_ptr;
7067 struct comp_unit_head header;
7068 unsigned int length;
8b70b953 7069
9c541725 7070 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 7071
a49dd8dd
JK
7072 /* Initialize it due to a false compiler warning. */
7073 header.signature = -1;
9c541725 7074 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 7075
78d4d2c5
JK
7076 /* We need to read the type's signature in order to build the hash
7077 table, but we don't need anything else just yet. */
348e048f 7078
ed2dc618 7079 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 7080 abbrev_section, ptr, section_kind);
348e048f 7081
78d4d2c5 7082 length = get_cu_length (&header);
6caca83c 7083
78d4d2c5
JK
7084 /* Skip dummy type units. */
7085 if (ptr >= info_ptr + length
43988095
JK
7086 || peek_abbrev_code (abfd, ptr) == 0
7087 || header.unit_type != DW_UT_type)
78d4d2c5
JK
7088 {
7089 info_ptr += length;
7090 continue;
7091 }
dee91e82 7092
78d4d2c5
JK
7093 if (types_htab == NULL)
7094 {
7095 if (dwo_file)
7096 types_htab = allocate_dwo_unit_table (objfile);
7097 else
7098 types_htab = allocate_signatured_type_table (objfile);
7099 }
8b70b953 7100
78d4d2c5
JK
7101 if (dwo_file)
7102 {
7103 sig_type = NULL;
7104 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7105 struct dwo_unit);
7106 dwo_tu->dwo_file = dwo_file;
43988095 7107 dwo_tu->signature = header.signature;
9c541725 7108 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 7109 dwo_tu->section = section;
9c541725 7110 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
7111 dwo_tu->length = length;
7112 }
7113 else
7114 {
7115 /* N.B.: type_offset is not usable if this type uses a DWO file.
7116 The real type_offset is in the DWO file. */
7117 dwo_tu = NULL;
7118 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7119 struct signatured_type);
43988095 7120 sig_type->signature = header.signature;
9c541725 7121 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 7122 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
7123 sig_type->per_cu.is_debug_types = 1;
7124 sig_type->per_cu.section = section;
9c541725 7125 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
7126 sig_type->per_cu.length = length;
7127 }
7128
7129 slot = htab_find_slot (types_htab,
7130 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7131 INSERT);
7132 gdb_assert (slot != NULL);
7133 if (*slot != NULL)
7134 {
9c541725 7135 sect_offset dup_sect_off;
0349ea22 7136
3019eac3
DE
7137 if (dwo_file)
7138 {
78d4d2c5
JK
7139 const struct dwo_unit *dup_tu
7140 = (const struct dwo_unit *) *slot;
7141
9c541725 7142 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
7143 }
7144 else
7145 {
78d4d2c5
JK
7146 const struct signatured_type *dup_tu
7147 = (const struct signatured_type *) *slot;
7148
9c541725 7149 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 7150 }
8b70b953 7151
78d4d2c5
JK
7152 complaint (&symfile_complaints,
7153 _("debug type entry at offset 0x%x is duplicate to"
7154 " the entry at offset 0x%x, signature %s"),
9c541725 7155 to_underlying (sect_off), to_underlying (dup_sect_off),
43988095 7156 hex_string (header.signature));
78d4d2c5
JK
7157 }
7158 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 7159
78d4d2c5
JK
7160 if (dwarf_read_debug > 1)
7161 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
9c541725 7162 to_underlying (sect_off),
43988095 7163 hex_string (header.signature));
3019eac3 7164
78d4d2c5
JK
7165 info_ptr += length;
7166 }
7167}
3019eac3 7168
78d4d2c5
JK
7169/* Create the hash table of all entries in the .debug_types
7170 (or .debug_types.dwo) section(s).
7171 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7172 otherwise it is NULL.
b3c8eb43 7173
78d4d2c5 7174 The result is a pointer to the hash table or NULL if there are no types.
348e048f 7175
78d4d2c5 7176 Note: This function processes DWO files only, not DWP files. */
348e048f 7177
78d4d2c5 7178static void
ed2dc618
SM
7179create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7180 struct dwo_file *dwo_file,
78d4d2c5
JK
7181 VEC (dwarf2_section_info_def) *types,
7182 htab_t &types_htab)
7183{
7184 int ix;
7185 struct dwarf2_section_info *section;
7186
7187 if (VEC_empty (dwarf2_section_info_def, types))
7188 return;
348e048f 7189
78d4d2c5
JK
7190 for (ix = 0;
7191 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7192 ++ix)
ed2dc618
SM
7193 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
7194 types_htab, rcuh_kind::TYPE);
3019eac3
DE
7195}
7196
7197/* Create the hash table of all entries in the .debug_types section,
7198 and initialize all_type_units.
7199 The result is zero if there is an error (e.g. missing .debug_types section),
7200 otherwise non-zero. */
7201
7202static int
ed2dc618 7203create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 7204{
78d4d2c5 7205 htab_t types_htab = NULL;
b4dd5633 7206 struct signatured_type **iter;
3019eac3 7207
ed2dc618
SM
7208 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
7209 &dwarf2_per_objfile->info, types_htab,
43988095 7210 rcuh_kind::COMPILE);
ed2dc618
SM
7211 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
7212 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
7213 if (types_htab == NULL)
7214 {
7215 dwarf2_per_objfile->signatured_types = NULL;
7216 return 0;
7217 }
7218
348e048f
DE
7219 dwarf2_per_objfile->signatured_types = types_htab;
7220
6aa5f3a6
DE
7221 dwarf2_per_objfile->n_type_units
7222 = dwarf2_per_objfile->n_allocated_type_units
7223 = htab_elements (types_htab);
8d749320
SM
7224 dwarf2_per_objfile->all_type_units =
7225 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
7226 iter = &dwarf2_per_objfile->all_type_units[0];
7227 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7228 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7229 == dwarf2_per_objfile->n_type_units);
1fd400ff 7230
348e048f
DE
7231 return 1;
7232}
7233
6aa5f3a6
DE
7234/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7235 If SLOT is non-NULL, it is the entry to use in the hash table.
7236 Otherwise we find one. */
7237
7238static struct signatured_type *
ed2dc618
SM
7239add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
7240 void **slot)
6aa5f3a6
DE
7241{
7242 struct objfile *objfile = dwarf2_per_objfile->objfile;
7243 int n_type_units = dwarf2_per_objfile->n_type_units;
7244 struct signatured_type *sig_type;
7245
7246 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7247 ++n_type_units;
7248 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7249 {
7250 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7251 dwarf2_per_objfile->n_allocated_type_units = 1;
7252 dwarf2_per_objfile->n_allocated_type_units *= 2;
7253 dwarf2_per_objfile->all_type_units
224c3ddb
SM
7254 = XRESIZEVEC (struct signatured_type *,
7255 dwarf2_per_objfile->all_type_units,
7256 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
7257 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7258 }
7259 dwarf2_per_objfile->n_type_units = n_type_units;
7260
7261 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7262 struct signatured_type);
7263 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7264 sig_type->signature = sig;
7265 sig_type->per_cu.is_debug_types = 1;
7266 if (dwarf2_per_objfile->using_index)
7267 {
7268 sig_type->per_cu.v.quick =
7269 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7270 struct dwarf2_per_cu_quick_data);
7271 }
7272
7273 if (slot == NULL)
7274 {
7275 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7276 sig_type, INSERT);
7277 }
7278 gdb_assert (*slot == NULL);
7279 *slot = sig_type;
7280 /* The rest of sig_type must be filled in by the caller. */
7281 return sig_type;
7282}
7283
a2ce51a0
DE
7284/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7285 Fill in SIG_ENTRY with DWO_ENTRY. */
7286
7287static void
ed2dc618 7288fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
7289 struct signatured_type *sig_entry,
7290 struct dwo_unit *dwo_entry)
7291{
7ee85ab1 7292 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
7293 gdb_assert (! sig_entry->per_cu.queued);
7294 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
7295 if (dwarf2_per_objfile->using_index)
7296 {
7297 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 7298 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
7299 }
7300 else
7301 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 7302 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 7303 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 7304 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
7305 gdb_assert (sig_entry->dwo_unit == NULL);
7306
7307 sig_entry->per_cu.section = dwo_entry->section;
9c541725 7308 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
7309 sig_entry->per_cu.length = dwo_entry->length;
7310 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 7311 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
7312 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7313 sig_entry->dwo_unit = dwo_entry;
7314}
7315
7316/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
7317 If we haven't read the TU yet, create the signatured_type data structure
7318 for a TU to be read in directly from a DWO file, bypassing the stub.
7319 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7320 using .gdb_index, then when reading a CU we want to stay in the DWO file
7321 containing that CU. Otherwise we could end up reading several other DWO
7322 files (due to comdat folding) to process the transitive closure of all the
7323 mentioned TUs, and that can be slow. The current DWO file will have every
7324 type signature that it needs.
a2ce51a0
DE
7325 We only do this for .gdb_index because in the psymtab case we already have
7326 to read all the DWOs to build the type unit groups. */
7327
7328static struct signatured_type *
7329lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7330{
518817b3
SM
7331 struct dwarf2_per_objfile *dwarf2_per_objfile
7332 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
7333 struct objfile *objfile = dwarf2_per_objfile->objfile;
7334 struct dwo_file *dwo_file;
7335 struct dwo_unit find_dwo_entry, *dwo_entry;
7336 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7337 void **slot;
a2ce51a0
DE
7338
7339 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7340
6aa5f3a6
DE
7341 /* If TU skeletons have been removed then we may not have read in any
7342 TUs yet. */
7343 if (dwarf2_per_objfile->signatured_types == NULL)
7344 {
7345 dwarf2_per_objfile->signatured_types
7346 = allocate_signatured_type_table (objfile);
7347 }
a2ce51a0
DE
7348
7349 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7350 Use the global signatured_types array to do our own comdat-folding
7351 of types. If this is the first time we're reading this TU, and
7352 the TU has an entry in .gdb_index, replace the recorded data from
7353 .gdb_index with this TU. */
a2ce51a0 7354
a2ce51a0 7355 find_sig_entry.signature = sig;
6aa5f3a6
DE
7356 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7357 &find_sig_entry, INSERT);
9a3c8263 7358 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7359
7360 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7361 read. Don't reassign the global entry to point to this DWO if that's
7362 the case. Also note that if the TU is already being read, it may not
7363 have come from a DWO, the program may be a mix of Fission-compiled
7364 code and non-Fission-compiled code. */
7365
7366 /* Have we already tried to read this TU?
7367 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7368 needn't exist in the global table yet). */
7369 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7370 return sig_entry;
7371
6aa5f3a6
DE
7372 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7373 dwo_unit of the TU itself. */
7374 dwo_file = cu->dwo_unit->dwo_file;
7375
a2ce51a0
DE
7376 /* Ok, this is the first time we're reading this TU. */
7377 if (dwo_file->tus == NULL)
7378 return NULL;
7379 find_dwo_entry.signature = sig;
9a3c8263 7380 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7381 if (dwo_entry == NULL)
7382 return NULL;
7383
6aa5f3a6
DE
7384 /* If the global table doesn't have an entry for this TU, add one. */
7385 if (sig_entry == NULL)
ed2dc618 7386 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7387
ed2dc618 7388 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7389 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7390 return sig_entry;
7391}
7392
a2ce51a0
DE
7393/* Subroutine of lookup_signatured_type.
7394 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7395 then try the DWP file. If the TU stub (skeleton) has been removed then
7396 it won't be in .gdb_index. */
a2ce51a0
DE
7397
7398static struct signatured_type *
7399lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7400{
518817b3
SM
7401 struct dwarf2_per_objfile *dwarf2_per_objfile
7402 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7403 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7404 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7405 struct dwo_unit *dwo_entry;
7406 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7407 void **slot;
a2ce51a0
DE
7408
7409 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7410 gdb_assert (dwp_file != NULL);
7411
6aa5f3a6
DE
7412 /* If TU skeletons have been removed then we may not have read in any
7413 TUs yet. */
7414 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7415 {
6aa5f3a6
DE
7416 dwarf2_per_objfile->signatured_types
7417 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7418 }
7419
6aa5f3a6
DE
7420 find_sig_entry.signature = sig;
7421 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7422 &find_sig_entry, INSERT);
9a3c8263 7423 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7424
7425 /* Have we already tried to read this TU?
7426 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7427 needn't exist in the global table yet). */
7428 if (sig_entry != NULL)
7429 return sig_entry;
7430
a2ce51a0
DE
7431 if (dwp_file->tus == NULL)
7432 return NULL;
ed2dc618 7433 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7434 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7435 if (dwo_entry == NULL)
7436 return NULL;
7437
ed2dc618
SM
7438 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7439 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7440
a2ce51a0
DE
7441 return sig_entry;
7442}
7443
380bca97 7444/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7445 Returns NULL if signature SIG is not present in the table.
7446 It is up to the caller to complain about this. */
348e048f
DE
7447
7448static struct signatured_type *
a2ce51a0 7449lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7450{
518817b3
SM
7451 struct dwarf2_per_objfile *dwarf2_per_objfile
7452 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7453
a2ce51a0
DE
7454 if (cu->dwo_unit
7455 && dwarf2_per_objfile->using_index)
7456 {
7457 /* We're in a DWO/DWP file, and we're using .gdb_index.
7458 These cases require special processing. */
ed2dc618 7459 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7460 return lookup_dwo_signatured_type (cu, sig);
7461 else
7462 return lookup_dwp_signatured_type (cu, sig);
7463 }
7464 else
7465 {
7466 struct signatured_type find_entry, *entry;
348e048f 7467
a2ce51a0
DE
7468 if (dwarf2_per_objfile->signatured_types == NULL)
7469 return NULL;
7470 find_entry.signature = sig;
9a3c8263
SM
7471 entry = ((struct signatured_type *)
7472 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7473 return entry;
7474 }
348e048f 7475}
42e7ad6c
DE
7476\f
7477/* Low level DIE reading support. */
348e048f 7478
d85a05f0
DJ
7479/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7480
7481static void
7482init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7483 struct dwarf2_cu *cu,
3019eac3 7484 struct dwarf2_section_info *section,
685af9cd
TT
7485 struct dwo_file *dwo_file,
7486 struct abbrev_table *abbrev_table)
d85a05f0 7487{
fceca515 7488 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7489 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7490 reader->cu = cu;
3019eac3 7491 reader->dwo_file = dwo_file;
dee91e82
DE
7492 reader->die_section = section;
7493 reader->buffer = section->buffer;
f664829e 7494 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7495 reader->comp_dir = NULL;
685af9cd 7496 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7497}
7498
b0c7bfa9
DE
7499/* Subroutine of init_cutu_and_read_dies to simplify it.
7500 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7501 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7502 already.
7503
7504 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7505 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7506 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7507 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7508 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7509 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7510 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7511 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7512 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7513 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7514 kept around for at least as long as *RESULT_READER.
7515
b0c7bfa9
DE
7516 The result is non-zero if a valid (non-dummy) DIE was found. */
7517
7518static int
7519read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7520 struct dwo_unit *dwo_unit,
b0c7bfa9 7521 struct die_info *stub_comp_unit_die,
a2ce51a0 7522 const char *stub_comp_dir,
b0c7bfa9 7523 struct die_reader_specs *result_reader,
d521ce57 7524 const gdb_byte **result_info_ptr,
b0c7bfa9 7525 struct die_info **result_comp_unit_die,
685af9cd
TT
7526 int *result_has_children,
7527 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7528{
ed2dc618 7529 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7530 struct objfile *objfile = dwarf2_per_objfile->objfile;
7531 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7532 bfd *abfd;
d521ce57 7533 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7534 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7535 int i,num_extra_attrs;
7536 struct dwarf2_section_info *dwo_abbrev_section;
7537 struct attribute *attr;
7538 struct die_info *comp_unit_die;
7539
b0aeadb3
DE
7540 /* At most one of these may be provided. */
7541 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7542
b0c7bfa9
DE
7543 /* These attributes aren't processed until later:
7544 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7545 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7546 referenced later. However, these attributes are found in the stub
7547 which we won't have later. In order to not impose this complication
7548 on the rest of the code, we read them here and copy them to the
7549 DWO CU/TU die. */
b0c7bfa9
DE
7550
7551 stmt_list = NULL;
7552 low_pc = NULL;
7553 high_pc = NULL;
7554 ranges = NULL;
7555 comp_dir = NULL;
7556
7557 if (stub_comp_unit_die != NULL)
7558 {
7559 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7560 DWO file. */
7561 if (! this_cu->is_debug_types)
7562 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7563 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7564 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7565 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7566 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7567
7568 /* There should be a DW_AT_addr_base attribute here (if needed).
7569 We need the value before we can process DW_FORM_GNU_addr_index. */
7570 cu->addr_base = 0;
7571 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7572 if (attr)
7573 cu->addr_base = DW_UNSND (attr);
7574
7575 /* There should be a DW_AT_ranges_base attribute here (if needed).
7576 We need the value before we can process DW_AT_ranges. */
7577 cu->ranges_base = 0;
7578 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7579 if (attr)
7580 cu->ranges_base = DW_UNSND (attr);
7581 }
a2ce51a0
DE
7582 else if (stub_comp_dir != NULL)
7583 {
7584 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7585 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7586 comp_dir->name = DW_AT_comp_dir;
7587 comp_dir->form = DW_FORM_string;
7588 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7589 DW_STRING (comp_dir) = stub_comp_dir;
7590 }
b0c7bfa9
DE
7591
7592 /* Set up for reading the DWO CU/TU. */
7593 cu->dwo_unit = dwo_unit;
685af9cd 7594 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7595 dwarf2_read_section (objfile, section);
a32a8923 7596 abfd = get_section_bfd_owner (section);
9c541725
PA
7597 begin_info_ptr = info_ptr = (section->buffer
7598 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7599 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7600
7601 if (this_cu->is_debug_types)
7602 {
b0c7bfa9
DE
7603 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7604
ed2dc618
SM
7605 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7606 &cu->header, section,
b0c7bfa9 7607 dwo_abbrev_section,
43988095 7608 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7609 /* This is not an assert because it can be caused by bad debug info. */
43988095 7610 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7611 {
7612 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7613 " TU at offset 0x%x [in module %s]"),
7614 hex_string (sig_type->signature),
43988095 7615 hex_string (cu->header.signature),
9c541725 7616 to_underlying (dwo_unit->sect_off),
a2ce51a0
DE
7617 bfd_get_filename (abfd));
7618 }
9c541725 7619 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7620 /* For DWOs coming from DWP files, we don't know the CU length
7621 nor the type's offset in the TU until now. */
7622 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7623 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7624
7625 /* Establish the type offset that can be used to lookup the type.
7626 For DWO files, we don't know it until now. */
9c541725
PA
7627 sig_type->type_offset_in_section
7628 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7629 }
7630 else
7631 {
ed2dc618
SM
7632 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7633 &cu->header, section,
b0c7bfa9 7634 dwo_abbrev_section,
43988095 7635 info_ptr, rcuh_kind::COMPILE);
9c541725 7636 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7637 /* For DWOs coming from DWP files, we don't know the CU length
7638 until now. */
7639 dwo_unit->length = get_cu_length (&cu->header);
7640 }
7641
685af9cd
TT
7642 *result_dwo_abbrev_table
7643 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7644 cu->header.abbrev_sect_off);
7645 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7646 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7647
7648 /* Read in the die, but leave space to copy over the attributes
7649 from the stub. This has the benefit of simplifying the rest of
7650 the code - all the work to maintain the illusion of a single
7651 DW_TAG_{compile,type}_unit DIE is done here. */
7652 num_extra_attrs = ((stmt_list != NULL)
7653 + (low_pc != NULL)
7654 + (high_pc != NULL)
7655 + (ranges != NULL)
7656 + (comp_dir != NULL));
7657 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7658 result_has_children, num_extra_attrs);
7659
7660 /* Copy over the attributes from the stub to the DIE we just read in. */
7661 comp_unit_die = *result_comp_unit_die;
7662 i = comp_unit_die->num_attrs;
7663 if (stmt_list != NULL)
7664 comp_unit_die->attrs[i++] = *stmt_list;
7665 if (low_pc != NULL)
7666 comp_unit_die->attrs[i++] = *low_pc;
7667 if (high_pc != NULL)
7668 comp_unit_die->attrs[i++] = *high_pc;
7669 if (ranges != NULL)
7670 comp_unit_die->attrs[i++] = *ranges;
7671 if (comp_dir != NULL)
7672 comp_unit_die->attrs[i++] = *comp_dir;
7673 comp_unit_die->num_attrs += num_extra_attrs;
7674
b4f54984 7675 if (dwarf_die_debug)
bf6af496
DE
7676 {
7677 fprintf_unfiltered (gdb_stdlog,
7678 "Read die from %s@0x%x of %s:\n",
a32a8923 7679 get_section_name (section),
bf6af496
DE
7680 (unsigned) (begin_info_ptr - section->buffer),
7681 bfd_get_filename (abfd));
b4f54984 7682 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7683 }
7684
a2ce51a0
DE
7685 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7686 TUs by skipping the stub and going directly to the entry in the DWO file.
7687 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7688 to get it via circuitous means. Blech. */
7689 if (comp_dir != NULL)
7690 result_reader->comp_dir = DW_STRING (comp_dir);
7691
b0c7bfa9
DE
7692 /* Skip dummy compilation units. */
7693 if (info_ptr >= begin_info_ptr + dwo_unit->length
7694 || peek_abbrev_code (abfd, info_ptr) == 0)
7695 return 0;
7696
7697 *result_info_ptr = info_ptr;
7698 return 1;
7699}
7700
7701/* Subroutine of init_cutu_and_read_dies to simplify it.
7702 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7703 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7704
7705static struct dwo_unit *
7706lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7707 struct die_info *comp_unit_die)
7708{
7709 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7710 ULONGEST signature;
7711 struct dwo_unit *dwo_unit;
7712 const char *comp_dir, *dwo_name;
7713
a2ce51a0
DE
7714 gdb_assert (cu != NULL);
7715
b0c7bfa9 7716 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
7717 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7718 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7719
7720 if (this_cu->is_debug_types)
7721 {
7722 struct signatured_type *sig_type;
7723
7724 /* Since this_cu is the first member of struct signatured_type,
7725 we can go from a pointer to one to a pointer to the other. */
7726 sig_type = (struct signatured_type *) this_cu;
7727 signature = sig_type->signature;
7728 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7729 }
7730 else
7731 {
7732 struct attribute *attr;
7733
7734 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7735 if (! attr)
7736 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7737 " [in module %s]"),
e3b94546 7738 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9
DE
7739 signature = DW_UNSND (attr);
7740 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7741 signature);
7742 }
7743
b0c7bfa9
DE
7744 return dwo_unit;
7745}
7746
a2ce51a0 7747/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7748 See it for a description of the parameters.
fcd3b13d 7749 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7750
7751static void
6aa5f3a6
DE
7752init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7753 int use_existing_cu, int keep,
a2ce51a0
DE
7754 die_reader_func_ftype *die_reader_func,
7755 void *data)
7756{
fcd3b13d 7757 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7758 struct signatured_type *sig_type;
a2ce51a0
DE
7759 struct die_reader_specs reader;
7760 const gdb_byte *info_ptr;
7761 struct die_info *comp_unit_die;
7762 int has_children;
ed2dc618 7763 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7764
7765 /* Verify we can do the following downcast, and that we have the
7766 data we need. */
7767 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7768 sig_type = (struct signatured_type *) this_cu;
7769 gdb_assert (sig_type->dwo_unit != NULL);
7770
6aa5f3a6
DE
7771 if (use_existing_cu && this_cu->cu != NULL)
7772 {
7773 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7774 /* There's no need to do the rereading_dwo_cu handling that
7775 init_cutu_and_read_dies does since we don't read the stub. */
7776 }
7777 else
7778 {
7779 /* If !use_existing_cu, this_cu->cu must be NULL. */
7780 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7781 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7782 }
7783
7784 /* A future optimization, if needed, would be to use an existing
7785 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7786 could share abbrev tables. */
a2ce51a0 7787
685af9cd
TT
7788 /* The abbreviation table used by READER, this must live at least as long as
7789 READER. */
7790 abbrev_table_up dwo_abbrev_table;
7791
a2ce51a0 7792 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7793 NULL /* stub_comp_unit_die */,
7794 sig_type->dwo_unit->dwo_file->comp_dir,
7795 &reader, &info_ptr,
685af9cd
TT
7796 &comp_unit_die, &has_children,
7797 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7798 {
7799 /* Dummy die. */
a2ce51a0
DE
7800 return;
7801 }
7802
7803 /* All the "real" work is done here. */
7804 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7805
6aa5f3a6 7806 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7807 but the alternative is making the latter more complex.
7808 This function is only for the special case of using DWO files directly:
7809 no point in overly complicating the general case just to handle this. */
fcd3b13d 7810 if (new_cu != NULL && keep)
a2ce51a0 7811 {
fcd3b13d
SM
7812 /* Link this CU into read_in_chain. */
7813 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7814 dwarf2_per_objfile->read_in_chain = this_cu;
7815 /* The chain owns it now. */
7816 new_cu.release ();
a2ce51a0 7817 }
a2ce51a0
DE
7818}
7819
fd820528 7820/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7821 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7822
f4dc4d17
DE
7823 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7824 Otherwise the table specified in the comp unit header is read in and used.
7825 This is an optimization for when we already have the abbrev table.
7826
dee91e82
DE
7827 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7828 Otherwise, a new CU is allocated with xmalloc.
7829
7830 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7831 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7832
7833 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7834 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7835
70221824 7836static void
fd820528 7837init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7838 struct abbrev_table *abbrev_table,
fd820528
DE
7839 int use_existing_cu, int keep,
7840 die_reader_func_ftype *die_reader_func,
7841 void *data)
c906108c 7842{
ed2dc618 7843 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7844 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7845 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7846 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7847 struct dwarf2_cu *cu;
d521ce57 7848 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7849 struct die_reader_specs reader;
d85a05f0 7850 struct die_info *comp_unit_die;
dee91e82 7851 int has_children;
d85a05f0 7852 struct attribute *attr;
dee91e82 7853 struct signatured_type *sig_type = NULL;
4bdcc0c1 7854 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7855 /* Non-zero if CU currently points to a DWO file and we need to
7856 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7857 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7858 int rereading_dwo_cu = 0;
c906108c 7859
b4f54984 7860 if (dwarf_die_debug)
09406207
DE
7861 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7862 this_cu->is_debug_types ? "type" : "comp",
9c541725 7863 to_underlying (this_cu->sect_off));
09406207 7864
dee91e82
DE
7865 if (use_existing_cu)
7866 gdb_assert (keep);
23745b47 7867
a2ce51a0
DE
7868 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7869 file (instead of going through the stub), short-circuit all of this. */
7870 if (this_cu->reading_dwo_directly)
7871 {
7872 /* Narrow down the scope of possibilities to have to understand. */
7873 gdb_assert (this_cu->is_debug_types);
7874 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7875 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7876 die_reader_func, data);
a2ce51a0
DE
7877 return;
7878 }
7879
dee91e82
DE
7880 /* This is cheap if the section is already read in. */
7881 dwarf2_read_section (objfile, section);
7882
9c541725 7883 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7884
7885 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7886
fcd3b13d 7887 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7888 if (use_existing_cu && this_cu->cu != NULL)
7889 {
7890 cu = this_cu->cu;
42e7ad6c
DE
7891 /* If this CU is from a DWO file we need to start over, we need to
7892 refetch the attributes from the skeleton CU.
7893 This could be optimized by retrieving those attributes from when we
7894 were here the first time: the previous comp_unit_die was stored in
7895 comp_unit_obstack. But there's no data yet that we need this
7896 optimization. */
7897 if (cu->dwo_unit != NULL)
7898 rereading_dwo_cu = 1;
dee91e82
DE
7899 }
7900 else
7901 {
7902 /* If !use_existing_cu, this_cu->cu must be NULL. */
7903 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7904 new_cu.reset (new dwarf2_cu (this_cu));
7905 cu = new_cu.get ();
42e7ad6c 7906 }
dee91e82 7907
b0c7bfa9 7908 /* Get the header. */
9c541725 7909 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7910 {
7911 /* We already have the header, there's no need to read it in again. */
9c541725 7912 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7913 }
7914 else
7915 {
3019eac3 7916 if (this_cu->is_debug_types)
dee91e82 7917 {
ed2dc618
SM
7918 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7919 &cu->header, section,
4bdcc0c1 7920 abbrev_section, info_ptr,
43988095 7921 rcuh_kind::TYPE);
dee91e82 7922
42e7ad6c
DE
7923 /* Since per_cu is the first member of struct signatured_type,
7924 we can go from a pointer to one to a pointer to the other. */
7925 sig_type = (struct signatured_type *) this_cu;
43988095 7926 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7927 gdb_assert (sig_type->type_offset_in_tu
7928 == cu->header.type_cu_offset_in_tu);
7929 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7930
42e7ad6c
DE
7931 /* LENGTH has not been set yet for type units if we're
7932 using .gdb_index. */
1ce1cefd 7933 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7934
7935 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7936 sig_type->type_offset_in_section =
7937 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7938
7939 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7940 }
7941 else
7942 {
ed2dc618
SM
7943 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7944 &cu->header, section,
4bdcc0c1 7945 abbrev_section,
43988095
JK
7946 info_ptr,
7947 rcuh_kind::COMPILE);
dee91e82 7948
9c541725 7949 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7950 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7951 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7952 }
7953 }
10b3939b 7954
6caca83c 7955 /* Skip dummy compilation units. */
dee91e82 7956 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7957 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7958 return;
6caca83c 7959
433df2d4
DE
7960 /* If we don't have them yet, read the abbrevs for this compilation unit.
7961 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7962 done (own the table through ABBREV_TABLE_HOLDER). */
7963 abbrev_table_up abbrev_table_holder;
f4dc4d17 7964 if (abbrev_table != NULL)
685af9cd
TT
7965 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7966 else
f4dc4d17 7967 {
685af9cd
TT
7968 abbrev_table_holder
7969 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7970 cu->header.abbrev_sect_off);
7971 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7972 }
af703f96 7973
dee91e82 7974 /* Read the top level CU/TU die. */
685af9cd 7975 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7976 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7977
b0c7bfa9 7978 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7979 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7980 table from the DWO file and pass the ownership over to us. It will be
7981 referenced from READER, so we must make sure to free it after we're done
7982 with READER.
7983
b0c7bfa9
DE
7984 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7985 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3 7986 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
685af9cd 7987 abbrev_table_up dwo_abbrev_table;
3019eac3
DE
7988 if (attr)
7989 {
3019eac3 7990 struct dwo_unit *dwo_unit;
b0c7bfa9 7991 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7992
7993 if (has_children)
6a506a2d
DE
7994 {
7995 complaint (&symfile_complaints,
7996 _("compilation unit with DW_AT_GNU_dwo_name"
7997 " has children (offset 0x%x) [in module %s]"),
9c541725 7998 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6a506a2d 7999 }
b0c7bfa9 8000 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 8001 if (dwo_unit != NULL)
3019eac3 8002 {
6a506a2d 8003 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 8004 comp_unit_die, NULL,
6a506a2d 8005 &reader, &info_ptr,
685af9cd
TT
8006 &dwo_comp_unit_die, &has_children,
8007 &dwo_abbrev_table) == 0)
6a506a2d
DE
8008 {
8009 /* Dummy die. */
6a506a2d
DE
8010 return;
8011 }
8012 comp_unit_die = dwo_comp_unit_die;
8013 }
8014 else
8015 {
8016 /* Yikes, we couldn't find the rest of the DIE, we only have
8017 the stub. A complaint has already been logged. There's
8018 not much more we can do except pass on the stub DIE to
8019 die_reader_func. We don't want to throw an error on bad
8020 debug info. */
3019eac3
DE
8021 }
8022 }
8023
b0c7bfa9 8024 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
8025 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8026
b0c7bfa9 8027 /* Done, clean up. */
fcd3b13d 8028 if (new_cu != NULL && keep)
348e048f 8029 {
fcd3b13d
SM
8030 /* Link this CU into read_in_chain. */
8031 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8032 dwarf2_per_objfile->read_in_chain = this_cu;
8033 /* The chain owns it now. */
8034 new_cu.release ();
348e048f 8035 }
dee91e82
DE
8036}
8037
33e80786
DE
8038/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
8039 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
8040 to have already done the lookup to find the DWO file).
dee91e82
DE
8041
8042 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 8043 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
8044
8045 We fill in THIS_CU->length.
8046
8047 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
8048 linker) then DIE_READER_FUNC will not get called.
8049
8050 THIS_CU->cu is always freed when done.
3019eac3
DE
8051 This is done in order to not leave THIS_CU->cu in a state where we have
8052 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
8053
8054static void
8055init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 8056 struct dwo_file *dwo_file,
dee91e82
DE
8057 die_reader_func_ftype *die_reader_func,
8058 void *data)
8059{
ed2dc618 8060 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 8061 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 8062 struct dwarf2_section_info *section = this_cu->section;
a32a8923 8063 bfd *abfd = get_section_bfd_owner (section);
33e80786 8064 struct dwarf2_section_info *abbrev_section;
d521ce57 8065 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 8066 struct die_reader_specs reader;
dee91e82
DE
8067 struct die_info *comp_unit_die;
8068 int has_children;
8069
b4f54984 8070 if (dwarf_die_debug)
09406207
DE
8071 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
8072 this_cu->is_debug_types ? "type" : "comp",
9c541725 8073 to_underlying (this_cu->sect_off));
09406207 8074
dee91e82
DE
8075 gdb_assert (this_cu->cu == NULL);
8076
33e80786
DE
8077 abbrev_section = (dwo_file != NULL
8078 ? &dwo_file->sections.abbrev
8079 : get_abbrev_section_for_cu (this_cu));
8080
dee91e82
DE
8081 /* This is cheap if the section is already read in. */
8082 dwarf2_read_section (objfile, section);
8083
fcd3b13d 8084 struct dwarf2_cu cu (this_cu);
dee91e82 8085
9c541725 8086 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
8087 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8088 &cu.header, section,
4bdcc0c1 8089 abbrev_section, info_ptr,
43988095
JK
8090 (this_cu->is_debug_types
8091 ? rcuh_kind::TYPE
8092 : rcuh_kind::COMPILE));
dee91e82 8093
1ce1cefd 8094 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
8095
8096 /* Skip dummy compilation units. */
8097 if (info_ptr >= begin_info_ptr + this_cu->length
8098 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 8099 return;
72bf9492 8100
685af9cd
TT
8101 abbrev_table_up abbrev_table
8102 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8103 cu.header.abbrev_sect_off);
dee91e82 8104
685af9cd 8105 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
8106 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8107
8108 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
8109}
8110
3019eac3
DE
8111/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8112 does not lookup the specified DWO file.
8113 This cannot be used to read DWO files.
dee91e82
DE
8114
8115 THIS_CU->cu is always freed when done.
3019eac3
DE
8116 This is done in order to not leave THIS_CU->cu in a state where we have
8117 to care whether it refers to the "main" CU or the DWO CU.
8118 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
8119
8120static void
8121init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8122 die_reader_func_ftype *die_reader_func,
8123 void *data)
8124{
33e80786 8125 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 8126}
0018ea6f
DE
8127\f
8128/* Type Unit Groups.
dee91e82 8129
0018ea6f
DE
8130 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8131 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8132 so that all types coming from the same compilation (.o file) are grouped
8133 together. A future step could be to put the types in the same symtab as
8134 the CU the types ultimately came from. */
ff013f42 8135
f4dc4d17
DE
8136static hashval_t
8137hash_type_unit_group (const void *item)
8138{
9a3c8263
SM
8139 const struct type_unit_group *tu_group
8140 = (const struct type_unit_group *) item;
f4dc4d17 8141
094b34ac 8142 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 8143}
348e048f
DE
8144
8145static int
f4dc4d17 8146eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 8147{
9a3c8263
SM
8148 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8149 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 8150
094b34ac 8151 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 8152}
348e048f 8153
f4dc4d17
DE
8154/* Allocate a hash table for type unit groups. */
8155
8156static htab_t
ed2dc618 8157allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
8158{
8159 return htab_create_alloc_ex (3,
8160 hash_type_unit_group,
8161 eq_type_unit_group,
8162 NULL,
ed2dc618 8163 &objfile->objfile_obstack,
f4dc4d17
DE
8164 hashtab_obstack_allocate,
8165 dummy_obstack_deallocate);
8166}
dee91e82 8167
f4dc4d17
DE
8168/* Type units that don't have DW_AT_stmt_list are grouped into their own
8169 partial symtabs. We combine several TUs per psymtab to not let the size
8170 of any one psymtab grow too big. */
8171#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8172#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 8173
094b34ac 8174/* Helper routine for get_type_unit_group.
f4dc4d17
DE
8175 Create the type_unit_group object used to hold one or more TUs. */
8176
8177static struct type_unit_group *
094b34ac 8178create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 8179{
518817b3
SM
8180 struct dwarf2_per_objfile *dwarf2_per_objfile
8181 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 8182 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 8183 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 8184 struct type_unit_group *tu_group;
f4dc4d17
DE
8185
8186 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8187 struct type_unit_group);
094b34ac 8188 per_cu = &tu_group->per_cu;
518817b3 8189 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 8190
094b34ac
DE
8191 if (dwarf2_per_objfile->using_index)
8192 {
8193 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8194 struct dwarf2_per_cu_quick_data);
094b34ac
DE
8195 }
8196 else
8197 {
9c541725 8198 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
8199 struct partial_symtab *pst;
8200 char *name;
8201
8202 /* Give the symtab a useful name for debug purposes. */
8203 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8204 name = xstrprintf ("<type_units_%d>",
8205 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8206 else
8207 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8208
8209 pst = create_partial_symtab (per_cu, name);
8210 pst->anonymous = 1;
f4dc4d17 8211
094b34ac
DE
8212 xfree (name);
8213 }
f4dc4d17 8214
094b34ac 8215 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 8216 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
8217
8218 return tu_group;
8219}
8220
094b34ac
DE
8221/* Look up the type_unit_group for type unit CU, and create it if necessary.
8222 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
8223
8224static struct type_unit_group *
ff39bb5e 8225get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 8226{
518817b3
SM
8227 struct dwarf2_per_objfile *dwarf2_per_objfile
8228 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8229 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8230 struct type_unit_group *tu_group;
8231 void **slot;
8232 unsigned int line_offset;
8233 struct type_unit_group type_unit_group_for_lookup;
8234
8235 if (dwarf2_per_objfile->type_unit_groups == NULL)
8236 {
8237 dwarf2_per_objfile->type_unit_groups =
ed2dc618 8238 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
8239 }
8240
8241 /* Do we need to create a new group, or can we use an existing one? */
8242
8243 if (stmt_list)
8244 {
8245 line_offset = DW_UNSND (stmt_list);
8246 ++tu_stats->nr_symtab_sharers;
8247 }
8248 else
8249 {
8250 /* Ugh, no stmt_list. Rare, but we have to handle it.
8251 We can do various things here like create one group per TU or
8252 spread them over multiple groups to split up the expansion work.
8253 To avoid worst case scenarios (too many groups or too large groups)
8254 we, umm, group them in bunches. */
8255 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8256 | (tu_stats->nr_stmt_less_type_units
8257 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8258 ++tu_stats->nr_stmt_less_type_units;
8259 }
8260
094b34ac 8261 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 8262 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
8263 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8264 &type_unit_group_for_lookup, INSERT);
8265 if (*slot != NULL)
8266 {
9a3c8263 8267 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
8268 gdb_assert (tu_group != NULL);
8269 }
8270 else
8271 {
9c541725 8272 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 8273 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
8274 *slot = tu_group;
8275 ++tu_stats->nr_symtabs;
8276 }
8277
8278 return tu_group;
8279}
0018ea6f
DE
8280\f
8281/* Partial symbol tables. */
8282
8283/* Create a psymtab named NAME and assign it to PER_CU.
8284
8285 The caller must fill in the following details:
8286 dirname, textlow, texthigh. */
8287
8288static struct partial_symtab *
8289create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8290{
e3b94546 8291 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
8292 struct partial_symtab *pst;
8293
18a94d75 8294 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
8295 objfile->global_psymbols,
8296 objfile->static_psymbols);
0018ea6f
DE
8297
8298 pst->psymtabs_addrmap_supported = 1;
8299
8300 /* This is the glue that links PST into GDB's symbol API. */
8301 pst->read_symtab_private = per_cu;
8302 pst->read_symtab = dwarf2_read_symtab;
8303 per_cu->v.psymtab = pst;
8304
8305 return pst;
8306}
8307
b93601f3
TT
8308/* The DATA object passed to process_psymtab_comp_unit_reader has this
8309 type. */
8310
8311struct process_psymtab_comp_unit_data
8312{
8313 /* True if we are reading a DW_TAG_partial_unit. */
8314
8315 int want_partial_unit;
8316
8317 /* The "pretend" language that is used if the CU doesn't declare a
8318 language. */
8319
8320 enum language pretend_language;
8321};
8322
0018ea6f
DE
8323/* die_reader_func for process_psymtab_comp_unit. */
8324
8325static void
8326process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8327 const gdb_byte *info_ptr,
0018ea6f
DE
8328 struct die_info *comp_unit_die,
8329 int has_children,
8330 void *data)
8331{
8332 struct dwarf2_cu *cu = reader->cu;
518817b3 8333 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 8334 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8335 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8336 CORE_ADDR baseaddr;
8337 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8338 struct partial_symtab *pst;
3a2b436a 8339 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8340 const char *filename;
9a3c8263
SM
8341 struct process_psymtab_comp_unit_data *info
8342 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 8343
b93601f3 8344 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
8345 return;
8346
8347 gdb_assert (! per_cu->is_debug_types);
8348
b93601f3 8349 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
8350
8351 cu->list_in_scope = &file_symbols;
8352
8353 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8354 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8355 if (filename == NULL)
0018ea6f 8356 filename = "";
0018ea6f
DE
8357
8358 pst = create_partial_symtab (per_cu, filename);
8359
8360 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8361 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
8362
8363 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8364
8365 dwarf2_find_base_address (comp_unit_die, cu);
8366
8367 /* Possibly set the default values of LOWPC and HIGHPC from
8368 `DW_AT_ranges'. */
3a2b436a
JK
8369 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8370 &best_highpc, cu, pst);
8371 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
8372 /* Store the contiguous range if it is not empty; it can be empty for
8373 CUs with no code. */
8374 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
8375 gdbarch_adjust_dwarf2_addr (gdbarch,
8376 best_lowpc + baseaddr),
8377 gdbarch_adjust_dwarf2_addr (gdbarch,
8378 best_highpc + baseaddr) - 1,
8379 pst);
0018ea6f
DE
8380
8381 /* Check if comp unit has_children.
8382 If so, read the rest of the partial symbols from this comp unit.
8383 If not, there's no more debug_info for this comp unit. */
8384 if (has_children)
8385 {
8386 struct partial_die_info *first_die;
8387 CORE_ADDR lowpc, highpc;
8388
8389 lowpc = ((CORE_ADDR) -1);
8390 highpc = ((CORE_ADDR) 0);
8391
8392 first_die = load_partial_dies (reader, info_ptr, 1);
8393
8394 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8395 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8396
8397 /* If we didn't find a lowpc, set it to highpc to avoid
8398 complaints from `maint check'. */
8399 if (lowpc == ((CORE_ADDR) -1))
8400 lowpc = highpc;
8401
8402 /* If the compilation unit didn't have an explicit address range,
8403 then use the information extracted from its child dies. */
e385593e 8404 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8405 {
8406 best_lowpc = lowpc;
8407 best_highpc = highpc;
8408 }
8409 }
3e29f34a
MR
8410 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8411 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 8412
8763cede 8413 end_psymtab_common (objfile, pst);
0018ea6f
DE
8414
8415 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8416 {
8417 int i;
8418 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8419 struct dwarf2_per_cu_data *iter;
8420
8421 /* Fill in 'dependencies' here; we fill in 'users' in a
8422 post-pass. */
8423 pst->number_of_dependencies = len;
8d749320
SM
8424 pst->dependencies =
8425 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
8426 for (i = 0;
8427 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8428 i, iter);
8429 ++i)
8430 pst->dependencies[i] = iter->v.psymtab;
8431
8432 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8433 }
8434
8435 /* Get the list of files included in the current compilation unit,
8436 and build a psymtab for each of them. */
8437 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8438
b4f54984 8439 if (dwarf_read_debug)
0018ea6f
DE
8440 {
8441 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8442
8443 fprintf_unfiltered (gdb_stdlog,
8444 "Psymtab for %s unit @0x%x: %s - %s"
8445 ", %d global, %d static syms\n",
8446 per_cu->is_debug_types ? "type" : "comp",
9c541725 8447 to_underlying (per_cu->sect_off),
0018ea6f
DE
8448 paddress (gdbarch, pst->textlow),
8449 paddress (gdbarch, pst->texthigh),
8450 pst->n_global_syms, pst->n_static_syms);
8451 }
8452}
8453
8454/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8455 Process compilation unit THIS_CU for a psymtab. */
8456
8457static void
8458process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8459 int want_partial_unit,
8460 enum language pretend_language)
0018ea6f
DE
8461{
8462 /* If this compilation unit was already read in, free the
8463 cached copy in order to read it in again. This is
8464 necessary because we skipped some symbols when we first
8465 read in the compilation unit (see load_partial_dies).
8466 This problem could be avoided, but the benefit is unclear. */
8467 if (this_cu->cu != NULL)
8468 free_one_cached_comp_unit (this_cu);
8469
f1902523
JK
8470 if (this_cu->is_debug_types)
8471 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8472 NULL);
8473 else
8474 {
8475 process_psymtab_comp_unit_data info;
8476 info.want_partial_unit = want_partial_unit;
8477 info.pretend_language = pretend_language;
8478 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8479 process_psymtab_comp_unit_reader, &info);
8480 }
0018ea6f
DE
8481
8482 /* Age out any secondary CUs. */
ed2dc618 8483 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8484}
f4dc4d17
DE
8485
8486/* Reader function for build_type_psymtabs. */
8487
8488static void
8489build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8490 const gdb_byte *info_ptr,
f4dc4d17
DE
8491 struct die_info *type_unit_die,
8492 int has_children,
8493 void *data)
8494{
ed2dc618 8495 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8496 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8497 struct objfile *objfile = dwarf2_per_objfile->objfile;
8498 struct dwarf2_cu *cu = reader->cu;
8499 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8500 struct signatured_type *sig_type;
f4dc4d17
DE
8501 struct type_unit_group *tu_group;
8502 struct attribute *attr;
8503 struct partial_die_info *first_die;
8504 CORE_ADDR lowpc, highpc;
8505 struct partial_symtab *pst;
8506
8507 gdb_assert (data == NULL);
0186c6a7
DE
8508 gdb_assert (per_cu->is_debug_types);
8509 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8510
8511 if (! has_children)
8512 return;
8513
8514 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8515 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8516
0186c6a7 8517 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
8518
8519 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8520 cu->list_in_scope = &file_symbols;
8521 pst = create_partial_symtab (per_cu, "");
8522 pst->anonymous = 1;
8523
8524 first_die = load_partial_dies (reader, info_ptr, 1);
8525
8526 lowpc = (CORE_ADDR) -1;
8527 highpc = (CORE_ADDR) 0;
8528 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8529
8763cede 8530 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8531}
8532
73051182
DE
8533/* Struct used to sort TUs by their abbreviation table offset. */
8534
8535struct tu_abbrev_offset
8536{
8537 struct signatured_type *sig_type;
8538 sect_offset abbrev_offset;
8539};
8540
8541/* Helper routine for build_type_psymtabs_1, passed to qsort. */
8542
8543static int
8544sort_tu_by_abbrev_offset (const void *ap, const void *bp)
8545{
9a3c8263
SM
8546 const struct tu_abbrev_offset * const *a
8547 = (const struct tu_abbrev_offset * const*) ap;
8548 const struct tu_abbrev_offset * const *b
8549 = (const struct tu_abbrev_offset * const*) bp;
9c541725
PA
8550 sect_offset aoff = (*a)->abbrev_offset;
8551 sect_offset boff = (*b)->abbrev_offset;
73051182
DE
8552
8553 return (aoff > boff) - (aoff < boff);
8554}
8555
8556/* Efficiently read all the type units.
8557 This does the bulk of the work for build_type_psymtabs.
8558
8559 The efficiency is because we sort TUs by the abbrev table they use and
8560 only read each abbrev table once. In one program there are 200K TUs
8561 sharing 8K abbrev tables.
8562
8563 The main purpose of this function is to support building the
8564 dwarf2_per_objfile->type_unit_groups table.
8565 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8566 can collapse the search space by grouping them by stmt_list.
8567 The savings can be significant, in the same program from above the 200K TUs
8568 share 8K stmt_list tables.
8569
8570 FUNC is expected to call get_type_unit_group, which will create the
8571 struct type_unit_group if necessary and add it to
8572 dwarf2_per_objfile->type_unit_groups. */
8573
8574static void
ed2dc618 8575build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8576{
73051182
DE
8577 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8578 struct cleanup *cleanups;
685af9cd 8579 abbrev_table_up abbrev_table;
73051182
DE
8580 sect_offset abbrev_offset;
8581 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
8582 int i;
8583
8584 /* It's up to the caller to not call us multiple times. */
8585 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8586
8587 if (dwarf2_per_objfile->n_type_units == 0)
8588 return;
8589
8590 /* TUs typically share abbrev tables, and there can be way more TUs than
8591 abbrev tables. Sort by abbrev table to reduce the number of times we
8592 read each abbrev table in.
8593 Alternatives are to punt or to maintain a cache of abbrev tables.
8594 This is simpler and efficient enough for now.
8595
8596 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8597 symtab to use). Typically TUs with the same abbrev offset have the same
8598 stmt_list value too so in practice this should work well.
8599
8600 The basic algorithm here is:
8601
8602 sort TUs by abbrev table
8603 for each TU with same abbrev table:
8604 read abbrev table if first user
8605 read TU top level DIE
8606 [IWBN if DWO skeletons had DW_AT_stmt_list]
8607 call FUNC */
8608
b4f54984 8609 if (dwarf_read_debug)
73051182
DE
8610 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8611
8612 /* Sort in a separate table to maintain the order of all_type_units
8613 for .gdb_index: TU indices directly index all_type_units. */
8614 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
8615 dwarf2_per_objfile->n_type_units);
8616 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8617 {
8618 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8619
8620 sorted_by_abbrev[i].sig_type = sig_type;
8621 sorted_by_abbrev[i].abbrev_offset =
ed2dc618
SM
8622 read_abbrev_offset (dwarf2_per_objfile,
8623 sig_type->per_cu.section,
9c541725 8624 sig_type->per_cu.sect_off);
73051182
DE
8625 }
8626 cleanups = make_cleanup (xfree, sorted_by_abbrev);
8627 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
8628 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
8629
9c541725 8630 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
8631
8632 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8633 {
8634 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8635
8636 /* Switch to the next abbrev table if necessary. */
8637 if (abbrev_table == NULL
9c541725 8638 || tu->abbrev_offset != abbrev_offset)
73051182 8639 {
73051182
DE
8640 abbrev_offset = tu->abbrev_offset;
8641 abbrev_table =
ed2dc618
SM
8642 abbrev_table_read_table (dwarf2_per_objfile,
8643 &dwarf2_per_objfile->abbrev,
73051182
DE
8644 abbrev_offset);
8645 ++tu_stats->nr_uniq_abbrev_tables;
8646 }
8647
685af9cd
TT
8648 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8649 0, 0, build_type_psymtabs_reader, NULL);
73051182
DE
8650 }
8651
73051182 8652 do_cleanups (cleanups);
6aa5f3a6 8653}
73051182 8654
6aa5f3a6
DE
8655/* Print collected type unit statistics. */
8656
8657static void
ed2dc618 8658print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8659{
8660 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8661
8662 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8663 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8664 dwarf2_per_objfile->n_type_units);
8665 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8666 tu_stats->nr_uniq_abbrev_tables);
8667 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8668 tu_stats->nr_symtabs);
8669 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8670 tu_stats->nr_symtab_sharers);
8671 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8672 tu_stats->nr_stmt_less_type_units);
8673 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8674 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8675}
8676
f4dc4d17
DE
8677/* Traversal function for build_type_psymtabs. */
8678
8679static int
8680build_type_psymtab_dependencies (void **slot, void *info)
8681{
ed2dc618
SM
8682 struct dwarf2_per_objfile *dwarf2_per_objfile
8683 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8684 struct objfile *objfile = dwarf2_per_objfile->objfile;
8685 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8686 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8687 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
8688 int len = VEC_length (sig_type_ptr, tu_group->tus);
8689 struct signatured_type *iter;
f4dc4d17
DE
8690 int i;
8691
8692 gdb_assert (len > 0);
0186c6a7 8693 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8694
8695 pst->number_of_dependencies = len;
8d749320
SM
8696 pst->dependencies =
8697 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 8698 for (i = 0;
0186c6a7 8699 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
8700 ++i)
8701 {
0186c6a7
DE
8702 gdb_assert (iter->per_cu.is_debug_types);
8703 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8704 iter->type_unit_group = tu_group;
f4dc4d17
DE
8705 }
8706
0186c6a7 8707 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
8708
8709 return 1;
8710}
8711
8712/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8713 Build partial symbol tables for the .debug_types comp-units. */
8714
8715static void
ed2dc618 8716build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8717{
ed2dc618 8718 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8719 return;
8720
ed2dc618 8721 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8722}
f4dc4d17 8723
6aa5f3a6
DE
8724/* Traversal function for process_skeletonless_type_unit.
8725 Read a TU in a DWO file and build partial symbols for it. */
8726
8727static int
8728process_skeletonless_type_unit (void **slot, void *info)
8729{
8730 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8731 struct dwarf2_per_objfile *dwarf2_per_objfile
8732 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8733 struct signatured_type find_entry, *entry;
8734
8735 /* If this TU doesn't exist in the global table, add it and read it in. */
8736
8737 if (dwarf2_per_objfile->signatured_types == NULL)
8738 {
8739 dwarf2_per_objfile->signatured_types
ed2dc618 8740 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8741 }
8742
8743 find_entry.signature = dwo_unit->signature;
8744 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8745 INSERT);
8746 /* If we've already seen this type there's nothing to do. What's happening
8747 is we're doing our own version of comdat-folding here. */
8748 if (*slot != NULL)
8749 return 1;
8750
8751 /* This does the job that create_all_type_units would have done for
8752 this TU. */
ed2dc618
SM
8753 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8754 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8755 *slot = entry;
8756
8757 /* This does the job that build_type_psymtabs_1 would have done. */
8758 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8759 build_type_psymtabs_reader, NULL);
8760
8761 return 1;
8762}
8763
8764/* Traversal function for process_skeletonless_type_units. */
8765
8766static int
8767process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8768{
8769 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8770
8771 if (dwo_file->tus != NULL)
8772 {
8773 htab_traverse_noresize (dwo_file->tus,
8774 process_skeletonless_type_unit, info);
8775 }
8776
8777 return 1;
8778}
8779
8780/* Scan all TUs of DWO files, verifying we've processed them.
8781 This is needed in case a TU was emitted without its skeleton.
8782 Note: This can't be done until we know what all the DWO files are. */
8783
8784static void
ed2dc618 8785process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8786{
8787 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8788 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8789 && dwarf2_per_objfile->dwo_files != NULL)
8790 {
8791 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8792 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8793 dwarf2_per_objfile);
6aa5f3a6 8794 }
348e048f
DE
8795}
8796
ed2dc618 8797/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8798
8799static void
ed2dc618 8800set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad
TT
8801{
8802 int i;
8803
8804 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8805 {
ed2dc618 8806 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
95554aad
TT
8807 struct partial_symtab *pst = per_cu->v.psymtab;
8808 int j;
8809
36586728
TT
8810 if (pst == NULL)
8811 continue;
8812
95554aad
TT
8813 for (j = 0; j < pst->number_of_dependencies; ++j)
8814 {
8815 /* Set the 'user' field only if it is not already set. */
8816 if (pst->dependencies[j]->user == NULL)
8817 pst->dependencies[j]->user = pst;
8818 }
8819 }
8820}
8821
93311388
DE
8822/* Build the partial symbol table by doing a quick pass through the
8823 .debug_info and .debug_abbrev sections. */
72bf9492 8824
93311388 8825static void
ed2dc618 8826dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8827{
791afaa2 8828 struct cleanup *back_to;
21b2bd31 8829 int i;
ed2dc618 8830 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8831
b4f54984 8832 if (dwarf_read_debug)
45cfd468
DE
8833 {
8834 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8835 objfile_name (objfile));
45cfd468
DE
8836 }
8837
98bfdba5
PA
8838 dwarf2_per_objfile->reading_partial_symbols = 1;
8839
be391dca 8840 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8841
93311388
DE
8842 /* Any cached compilation units will be linked by the per-objfile
8843 read_in_chain. Make sure to free them when we're done. */
ed2dc618 8844 back_to = make_cleanup (free_cached_comp_units, dwarf2_per_objfile);
72bf9492 8845
ed2dc618 8846 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8847
ed2dc618 8848 create_all_comp_units (dwarf2_per_objfile);
c906108c 8849
60606b2c
TT
8850 /* Create a temporary address map on a temporary obstack. We later
8851 copy this to the final obstack. */
8268c778 8852 auto_obstack temp_obstack;
791afaa2
TT
8853
8854 scoped_restore save_psymtabs_addrmap
8855 = make_scoped_restore (&objfile->psymtabs_addrmap,
8856 addrmap_create_mutable (&temp_obstack));
72bf9492 8857
21b2bd31 8858 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 8859 {
ed2dc618 8860 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
aaa75496 8861
b93601f3 8862 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 8863 }
ff013f42 8864
6aa5f3a6 8865 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8866 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8867
8868 /* Now that all TUs have been processed we can fill in the dependencies. */
8869 if (dwarf2_per_objfile->type_unit_groups != NULL)
8870 {
8871 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8872 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8873 }
8874
b4f54984 8875 if (dwarf_read_debug)
ed2dc618 8876 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8877
ed2dc618 8878 set_partial_user (dwarf2_per_objfile);
95554aad 8879
ff013f42
JK
8880 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8881 &objfile->objfile_obstack);
791afaa2
TT
8882 /* At this point we want to keep the address map. */
8883 save_psymtabs_addrmap.release ();
ff013f42 8884
ae038cb0 8885 do_cleanups (back_to);
45cfd468 8886
b4f54984 8887 if (dwarf_read_debug)
45cfd468 8888 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8889 objfile_name (objfile));
ae038cb0
DJ
8890}
8891
3019eac3 8892/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8893
8894static void
dee91e82 8895load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8896 const gdb_byte *info_ptr,
dee91e82
DE
8897 struct die_info *comp_unit_die,
8898 int has_children,
8899 void *data)
ae038cb0 8900{
dee91e82 8901 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8902
95554aad 8903 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8904
ae038cb0
DJ
8905 /* Check if comp unit has_children.
8906 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8907 If not, there's no more debug_info for this comp unit. */
d85a05f0 8908 if (has_children)
dee91e82
DE
8909 load_partial_dies (reader, info_ptr, 0);
8910}
98bfdba5 8911
dee91e82
DE
8912/* Load the partial DIEs for a secondary CU into memory.
8913 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8914
dee91e82
DE
8915static void
8916load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8917{
f4dc4d17
DE
8918 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8919 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8920}
8921
ae038cb0 8922static void
ed2dc618 8923read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8924 struct dwarf2_section_info *section,
f1902523 8925 struct dwarf2_section_info *abbrev_section,
36586728
TT
8926 unsigned int is_dwz,
8927 int *n_allocated,
8928 int *n_comp_units,
8929 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 8930{
d521ce57 8931 const gdb_byte *info_ptr;
ed2dc618 8932 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8933
b4f54984 8934 if (dwarf_read_debug)
bf6af496 8935 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8936 get_section_name (section),
8937 get_section_file_name (section));
bf6af496 8938
36586728 8939 dwarf2_read_section (objfile, section);
ae038cb0 8940
36586728 8941 info_ptr = section->buffer;
6e70227d 8942
36586728 8943 while (info_ptr < section->buffer + section->size)
ae038cb0 8944 {
ae038cb0 8945 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8946
9c541725 8947 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8948
f1902523 8949 comp_unit_head cu_header;
ed2dc618
SM
8950 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8951 abbrev_section, info_ptr,
8952 rcuh_kind::COMPILE);
ae038cb0
DJ
8953
8954 /* Save the compilation unit for later lookup. */
f1902523
JK
8955 if (cu_header.unit_type != DW_UT_type)
8956 {
8957 this_cu = XOBNEW (&objfile->objfile_obstack,
8958 struct dwarf2_per_cu_data);
8959 memset (this_cu, 0, sizeof (*this_cu));
8960 }
8961 else
8962 {
8963 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8964 struct signatured_type);
8965 memset (sig_type, 0, sizeof (*sig_type));
8966 sig_type->signature = cu_header.signature;
8967 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8968 this_cu = &sig_type->per_cu;
8969 }
8970 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8971 this_cu->sect_off = sect_off;
f1902523 8972 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8973 this_cu->is_dwz = is_dwz;
e3b94546 8974 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8975 this_cu->section = section;
ae038cb0 8976
36586728 8977 if (*n_comp_units == *n_allocated)
ae038cb0 8978 {
36586728 8979 *n_allocated *= 2;
224c3ddb
SM
8980 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
8981 *all_comp_units, *n_allocated);
ae038cb0 8982 }
36586728
TT
8983 (*all_comp_units)[*n_comp_units] = this_cu;
8984 ++*n_comp_units;
ae038cb0
DJ
8985
8986 info_ptr = info_ptr + this_cu->length;
8987 }
36586728
TT
8988}
8989
8990/* Create a list of all compilation units in OBJFILE.
8991 This is only done for -readnow and building partial symtabs. */
8992
8993static void
ed2dc618 8994create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728
TT
8995{
8996 int n_allocated;
8997 int n_comp_units;
8998 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 8999 struct dwz_file *dwz;
ed2dc618 9000 struct objfile *objfile = dwarf2_per_objfile->objfile;
36586728
TT
9001
9002 n_comp_units = 0;
9003 n_allocated = 10;
8d749320 9004 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728 9005
ed2dc618 9006 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
f1902523 9007 &dwarf2_per_objfile->abbrev, 0,
36586728
TT
9008 &n_allocated, &n_comp_units, &all_comp_units);
9009
ed2dc618 9010 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 9011 if (dwz != NULL)
ed2dc618
SM
9012 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
9013 1, &n_allocated, &n_comp_units,
4db1a1dc 9014 &all_comp_units);
ae038cb0 9015
8d749320
SM
9016 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
9017 struct dwarf2_per_cu_data *,
9018 n_comp_units);
ae038cb0
DJ
9019 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
9020 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
9021 xfree (all_comp_units);
9022 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
9023}
9024
5734ee8b 9025/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 9026 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 9027 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
9028 DW_AT_ranges). See the comments of add_partial_subprogram on how
9029 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 9030
72bf9492
DJ
9031static void
9032scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
9033 CORE_ADDR *highpc, int set_addrmap,
9034 struct dwarf2_cu *cu)
c906108c 9035{
72bf9492 9036 struct partial_die_info *pdi;
c906108c 9037
91c24f0a
DC
9038 /* Now, march along the PDI's, descending into ones which have
9039 interesting children but skipping the children of the other ones,
9040 until we reach the end of the compilation unit. */
c906108c 9041
72bf9492 9042 pdi = first_die;
91c24f0a 9043
72bf9492
DJ
9044 while (pdi != NULL)
9045 {
9046 fixup_partial_die (pdi, cu);
c906108c 9047
f55ee35c 9048 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
9049 children, so we need to look at them. Ditto for anonymous
9050 enums. */
933c6fe4 9051
72bf9492 9052 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 9053 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
9054 || pdi->tag == DW_TAG_imported_unit
9055 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 9056 {
72bf9492 9057 switch (pdi->tag)
c906108c
SS
9058 {
9059 case DW_TAG_subprogram:
b1dc1806 9060 case DW_TAG_inlined_subroutine:
cdc07690 9061 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 9062 break;
72929c62 9063 case DW_TAG_constant:
c906108c
SS
9064 case DW_TAG_variable:
9065 case DW_TAG_typedef:
91c24f0a 9066 case DW_TAG_union_type:
72bf9492 9067 if (!pdi->is_declaration)
63d06c5c 9068 {
72bf9492 9069 add_partial_symbol (pdi, cu);
63d06c5c
DC
9070 }
9071 break;
c906108c 9072 case DW_TAG_class_type:
680b30c7 9073 case DW_TAG_interface_type:
c906108c 9074 case DW_TAG_structure_type:
72bf9492 9075 if (!pdi->is_declaration)
c906108c 9076 {
72bf9492 9077 add_partial_symbol (pdi, cu);
c906108c 9078 }
e98c9e7c
TT
9079 if (cu->language == language_rust && pdi->has_children)
9080 scan_partial_symbols (pdi->die_child, lowpc, highpc,
9081 set_addrmap, cu);
c906108c 9082 break;
91c24f0a 9083 case DW_TAG_enumeration_type:
72bf9492
DJ
9084 if (!pdi->is_declaration)
9085 add_partial_enumeration (pdi, cu);
c906108c
SS
9086 break;
9087 case DW_TAG_base_type:
a02abb62 9088 case DW_TAG_subrange_type:
c906108c 9089 /* File scope base type definitions are added to the partial
c5aa993b 9090 symbol table. */
72bf9492 9091 add_partial_symbol (pdi, cu);
c906108c 9092 break;
d9fa45fe 9093 case DW_TAG_namespace:
cdc07690 9094 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 9095 break;
5d7cb8df 9096 case DW_TAG_module:
cdc07690 9097 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 9098 break;
95554aad
TT
9099 case DW_TAG_imported_unit:
9100 {
9101 struct dwarf2_per_cu_data *per_cu;
9102
f4dc4d17
DE
9103 /* For now we don't handle imported units in type units. */
9104 if (cu->per_cu->is_debug_types)
9105 {
9106 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9107 " supported in type units [in module %s]"),
518817b3 9108 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9109 }
9110
e3b94546
SM
9111 per_cu = dwarf2_find_containing_comp_unit
9112 (pdi->d.sect_off, pdi->is_dwz,
518817b3 9113 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
9114
9115 /* Go read the partial unit, if needed. */
9116 if (per_cu->v.psymtab == NULL)
b93601f3 9117 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 9118
f4dc4d17 9119 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 9120 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
9121 }
9122 break;
74921315
KS
9123 case DW_TAG_imported_declaration:
9124 add_partial_symbol (pdi, cu);
9125 break;
c906108c
SS
9126 default:
9127 break;
9128 }
9129 }
9130
72bf9492
DJ
9131 /* If the die has a sibling, skip to the sibling. */
9132
9133 pdi = pdi->die_sibling;
9134 }
9135}
9136
9137/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 9138
72bf9492 9139 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 9140 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
9141 Enumerators are an exception; they use the scope of their parent
9142 enumeration type, i.e. the name of the enumeration type is not
9143 prepended to the enumerator.
91c24f0a 9144
72bf9492
DJ
9145 There are two complexities. One is DW_AT_specification; in this
9146 case "parent" means the parent of the target of the specification,
9147 instead of the direct parent of the DIE. The other is compilers
9148 which do not emit DW_TAG_namespace; in this case we try to guess
9149 the fully qualified name of structure types from their members'
9150 linkage names. This must be done using the DIE's children rather
9151 than the children of any DW_AT_specification target. We only need
9152 to do this for structures at the top level, i.e. if the target of
9153 any DW_AT_specification (if any; otherwise the DIE itself) does not
9154 have a parent. */
9155
9156/* Compute the scope prefix associated with PDI's parent, in
9157 compilation unit CU. The result will be allocated on CU's
9158 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9159 field. NULL is returned if no prefix is necessary. */
15d034d0 9160static const char *
72bf9492
DJ
9161partial_die_parent_scope (struct partial_die_info *pdi,
9162 struct dwarf2_cu *cu)
9163{
15d034d0 9164 const char *grandparent_scope;
72bf9492 9165 struct partial_die_info *parent, *real_pdi;
91c24f0a 9166
72bf9492
DJ
9167 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9168 then this means the parent of the specification DIE. */
9169
9170 real_pdi = pdi;
72bf9492 9171 while (real_pdi->has_specification)
36586728
TT
9172 real_pdi = find_partial_die (real_pdi->spec_offset,
9173 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
9174
9175 parent = real_pdi->die_parent;
9176 if (parent == NULL)
9177 return NULL;
9178
9179 if (parent->scope_set)
9180 return parent->scope;
9181
9182 fixup_partial_die (parent, cu);
9183
10b3939b 9184 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 9185
acebe513
UW
9186 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9187 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9188 Work around this problem here. */
9189 if (cu->language == language_cplus
6e70227d 9190 && parent->tag == DW_TAG_namespace
acebe513
UW
9191 && strcmp (parent->name, "::") == 0
9192 && grandparent_scope == NULL)
9193 {
9194 parent->scope = NULL;
9195 parent->scope_set = 1;
9196 return NULL;
9197 }
9198
9c6c53f7
SA
9199 if (pdi->tag == DW_TAG_enumerator)
9200 /* Enumerators should not get the name of the enumeration as a prefix. */
9201 parent->scope = grandparent_scope;
9202 else if (parent->tag == DW_TAG_namespace
f55ee35c 9203 || parent->tag == DW_TAG_module
72bf9492
DJ
9204 || parent->tag == DW_TAG_structure_type
9205 || parent->tag == DW_TAG_class_type
680b30c7 9206 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
9207 || parent->tag == DW_TAG_union_type
9208 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
9209 {
9210 if (grandparent_scope == NULL)
9211 parent->scope = parent->name;
9212 else
3e43a32a
MS
9213 parent->scope = typename_concat (&cu->comp_unit_obstack,
9214 grandparent_scope,
f55ee35c 9215 parent->name, 0, cu);
72bf9492 9216 }
72bf9492
DJ
9217 else
9218 {
9219 /* FIXME drow/2004-04-01: What should we be doing with
9220 function-local names? For partial symbols, we should probably be
9221 ignoring them. */
9222 complaint (&symfile_complaints,
e2e0b3e5 9223 _("unhandled containing DIE tag %d for DIE at %d"),
9c541725 9224 parent->tag, to_underlying (pdi->sect_off));
72bf9492 9225 parent->scope = grandparent_scope;
c906108c
SS
9226 }
9227
72bf9492
DJ
9228 parent->scope_set = 1;
9229 return parent->scope;
9230}
9231
9232/* Return the fully scoped name associated with PDI, from compilation unit
9233 CU. The result will be allocated with malloc. */
4568ecf9 9234
72bf9492
DJ
9235static char *
9236partial_die_full_name (struct partial_die_info *pdi,
9237 struct dwarf2_cu *cu)
9238{
15d034d0 9239 const char *parent_scope;
72bf9492 9240
98bfdba5
PA
9241 /* If this is a template instantiation, we can not work out the
9242 template arguments from partial DIEs. So, unfortunately, we have
9243 to go through the full DIEs. At least any work we do building
9244 types here will be reused if full symbols are loaded later. */
9245 if (pdi->has_template_arguments)
9246 {
9247 fixup_partial_die (pdi, cu);
9248
9249 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9250 {
9251 struct die_info *die;
9252 struct attribute attr;
9253 struct dwarf2_cu *ref_cu = cu;
9254
b64f50a1 9255 /* DW_FORM_ref_addr is using section offset. */
b4069958 9256 attr.name = (enum dwarf_attribute) 0;
98bfdba5 9257 attr.form = DW_FORM_ref_addr;
9c541725 9258 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
9259 die = follow_die_ref (NULL, &attr, &ref_cu);
9260
9261 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9262 }
9263 }
9264
72bf9492
DJ
9265 parent_scope = partial_die_parent_scope (pdi, cu);
9266 if (parent_scope == NULL)
9267 return NULL;
9268 else
f55ee35c 9269 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
9270}
9271
9272static void
72bf9492 9273add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 9274{
518817b3
SM
9275 struct dwarf2_per_objfile *dwarf2_per_objfile
9276 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 9277 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9278 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 9279 CORE_ADDR addr = 0;
15d034d0 9280 const char *actual_name = NULL;
e142c38c 9281 CORE_ADDR baseaddr;
15d034d0 9282 char *built_actual_name;
e142c38c
DJ
9283
9284 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9285
15d034d0
TT
9286 built_actual_name = partial_die_full_name (pdi, cu);
9287 if (built_actual_name != NULL)
9288 actual_name = built_actual_name;
63d06c5c 9289
72bf9492
DJ
9290 if (actual_name == NULL)
9291 actual_name = pdi->name;
9292
c906108c
SS
9293 switch (pdi->tag)
9294 {
b1dc1806 9295 case DW_TAG_inlined_subroutine:
c906108c 9296 case DW_TAG_subprogram:
3e29f34a 9297 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 9298 if (pdi->is_external || cu->language == language_ada)
c906108c 9299 {
2cfa0c8d
JB
9300 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9301 of the global scope. But in Ada, we want to be able to access
9302 nested procedures globally. So all Ada subprograms are stored
9303 in the global scope. */
f47fb265 9304 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9305 built_actual_name != NULL,
f47fb265
MS
9306 VAR_DOMAIN, LOC_BLOCK,
9307 &objfile->global_psymbols,
1762568f 9308 addr, cu->language, objfile);
c906108c
SS
9309 }
9310 else
9311 {
f47fb265 9312 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9313 built_actual_name != NULL,
f47fb265
MS
9314 VAR_DOMAIN, LOC_BLOCK,
9315 &objfile->static_psymbols,
1762568f 9316 addr, cu->language, objfile);
c906108c 9317 }
0c1b455e
TT
9318
9319 if (pdi->main_subprogram && actual_name != NULL)
9320 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 9321 break;
72929c62
JB
9322 case DW_TAG_constant:
9323 {
af5bf4ad 9324 std::vector<partial_symbol *> *list;
72929c62
JB
9325
9326 if (pdi->is_external)
9327 list = &objfile->global_psymbols;
9328 else
9329 list = &objfile->static_psymbols;
f47fb265 9330 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9331 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 9332 list, 0, cu->language, objfile);
72929c62
JB
9333 }
9334 break;
c906108c 9335 case DW_TAG_variable:
95554aad
TT
9336 if (pdi->d.locdesc)
9337 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 9338
95554aad 9339 if (pdi->d.locdesc
caac4577
JG
9340 && addr == 0
9341 && !dwarf2_per_objfile->has_section_at_zero)
9342 {
9343 /* A global or static variable may also have been stripped
9344 out by the linker if unused, in which case its address
9345 will be nullified; do not add such variables into partial
9346 symbol table then. */
9347 }
9348 else if (pdi->is_external)
c906108c
SS
9349 {
9350 /* Global Variable.
9351 Don't enter into the minimal symbol tables as there is
9352 a minimal symbol table entry from the ELF symbols already.
9353 Enter into partial symbol table if it has a location
9354 descriptor or a type.
9355 If the location descriptor is missing, new_symbol will create
9356 a LOC_UNRESOLVED symbol, the address of the variable will then
9357 be determined from the minimal symbol table whenever the variable
9358 is referenced.
9359 The address for the partial symbol table entry is not
9360 used by GDB, but it comes in handy for debugging partial symbol
9361 table building. */
9362
95554aad 9363 if (pdi->d.locdesc || pdi->has_type)
f47fb265 9364 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9365 built_actual_name != NULL,
f47fb265
MS
9366 VAR_DOMAIN, LOC_STATIC,
9367 &objfile->global_psymbols,
1762568f 9368 addr + baseaddr,
f47fb265 9369 cu->language, objfile);
c906108c
SS
9370 }
9371 else
9372 {
ff908ebf
AW
9373 int has_loc = pdi->d.locdesc != NULL;
9374
9375 /* Static Variable. Skip symbols whose value we cannot know (those
9376 without location descriptors or constant values). */
9377 if (!has_loc && !pdi->has_const_value)
decbce07 9378 {
15d034d0 9379 xfree (built_actual_name);
decbce07
MS
9380 return;
9381 }
ff908ebf 9382
f47fb265 9383 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9384 built_actual_name != NULL,
f47fb265
MS
9385 VAR_DOMAIN, LOC_STATIC,
9386 &objfile->static_psymbols,
ff908ebf 9387 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 9388 cu->language, objfile);
c906108c
SS
9389 }
9390 break;
9391 case DW_TAG_typedef:
9392 case DW_TAG_base_type:
a02abb62 9393 case DW_TAG_subrange_type:
38d518c9 9394 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9395 built_actual_name != NULL,
176620f1 9396 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 9397 &objfile->static_psymbols,
1762568f 9398 0, cu->language, objfile);
c906108c 9399 break;
74921315 9400 case DW_TAG_imported_declaration:
72bf9492
DJ
9401 case DW_TAG_namespace:
9402 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9403 built_actual_name != NULL,
72bf9492
DJ
9404 VAR_DOMAIN, LOC_TYPEDEF,
9405 &objfile->global_psymbols,
1762568f 9406 0, cu->language, objfile);
72bf9492 9407 break;
530e8392
KB
9408 case DW_TAG_module:
9409 add_psymbol_to_list (actual_name, strlen (actual_name),
9410 built_actual_name != NULL,
9411 MODULE_DOMAIN, LOC_TYPEDEF,
9412 &objfile->global_psymbols,
1762568f 9413 0, cu->language, objfile);
530e8392 9414 break;
c906108c 9415 case DW_TAG_class_type:
680b30c7 9416 case DW_TAG_interface_type:
c906108c
SS
9417 case DW_TAG_structure_type:
9418 case DW_TAG_union_type:
9419 case DW_TAG_enumeration_type:
fa4028e9
JB
9420 /* Skip external references. The DWARF standard says in the section
9421 about "Structure, Union, and Class Type Entries": "An incomplete
9422 structure, union or class type is represented by a structure,
9423 union or class entry that does not have a byte size attribute
9424 and that has a DW_AT_declaration attribute." */
9425 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 9426 {
15d034d0 9427 xfree (built_actual_name);
decbce07
MS
9428 return;
9429 }
fa4028e9 9430
63d06c5c
DC
9431 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9432 static vs. global. */
38d518c9 9433 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9434 built_actual_name != NULL,
176620f1 9435 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 9436 cu->language == language_cplus
63d06c5c
DC
9437 ? &objfile->global_psymbols
9438 : &objfile->static_psymbols,
1762568f 9439 0, cu->language, objfile);
c906108c 9440
c906108c
SS
9441 break;
9442 case DW_TAG_enumerator:
38d518c9 9443 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9444 built_actual_name != NULL,
176620f1 9445 VAR_DOMAIN, LOC_CONST,
9c37b5ae 9446 cu->language == language_cplus
f6fe98ef
DJ
9447 ? &objfile->global_psymbols
9448 : &objfile->static_psymbols,
1762568f 9449 0, cu->language, objfile);
c906108c
SS
9450 break;
9451 default:
9452 break;
9453 }
5c4e30ca 9454
15d034d0 9455 xfree (built_actual_name);
c906108c
SS
9456}
9457
5c4e30ca
DC
9458/* Read a partial die corresponding to a namespace; also, add a symbol
9459 corresponding to that namespace to the symbol table. NAMESPACE is
9460 the name of the enclosing namespace. */
91c24f0a 9461
72bf9492
DJ
9462static void
9463add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9464 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9465 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9466{
72bf9492 9467 /* Add a symbol for the namespace. */
e7c27a73 9468
72bf9492 9469 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9470
9471 /* Now scan partial symbols in that namespace. */
9472
91c24f0a 9473 if (pdi->has_children)
cdc07690 9474 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9475}
9476
5d7cb8df
JK
9477/* Read a partial die corresponding to a Fortran module. */
9478
9479static void
9480add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9481 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9482{
530e8392
KB
9483 /* Add a symbol for the namespace. */
9484
9485 add_partial_symbol (pdi, cu);
9486
f55ee35c 9487 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9488
9489 if (pdi->has_children)
cdc07690 9490 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9491}
9492
b1dc1806
XR
9493/* Read a partial die corresponding to a subprogram or an inlined
9494 subprogram and create a partial symbol for that subprogram.
9495 When the CU language allows it, this routine also defines a partial
9496 symbol for each nested subprogram that this subprogram contains.
9497 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9498 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9499
cdc07690
YQ
9500 PDI may also be a lexical block, in which case we simply search
9501 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9502 Again, this is only performed when the CU language allows this
9503 type of definitions. */
9504
9505static void
9506add_partial_subprogram (struct partial_die_info *pdi,
9507 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9508 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9509{
b1dc1806 9510 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9511 {
9512 if (pdi->has_pc_info)
9513 {
9514 if (pdi->lowpc < *lowpc)
9515 *lowpc = pdi->lowpc;
9516 if (pdi->highpc > *highpc)
9517 *highpc = pdi->highpc;
cdc07690 9518 if (set_addrmap)
5734ee8b 9519 {
518817b3 9520 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9521 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9522 CORE_ADDR baseaddr;
9523 CORE_ADDR highpc;
9524 CORE_ADDR lowpc;
5734ee8b
DJ
9525
9526 baseaddr = ANOFFSET (objfile->section_offsets,
9527 SECT_OFF_TEXT (objfile));
3e29f34a
MR
9528 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9529 pdi->lowpc + baseaddr);
9530 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9531 pdi->highpc + baseaddr);
9532 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 9533 cu->per_cu->v.psymtab);
5734ee8b 9534 }
481860b3
GB
9535 }
9536
9537 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9538 {
bc30ff58 9539 if (!pdi->is_declaration)
e8d05480
JB
9540 /* Ignore subprogram DIEs that do not have a name, they are
9541 illegal. Do not emit a complaint at this point, we will
9542 do so when we convert this psymtab into a symtab. */
9543 if (pdi->name)
9544 add_partial_symbol (pdi, cu);
bc30ff58
JB
9545 }
9546 }
6e70227d 9547
bc30ff58
JB
9548 if (! pdi->has_children)
9549 return;
9550
9551 if (cu->language == language_ada)
9552 {
9553 pdi = pdi->die_child;
9554 while (pdi != NULL)
9555 {
9556 fixup_partial_die (pdi, cu);
9557 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9558 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9559 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9560 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9561 pdi = pdi->die_sibling;
9562 }
9563 }
9564}
9565
91c24f0a
DC
9566/* Read a partial die corresponding to an enumeration type. */
9567
72bf9492
DJ
9568static void
9569add_partial_enumeration (struct partial_die_info *enum_pdi,
9570 struct dwarf2_cu *cu)
91c24f0a 9571{
72bf9492 9572 struct partial_die_info *pdi;
91c24f0a
DC
9573
9574 if (enum_pdi->name != NULL)
72bf9492
DJ
9575 add_partial_symbol (enum_pdi, cu);
9576
9577 pdi = enum_pdi->die_child;
9578 while (pdi)
91c24f0a 9579 {
72bf9492 9580 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 9581 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 9582 else
72bf9492
DJ
9583 add_partial_symbol (pdi, cu);
9584 pdi = pdi->die_sibling;
91c24f0a 9585 }
91c24f0a
DC
9586}
9587
6caca83c
CC
9588/* Return the initial uleb128 in the die at INFO_PTR. */
9589
9590static unsigned int
d521ce57 9591peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9592{
9593 unsigned int bytes_read;
9594
9595 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9596}
9597
685af9cd
TT
9598/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9599 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9600
4bb7a0a7
DJ
9601 Return the corresponding abbrev, or NULL if the number is zero (indicating
9602 an empty DIE). In either case *BYTES_READ will be set to the length of
9603 the initial number. */
9604
9605static struct abbrev_info *
685af9cd
TT
9606peek_die_abbrev (const die_reader_specs &reader,
9607 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9608{
685af9cd 9609 dwarf2_cu *cu = reader.cu;
518817b3 9610 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9611 unsigned int abbrev_number
9612 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9613
9614 if (abbrev_number == 0)
9615 return NULL;
9616
685af9cd 9617 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9618 if (!abbrev)
9619 {
422b9917
DE
9620 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9621 " at offset 0x%x [in module %s]"),
9622 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9c541725 9623 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9624 }
9625
9626 return abbrev;
9627}
9628
93311388
DE
9629/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9630 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9631 DIE. Any children of the skipped DIEs will also be skipped. */
9632
d521ce57
TT
9633static const gdb_byte *
9634skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9635{
4bb7a0a7
DJ
9636 while (1)
9637 {
685af9cd
TT
9638 unsigned int bytes_read;
9639 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9640
4bb7a0a7
DJ
9641 if (abbrev == NULL)
9642 return info_ptr + bytes_read;
9643 else
dee91e82 9644 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9645 }
9646}
9647
93311388
DE
9648/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9649 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9650 abbrev corresponding to that skipped uleb128 should be passed in
9651 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9652 children. */
9653
d521ce57
TT
9654static const gdb_byte *
9655skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9656 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9657{
9658 unsigned int bytes_read;
9659 struct attribute attr;
dee91e82
DE
9660 bfd *abfd = reader->abfd;
9661 struct dwarf2_cu *cu = reader->cu;
d521ce57 9662 const gdb_byte *buffer = reader->buffer;
f664829e 9663 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9664 unsigned int form, i;
9665
9666 for (i = 0; i < abbrev->num_attrs; i++)
9667 {
9668 /* The only abbrev we care about is DW_AT_sibling. */
9669 if (abbrev->attrs[i].name == DW_AT_sibling)
9670 {
dee91e82 9671 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9672 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9673 complaint (&symfile_complaints,
9674 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9675 else
b9502d3f 9676 {
9c541725
PA
9677 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9678 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9679
9680 if (sibling_ptr < info_ptr)
9681 complaint (&symfile_complaints,
9682 _("DW_AT_sibling points backwards"));
22869d73
KS
9683 else if (sibling_ptr > reader->buffer_end)
9684 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9685 else
9686 return sibling_ptr;
9687 }
4bb7a0a7
DJ
9688 }
9689
9690 /* If it isn't DW_AT_sibling, skip this attribute. */
9691 form = abbrev->attrs[i].form;
9692 skip_attribute:
9693 switch (form)
9694 {
4bb7a0a7 9695 case DW_FORM_ref_addr:
ae411497
TT
9696 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9697 and later it is offset sized. */
9698 if (cu->header.version == 2)
9699 info_ptr += cu->header.addr_size;
9700 else
9701 info_ptr += cu->header.offset_size;
9702 break;
36586728
TT
9703 case DW_FORM_GNU_ref_alt:
9704 info_ptr += cu->header.offset_size;
9705 break;
ae411497 9706 case DW_FORM_addr:
4bb7a0a7
DJ
9707 info_ptr += cu->header.addr_size;
9708 break;
9709 case DW_FORM_data1:
9710 case DW_FORM_ref1:
9711 case DW_FORM_flag:
9712 info_ptr += 1;
9713 break;
2dc7f7b3 9714 case DW_FORM_flag_present:
43988095 9715 case DW_FORM_implicit_const:
2dc7f7b3 9716 break;
4bb7a0a7
DJ
9717 case DW_FORM_data2:
9718 case DW_FORM_ref2:
9719 info_ptr += 2;
9720 break;
9721 case DW_FORM_data4:
9722 case DW_FORM_ref4:
9723 info_ptr += 4;
9724 break;
9725 case DW_FORM_data8:
9726 case DW_FORM_ref8:
55f1336d 9727 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9728 info_ptr += 8;
9729 break;
0224619f
JK
9730 case DW_FORM_data16:
9731 info_ptr += 16;
9732 break;
4bb7a0a7 9733 case DW_FORM_string:
9b1c24c8 9734 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9735 info_ptr += bytes_read;
9736 break;
2dc7f7b3 9737 case DW_FORM_sec_offset:
4bb7a0a7 9738 case DW_FORM_strp:
36586728 9739 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9740 info_ptr += cu->header.offset_size;
9741 break;
2dc7f7b3 9742 case DW_FORM_exprloc:
4bb7a0a7
DJ
9743 case DW_FORM_block:
9744 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9745 info_ptr += bytes_read;
9746 break;
9747 case DW_FORM_block1:
9748 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9749 break;
9750 case DW_FORM_block2:
9751 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9752 break;
9753 case DW_FORM_block4:
9754 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9755 break;
9756 case DW_FORM_sdata:
9757 case DW_FORM_udata:
9758 case DW_FORM_ref_udata:
3019eac3
DE
9759 case DW_FORM_GNU_addr_index:
9760 case DW_FORM_GNU_str_index:
d521ce57 9761 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9762 break;
9763 case DW_FORM_indirect:
9764 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9765 info_ptr += bytes_read;
9766 /* We need to continue parsing from here, so just go back to
9767 the top. */
9768 goto skip_attribute;
9769
9770 default:
3e43a32a
MS
9771 error (_("Dwarf Error: Cannot handle %s "
9772 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9773 dwarf_form_name (form),
9774 bfd_get_filename (abfd));
9775 }
9776 }
9777
9778 if (abbrev->has_children)
dee91e82 9779 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9780 else
9781 return info_ptr;
9782}
9783
93311388 9784/* Locate ORIG_PDI's sibling.
dee91e82 9785 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9786
d521ce57 9787static const gdb_byte *
dee91e82
DE
9788locate_pdi_sibling (const struct die_reader_specs *reader,
9789 struct partial_die_info *orig_pdi,
d521ce57 9790 const gdb_byte *info_ptr)
91c24f0a
DC
9791{
9792 /* Do we know the sibling already? */
72bf9492 9793
91c24f0a
DC
9794 if (orig_pdi->sibling)
9795 return orig_pdi->sibling;
9796
9797 /* Are there any children to deal with? */
9798
9799 if (!orig_pdi->has_children)
9800 return info_ptr;
9801
4bb7a0a7 9802 /* Skip the children the long way. */
91c24f0a 9803
dee91e82 9804 return skip_children (reader, info_ptr);
91c24f0a
DC
9805}
9806
257e7a09 9807/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9808 not NULL. */
c906108c
SS
9809
9810static void
257e7a09
YQ
9811dwarf2_read_symtab (struct partial_symtab *self,
9812 struct objfile *objfile)
c906108c 9813{
ed2dc618
SM
9814 struct dwarf2_per_objfile *dwarf2_per_objfile
9815 = get_dwarf2_per_objfile (objfile);
9816
257e7a09 9817 if (self->readin)
c906108c 9818 {
442e4d9c 9819 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9820 self->filename);
442e4d9c
YQ
9821 }
9822 else
9823 {
9824 if (info_verbose)
c906108c 9825 {
442e4d9c 9826 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9827 self->filename);
442e4d9c 9828 gdb_flush (gdb_stdout);
c906108c 9829 }
c906108c 9830
442e4d9c
YQ
9831 /* If this psymtab is constructed from a debug-only objfile, the
9832 has_section_at_zero flag will not necessarily be correct. We
9833 can get the correct value for this flag by looking at the data
9834 associated with the (presumably stripped) associated objfile. */
9835 if (objfile->separate_debug_objfile_backlink)
9836 {
9837 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9838 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9839
442e4d9c
YQ
9840 dwarf2_per_objfile->has_section_at_zero
9841 = dpo_backlink->has_section_at_zero;
9842 }
b2ab525c 9843
442e4d9c 9844 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9845
257e7a09 9846 psymtab_to_symtab_1 (self);
c906108c 9847
442e4d9c
YQ
9848 /* Finish up the debug error message. */
9849 if (info_verbose)
9850 printf_filtered (_("done.\n"));
c906108c 9851 }
95554aad 9852
ed2dc618 9853 process_cu_includes (dwarf2_per_objfile);
c906108c 9854}
9cdd5dbd
DE
9855\f
9856/* Reading in full CUs. */
c906108c 9857
10b3939b
DJ
9858/* Add PER_CU to the queue. */
9859
9860static void
95554aad
TT
9861queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9862 enum language pretend_language)
10b3939b
DJ
9863{
9864 struct dwarf2_queue_item *item;
9865
9866 per_cu->queued = 1;
8d749320 9867 item = XNEW (struct dwarf2_queue_item);
10b3939b 9868 item->per_cu = per_cu;
95554aad 9869 item->pretend_language = pretend_language;
10b3939b
DJ
9870 item->next = NULL;
9871
9872 if (dwarf2_queue == NULL)
9873 dwarf2_queue = item;
9874 else
9875 dwarf2_queue_tail->next = item;
9876
9877 dwarf2_queue_tail = item;
9878}
9879
89e63ee4
DE
9880/* If PER_CU is not yet queued, add it to the queue.
9881 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9882 dependency.
0907af0c 9883 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9884 meaning either PER_CU is already queued or it is already loaded.
9885
9886 N.B. There is an invariant here that if a CU is queued then it is loaded.
9887 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9888
9889static int
89e63ee4 9890maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9891 struct dwarf2_per_cu_data *per_cu,
9892 enum language pretend_language)
9893{
9894 /* We may arrive here during partial symbol reading, if we need full
9895 DIEs to process an unusual case (e.g. template arguments). Do
9896 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9897 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9898 {
9899 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9900 return 1;
9901 return 0;
9902 }
9903
9904 /* Mark the dependence relation so that we don't flush PER_CU
9905 too early. */
89e63ee4
DE
9906 if (dependent_cu != NULL)
9907 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9908
9909 /* If it's already on the queue, we have nothing to do. */
9910 if (per_cu->queued)
9911 return 0;
9912
9913 /* If the compilation unit is already loaded, just mark it as
9914 used. */
9915 if (per_cu->cu != NULL)
9916 {
9917 per_cu->cu->last_used = 0;
9918 return 0;
9919 }
9920
9921 /* Add it to the queue. */
9922 queue_comp_unit (per_cu, pretend_language);
9923
9924 return 1;
9925}
9926
10b3939b
DJ
9927/* Process the queue. */
9928
9929static void
ed2dc618 9930process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9931{
9932 struct dwarf2_queue_item *item, *next_item;
9933
b4f54984 9934 if (dwarf_read_debug)
45cfd468
DE
9935 {
9936 fprintf_unfiltered (gdb_stdlog,
9937 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9938 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9939 }
9940
03dd20cc
DJ
9941 /* The queue starts out with one item, but following a DIE reference
9942 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9943 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9944 {
cc12ce38
DE
9945 if ((dwarf2_per_objfile->using_index
9946 ? !item->per_cu->v.quick->compunit_symtab
9947 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9948 /* Skip dummy CUs. */
9949 && item->per_cu->cu != NULL)
f4dc4d17
DE
9950 {
9951 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9952 unsigned int debug_print_threshold;
247f5c4f 9953 char buf[100];
f4dc4d17 9954
247f5c4f 9955 if (per_cu->is_debug_types)
f4dc4d17 9956 {
247f5c4f
DE
9957 struct signatured_type *sig_type =
9958 (struct signatured_type *) per_cu;
9959
9960 sprintf (buf, "TU %s at offset 0x%x",
73be47f5 9961 hex_string (sig_type->signature),
9c541725 9962 to_underlying (per_cu->sect_off));
73be47f5
DE
9963 /* There can be 100s of TUs.
9964 Only print them in verbose mode. */
9965 debug_print_threshold = 2;
f4dc4d17 9966 }
247f5c4f 9967 else
73be47f5 9968 {
9c541725
PA
9969 sprintf (buf, "CU at offset 0x%x",
9970 to_underlying (per_cu->sect_off));
73be47f5
DE
9971 debug_print_threshold = 1;
9972 }
247f5c4f 9973
b4f54984 9974 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9975 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9976
9977 if (per_cu->is_debug_types)
9978 process_full_type_unit (per_cu, item->pretend_language);
9979 else
9980 process_full_comp_unit (per_cu, item->pretend_language);
9981
b4f54984 9982 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9983 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9984 }
10b3939b
DJ
9985
9986 item->per_cu->queued = 0;
9987 next_item = item->next;
9988 xfree (item);
9989 }
9990
9991 dwarf2_queue_tail = NULL;
45cfd468 9992
b4f54984 9993 if (dwarf_read_debug)
45cfd468
DE
9994 {
9995 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9996 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9997 }
10b3939b
DJ
9998}
9999
10b3939b
DJ
10000/* Read in full symbols for PST, and anything it depends on. */
10001
c906108c 10002static void
fba45db2 10003psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 10004{
10b3939b 10005 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
10006 int i;
10007
95554aad
TT
10008 if (pst->readin)
10009 return;
10010
aaa75496 10011 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
10012 if (!pst->dependencies[i]->readin
10013 && pst->dependencies[i]->user == NULL)
aaa75496
JB
10014 {
10015 /* Inform about additional files that need to be read in. */
10016 if (info_verbose)
10017 {
a3f17187 10018 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
10019 fputs_filtered (" ", gdb_stdout);
10020 wrap_here ("");
10021 fputs_filtered ("and ", gdb_stdout);
10022 wrap_here ("");
10023 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 10024 wrap_here (""); /* Flush output. */
aaa75496
JB
10025 gdb_flush (gdb_stdout);
10026 }
10027 psymtab_to_symtab_1 (pst->dependencies[i]);
10028 }
10029
9a3c8263 10030 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
10031
10032 if (per_cu == NULL)
aaa75496
JB
10033 {
10034 /* It's an include file, no symbols to read for it.
10035 Everything is in the parent symtab. */
10036 pst->readin = 1;
10037 return;
10038 }
c906108c 10039
a0f42c21 10040 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
10041}
10042
dee91e82
DE
10043/* Trivial hash function for die_info: the hash value of a DIE
10044 is its offset in .debug_info for this objfile. */
10b3939b 10045
dee91e82
DE
10046static hashval_t
10047die_hash (const void *item)
10b3939b 10048{
9a3c8263 10049 const struct die_info *die = (const struct die_info *) item;
6502dd73 10050
9c541725 10051 return to_underlying (die->sect_off);
dee91e82 10052}
63d06c5c 10053
dee91e82
DE
10054/* Trivial comparison function for die_info structures: two DIEs
10055 are equal if they have the same offset. */
98bfdba5 10056
dee91e82
DE
10057static int
10058die_eq (const void *item_lhs, const void *item_rhs)
10059{
9a3c8263
SM
10060 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
10061 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 10062
9c541725 10063 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 10064}
c906108c 10065
dee91e82
DE
10066/* die_reader_func for load_full_comp_unit.
10067 This is identical to read_signatured_type_reader,
10068 but is kept separate for now. */
c906108c 10069
dee91e82
DE
10070static void
10071load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 10072 const gdb_byte *info_ptr,
dee91e82
DE
10073 struct die_info *comp_unit_die,
10074 int has_children,
10075 void *data)
10076{
10077 struct dwarf2_cu *cu = reader->cu;
9a3c8263 10078 enum language *language_ptr = (enum language *) data;
6caca83c 10079
dee91e82
DE
10080 gdb_assert (cu->die_hash == NULL);
10081 cu->die_hash =
10082 htab_create_alloc_ex (cu->header.length / 12,
10083 die_hash,
10084 die_eq,
10085 NULL,
10086 &cu->comp_unit_obstack,
10087 hashtab_obstack_allocate,
10088 dummy_obstack_deallocate);
e142c38c 10089
dee91e82
DE
10090 if (has_children)
10091 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10092 &info_ptr, comp_unit_die);
10093 cu->dies = comp_unit_die;
10094 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
10095
10096 /* We try not to read any attributes in this function, because not
9cdd5dbd 10097 all CUs needed for references have been loaded yet, and symbol
10b3939b 10098 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
10099 or we won't be able to build types correctly.
10100 Similarly, if we do not read the producer, we can not apply
10101 producer-specific interpretation. */
95554aad 10102 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 10103}
10b3939b 10104
dee91e82 10105/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 10106
dee91e82 10107static void
95554aad
TT
10108load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10109 enum language pretend_language)
dee91e82 10110{
3019eac3 10111 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 10112
f4dc4d17
DE
10113 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10114 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
10115}
10116
3da10d80
KS
10117/* Add a DIE to the delayed physname list. */
10118
10119static void
10120add_to_method_list (struct type *type, int fnfield_index, int index,
10121 const char *name, struct die_info *die,
10122 struct dwarf2_cu *cu)
10123{
10124 struct delayed_method_info mi;
10125 mi.type = type;
10126 mi.fnfield_index = fnfield_index;
10127 mi.index = index;
10128 mi.name = name;
10129 mi.die = die;
c89b44cd 10130 cu->method_list.push_back (mi);
3da10d80
KS
10131}
10132
3693fdb3
PA
10133/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10134 "const" / "volatile". If so, decrements LEN by the length of the
10135 modifier and return true. Otherwise return false. */
10136
10137template<size_t N>
10138static bool
10139check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10140{
10141 size_t mod_len = sizeof (mod) - 1;
10142 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10143 {
10144 len -= mod_len;
10145 return true;
10146 }
10147 return false;
10148}
10149
3da10d80
KS
10150/* Compute the physnames of any methods on the CU's method list.
10151
10152 The computation of method physnames is delayed in order to avoid the
10153 (bad) condition that one of the method's formal parameters is of an as yet
10154 incomplete type. */
10155
10156static void
10157compute_delayed_physnames (struct dwarf2_cu *cu)
10158{
3693fdb3 10159 /* Only C++ delays computing physnames. */
c89b44cd 10160 if (cu->method_list.empty ())
3693fdb3
PA
10161 return;
10162 gdb_assert (cu->language == language_cplus);
10163
c89b44cd 10164 for (struct delayed_method_info &mi : cu->method_list)
3da10d80 10165 {
1d06ead6 10166 const char *physname;
3da10d80 10167 struct fn_fieldlist *fn_flp
c89b44cd
TT
10168 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
10169 physname = dwarf2_physname (mi.name, mi.die, cu);
10170 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 10171 = physname ? physname : "";
3693fdb3
PA
10172
10173 /* Since there's no tag to indicate whether a method is a
10174 const/volatile overload, extract that information out of the
10175 demangled name. */
10176 if (physname != NULL)
10177 {
10178 size_t len = strlen (physname);
10179
10180 while (1)
10181 {
10182 if (physname[len] == ')') /* shortcut */
10183 break;
10184 else if (check_modifier (physname, len, " const"))
c89b44cd 10185 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 10186 else if (check_modifier (physname, len, " volatile"))
c89b44cd 10187 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
10188 else
10189 break;
10190 }
10191 }
3da10d80 10192 }
c89b44cd
TT
10193
10194 /* The list is no longer needed. */
10195 cu->method_list.clear ();
3da10d80
KS
10196}
10197
a766d390
DE
10198/* Go objects should be embedded in a DW_TAG_module DIE,
10199 and it's not clear if/how imported objects will appear.
10200 To keep Go support simple until that's worked out,
10201 go back through what we've read and create something usable.
10202 We could do this while processing each DIE, and feels kinda cleaner,
10203 but that way is more invasive.
10204 This is to, for example, allow the user to type "p var" or "b main"
10205 without having to specify the package name, and allow lookups
10206 of module.object to work in contexts that use the expression
10207 parser. */
10208
10209static void
10210fixup_go_packaging (struct dwarf2_cu *cu)
10211{
10212 char *package_name = NULL;
10213 struct pending *list;
10214 int i;
10215
10216 for (list = global_symbols; list != NULL; list = list->next)
10217 {
10218 for (i = 0; i < list->nsyms; ++i)
10219 {
10220 struct symbol *sym = list->symbol[i];
10221
10222 if (SYMBOL_LANGUAGE (sym) == language_go
10223 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10224 {
10225 char *this_package_name = go_symbol_package_name (sym);
10226
10227 if (this_package_name == NULL)
10228 continue;
10229 if (package_name == NULL)
10230 package_name = this_package_name;
10231 else
10232 {
518817b3
SM
10233 struct objfile *objfile
10234 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390
DE
10235 if (strcmp (package_name, this_package_name) != 0)
10236 complaint (&symfile_complaints,
10237 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
10238 (symbol_symtab (sym) != NULL
10239 ? symtab_to_filename_for_display
10240 (symbol_symtab (sym))
e3b94546 10241 : objfile_name (objfile)),
a766d390
DE
10242 this_package_name, package_name);
10243 xfree (this_package_name);
10244 }
10245 }
10246 }
10247 }
10248
10249 if (package_name != NULL)
10250 {
518817b3 10251 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 10252 const char *saved_package_name
224c3ddb
SM
10253 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10254 package_name,
10255 strlen (package_name));
19f392bc
UW
10256 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10257 saved_package_name);
a766d390
DE
10258 struct symbol *sym;
10259
10260 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10261
e623cf5d 10262 sym = allocate_symbol (objfile);
f85f34ed 10263 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
10264 SYMBOL_SET_NAMES (sym, saved_package_name,
10265 strlen (saved_package_name), 0, objfile);
a766d390
DE
10266 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10267 e.g., "main" finds the "main" module and not C's main(). */
10268 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 10269 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
10270 SYMBOL_TYPE (sym) = type;
10271
10272 add_symbol_to_list (sym, &global_symbols);
10273
10274 xfree (package_name);
10275 }
10276}
10277
95554aad
TT
10278/* Return the symtab for PER_CU. This works properly regardless of
10279 whether we're using the index or psymtabs. */
10280
43f3e411
DE
10281static struct compunit_symtab *
10282get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10283{
ed2dc618 10284 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10285 ? per_cu->v.quick->compunit_symtab
10286 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10287}
10288
10289/* A helper function for computing the list of all symbol tables
10290 included by PER_CU. */
10291
10292static void
43f3e411 10293recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 10294 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10295 struct dwarf2_per_cu_data *per_cu,
43f3e411 10296 struct compunit_symtab *immediate_parent)
95554aad
TT
10297{
10298 void **slot;
10299 int ix;
43f3e411 10300 struct compunit_symtab *cust;
95554aad
TT
10301 struct dwarf2_per_cu_data *iter;
10302
10303 slot = htab_find_slot (all_children, per_cu, INSERT);
10304 if (*slot != NULL)
10305 {
10306 /* This inclusion and its children have been processed. */
10307 return;
10308 }
10309
10310 *slot = per_cu;
10311 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10312 cust = get_compunit_symtab (per_cu);
10313 if (cust != NULL)
ec94af83
DE
10314 {
10315 /* If this is a type unit only add its symbol table if we haven't
10316 seen it yet (type unit per_cu's can share symtabs). */
10317 if (per_cu->is_debug_types)
10318 {
43f3e411 10319 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10320 if (*slot == NULL)
10321 {
43f3e411
DE
10322 *slot = cust;
10323 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10324 if (cust->user == NULL)
10325 cust->user = immediate_parent;
ec94af83
DE
10326 }
10327 }
10328 else
f9125b6c 10329 {
43f3e411
DE
10330 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10331 if (cust->user == NULL)
10332 cust->user = immediate_parent;
f9125b6c 10333 }
ec94af83 10334 }
95554aad
TT
10335
10336 for (ix = 0;
796a7ff8 10337 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 10338 ++ix)
ec94af83
DE
10339 {
10340 recursively_compute_inclusions (result, all_children,
43f3e411 10341 all_type_symtabs, iter, cust);
ec94af83 10342 }
95554aad
TT
10343}
10344
43f3e411 10345/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10346 PER_CU. */
10347
10348static void
43f3e411 10349compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10350{
f4dc4d17
DE
10351 gdb_assert (! per_cu->is_debug_types);
10352
796a7ff8 10353 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
10354 {
10355 int ix, len;
ec94af83 10356 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
10357 struct compunit_symtab *compunit_symtab_iter;
10358 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 10359 htab_t all_children, all_type_symtabs;
43f3e411 10360 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10361
10362 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10363 if (cust == NULL)
95554aad
TT
10364 return;
10365
10366 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10367 NULL, xcalloc, xfree);
ec94af83
DE
10368 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10369 NULL, xcalloc, xfree);
95554aad
TT
10370
10371 for (ix = 0;
796a7ff8 10372 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 10373 ix, per_cu_iter);
95554aad 10374 ++ix)
ec94af83
DE
10375 {
10376 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 10377 all_type_symtabs, per_cu_iter,
43f3e411 10378 cust);
ec94af83 10379 }
95554aad 10380
ec94af83 10381 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
10382 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10383 cust->includes
ed2dc618 10384 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10385 struct compunit_symtab *, len + 1);
95554aad 10386 for (ix = 0;
43f3e411
DE
10387 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10388 compunit_symtab_iter);
95554aad 10389 ++ix)
43f3e411
DE
10390 cust->includes[ix] = compunit_symtab_iter;
10391 cust->includes[len] = NULL;
95554aad 10392
43f3e411 10393 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 10394 htab_delete (all_children);
ec94af83 10395 htab_delete (all_type_symtabs);
95554aad
TT
10396 }
10397}
10398
10399/* Compute the 'includes' field for the symtabs of all the CUs we just
10400 read. */
10401
10402static void
ed2dc618 10403process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad
TT
10404{
10405 int ix;
10406 struct dwarf2_per_cu_data *iter;
10407
10408 for (ix = 0;
10409 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10410 ix, iter);
10411 ++ix)
f4dc4d17
DE
10412 {
10413 if (! iter->is_debug_types)
43f3e411 10414 compute_compunit_symtab_includes (iter);
f4dc4d17 10415 }
95554aad
TT
10416
10417 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10418}
10419
9cdd5dbd 10420/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10421 already been loaded into memory. */
10422
10423static void
95554aad
TT
10424process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10425 enum language pretend_language)
10b3939b 10426{
10b3939b 10427 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10428 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10429 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10430 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10431 CORE_ADDR lowpc, highpc;
43f3e411 10432 struct compunit_symtab *cust;
10b3939b 10433 CORE_ADDR baseaddr;
4359dff1 10434 struct block *static_block;
3e29f34a 10435 CORE_ADDR addr;
10b3939b
DJ
10436
10437 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10438
10b3939b 10439 buildsym_init ();
33c7c59d 10440 scoped_free_pendings free_pending;
c89b44cd
TT
10441
10442 /* Clear the list here in case something was left over. */
10443 cu->method_list.clear ();
10b3939b
DJ
10444
10445 cu->list_in_scope = &file_symbols;
c906108c 10446
95554aad
TT
10447 cu->language = pretend_language;
10448 cu->language_defn = language_def (cu->language);
10449
c906108c 10450 /* Do line number decoding in read_file_scope () */
10b3939b 10451 process_die (cu->dies, cu);
c906108c 10452
a766d390
DE
10453 /* For now fudge the Go package. */
10454 if (cu->language == language_go)
10455 fixup_go_packaging (cu);
10456
3da10d80
KS
10457 /* Now that we have processed all the DIEs in the CU, all the types
10458 should be complete, and it should now be safe to compute all of the
10459 physnames. */
10460 compute_delayed_physnames (cu);
3da10d80 10461
fae299cd
DC
10462 /* Some compilers don't define a DW_AT_high_pc attribute for the
10463 compilation unit. If the DW_AT_high_pc is missing, synthesize
10464 it, by scanning the DIE's below the compilation unit. */
10b3939b 10465 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10466
3e29f34a
MR
10467 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10468 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10469
10470 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10471 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10472 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10473 addrmap to help ensure it has an accurate map of pc values belonging to
10474 this comp unit. */
10475 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10476
43f3e411
DE
10477 cust = end_symtab_from_static_block (static_block,
10478 SECT_OFF_TEXT (objfile), 0);
c906108c 10479
43f3e411 10480 if (cust != NULL)
c906108c 10481 {
df15bd07 10482 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10483
8be455d7
JK
10484 /* Set symtab language to language from DW_AT_language. If the
10485 compilation is from a C file generated by language preprocessors, do
10486 not set the language if it was already deduced by start_subfile. */
43f3e411 10487 if (!(cu->language == language_c
40e3ad0e 10488 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10489 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10490
10491 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10492 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10493 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10494 there were bugs in prologue debug info, fixed later in GCC-4.5
10495 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10496
10497 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10498 needed, it would be wrong due to missing DW_AT_producer there.
10499
10500 Still one can confuse GDB by using non-standard GCC compilation
10501 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10502 */
ab260dad 10503 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10504 cust->locations_valid = 1;
e0d00bc7
JK
10505
10506 if (gcc_4_minor >= 5)
43f3e411 10507 cust->epilogue_unwind_valid = 1;
96408a79 10508
43f3e411 10509 cust->call_site_htab = cu->call_site_htab;
c906108c 10510 }
9291a0cd
TT
10511
10512 if (dwarf2_per_objfile->using_index)
43f3e411 10513 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10514 else
10515 {
10516 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10517 pst->compunit_symtab = cust;
9291a0cd
TT
10518 pst->readin = 1;
10519 }
c906108c 10520
95554aad
TT
10521 /* Push it for inclusion processing later. */
10522 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
f4dc4d17 10523}
45cfd468 10524
f4dc4d17
DE
10525/* Generate full symbol information for type unit PER_CU, whose DIEs have
10526 already been loaded into memory. */
10527
10528static void
10529process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10530 enum language pretend_language)
10531{
10532 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10533 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10534 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10535 struct compunit_symtab *cust;
0186c6a7
DE
10536 struct signatured_type *sig_type;
10537
10538 gdb_assert (per_cu->is_debug_types);
10539 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
10540
10541 buildsym_init ();
33c7c59d 10542 scoped_free_pendings free_pending;
c89b44cd
TT
10543
10544 /* Clear the list here in case something was left over. */
10545 cu->method_list.clear ();
f4dc4d17
DE
10546
10547 cu->list_in_scope = &file_symbols;
10548
10549 cu->language = pretend_language;
10550 cu->language_defn = language_def (cu->language);
10551
10552 /* The symbol tables are set up in read_type_unit_scope. */
10553 process_die (cu->dies, cu);
10554
10555 /* For now fudge the Go package. */
10556 if (cu->language == language_go)
10557 fixup_go_packaging (cu);
10558
10559 /* Now that we have processed all the DIEs in the CU, all the types
10560 should be complete, and it should now be safe to compute all of the
10561 physnames. */
10562 compute_delayed_physnames (cu);
f4dc4d17
DE
10563
10564 /* TUs share symbol tables.
10565 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10566 of it with end_expandable_symtab. Otherwise, complete the addition of
10567 this TU's symbols to the existing symtab. */
43f3e411 10568 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10569 {
43f3e411
DE
10570 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10571 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10572
43f3e411 10573 if (cust != NULL)
f4dc4d17
DE
10574 {
10575 /* Set symtab language to language from DW_AT_language. If the
10576 compilation is from a C file generated by language preprocessors,
10577 do not set the language if it was already deduced by
10578 start_subfile. */
43f3e411
DE
10579 if (!(cu->language == language_c
10580 && COMPUNIT_FILETABS (cust)->language != language_c))
10581 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10582 }
10583 }
10584 else
10585 {
0ab9ce85 10586 augment_type_symtab ();
43f3e411 10587 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10588 }
10589
10590 if (dwarf2_per_objfile->using_index)
43f3e411 10591 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10592 else
10593 {
10594 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10595 pst->compunit_symtab = cust;
f4dc4d17 10596 pst->readin = 1;
45cfd468 10597 }
c906108c
SS
10598}
10599
95554aad
TT
10600/* Process an imported unit DIE. */
10601
10602static void
10603process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10604{
10605 struct attribute *attr;
10606
f4dc4d17
DE
10607 /* For now we don't handle imported units in type units. */
10608 if (cu->per_cu->is_debug_types)
10609 {
10610 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10611 " supported in type units [in module %s]"),
518817b3 10612 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10613 }
10614
95554aad
TT
10615 attr = dwarf2_attr (die, DW_AT_import, cu);
10616 if (attr != NULL)
10617 {
9c541725
PA
10618 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10619 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10620 dwarf2_per_cu_data *per_cu
e3b94546 10621 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10622 cu->per_cu->dwarf2_per_objfile);
95554aad 10623
69d751e3 10624 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
10625 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10626 load_full_comp_unit (per_cu, cu->language);
10627
796a7ff8 10628 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
10629 per_cu);
10630 }
10631}
10632
4c8aa72d
PA
10633/* RAII object that represents a process_die scope: i.e.,
10634 starts/finishes processing a DIE. */
10635class process_die_scope
adde2bff 10636{
4c8aa72d
PA
10637public:
10638 process_die_scope (die_info *die, dwarf2_cu *cu)
10639 : m_die (die), m_cu (cu)
10640 {
10641 /* We should only be processing DIEs not already in process. */
10642 gdb_assert (!m_die->in_process);
10643 m_die->in_process = true;
10644 }
8c3cb9fa 10645
4c8aa72d
PA
10646 ~process_die_scope ()
10647 {
10648 m_die->in_process = false;
10649
10650 /* If we're done processing the DIE for the CU that owns the line
10651 header, we don't need the line header anymore. */
10652 if (m_cu->line_header_die_owner == m_die)
10653 {
10654 delete m_cu->line_header;
10655 m_cu->line_header = NULL;
10656 m_cu->line_header_die_owner = NULL;
10657 }
10658 }
10659
10660private:
10661 die_info *m_die;
10662 dwarf2_cu *m_cu;
10663};
adde2bff 10664
c906108c
SS
10665/* Process a die and its children. */
10666
10667static void
e7c27a73 10668process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10669{
4c8aa72d 10670 process_die_scope scope (die, cu);
adde2bff 10671
c906108c
SS
10672 switch (die->tag)
10673 {
10674 case DW_TAG_padding:
10675 break;
10676 case DW_TAG_compile_unit:
95554aad 10677 case DW_TAG_partial_unit:
e7c27a73 10678 read_file_scope (die, cu);
c906108c 10679 break;
348e048f
DE
10680 case DW_TAG_type_unit:
10681 read_type_unit_scope (die, cu);
10682 break;
c906108c 10683 case DW_TAG_subprogram:
c906108c 10684 case DW_TAG_inlined_subroutine:
edb3359d 10685 read_func_scope (die, cu);
c906108c
SS
10686 break;
10687 case DW_TAG_lexical_block:
14898363
L
10688 case DW_TAG_try_block:
10689 case DW_TAG_catch_block:
e7c27a73 10690 read_lexical_block_scope (die, cu);
c906108c 10691 break;
216f72a1 10692 case DW_TAG_call_site:
96408a79
SA
10693 case DW_TAG_GNU_call_site:
10694 read_call_site_scope (die, cu);
10695 break;
c906108c 10696 case DW_TAG_class_type:
680b30c7 10697 case DW_TAG_interface_type:
c906108c
SS
10698 case DW_TAG_structure_type:
10699 case DW_TAG_union_type:
134d01f1 10700 process_structure_scope (die, cu);
c906108c
SS
10701 break;
10702 case DW_TAG_enumeration_type:
134d01f1 10703 process_enumeration_scope (die, cu);
c906108c 10704 break;
134d01f1 10705
f792889a
DJ
10706 /* These dies have a type, but processing them does not create
10707 a symbol or recurse to process the children. Therefore we can
10708 read them on-demand through read_type_die. */
c906108c 10709 case DW_TAG_subroutine_type:
72019c9c 10710 case DW_TAG_set_type:
c906108c 10711 case DW_TAG_array_type:
c906108c 10712 case DW_TAG_pointer_type:
c906108c 10713 case DW_TAG_ptr_to_member_type:
c906108c 10714 case DW_TAG_reference_type:
4297a3f0 10715 case DW_TAG_rvalue_reference_type:
c906108c 10716 case DW_TAG_string_type:
c906108c 10717 break;
134d01f1 10718
c906108c 10719 case DW_TAG_base_type:
a02abb62 10720 case DW_TAG_subrange_type:
cb249c71 10721 case DW_TAG_typedef:
134d01f1
DJ
10722 /* Add a typedef symbol for the type definition, if it has a
10723 DW_AT_name. */
f792889a 10724 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10725 break;
c906108c 10726 case DW_TAG_common_block:
e7c27a73 10727 read_common_block (die, cu);
c906108c
SS
10728 break;
10729 case DW_TAG_common_inclusion:
10730 break;
d9fa45fe 10731 case DW_TAG_namespace:
4d4ec4e5 10732 cu->processing_has_namespace_info = 1;
e7c27a73 10733 read_namespace (die, cu);
d9fa45fe 10734 break;
5d7cb8df 10735 case DW_TAG_module:
4d4ec4e5 10736 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
10737 read_module (die, cu);
10738 break;
d9fa45fe 10739 case DW_TAG_imported_declaration:
74921315
KS
10740 cu->processing_has_namespace_info = 1;
10741 if (read_namespace_alias (die, cu))
10742 break;
10743 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 10744 case DW_TAG_imported_module:
4d4ec4e5 10745 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
10746 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10747 || cu->language != language_fortran))
10748 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10749 dwarf_tag_name (die->tag));
10750 read_import_statement (die, cu);
d9fa45fe 10751 break;
95554aad
TT
10752
10753 case DW_TAG_imported_unit:
10754 process_imported_unit_die (die, cu);
10755 break;
10756
71a3c369
TT
10757 case DW_TAG_variable:
10758 read_variable (die, cu);
10759 break;
10760
c906108c 10761 default:
e7c27a73 10762 new_symbol (die, NULL, cu);
c906108c
SS
10763 break;
10764 }
10765}
ca69b9e6
DE
10766\f
10767/* DWARF name computation. */
c906108c 10768
94af9270
KS
10769/* A helper function for dwarf2_compute_name which determines whether DIE
10770 needs to have the name of the scope prepended to the name listed in the
10771 die. */
10772
10773static int
10774die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10775{
1c809c68
TT
10776 struct attribute *attr;
10777
94af9270
KS
10778 switch (die->tag)
10779 {
10780 case DW_TAG_namespace:
10781 case DW_TAG_typedef:
10782 case DW_TAG_class_type:
10783 case DW_TAG_interface_type:
10784 case DW_TAG_structure_type:
10785 case DW_TAG_union_type:
10786 case DW_TAG_enumeration_type:
10787 case DW_TAG_enumerator:
10788 case DW_TAG_subprogram:
08a76f8a 10789 case DW_TAG_inlined_subroutine:
94af9270 10790 case DW_TAG_member:
74921315 10791 case DW_TAG_imported_declaration:
94af9270
KS
10792 return 1;
10793
10794 case DW_TAG_variable:
c2b0a229 10795 case DW_TAG_constant:
94af9270
KS
10796 /* We only need to prefix "globally" visible variables. These include
10797 any variable marked with DW_AT_external or any variable that
10798 lives in a namespace. [Variables in anonymous namespaces
10799 require prefixing, but they are not DW_AT_external.] */
10800
10801 if (dwarf2_attr (die, DW_AT_specification, cu))
10802 {
10803 struct dwarf2_cu *spec_cu = cu;
9a619af0 10804
94af9270
KS
10805 return die_needs_namespace (die_specification (die, &spec_cu),
10806 spec_cu);
10807 }
10808
1c809c68 10809 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10810 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10811 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10812 return 0;
10813 /* A variable in a lexical block of some kind does not need a
10814 namespace, even though in C++ such variables may be external
10815 and have a mangled name. */
10816 if (die->parent->tag == DW_TAG_lexical_block
10817 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10818 || die->parent->tag == DW_TAG_catch_block
10819 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10820 return 0;
10821 return 1;
94af9270
KS
10822
10823 default:
10824 return 0;
10825 }
10826}
10827
73b9be8b
KS
10828/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10829 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10830 defined for the given DIE. */
10831
10832static struct attribute *
10833dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10834{
10835 struct attribute *attr;
10836
10837 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10838 if (attr == NULL)
10839 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10840
10841 return attr;
10842}
10843
10844/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10845 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10846 defined for the given DIE. */
10847
10848static const char *
10849dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10850{
10851 const char *linkage_name;
10852
10853 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10854 if (linkage_name == NULL)
10855 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10856
10857 return linkage_name;
10858}
10859
94af9270 10860/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10861 compute the physname for the object, which include a method's:
9c37b5ae 10862 - formal parameters (C++),
a766d390 10863 - receiver type (Go),
a766d390
DE
10864
10865 The term "physname" is a bit confusing.
10866 For C++, for example, it is the demangled name.
10867 For Go, for example, it's the mangled name.
94af9270 10868
af6b7be1
JB
10869 For Ada, return the DIE's linkage name rather than the fully qualified
10870 name. PHYSNAME is ignored..
10871
94af9270
KS
10872 The result is allocated on the objfile_obstack and canonicalized. */
10873
10874static const char *
15d034d0
TT
10875dwarf2_compute_name (const char *name,
10876 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10877 int physname)
10878{
518817b3 10879 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10880
94af9270
KS
10881 if (name == NULL)
10882 name = dwarf2_name (die, cu);
10883
2ee7123e
DE
10884 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10885 but otherwise compute it by typename_concat inside GDB.
10886 FIXME: Actually this is not really true, or at least not always true.
10887 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 10888 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10889 will set the demangled name to the result of dwarf2_full_name, and it is
10890 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10891 if (cu->language == language_ada
10892 || (cu->language == language_fortran && physname))
10893 {
10894 /* For Ada unit, we prefer the linkage name over the name, as
10895 the former contains the exported name, which the user expects
10896 to be able to reference. Ideally, we want the user to be able
10897 to reference this entity using either natural or linkage name,
10898 but we haven't started looking at this enhancement yet. */
73b9be8b 10899 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10900
2ee7123e
DE
10901 if (linkage_name != NULL)
10902 return linkage_name;
f55ee35c
JK
10903 }
10904
94af9270
KS
10905 /* These are the only languages we know how to qualify names in. */
10906 if (name != NULL
9c37b5ae 10907 && (cu->language == language_cplus
c44af4eb
TT
10908 || cu->language == language_fortran || cu->language == language_d
10909 || cu->language == language_rust))
94af9270
KS
10910 {
10911 if (die_needs_namespace (die, cu))
10912 {
0d5cff50 10913 const char *prefix;
34a68019 10914 const char *canonical_name = NULL;
94af9270 10915
d7e74731
PA
10916 string_file buf;
10917
94af9270 10918 prefix = determine_prefix (die, cu);
94af9270
KS
10919 if (*prefix != '\0')
10920 {
f55ee35c
JK
10921 char *prefixed_name = typename_concat (NULL, prefix, name,
10922 physname, cu);
9a619af0 10923
d7e74731 10924 buf.puts (prefixed_name);
94af9270
KS
10925 xfree (prefixed_name);
10926 }
10927 else
d7e74731 10928 buf.puts (name);
94af9270 10929
98bfdba5
PA
10930 /* Template parameters may be specified in the DIE's DW_AT_name, or
10931 as children with DW_TAG_template_type_param or
10932 DW_TAG_value_type_param. If the latter, add them to the name
10933 here. If the name already has template parameters, then
10934 skip this step; some versions of GCC emit both, and
10935 it is more efficient to use the pre-computed name.
10936
10937 Something to keep in mind about this process: it is very
10938 unlikely, or in some cases downright impossible, to produce
10939 something that will match the mangled name of a function.
10940 If the definition of the function has the same debug info,
10941 we should be able to match up with it anyway. But fallbacks
10942 using the minimal symbol, for instance to find a method
10943 implemented in a stripped copy of libstdc++, will not work.
10944 If we do not have debug info for the definition, we will have to
10945 match them up some other way.
10946
10947 When we do name matching there is a related problem with function
10948 templates; two instantiated function templates are allowed to
10949 differ only by their return types, which we do not add here. */
10950
10951 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10952 {
10953 struct attribute *attr;
10954 struct die_info *child;
10955 int first = 1;
10956
10957 die->building_fullname = 1;
10958
10959 for (child = die->child; child != NULL; child = child->sibling)
10960 {
10961 struct type *type;
12df843f 10962 LONGEST value;
d521ce57 10963 const gdb_byte *bytes;
98bfdba5
PA
10964 struct dwarf2_locexpr_baton *baton;
10965 struct value *v;
10966
10967 if (child->tag != DW_TAG_template_type_param
10968 && child->tag != DW_TAG_template_value_param)
10969 continue;
10970
10971 if (first)
10972 {
d7e74731 10973 buf.puts ("<");
98bfdba5
PA
10974 first = 0;
10975 }
10976 else
d7e74731 10977 buf.puts (", ");
98bfdba5
PA
10978
10979 attr = dwarf2_attr (child, DW_AT_type, cu);
10980 if (attr == NULL)
10981 {
10982 complaint (&symfile_complaints,
10983 _("template parameter missing DW_AT_type"));
d7e74731 10984 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10985 continue;
10986 }
10987 type = die_type (child, cu);
10988
10989 if (child->tag == DW_TAG_template_type_param)
10990 {
d7e74731 10991 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
10992 continue;
10993 }
10994
10995 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10996 if (attr == NULL)
10997 {
10998 complaint (&symfile_complaints,
3e43a32a
MS
10999 _("template parameter missing "
11000 "DW_AT_const_value"));
d7e74731 11001 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
11002 continue;
11003 }
11004
11005 dwarf2_const_value_attr (attr, type, name,
11006 &cu->comp_unit_obstack, cu,
11007 &value, &bytes, &baton);
11008
11009 if (TYPE_NOSIGN (type))
11010 /* GDB prints characters as NUMBER 'CHAR'. If that's
11011 changed, this can use value_print instead. */
d7e74731 11012 c_printchar (value, type, &buf);
98bfdba5
PA
11013 else
11014 {
11015 struct value_print_options opts;
11016
11017 if (baton != NULL)
11018 v = dwarf2_evaluate_loc_desc (type, NULL,
11019 baton->data,
11020 baton->size,
11021 baton->per_cu);
11022 else if (bytes != NULL)
11023 {
11024 v = allocate_value (type);
11025 memcpy (value_contents_writeable (v), bytes,
11026 TYPE_LENGTH (type));
11027 }
11028 else
11029 v = value_from_longest (type, value);
11030
3e43a32a
MS
11031 /* Specify decimal so that we do not depend on
11032 the radix. */
98bfdba5
PA
11033 get_formatted_print_options (&opts, 'd');
11034 opts.raw = 1;
d7e74731 11035 value_print (v, &buf, &opts);
98bfdba5
PA
11036 release_value (v);
11037 value_free (v);
11038 }
11039 }
11040
11041 die->building_fullname = 0;
11042
11043 if (!first)
11044 {
11045 /* Close the argument list, with a space if necessary
11046 (nested templates). */
d7e74731
PA
11047 if (!buf.empty () && buf.string ().back () == '>')
11048 buf.puts (" >");
98bfdba5 11049 else
d7e74731 11050 buf.puts (">");
98bfdba5
PA
11051 }
11052 }
11053
9c37b5ae 11054 /* For C++ methods, append formal parameter type
94af9270 11055 information, if PHYSNAME. */
6e70227d 11056
94af9270 11057 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 11058 && cu->language == language_cplus)
94af9270
KS
11059 {
11060 struct type *type = read_type_die (die, cu);
11061
d7e74731 11062 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 11063 &type_print_raw_options);
94af9270 11064
9c37b5ae 11065 if (cu->language == language_cplus)
94af9270 11066 {
60430eff
DJ
11067 /* Assume that an artificial first parameter is
11068 "this", but do not crash if it is not. RealView
11069 marks unnamed (and thus unused) parameters as
11070 artificial; there is no way to differentiate
11071 the two cases. */
94af9270
KS
11072 if (TYPE_NFIELDS (type) > 0
11073 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11074 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11075 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11076 0))))
d7e74731 11077 buf.puts (" const");
94af9270
KS
11078 }
11079 }
11080
d7e74731 11081 const std::string &intermediate_name = buf.string ();
94af9270
KS
11082
11083 if (cu->language == language_cplus)
34a68019 11084 canonical_name
322a8516 11085 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11086 &objfile->per_bfd->storage_obstack);
11087
11088 /* If we only computed INTERMEDIATE_NAME, or if
11089 INTERMEDIATE_NAME is already canonical, then we need to
11090 copy it to the appropriate obstack. */
322a8516 11091 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
11092 name = ((const char *)
11093 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
11094 intermediate_name.c_str (),
11095 intermediate_name.length ()));
34a68019
TT
11096 else
11097 name = canonical_name;
94af9270
KS
11098 }
11099 }
11100
11101 return name;
11102}
11103
0114d602
DJ
11104/* Return the fully qualified name of DIE, based on its DW_AT_name.
11105 If scope qualifiers are appropriate they will be added. The result
34a68019 11106 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11107 not have a name. NAME may either be from a previous call to
11108 dwarf2_name or NULL.
11109
9c37b5ae 11110 The output string will be canonicalized (if C++). */
0114d602
DJ
11111
11112static const char *
15d034d0 11113dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11114{
94af9270
KS
11115 return dwarf2_compute_name (name, die, cu, 0);
11116}
0114d602 11117
94af9270
KS
11118/* Construct a physname for the given DIE in CU. NAME may either be
11119 from a previous call to dwarf2_name or NULL. The result will be
11120 allocated on the objfile_objstack or NULL if the DIE does not have a
11121 name.
0114d602 11122
9c37b5ae 11123 The output string will be canonicalized (if C++). */
0114d602 11124
94af9270 11125static const char *
15d034d0 11126dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11127{
518817b3 11128 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11129 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11130 int need_copy = 1;
11131
11132 /* In this case dwarf2_compute_name is just a shortcut not building anything
11133 on its own. */
11134 if (!die_needs_namespace (die, cu))
11135 return dwarf2_compute_name (name, die, cu, 1);
11136
73b9be8b 11137 mangled = dw2_linkage_name (die, cu);
900e11f9 11138
e98c9e7c
TT
11139 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11140 See https://github.com/rust-lang/rust/issues/32925. */
11141 if (cu->language == language_rust && mangled != NULL
11142 && strchr (mangled, '{') != NULL)
11143 mangled = NULL;
11144
900e11f9
JK
11145 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11146 has computed. */
791afaa2 11147 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11148 if (mangled != NULL)
900e11f9 11149 {
900e11f9 11150
a766d390
DE
11151 if (cu->language == language_go)
11152 {
5e2db402
TT
11153 /* This is a lie, but we already lie to the caller new_symbol.
11154 new_symbol assumes we return the mangled name.
a766d390 11155 This just undoes that lie until things are cleaned up. */
a766d390
DE
11156 }
11157 else
11158 {
0eb876f5
JB
11159 /* Use DMGL_RET_DROP for C++ template functions to suppress
11160 their return type. It is easier for GDB users to search
11161 for such functions as `name(params)' than `long name(params)'.
11162 In such case the minimal symbol names do not match the full
11163 symbol names but for template functions there is never a need
11164 to look up their definition from their declaration so
11165 the only disadvantage remains the minimal symbol variant
11166 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11167 demangled.reset (gdb_demangle (mangled,
11168 (DMGL_PARAMS | DMGL_ANSI
11169 | DMGL_RET_DROP)));
a766d390 11170 }
900e11f9 11171 if (demangled)
791afaa2 11172 canon = demangled.get ();
900e11f9
JK
11173 else
11174 {
11175 canon = mangled;
11176 need_copy = 0;
11177 }
11178 }
11179
11180 if (canon == NULL || check_physname)
11181 {
11182 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11183
11184 if (canon != NULL && strcmp (physname, canon) != 0)
11185 {
11186 /* It may not mean a bug in GDB. The compiler could also
11187 compute DW_AT_linkage_name incorrectly. But in such case
11188 GDB would need to be bug-to-bug compatible. */
11189
11190 complaint (&symfile_complaints,
11191 _("Computed physname <%s> does not match demangled <%s> "
11192 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9c541725 11193 physname, canon, mangled, to_underlying (die->sect_off),
4262abfb 11194 objfile_name (objfile));
900e11f9
JK
11195
11196 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11197 is available here - over computed PHYSNAME. It is safer
11198 against both buggy GDB and buggy compilers. */
11199
11200 retval = canon;
11201 }
11202 else
11203 {
11204 retval = physname;
11205 need_copy = 0;
11206 }
11207 }
11208 else
11209 retval = canon;
11210
11211 if (need_copy)
224c3ddb
SM
11212 retval = ((const char *)
11213 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11214 retval, strlen (retval)));
900e11f9 11215
900e11f9 11216 return retval;
0114d602
DJ
11217}
11218
74921315
KS
11219/* Inspect DIE in CU for a namespace alias. If one exists, record
11220 a new symbol for it.
11221
11222 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11223
11224static int
11225read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11226{
11227 struct attribute *attr;
11228
11229 /* If the die does not have a name, this is not a namespace
11230 alias. */
11231 attr = dwarf2_attr (die, DW_AT_name, cu);
11232 if (attr != NULL)
11233 {
11234 int num;
11235 struct die_info *d = die;
11236 struct dwarf2_cu *imported_cu = cu;
11237
11238 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11239 keep inspecting DIEs until we hit the underlying import. */
11240#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11241 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11242 {
11243 attr = dwarf2_attr (d, DW_AT_import, cu);
11244 if (attr == NULL)
11245 break;
11246
11247 d = follow_die_ref (d, attr, &imported_cu);
11248 if (d->tag != DW_TAG_imported_declaration)
11249 break;
11250 }
11251
11252 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11253 {
11254 complaint (&symfile_complaints,
11255 _("DIE at 0x%x has too many recursively imported "
9c541725 11256 "declarations"), to_underlying (d->sect_off));
74921315
KS
11257 return 0;
11258 }
11259
11260 if (attr != NULL)
11261 {
11262 struct type *type;
9c541725 11263 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11264
9c541725 11265 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11266 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11267 {
11268 /* This declaration is a global namespace alias. Add
11269 a symbol for it whose type is the aliased namespace. */
11270 new_symbol (die, type, cu);
11271 return 1;
11272 }
11273 }
11274 }
11275
11276 return 0;
11277}
11278
22cee43f
PMR
11279/* Return the using directives repository (global or local?) to use in the
11280 current context for LANGUAGE.
11281
11282 For Ada, imported declarations can materialize renamings, which *may* be
11283 global. However it is impossible (for now?) in DWARF to distinguish
11284 "external" imported declarations and "static" ones. As all imported
11285 declarations seem to be static in all other languages, make them all CU-wide
11286 global only in Ada. */
11287
11288static struct using_direct **
11289using_directives (enum language language)
11290{
11291 if (language == language_ada && context_stack_depth == 0)
11292 return &global_using_directives;
11293 else
11294 return &local_using_directives;
11295}
11296
27aa8d6a
SW
11297/* Read the import statement specified by the given die and record it. */
11298
11299static void
11300read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11301{
518817b3 11302 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11303 struct attribute *import_attr;
32019081 11304 struct die_info *imported_die, *child_die;
de4affc9 11305 struct dwarf2_cu *imported_cu;
27aa8d6a 11306 const char *imported_name;
794684b6 11307 const char *imported_name_prefix;
13387711
SW
11308 const char *canonical_name;
11309 const char *import_alias;
11310 const char *imported_declaration = NULL;
794684b6 11311 const char *import_prefix;
eb1e02fd 11312 std::vector<const char *> excludes;
13387711 11313
27aa8d6a
SW
11314 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11315 if (import_attr == NULL)
11316 {
11317 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11318 dwarf_tag_name (die->tag));
11319 return;
11320 }
11321
de4affc9
CC
11322 imported_cu = cu;
11323 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11324 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11325 if (imported_name == NULL)
11326 {
11327 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11328
11329 The import in the following code:
11330 namespace A
11331 {
11332 typedef int B;
11333 }
11334
11335 int main ()
11336 {
11337 using A::B;
11338 B b;
11339 return b;
11340 }
11341
11342 ...
11343 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11344 <52> DW_AT_decl_file : 1
11345 <53> DW_AT_decl_line : 6
11346 <54> DW_AT_import : <0x75>
11347 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11348 <59> DW_AT_name : B
11349 <5b> DW_AT_decl_file : 1
11350 <5c> DW_AT_decl_line : 2
11351 <5d> DW_AT_type : <0x6e>
11352 ...
11353 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11354 <76> DW_AT_byte_size : 4
11355 <77> DW_AT_encoding : 5 (signed)
11356
11357 imports the wrong die ( 0x75 instead of 0x58 ).
11358 This case will be ignored until the gcc bug is fixed. */
11359 return;
11360 }
11361
82856980
SW
11362 /* Figure out the local name after import. */
11363 import_alias = dwarf2_name (die, cu);
27aa8d6a 11364
794684b6
SW
11365 /* Figure out where the statement is being imported to. */
11366 import_prefix = determine_prefix (die, cu);
11367
11368 /* Figure out what the scope of the imported die is and prepend it
11369 to the name of the imported die. */
de4affc9 11370 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11371
f55ee35c
JK
11372 if (imported_die->tag != DW_TAG_namespace
11373 && imported_die->tag != DW_TAG_module)
794684b6 11374 {
13387711
SW
11375 imported_declaration = imported_name;
11376 canonical_name = imported_name_prefix;
794684b6 11377 }
13387711 11378 else if (strlen (imported_name_prefix) > 0)
12aaed36 11379 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11380 imported_name_prefix,
11381 (cu->language == language_d ? "." : "::"),
11382 imported_name, (char *) NULL);
13387711
SW
11383 else
11384 canonical_name = imported_name;
794684b6 11385
32019081
JK
11386 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11387 for (child_die = die->child; child_die && child_die->tag;
11388 child_die = sibling_die (child_die))
11389 {
11390 /* DWARF-4: A Fortran use statement with a “rename list” may be
11391 represented by an imported module entry with an import attribute
11392 referring to the module and owned entries corresponding to those
11393 entities that are renamed as part of being imported. */
11394
11395 if (child_die->tag != DW_TAG_imported_declaration)
11396 {
11397 complaint (&symfile_complaints,
11398 _("child DW_TAG_imported_declaration expected "
11399 "- DIE at 0x%x [in module %s]"),
9c541725 11400 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
11401 continue;
11402 }
11403
11404 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11405 if (import_attr == NULL)
11406 {
11407 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11408 dwarf_tag_name (child_die->tag));
11409 continue;
11410 }
11411
11412 imported_cu = cu;
11413 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11414 &imported_cu);
11415 imported_name = dwarf2_name (imported_die, imported_cu);
11416 if (imported_name == NULL)
11417 {
11418 complaint (&symfile_complaints,
11419 _("child DW_TAG_imported_declaration has unknown "
11420 "imported name - DIE at 0x%x [in module %s]"),
9c541725 11421 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
11422 continue;
11423 }
11424
eb1e02fd 11425 excludes.push_back (imported_name);
32019081
JK
11426
11427 process_die (child_die, cu);
11428 }
11429
22cee43f
PMR
11430 add_using_directive (using_directives (cu->language),
11431 import_prefix,
11432 canonical_name,
11433 import_alias,
11434 imported_declaration,
11435 excludes,
11436 0,
11437 &objfile->objfile_obstack);
27aa8d6a
SW
11438}
11439
5230b05a
WT
11440/* ICC<14 does not output the required DW_AT_declaration on incomplete
11441 types, but gives them a size of zero. Starting with version 14,
11442 ICC is compatible with GCC. */
11443
11444static int
11445producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11446{
11447 if (!cu->checked_producer)
11448 check_producer (cu);
11449
11450 return cu->producer_is_icc_lt_14;
11451}
11452
1b80a9fa
JK
11453/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11454 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11455 this, it was first present in GCC release 4.3.0. */
11456
11457static int
11458producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11459{
11460 if (!cu->checked_producer)
11461 check_producer (cu);
11462
11463 return cu->producer_is_gcc_lt_4_3;
11464}
11465
d721ba37
PA
11466static file_and_directory
11467find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11468{
d721ba37
PA
11469 file_and_directory res;
11470
9291a0cd
TT
11471 /* Find the filename. Do not use dwarf2_name here, since the filename
11472 is not a source language identifier. */
d721ba37
PA
11473 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11474 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11475
d721ba37
PA
11476 if (res.comp_dir == NULL
11477 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11478 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11479 {
d721ba37
PA
11480 res.comp_dir_storage = ldirname (res.name);
11481 if (!res.comp_dir_storage.empty ())
11482 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11483 }
d721ba37 11484 if (res.comp_dir != NULL)
9291a0cd
TT
11485 {
11486 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11487 directory, get rid of it. */
d721ba37 11488 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11489
d721ba37
PA
11490 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11491 res.comp_dir = cp + 1;
9291a0cd
TT
11492 }
11493
d721ba37
PA
11494 if (res.name == NULL)
11495 res.name = "<unknown>";
11496
11497 return res;
9291a0cd
TT
11498}
11499
f4dc4d17
DE
11500/* Handle DW_AT_stmt_list for a compilation unit.
11501 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11502 COMP_DIR is the compilation directory. LOWPC is passed to
11503 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11504
11505static void
11506handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11507 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11508{
518817b3
SM
11509 struct dwarf2_per_objfile *dwarf2_per_objfile
11510 = cu->per_cu->dwarf2_per_objfile;
527f3840 11511 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11512 struct attribute *attr;
527f3840
JK
11513 struct line_header line_header_local;
11514 hashval_t line_header_local_hash;
527f3840
JK
11515 void **slot;
11516 int decode_mapping;
2ab95328 11517
f4dc4d17
DE
11518 gdb_assert (! cu->per_cu->is_debug_types);
11519
2ab95328 11520 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11521 if (attr == NULL)
11522 return;
11523
9c541725 11524 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11525
11526 /* The line header hash table is only created if needed (it exists to
11527 prevent redundant reading of the line table for partial_units).
11528 If we're given a partial_unit, we'll need it. If we're given a
11529 compile_unit, then use the line header hash table if it's already
11530 created, but don't create one just yet. */
11531
11532 if (dwarf2_per_objfile->line_header_hash == NULL
11533 && die->tag == DW_TAG_partial_unit)
2ab95328 11534 {
527f3840
JK
11535 dwarf2_per_objfile->line_header_hash
11536 = htab_create_alloc_ex (127, line_header_hash_voidp,
11537 line_header_eq_voidp,
11538 free_line_header_voidp,
11539 &objfile->objfile_obstack,
11540 hashtab_obstack_allocate,
11541 dummy_obstack_deallocate);
11542 }
2ab95328 11543
9c541725 11544 line_header_local.sect_off = line_offset;
527f3840
JK
11545 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11546 line_header_local_hash = line_header_hash (&line_header_local);
11547 if (dwarf2_per_objfile->line_header_hash != NULL)
11548 {
11549 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11550 &line_header_local,
11551 line_header_local_hash, NO_INSERT);
11552
11553 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11554 is not present in *SLOT (since if there is something in *SLOT then
11555 it will be for a partial_unit). */
11556 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11557 {
527f3840 11558 gdb_assert (*slot != NULL);
9a3c8263 11559 cu->line_header = (struct line_header *) *slot;
527f3840 11560 return;
dee91e82 11561 }
2ab95328 11562 }
527f3840
JK
11563
11564 /* dwarf_decode_line_header does not yet provide sufficient information.
11565 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11566 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11567 if (lh == NULL)
527f3840 11568 return;
4c8aa72d
PA
11569
11570 cu->line_header = lh.release ();
11571 cu->line_header_die_owner = die;
527f3840
JK
11572
11573 if (dwarf2_per_objfile->line_header_hash == NULL)
11574 slot = NULL;
11575 else
11576 {
11577 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11578 &line_header_local,
11579 line_header_local_hash, INSERT);
11580 gdb_assert (slot != NULL);
11581 }
11582 if (slot != NULL && *slot == NULL)
11583 {
11584 /* This newly decoded line number information unit will be owned
11585 by line_header_hash hash table. */
11586 *slot = cu->line_header;
4c8aa72d 11587 cu->line_header_die_owner = NULL;
527f3840
JK
11588 }
11589 else
11590 {
11591 /* We cannot free any current entry in (*slot) as that struct line_header
11592 may be already used by multiple CUs. Create only temporary decoded
11593 line_header for this CU - it may happen at most once for each line
11594 number information unit. And if we're not using line_header_hash
11595 then this is what we want as well. */
11596 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11597 }
11598 decode_mapping = (die->tag != DW_TAG_partial_unit);
11599 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11600 decode_mapping);
fff8551c 11601
2ab95328
TT
11602}
11603
95554aad 11604/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11605
c906108c 11606static void
e7c27a73 11607read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11608{
518817b3
SM
11609 struct dwarf2_per_objfile *dwarf2_per_objfile
11610 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11611 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11612 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11613 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11614 CORE_ADDR highpc = ((CORE_ADDR) 0);
11615 struct attribute *attr;
c906108c 11616 struct die_info *child_die;
e142c38c 11617 CORE_ADDR baseaddr;
6e70227d 11618
e142c38c 11619 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11620
fae299cd 11621 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11622
11623 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11624 from finish_block. */
2acceee2 11625 if (lowpc == ((CORE_ADDR) -1))
c906108c 11626 lowpc = highpc;
3e29f34a 11627 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11628
d721ba37 11629 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11630
95554aad 11631 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 11632
f4b8a18d
KW
11633 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11634 standardised yet. As a workaround for the language detection we fall
11635 back to the DW_AT_producer string. */
11636 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11637 cu->language = language_opencl;
11638
3019eac3
DE
11639 /* Similar hack for Go. */
11640 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11641 set_cu_language (DW_LANG_Go, cu);
11642
d721ba37 11643 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11644
11645 /* Decode line number information if present. We do this before
11646 processing child DIEs, so that the line header table is available
11647 for DW_AT_decl_file. */
d721ba37 11648 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11649
11650 /* Process all dies in compilation unit. */
11651 if (die->child != NULL)
11652 {
11653 child_die = die->child;
11654 while (child_die && child_die->tag)
11655 {
11656 process_die (child_die, cu);
11657 child_die = sibling_die (child_die);
11658 }
11659 }
11660
11661 /* Decode macro information, if present. Dwarf 2 macro information
11662 refers to information in the line number info statement program
11663 header, so we can only read it if we've read the header
11664 successfully. */
0af92d60
JK
11665 attr = dwarf2_attr (die, DW_AT_macros, cu);
11666 if (attr == NULL)
11667 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11668 if (attr && cu->line_header)
11669 {
11670 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11671 complaint (&symfile_complaints,
0af92d60 11672 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11673
43f3e411 11674 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11675 }
11676 else
11677 {
11678 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11679 if (attr && cu->line_header)
11680 {
11681 unsigned int macro_offset = DW_UNSND (attr);
11682
43f3e411 11683 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11684 }
11685 }
3019eac3
DE
11686}
11687
f4dc4d17
DE
11688/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11689 Create the set of symtabs used by this TU, or if this TU is sharing
11690 symtabs with another TU and the symtabs have already been created
11691 then restore those symtabs in the line header.
11692 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
11693
11694static void
f4dc4d17 11695setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 11696{
f4dc4d17
DE
11697 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11698 struct type_unit_group *tu_group;
11699 int first_time;
3019eac3 11700 struct attribute *attr;
9c541725 11701 unsigned int i;
0186c6a7 11702 struct signatured_type *sig_type;
3019eac3 11703
f4dc4d17 11704 gdb_assert (per_cu->is_debug_types);
0186c6a7 11705 sig_type = (struct signatured_type *) per_cu;
3019eac3 11706
f4dc4d17 11707 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 11708
f4dc4d17 11709 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11710 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
11711 if (sig_type->type_unit_group == NULL)
11712 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11713 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11714
11715 /* If we've already processed this stmt_list there's no real need to
11716 do it again, we could fake it and just recreate the part we need
11717 (file name,index -> symtab mapping). If data shows this optimization
11718 is useful we can do it then. */
43f3e411 11719 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11720
11721 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11722 debug info. */
fff8551c 11723 line_header_up lh;
f4dc4d17 11724 if (attr != NULL)
3019eac3 11725 {
9c541725 11726 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
11727 lh = dwarf_decode_line_header (line_offset, cu);
11728 }
11729 if (lh == NULL)
11730 {
11731 if (first_time)
11732 dwarf2_start_symtab (cu, "", NULL, 0);
11733 else
11734 {
11735 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 11736 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11737 }
f4dc4d17 11738 return;
3019eac3
DE
11739 }
11740
4c8aa72d
PA
11741 cu->line_header = lh.release ();
11742 cu->line_header_die_owner = die;
3019eac3 11743
f4dc4d17
DE
11744 if (first_time)
11745 {
43f3e411 11746 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 11747
1fd60fc0
DE
11748 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11749 still initializing it, and our caller (a few levels up)
11750 process_full_type_unit still needs to know if this is the first
11751 time. */
11752
4c8aa72d
PA
11753 tu_group->num_symtabs = cu->line_header->file_names.size ();
11754 tu_group->symtabs = XNEWVEC (struct symtab *,
11755 cu->line_header->file_names.size ());
3019eac3 11756
4c8aa72d 11757 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11758 {
4c8aa72d 11759 file_entry &fe = cu->line_header->file_names[i];
3019eac3 11760
4c8aa72d 11761 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 11762
f4dc4d17
DE
11763 if (current_subfile->symtab == NULL)
11764 {
4c8aa72d
PA
11765 /* NOTE: start_subfile will recognize when it's been
11766 passed a file it has already seen. So we can't
11767 assume there's a simple mapping from
11768 cu->line_header->file_names to subfiles, plus
11769 cu->line_header->file_names may contain dups. */
43f3e411
DE
11770 current_subfile->symtab
11771 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
11772 }
11773
8c43009f
PA
11774 fe.symtab = current_subfile->symtab;
11775 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11776 }
11777 }
11778 else
3019eac3 11779 {
0ab9ce85 11780 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11781
4c8aa72d 11782 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11783 {
4c8aa72d 11784 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 11785
4c8aa72d 11786 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11787 }
3019eac3
DE
11788 }
11789
f4dc4d17
DE
11790 /* The main symtab is allocated last. Type units don't have DW_AT_name
11791 so they don't have a "real" (so to speak) symtab anyway.
11792 There is later code that will assign the main symtab to all symbols
11793 that don't have one. We need to handle the case of a symbol with a
11794 missing symtab (DW_AT_decl_file) anyway. */
11795}
3019eac3 11796
f4dc4d17
DE
11797/* Process DW_TAG_type_unit.
11798 For TUs we want to skip the first top level sibling if it's not the
11799 actual type being defined by this TU. In this case the first top
11800 level sibling is there to provide context only. */
3019eac3 11801
f4dc4d17
DE
11802static void
11803read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11804{
11805 struct die_info *child_die;
3019eac3 11806
f4dc4d17
DE
11807 prepare_one_comp_unit (cu, die, language_minimal);
11808
11809 /* Initialize (or reinitialize) the machinery for building symtabs.
11810 We do this before processing child DIEs, so that the line header table
11811 is available for DW_AT_decl_file. */
11812 setup_type_unit_groups (die, cu);
11813
11814 if (die->child != NULL)
11815 {
11816 child_die = die->child;
11817 while (child_die && child_die->tag)
11818 {
11819 process_die (child_die, cu);
11820 child_die = sibling_die (child_die);
11821 }
11822 }
3019eac3
DE
11823}
11824\f
80626a55
DE
11825/* DWO/DWP files.
11826
11827 http://gcc.gnu.org/wiki/DebugFission
11828 http://gcc.gnu.org/wiki/DebugFissionDWP
11829
11830 To simplify handling of both DWO files ("object" files with the DWARF info)
11831 and DWP files (a file with the DWOs packaged up into one file), we treat
11832 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11833
11834static hashval_t
11835hash_dwo_file (const void *item)
11836{
9a3c8263 11837 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11838 hashval_t hash;
3019eac3 11839
a2ce51a0
DE
11840 hash = htab_hash_string (dwo_file->dwo_name);
11841 if (dwo_file->comp_dir != NULL)
11842 hash += htab_hash_string (dwo_file->comp_dir);
11843 return hash;
3019eac3
DE
11844}
11845
11846static int
11847eq_dwo_file (const void *item_lhs, const void *item_rhs)
11848{
9a3c8263
SM
11849 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11850 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11851
a2ce51a0
DE
11852 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11853 return 0;
11854 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11855 return lhs->comp_dir == rhs->comp_dir;
11856 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11857}
11858
11859/* Allocate a hash table for DWO files. */
11860
11861static htab_t
ed2dc618 11862allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11863{
3019eac3
DE
11864 return htab_create_alloc_ex (41,
11865 hash_dwo_file,
11866 eq_dwo_file,
11867 NULL,
11868 &objfile->objfile_obstack,
11869 hashtab_obstack_allocate,
11870 dummy_obstack_deallocate);
11871}
11872
80626a55
DE
11873/* Lookup DWO file DWO_NAME. */
11874
11875static void **
ed2dc618
SM
11876lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11877 const char *dwo_name,
11878 const char *comp_dir)
80626a55
DE
11879{
11880 struct dwo_file find_entry;
11881 void **slot;
11882
11883 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11884 dwarf2_per_objfile->dwo_files
11885 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55
DE
11886
11887 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
11888 find_entry.dwo_name = dwo_name;
11889 find_entry.comp_dir = comp_dir;
80626a55
DE
11890 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11891
11892 return slot;
11893}
11894
3019eac3
DE
11895static hashval_t
11896hash_dwo_unit (const void *item)
11897{
9a3c8263 11898 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11899
11900 /* This drops the top 32 bits of the id, but is ok for a hash. */
11901 return dwo_unit->signature;
11902}
11903
11904static int
11905eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11906{
9a3c8263
SM
11907 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11908 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11909
11910 /* The signature is assumed to be unique within the DWO file.
11911 So while object file CU dwo_id's always have the value zero,
11912 that's OK, assuming each object file DWO file has only one CU,
11913 and that's the rule for now. */
11914 return lhs->signature == rhs->signature;
11915}
11916
11917/* Allocate a hash table for DWO CUs,TUs.
11918 There is one of these tables for each of CUs,TUs for each DWO file. */
11919
11920static htab_t
11921allocate_dwo_unit_table (struct objfile *objfile)
11922{
11923 /* Start out with a pretty small number.
11924 Generally DWO files contain only one CU and maybe some TUs. */
11925 return htab_create_alloc_ex (3,
11926 hash_dwo_unit,
11927 eq_dwo_unit,
11928 NULL,
11929 &objfile->objfile_obstack,
11930 hashtab_obstack_allocate,
11931 dummy_obstack_deallocate);
11932}
11933
80626a55 11934/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11935
19c3d4c9 11936struct create_dwo_cu_data
3019eac3
DE
11937{
11938 struct dwo_file *dwo_file;
19c3d4c9 11939 struct dwo_unit dwo_unit;
3019eac3
DE
11940};
11941
19c3d4c9 11942/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11943
11944static void
19c3d4c9
DE
11945create_dwo_cu_reader (const struct die_reader_specs *reader,
11946 const gdb_byte *info_ptr,
11947 struct die_info *comp_unit_die,
11948 int has_children,
11949 void *datap)
3019eac3
DE
11950{
11951 struct dwarf2_cu *cu = reader->cu;
9c541725 11952 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11953 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11954 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11955 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11956 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11957 struct attribute *attr;
3019eac3
DE
11958
11959 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11960 if (attr == NULL)
11961 {
19c3d4c9
DE
11962 complaint (&symfile_complaints,
11963 _("Dwarf Error: debug entry at offset 0x%x is missing"
11964 " its dwo_id [in module %s]"),
9c541725 11965 to_underlying (sect_off), dwo_file->dwo_name);
3019eac3
DE
11966 return;
11967 }
11968
3019eac3
DE
11969 dwo_unit->dwo_file = dwo_file;
11970 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 11971 dwo_unit->section = section;
9c541725 11972 dwo_unit->sect_off = sect_off;
3019eac3
DE
11973 dwo_unit->length = cu->per_cu->length;
11974
b4f54984 11975 if (dwarf_read_debug)
4031ecc5 11976 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9c541725
PA
11977 to_underlying (sect_off),
11978 hex_string (dwo_unit->signature));
3019eac3
DE
11979}
11980
33c5cd75 11981/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11982 Note: This function processes DWO files only, not DWP files. */
3019eac3 11983
33c5cd75 11984static void
ed2dc618
SM
11985create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11986 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11987 htab_t &cus_htab)
3019eac3
DE
11988{
11989 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11990 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11991
33c5cd75
DB
11992 dwarf2_read_section (objfile, &section);
11993 info_ptr = section.buffer;
3019eac3
DE
11994
11995 if (info_ptr == NULL)
33c5cd75 11996 return;
3019eac3 11997
b4f54984 11998 if (dwarf_read_debug)
19c3d4c9
DE
11999 {
12000 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
12001 get_section_name (&section),
12002 get_section_file_name (&section));
19c3d4c9 12003 }
3019eac3 12004
33c5cd75 12005 end_ptr = info_ptr + section.size;
3019eac3
DE
12006 while (info_ptr < end_ptr)
12007 {
12008 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
12009 struct create_dwo_cu_data create_dwo_cu_data;
12010 struct dwo_unit *dwo_unit;
12011 void **slot;
12012 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 12013
19c3d4c9
DE
12014 memset (&create_dwo_cu_data.dwo_unit, 0,
12015 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 12016 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 12017 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 12018 per_cu.is_debug_types = 0;
33c5cd75
DB
12019 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12020 per_cu.section = &section;
c5ed0576 12021 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
12022
12023 init_cutu_and_read_dies_no_follow (
12024 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12025 info_ptr += per_cu.length;
12026
12027 // If the unit could not be parsed, skip it.
12028 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12029 continue;
3019eac3 12030
33c5cd75
DB
12031 if (cus_htab == NULL)
12032 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 12033
33c5cd75
DB
12034 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12035 *dwo_unit = create_dwo_cu_data.dwo_unit;
12036 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12037 gdb_assert (slot != NULL);
12038 if (*slot != NULL)
19c3d4c9 12039 {
33c5cd75
DB
12040 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12041 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 12042
33c5cd75
DB
12043 complaint (&symfile_complaints,
12044 _("debug cu entry at offset 0x%x is duplicate to"
12045 " the entry at offset 0x%x, signature %s"),
12046 to_underlying (sect_off), to_underlying (dup_sect_off),
12047 hex_string (dwo_unit->signature));
19c3d4c9 12048 }
33c5cd75 12049 *slot = (void *)dwo_unit;
3019eac3 12050 }
3019eac3
DE
12051}
12052
80626a55
DE
12053/* DWP file .debug_{cu,tu}_index section format:
12054 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12055
d2415c6c
DE
12056 DWP Version 1:
12057
80626a55
DE
12058 Both index sections have the same format, and serve to map a 64-bit
12059 signature to a set of section numbers. Each section begins with a header,
12060 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12061 indexes, and a pool of 32-bit section numbers. The index sections will be
12062 aligned at 8-byte boundaries in the file.
12063
d2415c6c
DE
12064 The index section header consists of:
12065
12066 V, 32 bit version number
12067 -, 32 bits unused
12068 N, 32 bit number of compilation units or type units in the index
12069 M, 32 bit number of slots in the hash table
80626a55 12070
d2415c6c 12071 Numbers are recorded using the byte order of the application binary.
80626a55 12072
d2415c6c
DE
12073 The hash table begins at offset 16 in the section, and consists of an array
12074 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12075 order of the application binary). Unused slots in the hash table are 0.
12076 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12077
d2415c6c
DE
12078 The parallel table begins immediately after the hash table
12079 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12080 array of 32-bit indexes (using the byte order of the application binary),
12081 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12082 table contains a 32-bit index into the pool of section numbers. For unused
12083 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12084
73869dc2
DE
12085 The pool of section numbers begins immediately following the hash table
12086 (at offset 16 + 12 * M from the beginning of the section). The pool of
12087 section numbers consists of an array of 32-bit words (using the byte order
12088 of the application binary). Each item in the array is indexed starting
12089 from 0. The hash table entry provides the index of the first section
12090 number in the set. Additional section numbers in the set follow, and the
12091 set is terminated by a 0 entry (section number 0 is not used in ELF).
12092
12093 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12094 section must be the first entry in the set, and the .debug_abbrev.dwo must
12095 be the second entry. Other members of the set may follow in any order.
12096
12097 ---
12098
12099 DWP Version 2:
12100
12101 DWP Version 2 combines all the .debug_info, etc. sections into one,
12102 and the entries in the index tables are now offsets into these sections.
12103 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12104 section.
12105
12106 Index Section Contents:
12107 Header
12108 Hash Table of Signatures dwp_hash_table.hash_table
12109 Parallel Table of Indices dwp_hash_table.unit_table
12110 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12111 Table of Section Sizes dwp_hash_table.v2.sizes
12112
12113 The index section header consists of:
12114
12115 V, 32 bit version number
12116 L, 32 bit number of columns in the table of section offsets
12117 N, 32 bit number of compilation units or type units in the index
12118 M, 32 bit number of slots in the hash table
12119
12120 Numbers are recorded using the byte order of the application binary.
12121
12122 The hash table has the same format as version 1.
12123 The parallel table of indices has the same format as version 1,
12124 except that the entries are origin-1 indices into the table of sections
12125 offsets and the table of section sizes.
12126
12127 The table of offsets begins immediately following the parallel table
12128 (at offset 16 + 12 * M from the beginning of the section). The table is
12129 a two-dimensional array of 32-bit words (using the byte order of the
12130 application binary), with L columns and N+1 rows, in row-major order.
12131 Each row in the array is indexed starting from 0. The first row provides
12132 a key to the remaining rows: each column in this row provides an identifier
12133 for a debug section, and the offsets in the same column of subsequent rows
12134 refer to that section. The section identifiers are:
12135
12136 DW_SECT_INFO 1 .debug_info.dwo
12137 DW_SECT_TYPES 2 .debug_types.dwo
12138 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12139 DW_SECT_LINE 4 .debug_line.dwo
12140 DW_SECT_LOC 5 .debug_loc.dwo
12141 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12142 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12143 DW_SECT_MACRO 8 .debug_macro.dwo
12144
12145 The offsets provided by the CU and TU index sections are the base offsets
12146 for the contributions made by each CU or TU to the corresponding section
12147 in the package file. Each CU and TU header contains an abbrev_offset
12148 field, used to find the abbreviations table for that CU or TU within the
12149 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12150 be interpreted as relative to the base offset given in the index section.
12151 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12152 should be interpreted as relative to the base offset for .debug_line.dwo,
12153 and offsets into other debug sections obtained from DWARF attributes should
12154 also be interpreted as relative to the corresponding base offset.
12155
12156 The table of sizes begins immediately following the table of offsets.
12157 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12158 with L columns and N rows, in row-major order. Each row in the array is
12159 indexed starting from 1 (row 0 is shared by the two tables).
12160
12161 ---
12162
12163 Hash table lookup is handled the same in version 1 and 2:
12164
12165 We assume that N and M will not exceed 2^32 - 1.
12166 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12167
d2415c6c
DE
12168 Given a 64-bit compilation unit signature or a type signature S, an entry
12169 in the hash table is located as follows:
80626a55 12170
d2415c6c
DE
12171 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12172 the low-order k bits all set to 1.
80626a55 12173
d2415c6c 12174 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12175
d2415c6c
DE
12176 3) If the hash table entry at index H matches the signature, use that
12177 entry. If the hash table entry at index H is unused (all zeroes),
12178 terminate the search: the signature is not present in the table.
80626a55 12179
d2415c6c 12180 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12181
d2415c6c 12182 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12183 to stop at an unused slot or find the match. */
80626a55
DE
12184
12185/* Create a hash table to map DWO IDs to their CU/TU entry in
12186 .debug_{info,types}.dwo in DWP_FILE.
12187 Returns NULL if there isn't one.
12188 Note: This function processes DWP files only, not DWO files. */
12189
12190static struct dwp_hash_table *
ed2dc618
SM
12191create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12192 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12193{
12194 struct objfile *objfile = dwarf2_per_objfile->objfile;
12195 bfd *dbfd = dwp_file->dbfd;
948f8e3d 12196 const gdb_byte *index_ptr, *index_end;
80626a55 12197 struct dwarf2_section_info *index;
73869dc2 12198 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12199 struct dwp_hash_table *htab;
12200
12201 if (is_debug_types)
12202 index = &dwp_file->sections.tu_index;
12203 else
12204 index = &dwp_file->sections.cu_index;
12205
12206 if (dwarf2_section_empty_p (index))
12207 return NULL;
12208 dwarf2_read_section (objfile, index);
12209
12210 index_ptr = index->buffer;
12211 index_end = index_ptr + index->size;
12212
12213 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12214 index_ptr += 4;
12215 if (version == 2)
12216 nr_columns = read_4_bytes (dbfd, index_ptr);
12217 else
12218 nr_columns = 0;
12219 index_ptr += 4;
80626a55
DE
12220 nr_units = read_4_bytes (dbfd, index_ptr);
12221 index_ptr += 4;
12222 nr_slots = read_4_bytes (dbfd, index_ptr);
12223 index_ptr += 4;
12224
73869dc2 12225 if (version != 1 && version != 2)
80626a55 12226 {
21aa081e 12227 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12228 " [in module %s]"),
21aa081e 12229 pulongest (version), dwp_file->name);
80626a55
DE
12230 }
12231 if (nr_slots != (nr_slots & -nr_slots))
12232 {
21aa081e 12233 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12234 " is not power of 2 [in module %s]"),
21aa081e 12235 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12236 }
12237
12238 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12239 htab->version = version;
12240 htab->nr_columns = nr_columns;
80626a55
DE
12241 htab->nr_units = nr_units;
12242 htab->nr_slots = nr_slots;
12243 htab->hash_table = index_ptr;
12244 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12245
12246 /* Exit early if the table is empty. */
12247 if (nr_slots == 0 || nr_units == 0
12248 || (version == 2 && nr_columns == 0))
12249 {
12250 /* All must be zero. */
12251 if (nr_slots != 0 || nr_units != 0
12252 || (version == 2 && nr_columns != 0))
12253 {
12254 complaint (&symfile_complaints,
12255 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12256 " all zero [in modules %s]"),
12257 dwp_file->name);
12258 }
12259 return htab;
12260 }
12261
12262 if (version == 1)
12263 {
12264 htab->section_pool.v1.indices =
12265 htab->unit_table + sizeof (uint32_t) * nr_slots;
12266 /* It's harder to decide whether the section is too small in v1.
12267 V1 is deprecated anyway so we punt. */
12268 }
12269 else
12270 {
12271 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12272 int *ids = htab->section_pool.v2.section_ids;
12273 /* Reverse map for error checking. */
12274 int ids_seen[DW_SECT_MAX + 1];
12275 int i;
12276
12277 if (nr_columns < 2)
12278 {
12279 error (_("Dwarf Error: bad DWP hash table, too few columns"
12280 " in section table [in module %s]"),
12281 dwp_file->name);
12282 }
12283 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12284 {
12285 error (_("Dwarf Error: bad DWP hash table, too many columns"
12286 " in section table [in module %s]"),
12287 dwp_file->name);
12288 }
12289 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12290 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12291 for (i = 0; i < nr_columns; ++i)
12292 {
12293 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12294
12295 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12296 {
12297 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12298 " in section table [in module %s]"),
12299 id, dwp_file->name);
12300 }
12301 if (ids_seen[id] != -1)
12302 {
12303 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12304 " id %d in section table [in module %s]"),
12305 id, dwp_file->name);
12306 }
12307 ids_seen[id] = i;
12308 ids[i] = id;
12309 }
12310 /* Must have exactly one info or types section. */
12311 if (((ids_seen[DW_SECT_INFO] != -1)
12312 + (ids_seen[DW_SECT_TYPES] != -1))
12313 != 1)
12314 {
12315 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12316 " DWO info/types section [in module %s]"),
12317 dwp_file->name);
12318 }
12319 /* Must have an abbrev section. */
12320 if (ids_seen[DW_SECT_ABBREV] == -1)
12321 {
12322 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12323 " section [in module %s]"),
12324 dwp_file->name);
12325 }
12326 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12327 htab->section_pool.v2.sizes =
12328 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12329 * nr_units * nr_columns);
12330 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12331 * nr_units * nr_columns))
12332 > index_end)
12333 {
12334 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12335 " [in module %s]"),
12336 dwp_file->name);
12337 }
12338 }
80626a55
DE
12339
12340 return htab;
12341}
12342
12343/* Update SECTIONS with the data from SECTP.
12344
12345 This function is like the other "locate" section routines that are
12346 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12347 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12348
12349 The result is non-zero for success, or zero if an error was found. */
12350
12351static int
73869dc2
DE
12352locate_v1_virtual_dwo_sections (asection *sectp,
12353 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12354{
12355 const struct dwop_section_names *names = &dwop_section_names;
12356
12357 if (section_is_p (sectp->name, &names->abbrev_dwo))
12358 {
12359 /* There can be only one. */
049412e3 12360 if (sections->abbrev.s.section != NULL)
80626a55 12361 return 0;
049412e3 12362 sections->abbrev.s.section = sectp;
80626a55
DE
12363 sections->abbrev.size = bfd_get_section_size (sectp);
12364 }
12365 else if (section_is_p (sectp->name, &names->info_dwo)
12366 || section_is_p (sectp->name, &names->types_dwo))
12367 {
12368 /* There can be only one. */
049412e3 12369 if (sections->info_or_types.s.section != NULL)
80626a55 12370 return 0;
049412e3 12371 sections->info_or_types.s.section = sectp;
80626a55
DE
12372 sections->info_or_types.size = bfd_get_section_size (sectp);
12373 }
12374 else if (section_is_p (sectp->name, &names->line_dwo))
12375 {
12376 /* There can be only one. */
049412e3 12377 if (sections->line.s.section != NULL)
80626a55 12378 return 0;
049412e3 12379 sections->line.s.section = sectp;
80626a55
DE
12380 sections->line.size = bfd_get_section_size (sectp);
12381 }
12382 else if (section_is_p (sectp->name, &names->loc_dwo))
12383 {
12384 /* There can be only one. */
049412e3 12385 if (sections->loc.s.section != NULL)
80626a55 12386 return 0;
049412e3 12387 sections->loc.s.section = sectp;
80626a55
DE
12388 sections->loc.size = bfd_get_section_size (sectp);
12389 }
12390 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12391 {
12392 /* There can be only one. */
049412e3 12393 if (sections->macinfo.s.section != NULL)
80626a55 12394 return 0;
049412e3 12395 sections->macinfo.s.section = sectp;
80626a55
DE
12396 sections->macinfo.size = bfd_get_section_size (sectp);
12397 }
12398 else if (section_is_p (sectp->name, &names->macro_dwo))
12399 {
12400 /* There can be only one. */
049412e3 12401 if (sections->macro.s.section != NULL)
80626a55 12402 return 0;
049412e3 12403 sections->macro.s.section = sectp;
80626a55
DE
12404 sections->macro.size = bfd_get_section_size (sectp);
12405 }
12406 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12407 {
12408 /* There can be only one. */
049412e3 12409 if (sections->str_offsets.s.section != NULL)
80626a55 12410 return 0;
049412e3 12411 sections->str_offsets.s.section = sectp;
80626a55
DE
12412 sections->str_offsets.size = bfd_get_section_size (sectp);
12413 }
12414 else
12415 {
12416 /* No other kind of section is valid. */
12417 return 0;
12418 }
12419
12420 return 1;
12421}
12422
73869dc2
DE
12423/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12424 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12425 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12426 This is for DWP version 1 files. */
80626a55
DE
12427
12428static struct dwo_unit *
ed2dc618
SM
12429create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12430 struct dwp_file *dwp_file,
73869dc2
DE
12431 uint32_t unit_index,
12432 const char *comp_dir,
12433 ULONGEST signature, int is_debug_types)
80626a55
DE
12434{
12435 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12436 const struct dwp_hash_table *dwp_htab =
12437 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
12438 bfd *dbfd = dwp_file->dbfd;
12439 const char *kind = is_debug_types ? "TU" : "CU";
12440 struct dwo_file *dwo_file;
12441 struct dwo_unit *dwo_unit;
73869dc2 12442 struct virtual_v1_dwo_sections sections;
80626a55 12443 void **dwo_file_slot;
80626a55
DE
12444 int i;
12445
73869dc2
DE
12446 gdb_assert (dwp_file->version == 1);
12447
b4f54984 12448 if (dwarf_read_debug)
80626a55 12449 {
73869dc2 12450 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12451 kind,
73869dc2 12452 pulongest (unit_index), hex_string (signature),
80626a55
DE
12453 dwp_file->name);
12454 }
12455
19ac8c2e 12456 /* Fetch the sections of this DWO unit.
80626a55
DE
12457 Put a limit on the number of sections we look for so that bad data
12458 doesn't cause us to loop forever. */
12459
73869dc2 12460#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12461 (1 /* .debug_info or .debug_types */ \
12462 + 1 /* .debug_abbrev */ \
12463 + 1 /* .debug_line */ \
12464 + 1 /* .debug_loc */ \
12465 + 1 /* .debug_str_offsets */ \
19ac8c2e 12466 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12467 + 1 /* trailing zero */)
12468
12469 memset (&sections, 0, sizeof (sections));
80626a55 12470
73869dc2 12471 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12472 {
12473 asection *sectp;
12474 uint32_t section_nr =
12475 read_4_bytes (dbfd,
73869dc2
DE
12476 dwp_htab->section_pool.v1.indices
12477 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12478
12479 if (section_nr == 0)
12480 break;
12481 if (section_nr >= dwp_file->num_sections)
12482 {
12483 error (_("Dwarf Error: bad DWP hash table, section number too large"
12484 " [in module %s]"),
12485 dwp_file->name);
12486 }
12487
12488 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12489 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12490 {
12491 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12492 " [in module %s]"),
12493 dwp_file->name);
12494 }
12495 }
12496
12497 if (i < 2
a32a8923
DE
12498 || dwarf2_section_empty_p (&sections.info_or_types)
12499 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12500 {
12501 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12502 " [in module %s]"),
12503 dwp_file->name);
12504 }
73869dc2 12505 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12506 {
12507 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12508 " [in module %s]"),
12509 dwp_file->name);
12510 }
12511
12512 /* It's easier for the rest of the code if we fake a struct dwo_file and
12513 have dwo_unit "live" in that. At least for now.
12514
12515 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12516 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12517 file, we can combine them back into a virtual DWO file to save space
12518 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12519 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12520
791afaa2
TT
12521 std::string virtual_dwo_name =
12522 string_printf ("virtual-dwo/%d-%d-%d-%d",
12523 get_section_id (&sections.abbrev),
12524 get_section_id (&sections.line),
12525 get_section_id (&sections.loc),
12526 get_section_id (&sections.str_offsets));
80626a55 12527 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12528 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12529 virtual_dwo_name.c_str (),
12530 comp_dir);
80626a55
DE
12531 /* Create one if necessary. */
12532 if (*dwo_file_slot == NULL)
12533 {
b4f54984 12534 if (dwarf_read_debug)
80626a55
DE
12535 {
12536 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12537 virtual_dwo_name.c_str ());
80626a55
DE
12538 }
12539 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12540 dwo_file->dwo_name
12541 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12542 virtual_dwo_name.c_str (),
12543 virtual_dwo_name.size ());
0ac5b59e 12544 dwo_file->comp_dir = comp_dir;
80626a55
DE
12545 dwo_file->sections.abbrev = sections.abbrev;
12546 dwo_file->sections.line = sections.line;
12547 dwo_file->sections.loc = sections.loc;
12548 dwo_file->sections.macinfo = sections.macinfo;
12549 dwo_file->sections.macro = sections.macro;
12550 dwo_file->sections.str_offsets = sections.str_offsets;
12551 /* The "str" section is global to the entire DWP file. */
12552 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12553 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12554 there's no need to record it in dwo_file.
12555 Also, we can't simply record type sections in dwo_file because
12556 we record a pointer into the vector in dwo_unit. As we collect more
12557 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12558 for it, invalidating all copies of pointers into the previous
12559 contents. */
80626a55
DE
12560 *dwo_file_slot = dwo_file;
12561 }
12562 else
12563 {
b4f54984 12564 if (dwarf_read_debug)
80626a55
DE
12565 {
12566 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12567 virtual_dwo_name.c_str ());
80626a55 12568 }
9a3c8263 12569 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12570 }
80626a55
DE
12571
12572 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12573 dwo_unit->dwo_file = dwo_file;
12574 dwo_unit->signature = signature;
8d749320
SM
12575 dwo_unit->section =
12576 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12577 *dwo_unit->section = sections.info_or_types;
57d63ce2 12578 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12579
12580 return dwo_unit;
12581}
12582
73869dc2
DE
12583/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12584 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12585 piece within that section used by a TU/CU, return a virtual section
12586 of just that piece. */
12587
12588static struct dwarf2_section_info
ed2dc618
SM
12589create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12590 struct dwarf2_section_info *section,
73869dc2
DE
12591 bfd_size_type offset, bfd_size_type size)
12592{
12593 struct dwarf2_section_info result;
12594 asection *sectp;
12595
12596 gdb_assert (section != NULL);
12597 gdb_assert (!section->is_virtual);
12598
12599 memset (&result, 0, sizeof (result));
12600 result.s.containing_section = section;
12601 result.is_virtual = 1;
12602
12603 if (size == 0)
12604 return result;
12605
12606 sectp = get_section_bfd_section (section);
12607
12608 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12609 bounds of the real section. This is a pretty-rare event, so just
12610 flag an error (easier) instead of a warning and trying to cope. */
12611 if (sectp == NULL
12612 || offset + size > bfd_get_section_size (sectp))
12613 {
73869dc2
DE
12614 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12615 " in section %s [in module %s]"),
12616 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12617 objfile_name (dwarf2_per_objfile->objfile));
12618 }
12619
12620 result.virtual_offset = offset;
12621 result.size = size;
12622 return result;
12623}
12624
12625/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12626 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12627 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12628 This is for DWP version 2 files. */
12629
12630static struct dwo_unit *
ed2dc618
SM
12631create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12632 struct dwp_file *dwp_file,
73869dc2
DE
12633 uint32_t unit_index,
12634 const char *comp_dir,
12635 ULONGEST signature, int is_debug_types)
12636{
12637 struct objfile *objfile = dwarf2_per_objfile->objfile;
12638 const struct dwp_hash_table *dwp_htab =
12639 is_debug_types ? dwp_file->tus : dwp_file->cus;
12640 bfd *dbfd = dwp_file->dbfd;
12641 const char *kind = is_debug_types ? "TU" : "CU";
12642 struct dwo_file *dwo_file;
12643 struct dwo_unit *dwo_unit;
12644 struct virtual_v2_dwo_sections sections;
12645 void **dwo_file_slot;
73869dc2
DE
12646 int i;
12647
12648 gdb_assert (dwp_file->version == 2);
12649
b4f54984 12650 if (dwarf_read_debug)
73869dc2
DE
12651 {
12652 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12653 kind,
12654 pulongest (unit_index), hex_string (signature),
12655 dwp_file->name);
12656 }
12657
12658 /* Fetch the section offsets of this DWO unit. */
12659
12660 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12661
12662 for (i = 0; i < dwp_htab->nr_columns; ++i)
12663 {
12664 uint32_t offset = read_4_bytes (dbfd,
12665 dwp_htab->section_pool.v2.offsets
12666 + (((unit_index - 1) * dwp_htab->nr_columns
12667 + i)
12668 * sizeof (uint32_t)));
12669 uint32_t size = read_4_bytes (dbfd,
12670 dwp_htab->section_pool.v2.sizes
12671 + (((unit_index - 1) * dwp_htab->nr_columns
12672 + i)
12673 * sizeof (uint32_t)));
12674
12675 switch (dwp_htab->section_pool.v2.section_ids[i])
12676 {
12677 case DW_SECT_INFO:
12678 case DW_SECT_TYPES:
12679 sections.info_or_types_offset = offset;
12680 sections.info_or_types_size = size;
12681 break;
12682 case DW_SECT_ABBREV:
12683 sections.abbrev_offset = offset;
12684 sections.abbrev_size = size;
12685 break;
12686 case DW_SECT_LINE:
12687 sections.line_offset = offset;
12688 sections.line_size = size;
12689 break;
12690 case DW_SECT_LOC:
12691 sections.loc_offset = offset;
12692 sections.loc_size = size;
12693 break;
12694 case DW_SECT_STR_OFFSETS:
12695 sections.str_offsets_offset = offset;
12696 sections.str_offsets_size = size;
12697 break;
12698 case DW_SECT_MACINFO:
12699 sections.macinfo_offset = offset;
12700 sections.macinfo_size = size;
12701 break;
12702 case DW_SECT_MACRO:
12703 sections.macro_offset = offset;
12704 sections.macro_size = size;
12705 break;
12706 }
12707 }
12708
12709 /* It's easier for the rest of the code if we fake a struct dwo_file and
12710 have dwo_unit "live" in that. At least for now.
12711
12712 The DWP file can be made up of a random collection of CUs and TUs.
12713 However, for each CU + set of TUs that came from the same original DWO
12714 file, we can combine them back into a virtual DWO file to save space
12715 (fewer struct dwo_file objects to allocate). Remember that for really
12716 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12717
791afaa2
TT
12718 std::string virtual_dwo_name =
12719 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12720 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12721 (long) (sections.line_size ? sections.line_offset : 0),
12722 (long) (sections.loc_size ? sections.loc_offset : 0),
12723 (long) (sections.str_offsets_size
12724 ? sections.str_offsets_offset : 0));
73869dc2 12725 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12726 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12727 virtual_dwo_name.c_str (),
12728 comp_dir);
73869dc2
DE
12729 /* Create one if necessary. */
12730 if (*dwo_file_slot == NULL)
12731 {
b4f54984 12732 if (dwarf_read_debug)
73869dc2
DE
12733 {
12734 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12735 virtual_dwo_name.c_str ());
73869dc2
DE
12736 }
12737 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12738 dwo_file->dwo_name
12739 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12740 virtual_dwo_name.c_str (),
12741 virtual_dwo_name.size ());
73869dc2
DE
12742 dwo_file->comp_dir = comp_dir;
12743 dwo_file->sections.abbrev =
ed2dc618 12744 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12745 sections.abbrev_offset, sections.abbrev_size);
12746 dwo_file->sections.line =
ed2dc618 12747 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12748 sections.line_offset, sections.line_size);
12749 dwo_file->sections.loc =
ed2dc618 12750 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12751 sections.loc_offset, sections.loc_size);
12752 dwo_file->sections.macinfo =
ed2dc618 12753 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12754 sections.macinfo_offset, sections.macinfo_size);
12755 dwo_file->sections.macro =
ed2dc618 12756 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12757 sections.macro_offset, sections.macro_size);
12758 dwo_file->sections.str_offsets =
ed2dc618
SM
12759 create_dwp_v2_section (dwarf2_per_objfile,
12760 &dwp_file->sections.str_offsets,
73869dc2
DE
12761 sections.str_offsets_offset,
12762 sections.str_offsets_size);
12763 /* The "str" section is global to the entire DWP file. */
12764 dwo_file->sections.str = dwp_file->sections.str;
12765 /* The info or types section is assigned below to dwo_unit,
12766 there's no need to record it in dwo_file.
12767 Also, we can't simply record type sections in dwo_file because
12768 we record a pointer into the vector in dwo_unit. As we collect more
12769 types we'll grow the vector and eventually have to reallocate space
12770 for it, invalidating all copies of pointers into the previous
12771 contents. */
12772 *dwo_file_slot = dwo_file;
12773 }
12774 else
12775 {
b4f54984 12776 if (dwarf_read_debug)
73869dc2
DE
12777 {
12778 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12779 virtual_dwo_name.c_str ());
73869dc2 12780 }
9a3c8263 12781 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12782 }
73869dc2
DE
12783
12784 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12785 dwo_unit->dwo_file = dwo_file;
12786 dwo_unit->signature = signature;
8d749320
SM
12787 dwo_unit->section =
12788 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12789 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12790 is_debug_types
73869dc2
DE
12791 ? &dwp_file->sections.types
12792 : &dwp_file->sections.info,
12793 sections.info_or_types_offset,
12794 sections.info_or_types_size);
12795 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12796
12797 return dwo_unit;
12798}
12799
57d63ce2
DE
12800/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12801 Returns NULL if the signature isn't found. */
80626a55
DE
12802
12803static struct dwo_unit *
ed2dc618
SM
12804lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12805 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12806 ULONGEST signature, int is_debug_types)
80626a55 12807{
57d63ce2
DE
12808 const struct dwp_hash_table *dwp_htab =
12809 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 12810 bfd *dbfd = dwp_file->dbfd;
57d63ce2 12811 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12812 uint32_t hash = signature & mask;
12813 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12814 unsigned int i;
12815 void **slot;
870f88f7 12816 struct dwo_unit find_dwo_cu;
80626a55
DE
12817
12818 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12819 find_dwo_cu.signature = signature;
19ac8c2e
DE
12820 slot = htab_find_slot (is_debug_types
12821 ? dwp_file->loaded_tus
12822 : dwp_file->loaded_cus,
12823 &find_dwo_cu, INSERT);
80626a55
DE
12824
12825 if (*slot != NULL)
9a3c8263 12826 return (struct dwo_unit *) *slot;
80626a55
DE
12827
12828 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12829 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12830 {
12831 ULONGEST signature_in_table;
12832
12833 signature_in_table =
57d63ce2 12834 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12835 if (signature_in_table == signature)
12836 {
57d63ce2
DE
12837 uint32_t unit_index =
12838 read_4_bytes (dbfd,
12839 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12840
73869dc2
DE
12841 if (dwp_file->version == 1)
12842 {
ed2dc618
SM
12843 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12844 dwp_file, unit_index,
73869dc2
DE
12845 comp_dir, signature,
12846 is_debug_types);
12847 }
12848 else
12849 {
ed2dc618
SM
12850 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12851 dwp_file, unit_index,
73869dc2
DE
12852 comp_dir, signature,
12853 is_debug_types);
12854 }
9a3c8263 12855 return (struct dwo_unit *) *slot;
80626a55
DE
12856 }
12857 if (signature_in_table == 0)
12858 return NULL;
12859 hash = (hash + hash2) & mask;
12860 }
12861
12862 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12863 " [in module %s]"),
12864 dwp_file->name);
12865}
12866
ab5088bf 12867/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12868 Open the file specified by FILE_NAME and hand it off to BFD for
12869 preliminary analysis. Return a newly initialized bfd *, which
12870 includes a canonicalized copy of FILE_NAME.
80626a55 12871 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12872 SEARCH_CWD is true if the current directory is to be searched.
12873 It will be searched before debug-file-directory.
13aaf454
DE
12874 If successful, the file is added to the bfd include table of the
12875 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12876 If unable to find/open the file, return NULL.
3019eac3
DE
12877 NOTE: This function is derived from symfile_bfd_open. */
12878
192b62ce 12879static gdb_bfd_ref_ptr
ed2dc618
SM
12880try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12881 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12882{
24b9144d 12883 int desc;
3019eac3 12884 char *absolute_name;
9c02c129
DE
12885 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12886 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12887 to debug_file_directory. */
12888 char *search_path;
12889 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12890
6ac97d4c
DE
12891 if (search_cwd)
12892 {
12893 if (*debug_file_directory != '\0')
12894 search_path = concat (".", dirname_separator_string,
b36cec19 12895 debug_file_directory, (char *) NULL);
6ac97d4c
DE
12896 else
12897 search_path = xstrdup (".");
12898 }
9c02c129 12899 else
6ac97d4c 12900 search_path = xstrdup (debug_file_directory);
3019eac3 12901
24b9144d 12902 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12903 if (is_dwp)
12904 flags |= OPF_SEARCH_IN_PATH;
9c02c129 12905 desc = openp (search_path, flags, file_name,
3019eac3 12906 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 12907 xfree (search_path);
3019eac3
DE
12908 if (desc < 0)
12909 return NULL;
12910
192b62ce 12911 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 12912 xfree (absolute_name);
9c02c129
DE
12913 if (sym_bfd == NULL)
12914 return NULL;
192b62ce 12915 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12916
192b62ce
TT
12917 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12918 return NULL;
3019eac3 12919
13aaf454
DE
12920 /* Success. Record the bfd as having been included by the objfile's bfd.
12921 This is important because things like demangled_names_hash lives in the
12922 objfile's per_bfd space and may have references to things like symbol
12923 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12924 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12925
3019eac3
DE
12926 return sym_bfd;
12927}
12928
ab5088bf 12929/* Try to open DWO file FILE_NAME.
3019eac3
DE
12930 COMP_DIR is the DW_AT_comp_dir attribute.
12931 The result is the bfd handle of the file.
12932 If there is a problem finding or opening the file, return NULL.
12933 Upon success, the canonicalized path of the file is stored in the bfd,
12934 same as symfile_bfd_open. */
12935
192b62ce 12936static gdb_bfd_ref_ptr
ed2dc618
SM
12937open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12938 const char *file_name, const char *comp_dir)
3019eac3 12939{
80626a55 12940 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12941 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12942 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12943
12944 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12945
12946 if (comp_dir != NULL)
12947 {
b36cec19
PA
12948 char *path_to_try = concat (comp_dir, SLASH_STRING,
12949 file_name, (char *) NULL);
3019eac3
DE
12950
12951 /* NOTE: If comp_dir is a relative path, this will also try the
12952 search path, which seems useful. */
ed2dc618
SM
12953 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12954 path_to_try,
12955 0 /*is_dwp*/,
192b62ce 12956 1 /*search_cwd*/));
3019eac3
DE
12957 xfree (path_to_try);
12958 if (abfd != NULL)
12959 return abfd;
12960 }
12961
12962 /* That didn't work, try debug-file-directory, which, despite its name,
12963 is a list of paths. */
12964
12965 if (*debug_file_directory == '\0')
12966 return NULL;
12967
ed2dc618
SM
12968 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12969 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12970}
12971
80626a55
DE
12972/* This function is mapped across the sections and remembers the offset and
12973 size of each of the DWO debugging sections we are interested in. */
12974
12975static void
12976dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12977{
9a3c8263 12978 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12979 const struct dwop_section_names *names = &dwop_section_names;
12980
12981 if (section_is_p (sectp->name, &names->abbrev_dwo))
12982 {
049412e3 12983 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
12984 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12985 }
12986 else if (section_is_p (sectp->name, &names->info_dwo))
12987 {
049412e3 12988 dwo_sections->info.s.section = sectp;
80626a55
DE
12989 dwo_sections->info.size = bfd_get_section_size (sectp);
12990 }
12991 else if (section_is_p (sectp->name, &names->line_dwo))
12992 {
049412e3 12993 dwo_sections->line.s.section = sectp;
80626a55
DE
12994 dwo_sections->line.size = bfd_get_section_size (sectp);
12995 }
12996 else if (section_is_p (sectp->name, &names->loc_dwo))
12997 {
049412e3 12998 dwo_sections->loc.s.section = sectp;
80626a55
DE
12999 dwo_sections->loc.size = bfd_get_section_size (sectp);
13000 }
13001 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13002 {
049412e3 13003 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
13004 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
13005 }
13006 else if (section_is_p (sectp->name, &names->macro_dwo))
13007 {
049412e3 13008 dwo_sections->macro.s.section = sectp;
80626a55
DE
13009 dwo_sections->macro.size = bfd_get_section_size (sectp);
13010 }
13011 else if (section_is_p (sectp->name, &names->str_dwo))
13012 {
049412e3 13013 dwo_sections->str.s.section = sectp;
80626a55
DE
13014 dwo_sections->str.size = bfd_get_section_size (sectp);
13015 }
13016 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13017 {
049412e3 13018 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
13019 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
13020 }
13021 else if (section_is_p (sectp->name, &names->types_dwo))
13022 {
13023 struct dwarf2_section_info type_section;
13024
13025 memset (&type_section, 0, sizeof (type_section));
049412e3 13026 type_section.s.section = sectp;
80626a55
DE
13027 type_section.size = bfd_get_section_size (sectp);
13028 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
13029 &type_section);
13030 }
13031}
13032
ab5088bf 13033/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 13034 by PER_CU. This is for the non-DWP case.
80626a55 13035 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
13036
13037static struct dwo_file *
0ac5b59e
DE
13038open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13039 const char *dwo_name, const char *comp_dir)
3019eac3 13040{
ed2dc618 13041 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 13042 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13043 struct dwo_file *dwo_file;
3019eac3
DE
13044 struct cleanup *cleanups;
13045
ed2dc618 13046 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
80626a55
DE
13047 if (dbfd == NULL)
13048 {
b4f54984 13049 if (dwarf_read_debug)
80626a55
DE
13050 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13051 return NULL;
13052 }
13053 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
13054 dwo_file->dwo_name = dwo_name;
13055 dwo_file->comp_dir = comp_dir;
192b62ce 13056 dwo_file->dbfd = dbfd.release ();
3019eac3 13057
ed2dc618
SM
13058 free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
13059 cleanup_data->dwo_file = dwo_file;
13060 cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
13061
13062 cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
3019eac3 13063
192b62ce
TT
13064 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13065 &dwo_file->sections);
3019eac3 13066
ed2dc618
SM
13067 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13068 dwo_file->cus);
3019eac3 13069
ed2dc618
SM
13070 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
13071 dwo_file->sections.types, dwo_file->tus);
3019eac3
DE
13072
13073 discard_cleanups (cleanups);
13074
b4f54984 13075 if (dwarf_read_debug)
80626a55
DE
13076 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13077
3019eac3
DE
13078 return dwo_file;
13079}
13080
80626a55 13081/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13082 size of each of the DWP debugging sections common to version 1 and 2 that
13083 we are interested in. */
3019eac3 13084
80626a55 13085static void
73869dc2
DE
13086dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13087 void *dwp_file_ptr)
3019eac3 13088{
9a3c8263 13089 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13090 const struct dwop_section_names *names = &dwop_section_names;
13091 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13092
80626a55 13093 /* Record the ELF section number for later lookup: this is what the
73869dc2 13094 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13095 gdb_assert (elf_section_nr < dwp_file->num_sections);
13096 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13097
80626a55
DE
13098 /* Look for specific sections that we need. */
13099 if (section_is_p (sectp->name, &names->str_dwo))
13100 {
049412e3 13101 dwp_file->sections.str.s.section = sectp;
80626a55
DE
13102 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13103 }
13104 else if (section_is_p (sectp->name, &names->cu_index))
13105 {
049412e3 13106 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
13107 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13108 }
13109 else if (section_is_p (sectp->name, &names->tu_index))
13110 {
049412e3 13111 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
13112 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13113 }
13114}
3019eac3 13115
73869dc2
DE
13116/* This function is mapped across the sections and remembers the offset and
13117 size of each of the DWP version 2 debugging sections that we are interested
13118 in. This is split into a separate function because we don't know if we
13119 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13120
13121static void
13122dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13123{
9a3c8263 13124 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13125 const struct dwop_section_names *names = &dwop_section_names;
13126 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13127
13128 /* Record the ELF section number for later lookup: this is what the
13129 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13130 gdb_assert (elf_section_nr < dwp_file->num_sections);
13131 dwp_file->elf_sections[elf_section_nr] = sectp;
13132
13133 /* Look for specific sections that we need. */
13134 if (section_is_p (sectp->name, &names->abbrev_dwo))
13135 {
049412e3 13136 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
13137 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13138 }
13139 else if (section_is_p (sectp->name, &names->info_dwo))
13140 {
049412e3 13141 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
13142 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13143 }
13144 else if (section_is_p (sectp->name, &names->line_dwo))
13145 {
049412e3 13146 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
13147 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13148 }
13149 else if (section_is_p (sectp->name, &names->loc_dwo))
13150 {
049412e3 13151 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
13152 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13153 }
13154 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13155 {
049412e3 13156 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
13157 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13158 }
13159 else if (section_is_p (sectp->name, &names->macro_dwo))
13160 {
049412e3 13161 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
13162 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13163 }
13164 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13165 {
049412e3 13166 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
13167 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13168 }
13169 else if (section_is_p (sectp->name, &names->types_dwo))
13170 {
049412e3 13171 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
13172 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13173 }
13174}
13175
80626a55 13176/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13177
80626a55
DE
13178static hashval_t
13179hash_dwp_loaded_cutus (const void *item)
13180{
9a3c8263 13181 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13182
80626a55
DE
13183 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13184 return dwo_unit->signature;
3019eac3
DE
13185}
13186
80626a55 13187/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13188
80626a55
DE
13189static int
13190eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13191{
9a3c8263
SM
13192 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13193 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13194
80626a55
DE
13195 return dua->signature == dub->signature;
13196}
3019eac3 13197
80626a55 13198/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13199
80626a55
DE
13200static htab_t
13201allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13202{
13203 return htab_create_alloc_ex (3,
13204 hash_dwp_loaded_cutus,
13205 eq_dwp_loaded_cutus,
13206 NULL,
13207 &objfile->objfile_obstack,
13208 hashtab_obstack_allocate,
13209 dummy_obstack_deallocate);
13210}
3019eac3 13211
ab5088bf
DE
13212/* Try to open DWP file FILE_NAME.
13213 The result is the bfd handle of the file.
13214 If there is a problem finding or opening the file, return NULL.
13215 Upon success, the canonicalized path of the file is stored in the bfd,
13216 same as symfile_bfd_open. */
13217
192b62ce 13218static gdb_bfd_ref_ptr
ed2dc618
SM
13219open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13220 const char *file_name)
ab5088bf 13221{
ed2dc618
SM
13222 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13223 1 /*is_dwp*/,
192b62ce 13224 1 /*search_cwd*/));
6ac97d4c
DE
13225 if (abfd != NULL)
13226 return abfd;
13227
13228 /* Work around upstream bug 15652.
13229 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13230 [Whether that's a "bug" is debatable, but it is getting in our way.]
13231 We have no real idea where the dwp file is, because gdb's realpath-ing
13232 of the executable's path may have discarded the needed info.
13233 [IWBN if the dwp file name was recorded in the executable, akin to
13234 .gnu_debuglink, but that doesn't exist yet.]
13235 Strip the directory from FILE_NAME and search again. */
13236 if (*debug_file_directory != '\0')
13237 {
13238 /* Don't implicitly search the current directory here.
13239 If the user wants to search "." to handle this case,
13240 it must be added to debug-file-directory. */
ed2dc618
SM
13241 return try_open_dwop_file (dwarf2_per_objfile,
13242 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13243 0 /*search_cwd*/);
13244 }
13245
13246 return NULL;
ab5088bf
DE
13247}
13248
80626a55
DE
13249/* Initialize the use of the DWP file for the current objfile.
13250 By convention the name of the DWP file is ${objfile}.dwp.
13251 The result is NULL if it can't be found. */
a766d390 13252
80626a55 13253static struct dwp_file *
ed2dc618 13254open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13255{
13256 struct objfile *objfile = dwarf2_per_objfile->objfile;
13257 struct dwp_file *dwp_file;
80626a55 13258
82bf32bc
JK
13259 /* Try to find first .dwp for the binary file before any symbolic links
13260 resolving. */
6c447423
DE
13261
13262 /* If the objfile is a debug file, find the name of the real binary
13263 file and get the name of dwp file from there. */
d721ba37 13264 std::string dwp_name;
6c447423
DE
13265 if (objfile->separate_debug_objfile_backlink != NULL)
13266 {
13267 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13268 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13269
d721ba37 13270 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13271 }
13272 else
d721ba37
PA
13273 dwp_name = objfile->original_name;
13274
13275 dwp_name += ".dwp";
80626a55 13276
ed2dc618 13277 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13278 if (dbfd == NULL
13279 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13280 {
13281 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13282 dwp_name = objfile_name (objfile);
13283 dwp_name += ".dwp";
ed2dc618 13284 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13285 }
13286
80626a55
DE
13287 if (dbfd == NULL)
13288 {
b4f54984 13289 if (dwarf_read_debug)
d721ba37 13290 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 13291 return NULL;
3019eac3 13292 }
80626a55 13293 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
13294 dwp_file->name = bfd_get_filename (dbfd.get ());
13295 dwp_file->dbfd = dbfd.release ();
c906108c 13296
80626a55 13297 /* +1: section 0 is unused */
192b62ce 13298 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
13299 dwp_file->elf_sections =
13300 OBSTACK_CALLOC (&objfile->objfile_obstack,
13301 dwp_file->num_sections, asection *);
13302
192b62ce
TT
13303 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13304 dwp_file);
80626a55 13305
ed2dc618 13306 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
80626a55 13307
ed2dc618 13308 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
80626a55 13309
73869dc2 13310 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13311 if (dwp_file->cus && dwp_file->tus
13312 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13313 {
13314 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13315 pretty bizarre. We use pulongest here because that's the established
4d65956b 13316 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13317 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13318 " TU version %s [in DWP file %s]"),
13319 pulongest (dwp_file->cus->version),
d721ba37 13320 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13321 }
08302ed2
DE
13322
13323 if (dwp_file->cus)
13324 dwp_file->version = dwp_file->cus->version;
13325 else if (dwp_file->tus)
13326 dwp_file->version = dwp_file->tus->version;
13327 else
13328 dwp_file->version = 2;
73869dc2
DE
13329
13330 if (dwp_file->version == 2)
192b62ce
TT
13331 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13332 dwp_file);
73869dc2 13333
19ac8c2e
DE
13334 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13335 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13336
b4f54984 13337 if (dwarf_read_debug)
80626a55
DE
13338 {
13339 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13340 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13341 " %s CUs, %s TUs\n",
13342 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13343 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13344 }
13345
13346 return dwp_file;
3019eac3 13347}
c906108c 13348
ab5088bf
DE
13349/* Wrapper around open_and_init_dwp_file, only open it once. */
13350
13351static struct dwp_file *
ed2dc618 13352get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13353{
13354 if (! dwarf2_per_objfile->dwp_checked)
13355 {
ed2dc618
SM
13356 dwarf2_per_objfile->dwp_file
13357 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13358 dwarf2_per_objfile->dwp_checked = 1;
13359 }
13360 return dwarf2_per_objfile->dwp_file;
13361}
13362
80626a55
DE
13363/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13364 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13365 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13366 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13367 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13368
13369 This is called, for example, when wanting to read a variable with a
13370 complex location. Therefore we don't want to do file i/o for every call.
13371 Therefore we don't want to look for a DWO file on every call.
13372 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13373 then we check if we've already seen DWO_NAME, and only THEN do we check
13374 for a DWO file.
13375
1c658ad5 13376 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13377 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13378
3019eac3 13379static struct dwo_unit *
80626a55
DE
13380lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13381 const char *dwo_name, const char *comp_dir,
13382 ULONGEST signature, int is_debug_types)
3019eac3 13383{
ed2dc618 13384 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13385 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13386 const char *kind = is_debug_types ? "TU" : "CU";
13387 void **dwo_file_slot;
3019eac3 13388 struct dwo_file *dwo_file;
80626a55 13389 struct dwp_file *dwp_file;
cb1df416 13390
6a506a2d
DE
13391 /* First see if there's a DWP file.
13392 If we have a DWP file but didn't find the DWO inside it, don't
13393 look for the original DWO file. It makes gdb behave differently
13394 depending on whether one is debugging in the build tree. */
cf2c3c16 13395
ed2dc618 13396 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13397 if (dwp_file != NULL)
cf2c3c16 13398 {
80626a55
DE
13399 const struct dwp_hash_table *dwp_htab =
13400 is_debug_types ? dwp_file->tus : dwp_file->cus;
13401
13402 if (dwp_htab != NULL)
13403 {
13404 struct dwo_unit *dwo_cutu =
ed2dc618 13405 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13406 signature, is_debug_types);
80626a55
DE
13407
13408 if (dwo_cutu != NULL)
13409 {
b4f54984 13410 if (dwarf_read_debug)
80626a55
DE
13411 {
13412 fprintf_unfiltered (gdb_stdlog,
13413 "Virtual DWO %s %s found: @%s\n",
13414 kind, hex_string (signature),
13415 host_address_to_string (dwo_cutu));
13416 }
13417 return dwo_cutu;
13418 }
13419 }
13420 }
6a506a2d 13421 else
80626a55 13422 {
6a506a2d 13423 /* No DWP file, look for the DWO file. */
80626a55 13424
ed2dc618
SM
13425 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13426 dwo_name, comp_dir);
6a506a2d 13427 if (*dwo_file_slot == NULL)
80626a55 13428 {
6a506a2d
DE
13429 /* Read in the file and build a table of the CUs/TUs it contains. */
13430 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13431 }
6a506a2d 13432 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13433 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13434
6a506a2d 13435 if (dwo_file != NULL)
19c3d4c9 13436 {
6a506a2d
DE
13437 struct dwo_unit *dwo_cutu = NULL;
13438
13439 if (is_debug_types && dwo_file->tus)
13440 {
13441 struct dwo_unit find_dwo_cutu;
13442
13443 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13444 find_dwo_cutu.signature = signature;
9a3c8263
SM
13445 dwo_cutu
13446 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13447 }
33c5cd75 13448 else if (!is_debug_types && dwo_file->cus)
80626a55 13449 {
33c5cd75
DB
13450 struct dwo_unit find_dwo_cutu;
13451
13452 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13453 find_dwo_cutu.signature = signature;
13454 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13455 &find_dwo_cutu);
6a506a2d
DE
13456 }
13457
13458 if (dwo_cutu != NULL)
13459 {
b4f54984 13460 if (dwarf_read_debug)
6a506a2d
DE
13461 {
13462 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13463 kind, dwo_name, hex_string (signature),
13464 host_address_to_string (dwo_cutu));
13465 }
13466 return dwo_cutu;
80626a55
DE
13467 }
13468 }
2e276125 13469 }
9cdd5dbd 13470
80626a55
DE
13471 /* We didn't find it. This could mean a dwo_id mismatch, or
13472 someone deleted the DWO/DWP file, or the search path isn't set up
13473 correctly to find the file. */
13474
b4f54984 13475 if (dwarf_read_debug)
80626a55
DE
13476 {
13477 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13478 kind, dwo_name, hex_string (signature));
13479 }
3019eac3 13480
6656a72d
DE
13481 /* This is a warning and not a complaint because it can be caused by
13482 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13483 {
13484 /* Print the name of the DWP file if we looked there, helps the user
13485 better diagnose the problem. */
791afaa2 13486 std::string dwp_text;
43942612
DE
13487
13488 if (dwp_file != NULL)
791afaa2
TT
13489 dwp_text = string_printf (" [in DWP file %s]",
13490 lbasename (dwp_file->name));
43942612
DE
13491
13492 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
13493 " [in module %s]"),
13494 kind, dwo_name, hex_string (signature),
791afaa2 13495 dwp_text.c_str (),
43942612 13496 this_unit->is_debug_types ? "TU" : "CU",
9c541725 13497 to_underlying (this_unit->sect_off), objfile_name (objfile));
43942612 13498 }
3019eac3 13499 return NULL;
5fb290d7
DJ
13500}
13501
80626a55
DE
13502/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13503 See lookup_dwo_cutu_unit for details. */
13504
13505static struct dwo_unit *
13506lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13507 const char *dwo_name, const char *comp_dir,
13508 ULONGEST signature)
13509{
13510 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13511}
13512
13513/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13514 See lookup_dwo_cutu_unit for details. */
13515
13516static struct dwo_unit *
13517lookup_dwo_type_unit (struct signatured_type *this_tu,
13518 const char *dwo_name, const char *comp_dir)
13519{
13520 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13521}
13522
89e63ee4
DE
13523/* Traversal function for queue_and_load_all_dwo_tus. */
13524
13525static int
13526queue_and_load_dwo_tu (void **slot, void *info)
13527{
13528 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13529 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13530 ULONGEST signature = dwo_unit->signature;
13531 struct signatured_type *sig_type =
13532 lookup_dwo_signatured_type (per_cu->cu, signature);
13533
13534 if (sig_type != NULL)
13535 {
13536 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13537
13538 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13539 a real dependency of PER_CU on SIG_TYPE. That is detected later
13540 while processing PER_CU. */
13541 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13542 load_full_type_unit (sig_cu);
13543 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13544 }
13545
13546 return 1;
13547}
13548
13549/* Queue all TUs contained in the DWO of PER_CU to be read in.
13550 The DWO may have the only definition of the type, though it may not be
13551 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13552 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13553
13554static void
13555queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13556{
13557 struct dwo_unit *dwo_unit;
13558 struct dwo_file *dwo_file;
13559
13560 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13561 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13562 gdb_assert (per_cu->cu != NULL);
13563
13564 dwo_unit = per_cu->cu->dwo_unit;
13565 gdb_assert (dwo_unit != NULL);
13566
13567 dwo_file = dwo_unit->dwo_file;
13568 if (dwo_file->tus != NULL)
13569 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13570}
13571
3019eac3
DE
13572/* Free all resources associated with DWO_FILE.
13573 Close the DWO file and munmap the sections.
13574 All memory should be on the objfile obstack. */
348e048f
DE
13575
13576static void
3019eac3 13577free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 13578{
348e048f 13579
5c6fa7ab 13580 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 13581 gdb_bfd_unref (dwo_file->dbfd);
348e048f 13582
3019eac3
DE
13583 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13584}
348e048f 13585
3019eac3 13586/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 13587
3019eac3
DE
13588static void
13589free_dwo_file_cleanup (void *arg)
13590{
ed2dc618
SM
13591 struct free_dwo_file_cleanup_data *data
13592 = (struct free_dwo_file_cleanup_data *) arg;
13593 struct objfile *objfile = data->dwarf2_per_objfile->objfile;
348e048f 13594
ed2dc618
SM
13595 free_dwo_file (data->dwo_file, objfile);
13596
13597 xfree (data);
3019eac3 13598}
348e048f 13599
3019eac3 13600/* Traversal function for free_dwo_files. */
2ab95328 13601
3019eac3
DE
13602static int
13603free_dwo_file_from_slot (void **slot, void *info)
13604{
13605 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13606 struct objfile *objfile = (struct objfile *) info;
348e048f 13607
3019eac3 13608 free_dwo_file (dwo_file, objfile);
348e048f 13609
3019eac3
DE
13610 return 1;
13611}
348e048f 13612
3019eac3 13613/* Free all resources associated with DWO_FILES. */
348e048f 13614
3019eac3
DE
13615static void
13616free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13617{
13618 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 13619}
3019eac3
DE
13620\f
13621/* Read in various DIEs. */
348e048f 13622
d389af10 13623/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13624 Inherit only the children of the DW_AT_abstract_origin DIE not being
13625 already referenced by DW_AT_abstract_origin from the children of the
13626 current DIE. */
d389af10
JK
13627
13628static void
13629inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13630{
13631 struct die_info *child_die;
791afaa2 13632 sect_offset *offsetp;
d389af10
JK
13633 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13634 struct die_info *origin_die;
13635 /* Iterator of the ORIGIN_DIE children. */
13636 struct die_info *origin_child_die;
d389af10 13637 struct attribute *attr;
cd02d79d
PA
13638 struct dwarf2_cu *origin_cu;
13639 struct pending **origin_previous_list_in_scope;
d389af10
JK
13640
13641 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13642 if (!attr)
13643 return;
13644
cd02d79d
PA
13645 /* Note that following die references may follow to a die in a
13646 different cu. */
13647
13648 origin_cu = cu;
13649 origin_die = follow_die_ref (die, attr, &origin_cu);
13650
13651 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13652 symbols in. */
13653 origin_previous_list_in_scope = origin_cu->list_in_scope;
13654 origin_cu->list_in_scope = cu->list_in_scope;
13655
edb3359d
DJ
13656 if (die->tag != origin_die->tag
13657 && !(die->tag == DW_TAG_inlined_subroutine
13658 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
13659 complaint (&symfile_complaints,
13660 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9c541725
PA
13661 to_underlying (die->sect_off),
13662 to_underlying (origin_die->sect_off));
d389af10 13663
791afaa2 13664 std::vector<sect_offset> offsets;
d389af10 13665
3ea89b92
PMR
13666 for (child_die = die->child;
13667 child_die && child_die->tag;
13668 child_die = sibling_die (child_die))
13669 {
13670 struct die_info *child_origin_die;
13671 struct dwarf2_cu *child_origin_cu;
13672
13673 /* We are trying to process concrete instance entries:
216f72a1 13674 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13675 it's not relevant to our analysis here. i.e. detecting DIEs that are
13676 present in the abstract instance but not referenced in the concrete
13677 one. */
216f72a1
JK
13678 if (child_die->tag == DW_TAG_call_site
13679 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13680 continue;
13681
c38f313d
DJ
13682 /* For each CHILD_DIE, find the corresponding child of
13683 ORIGIN_DIE. If there is more than one layer of
13684 DW_AT_abstract_origin, follow them all; there shouldn't be,
13685 but GCC versions at least through 4.4 generate this (GCC PR
13686 40573). */
3ea89b92
PMR
13687 child_origin_die = child_die;
13688 child_origin_cu = cu;
c38f313d
DJ
13689 while (1)
13690 {
cd02d79d
PA
13691 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13692 child_origin_cu);
c38f313d
DJ
13693 if (attr == NULL)
13694 break;
cd02d79d
PA
13695 child_origin_die = follow_die_ref (child_origin_die, attr,
13696 &child_origin_cu);
c38f313d
DJ
13697 }
13698
d389af10
JK
13699 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13700 counterpart may exist. */
c38f313d 13701 if (child_origin_die != child_die)
d389af10 13702 {
edb3359d
DJ
13703 if (child_die->tag != child_origin_die->tag
13704 && !(child_die->tag == DW_TAG_inlined_subroutine
13705 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
13706 complaint (&symfile_complaints,
13707 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
13708 "different tags"),
13709 to_underlying (child_die->sect_off),
13710 to_underlying (child_origin_die->sect_off));
c38f313d
DJ
13711 if (child_origin_die->parent != origin_die)
13712 complaint (&symfile_complaints,
13713 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
13714 "different parents"),
13715 to_underlying (child_die->sect_off),
13716 to_underlying (child_origin_die->sect_off));
c38f313d 13717 else
791afaa2 13718 offsets.push_back (child_origin_die->sect_off);
d389af10 13719 }
d389af10 13720 }
791afaa2
TT
13721 std::sort (offsets.begin (), offsets.end ());
13722 sect_offset *offsets_end = offsets.data () + offsets.size ();
13723 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13724 if (offsetp[-1] == *offsetp)
3e43a32a
MS
13725 complaint (&symfile_complaints,
13726 _("Multiple children of DIE 0x%x refer "
13727 "to DIE 0x%x as their abstract origin"),
9c541725 13728 to_underlying (die->sect_off), to_underlying (*offsetp));
d389af10 13729
791afaa2 13730 offsetp = offsets.data ();
d389af10
JK
13731 origin_child_die = origin_die->child;
13732 while (origin_child_die && origin_child_die->tag)
13733 {
13734 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13735 while (offsetp < offsets_end
9c541725 13736 && *offsetp < origin_child_die->sect_off)
d389af10 13737 offsetp++;
b64f50a1 13738 if (offsetp >= offsets_end
9c541725 13739 || *offsetp > origin_child_die->sect_off)
d389af10 13740 {
adde2bff
DE
13741 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13742 Check whether we're already processing ORIGIN_CHILD_DIE.
13743 This can happen with mutually referenced abstract_origins.
13744 PR 16581. */
13745 if (!origin_child_die->in_process)
13746 process_die (origin_child_die, origin_cu);
d389af10
JK
13747 }
13748 origin_child_die = sibling_die (origin_child_die);
13749 }
cd02d79d 13750 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
13751}
13752
c906108c 13753static void
e7c27a73 13754read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13755{
518817b3 13756 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13757 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13758 struct context_stack *newobj;
c906108c
SS
13759 CORE_ADDR lowpc;
13760 CORE_ADDR highpc;
13761 struct die_info *child_die;
edb3359d 13762 struct attribute *attr, *call_line, *call_file;
15d034d0 13763 const char *name;
e142c38c 13764 CORE_ADDR baseaddr;
801e3a5b 13765 struct block *block;
edb3359d 13766 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13767 std::vector<struct symbol *> template_args;
34eaf542 13768 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13769
13770 if (inlined_func)
13771 {
13772 /* If we do not have call site information, we can't show the
13773 caller of this inlined function. That's too confusing, so
13774 only use the scope for local variables. */
13775 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13776 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13777 if (call_line == NULL || call_file == NULL)
13778 {
13779 read_lexical_block_scope (die, cu);
13780 return;
13781 }
13782 }
c906108c 13783
e142c38c
DJ
13784 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13785
94af9270 13786 name = dwarf2_name (die, cu);
c906108c 13787
e8d05480
JB
13788 /* Ignore functions with missing or empty names. These are actually
13789 illegal according to the DWARF standard. */
13790 if (name == NULL)
13791 {
13792 complaint (&symfile_complaints,
b64f50a1 13793 _("missing name for subprogram DIE at %d"),
9c541725 13794 to_underlying (die->sect_off));
e8d05480
JB
13795 return;
13796 }
13797
13798 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13799 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13800 <= PC_BOUNDS_INVALID)
e8d05480 13801 {
ae4d0c03
PM
13802 attr = dwarf2_attr (die, DW_AT_external, cu);
13803 if (!attr || !DW_UNSND (attr))
13804 complaint (&symfile_complaints,
3e43a32a
MS
13805 _("cannot get low and high bounds "
13806 "for subprogram DIE at %d"),
9c541725 13807 to_underlying (die->sect_off));
e8d05480
JB
13808 return;
13809 }
c906108c 13810
3e29f34a
MR
13811 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13812 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13813
34eaf542
TT
13814 /* If we have any template arguments, then we must allocate a
13815 different sort of symbol. */
13816 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13817 {
13818 if (child_die->tag == DW_TAG_template_type_param
13819 || child_die->tag == DW_TAG_template_value_param)
13820 {
e623cf5d 13821 templ_func = allocate_template_symbol (objfile);
cf724bc9 13822 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13823 break;
13824 }
13825 }
13826
fe978cb0 13827 newobj = push_context (0, lowpc);
5e2db402
TT
13828 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13829 (struct symbol *) templ_func);
4c2df51b 13830
4cecd739
DJ
13831 /* If there is a location expression for DW_AT_frame_base, record
13832 it. */
e142c38c 13833 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 13834 if (attr)
fe978cb0 13835 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13836
63e43d3a
PMR
13837 /* If there is a location for the static link, record it. */
13838 newobj->static_link = NULL;
13839 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13840 if (attr)
13841 {
224c3ddb
SM
13842 newobj->static_link
13843 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
13844 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13845 }
13846
e142c38c 13847 cu->list_in_scope = &local_symbols;
c906108c 13848
639d11d3 13849 if (die->child != NULL)
c906108c 13850 {
639d11d3 13851 child_die = die->child;
c906108c
SS
13852 while (child_die && child_die->tag)
13853 {
34eaf542
TT
13854 if (child_die->tag == DW_TAG_template_type_param
13855 || child_die->tag == DW_TAG_template_value_param)
13856 {
13857 struct symbol *arg = new_symbol (child_die, NULL, cu);
13858
f1078f66 13859 if (arg != NULL)
2f4732b0 13860 template_args.push_back (arg);
34eaf542
TT
13861 }
13862 else
13863 process_die (child_die, cu);
c906108c
SS
13864 child_die = sibling_die (child_die);
13865 }
13866 }
13867
d389af10
JK
13868 inherit_abstract_dies (die, cu);
13869
4a811a97
UW
13870 /* If we have a DW_AT_specification, we might need to import using
13871 directives from the context of the specification DIE. See the
13872 comment in determine_prefix. */
13873 if (cu->language == language_cplus
13874 && dwarf2_attr (die, DW_AT_specification, cu))
13875 {
13876 struct dwarf2_cu *spec_cu = cu;
13877 struct die_info *spec_die = die_specification (die, &spec_cu);
13878
13879 while (spec_die)
13880 {
13881 child_die = spec_die->child;
13882 while (child_die && child_die->tag)
13883 {
13884 if (child_die->tag == DW_TAG_imported_module)
13885 process_die (child_die, spec_cu);
13886 child_die = sibling_die (child_die);
13887 }
13888
13889 /* In some cases, GCC generates specification DIEs that
13890 themselves contain DW_AT_specification attributes. */
13891 spec_die = die_specification (spec_die, &spec_cu);
13892 }
13893 }
13894
fe978cb0 13895 newobj = pop_context ();
c906108c 13896 /* Make a block for the local symbols within. */
fe978cb0 13897 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 13898 newobj->static_link, lowpc, highpc);
801e3a5b 13899
df8a16a1 13900 /* For C++, set the block's scope. */
45280282
IB
13901 if ((cu->language == language_cplus
13902 || cu->language == language_fortran
c44af4eb
TT
13903 || cu->language == language_d
13904 || cu->language == language_rust)
4d4ec4e5 13905 && cu->processing_has_namespace_info)
195a3f6c
TT
13906 block_set_scope (block, determine_prefix (die, cu),
13907 &objfile->objfile_obstack);
df8a16a1 13908
801e3a5b
JB
13909 /* If we have address ranges, record them. */
13910 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13911
fe978cb0 13912 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 13913
34eaf542 13914 /* Attach template arguments to function. */
2f4732b0 13915 if (!template_args.empty ())
34eaf542
TT
13916 {
13917 gdb_assert (templ_func != NULL);
13918
2f4732b0 13919 templ_func->n_template_arguments = template_args.size ();
34eaf542 13920 templ_func->template_arguments
8d749320
SM
13921 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13922 templ_func->n_template_arguments);
34eaf542 13923 memcpy (templ_func->template_arguments,
2f4732b0 13924 template_args.data (),
34eaf542 13925 (templ_func->n_template_arguments * sizeof (struct symbol *)));
34eaf542
TT
13926 }
13927
208d8187
JB
13928 /* In C++, we can have functions nested inside functions (e.g., when
13929 a function declares a class that has methods). This means that
13930 when we finish processing a function scope, we may need to go
13931 back to building a containing block's symbol lists. */
fe978cb0 13932 local_symbols = newobj->locals;
22cee43f 13933 local_using_directives = newobj->local_using_directives;
208d8187 13934
921e78cf
JB
13935 /* If we've finished processing a top-level function, subsequent
13936 symbols go in the file symbol list. */
13937 if (outermost_context_p ())
e142c38c 13938 cu->list_in_scope = &file_symbols;
c906108c
SS
13939}
13940
13941/* Process all the DIES contained within a lexical block scope. Start
13942 a new scope, process the dies, and then close the scope. */
13943
13944static void
e7c27a73 13945read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13946{
518817b3 13947 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13948 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13949 struct context_stack *newobj;
c906108c
SS
13950 CORE_ADDR lowpc, highpc;
13951 struct die_info *child_die;
e142c38c
DJ
13952 CORE_ADDR baseaddr;
13953
13954 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13955
13956 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13957 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13958 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13959 be nasty. Might be easier to properly extend generic blocks to
af34e669 13960 describe ranges. */
e385593e
JK
13961 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13962 {
13963 case PC_BOUNDS_NOT_PRESENT:
13964 /* DW_TAG_lexical_block has no attributes, process its children as if
13965 there was no wrapping by that DW_TAG_lexical_block.
13966 GCC does no longer produces such DWARF since GCC r224161. */
13967 for (child_die = die->child;
13968 child_die != NULL && child_die->tag;
13969 child_die = sibling_die (child_die))
13970 process_die (child_die, cu);
13971 return;
13972 case PC_BOUNDS_INVALID:
13973 return;
13974 }
3e29f34a
MR
13975 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13976 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
13977
13978 push_context (0, lowpc);
639d11d3 13979 if (die->child != NULL)
c906108c 13980 {
639d11d3 13981 child_die = die->child;
c906108c
SS
13982 while (child_die && child_die->tag)
13983 {
e7c27a73 13984 process_die (child_die, cu);
c906108c
SS
13985 child_die = sibling_die (child_die);
13986 }
13987 }
3ea89b92 13988 inherit_abstract_dies (die, cu);
fe978cb0 13989 newobj = pop_context ();
c906108c 13990
22cee43f 13991 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 13992 {
801e3a5b 13993 struct block *block
63e43d3a 13994 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 13995 newobj->start_addr, highpc);
801e3a5b
JB
13996
13997 /* Note that recording ranges after traversing children, as we
13998 do here, means that recording a parent's ranges entails
13999 walking across all its children's ranges as they appear in
14000 the address map, which is quadratic behavior.
14001
14002 It would be nicer to record the parent's ranges before
14003 traversing its children, simply overriding whatever you find
14004 there. But since we don't even decide whether to create a
14005 block until after we've traversed its children, that's hard
14006 to do. */
14007 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 14008 }
fe978cb0 14009 local_symbols = newobj->locals;
22cee43f 14010 local_using_directives = newobj->local_using_directives;
c906108c
SS
14011}
14012
216f72a1 14013/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
14014
14015static void
14016read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
14017{
518817b3 14018 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
14019 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14020 CORE_ADDR pc, baseaddr;
14021 struct attribute *attr;
14022 struct call_site *call_site, call_site_local;
14023 void **slot;
14024 int nparams;
14025 struct die_info *child_die;
14026
14027 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14028
216f72a1
JK
14029 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
14030 if (attr == NULL)
14031 {
14032 /* This was a pre-DWARF-5 GNU extension alias
14033 for DW_AT_call_return_pc. */
14034 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14035 }
96408a79
SA
14036 if (!attr)
14037 {
14038 complaint (&symfile_complaints,
216f72a1 14039 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 14040 "DIE 0x%x [in module %s]"),
9c541725 14041 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
14042 return;
14043 }
31aa7e4e 14044 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 14045 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
14046
14047 if (cu->call_site_htab == NULL)
14048 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14049 NULL, &objfile->objfile_obstack,
14050 hashtab_obstack_allocate, NULL);
14051 call_site_local.pc = pc;
14052 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14053 if (*slot != NULL)
14054 {
14055 complaint (&symfile_complaints,
216f72a1 14056 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 14057 "DIE 0x%x [in module %s]"),
9c541725 14058 paddress (gdbarch, pc), to_underlying (die->sect_off),
4262abfb 14059 objfile_name (objfile));
96408a79
SA
14060 return;
14061 }
14062
14063 /* Count parameters at the caller. */
14064
14065 nparams = 0;
14066 for (child_die = die->child; child_die && child_die->tag;
14067 child_die = sibling_die (child_die))
14068 {
216f72a1
JK
14069 if (child_die->tag != DW_TAG_call_site_parameter
14070 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14071 {
14072 complaint (&symfile_complaints,
216f72a1
JK
14073 _("Tag %d is not DW_TAG_call_site_parameter in "
14074 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 14075 child_die->tag, to_underlying (child_die->sect_off),
4262abfb 14076 objfile_name (objfile));
96408a79
SA
14077 continue;
14078 }
14079
14080 nparams++;
14081 }
14082
224c3ddb
SM
14083 call_site
14084 = ((struct call_site *)
14085 obstack_alloc (&objfile->objfile_obstack,
14086 sizeof (*call_site)
14087 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
14088 *slot = call_site;
14089 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14090 call_site->pc = pc;
14091
216f72a1
JK
14092 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14093 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
14094 {
14095 struct die_info *func_die;
14096
14097 /* Skip also over DW_TAG_inlined_subroutine. */
14098 for (func_die = die->parent;
14099 func_die && func_die->tag != DW_TAG_subprogram
14100 && func_die->tag != DW_TAG_subroutine_type;
14101 func_die = func_die->parent);
14102
216f72a1
JK
14103 /* DW_AT_call_all_calls is a superset
14104 of DW_AT_call_all_tail_calls. */
96408a79 14105 if (func_die
216f72a1 14106 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14107 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14108 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14109 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14110 {
14111 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14112 not complete. But keep CALL_SITE for look ups via call_site_htab,
14113 both the initial caller containing the real return address PC and
14114 the final callee containing the current PC of a chain of tail
14115 calls do not need to have the tail call list complete. But any
14116 function candidate for a virtual tail call frame searched via
14117 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14118 determined unambiguously. */
14119 }
14120 else
14121 {
14122 struct type *func_type = NULL;
14123
14124 if (func_die)
14125 func_type = get_die_type (func_die, cu);
14126 if (func_type != NULL)
14127 {
14128 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14129
14130 /* Enlist this call site to the function. */
14131 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14132 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14133 }
14134 else
14135 complaint (&symfile_complaints,
216f72a1 14136 _("Cannot find function owning DW_TAG_call_site "
96408a79 14137 "DIE 0x%x [in module %s]"),
9c541725 14138 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
14139 }
14140 }
14141
216f72a1
JK
14142 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14143 if (attr == NULL)
14144 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14145 if (attr == NULL)
14146 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14147 if (attr == NULL)
216f72a1
JK
14148 {
14149 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14150 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14151 }
96408a79
SA
14152 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14153 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14154 /* Keep NULL DWARF_BLOCK. */;
14155 else if (attr_form_is_block (attr))
14156 {
14157 struct dwarf2_locexpr_baton *dlbaton;
14158
8d749320 14159 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14160 dlbaton->data = DW_BLOCK (attr)->data;
14161 dlbaton->size = DW_BLOCK (attr)->size;
14162 dlbaton->per_cu = cu->per_cu;
14163
14164 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14165 }
7771576e 14166 else if (attr_form_is_ref (attr))
96408a79 14167 {
96408a79
SA
14168 struct dwarf2_cu *target_cu = cu;
14169 struct die_info *target_die;
14170
ac9ec31b 14171 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14172 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14173 if (die_is_declaration (target_die, target_cu))
14174 {
7d45c7c3 14175 const char *target_physname;
9112db09
JK
14176
14177 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14178 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14179 if (target_physname == NULL)
9112db09 14180 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
14181 if (target_physname == NULL)
14182 complaint (&symfile_complaints,
216f72a1 14183 _("DW_AT_call_target target DIE has invalid "
96408a79 14184 "physname, for referencing DIE 0x%x [in module %s]"),
9c541725 14185 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 14186 else
7d455152 14187 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14188 }
14189 else
14190 {
14191 CORE_ADDR lowpc;
14192
14193 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14194 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14195 <= PC_BOUNDS_INVALID)
96408a79 14196 complaint (&symfile_complaints,
216f72a1 14197 _("DW_AT_call_target target DIE has invalid "
96408a79 14198 "low pc, for referencing DIE 0x%x [in module %s]"),
9c541725 14199 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 14200 else
3e29f34a
MR
14201 {
14202 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14203 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14204 }
96408a79
SA
14205 }
14206 }
14207 else
14208 complaint (&symfile_complaints,
216f72a1 14209 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 14210 "block nor reference, for DIE 0x%x [in module %s]"),
9c541725 14211 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
14212
14213 call_site->per_cu = cu->per_cu;
14214
14215 for (child_die = die->child;
14216 child_die && child_die->tag;
14217 child_die = sibling_die (child_die))
14218 {
96408a79 14219 struct call_site_parameter *parameter;
1788b2d3 14220 struct attribute *loc, *origin;
96408a79 14221
216f72a1
JK
14222 if (child_die->tag != DW_TAG_call_site_parameter
14223 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14224 {
14225 /* Already printed the complaint above. */
14226 continue;
14227 }
14228
14229 gdb_assert (call_site->parameter_count < nparams);
14230 parameter = &call_site->parameter[call_site->parameter_count];
14231
1788b2d3
JK
14232 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14233 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14234 register is contained in DW_AT_call_value. */
96408a79 14235
24c5c679 14236 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14237 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14238 if (origin == NULL)
14239 {
14240 /* This was a pre-DWARF-5 GNU extension alias
14241 for DW_AT_call_parameter. */
14242 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14243 }
7771576e 14244 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14245 {
1788b2d3 14246 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14247
14248 sect_offset sect_off
14249 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14250 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14251 {
14252 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14253 binding can be done only inside one CU. Such referenced DIE
14254 therefore cannot be even moved to DW_TAG_partial_unit. */
14255 complaint (&symfile_complaints,
216f72a1
JK
14256 _("DW_AT_call_parameter offset is not in CU for "
14257 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
14258 to_underlying (child_die->sect_off),
14259 objfile_name (objfile));
d76b7dbc
JK
14260 continue;
14261 }
9c541725
PA
14262 parameter->u.param_cu_off
14263 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14264 }
14265 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
14266 {
14267 complaint (&symfile_complaints,
14268 _("No DW_FORM_block* DW_AT_location for "
216f72a1 14269 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 14270 to_underlying (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14271 continue;
14272 }
24c5c679 14273 else
96408a79 14274 {
24c5c679
JK
14275 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14276 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14277 if (parameter->u.dwarf_reg != -1)
14278 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14279 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14280 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14281 &parameter->u.fb_offset))
14282 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14283 else
14284 {
14285 complaint (&symfile_complaints,
14286 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14287 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 14288 "DW_TAG_call_site child DIE 0x%x "
24c5c679 14289 "[in module %s]"),
9c541725
PA
14290 to_underlying (child_die->sect_off),
14291 objfile_name (objfile));
24c5c679
JK
14292 continue;
14293 }
96408a79
SA
14294 }
14295
216f72a1
JK
14296 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14297 if (attr == NULL)
14298 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14299 if (!attr_form_is_block (attr))
14300 {
14301 complaint (&symfile_complaints,
216f72a1
JK
14302 _("No DW_FORM_block* DW_AT_call_value for "
14303 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
14304 to_underlying (child_die->sect_off),
14305 objfile_name (objfile));
96408a79
SA
14306 continue;
14307 }
14308 parameter->value = DW_BLOCK (attr)->data;
14309 parameter->value_size = DW_BLOCK (attr)->size;
14310
14311 /* Parameters are not pre-cleared by memset above. */
14312 parameter->data_value = NULL;
14313 parameter->data_value_size = 0;
14314 call_site->parameter_count++;
14315
216f72a1
JK
14316 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14317 if (attr == NULL)
14318 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14319 if (attr)
14320 {
14321 if (!attr_form_is_block (attr))
14322 complaint (&symfile_complaints,
216f72a1
JK
14323 _("No DW_FORM_block* DW_AT_call_data_value for "
14324 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
14325 to_underlying (child_die->sect_off),
14326 objfile_name (objfile));
96408a79
SA
14327 else
14328 {
14329 parameter->data_value = DW_BLOCK (attr)->data;
14330 parameter->data_value_size = DW_BLOCK (attr)->size;
14331 }
14332 }
14333 }
14334}
14335
71a3c369
TT
14336/* Helper function for read_variable. If DIE represents a virtual
14337 table, then return the type of the concrete object that is
14338 associated with the virtual table. Otherwise, return NULL. */
14339
14340static struct type *
14341rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14342{
14343 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14344 if (attr == NULL)
14345 return NULL;
14346
14347 /* Find the type DIE. */
14348 struct die_info *type_die = NULL;
14349 struct dwarf2_cu *type_cu = cu;
14350
14351 if (attr_form_is_ref (attr))
14352 type_die = follow_die_ref (die, attr, &type_cu);
14353 if (type_die == NULL)
14354 return NULL;
14355
14356 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14357 return NULL;
14358 return die_containing_type (type_die, type_cu);
14359}
14360
14361/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14362
14363static void
14364read_variable (struct die_info *die, struct dwarf2_cu *cu)
14365{
14366 struct rust_vtable_symbol *storage = NULL;
14367
14368 if (cu->language == language_rust)
14369 {
14370 struct type *containing_type = rust_containing_type (die, cu);
14371
14372 if (containing_type != NULL)
14373 {
518817b3 14374 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369
TT
14375
14376 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14377 struct rust_vtable_symbol);
14378 initialize_objfile_symbol (storage);
14379 storage->concrete_type = containing_type;
cf724bc9 14380 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14381 }
14382 }
14383
5e2db402 14384 new_symbol (die, NULL, cu, storage);
71a3c369
TT
14385}
14386
43988095
JK
14387/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14388 reading .debug_rnglists.
14389 Callback's type should be:
14390 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14391 Return true if the attributes are present and valid, otherwise,
14392 return false. */
14393
14394template <typename Callback>
14395static bool
14396dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14397 Callback &&callback)
14398{
ed2dc618 14399 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14400 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14401 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14402 bfd *obfd = objfile->obfd;
43988095
JK
14403 /* Base address selection entry. */
14404 CORE_ADDR base;
14405 int found_base;
43988095 14406 const gdb_byte *buffer;
43988095
JK
14407 CORE_ADDR baseaddr;
14408 bool overflow = false;
14409
14410 found_base = cu->base_known;
14411 base = cu->base_address;
14412
14413 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14414 if (offset >= dwarf2_per_objfile->rnglists.size)
14415 {
14416 complaint (&symfile_complaints,
14417 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14418 offset);
14419 return false;
14420 }
14421 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14422
14423 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14424
14425 while (1)
14426 {
7814882a
JK
14427 /* Initialize it due to a false compiler warning. */
14428 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14429 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14430 + dwarf2_per_objfile->rnglists.size);
14431 unsigned int bytes_read;
14432
14433 if (buffer == buf_end)
14434 {
14435 overflow = true;
14436 break;
14437 }
14438 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14439 switch (rlet)
14440 {
14441 case DW_RLE_end_of_list:
14442 break;
14443 case DW_RLE_base_address:
14444 if (buffer + cu->header.addr_size > buf_end)
14445 {
14446 overflow = true;
14447 break;
14448 }
14449 base = read_address (obfd, buffer, cu, &bytes_read);
14450 found_base = 1;
14451 buffer += bytes_read;
14452 break;
14453 case DW_RLE_start_length:
14454 if (buffer + cu->header.addr_size > buf_end)
14455 {
14456 overflow = true;
14457 break;
14458 }
14459 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14460 buffer += bytes_read;
14461 range_end = (range_beginning
14462 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14463 buffer += bytes_read;
14464 if (buffer > buf_end)
14465 {
14466 overflow = true;
14467 break;
14468 }
14469 break;
14470 case DW_RLE_offset_pair:
14471 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14472 buffer += bytes_read;
14473 if (buffer > buf_end)
14474 {
14475 overflow = true;
14476 break;
14477 }
14478 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14479 buffer += bytes_read;
14480 if (buffer > buf_end)
14481 {
14482 overflow = true;
14483 break;
14484 }
14485 break;
14486 case DW_RLE_start_end:
14487 if (buffer + 2 * cu->header.addr_size > buf_end)
14488 {
14489 overflow = true;
14490 break;
14491 }
14492 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14493 buffer += bytes_read;
14494 range_end = read_address (obfd, buffer, cu, &bytes_read);
14495 buffer += bytes_read;
14496 break;
14497 default:
14498 complaint (&symfile_complaints,
14499 _("Invalid .debug_rnglists data (no base address)"));
14500 return false;
14501 }
14502 if (rlet == DW_RLE_end_of_list || overflow)
14503 break;
14504 if (rlet == DW_RLE_base_address)
14505 continue;
14506
14507 if (!found_base)
14508 {
14509 /* We have no valid base address for the ranges
14510 data. */
14511 complaint (&symfile_complaints,
14512 _("Invalid .debug_rnglists data (no base address)"));
14513 return false;
14514 }
14515
14516 if (range_beginning > range_end)
14517 {
14518 /* Inverted range entries are invalid. */
14519 complaint (&symfile_complaints,
14520 _("Invalid .debug_rnglists data (inverted range)"));
14521 return false;
14522 }
14523
14524 /* Empty range entries have no effect. */
14525 if (range_beginning == range_end)
14526 continue;
14527
14528 range_beginning += base;
14529 range_end += base;
14530
14531 /* A not-uncommon case of bad debug info.
14532 Don't pollute the addrmap with bad data. */
14533 if (range_beginning + baseaddr == 0
14534 && !dwarf2_per_objfile->has_section_at_zero)
14535 {
14536 complaint (&symfile_complaints,
14537 _(".debug_rnglists entry has start address of zero"
14538 " [in module %s]"), objfile_name (objfile));
14539 continue;
14540 }
14541
14542 callback (range_beginning, range_end);
14543 }
14544
14545 if (overflow)
14546 {
14547 complaint (&symfile_complaints,
14548 _("Offset %d is not terminated "
14549 "for DW_AT_ranges attribute"),
14550 offset);
14551 return false;
14552 }
14553
14554 return true;
14555}
14556
14557/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14558 Callback's type should be:
14559 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14560 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14561
43988095 14562template <typename Callback>
43039443 14563static int
5f46c5a5 14564dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14565 Callback &&callback)
43039443 14566{
ed2dc618 14567 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14568 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14569 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14570 struct comp_unit_head *cu_header = &cu->header;
14571 bfd *obfd = objfile->obfd;
14572 unsigned int addr_size = cu_header->addr_size;
14573 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14574 /* Base address selection entry. */
14575 CORE_ADDR base;
14576 int found_base;
14577 unsigned int dummy;
d521ce57 14578 const gdb_byte *buffer;
ff013f42 14579 CORE_ADDR baseaddr;
43039443 14580
43988095
JK
14581 if (cu_header->version >= 5)
14582 return dwarf2_rnglists_process (offset, cu, callback);
14583
d00adf39
DE
14584 found_base = cu->base_known;
14585 base = cu->base_address;
43039443 14586
be391dca 14587 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14588 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
14589 {
14590 complaint (&symfile_complaints,
14591 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14592 offset);
14593 return 0;
14594 }
dce234bc 14595 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14596
e7030f15 14597 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14598
43039443
JK
14599 while (1)
14600 {
14601 CORE_ADDR range_beginning, range_end;
14602
14603 range_beginning = read_address (obfd, buffer, cu, &dummy);
14604 buffer += addr_size;
14605 range_end = read_address (obfd, buffer, cu, &dummy);
14606 buffer += addr_size;
14607 offset += 2 * addr_size;
14608
14609 /* An end of list marker is a pair of zero addresses. */
14610 if (range_beginning == 0 && range_end == 0)
14611 /* Found the end of list entry. */
14612 break;
14613
14614 /* Each base address selection entry is a pair of 2 values.
14615 The first is the largest possible address, the second is
14616 the base address. Check for a base address here. */
14617 if ((range_beginning & mask) == mask)
14618 {
28d2bfb9
AB
14619 /* If we found the largest possible address, then we already
14620 have the base address in range_end. */
14621 base = range_end;
43039443
JK
14622 found_base = 1;
14623 continue;
14624 }
14625
14626 if (!found_base)
14627 {
14628 /* We have no valid base address for the ranges
14629 data. */
14630 complaint (&symfile_complaints,
14631 _("Invalid .debug_ranges data (no base address)"));
14632 return 0;
14633 }
14634
9277c30c
UW
14635 if (range_beginning > range_end)
14636 {
14637 /* Inverted range entries are invalid. */
14638 complaint (&symfile_complaints,
14639 _("Invalid .debug_ranges data (inverted range)"));
14640 return 0;
14641 }
14642
14643 /* Empty range entries have no effect. */
14644 if (range_beginning == range_end)
14645 continue;
14646
43039443
JK
14647 range_beginning += base;
14648 range_end += base;
14649
01093045
DE
14650 /* A not-uncommon case of bad debug info.
14651 Don't pollute the addrmap with bad data. */
14652 if (range_beginning + baseaddr == 0
14653 && !dwarf2_per_objfile->has_section_at_zero)
14654 {
14655 complaint (&symfile_complaints,
14656 _(".debug_ranges entry has start address of zero"
4262abfb 14657 " [in module %s]"), objfile_name (objfile));
01093045
DE
14658 continue;
14659 }
14660
5f46c5a5
JK
14661 callback (range_beginning, range_end);
14662 }
14663
14664 return 1;
14665}
14666
14667/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14668 Return 1 if the attributes are present and valid, otherwise, return 0.
14669 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14670
14671static int
14672dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14673 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14674 struct partial_symtab *ranges_pst)
14675{
518817b3 14676 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14677 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14678 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14679 SECT_OFF_TEXT (objfile));
14680 int low_set = 0;
14681 CORE_ADDR low = 0;
14682 CORE_ADDR high = 0;
14683 int retval;
14684
14685 retval = dwarf2_ranges_process (offset, cu,
14686 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14687 {
9277c30c 14688 if (ranges_pst != NULL)
3e29f34a
MR
14689 {
14690 CORE_ADDR lowpc;
14691 CORE_ADDR highpc;
14692
14693 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14694 range_beginning + baseaddr);
14695 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14696 range_end + baseaddr);
14697 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14698 ranges_pst);
14699 }
ff013f42 14700
43039443
JK
14701 /* FIXME: This is recording everything as a low-high
14702 segment of consecutive addresses. We should have a
14703 data structure for discontiguous block ranges
14704 instead. */
14705 if (! low_set)
14706 {
14707 low = range_beginning;
14708 high = range_end;
14709 low_set = 1;
14710 }
14711 else
14712 {
14713 if (range_beginning < low)
14714 low = range_beginning;
14715 if (range_end > high)
14716 high = range_end;
14717 }
5f46c5a5
JK
14718 });
14719 if (!retval)
14720 return 0;
43039443
JK
14721
14722 if (! low_set)
14723 /* If the first entry is an end-of-list marker, the range
14724 describes an empty scope, i.e. no instructions. */
14725 return 0;
14726
14727 if (low_return)
14728 *low_return = low;
14729 if (high_return)
14730 *high_return = high;
14731 return 1;
14732}
14733
3a2b436a
JK
14734/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14735 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14736 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14737
3a2b436a 14738static enum pc_bounds_kind
af34e669 14739dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14740 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14741 struct partial_symtab *pst)
c906108c 14742{
518817b3
SM
14743 struct dwarf2_per_objfile *dwarf2_per_objfile
14744 = cu->per_cu->dwarf2_per_objfile;
c906108c 14745 struct attribute *attr;
91da1414 14746 struct attribute *attr_high;
af34e669
DJ
14747 CORE_ADDR low = 0;
14748 CORE_ADDR high = 0;
e385593e 14749 enum pc_bounds_kind ret;
c906108c 14750
91da1414
MW
14751 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14752 if (attr_high)
af34e669 14753 {
e142c38c 14754 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 14755 if (attr)
91da1414 14756 {
31aa7e4e
JB
14757 low = attr_value_as_address (attr);
14758 high = attr_value_as_address (attr_high);
14759 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14760 high += low;
91da1414 14761 }
af34e669
DJ
14762 else
14763 /* Found high w/o low attribute. */
e385593e 14764 return PC_BOUNDS_INVALID;
af34e669
DJ
14765
14766 /* Found consecutive range of addresses. */
3a2b436a 14767 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14768 }
c906108c 14769 else
af34e669 14770 {
e142c38c 14771 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14772 if (attr != NULL)
14773 {
ab435259
DE
14774 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14775 We take advantage of the fact that DW_AT_ranges does not appear
14776 in DW_TAG_compile_unit of DWO files. */
14777 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14778 unsigned int ranges_offset = (DW_UNSND (attr)
14779 + (need_ranges_base
14780 ? cu->ranges_base
14781 : 0));
2e3cf129 14782
af34e669 14783 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14784 .debug_ranges section. */
2e3cf129 14785 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14786 return PC_BOUNDS_INVALID;
43039443 14787 /* Found discontinuous range of addresses. */
3a2b436a 14788 ret = PC_BOUNDS_RANGES;
af34e669 14789 }
e385593e
JK
14790 else
14791 return PC_BOUNDS_NOT_PRESENT;
af34e669 14792 }
c906108c 14793
9373cf26
JK
14794 /* read_partial_die has also the strict LOW < HIGH requirement. */
14795 if (high <= low)
e385593e 14796 return PC_BOUNDS_INVALID;
c906108c
SS
14797
14798 /* When using the GNU linker, .gnu.linkonce. sections are used to
14799 eliminate duplicate copies of functions and vtables and such.
14800 The linker will arbitrarily choose one and discard the others.
14801 The AT_*_pc values for such functions refer to local labels in
14802 these sections. If the section from that file was discarded, the
14803 labels are not in the output, so the relocs get a value of 0.
14804 If this is a discarded function, mark the pc bounds as invalid,
14805 so that GDB will ignore it. */
72dca2f5 14806 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14807 return PC_BOUNDS_INVALID;
c906108c
SS
14808
14809 *lowpc = low;
96408a79
SA
14810 if (highpc)
14811 *highpc = high;
af34e669 14812 return ret;
c906108c
SS
14813}
14814
b084d499
JB
14815/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14816 its low and high PC addresses. Do nothing if these addresses could not
14817 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14818 and HIGHPC to the high address if greater than HIGHPC. */
14819
14820static void
14821dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14822 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14823 struct dwarf2_cu *cu)
14824{
14825 CORE_ADDR low, high;
14826 struct die_info *child = die->child;
14827
e385593e 14828 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14829 {
325fac50
PA
14830 *lowpc = std::min (*lowpc, low);
14831 *highpc = std::max (*highpc, high);
b084d499
JB
14832 }
14833
14834 /* If the language does not allow nested subprograms (either inside
14835 subprograms or lexical blocks), we're done. */
14836 if (cu->language != language_ada)
14837 return;
6e70227d 14838
b084d499
JB
14839 /* Check all the children of the given DIE. If it contains nested
14840 subprograms, then check their pc bounds. Likewise, we need to
14841 check lexical blocks as well, as they may also contain subprogram
14842 definitions. */
14843 while (child && child->tag)
14844 {
14845 if (child->tag == DW_TAG_subprogram
14846 || child->tag == DW_TAG_lexical_block)
14847 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14848 child = sibling_die (child);
14849 }
14850}
14851
fae299cd
DC
14852/* Get the low and high pc's represented by the scope DIE, and store
14853 them in *LOWPC and *HIGHPC. If the correct values can't be
14854 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14855
14856static void
14857get_scope_pc_bounds (struct die_info *die,
14858 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14859 struct dwarf2_cu *cu)
14860{
14861 CORE_ADDR best_low = (CORE_ADDR) -1;
14862 CORE_ADDR best_high = (CORE_ADDR) 0;
14863 CORE_ADDR current_low, current_high;
14864
3a2b436a 14865 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14866 >= PC_BOUNDS_RANGES)
fae299cd
DC
14867 {
14868 best_low = current_low;
14869 best_high = current_high;
14870 }
14871 else
14872 {
14873 struct die_info *child = die->child;
14874
14875 while (child && child->tag)
14876 {
14877 switch (child->tag) {
14878 case DW_TAG_subprogram:
b084d499 14879 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14880 break;
14881 case DW_TAG_namespace:
f55ee35c 14882 case DW_TAG_module:
fae299cd
DC
14883 /* FIXME: carlton/2004-01-16: Should we do this for
14884 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14885 that current GCC's always emit the DIEs corresponding
14886 to definitions of methods of classes as children of a
14887 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14888 the DIEs giving the declarations, which could be
14889 anywhere). But I don't see any reason why the
14890 standards says that they have to be there. */
14891 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14892
14893 if (current_low != ((CORE_ADDR) -1))
14894 {
325fac50
PA
14895 best_low = std::min (best_low, current_low);
14896 best_high = std::max (best_high, current_high);
fae299cd
DC
14897 }
14898 break;
14899 default:
0963b4bd 14900 /* Ignore. */
fae299cd
DC
14901 break;
14902 }
14903
14904 child = sibling_die (child);
14905 }
14906 }
14907
14908 *lowpc = best_low;
14909 *highpc = best_high;
14910}
14911
801e3a5b
JB
14912/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14913 in DIE. */
380bca97 14914
801e3a5b
JB
14915static void
14916dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14917 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14918{
518817b3 14919 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14920 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14921 struct attribute *attr;
91da1414 14922 struct attribute *attr_high;
801e3a5b 14923
91da1414
MW
14924 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14925 if (attr_high)
801e3a5b 14926 {
801e3a5b
JB
14927 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14928 if (attr)
14929 {
31aa7e4e
JB
14930 CORE_ADDR low = attr_value_as_address (attr);
14931 CORE_ADDR high = attr_value_as_address (attr_high);
14932
14933 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14934 high += low;
9a619af0 14935
3e29f34a
MR
14936 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14937 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14938 record_block_range (block, low, high - 1);
801e3a5b
JB
14939 }
14940 }
14941
14942 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14943 if (attr)
14944 {
ab435259
DE
14945 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14946 We take advantage of the fact that DW_AT_ranges does not appear
14947 in DW_TAG_compile_unit of DWO files. */
14948 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14949
14950 /* The value of the DW_AT_ranges attribute is the offset of the
14951 address range list in the .debug_ranges section. */
ab435259
DE
14952 unsigned long offset = (DW_UNSND (attr)
14953 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 14954 const gdb_byte *buffer;
801e3a5b
JB
14955
14956 /* For some target architectures, but not others, the
14957 read_address function sign-extends the addresses it returns.
14958 To recognize base address selection entries, we need a
14959 mask. */
14960 unsigned int addr_size = cu->header.addr_size;
14961 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14962
14963 /* The base address, to which the next pair is relative. Note
14964 that this 'base' is a DWARF concept: most entries in a range
14965 list are relative, to reduce the number of relocs against the
14966 debugging information. This is separate from this function's
14967 'baseaddr' argument, which GDB uses to relocate debugging
14968 information from a shared library based on the address at
14969 which the library was loaded. */
d00adf39
DE
14970 CORE_ADDR base = cu->base_address;
14971 int base_known = cu->base_known;
801e3a5b 14972
5f46c5a5
JK
14973 dwarf2_ranges_process (offset, cu,
14974 [&] (CORE_ADDR start, CORE_ADDR end)
14975 {
58fdfd2c
JK
14976 start += baseaddr;
14977 end += baseaddr;
5f46c5a5
JK
14978 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14979 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14980 record_block_range (block, start, end - 1);
14981 });
801e3a5b
JB
14982 }
14983}
14984
685b1105
JK
14985/* Check whether the producer field indicates either of GCC < 4.6, or the
14986 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14987
685b1105
JK
14988static void
14989check_producer (struct dwarf2_cu *cu)
60d5a603 14990{
38360086 14991 int major, minor;
60d5a603
JK
14992
14993 if (cu->producer == NULL)
14994 {
14995 /* For unknown compilers expect their behavior is DWARF version
14996 compliant.
14997
14998 GCC started to support .debug_types sections by -gdwarf-4 since
14999 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
15000 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
15001 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
15002 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 15003 }
b1ffba5a 15004 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 15005 {
38360086
MW
15006 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
15007 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 15008 }
5230b05a
WT
15009 else if (producer_is_icc (cu->producer, &major, &minor))
15010 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
15011 else
15012 {
15013 /* For other non-GCC compilers, expect their behavior is DWARF version
15014 compliant. */
60d5a603
JK
15015 }
15016
ba919b58 15017 cu->checked_producer = 1;
685b1105 15018}
ba919b58 15019
685b1105
JK
15020/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
15021 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
15022 during 4.6.0 experimental. */
15023
15024static int
15025producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
15026{
15027 if (!cu->checked_producer)
15028 check_producer (cu);
15029
15030 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
15031}
15032
15033/* Return the default accessibility type if it is not overriden by
15034 DW_AT_accessibility. */
15035
15036static enum dwarf_access_attribute
15037dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15038{
15039 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15040 {
15041 /* The default DWARF 2 accessibility for members is public, the default
15042 accessibility for inheritance is private. */
15043
15044 if (die->tag != DW_TAG_inheritance)
15045 return DW_ACCESS_public;
15046 else
15047 return DW_ACCESS_private;
15048 }
15049 else
15050 {
15051 /* DWARF 3+ defines the default accessibility a different way. The same
15052 rules apply now for DW_TAG_inheritance as for the members and it only
15053 depends on the container kind. */
15054
15055 if (die->parent->tag == DW_TAG_class_type)
15056 return DW_ACCESS_private;
15057 else
15058 return DW_ACCESS_public;
15059 }
15060}
15061
74ac6d43
TT
15062/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15063 offset. If the attribute was not found return 0, otherwise return
15064 1. If it was found but could not properly be handled, set *OFFSET
15065 to 0. */
15066
15067static int
15068handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15069 LONGEST *offset)
15070{
15071 struct attribute *attr;
15072
15073 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15074 if (attr != NULL)
15075 {
15076 *offset = 0;
15077
15078 /* Note that we do not check for a section offset first here.
15079 This is because DW_AT_data_member_location is new in DWARF 4,
15080 so if we see it, we can assume that a constant form is really
15081 a constant and not a section offset. */
15082 if (attr_form_is_constant (attr))
15083 *offset = dwarf2_get_attr_constant_value (attr, 0);
15084 else if (attr_form_is_section_offset (attr))
15085 dwarf2_complex_location_expr_complaint ();
15086 else if (attr_form_is_block (attr))
15087 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15088 else
15089 dwarf2_complex_location_expr_complaint ();
15090
15091 return 1;
15092 }
15093
15094 return 0;
15095}
15096
c906108c
SS
15097/* Add an aggregate field to the field list. */
15098
15099static void
107d2387 15100dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15101 struct dwarf2_cu *cu)
6e70227d 15102{
518817b3 15103 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15104 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15105 struct nextfield *new_field;
15106 struct attribute *attr;
15107 struct field *fp;
15d034d0 15108 const char *fieldname = "";
c906108c
SS
15109
15110 /* Allocate a new field list entry and link it in. */
8d749320 15111 new_field = XNEW (struct nextfield);
b8c9b27d 15112 make_cleanup (xfree, new_field);
c906108c 15113 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
15114
15115 if (die->tag == DW_TAG_inheritance)
15116 {
15117 new_field->next = fip->baseclasses;
15118 fip->baseclasses = new_field;
15119 }
15120 else
15121 {
15122 new_field->next = fip->fields;
15123 fip->fields = new_field;
15124 }
c906108c
SS
15125 fip->nfields++;
15126
e142c38c 15127 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
15128 if (attr)
15129 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15130 else
15131 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15132 if (new_field->accessibility != DW_ACCESS_public)
15133 fip->non_public_fields = 1;
60d5a603 15134
e142c38c 15135 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
15136 if (attr)
15137 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15138 else
15139 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15140
15141 fp = &new_field->field;
a9a9bd0f 15142
e142c38c 15143 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15144 {
74ac6d43
TT
15145 LONGEST offset;
15146
a9a9bd0f 15147 /* Data member other than a C++ static data member. */
6e70227d 15148
c906108c 15149 /* Get type of field. */
e7c27a73 15150 fp->type = die_type (die, cu);
c906108c 15151
d6a843b5 15152 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15153
c906108c 15154 /* Get bit size of field (zero if none). */
e142c38c 15155 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
15156 if (attr)
15157 {
15158 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15159 }
15160 else
15161 {
15162 FIELD_BITSIZE (*fp) = 0;
15163 }
15164
15165 /* Get bit offset of field. */
74ac6d43
TT
15166 if (handle_data_member_location (die, cu, &offset))
15167 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15168 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
15169 if (attr)
15170 {
5e2b427d 15171 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
15172 {
15173 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15174 additional bit offset from the MSB of the containing
15175 anonymous object to the MSB of the field. We don't
15176 have to do anything special since we don't need to
15177 know the size of the anonymous object. */
f41f5e61 15178 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15179 }
15180 else
15181 {
15182 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15183 MSB of the anonymous object, subtract off the number of
15184 bits from the MSB of the field to the MSB of the
15185 object, and then subtract off the number of bits of
15186 the field itself. The result is the bit offset of
15187 the LSB of the field. */
c906108c
SS
15188 int anonymous_size;
15189 int bit_offset = DW_UNSND (attr);
15190
e142c38c 15191 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15192 if (attr)
15193 {
15194 /* The size of the anonymous object containing
15195 the bit field is explicit, so use the
15196 indicated size (in bytes). */
15197 anonymous_size = DW_UNSND (attr);
15198 }
15199 else
15200 {
15201 /* The size of the anonymous object containing
15202 the bit field must be inferred from the type
15203 attribute of the data member containing the
15204 bit field. */
15205 anonymous_size = TYPE_LENGTH (fp->type);
15206 }
f41f5e61
PA
15207 SET_FIELD_BITPOS (*fp,
15208 (FIELD_BITPOS (*fp)
15209 + anonymous_size * bits_per_byte
15210 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15211 }
15212 }
da5b30da
AA
15213 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15214 if (attr != NULL)
15215 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15216 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15217
15218 /* Get name of field. */
39cbfefa
DJ
15219 fieldname = dwarf2_name (die, cu);
15220 if (fieldname == NULL)
15221 fieldname = "";
d8151005
DJ
15222
15223 /* The name is already allocated along with this objfile, so we don't
15224 need to duplicate it for the type. */
15225 fp->name = fieldname;
c906108c
SS
15226
15227 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15228 pointer or virtual base class pointer) to private. */
e142c38c 15229 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15230 {
d48cc9dd 15231 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15232 new_field->accessibility = DW_ACCESS_private;
15233 fip->non_public_fields = 1;
15234 }
15235 }
a9a9bd0f 15236 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15237 {
a9a9bd0f
DC
15238 /* C++ static member. */
15239
15240 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15241 is a declaration, but all versions of G++ as of this writing
15242 (so through at least 3.2.1) incorrectly generate
15243 DW_TAG_variable tags. */
6e70227d 15244
ff355380 15245 const char *physname;
c906108c 15246
a9a9bd0f 15247 /* Get name of field. */
39cbfefa
DJ
15248 fieldname = dwarf2_name (die, cu);
15249 if (fieldname == NULL)
c906108c
SS
15250 return;
15251
254e6b9e 15252 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15253 if (attr
15254 /* Only create a symbol if this is an external value.
15255 new_symbol checks this and puts the value in the global symbol
15256 table, which we want. If it is not external, new_symbol
15257 will try to put the value in cu->list_in_scope which is wrong. */
15258 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15259 {
15260 /* A static const member, not much different than an enum as far as
15261 we're concerned, except that we can support more types. */
15262 new_symbol (die, NULL, cu);
15263 }
15264
2df3850c 15265 /* Get physical name. */
ff355380 15266 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15267
d8151005
DJ
15268 /* The name is already allocated along with this objfile, so we don't
15269 need to duplicate it for the type. */
15270 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15271 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15272 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15273 }
15274 else if (die->tag == DW_TAG_inheritance)
15275 {
74ac6d43 15276 LONGEST offset;
d4b96c9a 15277
74ac6d43
TT
15278 /* C++ base class field. */
15279 if (handle_data_member_location (die, cu, &offset))
15280 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15281 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15282 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
15283 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15284 fip->nbaseclasses++;
15285 }
15286}
15287
883fd55a
KS
15288/* Can the type given by DIE define another type? */
15289
15290static bool
15291type_can_define_types (const struct die_info *die)
15292{
15293 switch (die->tag)
15294 {
15295 case DW_TAG_typedef:
15296 case DW_TAG_class_type:
15297 case DW_TAG_structure_type:
15298 case DW_TAG_union_type:
15299 case DW_TAG_enumeration_type:
15300 return true;
15301
15302 default:
15303 return false;
15304 }
15305}
15306
15307/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15308
15309static void
883fd55a
KS
15310dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15311 struct dwarf2_cu *cu)
6e70227d 15312{
883fd55a
KS
15313 struct decl_field_list *new_field;
15314 struct decl_field *fp;
98751a41
JK
15315
15316 /* Allocate a new field list entry and link it in. */
883fd55a 15317 new_field = XCNEW (struct decl_field_list);
98751a41
JK
15318 make_cleanup (xfree, new_field);
15319
883fd55a 15320 gdb_assert (type_can_define_types (die));
98751a41
JK
15321
15322 fp = &new_field->field;
15323
883fd55a 15324 /* Get name of field. NULL is okay here, meaning an anonymous type. */
98751a41 15325 fp->name = dwarf2_name (die, cu);
98751a41
JK
15326 fp->type = read_type_die (die, cu);
15327
c191a687
KS
15328 /* Save accessibility. */
15329 enum dwarf_access_attribute accessibility;
15330 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15331 if (attr != NULL)
15332 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15333 else
15334 accessibility = dwarf2_default_access_attribute (die, cu);
15335 switch (accessibility)
15336 {
15337 case DW_ACCESS_public:
15338 /* The assumed value if neither private nor protected. */
15339 break;
15340 case DW_ACCESS_private:
15341 fp->is_private = 1;
15342 break;
15343 case DW_ACCESS_protected:
15344 fp->is_protected = 1;
15345 break;
15346 default:
37534686
KS
15347 complaint (&symfile_complaints,
15348 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15349 }
15350
883fd55a
KS
15351 if (die->tag == DW_TAG_typedef)
15352 {
15353 new_field->next = fip->typedef_field_list;
15354 fip->typedef_field_list = new_field;
15355 fip->typedef_field_list_count++;
15356 }
15357 else
15358 {
15359 new_field->next = fip->nested_types_list;
15360 fip->nested_types_list = new_field;
15361 fip->nested_types_list_count++;
15362 }
98751a41
JK
15363}
15364
c906108c
SS
15365/* Create the vector of fields, and attach it to the type. */
15366
15367static void
fba45db2 15368dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15369 struct dwarf2_cu *cu)
c906108c
SS
15370{
15371 int nfields = fip->nfields;
15372
15373 /* Record the field count, allocate space for the array of fields,
15374 and create blank accessibility bitfields if necessary. */
15375 TYPE_NFIELDS (type) = nfields;
15376 TYPE_FIELDS (type) = (struct field *)
15377 TYPE_ALLOC (type, sizeof (struct field) * nfields);
15378 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
15379
b4ba55a1 15380 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15381 {
15382 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15383
15384 TYPE_FIELD_PRIVATE_BITS (type) =
15385 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15386 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15387
15388 TYPE_FIELD_PROTECTED_BITS (type) =
15389 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15390 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15391
774b6a14
TT
15392 TYPE_FIELD_IGNORE_BITS (type) =
15393 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15394 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15395 }
15396
15397 /* If the type has baseclasses, allocate and clear a bit vector for
15398 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 15399 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
15400 {
15401 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 15402 unsigned char *pointer;
c906108c
SS
15403
15404 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15405 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15406 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
15407 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
15408 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
15409 }
15410
3e43a32a
MS
15411 /* Copy the saved-up fields into the field vector. Start from the head of
15412 the list, adding to the tail of the field array, so that they end up in
15413 the same order in the array in which they were added to the list. */
c906108c
SS
15414 while (nfields-- > 0)
15415 {
7d0ccb61
DJ
15416 struct nextfield *fieldp;
15417
15418 if (fip->fields)
15419 {
15420 fieldp = fip->fields;
15421 fip->fields = fieldp->next;
15422 }
15423 else
15424 {
15425 fieldp = fip->baseclasses;
15426 fip->baseclasses = fieldp->next;
15427 }
15428
15429 TYPE_FIELD (type, nfields) = fieldp->field;
15430 switch (fieldp->accessibility)
c906108c 15431 {
c5aa993b 15432 case DW_ACCESS_private:
b4ba55a1
JB
15433 if (cu->language != language_ada)
15434 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 15435 break;
c906108c 15436
c5aa993b 15437 case DW_ACCESS_protected:
b4ba55a1
JB
15438 if (cu->language != language_ada)
15439 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 15440 break;
c906108c 15441
c5aa993b
JM
15442 case DW_ACCESS_public:
15443 break;
c906108c 15444
c5aa993b
JM
15445 default:
15446 /* Unknown accessibility. Complain and treat it as public. */
15447 {
e2e0b3e5 15448 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 15449 fieldp->accessibility);
c5aa993b
JM
15450 }
15451 break;
c906108c
SS
15452 }
15453 if (nfields < fip->nbaseclasses)
15454 {
7d0ccb61 15455 switch (fieldp->virtuality)
c906108c 15456 {
c5aa993b
JM
15457 case DW_VIRTUALITY_virtual:
15458 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15459 if (cu->language == language_ada)
a73c6dcd 15460 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
15461 SET_TYPE_FIELD_VIRTUAL (type, nfields);
15462 break;
c906108c
SS
15463 }
15464 }
c906108c
SS
15465 }
15466}
15467
7d27a96d
TT
15468/* Return true if this member function is a constructor, false
15469 otherwise. */
15470
15471static int
15472dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15473{
15474 const char *fieldname;
fe978cb0 15475 const char *type_name;
7d27a96d
TT
15476 int len;
15477
15478 if (die->parent == NULL)
15479 return 0;
15480
15481 if (die->parent->tag != DW_TAG_structure_type
15482 && die->parent->tag != DW_TAG_union_type
15483 && die->parent->tag != DW_TAG_class_type)
15484 return 0;
15485
15486 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15487 type_name = dwarf2_name (die->parent, cu);
15488 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15489 return 0;
15490
15491 len = strlen (fieldname);
fe978cb0
PA
15492 return (strncmp (fieldname, type_name, len) == 0
15493 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15494}
15495
c906108c
SS
15496/* Add a member function to the proper fieldlist. */
15497
15498static void
107d2387 15499dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15500 struct type *type, struct dwarf2_cu *cu)
c906108c 15501{
518817b3 15502 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15503 struct attribute *attr;
15504 struct fnfieldlist *flp;
15505 int i;
15506 struct fn_field *fnp;
15d034d0 15507 const char *fieldname;
c906108c 15508 struct nextfnfield *new_fnfield;
f792889a 15509 struct type *this_type;
60d5a603 15510 enum dwarf_access_attribute accessibility;
c906108c 15511
b4ba55a1 15512 if (cu->language == language_ada)
a73c6dcd 15513 error (_("unexpected member function in Ada type"));
b4ba55a1 15514
2df3850c 15515 /* Get name of member function. */
39cbfefa
DJ
15516 fieldname = dwarf2_name (die, cu);
15517 if (fieldname == NULL)
2df3850c 15518 return;
c906108c 15519
c906108c
SS
15520 /* Look up member function name in fieldlist. */
15521 for (i = 0; i < fip->nfnfields; i++)
15522 {
27bfe10e 15523 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
15524 break;
15525 }
15526
15527 /* Create new list element if necessary. */
15528 if (i < fip->nfnfields)
15529 flp = &fip->fnfieldlists[i];
15530 else
15531 {
15532 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
15533 {
15534 fip->fnfieldlists = (struct fnfieldlist *)
15535 xrealloc (fip->fnfieldlists,
15536 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 15537 * sizeof (struct fnfieldlist));
c906108c 15538 if (fip->nfnfields == 0)
c13c43fd 15539 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
15540 }
15541 flp = &fip->fnfieldlists[fip->nfnfields];
15542 flp->name = fieldname;
15543 flp->length = 0;
15544 flp->head = NULL;
3da10d80 15545 i = fip->nfnfields++;
c906108c
SS
15546 }
15547
15548 /* Create a new member function field and chain it to the field list
0963b4bd 15549 entry. */
8d749320 15550 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 15551 make_cleanup (xfree, new_fnfield);
c906108c
SS
15552 memset (new_fnfield, 0, sizeof (struct nextfnfield));
15553 new_fnfield->next = flp->head;
15554 flp->head = new_fnfield;
15555 flp->length++;
15556
15557 /* Fill in the member function field info. */
15558 fnp = &new_fnfield->fnfield;
3da10d80
KS
15559
15560 /* Delay processing of the physname until later. */
9c37b5ae 15561 if (cu->language == language_cplus)
3da10d80
KS
15562 {
15563 add_to_method_list (type, i, flp->length - 1, fieldname,
15564 die, cu);
15565 }
15566 else
15567 {
1d06ead6 15568 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15569 fnp->physname = physname ? physname : "";
15570 }
15571
c906108c 15572 fnp->type = alloc_type (objfile);
f792889a
DJ
15573 this_type = read_type_die (die, cu);
15574 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15575 {
f792889a 15576 int nparams = TYPE_NFIELDS (this_type);
c906108c 15577
f792889a 15578 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15579 of the method itself (TYPE_CODE_METHOD). */
15580 smash_to_method_type (fnp->type, type,
f792889a
DJ
15581 TYPE_TARGET_TYPE (this_type),
15582 TYPE_FIELDS (this_type),
15583 TYPE_NFIELDS (this_type),
15584 TYPE_VARARGS (this_type));
c906108c
SS
15585
15586 /* Handle static member functions.
c5aa993b 15587 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15588 member functions. G++ helps GDB by marking the first
15589 parameter for non-static member functions (which is the this
15590 pointer) as artificial. We obtain this information from
15591 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15592 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15593 fnp->voffset = VOFFSET_STATIC;
15594 }
15595 else
e2e0b3e5 15596 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 15597 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15598
15599 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15600 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15601 fnp->fcontext = die_containing_type (die, cu);
c906108c 15602
3e43a32a
MS
15603 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15604 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15605
15606 /* Get accessibility. */
e142c38c 15607 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15608 if (attr)
aead7601 15609 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15610 else
15611 accessibility = dwarf2_default_access_attribute (die, cu);
15612 switch (accessibility)
c906108c 15613 {
60d5a603
JK
15614 case DW_ACCESS_private:
15615 fnp->is_private = 1;
15616 break;
15617 case DW_ACCESS_protected:
15618 fnp->is_protected = 1;
15619 break;
c906108c
SS
15620 }
15621
b02dede2 15622 /* Check for artificial methods. */
e142c38c 15623 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15624 if (attr && DW_UNSND (attr) != 0)
15625 fnp->is_artificial = 1;
15626
7d27a96d
TT
15627 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15628
0d564a31 15629 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15630 function. For older versions of GCC, this is an offset in the
15631 appropriate virtual table, as specified by DW_AT_containing_type.
15632 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15633 to the object address. */
15634
e142c38c 15635 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15636 if (attr)
8e19ed76 15637 {
aec5aa8b 15638 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15639 {
aec5aa8b
TT
15640 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15641 {
15642 /* Old-style GCC. */
15643 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15644 }
15645 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15646 || (DW_BLOCK (attr)->size > 1
15647 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15648 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15649 {
aec5aa8b
TT
15650 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15651 if ((fnp->voffset % cu->header.addr_size) != 0)
15652 dwarf2_complex_location_expr_complaint ();
15653 else
15654 fnp->voffset /= cu->header.addr_size;
15655 fnp->voffset += 2;
15656 }
15657 else
15658 dwarf2_complex_location_expr_complaint ();
15659
15660 if (!fnp->fcontext)
7e993ebf
KS
15661 {
15662 /* If there is no `this' field and no DW_AT_containing_type,
15663 we cannot actually find a base class context for the
15664 vtable! */
15665 if (TYPE_NFIELDS (this_type) == 0
15666 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15667 {
15668 complaint (&symfile_complaints,
15669 _("cannot determine context for virtual member "
15670 "function \"%s\" (offset %d)"),
9c541725 15671 fieldname, to_underlying (die->sect_off));
7e993ebf
KS
15672 }
15673 else
15674 {
15675 fnp->fcontext
15676 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15677 }
15678 }
aec5aa8b 15679 }
3690dd37 15680 else if (attr_form_is_section_offset (attr))
8e19ed76 15681 {
4d3c2250 15682 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15683 }
15684 else
15685 {
4d3c2250
KB
15686 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15687 fieldname);
8e19ed76 15688 }
0d564a31 15689 }
d48cc9dd
DJ
15690 else
15691 {
15692 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15693 if (attr && DW_UNSND (attr))
15694 {
15695 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15696 complaint (&symfile_complaints,
3e43a32a
MS
15697 _("Member function \"%s\" (offset %d) is virtual "
15698 "but the vtable offset is not specified"),
9c541725 15699 fieldname, to_underlying (die->sect_off));
9655fd1a 15700 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15701 TYPE_CPLUS_DYNAMIC (type) = 1;
15702 }
15703 }
c906108c
SS
15704}
15705
15706/* Create the vector of member function fields, and attach it to the type. */
15707
15708static void
fba45db2 15709dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15710 struct dwarf2_cu *cu)
c906108c
SS
15711{
15712 struct fnfieldlist *flp;
c906108c
SS
15713 int i;
15714
b4ba55a1 15715 if (cu->language == language_ada)
a73c6dcd 15716 error (_("unexpected member functions in Ada type"));
b4ba55a1 15717
c906108c
SS
15718 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15719 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15720 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
15721
15722 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
15723 {
15724 struct nextfnfield *nfp = flp->head;
15725 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15726 int k;
15727
15728 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
15729 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
15730 fn_flp->fn_fields = (struct fn_field *)
15731 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
15732 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 15733 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
15734 }
15735
15736 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
15737}
15738
1168df01
JB
15739/* Returns non-zero if NAME is the name of a vtable member in CU's
15740 language, zero otherwise. */
15741static int
15742is_vtable_name (const char *name, struct dwarf2_cu *cu)
15743{
15744 static const char vptr[] = "_vptr";
15745
9c37b5ae
TT
15746 /* Look for the C++ form of the vtable. */
15747 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15748 return 1;
15749
15750 return 0;
15751}
15752
c0dd20ea 15753/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15754 functions, with the ABI-specified layout. If TYPE describes
15755 such a structure, smash it into a member function type.
61049d3b
DJ
15756
15757 GCC shouldn't do this; it should just output pointer to member DIEs.
15758 This is GCC PR debug/28767. */
c0dd20ea 15759
0b92b5bb
TT
15760static void
15761quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15762{
09e2d7c7 15763 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15764
15765 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15766 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15767 return;
c0dd20ea
DJ
15768
15769 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15770 if (TYPE_FIELD_NAME (type, 0) == NULL
15771 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15772 || TYPE_FIELD_NAME (type, 1) == NULL
15773 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15774 return;
c0dd20ea
DJ
15775
15776 /* Find the type of the method. */
0b92b5bb 15777 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15778 if (pfn_type == NULL
15779 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15780 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15781 return;
c0dd20ea
DJ
15782
15783 /* Look for the "this" argument. */
15784 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15785 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15786 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15787 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15788 return;
c0dd20ea 15789
09e2d7c7 15790 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15791 new_type = alloc_type (objfile);
09e2d7c7 15792 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15793 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15794 TYPE_VARARGS (pfn_type));
0b92b5bb 15795 smash_to_methodptr_type (type, new_type);
c0dd20ea 15796}
1168df01 15797
685b1105 15798
c906108c 15799/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15800 (definition) to create a type for the structure or union. Fill in
15801 the type's name and general properties; the members will not be
83655187
DE
15802 processed until process_structure_scope. A symbol table entry for
15803 the type will also not be done until process_structure_scope (assuming
15804 the type has a name).
c906108c 15805
c767944b
DJ
15806 NOTE: we need to call these functions regardless of whether or not the
15807 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15808 structure or union. This gets the type entered into our set of
83655187 15809 user defined types. */
c906108c 15810
f792889a 15811static struct type *
134d01f1 15812read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15813{
518817b3 15814 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15815 struct type *type;
15816 struct attribute *attr;
15d034d0 15817 const char *name;
c906108c 15818
348e048f
DE
15819 /* If the definition of this type lives in .debug_types, read that type.
15820 Don't follow DW_AT_specification though, that will take us back up
15821 the chain and we want to go down. */
45e58e77 15822 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
15823 if (attr)
15824 {
ac9ec31b 15825 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15826
ac9ec31b 15827 /* The type's CU may not be the same as CU.
02142a6c 15828 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15829 return set_die_type (die, type, cu);
15830 }
15831
c0dd20ea 15832 type = alloc_type (objfile);
c906108c 15833 INIT_CPLUS_SPECIFIC (type);
93311388 15834
39cbfefa
DJ
15835 name = dwarf2_name (die, cu);
15836 if (name != NULL)
c906108c 15837 {
987504bb 15838 if (cu->language == language_cplus
c44af4eb
TT
15839 || cu->language == language_d
15840 || cu->language == language_rust)
63d06c5c 15841 {
15d034d0 15842 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15843
15844 /* dwarf2_full_name might have already finished building the DIE's
15845 type. If so, there is no need to continue. */
15846 if (get_die_type (die, cu) != NULL)
15847 return get_die_type (die, cu);
15848
15849 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
15850 if (die->tag == DW_TAG_structure_type
15851 || die->tag == DW_TAG_class_type)
15852 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
15853 }
15854 else
15855 {
d8151005
DJ
15856 /* The name is already allocated along with this objfile, so
15857 we don't need to duplicate it for the type. */
7d455152 15858 TYPE_TAG_NAME (type) = name;
94af9270
KS
15859 if (die->tag == DW_TAG_class_type)
15860 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 15861 }
c906108c
SS
15862 }
15863
15864 if (die->tag == DW_TAG_structure_type)
15865 {
15866 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15867 }
15868 else if (die->tag == DW_TAG_union_type)
15869 {
15870 TYPE_CODE (type) = TYPE_CODE_UNION;
15871 }
15872 else
15873 {
4753d33b 15874 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15875 }
15876
0cc2414c
TT
15877 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15878 TYPE_DECLARED_CLASS (type) = 1;
15879
e142c38c 15880 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15881 if (attr)
15882 {
155bfbd3
JB
15883 if (attr_form_is_constant (attr))
15884 TYPE_LENGTH (type) = DW_UNSND (attr);
15885 else
15886 {
15887 /* For the moment, dynamic type sizes are not supported
15888 by GDB's struct type. The actual size is determined
15889 on-demand when resolving the type of a given object,
15890 so set the type's length to zero for now. Otherwise,
15891 we record an expression as the length, and that expression
15892 could lead to a very large value, which could eventually
15893 lead to us trying to allocate that much memory when creating
15894 a value of that type. */
15895 TYPE_LENGTH (type) = 0;
15896 }
c906108c
SS
15897 }
15898 else
15899 {
15900 TYPE_LENGTH (type) = 0;
15901 }
15902
5230b05a 15903 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15904 {
5230b05a
WT
15905 /* ICC<14 does not output the required DW_AT_declaration on
15906 incomplete types, but gives them a size of zero. */
422b1cb0 15907 TYPE_STUB (type) = 1;
685b1105
JK
15908 }
15909 else
15910 TYPE_STUB_SUPPORTED (type) = 1;
15911
dc718098 15912 if (die_is_declaration (die, cu))
876cecd0 15913 TYPE_STUB (type) = 1;
a6c727b2
DJ
15914 else if (attr == NULL && die->child == NULL
15915 && producer_is_realview (cu->producer))
15916 /* RealView does not output the required DW_AT_declaration
15917 on incomplete types. */
15918 TYPE_STUB (type) = 1;
dc718098 15919
c906108c
SS
15920 /* We need to add the type field to the die immediately so we don't
15921 infinitely recurse when dealing with pointers to the structure
0963b4bd 15922 type within the structure itself. */
1c379e20 15923 set_die_type (die, type, cu);
c906108c 15924
7e314c57
JK
15925 /* set_die_type should be already done. */
15926 set_descriptive_type (type, die, cu);
15927
c767944b
DJ
15928 return type;
15929}
15930
15931/* Finish creating a structure or union type, including filling in
15932 its members and creating a symbol for it. */
15933
15934static void
15935process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15936{
518817b3 15937 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15938 struct die_info *child_die;
c767944b
DJ
15939 struct type *type;
15940
15941 type = get_die_type (die, cu);
15942 if (type == NULL)
15943 type = read_structure_type (die, cu);
15944
e142c38c 15945 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15946 {
15947 struct field_info fi;
2f4732b0 15948 std::vector<struct symbol *> template_args;
c767944b 15949 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
15950
15951 memset (&fi, 0, sizeof (struct field_info));
15952
639d11d3 15953 child_die = die->child;
c906108c
SS
15954
15955 while (child_die && child_die->tag)
15956 {
a9a9bd0f
DC
15957 if (child_die->tag == DW_TAG_member
15958 || child_die->tag == DW_TAG_variable)
c906108c 15959 {
a9a9bd0f
DC
15960 /* NOTE: carlton/2002-11-05: A C++ static data member
15961 should be a DW_TAG_member that is a declaration, but
15962 all versions of G++ as of this writing (so through at
15963 least 3.2.1) incorrectly generate DW_TAG_variable
15964 tags for them instead. */
e7c27a73 15965 dwarf2_add_field (&fi, child_die, cu);
c906108c 15966 }
8713b1b1 15967 else if (child_die->tag == DW_TAG_subprogram)
c906108c 15968 {
e98c9e7c
TT
15969 /* Rust doesn't have member functions in the C++ sense.
15970 However, it does emit ordinary functions as children
15971 of a struct DIE. */
15972 if (cu->language == language_rust)
15973 read_func_scope (child_die, cu);
15974 else
15975 {
15976 /* C++ member function. */
15977 dwarf2_add_member_fn (&fi, child_die, type, cu);
15978 }
c906108c
SS
15979 }
15980 else if (child_die->tag == DW_TAG_inheritance)
15981 {
15982 /* C++ base class field. */
e7c27a73 15983 dwarf2_add_field (&fi, child_die, cu);
c906108c 15984 }
883fd55a
KS
15985 else if (type_can_define_types (child_die))
15986 dwarf2_add_type_defn (&fi, child_die, cu);
34eaf542
TT
15987 else if (child_die->tag == DW_TAG_template_type_param
15988 || child_die->tag == DW_TAG_template_value_param)
15989 {
15990 struct symbol *arg = new_symbol (child_die, NULL, cu);
15991
f1078f66 15992 if (arg != NULL)
2f4732b0 15993 template_args.push_back (arg);
34eaf542
TT
15994 }
15995
c906108c
SS
15996 child_die = sibling_die (child_die);
15997 }
15998
34eaf542 15999 /* Attach template arguments to type. */
2f4732b0 16000 if (!template_args.empty ())
34eaf542
TT
16001 {
16002 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16003 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16004 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16005 = XOBNEWVEC (&objfile->objfile_obstack,
16006 struct symbol *,
16007 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16008 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16009 template_args.data (),
34eaf542
TT
16010 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16011 * sizeof (struct symbol *)));
34eaf542
TT
16012 }
16013
c906108c
SS
16014 /* Attach fields and member functions to the type. */
16015 if (fi.nfields)
e7c27a73 16016 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
16017 if (fi.nfnfields)
16018 {
e7c27a73 16019 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16020
c5aa993b 16021 /* Get the type which refers to the base class (possibly this
c906108c 16022 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16023 class from the DW_AT_containing_type attribute. This use of
16024 DW_AT_containing_type is a GNU extension. */
c906108c 16025
e142c38c 16026 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16027 {
e7c27a73 16028 struct type *t = die_containing_type (die, cu);
c906108c 16029
ae6ae975 16030 set_type_vptr_basetype (type, t);
c906108c
SS
16031 if (type == t)
16032 {
c906108c
SS
16033 int i;
16034
16035 /* Our own class provides vtbl ptr. */
16036 for (i = TYPE_NFIELDS (t) - 1;
16037 i >= TYPE_N_BASECLASSES (t);
16038 --i)
16039 {
0d5cff50 16040 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16041
1168df01 16042 if (is_vtable_name (fieldname, cu))
c906108c 16043 {
ae6ae975 16044 set_type_vptr_fieldno (type, i);
c906108c
SS
16045 break;
16046 }
16047 }
16048
16049 /* Complain if virtual function table field not found. */
16050 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 16051 complaint (&symfile_complaints,
3e43a32a
MS
16052 _("virtual function table pointer "
16053 "not found when defining class '%s'"),
4d3c2250
KB
16054 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16055 "");
c906108c
SS
16056 }
16057 else
16058 {
ae6ae975 16059 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16060 }
16061 }
f6235d4c 16062 else if (cu->producer
61012eef 16063 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16064 {
16065 /* The IBM XLC compiler does not provide direct indication
16066 of the containing type, but the vtable pointer is
16067 always named __vfp. */
16068
16069 int i;
16070
16071 for (i = TYPE_NFIELDS (type) - 1;
16072 i >= TYPE_N_BASECLASSES (type);
16073 --i)
16074 {
16075 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16076 {
ae6ae975
DE
16077 set_type_vptr_fieldno (type, i);
16078 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16079 break;
16080 }
16081 }
16082 }
c906108c 16083 }
98751a41
JK
16084
16085 /* Copy fi.typedef_field_list linked list elements content into the
16086 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16087 if (fi.typedef_field_list)
16088 {
16089 int i = fi.typedef_field_list_count;
16090
a0d7a4ff 16091 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16092 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16093 = ((struct decl_field *)
224c3ddb 16094 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
16095 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
16096
16097 /* Reverse the list order to keep the debug info elements order. */
16098 while (--i >= 0)
16099 {
883fd55a 16100 struct decl_field *dest, *src;
6e70227d 16101
98751a41
JK
16102 dest = &TYPE_TYPEDEF_FIELD (type, i);
16103 src = &fi.typedef_field_list->field;
16104 fi.typedef_field_list = fi.typedef_field_list->next;
16105 *dest = *src;
16106 }
16107 }
c767944b 16108
883fd55a
KS
16109 /* Copy fi.nested_types_list linked list elements content into the
16110 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16111 if (fi.nested_types_list != NULL && cu->language != language_ada)
16112 {
16113 int i = fi.nested_types_list_count;
16114
16115 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16116 TYPE_NESTED_TYPES_ARRAY (type)
16117 = ((struct decl_field *)
16118 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
16119 TYPE_NESTED_TYPES_COUNT (type) = i;
16120
16121 /* Reverse the list order to keep the debug info elements order. */
16122 while (--i >= 0)
16123 {
16124 struct decl_field *dest, *src;
16125
16126 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
16127 src = &fi.nested_types_list->field;
16128 fi.nested_types_list = fi.nested_types_list->next;
16129 *dest = *src;
16130 }
16131 }
16132
c767944b 16133 do_cleanups (back_to);
c906108c 16134 }
63d06c5c 16135
bb5ed363 16136 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 16137
90aeadfc
DC
16138 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16139 snapshots) has been known to create a die giving a declaration
16140 for a class that has, as a child, a die giving a definition for a
16141 nested class. So we have to process our children even if the
16142 current die is a declaration. Normally, of course, a declaration
16143 won't have any children at all. */
134d01f1 16144
ca040673
DE
16145 child_die = die->child;
16146
90aeadfc
DC
16147 while (child_die != NULL && child_die->tag)
16148 {
16149 if (child_die->tag == DW_TAG_member
16150 || child_die->tag == DW_TAG_variable
34eaf542
TT
16151 || child_die->tag == DW_TAG_inheritance
16152 || child_die->tag == DW_TAG_template_value_param
16153 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16154 {
90aeadfc 16155 /* Do nothing. */
134d01f1 16156 }
90aeadfc
DC
16157 else
16158 process_die (child_die, cu);
134d01f1 16159
90aeadfc 16160 child_die = sibling_die (child_die);
134d01f1
DJ
16161 }
16162
fa4028e9
JB
16163 /* Do not consider external references. According to the DWARF standard,
16164 these DIEs are identified by the fact that they have no byte_size
16165 attribute, and a declaration attribute. */
16166 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16167 || !die_is_declaration (die, cu))
c767944b 16168 new_symbol (die, type, cu);
134d01f1
DJ
16169}
16170
55426c9d
JB
16171/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16172 update TYPE using some information only available in DIE's children. */
16173
16174static void
16175update_enumeration_type_from_children (struct die_info *die,
16176 struct type *type,
16177 struct dwarf2_cu *cu)
16178{
60f7655a 16179 struct die_info *child_die;
55426c9d
JB
16180 int unsigned_enum = 1;
16181 int flag_enum = 1;
16182 ULONGEST mask = 0;
55426c9d 16183
8268c778 16184 auto_obstack obstack;
55426c9d 16185
60f7655a
DE
16186 for (child_die = die->child;
16187 child_die != NULL && child_die->tag;
16188 child_die = sibling_die (child_die))
55426c9d
JB
16189 {
16190 struct attribute *attr;
16191 LONGEST value;
16192 const gdb_byte *bytes;
16193 struct dwarf2_locexpr_baton *baton;
16194 const char *name;
60f7655a 16195
55426c9d
JB
16196 if (child_die->tag != DW_TAG_enumerator)
16197 continue;
16198
16199 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16200 if (attr == NULL)
16201 continue;
16202
16203 name = dwarf2_name (child_die, cu);
16204 if (name == NULL)
16205 name = "<anonymous enumerator>";
16206
16207 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16208 &value, &bytes, &baton);
16209 if (value < 0)
16210 {
16211 unsigned_enum = 0;
16212 flag_enum = 0;
16213 }
16214 else if ((mask & value) != 0)
16215 flag_enum = 0;
16216 else
16217 mask |= value;
16218
16219 /* If we already know that the enum type is neither unsigned, nor
16220 a flag type, no need to look at the rest of the enumerates. */
16221 if (!unsigned_enum && !flag_enum)
16222 break;
55426c9d
JB
16223 }
16224
16225 if (unsigned_enum)
16226 TYPE_UNSIGNED (type) = 1;
16227 if (flag_enum)
16228 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16229}
16230
134d01f1
DJ
16231/* Given a DW_AT_enumeration_type die, set its type. We do not
16232 complete the type's fields yet, or create any symbols. */
c906108c 16233
f792889a 16234static struct type *
134d01f1 16235read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16236{
518817b3 16237 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16238 struct type *type;
c906108c 16239 struct attribute *attr;
0114d602 16240 const char *name;
134d01f1 16241
348e048f
DE
16242 /* If the definition of this type lives in .debug_types, read that type.
16243 Don't follow DW_AT_specification though, that will take us back up
16244 the chain and we want to go down. */
45e58e77 16245 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16246 if (attr)
16247 {
ac9ec31b 16248 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16249
ac9ec31b 16250 /* The type's CU may not be the same as CU.
02142a6c 16251 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16252 return set_die_type (die, type, cu);
16253 }
16254
c906108c
SS
16255 type = alloc_type (objfile);
16256
16257 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16258 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16259 if (name != NULL)
7d455152 16260 TYPE_TAG_NAME (type) = name;
c906108c 16261
0626fc76
TT
16262 attr = dwarf2_attr (die, DW_AT_type, cu);
16263 if (attr != NULL)
16264 {
16265 struct type *underlying_type = die_type (die, cu);
16266
16267 TYPE_TARGET_TYPE (type) = underlying_type;
16268 }
16269
e142c38c 16270 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16271 if (attr)
16272 {
16273 TYPE_LENGTH (type) = DW_UNSND (attr);
16274 }
16275 else
16276 {
16277 TYPE_LENGTH (type) = 0;
16278 }
16279
137033e9
JB
16280 /* The enumeration DIE can be incomplete. In Ada, any type can be
16281 declared as private in the package spec, and then defined only
16282 inside the package body. Such types are known as Taft Amendment
16283 Types. When another package uses such a type, an incomplete DIE
16284 may be generated by the compiler. */
02eb380e 16285 if (die_is_declaration (die, cu))
876cecd0 16286 TYPE_STUB (type) = 1;
02eb380e 16287
0626fc76
TT
16288 /* Finish the creation of this type by using the enum's children.
16289 We must call this even when the underlying type has been provided
16290 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16291 update_enumeration_type_from_children (die, type, cu);
16292
0626fc76
TT
16293 /* If this type has an underlying type that is not a stub, then we
16294 may use its attributes. We always use the "unsigned" attribute
16295 in this situation, because ordinarily we guess whether the type
16296 is unsigned -- but the guess can be wrong and the underlying type
16297 can tell us the reality. However, we defer to a local size
16298 attribute if one exists, because this lets the compiler override
16299 the underlying type if needed. */
16300 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16301 {
16302 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16303 if (TYPE_LENGTH (type) == 0)
16304 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16305 }
16306
3d567982
TT
16307 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16308
f792889a 16309 return set_die_type (die, type, cu);
134d01f1
DJ
16310}
16311
16312/* Given a pointer to a die which begins an enumeration, process all
16313 the dies that define the members of the enumeration, and create the
16314 symbol for the enumeration type.
16315
16316 NOTE: We reverse the order of the element list. */
16317
16318static void
16319process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16320{
f792889a 16321 struct type *this_type;
134d01f1 16322
f792889a
DJ
16323 this_type = get_die_type (die, cu);
16324 if (this_type == NULL)
16325 this_type = read_enumeration_type (die, cu);
9dc481d3 16326
639d11d3 16327 if (die->child != NULL)
c906108c 16328 {
9dc481d3
DE
16329 struct die_info *child_die;
16330 struct symbol *sym;
16331 struct field *fields = NULL;
16332 int num_fields = 0;
15d034d0 16333 const char *name;
9dc481d3 16334
639d11d3 16335 child_die = die->child;
c906108c
SS
16336 while (child_die && child_die->tag)
16337 {
16338 if (child_die->tag != DW_TAG_enumerator)
16339 {
e7c27a73 16340 process_die (child_die, cu);
c906108c
SS
16341 }
16342 else
16343 {
39cbfefa
DJ
16344 name = dwarf2_name (child_die, cu);
16345 if (name)
c906108c 16346 {
f792889a 16347 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16348
16349 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16350 {
16351 fields = (struct field *)
16352 xrealloc (fields,
16353 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16354 * sizeof (struct field));
c906108c
SS
16355 }
16356
3567439c 16357 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16358 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16359 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16360 FIELD_BITSIZE (fields[num_fields]) = 0;
16361
16362 num_fields++;
16363 }
16364 }
16365
16366 child_die = sibling_die (child_die);
16367 }
16368
16369 if (num_fields)
16370 {
f792889a
DJ
16371 TYPE_NFIELDS (this_type) = num_fields;
16372 TYPE_FIELDS (this_type) = (struct field *)
16373 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16374 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16375 sizeof (struct field) * num_fields);
b8c9b27d 16376 xfree (fields);
c906108c 16377 }
c906108c 16378 }
134d01f1 16379
6c83ed52
TT
16380 /* If we are reading an enum from a .debug_types unit, and the enum
16381 is a declaration, and the enum is not the signatured type in the
16382 unit, then we do not want to add a symbol for it. Adding a
16383 symbol would in some cases obscure the true definition of the
16384 enum, giving users an incomplete type when the definition is
16385 actually available. Note that we do not want to do this for all
16386 enums which are just declarations, because C++0x allows forward
16387 enum declarations. */
3019eac3 16388 if (cu->per_cu->is_debug_types
6c83ed52
TT
16389 && die_is_declaration (die, cu))
16390 {
52dc124a 16391 struct signatured_type *sig_type;
6c83ed52 16392
c0f78cd4 16393 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16394 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16395 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16396 return;
16397 }
16398
f792889a 16399 new_symbol (die, this_type, cu);
c906108c
SS
16400}
16401
16402/* Extract all information from a DW_TAG_array_type DIE and put it in
16403 the DIE's type field. For now, this only handles one dimensional
16404 arrays. */
16405
f792889a 16406static struct type *
e7c27a73 16407read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16408{
518817b3 16409 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16410 struct die_info *child_die;
7e314c57 16411 struct type *type;
c906108c 16412 struct type *element_type, *range_type, *index_type;
c906108c 16413 struct attribute *attr;
15d034d0 16414 const char *name;
a405673c 16415 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16416 unsigned int bit_stride = 0;
c906108c 16417
e7c27a73 16418 element_type = die_type (die, cu);
c906108c 16419
7e314c57
JK
16420 /* The die_type call above may have already set the type for this DIE. */
16421 type = get_die_type (die, cu);
16422 if (type)
16423 return type;
16424
dc53a7ad
JB
16425 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16426 if (attr != NULL)
a405673c
JB
16427 {
16428 int stride_ok;
16429
16430 byte_stride_prop
16431 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16432 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16433 if (!stride_ok)
16434 {
16435 complaint (&symfile_complaints,
16436 _("unable to read array DW_AT_byte_stride "
16437 " - DIE at 0x%x [in module %s]"),
16438 to_underlying (die->sect_off),
518817b3 16439 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16440 /* Ignore this attribute. We will likely not be able to print
16441 arrays of this type correctly, but there is little we can do
16442 to help if we cannot read the attribute's value. */
16443 byte_stride_prop = NULL;
16444 }
16445 }
dc53a7ad
JB
16446
16447 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16448 if (attr != NULL)
16449 bit_stride = DW_UNSND (attr);
16450
c906108c
SS
16451 /* Irix 6.2 native cc creates array types without children for
16452 arrays with unspecified length. */
639d11d3 16453 if (die->child == NULL)
c906108c 16454 {
46bf5051 16455 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16456 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16457 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16458 byte_stride_prop, bit_stride);
f792889a 16459 return set_die_type (die, type, cu);
c906108c
SS
16460 }
16461
791afaa2 16462 std::vector<struct type *> range_types;
639d11d3 16463 child_die = die->child;
c906108c
SS
16464 while (child_die && child_die->tag)
16465 {
16466 if (child_die->tag == DW_TAG_subrange_type)
16467 {
f792889a 16468 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16469
f792889a 16470 if (child_type != NULL)
a02abb62 16471 {
0963b4bd
MS
16472 /* The range type was succesfully read. Save it for the
16473 array type creation. */
791afaa2 16474 range_types.push_back (child_type);
a02abb62 16475 }
c906108c
SS
16476 }
16477 child_die = sibling_die (child_die);
16478 }
16479
16480 /* Dwarf2 dimensions are output from left to right, create the
16481 necessary array types in backwards order. */
7ca2d3a3 16482
c906108c 16483 type = element_type;
7ca2d3a3
DL
16484
16485 if (read_array_order (die, cu) == DW_ORD_col_major)
16486 {
16487 int i = 0;
9a619af0 16488
791afaa2 16489 while (i < range_types.size ())
dc53a7ad 16490 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16491 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16492 }
16493 else
16494 {
791afaa2 16495 size_t ndim = range_types.size ();
7ca2d3a3 16496 while (ndim-- > 0)
dc53a7ad 16497 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16498 byte_stride_prop, bit_stride);
7ca2d3a3 16499 }
c906108c 16500
f5f8a009
EZ
16501 /* Understand Dwarf2 support for vector types (like they occur on
16502 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16503 array type. This is not part of the Dwarf2/3 standard yet, but a
16504 custom vendor extension. The main difference between a regular
16505 array and the vector variant is that vectors are passed by value
16506 to functions. */
e142c38c 16507 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16508 if (attr)
ea37ba09 16509 make_vector_type (type);
f5f8a009 16510
dbc98a8b
KW
16511 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16512 implementation may choose to implement triple vectors using this
16513 attribute. */
16514 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16515 if (attr)
16516 {
16517 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16518 TYPE_LENGTH (type) = DW_UNSND (attr);
16519 else
3e43a32a
MS
16520 complaint (&symfile_complaints,
16521 _("DW_AT_byte_size for array type smaller "
16522 "than the total size of elements"));
dbc98a8b
KW
16523 }
16524
39cbfefa
DJ
16525 name = dwarf2_name (die, cu);
16526 if (name)
16527 TYPE_NAME (type) = name;
6e70227d 16528
0963b4bd 16529 /* Install the type in the die. */
7e314c57
JK
16530 set_die_type (die, type, cu);
16531
16532 /* set_die_type should be already done. */
b4ba55a1
JB
16533 set_descriptive_type (type, die, cu);
16534
7e314c57 16535 return type;
c906108c
SS
16536}
16537
7ca2d3a3 16538static enum dwarf_array_dim_ordering
6e70227d 16539read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16540{
16541 struct attribute *attr;
16542
16543 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16544
aead7601
SM
16545 if (attr)
16546 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16547
0963b4bd
MS
16548 /* GNU F77 is a special case, as at 08/2004 array type info is the
16549 opposite order to the dwarf2 specification, but data is still
16550 laid out as per normal fortran.
7ca2d3a3 16551
0963b4bd
MS
16552 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16553 version checking. */
7ca2d3a3 16554
905e0470
PM
16555 if (cu->language == language_fortran
16556 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16557 {
16558 return DW_ORD_row_major;
16559 }
16560
6e70227d 16561 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16562 {
16563 case array_column_major:
16564 return DW_ORD_col_major;
16565 case array_row_major:
16566 default:
16567 return DW_ORD_row_major;
16568 };
16569}
16570
72019c9c 16571/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16572 the DIE's type field. */
72019c9c 16573
f792889a 16574static struct type *
72019c9c
GM
16575read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16576{
7e314c57
JK
16577 struct type *domain_type, *set_type;
16578 struct attribute *attr;
f792889a 16579
7e314c57
JK
16580 domain_type = die_type (die, cu);
16581
16582 /* The die_type call above may have already set the type for this DIE. */
16583 set_type = get_die_type (die, cu);
16584 if (set_type)
16585 return set_type;
16586
16587 set_type = create_set_type (NULL, domain_type);
16588
16589 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16590 if (attr)
16591 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16592
f792889a 16593 return set_die_type (die, set_type, cu);
72019c9c 16594}
7ca2d3a3 16595
0971de02
TT
16596/* A helper for read_common_block that creates a locexpr baton.
16597 SYM is the symbol which we are marking as computed.
16598 COMMON_DIE is the DIE for the common block.
16599 COMMON_LOC is the location expression attribute for the common
16600 block itself.
16601 MEMBER_LOC is the location expression attribute for the particular
16602 member of the common block that we are processing.
16603 CU is the CU from which the above come. */
16604
16605static void
16606mark_common_block_symbol_computed (struct symbol *sym,
16607 struct die_info *common_die,
16608 struct attribute *common_loc,
16609 struct attribute *member_loc,
16610 struct dwarf2_cu *cu)
16611{
518817b3
SM
16612 struct dwarf2_per_objfile *dwarf2_per_objfile
16613 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16614 struct objfile *objfile = dwarf2_per_objfile->objfile;
16615 struct dwarf2_locexpr_baton *baton;
16616 gdb_byte *ptr;
16617 unsigned int cu_off;
16618 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16619 LONGEST offset = 0;
16620
16621 gdb_assert (common_loc && member_loc);
16622 gdb_assert (attr_form_is_block (common_loc));
16623 gdb_assert (attr_form_is_block (member_loc)
16624 || attr_form_is_constant (member_loc));
16625
8d749320 16626 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16627 baton->per_cu = cu->per_cu;
16628 gdb_assert (baton->per_cu);
16629
16630 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16631
16632 if (attr_form_is_constant (member_loc))
16633 {
16634 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16635 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16636 }
16637 else
16638 baton->size += DW_BLOCK (member_loc)->size;
16639
224c3ddb 16640 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16641 baton->data = ptr;
16642
16643 *ptr++ = DW_OP_call4;
9c541725 16644 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16645 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16646 ptr += 4;
16647
16648 if (attr_form_is_constant (member_loc))
16649 {
16650 *ptr++ = DW_OP_addr;
16651 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16652 ptr += cu->header.addr_size;
16653 }
16654 else
16655 {
16656 /* We have to copy the data here, because DW_OP_call4 will only
16657 use a DW_AT_location attribute. */
16658 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16659 ptr += DW_BLOCK (member_loc)->size;
16660 }
16661
16662 *ptr++ = DW_OP_plus;
16663 gdb_assert (ptr - baton->data == baton->size);
16664
0971de02 16665 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16666 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16667}
16668
4357ac6c
TT
16669/* Create appropriate locally-scoped variables for all the
16670 DW_TAG_common_block entries. Also create a struct common_block
16671 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16672 is used to sepate the common blocks name namespace from regular
16673 variable names. */
c906108c
SS
16674
16675static void
e7c27a73 16676read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16677{
0971de02
TT
16678 struct attribute *attr;
16679
16680 attr = dwarf2_attr (die, DW_AT_location, cu);
16681 if (attr)
16682 {
16683 /* Support the .debug_loc offsets. */
16684 if (attr_form_is_block (attr))
16685 {
16686 /* Ok. */
16687 }
16688 else if (attr_form_is_section_offset (attr))
16689 {
16690 dwarf2_complex_location_expr_complaint ();
16691 attr = NULL;
16692 }
16693 else
16694 {
16695 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16696 "common block member");
16697 attr = NULL;
16698 }
16699 }
16700
639d11d3 16701 if (die->child != NULL)
c906108c 16702 {
518817b3 16703 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16704 struct die_info *child_die;
16705 size_t n_entries = 0, size;
16706 struct common_block *common_block;
16707 struct symbol *sym;
74ac6d43 16708
4357ac6c
TT
16709 for (child_die = die->child;
16710 child_die && child_die->tag;
16711 child_die = sibling_die (child_die))
16712 ++n_entries;
16713
16714 size = (sizeof (struct common_block)
16715 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16716 common_block
16717 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16718 size);
4357ac6c
TT
16719 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16720 common_block->n_entries = 0;
16721
16722 for (child_die = die->child;
16723 child_die && child_die->tag;
16724 child_die = sibling_die (child_die))
16725 {
16726 /* Create the symbol in the DW_TAG_common_block block in the current
16727 symbol scope. */
e7c27a73 16728 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16729 if (sym != NULL)
16730 {
16731 struct attribute *member_loc;
16732
16733 common_block->contents[common_block->n_entries++] = sym;
16734
16735 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16736 cu);
16737 if (member_loc)
16738 {
16739 /* GDB has handled this for a long time, but it is
16740 not specified by DWARF. It seems to have been
16741 emitted by gfortran at least as recently as:
16742 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16743 complaint (&symfile_complaints,
16744 _("Variable in common block has "
16745 "DW_AT_data_member_location "
16746 "- DIE at 0x%x [in module %s]"),
9c541725 16747 to_underlying (child_die->sect_off),
518817b3 16748 objfile_name (objfile));
0971de02
TT
16749
16750 if (attr_form_is_section_offset (member_loc))
16751 dwarf2_complex_location_expr_complaint ();
16752 else if (attr_form_is_constant (member_loc)
16753 || attr_form_is_block (member_loc))
16754 {
16755 if (attr)
16756 mark_common_block_symbol_computed (sym, die, attr,
16757 member_loc, cu);
16758 }
16759 else
16760 dwarf2_complex_location_expr_complaint ();
16761 }
16762 }
c906108c 16763 }
4357ac6c
TT
16764
16765 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16766 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16767 }
16768}
16769
0114d602 16770/* Create a type for a C++ namespace. */
d9fa45fe 16771
0114d602
DJ
16772static struct type *
16773read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16774{
518817b3 16775 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16776 const char *previous_prefix, *name;
9219021c 16777 int is_anonymous;
0114d602
DJ
16778 struct type *type;
16779
16780 /* For extensions, reuse the type of the original namespace. */
16781 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16782 {
16783 struct die_info *ext_die;
16784 struct dwarf2_cu *ext_cu = cu;
9a619af0 16785
0114d602
DJ
16786 ext_die = dwarf2_extension (die, &ext_cu);
16787 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16788
16789 /* EXT_CU may not be the same as CU.
02142a6c 16790 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16791 return set_die_type (die, type, cu);
16792 }
9219021c 16793
e142c38c 16794 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16795
16796 /* Now build the name of the current namespace. */
16797
0114d602
DJ
16798 previous_prefix = determine_prefix (die, cu);
16799 if (previous_prefix[0] != '\0')
16800 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16801 previous_prefix, name, 0, cu);
0114d602
DJ
16802
16803 /* Create the type. */
19f392bc 16804 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
16805 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16806
60531b24 16807 return set_die_type (die, type, cu);
0114d602
DJ
16808}
16809
22cee43f 16810/* Read a namespace scope. */
0114d602
DJ
16811
16812static void
16813read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16814{
518817b3 16815 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16816 int is_anonymous;
9219021c 16817
5c4e30ca
DC
16818 /* Add a symbol associated to this if we haven't seen the namespace
16819 before. Also, add a using directive if it's an anonymous
16820 namespace. */
9219021c 16821
f2f0e013 16822 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16823 {
16824 struct type *type;
16825
0114d602 16826 type = read_type_die (die, cu);
e7c27a73 16827 new_symbol (die, type, cu);
5c4e30ca 16828
e8e80198 16829 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16830 if (is_anonymous)
0114d602
DJ
16831 {
16832 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16833
eb1e02fd 16834 std::vector<const char *> excludes;
22cee43f
PMR
16835 add_using_directive (using_directives (cu->language),
16836 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16837 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16838 }
5c4e30ca 16839 }
9219021c 16840
639d11d3 16841 if (die->child != NULL)
d9fa45fe 16842 {
639d11d3 16843 struct die_info *child_die = die->child;
6e70227d 16844
d9fa45fe
DC
16845 while (child_die && child_die->tag)
16846 {
e7c27a73 16847 process_die (child_die, cu);
d9fa45fe
DC
16848 child_die = sibling_die (child_die);
16849 }
16850 }
38d518c9
EZ
16851}
16852
f55ee35c
JK
16853/* Read a Fortran module as type. This DIE can be only a declaration used for
16854 imported module. Still we need that type as local Fortran "use ... only"
16855 declaration imports depend on the created type in determine_prefix. */
16856
16857static struct type *
16858read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16859{
518817b3 16860 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16861 const char *module_name;
f55ee35c
JK
16862 struct type *type;
16863
16864 module_name = dwarf2_name (die, cu);
16865 if (!module_name)
3e43a32a
MS
16866 complaint (&symfile_complaints,
16867 _("DW_TAG_module has no name, offset 0x%x"),
9c541725 16868 to_underlying (die->sect_off));
19f392bc 16869 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
16870
16871 /* determine_prefix uses TYPE_TAG_NAME. */
16872 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16873
16874 return set_die_type (die, type, cu);
16875}
16876
5d7cb8df
JK
16877/* Read a Fortran module. */
16878
16879static void
16880read_module (struct die_info *die, struct dwarf2_cu *cu)
16881{
16882 struct die_info *child_die = die->child;
530e8392
KB
16883 struct type *type;
16884
16885 type = read_type_die (die, cu);
16886 new_symbol (die, type, cu);
5d7cb8df 16887
5d7cb8df
JK
16888 while (child_die && child_die->tag)
16889 {
16890 process_die (child_die, cu);
16891 child_die = sibling_die (child_die);
16892 }
16893}
16894
38d518c9
EZ
16895/* Return the name of the namespace represented by DIE. Set
16896 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16897 namespace. */
16898
16899static const char *
e142c38c 16900namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16901{
16902 struct die_info *current_die;
16903 const char *name = NULL;
16904
16905 /* Loop through the extensions until we find a name. */
16906
16907 for (current_die = die;
16908 current_die != NULL;
f2f0e013 16909 current_die = dwarf2_extension (die, &cu))
38d518c9 16910 {
96553a0c
DE
16911 /* We don't use dwarf2_name here so that we can detect the absence
16912 of a name -> anonymous namespace. */
7d45c7c3 16913 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16914
38d518c9
EZ
16915 if (name != NULL)
16916 break;
16917 }
16918
16919 /* Is it an anonymous namespace? */
16920
16921 *is_anonymous = (name == NULL);
16922 if (*is_anonymous)
2b1dbab0 16923 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16924
16925 return name;
d9fa45fe
DC
16926}
16927
c906108c
SS
16928/* Extract all information from a DW_TAG_pointer_type DIE and add to
16929 the user defined type vector. */
16930
f792889a 16931static struct type *
e7c27a73 16932read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16933{
518817b3
SM
16934 struct gdbarch *gdbarch
16935 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16936 struct comp_unit_head *cu_header = &cu->header;
c906108c 16937 struct type *type;
8b2dbe47
KB
16938 struct attribute *attr_byte_size;
16939 struct attribute *attr_address_class;
16940 int byte_size, addr_class;
7e314c57
JK
16941 struct type *target_type;
16942
16943 target_type = die_type (die, cu);
c906108c 16944
7e314c57
JK
16945 /* The die_type call above may have already set the type for this DIE. */
16946 type = get_die_type (die, cu);
16947 if (type)
16948 return type;
16949
16950 type = lookup_pointer_type (target_type);
8b2dbe47 16951
e142c38c 16952 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16953 if (attr_byte_size)
16954 byte_size = DW_UNSND (attr_byte_size);
c906108c 16955 else
8b2dbe47
KB
16956 byte_size = cu_header->addr_size;
16957
e142c38c 16958 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16959 if (attr_address_class)
16960 addr_class = DW_UNSND (attr_address_class);
16961 else
16962 addr_class = DW_ADDR_none;
16963
16964 /* If the pointer size or address class is different than the
16965 default, create a type variant marked as such and set the
16966 length accordingly. */
16967 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 16968 {
5e2b427d 16969 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16970 {
16971 int type_flags;
16972
849957d9 16973 type_flags = gdbarch_address_class_type_flags
5e2b427d 16974 (gdbarch, byte_size, addr_class);
876cecd0
TT
16975 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16976 == 0);
8b2dbe47
KB
16977 type = make_type_with_address_space (type, type_flags);
16978 }
16979 else if (TYPE_LENGTH (type) != byte_size)
16980 {
3e43a32a
MS
16981 complaint (&symfile_complaints,
16982 _("invalid pointer size %d"), byte_size);
8b2dbe47 16983 }
6e70227d 16984 else
9a619af0
MS
16985 {
16986 /* Should we also complain about unhandled address classes? */
16987 }
c906108c 16988 }
8b2dbe47
KB
16989
16990 TYPE_LENGTH (type) = byte_size;
f792889a 16991 return set_die_type (die, type, cu);
c906108c
SS
16992}
16993
16994/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16995 the user defined type vector. */
16996
f792889a 16997static struct type *
e7c27a73 16998read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16999{
17000 struct type *type;
17001 struct type *to_type;
17002 struct type *domain;
17003
e7c27a73
DJ
17004 to_type = die_type (die, cu);
17005 domain = die_containing_type (die, cu);
0d5de010 17006
7e314c57
JK
17007 /* The calls above may have already set the type for this DIE. */
17008 type = get_die_type (die, cu);
17009 if (type)
17010 return type;
17011
0d5de010
DJ
17012 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17013 type = lookup_methodptr_type (to_type);
7078baeb
TT
17014 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17015 {
518817b3
SM
17016 struct type *new_type
17017 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17018
17019 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17020 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17021 TYPE_VARARGS (to_type));
17022 type = lookup_methodptr_type (new_type);
17023 }
0d5de010
DJ
17024 else
17025 type = lookup_memberptr_type (to_type, domain);
c906108c 17026
f792889a 17027 return set_die_type (die, type, cu);
c906108c
SS
17028}
17029
4297a3f0 17030/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17031 the user defined type vector. */
17032
f792889a 17033static struct type *
4297a3f0
AV
17034read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17035 enum type_code refcode)
c906108c 17036{
e7c27a73 17037 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17038 struct type *type, *target_type;
c906108c
SS
17039 struct attribute *attr;
17040
4297a3f0
AV
17041 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17042
7e314c57
JK
17043 target_type = die_type (die, cu);
17044
17045 /* The die_type call above may have already set the type for this DIE. */
17046 type = get_die_type (die, cu);
17047 if (type)
17048 return type;
17049
4297a3f0 17050 type = lookup_reference_type (target_type, refcode);
e142c38c 17051 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17052 if (attr)
17053 {
17054 TYPE_LENGTH (type) = DW_UNSND (attr);
17055 }
17056 else
17057 {
107d2387 17058 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17059 }
f792889a 17060 return set_die_type (die, type, cu);
c906108c
SS
17061}
17062
cf363f18
MW
17063/* Add the given cv-qualifiers to the element type of the array. GCC
17064 outputs DWARF type qualifiers that apply to an array, not the
17065 element type. But GDB relies on the array element type to carry
17066 the cv-qualifiers. This mimics section 6.7.3 of the C99
17067 specification. */
17068
17069static struct type *
17070add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17071 struct type *base_type, int cnst, int voltl)
17072{
17073 struct type *el_type, *inner_array;
17074
17075 base_type = copy_type (base_type);
17076 inner_array = base_type;
17077
17078 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17079 {
17080 TYPE_TARGET_TYPE (inner_array) =
17081 copy_type (TYPE_TARGET_TYPE (inner_array));
17082 inner_array = TYPE_TARGET_TYPE (inner_array);
17083 }
17084
17085 el_type = TYPE_TARGET_TYPE (inner_array);
17086 cnst |= TYPE_CONST (el_type);
17087 voltl |= TYPE_VOLATILE (el_type);
17088 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17089
17090 return set_die_type (die, base_type, cu);
17091}
17092
f792889a 17093static struct type *
e7c27a73 17094read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17095{
f792889a 17096 struct type *base_type, *cv_type;
c906108c 17097
e7c27a73 17098 base_type = die_type (die, cu);
7e314c57
JK
17099
17100 /* The die_type call above may have already set the type for this DIE. */
17101 cv_type = get_die_type (die, cu);
17102 if (cv_type)
17103 return cv_type;
17104
2f608a3a
KW
17105 /* In case the const qualifier is applied to an array type, the element type
17106 is so qualified, not the array type (section 6.7.3 of C99). */
17107 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17108 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17109
f792889a
DJ
17110 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17111 return set_die_type (die, cv_type, cu);
c906108c
SS
17112}
17113
f792889a 17114static struct type *
e7c27a73 17115read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17116{
f792889a 17117 struct type *base_type, *cv_type;
c906108c 17118
e7c27a73 17119 base_type = die_type (die, cu);
7e314c57
JK
17120
17121 /* The die_type call above may have already set the type for this DIE. */
17122 cv_type = get_die_type (die, cu);
17123 if (cv_type)
17124 return cv_type;
17125
cf363f18
MW
17126 /* In case the volatile qualifier is applied to an array type, the
17127 element type is so qualified, not the array type (section 6.7.3
17128 of C99). */
17129 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17130 return add_array_cv_type (die, cu, base_type, 0, 1);
17131
f792889a
DJ
17132 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17133 return set_die_type (die, cv_type, cu);
c906108c
SS
17134}
17135
06d66ee9
TT
17136/* Handle DW_TAG_restrict_type. */
17137
17138static struct type *
17139read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17140{
17141 struct type *base_type, *cv_type;
17142
17143 base_type = die_type (die, cu);
17144
17145 /* The die_type call above may have already set the type for this DIE. */
17146 cv_type = get_die_type (die, cu);
17147 if (cv_type)
17148 return cv_type;
17149
17150 cv_type = make_restrict_type (base_type);
17151 return set_die_type (die, cv_type, cu);
17152}
17153
a2c2acaf
MW
17154/* Handle DW_TAG_atomic_type. */
17155
17156static struct type *
17157read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17158{
17159 struct type *base_type, *cv_type;
17160
17161 base_type = die_type (die, cu);
17162
17163 /* The die_type call above may have already set the type for this DIE. */
17164 cv_type = get_die_type (die, cu);
17165 if (cv_type)
17166 return cv_type;
17167
17168 cv_type = make_atomic_type (base_type);
17169 return set_die_type (die, cv_type, cu);
17170}
17171
c906108c
SS
17172/* Extract all information from a DW_TAG_string_type DIE and add to
17173 the user defined type vector. It isn't really a user defined type,
17174 but it behaves like one, with other DIE's using an AT_user_def_type
17175 attribute to reference it. */
17176
f792889a 17177static struct type *
e7c27a73 17178read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17179{
518817b3 17180 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17181 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17182 struct type *type, *range_type, *index_type, *char_type;
17183 struct attribute *attr;
17184 unsigned int length;
17185
e142c38c 17186 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17187 if (attr)
17188 {
17189 length = DW_UNSND (attr);
17190 }
17191 else
17192 {
0963b4bd 17193 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17194 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17195 if (attr)
17196 {
17197 length = DW_UNSND (attr);
17198 }
17199 else
17200 {
17201 length = 1;
17202 }
c906108c 17203 }
6ccb9162 17204
46bf5051 17205 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17206 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17207 char_type = language_string_char_type (cu->language_defn, gdbarch);
17208 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17209
f792889a 17210 return set_die_type (die, type, cu);
c906108c
SS
17211}
17212
4d804846
JB
17213/* Assuming that DIE corresponds to a function, returns nonzero
17214 if the function is prototyped. */
17215
17216static int
17217prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17218{
17219 struct attribute *attr;
17220
17221 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17222 if (attr && (DW_UNSND (attr) != 0))
17223 return 1;
17224
17225 /* The DWARF standard implies that the DW_AT_prototyped attribute
17226 is only meaninful for C, but the concept also extends to other
17227 languages that allow unprototyped functions (Eg: Objective C).
17228 For all other languages, assume that functions are always
17229 prototyped. */
17230 if (cu->language != language_c
17231 && cu->language != language_objc
17232 && cu->language != language_opencl)
17233 return 1;
17234
17235 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17236 prototyped and unprototyped functions; default to prototyped,
17237 since that is more common in modern code (and RealView warns
17238 about unprototyped functions). */
17239 if (producer_is_realview (cu->producer))
17240 return 1;
17241
17242 return 0;
17243}
17244
c906108c
SS
17245/* Handle DIES due to C code like:
17246
17247 struct foo
c5aa993b
JM
17248 {
17249 int (*funcp)(int a, long l);
17250 int b;
17251 };
c906108c 17252
0963b4bd 17253 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17254
f792889a 17255static struct type *
e7c27a73 17256read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17257{
518817b3 17258 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17259 struct type *type; /* Type that this function returns. */
17260 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17261 struct attribute *attr;
17262
e7c27a73 17263 type = die_type (die, cu);
7e314c57
JK
17264
17265 /* The die_type call above may have already set the type for this DIE. */
17266 ftype = get_die_type (die, cu);
17267 if (ftype)
17268 return ftype;
17269
0c8b41f1 17270 ftype = lookup_function_type (type);
c906108c 17271
4d804846 17272 if (prototyped_function_p (die, cu))
a6c727b2 17273 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17274
c055b101
CV
17275 /* Store the calling convention in the type if it's available in
17276 the subroutine die. Otherwise set the calling convention to
17277 the default value DW_CC_normal. */
17278 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17279 if (attr)
17280 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17281 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17282 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17283 else
17284 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17285
743649fd
MW
17286 /* Record whether the function returns normally to its caller or not
17287 if the DWARF producer set that information. */
17288 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17289 if (attr && (DW_UNSND (attr) != 0))
17290 TYPE_NO_RETURN (ftype) = 1;
17291
76c10ea2
GM
17292 /* We need to add the subroutine type to the die immediately so
17293 we don't infinitely recurse when dealing with parameters
0963b4bd 17294 declared as the same subroutine type. */
76c10ea2 17295 set_die_type (die, ftype, cu);
6e70227d 17296
639d11d3 17297 if (die->child != NULL)
c906108c 17298 {
bb5ed363 17299 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17300 struct die_info *child_die;
8072405b 17301 int nparams, iparams;
c906108c
SS
17302
17303 /* Count the number of parameters.
17304 FIXME: GDB currently ignores vararg functions, but knows about
17305 vararg member functions. */
8072405b 17306 nparams = 0;
639d11d3 17307 child_die = die->child;
c906108c
SS
17308 while (child_die && child_die->tag)
17309 {
17310 if (child_die->tag == DW_TAG_formal_parameter)
17311 nparams++;
17312 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17313 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17314 child_die = sibling_die (child_die);
17315 }
17316
17317 /* Allocate storage for parameters and fill them in. */
17318 TYPE_NFIELDS (ftype) = nparams;
17319 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17320 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17321
8072405b
JK
17322 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17323 even if we error out during the parameters reading below. */
17324 for (iparams = 0; iparams < nparams; iparams++)
17325 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17326
17327 iparams = 0;
639d11d3 17328 child_die = die->child;
c906108c
SS
17329 while (child_die && child_die->tag)
17330 {
17331 if (child_die->tag == DW_TAG_formal_parameter)
17332 {
3ce3b1ba
PA
17333 struct type *arg_type;
17334
17335 /* DWARF version 2 has no clean way to discern C++
17336 static and non-static member functions. G++ helps
17337 GDB by marking the first parameter for non-static
17338 member functions (which is the this pointer) as
17339 artificial. We pass this information to
17340 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17341
17342 DWARF version 3 added DW_AT_object_pointer, which GCC
17343 4.5 does not yet generate. */
e142c38c 17344 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17345 if (attr)
17346 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17347 else
9c37b5ae 17348 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17349 arg_type = die_type (child_die, cu);
17350
17351 /* RealView does not mark THIS as const, which the testsuite
17352 expects. GCC marks THIS as const in method definitions,
17353 but not in the class specifications (GCC PR 43053). */
17354 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17355 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17356 {
17357 int is_this = 0;
17358 struct dwarf2_cu *arg_cu = cu;
17359 const char *name = dwarf2_name (child_die, cu);
17360
17361 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17362 if (attr)
17363 {
17364 /* If the compiler emits this, use it. */
17365 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17366 is_this = 1;
17367 }
17368 else if (name && strcmp (name, "this") == 0)
17369 /* Function definitions will have the argument names. */
17370 is_this = 1;
17371 else if (name == NULL && iparams == 0)
17372 /* Declarations may not have the names, so like
17373 elsewhere in GDB, assume an artificial first
17374 argument is "this". */
17375 is_this = 1;
17376
17377 if (is_this)
17378 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17379 arg_type, 0);
17380 }
17381
17382 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17383 iparams++;
17384 }
17385 child_die = sibling_die (child_die);
17386 }
17387 }
17388
76c10ea2 17389 return ftype;
c906108c
SS
17390}
17391
f792889a 17392static struct type *
e7c27a73 17393read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17394{
518817b3 17395 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17396 const char *name = NULL;
3c8e0968 17397 struct type *this_type, *target_type;
c906108c 17398
94af9270 17399 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17400 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17401 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17402 set_die_type (die, this_type, cu);
3c8e0968
DE
17403 target_type = die_type (die, cu);
17404 if (target_type != this_type)
17405 TYPE_TARGET_TYPE (this_type) = target_type;
17406 else
17407 {
17408 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17409 spec and cause infinite loops in GDB. */
17410 complaint (&symfile_complaints,
17411 _("Self-referential DW_TAG_typedef "
17412 "- DIE at 0x%x [in module %s]"),
9c541725 17413 to_underlying (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17414 TYPE_TARGET_TYPE (this_type) = NULL;
17415 }
f792889a 17416 return this_type;
c906108c
SS
17417}
17418
9b790ce7
UW
17419/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17420 (which may be different from NAME) to the architecture back-end to allow
17421 it to guess the correct format if necessary. */
17422
17423static struct type *
17424dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17425 const char *name_hint)
17426{
17427 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17428 const struct floatformat **format;
17429 struct type *type;
17430
17431 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17432 if (format)
17433 type = init_float_type (objfile, bits, name, format);
17434 else
77b7c781 17435 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17436
17437 return type;
17438}
17439
c906108c
SS
17440/* Find a representation of a given base type and install
17441 it in the TYPE field of the die. */
17442
f792889a 17443static struct type *
e7c27a73 17444read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17445{
518817b3 17446 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17447 struct type *type;
17448 struct attribute *attr;
19f392bc 17449 int encoding = 0, bits = 0;
15d034d0 17450 const char *name;
c906108c 17451
e142c38c 17452 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17453 if (attr)
17454 {
17455 encoding = DW_UNSND (attr);
17456 }
e142c38c 17457 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17458 if (attr)
17459 {
19f392bc 17460 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17461 }
39cbfefa 17462 name = dwarf2_name (die, cu);
6ccb9162 17463 if (!name)
c906108c 17464 {
6ccb9162
UW
17465 complaint (&symfile_complaints,
17466 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17467 }
6ccb9162
UW
17468
17469 switch (encoding)
c906108c 17470 {
6ccb9162
UW
17471 case DW_ATE_address:
17472 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17473 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17474 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17475 break;
17476 case DW_ATE_boolean:
19f392bc 17477 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17478 break;
17479 case DW_ATE_complex_float:
9b790ce7 17480 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 17481 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17482 break;
17483 case DW_ATE_decimal_float:
19f392bc 17484 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17485 break;
17486 case DW_ATE_float:
9b790ce7 17487 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17488 break;
17489 case DW_ATE_signed:
19f392bc 17490 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17491 break;
17492 case DW_ATE_unsigned:
3b2b8fea
TT
17493 if (cu->language == language_fortran
17494 && name
61012eef 17495 && startswith (name, "character("))
19f392bc
UW
17496 type = init_character_type (objfile, bits, 1, name);
17497 else
17498 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
17499 break;
17500 case DW_ATE_signed_char:
6e70227d 17501 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17502 || cu->language == language_pascal
17503 || cu->language == language_fortran)
19f392bc
UW
17504 type = init_character_type (objfile, bits, 0, name);
17505 else
17506 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17507 break;
17508 case DW_ATE_unsigned_char:
868a0084 17509 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17510 || cu->language == language_pascal
c44af4eb
TT
17511 || cu->language == language_fortran
17512 || cu->language == language_rust)
19f392bc
UW
17513 type = init_character_type (objfile, bits, 1, name);
17514 else
17515 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 17516 break;
75079b2b 17517 case DW_ATE_UTF:
53e710ac
PA
17518 {
17519 gdbarch *arch = get_objfile_arch (objfile);
17520
17521 if (bits == 16)
17522 type = builtin_type (arch)->builtin_char16;
17523 else if (bits == 32)
17524 type = builtin_type (arch)->builtin_char32;
17525 else
17526 {
17527 complaint (&symfile_complaints,
17528 _("unsupported DW_ATE_UTF bit size: '%d'"),
17529 bits);
17530 type = init_integer_type (objfile, bits, 1, name);
17531 }
17532 return set_die_type (die, type, cu);
17533 }
75079b2b
TT
17534 break;
17535
6ccb9162
UW
17536 default:
17537 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17538 dwarf_type_encoding_name (encoding));
77b7c781 17539 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17540 break;
c906108c 17541 }
6ccb9162 17542
0114d602 17543 if (name && strcmp (name, "char") == 0)
876cecd0 17544 TYPE_NOSIGN (type) = 1;
0114d602 17545
f792889a 17546 return set_die_type (die, type, cu);
c906108c
SS
17547}
17548
80180f79
SA
17549/* Parse dwarf attribute if it's a block, reference or constant and put the
17550 resulting value of the attribute into struct bound_prop.
17551 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17552
17553static int
17554attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17555 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17556{
17557 struct dwarf2_property_baton *baton;
518817b3
SM
17558 struct obstack *obstack
17559 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79
SA
17560
17561 if (attr == NULL || prop == NULL)
17562 return 0;
17563
17564 if (attr_form_is_block (attr))
17565 {
8d749320 17566 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
17567 baton->referenced_type = NULL;
17568 baton->locexpr.per_cu = cu->per_cu;
17569 baton->locexpr.size = DW_BLOCK (attr)->size;
17570 baton->locexpr.data = DW_BLOCK (attr)->data;
17571 prop->data.baton = baton;
17572 prop->kind = PROP_LOCEXPR;
17573 gdb_assert (prop->data.baton != NULL);
17574 }
17575 else if (attr_form_is_ref (attr))
17576 {
17577 struct dwarf2_cu *target_cu = cu;
17578 struct die_info *target_die;
17579 struct attribute *target_attr;
17580
17581 target_die = follow_die_ref (die, attr, &target_cu);
17582 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17583 if (target_attr == NULL)
17584 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17585 target_cu);
80180f79
SA
17586 if (target_attr == NULL)
17587 return 0;
17588
df25ebbd 17589 switch (target_attr->name)
80180f79 17590 {
df25ebbd
JB
17591 case DW_AT_location:
17592 if (attr_form_is_section_offset (target_attr))
17593 {
8d749320 17594 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17595 baton->referenced_type = die_type (target_die, target_cu);
17596 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17597 prop->data.baton = baton;
17598 prop->kind = PROP_LOCLIST;
17599 gdb_assert (prop->data.baton != NULL);
17600 }
17601 else if (attr_form_is_block (target_attr))
17602 {
8d749320 17603 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17604 baton->referenced_type = die_type (target_die, target_cu);
17605 baton->locexpr.per_cu = cu->per_cu;
17606 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17607 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17608 prop->data.baton = baton;
17609 prop->kind = PROP_LOCEXPR;
17610 gdb_assert (prop->data.baton != NULL);
17611 }
17612 else
17613 {
17614 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17615 "dynamic property");
17616 return 0;
17617 }
17618 break;
17619 case DW_AT_data_member_location:
17620 {
17621 LONGEST offset;
17622
17623 if (!handle_data_member_location (target_die, target_cu,
17624 &offset))
17625 return 0;
17626
8d749320 17627 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
17628 baton->referenced_type = read_type_die (target_die->parent,
17629 target_cu);
df25ebbd
JB
17630 baton->offset_info.offset = offset;
17631 baton->offset_info.type = die_type (target_die, target_cu);
17632 prop->data.baton = baton;
17633 prop->kind = PROP_ADDR_OFFSET;
17634 break;
17635 }
80180f79
SA
17636 }
17637 }
17638 else if (attr_form_is_constant (attr))
17639 {
17640 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17641 prop->kind = PROP_CONST;
17642 }
17643 else
17644 {
17645 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17646 dwarf2_name (die, cu));
17647 return 0;
17648 }
17649
17650 return 1;
17651}
17652
a02abb62
JB
17653/* Read the given DW_AT_subrange DIE. */
17654
f792889a 17655static struct type *
a02abb62
JB
17656read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17657{
4c9ad8c2 17658 struct type *base_type, *orig_base_type;
a02abb62
JB
17659 struct type *range_type;
17660 struct attribute *attr;
729efb13 17661 struct dynamic_prop low, high;
4fae6e18 17662 int low_default_is_valid;
c451ebe5 17663 int high_bound_is_count = 0;
15d034d0 17664 const char *name;
43bbcdc2 17665 LONGEST negative_mask;
e77813c8 17666
4c9ad8c2
TT
17667 orig_base_type = die_type (die, cu);
17668 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17669 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17670 creating the range type, but we use the result of check_typedef
17671 when examining properties of the type. */
17672 base_type = check_typedef (orig_base_type);
a02abb62 17673
7e314c57
JK
17674 /* The die_type call above may have already set the type for this DIE. */
17675 range_type = get_die_type (die, cu);
17676 if (range_type)
17677 return range_type;
17678
729efb13
SA
17679 low.kind = PROP_CONST;
17680 high.kind = PROP_CONST;
17681 high.data.const_val = 0;
17682
4fae6e18
JK
17683 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17684 omitting DW_AT_lower_bound. */
17685 switch (cu->language)
6e70227d 17686 {
4fae6e18
JK
17687 case language_c:
17688 case language_cplus:
729efb13 17689 low.data.const_val = 0;
4fae6e18
JK
17690 low_default_is_valid = 1;
17691 break;
17692 case language_fortran:
729efb13 17693 low.data.const_val = 1;
4fae6e18
JK
17694 low_default_is_valid = 1;
17695 break;
17696 case language_d:
4fae6e18 17697 case language_objc:
c44af4eb 17698 case language_rust:
729efb13 17699 low.data.const_val = 0;
4fae6e18
JK
17700 low_default_is_valid = (cu->header.version >= 4);
17701 break;
17702 case language_ada:
17703 case language_m2:
17704 case language_pascal:
729efb13 17705 low.data.const_val = 1;
4fae6e18
JK
17706 low_default_is_valid = (cu->header.version >= 4);
17707 break;
17708 default:
729efb13 17709 low.data.const_val = 0;
4fae6e18
JK
17710 low_default_is_valid = 0;
17711 break;
a02abb62
JB
17712 }
17713
e142c38c 17714 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 17715 if (attr)
11c1ba78 17716 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
17717 else if (!low_default_is_valid)
17718 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17719 "- DIE at 0x%x [in module %s]"),
e3b94546 17720 to_underlying (die->sect_off),
518817b3 17721 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17722
e142c38c 17723 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 17724 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
17725 {
17726 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 17727 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 17728 {
c451ebe5
SA
17729 /* If bounds are constant do the final calculation here. */
17730 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17731 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17732 else
17733 high_bound_is_count = 1;
c2ff108b 17734 }
e77813c8
PM
17735 }
17736
17737 /* Dwarf-2 specifications explicitly allows to create subrange types
17738 without specifying a base type.
17739 In that case, the base type must be set to the type of
17740 the lower bound, upper bound or count, in that order, if any of these
17741 three attributes references an object that has a type.
17742 If no base type is found, the Dwarf-2 specifications say that
17743 a signed integer type of size equal to the size of an address should
17744 be used.
17745 For the following C code: `extern char gdb_int [];'
17746 GCC produces an empty range DIE.
17747 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 17748 high bound or count are not yet handled by this code. */
e77813c8
PM
17749 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17750 {
518817b3 17751 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e77813c8
PM
17752 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17753 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17754 struct type *int_type = objfile_type (objfile)->builtin_int;
17755
17756 /* Test "int", "long int", and "long long int" objfile types,
17757 and select the first one having a size above or equal to the
17758 architecture address size. */
17759 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17760 base_type = int_type;
17761 else
17762 {
17763 int_type = objfile_type (objfile)->builtin_long;
17764 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17765 base_type = int_type;
17766 else
17767 {
17768 int_type = objfile_type (objfile)->builtin_long_long;
17769 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17770 base_type = int_type;
17771 }
17772 }
17773 }
a02abb62 17774
dbb9c2b1
JB
17775 /* Normally, the DWARF producers are expected to use a signed
17776 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17777 But this is unfortunately not always the case, as witnessed
17778 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17779 is used instead. To work around that ambiguity, we treat
17780 the bounds as signed, and thus sign-extend their values, when
17781 the base type is signed. */
6e70227d 17782 negative_mask =
66c6502d 17783 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17784 if (low.kind == PROP_CONST
17785 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17786 low.data.const_val |= negative_mask;
17787 if (high.kind == PROP_CONST
17788 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17789 high.data.const_val |= negative_mask;
43bbcdc2 17790
729efb13 17791 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 17792
c451ebe5
SA
17793 if (high_bound_is_count)
17794 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17795
c2ff108b
JK
17796 /* Ada expects an empty array on no boundary attributes. */
17797 if (attr == NULL && cu->language != language_ada)
729efb13 17798 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17799
39cbfefa
DJ
17800 name = dwarf2_name (die, cu);
17801 if (name)
17802 TYPE_NAME (range_type) = name;
6e70227d 17803
e142c38c 17804 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
17805 if (attr)
17806 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17807
7e314c57
JK
17808 set_die_type (die, range_type, cu);
17809
17810 /* set_die_type should be already done. */
b4ba55a1
JB
17811 set_descriptive_type (range_type, die, cu);
17812
7e314c57 17813 return range_type;
a02abb62 17814}
6e70227d 17815
f792889a 17816static struct type *
81a17f79
JB
17817read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17818{
17819 struct type *type;
81a17f79 17820
518817b3
SM
17821 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17822 NULL);
0114d602 17823 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17824
74a2f8ff
JB
17825 /* In Ada, an unspecified type is typically used when the description
17826 of the type is defered to a different unit. When encountering
17827 such a type, we treat it as a stub, and try to resolve it later on,
17828 when needed. */
17829 if (cu->language == language_ada)
17830 TYPE_STUB (type) = 1;
17831
f792889a 17832 return set_die_type (die, type, cu);
81a17f79 17833}
a02abb62 17834
639d11d3
DC
17835/* Read a single die and all its descendents. Set the die's sibling
17836 field to NULL; set other fields in the die correctly, and set all
17837 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17838 location of the info_ptr after reading all of those dies. PARENT
17839 is the parent of the die in question. */
17840
17841static struct die_info *
dee91e82 17842read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17843 const gdb_byte *info_ptr,
17844 const gdb_byte **new_info_ptr,
dee91e82 17845 struct die_info *parent)
639d11d3
DC
17846{
17847 struct die_info *die;
d521ce57 17848 const gdb_byte *cur_ptr;
639d11d3
DC
17849 int has_children;
17850
bf6af496 17851 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
17852 if (die == NULL)
17853 {
17854 *new_info_ptr = cur_ptr;
17855 return NULL;
17856 }
93311388 17857 store_in_ref_table (die, reader->cu);
639d11d3
DC
17858
17859 if (has_children)
bf6af496 17860 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17861 else
17862 {
17863 die->child = NULL;
17864 *new_info_ptr = cur_ptr;
17865 }
17866
17867 die->sibling = NULL;
17868 die->parent = parent;
17869 return die;
17870}
17871
17872/* Read a die, all of its descendents, and all of its siblings; set
17873 all of the fields of all of the dies correctly. Arguments are as
17874 in read_die_and_children. */
17875
17876static struct die_info *
bf6af496 17877read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17878 const gdb_byte *info_ptr,
17879 const gdb_byte **new_info_ptr,
bf6af496 17880 struct die_info *parent)
639d11d3
DC
17881{
17882 struct die_info *first_die, *last_sibling;
d521ce57 17883 const gdb_byte *cur_ptr;
639d11d3 17884
c906108c 17885 cur_ptr = info_ptr;
639d11d3
DC
17886 first_die = last_sibling = NULL;
17887
17888 while (1)
c906108c 17889 {
639d11d3 17890 struct die_info *die
dee91e82 17891 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17892
1d325ec1 17893 if (die == NULL)
c906108c 17894 {
639d11d3
DC
17895 *new_info_ptr = cur_ptr;
17896 return first_die;
c906108c 17897 }
1d325ec1
DJ
17898
17899 if (!first_die)
17900 first_die = die;
c906108c 17901 else
1d325ec1
DJ
17902 last_sibling->sibling = die;
17903
17904 last_sibling = die;
c906108c 17905 }
c906108c
SS
17906}
17907
bf6af496
DE
17908/* Read a die, all of its descendents, and all of its siblings; set
17909 all of the fields of all of the dies correctly. Arguments are as
17910 in read_die_and_children.
17911 This the main entry point for reading a DIE and all its children. */
17912
17913static struct die_info *
17914read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17915 const gdb_byte *info_ptr,
17916 const gdb_byte **new_info_ptr,
bf6af496
DE
17917 struct die_info *parent)
17918{
17919 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17920 new_info_ptr, parent);
17921
b4f54984 17922 if (dwarf_die_debug)
bf6af496
DE
17923 {
17924 fprintf_unfiltered (gdb_stdlog,
17925 "Read die from %s@0x%x of %s:\n",
a32a8923 17926 get_section_name (reader->die_section),
bf6af496
DE
17927 (unsigned) (info_ptr - reader->die_section->buffer),
17928 bfd_get_filename (reader->abfd));
b4f54984 17929 dump_die (die, dwarf_die_debug);
bf6af496
DE
17930 }
17931
17932 return die;
17933}
17934
3019eac3
DE
17935/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17936 attributes.
17937 The caller is responsible for filling in the extra attributes
17938 and updating (*DIEP)->num_attrs.
17939 Set DIEP to point to a newly allocated die with its information,
17940 except for its child, sibling, and parent fields.
17941 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 17942
d521ce57 17943static const gdb_byte *
3019eac3 17944read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17945 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 17946 int *has_children, int num_extra_attrs)
93311388 17947{
b64f50a1 17948 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17949 struct abbrev_info *abbrev;
17950 struct die_info *die;
17951 struct dwarf2_cu *cu = reader->cu;
17952 bfd *abfd = reader->abfd;
17953
9c541725 17954 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17955 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17956 info_ptr += bytes_read;
17957 if (!abbrev_number)
17958 {
17959 *diep = NULL;
17960 *has_children = 0;
17961 return info_ptr;
17962 }
17963
685af9cd 17964 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17965 if (!abbrev)
348e048f
DE
17966 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17967 abbrev_number,
17968 bfd_get_filename (abfd));
17969
3019eac3 17970 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17971 die->sect_off = sect_off;
93311388
DE
17972 die->tag = abbrev->tag;
17973 die->abbrev = abbrev_number;
17974
3019eac3
DE
17975 /* Make the result usable.
17976 The caller needs to update num_attrs after adding the extra
17977 attributes. */
93311388
DE
17978 die->num_attrs = abbrev->num_attrs;
17979
17980 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
17981 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17982 info_ptr);
93311388
DE
17983
17984 *diep = die;
17985 *has_children = abbrev->has_children;
17986 return info_ptr;
17987}
17988
3019eac3
DE
17989/* Read a die and all its attributes.
17990 Set DIEP to point to a newly allocated die with its information,
17991 except for its child, sibling, and parent fields.
17992 Set HAS_CHILDREN to tell whether the die has children or not. */
17993
d521ce57 17994static const gdb_byte *
3019eac3 17995read_full_die (const struct die_reader_specs *reader,
d521ce57 17996 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
17997 int *has_children)
17998{
d521ce57 17999 const gdb_byte *result;
bf6af496
DE
18000
18001 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18002
b4f54984 18003 if (dwarf_die_debug)
bf6af496
DE
18004 {
18005 fprintf_unfiltered (gdb_stdlog,
18006 "Read die from %s@0x%x of %s:\n",
a32a8923 18007 get_section_name (reader->die_section),
bf6af496
DE
18008 (unsigned) (info_ptr - reader->die_section->buffer),
18009 bfd_get_filename (reader->abfd));
b4f54984 18010 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18011 }
18012
18013 return result;
3019eac3 18014}
433df2d4
DE
18015\f
18016/* Abbreviation tables.
3019eac3 18017
433df2d4 18018 In DWARF version 2, the description of the debugging information is
c906108c
SS
18019 stored in a separate .debug_abbrev section. Before we read any
18020 dies from a section we read in all abbreviations and install them
433df2d4
DE
18021 in a hash table. */
18022
18023/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18024
685af9cd
TT
18025struct abbrev_info *
18026abbrev_table::alloc_abbrev ()
433df2d4
DE
18027{
18028 struct abbrev_info *abbrev;
18029
685af9cd 18030 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18031 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18032
433df2d4
DE
18033 return abbrev;
18034}
18035
18036/* Add an abbreviation to the table. */
c906108c 18037
685af9cd
TT
18038void
18039abbrev_table::add_abbrev (unsigned int abbrev_number,
18040 struct abbrev_info *abbrev)
433df2d4
DE
18041{
18042 unsigned int hash_number;
18043
18044 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18045 abbrev->next = m_abbrevs[hash_number];
18046 m_abbrevs[hash_number] = abbrev;
433df2d4 18047}
dee91e82 18048
433df2d4
DE
18049/* Look up an abbrev in the table.
18050 Returns NULL if the abbrev is not found. */
18051
685af9cd
TT
18052struct abbrev_info *
18053abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18054{
433df2d4
DE
18055 unsigned int hash_number;
18056 struct abbrev_info *abbrev;
18057
18058 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18059 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18060
18061 while (abbrev)
18062 {
18063 if (abbrev->number == abbrev_number)
18064 return abbrev;
18065 abbrev = abbrev->next;
18066 }
18067 return NULL;
18068}
18069
18070/* Read in an abbrev table. */
18071
685af9cd 18072static abbrev_table_up
ed2dc618
SM
18073abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18074 struct dwarf2_section_info *section,
9c541725 18075 sect_offset sect_off)
433df2d4
DE
18076{
18077 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18078 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18079 const gdb_byte *abbrev_ptr;
c906108c
SS
18080 struct abbrev_info *cur_abbrev;
18081 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18082 unsigned int abbrev_form;
f3dd6933
DJ
18083 struct attr_abbrev *cur_attrs;
18084 unsigned int allocated_attrs;
c906108c 18085
685af9cd 18086 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18087
433df2d4 18088 dwarf2_read_section (objfile, section);
9c541725 18089 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18090 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18091 abbrev_ptr += bytes_read;
18092
f3dd6933 18093 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 18094 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 18095
0963b4bd 18096 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18097 while (abbrev_number)
18098 {
685af9cd 18099 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18100
18101 /* read in abbrev header */
18102 cur_abbrev->number = abbrev_number;
aead7601
SM
18103 cur_abbrev->tag
18104 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18105 abbrev_ptr += bytes_read;
18106 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18107 abbrev_ptr += 1;
18108
18109 /* now read in declarations */
22d2f3ab 18110 for (;;)
c906108c 18111 {
43988095
JK
18112 LONGEST implicit_const;
18113
22d2f3ab
JK
18114 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18115 abbrev_ptr += bytes_read;
18116 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18117 abbrev_ptr += bytes_read;
43988095
JK
18118 if (abbrev_form == DW_FORM_implicit_const)
18119 {
18120 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18121 &bytes_read);
18122 abbrev_ptr += bytes_read;
18123 }
18124 else
18125 {
18126 /* Initialize it due to a false compiler warning. */
18127 implicit_const = -1;
18128 }
22d2f3ab
JK
18129
18130 if (abbrev_name == 0)
18131 break;
18132
f3dd6933 18133 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18134 {
f3dd6933
DJ
18135 allocated_attrs += ATTR_ALLOC_CHUNK;
18136 cur_attrs
224c3ddb 18137 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18138 }
ae038cb0 18139
aead7601
SM
18140 cur_attrs[cur_abbrev->num_attrs].name
18141 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18142 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18143 = (enum dwarf_form) abbrev_form;
43988095 18144 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18145 ++cur_abbrev->num_attrs;
c906108c
SS
18146 }
18147
8d749320
SM
18148 cur_abbrev->attrs =
18149 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18150 cur_abbrev->num_attrs);
f3dd6933
DJ
18151 memcpy (cur_abbrev->attrs, cur_attrs,
18152 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18153
685af9cd 18154 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18155
18156 /* Get next abbreviation.
18157 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18158 always properly terminated with an abbrev number of 0.
18159 Exit loop if we encounter an abbreviation which we have
18160 already read (which means we are about to read the abbreviations
18161 for the next compile unit) or if the end of the abbreviation
18162 table is reached. */
433df2d4 18163 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18164 break;
18165 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18166 abbrev_ptr += bytes_read;
685af9cd 18167 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18168 break;
18169 }
f3dd6933
DJ
18170
18171 xfree (cur_attrs);
433df2d4 18172 return abbrev_table;
c906108c
SS
18173}
18174
72bf9492
DJ
18175/* Returns nonzero if TAG represents a type that we might generate a partial
18176 symbol for. */
18177
18178static int
18179is_type_tag_for_partial (int tag)
18180{
18181 switch (tag)
18182 {
18183#if 0
18184 /* Some types that would be reasonable to generate partial symbols for,
18185 that we don't at present. */
18186 case DW_TAG_array_type:
18187 case DW_TAG_file_type:
18188 case DW_TAG_ptr_to_member_type:
18189 case DW_TAG_set_type:
18190 case DW_TAG_string_type:
18191 case DW_TAG_subroutine_type:
18192#endif
18193 case DW_TAG_base_type:
18194 case DW_TAG_class_type:
680b30c7 18195 case DW_TAG_interface_type:
72bf9492
DJ
18196 case DW_TAG_enumeration_type:
18197 case DW_TAG_structure_type:
18198 case DW_TAG_subrange_type:
18199 case DW_TAG_typedef:
18200 case DW_TAG_union_type:
18201 return 1;
18202 default:
18203 return 0;
18204 }
18205}
18206
18207/* Load all DIEs that are interesting for partial symbols into memory. */
18208
18209static struct partial_die_info *
dee91e82 18210load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18211 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18212{
dee91e82 18213 struct dwarf2_cu *cu = reader->cu;
518817b3 18214 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492
DJ
18215 struct partial_die_info *part_die;
18216 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18217 unsigned int bytes_read;
5afb4e99 18218 unsigned int load_all = 0;
72bf9492
DJ
18219 int nesting_level = 1;
18220
18221 parent_die = NULL;
18222 last_die = NULL;
18223
7adf1e79
DE
18224 gdb_assert (cu->per_cu != NULL);
18225 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18226 load_all = 1;
18227
72bf9492
DJ
18228 cu->partial_dies
18229 = htab_create_alloc_ex (cu->header.length / 12,
18230 partial_die_hash,
18231 partial_die_eq,
18232 NULL,
18233 &cu->comp_unit_obstack,
18234 hashtab_obstack_allocate,
18235 dummy_obstack_deallocate);
18236
8d749320 18237 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
18238
18239 while (1)
18240 {
685af9cd 18241 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18242
18243 /* A NULL abbrev means the end of a series of children. */
18244 if (abbrev == NULL)
18245 {
18246 if (--nesting_level == 0)
18247 {
18248 /* PART_DIE was probably the last thing allocated on the
18249 comp_unit_obstack, so we could call obstack_free
18250 here. We don't do that because the waste is small,
18251 and will be cleaned up when we're done with this
18252 compilation unit. This way, we're also more robust
18253 against other users of the comp_unit_obstack. */
18254 return first_die;
18255 }
18256 info_ptr += bytes_read;
18257 last_die = parent_die;
18258 parent_die = parent_die->die_parent;
18259 continue;
18260 }
18261
98bfdba5
PA
18262 /* Check for template arguments. We never save these; if
18263 they're seen, we just mark the parent, and go on our way. */
18264 if (parent_die != NULL
18265 && cu->language == language_cplus
18266 && (abbrev->tag == DW_TAG_template_type_param
18267 || abbrev->tag == DW_TAG_template_value_param))
18268 {
18269 parent_die->has_template_arguments = 1;
18270
18271 if (!load_all)
18272 {
18273 /* We don't need a partial DIE for the template argument. */
dee91e82 18274 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18275 continue;
18276 }
18277 }
18278
0d99eb77 18279 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18280 Skip their other children. */
18281 if (!load_all
18282 && cu->language == language_cplus
18283 && parent_die != NULL
18284 && parent_die->tag == DW_TAG_subprogram)
18285 {
dee91e82 18286 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18287 continue;
18288 }
18289
5afb4e99
DJ
18290 /* Check whether this DIE is interesting enough to save. Normally
18291 we would not be interested in members here, but there may be
18292 later variables referencing them via DW_AT_specification (for
18293 static members). */
18294 if (!load_all
18295 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18296 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18297 && abbrev->tag != DW_TAG_enumerator
18298 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18299 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18300 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18301 && abbrev->tag != DW_TAG_variable
5afb4e99 18302 && abbrev->tag != DW_TAG_namespace
f55ee35c 18303 && abbrev->tag != DW_TAG_module
95554aad 18304 && abbrev->tag != DW_TAG_member
74921315
KS
18305 && abbrev->tag != DW_TAG_imported_unit
18306 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18307 {
18308 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18309 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18310 continue;
18311 }
18312
dee91e82
DE
18313 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
18314 info_ptr);
72bf9492
DJ
18315
18316 /* This two-pass algorithm for processing partial symbols has a
18317 high cost in cache pressure. Thus, handle some simple cases
18318 here which cover the majority of C partial symbols. DIEs
18319 which neither have specification tags in them, nor could have
18320 specification tags elsewhere pointing at them, can simply be
18321 processed and discarded.
18322
18323 This segment is also optional; scan_partial_symbols and
18324 add_partial_symbol will handle these DIEs if we chain
18325 them in normally. When compilers which do not emit large
18326 quantities of duplicate debug information are more common,
18327 this code can probably be removed. */
18328
18329 /* Any complete simple types at the top level (pretty much all
18330 of them, for a language without namespaces), can be processed
18331 directly. */
18332 if (parent_die == NULL
18333 && part_die->has_specification == 0
18334 && part_die->is_declaration == 0
d8228535 18335 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
18336 || part_die->tag == DW_TAG_base_type
18337 || part_die->tag == DW_TAG_subrange_type))
18338 {
18339 if (building_psymtab && part_die->name != NULL)
04a679b8 18340 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 18341 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 18342 &objfile->static_psymbols,
1762568f 18343 0, cu->language, objfile);
dee91e82 18344 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
18345 continue;
18346 }
18347
d8228535
JK
18348 /* The exception for DW_TAG_typedef with has_children above is
18349 a workaround of GCC PR debug/47510. In the case of this complaint
18350 type_name_no_tag_or_error will error on such types later.
18351
18352 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18353 it could not find the child DIEs referenced later, this is checked
18354 above. In correct DWARF DW_TAG_typedef should have no children. */
18355
18356 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
18357 complaint (&symfile_complaints,
18358 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18359 "- DIE at 0x%x [in module %s]"),
9c541725 18360 to_underlying (part_die->sect_off), objfile_name (objfile));
d8228535 18361
72bf9492
DJ
18362 /* If we're at the second level, and we're an enumerator, and
18363 our parent has no specification (meaning possibly lives in a
18364 namespace elsewhere), then we can add the partial symbol now
18365 instead of queueing it. */
18366 if (part_die->tag == DW_TAG_enumerator
18367 && parent_die != NULL
18368 && parent_die->die_parent == NULL
18369 && parent_die->tag == DW_TAG_enumeration_type
18370 && parent_die->has_specification == 0)
18371 {
18372 if (part_die->name == NULL)
3e43a32a
MS
18373 complaint (&symfile_complaints,
18374 _("malformed enumerator DIE ignored"));
72bf9492 18375 else if (building_psymtab)
04a679b8 18376 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 18377 VAR_DOMAIN, LOC_CONST,
9c37b5ae 18378 cu->language == language_cplus
bb5ed363
DE
18379 ? &objfile->global_psymbols
18380 : &objfile->static_psymbols,
1762568f 18381 0, cu->language, objfile);
72bf9492 18382
dee91e82 18383 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
18384 continue;
18385 }
18386
18387 /* We'll save this DIE so link it in. */
18388 part_die->die_parent = parent_die;
18389 part_die->die_sibling = NULL;
18390 part_die->die_child = NULL;
18391
18392 if (last_die && last_die == parent_die)
18393 last_die->die_child = part_die;
18394 else if (last_die)
18395 last_die->die_sibling = part_die;
18396
18397 last_die = part_die;
18398
18399 if (first_die == NULL)
18400 first_die = part_die;
18401
18402 /* Maybe add the DIE to the hash table. Not all DIEs that we
18403 find interesting need to be in the hash table, because we
18404 also have the parent/sibling/child chains; only those that we
18405 might refer to by offset later during partial symbol reading.
18406
18407 For now this means things that might have be the target of a
18408 DW_AT_specification, DW_AT_abstract_origin, or
18409 DW_AT_extension. DW_AT_extension will refer only to
18410 namespaces; DW_AT_abstract_origin refers to functions (and
18411 many things under the function DIE, but we do not recurse
18412 into function DIEs during partial symbol reading) and
18413 possibly variables as well; DW_AT_specification refers to
18414 declarations. Declarations ought to have the DW_AT_declaration
18415 flag. It happens that GCC forgets to put it in sometimes, but
18416 only for functions, not for types.
18417
18418 Adding more things than necessary to the hash table is harmless
18419 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18420 wasted time in find_partial_die, when we reread the compilation
18421 unit with load_all_dies set. */
72bf9492 18422
5afb4e99 18423 if (load_all
72929c62 18424 || abbrev->tag == DW_TAG_constant
5afb4e99 18425 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18426 || abbrev->tag == DW_TAG_variable
18427 || abbrev->tag == DW_TAG_namespace
18428 || part_die->is_declaration)
18429 {
18430 void **slot;
18431
18432 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18433 to_underlying (part_die->sect_off),
18434 INSERT);
72bf9492
DJ
18435 *slot = part_die;
18436 }
18437
8d749320 18438 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
18439
18440 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18441 we have no reason to follow the children of structures; for other
98bfdba5
PA
18442 languages we have to, so that we can get at method physnames
18443 to infer fully qualified class names, for DW_AT_specification,
18444 and for C++ template arguments. For C++, we also look one level
18445 inside functions to find template arguments (if the name of the
18446 function does not already contain the template arguments).
bc30ff58
JB
18447
18448 For Ada, we need to scan the children of subprograms and lexical
18449 blocks as well because Ada allows the definition of nested
18450 entities that could be interesting for the debugger, such as
18451 nested subprograms for instance. */
72bf9492 18452 if (last_die->has_children
5afb4e99
DJ
18453 && (load_all
18454 || last_die->tag == DW_TAG_namespace
f55ee35c 18455 || last_die->tag == DW_TAG_module
72bf9492 18456 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18457 || (cu->language == language_cplus
18458 && last_die->tag == DW_TAG_subprogram
18459 && (last_die->name == NULL
18460 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18461 || (cu->language != language_c
18462 && (last_die->tag == DW_TAG_class_type
680b30c7 18463 || last_die->tag == DW_TAG_interface_type
72bf9492 18464 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
18465 || last_die->tag == DW_TAG_union_type))
18466 || (cu->language == language_ada
18467 && (last_die->tag == DW_TAG_subprogram
18468 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18469 {
18470 nesting_level++;
18471 parent_die = last_die;
18472 continue;
18473 }
18474
18475 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18476 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18477
18478 /* Back to the top, do it again. */
18479 }
18480}
18481
c906108c
SS
18482/* Read a minimal amount of information into the minimal die structure. */
18483
d521ce57 18484static const gdb_byte *
dee91e82
DE
18485read_partial_die (const struct die_reader_specs *reader,
18486 struct partial_die_info *part_die,
18487 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 18488 const gdb_byte *info_ptr)
c906108c 18489{
dee91e82 18490 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18491 struct dwarf2_per_objfile *dwarf2_per_objfile
18492 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18493 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 18494 const gdb_byte *buffer = reader->buffer;
fa238c03 18495 unsigned int i;
c906108c 18496 struct attribute attr;
c5aa993b 18497 int has_low_pc_attr = 0;
c906108c 18498 int has_high_pc_attr = 0;
91da1414 18499 int high_pc_relative = 0;
c906108c 18500
72bf9492 18501 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 18502
9c541725 18503 part_die->sect_off = (sect_offset) (info_ptr - buffer);
72bf9492
DJ
18504
18505 info_ptr += abbrev_len;
18506
18507 if (abbrev == NULL)
18508 return info_ptr;
18509
c906108c
SS
18510 part_die->tag = abbrev->tag;
18511 part_die->has_children = abbrev->has_children;
c906108c
SS
18512
18513 for (i = 0; i < abbrev->num_attrs; ++i)
18514 {
dee91e82 18515 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
18516
18517 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18518 partial symbol table. */
c906108c
SS
18519 switch (attr.name)
18520 {
18521 case DW_AT_name:
71c25dea
TT
18522 switch (part_die->tag)
18523 {
18524 case DW_TAG_compile_unit:
95554aad 18525 case DW_TAG_partial_unit:
348e048f 18526 case DW_TAG_type_unit:
71c25dea
TT
18527 /* Compilation units have a DW_AT_name that is a filename, not
18528 a source language identifier. */
18529 case DW_TAG_enumeration_type:
18530 case DW_TAG_enumerator:
18531 /* These tags always have simple identifiers already; no need
18532 to canonicalize them. */
18533 part_die->name = DW_STRING (&attr);
18534 break;
18535 default:
18536 part_die->name
18537 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 18538 &objfile->per_bfd->storage_obstack);
71c25dea
TT
18539 break;
18540 }
c906108c 18541 break;
31ef98ae 18542 case DW_AT_linkage_name:
c906108c 18543 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18544 /* Note that both forms of linkage name might appear. We
18545 assume they will be the same, and we only store the last
18546 one we see. */
94af9270
KS
18547 if (cu->language == language_ada)
18548 part_die->name = DW_STRING (&attr);
abc72ce4 18549 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
18550 break;
18551 case DW_AT_low_pc:
18552 has_low_pc_attr = 1;
31aa7e4e 18553 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
18554 break;
18555 case DW_AT_high_pc:
18556 has_high_pc_attr = 1;
31aa7e4e
JB
18557 part_die->highpc = attr_value_as_address (&attr);
18558 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18559 high_pc_relative = 1;
c906108c
SS
18560 break;
18561 case DW_AT_location:
0963b4bd 18562 /* Support the .debug_loc offsets. */
8e19ed76
PS
18563 if (attr_form_is_block (&attr))
18564 {
95554aad 18565 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 18566 }
3690dd37 18567 else if (attr_form_is_section_offset (&attr))
8e19ed76 18568 {
4d3c2250 18569 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18570 }
18571 else
18572 {
4d3c2250
KB
18573 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18574 "partial symbol information");
8e19ed76 18575 }
c906108c 18576 break;
c906108c
SS
18577 case DW_AT_external:
18578 part_die->is_external = DW_UNSND (&attr);
18579 break;
18580 case DW_AT_declaration:
18581 part_die->is_declaration = DW_UNSND (&attr);
18582 break;
18583 case DW_AT_type:
18584 part_die->has_type = 1;
18585 break;
18586 case DW_AT_abstract_origin:
18587 case DW_AT_specification:
72bf9492
DJ
18588 case DW_AT_extension:
18589 part_die->has_specification = 1;
c764a876 18590 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
18591 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18592 || cu->per_cu->is_dwz);
c906108c
SS
18593 break;
18594 case DW_AT_sibling:
18595 /* Ignore absolute siblings, they might point outside of
18596 the current compile unit. */
18597 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
18598 complaint (&symfile_complaints,
18599 _("ignoring absolute DW_AT_sibling"));
c906108c 18600 else
b9502d3f 18601 {
9c541725
PA
18602 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18603 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18604
18605 if (sibling_ptr < info_ptr)
18606 complaint (&symfile_complaints,
18607 _("DW_AT_sibling points backwards"));
22869d73
KS
18608 else if (sibling_ptr > reader->buffer_end)
18609 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
18610 else
18611 part_die->sibling = sibling_ptr;
18612 }
c906108c 18613 break;
fa4028e9
JB
18614 case DW_AT_byte_size:
18615 part_die->has_byte_size = 1;
18616 break;
ff908ebf
AW
18617 case DW_AT_const_value:
18618 part_die->has_const_value = 1;
18619 break;
68511cec
CES
18620 case DW_AT_calling_convention:
18621 /* DWARF doesn't provide a way to identify a program's source-level
18622 entry point. DW_AT_calling_convention attributes are only meant
18623 to describe functions' calling conventions.
18624
18625 However, because it's a necessary piece of information in
0c1b455e
TT
18626 Fortran, and before DWARF 4 DW_CC_program was the only
18627 piece of debugging information whose definition refers to
18628 a 'main program' at all, several compilers marked Fortran
18629 main programs with DW_CC_program --- even when those
18630 functions use the standard calling conventions.
18631
18632 Although DWARF now specifies a way to provide this
18633 information, we support this practice for backward
18634 compatibility. */
68511cec 18635 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
18636 && cu->language == language_fortran)
18637 part_die->main_subprogram = 1;
68511cec 18638 break;
481860b3
GB
18639 case DW_AT_inline:
18640 if (DW_UNSND (&attr) == DW_INL_inlined
18641 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18642 part_die->may_be_inlined = 1;
18643 break;
95554aad
TT
18644
18645 case DW_AT_import:
18646 if (part_die->tag == DW_TAG_imported_unit)
36586728 18647 {
9c541725 18648 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
36586728
TT
18649 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18650 || cu->per_cu->is_dwz);
18651 }
95554aad
TT
18652 break;
18653
0c1b455e
TT
18654 case DW_AT_main_subprogram:
18655 part_die->main_subprogram = DW_UNSND (&attr);
18656 break;
18657
c906108c
SS
18658 default:
18659 break;
18660 }
18661 }
18662
91da1414
MW
18663 if (high_pc_relative)
18664 part_die->highpc += part_die->lowpc;
18665
9373cf26
JK
18666 if (has_low_pc_attr && has_high_pc_attr)
18667 {
18668 /* When using the GNU linker, .gnu.linkonce. sections are used to
18669 eliminate duplicate copies of functions and vtables and such.
18670 The linker will arbitrarily choose one and discard the others.
18671 The AT_*_pc values for such functions refer to local labels in
18672 these sections. If the section from that file was discarded, the
18673 labels are not in the output, so the relocs get a value of 0.
18674 If this is a discarded function, mark the pc bounds as invalid,
18675 so that GDB will ignore it. */
18676 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18677 {
bb5ed363 18678 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
18679
18680 complaint (&symfile_complaints,
18681 _("DW_AT_low_pc %s is zero "
18682 "for DIE at 0x%x [in module %s]"),
18683 paddress (gdbarch, part_die->lowpc),
9c541725 18684 to_underlying (part_die->sect_off), objfile_name (objfile));
9373cf26
JK
18685 }
18686 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18687 else if (part_die->lowpc >= part_die->highpc)
18688 {
bb5ed363 18689 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
18690
18691 complaint (&symfile_complaints,
18692 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18693 "for DIE at 0x%x [in module %s]"),
18694 paddress (gdbarch, part_die->lowpc),
18695 paddress (gdbarch, part_die->highpc),
9c541725
PA
18696 to_underlying (part_die->sect_off),
18697 objfile_name (objfile));
9373cf26
JK
18698 }
18699 else
18700 part_die->has_pc_info = 1;
18701 }
85cbf3d3 18702
c906108c
SS
18703 return info_ptr;
18704}
18705
72bf9492
DJ
18706/* Find a cached partial DIE at OFFSET in CU. */
18707
18708static struct partial_die_info *
9c541725 18709find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
72bf9492
DJ
18710{
18711 struct partial_die_info *lookup_die = NULL;
18712 struct partial_die_info part_die;
18713
9c541725 18714 part_die.sect_off = sect_off;
9a3c8263
SM
18715 lookup_die = ((struct partial_die_info *)
18716 htab_find_with_hash (cu->partial_dies, &part_die,
9c541725 18717 to_underlying (sect_off)));
72bf9492 18718
72bf9492
DJ
18719 return lookup_die;
18720}
18721
348e048f
DE
18722/* Find a partial DIE at OFFSET, which may or may not be in CU,
18723 except in the case of .debug_types DIEs which do not reference
18724 outside their CU (they do however referencing other types via
55f1336d 18725 DW_FORM_ref_sig8). */
72bf9492
DJ
18726
18727static struct partial_die_info *
9c541725 18728find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18729{
518817b3
SM
18730 struct dwarf2_per_objfile *dwarf2_per_objfile
18731 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18732 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18733 struct dwarf2_per_cu_data *per_cu = NULL;
18734 struct partial_die_info *pd = NULL;
72bf9492 18735
36586728 18736 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18737 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18738 {
9c541725 18739 pd = find_partial_die_in_comp_unit (sect_off, cu);
5afb4e99
DJ
18740 if (pd != NULL)
18741 return pd;
0d99eb77
DE
18742 /* We missed recording what we needed.
18743 Load all dies and try again. */
18744 per_cu = cu->per_cu;
5afb4e99 18745 }
0d99eb77
DE
18746 else
18747 {
18748 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18749 if (cu->per_cu->is_debug_types)
0d99eb77 18750 {
9c541725
PA
18751 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
18752 " external reference to offset 0x%x [in module %s].\n"),
18753 to_underlying (cu->header.sect_off), to_underlying (sect_off),
0d99eb77
DE
18754 bfd_get_filename (objfile->obfd));
18755 }
9c541725 18756 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18757 dwarf2_per_objfile);
72bf9492 18758
0d99eb77
DE
18759 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18760 load_partial_comp_unit (per_cu);
ae038cb0 18761
0d99eb77 18762 per_cu->cu->last_used = 0;
9c541725 18763 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
0d99eb77 18764 }
5afb4e99 18765
dee91e82
DE
18766 /* If we didn't find it, and not all dies have been loaded,
18767 load them all and try again. */
18768
5afb4e99
DJ
18769 if (pd == NULL && per_cu->load_all_dies == 0)
18770 {
5afb4e99 18771 per_cu->load_all_dies = 1;
fd820528
DE
18772
18773 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18774 THIS_CU->cu may already be in use. So we can't just free it and
18775 replace its DIEs with the ones we read in. Instead, we leave those
18776 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18777 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18778 set. */
dee91e82 18779 load_partial_comp_unit (per_cu);
5afb4e99 18780
9c541725 18781 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
5afb4e99
DJ
18782 }
18783
18784 if (pd == NULL)
18785 internal_error (__FILE__, __LINE__,
3e43a32a
MS
18786 _("could not find partial DIE 0x%x "
18787 "in cache [from module %s]\n"),
9c541725 18788 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 18789 return pd;
72bf9492
DJ
18790}
18791
abc72ce4
DE
18792/* See if we can figure out if the class lives in a namespace. We do
18793 this by looking for a member function; its demangled name will
18794 contain namespace info, if there is any. */
18795
18796static void
18797guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18798 struct dwarf2_cu *cu)
18799{
18800 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18801 what template types look like, because the demangler
18802 frequently doesn't give the same name as the debug info. We
18803 could fix this by only using the demangled name to get the
18804 prefix (but see comment in read_structure_type). */
18805
18806 struct partial_die_info *real_pdi;
18807 struct partial_die_info *child_pdi;
18808
18809 /* If this DIE (this DIE's specification, if any) has a parent, then
18810 we should not do this. We'll prepend the parent's fully qualified
18811 name when we create the partial symbol. */
18812
18813 real_pdi = struct_pdi;
18814 while (real_pdi->has_specification)
36586728
TT
18815 real_pdi = find_partial_die (real_pdi->spec_offset,
18816 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
18817
18818 if (real_pdi->die_parent != NULL)
18819 return;
18820
18821 for (child_pdi = struct_pdi->die_child;
18822 child_pdi != NULL;
18823 child_pdi = child_pdi->die_sibling)
18824 {
18825 if (child_pdi->tag == DW_TAG_subprogram
18826 && child_pdi->linkage_name != NULL)
18827 {
18828 char *actual_class_name
18829 = language_class_name_from_physname (cu->language_defn,
18830 child_pdi->linkage_name);
18831 if (actual_class_name != NULL)
18832 {
518817b3 18833 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 18834 struct_pdi->name
224c3ddb 18835 = ((const char *)
e3b94546 18836 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
18837 actual_class_name,
18838 strlen (actual_class_name)));
abc72ce4
DE
18839 xfree (actual_class_name);
18840 }
18841 break;
18842 }
18843 }
18844}
18845
72bf9492
DJ
18846/* Adjust PART_DIE before generating a symbol for it. This function
18847 may set the is_external flag or change the DIE's name. */
18848
18849static void
18850fixup_partial_die (struct partial_die_info *part_die,
18851 struct dwarf2_cu *cu)
18852{
abc72ce4
DE
18853 /* Once we've fixed up a die, there's no point in doing so again.
18854 This also avoids a memory leak if we were to call
18855 guess_partial_die_structure_name multiple times. */
18856 if (part_die->fixup_called)
18857 return;
18858
72bf9492
DJ
18859 /* If we found a reference attribute and the DIE has no name, try
18860 to find a name in the referred to DIE. */
18861
18862 if (part_die->name == NULL && part_die->has_specification)
18863 {
18864 struct partial_die_info *spec_die;
72bf9492 18865
36586728
TT
18866 spec_die = find_partial_die (part_die->spec_offset,
18867 part_die->spec_is_dwz, cu);
72bf9492 18868
10b3939b 18869 fixup_partial_die (spec_die, cu);
72bf9492
DJ
18870
18871 if (spec_die->name)
18872 {
18873 part_die->name = spec_die->name;
18874
18875 /* Copy DW_AT_external attribute if it is set. */
18876 if (spec_die->is_external)
18877 part_die->is_external = spec_die->is_external;
18878 }
18879 }
18880
18881 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
18882
18883 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 18884 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18885
abc72ce4
DE
18886 /* If there is no parent die to provide a namespace, and there are
18887 children, see if we can determine the namespace from their linkage
122d1940 18888 name. */
abc72ce4 18889 if (cu->language == language_cplus
518817b3
SM
18890 && !VEC_empty (dwarf2_section_info_def,
18891 cu->per_cu->dwarf2_per_objfile->types)
abc72ce4
DE
18892 && part_die->die_parent == NULL
18893 && part_die->has_children
18894 && (part_die->tag == DW_TAG_class_type
18895 || part_die->tag == DW_TAG_structure_type
18896 || part_die->tag == DW_TAG_union_type))
18897 guess_partial_die_structure_name (part_die, cu);
18898
53832f31
TT
18899 /* GCC might emit a nameless struct or union that has a linkage
18900 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18901 if (part_die->name == NULL
96408a79
SA
18902 && (part_die->tag == DW_TAG_class_type
18903 || part_die->tag == DW_TAG_interface_type
18904 || part_die->tag == DW_TAG_structure_type
18905 || part_die->tag == DW_TAG_union_type)
53832f31
TT
18906 && part_die->linkage_name != NULL)
18907 {
18908 char *demangled;
18909
8de20a37 18910 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
18911 if (demangled)
18912 {
96408a79
SA
18913 const char *base;
18914
18915 /* Strip any leading namespaces/classes, keep only the base name.
18916 DW_AT_name for named DIEs does not contain the prefixes. */
18917 base = strrchr (demangled, ':');
18918 if (base && base > demangled && base[-1] == ':')
18919 base++;
18920 else
18921 base = demangled;
18922
518817b3 18923 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 18924 part_die->name
224c3ddb 18925 = ((const char *)
e3b94546 18926 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 18927 base, strlen (base)));
53832f31
TT
18928 xfree (demangled);
18929 }
18930 }
18931
abc72ce4 18932 part_die->fixup_called = 1;
72bf9492
DJ
18933}
18934
a8329558 18935/* Read an attribute value described by an attribute form. */
c906108c 18936
d521ce57 18937static const gdb_byte *
dee91e82
DE
18938read_attribute_value (const struct die_reader_specs *reader,
18939 struct attribute *attr, unsigned form,
43988095 18940 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 18941{
dee91e82 18942 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18943 struct dwarf2_per_objfile *dwarf2_per_objfile
18944 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18945 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18946 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18947 bfd *abfd = reader->abfd;
e7c27a73 18948 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18949 unsigned int bytes_read;
18950 struct dwarf_block *blk;
18951
aead7601 18952 attr->form = (enum dwarf_form) form;
a8329558 18953 switch (form)
c906108c 18954 {
c906108c 18955 case DW_FORM_ref_addr:
ae411497 18956 if (cu->header.version == 2)
4568ecf9 18957 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 18958 else
4568ecf9
DE
18959 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18960 &cu->header, &bytes_read);
ae411497
TT
18961 info_ptr += bytes_read;
18962 break;
36586728
TT
18963 case DW_FORM_GNU_ref_alt:
18964 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18965 info_ptr += bytes_read;
18966 break;
ae411497 18967 case DW_FORM_addr:
e7c27a73 18968 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 18969 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18970 info_ptr += bytes_read;
c906108c
SS
18971 break;
18972 case DW_FORM_block2:
7b5a2f43 18973 blk = dwarf_alloc_block (cu);
c906108c
SS
18974 blk->size = read_2_bytes (abfd, info_ptr);
18975 info_ptr += 2;
18976 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18977 info_ptr += blk->size;
18978 DW_BLOCK (attr) = blk;
18979 break;
18980 case DW_FORM_block4:
7b5a2f43 18981 blk = dwarf_alloc_block (cu);
c906108c
SS
18982 blk->size = read_4_bytes (abfd, info_ptr);
18983 info_ptr += 4;
18984 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18985 info_ptr += blk->size;
18986 DW_BLOCK (attr) = blk;
18987 break;
18988 case DW_FORM_data2:
18989 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18990 info_ptr += 2;
18991 break;
18992 case DW_FORM_data4:
18993 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18994 info_ptr += 4;
18995 break;
18996 case DW_FORM_data8:
18997 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18998 info_ptr += 8;
18999 break;
0224619f
JK
19000 case DW_FORM_data16:
19001 blk = dwarf_alloc_block (cu);
19002 blk->size = 16;
19003 blk->data = read_n_bytes (abfd, info_ptr, 16);
19004 info_ptr += 16;
19005 DW_BLOCK (attr) = blk;
19006 break;
2dc7f7b3
TT
19007 case DW_FORM_sec_offset:
19008 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19009 info_ptr += bytes_read;
19010 break;
c906108c 19011 case DW_FORM_string:
9b1c24c8 19012 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19013 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19014 info_ptr += bytes_read;
19015 break;
4bdf3d34 19016 case DW_FORM_strp:
36586728
TT
19017 if (!cu->per_cu->is_dwz)
19018 {
ed2dc618
SM
19019 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19020 abfd, info_ptr, cu_header,
36586728
TT
19021 &bytes_read);
19022 DW_STRING_IS_CANONICAL (attr) = 0;
19023 info_ptr += bytes_read;
19024 break;
19025 }
19026 /* FALLTHROUGH */
43988095
JK
19027 case DW_FORM_line_strp:
19028 if (!cu->per_cu->is_dwz)
19029 {
ed2dc618
SM
19030 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19031 abfd, info_ptr,
43988095
JK
19032 cu_header, &bytes_read);
19033 DW_STRING_IS_CANONICAL (attr) = 0;
19034 info_ptr += bytes_read;
19035 break;
19036 }
19037 /* FALLTHROUGH */
36586728
TT
19038 case DW_FORM_GNU_strp_alt:
19039 {
ed2dc618 19040 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19041 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19042 &bytes_read);
19043
ed2dc618
SM
19044 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19045 dwz, str_offset);
36586728
TT
19046 DW_STRING_IS_CANONICAL (attr) = 0;
19047 info_ptr += bytes_read;
19048 }
4bdf3d34 19049 break;
2dc7f7b3 19050 case DW_FORM_exprloc:
c906108c 19051 case DW_FORM_block:
7b5a2f43 19052 blk = dwarf_alloc_block (cu);
c906108c
SS
19053 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19054 info_ptr += bytes_read;
19055 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19056 info_ptr += blk->size;
19057 DW_BLOCK (attr) = blk;
19058 break;
19059 case DW_FORM_block1:
7b5a2f43 19060 blk = dwarf_alloc_block (cu);
c906108c
SS
19061 blk->size = read_1_byte (abfd, info_ptr);
19062 info_ptr += 1;
19063 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19064 info_ptr += blk->size;
19065 DW_BLOCK (attr) = blk;
19066 break;
19067 case DW_FORM_data1:
19068 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19069 info_ptr += 1;
19070 break;
19071 case DW_FORM_flag:
19072 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19073 info_ptr += 1;
19074 break;
2dc7f7b3
TT
19075 case DW_FORM_flag_present:
19076 DW_UNSND (attr) = 1;
19077 break;
c906108c
SS
19078 case DW_FORM_sdata:
19079 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19080 info_ptr += bytes_read;
19081 break;
19082 case DW_FORM_udata:
19083 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19084 info_ptr += bytes_read;
19085 break;
19086 case DW_FORM_ref1:
9c541725 19087 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19088 + read_1_byte (abfd, info_ptr));
c906108c
SS
19089 info_ptr += 1;
19090 break;
19091 case DW_FORM_ref2:
9c541725 19092 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19093 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19094 info_ptr += 2;
19095 break;
19096 case DW_FORM_ref4:
9c541725 19097 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19098 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19099 info_ptr += 4;
19100 break;
613e1657 19101 case DW_FORM_ref8:
9c541725 19102 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19103 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19104 info_ptr += 8;
19105 break;
55f1336d 19106 case DW_FORM_ref_sig8:
ac9ec31b 19107 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19108 info_ptr += 8;
19109 break;
c906108c 19110 case DW_FORM_ref_udata:
9c541725 19111 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19112 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19113 info_ptr += bytes_read;
19114 break;
c906108c 19115 case DW_FORM_indirect:
a8329558
KW
19116 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19117 info_ptr += bytes_read;
43988095
JK
19118 if (form == DW_FORM_implicit_const)
19119 {
19120 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19121 info_ptr += bytes_read;
19122 }
19123 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19124 info_ptr);
19125 break;
19126 case DW_FORM_implicit_const:
19127 DW_SND (attr) = implicit_const;
a8329558 19128 break;
3019eac3
DE
19129 case DW_FORM_GNU_addr_index:
19130 if (reader->dwo_file == NULL)
19131 {
19132 /* For now flag a hard error.
19133 Later we can turn this into a complaint. */
19134 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19135 dwarf_form_name (form),
19136 bfd_get_filename (abfd));
19137 }
19138 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19139 info_ptr += bytes_read;
19140 break;
19141 case DW_FORM_GNU_str_index:
19142 if (reader->dwo_file == NULL)
19143 {
19144 /* For now flag a hard error.
19145 Later we can turn this into a complaint if warranted. */
19146 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19147 dwarf_form_name (form),
19148 bfd_get_filename (abfd));
19149 }
19150 {
19151 ULONGEST str_index =
19152 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19153
342587c4 19154 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
19155 DW_STRING_IS_CANONICAL (attr) = 0;
19156 info_ptr += bytes_read;
19157 }
19158 break;
c906108c 19159 default:
8a3fe4f8 19160 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19161 dwarf_form_name (form),
19162 bfd_get_filename (abfd));
c906108c 19163 }
28e94949 19164
36586728 19165 /* Super hack. */
7771576e 19166 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19167 attr->form = DW_FORM_GNU_ref_alt;
19168
28e94949
JB
19169 /* We have seen instances where the compiler tried to emit a byte
19170 size attribute of -1 which ended up being encoded as an unsigned
19171 0xffffffff. Although 0xffffffff is technically a valid size value,
19172 an object of this size seems pretty unlikely so we can relatively
19173 safely treat these cases as if the size attribute was invalid and
19174 treat them as zero by default. */
19175 if (attr->name == DW_AT_byte_size
19176 && form == DW_FORM_data4
19177 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19178 {
19179 complaint
19180 (&symfile_complaints,
43bbcdc2
PH
19181 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19182 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19183 DW_UNSND (attr) = 0;
19184 }
28e94949 19185
c906108c
SS
19186 return info_ptr;
19187}
19188
a8329558
KW
19189/* Read an attribute described by an abbreviated attribute. */
19190
d521ce57 19191static const gdb_byte *
dee91e82
DE
19192read_attribute (const struct die_reader_specs *reader,
19193 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19194 const gdb_byte *info_ptr)
a8329558
KW
19195{
19196 attr->name = abbrev->name;
43988095
JK
19197 return read_attribute_value (reader, attr, abbrev->form,
19198 abbrev->implicit_const, info_ptr);
a8329558
KW
19199}
19200
0963b4bd 19201/* Read dwarf information from a buffer. */
c906108c
SS
19202
19203static unsigned int
a1855c1d 19204read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19205{
fe1b8b76 19206 return bfd_get_8 (abfd, buf);
c906108c
SS
19207}
19208
19209static int
a1855c1d 19210read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19211{
fe1b8b76 19212 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19213}
19214
19215static unsigned int
a1855c1d 19216read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19217{
fe1b8b76 19218 return bfd_get_16 (abfd, buf);
c906108c
SS
19219}
19220
21ae7a4d 19221static int
a1855c1d 19222read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19223{
19224 return bfd_get_signed_16 (abfd, buf);
19225}
19226
c906108c 19227static unsigned int
a1855c1d 19228read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19229{
fe1b8b76 19230 return bfd_get_32 (abfd, buf);
c906108c
SS
19231}
19232
21ae7a4d 19233static int
a1855c1d 19234read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19235{
19236 return bfd_get_signed_32 (abfd, buf);
19237}
19238
93311388 19239static ULONGEST
a1855c1d 19240read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19241{
fe1b8b76 19242 return bfd_get_64 (abfd, buf);
c906108c
SS
19243}
19244
19245static CORE_ADDR
d521ce57 19246read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19247 unsigned int *bytes_read)
c906108c 19248{
e7c27a73 19249 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19250 CORE_ADDR retval = 0;
19251
107d2387 19252 if (cu_header->signed_addr_p)
c906108c 19253 {
107d2387
AC
19254 switch (cu_header->addr_size)
19255 {
19256 case 2:
fe1b8b76 19257 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19258 break;
19259 case 4:
fe1b8b76 19260 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19261 break;
19262 case 8:
fe1b8b76 19263 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19264 break;
19265 default:
8e65ff28 19266 internal_error (__FILE__, __LINE__,
e2e0b3e5 19267 _("read_address: bad switch, signed [in module %s]"),
659b0389 19268 bfd_get_filename (abfd));
107d2387
AC
19269 }
19270 }
19271 else
19272 {
19273 switch (cu_header->addr_size)
19274 {
19275 case 2:
fe1b8b76 19276 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19277 break;
19278 case 4:
fe1b8b76 19279 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19280 break;
19281 case 8:
fe1b8b76 19282 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19283 break;
19284 default:
8e65ff28 19285 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19286 _("read_address: bad switch, "
19287 "unsigned [in module %s]"),
659b0389 19288 bfd_get_filename (abfd));
107d2387 19289 }
c906108c 19290 }
64367e0a 19291
107d2387
AC
19292 *bytes_read = cu_header->addr_size;
19293 return retval;
c906108c
SS
19294}
19295
f7ef9339 19296/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19297 specification allows the initial length to take up either 4 bytes
19298 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19299 bytes describe the length and all offsets will be 8 bytes in length
19300 instead of 4.
19301
f7ef9339
KB
19302 An older, non-standard 64-bit format is also handled by this
19303 function. The older format in question stores the initial length
19304 as an 8-byte quantity without an escape value. Lengths greater
19305 than 2^32 aren't very common which means that the initial 4 bytes
19306 is almost always zero. Since a length value of zero doesn't make
19307 sense for the 32-bit format, this initial zero can be considered to
19308 be an escape value which indicates the presence of the older 64-bit
19309 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19310 greater than 4GB. If it becomes necessary to handle lengths
19311 somewhat larger than 4GB, we could allow other small values (such
19312 as the non-sensical values of 1, 2, and 3) to also be used as
19313 escape values indicating the presence of the old format.
f7ef9339 19314
917c78fc
MK
19315 The value returned via bytes_read should be used to increment the
19316 relevant pointer after calling read_initial_length().
c764a876 19317
613e1657
KB
19318 [ Note: read_initial_length() and read_offset() are based on the
19319 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19320 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19321 from:
19322
f7ef9339 19323 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19324
613e1657
KB
19325 This document is only a draft and is subject to change. (So beware.)
19326
f7ef9339 19327 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19328 determined empirically by examining 64-bit ELF files produced by
19329 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19330
19331 - Kevin, July 16, 2002
613e1657
KB
19332 ] */
19333
19334static LONGEST
d521ce57 19335read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19336{
fe1b8b76 19337 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19338
dd373385 19339 if (length == 0xffffffff)
613e1657 19340 {
fe1b8b76 19341 length = bfd_get_64 (abfd, buf + 4);
613e1657 19342 *bytes_read = 12;
613e1657 19343 }
dd373385 19344 else if (length == 0)
f7ef9339 19345 {
dd373385 19346 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19347 length = bfd_get_64 (abfd, buf);
f7ef9339 19348 *bytes_read = 8;
f7ef9339 19349 }
613e1657
KB
19350 else
19351 {
19352 *bytes_read = 4;
613e1657
KB
19353 }
19354
c764a876
DE
19355 return length;
19356}
dd373385 19357
c764a876
DE
19358/* Cover function for read_initial_length.
19359 Returns the length of the object at BUF, and stores the size of the
19360 initial length in *BYTES_READ and stores the size that offsets will be in
19361 *OFFSET_SIZE.
19362 If the initial length size is not equivalent to that specified in
19363 CU_HEADER then issue a complaint.
19364 This is useful when reading non-comp-unit headers. */
dd373385 19365
c764a876 19366static LONGEST
d521ce57 19367read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19368 const struct comp_unit_head *cu_header,
19369 unsigned int *bytes_read,
19370 unsigned int *offset_size)
19371{
19372 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19373
19374 gdb_assert (cu_header->initial_length_size == 4
19375 || cu_header->initial_length_size == 8
19376 || cu_header->initial_length_size == 12);
19377
19378 if (cu_header->initial_length_size != *bytes_read)
19379 complaint (&symfile_complaints,
19380 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19381
c764a876 19382 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19383 return length;
613e1657
KB
19384}
19385
19386/* Read an offset from the data stream. The size of the offset is
917c78fc 19387 given by cu_header->offset_size. */
613e1657
KB
19388
19389static LONGEST
d521ce57
TT
19390read_offset (bfd *abfd, const gdb_byte *buf,
19391 const struct comp_unit_head *cu_header,
891d2f0b 19392 unsigned int *bytes_read)
c764a876
DE
19393{
19394 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19395
c764a876
DE
19396 *bytes_read = cu_header->offset_size;
19397 return offset;
19398}
19399
19400/* Read an offset from the data stream. */
19401
19402static LONGEST
d521ce57 19403read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19404{
19405 LONGEST retval = 0;
19406
c764a876 19407 switch (offset_size)
613e1657
KB
19408 {
19409 case 4:
fe1b8b76 19410 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19411 break;
19412 case 8:
fe1b8b76 19413 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19414 break;
19415 default:
8e65ff28 19416 internal_error (__FILE__, __LINE__,
c764a876 19417 _("read_offset_1: bad switch [in module %s]"),
659b0389 19418 bfd_get_filename (abfd));
613e1657
KB
19419 }
19420
917c78fc 19421 return retval;
613e1657
KB
19422}
19423
d521ce57
TT
19424static const gdb_byte *
19425read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19426{
19427 /* If the size of a host char is 8 bits, we can return a pointer
19428 to the buffer, otherwise we have to copy the data to a buffer
19429 allocated on the temporary obstack. */
4bdf3d34 19430 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19431 return buf;
c906108c
SS
19432}
19433
d521ce57
TT
19434static const char *
19435read_direct_string (bfd *abfd, const gdb_byte *buf,
19436 unsigned int *bytes_read_ptr)
c906108c
SS
19437{
19438 /* If the size of a host char is 8 bits, we can return a pointer
19439 to the string, otherwise we have to copy the string to a buffer
19440 allocated on the temporary obstack. */
4bdf3d34 19441 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19442 if (*buf == '\0')
19443 {
19444 *bytes_read_ptr = 1;
19445 return NULL;
19446 }
d521ce57
TT
19447 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19448 return (const char *) buf;
4bdf3d34
JJ
19449}
19450
43988095
JK
19451/* Return pointer to string at section SECT offset STR_OFFSET with error
19452 reporting strings FORM_NAME and SECT_NAME. */
19453
d521ce57 19454static const char *
ed2dc618
SM
19455read_indirect_string_at_offset_from (struct objfile *objfile,
19456 bfd *abfd, LONGEST str_offset,
43988095
JK
19457 struct dwarf2_section_info *sect,
19458 const char *form_name,
19459 const char *sect_name)
19460{
ed2dc618 19461 dwarf2_read_section (objfile, sect);
43988095
JK
19462 if (sect->buffer == NULL)
19463 error (_("%s used without %s section [in module %s]"),
19464 form_name, sect_name, bfd_get_filename (abfd));
19465 if (str_offset >= sect->size)
19466 error (_("%s pointing outside of %s section [in module %s]"),
19467 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19468 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19469 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19470 return NULL;
43988095
JK
19471 return (const char *) (sect->buffer + str_offset);
19472}
19473
19474/* Return pointer to string at .debug_str offset STR_OFFSET. */
19475
19476static const char *
ed2dc618
SM
19477read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19478 bfd *abfd, LONGEST str_offset)
43988095 19479{
ed2dc618
SM
19480 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19481 abfd, str_offset,
43988095
JK
19482 &dwarf2_per_objfile->str,
19483 "DW_FORM_strp", ".debug_str");
19484}
19485
19486/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19487
19488static const char *
ed2dc618
SM
19489read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19490 bfd *abfd, LONGEST str_offset)
43988095 19491{
ed2dc618
SM
19492 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19493 abfd, str_offset,
43988095
JK
19494 &dwarf2_per_objfile->line_str,
19495 "DW_FORM_line_strp",
19496 ".debug_line_str");
c906108c
SS
19497}
19498
36586728
TT
19499/* Read a string at offset STR_OFFSET in the .debug_str section from
19500 the .dwz file DWZ. Throw an error if the offset is too large. If
19501 the string consists of a single NUL byte, return NULL; otherwise
19502 return a pointer to the string. */
19503
d521ce57 19504static const char *
ed2dc618
SM
19505read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19506 LONGEST str_offset)
36586728 19507{
ed2dc618 19508 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19509
19510 if (dwz->str.buffer == NULL)
19511 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19512 "section [in module %s]"),
19513 bfd_get_filename (dwz->dwz_bfd));
19514 if (str_offset >= dwz->str.size)
19515 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19516 ".debug_str section [in module %s]"),
19517 bfd_get_filename (dwz->dwz_bfd));
19518 gdb_assert (HOST_CHAR_BIT == 8);
19519 if (dwz->str.buffer[str_offset] == '\0')
19520 return NULL;
d521ce57 19521 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19522}
19523
43988095
JK
19524/* Return pointer to string at .debug_str offset as read from BUF.
19525 BUF is assumed to be in a compilation unit described by CU_HEADER.
19526 Return *BYTES_READ_PTR count of bytes read from BUF. */
19527
d521ce57 19528static const char *
ed2dc618
SM
19529read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19530 const gdb_byte *buf,
cf2c3c16
TT
19531 const struct comp_unit_head *cu_header,
19532 unsigned int *bytes_read_ptr)
19533{
19534 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19535
ed2dc618 19536 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19537}
19538
43988095
JK
19539/* Return pointer to string at .debug_line_str offset as read from BUF.
19540 BUF is assumed to be in a compilation unit described by CU_HEADER.
19541 Return *BYTES_READ_PTR count of bytes read from BUF. */
19542
19543static const char *
ed2dc618
SM
19544read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19545 bfd *abfd, const gdb_byte *buf,
43988095
JK
19546 const struct comp_unit_head *cu_header,
19547 unsigned int *bytes_read_ptr)
19548{
19549 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19550
ed2dc618
SM
19551 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19552 str_offset);
43988095
JK
19553}
19554
19555ULONGEST
d521ce57 19556read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19557 unsigned int *bytes_read_ptr)
c906108c 19558{
12df843f 19559 ULONGEST result;
ce5d95e1 19560 unsigned int num_read;
870f88f7 19561 int shift;
c906108c
SS
19562 unsigned char byte;
19563
19564 result = 0;
19565 shift = 0;
19566 num_read = 0;
c906108c
SS
19567 while (1)
19568 {
fe1b8b76 19569 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19570 buf++;
19571 num_read++;
12df843f 19572 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19573 if ((byte & 128) == 0)
19574 {
19575 break;
19576 }
19577 shift += 7;
19578 }
19579 *bytes_read_ptr = num_read;
19580 return result;
19581}
19582
12df843f 19583static LONGEST
d521ce57
TT
19584read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19585 unsigned int *bytes_read_ptr)
c906108c 19586{
12df843f 19587 LONGEST result;
870f88f7 19588 int shift, num_read;
c906108c
SS
19589 unsigned char byte;
19590
19591 result = 0;
19592 shift = 0;
c906108c 19593 num_read = 0;
c906108c
SS
19594 while (1)
19595 {
fe1b8b76 19596 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19597 buf++;
19598 num_read++;
12df843f 19599 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
19600 shift += 7;
19601 if ((byte & 128) == 0)
19602 {
19603 break;
19604 }
19605 }
77e0b926 19606 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 19607 result |= -(((LONGEST) 1) << shift);
c906108c
SS
19608 *bytes_read_ptr = num_read;
19609 return result;
19610}
19611
3019eac3
DE
19612/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19613 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19614 ADDR_SIZE is the size of addresses from the CU header. */
19615
19616static CORE_ADDR
ed2dc618
SM
19617read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19618 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
19619{
19620 struct objfile *objfile = dwarf2_per_objfile->objfile;
19621 bfd *abfd = objfile->obfd;
19622 const gdb_byte *info_ptr;
19623
19624 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19625 if (dwarf2_per_objfile->addr.buffer == NULL)
19626 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19627 objfile_name (objfile));
3019eac3
DE
19628 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19629 error (_("DW_FORM_addr_index pointing outside of "
19630 ".debug_addr section [in module %s]"),
4262abfb 19631 objfile_name (objfile));
3019eac3
DE
19632 info_ptr = (dwarf2_per_objfile->addr.buffer
19633 + addr_base + addr_index * addr_size);
19634 if (addr_size == 4)
19635 return bfd_get_32 (abfd, info_ptr);
19636 else
19637 return bfd_get_64 (abfd, info_ptr);
19638}
19639
19640/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19641
19642static CORE_ADDR
19643read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19644{
518817b3
SM
19645 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19646 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19647}
19648
19649/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19650
19651static CORE_ADDR
d521ce57 19652read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19653 unsigned int *bytes_read)
19654{
518817b3 19655 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19656 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19657
19658 return read_addr_index (cu, addr_index);
19659}
19660
19661/* Data structure to pass results from dwarf2_read_addr_index_reader
19662 back to dwarf2_read_addr_index. */
19663
19664struct dwarf2_read_addr_index_data
19665{
19666 ULONGEST addr_base;
19667 int addr_size;
19668};
19669
19670/* die_reader_func for dwarf2_read_addr_index. */
19671
19672static void
19673dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19674 const gdb_byte *info_ptr,
3019eac3
DE
19675 struct die_info *comp_unit_die,
19676 int has_children,
19677 void *data)
19678{
19679 struct dwarf2_cu *cu = reader->cu;
19680 struct dwarf2_read_addr_index_data *aidata =
19681 (struct dwarf2_read_addr_index_data *) data;
19682
19683 aidata->addr_base = cu->addr_base;
19684 aidata->addr_size = cu->header.addr_size;
19685}
19686
19687/* Given an index in .debug_addr, fetch the value.
19688 NOTE: This can be called during dwarf expression evaluation,
19689 long after the debug information has been read, and thus per_cu->cu
19690 may no longer exist. */
19691
19692CORE_ADDR
19693dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19694 unsigned int addr_index)
19695{
ed2dc618
SM
19696 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19697 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
19698 struct dwarf2_cu *cu = per_cu->cu;
19699 ULONGEST addr_base;
19700 int addr_size;
19701
3019eac3
DE
19702 /* We need addr_base and addr_size.
19703 If we don't have PER_CU->cu, we have to get it.
19704 Nasty, but the alternative is storing the needed info in PER_CU,
19705 which at this point doesn't seem justified: it's not clear how frequently
19706 it would get used and it would increase the size of every PER_CU.
19707 Entry points like dwarf2_per_cu_addr_size do a similar thing
19708 so we're not in uncharted territory here.
19709 Alas we need to be a bit more complicated as addr_base is contained
19710 in the DIE.
19711
19712 We don't need to read the entire CU(/TU).
19713 We just need the header and top level die.
a1b64ce1 19714
3019eac3 19715 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19716 For now we skip this optimization. */
3019eac3
DE
19717
19718 if (cu != NULL)
19719 {
19720 addr_base = cu->addr_base;
19721 addr_size = cu->header.addr_size;
19722 }
19723 else
19724 {
19725 struct dwarf2_read_addr_index_data aidata;
19726
a1b64ce1
DE
19727 /* Note: We can't use init_cutu_and_read_dies_simple here,
19728 we need addr_base. */
19729 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19730 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
19731 addr_base = aidata.addr_base;
19732 addr_size = aidata.addr_size;
19733 }
19734
ed2dc618
SM
19735 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19736 addr_size);
3019eac3
DE
19737}
19738
57d63ce2
DE
19739/* Given a DW_FORM_GNU_str_index, fetch the string.
19740 This is only used by the Fission support. */
3019eac3 19741
d521ce57 19742static const char *
342587c4 19743read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 19744{
ed2dc618 19745 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19746 struct dwarf2_per_objfile *dwarf2_per_objfile
19747 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19748 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19749 const char *objf_name = objfile_name (objfile);
3019eac3 19750 bfd *abfd = objfile->obfd;
73869dc2
DE
19751 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19752 struct dwarf2_section_info *str_offsets_section =
19753 &reader->dwo_file->sections.str_offsets;
d521ce57 19754 const gdb_byte *info_ptr;
3019eac3 19755 ULONGEST str_offset;
57d63ce2 19756 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 19757
73869dc2
DE
19758 dwarf2_read_section (objfile, str_section);
19759 dwarf2_read_section (objfile, str_offsets_section);
19760 if (str_section->buffer == NULL)
57d63ce2 19761 error (_("%s used without .debug_str.dwo section"
9c541725
PA
19762 " in CU at offset 0x%x [in module %s]"),
19763 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19764 if (str_offsets_section->buffer == NULL)
57d63ce2 19765 error (_("%s used without .debug_str_offsets.dwo section"
9c541725
PA
19766 " in CU at offset 0x%x [in module %s]"),
19767 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19768 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 19769 error (_("%s pointing outside of .debug_str_offsets.dwo"
9c541725
PA
19770 " section in CU at offset 0x%x [in module %s]"),
19771 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19772 info_ptr = (str_offsets_section->buffer
3019eac3
DE
19773 + str_index * cu->header.offset_size);
19774 if (cu->header.offset_size == 4)
19775 str_offset = bfd_get_32 (abfd, info_ptr);
19776 else
19777 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19778 if (str_offset >= str_section->size)
57d63ce2 19779 error (_("Offset from %s pointing outside of"
9c541725
PA
19780 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
19781 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19782 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19783}
19784
3019eac3
DE
19785/* Return the length of an LEB128 number in BUF. */
19786
19787static int
19788leb128_size (const gdb_byte *buf)
19789{
19790 const gdb_byte *begin = buf;
19791 gdb_byte byte;
19792
19793 while (1)
19794 {
19795 byte = *buf++;
19796 if ((byte & 128) == 0)
19797 return buf - begin;
19798 }
19799}
19800
c906108c 19801static void
e142c38c 19802set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19803{
19804 switch (lang)
19805 {
19806 case DW_LANG_C89:
76bee0cc 19807 case DW_LANG_C99:
0cfd832f 19808 case DW_LANG_C11:
c906108c 19809 case DW_LANG_C:
d1be3247 19810 case DW_LANG_UPC:
e142c38c 19811 cu->language = language_c;
c906108c 19812 break;
9c37b5ae 19813 case DW_LANG_Java:
c906108c 19814 case DW_LANG_C_plus_plus:
0cfd832f
MW
19815 case DW_LANG_C_plus_plus_11:
19816 case DW_LANG_C_plus_plus_14:
e142c38c 19817 cu->language = language_cplus;
c906108c 19818 break;
6aecb9c2
JB
19819 case DW_LANG_D:
19820 cu->language = language_d;
19821 break;
c906108c
SS
19822 case DW_LANG_Fortran77:
19823 case DW_LANG_Fortran90:
b21b22e0 19824 case DW_LANG_Fortran95:
f7de9aab
MW
19825 case DW_LANG_Fortran03:
19826 case DW_LANG_Fortran08:
e142c38c 19827 cu->language = language_fortran;
c906108c 19828 break;
a766d390
DE
19829 case DW_LANG_Go:
19830 cu->language = language_go;
19831 break;
c906108c 19832 case DW_LANG_Mips_Assembler:
e142c38c 19833 cu->language = language_asm;
c906108c
SS
19834 break;
19835 case DW_LANG_Ada83:
8aaf0b47 19836 case DW_LANG_Ada95:
bc5f45f8
JB
19837 cu->language = language_ada;
19838 break;
72019c9c
GM
19839 case DW_LANG_Modula2:
19840 cu->language = language_m2;
19841 break;
fe8e67fd
PM
19842 case DW_LANG_Pascal83:
19843 cu->language = language_pascal;
19844 break;
22566fbd
DJ
19845 case DW_LANG_ObjC:
19846 cu->language = language_objc;
19847 break;
c44af4eb
TT
19848 case DW_LANG_Rust:
19849 case DW_LANG_Rust_old:
19850 cu->language = language_rust;
19851 break;
c906108c
SS
19852 case DW_LANG_Cobol74:
19853 case DW_LANG_Cobol85:
c906108c 19854 default:
e142c38c 19855 cu->language = language_minimal;
c906108c
SS
19856 break;
19857 }
e142c38c 19858 cu->language_defn = language_def (cu->language);
c906108c
SS
19859}
19860
19861/* Return the named attribute or NULL if not there. */
19862
19863static struct attribute *
e142c38c 19864dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19865{
a48e046c 19866 for (;;)
c906108c 19867 {
a48e046c
TT
19868 unsigned int i;
19869 struct attribute *spec = NULL;
19870
19871 for (i = 0; i < die->num_attrs; ++i)
19872 {
19873 if (die->attrs[i].name == name)
19874 return &die->attrs[i];
19875 if (die->attrs[i].name == DW_AT_specification
19876 || die->attrs[i].name == DW_AT_abstract_origin)
19877 spec = &die->attrs[i];
19878 }
19879
19880 if (!spec)
19881 break;
c906108c 19882
f2f0e013 19883 die = follow_die_ref (die, spec, &cu);
f2f0e013 19884 }
c5aa993b 19885
c906108c
SS
19886 return NULL;
19887}
19888
348e048f
DE
19889/* Return the named attribute or NULL if not there,
19890 but do not follow DW_AT_specification, etc.
19891 This is for use in contexts where we're reading .debug_types dies.
19892 Following DW_AT_specification, DW_AT_abstract_origin will take us
19893 back up the chain, and we want to go down. */
19894
19895static struct attribute *
45e58e77 19896dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19897{
19898 unsigned int i;
19899
19900 for (i = 0; i < die->num_attrs; ++i)
19901 if (die->attrs[i].name == name)
19902 return &die->attrs[i];
19903
19904 return NULL;
19905}
19906
7d45c7c3
KB
19907/* Return the string associated with a string-typed attribute, or NULL if it
19908 is either not found or is of an incorrect type. */
19909
19910static const char *
19911dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19912{
19913 struct attribute *attr;
19914 const char *str = NULL;
19915
19916 attr = dwarf2_attr (die, name, cu);
19917
19918 if (attr != NULL)
19919 {
43988095 19920 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
19921 || attr->form == DW_FORM_string
19922 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19923 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19924 str = DW_STRING (attr);
19925 else
19926 complaint (&symfile_complaints,
19927 _("string type expected for attribute %s for "
19928 "DIE at 0x%x in module %s"),
9c541725 19929 dwarf_attr_name (name), to_underlying (die->sect_off),
518817b3 19930 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19931 }
19932
19933 return str;
19934}
19935
05cf31d1
JB
19936/* Return non-zero iff the attribute NAME is defined for the given DIE,
19937 and holds a non-zero value. This function should only be used for
2dc7f7b3 19938 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19939
19940static int
19941dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19942{
19943 struct attribute *attr = dwarf2_attr (die, name, cu);
19944
19945 return (attr && DW_UNSND (attr));
19946}
19947
3ca72b44 19948static int
e142c38c 19949die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19950{
05cf31d1
JB
19951 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19952 which value is non-zero. However, we have to be careful with
19953 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19954 (via dwarf2_flag_true_p) follows this attribute. So we may
19955 end up accidently finding a declaration attribute that belongs
19956 to a different DIE referenced by the specification attribute,
19957 even though the given DIE does not have a declaration attribute. */
19958 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19959 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19960}
19961
63d06c5c 19962/* Return the die giving the specification for DIE, if there is
f2f0e013 19963 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19964 containing the return value on output. If there is no
19965 specification, but there is an abstract origin, that is
19966 returned. */
63d06c5c
DC
19967
19968static struct die_info *
f2f0e013 19969die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19970{
f2f0e013
DJ
19971 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19972 *spec_cu);
63d06c5c 19973
edb3359d
DJ
19974 if (spec_attr == NULL)
19975 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19976
63d06c5c
DC
19977 if (spec_attr == NULL)
19978 return NULL;
19979 else
f2f0e013 19980 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19981}
c906108c 19982
527f3840
JK
19983/* Stub for free_line_header to match void * callback types. */
19984
19985static void
19986free_line_header_voidp (void *arg)
19987{
9a3c8263 19988 struct line_header *lh = (struct line_header *) arg;
527f3840 19989
fff8551c 19990 delete lh;
527f3840
JK
19991}
19992
fff8551c
PA
19993void
19994line_header::add_include_dir (const char *include_dir)
c906108c 19995{
27e0867f 19996 if (dwarf_line_debug >= 2)
fff8551c
PA
19997 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19998 include_dirs.size () + 1, include_dir);
27e0867f 19999
fff8551c 20000 include_dirs.push_back (include_dir);
debd256d 20001}
6e70227d 20002
fff8551c
PA
20003void
20004line_header::add_file_name (const char *name,
ecfb656c 20005 dir_index d_index,
fff8551c
PA
20006 unsigned int mod_time,
20007 unsigned int length)
debd256d 20008{
27e0867f
DE
20009 if (dwarf_line_debug >= 2)
20010 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 20011 (unsigned) file_names.size () + 1, name);
27e0867f 20012
ecfb656c 20013 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20014}
6e70227d 20015
83769d0b 20016/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20017
20018static struct dwarf2_section_info *
20019get_debug_line_section (struct dwarf2_cu *cu)
20020{
20021 struct dwarf2_section_info *section;
518817b3
SM
20022 struct dwarf2_per_objfile *dwarf2_per_objfile
20023 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20024
20025 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20026 DWO file. */
20027 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20028 section = &cu->dwo_unit->dwo_file->sections.line;
20029 else if (cu->per_cu->is_dwz)
20030 {
ed2dc618 20031 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20032
20033 section = &dwz->line;
20034 }
20035 else
20036 section = &dwarf2_per_objfile->line;
20037
20038 return section;
20039}
20040
43988095
JK
20041/* Read directory or file name entry format, starting with byte of
20042 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20043 entries count and the entries themselves in the described entry
20044 format. */
20045
20046static void
ed2dc618
SM
20047read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20048 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20049 struct line_header *lh,
20050 const struct comp_unit_head *cu_header,
20051 void (*callback) (struct line_header *lh,
20052 const char *name,
ecfb656c 20053 dir_index d_index,
43988095
JK
20054 unsigned int mod_time,
20055 unsigned int length))
20056{
20057 gdb_byte format_count, formati;
20058 ULONGEST data_count, datai;
20059 const gdb_byte *buf = *bufp;
20060 const gdb_byte *format_header_data;
43988095
JK
20061 unsigned int bytes_read;
20062
20063 format_count = read_1_byte (abfd, buf);
20064 buf += 1;
20065 format_header_data = buf;
20066 for (formati = 0; formati < format_count; formati++)
20067 {
20068 read_unsigned_leb128 (abfd, buf, &bytes_read);
20069 buf += bytes_read;
20070 read_unsigned_leb128 (abfd, buf, &bytes_read);
20071 buf += bytes_read;
20072 }
20073
20074 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20075 buf += bytes_read;
20076 for (datai = 0; datai < data_count; datai++)
20077 {
20078 const gdb_byte *format = format_header_data;
20079 struct file_entry fe;
20080
43988095
JK
20081 for (formati = 0; formati < format_count; formati++)
20082 {
ecfb656c 20083 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20084 format += bytes_read;
43988095 20085
ecfb656c 20086 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20087 format += bytes_read;
ecfb656c
PA
20088
20089 gdb::optional<const char *> string;
20090 gdb::optional<unsigned int> uint;
20091
43988095
JK
20092 switch (form)
20093 {
20094 case DW_FORM_string:
ecfb656c 20095 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20096 buf += bytes_read;
20097 break;
20098
20099 case DW_FORM_line_strp:
ed2dc618
SM
20100 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20101 abfd, buf,
ecfb656c
PA
20102 cu_header,
20103 &bytes_read));
43988095
JK
20104 buf += bytes_read;
20105 break;
20106
20107 case DW_FORM_data1:
ecfb656c 20108 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20109 buf += 1;
20110 break;
20111
20112 case DW_FORM_data2:
ecfb656c 20113 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20114 buf += 2;
20115 break;
20116
20117 case DW_FORM_data4:
ecfb656c 20118 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20119 buf += 4;
20120 break;
20121
20122 case DW_FORM_data8:
ecfb656c 20123 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20124 buf += 8;
20125 break;
20126
20127 case DW_FORM_udata:
ecfb656c 20128 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20129 buf += bytes_read;
20130 break;
20131
20132 case DW_FORM_block:
20133 /* It is valid only for DW_LNCT_timestamp which is ignored by
20134 current GDB. */
20135 break;
20136 }
ecfb656c
PA
20137
20138 switch (content_type)
20139 {
20140 case DW_LNCT_path:
20141 if (string.has_value ())
20142 fe.name = *string;
20143 break;
20144 case DW_LNCT_directory_index:
20145 if (uint.has_value ())
20146 fe.d_index = (dir_index) *uint;
20147 break;
20148 case DW_LNCT_timestamp:
20149 if (uint.has_value ())
20150 fe.mod_time = *uint;
20151 break;
20152 case DW_LNCT_size:
20153 if (uint.has_value ())
20154 fe.length = *uint;
20155 break;
20156 case DW_LNCT_MD5:
20157 break;
20158 default:
20159 complaint (&symfile_complaints,
20160 _("Unknown format content type %s"),
20161 pulongest (content_type));
20162 }
43988095
JK
20163 }
20164
ecfb656c 20165 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20166 }
20167
20168 *bufp = buf;
20169}
20170
debd256d 20171/* Read the statement program header starting at OFFSET in
3019eac3 20172 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20173 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20174 Returns NULL if there is a problem reading the header, e.g., if it
20175 has a version we don't understand.
debd256d
JB
20176
20177 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20178 the returned object point into the dwarf line section buffer,
20179 and must not be freed. */
ae2de4f8 20180
fff8551c 20181static line_header_up
9c541725 20182dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20183{
d521ce57 20184 const gdb_byte *line_ptr;
c764a876 20185 unsigned int bytes_read, offset_size;
debd256d 20186 int i;
d521ce57 20187 const char *cur_dir, *cur_file;
3019eac3
DE
20188 struct dwarf2_section_info *section;
20189 bfd *abfd;
518817b3
SM
20190 struct dwarf2_per_objfile *dwarf2_per_objfile
20191 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20192
36586728 20193 section = get_debug_line_section (cu);
3019eac3
DE
20194 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20195 if (section->buffer == NULL)
debd256d 20196 {
3019eac3
DE
20197 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20198 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20199 else
20200 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
20201 return 0;
20202 }
20203
fceca515
DE
20204 /* We can't do this until we know the section is non-empty.
20205 Only then do we know we have such a section. */
a32a8923 20206 abfd = get_section_bfd_owner (section);
fceca515 20207
a738430d
MK
20208 /* Make sure that at least there's room for the total_length field.
20209 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20210 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20211 {
4d3c2250 20212 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20213 return 0;
20214 }
20215
fff8551c 20216 line_header_up lh (new line_header ());
debd256d 20217
9c541725 20218 lh->sect_off = sect_off;
527f3840
JK
20219 lh->offset_in_dwz = cu->per_cu->is_dwz;
20220
9c541725 20221 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20222
a738430d 20223 /* Read in the header. */
6e70227d 20224 lh->total_length =
c764a876
DE
20225 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20226 &bytes_read, &offset_size);
debd256d 20227 line_ptr += bytes_read;
3019eac3 20228 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20229 {
4d3c2250 20230 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20231 return 0;
20232 }
20233 lh->statement_program_end = line_ptr + lh->total_length;
20234 lh->version = read_2_bytes (abfd, line_ptr);
20235 line_ptr += 2;
43988095 20236 if (lh->version > 5)
cd366ee8
DE
20237 {
20238 /* This is a version we don't understand. The format could have
20239 changed in ways we don't handle properly so just punt. */
20240 complaint (&symfile_complaints,
20241 _("unsupported version in .debug_line section"));
20242 return NULL;
20243 }
43988095
JK
20244 if (lh->version >= 5)
20245 {
20246 gdb_byte segment_selector_size;
20247
20248 /* Skip address size. */
20249 read_1_byte (abfd, line_ptr);
20250 line_ptr += 1;
20251
20252 segment_selector_size = read_1_byte (abfd, line_ptr);
20253 line_ptr += 1;
20254 if (segment_selector_size != 0)
20255 {
20256 complaint (&symfile_complaints,
20257 _("unsupported segment selector size %u "
20258 "in .debug_line section"),
20259 segment_selector_size);
20260 return NULL;
20261 }
20262 }
c764a876
DE
20263 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20264 line_ptr += offset_size;
debd256d
JB
20265 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20266 line_ptr += 1;
2dc7f7b3
TT
20267 if (lh->version >= 4)
20268 {
20269 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20270 line_ptr += 1;
20271 }
20272 else
20273 lh->maximum_ops_per_instruction = 1;
20274
20275 if (lh->maximum_ops_per_instruction == 0)
20276 {
20277 lh->maximum_ops_per_instruction = 1;
20278 complaint (&symfile_complaints,
3e43a32a
MS
20279 _("invalid maximum_ops_per_instruction "
20280 "in `.debug_line' section"));
2dc7f7b3
TT
20281 }
20282
debd256d
JB
20283 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20284 line_ptr += 1;
20285 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20286 line_ptr += 1;
20287 lh->line_range = read_1_byte (abfd, line_ptr);
20288 line_ptr += 1;
20289 lh->opcode_base = read_1_byte (abfd, line_ptr);
20290 line_ptr += 1;
fff8551c 20291 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20292
20293 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20294 for (i = 1; i < lh->opcode_base; ++i)
20295 {
20296 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20297 line_ptr += 1;
20298 }
20299
43988095 20300 if (lh->version >= 5)
debd256d 20301 {
43988095 20302 /* Read directory table. */
ed2dc618
SM
20303 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20304 &cu->header,
fff8551c 20305 [] (struct line_header *lh, const char *name,
ecfb656c 20306 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20307 unsigned int length)
20308 {
20309 lh->add_include_dir (name);
20310 });
debd256d 20311
43988095 20312 /* Read file name table. */
ed2dc618
SM
20313 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20314 &cu->header,
fff8551c 20315 [] (struct line_header *lh, const char *name,
ecfb656c 20316 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20317 unsigned int length)
20318 {
ecfb656c 20319 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 20320 });
43988095
JK
20321 }
20322 else
debd256d 20323 {
43988095
JK
20324 /* Read directory table. */
20325 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20326 {
20327 line_ptr += bytes_read;
fff8551c 20328 lh->add_include_dir (cur_dir);
43988095 20329 }
debd256d
JB
20330 line_ptr += bytes_read;
20331
43988095
JK
20332 /* Read file name table. */
20333 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20334 {
ecfb656c
PA
20335 unsigned int mod_time, length;
20336 dir_index d_index;
43988095
JK
20337
20338 line_ptr += bytes_read;
ecfb656c 20339 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20340 line_ptr += bytes_read;
20341 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20342 line_ptr += bytes_read;
20343 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20344 line_ptr += bytes_read;
20345
ecfb656c 20346 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20347 }
20348 line_ptr += bytes_read;
debd256d 20349 }
6e70227d 20350 lh->statement_program_start = line_ptr;
debd256d 20351
3019eac3 20352 if (line_ptr > (section->buffer + section->size))
4d3c2250 20353 complaint (&symfile_complaints,
3e43a32a
MS
20354 _("line number info header doesn't "
20355 "fit in `.debug_line' section"));
debd256d 20356
debd256d
JB
20357 return lh;
20358}
c906108c 20359
c6da4cef
DE
20360/* Subroutine of dwarf_decode_lines to simplify it.
20361 Return the file name of the psymtab for included file FILE_INDEX
20362 in line header LH of PST.
20363 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20364 If space for the result is malloc'd, *NAME_HOLDER will be set.
20365 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20366
d521ce57 20367static const char *
c6da4cef
DE
20368psymtab_include_file_name (const struct line_header *lh, int file_index,
20369 const struct partial_symtab *pst,
c89b44cd
TT
20370 const char *comp_dir,
20371 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20372{
8c43009f 20373 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20374 const char *include_name = fe.name;
20375 const char *include_name_to_compare = include_name;
72b9f47f 20376 const char *pst_filename;
c6da4cef
DE
20377 int file_is_pst;
20378
8c43009f 20379 const char *dir_name = fe.include_dir (lh);
c6da4cef 20380
c89b44cd 20381 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20382 if (!IS_ABSOLUTE_PATH (include_name)
20383 && (dir_name != NULL || comp_dir != NULL))
20384 {
20385 /* Avoid creating a duplicate psymtab for PST.
20386 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20387 Before we do the comparison, however, we need to account
20388 for DIR_NAME and COMP_DIR.
20389 First prepend dir_name (if non-NULL). If we still don't
20390 have an absolute path prepend comp_dir (if non-NULL).
20391 However, the directory we record in the include-file's
20392 psymtab does not contain COMP_DIR (to match the
20393 corresponding symtab(s)).
20394
20395 Example:
20396
20397 bash$ cd /tmp
20398 bash$ gcc -g ./hello.c
20399 include_name = "hello.c"
20400 dir_name = "."
20401 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20402 DW_AT_name = "./hello.c"
20403
20404 */
c6da4cef
DE
20405
20406 if (dir_name != NULL)
20407 {
c89b44cd
TT
20408 name_holder->reset (concat (dir_name, SLASH_STRING,
20409 include_name, (char *) NULL));
20410 include_name = name_holder->get ();
c6da4cef 20411 include_name_to_compare = include_name;
c6da4cef
DE
20412 }
20413 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20414 {
c89b44cd
TT
20415 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20416 include_name, (char *) NULL));
20417 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20418 }
20419 }
20420
20421 pst_filename = pst->filename;
c89b44cd 20422 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20423 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20424 {
c89b44cd
TT
20425 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20426 pst_filename, (char *) NULL));
20427 pst_filename = copied_name.get ();
c6da4cef
DE
20428 }
20429
1e3fad37 20430 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20431
c6da4cef
DE
20432 if (file_is_pst)
20433 return NULL;
20434 return include_name;
20435}
20436
d9b3de22
DE
20437/* State machine to track the state of the line number program. */
20438
6f77053d 20439class lnp_state_machine
d9b3de22 20440{
6f77053d
PA
20441public:
20442 /* Initialize a machine state for the start of a line number
20443 program. */
20444 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20445
8c43009f
PA
20446 file_entry *current_file ()
20447 {
20448 /* lh->file_names is 0-based, but the file name numbers in the
20449 statement program are 1-based. */
6f77053d
PA
20450 return m_line_header->file_name_at (m_file);
20451 }
20452
20453 /* Record the line in the state machine. END_SEQUENCE is true if
20454 we're processing the end of a sequence. */
20455 void record_line (bool end_sequence);
20456
20457 /* Check address and if invalid nop-out the rest of the lines in this
20458 sequence. */
20459 void check_line_address (struct dwarf2_cu *cu,
20460 const gdb_byte *line_ptr,
20461 CORE_ADDR lowpc, CORE_ADDR address);
20462
20463 void handle_set_discriminator (unsigned int discriminator)
20464 {
20465 m_discriminator = discriminator;
20466 m_line_has_non_zero_discriminator |= discriminator != 0;
20467 }
20468
20469 /* Handle DW_LNE_set_address. */
20470 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20471 {
20472 m_op_index = 0;
20473 address += baseaddr;
20474 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20475 }
20476
20477 /* Handle DW_LNS_advance_pc. */
20478 void handle_advance_pc (CORE_ADDR adjust);
20479
20480 /* Handle a special opcode. */
20481 void handle_special_opcode (unsigned char op_code);
20482
20483 /* Handle DW_LNS_advance_line. */
20484 void handle_advance_line (int line_delta)
20485 {
20486 advance_line (line_delta);
20487 }
20488
20489 /* Handle DW_LNS_set_file. */
20490 void handle_set_file (file_name_index file);
20491
20492 /* Handle DW_LNS_negate_stmt. */
20493 void handle_negate_stmt ()
20494 {
20495 m_is_stmt = !m_is_stmt;
20496 }
20497
20498 /* Handle DW_LNS_const_add_pc. */
20499 void handle_const_add_pc ();
20500
20501 /* Handle DW_LNS_fixed_advance_pc. */
20502 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20503 {
20504 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20505 m_op_index = 0;
20506 }
20507
20508 /* Handle DW_LNS_copy. */
20509 void handle_copy ()
20510 {
20511 record_line (false);
20512 m_discriminator = 0;
20513 }
20514
20515 /* Handle DW_LNE_end_sequence. */
20516 void handle_end_sequence ()
20517 {
20518 m_record_line_callback = ::record_line;
20519 }
20520
20521private:
20522 /* Advance the line by LINE_DELTA. */
20523 void advance_line (int line_delta)
20524 {
20525 m_line += line_delta;
20526
20527 if (line_delta != 0)
20528 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20529 }
20530
6f77053d
PA
20531 gdbarch *m_gdbarch;
20532
20533 /* True if we're recording lines.
20534 Otherwise we're building partial symtabs and are just interested in
20535 finding include files mentioned by the line number program. */
20536 bool m_record_lines_p;
20537
8c43009f 20538 /* The line number header. */
6f77053d 20539 line_header *m_line_header;
8c43009f 20540
6f77053d
PA
20541 /* These are part of the standard DWARF line number state machine,
20542 and initialized according to the DWARF spec. */
d9b3de22 20543
6f77053d 20544 unsigned char m_op_index = 0;
8c43009f 20545 /* The line table index (1-based) of the current file. */
6f77053d
PA
20546 file_name_index m_file = (file_name_index) 1;
20547 unsigned int m_line = 1;
20548
20549 /* These are initialized in the constructor. */
20550
20551 CORE_ADDR m_address;
20552 bool m_is_stmt;
20553 unsigned int m_discriminator;
d9b3de22
DE
20554
20555 /* Additional bits of state we need to track. */
20556
20557 /* The last file that we called dwarf2_start_subfile for.
20558 This is only used for TLLs. */
6f77053d 20559 unsigned int m_last_file = 0;
d9b3de22 20560 /* The last file a line number was recorded for. */
6f77053d 20561 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
20562
20563 /* The function to call to record a line. */
6f77053d 20564 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
20565
20566 /* The last line number that was recorded, used to coalesce
20567 consecutive entries for the same line. This can happen, for
20568 example, when discriminators are present. PR 17276. */
6f77053d
PA
20569 unsigned int m_last_line = 0;
20570 bool m_line_has_non_zero_discriminator = false;
8c43009f 20571};
d9b3de22 20572
6f77053d
PA
20573void
20574lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20575{
20576 CORE_ADDR addr_adj = (((m_op_index + adjust)
20577 / m_line_header->maximum_ops_per_instruction)
20578 * m_line_header->minimum_instruction_length);
20579 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20580 m_op_index = ((m_op_index + adjust)
20581 % m_line_header->maximum_ops_per_instruction);
20582}
d9b3de22 20583
6f77053d
PA
20584void
20585lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20586{
6f77053d
PA
20587 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20588 CORE_ADDR addr_adj = (((m_op_index
20589 + (adj_opcode / m_line_header->line_range))
20590 / m_line_header->maximum_ops_per_instruction)
20591 * m_line_header->minimum_instruction_length);
20592 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20593 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20594 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20595
6f77053d
PA
20596 int line_delta = (m_line_header->line_base
20597 + (adj_opcode % m_line_header->line_range));
20598 advance_line (line_delta);
20599 record_line (false);
20600 m_discriminator = 0;
20601}
d9b3de22 20602
6f77053d
PA
20603void
20604lnp_state_machine::handle_set_file (file_name_index file)
20605{
20606 m_file = file;
20607
20608 const file_entry *fe = current_file ();
20609 if (fe == NULL)
20610 dwarf2_debug_line_missing_file_complaint ();
20611 else if (m_record_lines_p)
20612 {
20613 const char *dir = fe->include_dir (m_line_header);
20614
20615 m_last_subfile = current_subfile;
20616 m_line_has_non_zero_discriminator = m_discriminator != 0;
20617 dwarf2_start_subfile (fe->name, dir);
20618 }
20619}
20620
20621void
20622lnp_state_machine::handle_const_add_pc ()
20623{
20624 CORE_ADDR adjust
20625 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20626
20627 CORE_ADDR addr_adj
20628 = (((m_op_index + adjust)
20629 / m_line_header->maximum_ops_per_instruction)
20630 * m_line_header->minimum_instruction_length);
20631
20632 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20633 m_op_index = ((m_op_index + adjust)
20634 % m_line_header->maximum_ops_per_instruction);
20635}
d9b3de22 20636
c91513d8
PP
20637/* Ignore this record_line request. */
20638
20639static void
20640noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20641{
20642 return;
20643}
20644
a05a36a5
DE
20645/* Return non-zero if we should add LINE to the line number table.
20646 LINE is the line to add, LAST_LINE is the last line that was added,
20647 LAST_SUBFILE is the subfile for LAST_LINE.
20648 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20649 had a non-zero discriminator.
20650
20651 We have to be careful in the presence of discriminators.
20652 E.g., for this line:
20653
20654 for (i = 0; i < 100000; i++);
20655
20656 clang can emit four line number entries for that one line,
20657 each with a different discriminator.
20658 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20659
20660 However, we want gdb to coalesce all four entries into one.
20661 Otherwise the user could stepi into the middle of the line and
20662 gdb would get confused about whether the pc really was in the
20663 middle of the line.
20664
20665 Things are further complicated by the fact that two consecutive
20666 line number entries for the same line is a heuristic used by gcc
20667 to denote the end of the prologue. So we can't just discard duplicate
20668 entries, we have to be selective about it. The heuristic we use is
20669 that we only collapse consecutive entries for the same line if at least
20670 one of those entries has a non-zero discriminator. PR 17276.
20671
20672 Note: Addresses in the line number state machine can never go backwards
20673 within one sequence, thus this coalescing is ok. */
20674
20675static int
20676dwarf_record_line_p (unsigned int line, unsigned int last_line,
20677 int line_has_non_zero_discriminator,
20678 struct subfile *last_subfile)
20679{
20680 if (current_subfile != last_subfile)
20681 return 1;
20682 if (line != last_line)
20683 return 1;
20684 /* Same line for the same file that we've seen already.
20685 As a last check, for pr 17276, only record the line if the line
20686 has never had a non-zero discriminator. */
20687 if (!line_has_non_zero_discriminator)
20688 return 1;
20689 return 0;
20690}
20691
252a6764
DE
20692/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20693 in the line table of subfile SUBFILE. */
20694
20695static void
d9b3de22
DE
20696dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20697 unsigned int line, CORE_ADDR address,
20698 record_line_ftype p_record_line)
252a6764
DE
20699{
20700 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20701
27e0867f
DE
20702 if (dwarf_line_debug)
20703 {
20704 fprintf_unfiltered (gdb_stdlog,
20705 "Recording line %u, file %s, address %s\n",
20706 line, lbasename (subfile->name),
20707 paddress (gdbarch, address));
20708 }
20709
d5962de5 20710 (*p_record_line) (subfile, line, addr);
252a6764
DE
20711}
20712
20713/* Subroutine of dwarf_decode_lines_1 to simplify it.
20714 Mark the end of a set of line number records.
d9b3de22 20715 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20716 If SUBFILE is NULL the request is ignored. */
20717
20718static void
20719dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20720 CORE_ADDR address, record_line_ftype p_record_line)
20721{
27e0867f
DE
20722 if (subfile == NULL)
20723 return;
20724
20725 if (dwarf_line_debug)
20726 {
20727 fprintf_unfiltered (gdb_stdlog,
20728 "Finishing current line, file %s, address %s\n",
20729 lbasename (subfile->name),
20730 paddress (gdbarch, address));
20731 }
20732
d9b3de22
DE
20733 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20734}
20735
6f77053d
PA
20736void
20737lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20738{
d9b3de22
DE
20739 if (dwarf_line_debug)
20740 {
20741 fprintf_unfiltered (gdb_stdlog,
20742 "Processing actual line %u: file %u,"
20743 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
20744 m_line, to_underlying (m_file),
20745 paddress (m_gdbarch, m_address),
20746 m_is_stmt, m_discriminator);
d9b3de22
DE
20747 }
20748
6f77053d 20749 file_entry *fe = current_file ();
8c43009f
PA
20750
20751 if (fe == NULL)
d9b3de22
DE
20752 dwarf2_debug_line_missing_file_complaint ();
20753 /* For now we ignore lines not starting on an instruction boundary.
20754 But not when processing end_sequence for compatibility with the
20755 previous version of the code. */
6f77053d 20756 else if (m_op_index == 0 || end_sequence)
d9b3de22 20757 {
8c43009f 20758 fe->included_p = 1;
6f77053d 20759 if (m_record_lines_p && m_is_stmt)
d9b3de22 20760 {
6f77053d 20761 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 20762 {
6f77053d
PA
20763 dwarf_finish_line (m_gdbarch, m_last_subfile,
20764 m_address, m_record_line_callback);
d9b3de22
DE
20765 }
20766
20767 if (!end_sequence)
20768 {
6f77053d
PA
20769 if (dwarf_record_line_p (m_line, m_last_line,
20770 m_line_has_non_zero_discriminator,
20771 m_last_subfile))
d9b3de22 20772 {
6f77053d
PA
20773 dwarf_record_line_1 (m_gdbarch, current_subfile,
20774 m_line, m_address,
20775 m_record_line_callback);
d9b3de22 20776 }
6f77053d
PA
20777 m_last_subfile = current_subfile;
20778 m_last_line = m_line;
d9b3de22
DE
20779 }
20780 }
20781 }
20782}
20783
6f77053d
PA
20784lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20785 bool record_lines_p)
d9b3de22 20786{
6f77053d
PA
20787 m_gdbarch = arch;
20788 m_record_lines_p = record_lines_p;
20789 m_line_header = lh;
d9b3de22 20790
6f77053d 20791 m_record_line_callback = ::record_line;
d9b3de22 20792
d9b3de22
DE
20793 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20794 was a line entry for it so that the backend has a chance to adjust it
20795 and also record it in case it needs it. This is currently used by MIPS
20796 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20797 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20798 m_is_stmt = lh->default_is_stmt;
20799 m_discriminator = 0;
252a6764
DE
20800}
20801
6f77053d
PA
20802void
20803lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20804 const gdb_byte *line_ptr,
20805 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
20806{
20807 /* If address < lowpc then it's not a usable value, it's outside the
20808 pc range of the CU. However, we restrict the test to only address
20809 values of zero to preserve GDB's previous behaviour which is to
20810 handle the specific case of a function being GC'd by the linker. */
20811
20812 if (address == 0 && address < lowpc)
20813 {
20814 /* This line table is for a function which has been
20815 GCd by the linker. Ignore it. PR gdb/12528 */
20816
518817b3 20817 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20818 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20819
20820 complaint (&symfile_complaints,
20821 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20822 line_offset, objfile_name (objfile));
6f77053d
PA
20823 m_record_line_callback = noop_record_line;
20824 /* Note: record_line_callback is left as noop_record_line until
20825 we see DW_LNE_end_sequence. */
924c2928
DE
20826 }
20827}
20828
f3f5162e 20829/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20830 Process the line number information in LH.
20831 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20832 program in order to set included_p for every referenced header. */
debd256d 20833
c906108c 20834static void
43f3e411
DE
20835dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20836 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20837{
d521ce57
TT
20838 const gdb_byte *line_ptr, *extended_end;
20839 const gdb_byte *line_end;
a8c50c1f 20840 unsigned int bytes_read, extended_len;
699ca60a 20841 unsigned char op_code, extended_op;
e142c38c 20842 CORE_ADDR baseaddr;
518817b3 20843 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20844 bfd *abfd = objfile->obfd;
fbf65064 20845 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20846 /* True if we're recording line info (as opposed to building partial
20847 symtabs and just interested in finding include files mentioned by
20848 the line number program). */
20849 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
20850
20851 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 20852
debd256d
JB
20853 line_ptr = lh->statement_program_start;
20854 line_end = lh->statement_program_end;
c906108c
SS
20855
20856 /* Read the statement sequences until there's nothing left. */
20857 while (line_ptr < line_end)
20858 {
6f77053d
PA
20859 /* The DWARF line number program state machine. Reset the state
20860 machine at the start of each sequence. */
20861 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20862 bool end_sequence = false;
d9b3de22 20863
8c43009f 20864 if (record_lines_p)
c906108c 20865 {
8c43009f
PA
20866 /* Start a subfile for the current file of the state
20867 machine. */
20868 const file_entry *fe = state_machine.current_file ();
20869
20870 if (fe != NULL)
20871 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
20872 }
20873
a738430d 20874 /* Decode the table. */
d9b3de22 20875 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20876 {
20877 op_code = read_1_byte (abfd, line_ptr);
20878 line_ptr += 1;
9aa1fe7e 20879
debd256d 20880 if (op_code >= lh->opcode_base)
6e70227d 20881 {
8e07a239 20882 /* Special opcode. */
6f77053d 20883 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20884 }
20885 else switch (op_code)
c906108c
SS
20886 {
20887 case DW_LNS_extended_op:
3e43a32a
MS
20888 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20889 &bytes_read);
473b7be6 20890 line_ptr += bytes_read;
a8c50c1f 20891 extended_end = line_ptr + extended_len;
c906108c
SS
20892 extended_op = read_1_byte (abfd, line_ptr);
20893 line_ptr += 1;
20894 switch (extended_op)
20895 {
20896 case DW_LNE_end_sequence:
6f77053d
PA
20897 state_machine.handle_end_sequence ();
20898 end_sequence = true;
c906108c
SS
20899 break;
20900 case DW_LNE_set_address:
d9b3de22
DE
20901 {
20902 CORE_ADDR address
20903 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 20904 line_ptr += bytes_read;
6f77053d
PA
20905
20906 state_machine.check_line_address (cu, line_ptr,
20907 lowpc, address);
20908 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20909 }
c906108c
SS
20910 break;
20911 case DW_LNE_define_file:
debd256d 20912 {
d521ce57 20913 const char *cur_file;
ecfb656c
PA
20914 unsigned int mod_time, length;
20915 dir_index dindex;
6e70227d 20916
3e43a32a
MS
20917 cur_file = read_direct_string (abfd, line_ptr,
20918 &bytes_read);
debd256d 20919 line_ptr += bytes_read;
ecfb656c 20920 dindex = (dir_index)
debd256d
JB
20921 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20922 line_ptr += bytes_read;
20923 mod_time =
20924 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20925 line_ptr += bytes_read;
20926 length =
20927 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20928 line_ptr += bytes_read;
ecfb656c 20929 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20930 }
c906108c 20931 break;
d0c6ba3d 20932 case DW_LNE_set_discriminator:
6f77053d
PA
20933 {
20934 /* The discriminator is not interesting to the
20935 debugger; just ignore it. We still need to
20936 check its value though:
20937 if there are consecutive entries for the same
20938 (non-prologue) line we want to coalesce them.
20939 PR 17276. */
20940 unsigned int discr
20941 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20942 line_ptr += bytes_read;
20943
20944 state_machine.handle_set_discriminator (discr);
20945 }
d0c6ba3d 20946 break;
c906108c 20947 default:
4d3c2250 20948 complaint (&symfile_complaints,
e2e0b3e5 20949 _("mangled .debug_line section"));
debd256d 20950 return;
c906108c 20951 }
a8c50c1f
DJ
20952 /* Make sure that we parsed the extended op correctly. If e.g.
20953 we expected a different address size than the producer used,
20954 we may have read the wrong number of bytes. */
20955 if (line_ptr != extended_end)
20956 {
20957 complaint (&symfile_complaints,
20958 _("mangled .debug_line section"));
20959 return;
20960 }
c906108c
SS
20961 break;
20962 case DW_LNS_copy:
6f77053d 20963 state_machine.handle_copy ();
c906108c
SS
20964 break;
20965 case DW_LNS_advance_pc:
2dc7f7b3
TT
20966 {
20967 CORE_ADDR adjust
20968 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20969 line_ptr += bytes_read;
6f77053d
PA
20970
20971 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20972 }
c906108c
SS
20973 break;
20974 case DW_LNS_advance_line:
a05a36a5
DE
20975 {
20976 int line_delta
20977 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20978 line_ptr += bytes_read;
6f77053d
PA
20979
20980 state_machine.handle_advance_line (line_delta);
a05a36a5 20981 }
c906108c
SS
20982 break;
20983 case DW_LNS_set_file:
d9b3de22 20984 {
6f77053d 20985 file_name_index file
ecfb656c
PA
20986 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20987 &bytes_read);
d9b3de22 20988 line_ptr += bytes_read;
8c43009f 20989
6f77053d 20990 state_machine.handle_set_file (file);
d9b3de22 20991 }
c906108c
SS
20992 break;
20993 case DW_LNS_set_column:
0ad93d4f 20994 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20995 line_ptr += bytes_read;
20996 break;
20997 case DW_LNS_negate_stmt:
6f77053d 20998 state_machine.handle_negate_stmt ();
c906108c
SS
20999 break;
21000 case DW_LNS_set_basic_block:
c906108c 21001 break;
c2c6d25f
JM
21002 /* Add to the address register of the state machine the
21003 address increment value corresponding to special opcode
a738430d
MK
21004 255. I.e., this value is scaled by the minimum
21005 instruction length since special opcode 255 would have
b021a221 21006 scaled the increment. */
c906108c 21007 case DW_LNS_const_add_pc:
6f77053d 21008 state_machine.handle_const_add_pc ();
c906108c
SS
21009 break;
21010 case DW_LNS_fixed_advance_pc:
3e29f34a 21011 {
6f77053d 21012 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21013 line_ptr += 2;
6f77053d
PA
21014
21015 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21016 }
c906108c 21017 break;
9aa1fe7e 21018 default:
a738430d
MK
21019 {
21020 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21021 int i;
a738430d 21022
debd256d 21023 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21024 {
21025 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21026 line_ptr += bytes_read;
21027 }
21028 }
c906108c
SS
21029 }
21030 }
d9b3de22
DE
21031
21032 if (!end_sequence)
21033 dwarf2_debug_line_missing_end_sequence_complaint ();
21034
21035 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21036 in which case we still finish recording the last line). */
6f77053d 21037 state_machine.record_line (true);
c906108c 21038 }
f3f5162e
DE
21039}
21040
21041/* Decode the Line Number Program (LNP) for the given line_header
21042 structure and CU. The actual information extracted and the type
21043 of structures created from the LNP depends on the value of PST.
21044
21045 1. If PST is NULL, then this procedure uses the data from the program
21046 to create all necessary symbol tables, and their linetables.
21047
21048 2. If PST is not NULL, this procedure reads the program to determine
21049 the list of files included by the unit represented by PST, and
21050 builds all the associated partial symbol tables.
21051
21052 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21053 It is used for relative paths in the line table.
21054 NOTE: When processing partial symtabs (pst != NULL),
21055 comp_dir == pst->dirname.
21056
21057 NOTE: It is important that psymtabs have the same file name (via strcmp)
21058 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21059 symtab we don't use it in the name of the psymtabs we create.
21060 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21061 A good testcase for this is mb-inline.exp.
21062
527f3840
JK
21063 LOWPC is the lowest address in CU (or 0 if not known).
21064
21065 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21066 for its PC<->lines mapping information. Otherwise only the filename
21067 table is read in. */
f3f5162e
DE
21068
21069static void
21070dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21071 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21072 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21073{
518817b3 21074 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21075 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21076
527f3840
JK
21077 if (decode_mapping)
21078 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21079
21080 if (decode_for_pst_p)
21081 {
21082 int file_index;
21083
21084 /* Now that we're done scanning the Line Header Program, we can
21085 create the psymtab of each included file. */
fff8551c 21086 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
21087 if (lh->file_names[file_index].included_p == 1)
21088 {
c89b44cd 21089 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21090 const char *include_name =
c89b44cd
TT
21091 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21092 &name_holder);
c6da4cef 21093 if (include_name != NULL)
aaa75496
JB
21094 dwarf2_create_include_psymtab (include_name, pst, objfile);
21095 }
21096 }
cb1df416
DJ
21097 else
21098 {
21099 /* Make sure a symtab is created for every file, even files
21100 which contain only variables (i.e. no code with associated
21101 line numbers). */
43f3e411 21102 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 21103 int i;
cb1df416 21104
fff8551c 21105 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 21106 {
8c43009f 21107 file_entry &fe = lh->file_names[i];
9a619af0 21108
8c43009f 21109 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 21110
cb1df416 21111 if (current_subfile->symtab == NULL)
43f3e411
DE
21112 {
21113 current_subfile->symtab
21114 = allocate_symtab (cust, current_subfile->name);
21115 }
8c43009f 21116 fe.symtab = current_subfile->symtab;
cb1df416
DJ
21117 }
21118 }
c906108c
SS
21119}
21120
21121/* Start a subfile for DWARF. FILENAME is the name of the file and
21122 DIRNAME the name of the source directory which contains FILENAME
4d663531 21123 or NULL if not known.
c906108c
SS
21124 This routine tries to keep line numbers from identical absolute and
21125 relative file names in a common subfile.
21126
21127 Using the `list' example from the GDB testsuite, which resides in
21128 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21129 of /srcdir/list0.c yields the following debugging information for list0.c:
21130
c5aa993b 21131 DW_AT_name: /srcdir/list0.c
4d663531 21132 DW_AT_comp_dir: /compdir
357e46e7 21133 files.files[0].name: list0.h
c5aa993b 21134 files.files[0].dir: /srcdir
357e46e7 21135 files.files[1].name: list0.c
c5aa993b 21136 files.files[1].dir: /srcdir
c906108c
SS
21137
21138 The line number information for list0.c has to end up in a single
4f1520fb
FR
21139 subfile, so that `break /srcdir/list0.c:1' works as expected.
21140 start_subfile will ensure that this happens provided that we pass the
21141 concatenation of files.files[1].dir and files.files[1].name as the
21142 subfile's name. */
c906108c
SS
21143
21144static void
4d663531 21145dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 21146{
d521ce57 21147 char *copy = NULL;
4f1520fb 21148
4d663531 21149 /* In order not to lose the line information directory,
4f1520fb
FR
21150 we concatenate it to the filename when it makes sense.
21151 Note that the Dwarf3 standard says (speaking of filenames in line
21152 information): ``The directory index is ignored for file names
21153 that represent full path names''. Thus ignoring dirname in the
21154 `else' branch below isn't an issue. */
c906108c 21155
d5166ae1 21156 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21157 {
21158 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21159 filename = copy;
21160 }
c906108c 21161
4d663531 21162 start_subfile (filename);
4f1520fb 21163
d521ce57
TT
21164 if (copy != NULL)
21165 xfree (copy);
c906108c
SS
21166}
21167
f4dc4d17
DE
21168/* Start a symtab for DWARF.
21169 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21170
43f3e411 21171static struct compunit_symtab *
f4dc4d17 21172dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 21173 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 21174{
43f3e411 21175 struct compunit_symtab *cust
518817b3
SM
21176 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21177 low_pc, cu->language);
43f3e411 21178
f4dc4d17
DE
21179 record_debugformat ("DWARF 2");
21180 record_producer (cu->producer);
21181
21182 /* We assume that we're processing GCC output. */
21183 processing_gcc_compilation = 2;
21184
4d4ec4e5 21185 cu->processing_has_namespace_info = 0;
43f3e411
DE
21186
21187 return cust;
f4dc4d17
DE
21188}
21189
4c2df51b
DJ
21190static void
21191var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21192 struct dwarf2_cu *cu)
4c2df51b 21193{
518817b3 21194 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21195 struct comp_unit_head *cu_header = &cu->header;
21196
4c2df51b
DJ
21197 /* NOTE drow/2003-01-30: There used to be a comment and some special
21198 code here to turn a symbol with DW_AT_external and a
21199 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21200 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21201 with some versions of binutils) where shared libraries could have
21202 relocations against symbols in their debug information - the
21203 minimal symbol would have the right address, but the debug info
21204 would not. It's no longer necessary, because we will explicitly
21205 apply relocations when we read in the debug information now. */
21206
21207 /* A DW_AT_location attribute with no contents indicates that a
21208 variable has been optimized away. */
21209 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21210 {
f1e6e072 21211 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21212 return;
21213 }
21214
21215 /* Handle one degenerate form of location expression specially, to
21216 preserve GDB's previous behavior when section offsets are
3019eac3
DE
21217 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21218 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21219
21220 if (attr_form_is_block (attr)
3019eac3
DE
21221 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21222 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21223 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21224 && (DW_BLOCK (attr)->size
21225 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21226 {
891d2f0b 21227 unsigned int dummy;
4c2df51b 21228
3019eac3
DE
21229 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21230 SYMBOL_VALUE_ADDRESS (sym) =
21231 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21232 else
21233 SYMBOL_VALUE_ADDRESS (sym) =
21234 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 21235 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
21236 fixup_symbol_section (sym, objfile);
21237 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21238 SYMBOL_SECTION (sym));
4c2df51b
DJ
21239 return;
21240 }
21241
21242 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21243 expression evaluator, and use LOC_COMPUTED only when necessary
21244 (i.e. when the value of a register or memory location is
21245 referenced, or a thread-local block, etc.). Then again, it might
21246 not be worthwhile. I'm assuming that it isn't unless performance
21247 or memory numbers show me otherwise. */
21248
f1e6e072 21249 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21250
f1e6e072 21251 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 21252 cu->has_loclist = 1;
4c2df51b
DJ
21253}
21254
c906108c
SS
21255/* Given a pointer to a DWARF information entry, figure out if we need
21256 to make a symbol table entry for it, and if so, create a new entry
21257 and return a pointer to it.
21258 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21259 used the passed type.
21260 If SPACE is not NULL, use it to hold the new symbol. If it is
21261 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21262
21263static struct symbol *
5e2db402
TT
21264new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21265 struct symbol *space)
c906108c 21266{
518817b3
SM
21267 struct dwarf2_per_objfile *dwarf2_per_objfile
21268 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21269 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21270 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21271 struct symbol *sym = NULL;
15d034d0 21272 const char *name;
c906108c
SS
21273 struct attribute *attr = NULL;
21274 struct attribute *attr2 = NULL;
e142c38c 21275 CORE_ADDR baseaddr;
e37fd15a
SW
21276 struct pending **list_to_add = NULL;
21277
edb3359d 21278 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21279
21280 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21281
94af9270 21282 name = dwarf2_name (die, cu);
c906108c
SS
21283 if (name)
21284 {
94af9270 21285 const char *linkagename;
34eaf542 21286 int suppress_add = 0;
94af9270 21287
34eaf542
TT
21288 if (space)
21289 sym = space;
21290 else
e623cf5d 21291 sym = allocate_symbol (objfile);
c906108c 21292 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21293
21294 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21295 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21296 linkagename = dwarf2_physname (name, die, cu);
21297 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21298
f55ee35c
JK
21299 /* Fortran does not have mangling standard and the mangling does differ
21300 between gfortran, iFort etc. */
21301 if (cu->language == language_fortran
b250c185 21302 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21303 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21304 dwarf2_full_name (name, die, cu),
29df156d 21305 NULL);
f55ee35c 21306
c906108c 21307 /* Default assumptions.
c5aa993b 21308 Use the passed type or decode it from the die. */
176620f1 21309 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21310 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21311 if (type != NULL)
21312 SYMBOL_TYPE (sym) = type;
21313 else
e7c27a73 21314 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21315 attr = dwarf2_attr (die,
21316 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21317 cu);
c906108c
SS
21318 if (attr)
21319 {
21320 SYMBOL_LINE (sym) = DW_UNSND (attr);
21321 }
cb1df416 21322
edb3359d
DJ
21323 attr = dwarf2_attr (die,
21324 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21325 cu);
cb1df416
DJ
21326 if (attr)
21327 {
ecfb656c 21328 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21329 struct file_entry *fe;
9a619af0 21330
ecfb656c
PA
21331 if (cu->line_header != NULL)
21332 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21333 else
21334 fe = NULL;
21335
21336 if (fe == NULL)
cb1df416
DJ
21337 complaint (&symfile_complaints,
21338 _("file index out of range"));
8c43009f
PA
21339 else
21340 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21341 }
21342
c906108c
SS
21343 switch (die->tag)
21344 {
21345 case DW_TAG_label:
e142c38c 21346 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21347 if (attr)
3e29f34a
MR
21348 {
21349 CORE_ADDR addr;
21350
21351 addr = attr_value_as_address (attr);
21352 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21353 SYMBOL_VALUE_ADDRESS (sym) = addr;
21354 }
0f5238ed
TT
21355 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21356 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21357 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 21358 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21359 break;
21360 case DW_TAG_subprogram:
21361 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21362 finish_block. */
f1e6e072 21363 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21364 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
21365 if ((attr2 && (DW_UNSND (attr2) != 0))
21366 || cu->language == language_ada)
c906108c 21367 {
2cfa0c8d
JB
21368 /* Subprograms marked external are stored as a global symbol.
21369 Ada subprograms, whether marked external or not, are always
21370 stored as a global symbol, because we want to be able to
21371 access them globally. For instance, we want to be able
21372 to break on a nested subprogram without having to
21373 specify the context. */
e37fd15a 21374 list_to_add = &global_symbols;
c906108c
SS
21375 }
21376 else
21377 {
e37fd15a 21378 list_to_add = cu->list_in_scope;
c906108c
SS
21379 }
21380 break;
edb3359d
DJ
21381 case DW_TAG_inlined_subroutine:
21382 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21383 finish_block. */
f1e6e072 21384 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21385 SYMBOL_INLINED (sym) = 1;
481860b3 21386 list_to_add = cu->list_in_scope;
edb3359d 21387 break;
34eaf542
TT
21388 case DW_TAG_template_value_param:
21389 suppress_add = 1;
21390 /* Fall through. */
72929c62 21391 case DW_TAG_constant:
c906108c 21392 case DW_TAG_variable:
254e6b9e 21393 case DW_TAG_member:
0963b4bd
MS
21394 /* Compilation with minimal debug info may result in
21395 variables with missing type entries. Change the
21396 misleading `void' type to something sensible. */
c906108c 21397 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21398 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21399
e142c38c 21400 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21401 /* In the case of DW_TAG_member, we should only be called for
21402 static const members. */
21403 if (die->tag == DW_TAG_member)
21404 {
3863f96c
DE
21405 /* dwarf2_add_field uses die_is_declaration,
21406 so we do the same. */
254e6b9e
DE
21407 gdb_assert (die_is_declaration (die, cu));
21408 gdb_assert (attr);
21409 }
c906108c
SS
21410 if (attr)
21411 {
e7c27a73 21412 dwarf2_const_value (attr, sym, cu);
e142c38c 21413 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21414 if (!suppress_add)
34eaf542
TT
21415 {
21416 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 21417 list_to_add = &global_symbols;
34eaf542 21418 else
e37fd15a 21419 list_to_add = cu->list_in_scope;
34eaf542 21420 }
c906108c
SS
21421 break;
21422 }
e142c38c 21423 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21424 if (attr)
21425 {
e7c27a73 21426 var_decode_location (attr, sym, cu);
e142c38c 21427 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21428
21429 /* Fortran explicitly imports any global symbols to the local
21430 scope by DW_TAG_common_block. */
21431 if (cu->language == language_fortran && die->parent
21432 && die->parent->tag == DW_TAG_common_block)
21433 attr2 = NULL;
21434
caac4577
JG
21435 if (SYMBOL_CLASS (sym) == LOC_STATIC
21436 && SYMBOL_VALUE_ADDRESS (sym) == 0
21437 && !dwarf2_per_objfile->has_section_at_zero)
21438 {
21439 /* When a static variable is eliminated by the linker,
21440 the corresponding debug information is not stripped
21441 out, but the variable address is set to null;
21442 do not add such variables into symbol table. */
21443 }
21444 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21445 {
f55ee35c
JK
21446 /* Workaround gfortran PR debug/40040 - it uses
21447 DW_AT_location for variables in -fPIC libraries which may
21448 get overriden by other libraries/executable and get
21449 a different address. Resolve it by the minimal symbol
21450 which may come from inferior's executable using copy
21451 relocation. Make this workaround only for gfortran as for
21452 other compilers GDB cannot guess the minimal symbol
21453 Fortran mangling kind. */
21454 if (cu->language == language_fortran && die->parent
21455 && die->parent->tag == DW_TAG_module
21456 && cu->producer
28586665 21457 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 21458 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 21459
1c809c68
TT
21460 /* A variable with DW_AT_external is never static,
21461 but it may be block-scoped. */
21462 list_to_add = (cu->list_in_scope == &file_symbols
21463 ? &global_symbols : cu->list_in_scope);
1c809c68 21464 }
c906108c 21465 else
e37fd15a 21466 list_to_add = cu->list_in_scope;
c906108c
SS
21467 }
21468 else
21469 {
21470 /* We do not know the address of this symbol.
c5aa993b
JM
21471 If it is an external symbol and we have type information
21472 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21473 The address of the variable will then be determined from
21474 the minimal symbol table whenever the variable is
21475 referenced. */
e142c38c 21476 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21477
21478 /* Fortran explicitly imports any global symbols to the local
21479 scope by DW_TAG_common_block. */
21480 if (cu->language == language_fortran && die->parent
21481 && die->parent->tag == DW_TAG_common_block)
21482 {
21483 /* SYMBOL_CLASS doesn't matter here because
21484 read_common_block is going to reset it. */
21485 if (!suppress_add)
21486 list_to_add = cu->list_in_scope;
21487 }
21488 else if (attr2 && (DW_UNSND (attr2) != 0)
21489 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21490 {
0fe7935b
DJ
21491 /* A variable with DW_AT_external is never static, but it
21492 may be block-scoped. */
21493 list_to_add = (cu->list_in_scope == &file_symbols
21494 ? &global_symbols : cu->list_in_scope);
21495
f1e6e072 21496 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21497 }
442ddf59
JK
21498 else if (!die_is_declaration (die, cu))
21499 {
21500 /* Use the default LOC_OPTIMIZED_OUT class. */
21501 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21502 if (!suppress_add)
21503 list_to_add = cu->list_in_scope;
442ddf59 21504 }
c906108c
SS
21505 }
21506 break;
21507 case DW_TAG_formal_parameter:
edb3359d
DJ
21508 /* If we are inside a function, mark this as an argument. If
21509 not, we might be looking at an argument to an inlined function
21510 when we do not have enough information to show inlined frames;
21511 pretend it's a local variable in that case so that the user can
21512 still see it. */
21513 if (context_stack_depth > 0
21514 && context_stack[context_stack_depth - 1].name != NULL)
21515 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 21516 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21517 if (attr)
21518 {
e7c27a73 21519 var_decode_location (attr, sym, cu);
c906108c 21520 }
e142c38c 21521 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21522 if (attr)
21523 {
e7c27a73 21524 dwarf2_const_value (attr, sym, cu);
c906108c 21525 }
f346a30d 21526
e37fd15a 21527 list_to_add = cu->list_in_scope;
c906108c
SS
21528 break;
21529 case DW_TAG_unspecified_parameters:
21530 /* From varargs functions; gdb doesn't seem to have any
21531 interest in this information, so just ignore it for now.
21532 (FIXME?) */
21533 break;
34eaf542
TT
21534 case DW_TAG_template_type_param:
21535 suppress_add = 1;
21536 /* Fall through. */
c906108c 21537 case DW_TAG_class_type:
680b30c7 21538 case DW_TAG_interface_type:
c906108c
SS
21539 case DW_TAG_structure_type:
21540 case DW_TAG_union_type:
72019c9c 21541 case DW_TAG_set_type:
c906108c 21542 case DW_TAG_enumeration_type:
f1e6e072 21543 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21544 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21545
63d06c5c 21546 {
9c37b5ae 21547 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21548 really ever be static objects: otherwise, if you try
21549 to, say, break of a class's method and you're in a file
21550 which doesn't mention that class, it won't work unless
21551 the check for all static symbols in lookup_symbol_aux
21552 saves you. See the OtherFileClass tests in
21553 gdb.c++/namespace.exp. */
21554
e37fd15a 21555 if (!suppress_add)
34eaf542 21556 {
34eaf542 21557 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21558 && cu->language == language_cplus
34eaf542 21559 ? &global_symbols : cu->list_in_scope);
63d06c5c 21560
64382290 21561 /* The semantics of C++ state that "struct foo {
9c37b5ae 21562 ... }" also defines a typedef for "foo". */
64382290 21563 if (cu->language == language_cplus
45280282 21564 || cu->language == language_ada
c44af4eb
TT
21565 || cu->language == language_d
21566 || cu->language == language_rust)
64382290
TT
21567 {
21568 /* The symbol's name is already allocated along
21569 with this objfile, so we don't need to
21570 duplicate it for the type. */
21571 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21572 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21573 }
63d06c5c
DC
21574 }
21575 }
c906108c
SS
21576 break;
21577 case DW_TAG_typedef:
f1e6e072 21578 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21579 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21580 list_to_add = cu->list_in_scope;
63d06c5c 21581 break;
c906108c 21582 case DW_TAG_base_type:
a02abb62 21583 case DW_TAG_subrange_type:
f1e6e072 21584 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21585 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21586 list_to_add = cu->list_in_scope;
c906108c
SS
21587 break;
21588 case DW_TAG_enumerator:
e142c38c 21589 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21590 if (attr)
21591 {
e7c27a73 21592 dwarf2_const_value (attr, sym, cu);
c906108c 21593 }
63d06c5c
DC
21594 {
21595 /* NOTE: carlton/2003-11-10: See comment above in the
21596 DW_TAG_class_type, etc. block. */
21597
e142c38c 21598 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21599 && cu->language == language_cplus
e142c38c 21600 ? &global_symbols : cu->list_in_scope);
63d06c5c 21601 }
c906108c 21602 break;
74921315 21603 case DW_TAG_imported_declaration:
5c4e30ca 21604 case DW_TAG_namespace:
f1e6e072 21605 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 21606 list_to_add = &global_symbols;
5c4e30ca 21607 break;
530e8392
KB
21608 case DW_TAG_module:
21609 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21610 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21611 list_to_add = &global_symbols;
21612 break;
4357ac6c 21613 case DW_TAG_common_block:
f1e6e072 21614 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
21615 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21616 add_symbol_to_list (sym, cu->list_in_scope);
21617 break;
c906108c
SS
21618 default:
21619 /* Not a tag we recognize. Hopefully we aren't processing
21620 trash data, but since we must specifically ignore things
21621 we don't recognize, there is nothing else we should do at
0963b4bd 21622 this point. */
e2e0b3e5 21623 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 21624 dwarf_tag_name (die->tag));
c906108c
SS
21625 break;
21626 }
df8a16a1 21627
e37fd15a
SW
21628 if (suppress_add)
21629 {
21630 sym->hash_next = objfile->template_symbols;
21631 objfile->template_symbols = sym;
21632 list_to_add = NULL;
21633 }
21634
21635 if (list_to_add != NULL)
21636 add_symbol_to_list (sym, list_to_add);
21637
df8a16a1
DJ
21638 /* For the benefit of old versions of GCC, check for anonymous
21639 namespaces based on the demangled name. */
4d4ec4e5 21640 if (!cu->processing_has_namespace_info
94af9270 21641 && cu->language == language_cplus)
a10964d1 21642 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
21643 }
21644 return (sym);
21645}
21646
98bfdba5
PA
21647/* Given an attr with a DW_FORM_dataN value in host byte order,
21648 zero-extend it as appropriate for the symbol's type. The DWARF
21649 standard (v4) is not entirely clear about the meaning of using
21650 DW_FORM_dataN for a constant with a signed type, where the type is
21651 wider than the data. The conclusion of a discussion on the DWARF
21652 list was that this is unspecified. We choose to always zero-extend
21653 because that is the interpretation long in use by GCC. */
c906108c 21654
98bfdba5 21655static gdb_byte *
ff39bb5e 21656dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21657 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21658{
518817b3 21659 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21660 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21661 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21662 LONGEST l = DW_UNSND (attr);
21663
21664 if (bits < sizeof (*value) * 8)
21665 {
21666 l &= ((LONGEST) 1 << bits) - 1;
21667 *value = l;
21668 }
21669 else if (bits == sizeof (*value) * 8)
21670 *value = l;
21671 else
21672 {
224c3ddb 21673 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21674 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21675 return bytes;
21676 }
21677
21678 return NULL;
21679}
21680
21681/* Read a constant value from an attribute. Either set *VALUE, or if
21682 the value does not fit in *VALUE, set *BYTES - either already
21683 allocated on the objfile obstack, or newly allocated on OBSTACK,
21684 or, set *BATON, if we translated the constant to a location
21685 expression. */
21686
21687static void
ff39bb5e 21688dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21689 const char *name, struct obstack *obstack,
21690 struct dwarf2_cu *cu,
d521ce57 21691 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21692 struct dwarf2_locexpr_baton **baton)
21693{
518817b3 21694 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21695 struct comp_unit_head *cu_header = &cu->header;
c906108c 21696 struct dwarf_block *blk;
98bfdba5
PA
21697 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21698 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21699
21700 *value = 0;
21701 *bytes = NULL;
21702 *baton = NULL;
c906108c
SS
21703
21704 switch (attr->form)
21705 {
21706 case DW_FORM_addr:
3019eac3 21707 case DW_FORM_GNU_addr_index:
ac56253d 21708 {
ac56253d
TT
21709 gdb_byte *data;
21710
98bfdba5
PA
21711 if (TYPE_LENGTH (type) != cu_header->addr_size)
21712 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21713 cu_header->addr_size,
98bfdba5 21714 TYPE_LENGTH (type));
ac56253d
TT
21715 /* Symbols of this form are reasonably rare, so we just
21716 piggyback on the existing location code rather than writing
21717 a new implementation of symbol_computed_ops. */
8d749320 21718 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21719 (*baton)->per_cu = cu->per_cu;
21720 gdb_assert ((*baton)->per_cu);
ac56253d 21721
98bfdba5 21722 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21723 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21724 (*baton)->data = data;
ac56253d
TT
21725
21726 data[0] = DW_OP_addr;
21727 store_unsigned_integer (&data[1], cu_header->addr_size,
21728 byte_order, DW_ADDR (attr));
21729 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21730 }
c906108c 21731 break;
4ac36638 21732 case DW_FORM_string:
93b5768b 21733 case DW_FORM_strp:
3019eac3 21734 case DW_FORM_GNU_str_index:
36586728 21735 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21736 /* DW_STRING is already allocated on the objfile obstack, point
21737 directly to it. */
d521ce57 21738 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21739 break;
c906108c
SS
21740 case DW_FORM_block1:
21741 case DW_FORM_block2:
21742 case DW_FORM_block4:
21743 case DW_FORM_block:
2dc7f7b3 21744 case DW_FORM_exprloc:
0224619f 21745 case DW_FORM_data16:
c906108c 21746 blk = DW_BLOCK (attr);
98bfdba5
PA
21747 if (TYPE_LENGTH (type) != blk->size)
21748 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21749 TYPE_LENGTH (type));
21750 *bytes = blk->data;
c906108c 21751 break;
2df3850c
JM
21752
21753 /* The DW_AT_const_value attributes are supposed to carry the
21754 symbol's value "represented as it would be on the target
21755 architecture." By the time we get here, it's already been
21756 converted to host endianness, so we just need to sign- or
21757 zero-extend it as appropriate. */
21758 case DW_FORM_data1:
3aef2284 21759 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21760 break;
c906108c 21761 case DW_FORM_data2:
3aef2284 21762 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21763 break;
c906108c 21764 case DW_FORM_data4:
3aef2284 21765 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21766 break;
c906108c 21767 case DW_FORM_data8:
3aef2284 21768 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21769 break;
21770
c906108c 21771 case DW_FORM_sdata:
663c44ac 21772 case DW_FORM_implicit_const:
98bfdba5 21773 *value = DW_SND (attr);
2df3850c
JM
21774 break;
21775
c906108c 21776 case DW_FORM_udata:
98bfdba5 21777 *value = DW_UNSND (attr);
c906108c 21778 break;
2df3850c 21779
c906108c 21780 default:
4d3c2250 21781 complaint (&symfile_complaints,
e2e0b3e5 21782 _("unsupported const value attribute form: '%s'"),
4d3c2250 21783 dwarf_form_name (attr->form));
98bfdba5 21784 *value = 0;
c906108c
SS
21785 break;
21786 }
21787}
21788
2df3850c 21789
98bfdba5
PA
21790/* Copy constant value from an attribute to a symbol. */
21791
2df3850c 21792static void
ff39bb5e 21793dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21794 struct dwarf2_cu *cu)
2df3850c 21795{
518817b3 21796 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21797 LONGEST value;
d521ce57 21798 const gdb_byte *bytes;
98bfdba5 21799 struct dwarf2_locexpr_baton *baton;
2df3850c 21800
98bfdba5
PA
21801 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21802 SYMBOL_PRINT_NAME (sym),
21803 &objfile->objfile_obstack, cu,
21804 &value, &bytes, &baton);
2df3850c 21805
98bfdba5
PA
21806 if (baton != NULL)
21807 {
98bfdba5 21808 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21809 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21810 }
21811 else if (bytes != NULL)
21812 {
21813 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21814 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21815 }
21816 else
21817 {
21818 SYMBOL_VALUE (sym) = value;
f1e6e072 21819 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21820 }
2df3850c
JM
21821}
21822
c906108c
SS
21823/* Return the type of the die in question using its DW_AT_type attribute. */
21824
21825static struct type *
e7c27a73 21826die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21827{
c906108c 21828 struct attribute *type_attr;
c906108c 21829
e142c38c 21830 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21831 if (!type_attr)
21832 {
518817b3 21833 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21834 /* A missing DW_AT_type represents a void type. */
518817b3 21835 return objfile_type (objfile)->builtin_void;
c906108c 21836 }
348e048f 21837
673bfd45 21838 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21839}
21840
b4ba55a1
JB
21841/* True iff CU's producer generates GNAT Ada auxiliary information
21842 that allows to find parallel types through that information instead
21843 of having to do expensive parallel lookups by type name. */
21844
21845static int
21846need_gnat_info (struct dwarf2_cu *cu)
21847{
de4cb04a
JB
21848 /* Assume that the Ada compiler was GNAT, which always produces
21849 the auxiliary information. */
21850 return (cu->language == language_ada);
b4ba55a1
JB
21851}
21852
b4ba55a1
JB
21853/* Return the auxiliary type of the die in question using its
21854 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21855 attribute is not present. */
21856
21857static struct type *
21858die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21859{
b4ba55a1 21860 struct attribute *type_attr;
b4ba55a1
JB
21861
21862 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21863 if (!type_attr)
21864 return NULL;
21865
673bfd45 21866 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21867}
21868
21869/* If DIE has a descriptive_type attribute, then set the TYPE's
21870 descriptive type accordingly. */
21871
21872static void
21873set_descriptive_type (struct type *type, struct die_info *die,
21874 struct dwarf2_cu *cu)
21875{
21876 struct type *descriptive_type = die_descriptive_type (die, cu);
21877
21878 if (descriptive_type)
21879 {
21880 ALLOCATE_GNAT_AUX_TYPE (type);
21881 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21882 }
21883}
21884
c906108c
SS
21885/* Return the containing type of the die in question using its
21886 DW_AT_containing_type attribute. */
21887
21888static struct type *
e7c27a73 21889die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21890{
c906108c 21891 struct attribute *type_attr;
518817b3 21892 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21893
e142c38c 21894 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21895 if (!type_attr)
21896 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21897 "[in module %s]"), objfile_name (objfile));
33ac96f0 21898
673bfd45 21899 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21900}
21901
ac9ec31b
DE
21902/* Return an error marker type to use for the ill formed type in DIE/CU. */
21903
21904static struct type *
21905build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21906{
518817b3
SM
21907 struct dwarf2_per_objfile *dwarf2_per_objfile
21908 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
21909 struct objfile *objfile = dwarf2_per_objfile->objfile;
21910 char *message, *saved;
21911
21912 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 21913 objfile_name (objfile),
9c541725
PA
21914 to_underlying (cu->header.sect_off),
21915 to_underlying (die->sect_off));
224c3ddb
SM
21916 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21917 message, strlen (message));
ac9ec31b
DE
21918 xfree (message);
21919
19f392bc 21920 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21921}
21922
673bfd45 21923/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21924 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21925 DW_AT_containing_type.
673bfd45
DE
21926 If there is no type substitute an error marker. */
21927
c906108c 21928static struct type *
ff39bb5e 21929lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21930 struct dwarf2_cu *cu)
c906108c 21931{
518817b3
SM
21932 struct dwarf2_per_objfile *dwarf2_per_objfile
21933 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21934 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21935 struct type *this_type;
21936
ac9ec31b
DE
21937 gdb_assert (attr->name == DW_AT_type
21938 || attr->name == DW_AT_GNAT_descriptive_type
21939 || attr->name == DW_AT_containing_type);
21940
673bfd45
DE
21941 /* First see if we have it cached. */
21942
36586728
TT
21943 if (attr->form == DW_FORM_GNU_ref_alt)
21944 {
21945 struct dwarf2_per_cu_data *per_cu;
9c541725 21946 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21947
ed2dc618
SM
21948 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21949 dwarf2_per_objfile);
9c541725 21950 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21951 }
7771576e 21952 else if (attr_form_is_ref (attr))
673bfd45 21953 {
9c541725 21954 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21955
9c541725 21956 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21957 }
55f1336d 21958 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21959 {
ac9ec31b 21960 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21961
ac9ec31b 21962 return get_signatured_type (die, signature, cu);
673bfd45
DE
21963 }
21964 else
21965 {
ac9ec31b
DE
21966 complaint (&symfile_complaints,
21967 _("Dwarf Error: Bad type attribute %s in DIE"
21968 " at 0x%x [in module %s]"),
9c541725 21969 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
4262abfb 21970 objfile_name (objfile));
ac9ec31b 21971 return build_error_marker_type (cu, die);
673bfd45
DE
21972 }
21973
21974 /* If not cached we need to read it in. */
21975
21976 if (this_type == NULL)
21977 {
ac9ec31b 21978 struct die_info *type_die = NULL;
673bfd45
DE
21979 struct dwarf2_cu *type_cu = cu;
21980
7771576e 21981 if (attr_form_is_ref (attr))
ac9ec31b
DE
21982 type_die = follow_die_ref (die, attr, &type_cu);
21983 if (type_die == NULL)
21984 return build_error_marker_type (cu, die);
21985 /* If we find the type now, it's probably because the type came
3019eac3
DE
21986 from an inter-CU reference and the type's CU got expanded before
21987 ours. */
ac9ec31b 21988 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21989 }
21990
21991 /* If we still don't have a type use an error marker. */
21992
21993 if (this_type == NULL)
ac9ec31b 21994 return build_error_marker_type (cu, die);
673bfd45 21995
f792889a 21996 return this_type;
c906108c
SS
21997}
21998
673bfd45
DE
21999/* Return the type in DIE, CU.
22000 Returns NULL for invalid types.
22001
02142a6c 22002 This first does a lookup in die_type_hash,
673bfd45
DE
22003 and only reads the die in if necessary.
22004
22005 NOTE: This can be called when reading in partial or full symbols. */
22006
f792889a 22007static struct type *
e7c27a73 22008read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22009{
f792889a
DJ
22010 struct type *this_type;
22011
22012 this_type = get_die_type (die, cu);
22013 if (this_type)
22014 return this_type;
22015
673bfd45
DE
22016 return read_type_die_1 (die, cu);
22017}
22018
22019/* Read the type in DIE, CU.
22020 Returns NULL for invalid types. */
22021
22022static struct type *
22023read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22024{
22025 struct type *this_type = NULL;
22026
c906108c
SS
22027 switch (die->tag)
22028 {
22029 case DW_TAG_class_type:
680b30c7 22030 case DW_TAG_interface_type:
c906108c
SS
22031 case DW_TAG_structure_type:
22032 case DW_TAG_union_type:
f792889a 22033 this_type = read_structure_type (die, cu);
c906108c
SS
22034 break;
22035 case DW_TAG_enumeration_type:
f792889a 22036 this_type = read_enumeration_type (die, cu);
c906108c
SS
22037 break;
22038 case DW_TAG_subprogram:
22039 case DW_TAG_subroutine_type:
edb3359d 22040 case DW_TAG_inlined_subroutine:
f792889a 22041 this_type = read_subroutine_type (die, cu);
c906108c
SS
22042 break;
22043 case DW_TAG_array_type:
f792889a 22044 this_type = read_array_type (die, cu);
c906108c 22045 break;
72019c9c 22046 case DW_TAG_set_type:
f792889a 22047 this_type = read_set_type (die, cu);
72019c9c 22048 break;
c906108c 22049 case DW_TAG_pointer_type:
f792889a 22050 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22051 break;
22052 case DW_TAG_ptr_to_member_type:
f792889a 22053 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22054 break;
22055 case DW_TAG_reference_type:
4297a3f0
AV
22056 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22057 break;
22058 case DW_TAG_rvalue_reference_type:
22059 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22060 break;
22061 case DW_TAG_const_type:
f792889a 22062 this_type = read_tag_const_type (die, cu);
c906108c
SS
22063 break;
22064 case DW_TAG_volatile_type:
f792889a 22065 this_type = read_tag_volatile_type (die, cu);
c906108c 22066 break;
06d66ee9
TT
22067 case DW_TAG_restrict_type:
22068 this_type = read_tag_restrict_type (die, cu);
22069 break;
c906108c 22070 case DW_TAG_string_type:
f792889a 22071 this_type = read_tag_string_type (die, cu);
c906108c
SS
22072 break;
22073 case DW_TAG_typedef:
f792889a 22074 this_type = read_typedef (die, cu);
c906108c 22075 break;
a02abb62 22076 case DW_TAG_subrange_type:
f792889a 22077 this_type = read_subrange_type (die, cu);
a02abb62 22078 break;
c906108c 22079 case DW_TAG_base_type:
f792889a 22080 this_type = read_base_type (die, cu);
c906108c 22081 break;
81a17f79 22082 case DW_TAG_unspecified_type:
f792889a 22083 this_type = read_unspecified_type (die, cu);
81a17f79 22084 break;
0114d602
DJ
22085 case DW_TAG_namespace:
22086 this_type = read_namespace_type (die, cu);
22087 break;
f55ee35c
JK
22088 case DW_TAG_module:
22089 this_type = read_module_type (die, cu);
22090 break;
a2c2acaf
MW
22091 case DW_TAG_atomic_type:
22092 this_type = read_tag_atomic_type (die, cu);
22093 break;
c906108c 22094 default:
3e43a32a
MS
22095 complaint (&symfile_complaints,
22096 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 22097 dwarf_tag_name (die->tag));
c906108c
SS
22098 break;
22099 }
63d06c5c 22100
f792889a 22101 return this_type;
63d06c5c
DC
22102}
22103
abc72ce4
DE
22104/* See if we can figure out if the class lives in a namespace. We do
22105 this by looking for a member function; its demangled name will
22106 contain namespace info, if there is any.
22107 Return the computed name or NULL.
22108 Space for the result is allocated on the objfile's obstack.
22109 This is the full-die version of guess_partial_die_structure_name.
22110 In this case we know DIE has no useful parent. */
22111
22112static char *
22113guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22114{
22115 struct die_info *spec_die;
22116 struct dwarf2_cu *spec_cu;
22117 struct die_info *child;
518817b3 22118 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22119
22120 spec_cu = cu;
22121 spec_die = die_specification (die, &spec_cu);
22122 if (spec_die != NULL)
22123 {
22124 die = spec_die;
22125 cu = spec_cu;
22126 }
22127
22128 for (child = die->child;
22129 child != NULL;
22130 child = child->sibling)
22131 {
22132 if (child->tag == DW_TAG_subprogram)
22133 {
73b9be8b 22134 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22135
7d45c7c3 22136 if (linkage_name != NULL)
abc72ce4
DE
22137 {
22138 char *actual_name
22139 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22140 linkage_name);
abc72ce4
DE
22141 char *name = NULL;
22142
22143 if (actual_name != NULL)
22144 {
15d034d0 22145 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22146
22147 if (die_name != NULL
22148 && strcmp (die_name, actual_name) != 0)
22149 {
22150 /* Strip off the class name from the full name.
22151 We want the prefix. */
22152 int die_name_len = strlen (die_name);
22153 int actual_name_len = strlen (actual_name);
22154
22155 /* Test for '::' as a sanity check. */
22156 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22157 && actual_name[actual_name_len
22158 - die_name_len - 1] == ':')
224c3ddb 22159 name = (char *) obstack_copy0 (
e3b94546 22160 &objfile->per_bfd->storage_obstack,
224c3ddb 22161 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22162 }
22163 }
22164 xfree (actual_name);
22165 return name;
22166 }
22167 }
22168 }
22169
22170 return NULL;
22171}
22172
96408a79
SA
22173/* GCC might emit a nameless typedef that has a linkage name. Determine the
22174 prefix part in such case. See
22175 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22176
a121b7c1 22177static const char *
96408a79
SA
22178anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22179{
22180 struct attribute *attr;
e6a959d6 22181 const char *base;
96408a79
SA
22182
22183 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22184 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22185 return NULL;
22186
7d45c7c3 22187 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22188 return NULL;
22189
73b9be8b 22190 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22191 if (attr == NULL || DW_STRING (attr) == NULL)
22192 return NULL;
22193
22194 /* dwarf2_name had to be already called. */
22195 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22196
22197 /* Strip the base name, keep any leading namespaces/classes. */
22198 base = strrchr (DW_STRING (attr), ':');
22199 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22200 return "";
22201
518817b3 22202 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e3b94546 22203 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
22204 DW_STRING (attr),
22205 &base[-1] - DW_STRING (attr));
96408a79
SA
22206}
22207
fdde2d81 22208/* Return the name of the namespace/class that DIE is defined within,
0114d602 22209 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22210
0114d602
DJ
22211 For example, if we're within the method foo() in the following
22212 code:
22213
22214 namespace N {
22215 class C {
22216 void foo () {
22217 }
22218 };
22219 }
22220
22221 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22222
0d5cff50 22223static const char *
e142c38c 22224determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22225{
518817b3
SM
22226 struct dwarf2_per_objfile *dwarf2_per_objfile
22227 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22228 struct die_info *parent, *spec_die;
22229 struct dwarf2_cu *spec_cu;
22230 struct type *parent_type;
a121b7c1 22231 const char *retval;
63d06c5c 22232
9c37b5ae 22233 if (cu->language != language_cplus
c44af4eb
TT
22234 && cu->language != language_fortran && cu->language != language_d
22235 && cu->language != language_rust)
0114d602
DJ
22236 return "";
22237
96408a79
SA
22238 retval = anonymous_struct_prefix (die, cu);
22239 if (retval)
22240 return retval;
22241
0114d602
DJ
22242 /* We have to be careful in the presence of DW_AT_specification.
22243 For example, with GCC 3.4, given the code
22244
22245 namespace N {
22246 void foo() {
22247 // Definition of N::foo.
22248 }
22249 }
22250
22251 then we'll have a tree of DIEs like this:
22252
22253 1: DW_TAG_compile_unit
22254 2: DW_TAG_namespace // N
22255 3: DW_TAG_subprogram // declaration of N::foo
22256 4: DW_TAG_subprogram // definition of N::foo
22257 DW_AT_specification // refers to die #3
22258
22259 Thus, when processing die #4, we have to pretend that we're in
22260 the context of its DW_AT_specification, namely the contex of die
22261 #3. */
22262 spec_cu = cu;
22263 spec_die = die_specification (die, &spec_cu);
22264 if (spec_die == NULL)
22265 parent = die->parent;
22266 else
63d06c5c 22267 {
0114d602
DJ
22268 parent = spec_die->parent;
22269 cu = spec_cu;
63d06c5c 22270 }
0114d602
DJ
22271
22272 if (parent == NULL)
22273 return "";
98bfdba5
PA
22274 else if (parent->building_fullname)
22275 {
22276 const char *name;
22277 const char *parent_name;
22278
22279 /* It has been seen on RealView 2.2 built binaries,
22280 DW_TAG_template_type_param types actually _defined_ as
22281 children of the parent class:
22282
22283 enum E {};
22284 template class <class Enum> Class{};
22285 Class<enum E> class_e;
22286
22287 1: DW_TAG_class_type (Class)
22288 2: DW_TAG_enumeration_type (E)
22289 3: DW_TAG_enumerator (enum1:0)
22290 3: DW_TAG_enumerator (enum2:1)
22291 ...
22292 2: DW_TAG_template_type_param
22293 DW_AT_type DW_FORM_ref_udata (E)
22294
22295 Besides being broken debug info, it can put GDB into an
22296 infinite loop. Consider:
22297
22298 When we're building the full name for Class<E>, we'll start
22299 at Class, and go look over its template type parameters,
22300 finding E. We'll then try to build the full name of E, and
22301 reach here. We're now trying to build the full name of E,
22302 and look over the parent DIE for containing scope. In the
22303 broken case, if we followed the parent DIE of E, we'd again
22304 find Class, and once again go look at its template type
22305 arguments, etc., etc. Simply don't consider such parent die
22306 as source-level parent of this die (it can't be, the language
22307 doesn't allow it), and break the loop here. */
22308 name = dwarf2_name (die, cu);
22309 parent_name = dwarf2_name (parent, cu);
22310 complaint (&symfile_complaints,
22311 _("template param type '%s' defined within parent '%s'"),
22312 name ? name : "<unknown>",
22313 parent_name ? parent_name : "<unknown>");
22314 return "";
22315 }
63d06c5c 22316 else
0114d602
DJ
22317 switch (parent->tag)
22318 {
63d06c5c 22319 case DW_TAG_namespace:
0114d602 22320 parent_type = read_type_die (parent, cu);
acebe513
UW
22321 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22322 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22323 Work around this problem here. */
22324 if (cu->language == language_cplus
22325 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22326 return "";
0114d602
DJ
22327 /* We give a name to even anonymous namespaces. */
22328 return TYPE_TAG_NAME (parent_type);
63d06c5c 22329 case DW_TAG_class_type:
680b30c7 22330 case DW_TAG_interface_type:
63d06c5c 22331 case DW_TAG_structure_type:
0114d602 22332 case DW_TAG_union_type:
f55ee35c 22333 case DW_TAG_module:
0114d602
DJ
22334 parent_type = read_type_die (parent, cu);
22335 if (TYPE_TAG_NAME (parent_type) != NULL)
22336 return TYPE_TAG_NAME (parent_type);
22337 else
22338 /* An anonymous structure is only allowed non-static data
22339 members; no typedefs, no member functions, et cetera.
22340 So it does not need a prefix. */
22341 return "";
abc72ce4 22342 case DW_TAG_compile_unit:
95554aad 22343 case DW_TAG_partial_unit:
abc72ce4
DE
22344 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22345 if (cu->language == language_cplus
8b70b953 22346 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
22347 && die->child != NULL
22348 && (die->tag == DW_TAG_class_type
22349 || die->tag == DW_TAG_structure_type
22350 || die->tag == DW_TAG_union_type))
22351 {
22352 char *name = guess_full_die_structure_name (die, cu);
22353 if (name != NULL)
22354 return name;
22355 }
22356 return "";
3d567982
TT
22357 case DW_TAG_enumeration_type:
22358 parent_type = read_type_die (parent, cu);
22359 if (TYPE_DECLARED_CLASS (parent_type))
22360 {
22361 if (TYPE_TAG_NAME (parent_type) != NULL)
22362 return TYPE_TAG_NAME (parent_type);
22363 return "";
22364 }
22365 /* Fall through. */
63d06c5c 22366 default:
8176b9b8 22367 return determine_prefix (parent, cu);
63d06c5c 22368 }
63d06c5c
DC
22369}
22370
3e43a32a
MS
22371/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22372 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22373 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22374 an obconcat, otherwise allocate storage for the result. The CU argument is
22375 used to determine the language and hence, the appropriate separator. */
987504bb 22376
f55ee35c 22377#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22378
22379static char *
f55ee35c
JK
22380typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22381 int physname, struct dwarf2_cu *cu)
63d06c5c 22382{
f55ee35c 22383 const char *lead = "";
5c315b68 22384 const char *sep;
63d06c5c 22385
3e43a32a
MS
22386 if (suffix == NULL || suffix[0] == '\0'
22387 || prefix == NULL || prefix[0] == '\0')
987504bb 22388 sep = "";
45280282
IB
22389 else if (cu->language == language_d)
22390 {
22391 /* For D, the 'main' function could be defined in any module, but it
22392 should never be prefixed. */
22393 if (strcmp (suffix, "D main") == 0)
22394 {
22395 prefix = "";
22396 sep = "";
22397 }
22398 else
22399 sep = ".";
22400 }
f55ee35c
JK
22401 else if (cu->language == language_fortran && physname)
22402 {
22403 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22404 DW_AT_MIPS_linkage_name is preferred and used instead. */
22405
22406 lead = "__";
22407 sep = "_MOD_";
22408 }
987504bb
JJ
22409 else
22410 sep = "::";
63d06c5c 22411
6dd47d34
DE
22412 if (prefix == NULL)
22413 prefix = "";
22414 if (suffix == NULL)
22415 suffix = "";
22416
987504bb
JJ
22417 if (obs == NULL)
22418 {
3e43a32a 22419 char *retval
224c3ddb
SM
22420 = ((char *)
22421 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22422
f55ee35c
JK
22423 strcpy (retval, lead);
22424 strcat (retval, prefix);
6dd47d34
DE
22425 strcat (retval, sep);
22426 strcat (retval, suffix);
63d06c5c
DC
22427 return retval;
22428 }
987504bb
JJ
22429 else
22430 {
22431 /* We have an obstack. */
f55ee35c 22432 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22433 }
63d06c5c
DC
22434}
22435
c906108c
SS
22436/* Return sibling of die, NULL if no sibling. */
22437
f9aca02d 22438static struct die_info *
fba45db2 22439sibling_die (struct die_info *die)
c906108c 22440{
639d11d3 22441 return die->sibling;
c906108c
SS
22442}
22443
71c25dea
TT
22444/* Get name of a die, return NULL if not found. */
22445
15d034d0
TT
22446static const char *
22447dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22448 struct obstack *obstack)
22449{
22450 if (name && cu->language == language_cplus)
22451 {
2f408ecb 22452 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22453
2f408ecb 22454 if (!canon_name.empty ())
71c25dea 22455 {
2f408ecb
PA
22456 if (canon_name != name)
22457 name = (const char *) obstack_copy0 (obstack,
22458 canon_name.c_str (),
22459 canon_name.length ());
71c25dea
TT
22460 }
22461 }
22462
22463 return name;
c906108c
SS
22464}
22465
96553a0c
DE
22466/* Get name of a die, return NULL if not found.
22467 Anonymous namespaces are converted to their magic string. */
9219021c 22468
15d034d0 22469static const char *
e142c38c 22470dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22471{
22472 struct attribute *attr;
518817b3 22473 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22474
e142c38c 22475 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22476 if ((!attr || !DW_STRING (attr))
96553a0c 22477 && die->tag != DW_TAG_namespace
53832f31
TT
22478 && die->tag != DW_TAG_class_type
22479 && die->tag != DW_TAG_interface_type
22480 && die->tag != DW_TAG_structure_type
22481 && die->tag != DW_TAG_union_type)
71c25dea
TT
22482 return NULL;
22483
22484 switch (die->tag)
22485 {
22486 case DW_TAG_compile_unit:
95554aad 22487 case DW_TAG_partial_unit:
71c25dea
TT
22488 /* Compilation units have a DW_AT_name that is a filename, not
22489 a source language identifier. */
22490 case DW_TAG_enumeration_type:
22491 case DW_TAG_enumerator:
22492 /* These tags always have simple identifiers already; no need
22493 to canonicalize them. */
22494 return DW_STRING (attr);
907af001 22495
96553a0c
DE
22496 case DW_TAG_namespace:
22497 if (attr != NULL && DW_STRING (attr) != NULL)
22498 return DW_STRING (attr);
22499 return CP_ANONYMOUS_NAMESPACE_STR;
22500
907af001
UW
22501 case DW_TAG_class_type:
22502 case DW_TAG_interface_type:
22503 case DW_TAG_structure_type:
22504 case DW_TAG_union_type:
22505 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22506 structures or unions. These were of the form "._%d" in GCC 4.1,
22507 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22508 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22509 if (attr && DW_STRING (attr)
61012eef
GB
22510 && (startswith (DW_STRING (attr), "._")
22511 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22512 return NULL;
53832f31
TT
22513
22514 /* GCC might emit a nameless typedef that has a linkage name. See
22515 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22516 if (!attr || DW_STRING (attr) == NULL)
22517 {
df5c6c50 22518 char *demangled = NULL;
53832f31 22519
73b9be8b 22520 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22521 if (attr == NULL || DW_STRING (attr) == NULL)
22522 return NULL;
22523
df5c6c50
JK
22524 /* Avoid demangling DW_STRING (attr) the second time on a second
22525 call for the same DIE. */
22526 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22527 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22528
22529 if (demangled)
22530 {
e6a959d6 22531 const char *base;
96408a79 22532
53832f31 22533 /* FIXME: we already did this for the partial symbol... */
34a68019 22534 DW_STRING (attr)
224c3ddb 22535 = ((const char *)
e3b94546 22536 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 22537 demangled, strlen (demangled)));
53832f31
TT
22538 DW_STRING_IS_CANONICAL (attr) = 1;
22539 xfree (demangled);
96408a79
SA
22540
22541 /* Strip any leading namespaces/classes, keep only the base name.
22542 DW_AT_name for named DIEs does not contain the prefixes. */
22543 base = strrchr (DW_STRING (attr), ':');
22544 if (base && base > DW_STRING (attr) && base[-1] == ':')
22545 return &base[1];
22546 else
22547 return DW_STRING (attr);
53832f31
TT
22548 }
22549 }
907af001
UW
22550 break;
22551
71c25dea 22552 default:
907af001
UW
22553 break;
22554 }
22555
22556 if (!DW_STRING_IS_CANONICAL (attr))
22557 {
22558 DW_STRING (attr)
22559 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22560 &objfile->per_bfd->storage_obstack);
907af001 22561 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22562 }
907af001 22563 return DW_STRING (attr);
9219021c
DC
22564}
22565
22566/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22567 is none. *EXT_CU is the CU containing DIE on input, and the CU
22568 containing the return value on output. */
9219021c
DC
22569
22570static struct die_info *
f2f0e013 22571dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22572{
22573 struct attribute *attr;
9219021c 22574
f2f0e013 22575 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22576 if (attr == NULL)
22577 return NULL;
22578
f2f0e013 22579 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22580}
22581
c906108c
SS
22582/* Convert a DIE tag into its string name. */
22583
f39c6ffd 22584static const char *
aa1ee363 22585dwarf_tag_name (unsigned tag)
c906108c 22586{
f39c6ffd
TT
22587 const char *name = get_DW_TAG_name (tag);
22588
22589 if (name == NULL)
22590 return "DW_TAG_<unknown>";
22591
22592 return name;
c906108c
SS
22593}
22594
22595/* Convert a DWARF attribute code into its string name. */
22596
f39c6ffd 22597static const char *
aa1ee363 22598dwarf_attr_name (unsigned attr)
c906108c 22599{
f39c6ffd
TT
22600 const char *name;
22601
c764a876 22602#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22603 if (attr == DW_AT_MIPS_fde)
22604 return "DW_AT_MIPS_fde";
22605#else
22606 if (attr == DW_AT_HP_block_index)
22607 return "DW_AT_HP_block_index";
c764a876 22608#endif
f39c6ffd
TT
22609
22610 name = get_DW_AT_name (attr);
22611
22612 if (name == NULL)
22613 return "DW_AT_<unknown>";
22614
22615 return name;
c906108c
SS
22616}
22617
22618/* Convert a DWARF value form code into its string name. */
22619
f39c6ffd 22620static const char *
aa1ee363 22621dwarf_form_name (unsigned form)
c906108c 22622{
f39c6ffd
TT
22623 const char *name = get_DW_FORM_name (form);
22624
22625 if (name == NULL)
22626 return "DW_FORM_<unknown>";
22627
22628 return name;
c906108c
SS
22629}
22630
a121b7c1 22631static const char *
fba45db2 22632dwarf_bool_name (unsigned mybool)
c906108c
SS
22633{
22634 if (mybool)
22635 return "TRUE";
22636 else
22637 return "FALSE";
22638}
22639
22640/* Convert a DWARF type code into its string name. */
22641
f39c6ffd 22642static const char *
aa1ee363 22643dwarf_type_encoding_name (unsigned enc)
c906108c 22644{
f39c6ffd 22645 const char *name = get_DW_ATE_name (enc);
c906108c 22646
f39c6ffd
TT
22647 if (name == NULL)
22648 return "DW_ATE_<unknown>";
c906108c 22649
f39c6ffd 22650 return name;
c906108c 22651}
c906108c 22652
f9aca02d 22653static void
d97bc12b 22654dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22655{
22656 unsigned int i;
22657
d97bc12b
DE
22658 print_spaces (indent, f);
22659 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
9c541725
PA
22660 dwarf_tag_name (die->tag), die->abbrev,
22661 to_underlying (die->sect_off));
d97bc12b
DE
22662
22663 if (die->parent != NULL)
22664 {
22665 print_spaces (indent, f);
22666 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9c541725 22667 to_underlying (die->parent->sect_off));
d97bc12b
DE
22668 }
22669
22670 print_spaces (indent, f);
22671 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22672 dwarf_bool_name (die->child != NULL));
c906108c 22673
d97bc12b
DE
22674 print_spaces (indent, f);
22675 fprintf_unfiltered (f, " attributes:\n");
22676
c906108c
SS
22677 for (i = 0; i < die->num_attrs; ++i)
22678 {
d97bc12b
DE
22679 print_spaces (indent, f);
22680 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22681 dwarf_attr_name (die->attrs[i].name),
22682 dwarf_form_name (die->attrs[i].form));
d97bc12b 22683
c906108c
SS
22684 switch (die->attrs[i].form)
22685 {
c906108c 22686 case DW_FORM_addr:
3019eac3 22687 case DW_FORM_GNU_addr_index:
d97bc12b 22688 fprintf_unfiltered (f, "address: ");
5af949e3 22689 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22690 break;
22691 case DW_FORM_block2:
22692 case DW_FORM_block4:
22693 case DW_FORM_block:
22694 case DW_FORM_block1:
56eb65bd
SP
22695 fprintf_unfiltered (f, "block: size %s",
22696 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22697 break;
2dc7f7b3 22698 case DW_FORM_exprloc:
56eb65bd
SP
22699 fprintf_unfiltered (f, "expression: size %s",
22700 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22701 break;
0224619f
JK
22702 case DW_FORM_data16:
22703 fprintf_unfiltered (f, "constant of 16 bytes");
22704 break;
4568ecf9
DE
22705 case DW_FORM_ref_addr:
22706 fprintf_unfiltered (f, "ref address: ");
22707 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22708 break;
36586728
TT
22709 case DW_FORM_GNU_ref_alt:
22710 fprintf_unfiltered (f, "alt ref address: ");
22711 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22712 break;
10b3939b
DJ
22713 case DW_FORM_ref1:
22714 case DW_FORM_ref2:
22715 case DW_FORM_ref4:
4568ecf9
DE
22716 case DW_FORM_ref8:
22717 case DW_FORM_ref_udata:
d97bc12b 22718 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22719 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22720 break;
c906108c
SS
22721 case DW_FORM_data1:
22722 case DW_FORM_data2:
22723 case DW_FORM_data4:
ce5d95e1 22724 case DW_FORM_data8:
c906108c
SS
22725 case DW_FORM_udata:
22726 case DW_FORM_sdata:
43bbcdc2
PH
22727 fprintf_unfiltered (f, "constant: %s",
22728 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22729 break;
2dc7f7b3
TT
22730 case DW_FORM_sec_offset:
22731 fprintf_unfiltered (f, "section offset: %s",
22732 pulongest (DW_UNSND (&die->attrs[i])));
22733 break;
55f1336d 22734 case DW_FORM_ref_sig8:
ac9ec31b
DE
22735 fprintf_unfiltered (f, "signature: %s",
22736 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22737 break;
c906108c 22738 case DW_FORM_string:
4bdf3d34 22739 case DW_FORM_strp:
43988095 22740 case DW_FORM_line_strp:
3019eac3 22741 case DW_FORM_GNU_str_index:
36586728 22742 case DW_FORM_GNU_strp_alt:
8285870a 22743 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22744 DW_STRING (&die->attrs[i])
8285870a
JK
22745 ? DW_STRING (&die->attrs[i]) : "",
22746 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22747 break;
22748 case DW_FORM_flag:
22749 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22750 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22751 else
d97bc12b 22752 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22753 break;
2dc7f7b3
TT
22754 case DW_FORM_flag_present:
22755 fprintf_unfiltered (f, "flag: TRUE");
22756 break;
a8329558 22757 case DW_FORM_indirect:
0963b4bd
MS
22758 /* The reader will have reduced the indirect form to
22759 the "base form" so this form should not occur. */
3e43a32a
MS
22760 fprintf_unfiltered (f,
22761 "unexpected attribute form: DW_FORM_indirect");
a8329558 22762 break;
663c44ac
JK
22763 case DW_FORM_implicit_const:
22764 fprintf_unfiltered (f, "constant: %s",
22765 plongest (DW_SND (&die->attrs[i])));
22766 break;
c906108c 22767 default:
d97bc12b 22768 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22769 die->attrs[i].form);
d97bc12b 22770 break;
c906108c 22771 }
d97bc12b 22772 fprintf_unfiltered (f, "\n");
c906108c
SS
22773 }
22774}
22775
f9aca02d 22776static void
d97bc12b 22777dump_die_for_error (struct die_info *die)
c906108c 22778{
d97bc12b
DE
22779 dump_die_shallow (gdb_stderr, 0, die);
22780}
22781
22782static void
22783dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22784{
22785 int indent = level * 4;
22786
22787 gdb_assert (die != NULL);
22788
22789 if (level >= max_level)
22790 return;
22791
22792 dump_die_shallow (f, indent, die);
22793
22794 if (die->child != NULL)
c906108c 22795 {
d97bc12b
DE
22796 print_spaces (indent, f);
22797 fprintf_unfiltered (f, " Children:");
22798 if (level + 1 < max_level)
22799 {
22800 fprintf_unfiltered (f, "\n");
22801 dump_die_1 (f, level + 1, max_level, die->child);
22802 }
22803 else
22804 {
3e43a32a
MS
22805 fprintf_unfiltered (f,
22806 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22807 }
22808 }
22809
22810 if (die->sibling != NULL && level > 0)
22811 {
22812 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22813 }
22814}
22815
d97bc12b
DE
22816/* This is called from the pdie macro in gdbinit.in.
22817 It's not static so gcc will keep a copy callable from gdb. */
22818
22819void
22820dump_die (struct die_info *die, int max_level)
22821{
22822 dump_die_1 (gdb_stdlog, 0, max_level, die);
22823}
22824
f9aca02d 22825static void
51545339 22826store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22827{
51545339 22828 void **slot;
c906108c 22829
9c541725
PA
22830 slot = htab_find_slot_with_hash (cu->die_hash, die,
22831 to_underlying (die->sect_off),
b64f50a1 22832 INSERT);
51545339
DJ
22833
22834 *slot = die;
c906108c
SS
22835}
22836
b64f50a1
JK
22837/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22838 required kind. */
22839
22840static sect_offset
ff39bb5e 22841dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22842{
7771576e 22843 if (attr_form_is_ref (attr))
9c541725 22844 return (sect_offset) DW_UNSND (attr);
93311388
DE
22845
22846 complaint (&symfile_complaints,
22847 _("unsupported die ref attribute form: '%s'"),
22848 dwarf_form_name (attr->form));
9c541725 22849 return {};
c906108c
SS
22850}
22851
43bbcdc2
PH
22852/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22853 * the value held by the attribute is not constant. */
a02abb62 22854
43bbcdc2 22855static LONGEST
ff39bb5e 22856dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22857{
663c44ac 22858 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22859 return DW_SND (attr);
22860 else if (attr->form == DW_FORM_udata
22861 || attr->form == DW_FORM_data1
22862 || attr->form == DW_FORM_data2
22863 || attr->form == DW_FORM_data4
22864 || attr->form == DW_FORM_data8)
22865 return DW_UNSND (attr);
22866 else
22867 {
0224619f 22868 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
22869 complaint (&symfile_complaints,
22870 _("Attribute value is not a constant (%s)"),
a02abb62
JB
22871 dwarf_form_name (attr->form));
22872 return default_value;
22873 }
22874}
22875
348e048f
DE
22876/* Follow reference or signature attribute ATTR of SRC_DIE.
22877 On entry *REF_CU is the CU of SRC_DIE.
22878 On exit *REF_CU is the CU of the result. */
22879
22880static struct die_info *
ff39bb5e 22881follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22882 struct dwarf2_cu **ref_cu)
22883{
22884 struct die_info *die;
22885
7771576e 22886 if (attr_form_is_ref (attr))
348e048f 22887 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22888 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22889 die = follow_die_sig (src_die, attr, ref_cu);
22890 else
22891 {
22892 dump_die_for_error (src_die);
22893 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22894 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22895 }
22896
22897 return die;
03dd20cc
DJ
22898}
22899
5c631832 22900/* Follow reference OFFSET.
673bfd45
DE
22901 On entry *REF_CU is the CU of the source die referencing OFFSET.
22902 On exit *REF_CU is the CU of the result.
22903 Returns NULL if OFFSET is invalid. */
f504f079 22904
f9aca02d 22905static struct die_info *
9c541725 22906follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22907 struct dwarf2_cu **ref_cu)
c906108c 22908{
10b3939b 22909 struct die_info temp_die;
f2f0e013 22910 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22911 struct dwarf2_per_objfile *dwarf2_per_objfile
22912 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22913 struct objfile *objfile = dwarf2_per_objfile->objfile;
10b3939b 22914
348e048f
DE
22915 gdb_assert (cu->per_cu != NULL);
22916
98bfdba5
PA
22917 target_cu = cu;
22918
3019eac3 22919 if (cu->per_cu->is_debug_types)
348e048f
DE
22920 {
22921 /* .debug_types CUs cannot reference anything outside their CU.
22922 If they need to, they have to reference a signatured type via
55f1336d 22923 DW_FORM_ref_sig8. */
9c541725 22924 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 22925 return NULL;
348e048f 22926 }
36586728 22927 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 22928 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
22929 {
22930 struct dwarf2_per_cu_data *per_cu;
9a619af0 22931
9c541725 22932 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22933 dwarf2_per_objfile);
03dd20cc
DJ
22934
22935 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
22936 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22937 load_full_comp_unit (per_cu, cu->language);
03dd20cc 22938
10b3939b
DJ
22939 target_cu = per_cu->cu;
22940 }
98bfdba5
PA
22941 else if (cu->dies == NULL)
22942 {
22943 /* We're loading full DIEs during partial symbol reading. */
22944 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 22945 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 22946 }
c906108c 22947
f2f0e013 22948 *ref_cu = target_cu;
9c541725 22949 temp_die.sect_off = sect_off;
9a3c8263 22950 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22951 &temp_die,
22952 to_underlying (sect_off));
5c631832 22953}
10b3939b 22954
5c631832
JK
22955/* Follow reference attribute ATTR of SRC_DIE.
22956 On entry *REF_CU is the CU of SRC_DIE.
22957 On exit *REF_CU is the CU of the result. */
22958
22959static struct die_info *
ff39bb5e 22960follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22961 struct dwarf2_cu **ref_cu)
22962{
9c541725 22963 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22964 struct dwarf2_cu *cu = *ref_cu;
22965 struct die_info *die;
22966
9c541725 22967 die = follow_die_offset (sect_off,
36586728
TT
22968 (attr->form == DW_FORM_GNU_ref_alt
22969 || cu->per_cu->is_dwz),
22970 ref_cu);
5c631832
JK
22971 if (!die)
22972 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
22973 "at 0x%x [in module %s]"),
9c541725 22974 to_underlying (sect_off), to_underlying (src_die->sect_off),
518817b3 22975 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22976
5c631832
JK
22977 return die;
22978}
22979
9c541725 22980/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 22981 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
22982 dwarf2_locexpr_baton->data has lifetime of
22983 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
22984
22985struct dwarf2_locexpr_baton
9c541725 22986dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
22987 struct dwarf2_per_cu_data *per_cu,
22988 CORE_ADDR (*get_frame_pc) (void *baton),
22989 void *baton)
5c631832 22990{
918dd910 22991 struct dwarf2_cu *cu;
5c631832
JK
22992 struct die_info *die;
22993 struct attribute *attr;
22994 struct dwarf2_locexpr_baton retval;
e3b94546 22995 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ed2dc618
SM
22996 struct dwarf2_per_objfile *dwarf2_per_objfile
22997 = get_dwarf2_per_objfile (objfile);
8cf6f0b1 22998
918dd910
JK
22999 if (per_cu->cu == NULL)
23000 load_cu (per_cu);
23001 cu = per_cu->cu;
cc12ce38
DE
23002 if (cu == NULL)
23003 {
23004 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23005 Instead just throw an error, not much else we can do. */
23006 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
e3b94546 23007 to_underlying (sect_off), objfile_name (objfile));
cc12ce38 23008 }
918dd910 23009
9c541725 23010 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832
JK
23011 if (!die)
23012 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
e3b94546 23013 to_underlying (sect_off), objfile_name (objfile));
5c631832
JK
23014
23015 attr = dwarf2_attr (die, DW_AT_location, cu);
23016 if (!attr)
23017 {
e103e986
JK
23018 /* DWARF: "If there is no such attribute, then there is no effect.".
23019 DATA is ignored if SIZE is 0. */
5c631832 23020
e103e986 23021 retval.data = NULL;
5c631832
JK
23022 retval.size = 0;
23023 }
8cf6f0b1
TT
23024 else if (attr_form_is_section_offset (attr))
23025 {
23026 struct dwarf2_loclist_baton loclist_baton;
23027 CORE_ADDR pc = (*get_frame_pc) (baton);
23028 size_t size;
23029
23030 fill_in_loclist_baton (cu, &loclist_baton, attr);
23031
23032 retval.data = dwarf2_find_location_expression (&loclist_baton,
23033 &size, pc);
23034 retval.size = size;
23035 }
5c631832
JK
23036 else
23037 {
23038 if (!attr_form_is_block (attr))
23039 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
23040 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
e3b94546 23041 to_underlying (sect_off), objfile_name (objfile));
5c631832
JK
23042
23043 retval.data = DW_BLOCK (attr)->data;
23044 retval.size = DW_BLOCK (attr)->size;
23045 }
23046 retval.per_cu = cu->per_cu;
918dd910 23047
ed2dc618 23048 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23049
5c631832 23050 return retval;
348e048f
DE
23051}
23052
8b9737bf
TT
23053/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23054 offset. */
23055
23056struct dwarf2_locexpr_baton
23057dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23058 struct dwarf2_per_cu_data *per_cu,
23059 CORE_ADDR (*get_frame_pc) (void *baton),
23060 void *baton)
23061{
9c541725 23062 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23063
9c541725 23064 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23065}
23066
b6807d98
TT
23067/* Write a constant of a given type as target-ordered bytes into
23068 OBSTACK. */
23069
23070static const gdb_byte *
23071write_constant_as_bytes (struct obstack *obstack,
23072 enum bfd_endian byte_order,
23073 struct type *type,
23074 ULONGEST value,
23075 LONGEST *len)
23076{
23077 gdb_byte *result;
23078
23079 *len = TYPE_LENGTH (type);
224c3ddb 23080 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23081 store_unsigned_integer (result, *len, byte_order, value);
23082
23083 return result;
23084}
23085
23086/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23087 pointer to the constant bytes and set LEN to the length of the
23088 data. If memory is needed, allocate it on OBSTACK. If the DIE
23089 does not have a DW_AT_const_value, return NULL. */
23090
23091const gdb_byte *
9c541725 23092dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23093 struct dwarf2_per_cu_data *per_cu,
23094 struct obstack *obstack,
23095 LONGEST *len)
23096{
23097 struct dwarf2_cu *cu;
23098 struct die_info *die;
23099 struct attribute *attr;
23100 const gdb_byte *result = NULL;
23101 struct type *type;
23102 LONGEST value;
23103 enum bfd_endian byte_order;
e3b94546 23104 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23105
b6807d98
TT
23106 if (per_cu->cu == NULL)
23107 load_cu (per_cu);
23108 cu = per_cu->cu;
cc12ce38
DE
23109 if (cu == NULL)
23110 {
23111 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23112 Instead just throw an error, not much else we can do. */
23113 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
e3b94546 23114 to_underlying (sect_off), objfile_name (objfile));
cc12ce38 23115 }
b6807d98 23116
9c541725 23117 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98
TT
23118 if (!die)
23119 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
e3b94546 23120 to_underlying (sect_off), objfile_name (objfile));
b6807d98
TT
23121
23122
23123 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23124 if (attr == NULL)
23125 return NULL;
23126
e3b94546 23127 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23128 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23129
23130 switch (attr->form)
23131 {
23132 case DW_FORM_addr:
23133 case DW_FORM_GNU_addr_index:
23134 {
23135 gdb_byte *tem;
23136
23137 *len = cu->header.addr_size;
224c3ddb 23138 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23139 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23140 result = tem;
23141 }
23142 break;
23143 case DW_FORM_string:
23144 case DW_FORM_strp:
23145 case DW_FORM_GNU_str_index:
23146 case DW_FORM_GNU_strp_alt:
23147 /* DW_STRING is already allocated on the objfile obstack, point
23148 directly to it. */
23149 result = (const gdb_byte *) DW_STRING (attr);
23150 *len = strlen (DW_STRING (attr));
23151 break;
23152 case DW_FORM_block1:
23153 case DW_FORM_block2:
23154 case DW_FORM_block4:
23155 case DW_FORM_block:
23156 case DW_FORM_exprloc:
0224619f 23157 case DW_FORM_data16:
b6807d98
TT
23158 result = DW_BLOCK (attr)->data;
23159 *len = DW_BLOCK (attr)->size;
23160 break;
23161
23162 /* The DW_AT_const_value attributes are supposed to carry the
23163 symbol's value "represented as it would be on the target
23164 architecture." By the time we get here, it's already been
23165 converted to host endianness, so we just need to sign- or
23166 zero-extend it as appropriate. */
23167 case DW_FORM_data1:
23168 type = die_type (die, cu);
23169 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23170 if (result == NULL)
23171 result = write_constant_as_bytes (obstack, byte_order,
23172 type, value, len);
23173 break;
23174 case DW_FORM_data2:
23175 type = die_type (die, cu);
23176 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23177 if (result == NULL)
23178 result = write_constant_as_bytes (obstack, byte_order,
23179 type, value, len);
23180 break;
23181 case DW_FORM_data4:
23182 type = die_type (die, cu);
23183 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23184 if (result == NULL)
23185 result = write_constant_as_bytes (obstack, byte_order,
23186 type, value, len);
23187 break;
23188 case DW_FORM_data8:
23189 type = die_type (die, cu);
23190 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23191 if (result == NULL)
23192 result = write_constant_as_bytes (obstack, byte_order,
23193 type, value, len);
23194 break;
23195
23196 case DW_FORM_sdata:
663c44ac 23197 case DW_FORM_implicit_const:
b6807d98
TT
23198 type = die_type (die, cu);
23199 result = write_constant_as_bytes (obstack, byte_order,
23200 type, DW_SND (attr), len);
23201 break;
23202
23203 case DW_FORM_udata:
23204 type = die_type (die, cu);
23205 result = write_constant_as_bytes (obstack, byte_order,
23206 type, DW_UNSND (attr), len);
23207 break;
23208
23209 default:
23210 complaint (&symfile_complaints,
23211 _("unsupported const value attribute form: '%s'"),
23212 dwarf_form_name (attr->form));
23213 break;
23214 }
23215
23216 return result;
23217}
23218
7942e96e
AA
23219/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23220 valid type for this die is found. */
23221
23222struct type *
9c541725 23223dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23224 struct dwarf2_per_cu_data *per_cu)
23225{
23226 struct dwarf2_cu *cu;
23227 struct die_info *die;
23228
7942e96e
AA
23229 if (per_cu->cu == NULL)
23230 load_cu (per_cu);
23231 cu = per_cu->cu;
23232 if (!cu)
23233 return NULL;
23234
9c541725 23235 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23236 if (!die)
23237 return NULL;
23238
23239 return die_type (die, cu);
23240}
23241
8a9b8146
TT
23242/* Return the type of the DIE at DIE_OFFSET in the CU named by
23243 PER_CU. */
23244
23245struct type *
b64f50a1 23246dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23247 struct dwarf2_per_cu_data *per_cu)
23248{
9c541725 23249 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23250 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23251}
23252
ac9ec31b 23253/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23254 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23255 On exit *REF_CU is the CU of the result.
23256 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23257
23258static struct die_info *
ac9ec31b
DE
23259follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23260 struct dwarf2_cu **ref_cu)
348e048f 23261{
348e048f 23262 struct die_info temp_die;
348e048f
DE
23263 struct dwarf2_cu *sig_cu;
23264 struct die_info *die;
23265
ac9ec31b
DE
23266 /* While it might be nice to assert sig_type->type == NULL here,
23267 we can get here for DW_AT_imported_declaration where we need
23268 the DIE not the type. */
348e048f
DE
23269
23270 /* If necessary, add it to the queue and load its DIEs. */
23271
95554aad 23272 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23273 read_signatured_type (sig_type);
348e048f 23274
348e048f 23275 sig_cu = sig_type->per_cu.cu;
69d751e3 23276 gdb_assert (sig_cu != NULL);
9c541725
PA
23277 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23278 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23279 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23280 to_underlying (temp_die.sect_off));
348e048f
DE
23281 if (die)
23282 {
ed2dc618 23283 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23284 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23285
796a7ff8
DE
23286 /* For .gdb_index version 7 keep track of included TUs.
23287 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23288 if (dwarf2_per_objfile->index_table != NULL
23289 && dwarf2_per_objfile->index_table->version <= 7)
23290 {
23291 VEC_safe_push (dwarf2_per_cu_ptr,
23292 (*ref_cu)->per_cu->imported_symtabs,
23293 sig_cu->per_cu);
23294 }
23295
348e048f
DE
23296 *ref_cu = sig_cu;
23297 return die;
23298 }
23299
ac9ec31b
DE
23300 return NULL;
23301}
23302
23303/* Follow signatured type referenced by ATTR in SRC_DIE.
23304 On entry *REF_CU is the CU of SRC_DIE.
23305 On exit *REF_CU is the CU of the result.
23306 The result is the DIE of the type.
23307 If the referenced type cannot be found an error is thrown. */
23308
23309static struct die_info *
ff39bb5e 23310follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23311 struct dwarf2_cu **ref_cu)
23312{
23313 ULONGEST signature = DW_SIGNATURE (attr);
23314 struct signatured_type *sig_type;
23315 struct die_info *die;
23316
23317 gdb_assert (attr->form == DW_FORM_ref_sig8);
23318
a2ce51a0 23319 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23320 /* sig_type will be NULL if the signatured type is missing from
23321 the debug info. */
23322 if (sig_type == NULL)
23323 {
23324 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23325 " from DIE at 0x%x [in module %s]"),
9c541725 23326 hex_string (signature), to_underlying (src_die->sect_off),
518817b3 23327 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23328 }
23329
23330 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23331 if (die == NULL)
23332 {
23333 dump_die_for_error (src_die);
23334 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23335 " from DIE at 0x%x [in module %s]"),
9c541725 23336 hex_string (signature), to_underlying (src_die->sect_off),
518817b3 23337 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23338 }
23339
23340 return die;
23341}
23342
23343/* Get the type specified by SIGNATURE referenced in DIE/CU,
23344 reading in and processing the type unit if necessary. */
23345
23346static struct type *
23347get_signatured_type (struct die_info *die, ULONGEST signature,
23348 struct dwarf2_cu *cu)
23349{
518817b3
SM
23350 struct dwarf2_per_objfile *dwarf2_per_objfile
23351 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23352 struct signatured_type *sig_type;
23353 struct dwarf2_cu *type_cu;
23354 struct die_info *type_die;
23355 struct type *type;
23356
a2ce51a0 23357 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23358 /* sig_type will be NULL if the signatured type is missing from
23359 the debug info. */
23360 if (sig_type == NULL)
23361 {
23362 complaint (&symfile_complaints,
23363 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23364 " from DIE at 0x%x [in module %s]"),
9c541725 23365 hex_string (signature), to_underlying (die->sect_off),
4262abfb 23366 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23367 return build_error_marker_type (cu, die);
23368 }
23369
23370 /* If we already know the type we're done. */
23371 if (sig_type->type != NULL)
23372 return sig_type->type;
23373
23374 type_cu = cu;
23375 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23376 if (type_die != NULL)
23377 {
23378 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23379 is created. This is important, for example, because for c++ classes
23380 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23381 type = read_type_die (type_die, type_cu);
23382 if (type == NULL)
23383 {
23384 complaint (&symfile_complaints,
23385 _("Dwarf Error: Cannot build signatured type %s"
23386 " referenced from DIE at 0x%x [in module %s]"),
9c541725 23387 hex_string (signature), to_underlying (die->sect_off),
4262abfb 23388 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23389 type = build_error_marker_type (cu, die);
23390 }
23391 }
23392 else
23393 {
23394 complaint (&symfile_complaints,
23395 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23396 " from DIE at 0x%x [in module %s]"),
9c541725 23397 hex_string (signature), to_underlying (die->sect_off),
4262abfb 23398 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23399 type = build_error_marker_type (cu, die);
23400 }
23401 sig_type->type = type;
23402
23403 return type;
23404}
23405
23406/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23407 reading in and processing the type unit if necessary. */
23408
23409static struct type *
ff39bb5e 23410get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23411 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23412{
23413 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23414 if (attr_form_is_ref (attr))
ac9ec31b
DE
23415 {
23416 struct dwarf2_cu *type_cu = cu;
23417 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23418
23419 return read_type_die (type_die, type_cu);
23420 }
23421 else if (attr->form == DW_FORM_ref_sig8)
23422 {
23423 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23424 }
23425 else
23426 {
518817b3
SM
23427 struct dwarf2_per_objfile *dwarf2_per_objfile
23428 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23429
ac9ec31b
DE
23430 complaint (&symfile_complaints,
23431 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23432 " at 0x%x [in module %s]"),
9c541725 23433 dwarf_form_name (attr->form), to_underlying (die->sect_off),
4262abfb 23434 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23435 return build_error_marker_type (cu, die);
23436 }
348e048f
DE
23437}
23438
e5fe5e75 23439/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23440
23441static void
e5fe5e75 23442load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23443{
52dc124a 23444 struct signatured_type *sig_type;
348e048f 23445
f4dc4d17
DE
23446 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23447 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23448
6721b2ec
DE
23449 /* We have the per_cu, but we need the signatured_type.
23450 Fortunately this is an easy translation. */
23451 gdb_assert (per_cu->is_debug_types);
23452 sig_type = (struct signatured_type *) per_cu;
348e048f 23453
6721b2ec 23454 gdb_assert (per_cu->cu == NULL);
348e048f 23455
52dc124a 23456 read_signatured_type (sig_type);
348e048f 23457
6721b2ec 23458 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23459}
23460
dee91e82
DE
23461/* die_reader_func for read_signatured_type.
23462 This is identical to load_full_comp_unit_reader,
23463 but is kept separate for now. */
348e048f
DE
23464
23465static void
dee91e82 23466read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23467 const gdb_byte *info_ptr,
dee91e82
DE
23468 struct die_info *comp_unit_die,
23469 int has_children,
23470 void *data)
348e048f 23471{
dee91e82 23472 struct dwarf2_cu *cu = reader->cu;
348e048f 23473
dee91e82
DE
23474 gdb_assert (cu->die_hash == NULL);
23475 cu->die_hash =
23476 htab_create_alloc_ex (cu->header.length / 12,
23477 die_hash,
23478 die_eq,
23479 NULL,
23480 &cu->comp_unit_obstack,
23481 hashtab_obstack_allocate,
23482 dummy_obstack_deallocate);
348e048f 23483
dee91e82
DE
23484 if (has_children)
23485 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23486 &info_ptr, comp_unit_die);
23487 cu->dies = comp_unit_die;
23488 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23489
23490 /* We try not to read any attributes in this function, because not
9cdd5dbd 23491 all CUs needed for references have been loaded yet, and symbol
348e048f 23492 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23493 or we won't be able to build types correctly.
23494 Similarly, if we do not read the producer, we can not apply
23495 producer-specific interpretation. */
95554aad 23496 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23497}
348e048f 23498
3019eac3
DE
23499/* Read in a signatured type and build its CU and DIEs.
23500 If the type is a stub for the real type in a DWO file,
23501 read in the real type from the DWO file as well. */
dee91e82
DE
23502
23503static void
23504read_signatured_type (struct signatured_type *sig_type)
23505{
23506 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23507
3019eac3 23508 gdb_assert (per_cu->is_debug_types);
dee91e82 23509 gdb_assert (per_cu->cu == NULL);
348e048f 23510
f4dc4d17
DE
23511 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23512 read_signatured_type_reader, NULL);
7ee85ab1 23513 sig_type->per_cu.tu_read = 1;
c906108c
SS
23514}
23515
c906108c
SS
23516/* Decode simple location descriptions.
23517 Given a pointer to a dwarf block that defines a location, compute
23518 the location and return the value.
23519
4cecd739
DJ
23520 NOTE drow/2003-11-18: This function is called in two situations
23521 now: for the address of static or global variables (partial symbols
23522 only) and for offsets into structures which are expected to be
23523 (more or less) constant. The partial symbol case should go away,
23524 and only the constant case should remain. That will let this
23525 function complain more accurately. A few special modes are allowed
23526 without complaint for global variables (for instance, global
23527 register values and thread-local values).
c906108c
SS
23528
23529 A location description containing no operations indicates that the
4cecd739 23530 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23531 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23532 callers will only want a very basic result and this can become a
21ae7a4d
JK
23533 complaint.
23534
23535 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23536
23537static CORE_ADDR
e7c27a73 23538decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23539{
518817b3 23540 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23541 size_t i;
23542 size_t size = blk->size;
d521ce57 23543 const gdb_byte *data = blk->data;
21ae7a4d
JK
23544 CORE_ADDR stack[64];
23545 int stacki;
23546 unsigned int bytes_read, unsnd;
23547 gdb_byte op;
c906108c 23548
21ae7a4d
JK
23549 i = 0;
23550 stacki = 0;
23551 stack[stacki] = 0;
23552 stack[++stacki] = 0;
23553
23554 while (i < size)
23555 {
23556 op = data[i++];
23557 switch (op)
23558 {
23559 case DW_OP_lit0:
23560 case DW_OP_lit1:
23561 case DW_OP_lit2:
23562 case DW_OP_lit3:
23563 case DW_OP_lit4:
23564 case DW_OP_lit5:
23565 case DW_OP_lit6:
23566 case DW_OP_lit7:
23567 case DW_OP_lit8:
23568 case DW_OP_lit9:
23569 case DW_OP_lit10:
23570 case DW_OP_lit11:
23571 case DW_OP_lit12:
23572 case DW_OP_lit13:
23573 case DW_OP_lit14:
23574 case DW_OP_lit15:
23575 case DW_OP_lit16:
23576 case DW_OP_lit17:
23577 case DW_OP_lit18:
23578 case DW_OP_lit19:
23579 case DW_OP_lit20:
23580 case DW_OP_lit21:
23581 case DW_OP_lit22:
23582 case DW_OP_lit23:
23583 case DW_OP_lit24:
23584 case DW_OP_lit25:
23585 case DW_OP_lit26:
23586 case DW_OP_lit27:
23587 case DW_OP_lit28:
23588 case DW_OP_lit29:
23589 case DW_OP_lit30:
23590 case DW_OP_lit31:
23591 stack[++stacki] = op - DW_OP_lit0;
23592 break;
f1bea926 23593
21ae7a4d
JK
23594 case DW_OP_reg0:
23595 case DW_OP_reg1:
23596 case DW_OP_reg2:
23597 case DW_OP_reg3:
23598 case DW_OP_reg4:
23599 case DW_OP_reg5:
23600 case DW_OP_reg6:
23601 case DW_OP_reg7:
23602 case DW_OP_reg8:
23603 case DW_OP_reg9:
23604 case DW_OP_reg10:
23605 case DW_OP_reg11:
23606 case DW_OP_reg12:
23607 case DW_OP_reg13:
23608 case DW_OP_reg14:
23609 case DW_OP_reg15:
23610 case DW_OP_reg16:
23611 case DW_OP_reg17:
23612 case DW_OP_reg18:
23613 case DW_OP_reg19:
23614 case DW_OP_reg20:
23615 case DW_OP_reg21:
23616 case DW_OP_reg22:
23617 case DW_OP_reg23:
23618 case DW_OP_reg24:
23619 case DW_OP_reg25:
23620 case DW_OP_reg26:
23621 case DW_OP_reg27:
23622 case DW_OP_reg28:
23623 case DW_OP_reg29:
23624 case DW_OP_reg30:
23625 case DW_OP_reg31:
23626 stack[++stacki] = op - DW_OP_reg0;
23627 if (i < size)
23628 dwarf2_complex_location_expr_complaint ();
23629 break;
c906108c 23630
21ae7a4d
JK
23631 case DW_OP_regx:
23632 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23633 i += bytes_read;
23634 stack[++stacki] = unsnd;
23635 if (i < size)
23636 dwarf2_complex_location_expr_complaint ();
23637 break;
c906108c 23638
21ae7a4d
JK
23639 case DW_OP_addr:
23640 stack[++stacki] = read_address (objfile->obfd, &data[i],
23641 cu, &bytes_read);
23642 i += bytes_read;
23643 break;
d53d4ac5 23644
21ae7a4d
JK
23645 case DW_OP_const1u:
23646 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23647 i += 1;
23648 break;
23649
23650 case DW_OP_const1s:
23651 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23652 i += 1;
23653 break;
23654
23655 case DW_OP_const2u:
23656 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23657 i += 2;
23658 break;
23659
23660 case DW_OP_const2s:
23661 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23662 i += 2;
23663 break;
d53d4ac5 23664
21ae7a4d
JK
23665 case DW_OP_const4u:
23666 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23667 i += 4;
23668 break;
23669
23670 case DW_OP_const4s:
23671 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23672 i += 4;
23673 break;
23674
585861ea
JK
23675 case DW_OP_const8u:
23676 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23677 i += 8;
23678 break;
23679
21ae7a4d
JK
23680 case DW_OP_constu:
23681 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23682 &bytes_read);
23683 i += bytes_read;
23684 break;
23685
23686 case DW_OP_consts:
23687 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23688 i += bytes_read;
23689 break;
23690
23691 case DW_OP_dup:
23692 stack[stacki + 1] = stack[stacki];
23693 stacki++;
23694 break;
23695
23696 case DW_OP_plus:
23697 stack[stacki - 1] += stack[stacki];
23698 stacki--;
23699 break;
23700
23701 case DW_OP_plus_uconst:
23702 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23703 &bytes_read);
23704 i += bytes_read;
23705 break;
23706
23707 case DW_OP_minus:
23708 stack[stacki - 1] -= stack[stacki];
23709 stacki--;
23710 break;
23711
23712 case DW_OP_deref:
23713 /* If we're not the last op, then we definitely can't encode
23714 this using GDB's address_class enum. This is valid for partial
23715 global symbols, although the variable's address will be bogus
23716 in the psymtab. */
23717 if (i < size)
23718 dwarf2_complex_location_expr_complaint ();
23719 break;
23720
23721 case DW_OP_GNU_push_tls_address:
4aa4e28b 23722 case DW_OP_form_tls_address:
21ae7a4d
JK
23723 /* The top of the stack has the offset from the beginning
23724 of the thread control block at which the variable is located. */
23725 /* Nothing should follow this operator, so the top of stack would
23726 be returned. */
23727 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23728 address will be bogus in the psymtab. Make it always at least
23729 non-zero to not look as a variable garbage collected by linker
23730 which have DW_OP_addr 0. */
21ae7a4d
JK
23731 if (i < size)
23732 dwarf2_complex_location_expr_complaint ();
585861ea 23733 stack[stacki]++;
21ae7a4d
JK
23734 break;
23735
23736 case DW_OP_GNU_uninit:
23737 break;
23738
3019eac3 23739 case DW_OP_GNU_addr_index:
49f6c839 23740 case DW_OP_GNU_const_index:
3019eac3
DE
23741 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23742 &bytes_read);
23743 i += bytes_read;
23744 break;
23745
21ae7a4d
JK
23746 default:
23747 {
f39c6ffd 23748 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23749
23750 if (name)
23751 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23752 name);
23753 else
23754 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23755 op);
23756 }
23757
23758 return (stack[stacki]);
d53d4ac5 23759 }
3c6e0cb3 23760
21ae7a4d
JK
23761 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23762 outside of the allocated space. Also enforce minimum>0. */
23763 if (stacki >= ARRAY_SIZE (stack) - 1)
23764 {
23765 complaint (&symfile_complaints,
23766 _("location description stack overflow"));
23767 return 0;
23768 }
23769
23770 if (stacki <= 0)
23771 {
23772 complaint (&symfile_complaints,
23773 _("location description stack underflow"));
23774 return 0;
23775 }
23776 }
23777 return (stack[stacki]);
c906108c
SS
23778}
23779
23780/* memory allocation interface */
23781
c906108c 23782static struct dwarf_block *
7b5a2f43 23783dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23784{
8d749320 23785 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23786}
23787
c906108c 23788static struct die_info *
b60c80d6 23789dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23790{
23791 struct die_info *die;
b60c80d6
DJ
23792 size_t size = sizeof (struct die_info);
23793
23794 if (num_attrs > 1)
23795 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23796
b60c80d6 23797 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23798 memset (die, 0, sizeof (struct die_info));
23799 return (die);
23800}
2e276125
JB
23801
23802\f
23803/* Macro support. */
23804
233d95b5
JK
23805/* Return file name relative to the compilation directory of file number I in
23806 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 23807 responsible for freeing it. */
233d95b5 23808
2e276125 23809static char *
233d95b5 23810file_file_name (int file, struct line_header *lh)
2e276125 23811{
6a83a1e6
EZ
23812 /* Is the file number a valid index into the line header's file name
23813 table? Remember that file numbers start with one, not zero. */
fff8551c 23814 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 23815 {
8c43009f 23816 const file_entry &fe = lh->file_names[file - 1];
6e70227d 23817
8c43009f
PA
23818 if (!IS_ABSOLUTE_PATH (fe.name))
23819 {
23820 const char *dir = fe.include_dir (lh);
23821 if (dir != NULL)
23822 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23823 }
23824 return xstrdup (fe.name);
6a83a1e6 23825 }
2e276125
JB
23826 else
23827 {
6a83a1e6
EZ
23828 /* The compiler produced a bogus file number. We can at least
23829 record the macro definitions made in the file, even if we
23830 won't be able to find the file by name. */
23831 char fake_name[80];
9a619af0 23832
8c042590
PM
23833 xsnprintf (fake_name, sizeof (fake_name),
23834 "<bad macro file number %d>", file);
2e276125 23835
6e70227d 23836 complaint (&symfile_complaints,
6a83a1e6
EZ
23837 _("bad file number in macro information (%d)"),
23838 file);
2e276125 23839
6a83a1e6 23840 return xstrdup (fake_name);
2e276125
JB
23841 }
23842}
23843
233d95b5
JK
23844/* Return the full name of file number I in *LH's file name table.
23845 Use COMP_DIR as the name of the current directory of the
23846 compilation. The result is allocated using xmalloc; the caller is
23847 responsible for freeing it. */
23848static char *
23849file_full_name (int file, struct line_header *lh, const char *comp_dir)
23850{
23851 /* Is the file number a valid index into the line header's file name
23852 table? Remember that file numbers start with one, not zero. */
fff8551c 23853 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
23854 {
23855 char *relative = file_file_name (file, lh);
23856
23857 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23858 return relative;
b36cec19
PA
23859 return reconcat (relative, comp_dir, SLASH_STRING,
23860 relative, (char *) NULL);
233d95b5
JK
23861 }
23862 else
23863 return file_file_name (file, lh);
23864}
23865
2e276125
JB
23866
23867static struct macro_source_file *
23868macro_start_file (int file, int line,
23869 struct macro_source_file *current_file,
43f3e411 23870 struct line_header *lh)
2e276125 23871{
233d95b5
JK
23872 /* File name relative to the compilation directory of this source file. */
23873 char *file_name = file_file_name (file, lh);
2e276125 23874
2e276125 23875 if (! current_file)
abc9d0dc 23876 {
fc474241
DE
23877 /* Note: We don't create a macro table for this compilation unit
23878 at all until we actually get a filename. */
43f3e411 23879 struct macro_table *macro_table = get_macro_table ();
fc474241 23880
abc9d0dc
TT
23881 /* If we have no current file, then this must be the start_file
23882 directive for the compilation unit's main source file. */
fc474241
DE
23883 current_file = macro_set_main (macro_table, file_name);
23884 macro_define_special (macro_table);
abc9d0dc 23885 }
2e276125 23886 else
233d95b5 23887 current_file = macro_include (current_file, line, file_name);
2e276125 23888
233d95b5 23889 xfree (file_name);
6e70227d 23890
2e276125
JB
23891 return current_file;
23892}
23893
2e276125
JB
23894static const char *
23895consume_improper_spaces (const char *p, const char *body)
23896{
23897 if (*p == ' ')
23898 {
4d3c2250 23899 complaint (&symfile_complaints,
3e43a32a
MS
23900 _("macro definition contains spaces "
23901 "in formal argument list:\n`%s'"),
4d3c2250 23902 body);
2e276125
JB
23903
23904 while (*p == ' ')
23905 p++;
23906 }
23907
23908 return p;
23909}
23910
23911
23912static void
23913parse_macro_definition (struct macro_source_file *file, int line,
23914 const char *body)
23915{
23916 const char *p;
23917
23918 /* The body string takes one of two forms. For object-like macro
23919 definitions, it should be:
23920
23921 <macro name> " " <definition>
23922
23923 For function-like macro definitions, it should be:
23924
23925 <macro name> "() " <definition>
23926 or
23927 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23928
23929 Spaces may appear only where explicitly indicated, and in the
23930 <definition>.
23931
23932 The Dwarf 2 spec says that an object-like macro's name is always
23933 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23934 the space when the macro's definition is the empty string.
2e276125
JB
23935
23936 The Dwarf 2 spec says that there should be no spaces between the
23937 formal arguments in a function-like macro's formal argument list,
23938 but versions of GCC around March 2002 include spaces after the
23939 commas. */
23940
23941
23942 /* Find the extent of the macro name. The macro name is terminated
23943 by either a space or null character (for an object-like macro) or
23944 an opening paren (for a function-like macro). */
23945 for (p = body; *p; p++)
23946 if (*p == ' ' || *p == '(')
23947 break;
23948
23949 if (*p == ' ' || *p == '\0')
23950 {
23951 /* It's an object-like macro. */
23952 int name_len = p - body;
3f8a7804 23953 char *name = savestring (body, name_len);
2e276125
JB
23954 const char *replacement;
23955
23956 if (*p == ' ')
23957 replacement = body + name_len + 1;
23958 else
23959 {
4d3c2250 23960 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23961 replacement = body + name_len;
23962 }
6e70227d 23963
2e276125
JB
23964 macro_define_object (file, line, name, replacement);
23965
23966 xfree (name);
23967 }
23968 else if (*p == '(')
23969 {
23970 /* It's a function-like macro. */
3f8a7804 23971 char *name = savestring (body, p - body);
2e276125
JB
23972 int argc = 0;
23973 int argv_size = 1;
8d749320 23974 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23975
23976 p++;
23977
23978 p = consume_improper_spaces (p, body);
23979
23980 /* Parse the formal argument list. */
23981 while (*p && *p != ')')
23982 {
23983 /* Find the extent of the current argument name. */
23984 const char *arg_start = p;
23985
23986 while (*p && *p != ',' && *p != ')' && *p != ' ')
23987 p++;
23988
23989 if (! *p || p == arg_start)
4d3c2250 23990 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23991 else
23992 {
23993 /* Make sure argv has room for the new argument. */
23994 if (argc >= argv_size)
23995 {
23996 argv_size *= 2;
224c3ddb 23997 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
23998 }
23999
3f8a7804 24000 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24001 }
24002
24003 p = consume_improper_spaces (p, body);
24004
24005 /* Consume the comma, if present. */
24006 if (*p == ',')
24007 {
24008 p++;
24009
24010 p = consume_improper_spaces (p, body);
24011 }
24012 }
24013
24014 if (*p == ')')
24015 {
24016 p++;
24017
24018 if (*p == ' ')
24019 /* Perfectly formed definition, no complaints. */
24020 macro_define_function (file, line, name,
6e70227d 24021 argc, (const char **) argv,
2e276125
JB
24022 p + 1);
24023 else if (*p == '\0')
24024 {
24025 /* Complain, but do define it. */
4d3c2250 24026 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24027 macro_define_function (file, line, name,
6e70227d 24028 argc, (const char **) argv,
2e276125
JB
24029 p);
24030 }
24031 else
24032 /* Just complain. */
4d3c2250 24033 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24034 }
24035 else
24036 /* Just complain. */
4d3c2250 24037 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24038
24039 xfree (name);
24040 {
24041 int i;
24042
24043 for (i = 0; i < argc; i++)
24044 xfree (argv[i]);
24045 }
24046 xfree (argv);
24047 }
24048 else
4d3c2250 24049 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24050}
24051
cf2c3c16
TT
24052/* Skip some bytes from BYTES according to the form given in FORM.
24053 Returns the new pointer. */
2e276125 24054
d521ce57
TT
24055static const gdb_byte *
24056skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24057 enum dwarf_form form,
24058 unsigned int offset_size,
24059 struct dwarf2_section_info *section)
2e276125 24060{
cf2c3c16 24061 unsigned int bytes_read;
2e276125 24062
cf2c3c16 24063 switch (form)
2e276125 24064 {
cf2c3c16
TT
24065 case DW_FORM_data1:
24066 case DW_FORM_flag:
24067 ++bytes;
24068 break;
24069
24070 case DW_FORM_data2:
24071 bytes += 2;
24072 break;
24073
24074 case DW_FORM_data4:
24075 bytes += 4;
24076 break;
24077
24078 case DW_FORM_data8:
24079 bytes += 8;
24080 break;
24081
0224619f
JK
24082 case DW_FORM_data16:
24083 bytes += 16;
24084 break;
24085
cf2c3c16
TT
24086 case DW_FORM_string:
24087 read_direct_string (abfd, bytes, &bytes_read);
24088 bytes += bytes_read;
24089 break;
24090
24091 case DW_FORM_sec_offset:
24092 case DW_FORM_strp:
36586728 24093 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24094 bytes += offset_size;
24095 break;
24096
24097 case DW_FORM_block:
24098 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24099 bytes += bytes_read;
24100 break;
24101
24102 case DW_FORM_block1:
24103 bytes += 1 + read_1_byte (abfd, bytes);
24104 break;
24105 case DW_FORM_block2:
24106 bytes += 2 + read_2_bytes (abfd, bytes);
24107 break;
24108 case DW_FORM_block4:
24109 bytes += 4 + read_4_bytes (abfd, bytes);
24110 break;
24111
24112 case DW_FORM_sdata:
24113 case DW_FORM_udata:
3019eac3
DE
24114 case DW_FORM_GNU_addr_index:
24115 case DW_FORM_GNU_str_index:
d521ce57 24116 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24117 if (bytes == NULL)
24118 {
24119 dwarf2_section_buffer_overflow_complaint (section);
24120 return NULL;
24121 }
cf2c3c16
TT
24122 break;
24123
663c44ac
JK
24124 case DW_FORM_implicit_const:
24125 break;
24126
cf2c3c16
TT
24127 default:
24128 {
cf2c3c16
TT
24129 complaint (&symfile_complaints,
24130 _("invalid form 0x%x in `%s'"),
a32a8923 24131 form, get_section_name (section));
cf2c3c16
TT
24132 return NULL;
24133 }
2e276125
JB
24134 }
24135
cf2c3c16
TT
24136 return bytes;
24137}
757a13d0 24138
cf2c3c16
TT
24139/* A helper for dwarf_decode_macros that handles skipping an unknown
24140 opcode. Returns an updated pointer to the macro data buffer; or,
24141 on error, issues a complaint and returns NULL. */
757a13d0 24142
d521ce57 24143static const gdb_byte *
cf2c3c16 24144skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24145 const gdb_byte **opcode_definitions,
24146 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24147 bfd *abfd,
24148 unsigned int offset_size,
24149 struct dwarf2_section_info *section)
24150{
24151 unsigned int bytes_read, i;
24152 unsigned long arg;
d521ce57 24153 const gdb_byte *defn;
2e276125 24154
cf2c3c16 24155 if (opcode_definitions[opcode] == NULL)
2e276125 24156 {
cf2c3c16
TT
24157 complaint (&symfile_complaints,
24158 _("unrecognized DW_MACFINO opcode 0x%x"),
24159 opcode);
24160 return NULL;
24161 }
2e276125 24162
cf2c3c16
TT
24163 defn = opcode_definitions[opcode];
24164 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24165 defn += bytes_read;
2e276125 24166
cf2c3c16
TT
24167 for (i = 0; i < arg; ++i)
24168 {
aead7601
SM
24169 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24170 (enum dwarf_form) defn[i], offset_size,
f664829e 24171 section);
cf2c3c16
TT
24172 if (mac_ptr == NULL)
24173 {
24174 /* skip_form_bytes already issued the complaint. */
24175 return NULL;
24176 }
24177 }
757a13d0 24178
cf2c3c16
TT
24179 return mac_ptr;
24180}
757a13d0 24181
cf2c3c16
TT
24182/* A helper function which parses the header of a macro section.
24183 If the macro section is the extended (for now called "GNU") type,
24184 then this updates *OFFSET_SIZE. Returns a pointer to just after
24185 the header, or issues a complaint and returns NULL on error. */
757a13d0 24186
d521ce57
TT
24187static const gdb_byte *
24188dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24189 bfd *abfd,
d521ce57 24190 const gdb_byte *mac_ptr,
cf2c3c16
TT
24191 unsigned int *offset_size,
24192 int section_is_gnu)
24193{
24194 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24195
cf2c3c16
TT
24196 if (section_is_gnu)
24197 {
24198 unsigned int version, flags;
757a13d0 24199
cf2c3c16 24200 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24201 if (version != 4 && version != 5)
cf2c3c16
TT
24202 {
24203 complaint (&symfile_complaints,
24204 _("unrecognized version `%d' in .debug_macro section"),
24205 version);
24206 return NULL;
24207 }
24208 mac_ptr += 2;
757a13d0 24209
cf2c3c16
TT
24210 flags = read_1_byte (abfd, mac_ptr);
24211 ++mac_ptr;
24212 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24213
cf2c3c16
TT
24214 if ((flags & 2) != 0)
24215 /* We don't need the line table offset. */
24216 mac_ptr += *offset_size;
757a13d0 24217
cf2c3c16
TT
24218 /* Vendor opcode descriptions. */
24219 if ((flags & 4) != 0)
24220 {
24221 unsigned int i, count;
757a13d0 24222
cf2c3c16
TT
24223 count = read_1_byte (abfd, mac_ptr);
24224 ++mac_ptr;
24225 for (i = 0; i < count; ++i)
24226 {
24227 unsigned int opcode, bytes_read;
24228 unsigned long arg;
24229
24230 opcode = read_1_byte (abfd, mac_ptr);
24231 ++mac_ptr;
24232 opcode_definitions[opcode] = mac_ptr;
24233 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24234 mac_ptr += bytes_read;
24235 mac_ptr += arg;
24236 }
757a13d0 24237 }
cf2c3c16 24238 }
757a13d0 24239
cf2c3c16
TT
24240 return mac_ptr;
24241}
757a13d0 24242
cf2c3c16 24243/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24244 including DW_MACRO_import. */
cf2c3c16
TT
24245
24246static void
ed2dc618
SM
24247dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24248 bfd *abfd,
d521ce57 24249 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24250 struct macro_source_file *current_file,
43f3e411 24251 struct line_header *lh,
cf2c3c16 24252 struct dwarf2_section_info *section,
36586728 24253 int section_is_gnu, int section_is_dwz,
cf2c3c16 24254 unsigned int offset_size,
8fc3fc34 24255 htab_t include_hash)
cf2c3c16 24256{
4d663531 24257 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24258 enum dwarf_macro_record_type macinfo_type;
24259 int at_commandline;
d521ce57 24260 const gdb_byte *opcode_definitions[256];
757a13d0 24261
cf2c3c16
TT
24262 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24263 &offset_size, section_is_gnu);
24264 if (mac_ptr == NULL)
24265 {
24266 /* We already issued a complaint. */
24267 return;
24268 }
757a13d0
JK
24269
24270 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24271 GDB is still reading the definitions from command line. First
24272 DW_MACINFO_start_file will need to be ignored as it was already executed
24273 to create CURRENT_FILE for the main source holding also the command line
24274 definitions. On first met DW_MACINFO_start_file this flag is reset to
24275 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24276
24277 at_commandline = 1;
24278
24279 do
24280 {
24281 /* Do we at least have room for a macinfo type byte? */
24282 if (mac_ptr >= mac_end)
24283 {
f664829e 24284 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24285 break;
24286 }
24287
aead7601 24288 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24289 mac_ptr++;
24290
cf2c3c16
TT
24291 /* Note that we rely on the fact that the corresponding GNU and
24292 DWARF constants are the same. */
132448f8
SM
24293 DIAGNOSTIC_PUSH
24294 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24295 switch (macinfo_type)
24296 {
24297 /* A zero macinfo type indicates the end of the macro
24298 information. */
24299 case 0:
24300 break;
2e276125 24301
0af92d60
JK
24302 case DW_MACRO_define:
24303 case DW_MACRO_undef:
24304 case DW_MACRO_define_strp:
24305 case DW_MACRO_undef_strp:
24306 case DW_MACRO_define_sup:
24307 case DW_MACRO_undef_sup:
2e276125 24308 {
891d2f0b 24309 unsigned int bytes_read;
2e276125 24310 int line;
d521ce57 24311 const char *body;
cf2c3c16 24312 int is_define;
2e276125 24313
cf2c3c16
TT
24314 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24315 mac_ptr += bytes_read;
24316
0af92d60
JK
24317 if (macinfo_type == DW_MACRO_define
24318 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24319 {
24320 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24321 mac_ptr += bytes_read;
24322 }
24323 else
24324 {
24325 LONGEST str_offset;
24326
24327 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24328 mac_ptr += offset_size;
2e276125 24329
0af92d60
JK
24330 if (macinfo_type == DW_MACRO_define_sup
24331 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24332 || section_is_dwz)
36586728 24333 {
ed2dc618
SM
24334 struct dwz_file *dwz
24335 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24336
ed2dc618
SM
24337 body = read_indirect_string_from_dwz (objfile,
24338 dwz, str_offset);
36586728
TT
24339 }
24340 else
ed2dc618
SM
24341 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24342 abfd, str_offset);
cf2c3c16
TT
24343 }
24344
0af92d60
JK
24345 is_define = (macinfo_type == DW_MACRO_define
24346 || macinfo_type == DW_MACRO_define_strp
24347 || macinfo_type == DW_MACRO_define_sup);
2e276125 24348 if (! current_file)
757a13d0
JK
24349 {
24350 /* DWARF violation as no main source is present. */
24351 complaint (&symfile_complaints,
24352 _("debug info with no main source gives macro %s "
24353 "on line %d: %s"),
cf2c3c16
TT
24354 is_define ? _("definition") : _("undefinition"),
24355 line, body);
757a13d0
JK
24356 break;
24357 }
3e43a32a
MS
24358 if ((line == 0 && !at_commandline)
24359 || (line != 0 && at_commandline))
4d3c2250 24360 complaint (&symfile_complaints,
757a13d0
JK
24361 _("debug info gives %s macro %s with %s line %d: %s"),
24362 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24363 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24364 line == 0 ? _("zero") : _("non-zero"), line, body);
24365
cf2c3c16 24366 if (is_define)
757a13d0 24367 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24368 else
24369 {
0af92d60
JK
24370 gdb_assert (macinfo_type == DW_MACRO_undef
24371 || macinfo_type == DW_MACRO_undef_strp
24372 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24373 macro_undef (current_file, line, body);
24374 }
2e276125
JB
24375 }
24376 break;
24377
0af92d60 24378 case DW_MACRO_start_file:
2e276125 24379 {
891d2f0b 24380 unsigned int bytes_read;
2e276125
JB
24381 int line, file;
24382
24383 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24384 mac_ptr += bytes_read;
24385 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24386 mac_ptr += bytes_read;
24387
3e43a32a
MS
24388 if ((line == 0 && !at_commandline)
24389 || (line != 0 && at_commandline))
757a13d0
JK
24390 complaint (&symfile_complaints,
24391 _("debug info gives source %d included "
24392 "from %s at %s line %d"),
24393 file, at_commandline ? _("command-line") : _("file"),
24394 line == 0 ? _("zero") : _("non-zero"), line);
24395
24396 if (at_commandline)
24397 {
0af92d60 24398 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24399 pass one. */
757a13d0
JK
24400 at_commandline = 0;
24401 }
24402 else
43f3e411 24403 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
24404 }
24405 break;
24406
0af92d60 24407 case DW_MACRO_end_file:
2e276125 24408 if (! current_file)
4d3c2250 24409 complaint (&symfile_complaints,
3e43a32a
MS
24410 _("macro debug info has an unmatched "
24411 "`close_file' directive"));
2e276125
JB
24412 else
24413 {
24414 current_file = current_file->included_by;
24415 if (! current_file)
24416 {
cf2c3c16 24417 enum dwarf_macro_record_type next_type;
2e276125
JB
24418
24419 /* GCC circa March 2002 doesn't produce the zero
24420 type byte marking the end of the compilation
24421 unit. Complain if it's not there, but exit no
24422 matter what. */
24423
24424 /* Do we at least have room for a macinfo type byte? */
24425 if (mac_ptr >= mac_end)
24426 {
f664829e 24427 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24428 return;
24429 }
24430
24431 /* We don't increment mac_ptr here, so this is just
24432 a look-ahead. */
aead7601
SM
24433 next_type
24434 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24435 mac_ptr);
2e276125 24436 if (next_type != 0)
4d3c2250 24437 complaint (&symfile_complaints,
3e43a32a
MS
24438 _("no terminating 0-type entry for "
24439 "macros in `.debug_macinfo' section"));
2e276125
JB
24440
24441 return;
24442 }
24443 }
24444 break;
24445
0af92d60
JK
24446 case DW_MACRO_import:
24447 case DW_MACRO_import_sup:
cf2c3c16
TT
24448 {
24449 LONGEST offset;
8fc3fc34 24450 void **slot;
a036ba48
TT
24451 bfd *include_bfd = abfd;
24452 struct dwarf2_section_info *include_section = section;
d521ce57 24453 const gdb_byte *include_mac_end = mac_end;
a036ba48 24454 int is_dwz = section_is_dwz;
d521ce57 24455 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24456
24457 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24458 mac_ptr += offset_size;
24459
0af92d60 24460 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24461 {
ed2dc618 24462 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24463
4d663531 24464 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24465
a036ba48 24466 include_section = &dwz->macro;
a32a8923 24467 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24468 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24469 is_dwz = 1;
24470 }
24471
24472 new_mac_ptr = include_section->buffer + offset;
24473 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24474
8fc3fc34
TT
24475 if (*slot != NULL)
24476 {
24477 /* This has actually happened; see
24478 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24479 complaint (&symfile_complaints,
0af92d60 24480 _("recursive DW_MACRO_import in "
8fc3fc34
TT
24481 ".debug_macro section"));
24482 }
24483 else
24484 {
d521ce57 24485 *slot = (void *) new_mac_ptr;
36586728 24486
ed2dc618
SM
24487 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24488 include_bfd, new_mac_ptr,
43f3e411 24489 include_mac_end, current_file, lh,
36586728 24490 section, section_is_gnu, is_dwz,
4d663531 24491 offset_size, include_hash);
8fc3fc34 24492
d521ce57 24493 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24494 }
cf2c3c16
TT
24495 }
24496 break;
24497
2e276125 24498 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24499 if (!section_is_gnu)
24500 {
24501 unsigned int bytes_read;
2e276125 24502
ac298888
TT
24503 /* This reads the constant, but since we don't recognize
24504 any vendor extensions, we ignore it. */
24505 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24506 mac_ptr += bytes_read;
24507 read_direct_string (abfd, mac_ptr, &bytes_read);
24508 mac_ptr += bytes_read;
2e276125 24509
cf2c3c16
TT
24510 /* We don't recognize any vendor extensions. */
24511 break;
24512 }
24513 /* FALLTHROUGH */
24514
24515 default:
24516 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24517 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24518 section);
24519 if (mac_ptr == NULL)
24520 return;
24521 break;
2e276125 24522 }
132448f8 24523 DIAGNOSTIC_POP
757a13d0 24524 } while (macinfo_type != 0);
2e276125 24525}
8e19ed76 24526
cf2c3c16 24527static void
09262596 24528dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24529 int section_is_gnu)
cf2c3c16 24530{
518817b3
SM
24531 struct dwarf2_per_objfile *dwarf2_per_objfile
24532 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24533 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24534 struct line_header *lh = cu->line_header;
24535 bfd *abfd;
d521ce57 24536 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24537 struct macro_source_file *current_file = 0;
24538 enum dwarf_macro_record_type macinfo_type;
24539 unsigned int offset_size = cu->header.offset_size;
d521ce57 24540 const gdb_byte *opcode_definitions[256];
8fc3fc34 24541 void **slot;
09262596
DE
24542 struct dwarf2_section_info *section;
24543 const char *section_name;
24544
24545 if (cu->dwo_unit != NULL)
24546 {
24547 if (section_is_gnu)
24548 {
24549 section = &cu->dwo_unit->dwo_file->sections.macro;
24550 section_name = ".debug_macro.dwo";
24551 }
24552 else
24553 {
24554 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24555 section_name = ".debug_macinfo.dwo";
24556 }
24557 }
24558 else
24559 {
24560 if (section_is_gnu)
24561 {
24562 section = &dwarf2_per_objfile->macro;
24563 section_name = ".debug_macro";
24564 }
24565 else
24566 {
24567 section = &dwarf2_per_objfile->macinfo;
24568 section_name = ".debug_macinfo";
24569 }
24570 }
cf2c3c16 24571
bb5ed363 24572 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24573 if (section->buffer == NULL)
24574 {
fceca515 24575 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
24576 return;
24577 }
a32a8923 24578 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24579
24580 /* First pass: Find the name of the base filename.
24581 This filename is needed in order to process all macros whose definition
24582 (or undefinition) comes from the command line. These macros are defined
24583 before the first DW_MACINFO_start_file entry, and yet still need to be
24584 associated to the base file.
24585
24586 To determine the base file name, we scan the macro definitions until we
24587 reach the first DW_MACINFO_start_file entry. We then initialize
24588 CURRENT_FILE accordingly so that any macro definition found before the
24589 first DW_MACINFO_start_file can still be associated to the base file. */
24590
24591 mac_ptr = section->buffer + offset;
24592 mac_end = section->buffer + section->size;
24593
24594 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24595 &offset_size, section_is_gnu);
24596 if (mac_ptr == NULL)
24597 {
24598 /* We already issued a complaint. */
24599 return;
24600 }
24601
24602 do
24603 {
24604 /* Do we at least have room for a macinfo type byte? */
24605 if (mac_ptr >= mac_end)
24606 {
24607 /* Complaint is printed during the second pass as GDB will probably
24608 stop the first pass earlier upon finding
24609 DW_MACINFO_start_file. */
24610 break;
24611 }
24612
aead7601 24613 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24614 mac_ptr++;
24615
24616 /* Note that we rely on the fact that the corresponding GNU and
24617 DWARF constants are the same. */
132448f8
SM
24618 DIAGNOSTIC_PUSH
24619 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
24620 switch (macinfo_type)
24621 {
24622 /* A zero macinfo type indicates the end of the macro
24623 information. */
24624 case 0:
24625 break;
24626
0af92d60
JK
24627 case DW_MACRO_define:
24628 case DW_MACRO_undef:
cf2c3c16
TT
24629 /* Only skip the data by MAC_PTR. */
24630 {
24631 unsigned int bytes_read;
24632
24633 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24634 mac_ptr += bytes_read;
24635 read_direct_string (abfd, mac_ptr, &bytes_read);
24636 mac_ptr += bytes_read;
24637 }
24638 break;
24639
0af92d60 24640 case DW_MACRO_start_file:
cf2c3c16
TT
24641 {
24642 unsigned int bytes_read;
24643 int line, file;
24644
24645 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24646 mac_ptr += bytes_read;
24647 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24648 mac_ptr += bytes_read;
24649
43f3e411 24650 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
24651 }
24652 break;
24653
0af92d60 24654 case DW_MACRO_end_file:
cf2c3c16
TT
24655 /* No data to skip by MAC_PTR. */
24656 break;
24657
0af92d60
JK
24658 case DW_MACRO_define_strp:
24659 case DW_MACRO_undef_strp:
24660 case DW_MACRO_define_sup:
24661 case DW_MACRO_undef_sup:
cf2c3c16
TT
24662 {
24663 unsigned int bytes_read;
24664
24665 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24666 mac_ptr += bytes_read;
24667 mac_ptr += offset_size;
24668 }
24669 break;
24670
0af92d60
JK
24671 case DW_MACRO_import:
24672 case DW_MACRO_import_sup:
cf2c3c16 24673 /* Note that, according to the spec, a transparent include
0af92d60 24674 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
24675 skip this opcode. */
24676 mac_ptr += offset_size;
24677 break;
24678
24679 case DW_MACINFO_vendor_ext:
24680 /* Only skip the data by MAC_PTR. */
24681 if (!section_is_gnu)
24682 {
24683 unsigned int bytes_read;
24684
24685 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24686 mac_ptr += bytes_read;
24687 read_direct_string (abfd, mac_ptr, &bytes_read);
24688 mac_ptr += bytes_read;
24689 }
24690 /* FALLTHROUGH */
24691
24692 default:
24693 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24694 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24695 section);
24696 if (mac_ptr == NULL)
24697 return;
24698 break;
24699 }
132448f8 24700 DIAGNOSTIC_POP
cf2c3c16
TT
24701 } while (macinfo_type != 0 && current_file == NULL);
24702
24703 /* Second pass: Process all entries.
24704
24705 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24706 command-line macro definitions/undefinitions. This flag is unset when we
24707 reach the first DW_MACINFO_start_file entry. */
24708
fc4007c9
TT
24709 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24710 htab_eq_pointer,
24711 NULL, xcalloc, xfree));
8fc3fc34 24712 mac_ptr = section->buffer + offset;
fc4007c9 24713 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 24714 *slot = (void *) mac_ptr;
ed2dc618
SM
24715 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24716 abfd, mac_ptr, mac_end,
43f3e411 24717 current_file, lh, section,
fc4007c9
TT
24718 section_is_gnu, 0, offset_size,
24719 include_hash.get ());
cf2c3c16
TT
24720}
24721
8e19ed76 24722/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 24723 if so return true else false. */
380bca97 24724
8e19ed76 24725static int
6e5a29e1 24726attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
24727{
24728 return (attr == NULL ? 0 :
24729 attr->form == DW_FORM_block1
24730 || attr->form == DW_FORM_block2
24731 || attr->form == DW_FORM_block4
2dc7f7b3
TT
24732 || attr->form == DW_FORM_block
24733 || attr->form == DW_FORM_exprloc);
8e19ed76 24734}
4c2df51b 24735
c6a0999f
JB
24736/* Return non-zero if ATTR's value is a section offset --- classes
24737 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24738 You may use DW_UNSND (attr) to retrieve such offsets.
24739
24740 Section 7.5.4, "Attribute Encodings", explains that no attribute
24741 may have a value that belongs to more than one of these classes; it
24742 would be ambiguous if we did, because we use the same forms for all
24743 of them. */
380bca97 24744
3690dd37 24745static int
6e5a29e1 24746attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
24747{
24748 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
24749 || attr->form == DW_FORM_data8
24750 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
24751}
24752
3690dd37
JB
24753/* Return non-zero if ATTR's value falls in the 'constant' class, or
24754 zero otherwise. When this function returns true, you can apply
24755 dwarf2_get_attr_constant_value to it.
24756
24757 However, note that for some attributes you must check
24758 attr_form_is_section_offset before using this test. DW_FORM_data4
24759 and DW_FORM_data8 are members of both the constant class, and of
24760 the classes that contain offsets into other debug sections
24761 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24762 that, if an attribute's can be either a constant or one of the
24763 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
24764 taken as section offsets, not constants.
24765
24766 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24767 cannot handle that. */
380bca97 24768
3690dd37 24769static int
6e5a29e1 24770attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
24771{
24772 switch (attr->form)
24773 {
24774 case DW_FORM_sdata:
24775 case DW_FORM_udata:
24776 case DW_FORM_data1:
24777 case DW_FORM_data2:
24778 case DW_FORM_data4:
24779 case DW_FORM_data8:
663c44ac 24780 case DW_FORM_implicit_const:
3690dd37
JB
24781 return 1;
24782 default:
24783 return 0;
24784 }
24785}
24786
7771576e
SA
24787
24788/* DW_ADDR is always stored already as sect_offset; despite for the forms
24789 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24790
24791static int
6e5a29e1 24792attr_form_is_ref (const struct attribute *attr)
7771576e
SA
24793{
24794 switch (attr->form)
24795 {
24796 case DW_FORM_ref_addr:
24797 case DW_FORM_ref1:
24798 case DW_FORM_ref2:
24799 case DW_FORM_ref4:
24800 case DW_FORM_ref8:
24801 case DW_FORM_ref_udata:
24802 case DW_FORM_GNU_ref_alt:
24803 return 1;
24804 default:
24805 return 0;
24806 }
24807}
24808
3019eac3
DE
24809/* Return the .debug_loc section to use for CU.
24810 For DWO files use .debug_loc.dwo. */
24811
24812static struct dwarf2_section_info *
24813cu_debug_loc_section (struct dwarf2_cu *cu)
24814{
518817b3
SM
24815 struct dwarf2_per_objfile *dwarf2_per_objfile
24816 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24817
3019eac3 24818 if (cu->dwo_unit)
43988095
JK
24819 {
24820 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24821
24822 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24823 }
24824 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24825 : &dwarf2_per_objfile->loc);
3019eac3
DE
24826}
24827
8cf6f0b1
TT
24828/* A helper function that fills in a dwarf2_loclist_baton. */
24829
24830static void
24831fill_in_loclist_baton (struct dwarf2_cu *cu,
24832 struct dwarf2_loclist_baton *baton,
ff39bb5e 24833 const struct attribute *attr)
8cf6f0b1 24834{
518817b3
SM
24835 struct dwarf2_per_objfile *dwarf2_per_objfile
24836 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
24837 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24838
24839 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
24840
24841 baton->per_cu = cu->per_cu;
24842 gdb_assert (baton->per_cu);
24843 /* We don't know how long the location list is, but make sure we
24844 don't run off the edge of the section. */
3019eac3
DE
24845 baton->size = section->size - DW_UNSND (attr);
24846 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 24847 baton->base_address = cu->base_address;
f664829e 24848 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24849}
24850
4c2df51b 24851static void
ff39bb5e 24852dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24853 struct dwarf2_cu *cu, int is_block)
4c2df51b 24854{
518817b3
SM
24855 struct dwarf2_per_objfile *dwarf2_per_objfile
24856 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24857 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 24858 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24859
3690dd37 24860 if (attr_form_is_section_offset (attr)
3019eac3 24861 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24862 the section. If so, fall through to the complaint in the
24863 other branch. */
3019eac3 24864 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 24865 {
0d53c4c4 24866 struct dwarf2_loclist_baton *baton;
4c2df51b 24867
8d749320 24868 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24869
8cf6f0b1 24870 fill_in_loclist_baton (cu, baton, attr);
be391dca 24871
d00adf39 24872 if (cu->base_known == 0)
0d53c4c4 24873 complaint (&symfile_complaints,
3e43a32a
MS
24874 _("Location list used without "
24875 "specifying the CU base address."));
4c2df51b 24876
f1e6e072
TT
24877 SYMBOL_ACLASS_INDEX (sym) = (is_block
24878 ? dwarf2_loclist_block_index
24879 : dwarf2_loclist_index);
0d53c4c4
DJ
24880 SYMBOL_LOCATION_BATON (sym) = baton;
24881 }
24882 else
24883 {
24884 struct dwarf2_locexpr_baton *baton;
24885
8d749320 24886 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
24887 baton->per_cu = cu->per_cu;
24888 gdb_assert (baton->per_cu);
0d53c4c4
DJ
24889
24890 if (attr_form_is_block (attr))
24891 {
24892 /* Note that we're just copying the block's data pointer
24893 here, not the actual data. We're still pointing into the
6502dd73
DJ
24894 info_buffer for SYM's objfile; right now we never release
24895 that buffer, but when we do clean up properly this may
24896 need to change. */
0d53c4c4
DJ
24897 baton->size = DW_BLOCK (attr)->size;
24898 baton->data = DW_BLOCK (attr)->data;
24899 }
24900 else
24901 {
24902 dwarf2_invalid_attrib_class_complaint ("location description",
24903 SYMBOL_NATURAL_NAME (sym));
24904 baton->size = 0;
0d53c4c4 24905 }
6e70227d 24906
f1e6e072
TT
24907 SYMBOL_ACLASS_INDEX (sym) = (is_block
24908 ? dwarf2_locexpr_block_index
24909 : dwarf2_locexpr_index);
0d53c4c4
DJ
24910 SYMBOL_LOCATION_BATON (sym) = baton;
24911 }
4c2df51b 24912}
6502dd73 24913
9aa1f1e3
TT
24914/* Return the OBJFILE associated with the compilation unit CU. If CU
24915 came from a separate debuginfo file, then the master objfile is
24916 returned. */
ae0d2f24
UW
24917
24918struct objfile *
24919dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24920{
e3b94546 24921 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
24922
24923 /* Return the master objfile, so that we can report and look up the
24924 correct file containing this variable. */
24925 if (objfile->separate_debug_objfile_backlink)
24926 objfile = objfile->separate_debug_objfile_backlink;
24927
24928 return objfile;
24929}
24930
96408a79
SA
24931/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24932 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24933 CU_HEADERP first. */
24934
24935static const struct comp_unit_head *
24936per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24937 struct dwarf2_per_cu_data *per_cu)
24938{
d521ce57 24939 const gdb_byte *info_ptr;
96408a79
SA
24940
24941 if (per_cu->cu)
24942 return &per_cu->cu->header;
24943
9c541725 24944 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24945
24946 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24947 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24948 rcuh_kind::COMPILE);
96408a79
SA
24949
24950 return cu_headerp;
24951}
24952
ae0d2f24
UW
24953/* Return the address size given in the compilation unit header for CU. */
24954
98714339 24955int
ae0d2f24
UW
24956dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24957{
96408a79
SA
24958 struct comp_unit_head cu_header_local;
24959 const struct comp_unit_head *cu_headerp;
c471e790 24960
96408a79
SA
24961 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24962
24963 return cu_headerp->addr_size;
ae0d2f24
UW
24964}
24965
9eae7c52
TT
24966/* Return the offset size given in the compilation unit header for CU. */
24967
24968int
24969dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24970{
96408a79
SA
24971 struct comp_unit_head cu_header_local;
24972 const struct comp_unit_head *cu_headerp;
9c6c53f7 24973
96408a79
SA
24974 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24975
24976 return cu_headerp->offset_size;
24977}
24978
24979/* See its dwarf2loc.h declaration. */
24980
24981int
24982dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24983{
24984 struct comp_unit_head cu_header_local;
24985 const struct comp_unit_head *cu_headerp;
24986
24987 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24988
24989 if (cu_headerp->version == 2)
24990 return cu_headerp->addr_size;
24991 else
24992 return cu_headerp->offset_size;
181cebd4
JK
24993}
24994
9aa1f1e3
TT
24995/* Return the text offset of the CU. The returned offset comes from
24996 this CU's objfile. If this objfile came from a separate debuginfo
24997 file, then the offset may be different from the corresponding
24998 offset in the parent objfile. */
24999
25000CORE_ADDR
25001dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25002{
e3b94546 25003 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
25004
25005 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25006}
25007
43988095
JK
25008/* Return DWARF version number of PER_CU. */
25009
25010short
25011dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25012{
25013 return per_cu->dwarf_version;
25014}
25015
348e048f
DE
25016/* Locate the .debug_info compilation unit from CU's objfile which contains
25017 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25018
25019static struct dwarf2_per_cu_data *
9c541725 25020dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25021 unsigned int offset_in_dwz,
ed2dc618 25022 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25023{
25024 struct dwarf2_per_cu_data *this_cu;
25025 int low, high;
36586728 25026 const sect_offset *cu_off;
ae038cb0 25027
ae038cb0
DJ
25028 low = 0;
25029 high = dwarf2_per_objfile->n_comp_units - 1;
25030 while (high > low)
25031 {
36586728 25032 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25033 int mid = low + (high - low) / 2;
9a619af0 25034
36586728 25035 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 25036 cu_off = &mid_cu->sect_off;
36586728 25037 if (mid_cu->is_dwz > offset_in_dwz
9c541725 25038 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
25039 high = mid;
25040 else
25041 low = mid + 1;
25042 }
25043 gdb_assert (low == high);
36586728 25044 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
25045 cu_off = &this_cu->sect_off;
25046 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 25047 {
36586728 25048 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25049 error (_("Dwarf Error: could not find partial DIE containing "
9c541725 25050 "offset 0x%x [in module %s]"),
ed2dc618
SM
25051 to_underlying (sect_off),
25052 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25053
9c541725
PA
25054 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25055 <= sect_off);
ae038cb0
DJ
25056 return dwarf2_per_objfile->all_comp_units[low-1];
25057 }
25058 else
25059 {
25060 this_cu = dwarf2_per_objfile->all_comp_units[low];
25061 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725
PA
25062 && sect_off >= this_cu->sect_off + this_cu->length)
25063 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
25064 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25065 return this_cu;
25066 }
25067}
25068
23745b47 25069/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25070
fcd3b13d
SM
25071dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25072 : per_cu (per_cu_),
25073 mark (0),
25074 has_loclist (0),
25075 checked_producer (0),
25076 producer_is_gxx_lt_4_6 (0),
25077 producer_is_gcc_lt_4_3 (0),
25078 producer_is_icc_lt_14 (0),
25079 processing_has_namespace_info (0)
93311388 25080{
fcd3b13d
SM
25081 per_cu->cu = this;
25082}
25083
25084/* Destroy a dwarf2_cu. */
25085
25086dwarf2_cu::~dwarf2_cu ()
25087{
25088 per_cu->cu = NULL;
9816fde3
JK
25089}
25090
25091/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25092
25093static void
95554aad
TT
25094prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25095 enum language pretend_language)
9816fde3
JK
25096{
25097 struct attribute *attr;
25098
25099 /* Set the language we're debugging. */
25100 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25101 if (attr)
25102 set_cu_language (DW_UNSND (attr), cu);
25103 else
9cded63f 25104 {
95554aad 25105 cu->language = pretend_language;
9cded63f
TT
25106 cu->language_defn = language_def (cu->language);
25107 }
dee91e82 25108
7d45c7c3 25109 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25110}
25111
ae038cb0
DJ
25112/* Free all cached compilation units. */
25113
25114static void
25115free_cached_comp_units (void *data)
25116{
ed2dc618
SM
25117 struct dwarf2_per_objfile *dwarf2_per_objfile
25118 = (struct dwarf2_per_objfile *) data;
25119
330cdd98 25120 dwarf2_per_objfile->free_cached_comp_units ();
ae038cb0
DJ
25121}
25122
25123/* Increase the age counter on each cached compilation unit, and free
25124 any that are too old. */
25125
25126static void
ed2dc618 25127age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25128{
25129 struct dwarf2_per_cu_data *per_cu, **last_chain;
25130
25131 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25132 per_cu = dwarf2_per_objfile->read_in_chain;
25133 while (per_cu != NULL)
25134 {
25135 per_cu->cu->last_used ++;
b4f54984 25136 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25137 dwarf2_mark (per_cu->cu);
25138 per_cu = per_cu->cu->read_in_chain;
25139 }
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
25149 if (!per_cu->cu->mark)
25150 {
fcd3b13d 25151 delete per_cu->cu;
ae038cb0
DJ
25152 *last_chain = next_cu;
25153 }
25154 else
25155 last_chain = &per_cu->cu->read_in_chain;
25156
25157 per_cu = next_cu;
25158 }
25159}
25160
25161/* Remove a single compilation unit from the cache. */
25162
25163static void
dee91e82 25164free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25165{
25166 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25167 struct dwarf2_per_objfile *dwarf2_per_objfile
25168 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25169
25170 per_cu = dwarf2_per_objfile->read_in_chain;
25171 last_chain = &dwarf2_per_objfile->read_in_chain;
25172 while (per_cu != NULL)
25173 {
25174 struct dwarf2_per_cu_data *next_cu;
25175
25176 next_cu = per_cu->cu->read_in_chain;
25177
dee91e82 25178 if (per_cu == target_per_cu)
ae038cb0 25179 {
fcd3b13d 25180 delete per_cu->cu;
dee91e82 25181 per_cu->cu = NULL;
ae038cb0
DJ
25182 *last_chain = next_cu;
25183 break;
25184 }
25185 else
25186 last_chain = &per_cu->cu->read_in_chain;
25187
25188 per_cu = next_cu;
25189 }
25190}
25191
fe3e1990
DJ
25192/* Release all extra memory associated with OBJFILE. */
25193
25194void
25195dwarf2_free_objfile (struct objfile *objfile)
25196{
ed2dc618
SM
25197 struct dwarf2_per_objfile *dwarf2_per_objfile
25198 = get_dwarf2_per_objfile (objfile);
fe3e1990
DJ
25199
25200 if (dwarf2_per_objfile == NULL)
25201 return;
25202
330cdd98 25203 dwarf2_per_objfile->~dwarf2_per_objfile ();
fe3e1990
DJ
25204}
25205
dee91e82
DE
25206/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25207 We store these in a hash table separate from the DIEs, and preserve them
25208 when the DIEs are flushed out of cache.
25209
25210 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25211 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25212 or the type may come from a DWO file. Furthermore, while it's more logical
25213 to use per_cu->section+offset, with Fission the section with the data is in
25214 the DWO file but we don't know that section at the point we need it.
25215 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25216 because we can enter the lookup routine, get_die_type_at_offset, from
25217 outside this file, and thus won't necessarily have PER_CU->cu.
25218 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25219
dee91e82 25220struct dwarf2_per_cu_offset_and_type
1c379e20 25221{
dee91e82 25222 const struct dwarf2_per_cu_data *per_cu;
9c541725 25223 sect_offset sect_off;
1c379e20
DJ
25224 struct type *type;
25225};
25226
dee91e82 25227/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25228
25229static hashval_t
dee91e82 25230per_cu_offset_and_type_hash (const void *item)
1c379e20 25231{
9a3c8263
SM
25232 const struct dwarf2_per_cu_offset_and_type *ofs
25233 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25234
9c541725 25235 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25236}
25237
dee91e82 25238/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25239
25240static int
dee91e82 25241per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25242{
9a3c8263
SM
25243 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25244 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25245 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25246 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25247
dee91e82 25248 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25249 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25250}
25251
25252/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25253 table if necessary. For convenience, return TYPE.
25254
25255 The DIEs reading must have careful ordering to:
25256 * Not cause infite loops trying to read in DIEs as a prerequisite for
25257 reading current DIE.
25258 * Not trying to dereference contents of still incompletely read in types
25259 while reading in other DIEs.
25260 * Enable referencing still incompletely read in types just by a pointer to
25261 the type without accessing its fields.
25262
25263 Therefore caller should follow these rules:
25264 * Try to fetch any prerequisite types we may need to build this DIE type
25265 before building the type and calling set_die_type.
e71ec853 25266 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25267 possible before fetching more types to complete the current type.
25268 * Make the type as complete as possible before fetching more types. */
1c379e20 25269
f792889a 25270static struct type *
1c379e20
DJ
25271set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25272{
518817b3
SM
25273 struct dwarf2_per_objfile *dwarf2_per_objfile
25274 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25275 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25276 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25277 struct attribute *attr;
25278 struct dynamic_prop prop;
1c379e20 25279
b4ba55a1
JB
25280 /* For Ada types, make sure that the gnat-specific data is always
25281 initialized (if not already set). There are a few types where
25282 we should not be doing so, because the type-specific area is
25283 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25284 where the type-specific area is used to store the floatformat).
25285 But this is not a problem, because the gnat-specific information
25286 is actually not needed for these types. */
25287 if (need_gnat_info (cu)
25288 && TYPE_CODE (type) != TYPE_CODE_FUNC
25289 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25290 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25291 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25292 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25293 && !HAVE_GNAT_AUX_INFO (type))
25294 INIT_GNAT_SPECIFIC (type);
25295
3f2f83dd
KB
25296 /* Read DW_AT_allocated and set in type. */
25297 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25298 if (attr_form_is_block (attr))
25299 {
25300 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25301 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25302 }
25303 else if (attr != NULL)
25304 {
25305 complaint (&symfile_complaints,
9c541725
PA
25306 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
25307 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25308 to_underlying (die->sect_off));
3f2f83dd
KB
25309 }
25310
25311 /* Read DW_AT_associated and set in type. */
25312 attr = dwarf2_attr (die, DW_AT_associated, cu);
25313 if (attr_form_is_block (attr))
25314 {
25315 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25316 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25317 }
25318 else if (attr != NULL)
25319 {
25320 complaint (&symfile_complaints,
9c541725
PA
25321 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
25322 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25323 to_underlying (die->sect_off));
3f2f83dd
KB
25324 }
25325
3cdcd0ce
JB
25326 /* Read DW_AT_data_location and set in type. */
25327 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25328 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25329 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25330
dee91e82 25331 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25332 {
dee91e82
DE
25333 dwarf2_per_objfile->die_type_hash =
25334 htab_create_alloc_ex (127,
25335 per_cu_offset_and_type_hash,
25336 per_cu_offset_and_type_eq,
25337 NULL,
25338 &objfile->objfile_obstack,
25339 hashtab_obstack_allocate,
25340 dummy_obstack_deallocate);
f792889a 25341 }
1c379e20 25342
dee91e82 25343 ofs.per_cu = cu->per_cu;
9c541725 25344 ofs.sect_off = die->sect_off;
1c379e20 25345 ofs.type = type;
dee91e82
DE
25346 slot = (struct dwarf2_per_cu_offset_and_type **)
25347 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
25348 if (*slot)
25349 complaint (&symfile_complaints,
25350 _("A problem internal to GDB: DIE 0x%x has type already set"),
9c541725 25351 to_underlying (die->sect_off));
8d749320
SM
25352 *slot = XOBNEW (&objfile->objfile_obstack,
25353 struct dwarf2_per_cu_offset_and_type);
1c379e20 25354 **slot = ofs;
f792889a 25355 return type;
1c379e20
DJ
25356}
25357
9c541725 25358/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25359 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25360
25361static struct type *
9c541725 25362get_die_type_at_offset (sect_offset sect_off,
673bfd45 25363 struct dwarf2_per_cu_data *per_cu)
1c379e20 25364{
dee91e82 25365 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25366 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25367
dee91e82 25368 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25369 return NULL;
1c379e20 25370
dee91e82 25371 ofs.per_cu = per_cu;
9c541725 25372 ofs.sect_off = sect_off;
9a3c8263
SM
25373 slot = ((struct dwarf2_per_cu_offset_and_type *)
25374 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25375 if (slot)
25376 return slot->type;
25377 else
25378 return NULL;
25379}
25380
02142a6c 25381/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25382 or return NULL if DIE does not have a saved type. */
25383
25384static struct type *
25385get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25386{
9c541725 25387 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25388}
25389
10b3939b
DJ
25390/* Add a dependence relationship from CU to REF_PER_CU. */
25391
25392static void
25393dwarf2_add_dependence (struct dwarf2_cu *cu,
25394 struct dwarf2_per_cu_data *ref_per_cu)
25395{
25396 void **slot;
25397
25398 if (cu->dependencies == NULL)
25399 cu->dependencies
25400 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25401 NULL, &cu->comp_unit_obstack,
25402 hashtab_obstack_allocate,
25403 dummy_obstack_deallocate);
25404
25405 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25406 if (*slot == NULL)
25407 *slot = ref_per_cu;
25408}
1c379e20 25409
f504f079
DE
25410/* Subroutine of dwarf2_mark to pass to htab_traverse.
25411 Set the mark field in every compilation unit in the
ae038cb0
DJ
25412 cache that we must keep because we are keeping CU. */
25413
10b3939b
DJ
25414static int
25415dwarf2_mark_helper (void **slot, void *data)
25416{
25417 struct dwarf2_per_cu_data *per_cu;
25418
25419 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25420
25421 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25422 reading of the chain. As such dependencies remain valid it is not much
25423 useful to track and undo them during QUIT cleanups. */
25424 if (per_cu->cu == NULL)
25425 return 1;
25426
10b3939b
DJ
25427 if (per_cu->cu->mark)
25428 return 1;
25429 per_cu->cu->mark = 1;
25430
25431 if (per_cu->cu->dependencies != NULL)
25432 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25433
25434 return 1;
25435}
25436
f504f079
DE
25437/* Set the mark field in CU and in every other compilation unit in the
25438 cache that we must keep because we are keeping CU. */
25439
ae038cb0
DJ
25440static void
25441dwarf2_mark (struct dwarf2_cu *cu)
25442{
25443 if (cu->mark)
25444 return;
25445 cu->mark = 1;
10b3939b
DJ
25446 if (cu->dependencies != NULL)
25447 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25448}
25449
25450static void
25451dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25452{
25453 while (per_cu)
25454 {
25455 per_cu->cu->mark = 0;
25456 per_cu = per_cu->cu->read_in_chain;
25457 }
72bf9492
DJ
25458}
25459
72bf9492
DJ
25460/* Trivial hash function for partial_die_info: the hash value of a DIE
25461 is its offset in .debug_info for this objfile. */
25462
25463static hashval_t
25464partial_die_hash (const void *item)
25465{
9a3c8263
SM
25466 const struct partial_die_info *part_die
25467 = (const struct partial_die_info *) item;
9a619af0 25468
9c541725 25469 return to_underlying (part_die->sect_off);
72bf9492
DJ
25470}
25471
25472/* Trivial comparison function for partial_die_info structures: two DIEs
25473 are equal if they have the same offset. */
25474
25475static int
25476partial_die_eq (const void *item_lhs, const void *item_rhs)
25477{
9a3c8263
SM
25478 const struct partial_die_info *part_die_lhs
25479 = (const struct partial_die_info *) item_lhs;
25480 const struct partial_die_info *part_die_rhs
25481 = (const struct partial_die_info *) item_rhs;
9a619af0 25482
9c541725 25483 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25484}
25485
b4f54984
DE
25486static struct cmd_list_element *set_dwarf_cmdlist;
25487static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25488
25489static void
981a3fb3 25490set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25491{
b4f54984 25492 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25493 gdb_stdout);
ae038cb0
DJ
25494}
25495
25496static void
981a3fb3 25497show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25498{
b4f54984 25499 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25500}
25501
ae2de4f8 25502/* The "save gdb-index" command. */
9291a0cd 25503
437afbb8
JK
25504/* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25505 error checking. */
25506
25507static void
25508file_write (FILE *file, const void *data, size_t size)
25509{
25510 if (fwrite (data, 1, size, file) != size)
25511 error (_("couldn't data write to file"));
25512}
25513
25514/* Write the contents of VEC to FILE, with error checking. */
25515
25516template<typename Elem, typename Alloc>
25517static void
25518file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25519{
25520 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25521}
25522
bc8f2430
JK
25523/* In-memory buffer to prepare data to be written later to a file. */
25524class data_buf
9291a0cd 25525{
bc8f2430 25526public:
bc8f2430
JK
25527 /* Copy DATA to the end of the buffer. */
25528 template<typename T>
25529 void append_data (const T &data)
25530 {
25531 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25532 reinterpret_cast<const gdb_byte *> (&data + 1),
c2f134ac 25533 grow (sizeof (data)));
bc8f2430 25534 }
b89be57b 25535
c2f134ac
PA
25536 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25537 terminating zero is appended too. */
bc8f2430
JK
25538 void append_cstr0 (const char *cstr)
25539 {
25540 const size_t size = strlen (cstr) + 1;
c2f134ac
PA
25541 std::copy (cstr, cstr + size, grow (size));
25542 }
25543
437afbb8
JK
25544 /* Store INPUT as ULEB128 to the end of buffer. */
25545 void append_unsigned_leb128 (ULONGEST input)
25546 {
25547 for (;;)
25548 {
25549 gdb_byte output = input & 0x7f;
25550 input >>= 7;
25551 if (input)
25552 output |= 0x80;
25553 append_data (output);
25554 if (input == 0)
25555 break;
25556 }
25557 }
25558
c2f134ac
PA
25559 /* Accept a host-format integer in VAL and append it to the buffer
25560 as a target-format integer which is LEN bytes long. */
25561 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25562 {
25563 ::store_unsigned_integer (grow (len), len, byte_order, val);
bc8f2430 25564 }
9291a0cd 25565
bc8f2430
JK
25566 /* Return the size of the buffer. */
25567 size_t size () const
25568 {
25569 return m_vec.size ();
25570 }
25571
437afbb8
JK
25572 /* Return true iff the buffer is empty. */
25573 bool empty () const
25574 {
25575 return m_vec.empty ();
25576 }
25577
bc8f2430
JK
25578 /* Write the buffer to FILE. */
25579 void file_write (FILE *file) const
25580 {
437afbb8 25581 ::file_write (file, m_vec);
bc8f2430
JK
25582 }
25583
25584private:
c2f134ac
PA
25585 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25586 the start of the new block. */
25587 gdb_byte *grow (size_t size)
25588 {
25589 m_vec.resize (m_vec.size () + size);
25590 return &*m_vec.end () - size;
25591 }
25592
d5722aa2 25593 gdb::byte_vector m_vec;
bc8f2430 25594};
9291a0cd
TT
25595
25596/* An entry in the symbol table. */
25597struct symtab_index_entry
25598{
25599 /* The name of the symbol. */
25600 const char *name;
25601 /* The offset of the name in the constant pool. */
25602 offset_type index_offset;
25603 /* A sorted vector of the indices of all the CUs that hold an object
25604 of this name. */
bc8f2430 25605 std::vector<offset_type> cu_indices;
9291a0cd
TT
25606};
25607
25608/* The symbol table. This is a power-of-2-sized hash table. */
25609struct mapped_symtab
25610{
bc8f2430
JK
25611 mapped_symtab ()
25612 {
25613 data.resize (1024);
25614 }
b89be57b 25615
bc8f2430 25616 offset_type n_elements = 0;
4b76cda9 25617 std::vector<symtab_index_entry> data;
bc8f2430 25618};
9291a0cd 25619
bc8f2430 25620/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
559a7a62
JK
25621 the slot.
25622
25623 Function is used only during write_hash_table so no index format backward
25624 compatibility is needed. */
b89be57b 25625
4b76cda9 25626static symtab_index_entry &
9291a0cd
TT
25627find_slot (struct mapped_symtab *symtab, const char *name)
25628{
559a7a62 25629 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd 25630
bc8f2430
JK
25631 index = hash & (symtab->data.size () - 1);
25632 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
9291a0cd
TT
25633
25634 for (;;)
25635 {
4b76cda9
PA
25636 if (symtab->data[index].name == NULL
25637 || strcmp (name, symtab->data[index].name) == 0)
bc8f2430
JK
25638 return symtab->data[index];
25639 index = (index + step) & (symtab->data.size () - 1);
9291a0cd
TT
25640 }
25641}
25642
25643/* Expand SYMTAB's hash table. */
b89be57b 25644
9291a0cd
TT
25645static void
25646hash_expand (struct mapped_symtab *symtab)
25647{
bc8f2430 25648 auto old_entries = std::move (symtab->data);
9291a0cd 25649
bc8f2430
JK
25650 symtab->data.clear ();
25651 symtab->data.resize (old_entries.size () * 2);
9291a0cd 25652
bc8f2430 25653 for (auto &it : old_entries)
4b76cda9 25654 if (it.name != NULL)
bc8f2430 25655 {
4b76cda9 25656 auto &ref = find_slot (symtab, it.name);
bc8f2430
JK
25657 ref = std::move (it);
25658 }
9291a0cd
TT
25659}
25660
156942c7
DE
25661/* Add an entry to SYMTAB. NAME is the name of the symbol.
25662 CU_INDEX is the index of the CU in which the symbol appears.
25663 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 25664
9291a0cd
TT
25665static void
25666add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 25667 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
25668 offset_type cu_index)
25669{
156942c7 25670 offset_type cu_index_and_attrs;
9291a0cd
TT
25671
25672 ++symtab->n_elements;
bc8f2430 25673 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
9291a0cd
TT
25674 hash_expand (symtab);
25675
4b76cda9
PA
25676 symtab_index_entry &slot = find_slot (symtab, name);
25677 if (slot.name == NULL)
9291a0cd 25678 {
4b76cda9 25679 slot.name = name;
156942c7 25680 /* index_offset is set later. */
9291a0cd 25681 }
156942c7
DE
25682
25683 cu_index_and_attrs = 0;
25684 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
25685 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
25686 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
25687
25688 /* We don't want to record an index value twice as we want to avoid the
25689 duplication.
25690 We process all global symbols and then all static symbols
25691 (which would allow us to avoid the duplication by only having to check
25692 the last entry pushed), but a symbol could have multiple kinds in one CU.
25693 To keep things simple we don't worry about the duplication here and
25694 sort and uniqufy the list after we've processed all symbols. */
4b76cda9 25695 slot.cu_indices.push_back (cu_index_and_attrs);
156942c7
DE
25696}
25697
25698/* Sort and remove duplicates of all symbols' cu_indices lists. */
25699
25700static void
25701uniquify_cu_indices (struct mapped_symtab *symtab)
25702{
4b76cda9 25703 for (auto &entry : symtab->data)
156942c7 25704 {
4b76cda9 25705 if (entry.name != NULL && !entry.cu_indices.empty ())
156942c7 25706 {
4b76cda9 25707 auto &cu_indices = entry.cu_indices;
6fd931f2
PA
25708 std::sort (cu_indices.begin (), cu_indices.end ());
25709 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
25710 cu_indices.erase (from, cu_indices.end ());
156942c7
DE
25711 }
25712 }
9291a0cd
TT
25713}
25714
bc8f2430
JK
25715/* A form of 'const char *' suitable for container keys. Only the
25716 pointer is stored. The strings themselves are compared, not the
25717 pointers. */
25718class c_str_view
9291a0cd 25719{
bc8f2430
JK
25720public:
25721 c_str_view (const char *cstr)
25722 : m_cstr (cstr)
25723 {}
9291a0cd 25724
bc8f2430
JK
25725 bool operator== (const c_str_view &other) const
25726 {
25727 return strcmp (m_cstr, other.m_cstr) == 0;
25728 }
9291a0cd 25729
437afbb8
JK
25730 /* Return the underlying C string. Note, the returned string is
25731 only a reference with lifetime of this object. */
25732 const char *c_str () const
25733 {
25734 return m_cstr;
25735 }
25736
bc8f2430
JK
25737private:
25738 friend class c_str_view_hasher;
25739 const char *const m_cstr;
25740};
9291a0cd 25741
bc8f2430
JK
25742/* A std::unordered_map::hasher for c_str_view that uses the right
25743 hash function for strings in a mapped index. */
25744class c_str_view_hasher
25745{
25746public:
25747 size_t operator () (const c_str_view &x) const
25748 {
25749 return mapped_index_string_hash (INT_MAX, x.m_cstr);
25750 }
25751};
b89be57b 25752
bc8f2430
JK
25753/* A std::unordered_map::hasher for std::vector<>. */
25754template<typename T>
25755class vector_hasher
9291a0cd 25756{
bc8f2430
JK
25757public:
25758 size_t operator () (const std::vector<T> &key) const
25759 {
25760 return iterative_hash (key.data (),
25761 sizeof (key.front ()) * key.size (), 0);
25762 }
25763};
9291a0cd 25764
bc8f2430
JK
25765/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
25766 constant pool entries going into the data buffer CPOOL. */
3876f04e 25767
bc8f2430
JK
25768static void
25769write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
25770{
25771 {
25772 /* Elements are sorted vectors of the indices of all the CUs that
25773 hold an object of this name. */
25774 std::unordered_map<std::vector<offset_type>, offset_type,
25775 vector_hasher<offset_type>>
25776 symbol_hash_table;
25777
25778 /* We add all the index vectors to the constant pool first, to
25779 ensure alignment is ok. */
4b76cda9 25780 for (symtab_index_entry &entry : symtab->data)
bc8f2430 25781 {
4b76cda9 25782 if (entry.name == NULL)
bc8f2430 25783 continue;
4b76cda9 25784 gdb_assert (entry.index_offset == 0);
70a1152b
PA
25785
25786 /* Finding before inserting is faster than always trying to
25787 insert, because inserting always allocates a node, does the
25788 lookup, and then destroys the new node if another node
25789 already had the same key. C++17 try_emplace will avoid
25790 this. */
25791 const auto found
4b76cda9 25792 = symbol_hash_table.find (entry.cu_indices);
70a1152b
PA
25793 if (found != symbol_hash_table.end ())
25794 {
4b76cda9 25795 entry.index_offset = found->second;
70a1152b
PA
25796 continue;
25797 }
25798
4b76cda9
PA
25799 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
25800 entry.index_offset = cpool.size ();
25801 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
25802 for (const auto index : entry.cu_indices)
25803 cpool.append_data (MAYBE_SWAP (index));
bc8f2430
JK
25804 }
25805 }
9291a0cd
TT
25806
25807 /* Now write out the hash table. */
bc8f2430 25808 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
4b76cda9 25809 for (const auto &entry : symtab->data)
9291a0cd
TT
25810 {
25811 offset_type str_off, vec_off;
25812
4b76cda9 25813 if (entry.name != NULL)
9291a0cd 25814 {
4b76cda9 25815 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
bc8f2430 25816 if (insertpair.second)
4b76cda9 25817 cpool.append_cstr0 (entry.name);
bc8f2430 25818 str_off = insertpair.first->second;
4b76cda9 25819 vec_off = entry.index_offset;
9291a0cd
TT
25820 }
25821 else
25822 {
25823 /* While 0 is a valid constant pool index, it is not valid
25824 to have 0 for both offsets. */
25825 str_off = 0;
25826 vec_off = 0;
25827 }
25828
bc8f2430
JK
25829 output.append_data (MAYBE_SWAP (str_off));
25830 output.append_data (MAYBE_SWAP (vec_off));
9291a0cd 25831 }
9291a0cd
TT
25832}
25833
bc8f2430 25834typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
0a5429f6
DE
25835
25836/* Helper struct for building the address table. */
25837struct addrmap_index_data
25838{
bc8f2430
JK
25839 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
25840 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
25841 {}
25842
0a5429f6 25843 struct objfile *objfile;
bc8f2430
JK
25844 data_buf &addr_vec;
25845 psym_index_map &cu_index_htab;
0a5429f6
DE
25846
25847 /* Non-zero if the previous_* fields are valid.
25848 We can't write an entry until we see the next entry (since it is only then
25849 that we know the end of the entry). */
25850 int previous_valid;
25851 /* Index of the CU in the table of all CUs in the index file. */
25852 unsigned int previous_cu_index;
0963b4bd 25853 /* Start address of the CU. */
0a5429f6
DE
25854 CORE_ADDR previous_cu_start;
25855};
25856
bc8f2430 25857/* Write an address entry to ADDR_VEC. */
b89be57b 25858
9291a0cd 25859static void
bc8f2430 25860add_address_entry (struct objfile *objfile, data_buf &addr_vec,
0a5429f6 25861 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 25862{
9291a0cd
TT
25863 CORE_ADDR baseaddr;
25864
25865 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25866
c2f134ac
PA
25867 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
25868 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
bc8f2430 25869 addr_vec.append_data (MAYBE_SWAP (cu_index));
0a5429f6
DE
25870}
25871
25872/* Worker function for traversing an addrmap to build the address table. */
25873
25874static int
25875add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
25876{
9a3c8263
SM
25877 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
25878 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
25879
25880 if (data->previous_valid)
bc8f2430 25881 add_address_entry (data->objfile, data->addr_vec,
0a5429f6
DE
25882 data->previous_cu_start, start_addr,
25883 data->previous_cu_index);
25884
25885 data->previous_cu_start = start_addr;
25886 if (pst != NULL)
25887 {
bc8f2430
JK
25888 const auto it = data->cu_index_htab.find (pst);
25889 gdb_assert (it != data->cu_index_htab.cend ());
25890 data->previous_cu_index = it->second;
0a5429f6
DE
25891 data->previous_valid = 1;
25892 }
25893 else
bc8f2430 25894 data->previous_valid = 0;
0a5429f6
DE
25895
25896 return 0;
25897}
25898
bc8f2430 25899/* Write OBJFILE's address map to ADDR_VEC.
0a5429f6
DE
25900 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
25901 in the index file. */
25902
25903static void
bc8f2430
JK
25904write_address_map (struct objfile *objfile, data_buf &addr_vec,
25905 psym_index_map &cu_index_htab)
0a5429f6 25906{
bc8f2430 25907 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
0a5429f6
DE
25908
25909 /* When writing the address table, we have to cope with the fact that
25910 the addrmap iterator only provides the start of a region; we have to
25911 wait until the next invocation to get the start of the next region. */
25912
25913 addrmap_index_data.objfile = objfile;
0a5429f6
DE
25914 addrmap_index_data.previous_valid = 0;
25915
25916 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
25917 &addrmap_index_data);
25918
25919 /* It's highly unlikely the last entry (end address = 0xff...ff)
25920 is valid, but we should still handle it.
25921 The end address is recorded as the start of the next region, but that
25922 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
25923 anyway. */
25924 if (addrmap_index_data.previous_valid)
bc8f2430 25925 add_address_entry (objfile, addr_vec,
0a5429f6
DE
25926 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
25927 addrmap_index_data.previous_cu_index);
9291a0cd
TT
25928}
25929
156942c7
DE
25930/* Return the symbol kind of PSYM. */
25931
25932static gdb_index_symbol_kind
25933symbol_kind (struct partial_symbol *psym)
25934{
25935 domain_enum domain = PSYMBOL_DOMAIN (psym);
25936 enum address_class aclass = PSYMBOL_CLASS (psym);
25937
25938 switch (domain)
25939 {
25940 case VAR_DOMAIN:
25941 switch (aclass)
25942 {
25943 case LOC_BLOCK:
25944 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
25945 case LOC_TYPEDEF:
25946 return GDB_INDEX_SYMBOL_KIND_TYPE;
25947 case LOC_COMPUTED:
25948 case LOC_CONST_BYTES:
25949 case LOC_OPTIMIZED_OUT:
25950 case LOC_STATIC:
25951 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25952 case LOC_CONST:
25953 /* Note: It's currently impossible to recognize psyms as enum values
25954 short of reading the type info. For now punt. */
25955 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25956 default:
25957 /* There are other LOC_FOO values that one might want to classify
25958 as variables, but dwarf2read.c doesn't currently use them. */
25959 return GDB_INDEX_SYMBOL_KIND_OTHER;
25960 }
25961 case STRUCT_DOMAIN:
25962 return GDB_INDEX_SYMBOL_KIND_TYPE;
25963 default:
25964 return GDB_INDEX_SYMBOL_KIND_OTHER;
25965 }
25966}
25967
9291a0cd 25968/* Add a list of partial symbols to SYMTAB. */
b89be57b 25969
9291a0cd
TT
25970static void
25971write_psymbols (struct mapped_symtab *symtab,
bc8f2430 25972 std::unordered_set<partial_symbol *> &psyms_seen,
9291a0cd
TT
25973 struct partial_symbol **psymp,
25974 int count,
987d643c
TT
25975 offset_type cu_index,
25976 int is_static)
9291a0cd
TT
25977{
25978 for (; count-- > 0; ++psymp)
25979 {
156942c7 25980 struct partial_symbol *psym = *psymp;
987d643c 25981
156942c7 25982 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 25983 error (_("Ada is not currently supported by the index"));
987d643c 25984
987d643c 25985 /* Only add a given psymbol once. */
bc8f2430 25986 if (psyms_seen.insert (psym).second)
987d643c 25987 {
156942c7
DE
25988 gdb_index_symbol_kind kind = symbol_kind (psym);
25989
156942c7
DE
25990 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
25991 is_static, kind, cu_index);
987d643c 25992 }
9291a0cd
TT
25993 }
25994}
25995
1fd400ff
TT
25996/* A helper struct used when iterating over debug_types. */
25997struct signatured_type_index_data
25998{
bc8f2430
JK
25999 signatured_type_index_data (data_buf &types_list_,
26000 std::unordered_set<partial_symbol *> &psyms_seen_)
26001 : types_list (types_list_), psyms_seen (psyms_seen_)
26002 {}
26003
1fd400ff
TT
26004 struct objfile *objfile;
26005 struct mapped_symtab *symtab;
bc8f2430
JK
26006 data_buf &types_list;
26007 std::unordered_set<partial_symbol *> &psyms_seen;
1fd400ff
TT
26008 int cu_index;
26009};
26010
26011/* A helper function that writes a single signatured_type to an
26012 obstack. */
b89be57b 26013
1fd400ff
TT
26014static int
26015write_one_signatured_type (void **slot, void *d)
26016{
9a3c8263
SM
26017 struct signatured_type_index_data *info
26018 = (struct signatured_type_index_data *) d;
1fd400ff 26019 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 26020 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
26021
26022 write_psymbols (info->symtab,
987d643c 26023 info->psyms_seen,
af5bf4ad 26024 &info->objfile->global_psymbols[psymtab->globals_offset],
987d643c
TT
26025 psymtab->n_global_syms, info->cu_index,
26026 0);
1fd400ff 26027 write_psymbols (info->symtab,
987d643c 26028 info->psyms_seen,
af5bf4ad 26029 &info->objfile->static_psymbols[psymtab->statics_offset],
987d643c
TT
26030 psymtab->n_static_syms, info->cu_index,
26031 1);
1fd400ff 26032
c2f134ac
PA
26033 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26034 to_underlying (entry->per_cu.sect_off));
26035 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26036 to_underlying (entry->type_offset_in_tu));
26037 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
1fd400ff
TT
26038
26039 ++info->cu_index;
26040
26041 return 1;
26042}
26043
e8f8bcb3
PA
26044/* Recurse into all "included" dependencies and count their symbols as
26045 if they appeared in this psymtab. */
26046
26047static void
26048recursively_count_psymbols (struct partial_symtab *psymtab,
26049 size_t &psyms_seen)
26050{
26051 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26052 if (psymtab->dependencies[i]->user != NULL)
26053 recursively_count_psymbols (psymtab->dependencies[i],
26054 psyms_seen);
26055
26056 psyms_seen += psymtab->n_global_syms;
26057 psyms_seen += psymtab->n_static_syms;
26058}
26059
95554aad
TT
26060/* Recurse into all "included" dependencies and write their symbols as
26061 if they appeared in this psymtab. */
26062
26063static void
26064recursively_write_psymbols (struct objfile *objfile,
26065 struct partial_symtab *psymtab,
26066 struct mapped_symtab *symtab,
bc8f2430 26067 std::unordered_set<partial_symbol *> &psyms_seen,
95554aad
TT
26068 offset_type cu_index)
26069{
26070 int i;
26071
26072 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26073 if (psymtab->dependencies[i]->user != NULL)
26074 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26075 symtab, psyms_seen, cu_index);
26076
26077 write_psymbols (symtab,
26078 psyms_seen,
af5bf4ad 26079 &objfile->global_psymbols[psymtab->globals_offset],
95554aad
TT
26080 psymtab->n_global_syms, cu_index,
26081 0);
26082 write_psymbols (symtab,
26083 psyms_seen,
af5bf4ad 26084 &objfile->static_psymbols[psymtab->statics_offset],
95554aad
TT
26085 psymtab->n_static_syms, cu_index,
26086 1);
26087}
26088
437afbb8
JK
26089/* DWARF-5 .debug_names builder. */
26090class debug_names
26091{
26092public:
ed2dc618
SM
26093 debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64,
26094 bfd_endian dwarf5_byte_order)
437afbb8
JK
26095 : m_dwarf5_byte_order (dwarf5_byte_order),
26096 m_dwarf32 (dwarf5_byte_order),
26097 m_dwarf64 (dwarf5_byte_order),
26098 m_dwarf (is_dwarf64
26099 ? static_cast<dwarf &> (m_dwarf64)
26100 : static_cast<dwarf &> (m_dwarf32)),
26101 m_name_table_string_offs (m_dwarf.name_table_string_offs),
ed2dc618
SM
26102 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
26103 m_debugstrlookup (dwarf2_per_objfile)
437afbb8 26104 {}
9291a0cd 26105
8af5c486
JK
26106 int dwarf5_offset_size () const
26107 {
26108 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26109 return dwarf5_is_dwarf64 ? 8 : 4;
26110 }
26111
26112 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26113 enum class unit_kind { cu, tu };
26114
437afbb8 26115 /* Insert one symbol. */
8af5c486
JK
26116 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26117 unit_kind kind)
437afbb8
JK
26118 {
26119 const int dwarf_tag = psymbol_tag (psym);
26120 if (dwarf_tag == 0)
26121 return;
26122 const char *const name = SYMBOL_SEARCH_NAME (psym);
26123 const auto insertpair
26124 = m_name_to_value_set.emplace (c_str_view (name),
26125 std::set<symbol_value> ());
26126 std::set<symbol_value> &value_set = insertpair.first->second;
8af5c486 26127 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
437afbb8 26128 }
9291a0cd 26129
437afbb8
JK
26130 /* Build all the tables. All symbols must be already inserted.
26131 This function does not call file_write, caller has to do it
26132 afterwards. */
26133 void build ()
26134 {
26135 /* Verify the build method has not be called twice. */
26136 gdb_assert (m_abbrev_table.empty ());
26137 const size_t name_count = m_name_to_value_set.size ();
26138 m_bucket_table.resize
26139 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26140 m_hash_table.reserve (name_count);
26141 m_name_table_string_offs.reserve (name_count);
26142 m_name_table_entry_offs.reserve (name_count);
26143
26144 /* Map each hash of symbol to its name and value. */
26145 struct hash_it_pair
26146 {
26147 uint32_t hash;
26148 decltype (m_name_to_value_set)::const_iterator it;
26149 };
26150 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26151 bucket_hash.resize (m_bucket_table.size ());
26152 for (decltype (m_name_to_value_set)::const_iterator it
26153 = m_name_to_value_set.cbegin ();
26154 it != m_name_to_value_set.cend ();
26155 ++it)
26156 {
26157 const char *const name = it->first.c_str ();
26158 const uint32_t hash = dwarf5_djb_hash (name);
26159 hash_it_pair hashitpair;
26160 hashitpair.hash = hash;
26161 hashitpair.it = it;
26162 auto &slot = bucket_hash[hash % bucket_hash.size()];
26163 slot.push_front (std::move (hashitpair));
26164 }
26165 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26166 {
26167 const std::forward_list<hash_it_pair> &hashitlist
26168 = bucket_hash[bucket_ix];
26169 if (hashitlist.empty ())
26170 continue;
26171 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26172 /* The hashes array is indexed starting at 1. */
26173 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26174 sizeof (bucket_slot), m_dwarf5_byte_order,
26175 m_hash_table.size () + 1);
26176 for (const hash_it_pair &hashitpair : hashitlist)
26177 {
26178 m_hash_table.push_back (0);
26179 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26180 (&m_hash_table.back ()),
26181 sizeof (m_hash_table.back ()),
26182 m_dwarf5_byte_order, hashitpair.hash);
26183 const c_str_view &name = hashitpair.it->first;
26184 const std::set<symbol_value> &value_set = hashitpair.it->second;
26185 m_name_table_string_offs.push_back_reorder
26186 (m_debugstrlookup.lookup (name.c_str ()));
26187 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26188 gdb_assert (!value_set.empty ());
26189 for (const symbol_value &value : value_set)
26190 {
26191 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
8af5c486
JK
26192 value.is_static,
26193 value.kind)];
437afbb8
JK
26194 if (idx == 0)
26195 {
26196 idx = m_idx_next++;
26197 m_abbrev_table.append_unsigned_leb128 (idx);
26198 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
8af5c486
JK
26199 m_abbrev_table.append_unsigned_leb128
26200 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26201 : DW_IDX_type_unit);
437afbb8
JK
26202 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26203 m_abbrev_table.append_unsigned_leb128 (value.is_static
26204 ? DW_IDX_GNU_internal
26205 : DW_IDX_GNU_external);
26206 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26207
26208 /* Terminate attributes list. */
26209 m_abbrev_table.append_unsigned_leb128 (0);
26210 m_abbrev_table.append_unsigned_leb128 (0);
26211 }
9291a0cd 26212
437afbb8
JK
26213 m_entry_pool.append_unsigned_leb128 (idx);
26214 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26215 }
9291a0cd 26216
437afbb8
JK
26217 /* Terminate the list of CUs. */
26218 m_entry_pool.append_unsigned_leb128 (0);
26219 }
26220 }
26221 gdb_assert (m_hash_table.size () == name_count);
987d643c 26222
437afbb8
JK
26223 /* Terminate tags list. */
26224 m_abbrev_table.append_unsigned_leb128 (0);
26225 }
0a5429f6 26226
437afbb8
JK
26227 /* Return .debug_names bucket count. This must be called only after
26228 calling the build method. */
26229 uint32_t bucket_count () const
26230 {
26231 /* Verify the build method has been already called. */
26232 gdb_assert (!m_abbrev_table.empty ());
26233 const uint32_t retval = m_bucket_table.size ();
26234
26235 /* Check for overflow. */
26236 gdb_assert (retval == m_bucket_table.size ());
26237 return retval;
26238 }
26239
26240 /* Return .debug_names names count. This must be called only after
26241 calling the build method. */
26242 uint32_t name_count () const
26243 {
26244 /* Verify the build method has been already called. */
26245 gdb_assert (!m_abbrev_table.empty ());
26246 const uint32_t retval = m_hash_table.size ();
26247
26248 /* Check for overflow. */
26249 gdb_assert (retval == m_hash_table.size ());
26250 return retval;
26251 }
26252
26253 /* Return number of bytes of .debug_names abbreviation table. This
26254 must be called only after calling the build method. */
26255 uint32_t abbrev_table_bytes () const
26256 {
26257 gdb_assert (!m_abbrev_table.empty ());
26258 return m_abbrev_table.size ();
26259 }
26260
26261 /* Recurse into all "included" dependencies and store their symbols
26262 as if they appeared in this psymtab. */
26263 void recursively_write_psymbols
26264 (struct objfile *objfile,
26265 struct partial_symtab *psymtab,
26266 std::unordered_set<partial_symbol *> &psyms_seen,
26267 int cu_index)
26268 {
26269 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26270 if (psymtab->dependencies[i]->user != NULL)
26271 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26272 psyms_seen, cu_index);
26273
26274 write_psymbols (psyms_seen,
26275 &objfile->global_psymbols[psymtab->globals_offset],
8af5c486 26276 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
437afbb8
JK
26277 write_psymbols (psyms_seen,
26278 &objfile->static_psymbols[psymtab->statics_offset],
8af5c486 26279 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
437afbb8
JK
26280 }
26281
26282 /* Return number of bytes the .debug_names section will have. This
26283 must be called only after calling the build method. */
26284 size_t bytes () const
26285 {
26286 /* Verify the build method has been already called. */
26287 gdb_assert (!m_abbrev_table.empty ());
26288 size_t expected_bytes = 0;
26289 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26290 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26291 expected_bytes += m_name_table_string_offs.bytes ();
26292 expected_bytes += m_name_table_entry_offs.bytes ();
26293 expected_bytes += m_abbrev_table.size ();
26294 expected_bytes += m_entry_pool.size ();
26295 return expected_bytes;
26296 }
26297
26298 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26299 FILE_STR. This must be called only after calling the build
26300 method. */
26301 void file_write (FILE *file_names, FILE *file_str) const
26302 {
26303 /* Verify the build method has been already called. */
26304 gdb_assert (!m_abbrev_table.empty ());
26305 ::file_write (file_names, m_bucket_table);
26306 ::file_write (file_names, m_hash_table);
26307 m_name_table_string_offs.file_write (file_names);
26308 m_name_table_entry_offs.file_write (file_names);
26309 m_abbrev_table.file_write (file_names);
26310 m_entry_pool.file_write (file_names);
26311 m_debugstrlookup.file_write (file_str);
26312 }
26313
8af5c486
JK
26314 /* A helper user data for write_one_signatured_type. */
26315 class write_one_signatured_type_data
26316 {
26317 public:
26318 write_one_signatured_type_data (debug_names &nametable_,
26319 signatured_type_index_data &&info_)
26320 : nametable (nametable_), info (std::move (info_))
26321 {}
26322 debug_names &nametable;
26323 struct signatured_type_index_data info;
26324 };
26325
26326 /* A helper function to pass write_one_signatured_type to
26327 htab_traverse_noresize. */
26328 static int
26329 write_one_signatured_type (void **slot, void *d)
26330 {
26331 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26332 struct signatured_type_index_data *info = &data->info;
26333 struct signatured_type *entry = (struct signatured_type *) *slot;
26334
26335 data->nametable.write_one_signatured_type (entry, info);
26336
26337 return 1;
26338 }
26339
437afbb8
JK
26340private:
26341
26342 /* Storage for symbol names mapping them to their .debug_str section
26343 offsets. */
26344 class debug_str_lookup
26345 {
26346 public:
26347
26348 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26349 All .debug_str section strings are automatically stored. */
ed2dc618
SM
26350 debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile)
26351 : m_abfd (dwarf2_per_objfile->objfile->obfd),
26352 m_dwarf2_per_objfile (dwarf2_per_objfile)
437afbb8
JK
26353 {
26354 dwarf2_read_section (dwarf2_per_objfile->objfile,
26355 &dwarf2_per_objfile->str);
26356 if (dwarf2_per_objfile->str.buffer == NULL)
26357 return;
26358 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26359 data < (dwarf2_per_objfile->str.buffer
26360 + dwarf2_per_objfile->str.size);)
26361 {
26362 const char *const s = reinterpret_cast<const char *> (data);
26363 const auto insertpair
26364 = m_str_table.emplace (c_str_view (s),
26365 data - dwarf2_per_objfile->str.buffer);
26366 if (!insertpair.second)
26367 complaint (&symfile_complaints,
26368 _("Duplicate string \"%s\" in "
26369 ".debug_str section [in module %s]"),
26370 s, bfd_get_filename (m_abfd));
26371 data += strlen (s) + 1;
26372 }
26373 }
26374
26375 /* Return offset of symbol name S in the .debug_str section. Add
26376 such symbol to the section's end if it does not exist there
26377 yet. */
26378 size_t lookup (const char *s)
26379 {
26380 const auto it = m_str_table.find (c_str_view (s));
26381 if (it != m_str_table.end ())
26382 return it->second;
ed2dc618 26383 const size_t offset = (m_dwarf2_per_objfile->str.size
437afbb8
JK
26384 + m_str_add_buf.size ());
26385 m_str_table.emplace (c_str_view (s), offset);
26386 m_str_add_buf.append_cstr0 (s);
26387 return offset;
26388 }
26389
26390 /* Append the end of the .debug_str section to FILE. */
26391 void file_write (FILE *file) const
26392 {
26393 m_str_add_buf.file_write (file);
26394 }
26395
26396 private:
26397 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26398 bfd *const m_abfd;
ed2dc618 26399 struct dwarf2_per_objfile *m_dwarf2_per_objfile;
437afbb8
JK
26400
26401 /* Data to add at the end of .debug_str for new needed symbol names. */
26402 data_buf m_str_add_buf;
26403 };
26404
26405 /* Container to map used DWARF tags to their .debug_names abbreviation
26406 tags. */
26407 class index_key
26408 {
26409 public:
8af5c486
JK
26410 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26411 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
437afbb8
JK
26412 {
26413 }
26414
26415 bool
26416 operator== (const index_key &other) const
26417 {
8af5c486
JK
26418 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26419 && kind == other.kind);
437afbb8
JK
26420 }
26421
26422 const int dwarf_tag;
26423 const bool is_static;
8af5c486 26424 const unit_kind kind;
437afbb8
JK
26425 };
26426
26427 /* Provide std::unordered_map::hasher for index_key. */
26428 class index_key_hasher
26429 {
26430 public:
26431 size_t
26432 operator () (const index_key &key) const
26433 {
26434 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26435 }
26436 };
26437
26438 /* Parameters of one symbol entry. */
26439 class symbol_value
26440 {
26441 public:
26442 const int dwarf_tag, cu_index;
26443 const bool is_static;
8af5c486 26444 const unit_kind kind;
437afbb8 26445
8af5c486
JK
26446 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26447 unit_kind kind_)
26448 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26449 kind (kind_)
437afbb8
JK
26450 {}
26451
26452 bool
26453 operator< (const symbol_value &other) const
26454 {
26455#define X(n) \
26456 do \
26457 { \
26458 if (n < other.n) \
26459 return true; \
26460 if (n > other.n) \
26461 return false; \
26462 } \
26463 while (0)
26464 X (dwarf_tag);
26465 X (is_static);
8af5c486 26466 X (kind);
437afbb8
JK
26467 X (cu_index);
26468#undef X
26469 return false;
26470 }
26471 };
26472
26473 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26474 output. */
26475 class offset_vec
26476 {
26477 protected:
26478 const bfd_endian dwarf5_byte_order;
26479 public:
26480 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26481 : dwarf5_byte_order (dwarf5_byte_order_)
26482 {}
26483
26484 /* Call std::vector::reserve for NELEM elements. */
26485 virtual void reserve (size_t nelem) = 0;
26486
26487 /* Call std::vector::push_back with store_unsigned_integer byte
26488 reordering for ELEM. */
26489 virtual void push_back_reorder (size_t elem) = 0;
26490
26491 /* Return expected output size in bytes. */
26492 virtual size_t bytes () const = 0;
26493
26494 /* Write name table to FILE. */
26495 virtual void file_write (FILE *file) const = 0;
26496 };
26497
26498 /* Template to unify DWARF-32 and DWARF-64 output. */
26499 template<typename OffsetSize>
26500 class offset_vec_tmpl : public offset_vec
26501 {
26502 public:
26503 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26504 : offset_vec (dwarf5_byte_order_)
26505 {}
26506
26507 /* Implement offset_vec::reserve. */
26508 void reserve (size_t nelem) override
26509 {
26510 m_vec.reserve (nelem);
26511 }
26512
26513 /* Implement offset_vec::push_back_reorder. */
26514 void push_back_reorder (size_t elem) override
26515 {
26516 m_vec.push_back (elem);
26517 /* Check for overflow. */
26518 gdb_assert (m_vec.back () == elem);
26519 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26520 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26521 }
26522
26523 /* Implement offset_vec::bytes. */
26524 size_t bytes () const override
26525 {
26526 return m_vec.size () * sizeof (m_vec[0]);
26527 }
26528
26529 /* Implement offset_vec::file_write. */
26530 void file_write (FILE *file) const override
26531 {
26532 ::file_write (file, m_vec);
26533 }
26534
26535 private:
26536 std::vector<OffsetSize> m_vec;
26537 };
26538
26539 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26540 respecting name table width. */
26541 class dwarf
26542 {
26543 public:
26544 offset_vec &name_table_string_offs, &name_table_entry_offs;
26545
26546 dwarf (offset_vec &name_table_string_offs_,
26547 offset_vec &name_table_entry_offs_)
26548 : name_table_string_offs (name_table_string_offs_),
26549 name_table_entry_offs (name_table_entry_offs_)
26550 {
26551 }
26552 };
e8f8bcb3 26553
437afbb8
JK
26554 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26555 respecting name table width. */
26556 template<typename OffsetSize>
26557 class dwarf_tmpl : public dwarf
26558 {
26559 public:
26560 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26561 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26562 m_name_table_string_offs (dwarf5_byte_order_),
26563 m_name_table_entry_offs (dwarf5_byte_order_)
26564 {}
26565
26566 private:
26567 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26568 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26569 };
26570
26571 /* Try to reconstruct original DWARF tag for given partial_symbol.
26572 This function is not DWARF-5 compliant but it is sufficient for
26573 GDB as a DWARF-5 index consumer. */
26574 static int psymbol_tag (const struct partial_symbol *psym)
26575 {
26576 domain_enum domain = PSYMBOL_DOMAIN (psym);
26577 enum address_class aclass = PSYMBOL_CLASS (psym);
26578
26579 switch (domain)
26580 {
26581 case VAR_DOMAIN:
26582 switch (aclass)
26583 {
26584 case LOC_BLOCK:
26585 return DW_TAG_subprogram;
26586 case LOC_TYPEDEF:
26587 return DW_TAG_typedef;
26588 case LOC_COMPUTED:
26589 case LOC_CONST_BYTES:
26590 case LOC_OPTIMIZED_OUT:
26591 case LOC_STATIC:
26592 return DW_TAG_variable;
26593 case LOC_CONST:
26594 /* Note: It's currently impossible to recognize psyms as enum values
26595 short of reading the type info. For now punt. */
26596 return DW_TAG_variable;
26597 default:
26598 /* There are other LOC_FOO values that one might want to classify
26599 as variables, but dwarf2read.c doesn't currently use them. */
26600 return DW_TAG_variable;
26601 }
26602 case STRUCT_DOMAIN:
26603 return DW_TAG_structure_type;
26604 default:
26605 return 0;
26606 }
26607 }
26608
26609 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26610 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26611 struct partial_symbol **psymp, int count, int cu_index,
8af5c486 26612 bool is_static, unit_kind kind)
437afbb8
JK
26613 {
26614 for (; count-- > 0; ++psymp)
26615 {
26616 struct partial_symbol *psym = *psymp;
26617
26618 if (SYMBOL_LANGUAGE (psym) == language_ada)
26619 error (_("Ada is not currently supported by the index"));
26620
26621 /* Only add a given psymbol once. */
26622 if (psyms_seen.insert (psym).second)
8af5c486 26623 insert (psym, cu_index, is_static, kind);
437afbb8
JK
26624 }
26625 }
26626
8af5c486
JK
26627 /* A helper function that writes a single signatured_type
26628 to a debug_names. */
26629 void
26630 write_one_signatured_type (struct signatured_type *entry,
26631 struct signatured_type_index_data *info)
26632 {
26633 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26634
26635 write_psymbols (info->psyms_seen,
26636 &info->objfile->global_psymbols[psymtab->globals_offset],
26637 psymtab->n_global_syms, info->cu_index, false,
26638 unit_kind::tu);
26639 write_psymbols (info->psyms_seen,
26640 &info->objfile->static_psymbols[psymtab->statics_offset],
26641 psymtab->n_static_syms, info->cu_index, true,
26642 unit_kind::tu);
26643
26644 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
26645 to_underlying (entry->per_cu.sect_off));
26646
26647 ++info->cu_index;
26648 }
26649
437afbb8
JK
26650 /* Store value of each symbol. */
26651 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
26652 m_name_to_value_set;
26653
26654 /* Tables of DWARF-5 .debug_names. They are in object file byte
26655 order. */
26656 std::vector<uint32_t> m_bucket_table;
26657 std::vector<uint32_t> m_hash_table;
26658
26659 const bfd_endian m_dwarf5_byte_order;
26660 dwarf_tmpl<uint32_t> m_dwarf32;
26661 dwarf_tmpl<uint64_t> m_dwarf64;
26662 dwarf &m_dwarf;
26663 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
26664 debug_str_lookup m_debugstrlookup;
26665
26666 /* Map each used .debug_names abbreviation tag parameter to its
26667 index value. */
26668 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
26669
26670 /* Next unused .debug_names abbreviation tag for
26671 m_indexkey_to_idx. */
26672 int m_idx_next = 1;
26673
26674 /* .debug_names abbreviation table. */
26675 data_buf m_abbrev_table;
26676
26677 /* .debug_names entry pool. */
26678 data_buf m_entry_pool;
26679};
26680
26681/* Return iff any of the needed offsets does not fit into 32-bit
26682 .debug_names section. */
26683
26684static bool
ed2dc618 26685check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile)
437afbb8
JK
26686{
26687 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26688 {
26689 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
26690
26691 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26692 return true;
26693 }
26694 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26695 {
26696 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26697 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26698
26699 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26700 return true;
26701 }
26702 return false;
26703}
26704
26705/* The psyms_seen set is potentially going to be largish (~40k
26706 elements when indexing a -g3 build of GDB itself). Estimate the
26707 number of elements in order to avoid too many rehashes, which
26708 require rebuilding buckets and thus many trips to
26709 malloc/free. */
26710
26711static size_t
ed2dc618 26712psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile)
437afbb8 26713{
e8f8bcb3
PA
26714 size_t psyms_count = 0;
26715 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26716 {
26717 struct dwarf2_per_cu_data *per_cu
26718 = dwarf2_per_objfile->all_comp_units[i];
26719 struct partial_symtab *psymtab = per_cu->v.psymtab;
26720
26721 if (psymtab != NULL && psymtab->user == NULL)
26722 recursively_count_psymbols (psymtab, psyms_count);
26723 }
26724 /* Generating an index for gdb itself shows a ratio of
26725 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
437afbb8
JK
26726 return psyms_count / 4;
26727}
26728
26729/* Write new .gdb_index section for OBJFILE into OUT_FILE.
26730 Return how many bytes were expected to be written into OUT_FILE. */
26731
26732static size_t
ed2dc618 26733write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file)
437afbb8 26734{
ed2dc618 26735 struct objfile *objfile = dwarf2_per_objfile->objfile;
437afbb8
JK
26736 mapped_symtab symtab;
26737 data_buf cu_list;
26738
26739 /* While we're scanning CU's create a table that maps a psymtab pointer
26740 (which is what addrmap records) to its index (which is what is recorded
26741 in the index file). This will later be needed to write the address
26742 table. */
26743 psym_index_map cu_index_htab;
26744 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
26745
26746 /* The CU list is already sorted, so we don't need to do additional
26747 work here. Also, the debug_types entries do not appear in
26748 all_comp_units, but only in their own hash table. */
26749
ed2dc618
SM
26750 std::unordered_set<partial_symbol *> psyms_seen
26751 (psyms_seen_size (dwarf2_per_objfile));
bc8f2430 26752 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 26753 {
3e43a32a
MS
26754 struct dwarf2_per_cu_data *per_cu
26755 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 26756 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 26757
92fac807
JK
26758 /* CU of a shared file from 'dwz -m' may be unused by this main file.
26759 It may be referenced from a local scope but in such case it does not
26760 need to be present in .gdb_index. */
26761 if (psymtab == NULL)
26762 continue;
26763
95554aad 26764 if (psymtab->user == NULL)
bc8f2430
JK
26765 recursively_write_psymbols (objfile, psymtab, &symtab,
26766 psyms_seen, i);
9291a0cd 26767
bc8f2430
JK
26768 const auto insertpair = cu_index_htab.emplace (psymtab, i);
26769 gdb_assert (insertpair.second);
9291a0cd 26770
c2f134ac
PA
26771 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
26772 to_underlying (per_cu->sect_off));
26773 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
26774 }
26775
0a5429f6 26776 /* Dump the address map. */
bc8f2430
JK
26777 data_buf addr_vec;
26778 write_address_map (objfile, addr_vec, cu_index_htab);
0a5429f6 26779
1fd400ff 26780 /* Write out the .debug_type entries, if any. */
bc8f2430 26781 data_buf types_cu_list;
1fd400ff
TT
26782 if (dwarf2_per_objfile->signatured_types)
26783 {
bc8f2430
JK
26784 signatured_type_index_data sig_data (types_cu_list,
26785 psyms_seen);
1fd400ff
TT
26786
26787 sig_data.objfile = objfile;
bc8f2430 26788 sig_data.symtab = &symtab;
1fd400ff
TT
26789 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
26790 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26791 write_one_signatured_type, &sig_data);
26792 }
26793
156942c7
DE
26794 /* Now that we've processed all symbols we can shrink their cu_indices
26795 lists. */
bc8f2430 26796 uniquify_cu_indices (&symtab);
156942c7 26797
bc8f2430
JK
26798 data_buf symtab_vec, constant_pool;
26799 write_hash_table (&symtab, symtab_vec, constant_pool);
9291a0cd 26800
bc8f2430
JK
26801 data_buf contents;
26802 const offset_type size_of_contents = 6 * sizeof (offset_type);
26803 offset_type total_len = size_of_contents;
9291a0cd
TT
26804
26805 /* The version number. */
bc8f2430 26806 contents.append_data (MAYBE_SWAP (8));
9291a0cd
TT
26807
26808 /* The offset of the CU list from the start of the file. */
bc8f2430
JK
26809 contents.append_data (MAYBE_SWAP (total_len));
26810 total_len += cu_list.size ();
9291a0cd 26811
1fd400ff 26812 /* The offset of the types CU list from the start of the file. */
bc8f2430
JK
26813 contents.append_data (MAYBE_SWAP (total_len));
26814 total_len += types_cu_list.size ();
1fd400ff 26815
9291a0cd 26816 /* The offset of the address table from the start of the file. */
bc8f2430
JK
26817 contents.append_data (MAYBE_SWAP (total_len));
26818 total_len += addr_vec.size ();
9291a0cd
TT
26819
26820 /* The offset of the symbol table from the start of the file. */
bc8f2430
JK
26821 contents.append_data (MAYBE_SWAP (total_len));
26822 total_len += symtab_vec.size ();
9291a0cd
TT
26823
26824 /* The offset of the constant pool from the start of the file. */
bc8f2430
JK
26825 contents.append_data (MAYBE_SWAP (total_len));
26826 total_len += constant_pool.size ();
9291a0cd 26827
bc8f2430 26828 gdb_assert (contents.size () == size_of_contents);
9291a0cd 26829
bc8f2430
JK
26830 contents.file_write (out_file);
26831 cu_list.file_write (out_file);
26832 types_cu_list.file_write (out_file);
26833 addr_vec.file_write (out_file);
26834 symtab_vec.file_write (out_file);
26835 constant_pool.file_write (out_file);
9291a0cd 26836
437afbb8
JK
26837 return total_len;
26838}
26839
26840/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
26841static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
26842
26843/* Write a new .debug_names section for OBJFILE into OUT_FILE, write
26844 needed addition to .debug_str section to OUT_FILE_STR. Return how
26845 many bytes were expected to be written into OUT_FILE. */
26846
26847static size_t
ed2dc618
SM
26848write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
26849 FILE *out_file, FILE *out_file_str)
437afbb8 26850{
ed2dc618
SM
26851 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile);
26852 struct objfile *objfile = dwarf2_per_objfile->objfile;
437afbb8
JK
26853 const enum bfd_endian dwarf5_byte_order
26854 = gdbarch_byte_order (get_objfile_arch (objfile));
26855
26856 /* The CU list is already sorted, so we don't need to do additional
26857 work here. Also, the debug_types entries do not appear in
26858 all_comp_units, but only in their own hash table. */
26859 data_buf cu_list;
ed2dc618
SM
26860 debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64,
26861 dwarf5_byte_order);
26862 std::unordered_set<partial_symbol *>
26863 psyms_seen (psyms_seen_size (dwarf2_per_objfile));
437afbb8
JK
26864 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26865 {
26866 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
26867 partial_symtab *psymtab = per_cu->v.psymtab;
26868
26869 /* CU of a shared file from 'dwz -m' may be unused by this main
26870 file. It may be referenced from a local scope but in such
26871 case it does not need to be present in .debug_names. */
26872 if (psymtab == NULL)
26873 continue;
26874
26875 if (psymtab->user == NULL)
26876 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
26877
8af5c486 26878 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
437afbb8
JK
26879 to_underlying (per_cu->sect_off));
26880 }
437afbb8 26881
8af5c486 26882 /* Write out the .debug_type entries, if any. */
437afbb8 26883 data_buf types_cu_list;
8af5c486 26884 if (dwarf2_per_objfile->signatured_types)
437afbb8 26885 {
8af5c486
JK
26886 debug_names::write_one_signatured_type_data sig_data (nametable,
26887 signatured_type_index_data (types_cu_list, psyms_seen));
437afbb8 26888
8af5c486
JK
26889 sig_data.info.objfile = objfile;
26890 /* It is used only for gdb_index. */
26891 sig_data.info.symtab = nullptr;
26892 sig_data.info.cu_index = 0;
26893 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26894 debug_names::write_one_signatured_type,
26895 &sig_data);
437afbb8
JK
26896 }
26897
8af5c486
JK
26898 nametable.build ();
26899
26900 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
26901
437afbb8
JK
26902 const offset_type bytes_of_header
26903 = ((dwarf5_is_dwarf64 ? 12 : 4)
26904 + 2 + 2 + 7 * 4
26905 + sizeof (dwarf5_gdb_augmentation));
26906 size_t expected_bytes = 0;
26907 expected_bytes += bytes_of_header;
26908 expected_bytes += cu_list.size ();
26909 expected_bytes += types_cu_list.size ();
26910 expected_bytes += nametable.bytes ();
26911 data_buf header;
26912
26913 if (!dwarf5_is_dwarf64)
26914 {
26915 const uint64_t size64 = expected_bytes - 4;
26916 gdb_assert (size64 < 0xfffffff0);
26917 header.append_uint (4, dwarf5_byte_order, size64);
26918 }
26919 else
26920 {
26921 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
26922 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
26923 }
26924
26925 /* The version number. */
26926 header.append_uint (2, dwarf5_byte_order, 5);
26927
26928 /* Padding. */
26929 header.append_uint (2, dwarf5_byte_order, 0);
26930
26931 /* comp_unit_count - The number of CUs in the CU list. */
26932 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
26933
26934 /* local_type_unit_count - The number of TUs in the local TU
26935 list. */
26936 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
26937
26938 /* foreign_type_unit_count - The number of TUs in the foreign TU
26939 list. */
26940 header.append_uint (4, dwarf5_byte_order, 0);
26941
26942 /* bucket_count - The number of hash buckets in the hash lookup
26943 table. */
26944 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
26945
26946 /* name_count - The number of unique names in the index. */
26947 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
26948
26949 /* abbrev_table_size - The size in bytes of the abbreviations
26950 table. */
26951 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
26952
26953 /* augmentation_string_size - The size in bytes of the augmentation
26954 string. This value is rounded up to a multiple of 4. */
26955 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
26956 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
26957 header.append_data (dwarf5_gdb_augmentation);
26958
26959 gdb_assert (header.size () == bytes_of_header);
26960
26961 header.file_write (out_file);
26962 cu_list.file_write (out_file);
26963 types_cu_list.file_write (out_file);
26964 nametable.file_write (out_file, out_file_str);
26965
26966 return expected_bytes;
26967}
26968
26969/* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
26970 position is at the end of the file. */
26971
26972static void
26973assert_file_size (FILE *file, const char *filename, size_t expected_size)
26974{
26975 const auto file_size = ftell (file);
26976 if (file_size == -1)
26977 error (_("Can't get `%s' size"), filename);
26978 gdb_assert (file_size == expected_size);
26979}
26980
437afbb8
JK
26981/* Create an index file for OBJFILE in the directory DIR. */
26982
26983static void
ed2dc618
SM
26984write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
26985 const char *dir,
437afbb8
JK
26986 dw_index_kind index_kind)
26987{
ed2dc618
SM
26988 struct objfile *objfile = dwarf2_per_objfile->objfile;
26989
437afbb8
JK
26990 if (dwarf2_per_objfile->using_index)
26991 error (_("Cannot use an index to create the index"));
26992
26993 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
26994 error (_("Cannot make an index when the file has multiple .debug_types sections"));
26995
26996 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
26997 return;
26998
26999 struct stat st;
27000 if (stat (objfile_name (objfile), &st) < 0)
27001 perror_with_name (objfile_name (objfile));
27002
27003 std::string filename (std::string (dir) + SLASH_STRING
27004 + lbasename (objfile_name (objfile))
27005 + (index_kind == dw_index_kind::DEBUG_NAMES
27006 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
27007
27008 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
27009 if (!out_file)
27010 error (_("Can't open `%s' for writing"), filename.c_str ());
27011
27012 /* Order matters here; we want FILE to be closed before FILENAME is
27013 unlinked, because on MS-Windows one cannot delete a file that is
27014 still open. (Don't call anything here that might throw until
27015 file_closer is created.) */
27016 gdb::unlinker unlink_file (filename.c_str ());
27017 gdb_file_up close_out_file (out_file);
27018
27019 if (index_kind == dw_index_kind::DEBUG_NAMES)
27020 {
27021 std::string filename_str (std::string (dir) + SLASH_STRING
27022 + lbasename (objfile_name (objfile))
27023 + DEBUG_STR_SUFFIX);
27024 FILE *out_file_str
27025 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
27026 if (!out_file_str)
27027 error (_("Can't open `%s' for writing"), filename_str.c_str ());
27028 gdb::unlinker unlink_file_str (filename_str.c_str ());
27029 gdb_file_up close_out_file_str (out_file_str);
27030
27031 const size_t total_len
ed2dc618 27032 = write_debug_names (dwarf2_per_objfile, out_file, out_file_str);
437afbb8
JK
27033 assert_file_size (out_file, filename.c_str (), total_len);
27034
27035 /* We want to keep the file .debug_str file too. */
27036 unlink_file_str.keep ();
27037 }
27038 else
27039 {
27040 const size_t total_len
ed2dc618 27041 = write_gdbindex (dwarf2_per_objfile, out_file);
437afbb8
JK
27042 assert_file_size (out_file, filename.c_str (), total_len);
27043 }
27044
bef155c3
TT
27045 /* We want to keep the file. */
27046 unlink_file.keep ();
9291a0cd
TT
27047}
27048
90476074
TT
27049/* Implementation of the `save gdb-index' command.
27050
437afbb8
JK
27051 Note that the .gdb_index file format used by this command is
27052 documented in the GDB manual. Any changes here must be documented
27053 there. */
11570e71 27054
9291a0cd 27055static void
8384c356 27056save_gdb_index_command (const char *arg, int from_tty)
9291a0cd
TT
27057{
27058 struct objfile *objfile;
437afbb8
JK
27059 const char dwarf5space[] = "-dwarf-5 ";
27060 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
27061
27062 if (!arg)
27063 arg = "";
27064
27065 arg = skip_spaces (arg);
27066 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
27067 {
27068 index_kind = dw_index_kind::DEBUG_NAMES;
27069 arg += strlen (dwarf5space);
27070 arg = skip_spaces (arg);
27071 }
9291a0cd 27072
437afbb8
JK
27073 if (!*arg)
27074 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
9291a0cd
TT
27075
27076 ALL_OBJFILES (objfile)
27077 {
27078 struct stat st;
27079
27080 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 27081 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
27082 continue;
27083
ed2dc618
SM
27084 struct dwarf2_per_objfile *dwarf2_per_objfile
27085 = get_dwarf2_per_objfile (objfile);
9291a0cd 27086
ed2dc618
SM
27087 if (dwarf2_per_objfile != NULL)
27088 {
492d29ea 27089 TRY
9291a0cd 27090 {
ed2dc618 27091 write_psymtabs_to_index (dwarf2_per_objfile, arg, index_kind);
9291a0cd 27092 }
492d29ea
PA
27093 CATCH (except, RETURN_MASK_ERROR)
27094 {
27095 exception_fprintf (gdb_stderr, except,
27096 _("Error while writing index for `%s': "),
27097 objfile_name (objfile));
27098 }
27099 END_CATCH
9291a0cd 27100 }
ed2dc618 27101
9291a0cd 27102 }
dce234bc
PP
27103}
27104
9291a0cd
TT
27105\f
27106
b4f54984 27107int dwarf_always_disassemble;
9eae7c52
TT
27108
27109static void
b4f54984
DE
27110show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27111 struct cmd_list_element *c, const char *value)
9eae7c52 27112{
3e43a32a
MS
27113 fprintf_filtered (file,
27114 _("Whether to always disassemble "
27115 "DWARF expressions is %s.\n"),
9eae7c52
TT
27116 value);
27117}
27118
900e11f9
JK
27119static void
27120show_check_physname (struct ui_file *file, int from_tty,
27121 struct cmd_list_element *c, const char *value)
27122{
27123 fprintf_filtered (file,
27124 _("Whether to check \"physname\" is %s.\n"),
27125 value);
27126}
27127
6502dd73
DJ
27128void
27129_initialize_dwarf2_read (void)
27130{
96d19272
JK
27131 struct cmd_list_element *c;
27132
fc8e7e75 27133 dwarf2_objfile_data_key = register_objfile_data ();
ae038cb0 27134
b4f54984
DE
27135 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27136Set DWARF specific variables.\n\
27137Configure DWARF variables such as the cache size"),
27138 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
27139 0/*allow-unknown*/, &maintenance_set_cmdlist);
27140
b4f54984
DE
27141 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27142Show DWARF specific variables\n\
27143Show DWARF variables such as the cache size"),
27144 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
27145 0/*allow-unknown*/, &maintenance_show_cmdlist);
27146
27147 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
27148 &dwarf_max_cache_age, _("\
27149Set the upper bound on the age of cached DWARF compilation units."), _("\
27150Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
27151A higher limit means that cached compilation units will be stored\n\
27152in memory longer, and more total memory will be used. Zero disables\n\
27153caching, which can slow down startup."),
2c5b56ce 27154 NULL,
b4f54984
DE
27155 show_dwarf_max_cache_age,
27156 &set_dwarf_cmdlist,
27157 &show_dwarf_cmdlist);
d97bc12b 27158
9eae7c52 27159 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 27160 &dwarf_always_disassemble, _("\
9eae7c52
TT
27161Set whether `info address' always disassembles DWARF expressions."), _("\
27162Show whether `info address' always disassembles DWARF expressions."), _("\
27163When enabled, DWARF expressions are always printed in an assembly-like\n\
27164syntax. When disabled, expressions will be printed in a more\n\
27165conversational style, when possible."),
27166 NULL,
b4f54984
DE
27167 show_dwarf_always_disassemble,
27168 &set_dwarf_cmdlist,
27169 &show_dwarf_cmdlist);
27170
27171 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27172Set debugging of the DWARF reader."), _("\
27173Show debugging of the DWARF reader."), _("\
27174When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
27175reading and symtab expansion. A value of 1 (one) provides basic\n\
27176information. A value greater than 1 provides more verbose information."),
45cfd468
DE
27177 NULL,
27178 NULL,
27179 &setdebuglist, &showdebuglist);
27180
b4f54984
DE
27181 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27182Set debugging of the DWARF DIE reader."), _("\
27183Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
27184When enabled (non-zero), DIEs are dumped after they are read in.\n\
27185The value is the maximum depth to print."),
ccce17b0
YQ
27186 NULL,
27187 NULL,
27188 &setdebuglist, &showdebuglist);
9291a0cd 27189
27e0867f
DE
27190 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27191Set debugging of the dwarf line reader."), _("\
27192Show debugging of the dwarf line reader."), _("\
27193When enabled (non-zero), line number entries are dumped as they are read in.\n\
27194A value of 1 (one) provides basic information.\n\
27195A value greater than 1 provides more verbose information."),
27196 NULL,
27197 NULL,
27198 &setdebuglist, &showdebuglist);
27199
900e11f9
JK
27200 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27201Set cross-checking of \"physname\" code against demangler."), _("\
27202Show cross-checking of \"physname\" code against demangler."), _("\
27203When enabled, GDB's internal \"physname\" code is checked against\n\
27204the demangler."),
27205 NULL, show_check_physname,
27206 &setdebuglist, &showdebuglist);
27207
e615022a
DE
27208 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27209 no_class, &use_deprecated_index_sections, _("\
27210Set whether to use deprecated gdb_index sections."), _("\
27211Show whether to use deprecated gdb_index sections."), _("\
27212When enabled, deprecated .gdb_index sections are used anyway.\n\
27213Normally they are ignored either because of a missing feature or\n\
27214performance issue.\n\
27215Warning: This option must be enabled before gdb reads the file."),
27216 NULL,
27217 NULL,
27218 &setlist, &showlist);
27219
96d19272 27220 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 27221 _("\
fc1a9d6e 27222Save a gdb-index file.\n\
437afbb8
JK
27223Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27224\n\
27225No options create one file with .gdb-index extension for pre-DWARF-5\n\
27226compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27227extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
96d19272
JK
27228 &save_cmdlist);
27229 set_cmd_completer (c, filename_completer);
f1e6e072
TT
27230
27231 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27232 &dwarf2_locexpr_funcs);
27233 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27234 &dwarf2_loclist_funcs);
27235
27236 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27237 &dwarf2_block_frame_base_locexpr_funcs);
27238 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27239 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
27240
27241#if GDB_SELF_TEST
27242 selftests::register_test ("dw2_expand_symtabs_matching",
27243 selftests::dw2_expand_symtabs_matching::run_test);
27244#endif
6502dd73 27245}
This page took 4.582046 seconds and 4 git commands to generate.