treat Ada DW_TAG_unspecified_type DIEs as stub types
[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
90typedef struct symbol *symbolp;
91DEF_VEC_P (symbolp);
d8151005 92
73be47f5
DE
93/* When == 1, print basic high level tracing messages.
94 When > 1, be more verbose.
b4f54984
DE
95 This is in contrast to the low level DIE reading of dwarf_die_debug. */
96static unsigned int dwarf_read_debug = 0;
45cfd468 97
d97bc12b 98/* When non-zero, dump DIEs after they are read in. */
b4f54984 99static unsigned int dwarf_die_debug = 0;
d97bc12b 100
27e0867f
DE
101/* When non-zero, dump line number entries as they are read in. */
102static unsigned int dwarf_line_debug = 0;
103
900e11f9
JK
104/* When non-zero, cross-check physname against demangler. */
105static int check_physname = 0;
106
481860b3 107/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 108static int use_deprecated_index_sections = 0;
481860b3 109
6502dd73
DJ
110static const struct objfile_data *dwarf2_objfile_data_key;
111
f1e6e072
TT
112/* The "aclass" indices for various kinds of computed DWARF symbols. */
113
114static int dwarf2_locexpr_index;
115static int dwarf2_loclist_index;
116static int dwarf2_locexpr_block_index;
117static int dwarf2_loclist_block_index;
118
73869dc2
DE
119/* A descriptor for dwarf sections.
120
121 S.ASECTION, SIZE are typically initialized when the objfile is first
122 scanned. BUFFER, READIN are filled in later when the section is read.
123 If the section contained compressed data then SIZE is updated to record
124 the uncompressed size of the section.
125
126 DWP file format V2 introduces a wrinkle that is easiest to handle by
127 creating the concept of virtual sections contained within a real section.
128 In DWP V2 the sections of the input DWO files are concatenated together
129 into one section, but section offsets are kept relative to the original
130 input section.
131 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
132 the real section this "virtual" section is contained in, and BUFFER,SIZE
133 describe the virtual section. */
134
dce234bc
PP
135struct dwarf2_section_info
136{
73869dc2
DE
137 union
138 {
e5aa3347 139 /* If this is a real section, the bfd section. */
049412e3 140 asection *section;
73869dc2 141 /* If this is a virtual section, pointer to the containing ("real")
e5aa3347 142 section. */
73869dc2
DE
143 struct dwarf2_section_info *containing_section;
144 } s;
19ac8c2e 145 /* Pointer to section data, only valid if readin. */
d521ce57 146 const gdb_byte *buffer;
73869dc2 147 /* The size of the section, real or virtual. */
dce234bc 148 bfd_size_type size;
73869dc2
DE
149 /* If this is a virtual section, the offset in the real section.
150 Only valid if is_virtual. */
151 bfd_size_type virtual_offset;
be391dca 152 /* True if we have tried to read this section. */
73869dc2
DE
153 char readin;
154 /* True if this is a virtual section, False otherwise.
049412e3 155 This specifies which of s.section and s.containing_section to use. */
73869dc2 156 char is_virtual;
dce234bc
PP
157};
158
8b70b953
TT
159typedef struct dwarf2_section_info dwarf2_section_info_def;
160DEF_VEC_O (dwarf2_section_info_def);
161
9291a0cd
TT
162/* All offsets in the index are of this type. It must be
163 architecture-independent. */
164typedef uint32_t offset_type;
165
166DEF_VEC_I (offset_type);
167
156942c7
DE
168/* Ensure only legit values are used. */
169#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
170 do { \
171 gdb_assert ((unsigned int) (value) <= 1); \
172 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
173 } while (0)
174
175/* Ensure only legit values are used. */
176#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
177 do { \
178 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
179 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
180 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
181 } while (0)
182
183/* Ensure we don't use more than the alloted nuber of bits for the CU. */
184#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
185 do { \
186 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
187 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
188 } while (0)
189
3f563c84
PA
190#if WORDS_BIGENDIAN
191
192/* Convert VALUE between big- and little-endian. */
193
194static offset_type
195byte_swap (offset_type value)
196{
197 offset_type result;
198
199 result = (value & 0xff) << 24;
200 result |= (value & 0xff00) << 8;
201 result |= (value & 0xff0000) >> 8;
202 result |= (value & 0xff000000) >> 24;
203 return result;
204}
205
206#define MAYBE_SWAP(V) byte_swap (V)
207
208#else
209#define MAYBE_SWAP(V) static_cast<offset_type> (V)
210#endif /* WORDS_BIGENDIAN */
211
212/* An index into a (C++) symbol name component in a symbol name as
213 recorded in the mapped_index's symbol table. For each C++ symbol
214 in the symbol table, we record one entry for the start of each
215 component in the symbol in a table of name components, and then
216 sort the table, in order to be able to binary search symbol names,
217 ignoring leading namespaces, both completion and regular look up.
218 For example, for symbol "A::B::C", we'll have an entry that points
219 to "A::B::C", another that points to "B::C", and another for "C".
220 Note that function symbols in GDB index have no parameter
221 information, just the function/method names. You can convert a
222 name_component to a "const char *" using the
223 'mapped_index::symbol_name_at(offset_type)' method. */
224
225struct name_component
226{
227 /* Offset in the symbol name where the component starts. Stored as
228 a (32-bit) offset instead of a pointer to save memory and improve
229 locality on 64-bit architectures. */
230 offset_type name_offset;
231
232 /* The symbol's index in the symbol and constant pool tables of a
233 mapped_index. */
234 offset_type idx;
235};
236
44ed8f3e
PA
237/* Base class containing bits shared by both .gdb_index and
238 .debug_name indexes. */
239
240struct mapped_index_base
241{
242 /* The name_component table (a sorted vector). See name_component's
243 description above. */
244 std::vector<name_component> name_components;
245
246 /* How NAME_COMPONENTS is sorted. */
247 enum case_sensitivity name_components_casing;
248
249 /* Return the number of names in the symbol table. */
250 virtual size_t symbol_name_count () const = 0;
251
252 /* Get the name of the symbol at IDX in the symbol table. */
253 virtual const char *symbol_name_at (offset_type idx) const = 0;
254
255 /* Return whether the name at IDX in the symbol table should be
256 ignored. */
257 virtual bool symbol_name_slot_invalid (offset_type idx) const
258 {
259 return false;
260 }
261
262 /* Build the symbol name component sorted vector, if we haven't
263 yet. */
264 void build_name_components ();
265
266 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
267 possible matches for LN_NO_PARAMS in the name component
268 vector. */
269 std::pair<std::vector<name_component>::const_iterator,
270 std::vector<name_component>::const_iterator>
271 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
272
273 /* Prevent deleting/destroying via a base class pointer. */
274protected:
275 ~mapped_index_base() = default;
276};
277
9291a0cd
TT
278/* A description of the mapped index. The file format is described in
279 a comment by the code that writes the index. */
fc898b42 280struct mapped_index final : public mapped_index_base
9291a0cd 281{
f00a2de2
PA
282 /* A slot/bucket in the symbol table hash. */
283 struct symbol_table_slot
284 {
285 const offset_type name;
286 const offset_type vec;
287 };
288
559a7a62
JK
289 /* Index data format version. */
290 int version;
291
9291a0cd
TT
292 /* The total length of the buffer. */
293 off_t total_size;
b11b1f88 294
f00a2de2
PA
295 /* The address table data. */
296 gdb::array_view<const gdb_byte> address_table;
b11b1f88 297
3876f04e 298 /* The symbol table, implemented as a hash table. */
f00a2de2 299 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 300
9291a0cd
TT
301 /* A pointer to the constant pool. */
302 const char *constant_pool;
3f563c84 303
44ed8f3e
PA
304 bool symbol_name_slot_invalid (offset_type idx) const override
305 {
306 const auto &bucket = this->symbol_table[idx];
307 return bucket.name == 0 && bucket.vec;
308 }
5c58de74 309
3f563c84
PA
310 /* Convenience method to get at the name of the symbol at IDX in the
311 symbol table. */
44ed8f3e 312 const char *symbol_name_at (offset_type idx) const override
f00a2de2 313 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 314
44ed8f3e
PA
315 size_t symbol_name_count () const override
316 { return this->symbol_table.size (); }
9291a0cd
TT
317};
318
927aa2e7
JK
319/* A description of the mapped .debug_names.
320 Uninitialized map has CU_COUNT 0. */
fc898b42 321struct mapped_debug_names final : public mapped_index_base
927aa2e7
JK
322{
323 bfd_endian dwarf5_byte_order;
324 bool dwarf5_is_dwarf64;
325 bool augmentation_is_gdb;
326 uint8_t offset_size;
327 uint32_t cu_count = 0;
328 uint32_t tu_count, bucket_count, name_count;
329 const gdb_byte *cu_table_reordered, *tu_table_reordered;
330 const uint32_t *bucket_table_reordered, *hash_table_reordered;
331 const gdb_byte *name_table_string_offs_reordered;
332 const gdb_byte *name_table_entry_offs_reordered;
333 const gdb_byte *entry_pool;
334
335 struct index_val
336 {
337 ULONGEST dwarf_tag;
338 struct attr
339 {
340 /* Attribute name DW_IDX_*. */
341 ULONGEST dw_idx;
342
343 /* Attribute form DW_FORM_*. */
344 ULONGEST form;
345
346 /* Value if FORM is DW_FORM_implicit_const. */
347 LONGEST implicit_const;
348 };
349 std::vector<attr> attr_vec;
350 };
351
352 std::unordered_map<ULONGEST, index_val> abbrev_map;
353
354 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
355
356 /* Implementation of the mapped_index_base virtual interface, for
357 the name_components cache. */
358
359 const char *symbol_name_at (offset_type idx) const override
360 { return namei_to_name (idx); }
361
362 size_t symbol_name_count () const override
363 { return this->name_count; }
927aa2e7
JK
364};
365
95554aad
TT
366typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
367DEF_VEC_P (dwarf2_per_cu_ptr);
368
52059ffd
TT
369struct tu_stats
370{
371 int nr_uniq_abbrev_tables;
372 int nr_symtabs;
373 int nr_symtab_sharers;
374 int nr_stmt_less_type_units;
375 int nr_all_type_units_reallocs;
376};
377
9cdd5dbd
DE
378/* Collection of data recorded per objfile.
379 This hangs off of dwarf2_objfile_data_key. */
380
6502dd73
DJ
381struct dwarf2_per_objfile
382{
330cdd98
PA
383 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
384 dwarf2 section names, or is NULL if the standard ELF names are
385 used. */
386 dwarf2_per_objfile (struct objfile *objfile,
387 const dwarf2_debug_sections *names);
ae038cb0 388
330cdd98
PA
389 ~dwarf2_per_objfile ();
390
d6541620 391 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
330cdd98
PA
392
393 /* Free all cached compilation units. */
394 void free_cached_comp_units ();
395private:
396 /* This function is mapped across the sections and remembers the
397 offset and size of each of the debugging sections we are
398 interested in. */
399 void locate_sections (bfd *abfd, asection *sectp,
400 const dwarf2_debug_sections &names);
401
402public:
403 dwarf2_section_info info {};
404 dwarf2_section_info abbrev {};
405 dwarf2_section_info line {};
406 dwarf2_section_info loc {};
407 dwarf2_section_info loclists {};
408 dwarf2_section_info macinfo {};
409 dwarf2_section_info macro {};
410 dwarf2_section_info str {};
411 dwarf2_section_info line_str {};
412 dwarf2_section_info ranges {};
413 dwarf2_section_info rnglists {};
414 dwarf2_section_info addr {};
415 dwarf2_section_info frame {};
416 dwarf2_section_info eh_frame {};
417 dwarf2_section_info gdb_index {};
927aa2e7
JK
418 dwarf2_section_info debug_names {};
419 dwarf2_section_info debug_aranges {};
330cdd98
PA
420
421 VEC (dwarf2_section_info_def) *types = NULL;
8b70b953 422
be391dca 423 /* Back link. */
330cdd98 424 struct objfile *objfile = NULL;
be391dca 425
d467dd73 426 /* Table of all the compilation units. This is used to locate
10b3939b 427 the target compilation unit of a particular reference. */
330cdd98 428 struct dwarf2_per_cu_data **all_comp_units = NULL;
ae038cb0
DJ
429
430 /* The number of compilation units in ALL_COMP_UNITS. */
330cdd98 431 int n_comp_units = 0;
ae038cb0 432
1fd400ff 433 /* The number of .debug_types-related CUs. */
330cdd98 434 int n_type_units = 0;
1fd400ff 435
6aa5f3a6
DE
436 /* The number of elements allocated in all_type_units.
437 If there are skeleton-less TUs, we add them to all_type_units lazily. */
330cdd98 438 int n_allocated_type_units = 0;
6aa5f3a6 439
a2ce51a0
DE
440 /* The .debug_types-related CUs (TUs).
441 This is stored in malloc space because we may realloc it. */
330cdd98 442 struct signatured_type **all_type_units = NULL;
1fd400ff 443
f4dc4d17
DE
444 /* Table of struct type_unit_group objects.
445 The hash key is the DW_AT_stmt_list value. */
330cdd98 446 htab_t type_unit_groups {};
72dca2f5 447
348e048f
DE
448 /* A table mapping .debug_types signatures to its signatured_type entry.
449 This is NULL if the .debug_types section hasn't been read in yet. */
330cdd98 450 htab_t signatured_types {};
348e048f 451
f4dc4d17
DE
452 /* Type unit statistics, to see how well the scaling improvements
453 are doing. */
330cdd98 454 struct tu_stats tu_stats {};
f4dc4d17
DE
455
456 /* A chain of compilation units that are currently read in, so that
457 they can be freed later. */
330cdd98 458 dwarf2_per_cu_data *read_in_chain = NULL;
f4dc4d17 459
3019eac3
DE
460 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
461 This is NULL if the table hasn't been allocated yet. */
330cdd98 462 htab_t dwo_files {};
3019eac3 463
330cdd98
PA
464 /* True if we've checked for whether there is a DWP file. */
465 bool dwp_checked = false;
80626a55
DE
466
467 /* The DWP file if there is one, or NULL. */
330cdd98 468 struct dwp_file *dwp_file = NULL;
80626a55 469
36586728
TT
470 /* The shared '.dwz' file, if one exists. This is used when the
471 original data was compressed using 'dwz -m'. */
330cdd98 472 struct dwz_file *dwz_file = NULL;
36586728 473
330cdd98 474 /* A flag indicating whether this objfile has a section loaded at a
72dca2f5 475 VMA of 0. */
330cdd98 476 bool has_section_at_zero = false;
9291a0cd 477
ae2de4f8
DE
478 /* True if we are using the mapped index,
479 or we are faking it for OBJF_READNOW's sake. */
330cdd98 480 bool using_index = false;
9291a0cd 481
ae2de4f8 482 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
330cdd98 483 mapped_index *index_table = NULL;
98bfdba5 484
927aa2e7
JK
485 /* The mapped index, or NULL if .debug_names is missing or not being used. */
486 std::unique_ptr<mapped_debug_names> debug_names_table;
487
7b9f3c50 488 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
489 TUs typically share line table entries with a CU, so we maintain a
490 separate table of all line table entries to support the sharing.
491 Note that while there can be way more TUs than CUs, we've already
492 sorted all the TUs into "type unit groups", grouped by their
493 DW_AT_stmt_list value. Therefore the only sharing done here is with a
494 CU and its associated TU group if there is one. */
330cdd98 495 htab_t quick_file_names_table {};
7b9f3c50 496
98bfdba5
PA
497 /* Set during partial symbol reading, to prevent queueing of full
498 symbols. */
330cdd98 499 bool reading_partial_symbols = false;
673bfd45 500
dee91e82 501 /* Table mapping type DIEs to their struct type *.
673bfd45 502 This is NULL if not allocated yet.
02142a6c 503 The mapping is done via (CU/TU + DIE offset) -> type. */
330cdd98 504 htab_t die_type_hash {};
95554aad
TT
505
506 /* The CUs we recently read. */
330cdd98 507 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
527f3840
JK
508
509 /* Table containing line_header indexed by offset and offset_in_dwz. */
330cdd98 510 htab_t line_header_hash {};
bbf2f4df
PA
511
512 /* Table containing all filenames. This is an optional because the
513 table is lazily constructed on first access. */
514 gdb::optional<filename_seen_cache> filenames_cache;
6502dd73
DJ
515};
516
517static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 518
251d32d9 519/* Default names of the debugging sections. */
c906108c 520
233a11ab
CS
521/* Note that if the debugging section has been compressed, it might
522 have a name like .zdebug_info. */
523
9cdd5dbd
DE
524static const struct dwarf2_debug_sections dwarf2_elf_names =
525{
251d32d9
TG
526 { ".debug_info", ".zdebug_info" },
527 { ".debug_abbrev", ".zdebug_abbrev" },
528 { ".debug_line", ".zdebug_line" },
529 { ".debug_loc", ".zdebug_loc" },
43988095 530 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 531 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 532 { ".debug_macro", ".zdebug_macro" },
251d32d9 533 { ".debug_str", ".zdebug_str" },
43988095 534 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 535 { ".debug_ranges", ".zdebug_ranges" },
43988095 536 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 537 { ".debug_types", ".zdebug_types" },
3019eac3 538 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
539 { ".debug_frame", ".zdebug_frame" },
540 { ".eh_frame", NULL },
24d3216f 541 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
542 { ".debug_names", ".zdebug_names" },
543 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 544 23
251d32d9 545};
c906108c 546
80626a55 547/* List of DWO/DWP sections. */
3019eac3 548
80626a55 549static const struct dwop_section_names
3019eac3
DE
550{
551 struct dwarf2_section_names abbrev_dwo;
552 struct dwarf2_section_names info_dwo;
553 struct dwarf2_section_names line_dwo;
554 struct dwarf2_section_names loc_dwo;
43988095 555 struct dwarf2_section_names loclists_dwo;
09262596
DE
556 struct dwarf2_section_names macinfo_dwo;
557 struct dwarf2_section_names macro_dwo;
3019eac3
DE
558 struct dwarf2_section_names str_dwo;
559 struct dwarf2_section_names str_offsets_dwo;
560 struct dwarf2_section_names types_dwo;
80626a55
DE
561 struct dwarf2_section_names cu_index;
562 struct dwarf2_section_names tu_index;
3019eac3 563}
80626a55 564dwop_section_names =
3019eac3
DE
565{
566 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
567 { ".debug_info.dwo", ".zdebug_info.dwo" },
568 { ".debug_line.dwo", ".zdebug_line.dwo" },
569 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 570 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
571 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
572 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
573 { ".debug_str.dwo", ".zdebug_str.dwo" },
574 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
575 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
576 { ".debug_cu_index", ".zdebug_cu_index" },
577 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
578};
579
c906108c
SS
580/* local data types */
581
107d2387
AC
582/* The data in a compilation unit header, after target2host
583 translation, looks like this. */
c906108c 584struct comp_unit_head
a738430d 585{
c764a876 586 unsigned int length;
a738430d 587 short version;
a738430d
MK
588 unsigned char addr_size;
589 unsigned char signed_addr_p;
9c541725 590 sect_offset abbrev_sect_off;
57349743 591
a738430d
MK
592 /* Size of file offsets; either 4 or 8. */
593 unsigned int offset_size;
57349743 594
a738430d
MK
595 /* Size of the length field; either 4 or 12. */
596 unsigned int initial_length_size;
57349743 597
43988095
JK
598 enum dwarf_unit_type unit_type;
599
a738430d
MK
600 /* Offset to the first byte of this compilation unit header in the
601 .debug_info section, for resolving relative reference dies. */
9c541725 602 sect_offset sect_off;
57349743 603
d00adf39
DE
604 /* Offset to first die in this cu from the start of the cu.
605 This will be the first byte following the compilation unit header. */
9c541725 606 cu_offset first_die_cu_offset;
43988095
JK
607
608 /* 64-bit signature of this type unit - it is valid only for
609 UNIT_TYPE DW_UT_type. */
610 ULONGEST signature;
611
612 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 613 cu_offset type_cu_offset_in_tu;
a738430d 614};
c906108c 615
3da10d80
KS
616/* Type used for delaying computation of method physnames.
617 See comments for compute_delayed_physnames. */
618struct delayed_method_info
619{
620 /* The type to which the method is attached, i.e., its parent class. */
621 struct type *type;
622
623 /* The index of the method in the type's function fieldlists. */
624 int fnfield_index;
625
626 /* The index of the method in the fieldlist. */
627 int index;
628
629 /* The name of the DIE. */
630 const char *name;
631
632 /* The DIE associated with this method. */
633 struct die_info *die;
634};
635
636typedef struct delayed_method_info delayed_method_info;
637DEF_VEC_O (delayed_method_info);
638
e7c27a73
DJ
639/* Internal state when decoding a particular compilation unit. */
640struct dwarf2_cu
641{
642 /* The objfile containing this compilation unit. */
643 struct objfile *objfile;
644
d00adf39 645 /* The header of the compilation unit. */
e7c27a73 646 struct comp_unit_head header;
e142c38c 647
d00adf39
DE
648 /* Base address of this compilation unit. */
649 CORE_ADDR base_address;
650
651 /* Non-zero if base_address has been set. */
652 int base_known;
653
e142c38c
DJ
654 /* The language we are debugging. */
655 enum language language;
656 const struct language_defn *language_defn;
657
b0f35d58
DL
658 const char *producer;
659
e142c38c
DJ
660 /* The generic symbol table building routines have separate lists for
661 file scope symbols and all all other scopes (local scopes). So
662 we need to select the right one to pass to add_symbol_to_list().
663 We do it by keeping a pointer to the correct list in list_in_scope.
664
665 FIXME: The original dwarf code just treated the file scope as the
666 first local scope, and all other local scopes as nested local
667 scopes, and worked fine. Check to see if we really need to
668 distinguish these in buildsym.c. */
669 struct pending **list_in_scope;
670
433df2d4
DE
671 /* The abbrev table for this CU.
672 Normally this points to the abbrev table in the objfile.
673 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
674 struct abbrev_table *abbrev_table;
72bf9492 675
b64f50a1
JK
676 /* Hash table holding all the loaded partial DIEs
677 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
678 htab_t partial_dies;
679
680 /* Storage for things with the same lifetime as this read-in compilation
681 unit, including partial DIEs. */
682 struct obstack comp_unit_obstack;
683
ae038cb0
DJ
684 /* When multiple dwarf2_cu structures are living in memory, this field
685 chains them all together, so that they can be released efficiently.
686 We will probably also want a generation counter so that most-recently-used
687 compilation units are cached... */
688 struct dwarf2_per_cu_data *read_in_chain;
689
69d751e3 690 /* Backlink to our per_cu entry. */
ae038cb0
DJ
691 struct dwarf2_per_cu_data *per_cu;
692
693 /* How many compilation units ago was this CU last referenced? */
694 int last_used;
695
b64f50a1
JK
696 /* A hash table of DIE cu_offset for following references with
697 die_info->offset.sect_off as hash. */
51545339 698 htab_t die_hash;
10b3939b
DJ
699
700 /* Full DIEs if read in. */
701 struct die_info *dies;
702
703 /* A set of pointers to dwarf2_per_cu_data objects for compilation
704 units referenced by this one. Only set during full symbol processing;
705 partial symbol tables do not have dependencies. */
706 htab_t dependencies;
707
cb1df416
DJ
708 /* Header data from the line table, during full symbol processing. */
709 struct line_header *line_header;
4c8aa72d
PA
710 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
711 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
712 this is the DW_TAG_compile_unit die for this CU. We'll hold on
713 to the line header as long as this DIE is being processed. See
714 process_die_scope. */
715 die_info *line_header_die_owner;
cb1df416 716
3da10d80
KS
717 /* A list of methods which need to have physnames computed
718 after all type information has been read. */
719 VEC (delayed_method_info) *method_list;
720
96408a79
SA
721 /* To be copied to symtab->call_site_htab. */
722 htab_t call_site_htab;
723
034e5797
DE
724 /* Non-NULL if this CU came from a DWO file.
725 There is an invariant here that is important to remember:
726 Except for attributes copied from the top level DIE in the "main"
727 (or "stub") file in preparation for reading the DWO file
728 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
729 Either there isn't a DWO file (in which case this is NULL and the point
730 is moot), or there is and either we're not going to read it (in which
731 case this is NULL) or there is and we are reading it (in which case this
732 is non-NULL). */
3019eac3
DE
733 struct dwo_unit *dwo_unit;
734
735 /* The DW_AT_addr_base attribute if present, zero otherwise
736 (zero is a valid value though).
1dbab08b 737 Note this value comes from the Fission stub CU/TU's DIE. */
3019eac3
DE
738 ULONGEST addr_base;
739
2e3cf129
DE
740 /* The DW_AT_ranges_base attribute if present, zero otherwise
741 (zero is a valid value though).
1dbab08b 742 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 743 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
744 be used without needing to know whether DWO files are in use or not.
745 N.B. This does not apply to DW_AT_ranges appearing in
746 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
747 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
748 DW_AT_ranges_base *would* have to be applied, and we'd have to care
749 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
750 ULONGEST ranges_base;
751
ae038cb0
DJ
752 /* Mark used when releasing cached dies. */
753 unsigned int mark : 1;
754
8be455d7
JK
755 /* This CU references .debug_loc. See the symtab->locations_valid field.
756 This test is imperfect as there may exist optimized debug code not using
757 any location list and still facing inlining issues if handled as
758 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 759 unsigned int has_loclist : 1;
ba919b58 760
1b80a9fa
JK
761 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
762 if all the producer_is_* fields are valid. This information is cached
763 because profiling CU expansion showed excessive time spent in
764 producer_is_gxx_lt_4_6. */
ba919b58
TT
765 unsigned int checked_producer : 1;
766 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 767 unsigned int producer_is_gcc_lt_4_3 : 1;
5230b05a 768 unsigned int producer_is_icc_lt_14 : 1;
4d4ec4e5
TT
769
770 /* When set, the file that we're processing is known to have
771 debugging info for C++ namespaces. GCC 3.3.x did not produce
772 this information, but later versions do. */
773
774 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
775};
776
10b3939b
DJ
777/* Persistent data held for a compilation unit, even when not
778 processing it. We put a pointer to this structure in the
28dee7f5 779 read_symtab_private field of the psymtab. */
10b3939b 780
ae038cb0
DJ
781struct dwarf2_per_cu_data
782{
36586728 783 /* The start offset and length of this compilation unit.
45452591 784 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
785 initial_length_size.
786 If the DIE refers to a DWO file, this is always of the original die,
787 not the DWO file. */
9c541725 788 sect_offset sect_off;
36586728 789 unsigned int length;
ae038cb0 790
43988095
JK
791 /* DWARF standard version this data has been read from (such as 4 or 5). */
792 short dwarf_version;
793
ae038cb0
DJ
794 /* Flag indicating this compilation unit will be read in before
795 any of the current compilation units are processed. */
c764a876 796 unsigned int queued : 1;
ae038cb0 797
0d99eb77
DE
798 /* This flag will be set when reading partial DIEs if we need to load
799 absolutely all DIEs for this compilation unit, instead of just the ones
800 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
801 hash table and don't find it. */
802 unsigned int load_all_dies : 1;
803
0186c6a7
DE
804 /* Non-zero if this CU is from .debug_types.
805 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
806 this is non-zero. */
3019eac3
DE
807 unsigned int is_debug_types : 1;
808
36586728
TT
809 /* Non-zero if this CU is from the .dwz file. */
810 unsigned int is_dwz : 1;
811
a2ce51a0
DE
812 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
813 This flag is only valid if is_debug_types is true.
814 We can't read a CU directly from a DWO file: There are required
815 attributes in the stub. */
816 unsigned int reading_dwo_directly : 1;
817
7ee85ab1
DE
818 /* Non-zero if the TU has been read.
819 This is used to assist the "Stay in DWO Optimization" for Fission:
820 When reading a DWO, it's faster to read TUs from the DWO instead of
821 fetching them from random other DWOs (due to comdat folding).
822 If the TU has already been read, the optimization is unnecessary
823 (and unwise - we don't want to change where gdb thinks the TU lives
824 "midflight").
825 This flag is only valid if is_debug_types is true. */
826 unsigned int tu_read : 1;
827
3019eac3
DE
828 /* The section this CU/TU lives in.
829 If the DIE refers to a DWO file, this is always the original die,
830 not the DWO file. */
8a0459fd 831 struct dwarf2_section_info *section;
348e048f 832
17ea53c3 833 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
834 of the CU cache it gets reset to NULL again. This is left as NULL for
835 dummy CUs (a CU header, but nothing else). */
ae038cb0 836 struct dwarf2_cu *cu;
1c379e20 837
9cdd5dbd
DE
838 /* The corresponding objfile.
839 Normally we can get the objfile from dwarf2_per_objfile.
840 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
841 struct objfile *objfile;
842
fffbe6a8
YQ
843 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
844 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
845 union
846 {
847 /* The partial symbol table associated with this compilation unit,
95554aad 848 or NULL for unread partial units. */
9291a0cd
TT
849 struct partial_symtab *psymtab;
850
851 /* Data needed by the "quick" functions. */
852 struct dwarf2_per_cu_quick_data *quick;
853 } v;
95554aad 854
796a7ff8
DE
855 /* The CUs we import using DW_TAG_imported_unit. This is filled in
856 while reading psymtabs, used to compute the psymtab dependencies,
857 and then cleared. Then it is filled in again while reading full
858 symbols, and only deleted when the objfile is destroyed.
859
860 This is also used to work around a difference between the way gold
861 generates .gdb_index version <=7 and the way gdb does. Arguably this
862 is a gold bug. For symbols coming from TUs, gold records in the index
863 the CU that includes the TU instead of the TU itself. This breaks
864 dw2_lookup_symbol: It assumes that if the index says symbol X lives
865 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
866 will find X. Alas TUs live in their own symtab, so after expanding CU Y
867 we need to look in TU Z to find X. Fortunately, this is akin to
868 DW_TAG_imported_unit, so we just use the same mechanism: For
869 .gdb_index version <=7 this also records the TUs that the CU referred
870 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
871 indices so we only pay a price for gold generated indices.
872 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 873 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
874};
875
348e048f
DE
876/* Entry in the signatured_types hash table. */
877
878struct signatured_type
879{
42e7ad6c 880 /* The "per_cu" object of this type.
ac9ec31b 881 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
882 N.B.: This is the first member so that it's easy to convert pointers
883 between them. */
884 struct dwarf2_per_cu_data per_cu;
885
3019eac3 886 /* The type's signature. */
348e048f
DE
887 ULONGEST signature;
888
3019eac3 889 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
890 If this TU is a DWO stub and the definition lives in a DWO file
891 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
892 cu_offset type_offset_in_tu;
893
894 /* Offset in the section of the type's DIE.
895 If the definition lives in a DWO file, this is the offset in the
896 .debug_types.dwo section.
897 The value is zero until the actual value is known.
898 Zero is otherwise not a valid section offset. */
899 sect_offset type_offset_in_section;
0186c6a7
DE
900
901 /* Type units are grouped by their DW_AT_stmt_list entry so that they
902 can share them. This points to the containing symtab. */
903 struct type_unit_group *type_unit_group;
ac9ec31b
DE
904
905 /* The type.
906 The first time we encounter this type we fully read it in and install it
907 in the symbol tables. Subsequent times we only need the type. */
908 struct type *type;
a2ce51a0
DE
909
910 /* Containing DWO unit.
911 This field is valid iff per_cu.reading_dwo_directly. */
912 struct dwo_unit *dwo_unit;
348e048f
DE
913};
914
0186c6a7
DE
915typedef struct signatured_type *sig_type_ptr;
916DEF_VEC_P (sig_type_ptr);
917
094b34ac
DE
918/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
919 This includes type_unit_group and quick_file_names. */
920
921struct stmt_list_hash
922{
923 /* The DWO unit this table is from or NULL if there is none. */
924 struct dwo_unit *dwo_unit;
925
926 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 927 sect_offset line_sect_off;
094b34ac
DE
928};
929
f4dc4d17
DE
930/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
931 an object of this type. */
932
933struct type_unit_group
934{
0186c6a7 935 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
936 To simplify things we create an artificial CU that "includes" all the
937 type units using this stmt_list so that the rest of the code still has
938 a "per_cu" handle on the symtab.
939 This PER_CU is recognized by having no section. */
8a0459fd 940#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
941 struct dwarf2_per_cu_data per_cu;
942
0186c6a7
DE
943 /* The TUs that share this DW_AT_stmt_list entry.
944 This is added to while parsing type units to build partial symtabs,
945 and is deleted afterwards and not used again. */
946 VEC (sig_type_ptr) *tus;
f4dc4d17 947
43f3e411 948 /* The compunit symtab.
094b34ac 949 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
950 so we create an essentially anonymous symtab as the compunit symtab. */
951 struct compunit_symtab *compunit_symtab;
f4dc4d17 952
094b34ac
DE
953 /* The data used to construct the hash key. */
954 struct stmt_list_hash hash;
f4dc4d17
DE
955
956 /* The number of symtabs from the line header.
957 The value here must match line_header.num_file_names. */
958 unsigned int num_symtabs;
959
960 /* The symbol tables for this TU (obtained from the files listed in
961 DW_AT_stmt_list).
962 WARNING: The order of entries here must match the order of entries
963 in the line header. After the first TU using this type_unit_group, the
964 line header for the subsequent TUs is recreated from this. This is done
965 because we need to use the same symtabs for each TU using the same
966 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
967 there's no guarantee the line header doesn't have duplicate entries. */
968 struct symtab **symtabs;
969};
970
73869dc2 971/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
972
973struct dwo_sections
974{
975 struct dwarf2_section_info abbrev;
3019eac3
DE
976 struct dwarf2_section_info line;
977 struct dwarf2_section_info loc;
43988095 978 struct dwarf2_section_info loclists;
09262596
DE
979 struct dwarf2_section_info macinfo;
980 struct dwarf2_section_info macro;
3019eac3
DE
981 struct dwarf2_section_info str;
982 struct dwarf2_section_info str_offsets;
80626a55
DE
983 /* In the case of a virtual DWO file, these two are unused. */
984 struct dwarf2_section_info info;
3019eac3
DE
985 VEC (dwarf2_section_info_def) *types;
986};
987
c88ee1f0 988/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
989
990struct dwo_unit
991{
992 /* Backlink to the containing struct dwo_file. */
993 struct dwo_file *dwo_file;
994
995 /* The "id" that distinguishes this CU/TU.
996 .debug_info calls this "dwo_id", .debug_types calls this "signature".
997 Since signatures came first, we stick with it for consistency. */
998 ULONGEST signature;
999
1000 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 1001 struct dwarf2_section_info *section;
3019eac3 1002
9c541725
PA
1003 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
1004 sect_offset sect_off;
3019eac3
DE
1005 unsigned int length;
1006
1007 /* For types, offset in the type's DIE of the type defined by this TU. */
1008 cu_offset type_offset_in_tu;
1009};
1010
73869dc2
DE
1011/* include/dwarf2.h defines the DWP section codes.
1012 It defines a max value but it doesn't define a min value, which we
1013 use for error checking, so provide one. */
1014
1015enum dwp_v2_section_ids
1016{
1017 DW_SECT_MIN = 1
1018};
1019
80626a55 1020/* Data for one DWO file.
57d63ce2
DE
1021
1022 This includes virtual DWO files (a virtual DWO file is a DWO file as it
1023 appears in a DWP file). DWP files don't really have DWO files per se -
1024 comdat folding of types "loses" the DWO file they came from, and from
1025 a high level view DWP files appear to contain a mass of random types.
1026 However, to maintain consistency with the non-DWP case we pretend DWP
1027 files contain virtual DWO files, and we assign each TU with one virtual
1028 DWO file (generally based on the line and abbrev section offsets -
1029 a heuristic that seems to work in practice). */
3019eac3
DE
1030
1031struct dwo_file
1032{
0ac5b59e 1033 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
1034 For virtual DWO files the name is constructed from the section offsets
1035 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
1036 from related CU+TUs. */
0ac5b59e
DE
1037 const char *dwo_name;
1038
1039 /* The DW_AT_comp_dir attribute. */
1040 const char *comp_dir;
3019eac3 1041
80626a55
DE
1042 /* The bfd, when the file is open. Otherwise this is NULL.
1043 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1044 bfd *dbfd;
3019eac3 1045
73869dc2
DE
1046 /* The sections that make up this DWO file.
1047 Remember that for virtual DWO files in DWP V2, these are virtual
1048 sections (for lack of a better name). */
3019eac3
DE
1049 struct dwo_sections sections;
1050
33c5cd75
DB
1051 /* The CUs in the file.
1052 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1053 an extension to handle LLVM's Link Time Optimization output (where
1054 multiple source files may be compiled into a single object/dwo pair). */
1055 htab_t cus;
3019eac3
DE
1056
1057 /* Table of TUs in the file.
1058 Each element is a struct dwo_unit. */
1059 htab_t tus;
1060};
1061
80626a55
DE
1062/* These sections are what may appear in a DWP file. */
1063
1064struct dwp_sections
1065{
73869dc2 1066 /* These are used by both DWP version 1 and 2. */
80626a55
DE
1067 struct dwarf2_section_info str;
1068 struct dwarf2_section_info cu_index;
1069 struct dwarf2_section_info tu_index;
73869dc2
DE
1070
1071 /* These are only used by DWP version 2 files.
1072 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1073 sections are referenced by section number, and are not recorded here.
1074 In DWP version 2 there is at most one copy of all these sections, each
1075 section being (effectively) comprised of the concatenation of all of the
1076 individual sections that exist in the version 1 format.
1077 To keep the code simple we treat each of these concatenated pieces as a
1078 section itself (a virtual section?). */
1079 struct dwarf2_section_info abbrev;
1080 struct dwarf2_section_info info;
1081 struct dwarf2_section_info line;
1082 struct dwarf2_section_info loc;
1083 struct dwarf2_section_info macinfo;
1084 struct dwarf2_section_info macro;
1085 struct dwarf2_section_info str_offsets;
1086 struct dwarf2_section_info types;
80626a55
DE
1087};
1088
73869dc2
DE
1089/* These sections are what may appear in a virtual DWO file in DWP version 1.
1090 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 1091
73869dc2 1092struct virtual_v1_dwo_sections
80626a55
DE
1093{
1094 struct dwarf2_section_info abbrev;
1095 struct dwarf2_section_info line;
1096 struct dwarf2_section_info loc;
1097 struct dwarf2_section_info macinfo;
1098 struct dwarf2_section_info macro;
1099 struct dwarf2_section_info str_offsets;
1100 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 1101 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
1102 struct dwarf2_section_info info_or_types;
1103};
1104
73869dc2
DE
1105/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1106 In version 2, the sections of the DWO files are concatenated together
1107 and stored in one section of that name. Thus each ELF section contains
1108 several "virtual" sections. */
1109
1110struct virtual_v2_dwo_sections
1111{
1112 bfd_size_type abbrev_offset;
1113 bfd_size_type abbrev_size;
1114
1115 bfd_size_type line_offset;
1116 bfd_size_type line_size;
1117
1118 bfd_size_type loc_offset;
1119 bfd_size_type loc_size;
1120
1121 bfd_size_type macinfo_offset;
1122 bfd_size_type macinfo_size;
1123
1124 bfd_size_type macro_offset;
1125 bfd_size_type macro_size;
1126
1127 bfd_size_type str_offsets_offset;
1128 bfd_size_type str_offsets_size;
1129
1130 /* Each DWP hash table entry records one CU or one TU.
1131 That is recorded here, and copied to dwo_unit.section. */
1132 bfd_size_type info_or_types_offset;
1133 bfd_size_type info_or_types_size;
1134};
1135
80626a55
DE
1136/* Contents of DWP hash tables. */
1137
1138struct dwp_hash_table
1139{
73869dc2 1140 uint32_t version, nr_columns;
80626a55 1141 uint32_t nr_units, nr_slots;
73869dc2
DE
1142 const gdb_byte *hash_table, *unit_table;
1143 union
1144 {
1145 struct
1146 {
1147 const gdb_byte *indices;
1148 } v1;
1149 struct
1150 {
1151 /* This is indexed by column number and gives the id of the section
1152 in that column. */
1153#define MAX_NR_V2_DWO_SECTIONS \
1154 (1 /* .debug_info or .debug_types */ \
1155 + 1 /* .debug_abbrev */ \
1156 + 1 /* .debug_line */ \
1157 + 1 /* .debug_loc */ \
1158 + 1 /* .debug_str_offsets */ \
1159 + 1 /* .debug_macro or .debug_macinfo */)
1160 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1161 const gdb_byte *offsets;
1162 const gdb_byte *sizes;
1163 } v2;
1164 } section_pool;
80626a55
DE
1165};
1166
1167/* Data for one DWP file. */
1168
1169struct dwp_file
1170{
1171 /* Name of the file. */
1172 const char *name;
1173
73869dc2
DE
1174 /* File format version. */
1175 int version;
1176
93417882 1177 /* The bfd. */
80626a55
DE
1178 bfd *dbfd;
1179
1180 /* Section info for this file. */
1181 struct dwp_sections sections;
1182
57d63ce2 1183 /* Table of CUs in the file. */
80626a55
DE
1184 const struct dwp_hash_table *cus;
1185
1186 /* Table of TUs in the file. */
1187 const struct dwp_hash_table *tus;
1188
19ac8c2e
DE
1189 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1190 htab_t loaded_cus;
1191 htab_t loaded_tus;
80626a55 1192
73869dc2
DE
1193 /* Table to map ELF section numbers to their sections.
1194 This is only needed for the DWP V1 file format. */
80626a55
DE
1195 unsigned int num_sections;
1196 asection **elf_sections;
1197};
1198
36586728
TT
1199/* This represents a '.dwz' file. */
1200
1201struct dwz_file
1202{
1203 /* A dwz file can only contain a few sections. */
1204 struct dwarf2_section_info abbrev;
1205 struct dwarf2_section_info info;
1206 struct dwarf2_section_info str;
1207 struct dwarf2_section_info line;
1208 struct dwarf2_section_info macro;
2ec9a5e0 1209 struct dwarf2_section_info gdb_index;
927aa2e7 1210 struct dwarf2_section_info debug_names;
36586728
TT
1211
1212 /* The dwz's BFD. */
1213 bfd *dwz_bfd;
1214};
1215
0963b4bd
MS
1216/* Struct used to pass misc. parameters to read_die_and_children, et
1217 al. which are used for both .debug_info and .debug_types dies.
1218 All parameters here are unchanging for the life of the call. This
dee91e82 1219 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1220
1221struct die_reader_specs
1222{
a32a8923 1223 /* The bfd of die_section. */
93311388
DE
1224 bfd* abfd;
1225
1226 /* The CU of the DIE we are parsing. */
1227 struct dwarf2_cu *cu;
1228
80626a55 1229 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1230 struct dwo_file *dwo_file;
1231
dee91e82 1232 /* The section the die comes from.
3019eac3 1233 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1234 struct dwarf2_section_info *die_section;
1235
1236 /* die_section->buffer. */
d521ce57 1237 const gdb_byte *buffer;
f664829e
DE
1238
1239 /* The end of the buffer. */
1240 const gdb_byte *buffer_end;
a2ce51a0
DE
1241
1242 /* The value of the DW_AT_comp_dir attribute. */
1243 const char *comp_dir;
93311388
DE
1244};
1245
fd820528 1246/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1247typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1248 const gdb_byte *info_ptr,
dee91e82
DE
1249 struct die_info *comp_unit_die,
1250 int has_children,
1251 void *data);
1252
ecfb656c
PA
1253/* A 1-based directory index. This is a strong typedef to prevent
1254 accidentally using a directory index as a 0-based index into an
1255 array/vector. */
1256enum class dir_index : unsigned int {};
1257
1258/* Likewise, a 1-based file name index. */
1259enum class file_name_index : unsigned int {};
1260
52059ffd
TT
1261struct file_entry
1262{
fff8551c
PA
1263 file_entry () = default;
1264
ecfb656c 1265 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
1266 unsigned int mod_time_, unsigned int length_)
1267 : name (name_),
ecfb656c 1268 d_index (d_index_),
fff8551c
PA
1269 mod_time (mod_time_),
1270 length (length_)
1271 {}
1272
ecfb656c
PA
1273 /* Return the include directory at D_INDEX stored in LH. Returns
1274 NULL if D_INDEX is out of bounds. */
8c43009f
PA
1275 const char *include_dir (const line_header *lh) const;
1276
fff8551c
PA
1277 /* The file name. Note this is an observing pointer. The memory is
1278 owned by debug_line_buffer. */
1279 const char *name {};
1280
8c43009f 1281 /* The directory index (1-based). */
ecfb656c 1282 dir_index d_index {};
fff8551c
PA
1283
1284 unsigned int mod_time {};
1285
1286 unsigned int length {};
1287
1288 /* True if referenced by the Line Number Program. */
1289 bool included_p {};
1290
83769d0b 1291 /* The associated symbol table, if any. */
fff8551c 1292 struct symtab *symtab {};
52059ffd
TT
1293};
1294
debd256d
JB
1295/* The line number information for a compilation unit (found in the
1296 .debug_line section) begins with a "statement program header",
1297 which contains the following information. */
1298struct line_header
1299{
fff8551c
PA
1300 line_header ()
1301 : offset_in_dwz {}
1302 {}
1303
1304 /* Add an entry to the include directory table. */
1305 void add_include_dir (const char *include_dir);
1306
1307 /* Add an entry to the file name table. */
ecfb656c 1308 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1309 unsigned int mod_time, unsigned int length);
1310
ecfb656c 1311 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 1312 is out of bounds. */
ecfb656c 1313 const char *include_dir_at (dir_index index) const
8c43009f 1314 {
ecfb656c
PA
1315 /* Convert directory index number (1-based) to vector index
1316 (0-based). */
1317 size_t vec_index = to_underlying (index) - 1;
1318
1319 if (vec_index >= include_dirs.size ())
8c43009f 1320 return NULL;
ecfb656c 1321 return include_dirs[vec_index];
8c43009f
PA
1322 }
1323
ecfb656c 1324 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 1325 is out of bounds. */
ecfb656c 1326 file_entry *file_name_at (file_name_index index)
8c43009f 1327 {
ecfb656c
PA
1328 /* Convert file name index number (1-based) to vector index
1329 (0-based). */
1330 size_t vec_index = to_underlying (index) - 1;
1331
1332 if (vec_index >= file_names.size ())
fff8551c 1333 return NULL;
ecfb656c 1334 return &file_names[vec_index];
fff8551c
PA
1335 }
1336
1337 /* Const version of the above. */
1338 const file_entry *file_name_at (unsigned int index) const
1339 {
1340 if (index >= file_names.size ())
8c43009f
PA
1341 return NULL;
1342 return &file_names[index];
1343 }
1344
527f3840 1345 /* Offset of line number information in .debug_line section. */
9c541725 1346 sect_offset sect_off {};
527f3840
JK
1347
1348 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1349 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1350
1351 unsigned int total_length {};
1352 unsigned short version {};
1353 unsigned int header_length {};
1354 unsigned char minimum_instruction_length {};
1355 unsigned char maximum_ops_per_instruction {};
1356 unsigned char default_is_stmt {};
1357 int line_base {};
1358 unsigned char line_range {};
1359 unsigned char opcode_base {};
debd256d
JB
1360
1361 /* standard_opcode_lengths[i] is the number of operands for the
1362 standard opcode whose value is i. This means that
1363 standard_opcode_lengths[0] is unused, and the last meaningful
1364 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1365 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1366
fff8551c
PA
1367 /* The include_directories table. Note these are observing
1368 pointers. The memory is owned by debug_line_buffer. */
1369 std::vector<const char *> include_dirs;
debd256d 1370
fff8551c
PA
1371 /* The file_names table. */
1372 std::vector<file_entry> file_names;
debd256d
JB
1373
1374 /* The start and end of the statement program following this
6502dd73 1375 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1376 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1377};
c906108c 1378
fff8551c
PA
1379typedef std::unique_ptr<line_header> line_header_up;
1380
8c43009f
PA
1381const char *
1382file_entry::include_dir (const line_header *lh) const
1383{
ecfb656c 1384 return lh->include_dir_at (d_index);
8c43009f
PA
1385}
1386
c906108c 1387/* When we construct a partial symbol table entry we only
0963b4bd 1388 need this much information. */
c906108c
SS
1389struct partial_die_info
1390 {
72bf9492 1391 /* Offset of this DIE. */
9c541725 1392 sect_offset sect_off;
72bf9492
DJ
1393
1394 /* DWARF-2 tag for this DIE. */
1395 ENUM_BITFIELD(dwarf_tag) tag : 16;
1396
72bf9492
DJ
1397 /* Assorted flags describing the data found in this DIE. */
1398 unsigned int has_children : 1;
1399 unsigned int is_external : 1;
1400 unsigned int is_declaration : 1;
1401 unsigned int has_type : 1;
1402 unsigned int has_specification : 1;
1403 unsigned int has_pc_info : 1;
481860b3 1404 unsigned int may_be_inlined : 1;
72bf9492 1405
0c1b455e
TT
1406 /* This DIE has been marked DW_AT_main_subprogram. */
1407 unsigned int main_subprogram : 1;
1408
72bf9492
DJ
1409 /* Flag set if the SCOPE field of this structure has been
1410 computed. */
1411 unsigned int scope_set : 1;
1412
fa4028e9
JB
1413 /* Flag set if the DIE has a byte_size attribute. */
1414 unsigned int has_byte_size : 1;
1415
ff908ebf
AW
1416 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1417 unsigned int has_const_value : 1;
1418
98bfdba5
PA
1419 /* Flag set if any of the DIE's children are template arguments. */
1420 unsigned int has_template_arguments : 1;
1421
abc72ce4
DE
1422 /* Flag set if fixup_partial_die has been called on this die. */
1423 unsigned int fixup_called : 1;
1424
36586728
TT
1425 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1426 unsigned int is_dwz : 1;
1427
1428 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1429 unsigned int spec_is_dwz : 1;
1430
72bf9492 1431 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1432 sometimes a default name for unnamed DIEs. */
15d034d0 1433 const char *name;
72bf9492 1434
abc72ce4
DE
1435 /* The linkage name, if present. */
1436 const char *linkage_name;
1437
72bf9492
DJ
1438 /* The scope to prepend to our children. This is generally
1439 allocated on the comp_unit_obstack, so will disappear
1440 when this compilation unit leaves the cache. */
15d034d0 1441 const char *scope;
72bf9492 1442
95554aad
TT
1443 /* Some data associated with the partial DIE. The tag determines
1444 which field is live. */
1445 union
1446 {
1447 /* The location description associated with this DIE, if any. */
1448 struct dwarf_block *locdesc;
1449 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1450 sect_offset sect_off;
95554aad 1451 } d;
72bf9492
DJ
1452
1453 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1454 CORE_ADDR lowpc;
1455 CORE_ADDR highpc;
72bf9492 1456
93311388 1457 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1458 DW_AT_sibling, if any. */
abc72ce4
DE
1459 /* NOTE: This member isn't strictly necessary, read_partial_die could
1460 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1461 const gdb_byte *sibling;
72bf9492
DJ
1462
1463 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1464 DW_AT_specification (or DW_AT_abstract_origin or
1465 DW_AT_extension). */
b64f50a1 1466 sect_offset spec_offset;
72bf9492
DJ
1467
1468 /* Pointers to this DIE's parent, first child, and next sibling,
1469 if any. */
1470 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1471 };
1472
0963b4bd 1473/* This data structure holds the information of an abbrev. */
c906108c
SS
1474struct abbrev_info
1475 {
1476 unsigned int number; /* number identifying abbrev */
1477 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1478 unsigned short has_children; /* boolean */
1479 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1480 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1481 struct abbrev_info *next; /* next in chain */
1482 };
1483
1484struct attr_abbrev
1485 {
9d25dd43
DE
1486 ENUM_BITFIELD(dwarf_attribute) name : 16;
1487 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1488
1489 /* It is valid only if FORM is DW_FORM_implicit_const. */
1490 LONGEST implicit_const;
c906108c
SS
1491 };
1492
433df2d4
DE
1493/* Size of abbrev_table.abbrev_hash_table. */
1494#define ABBREV_HASH_SIZE 121
1495
1496/* Top level data structure to contain an abbreviation table. */
1497
1498struct abbrev_table
1499{
f4dc4d17
DE
1500 /* Where the abbrev table came from.
1501 This is used as a sanity check when the table is used. */
9c541725 1502 sect_offset sect_off;
433df2d4
DE
1503
1504 /* Storage for the abbrev table. */
1505 struct obstack abbrev_obstack;
1506
1507 /* Hash table of abbrevs.
1508 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1509 It could be statically allocated, but the previous code didn't so we
1510 don't either. */
1511 struct abbrev_info **abbrevs;
1512};
1513
0963b4bd 1514/* Attributes have a name and a value. */
b60c80d6
DJ
1515struct attribute
1516 {
9d25dd43 1517 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1518 ENUM_BITFIELD(dwarf_form) form : 15;
1519
1520 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1521 field should be in u.str (existing only for DW_STRING) but it is kept
1522 here for better struct attribute alignment. */
1523 unsigned int string_is_canonical : 1;
1524
b60c80d6
DJ
1525 union
1526 {
15d034d0 1527 const char *str;
b60c80d6 1528 struct dwarf_block *blk;
43bbcdc2
PH
1529 ULONGEST unsnd;
1530 LONGEST snd;
b60c80d6 1531 CORE_ADDR addr;
ac9ec31b 1532 ULONGEST signature;
b60c80d6
DJ
1533 }
1534 u;
1535 };
1536
0963b4bd 1537/* This data structure holds a complete die structure. */
c906108c
SS
1538struct die_info
1539 {
76815b17
DE
1540 /* DWARF-2 tag for this DIE. */
1541 ENUM_BITFIELD(dwarf_tag) tag : 16;
1542
1543 /* Number of attributes */
98bfdba5
PA
1544 unsigned char num_attrs;
1545
1546 /* True if we're presently building the full type name for the
1547 type derived from this DIE. */
1548 unsigned char building_fullname : 1;
76815b17 1549
adde2bff
DE
1550 /* True if this die is in process. PR 16581. */
1551 unsigned char in_process : 1;
1552
76815b17
DE
1553 /* Abbrev number */
1554 unsigned int abbrev;
1555
93311388 1556 /* Offset in .debug_info or .debug_types section. */
9c541725 1557 sect_offset sect_off;
78ba4af6
JB
1558
1559 /* The dies in a compilation unit form an n-ary tree. PARENT
1560 points to this die's parent; CHILD points to the first child of
1561 this node; and all the children of a given node are chained
4950bc1c 1562 together via their SIBLING fields. */
639d11d3
DC
1563 struct die_info *child; /* Its first child, if any. */
1564 struct die_info *sibling; /* Its next sibling, if any. */
1565 struct die_info *parent; /* Its parent, if any. */
c906108c 1566
b60c80d6
DJ
1567 /* An array of attributes, with NUM_ATTRS elements. There may be
1568 zero, but it's not common and zero-sized arrays are not
1569 sufficiently portable C. */
1570 struct attribute attrs[1];
c906108c
SS
1571 };
1572
0963b4bd 1573/* Get at parts of an attribute structure. */
c906108c
SS
1574
1575#define DW_STRING(attr) ((attr)->u.str)
8285870a 1576#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1577#define DW_UNSND(attr) ((attr)->u.unsnd)
1578#define DW_BLOCK(attr) ((attr)->u.blk)
1579#define DW_SND(attr) ((attr)->u.snd)
1580#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1581#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1582
0963b4bd 1583/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1584struct dwarf_block
1585 {
56eb65bd 1586 size_t size;
1d6edc3c
JK
1587
1588 /* Valid only if SIZE is not zero. */
d521ce57 1589 const gdb_byte *data;
c906108c
SS
1590 };
1591
c906108c
SS
1592#ifndef ATTR_ALLOC_CHUNK
1593#define ATTR_ALLOC_CHUNK 4
1594#endif
1595
c906108c
SS
1596/* Allocate fields for structs, unions and enums in this size. */
1597#ifndef DW_FIELD_ALLOC_CHUNK
1598#define DW_FIELD_ALLOC_CHUNK 4
1599#endif
1600
c906108c
SS
1601/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1602 but this would require a corresponding change in unpack_field_as_long
1603 and friends. */
1604static int bits_per_byte = 8;
1605
52059ffd
TT
1606struct nextfield
1607{
1608 struct nextfield *next;
1609 int accessibility;
1610 int virtuality;
1611 struct field field;
1612};
1613
1614struct nextfnfield
1615{
1616 struct nextfnfield *next;
1617 struct fn_field fnfield;
1618};
1619
1620struct fnfieldlist
1621{
1622 const char *name;
1623 int length;
1624 struct nextfnfield *head;
1625};
1626
883fd55a 1627struct decl_field_list
52059ffd 1628{
883fd55a
KS
1629 struct decl_field field;
1630 struct decl_field_list *next;
52059ffd
TT
1631};
1632
c906108c
SS
1633/* The routines that read and process dies for a C struct or C++ class
1634 pass lists of data member fields and lists of member function fields
1635 in an instance of a field_info structure, as defined below. */
1636struct field_info
c5aa993b 1637 {
0963b4bd 1638 /* List of data member and baseclasses fields. */
52059ffd 1639 struct nextfield *fields, *baseclasses;
c906108c 1640
7d0ccb61 1641 /* Number of fields (including baseclasses). */
c5aa993b 1642 int nfields;
c906108c 1643
c5aa993b
JM
1644 /* Number of baseclasses. */
1645 int nbaseclasses;
c906108c 1646
c5aa993b
JM
1647 /* Set if the accesibility of one of the fields is not public. */
1648 int non_public_fields;
c906108c 1649
c5aa993b
JM
1650 /* Member function fieldlist array, contains name of possibly overloaded
1651 member function, number of overloaded member functions and a pointer
1652 to the head of the member function field chain. */
52059ffd 1653 struct fnfieldlist *fnfieldlists;
c906108c 1654
c5aa993b
JM
1655 /* Number of entries in the fnfieldlists array. */
1656 int nfnfields;
98751a41
JK
1657
1658 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1659 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
883fd55a 1660 struct decl_field_list *typedef_field_list;
98751a41 1661 unsigned typedef_field_list_count;
883fd55a
KS
1662
1663 /* Nested types defined by this class and the number of elements in this
1664 list. */
1665 struct decl_field_list *nested_types_list;
1666 unsigned nested_types_list_count;
c5aa993b 1667 };
c906108c 1668
10b3939b
DJ
1669/* One item on the queue of compilation units to read in full symbols
1670 for. */
1671struct dwarf2_queue_item
1672{
1673 struct dwarf2_per_cu_data *per_cu;
95554aad 1674 enum language pretend_language;
10b3939b
DJ
1675 struct dwarf2_queue_item *next;
1676};
1677
1678/* The current queue. */
1679static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1680
ae038cb0
DJ
1681/* Loaded secondary compilation units are kept in memory until they
1682 have not been referenced for the processing of this many
1683 compilation units. Set this to zero to disable caching. Cache
1684 sizes of up to at least twenty will improve startup time for
1685 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1686static int dwarf_max_cache_age = 5;
920d2a44 1687static void
b4f54984
DE
1688show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1689 struct cmd_list_element *c, const char *value)
920d2a44 1690{
3e43a32a 1691 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1692 "DWARF compilation units is %s.\n"),
920d2a44
AC
1693 value);
1694}
4390d890 1695\f
c906108c
SS
1696/* local function prototypes */
1697
a32a8923
DE
1698static const char *get_section_name (const struct dwarf2_section_info *);
1699
1700static const char *get_section_file_name (const struct dwarf2_section_info *);
1701
918dd910
JK
1702static void dwarf2_find_base_address (struct die_info *die,
1703 struct dwarf2_cu *cu);
1704
0018ea6f
DE
1705static struct partial_symtab *create_partial_symtab
1706 (struct dwarf2_per_cu_data *per_cu, const char *name);
1707
f1902523
JK
1708static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1709 const gdb_byte *info_ptr,
1710 struct die_info *type_unit_die,
1711 int has_children, void *data);
1712
c67a9c90 1713static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1714
72bf9492
DJ
1715static void scan_partial_symbols (struct partial_die_info *,
1716 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1717 int, struct dwarf2_cu *);
c906108c 1718
72bf9492
DJ
1719static void add_partial_symbol (struct partial_die_info *,
1720 struct dwarf2_cu *);
63d06c5c 1721
72bf9492
DJ
1722static void add_partial_namespace (struct partial_die_info *pdi,
1723 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1724 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1725
5d7cb8df 1726static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1727 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1728 struct dwarf2_cu *cu);
1729
72bf9492
DJ
1730static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1731 struct dwarf2_cu *cu);
91c24f0a 1732
bc30ff58
JB
1733static void add_partial_subprogram (struct partial_die_info *pdi,
1734 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1735 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1736
257e7a09
YQ
1737static void dwarf2_read_symtab (struct partial_symtab *,
1738 struct objfile *);
c906108c 1739
a14ed312 1740static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1741
433df2d4
DE
1742static struct abbrev_info *abbrev_table_lookup_abbrev
1743 (const struct abbrev_table *, unsigned int);
1744
1745static struct abbrev_table *abbrev_table_read_table
1746 (struct dwarf2_section_info *, sect_offset);
1747
1748static void abbrev_table_free (struct abbrev_table *);
1749
f4dc4d17
DE
1750static void abbrev_table_free_cleanup (void *);
1751
dee91e82
DE
1752static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1753 struct dwarf2_section_info *);
c906108c 1754
f3dd6933 1755static void dwarf2_free_abbrev_table (void *);
c906108c 1756
d521ce57 1757static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1758
dee91e82 1759static struct partial_die_info *load_partial_dies
d521ce57 1760 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1761
d521ce57
TT
1762static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1763 struct partial_die_info *,
1764 struct abbrev_info *,
1765 unsigned int,
1766 const gdb_byte *);
c906108c 1767
36586728 1768static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1769 struct dwarf2_cu *);
72bf9492
DJ
1770
1771static void fixup_partial_die (struct partial_die_info *,
1772 struct dwarf2_cu *);
1773
d521ce57
TT
1774static const gdb_byte *read_attribute (const struct die_reader_specs *,
1775 struct attribute *, struct attr_abbrev *,
1776 const gdb_byte *);
a8329558 1777
a1855c1d 1778static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1779
a1855c1d 1780static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1781
a1855c1d 1782static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1783
a1855c1d 1784static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1785
a1855c1d 1786static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1787
d521ce57 1788static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1789 unsigned int *);
c906108c 1790
d521ce57 1791static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1792
1793static LONGEST read_checked_initial_length_and_offset
d521ce57 1794 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1795 unsigned int *, unsigned int *);
613e1657 1796
d521ce57
TT
1797static LONGEST read_offset (bfd *, const gdb_byte *,
1798 const struct comp_unit_head *,
c764a876
DE
1799 unsigned int *);
1800
d521ce57 1801static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1802
f4dc4d17
DE
1803static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1804 sect_offset);
1805
d521ce57 1806static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1807
d521ce57 1808static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1809
d521ce57
TT
1810static const char *read_indirect_string (bfd *, const gdb_byte *,
1811 const struct comp_unit_head *,
1812 unsigned int *);
4bdf3d34 1813
43988095
JK
1814static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1815 const struct comp_unit_head *,
1816 unsigned int *);
36586728 1817
927aa2e7
JK
1818static const char *read_indirect_string_at_offset (bfd *abfd,
1819 LONGEST str_offset);
1820
43988095 1821static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
c906108c 1822
d521ce57 1823static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1824
d521ce57
TT
1825static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1826 const gdb_byte *,
3019eac3
DE
1827 unsigned int *);
1828
d521ce57 1829static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1830 ULONGEST str_index);
3019eac3 1831
e142c38c 1832static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1833
e142c38c
DJ
1834static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1835 struct dwarf2_cu *);
c906108c 1836
348e048f 1837static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1838 unsigned int);
348e048f 1839
7d45c7c3
KB
1840static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1841 struct dwarf2_cu *cu);
1842
05cf31d1
JB
1843static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1844 struct dwarf2_cu *cu);
1845
e142c38c 1846static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1847
e142c38c 1848static struct die_info *die_specification (struct die_info *die,
f2f0e013 1849 struct dwarf2_cu **);
63d06c5c 1850
9c541725 1851static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1852 struct dwarf2_cu *cu);
debd256d 1853
f3f5162e 1854static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1855 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1856 CORE_ADDR, int decode_mapping);
c906108c 1857
4d663531 1858static void dwarf2_start_subfile (const char *, const char *);
c906108c 1859
43f3e411
DE
1860static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1861 const char *, const char *,
1862 CORE_ADDR);
f4dc4d17 1863
a14ed312 1864static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1865 struct dwarf2_cu *);
c906108c 1866
34eaf542
TT
1867static struct symbol *new_symbol_full (struct die_info *, struct type *,
1868 struct dwarf2_cu *, struct symbol *);
1869
ff39bb5e 1870static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1871 struct dwarf2_cu *);
c906108c 1872
ff39bb5e 1873static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1874 struct type *type,
1875 const char *name,
1876 struct obstack *obstack,
12df843f 1877 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1878 const gdb_byte **bytes,
98bfdba5 1879 struct dwarf2_locexpr_baton **baton);
2df3850c 1880
e7c27a73 1881static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1882
b4ba55a1
JB
1883static int need_gnat_info (struct dwarf2_cu *);
1884
3e43a32a
MS
1885static struct type *die_descriptive_type (struct die_info *,
1886 struct dwarf2_cu *);
b4ba55a1
JB
1887
1888static void set_descriptive_type (struct type *, struct die_info *,
1889 struct dwarf2_cu *);
1890
e7c27a73
DJ
1891static struct type *die_containing_type (struct die_info *,
1892 struct dwarf2_cu *);
c906108c 1893
ff39bb5e 1894static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1895 struct dwarf2_cu *);
c906108c 1896
f792889a 1897static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1898
673bfd45
DE
1899static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1900
0d5cff50 1901static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1902
6e70227d 1903static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1904 const char *suffix, int physname,
1905 struct dwarf2_cu *cu);
63d06c5c 1906
e7c27a73 1907static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1908
348e048f
DE
1909static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1910
e7c27a73 1911static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1912
e7c27a73 1913static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1914
96408a79
SA
1915static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1916
71a3c369
TT
1917static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1918
ff013f42
JK
1919static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1920 struct dwarf2_cu *, struct partial_symtab *);
1921
3a2b436a 1922/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1923 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1924enum pc_bounds_kind
1925{
e385593e 1926 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1927 PC_BOUNDS_NOT_PRESENT,
1928
e385593e
JK
1929 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1930 were present but they do not form a valid range of PC addresses. */
1931 PC_BOUNDS_INVALID,
1932
3a2b436a
JK
1933 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1934 PC_BOUNDS_RANGES,
1935
1936 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1937 PC_BOUNDS_HIGH_LOW,
1938};
1939
1940static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1941 CORE_ADDR *, CORE_ADDR *,
1942 struct dwarf2_cu *,
1943 struct partial_symtab *);
c906108c 1944
fae299cd
DC
1945static void get_scope_pc_bounds (struct die_info *,
1946 CORE_ADDR *, CORE_ADDR *,
1947 struct dwarf2_cu *);
1948
801e3a5b
JB
1949static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1950 CORE_ADDR, struct dwarf2_cu *);
1951
a14ed312 1952static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1953 struct dwarf2_cu *);
c906108c 1954
a14ed312 1955static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1956 struct type *, struct dwarf2_cu *);
c906108c 1957
a14ed312 1958static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1959 struct die_info *, struct type *,
e7c27a73 1960 struct dwarf2_cu *);
c906108c 1961
a14ed312 1962static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1963 struct type *,
1964 struct dwarf2_cu *);
c906108c 1965
134d01f1 1966static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1967
e7c27a73 1968static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1969
e7c27a73 1970static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1971
5d7cb8df
JK
1972static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1973
22cee43f
PMR
1974static struct using_direct **using_directives (enum language);
1975
27aa8d6a
SW
1976static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1977
74921315
KS
1978static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1979
f55ee35c
JK
1980static struct type *read_module_type (struct die_info *die,
1981 struct dwarf2_cu *cu);
1982
38d518c9 1983static const char *namespace_name (struct die_info *die,
e142c38c 1984 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1985
134d01f1 1986static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1987
e7c27a73 1988static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1989
6e70227d 1990static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1991 struct dwarf2_cu *);
1992
bf6af496 1993static struct die_info *read_die_and_siblings_1
d521ce57 1994 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1995 struct die_info *);
639d11d3 1996
dee91e82 1997static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1998 const gdb_byte *info_ptr,
1999 const gdb_byte **new_info_ptr,
639d11d3
DC
2000 struct die_info *parent);
2001
d521ce57
TT
2002static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
2003 struct die_info **, const gdb_byte *,
2004 int *, int);
3019eac3 2005
d521ce57
TT
2006static const gdb_byte *read_full_die (const struct die_reader_specs *,
2007 struct die_info **, const gdb_byte *,
2008 int *);
93311388 2009
e7c27a73 2010static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 2011
15d034d0
TT
2012static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
2013 struct obstack *);
71c25dea 2014
15d034d0 2015static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 2016
15d034d0 2017static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
2018 struct die_info *die,
2019 struct dwarf2_cu *cu);
2020
ca69b9e6
DE
2021static const char *dwarf2_physname (const char *name, struct die_info *die,
2022 struct dwarf2_cu *cu);
2023
e142c38c 2024static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 2025 struct dwarf2_cu **);
9219021c 2026
f39c6ffd 2027static const char *dwarf_tag_name (unsigned int);
c906108c 2028
f39c6ffd 2029static const char *dwarf_attr_name (unsigned int);
c906108c 2030
f39c6ffd 2031static const char *dwarf_form_name (unsigned int);
c906108c 2032
a121b7c1 2033static const char *dwarf_bool_name (unsigned int);
c906108c 2034
f39c6ffd 2035static const char *dwarf_type_encoding_name (unsigned int);
c906108c 2036
f9aca02d 2037static struct die_info *sibling_die (struct die_info *);
c906108c 2038
d97bc12b
DE
2039static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2040
2041static void dump_die_for_error (struct die_info *);
2042
2043static void dump_die_1 (struct ui_file *, int level, int max_level,
2044 struct die_info *);
c906108c 2045
d97bc12b 2046/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 2047
51545339 2048static void store_in_ref_table (struct die_info *,
10b3939b 2049 struct dwarf2_cu *);
c906108c 2050
ff39bb5e 2051static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 2052
ff39bb5e 2053static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 2054
348e048f 2055static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 2056 const struct attribute *,
348e048f
DE
2057 struct dwarf2_cu **);
2058
10b3939b 2059static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 2060 const struct attribute *,
f2f0e013 2061 struct dwarf2_cu **);
c906108c 2062
348e048f 2063static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 2064 const struct attribute *,
348e048f
DE
2065 struct dwarf2_cu **);
2066
ac9ec31b
DE
2067static struct type *get_signatured_type (struct die_info *, ULONGEST,
2068 struct dwarf2_cu *);
2069
2070static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 2071 const struct attribute *,
ac9ec31b
DE
2072 struct dwarf2_cu *);
2073
e5fe5e75 2074static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 2075
52dc124a 2076static void read_signatured_type (struct signatured_type *);
348e048f 2077
63e43d3a
PMR
2078static int attr_to_dynamic_prop (const struct attribute *attr,
2079 struct die_info *die, struct dwarf2_cu *cu,
2080 struct dynamic_prop *prop);
2081
c906108c
SS
2082/* memory allocation interface */
2083
7b5a2f43 2084static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 2085
b60c80d6 2086static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 2087
43f3e411 2088static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 2089
6e5a29e1 2090static int attr_form_is_block (const struct attribute *);
8e19ed76 2091
6e5a29e1 2092static int attr_form_is_section_offset (const struct attribute *);
3690dd37 2093
6e5a29e1 2094static int attr_form_is_constant (const struct attribute *);
3690dd37 2095
6e5a29e1 2096static int attr_form_is_ref (const struct attribute *);
7771576e 2097
8cf6f0b1
TT
2098static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2099 struct dwarf2_loclist_baton *baton,
ff39bb5e 2100 const struct attribute *attr);
8cf6f0b1 2101
ff39bb5e 2102static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 2103 struct symbol *sym,
f1e6e072
TT
2104 struct dwarf2_cu *cu,
2105 int is_block);
4c2df51b 2106
d521ce57
TT
2107static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2108 const gdb_byte *info_ptr,
2109 struct abbrev_info *abbrev);
4bb7a0a7 2110
72bf9492
DJ
2111static void free_stack_comp_unit (void *);
2112
72bf9492
DJ
2113static hashval_t partial_die_hash (const void *item);
2114
2115static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2116
ae038cb0 2117static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
9c541725 2118 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 2119
9816fde3 2120static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 2121 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
2122
2123static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
2124 struct die_info *comp_unit_die,
2125 enum language pretend_language);
93311388 2126
68dc6402 2127static void free_heap_comp_unit (void *);
ae038cb0
DJ
2128
2129static void free_cached_comp_units (void *);
2130
2131static void age_cached_comp_units (void);
2132
dee91e82 2133static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 2134
f792889a
DJ
2135static struct type *set_die_type (struct die_info *, struct type *,
2136 struct dwarf2_cu *);
1c379e20 2137
ae038cb0
DJ
2138static void create_all_comp_units (struct objfile *);
2139
0e50663e 2140static int create_all_type_units (struct objfile *);
1fd400ff 2141
95554aad
TT
2142static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2143 enum language);
10b3939b 2144
95554aad
TT
2145static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2146 enum language);
10b3939b 2147
f4dc4d17
DE
2148static void process_full_type_unit (struct dwarf2_per_cu_data *,
2149 enum language);
2150
10b3939b
DJ
2151static void dwarf2_add_dependence (struct dwarf2_cu *,
2152 struct dwarf2_per_cu_data *);
2153
ae038cb0
DJ
2154static void dwarf2_mark (struct dwarf2_cu *);
2155
2156static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2157
b64f50a1 2158static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 2159 struct dwarf2_per_cu_data *);
673bfd45 2160
f792889a 2161static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 2162
9291a0cd
TT
2163static void dwarf2_release_queue (void *dummy);
2164
95554aad
TT
2165static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2166 enum language pretend_language);
2167
a0f42c21 2168static void process_queue (void);
9291a0cd 2169
d721ba37
PA
2170/* The return type of find_file_and_directory. Note, the enclosed
2171 string pointers are only valid while this object is valid. */
2172
2173struct file_and_directory
2174{
2175 /* The filename. This is never NULL. */
2176 const char *name;
2177
2178 /* The compilation directory. NULL if not known. If we needed to
2179 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2180 points directly to the DW_AT_comp_dir string attribute owned by
2181 the obstack that owns the DIE. */
2182 const char *comp_dir;
2183
2184 /* If we needed to build a new string for comp_dir, this is what
2185 owns the storage. */
2186 std::string comp_dir_storage;
2187};
2188
2189static file_and_directory find_file_and_directory (struct die_info *die,
2190 struct dwarf2_cu *cu);
9291a0cd
TT
2191
2192static char *file_full_name (int file, struct line_header *lh,
2193 const char *comp_dir);
2194
43988095
JK
2195/* Expected enum dwarf_unit_type for read_comp_unit_head. */
2196enum class rcuh_kind { COMPILE, TYPE };
2197
d521ce57 2198static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
2199 (struct comp_unit_head *header,
2200 struct dwarf2_section_info *section,
d521ce57 2201 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 2202 rcuh_kind section_kind);
36586728 2203
fd820528 2204static void init_cutu_and_read_dies
f4dc4d17
DE
2205 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2206 int use_existing_cu, int keep,
3019eac3
DE
2207 die_reader_func_ftype *die_reader_func, void *data);
2208
dee91e82
DE
2209static void init_cutu_and_read_dies_simple
2210 (struct dwarf2_per_cu_data *this_cu,
2211 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 2212
673bfd45 2213static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2214
3019eac3
DE
2215static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2216
57d63ce2
DE
2217static struct dwo_unit *lookup_dwo_unit_in_dwp
2218 (struct dwp_file *dwp_file, const char *comp_dir,
2219 ULONGEST signature, int is_debug_types);
a2ce51a0
DE
2220
2221static struct dwp_file *get_dwp_file (void);
2222
3019eac3 2223static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2224 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2225
2226static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2227 (struct signatured_type *, const char *, const char *);
3019eac3 2228
89e63ee4
DE
2229static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2230
3019eac3
DE
2231static void free_dwo_file_cleanup (void *);
2232
95554aad
TT
2233static void process_cu_includes (void);
2234
1b80a9fa 2235static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2236
2237static void free_line_header_voidp (void *arg);
4390d890
DE
2238\f
2239/* Various complaints about symbol reading that don't abort the process. */
2240
2241static void
2242dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2243{
2244 complaint (&symfile_complaints,
2245 _("statement list doesn't fit in .debug_line section"));
2246}
2247
2248static void
2249dwarf2_debug_line_missing_file_complaint (void)
2250{
2251 complaint (&symfile_complaints,
2252 _(".debug_line section has line data without a file"));
2253}
2254
2255static void
2256dwarf2_debug_line_missing_end_sequence_complaint (void)
2257{
2258 complaint (&symfile_complaints,
2259 _(".debug_line section has line "
2260 "program sequence without an end"));
2261}
2262
2263static void
2264dwarf2_complex_location_expr_complaint (void)
2265{
2266 complaint (&symfile_complaints, _("location expression too complex"));
2267}
2268
2269static void
2270dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2271 int arg3)
2272{
2273 complaint (&symfile_complaints,
2274 _("const value length mismatch for '%s', got %d, expected %d"),
2275 arg1, arg2, arg3);
2276}
2277
2278static void
2279dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2280{
2281 complaint (&symfile_complaints,
2282 _("debug info runs off end of %s section"
2283 " [in module %s]"),
a32a8923
DE
2284 get_section_name (section),
2285 get_section_file_name (section));
4390d890 2286}
1b80a9fa 2287
4390d890
DE
2288static void
2289dwarf2_macro_malformed_definition_complaint (const char *arg1)
2290{
2291 complaint (&symfile_complaints,
2292 _("macro debug info contains a "
2293 "malformed macro definition:\n`%s'"),
2294 arg1);
2295}
2296
2297static void
2298dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2299{
2300 complaint (&symfile_complaints,
2301 _("invalid attribute class or form for '%s' in '%s'"),
2302 arg1, arg2);
2303}
527f3840
JK
2304
2305/* Hash function for line_header_hash. */
2306
2307static hashval_t
2308line_header_hash (const struct line_header *ofs)
2309{
9c541725 2310 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2311}
2312
2313/* Hash function for htab_create_alloc_ex for line_header_hash. */
2314
2315static hashval_t
2316line_header_hash_voidp (const void *item)
2317{
9a3c8263 2318 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2319
2320 return line_header_hash (ofs);
2321}
2322
2323/* Equality function for line_header_hash. */
2324
2325static int
2326line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2327{
9a3c8263
SM
2328 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2329 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2330
9c541725 2331 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2332 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2333}
2334
4390d890 2335\f
9291a0cd 2336
31aa7e4e
JB
2337/* Read the given attribute value as an address, taking the attribute's
2338 form into account. */
2339
2340static CORE_ADDR
2341attr_value_as_address (struct attribute *attr)
2342{
2343 CORE_ADDR addr;
2344
2345 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2346 {
2347 /* Aside from a few clearly defined exceptions, attributes that
2348 contain an address must always be in DW_FORM_addr form.
2349 Unfortunately, some compilers happen to be violating this
2350 requirement by encoding addresses using other forms, such
2351 as DW_FORM_data4 for example. For those broken compilers,
2352 we try to do our best, without any guarantee of success,
2353 to interpret the address correctly. It would also be nice
2354 to generate a complaint, but that would require us to maintain
2355 a list of legitimate cases where a non-address form is allowed,
2356 as well as update callers to pass in at least the CU's DWARF
2357 version. This is more overhead than what we're willing to
2358 expand for a pretty rare case. */
2359 addr = DW_UNSND (attr);
2360 }
2361 else
2362 addr = DW_ADDR (attr);
2363
2364 return addr;
2365}
2366
9291a0cd 2367/* The suffix for an index file. */
437afbb8
JK
2368#define INDEX4_SUFFIX ".gdb-index"
2369#define INDEX5_SUFFIX ".debug_names"
2370#define DEBUG_STR_SUFFIX ".debug_str"
9291a0cd 2371
330cdd98
PA
2372/* See declaration. */
2373
2374dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2375 const dwarf2_debug_sections *names)
2376 : objfile (objfile_)
2377{
2378 if (names == NULL)
2379 names = &dwarf2_elf_names;
2380
2381 bfd *obfd = objfile->obfd;
2382
2383 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2384 locate_sections (obfd, sec, *names);
2385}
2386
2387dwarf2_per_objfile::~dwarf2_per_objfile ()
2388{
2389 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2390 free_cached_comp_units ();
2391
2392 if (quick_file_names_table)
2393 htab_delete (quick_file_names_table);
2394
2395 if (line_header_hash)
2396 htab_delete (line_header_hash);
2397
2398 /* Everything else should be on the objfile obstack. */
2399}
2400
2401/* See declaration. */
2402
2403void
2404dwarf2_per_objfile::free_cached_comp_units ()
2405{
2406 dwarf2_per_cu_data *per_cu = read_in_chain;
2407 dwarf2_per_cu_data **last_chain = &read_in_chain;
2408 while (per_cu != NULL)
2409 {
2410 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2411
2412 free_heap_comp_unit (per_cu->cu);
2413 *last_chain = next_cu;
2414 per_cu = next_cu;
2415 }
2416}
2417
c906108c 2418/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2419 information and return true if we have enough to do something.
2420 NAMES points to the dwarf2 section names, or is NULL if the standard
2421 ELF names are used. */
c906108c
SS
2422
2423int
251d32d9
TG
2424dwarf2_has_info (struct objfile *objfile,
2425 const struct dwarf2_debug_sections *names)
c906108c 2426{
97cbe998
SDJ
2427 if (objfile->flags & OBJF_READNEVER)
2428 return 0;
2429
9a3c8263
SM
2430 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2431 objfile_data (objfile, dwarf2_objfile_data_key));
be391dca
TT
2432 if (!dwarf2_per_objfile)
2433 {
2434 /* Initialize per-objfile state. */
2435 struct dwarf2_per_objfile *data
8d749320 2436 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
9a619af0 2437
330cdd98
PA
2438 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2439 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
be391dca 2440 }
73869dc2 2441 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2442 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2443 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2444 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2445}
2446
2447/* Return the containing section of virtual section SECTION. */
2448
2449static struct dwarf2_section_info *
2450get_containing_section (const struct dwarf2_section_info *section)
2451{
2452 gdb_assert (section->is_virtual);
2453 return section->s.containing_section;
c906108c
SS
2454}
2455
a32a8923
DE
2456/* Return the bfd owner of SECTION. */
2457
2458static struct bfd *
2459get_section_bfd_owner (const struct dwarf2_section_info *section)
2460{
73869dc2
DE
2461 if (section->is_virtual)
2462 {
2463 section = get_containing_section (section);
2464 gdb_assert (!section->is_virtual);
2465 }
049412e3 2466 return section->s.section->owner;
a32a8923
DE
2467}
2468
2469/* Return the bfd section of SECTION.
2470 Returns NULL if the section is not present. */
2471
2472static asection *
2473get_section_bfd_section (const struct dwarf2_section_info *section)
2474{
73869dc2
DE
2475 if (section->is_virtual)
2476 {
2477 section = get_containing_section (section);
2478 gdb_assert (!section->is_virtual);
2479 }
049412e3 2480 return section->s.section;
a32a8923
DE
2481}
2482
2483/* Return the name of SECTION. */
2484
2485static const char *
2486get_section_name (const struct dwarf2_section_info *section)
2487{
2488 asection *sectp = get_section_bfd_section (section);
2489
2490 gdb_assert (sectp != NULL);
2491 return bfd_section_name (get_section_bfd_owner (section), sectp);
2492}
2493
2494/* Return the name of the file SECTION is in. */
2495
2496static const char *
2497get_section_file_name (const struct dwarf2_section_info *section)
2498{
2499 bfd *abfd = get_section_bfd_owner (section);
2500
2501 return bfd_get_filename (abfd);
2502}
2503
2504/* Return the id of SECTION.
2505 Returns 0 if SECTION doesn't exist. */
2506
2507static int
2508get_section_id (const struct dwarf2_section_info *section)
2509{
2510 asection *sectp = get_section_bfd_section (section);
2511
2512 if (sectp == NULL)
2513 return 0;
2514 return sectp->id;
2515}
2516
2517/* Return the flags of SECTION.
73869dc2 2518 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2519
2520static int
2521get_section_flags (const struct dwarf2_section_info *section)
2522{
2523 asection *sectp = get_section_bfd_section (section);
2524
2525 gdb_assert (sectp != NULL);
2526 return bfd_get_section_flags (sectp->owner, sectp);
2527}
2528
251d32d9
TG
2529/* When loading sections, we look either for uncompressed section or for
2530 compressed section names. */
233a11ab
CS
2531
2532static int
251d32d9
TG
2533section_is_p (const char *section_name,
2534 const struct dwarf2_section_names *names)
233a11ab 2535{
251d32d9
TG
2536 if (names->normal != NULL
2537 && strcmp (section_name, names->normal) == 0)
2538 return 1;
2539 if (names->compressed != NULL
2540 && strcmp (section_name, names->compressed) == 0)
2541 return 1;
2542 return 0;
233a11ab
CS
2543}
2544
330cdd98 2545/* See declaration. */
c906108c 2546
330cdd98
PA
2547void
2548dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2549 const dwarf2_debug_sections &names)
c906108c 2550{
dc7650b8 2551 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2552
dc7650b8
JK
2553 if ((aflag & SEC_HAS_CONTENTS) == 0)
2554 {
2555 }
330cdd98 2556 else if (section_is_p (sectp->name, &names.info))
c906108c 2557 {
330cdd98
PA
2558 this->info.s.section = sectp;
2559 this->info.size = bfd_get_section_size (sectp);
c906108c 2560 }
330cdd98 2561 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2562 {
330cdd98
PA
2563 this->abbrev.s.section = sectp;
2564 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2565 }
330cdd98 2566 else if (section_is_p (sectp->name, &names.line))
c906108c 2567 {
330cdd98
PA
2568 this->line.s.section = sectp;
2569 this->line.size = bfd_get_section_size (sectp);
c906108c 2570 }
330cdd98 2571 else if (section_is_p (sectp->name, &names.loc))
c906108c 2572 {
330cdd98
PA
2573 this->loc.s.section = sectp;
2574 this->loc.size = bfd_get_section_size (sectp);
c906108c 2575 }
330cdd98 2576 else if (section_is_p (sectp->name, &names.loclists))
43988095 2577 {
330cdd98
PA
2578 this->loclists.s.section = sectp;
2579 this->loclists.size = bfd_get_section_size (sectp);
43988095 2580 }
330cdd98 2581 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2582 {
330cdd98
PA
2583 this->macinfo.s.section = sectp;
2584 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2585 }
330cdd98 2586 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2587 {
330cdd98
PA
2588 this->macro.s.section = sectp;
2589 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2590 }
330cdd98 2591 else if (section_is_p (sectp->name, &names.str))
c906108c 2592 {
330cdd98
PA
2593 this->str.s.section = sectp;
2594 this->str.size = bfd_get_section_size (sectp);
c906108c 2595 }
330cdd98 2596 else if (section_is_p (sectp->name, &names.line_str))
43988095 2597 {
330cdd98
PA
2598 this->line_str.s.section = sectp;
2599 this->line_str.size = bfd_get_section_size (sectp);
43988095 2600 }
330cdd98 2601 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2602 {
330cdd98
PA
2603 this->addr.s.section = sectp;
2604 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2605 }
330cdd98 2606 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2607 {
330cdd98
PA
2608 this->frame.s.section = sectp;
2609 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2610 }
330cdd98 2611 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2612 {
330cdd98
PA
2613 this->eh_frame.s.section = sectp;
2614 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2615 }
330cdd98 2616 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2617 {
330cdd98
PA
2618 this->ranges.s.section = sectp;
2619 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2620 }
330cdd98 2621 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2622 {
330cdd98
PA
2623 this->rnglists.s.section = sectp;
2624 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2625 }
330cdd98 2626 else if (section_is_p (sectp->name, &names.types))
348e048f 2627 {
8b70b953
TT
2628 struct dwarf2_section_info type_section;
2629
2630 memset (&type_section, 0, sizeof (type_section));
049412e3 2631 type_section.s.section = sectp;
8b70b953
TT
2632 type_section.size = bfd_get_section_size (sectp);
2633
330cdd98 2634 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2635 &type_section);
348e048f 2636 }
330cdd98 2637 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2638 {
330cdd98
PA
2639 this->gdb_index.s.section = sectp;
2640 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2641 }
927aa2e7
JK
2642 else if (section_is_p (sectp->name, &names.debug_names))
2643 {
2644 this->debug_names.s.section = sectp;
2645 this->debug_names.size = bfd_get_section_size (sectp);
2646 }
2647 else if (section_is_p (sectp->name, &names.debug_aranges))
2648 {
2649 this->debug_aranges.s.section = sectp;
2650 this->debug_aranges.size = bfd_get_section_size (sectp);
2651 }
dce234bc 2652
b4e1fd61 2653 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2654 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2655 this->has_section_at_zero = true;
c906108c
SS
2656}
2657
fceca515
DE
2658/* A helper function that decides whether a section is empty,
2659 or not present. */
9e0ac564
TT
2660
2661static int
19ac8c2e 2662dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2663{
73869dc2
DE
2664 if (section->is_virtual)
2665 return section->size == 0;
049412e3 2666 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2667}
2668
3019eac3
DE
2669/* Read the contents of the section INFO.
2670 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2671 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2672 of the DWO file.
dce234bc 2673 If the section is compressed, uncompress it before returning. */
c906108c 2674
dce234bc
PP
2675static void
2676dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2677{
a32a8923 2678 asection *sectp;
3019eac3 2679 bfd *abfd;
dce234bc 2680 gdb_byte *buf, *retbuf;
c906108c 2681
be391dca
TT
2682 if (info->readin)
2683 return;
dce234bc 2684 info->buffer = NULL;
be391dca 2685 info->readin = 1;
188dd5d6 2686
9e0ac564 2687 if (dwarf2_section_empty_p (info))
dce234bc 2688 return;
c906108c 2689
a32a8923 2690 sectp = get_section_bfd_section (info);
3019eac3 2691
73869dc2
DE
2692 /* If this is a virtual section we need to read in the real one first. */
2693 if (info->is_virtual)
2694 {
2695 struct dwarf2_section_info *containing_section =
2696 get_containing_section (info);
2697
2698 gdb_assert (sectp != NULL);
2699 if ((sectp->flags & SEC_RELOC) != 0)
2700 {
2701 error (_("Dwarf Error: DWP format V2 with relocations is not"
2702 " supported in section %s [in module %s]"),
2703 get_section_name (info), get_section_file_name (info));
2704 }
2705 dwarf2_read_section (objfile, containing_section);
2706 /* Other code should have already caught virtual sections that don't
2707 fit. */
2708 gdb_assert (info->virtual_offset + info->size
2709 <= containing_section->size);
2710 /* If the real section is empty or there was a problem reading the
2711 section we shouldn't get here. */
2712 gdb_assert (containing_section->buffer != NULL);
2713 info->buffer = containing_section->buffer + info->virtual_offset;
2714 return;
2715 }
2716
4bf44c1c
TT
2717 /* If the section has relocations, we must read it ourselves.
2718 Otherwise we attach it to the BFD. */
2719 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2720 {
d521ce57 2721 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2722 return;
dce234bc 2723 }
dce234bc 2724
224c3ddb 2725 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2726 info->buffer = buf;
dce234bc
PP
2727
2728 /* When debugging .o files, we may need to apply relocations; see
2729 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2730 We never compress sections in .o files, so we only need to
2731 try this when the section is not compressed. */
ac8035ab 2732 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2733 if (retbuf != NULL)
2734 {
2735 info->buffer = retbuf;
2736 return;
2737 }
2738
a32a8923
DE
2739 abfd = get_section_bfd_owner (info);
2740 gdb_assert (abfd != NULL);
2741
dce234bc
PP
2742 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2743 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2744 {
2745 error (_("Dwarf Error: Can't read DWARF data"
2746 " in section %s [in module %s]"),
2747 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2748 }
dce234bc
PP
2749}
2750
9e0ac564
TT
2751/* A helper function that returns the size of a section in a safe way.
2752 If you are positive that the section has been read before using the
2753 size, then it is safe to refer to the dwarf2_section_info object's
2754 "size" field directly. In other cases, you must call this
2755 function, because for compressed sections the size field is not set
2756 correctly until the section has been read. */
2757
2758static bfd_size_type
2759dwarf2_section_size (struct objfile *objfile,
2760 struct dwarf2_section_info *info)
2761{
2762 if (!info->readin)
2763 dwarf2_read_section (objfile, info);
2764 return info->size;
2765}
2766
dce234bc 2767/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2768 SECTION_NAME. */
af34e669 2769
dce234bc 2770void
3017a003
TG
2771dwarf2_get_section_info (struct objfile *objfile,
2772 enum dwarf2_section_enum sect,
d521ce57 2773 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2774 bfd_size_type *sizep)
2775{
2776 struct dwarf2_per_objfile *data
9a3c8263
SM
2777 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2778 dwarf2_objfile_data_key);
dce234bc 2779 struct dwarf2_section_info *info;
a3b2a86b
TT
2780
2781 /* We may see an objfile without any DWARF, in which case we just
2782 return nothing. */
2783 if (data == NULL)
2784 {
2785 *sectp = NULL;
2786 *bufp = NULL;
2787 *sizep = 0;
2788 return;
2789 }
3017a003
TG
2790 switch (sect)
2791 {
2792 case DWARF2_DEBUG_FRAME:
2793 info = &data->frame;
2794 break;
2795 case DWARF2_EH_FRAME:
2796 info = &data->eh_frame;
2797 break;
2798 default:
2799 gdb_assert_not_reached ("unexpected section");
2800 }
dce234bc 2801
9e0ac564 2802 dwarf2_read_section (objfile, info);
dce234bc 2803
a32a8923 2804 *sectp = get_section_bfd_section (info);
dce234bc
PP
2805 *bufp = info->buffer;
2806 *sizep = info->size;
2807}
2808
36586728
TT
2809/* A helper function to find the sections for a .dwz file. */
2810
2811static void
2812locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2813{
9a3c8263 2814 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2815
2816 /* Note that we only support the standard ELF names, because .dwz
2817 is ELF-only (at the time of writing). */
2818 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2819 {
049412e3 2820 dwz_file->abbrev.s.section = sectp;
36586728
TT
2821 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2822 }
2823 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2824 {
049412e3 2825 dwz_file->info.s.section = sectp;
36586728
TT
2826 dwz_file->info.size = bfd_get_section_size (sectp);
2827 }
2828 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2829 {
049412e3 2830 dwz_file->str.s.section = sectp;
36586728
TT
2831 dwz_file->str.size = bfd_get_section_size (sectp);
2832 }
2833 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2834 {
049412e3 2835 dwz_file->line.s.section = sectp;
36586728
TT
2836 dwz_file->line.size = bfd_get_section_size (sectp);
2837 }
2838 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2839 {
049412e3 2840 dwz_file->macro.s.section = sectp;
36586728
TT
2841 dwz_file->macro.size = bfd_get_section_size (sectp);
2842 }
2ec9a5e0
TT
2843 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2844 {
049412e3 2845 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2846 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2847 }
927aa2e7
JK
2848 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2849 {
2850 dwz_file->debug_names.s.section = sectp;
2851 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2852 }
36586728
TT
2853}
2854
4db1a1dc
TT
2855/* Open the separate '.dwz' debug file, if needed. Return NULL if
2856 there is no .gnu_debugaltlink section in the file. Error if there
2857 is such a section but the file cannot be found. */
36586728
TT
2858
2859static struct dwz_file *
2860dwarf2_get_dwz_file (void)
2861{
36586728
TT
2862 const char *filename;
2863 struct dwz_file *result;
acd13123 2864 bfd_size_type buildid_len_arg;
dc294be5
TT
2865 size_t buildid_len;
2866 bfd_byte *buildid;
36586728
TT
2867
2868 if (dwarf2_per_objfile->dwz_file != NULL)
2869 return dwarf2_per_objfile->dwz_file;
2870
4db1a1dc 2871 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2872 gdb::unique_xmalloc_ptr<char> data
2873 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2874 &buildid_len_arg, &buildid));
4db1a1dc
TT
2875 if (data == NULL)
2876 {
2877 if (bfd_get_error () == bfd_error_no_error)
2878 return NULL;
2879 error (_("could not read '.gnu_debugaltlink' section: %s"),
2880 bfd_errmsg (bfd_get_error ()));
2881 }
791afaa2
TT
2882
2883 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2884
acd13123
TT
2885 buildid_len = (size_t) buildid_len_arg;
2886
791afaa2 2887 filename = data.get ();
d721ba37
PA
2888
2889 std::string abs_storage;
36586728
TT
2890 if (!IS_ABSOLUTE_PATH (filename))
2891 {
14278e1f
TT
2892 gdb::unique_xmalloc_ptr<char> abs
2893 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2894
14278e1f 2895 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2896 filename = abs_storage.c_str ();
36586728
TT
2897 }
2898
dc294be5
TT
2899 /* First try the file name given in the section. If that doesn't
2900 work, try to use the build-id instead. */
192b62ce 2901 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2902 if (dwz_bfd != NULL)
36586728 2903 {
192b62ce
TT
2904 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2905 dwz_bfd.release ();
36586728
TT
2906 }
2907
dc294be5
TT
2908 if (dwz_bfd == NULL)
2909 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2910
2911 if (dwz_bfd == NULL)
2912 error (_("could not find '.gnu_debugaltlink' file for %s"),
2913 objfile_name (dwarf2_per_objfile->objfile));
2914
36586728
TT
2915 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2916 struct dwz_file);
192b62ce 2917 result->dwz_bfd = dwz_bfd.release ();
36586728 2918
192b62ce 2919 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728 2920
192b62ce 2921 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2922 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2923 return result;
2924}
9291a0cd 2925\f
7b9f3c50
DE
2926/* DWARF quick_symbols_functions support. */
2927
2928/* TUs can share .debug_line entries, and there can be a lot more TUs than
2929 unique line tables, so we maintain a separate table of all .debug_line
2930 derived entries to support the sharing.
2931 All the quick functions need is the list of file names. We discard the
2932 line_header when we're done and don't need to record it here. */
2933struct quick_file_names
2934{
094b34ac
DE
2935 /* The data used to construct the hash key. */
2936 struct stmt_list_hash hash;
7b9f3c50
DE
2937
2938 /* The number of entries in file_names, real_names. */
2939 unsigned int num_file_names;
2940
2941 /* The file names from the line table, after being run through
2942 file_full_name. */
2943 const char **file_names;
2944
2945 /* The file names from the line table after being run through
2946 gdb_realpath. These are computed lazily. */
2947 const char **real_names;
2948};
2949
2950/* When using the index (and thus not using psymtabs), each CU has an
2951 object of this type. This is used to hold information needed by
2952 the various "quick" methods. */
2953struct dwarf2_per_cu_quick_data
2954{
2955 /* The file table. This can be NULL if there was no file table
2956 or it's currently not read in.
2957 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2958 struct quick_file_names *file_names;
2959
2960 /* The corresponding symbol table. This is NULL if symbols for this
2961 CU have not yet been read. */
43f3e411 2962 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2963
2964 /* A temporary mark bit used when iterating over all CUs in
2965 expand_symtabs_matching. */
2966 unsigned int mark : 1;
2967
2968 /* True if we've tried to read the file table and found there isn't one.
2969 There will be no point in trying to read it again next time. */
2970 unsigned int no_file_data : 1;
2971};
2972
094b34ac
DE
2973/* Utility hash function for a stmt_list_hash. */
2974
2975static hashval_t
2976hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2977{
2978 hashval_t v = 0;
2979
2980 if (stmt_list_hash->dwo_unit != NULL)
2981 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2982 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2983 return v;
2984}
2985
2986/* Utility equality function for a stmt_list_hash. */
2987
2988static int
2989eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2990 const struct stmt_list_hash *rhs)
2991{
2992 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2993 return 0;
2994 if (lhs->dwo_unit != NULL
2995 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2996 return 0;
2997
9c541725 2998 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2999}
3000
7b9f3c50
DE
3001/* Hash function for a quick_file_names. */
3002
3003static hashval_t
3004hash_file_name_entry (const void *e)
3005{
9a3c8263
SM
3006 const struct quick_file_names *file_data
3007 = (const struct quick_file_names *) e;
7b9f3c50 3008
094b34ac 3009 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
3010}
3011
3012/* Equality function for a quick_file_names. */
3013
3014static int
3015eq_file_name_entry (const void *a, const void *b)
3016{
9a3c8263
SM
3017 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3018 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 3019
094b34ac 3020 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
3021}
3022
3023/* Delete function for a quick_file_names. */
3024
3025static void
3026delete_file_name_entry (void *e)
3027{
9a3c8263 3028 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
3029 int i;
3030
3031 for (i = 0; i < file_data->num_file_names; ++i)
3032 {
3033 xfree ((void*) file_data->file_names[i]);
3034 if (file_data->real_names)
3035 xfree ((void*) file_data->real_names[i]);
3036 }
3037
3038 /* The space for the struct itself lives on objfile_obstack,
3039 so we don't free it here. */
3040}
3041
3042/* Create a quick_file_names hash table. */
3043
3044static htab_t
3045create_quick_file_names_table (unsigned int nr_initial_entries)
3046{
3047 return htab_create_alloc (nr_initial_entries,
3048 hash_file_name_entry, eq_file_name_entry,
3049 delete_file_name_entry, xcalloc, xfree);
3050}
9291a0cd 3051
918dd910
JK
3052/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3053 have to be created afterwards. You should call age_cached_comp_units after
3054 processing PER_CU->CU. dw2_setup must have been already called. */
3055
3056static void
3057load_cu (struct dwarf2_per_cu_data *per_cu)
3058{
3019eac3 3059 if (per_cu->is_debug_types)
e5fe5e75 3060 load_full_type_unit (per_cu);
918dd910 3061 else
95554aad 3062 load_full_comp_unit (per_cu, language_minimal);
918dd910 3063
cc12ce38
DE
3064 if (per_cu->cu == NULL)
3065 return; /* Dummy CU. */
2dc860c0
DE
3066
3067 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
3068}
3069
a0f42c21 3070/* Read in the symbols for PER_CU. */
2fdf6df6 3071
9291a0cd 3072static void
a0f42c21 3073dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
3074{
3075 struct cleanup *back_to;
3076
f4dc4d17
DE
3077 /* Skip type_unit_groups, reading the type units they contain
3078 is handled elsewhere. */
3079 if (IS_TYPE_UNIT_GROUP (per_cu))
3080 return;
3081
9291a0cd
TT
3082 back_to = make_cleanup (dwarf2_release_queue, NULL);
3083
95554aad 3084 if (dwarf2_per_objfile->using_index
43f3e411 3085 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
3086 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3087 {
3088 queue_comp_unit (per_cu, language_minimal);
3089 load_cu (per_cu);
89e63ee4
DE
3090
3091 /* If we just loaded a CU from a DWO, and we're working with an index
3092 that may badly handle TUs, load all the TUs in that DWO as well.
3093 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3094 if (!per_cu->is_debug_types
cc12ce38 3095 && per_cu->cu != NULL
89e63ee4
DE
3096 && per_cu->cu->dwo_unit != NULL
3097 && dwarf2_per_objfile->index_table != NULL
3098 && dwarf2_per_objfile->index_table->version <= 7
3099 /* DWP files aren't supported yet. */
3100 && get_dwp_file () == NULL)
3101 queue_and_load_all_dwo_tus (per_cu);
95554aad 3102 }
9291a0cd 3103
a0f42c21 3104 process_queue ();
9291a0cd
TT
3105
3106 /* Age the cache, releasing compilation units that have not
3107 been used recently. */
3108 age_cached_comp_units ();
3109
3110 do_cleanups (back_to);
3111}
3112
3113/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3114 the objfile from which this CU came. Returns the resulting symbol
3115 table. */
2fdf6df6 3116
43f3e411 3117static struct compunit_symtab *
a0f42c21 3118dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 3119{
95554aad 3120 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 3121 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3122 {
3123 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
c83dd867 3124 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 3125 dw2_do_instantiate_symtab (per_cu);
95554aad 3126 process_cu_includes ();
9291a0cd
TT
3127 do_cleanups (back_to);
3128 }
f194fefb 3129
43f3e411 3130 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
3131}
3132
8832e7e3 3133/* Return the CU/TU given its index.
f4dc4d17
DE
3134
3135 This is intended for loops like:
3136
3137 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3138 + dwarf2_per_objfile->n_type_units); ++i)
3139 {
8832e7e3 3140 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
3141
3142 ...;
3143 }
3144*/
2fdf6df6 3145
1fd400ff 3146static struct dwarf2_per_cu_data *
8832e7e3 3147dw2_get_cutu (int index)
1fd400ff
TT
3148{
3149 if (index >= dwarf2_per_objfile->n_comp_units)
3150 {
f4dc4d17 3151 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
3152 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3153 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
3154 }
3155
3156 return dwarf2_per_objfile->all_comp_units[index];
3157}
3158
8832e7e3
DE
3159/* Return the CU given its index.
3160 This differs from dw2_get_cutu in that it's for when you know INDEX
3161 refers to a CU. */
f4dc4d17
DE
3162
3163static struct dwarf2_per_cu_data *
8832e7e3 3164dw2_get_cu (int index)
f4dc4d17 3165{
8832e7e3 3166 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 3167
1fd400ff
TT
3168 return dwarf2_per_objfile->all_comp_units[index];
3169}
3170
4b514bc8
JK
3171/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3172 objfile_obstack, and constructed with the specified field
3173 values. */
3174
3175static dwarf2_per_cu_data *
3176create_cu_from_index_list (struct objfile *objfile,
3177 struct dwarf2_section_info *section,
3178 int is_dwz,
3179 sect_offset sect_off, ULONGEST length)
3180{
3181 dwarf2_per_cu_data *the_cu
3182 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3183 struct dwarf2_per_cu_data);
3184 the_cu->sect_off = sect_off;
3185 the_cu->length = length;
3186 the_cu->objfile = objfile;
3187 the_cu->section = section;
3188 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3189 struct dwarf2_per_cu_quick_data);
3190 the_cu->is_dwz = is_dwz;
3191 return the_cu;
3192}
3193
2ec9a5e0
TT
3194/* A helper for create_cus_from_index that handles a given list of
3195 CUs. */
2fdf6df6 3196
74a0d9f6 3197static void
2ec9a5e0
TT
3198create_cus_from_index_list (struct objfile *objfile,
3199 const gdb_byte *cu_list, offset_type n_elements,
3200 struct dwarf2_section_info *section,
3201 int is_dwz,
3202 int base_offset)
9291a0cd
TT
3203{
3204 offset_type i;
9291a0cd 3205
2ec9a5e0 3206 for (i = 0; i < n_elements; i += 2)
9291a0cd 3207 {
74a0d9f6 3208 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3209
3210 sect_offset sect_off
3211 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3212 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3213 cu_list += 2 * 8;
3214
4b514bc8
JK
3215 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3216 = create_cu_from_index_list (objfile, section, is_dwz, sect_off, length);
9291a0cd 3217 }
9291a0cd
TT
3218}
3219
2ec9a5e0 3220/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3221 the CU objects for this objfile. */
2ec9a5e0 3222
74a0d9f6 3223static void
2ec9a5e0
TT
3224create_cus_from_index (struct objfile *objfile,
3225 const gdb_byte *cu_list, offset_type cu_list_elements,
3226 const gdb_byte *dwz_list, offset_type dwz_elements)
3227{
3228 struct dwz_file *dwz;
3229
3230 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
3231 dwarf2_per_objfile->all_comp_units =
3232 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3233 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 3234
74a0d9f6
JK
3235 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3236 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
3237
3238 if (dwz_elements == 0)
74a0d9f6 3239 return;
2ec9a5e0
TT
3240
3241 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
3242 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3243 cu_list_elements / 2);
2ec9a5e0
TT
3244}
3245
1fd400ff 3246/* Create the signatured type hash table from the index. */
673bfd45 3247
74a0d9f6 3248static void
673bfd45 3249create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3250 struct dwarf2_section_info *section,
673bfd45
DE
3251 const gdb_byte *bytes,
3252 offset_type elements)
1fd400ff
TT
3253{
3254 offset_type i;
673bfd45 3255 htab_t sig_types_hash;
1fd400ff 3256
6aa5f3a6
DE
3257 dwarf2_per_objfile->n_type_units
3258 = dwarf2_per_objfile->n_allocated_type_units
3259 = elements / 3;
8d749320
SM
3260 dwarf2_per_objfile->all_type_units =
3261 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3262
673bfd45 3263 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3264
3265 for (i = 0; i < elements; i += 3)
3266 {
52dc124a 3267 struct signatured_type *sig_type;
9c541725 3268 ULONGEST signature;
1fd400ff 3269 void **slot;
9c541725 3270 cu_offset type_offset_in_tu;
1fd400ff 3271
74a0d9f6 3272 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3273 sect_offset sect_off
3274 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3275 type_offset_in_tu
3276 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3277 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3278 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3279 bytes += 3 * 8;
3280
52dc124a 3281 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3282 struct signatured_type);
52dc124a 3283 sig_type->signature = signature;
9c541725 3284 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3285 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3286 sig_type->per_cu.section = section;
9c541725 3287 sig_type->per_cu.sect_off = sect_off;
52dc124a
DE
3288 sig_type->per_cu.objfile = objfile;
3289 sig_type->per_cu.v.quick
1fd400ff
TT
3290 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3291 struct dwarf2_per_cu_quick_data);
3292
52dc124a
DE
3293 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3294 *slot = sig_type;
1fd400ff 3295
b4dd5633 3296 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3297 }
3298
673bfd45 3299 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3300}
3301
927aa2e7
JK
3302/* Create the signatured type hash table from .debug_names. */
3303
3304static void
3305create_signatured_type_table_from_debug_names
3306 (struct objfile *objfile,
3307 const mapped_debug_names &map,
3308 struct dwarf2_section_info *section,
3309 struct dwarf2_section_info *abbrev_section)
3310{
3311 dwarf2_read_section (objfile, section);
3312 dwarf2_read_section (objfile, abbrev_section);
3313
3314 dwarf2_per_objfile->n_type_units
3315 = dwarf2_per_objfile->n_allocated_type_units
3316 = map.tu_count;
3317 dwarf2_per_objfile->all_type_units
3318 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3319
3320 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3321
3322 for (uint32_t i = 0; i < map.tu_count; ++i)
3323 {
3324 struct signatured_type *sig_type;
3325 ULONGEST signature;
3326 void **slot;
3327 cu_offset type_offset_in_tu;
3328
3329 sect_offset sect_off
3330 = (sect_offset) (extract_unsigned_integer
3331 (map.tu_table_reordered + i * map.offset_size,
3332 map.offset_size,
3333 map.dwarf5_byte_order));
3334
3335 comp_unit_head cu_header;
3336 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
3337 section->buffer + to_underlying (sect_off),
3338 rcuh_kind::TYPE);
3339
3340 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3341 struct signatured_type);
3342 sig_type->signature = cu_header.signature;
3343 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3344 sig_type->per_cu.is_debug_types = 1;
3345 sig_type->per_cu.section = section;
3346 sig_type->per_cu.sect_off = sect_off;
3347 sig_type->per_cu.objfile = objfile;
3348 sig_type->per_cu.v.quick
3349 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3350 struct dwarf2_per_cu_quick_data);
3351
3352 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3353 *slot = sig_type;
3354
3355 dwarf2_per_objfile->all_type_units[i] = sig_type;
3356 }
3357
3358 dwarf2_per_objfile->signatured_types = sig_types_hash;
3359}
3360
9291a0cd
TT
3361/* Read the address map data from the mapped index, and use it to
3362 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3363
9291a0cd
TT
3364static void
3365create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3366{
3e29f34a 3367 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3368 const gdb_byte *iter, *end;
9291a0cd 3369 struct addrmap *mutable_map;
9291a0cd
TT
3370 CORE_ADDR baseaddr;
3371
8268c778
PA
3372 auto_obstack temp_obstack;
3373
9291a0cd
TT
3374 mutable_map = addrmap_create_mutable (&temp_obstack);
3375
f00a2de2
PA
3376 iter = index->address_table.data ();
3377 end = iter + index->address_table.size ();
9291a0cd
TT
3378
3379 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3380
3381 while (iter < end)
3382 {
3383 ULONGEST hi, lo, cu_index;
3384 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3385 iter += 8;
3386 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3387 iter += 8;
3388 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3389 iter += 4;
f652bce2 3390
24a55014 3391 if (lo > hi)
f652bce2 3392 {
24a55014
DE
3393 complaint (&symfile_complaints,
3394 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3395 hex_string (lo), hex_string (hi));
24a55014 3396 continue;
f652bce2 3397 }
24a55014
DE
3398
3399 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3400 {
3401 complaint (&symfile_complaints,
3402 _(".gdb_index address table has invalid CU number %u"),
3403 (unsigned) cu_index);
24a55014 3404 continue;
f652bce2 3405 }
24a55014 3406
3e29f34a
MR
3407 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3408 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3409 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
9291a0cd
TT
3410 }
3411
3412 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3413 &objfile->objfile_obstack);
9291a0cd
TT
3414}
3415
927aa2e7
JK
3416/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3417 populate the objfile's psymtabs_addrmap. */
3418
3419static void
3420create_addrmap_from_aranges (struct objfile *objfile,
3421 struct dwarf2_section_info *section)
3422{
3423 bfd *abfd = objfile->obfd;
3424 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3425 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3426 SECT_OFF_TEXT (objfile));
3427
3428 auto_obstack temp_obstack;
3429 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3430
3431 std::unordered_map<sect_offset,
3432 dwarf2_per_cu_data *,
3433 gdb::hash_enum<sect_offset>>
3434 debug_info_offset_to_per_cu;
3435 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3436 {
3437 dwarf2_per_cu_data *per_cu = dw2_get_cutu (cui);
3438 const auto insertpair
3439 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3440 if (!insertpair.second)
3441 {
3442 warning (_("Section .debug_aranges in %s has duplicate "
3443 "debug_info_offset %u, ignoring .debug_aranges."),
3444 objfile_name (objfile), to_underlying (per_cu->sect_off));
3445 return;
3446 }
3447 }
3448
3449 dwarf2_read_section (objfile, section);
3450
3451 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3452
3453 const gdb_byte *addr = section->buffer;
3454
3455 while (addr < section->buffer + section->size)
3456 {
3457 const gdb_byte *const entry_addr = addr;
3458 unsigned int bytes_read;
3459
3460 const LONGEST entry_length = read_initial_length (abfd, addr,
3461 &bytes_read);
3462 addr += bytes_read;
3463
3464 const gdb_byte *const entry_end = addr + entry_length;
3465 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3466 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3467 if (addr + entry_length > section->buffer + section->size)
3468 {
3469 warning (_("Section .debug_aranges in %s entry at offset %zu "
3470 "length %s exceeds section length %s, "
3471 "ignoring .debug_aranges."),
3472 objfile_name (objfile), entry_addr - section->buffer,
3473 plongest (bytes_read + entry_length),
3474 pulongest (section->size));
3475 return;
3476 }
3477
3478 /* The version number. */
3479 const uint16_t version = read_2_bytes (abfd, addr);
3480 addr += 2;
3481 if (version != 2)
3482 {
3483 warning (_("Section .debug_aranges in %s entry at offset %zu "
3484 "has unsupported version %d, ignoring .debug_aranges."),
3485 objfile_name (objfile), entry_addr - section->buffer,
3486 version);
3487 return;
3488 }
3489
3490 const uint64_t debug_info_offset
3491 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3492 addr += offset_size;
3493 const auto per_cu_it
3494 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3495 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3496 {
3497 warning (_("Section .debug_aranges in %s entry at offset %zu "
3498 "debug_info_offset %s does not exists, "
3499 "ignoring .debug_aranges."),
3500 objfile_name (objfile), entry_addr - section->buffer,
3501 pulongest (debug_info_offset));
3502 return;
3503 }
3504 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3505
3506 const uint8_t address_size = *addr++;
3507 if (address_size < 1 || address_size > 8)
3508 {
3509 warning (_("Section .debug_aranges in %s entry at offset %zu "
3510 "address_size %u is invalid, ignoring .debug_aranges."),
3511 objfile_name (objfile), entry_addr - section->buffer,
3512 address_size);
3513 return;
3514 }
3515
3516 const uint8_t segment_selector_size = *addr++;
3517 if (segment_selector_size != 0)
3518 {
3519 warning (_("Section .debug_aranges in %s entry at offset %zu "
3520 "segment_selector_size %u is not supported, "
3521 "ignoring .debug_aranges."),
3522 objfile_name (objfile), entry_addr - section->buffer,
3523 segment_selector_size);
3524 return;
3525 }
3526
3527 /* Must pad to an alignment boundary that is twice the address
3528 size. It is undocumented by the DWARF standard but GCC does
3529 use it. */
3530 for (size_t padding = ((-(addr - section->buffer))
3531 & (2 * address_size - 1));
3532 padding > 0; padding--)
3533 if (*addr++ != 0)
3534 {
3535 warning (_("Section .debug_aranges in %s entry at offset %zu "
3536 "padding is not zero, ignoring .debug_aranges."),
3537 objfile_name (objfile), entry_addr - section->buffer);
3538 return;
3539 }
3540
3541 for (;;)
3542 {
3543 if (addr + 2 * address_size > entry_end)
3544 {
3545 warning (_("Section .debug_aranges in %s entry at offset %zu "
3546 "address list is not properly terminated, "
3547 "ignoring .debug_aranges."),
3548 objfile_name (objfile), entry_addr - section->buffer);
3549 return;
3550 }
3551 ULONGEST start = extract_unsigned_integer (addr, address_size,
3552 dwarf5_byte_order);
3553 addr += address_size;
3554 ULONGEST length = extract_unsigned_integer (addr, address_size,
3555 dwarf5_byte_order);
3556 addr += address_size;
3557 if (start == 0 && length == 0)
3558 break;
3559 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3560 {
3561 /* Symbol was eliminated due to a COMDAT group. */
3562 continue;
3563 }
3564 ULONGEST end = start + length;
3565 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3566 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3567 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3568 }
3569 }
3570
3571 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3572 &objfile->objfile_obstack);
3573}
3574
59d7bcaf
JK
3575/* The hash function for strings in the mapped index. This is the same as
3576 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3577 implementation. This is necessary because the hash function is tied to the
3578 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3579 SYMBOL_HASH_NEXT.
3580
3581 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3582
9291a0cd 3583static hashval_t
559a7a62 3584mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3585{
3586 const unsigned char *str = (const unsigned char *) p;
3587 hashval_t r = 0;
3588 unsigned char c;
3589
3590 while ((c = *str++) != 0)
559a7a62
JK
3591 {
3592 if (index_version >= 5)
3593 c = tolower (c);
3594 r = r * 67 + c - 113;
3595 }
9291a0cd
TT
3596
3597 return r;
3598}
3599
3600/* Find a slot in the mapped index INDEX for the object named NAME.
3601 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3602 constant pool and return true. If NAME cannot be found, return
3603 false. */
2fdf6df6 3604
109483d9 3605static bool
9291a0cd
TT
3606find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3607 offset_type **vec_out)
3608{
0cf03b49 3609 offset_type hash;
9291a0cd 3610 offset_type slot, step;
559a7a62 3611 int (*cmp) (const char *, const char *);
9291a0cd 3612
791afaa2 3613 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3614 if (current_language->la_language == language_cplus
45280282
IB
3615 || current_language->la_language == language_fortran
3616 || current_language->la_language == language_d)
0cf03b49
JK
3617 {
3618 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3619 not contain any. */
a8719064 3620
72998fb3 3621 if (strchr (name, '(') != NULL)
0cf03b49 3622 {
109483d9 3623 without_params = cp_remove_params (name);
0cf03b49 3624
72998fb3 3625 if (without_params != NULL)
791afaa2 3626 name = without_params.get ();
0cf03b49
JK
3627 }
3628 }
3629
559a7a62 3630 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3631 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3632 simulate our NAME being searched is also lowercased. */
3633 hash = mapped_index_string_hash ((index->version == 4
3634 && case_sensitivity == case_sensitive_off
3635 ? 5 : index->version),
3636 name);
3637
f00a2de2
PA
3638 slot = hash & (index->symbol_table.size () - 1);
3639 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3640 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3641
3642 for (;;)
3643 {
9291a0cd 3644 const char *str;
f00a2de2
PA
3645
3646 const auto &bucket = index->symbol_table[slot];
3647 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3648 return false;
9291a0cd 3649
f00a2de2 3650 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3651 if (!cmp (name, str))
9291a0cd
TT
3652 {
3653 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3654 + MAYBE_SWAP (bucket.vec));
109483d9 3655 return true;
9291a0cd
TT
3656 }
3657
f00a2de2 3658 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3659 }
3660}
3661
2ec9a5e0
TT
3662/* A helper function that reads the .gdb_index from SECTION and fills
3663 in MAP. FILENAME is the name of the file containing the section;
3664 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3665 ok to use deprecated sections.
3666
3667 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3668 out parameters that are filled in with information about the CU and
3669 TU lists in the section.
3670
3671 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3672
9291a0cd 3673static int
2ec9a5e0
TT
3674read_index_from_section (struct objfile *objfile,
3675 const char *filename,
3676 int deprecated_ok,
3677 struct dwarf2_section_info *section,
3678 struct mapped_index *map,
3679 const gdb_byte **cu_list,
3680 offset_type *cu_list_elements,
3681 const gdb_byte **types_list,
3682 offset_type *types_list_elements)
9291a0cd 3683{
948f8e3d 3684 const gdb_byte *addr;
2ec9a5e0 3685 offset_type version;
b3b272e1 3686 offset_type *metadata;
1fd400ff 3687 int i;
9291a0cd 3688
2ec9a5e0 3689 if (dwarf2_section_empty_p (section))
9291a0cd 3690 return 0;
82430852
JK
3691
3692 /* Older elfutils strip versions could keep the section in the main
3693 executable while splitting it for the separate debug info file. */
a32a8923 3694 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3695 return 0;
3696
2ec9a5e0 3697 dwarf2_read_section (objfile, section);
9291a0cd 3698
2ec9a5e0 3699 addr = section->buffer;
9291a0cd 3700 /* Version check. */
1fd400ff 3701 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3702 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3703 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3704 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3705 indices. */
831adc1f 3706 if (version < 4)
481860b3
GB
3707 {
3708 static int warning_printed = 0;
3709 if (!warning_printed)
3710 {
3711 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3712 filename);
481860b3
GB
3713 warning_printed = 1;
3714 }
3715 return 0;
3716 }
3717 /* Index version 4 uses a different hash function than index version
3718 5 and later.
3719
3720 Versions earlier than 6 did not emit psymbols for inlined
3721 functions. Using these files will cause GDB not to be able to
3722 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3723 indices unless the user has done
3724 "set use-deprecated-index-sections on". */
2ec9a5e0 3725 if (version < 6 && !deprecated_ok)
481860b3
GB
3726 {
3727 static int warning_printed = 0;
3728 if (!warning_printed)
3729 {
e615022a
DE
3730 warning (_("\
3731Skipping deprecated .gdb_index section in %s.\n\
3732Do \"set use-deprecated-index-sections on\" before the file is read\n\
3733to use the section anyway."),
2ec9a5e0 3734 filename);
481860b3
GB
3735 warning_printed = 1;
3736 }
3737 return 0;
3738 }
796a7ff8 3739 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3740 of the TU (for symbols coming from TUs),
3741 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3742 Plus gold-generated indices can have duplicate entries for global symbols,
3743 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3744 These are just performance bugs, and we can't distinguish gdb-generated
3745 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3746
481860b3 3747 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3748 longer backward compatible. */
796a7ff8 3749 if (version > 8)
594e8718 3750 return 0;
9291a0cd 3751
559a7a62 3752 map->version = version;
2ec9a5e0 3753 map->total_size = section->size;
9291a0cd
TT
3754
3755 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3756
3757 i = 0;
2ec9a5e0
TT
3758 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3759 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3760 / 8);
1fd400ff
TT
3761 ++i;
3762
2ec9a5e0
TT
3763 *types_list = addr + MAYBE_SWAP (metadata[i]);
3764 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3765 - MAYBE_SWAP (metadata[i]))
3766 / 8);
987d643c 3767 ++i;
1fd400ff 3768
f00a2de2
PA
3769 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3770 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3771 map->address_table
3772 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3773 ++i;
3774
f00a2de2
PA
3775 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3776 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3777 map->symbol_table
3778 = gdb::array_view<mapped_index::symbol_table_slot>
3779 ((mapped_index::symbol_table_slot *) symbol_table,
3780 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3781
f00a2de2 3782 ++i;
f9d83a0b 3783 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3784
2ec9a5e0
TT
3785 return 1;
3786}
3787
927aa2e7 3788/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3789 elements of all the CUs and return 1. Otherwise, return 0. */
3790
3791static int
3792dwarf2_read_index (struct objfile *objfile)
3793{
3794 struct mapped_index local_map, *map;
3795 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3796 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3797 struct dwz_file *dwz;
2ec9a5e0 3798
4262abfb 3799 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3800 use_deprecated_index_sections,
3801 &dwarf2_per_objfile->gdb_index, &local_map,
3802 &cu_list, &cu_list_elements,
3803 &types_list, &types_list_elements))
3804 return 0;
3805
0fefef59 3806 /* Don't use the index if it's empty. */
f00a2de2 3807 if (local_map.symbol_table.empty ())
0fefef59
DE
3808 return 0;
3809
2ec9a5e0
TT
3810 /* If there is a .dwz file, read it so we can get its CU list as
3811 well. */
4db1a1dc
TT
3812 dwz = dwarf2_get_dwz_file ();
3813 if (dwz != NULL)
2ec9a5e0 3814 {
2ec9a5e0
TT
3815 struct mapped_index dwz_map;
3816 const gdb_byte *dwz_types_ignore;
3817 offset_type dwz_types_elements_ignore;
3818
3819 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3820 1,
3821 &dwz->gdb_index, &dwz_map,
3822 &dwz_list, &dwz_list_elements,
3823 &dwz_types_ignore,
3824 &dwz_types_elements_ignore))
3825 {
3826 warning (_("could not read '.gdb_index' section from %s; skipping"),
3827 bfd_get_filename (dwz->dwz_bfd));
3828 return 0;
3829 }
3830 }
3831
74a0d9f6
JK
3832 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3833 dwz_list_elements);
1fd400ff 3834
8b70b953
TT
3835 if (types_list_elements)
3836 {
3837 struct dwarf2_section_info *section;
3838
3839 /* We can only handle a single .debug_types when we have an
3840 index. */
3841 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3842 return 0;
3843
3844 section = VEC_index (dwarf2_section_info_def,
3845 dwarf2_per_objfile->types, 0);
3846
74a0d9f6
JK
3847 create_signatured_type_table_from_index (objfile, section, types_list,
3848 types_list_elements);
8b70b953 3849 }
9291a0cd 3850
2ec9a5e0
TT
3851 create_addrmap_from_index (objfile, &local_map);
3852
8d749320 3853 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3f563c84 3854 map = new (map) mapped_index ();
2ec9a5e0 3855 *map = local_map;
9291a0cd
TT
3856
3857 dwarf2_per_objfile->index_table = map;
3858 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3859 dwarf2_per_objfile->quick_file_names_table =
3860 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3861
3862 return 1;
3863}
3864
3865/* A helper for the "quick" functions which sets the global
3866 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 3867
9291a0cd
TT
3868static void
3869dw2_setup (struct objfile *objfile)
3870{
9a3c8263
SM
3871 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3872 objfile_data (objfile, dwarf2_objfile_data_key));
9291a0cd
TT
3873 gdb_assert (dwarf2_per_objfile);
3874}
3875
dee91e82 3876/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3877
dee91e82
DE
3878static void
3879dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3880 const gdb_byte *info_ptr,
dee91e82
DE
3881 struct die_info *comp_unit_die,
3882 int has_children,
3883 void *data)
9291a0cd 3884{
dee91e82
DE
3885 struct dwarf2_cu *cu = reader->cu;
3886 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3887 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3888 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3889 struct attribute *attr;
dee91e82 3890 int i;
7b9f3c50
DE
3891 void **slot;
3892 struct quick_file_names *qfn;
9291a0cd 3893
0186c6a7
DE
3894 gdb_assert (! this_cu->is_debug_types);
3895
07261596
TT
3896 /* Our callers never want to match partial units -- instead they
3897 will match the enclosing full CU. */
3898 if (comp_unit_die->tag == DW_TAG_partial_unit)
3899 {
3900 this_cu->v.quick->no_file_data = 1;
3901 return;
3902 }
3903
0186c6a7 3904 lh_cu = this_cu;
7b9f3c50 3905 slot = NULL;
dee91e82 3906
fff8551c 3907 line_header_up lh;
9c541725 3908 sect_offset line_offset {};
fff8551c 3909
dee91e82 3910 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3911 if (attr)
3912 {
7b9f3c50
DE
3913 struct quick_file_names find_entry;
3914
9c541725 3915 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3916
3917 /* We may have already read in this line header (TU line header sharing).
3918 If we have we're done. */
094b34ac 3919 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3920 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3921 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3922 &find_entry, INSERT);
3923 if (*slot != NULL)
3924 {
9a3c8263 3925 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3926 return;
7b9f3c50
DE
3927 }
3928
3019eac3 3929 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3930 }
3931 if (lh == NULL)
3932 {
094b34ac 3933 lh_cu->v.quick->no_file_data = 1;
dee91e82 3934 return;
9291a0cd
TT
3935 }
3936
8d749320 3937 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3938 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3939 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3940 gdb_assert (slot != NULL);
3941 *slot = qfn;
9291a0cd 3942
d721ba37 3943 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3944
fff8551c 3945 qfn->num_file_names = lh->file_names.size ();
8d749320 3946 qfn->file_names =
fff8551c
PA
3947 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3948 for (i = 0; i < lh->file_names.size (); ++i)
3949 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3950 qfn->real_names = NULL;
9291a0cd 3951
094b34ac 3952 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3953}
3954
3955/* A helper for the "quick" functions which attempts to read the line
3956 table for THIS_CU. */
3957
3958static struct quick_file_names *
e4a48d9d 3959dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3960{
0186c6a7
DE
3961 /* This should never be called for TUs. */
3962 gdb_assert (! this_cu->is_debug_types);
3963 /* Nor type unit groups. */
3964 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3965
dee91e82
DE
3966 if (this_cu->v.quick->file_names != NULL)
3967 return this_cu->v.quick->file_names;
3968 /* If we know there is no line data, no point in looking again. */
3969 if (this_cu->v.quick->no_file_data)
3970 return NULL;
3971
0186c6a7 3972 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3973
3974 if (this_cu->v.quick->no_file_data)
3975 return NULL;
3976 return this_cu->v.quick->file_names;
9291a0cd
TT
3977}
3978
3979/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3980 real path for a given file name from the line table. */
2fdf6df6 3981
9291a0cd 3982static const char *
7b9f3c50
DE
3983dw2_get_real_path (struct objfile *objfile,
3984 struct quick_file_names *qfn, int index)
9291a0cd 3985{
7b9f3c50
DE
3986 if (qfn->real_names == NULL)
3987 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3988 qfn->num_file_names, const char *);
9291a0cd 3989
7b9f3c50 3990 if (qfn->real_names[index] == NULL)
14278e1f 3991 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3992
7b9f3c50 3993 return qfn->real_names[index];
9291a0cd
TT
3994}
3995
3996static struct symtab *
3997dw2_find_last_source_symtab (struct objfile *objfile)
3998{
43f3e411 3999 struct compunit_symtab *cust;
9291a0cd 4000 int index;
ae2de4f8 4001
9291a0cd
TT
4002 dw2_setup (objfile);
4003 index = dwarf2_per_objfile->n_comp_units - 1;
43f3e411
DE
4004 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
4005 if (cust == NULL)
4006 return NULL;
4007 return compunit_primary_filetab (cust);
9291a0cd
TT
4008}
4009
7b9f3c50
DE
4010/* Traversal function for dw2_forget_cached_source_info. */
4011
4012static int
4013dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 4014{
7b9f3c50 4015 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 4016
7b9f3c50 4017 if (file_data->real_names)
9291a0cd 4018 {
7b9f3c50 4019 int i;
9291a0cd 4020
7b9f3c50 4021 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 4022 {
7b9f3c50
DE
4023 xfree ((void*) file_data->real_names[i]);
4024 file_data->real_names[i] = NULL;
9291a0cd
TT
4025 }
4026 }
7b9f3c50
DE
4027
4028 return 1;
4029}
4030
4031static void
4032dw2_forget_cached_source_info (struct objfile *objfile)
4033{
4034 dw2_setup (objfile);
4035
4036 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4037 dw2_free_cached_file_names, NULL);
9291a0cd
TT
4038}
4039
f8eba3c6
TT
4040/* Helper function for dw2_map_symtabs_matching_filename that expands
4041 the symtabs and calls the iterator. */
4042
4043static int
4044dw2_map_expand_apply (struct objfile *objfile,
4045 struct dwarf2_per_cu_data *per_cu,
f5b95b50 4046 const char *name, const char *real_path,
14bc53a8 4047 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 4048{
43f3e411 4049 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
4050
4051 /* Don't visit already-expanded CUs. */
43f3e411 4052 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
4053 return 0;
4054
4055 /* This may expand more than one symtab, and we want to iterate over
4056 all of them. */
a0f42c21 4057 dw2_instantiate_symtab (per_cu);
f8eba3c6 4058
14bc53a8
PA
4059 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4060 last_made, callback);
f8eba3c6
TT
4061}
4062
4063/* Implementation of the map_symtabs_matching_filename method. */
4064
14bc53a8
PA
4065static bool
4066dw2_map_symtabs_matching_filename
4067 (struct objfile *objfile, const char *name, const char *real_path,
4068 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
4069{
4070 int i;
c011a4f4 4071 const char *name_basename = lbasename (name);
9291a0cd
TT
4072
4073 dw2_setup (objfile);
ae2de4f8 4074
848e3e78
DE
4075 /* The rule is CUs specify all the files, including those used by
4076 any TU, so there's no need to scan TUs here. */
f4dc4d17 4077
848e3e78 4078 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4079 {
4080 int j;
8832e7e3 4081 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 4082 struct quick_file_names *file_data;
9291a0cd 4083
3d7bb9d9 4084 /* We only need to look at symtabs not already expanded. */
43f3e411 4085 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4086 continue;
4087
e4a48d9d 4088 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4089 if (file_data == NULL)
9291a0cd
TT
4090 continue;
4091
7b9f3c50 4092 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4093 {
7b9f3c50 4094 const char *this_name = file_data->file_names[j];
da235a7c 4095 const char *this_real_name;
9291a0cd 4096
af529f8f 4097 if (compare_filenames_for_search (this_name, name))
9291a0cd 4098 {
f5b95b50 4099 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4100 callback))
4101 return true;
288e77a7 4102 continue;
4aac40c8 4103 }
9291a0cd 4104
c011a4f4
DE
4105 /* Before we invoke realpath, which can get expensive when many
4106 files are involved, do a quick comparison of the basenames. */
4107 if (! basenames_may_differ
4108 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4109 continue;
4110
da235a7c
JK
4111 this_real_name = dw2_get_real_path (objfile, file_data, j);
4112 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 4113 {
da235a7c 4114 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4115 callback))
4116 return true;
288e77a7 4117 continue;
da235a7c 4118 }
9291a0cd 4119
da235a7c
JK
4120 if (real_path != NULL)
4121 {
af529f8f
JK
4122 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4123 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 4124 if (this_real_name != NULL
af529f8f 4125 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 4126 {
f5b95b50 4127 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4128 callback))
4129 return true;
288e77a7 4130 continue;
9291a0cd
TT
4131 }
4132 }
4133 }
4134 }
4135
14bc53a8 4136 return false;
9291a0cd
TT
4137}
4138
da51c347
DE
4139/* Struct used to manage iterating over all CUs looking for a symbol. */
4140
4141struct dw2_symtab_iterator
9291a0cd 4142{
da51c347
DE
4143 /* The internalized form of .gdb_index. */
4144 struct mapped_index *index;
4145 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4146 int want_specific_block;
4147 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4148 Unused if !WANT_SPECIFIC_BLOCK. */
4149 int block_index;
4150 /* The kind of symbol we're looking for. */
4151 domain_enum domain;
4152 /* The list of CUs from the index entry of the symbol,
4153 or NULL if not found. */
4154 offset_type *vec;
4155 /* The next element in VEC to look at. */
4156 int next;
4157 /* The number of elements in VEC, or zero if there is no match. */
4158 int length;
8943b874
DE
4159 /* Have we seen a global version of the symbol?
4160 If so we can ignore all further global instances.
4161 This is to work around gold/15646, inefficient gold-generated
4162 indices. */
4163 int global_seen;
da51c347 4164};
9291a0cd 4165
da51c347
DE
4166/* Initialize the index symtab iterator ITER.
4167 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4168 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 4169
9291a0cd 4170static void
da51c347
DE
4171dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
4172 struct mapped_index *index,
4173 int want_specific_block,
4174 int block_index,
4175 domain_enum domain,
4176 const char *name)
4177{
4178 iter->index = index;
4179 iter->want_specific_block = want_specific_block;
4180 iter->block_index = block_index;
4181 iter->domain = domain;
4182 iter->next = 0;
8943b874 4183 iter->global_seen = 0;
da51c347
DE
4184
4185 if (find_slot_in_mapped_hash (index, name, &iter->vec))
4186 iter->length = MAYBE_SWAP (*iter->vec);
4187 else
4188 {
4189 iter->vec = NULL;
4190 iter->length = 0;
4191 }
4192}
4193
4194/* Return the next matching CU or NULL if there are no more. */
4195
4196static struct dwarf2_per_cu_data *
4197dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4198{
4199 for ( ; iter->next < iter->length; ++iter->next)
4200 {
4201 offset_type cu_index_and_attrs =
4202 MAYBE_SWAP (iter->vec[iter->next + 1]);
4203 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 4204 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
4205 int want_static = iter->block_index != GLOBAL_BLOCK;
4206 /* This value is only valid for index versions >= 7. */
4207 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4208 gdb_index_symbol_kind symbol_kind =
4209 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4210 /* Only check the symbol attributes if they're present.
4211 Indices prior to version 7 don't record them,
4212 and indices >= 7 may elide them for certain symbols
4213 (gold does this). */
4214 int attrs_valid =
4215 (iter->index->version >= 7
4216 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4217
3190f0c6
DE
4218 /* Don't crash on bad data. */
4219 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4220 + dwarf2_per_objfile->n_type_units))
4221 {
4222 complaint (&symfile_complaints,
4223 _(".gdb_index entry has bad CU index"
4262abfb
JK
4224 " [in module %s]"),
4225 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
4226 continue;
4227 }
4228
8832e7e3 4229 per_cu = dw2_get_cutu (cu_index);
3190f0c6 4230
da51c347 4231 /* Skip if already read in. */
43f3e411 4232 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
4233 continue;
4234
8943b874
DE
4235 /* Check static vs global. */
4236 if (attrs_valid)
4237 {
4238 if (iter->want_specific_block
4239 && want_static != is_static)
4240 continue;
4241 /* Work around gold/15646. */
4242 if (!is_static && iter->global_seen)
4243 continue;
4244 if (!is_static)
4245 iter->global_seen = 1;
4246 }
da51c347
DE
4247
4248 /* Only check the symbol's kind if it has one. */
4249 if (attrs_valid)
4250 {
4251 switch (iter->domain)
4252 {
4253 case VAR_DOMAIN:
4254 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4255 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4256 /* Some types are also in VAR_DOMAIN. */
4257 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4258 continue;
4259 break;
4260 case STRUCT_DOMAIN:
4261 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4262 continue;
4263 break;
4264 case LABEL_DOMAIN:
4265 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4266 continue;
4267 break;
4268 default:
4269 break;
4270 }
4271 }
4272
4273 ++iter->next;
4274 return per_cu;
4275 }
4276
4277 return NULL;
4278}
4279
43f3e411 4280static struct compunit_symtab *
da51c347
DE
4281dw2_lookup_symbol (struct objfile *objfile, int block_index,
4282 const char *name, domain_enum domain)
9291a0cd 4283{
43f3e411 4284 struct compunit_symtab *stab_best = NULL;
156942c7
DE
4285 struct mapped_index *index;
4286
9291a0cd
TT
4287 dw2_setup (objfile);
4288
b5ec771e
PA
4289 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4290
156942c7
DE
4291 index = dwarf2_per_objfile->index_table;
4292
da51c347 4293 /* index is NULL if OBJF_READNOW. */
156942c7 4294 if (index)
9291a0cd 4295 {
da51c347
DE
4296 struct dw2_symtab_iterator iter;
4297 struct dwarf2_per_cu_data *per_cu;
4298
4299 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 4300
da51c347 4301 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 4302 {
b2e2f908 4303 struct symbol *sym, *with_opaque = NULL;
43f3e411
DE
4304 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4305 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
f194fefb 4306 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4307
b2e2f908
DE
4308 sym = block_find_symbol (block, name, domain,
4309 block_find_non_opaque_type_preferred,
4310 &with_opaque);
4311
da51c347
DE
4312 /* Some caution must be observed with overloaded functions
4313 and methods, since the index will not contain any overload
4314 information (but NAME might contain it). */
da51c347 4315
b2e2f908 4316 if (sym != NULL
b5ec771e 4317 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
b2e2f908
DE
4318 return stab;
4319 if (with_opaque != NULL
b5ec771e 4320 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
b2e2f908 4321 stab_best = stab;
da51c347
DE
4322
4323 /* Keep looking through other CUs. */
9291a0cd
TT
4324 }
4325 }
9291a0cd 4326
da51c347 4327 return stab_best;
9291a0cd
TT
4328}
4329
4330static void
4331dw2_print_stats (struct objfile *objfile)
4332{
e4a48d9d 4333 int i, total, count;
9291a0cd
TT
4334
4335 dw2_setup (objfile);
e4a48d9d 4336 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 4337 count = 0;
e4a48d9d 4338 for (i = 0; i < total; ++i)
9291a0cd 4339 {
8832e7e3 4340 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4341
43f3e411 4342 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4343 ++count;
4344 }
e4a48d9d 4345 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4346 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4347}
4348
779bd270
DE
4349/* This dumps minimal information about the index.
4350 It is called via "mt print objfiles".
4351 One use is to verify .gdb_index has been loaded by the
4352 gdb.dwarf2/gdb-index.exp testcase. */
4353
9291a0cd
TT
4354static void
4355dw2_dump (struct objfile *objfile)
4356{
779bd270
DE
4357 dw2_setup (objfile);
4358 gdb_assert (dwarf2_per_objfile->using_index);
4359 printf_filtered (".gdb_index:");
4360 if (dwarf2_per_objfile->index_table != NULL)
4361 {
4362 printf_filtered (" version %d\n",
4363 dwarf2_per_objfile->index_table->version);
4364 }
4365 else
4366 printf_filtered (" faked for \"readnow\"\n");
4367 printf_filtered ("\n");
9291a0cd
TT
4368}
4369
4370static void
3189cb12
DE
4371dw2_relocate (struct objfile *objfile,
4372 const struct section_offsets *new_offsets,
4373 const struct section_offsets *delta)
9291a0cd
TT
4374{
4375 /* There's nothing to relocate here. */
4376}
4377
4378static void
4379dw2_expand_symtabs_for_function (struct objfile *objfile,
4380 const char *func_name)
4381{
da51c347
DE
4382 struct mapped_index *index;
4383
4384 dw2_setup (objfile);
4385
4386 index = dwarf2_per_objfile->index_table;
4387
4388 /* index is NULL if OBJF_READNOW. */
4389 if (index)
4390 {
4391 struct dw2_symtab_iterator iter;
4392 struct dwarf2_per_cu_data *per_cu;
4393
4394 /* Note: It doesn't matter what we pass for block_index here. */
4395 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4396 func_name);
4397
4398 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4399 dw2_instantiate_symtab (per_cu);
4400 }
9291a0cd
TT
4401}
4402
4403static void
4404dw2_expand_all_symtabs (struct objfile *objfile)
4405{
4406 int i;
4407
4408 dw2_setup (objfile);
1fd400ff
TT
4409
4410 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4411 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4412 {
8832e7e3 4413 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4414
a0f42c21 4415 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4416 }
4417}
4418
4419static void
652a8996
JK
4420dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4421 const char *fullname)
9291a0cd
TT
4422{
4423 int i;
4424
4425 dw2_setup (objfile);
d4637a04
DE
4426
4427 /* We don't need to consider type units here.
4428 This is only called for examining code, e.g. expand_line_sal.
4429 There can be an order of magnitude (or more) more type units
4430 than comp units, and we avoid them if we can. */
4431
4432 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4433 {
4434 int j;
8832e7e3 4435 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7b9f3c50 4436 struct quick_file_names *file_data;
9291a0cd 4437
3d7bb9d9 4438 /* We only need to look at symtabs not already expanded. */
43f3e411 4439 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4440 continue;
4441
e4a48d9d 4442 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4443 if (file_data == NULL)
9291a0cd
TT
4444 continue;
4445
7b9f3c50 4446 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4447 {
652a8996
JK
4448 const char *this_fullname = file_data->file_names[j];
4449
4450 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4451 {
a0f42c21 4452 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4453 break;
4454 }
4455 }
4456 }
4457}
4458
9291a0cd 4459static void
ade7ed9e 4460dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4461 const char * name, domain_enum domain,
ade7ed9e 4462 int global,
40658b94
PH
4463 int (*callback) (struct block *,
4464 struct symbol *, void *),
b5ec771e 4465 void *data, symbol_name_match_type match,
2edb89d3 4466 symbol_compare_ftype *ordered_compare)
9291a0cd 4467{
40658b94 4468 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4469 current language is Ada for a non-Ada objfile using GNU index. As Ada
4470 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4471}
4472
b5ec771e
PA
4473/* Symbol name matcher for .gdb_index names.
4474
4475 Symbol names in .gdb_index have a few particularities:
4476
4477 - There's no indication of which is the language of each symbol.
4478
4479 Since each language has its own symbol name matching algorithm,
4480 and we don't know which language is the right one, we must match
3f563c84
PA
4481 each symbol against all languages. This would be a potential
4482 performance problem if it were not mitigated by the
4483 mapped_index::name_components lookup table, which significantly
4484 reduces the number of times we need to call into this matcher,
4485 making it a non-issue.
b5ec771e
PA
4486
4487 - Symbol names in the index have no overload (parameter)
4488 information. I.e., in C++, "foo(int)" and "foo(long)" both
4489 appear as "foo" in the index, for example.
4490
4491 This means that the lookup names passed to the symbol name
4492 matcher functions must have no parameter information either
4493 because (e.g.) symbol search name "foo" does not match
4494 lookup-name "foo(int)" [while swapping search name for lookup
4495 name would match].
4496*/
4497class gdb_index_symbol_name_matcher
4498{
4499public:
4500 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4501 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4502
4503 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4504 Returns true if any matcher matches. */
4505 bool matches (const char *symbol_name);
4506
4507private:
4508 /* A reference to the lookup name we're matching against. */
4509 const lookup_name_info &m_lookup_name;
4510
4511 /* A vector holding all the different symbol name matchers, for all
4512 languages. */
4513 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4514};
4515
4516gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4517 (const lookup_name_info &lookup_name)
4518 : m_lookup_name (lookup_name)
4519{
4520 /* Prepare the vector of comparison functions upfront, to avoid
4521 doing the same work for each symbol. Care is taken to avoid
4522 matching with the same matcher more than once if/when multiple
4523 languages use the same matcher function. */
4524 auto &matchers = m_symbol_name_matcher_funcs;
4525 matchers.reserve (nr_languages);
4526
4527 matchers.push_back (default_symbol_name_matcher);
4528
4529 for (int i = 0; i < nr_languages; i++)
4530 {
4531 const language_defn *lang = language_def ((enum language) i);
4532 if (lang->la_get_symbol_name_matcher != NULL)
4533 {
4534 symbol_name_matcher_ftype *name_matcher
4535 = lang->la_get_symbol_name_matcher (m_lookup_name);
4536
4537 /* Don't insert the same comparison routine more than once.
4538 Note that we do this linear walk instead of a cheaper
4539 sorted insert, or use a std::set or something like that,
4540 because relative order of function addresses is not
4541 stable. This is not a problem in practice because the
4542 number of supported languages is low, and the cost here
4543 is tiny compared to the number of searches we'll do
4544 afterwards using this object. */
4545 if (std::find (matchers.begin (), matchers.end (), name_matcher)
4546 == matchers.end ())
4547 matchers.push_back (name_matcher);
4548 }
4549 }
4550}
4551
4552bool
4553gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4554{
4555 for (auto matches_name : m_symbol_name_matcher_funcs)
4556 if (matches_name (symbol_name, m_lookup_name, NULL))
4557 return true;
4558
4559 return false;
4560}
4561
e1ef7d7a
PA
4562/* Starting from a search name, return the string that finds the upper
4563 bound of all strings that start with SEARCH_NAME in a sorted name
4564 list. Returns the empty string to indicate that the upper bound is
4565 the end of the list. */
4566
4567static std::string
4568make_sort_after_prefix_name (const char *search_name)
4569{
4570 /* When looking to complete "func", we find the upper bound of all
4571 symbols that start with "func" by looking for where we'd insert
4572 the closest string that would follow "func" in lexicographical
4573 order. Usually, that's "func"-with-last-character-incremented,
4574 i.e. "fund". Mind non-ASCII characters, though. Usually those
4575 will be UTF-8 multi-byte sequences, but we can't be certain.
4576 Especially mind the 0xff character, which is a valid character in
4577 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4578 rule out compilers allowing it in identifiers. Note that
4579 conveniently, strcmp/strcasecmp are specified to compare
4580 characters interpreted as unsigned char. So what we do is treat
4581 the whole string as a base 256 number composed of a sequence of
4582 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4583 to 0, and carries 1 to the following more-significant position.
4584 If the very first character in SEARCH_NAME ends up incremented
4585 and carries/overflows, then the upper bound is the end of the
4586 list. The string after the empty string is also the empty
4587 string.
4588
4589 Some examples of this operation:
4590
4591 SEARCH_NAME => "+1" RESULT
4592
4593 "abc" => "abd"
4594 "ab\xff" => "ac"
4595 "\xff" "a" "\xff" => "\xff" "b"
4596 "\xff" => ""
4597 "\xff\xff" => ""
4598 "" => ""
4599
4600 Then, with these symbols for example:
4601
4602 func
4603 func1
4604 fund
4605
4606 completing "func" looks for symbols between "func" and
4607 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4608 which finds "func" and "func1", but not "fund".
4609
4610 And with:
4611
4612 funcÿ (Latin1 'ÿ' [0xff])
4613 funcÿ1
4614 fund
4615
4616 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4617 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4618
4619 And with:
4620
4621 ÿÿ (Latin1 'ÿ' [0xff])
4622 ÿÿ1
4623
4624 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4625 the end of the list.
4626 */
4627 std::string after = search_name;
4628 while (!after.empty () && (unsigned char) after.back () == 0xff)
4629 after.pop_back ();
4630 if (!after.empty ())
4631 after.back () = (unsigned char) after.back () + 1;
4632 return after;
4633}
4634
5c58de74 4635/* See declaration. */
61d96d7e 4636
5c58de74
PA
4637std::pair<std::vector<name_component>::const_iterator,
4638 std::vector<name_component>::const_iterator>
44ed8f3e 4639mapped_index_base::find_name_components_bounds
5c58de74 4640 (const lookup_name_info &lookup_name_without_params) const
3f563c84 4641{
5c58de74
PA
4642 auto *name_cmp
4643 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84
PA
4644
4645 const char *cplus
c62446b1 4646 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4647
3f563c84
PA
4648 /* Comparison function object for lower_bound that matches against a
4649 given symbol name. */
4650 auto lookup_compare_lower = [&] (const name_component &elem,
4651 const char *name)
4652 {
5c58de74 4653 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4654 const char *elem_name = elem_qualified + elem.name_offset;
4655 return name_cmp (elem_name, name) < 0;
4656 };
4657
4658 /* Comparison function object for upper_bound that matches against a
4659 given symbol name. */
4660 auto lookup_compare_upper = [&] (const char *name,
4661 const name_component &elem)
4662 {
5c58de74 4663 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4664 const char *elem_name = elem_qualified + elem.name_offset;
4665 return name_cmp (name, elem_name) < 0;
4666 };
4667
5c58de74
PA
4668 auto begin = this->name_components.begin ();
4669 auto end = this->name_components.end ();
3f563c84
PA
4670
4671 /* Find the lower bound. */
4672 auto lower = [&] ()
4673 {
5c58de74 4674 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4675 return begin;
4676 else
4677 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4678 } ();
4679
4680 /* Find the upper bound. */
4681 auto upper = [&] ()
4682 {
5c58de74 4683 if (lookup_name_without_params.completion_mode ())
3f563c84 4684 {
e1ef7d7a
PA
4685 /* In completion mode, we want UPPER to point past all
4686 symbols names that have the same prefix. I.e., with
4687 these symbols, and completing "func":
4688
4689 function << lower bound
4690 function1
4691 other_function << upper bound
4692
4693 We find the upper bound by looking for the insertion
4694 point of "func"-with-last-character-incremented,
4695 i.e. "fund". */
4696 std::string after = make_sort_after_prefix_name (cplus);
4697 if (after.empty ())
3f563c84 4698 return end;
e6b2f5ef
PA
4699 return std::lower_bound (lower, end, after.c_str (),
4700 lookup_compare_lower);
3f563c84
PA
4701 }
4702 else
4703 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4704 } ();
4705
5c58de74
PA
4706 return {lower, upper};
4707}
4708
4709/* See declaration. */
4710
4711void
44ed8f3e 4712mapped_index_base::build_name_components ()
5c58de74
PA
4713{
4714 if (!this->name_components.empty ())
4715 return;
4716
4717 this->name_components_casing = case_sensitivity;
4718 auto *name_cmp
4719 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4720
4721 /* The code below only knows how to break apart components of C++
4722 symbol names (and other languages that use '::' as
4723 namespace/module separator). If we add support for wild matching
4724 to some language that uses some other operator (E.g., Ada, Go and
4725 D use '.'), then we'll need to try splitting the symbol name
4726 according to that language too. Note that Ada does support wild
4727 matching, but doesn't currently support .gdb_index. */
44ed8f3e
PA
4728 auto count = this->symbol_name_count ();
4729 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4730 {
44ed8f3e 4731 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4732 continue;
4733
4734 const char *name = this->symbol_name_at (idx);
4735
4736 /* Add each name component to the name component table. */
4737 unsigned int previous_len = 0;
4738 for (unsigned int current_len = cp_find_first_component (name);
4739 name[current_len] != '\0';
4740 current_len += cp_find_first_component (name + current_len))
4741 {
4742 gdb_assert (name[current_len] == ':');
4743 this->name_components.push_back ({previous_len, idx});
4744 /* Skip the '::'. */
4745 current_len += 2;
4746 previous_len = current_len;
4747 }
4748 this->name_components.push_back ({previous_len, idx});
4749 }
4750
4751 /* Sort name_components elements by name. */
4752 auto name_comp_compare = [&] (const name_component &left,
4753 const name_component &right)
4754 {
4755 const char *left_qualified = this->symbol_name_at (left.idx);
4756 const char *right_qualified = this->symbol_name_at (right.idx);
4757
4758 const char *left_name = left_qualified + left.name_offset;
4759 const char *right_name = right_qualified + right.name_offset;
4760
4761 return name_cmp (left_name, right_name) < 0;
4762 };
4763
4764 std::sort (this->name_components.begin (),
4765 this->name_components.end (),
4766 name_comp_compare);
4767}
4768
4769/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4770 mapped_index_base instead of the containing objfile. This is split
4771 to a separate function in order to be able to unit test the
4772 name_components matching using a mock mapped_index_base. For each
5c58de74 4773 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4774 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4775
4776static void
4777dw2_expand_symtabs_matching_symbol
44ed8f3e 4778 (mapped_index_base &index,
5c58de74
PA
4779 const lookup_name_info &lookup_name_in,
4780 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4781 enum search_domain kind,
4782 gdb::function_view<void (offset_type)> match_callback)
4783{
4784 lookup_name_info lookup_name_without_params
4785 = lookup_name_in.make_ignore_params ();
4786 gdb_index_symbol_name_matcher lookup_name_matcher
4787 (lookup_name_without_params);
4788
4789 /* Build the symbol name component sorted vector, if we haven't
4790 yet. */
4791 index.build_name_components ();
4792
4793 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4794
3f563c84
PA
4795 /* Now for each symbol name in range, check to see if we have a name
4796 match, and if so, call the MATCH_CALLBACK callback. */
4797
4798 /* The same symbol may appear more than once in the range though.
4799 E.g., if we're looking for symbols that complete "w", and we have
4800 a symbol named "w1::w2", we'll find the two name components for
4801 that same symbol in the range. To be sure we only call the
4802 callback once per symbol, we first collect the symbol name
4803 indexes that matched in a temporary vector and ignore
4804 duplicates. */
4805 std::vector<offset_type> matches;
5c58de74 4806 matches.reserve (std::distance (bounds.first, bounds.second));
3f563c84 4807
5c58de74 4808 for (; bounds.first != bounds.second; ++bounds.first)
3f563c84 4809 {
5c58de74 4810 const char *qualified = index.symbol_name_at (bounds.first->idx);
3f563c84
PA
4811
4812 if (!lookup_name_matcher.matches (qualified)
4813 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4814 continue;
4815
5c58de74 4816 matches.push_back (bounds.first->idx);
3f563c84
PA
4817 }
4818
4819 std::sort (matches.begin (), matches.end ());
4820
4821 /* Finally call the callback, once per match. */
4822 ULONGEST prev = -1;
4823 for (offset_type idx : matches)
4824 {
4825 if (prev != idx)
4826 {
4827 match_callback (idx);
4828 prev = idx;
4829 }
4830 }
4831
4832 /* Above we use a type wider than idx's for 'prev', since 0 and
4833 (offset_type)-1 are both possible values. */
4834 static_assert (sizeof (prev) > sizeof (offset_type), "");
4835}
4836
c62446b1
PA
4837#if GDB_SELF_TEST
4838
4839namespace selftests { namespace dw2_expand_symtabs_matching {
4840
a3c5fafd
PA
4841/* A mock .gdb_index/.debug_names-like name index table, enough to
4842 exercise dw2_expand_symtabs_matching_symbol, which works with the
4843 mapped_index_base interface. Builds an index from the symbol list
4844 passed as parameter to the constructor. */
4845class mock_mapped_index : public mapped_index_base
c62446b1
PA
4846{
4847public:
a3c5fafd
PA
4848 mock_mapped_index (gdb::array_view<const char *> symbols)
4849 : m_symbol_table (symbols)
c62446b1
PA
4850 {}
4851
a3c5fafd 4852 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4853
a3c5fafd
PA
4854 /* Return the number of names in the symbol table. */
4855 virtual size_t symbol_name_count () const
c62446b1 4856 {
a3c5fafd 4857 return m_symbol_table.size ();
c62446b1
PA
4858 }
4859
a3c5fafd
PA
4860 /* Get the name of the symbol at IDX in the symbol table. */
4861 virtual const char *symbol_name_at (offset_type idx) const
4862 {
4863 return m_symbol_table[idx];
4864 }
c62446b1 4865
a3c5fafd
PA
4866private:
4867 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4868};
4869
4870/* Convenience function that converts a NULL pointer to a "<null>"
4871 string, to pass to print routines. */
4872
4873static const char *
4874string_or_null (const char *str)
4875{
4876 return str != NULL ? str : "<null>";
4877}
4878
4879/* Check if a lookup_name_info built from
4880 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4881 index. EXPECTED_LIST is the list of expected matches, in expected
4882 matching order. If no match expected, then an empty list is
4883 specified. Returns true on success. On failure prints a warning
4884 indicating the file:line that failed, and returns false. */
4885
4886static bool
4887check_match (const char *file, int line,
4888 mock_mapped_index &mock_index,
4889 const char *name, symbol_name_match_type match_type,
4890 bool completion_mode,
4891 std::initializer_list<const char *> expected_list)
4892{
4893 lookup_name_info lookup_name (name, match_type, completion_mode);
4894
4895 bool matched = true;
4896
4897 auto mismatch = [&] (const char *expected_str,
4898 const char *got)
4899 {
4900 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4901 "expected=\"%s\", got=\"%s\"\n"),
4902 file, line,
4903 (match_type == symbol_name_match_type::FULL
4904 ? "FULL" : "WILD"),
4905 name, string_or_null (expected_str), string_or_null (got));
4906 matched = false;
4907 };
4908
4909 auto expected_it = expected_list.begin ();
4910 auto expected_end = expected_list.end ();
4911
a3c5fafd 4912 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4913 NULL, ALL_DOMAIN,
4914 [&] (offset_type idx)
4915 {
a3c5fafd 4916 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4917 const char *expected_str
4918 = expected_it == expected_end ? NULL : *expected_it++;
4919
4920 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4921 mismatch (expected_str, matched_name);
4922 });
4923
4924 const char *expected_str
4925 = expected_it == expected_end ? NULL : *expected_it++;
4926 if (expected_str != NULL)
4927 mismatch (expected_str, NULL);
4928
4929 return matched;
4930}
4931
4932/* The symbols added to the mock mapped_index for testing (in
4933 canonical form). */
4934static const char *test_symbols[] = {
4935 "function",
4936 "std::bar",
4937 "std::zfunction",
4938 "std::zfunction2",
4939 "w1::w2",
4940 "ns::foo<char*>",
4941 "ns::foo<int>",
4942 "ns::foo<long>",
a20714ff
PA
4943 "ns2::tmpl<int>::foo2",
4944 "(anonymous namespace)::A::B::C",
c62446b1 4945
e1ef7d7a
PA
4946 /* These are used to check that the increment-last-char in the
4947 matching algorithm for completion doesn't match "t1_fund" when
4948 completing "t1_func". */
4949 "t1_func",
4950 "t1_func1",
4951 "t1_fund",
4952 "t1_fund1",
4953
4954 /* A UTF-8 name with multi-byte sequences to make sure that
4955 cp-name-parser understands this as a single identifier ("função"
4956 is "function" in PT). */
4957 u8"u8função",
4958
4959 /* \377 (0xff) is Latin1 'ÿ'. */
4960 "yfunc\377",
4961
4962 /* \377 (0xff) is Latin1 'ÿ'. */
4963 "\377",
4964 "\377\377123",
4965
c62446b1
PA
4966 /* A name with all sorts of complications. Starts with "z" to make
4967 it easier for the completion tests below. */
4968#define Z_SYM_NAME \
4969 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4970 "::tuple<(anonymous namespace)::ui*, " \
4971 "std::default_delete<(anonymous namespace)::ui>, void>"
4972
4973 Z_SYM_NAME
4974};
4975
a3c5fafd
PA
4976/* Returns true if the mapped_index_base::find_name_component_bounds
4977 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4978 in completion mode. */
5c58de74
PA
4979
4980static bool
a3c5fafd 4981check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4982 const char *search_name,
4983 gdb::array_view<const char *> expected_syms)
4984{
4985 lookup_name_info lookup_name (search_name,
4986 symbol_name_match_type::FULL, true);
4987
4988 auto bounds = index.find_name_components_bounds (lookup_name);
4989
4990 size_t distance = std::distance (bounds.first, bounds.second);
4991 if (distance != expected_syms.size ())
4992 return false;
4993
4994 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4995 {
4996 auto nc_elem = bounds.first + exp_elem;
4997 const char *qualified = index.symbol_name_at (nc_elem->idx);
4998 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4999 return false;
5000 }
5001
5002 return true;
5003}
5004
5005/* Test the lower-level mapped_index::find_name_component_bounds
5006 method. */
5007
c62446b1 5008static void
5c58de74
PA
5009test_mapped_index_find_name_component_bounds ()
5010{
5011 mock_mapped_index mock_index (test_symbols);
5012
a3c5fafd 5013 mock_index.build_name_components ();
5c58de74
PA
5014
5015 /* Test the lower-level mapped_index::find_name_component_bounds
5016 method in completion mode. */
5017 {
5018 static const char *expected_syms[] = {
5019 "t1_func",
5020 "t1_func1",
5c58de74
PA
5021 };
5022
a3c5fafd 5023 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
5024 "t1_func", expected_syms));
5025 }
5026
5027 /* Check that the increment-last-char in the name matching algorithm
5028 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5029 {
5030 static const char *expected_syms1[] = {
5031 "\377",
5032 "\377\377123",
5033 };
a3c5fafd 5034 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
5035 "\377", expected_syms1));
5036
5037 static const char *expected_syms2[] = {
5038 "\377\377123",
5039 };
a3c5fafd 5040 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
5041 "\377\377", expected_syms2));
5042 }
5043}
5044
5045/* Test dw2_expand_symtabs_matching_symbol. */
5046
5047static void
5048test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
5049{
5050 mock_mapped_index mock_index (test_symbols);
5051
5052 /* We let all tests run until the end even if some fails, for debug
5053 convenience. */
5054 bool any_mismatch = false;
5055
5056 /* Create the expected symbols list (an initializer_list). Needed
5057 because lists have commas, and we need to pass them to CHECK,
5058 which is a macro. */
5059#define EXPECT(...) { __VA_ARGS__ }
5060
5061 /* Wrapper for check_match that passes down the current
5062 __FILE__/__LINE__. */
5063#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5064 any_mismatch |= !check_match (__FILE__, __LINE__, \
5065 mock_index, \
5066 NAME, MATCH_TYPE, COMPLETION_MODE, \
5067 EXPECTED_LIST)
5068
5069 /* Identity checks. */
5070 for (const char *sym : test_symbols)
5071 {
5072 /* Should be able to match all existing symbols. */
5073 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5074 EXPECT (sym));
5075
5076 /* Should be able to match all existing symbols with
5077 parameters. */
5078 std::string with_params = std::string (sym) + "(int)";
5079 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5080 EXPECT (sym));
5081
5082 /* Should be able to match all existing symbols with
5083 parameters and qualifiers. */
5084 with_params = std::string (sym) + " ( int ) const";
5085 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5086 EXPECT (sym));
5087
5088 /* This should really find sym, but cp-name-parser.y doesn't
5089 know about lvalue/rvalue qualifiers yet. */
5090 with_params = std::string (sym) + " ( int ) &&";
5091 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5092 {});
5093 }
5094
e1ef7d7a
PA
5095 /* Check that the name matching algorithm for completion doesn't get
5096 confused with Latin1 'ÿ' / 0xff. */
5097 {
5098 static const char str[] = "\377";
5099 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5100 EXPECT ("\377", "\377\377123"));
5101 }
5102
5103 /* Check that the increment-last-char in the matching algorithm for
5104 completion doesn't match "t1_fund" when completing "t1_func". */
5105 {
5106 static const char str[] = "t1_func";
5107 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5108 EXPECT ("t1_func", "t1_func1"));
5109 }
5110
c62446b1
PA
5111 /* Check that completion mode works at each prefix of the expected
5112 symbol name. */
5113 {
5114 static const char str[] = "function(int)";
5115 size_t len = strlen (str);
5116 std::string lookup;
5117
5118 for (size_t i = 1; i < len; i++)
5119 {
5120 lookup.assign (str, i);
5121 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5122 EXPECT ("function"));
5123 }
5124 }
5125
5126 /* While "w" is a prefix of both components, the match function
5127 should still only be called once. */
5128 {
5129 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5130 EXPECT ("w1::w2"));
a20714ff
PA
5131 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5132 EXPECT ("w1::w2"));
c62446b1
PA
5133 }
5134
5135 /* Same, with a "complicated" symbol. */
5136 {
5137 static const char str[] = Z_SYM_NAME;
5138 size_t len = strlen (str);
5139 std::string lookup;
5140
5141 for (size_t i = 1; i < len; i++)
5142 {
5143 lookup.assign (str, i);
5144 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5145 EXPECT (Z_SYM_NAME));
5146 }
5147 }
5148
5149 /* In FULL mode, an incomplete symbol doesn't match. */
5150 {
5151 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5152 {});
5153 }
5154
5155 /* A complete symbol with parameters matches any overload, since the
5156 index has no overload info. */
5157 {
5158 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5159 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
5160 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5161 EXPECT ("std::zfunction", "std::zfunction2"));
5162 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5163 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
5164 }
5165
5166 /* Check that whitespace is ignored appropriately. A symbol with a
5167 template argument list. */
5168 {
5169 static const char expected[] = "ns::foo<int>";
5170 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5171 EXPECT (expected));
a20714ff
PA
5172 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5173 EXPECT (expected));
c62446b1
PA
5174 }
5175
5176 /* Check that whitespace is ignored appropriately. A symbol with a
5177 template argument list that includes a pointer. */
5178 {
5179 static const char expected[] = "ns::foo<char*>";
5180 /* Try both completion and non-completion modes. */
5181 static const bool completion_mode[2] = {false, true};
5182 for (size_t i = 0; i < 2; i++)
5183 {
5184 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5185 completion_mode[i], EXPECT (expected));
a20714ff
PA
5186 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5187 completion_mode[i], EXPECT (expected));
c62446b1
PA
5188
5189 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5190 completion_mode[i], EXPECT (expected));
a20714ff
PA
5191 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5192 completion_mode[i], EXPECT (expected));
c62446b1
PA
5193 }
5194 }
5195
5196 {
5197 /* Check method qualifiers are ignored. */
5198 static const char expected[] = "ns::foo<char*>";
5199 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5200 symbol_name_match_type::FULL, true, EXPECT (expected));
5201 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5202 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
5203 CHECK_MATCH ("foo < char * > ( int ) const",
5204 symbol_name_match_type::WILD, true, EXPECT (expected));
5205 CHECK_MATCH ("foo < char * > ( int ) &&",
5206 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
5207 }
5208
5209 /* Test lookup names that don't match anything. */
5210 {
a20714ff
PA
5211 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5212 {});
5213
c62446b1
PA
5214 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5215 {});
5216 }
5217
a20714ff
PA
5218 /* Some wild matching tests, exercising "(anonymous namespace)",
5219 which should not be confused with a parameter list. */
5220 {
5221 static const char *syms[] = {
5222 "A::B::C",
5223 "B::C",
5224 "C",
5225 "A :: B :: C ( int )",
5226 "B :: C ( int )",
5227 "C ( int )",
5228 };
5229
5230 for (const char *s : syms)
5231 {
5232 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5233 EXPECT ("(anonymous namespace)::A::B::C"));
5234 }
5235 }
5236
5237 {
5238 static const char expected[] = "ns2::tmpl<int>::foo2";
5239 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5240 EXPECT (expected));
5241 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5242 EXPECT (expected));
5243 }
5244
c62446b1
PA
5245 SELF_CHECK (!any_mismatch);
5246
5247#undef EXPECT
5248#undef CHECK_MATCH
5249}
5250
5c58de74
PA
5251static void
5252run_test ()
5253{
5254 test_mapped_index_find_name_component_bounds ();
5255 test_dw2_expand_symtabs_matching_symbol ();
5256}
5257
c62446b1
PA
5258}} // namespace selftests::dw2_expand_symtabs_matching
5259
5260#endif /* GDB_SELF_TEST */
5261
4b514bc8
JK
5262/* If FILE_MATCHER is NULL or if PER_CU has
5263 dwarf2_per_cu_quick_data::MARK set (see
5264 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5265 EXPANSION_NOTIFY on it. */
5266
5267static void
5268dw2_expand_symtabs_matching_one
5269 (struct dwarf2_per_cu_data *per_cu,
5270 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5271 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5272{
5273 if (file_matcher == NULL || per_cu->v.quick->mark)
5274 {
5275 bool symtab_was_null
5276 = (per_cu->v.quick->compunit_symtab == NULL);
5277
5278 dw2_instantiate_symtab (per_cu);
5279
5280 if (expansion_notify != NULL
5281 && symtab_was_null
5282 && per_cu->v.quick->compunit_symtab != NULL)
5283 expansion_notify (per_cu->v.quick->compunit_symtab);
5284 }
5285}
5286
3f563c84
PA
5287/* Helper for dw2_expand_matching symtabs. Called on each symbol
5288 matched, to expand corresponding CUs that were marked. IDX is the
5289 index of the symbol name that matched. */
5290
5291static void
5292dw2_expand_marked_cus
5293 (mapped_index &index, offset_type idx,
5294 struct objfile *objfile,
5295 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5296 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5297 search_domain kind)
5298{
3f563c84
PA
5299 offset_type *vec, vec_len, vec_idx;
5300 bool global_seen = false;
5301
61920122 5302 vec = (offset_type *) (index.constant_pool
f00a2de2 5303 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5304 vec_len = MAYBE_SWAP (vec[0]);
5305 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5306 {
5307 struct dwarf2_per_cu_data *per_cu;
5308 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5309 /* This value is only valid for index versions >= 7. */
5310 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5311 gdb_index_symbol_kind symbol_kind =
5312 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5313 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5314 /* Only check the symbol attributes if they're present.
5315 Indices prior to version 7 don't record them,
5316 and indices >= 7 may elide them for certain symbols
5317 (gold does this). */
5318 int attrs_valid =
5319 (index.version >= 7
5320 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5321
5322 /* Work around gold/15646. */
5323 if (attrs_valid)
9291a0cd 5324 {
61920122
PA
5325 if (!is_static && global_seen)
5326 continue;
5327 if (!is_static)
5328 global_seen = true;
5329 }
3190f0c6 5330
61920122
PA
5331 /* Only check the symbol's kind if it has one. */
5332 if (attrs_valid)
5333 {
5334 switch (kind)
8943b874 5335 {
61920122
PA
5336 case VARIABLES_DOMAIN:
5337 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5338 continue;
5339 break;
5340 case FUNCTIONS_DOMAIN:
5341 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5342 continue;
61920122
PA
5343 break;
5344 case TYPES_DOMAIN:
5345 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5346 continue;
5347 break;
5348 default:
5349 break;
8943b874 5350 }
61920122 5351 }
8943b874 5352
61920122
PA
5353 /* Don't crash on bad data. */
5354 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5355 + dwarf2_per_objfile->n_type_units))
5356 {
5357 complaint (&symfile_complaints,
5358 _(".gdb_index entry has bad CU index"
5359 " [in module %s]"), objfile_name (objfile));
5360 continue;
5361 }
5362
5363 per_cu = dw2_get_cutu (cu_index);
4b514bc8
JK
5364 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5365 expansion_notify);
61920122
PA
5366 }
5367}
5368
4b514bc8
JK
5369/* If FILE_MATCHER is non-NULL, set all the
5370 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5371 that match FILE_MATCHER. */
5372
61920122 5373static void
4b514bc8
JK
5374dw_expand_symtabs_matching_file_matcher
5375 (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5376{
4b514bc8 5377 if (file_matcher == NULL)
61920122
PA
5378 return;
5379
4b514bc8
JK
5380 objfile *const objfile = dwarf2_per_objfile->objfile;
5381
5382 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5383 htab_eq_pointer,
5384 NULL, xcalloc, xfree));
5385 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5386 htab_eq_pointer,
5387 NULL, xcalloc, xfree));
61920122 5388
4b514bc8
JK
5389 /* The rule is CUs specify all the files, including those used by
5390 any TU, so there's no need to scan TUs here. */
61920122 5391
927aa2e7
JK
5392 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5393 {
5394 int j;
5395 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5396 struct quick_file_names *file_data;
5397 void **slot;
5398
5399 QUIT;
5400
5401 per_cu->v.quick->mark = 0;
5402
5403 /* We only need to look at symtabs not already expanded. */
5404 if (per_cu->v.quick->compunit_symtab)
5405 continue;
5406
5407 file_data = dw2_get_file_names (per_cu);
5408 if (file_data == NULL)
5409 continue;
5410
5411 if (htab_find (visited_not_found.get (), file_data) != NULL)
5412 continue;
5413 else if (htab_find (visited_found.get (), file_data) != NULL)
5414 {
5415 per_cu->v.quick->mark = 1;
5416 continue;
5417 }
5418
5419 for (j = 0; j < file_data->num_file_names; ++j)
5420 {
5421 const char *this_real_name;
5422
5423 if (file_matcher (file_data->file_names[j], false))
5424 {
5425 per_cu->v.quick->mark = 1;
5426 break;
5427 }
5428
5429 /* Before we invoke realpath, which can get expensive when many
5430 files are involved, do a quick comparison of the basenames. */
5431 if (!basenames_may_differ
5432 && !file_matcher (lbasename (file_data->file_names[j]),
5433 true))
5434 continue;
5435
5436 this_real_name = dw2_get_real_path (objfile, file_data, j);
5437 if (file_matcher (this_real_name, false))
5438 {
5439 per_cu->v.quick->mark = 1;
5440 break;
5441 }
5442 }
5443
5444 slot = htab_find_slot (per_cu->v.quick->mark
5445 ? visited_found.get ()
5446 : visited_not_found.get (),
5447 file_data, INSERT);
5448 *slot = file_data;
5449 }
5450}
5451
5452static void
5453dw2_expand_symtabs_matching
5454 (struct objfile *objfile,
5455 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5456 const lookup_name_info &lookup_name,
5457 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5458 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5459 enum search_domain kind)
5460{
927aa2e7
JK
5461 dw2_setup (objfile);
5462
5463 /* index_table is NULL if OBJF_READNOW. */
5464 if (!dwarf2_per_objfile->index_table)
5465 return;
5466
5467 dw_expand_symtabs_matching_file_matcher (file_matcher);
5468
5469 mapped_index &index = *dwarf2_per_objfile->index_table;
5470
5471 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5472 symbol_matcher,
5473 kind, [&] (offset_type idx)
5474 {
5475 dw2_expand_marked_cus (index, idx, objfile, file_matcher,
5476 expansion_notify, kind);
5477 });
5478}
5479
5480/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5481 symtab. */
5482
5483static struct compunit_symtab *
5484recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5485 CORE_ADDR pc)
5486{
5487 int i;
5488
5489 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5490 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5491 return cust;
5492
5493 if (cust->includes == NULL)
5494 return NULL;
5495
5496 for (i = 0; cust->includes[i]; ++i)
5497 {
5498 struct compunit_symtab *s = cust->includes[i];
5499
5500 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5501 if (s != NULL)
5502 return s;
5503 }
5504
5505 return NULL;
5506}
5507
5508static struct compunit_symtab *
5509dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5510 struct bound_minimal_symbol msymbol,
5511 CORE_ADDR pc,
5512 struct obj_section *section,
5513 int warn_if_readin)
5514{
5515 struct dwarf2_per_cu_data *data;
5516 struct compunit_symtab *result;
5517
5518 dw2_setup (objfile);
5519
5520 if (!objfile->psymtabs_addrmap)
5521 return NULL;
5522
5523 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5524 pc);
5525 if (!data)
5526 return NULL;
5527
5528 if (warn_if_readin && data->v.quick->compunit_symtab)
5529 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5530 paddress (get_objfile_arch (objfile), pc));
5531
5532 result
5533 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5534 pc);
5535 gdb_assert (result != NULL);
5536 return result;
5537}
5538
5539static void
5540dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5541 void *data, int need_fullname)
5542{
5543 dw2_setup (objfile);
5544
5545 if (!dwarf2_per_objfile->filenames_cache)
5546 {
5547 dwarf2_per_objfile->filenames_cache.emplace ();
5548
5549 htab_up visited (htab_create_alloc (10,
5550 htab_hash_pointer, htab_eq_pointer,
5551 NULL, xcalloc, xfree));
5552
5553 /* The rule is CUs specify all the files, including those used
5554 by any TU, so there's no need to scan TUs here. We can
5555 ignore file names coming from already-expanded CUs. */
5556
5557 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5558 {
5559 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
5560
5561 if (per_cu->v.quick->compunit_symtab)
5562 {
5563 void **slot = htab_find_slot (visited.get (),
5564 per_cu->v.quick->file_names,
5565 INSERT);
5566
5567 *slot = per_cu->v.quick->file_names;
5568 }
5569 }
5570
5571 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5572 {
5573 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5574 struct quick_file_names *file_data;
5575 void **slot;
5576
5577 /* We only need to look at symtabs not already expanded. */
5578 if (per_cu->v.quick->compunit_symtab)
5579 continue;
5580
5581 file_data = dw2_get_file_names (per_cu);
5582 if (file_data == NULL)
5583 continue;
5584
5585 slot = htab_find_slot (visited.get (), file_data, INSERT);
5586 if (*slot)
5587 {
5588 /* Already visited. */
5589 continue;
5590 }
5591 *slot = file_data;
5592
5593 for (int j = 0; j < file_data->num_file_names; ++j)
5594 {
5595 const char *filename = file_data->file_names[j];
5596 dwarf2_per_objfile->filenames_cache->seen (filename);
5597 }
5598 }
5599 }
5600
5601 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5602 {
5603 gdb::unique_xmalloc_ptr<char> this_real_name;
5604
5605 if (need_fullname)
5606 this_real_name = gdb_realpath (filename);
5607 (*fun) (filename, this_real_name.get (), data);
5608 });
5609}
5610
5611static int
5612dw2_has_symbols (struct objfile *objfile)
5613{
5614 return 1;
5615}
5616
5617const struct quick_symbol_functions dwarf2_gdb_index_functions =
5618{
5619 dw2_has_symbols,
5620 dw2_find_last_source_symtab,
5621 dw2_forget_cached_source_info,
5622 dw2_map_symtabs_matching_filename,
5623 dw2_lookup_symbol,
5624 dw2_print_stats,
5625 dw2_dump,
5626 dw2_relocate,
5627 dw2_expand_symtabs_for_function,
5628 dw2_expand_all_symtabs,
5629 dw2_expand_symtabs_with_fullname,
5630 dw2_map_matching_symbols,
5631 dw2_expand_symtabs_matching,
5632 dw2_find_pc_sect_compunit_symtab,
5633 NULL,
5634 dw2_map_symbol_filenames
5635};
5636
5637/* DWARF-5 debug_names reader. */
5638
5639/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5640static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5641
5642/* A helper function that reads the .debug_names section in SECTION
5643 and fills in MAP. FILENAME is the name of the file containing the
5644 section; it is used for error reporting.
5645
5646 Returns true if all went well, false otherwise. */
5647
5648static bool
5649read_debug_names_from_section (struct objfile *objfile,
5650 const char *filename,
5651 struct dwarf2_section_info *section,
5652 mapped_debug_names &map)
5653{
5654 if (dwarf2_section_empty_p (section))
5655 return false;
5656
5657 /* Older elfutils strip versions could keep the section in the main
5658 executable while splitting it for the separate debug info file. */
5659 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5660 return false;
5661
5662 dwarf2_read_section (objfile, section);
5663
5664 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5665
5666 const gdb_byte *addr = section->buffer;
5667
5668 bfd *const abfd = get_section_bfd_owner (section);
5669
5670 unsigned int bytes_read;
5671 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5672 addr += bytes_read;
5673
5674 map.dwarf5_is_dwarf64 = bytes_read != 4;
5675 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5676 if (bytes_read + length != section->size)
5677 {
5678 /* There may be multiple per-CU indices. */
5679 warning (_("Section .debug_names in %s length %s does not match "
5680 "section length %s, ignoring .debug_names."),
5681 filename, plongest (bytes_read + length),
5682 pulongest (section->size));
5683 return false;
5684 }
5685
5686 /* The version number. */
5687 uint16_t version = read_2_bytes (abfd, addr);
5688 addr += 2;
5689 if (version != 5)
5690 {
5691 warning (_("Section .debug_names in %s has unsupported version %d, "
5692 "ignoring .debug_names."),
5693 filename, version);
5694 return false;
5695 }
5696
5697 /* Padding. */
5698 uint16_t padding = read_2_bytes (abfd, addr);
5699 addr += 2;
5700 if (padding != 0)
5701 {
5702 warning (_("Section .debug_names in %s has unsupported padding %d, "
5703 "ignoring .debug_names."),
5704 filename, padding);
5705 return false;
5706 }
5707
5708 /* comp_unit_count - The number of CUs in the CU list. */
5709 map.cu_count = read_4_bytes (abfd, addr);
5710 addr += 4;
5711
5712 /* local_type_unit_count - The number of TUs in the local TU
5713 list. */
5714 map.tu_count = read_4_bytes (abfd, addr);
5715 addr += 4;
5716
5717 /* foreign_type_unit_count - The number of TUs in the foreign TU
5718 list. */
5719 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5720 addr += 4;
5721 if (foreign_tu_count != 0)
5722 {
5723 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5724 "ignoring .debug_names."),
5725 filename, static_cast<unsigned long> (foreign_tu_count));
5726 return false;
5727 }
5728
5729 /* bucket_count - The number of hash buckets in the hash lookup
5730 table. */
5731 map.bucket_count = read_4_bytes (abfd, addr);
5732 addr += 4;
5733
5734 /* name_count - The number of unique names in the index. */
5735 map.name_count = read_4_bytes (abfd, addr);
5736 addr += 4;
5737
5738 /* abbrev_table_size - The size in bytes of the abbreviations
5739 table. */
5740 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5741 addr += 4;
5742
5743 /* augmentation_string_size - The size in bytes of the augmentation
5744 string. This value is rounded up to a multiple of 4. */
5745 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5746 addr += 4;
5747 map.augmentation_is_gdb = ((augmentation_string_size
5748 == sizeof (dwarf5_augmentation))
5749 && memcmp (addr, dwarf5_augmentation,
5750 sizeof (dwarf5_augmentation)) == 0);
5751 augmentation_string_size += (-augmentation_string_size) & 3;
5752 addr += augmentation_string_size;
5753
5754 /* List of CUs */
5755 map.cu_table_reordered = addr;
5756 addr += map.cu_count * map.offset_size;
5757
5758 /* List of Local TUs */
5759 map.tu_table_reordered = addr;
5760 addr += map.tu_count * map.offset_size;
5761
5762 /* Hash Lookup Table */
5763 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5764 addr += map.bucket_count * 4;
5765 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5766 addr += map.name_count * 4;
5767
5768 /* Name Table */
5769 map.name_table_string_offs_reordered = addr;
5770 addr += map.name_count * map.offset_size;
5771 map.name_table_entry_offs_reordered = addr;
5772 addr += map.name_count * map.offset_size;
5773
5774 const gdb_byte *abbrev_table_start = addr;
5775 for (;;)
5776 {
5777 unsigned int bytes_read;
5778 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5779 addr += bytes_read;
5780 if (index_num == 0)
5781 break;
5782
5783 const auto insertpair
5784 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5785 if (!insertpair.second)
5786 {
5787 warning (_("Section .debug_names in %s has duplicate index %s, "
5788 "ignoring .debug_names."),
5789 filename, pulongest (index_num));
5790 return false;
5791 }
5792 mapped_debug_names::index_val &indexval = insertpair.first->second;
5793 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5794 addr += bytes_read;
5795
5796 for (;;)
5797 {
5798 mapped_debug_names::index_val::attr attr;
5799 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5800 addr += bytes_read;
5801 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5802 addr += bytes_read;
5803 if (attr.form == DW_FORM_implicit_const)
5804 {
5805 attr.implicit_const = read_signed_leb128 (abfd, addr,
5806 &bytes_read);
5807 addr += bytes_read;
5808 }
5809 if (attr.dw_idx == 0 && attr.form == 0)
5810 break;
5811 indexval.attr_vec.push_back (std::move (attr));
5812 }
5813 }
5814 if (addr != abbrev_table_start + abbrev_table_size)
5815 {
5816 warning (_("Section .debug_names in %s has abbreviation_table "
5817 "of size %zu vs. written as %u, ignoring .debug_names."),
5818 filename, addr - abbrev_table_start, abbrev_table_size);
5819 return false;
5820 }
5821 map.entry_pool = addr;
5822
5823 return true;
5824}
5825
5826/* A helper for create_cus_from_debug_names that handles the MAP's CU
5827 list. */
5828
5829static void
5830create_cus_from_debug_names_list (struct objfile *objfile,
5831 const mapped_debug_names &map,
5832 dwarf2_section_info &section,
5833 bool is_dwz, int base_offset)
5834{
5835 sect_offset sect_off_prev;
5836 for (uint32_t i = 0; i <= map.cu_count; ++i)
5837 {
5838 sect_offset sect_off_next;
5839 if (i < map.cu_count)
5840 {
5841 sect_off_next
5842 = (sect_offset) (extract_unsigned_integer
5843 (map.cu_table_reordered + i * map.offset_size,
5844 map.offset_size,
5845 map.dwarf5_byte_order));
5846 }
5847 else
5848 sect_off_next = (sect_offset) section.size;
5849 if (i >= 1)
5850 {
5851 const ULONGEST length = sect_off_next - sect_off_prev;
5852 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
5853 = create_cu_from_index_list (objfile, &section, is_dwz,
5854 sect_off_prev, length);
5855 }
5856 sect_off_prev = sect_off_next;
5857 }
5858}
5859
5860/* Read the CU list from the mapped index, and use it to create all
5861 the CU objects for this objfile. */
5862
5863static void
5864create_cus_from_debug_names (struct objfile *objfile,
5865 const mapped_debug_names &map,
5866 const mapped_debug_names &dwz_map)
5867{
5868
5869 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
5870 dwarf2_per_objfile->all_comp_units
5871 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
5872 dwarf2_per_objfile->n_comp_units);
5873
5874 create_cus_from_debug_names_list (objfile, map, dwarf2_per_objfile->info,
5875 false /* is_dwz */,
5876 0 /* base_offset */);
5877
5878 if (dwz_map.cu_count == 0)
5879 return;
5880
5881 dwz_file *dwz = dwarf2_get_dwz_file ();
5882 create_cus_from_debug_names_list (objfile, dwz_map, dwz->info,
5883 true /* is_dwz */,
5884 map.cu_count /* base_offset */);
5885}
5886
5887/* Read .debug_names. If everything went ok, initialize the "quick"
5888 elements of all the CUs and return true. Otherwise, return false. */
5889
5890static bool
5891dwarf2_read_debug_names (struct objfile *objfile)
5892{
5893 mapped_debug_names local_map, dwz_map;
5894
5895 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5896 &dwarf2_per_objfile->debug_names,
5897 local_map))
5898 return false;
5899
5900 /* Don't use the index if it's empty. */
5901 if (local_map.name_count == 0)
5902 return false;
5903
5904 /* If there is a .dwz file, read it so we can get its CU list as
5905 well. */
5906 dwz_file *dwz = dwarf2_get_dwz_file ();
5907 if (dwz != NULL)
5908 {
5909 if (!read_debug_names_from_section (objfile,
5910 bfd_get_filename (dwz->dwz_bfd),
5911 &dwz->debug_names, dwz_map))
5912 {
5913 warning (_("could not read '.debug_names' section from %s; skipping"),
5914 bfd_get_filename (dwz->dwz_bfd));
5915 return false;
5916 }
5917 }
5918
5919 create_cus_from_debug_names (objfile, local_map, dwz_map);
5920
5921 if (local_map.tu_count != 0)
5922 {
5923 /* We can only handle a single .debug_types when we have an
5924 index. */
5925 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5926 return false;
5927
5928 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5929 dwarf2_per_objfile->types, 0);
5930
5931 create_signatured_type_table_from_debug_names
5932 (objfile, local_map, section, &dwarf2_per_objfile->abbrev);
5933 }
5934
5935 create_addrmap_from_aranges (objfile, &dwarf2_per_objfile->debug_aranges);
5936
5937 dwarf2_per_objfile->debug_names_table.reset (new mapped_debug_names);
5938 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5939 dwarf2_per_objfile->using_index = 1;
5940 dwarf2_per_objfile->quick_file_names_table =
5941 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5942
5943 return true;
5944}
5945
5946/* Symbol name hashing function as specified by DWARF-5. */
5947
5948static uint32_t
5949dwarf5_djb_hash (const char *str_)
5950{
5951 const unsigned char *str = (const unsigned char *) str_;
5952
5953 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
5954 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
5955
5956 uint32_t hash = 5381;
5957 while (int c = *str++)
5958 hash = hash * 33 + tolower (c);
5959 return hash;
5960}
5961
5962/* Type used to manage iterating over all CUs looking for a symbol for
5963 .debug_names. */
5964
5965class dw2_debug_names_iterator
5966{
5967public:
5968 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5969 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5970 dw2_debug_names_iterator (const mapped_debug_names &map,
5971 bool want_specific_block,
5972 block_enum block_index, domain_enum domain,
5973 const char *name)
5974 : m_map (map), m_want_specific_block (want_specific_block),
5975 m_block_index (block_index), m_domain (domain),
5976 m_addr (find_vec_in_debug_names (map, name))
5977 {}
5978
5979 dw2_debug_names_iterator (const mapped_debug_names &map,
5980 search_domain search, uint32_t namei)
5981 : m_map (map),
5982 m_search (search),
5983 m_addr (find_vec_in_debug_names (map, namei))
5984 {}
5985
5986 /* Return the next matching CU or NULL if there are no more. */
5987 dwarf2_per_cu_data *next ();
5988
5989private:
5990 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5991 const char *name);
5992 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5993 uint32_t namei);
5994
5995 /* The internalized form of .debug_names. */
5996 const mapped_debug_names &m_map;
5997
5998 /* If true, only look for symbols that match BLOCK_INDEX. */
5999 const bool m_want_specific_block = false;
6000
6001 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6002 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6003 value. */
6004 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6005
6006 /* The kind of symbol we're looking for. */
6007 const domain_enum m_domain = UNDEF_DOMAIN;
6008 const search_domain m_search = ALL_DOMAIN;
6009
6010 /* The list of CUs from the index entry of the symbol, or NULL if
6011 not found. */
6012 const gdb_byte *m_addr;
6013};
6014
6015const char *
6016mapped_debug_names::namei_to_name (uint32_t namei) const
6017{
6018 const ULONGEST namei_string_offs
6019 = extract_unsigned_integer ((name_table_string_offs_reordered
6020 + namei * offset_size),
6021 offset_size,
6022 dwarf5_byte_order);
6023 return read_indirect_string_at_offset
6024 (dwarf2_per_objfile->objfile->obfd, namei_string_offs);
6025}
6026
6027/* Find a slot in .debug_names for the object named NAME. If NAME is
6028 found, return pointer to its pool data. If NAME cannot be found,
6029 return NULL. */
6030
6031const gdb_byte *
6032dw2_debug_names_iterator::find_vec_in_debug_names
6033 (const mapped_debug_names &map, const char *name)
6034{
6035 int (*cmp) (const char *, const char *);
6036
6037 if (current_language->la_language == language_cplus
6038 || current_language->la_language == language_fortran
6039 || current_language->la_language == language_d)
6040 {
6041 /* NAME is already canonical. Drop any qualifiers as
6042 .debug_names does not contain any. */
6043
6044 if (strchr (name, '(') != NULL)
6045 {
6046 gdb::unique_xmalloc_ptr<char> without_params
6047 = cp_remove_params (name);
6048
6049 if (without_params != NULL)
6050 {
6051 name = without_params.get();
6052 }
6053 }
6054 }
6055
6056 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6057
6058 const uint32_t full_hash = dwarf5_djb_hash (name);
6059 uint32_t namei
6060 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6061 (map.bucket_table_reordered
6062 + (full_hash % map.bucket_count)), 4,
6063 map.dwarf5_byte_order);
6064 if (namei == 0)
6065 return NULL;
6066 --namei;
6067 if (namei >= map.name_count)
6068 {
6069 complaint (&symfile_complaints,
6070 _("Wrong .debug_names with name index %u but name_count=%u "
6071 "[in module %s]"),
6072 namei, map.name_count,
6073 objfile_name (dwarf2_per_objfile->objfile));
6074 return NULL;
6075 }
6076
6077 for (;;)
6078 {
6079 const uint32_t namei_full_hash
6080 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6081 (map.hash_table_reordered + namei), 4,
6082 map.dwarf5_byte_order);
6083 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6084 return NULL;
6085
6086 if (full_hash == namei_full_hash)
6087 {
6088 const char *const namei_string = map.namei_to_name (namei);
6089
6090#if 0 /* An expensive sanity check. */
6091 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6092 {
6093 complaint (&symfile_complaints,
6094 _("Wrong .debug_names hash for string at index %u "
6095 "[in module %s]"),
6096 namei, objfile_name (dwarf2_per_objfile->objfile));
6097 return NULL;
6098 }
6099#endif
6100
6101 if (cmp (namei_string, name) == 0)
6102 {
6103 const ULONGEST namei_entry_offs
6104 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6105 + namei * map.offset_size),
6106 map.offset_size, map.dwarf5_byte_order);
6107 return map.entry_pool + namei_entry_offs;
6108 }
6109 }
6110
6111 ++namei;
6112 if (namei >= map.name_count)
6113 return NULL;
6114 }
6115}
6116
6117const gdb_byte *
6118dw2_debug_names_iterator::find_vec_in_debug_names
6119 (const mapped_debug_names &map, uint32_t namei)
6120{
6121 if (namei >= map.name_count)
6122 {
6123 complaint (&symfile_complaints,
6124 _("Wrong .debug_names with name index %u but name_count=%u "
6125 "[in module %s]"),
6126 namei, map.name_count,
6127 objfile_name (dwarf2_per_objfile->objfile));
6128 return NULL;
6129 }
6130
6131 const ULONGEST namei_entry_offs
6132 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6133 + namei * map.offset_size),
6134 map.offset_size, map.dwarf5_byte_order);
6135 return map.entry_pool + namei_entry_offs;
6136}
6137
6138/* See dw2_debug_names_iterator. */
6139
6140dwarf2_per_cu_data *
6141dw2_debug_names_iterator::next ()
6142{
6143 if (m_addr == NULL)
6144 return NULL;
6145
6146 bfd *const abfd = dwarf2_per_objfile->objfile->obfd;
6147
6148 again:
6149
6150 unsigned int bytes_read;
6151 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6152 m_addr += bytes_read;
6153 if (abbrev == 0)
6154 return NULL;
6155
6156 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6157 if (indexval_it == m_map.abbrev_map.cend ())
6158 {
6159 complaint (&symfile_complaints,
6160 _("Wrong .debug_names undefined abbrev code %s "
6161 "[in module %s]"),
6162 pulongest (abbrev), objfile_name (dwarf2_per_objfile->objfile));
6163 return NULL;
6164 }
6165 const mapped_debug_names::index_val &indexval = indexval_it->second;
6166 bool have_is_static = false;
6167 bool is_static;
6168 dwarf2_per_cu_data *per_cu = NULL;
6169 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6170 {
6171 ULONGEST ull;
6172 switch (attr.form)
6173 {
6174 case DW_FORM_implicit_const:
6175 ull = attr.implicit_const;
6176 break;
6177 case DW_FORM_flag_present:
6178 ull = 1;
6179 break;
6180 case DW_FORM_udata:
6181 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6182 m_addr += bytes_read;
6183 break;
6184 default:
6185 complaint (&symfile_complaints,
6186 _("Unsupported .debug_names form %s [in module %s]"),
6187 dwarf_form_name (attr.form),
6188 objfile_name (dwarf2_per_objfile->objfile));
6189 return NULL;
6190 }
6191 switch (attr.dw_idx)
6192 {
6193 case DW_IDX_compile_unit:
6194 /* Don't crash on bad data. */
8af5c486 6195 if (ull >= dwarf2_per_objfile->n_comp_units)
927aa2e7
JK
6196 {
6197 complaint (&symfile_complaints,
6198 _(".debug_names entry has bad CU index %s"
6199 " [in module %s]"),
6200 pulongest (ull),
6201 objfile_name (dwarf2_per_objfile->objfile));
6202 continue;
6203 }
6204 per_cu = dw2_get_cutu (ull);
6205 break;
8af5c486
JK
6206 case DW_IDX_type_unit:
6207 /* Don't crash on bad data. */
6208 if (ull >= dwarf2_per_objfile->n_type_units)
6209 {
6210 complaint (&symfile_complaints,
6211 _(".debug_names entry has bad TU index %s"
6212 " [in module %s]"),
6213 pulongest (ull),
6214 objfile_name (dwarf2_per_objfile->objfile));
6215 continue;
6216 }
6217 per_cu = dw2_get_cutu (dwarf2_per_objfile->n_comp_units + ull);
6218 break;
927aa2e7
JK
6219 case DW_IDX_GNU_internal:
6220 if (!m_map.augmentation_is_gdb)
6221 break;
6222 have_is_static = true;
6223 is_static = true;
6224 break;
6225 case DW_IDX_GNU_external:
6226 if (!m_map.augmentation_is_gdb)
6227 break;
6228 have_is_static = true;
6229 is_static = false;
6230 break;
6231 }
6232 }
6233
6234 /* Skip if already read in. */
6235 if (per_cu->v.quick->compunit_symtab)
6236 goto again;
6237
6238 /* Check static vs global. */
6239 if (have_is_static)
6240 {
6241 const bool want_static = m_block_index != GLOBAL_BLOCK;
6242 if (m_want_specific_block && want_static != is_static)
6243 goto again;
6244 }
6245
6246 /* Match dw2_symtab_iter_next, symbol_kind
6247 and debug_names::psymbol_tag. */
6248 switch (m_domain)
6249 {
6250 case VAR_DOMAIN:
6251 switch (indexval.dwarf_tag)
6252 {
6253 case DW_TAG_variable:
6254 case DW_TAG_subprogram:
6255 /* Some types are also in VAR_DOMAIN. */
6256 case DW_TAG_typedef:
6257 case DW_TAG_structure_type:
6258 break;
6259 default:
6260 goto again;
6261 }
6262 break;
6263 case STRUCT_DOMAIN:
6264 switch (indexval.dwarf_tag)
6265 {
6266 case DW_TAG_typedef:
6267 case DW_TAG_structure_type:
6268 break;
6269 default:
6270 goto again;
6271 }
6272 break;
6273 case LABEL_DOMAIN:
6274 switch (indexval.dwarf_tag)
6275 {
6276 case 0:
6277 case DW_TAG_variable:
6278 break;
6279 default:
6280 goto again;
6281 }
6282 break;
6283 default:
6284 break;
6285 }
6286
6287 /* Match dw2_expand_symtabs_matching, symbol_kind and
6288 debug_names::psymbol_tag. */
6289 switch (m_search)
4b514bc8 6290 {
927aa2e7
JK
6291 case VARIABLES_DOMAIN:
6292 switch (indexval.dwarf_tag)
4b514bc8 6293 {
927aa2e7
JK
6294 case DW_TAG_variable:
6295 break;
6296 default:
6297 goto again;
4b514bc8 6298 }
927aa2e7
JK
6299 break;
6300 case FUNCTIONS_DOMAIN:
6301 switch (indexval.dwarf_tag)
4b514bc8 6302 {
927aa2e7
JK
6303 case DW_TAG_subprogram:
6304 break;
6305 default:
6306 goto again;
4b514bc8 6307 }
927aa2e7
JK
6308 break;
6309 case TYPES_DOMAIN:
6310 switch (indexval.dwarf_tag)
6311 {
6312 case DW_TAG_typedef:
6313 case DW_TAG_structure_type:
6314 break;
6315 default:
6316 goto again;
6317 }
6318 break;
6319 default:
6320 break;
4b514bc8 6321 }
927aa2e7
JK
6322
6323 return per_cu;
4b514bc8 6324}
61920122 6325
927aa2e7
JK
6326static struct compunit_symtab *
6327dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6328 const char *name, domain_enum domain)
4b514bc8 6329{
927aa2e7 6330 const block_enum block_index = static_cast<block_enum> (block_index_int);
4b514bc8 6331 dw2_setup (objfile);
61920122 6332
927aa2e7
JK
6333 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6334 if (!mapp)
61920122 6335 {
927aa2e7
JK
6336 /* index is NULL if OBJF_READNOW. */
6337 return NULL;
6338 }
6339 const auto &map = *mapp;
9291a0cd 6340
927aa2e7
JK
6341 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6342 block_index, domain, name);
9703b513 6343
927aa2e7
JK
6344 struct compunit_symtab *stab_best = NULL;
6345 struct dwarf2_per_cu_data *per_cu;
6346 while ((per_cu = iter.next ()) != NULL)
6347 {
6348 struct symbol *sym, *with_opaque = NULL;
6349 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6350 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6351 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6352
927aa2e7
JK
6353 sym = block_find_symbol (block, name, domain,
6354 block_find_non_opaque_type_preferred,
6355 &with_opaque);
9703b513 6356
927aa2e7
JK
6357 /* Some caution must be observed with overloaded functions and
6358 methods, since the index will not contain any overload
6359 information (but NAME might contain it). */
a3ec0bb1 6360
927aa2e7
JK
6361 if (sym != NULL
6362 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6363 return stab;
6364 if (with_opaque != NULL
6365 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6366 stab_best = stab;
9703b513 6367
927aa2e7 6368 /* Keep looking through other CUs. */
9703b513
TT
6369 }
6370
927aa2e7 6371 return stab_best;
9703b513
TT
6372}
6373
927aa2e7
JK
6374/* This dumps minimal information about .debug_names. It is called
6375 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6376 uses this to verify that .debug_names has been loaded. */
9291a0cd 6377
927aa2e7
JK
6378static void
6379dw2_debug_names_dump (struct objfile *objfile)
6380{
9291a0cd 6381 dw2_setup (objfile);
927aa2e7
JK
6382 gdb_assert (dwarf2_per_objfile->using_index);
6383 printf_filtered (".debug_names:");
6384 if (dwarf2_per_objfile->debug_names_table)
6385 printf_filtered (" exists\n");
6386 else
6387 printf_filtered (" faked for \"readnow\"\n");
6388 printf_filtered ("\n");
9291a0cd
TT
6389}
6390
9291a0cd 6391static void
927aa2e7
JK
6392dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6393 const char *func_name)
9291a0cd 6394{
9291a0cd 6395 dw2_setup (objfile);
ae2de4f8 6396
927aa2e7
JK
6397 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6398 if (dwarf2_per_objfile->debug_names_table)
24c79950 6399 {
927aa2e7 6400 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6401
927aa2e7
JK
6402 /* Note: It doesn't matter what we pass for block_index here. */
6403 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6404 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
24c79950 6405
927aa2e7
JK
6406 struct dwarf2_per_cu_data *per_cu;
6407 while ((per_cu = iter.next ()) != NULL)
6408 dw2_instantiate_symtab (per_cu);
6409 }
6410}
24c79950 6411
927aa2e7
JK
6412static void
6413dw2_debug_names_expand_symtabs_matching
6414 (struct objfile *objfile,
6415 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6416 const lookup_name_info &lookup_name,
6417 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6418 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6419 enum search_domain kind)
6420{
6421 dw2_setup (objfile);
9291a0cd 6422
927aa2e7
JK
6423 /* debug_names_table is NULL if OBJF_READNOW. */
6424 if (!dwarf2_per_objfile->debug_names_table)
6425 return;
9291a0cd 6426
927aa2e7 6427 dw_expand_symtabs_matching_file_matcher (file_matcher);
24c79950 6428
44ed8f3e 6429 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6430
44ed8f3e
PA
6431 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6432 symbol_matcher,
6433 kind, [&] (offset_type namei)
927aa2e7 6434 {
927aa2e7
JK
6435 /* The name was matched, now expand corresponding CUs that were
6436 marked. */
6437 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6438
927aa2e7
JK
6439 struct dwarf2_per_cu_data *per_cu;
6440 while ((per_cu = iter.next ()) != NULL)
6441 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6442 expansion_notify);
44ed8f3e 6443 });
9291a0cd
TT
6444}
6445
927aa2e7 6446const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6447{
6448 dw2_has_symbols,
6449 dw2_find_last_source_symtab,
6450 dw2_forget_cached_source_info,
f8eba3c6 6451 dw2_map_symtabs_matching_filename,
927aa2e7 6452 dw2_debug_names_lookup_symbol,
9291a0cd 6453 dw2_print_stats,
927aa2e7 6454 dw2_debug_names_dump,
9291a0cd 6455 dw2_relocate,
927aa2e7 6456 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6457 dw2_expand_all_symtabs,
652a8996 6458 dw2_expand_symtabs_with_fullname,
40658b94 6459 dw2_map_matching_symbols,
927aa2e7 6460 dw2_debug_names_expand_symtabs_matching,
43f3e411 6461 dw2_find_pc_sect_compunit_symtab,
71a3c369 6462 NULL,
9291a0cd
TT
6463 dw2_map_symbol_filenames
6464};
6465
3c0aa29a 6466/* See symfile.h. */
9291a0cd 6467
3c0aa29a
PA
6468bool
6469dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd
TT
6470{
6471 /* If we're about to read full symbols, don't bother with the
6472 indices. In this case we also don't care if some other debug
6473 format is making psymtabs, because they are all about to be
6474 expanded anyway. */
6475 if ((objfile->flags & OBJF_READNOW))
6476 {
6477 int i;
6478
6479 dwarf2_per_objfile->using_index = 1;
6480 create_all_comp_units (objfile);
0e50663e 6481 create_all_type_units (objfile);
7b9f3c50
DE
6482 dwarf2_per_objfile->quick_file_names_table =
6483 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 6484
1fd400ff 6485 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 6486 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 6487 {
8832e7e3 6488 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 6489
e254ef6a
DE
6490 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6491 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6492 }
6493
6494 /* Return 1 so that gdb sees the "quick" functions. However,
6495 these functions will be no-ops because we will have expanded
6496 all symtabs. */
3c0aa29a
PA
6497 *index_kind = dw_index_kind::GDB_INDEX;
6498 return true;
9291a0cd
TT
6499 }
6500
927aa2e7 6501 if (dwarf2_read_debug_names (objfile))
3c0aa29a
PA
6502 {
6503 *index_kind = dw_index_kind::DEBUG_NAMES;
6504 return true;
6505 }
927aa2e7 6506
9291a0cd 6507 if (dwarf2_read_index (objfile))
3c0aa29a
PA
6508 {
6509 *index_kind = dw_index_kind::GDB_INDEX;
6510 return true;
6511 }
9291a0cd 6512
3c0aa29a 6513 return false;
9291a0cd
TT
6514}
6515
6516\f
6517
dce234bc
PP
6518/* Build a partial symbol table. */
6519
6520void
f29dff0a 6521dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6522{
c9bf0622 6523
af5bf4ad
SM
6524 if (objfile->global_psymbols.capacity () == 0
6525 && objfile->static_psymbols.capacity () == 0)
6526 init_psymbol_list (objfile, 1024);
c906108c 6527
492d29ea 6528 TRY
c9bf0622
TT
6529 {
6530 /* This isn't really ideal: all the data we allocate on the
6531 objfile's obstack is still uselessly kept around. However,
6532 freeing it seems unsafe. */
906768f9 6533 psymtab_discarder psymtabs (objfile);
c9bf0622 6534 dwarf2_build_psymtabs_hard (objfile);
906768f9 6535 psymtabs.keep ();
c9bf0622 6536 }
492d29ea
PA
6537 CATCH (except, RETURN_MASK_ERROR)
6538 {
6539 exception_print (gdb_stderr, except);
6540 }
6541 END_CATCH
c906108c 6542}
c906108c 6543
1ce1cefd
DE
6544/* Return the total length of the CU described by HEADER. */
6545
6546static unsigned int
6547get_cu_length (const struct comp_unit_head *header)
6548{
6549 return header->initial_length_size + header->length;
6550}
6551
9c541725 6552/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6553
9c541725
PA
6554static inline bool
6555offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6556{
9c541725
PA
6557 sect_offset bottom = cu_header->sect_off;
6558 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6559
9c541725 6560 return sect_off >= bottom && sect_off < top;
45452591
DE
6561}
6562
3b80fe9b
DE
6563/* Find the base address of the compilation unit for range lists and
6564 location lists. It will normally be specified by DW_AT_low_pc.
6565 In DWARF-3 draft 4, the base address could be overridden by
6566 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6567 compilation units with discontinuous ranges. */
6568
6569static void
6570dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6571{
6572 struct attribute *attr;
6573
6574 cu->base_known = 0;
6575 cu->base_address = 0;
6576
6577 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6578 if (attr)
6579 {
31aa7e4e 6580 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6581 cu->base_known = 1;
6582 }
6583 else
6584 {
6585 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6586 if (attr)
6587 {
31aa7e4e 6588 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6589 cu->base_known = 1;
6590 }
6591 }
6592}
6593
93311388 6594/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6595 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6596 NOTE: This leaves members offset, first_die_offset to be filled in
6597 by the caller. */
107d2387 6598
d521ce57 6599static const gdb_byte *
107d2387 6600read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6601 const gdb_byte *info_ptr,
6602 struct dwarf2_section_info *section,
6603 rcuh_kind section_kind)
107d2387
AC
6604{
6605 int signed_addr;
891d2f0b 6606 unsigned int bytes_read;
43988095
JK
6607 const char *filename = get_section_file_name (section);
6608 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6609
6610 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6611 cu_header->initial_length_size = bytes_read;
6612 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6613 info_ptr += bytes_read;
107d2387
AC
6614 cu_header->version = read_2_bytes (abfd, info_ptr);
6615 info_ptr += 2;
43988095
JK
6616 if (cu_header->version < 5)
6617 switch (section_kind)
6618 {
6619 case rcuh_kind::COMPILE:
6620 cu_header->unit_type = DW_UT_compile;
6621 break;
6622 case rcuh_kind::TYPE:
6623 cu_header->unit_type = DW_UT_type;
6624 break;
6625 default:
6626 internal_error (__FILE__, __LINE__,
6627 _("read_comp_unit_head: invalid section_kind"));
6628 }
6629 else
6630 {
6631 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6632 (read_1_byte (abfd, info_ptr));
6633 info_ptr += 1;
6634 switch (cu_header->unit_type)
6635 {
6636 case DW_UT_compile:
6637 if (section_kind != rcuh_kind::COMPILE)
6638 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6639 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6640 filename);
6641 break;
6642 case DW_UT_type:
6643 section_kind = rcuh_kind::TYPE;
6644 break;
6645 default:
6646 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6647 "(is %d, should be %d or %d) [in module %s]"),
6648 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6649 }
6650
6651 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6652 info_ptr += 1;
6653 }
9c541725
PA
6654 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6655 cu_header,
6656 &bytes_read);
613e1657 6657 info_ptr += bytes_read;
43988095
JK
6658 if (cu_header->version < 5)
6659 {
6660 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6661 info_ptr += 1;
6662 }
107d2387
AC
6663 signed_addr = bfd_get_sign_extend_vma (abfd);
6664 if (signed_addr < 0)
8e65ff28 6665 internal_error (__FILE__, __LINE__,
e2e0b3e5 6666 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6667 cu_header->signed_addr_p = signed_addr;
c764a876 6668
43988095
JK
6669 if (section_kind == rcuh_kind::TYPE)
6670 {
6671 LONGEST type_offset;
6672
6673 cu_header->signature = read_8_bytes (abfd, info_ptr);
6674 info_ptr += 8;
6675
6676 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6677 info_ptr += bytes_read;
9c541725
PA
6678 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6679 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6680 error (_("Dwarf Error: Too big type_offset in compilation unit "
6681 "header (is %s) [in module %s]"), plongest (type_offset),
6682 filename);
6683 }
6684
107d2387
AC
6685 return info_ptr;
6686}
6687
36586728
TT
6688/* Helper function that returns the proper abbrev section for
6689 THIS_CU. */
6690
6691static struct dwarf2_section_info *
6692get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6693{
6694 struct dwarf2_section_info *abbrev;
6695
6696 if (this_cu->is_dwz)
6697 abbrev = &dwarf2_get_dwz_file ()->abbrev;
6698 else
6699 abbrev = &dwarf2_per_objfile->abbrev;
6700
6701 return abbrev;
6702}
6703
9ff913ba
DE
6704/* Subroutine of read_and_check_comp_unit_head and
6705 read_and_check_type_unit_head to simplify them.
6706 Perform various error checking on the header. */
6707
6708static void
6709error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
6710 struct dwarf2_section_info *section,
6711 struct dwarf2_section_info *abbrev_section)
9ff913ba 6712{
a32a8923 6713 const char *filename = get_section_file_name (section);
9ff913ba 6714
43988095 6715 if (header->version < 2 || header->version > 5)
9ff913ba 6716 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 6717 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
6718 filename);
6719
9c541725 6720 if (to_underlying (header->abbrev_sect_off)
36586728 6721 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9c541725
PA
6722 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
6723 "(offset 0x%x + 6) [in module %s]"),
6724 to_underlying (header->abbrev_sect_off),
6725 to_underlying (header->sect_off),
9ff913ba
DE
6726 filename);
6727
9c541725 6728 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6729 avoid potential 32-bit overflow. */
9c541725 6730 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6731 > section->size)
9c541725
PA
6732 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6733 "(offset 0x%x + 0) [in module %s]"),
6734 header->length, to_underlying (header->sect_off),
9ff913ba
DE
6735 filename);
6736}
6737
6738/* Read in a CU/TU header and perform some basic error checking.
6739 The contents of the header are stored in HEADER.
6740 The result is a pointer to the start of the first DIE. */
adabb602 6741
d521ce57 6742static const gdb_byte *
9ff913ba
DE
6743read_and_check_comp_unit_head (struct comp_unit_head *header,
6744 struct dwarf2_section_info *section,
4bdcc0c1 6745 struct dwarf2_section_info *abbrev_section,
d521ce57 6746 const gdb_byte *info_ptr,
43988095 6747 rcuh_kind section_kind)
72bf9492 6748{
d521ce57 6749 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6750
9c541725 6751 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6752
43988095 6753 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6754
9c541725 6755 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6756
4bdcc0c1 6757 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
6758
6759 return info_ptr;
348e048f
DE
6760}
6761
f4dc4d17
DE
6762/* Fetch the abbreviation table offset from a comp or type unit header. */
6763
6764static sect_offset
6765read_abbrev_offset (struct dwarf2_section_info *section,
9c541725 6766 sect_offset sect_off)
f4dc4d17 6767{
a32a8923 6768 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6769 const gdb_byte *info_ptr;
ac298888 6770 unsigned int initial_length_size, offset_size;
43988095 6771 uint16_t version;
f4dc4d17
DE
6772
6773 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6774 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6775 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6776 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6777 info_ptr += initial_length_size;
6778
6779 version = read_2_bytes (abfd, info_ptr);
6780 info_ptr += 2;
6781 if (version >= 5)
6782 {
6783 /* Skip unit type and address size. */
6784 info_ptr += 2;
6785 }
6786
9c541725 6787 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6788}
6789
aaa75496
JB
6790/* Allocate a new partial symtab for file named NAME and mark this new
6791 partial symtab as being an include of PST. */
6792
6793static void
d521ce57 6794dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6795 struct objfile *objfile)
6796{
6797 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6798
fbd9ab74
JK
6799 if (!IS_ABSOLUTE_PATH (subpst->filename))
6800 {
6801 /* It shares objfile->objfile_obstack. */
6802 subpst->dirname = pst->dirname;
6803 }
6804
aaa75496
JB
6805 subpst->textlow = 0;
6806 subpst->texthigh = 0;
6807
8d749320
SM
6808 subpst->dependencies
6809 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
6810 subpst->dependencies[0] = pst;
6811 subpst->number_of_dependencies = 1;
6812
6813 subpst->globals_offset = 0;
6814 subpst->n_global_syms = 0;
6815 subpst->statics_offset = 0;
6816 subpst->n_static_syms = 0;
43f3e411 6817 subpst->compunit_symtab = NULL;
aaa75496
JB
6818 subpst->read_symtab = pst->read_symtab;
6819 subpst->readin = 0;
6820
6821 /* No private part is necessary for include psymtabs. This property
6822 can be used to differentiate between such include psymtabs and
10b3939b 6823 the regular ones. */
58a9656e 6824 subpst->read_symtab_private = NULL;
aaa75496
JB
6825}
6826
6827/* Read the Line Number Program data and extract the list of files
6828 included by the source file represented by PST. Build an include
d85a05f0 6829 partial symtab for each of these included files. */
aaa75496
JB
6830
6831static void
6832dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6833 struct die_info *die,
6834 struct partial_symtab *pst)
aaa75496 6835{
fff8551c 6836 line_header_up lh;
d85a05f0 6837 struct attribute *attr;
aaa75496 6838
d85a05f0
DJ
6839 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6840 if (attr)
9c541725 6841 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6842 if (lh == NULL)
6843 return; /* No linetable, so no includes. */
6844
c6da4cef 6845 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 6846 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
6847}
6848
348e048f 6849static hashval_t
52dc124a 6850hash_signatured_type (const void *item)
348e048f 6851{
9a3c8263
SM
6852 const struct signatured_type *sig_type
6853 = (const struct signatured_type *) item;
9a619af0 6854
348e048f 6855 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6856 return sig_type->signature;
348e048f
DE
6857}
6858
6859static int
52dc124a 6860eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6861{
9a3c8263
SM
6862 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6863 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6864
348e048f
DE
6865 return lhs->signature == rhs->signature;
6866}
6867
1fd400ff
TT
6868/* Allocate a hash table for signatured types. */
6869
6870static htab_t
673bfd45 6871allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6872{
6873 return htab_create_alloc_ex (41,
52dc124a
DE
6874 hash_signatured_type,
6875 eq_signatured_type,
1fd400ff
TT
6876 NULL,
6877 &objfile->objfile_obstack,
6878 hashtab_obstack_allocate,
6879 dummy_obstack_deallocate);
6880}
6881
d467dd73 6882/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6883
6884static int
d467dd73 6885add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6886{
9a3c8263
SM
6887 struct signatured_type *sigt = (struct signatured_type *) *slot;
6888 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 6889
b4dd5633 6890 **datap = sigt;
1fd400ff
TT
6891 ++*datap;
6892
6893 return 1;
6894}
6895
78d4d2c5 6896/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6897 and fill them into TYPES_HTAB. It will process only type units,
6898 therefore DW_UT_type. */
c88ee1f0 6899
78d4d2c5
JK
6900static void
6901create_debug_type_hash_table (struct dwo_file *dwo_file,
43988095
JK
6902 dwarf2_section_info *section, htab_t &types_htab,
6903 rcuh_kind section_kind)
348e048f 6904{
3019eac3 6905 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6906 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6907 bfd *abfd;
6908 const gdb_byte *info_ptr, *end_ptr;
348e048f 6909
4bdcc0c1
DE
6910 abbrev_section = (dwo_file != NULL
6911 ? &dwo_file->sections.abbrev
6912 : &dwarf2_per_objfile->abbrev);
6913
b4f54984 6914 if (dwarf_read_debug)
43988095
JK
6915 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6916 get_section_name (section),
a32a8923 6917 get_section_file_name (abbrev_section));
09406207 6918
78d4d2c5
JK
6919 dwarf2_read_section (objfile, section);
6920 info_ptr = section->buffer;
348e048f 6921
78d4d2c5
JK
6922 if (info_ptr == NULL)
6923 return;
348e048f 6924
78d4d2c5
JK
6925 /* We can't set abfd until now because the section may be empty or
6926 not present, in which case the bfd is unknown. */
6927 abfd = get_section_bfd_owner (section);
348e048f 6928
78d4d2c5
JK
6929 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6930 because we don't need to read any dies: the signature is in the
6931 header. */
3019eac3 6932
78d4d2c5
JK
6933 end_ptr = info_ptr + section->size;
6934 while (info_ptr < end_ptr)
6935 {
78d4d2c5
JK
6936 struct signatured_type *sig_type;
6937 struct dwo_unit *dwo_tu;
6938 void **slot;
6939 const gdb_byte *ptr = info_ptr;
6940 struct comp_unit_head header;
6941 unsigned int length;
8b70b953 6942
9c541725 6943 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6944
a49dd8dd
JK
6945 /* Initialize it due to a false compiler warning. */
6946 header.signature = -1;
9c541725 6947 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6948
78d4d2c5
JK
6949 /* We need to read the type's signature in order to build the hash
6950 table, but we don't need anything else just yet. */
348e048f 6951
43988095
JK
6952 ptr = read_and_check_comp_unit_head (&header, section,
6953 abbrev_section, ptr, section_kind);
348e048f 6954
78d4d2c5 6955 length = get_cu_length (&header);
6caca83c 6956
78d4d2c5
JK
6957 /* Skip dummy type units. */
6958 if (ptr >= info_ptr + length
43988095
JK
6959 || peek_abbrev_code (abfd, ptr) == 0
6960 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6961 {
6962 info_ptr += length;
6963 continue;
6964 }
dee91e82 6965
78d4d2c5
JK
6966 if (types_htab == NULL)
6967 {
6968 if (dwo_file)
6969 types_htab = allocate_dwo_unit_table (objfile);
6970 else
6971 types_htab = allocate_signatured_type_table (objfile);
6972 }
8b70b953 6973
78d4d2c5
JK
6974 if (dwo_file)
6975 {
6976 sig_type = NULL;
6977 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6978 struct dwo_unit);
6979 dwo_tu->dwo_file = dwo_file;
43988095 6980 dwo_tu->signature = header.signature;
9c541725 6981 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6982 dwo_tu->section = section;
9c541725 6983 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6984 dwo_tu->length = length;
6985 }
6986 else
6987 {
6988 /* N.B.: type_offset is not usable if this type uses a DWO file.
6989 The real type_offset is in the DWO file. */
6990 dwo_tu = NULL;
6991 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6992 struct signatured_type);
43988095 6993 sig_type->signature = header.signature;
9c541725 6994 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6995 sig_type->per_cu.objfile = objfile;
6996 sig_type->per_cu.is_debug_types = 1;
6997 sig_type->per_cu.section = section;
9c541725 6998 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6999 sig_type->per_cu.length = length;
7000 }
7001
7002 slot = htab_find_slot (types_htab,
7003 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7004 INSERT);
7005 gdb_assert (slot != NULL);
7006 if (*slot != NULL)
7007 {
9c541725 7008 sect_offset dup_sect_off;
0349ea22 7009
3019eac3
DE
7010 if (dwo_file)
7011 {
78d4d2c5
JK
7012 const struct dwo_unit *dup_tu
7013 = (const struct dwo_unit *) *slot;
7014
9c541725 7015 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
7016 }
7017 else
7018 {
78d4d2c5
JK
7019 const struct signatured_type *dup_tu
7020 = (const struct signatured_type *) *slot;
7021
9c541725 7022 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 7023 }
8b70b953 7024
78d4d2c5
JK
7025 complaint (&symfile_complaints,
7026 _("debug type entry at offset 0x%x is duplicate to"
7027 " the entry at offset 0x%x, signature %s"),
9c541725 7028 to_underlying (sect_off), to_underlying (dup_sect_off),
43988095 7029 hex_string (header.signature));
78d4d2c5
JK
7030 }
7031 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 7032
78d4d2c5
JK
7033 if (dwarf_read_debug > 1)
7034 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
9c541725 7035 to_underlying (sect_off),
43988095 7036 hex_string (header.signature));
3019eac3 7037
78d4d2c5
JK
7038 info_ptr += length;
7039 }
7040}
3019eac3 7041
78d4d2c5
JK
7042/* Create the hash table of all entries in the .debug_types
7043 (or .debug_types.dwo) section(s).
7044 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7045 otherwise it is NULL.
b3c8eb43 7046
78d4d2c5 7047 The result is a pointer to the hash table or NULL if there are no types.
348e048f 7048
78d4d2c5 7049 Note: This function processes DWO files only, not DWP files. */
348e048f 7050
78d4d2c5
JK
7051static void
7052create_debug_types_hash_table (struct dwo_file *dwo_file,
7053 VEC (dwarf2_section_info_def) *types,
7054 htab_t &types_htab)
7055{
7056 int ix;
7057 struct dwarf2_section_info *section;
7058
7059 if (VEC_empty (dwarf2_section_info_def, types))
7060 return;
348e048f 7061
78d4d2c5
JK
7062 for (ix = 0;
7063 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7064 ++ix)
43988095
JK
7065 create_debug_type_hash_table (dwo_file, section, types_htab,
7066 rcuh_kind::TYPE);
3019eac3
DE
7067}
7068
7069/* Create the hash table of all entries in the .debug_types section,
7070 and initialize all_type_units.
7071 The result is zero if there is an error (e.g. missing .debug_types section),
7072 otherwise non-zero. */
7073
7074static int
7075create_all_type_units (struct objfile *objfile)
7076{
78d4d2c5 7077 htab_t types_htab = NULL;
b4dd5633 7078 struct signatured_type **iter;
3019eac3 7079
43988095
JK
7080 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
7081 rcuh_kind::COMPILE);
78d4d2c5 7082 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
3019eac3
DE
7083 if (types_htab == NULL)
7084 {
7085 dwarf2_per_objfile->signatured_types = NULL;
7086 return 0;
7087 }
7088
348e048f
DE
7089 dwarf2_per_objfile->signatured_types = types_htab;
7090
6aa5f3a6
DE
7091 dwarf2_per_objfile->n_type_units
7092 = dwarf2_per_objfile->n_allocated_type_units
7093 = htab_elements (types_htab);
8d749320
SM
7094 dwarf2_per_objfile->all_type_units =
7095 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
7096 iter = &dwarf2_per_objfile->all_type_units[0];
7097 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7098 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7099 == dwarf2_per_objfile->n_type_units);
1fd400ff 7100
348e048f
DE
7101 return 1;
7102}
7103
6aa5f3a6
DE
7104/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7105 If SLOT is non-NULL, it is the entry to use in the hash table.
7106 Otherwise we find one. */
7107
7108static struct signatured_type *
7109add_type_unit (ULONGEST sig, void **slot)
7110{
7111 struct objfile *objfile = dwarf2_per_objfile->objfile;
7112 int n_type_units = dwarf2_per_objfile->n_type_units;
7113 struct signatured_type *sig_type;
7114
7115 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7116 ++n_type_units;
7117 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7118 {
7119 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7120 dwarf2_per_objfile->n_allocated_type_units = 1;
7121 dwarf2_per_objfile->n_allocated_type_units *= 2;
7122 dwarf2_per_objfile->all_type_units
224c3ddb
SM
7123 = XRESIZEVEC (struct signatured_type *,
7124 dwarf2_per_objfile->all_type_units,
7125 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
7126 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7127 }
7128 dwarf2_per_objfile->n_type_units = n_type_units;
7129
7130 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7131 struct signatured_type);
7132 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7133 sig_type->signature = sig;
7134 sig_type->per_cu.is_debug_types = 1;
7135 if (dwarf2_per_objfile->using_index)
7136 {
7137 sig_type->per_cu.v.quick =
7138 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7139 struct dwarf2_per_cu_quick_data);
7140 }
7141
7142 if (slot == NULL)
7143 {
7144 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7145 sig_type, INSERT);
7146 }
7147 gdb_assert (*slot == NULL);
7148 *slot = sig_type;
7149 /* The rest of sig_type must be filled in by the caller. */
7150 return sig_type;
7151}
7152
a2ce51a0
DE
7153/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7154 Fill in SIG_ENTRY with DWO_ENTRY. */
7155
7156static void
7157fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
7158 struct signatured_type *sig_entry,
7159 struct dwo_unit *dwo_entry)
7160{
7ee85ab1 7161 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
7162 gdb_assert (! sig_entry->per_cu.queued);
7163 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
7164 if (dwarf2_per_objfile->using_index)
7165 {
7166 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 7167 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
7168 }
7169 else
7170 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 7171 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 7172 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 7173 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
7174 gdb_assert (sig_entry->dwo_unit == NULL);
7175
7176 sig_entry->per_cu.section = dwo_entry->section;
9c541725 7177 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
7178 sig_entry->per_cu.length = dwo_entry->length;
7179 sig_entry->per_cu.reading_dwo_directly = 1;
7180 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
7181 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7182 sig_entry->dwo_unit = dwo_entry;
7183}
7184
7185/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
7186 If we haven't read the TU yet, create the signatured_type data structure
7187 for a TU to be read in directly from a DWO file, bypassing the stub.
7188 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7189 using .gdb_index, then when reading a CU we want to stay in the DWO file
7190 containing that CU. Otherwise we could end up reading several other DWO
7191 files (due to comdat folding) to process the transitive closure of all the
7192 mentioned TUs, and that can be slow. The current DWO file will have every
7193 type signature that it needs.
a2ce51a0
DE
7194 We only do this for .gdb_index because in the psymtab case we already have
7195 to read all the DWOs to build the type unit groups. */
7196
7197static struct signatured_type *
7198lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7199{
7200 struct objfile *objfile = dwarf2_per_objfile->objfile;
7201 struct dwo_file *dwo_file;
7202 struct dwo_unit find_dwo_entry, *dwo_entry;
7203 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7204 void **slot;
a2ce51a0
DE
7205
7206 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7207
6aa5f3a6
DE
7208 /* If TU skeletons have been removed then we may not have read in any
7209 TUs yet. */
7210 if (dwarf2_per_objfile->signatured_types == NULL)
7211 {
7212 dwarf2_per_objfile->signatured_types
7213 = allocate_signatured_type_table (objfile);
7214 }
a2ce51a0
DE
7215
7216 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7217 Use the global signatured_types array to do our own comdat-folding
7218 of types. If this is the first time we're reading this TU, and
7219 the TU has an entry in .gdb_index, replace the recorded data from
7220 .gdb_index with this TU. */
a2ce51a0 7221
a2ce51a0 7222 find_sig_entry.signature = sig;
6aa5f3a6
DE
7223 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7224 &find_sig_entry, INSERT);
9a3c8263 7225 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7226
7227 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7228 read. Don't reassign the global entry to point to this DWO if that's
7229 the case. Also note that if the TU is already being read, it may not
7230 have come from a DWO, the program may be a mix of Fission-compiled
7231 code and non-Fission-compiled code. */
7232
7233 /* Have we already tried to read this TU?
7234 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7235 needn't exist in the global table yet). */
7236 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7237 return sig_entry;
7238
6aa5f3a6
DE
7239 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7240 dwo_unit of the TU itself. */
7241 dwo_file = cu->dwo_unit->dwo_file;
7242
a2ce51a0
DE
7243 /* Ok, this is the first time we're reading this TU. */
7244 if (dwo_file->tus == NULL)
7245 return NULL;
7246 find_dwo_entry.signature = sig;
9a3c8263 7247 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7248 if (dwo_entry == NULL)
7249 return NULL;
7250
6aa5f3a6
DE
7251 /* If the global table doesn't have an entry for this TU, add one. */
7252 if (sig_entry == NULL)
7253 sig_entry = add_type_unit (sig, slot);
7254
a2ce51a0 7255 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 7256 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7257 return sig_entry;
7258}
7259
a2ce51a0
DE
7260/* Subroutine of lookup_signatured_type.
7261 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7262 then try the DWP file. If the TU stub (skeleton) has been removed then
7263 it won't be in .gdb_index. */
a2ce51a0
DE
7264
7265static struct signatured_type *
7266lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7267{
7268 struct objfile *objfile = dwarf2_per_objfile->objfile;
7269 struct dwp_file *dwp_file = get_dwp_file ();
7270 struct dwo_unit *dwo_entry;
7271 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7272 void **slot;
a2ce51a0
DE
7273
7274 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7275 gdb_assert (dwp_file != NULL);
7276
6aa5f3a6
DE
7277 /* If TU skeletons have been removed then we may not have read in any
7278 TUs yet. */
7279 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7280 {
6aa5f3a6
DE
7281 dwarf2_per_objfile->signatured_types
7282 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7283 }
7284
6aa5f3a6
DE
7285 find_sig_entry.signature = sig;
7286 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7287 &find_sig_entry, INSERT);
9a3c8263 7288 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7289
7290 /* Have we already tried to read this TU?
7291 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7292 needn't exist in the global table yet). */
7293 if (sig_entry != NULL)
7294 return sig_entry;
7295
a2ce51a0
DE
7296 if (dwp_file->tus == NULL)
7297 return NULL;
57d63ce2
DE
7298 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
7299 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7300 if (dwo_entry == NULL)
7301 return NULL;
7302
6aa5f3a6 7303 sig_entry = add_type_unit (sig, slot);
a2ce51a0
DE
7304 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
7305
a2ce51a0
DE
7306 return sig_entry;
7307}
7308
380bca97 7309/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7310 Returns NULL if signature SIG is not present in the table.
7311 It is up to the caller to complain about this. */
348e048f
DE
7312
7313static struct signatured_type *
a2ce51a0 7314lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7315{
a2ce51a0
DE
7316 if (cu->dwo_unit
7317 && dwarf2_per_objfile->using_index)
7318 {
7319 /* We're in a DWO/DWP file, and we're using .gdb_index.
7320 These cases require special processing. */
7321 if (get_dwp_file () == NULL)
7322 return lookup_dwo_signatured_type (cu, sig);
7323 else
7324 return lookup_dwp_signatured_type (cu, sig);
7325 }
7326 else
7327 {
7328 struct signatured_type find_entry, *entry;
348e048f 7329
a2ce51a0
DE
7330 if (dwarf2_per_objfile->signatured_types == NULL)
7331 return NULL;
7332 find_entry.signature = sig;
9a3c8263
SM
7333 entry = ((struct signatured_type *)
7334 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7335 return entry;
7336 }
348e048f 7337}
42e7ad6c
DE
7338\f
7339/* Low level DIE reading support. */
348e048f 7340
d85a05f0
DJ
7341/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7342
7343static void
7344init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7345 struct dwarf2_cu *cu,
3019eac3
DE
7346 struct dwarf2_section_info *section,
7347 struct dwo_file *dwo_file)
d85a05f0 7348{
fceca515 7349 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7350 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7351 reader->cu = cu;
3019eac3 7352 reader->dwo_file = dwo_file;
dee91e82
DE
7353 reader->die_section = section;
7354 reader->buffer = section->buffer;
f664829e 7355 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7356 reader->comp_dir = NULL;
d85a05f0
DJ
7357}
7358
b0c7bfa9
DE
7359/* Subroutine of init_cutu_and_read_dies to simplify it.
7360 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7361 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7362 already.
7363
7364 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7365 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7366 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7367 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7368 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7369 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7370 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7371 are filled in with the info of the DIE from the DWO file.
7372 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
7373 provided an abbrev table to use.
7374 The result is non-zero if a valid (non-dummy) DIE was found. */
7375
7376static int
7377read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7378 struct dwo_unit *dwo_unit,
7379 int abbrev_table_provided,
7380 struct die_info *stub_comp_unit_die,
a2ce51a0 7381 const char *stub_comp_dir,
b0c7bfa9 7382 struct die_reader_specs *result_reader,
d521ce57 7383 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
7384 struct die_info **result_comp_unit_die,
7385 int *result_has_children)
7386{
7387 struct objfile *objfile = dwarf2_per_objfile->objfile;
7388 struct dwarf2_cu *cu = this_cu->cu;
7389 struct dwarf2_section_info *section;
7390 bfd *abfd;
d521ce57 7391 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7392 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7393 int i,num_extra_attrs;
7394 struct dwarf2_section_info *dwo_abbrev_section;
7395 struct attribute *attr;
7396 struct die_info *comp_unit_die;
7397
b0aeadb3
DE
7398 /* At most one of these may be provided. */
7399 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7400
b0c7bfa9
DE
7401 /* These attributes aren't processed until later:
7402 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7403 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7404 referenced later. However, these attributes are found in the stub
7405 which we won't have later. In order to not impose this complication
7406 on the rest of the code, we read them here and copy them to the
7407 DWO CU/TU die. */
b0c7bfa9
DE
7408
7409 stmt_list = NULL;
7410 low_pc = NULL;
7411 high_pc = NULL;
7412 ranges = NULL;
7413 comp_dir = NULL;
7414
7415 if (stub_comp_unit_die != NULL)
7416 {
7417 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7418 DWO file. */
7419 if (! this_cu->is_debug_types)
7420 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7421 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7422 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7423 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7424 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7425
7426 /* There should be a DW_AT_addr_base attribute here (if needed).
7427 We need the value before we can process DW_FORM_GNU_addr_index. */
7428 cu->addr_base = 0;
7429 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7430 if (attr)
7431 cu->addr_base = DW_UNSND (attr);
7432
7433 /* There should be a DW_AT_ranges_base attribute here (if needed).
7434 We need the value before we can process DW_AT_ranges. */
7435 cu->ranges_base = 0;
7436 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7437 if (attr)
7438 cu->ranges_base = DW_UNSND (attr);
7439 }
a2ce51a0
DE
7440 else if (stub_comp_dir != NULL)
7441 {
7442 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7443 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7444 comp_dir->name = DW_AT_comp_dir;
7445 comp_dir->form = DW_FORM_string;
7446 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7447 DW_STRING (comp_dir) = stub_comp_dir;
7448 }
b0c7bfa9
DE
7449
7450 /* Set up for reading the DWO CU/TU. */
7451 cu->dwo_unit = dwo_unit;
7452 section = dwo_unit->section;
7453 dwarf2_read_section (objfile, section);
a32a8923 7454 abfd = get_section_bfd_owner (section);
9c541725
PA
7455 begin_info_ptr = info_ptr = (section->buffer
7456 + to_underlying (dwo_unit->sect_off));
b0c7bfa9
DE
7457 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7458 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
7459
7460 if (this_cu->is_debug_types)
7461 {
b0c7bfa9
DE
7462 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7463
43988095 7464 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
b0c7bfa9 7465 dwo_abbrev_section,
43988095 7466 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7467 /* This is not an assert because it can be caused by bad debug info. */
43988095 7468 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7469 {
7470 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7471 " TU at offset 0x%x [in module %s]"),
7472 hex_string (sig_type->signature),
43988095 7473 hex_string (cu->header.signature),
9c541725 7474 to_underlying (dwo_unit->sect_off),
a2ce51a0
DE
7475 bfd_get_filename (abfd));
7476 }
9c541725 7477 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7478 /* For DWOs coming from DWP files, we don't know the CU length
7479 nor the type's offset in the TU until now. */
7480 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7481 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7482
7483 /* Establish the type offset that can be used to lookup the type.
7484 For DWO files, we don't know it until now. */
9c541725
PA
7485 sig_type->type_offset_in_section
7486 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7487 }
7488 else
7489 {
7490 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
7491 dwo_abbrev_section,
43988095 7492 info_ptr, rcuh_kind::COMPILE);
9c541725 7493 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7494 /* For DWOs coming from DWP files, we don't know the CU length
7495 until now. */
7496 dwo_unit->length = get_cu_length (&cu->header);
7497 }
7498
02142a6c
DE
7499 /* Replace the CU's original abbrev table with the DWO's.
7500 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
7501 if (abbrev_table_provided)
7502 {
7503 /* Don't free the provided abbrev table, the caller of
7504 init_cutu_and_read_dies owns it. */
7505 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 7506 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
7507 make_cleanup (dwarf2_free_abbrev_table, cu);
7508 }
7509 else
7510 {
7511 dwarf2_free_abbrev_table (cu);
7512 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 7513 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
7514 }
7515
7516 /* Read in the die, but leave space to copy over the attributes
7517 from the stub. This has the benefit of simplifying the rest of
7518 the code - all the work to maintain the illusion of a single
7519 DW_TAG_{compile,type}_unit DIE is done here. */
7520 num_extra_attrs = ((stmt_list != NULL)
7521 + (low_pc != NULL)
7522 + (high_pc != NULL)
7523 + (ranges != NULL)
7524 + (comp_dir != NULL));
7525 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7526 result_has_children, num_extra_attrs);
7527
7528 /* Copy over the attributes from the stub to the DIE we just read in. */
7529 comp_unit_die = *result_comp_unit_die;
7530 i = comp_unit_die->num_attrs;
7531 if (stmt_list != NULL)
7532 comp_unit_die->attrs[i++] = *stmt_list;
7533 if (low_pc != NULL)
7534 comp_unit_die->attrs[i++] = *low_pc;
7535 if (high_pc != NULL)
7536 comp_unit_die->attrs[i++] = *high_pc;
7537 if (ranges != NULL)
7538 comp_unit_die->attrs[i++] = *ranges;
7539 if (comp_dir != NULL)
7540 comp_unit_die->attrs[i++] = *comp_dir;
7541 comp_unit_die->num_attrs += num_extra_attrs;
7542
b4f54984 7543 if (dwarf_die_debug)
bf6af496
DE
7544 {
7545 fprintf_unfiltered (gdb_stdlog,
7546 "Read die from %s@0x%x of %s:\n",
a32a8923 7547 get_section_name (section),
bf6af496
DE
7548 (unsigned) (begin_info_ptr - section->buffer),
7549 bfd_get_filename (abfd));
b4f54984 7550 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7551 }
7552
a2ce51a0
DE
7553 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7554 TUs by skipping the stub and going directly to the entry in the DWO file.
7555 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7556 to get it via circuitous means. Blech. */
7557 if (comp_dir != NULL)
7558 result_reader->comp_dir = DW_STRING (comp_dir);
7559
b0c7bfa9
DE
7560 /* Skip dummy compilation units. */
7561 if (info_ptr >= begin_info_ptr + dwo_unit->length
7562 || peek_abbrev_code (abfd, info_ptr) == 0)
7563 return 0;
7564
7565 *result_info_ptr = info_ptr;
7566 return 1;
7567}
7568
7569/* Subroutine of init_cutu_and_read_dies to simplify it.
7570 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7571 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7572
7573static struct dwo_unit *
7574lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7575 struct die_info *comp_unit_die)
7576{
7577 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7578 ULONGEST signature;
7579 struct dwo_unit *dwo_unit;
7580 const char *comp_dir, *dwo_name;
7581
a2ce51a0
DE
7582 gdb_assert (cu != NULL);
7583
b0c7bfa9 7584 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
7585 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7586 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7587
7588 if (this_cu->is_debug_types)
7589 {
7590 struct signatured_type *sig_type;
7591
7592 /* Since this_cu is the first member of struct signatured_type,
7593 we can go from a pointer to one to a pointer to the other. */
7594 sig_type = (struct signatured_type *) this_cu;
7595 signature = sig_type->signature;
7596 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7597 }
7598 else
7599 {
7600 struct attribute *attr;
7601
7602 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7603 if (! attr)
7604 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7605 " [in module %s]"),
4262abfb 7606 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
7607 signature = DW_UNSND (attr);
7608 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7609 signature);
7610 }
7611
b0c7bfa9
DE
7612 return dwo_unit;
7613}
7614
a2ce51a0 7615/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6
DE
7616 See it for a description of the parameters.
7617 Read a TU directly from a DWO file, bypassing the stub.
7618
7619 Note: This function could be a little bit simpler if we shared cleanups
7620 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
7621 to do, so we keep this function self-contained. Or we could move this
7622 into our caller, but it's complex enough already. */
a2ce51a0
DE
7623
7624static void
6aa5f3a6
DE
7625init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7626 int use_existing_cu, int keep,
a2ce51a0
DE
7627 die_reader_func_ftype *die_reader_func,
7628 void *data)
7629{
7630 struct dwarf2_cu *cu;
7631 struct signatured_type *sig_type;
6aa5f3a6 7632 struct cleanup *cleanups, *free_cu_cleanup = NULL;
a2ce51a0
DE
7633 struct die_reader_specs reader;
7634 const gdb_byte *info_ptr;
7635 struct die_info *comp_unit_die;
7636 int has_children;
7637
7638 /* Verify we can do the following downcast, and that we have the
7639 data we need. */
7640 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7641 sig_type = (struct signatured_type *) this_cu;
7642 gdb_assert (sig_type->dwo_unit != NULL);
7643
7644 cleanups = make_cleanup (null_cleanup, NULL);
7645
6aa5f3a6
DE
7646 if (use_existing_cu && this_cu->cu != NULL)
7647 {
7648 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7649 cu = this_cu->cu;
7650 /* There's no need to do the rereading_dwo_cu handling that
7651 init_cutu_and_read_dies does since we don't read the stub. */
7652 }
7653 else
7654 {
7655 /* If !use_existing_cu, this_cu->cu must be NULL. */
7656 gdb_assert (this_cu->cu == NULL);
8d749320 7657 cu = XNEW (struct dwarf2_cu);
6aa5f3a6
DE
7658 init_one_comp_unit (cu, this_cu);
7659 /* If an error occurs while loading, release our storage. */
7660 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
7661 }
7662
7663 /* A future optimization, if needed, would be to use an existing
7664 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7665 could share abbrev tables. */
a2ce51a0
DE
7666
7667 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7668 0 /* abbrev_table_provided */,
7669 NULL /* stub_comp_unit_die */,
7670 sig_type->dwo_unit->dwo_file->comp_dir,
7671 &reader, &info_ptr,
7672 &comp_unit_die, &has_children) == 0)
7673 {
7674 /* Dummy die. */
7675 do_cleanups (cleanups);
7676 return;
7677 }
7678
7679 /* All the "real" work is done here. */
7680 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7681
6aa5f3a6 7682 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7683 but the alternative is making the latter more complex.
7684 This function is only for the special case of using DWO files directly:
7685 no point in overly complicating the general case just to handle this. */
6aa5f3a6 7686 if (free_cu_cleanup != NULL)
a2ce51a0 7687 {
6aa5f3a6
DE
7688 if (keep)
7689 {
7690 /* We've successfully allocated this compilation unit. Let our
7691 caller clean it up when finished with it. */
7692 discard_cleanups (free_cu_cleanup);
a2ce51a0 7693
6aa5f3a6
DE
7694 /* We can only discard free_cu_cleanup and all subsequent cleanups.
7695 So we have to manually free the abbrev table. */
7696 dwarf2_free_abbrev_table (cu);
a2ce51a0 7697
6aa5f3a6
DE
7698 /* Link this CU into read_in_chain. */
7699 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7700 dwarf2_per_objfile->read_in_chain = this_cu;
7701 }
7702 else
7703 do_cleanups (free_cu_cleanup);
a2ce51a0 7704 }
a2ce51a0
DE
7705
7706 do_cleanups (cleanups);
7707}
7708
fd820528 7709/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7710 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7711
f4dc4d17
DE
7712 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7713 Otherwise the table specified in the comp unit header is read in and used.
7714 This is an optimization for when we already have the abbrev table.
7715
dee91e82
DE
7716 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7717 Otherwise, a new CU is allocated with xmalloc.
7718
7719 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7720 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7721
7722 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7723 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7724
70221824 7725static void
fd820528 7726init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7727 struct abbrev_table *abbrev_table,
fd820528
DE
7728 int use_existing_cu, int keep,
7729 die_reader_func_ftype *die_reader_func,
7730 void *data)
c906108c 7731{
dee91e82 7732 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7733 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7734 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7735 struct dwarf2_cu *cu;
d521ce57 7736 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7737 struct die_reader_specs reader;
d85a05f0 7738 struct die_info *comp_unit_die;
dee91e82 7739 int has_children;
d85a05f0 7740 struct attribute *attr;
365156ad 7741 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 7742 struct signatured_type *sig_type = NULL;
4bdcc0c1 7743 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7744 /* Non-zero if CU currently points to a DWO file and we need to
7745 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7746 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7747 int rereading_dwo_cu = 0;
c906108c 7748
b4f54984 7749 if (dwarf_die_debug)
09406207
DE
7750 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7751 this_cu->is_debug_types ? "type" : "comp",
9c541725 7752 to_underlying (this_cu->sect_off));
09406207 7753
dee91e82
DE
7754 if (use_existing_cu)
7755 gdb_assert (keep);
23745b47 7756
a2ce51a0
DE
7757 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7758 file (instead of going through the stub), short-circuit all of this. */
7759 if (this_cu->reading_dwo_directly)
7760 {
7761 /* Narrow down the scope of possibilities to have to understand. */
7762 gdb_assert (this_cu->is_debug_types);
7763 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7764 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7765 die_reader_func, data);
a2ce51a0
DE
7766 return;
7767 }
7768
dee91e82
DE
7769 cleanups = make_cleanup (null_cleanup, NULL);
7770
7771 /* This is cheap if the section is already read in. */
7772 dwarf2_read_section (objfile, section);
7773
9c541725 7774 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7775
7776 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
7777
7778 if (use_existing_cu && this_cu->cu != NULL)
7779 {
7780 cu = this_cu->cu;
42e7ad6c
DE
7781 /* If this CU is from a DWO file we need to start over, we need to
7782 refetch the attributes from the skeleton CU.
7783 This could be optimized by retrieving those attributes from when we
7784 were here the first time: the previous comp_unit_die was stored in
7785 comp_unit_obstack. But there's no data yet that we need this
7786 optimization. */
7787 if (cu->dwo_unit != NULL)
7788 rereading_dwo_cu = 1;
dee91e82
DE
7789 }
7790 else
7791 {
7792 /* If !use_existing_cu, this_cu->cu must be NULL. */
7793 gdb_assert (this_cu->cu == NULL);
8d749320 7794 cu = XNEW (struct dwarf2_cu);
dee91e82 7795 init_one_comp_unit (cu, this_cu);
dee91e82 7796 /* If an error occurs while loading, release our storage. */
365156ad 7797 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 7798 }
dee91e82 7799
b0c7bfa9 7800 /* Get the header. */
9c541725 7801 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7802 {
7803 /* We already have the header, there's no need to read it in again. */
9c541725 7804 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7805 }
7806 else
7807 {
3019eac3 7808 if (this_cu->is_debug_types)
dee91e82 7809 {
43988095 7810 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4bdcc0c1 7811 abbrev_section, info_ptr,
43988095 7812 rcuh_kind::TYPE);
dee91e82 7813
42e7ad6c
DE
7814 /* Since per_cu is the first member of struct signatured_type,
7815 we can go from a pointer to one to a pointer to the other. */
7816 sig_type = (struct signatured_type *) this_cu;
43988095 7817 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7818 gdb_assert (sig_type->type_offset_in_tu
7819 == cu->header.type_cu_offset_in_tu);
7820 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7821
42e7ad6c
DE
7822 /* LENGTH has not been set yet for type units if we're
7823 using .gdb_index. */
1ce1cefd 7824 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7825
7826 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7827 sig_type->type_offset_in_section =
7828 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7829
7830 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7831 }
7832 else
7833 {
4bdcc0c1
DE
7834 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
7835 abbrev_section,
43988095
JK
7836 info_ptr,
7837 rcuh_kind::COMPILE);
dee91e82 7838
9c541725 7839 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7840 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7841 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7842 }
7843 }
10b3939b 7844
6caca83c 7845 /* Skip dummy compilation units. */
dee91e82 7846 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
7847 || peek_abbrev_code (abfd, info_ptr) == 0)
7848 {
dee91e82 7849 do_cleanups (cleanups);
21b2bd31 7850 return;
6caca83c
CC
7851 }
7852
433df2d4
DE
7853 /* If we don't have them yet, read the abbrevs for this compilation unit.
7854 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
7855 done. Note that it's important that if the CU had an abbrev table
7856 on entry we don't free it when we're done: Somewhere up the call stack
7857 it may be in use. */
f4dc4d17
DE
7858 if (abbrev_table != NULL)
7859 {
7860 gdb_assert (cu->abbrev_table == NULL);
9c541725 7861 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
f4dc4d17
DE
7862 cu->abbrev_table = abbrev_table;
7863 }
7864 else if (cu->abbrev_table == NULL)
dee91e82 7865 {
4bdcc0c1 7866 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
7867 make_cleanup (dwarf2_free_abbrev_table, cu);
7868 }
42e7ad6c
DE
7869 else if (rereading_dwo_cu)
7870 {
7871 dwarf2_free_abbrev_table (cu);
7872 dwarf2_read_abbrevs (cu, abbrev_section);
7873 }
af703f96 7874
dee91e82 7875 /* Read the top level CU/TU die. */
3019eac3 7876 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 7877 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7878
b0c7bfa9
DE
7879 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7880 from the DWO file.
7881 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7882 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
7883 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7884 if (attr)
7885 {
3019eac3 7886 struct dwo_unit *dwo_unit;
b0c7bfa9 7887 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7888
7889 if (has_children)
6a506a2d
DE
7890 {
7891 complaint (&symfile_complaints,
7892 _("compilation unit with DW_AT_GNU_dwo_name"
7893 " has children (offset 0x%x) [in module %s]"),
9c541725 7894 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6a506a2d 7895 }
b0c7bfa9 7896 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7897 if (dwo_unit != NULL)
3019eac3 7898 {
6a506a2d
DE
7899 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7900 abbrev_table != NULL,
a2ce51a0 7901 comp_unit_die, NULL,
6a506a2d
DE
7902 &reader, &info_ptr,
7903 &dwo_comp_unit_die, &has_children) == 0)
7904 {
7905 /* Dummy die. */
7906 do_cleanups (cleanups);
7907 return;
7908 }
7909 comp_unit_die = dwo_comp_unit_die;
7910 }
7911 else
7912 {
7913 /* Yikes, we couldn't find the rest of the DIE, we only have
7914 the stub. A complaint has already been logged. There's
7915 not much more we can do except pass on the stub DIE to
7916 die_reader_func. We don't want to throw an error on bad
7917 debug info. */
3019eac3
DE
7918 }
7919 }
7920
b0c7bfa9 7921 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7922 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7923
b0c7bfa9 7924 /* Done, clean up. */
365156ad 7925 if (free_cu_cleanup != NULL)
348e048f 7926 {
365156ad
TT
7927 if (keep)
7928 {
7929 /* We've successfully allocated this compilation unit. Let our
7930 caller clean it up when finished with it. */
7931 discard_cleanups (free_cu_cleanup);
dee91e82 7932
365156ad
TT
7933 /* We can only discard free_cu_cleanup and all subsequent cleanups.
7934 So we have to manually free the abbrev table. */
7935 dwarf2_free_abbrev_table (cu);
dee91e82 7936
365156ad
TT
7937 /* Link this CU into read_in_chain. */
7938 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7939 dwarf2_per_objfile->read_in_chain = this_cu;
7940 }
7941 else
7942 do_cleanups (free_cu_cleanup);
348e048f 7943 }
365156ad
TT
7944
7945 do_cleanups (cleanups);
dee91e82
DE
7946}
7947
33e80786
DE
7948/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7949 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7950 to have already done the lookup to find the DWO file).
dee91e82
DE
7951
7952 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7953 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7954
7955 We fill in THIS_CU->length.
7956
7957 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7958 linker) then DIE_READER_FUNC will not get called.
7959
7960 THIS_CU->cu is always freed when done.
3019eac3
DE
7961 This is done in order to not leave THIS_CU->cu in a state where we have
7962 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7963
7964static void
7965init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7966 struct dwo_file *dwo_file,
dee91e82
DE
7967 die_reader_func_ftype *die_reader_func,
7968 void *data)
7969{
7970 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7971 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7972 bfd *abfd = get_section_bfd_owner (section);
33e80786 7973 struct dwarf2_section_info *abbrev_section;
dee91e82 7974 struct dwarf2_cu cu;
d521ce57 7975 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
7976 struct die_reader_specs reader;
7977 struct cleanup *cleanups;
7978 struct die_info *comp_unit_die;
7979 int has_children;
7980
b4f54984 7981 if (dwarf_die_debug)
09406207
DE
7982 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7983 this_cu->is_debug_types ? "type" : "comp",
9c541725 7984 to_underlying (this_cu->sect_off));
09406207 7985
dee91e82
DE
7986 gdb_assert (this_cu->cu == NULL);
7987
33e80786
DE
7988 abbrev_section = (dwo_file != NULL
7989 ? &dwo_file->sections.abbrev
7990 : get_abbrev_section_for_cu (this_cu));
7991
dee91e82
DE
7992 /* This is cheap if the section is already read in. */
7993 dwarf2_read_section (objfile, section);
7994
7995 init_one_comp_unit (&cu, this_cu);
7996
7997 cleanups = make_cleanup (free_stack_comp_unit, &cu);
7998
9c541725 7999 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4bdcc0c1
DE
8000 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
8001 abbrev_section, info_ptr,
43988095
JK
8002 (this_cu->is_debug_types
8003 ? rcuh_kind::TYPE
8004 : rcuh_kind::COMPILE));
dee91e82 8005
1ce1cefd 8006 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
8007
8008 /* Skip dummy compilation units. */
8009 if (info_ptr >= begin_info_ptr + this_cu->length
8010 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 8011 {
dee91e82 8012 do_cleanups (cleanups);
21b2bd31 8013 return;
93311388 8014 }
72bf9492 8015
dee91e82
DE
8016 dwarf2_read_abbrevs (&cu, abbrev_section);
8017 make_cleanup (dwarf2_free_abbrev_table, &cu);
8018
3019eac3 8019 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
8020 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8021
8022 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8023
8024 do_cleanups (cleanups);
8025}
8026
3019eac3
DE
8027/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8028 does not lookup the specified DWO file.
8029 This cannot be used to read DWO files.
dee91e82
DE
8030
8031 THIS_CU->cu is always freed when done.
3019eac3
DE
8032 This is done in order to not leave THIS_CU->cu in a state where we have
8033 to care whether it refers to the "main" CU or the DWO CU.
8034 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
8035
8036static void
8037init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8038 die_reader_func_ftype *die_reader_func,
8039 void *data)
8040{
33e80786 8041 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 8042}
0018ea6f
DE
8043\f
8044/* Type Unit Groups.
dee91e82 8045
0018ea6f
DE
8046 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8047 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8048 so that all types coming from the same compilation (.o file) are grouped
8049 together. A future step could be to put the types in the same symtab as
8050 the CU the types ultimately came from. */
ff013f42 8051
f4dc4d17
DE
8052static hashval_t
8053hash_type_unit_group (const void *item)
8054{
9a3c8263
SM
8055 const struct type_unit_group *tu_group
8056 = (const struct type_unit_group *) item;
f4dc4d17 8057
094b34ac 8058 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 8059}
348e048f
DE
8060
8061static int
f4dc4d17 8062eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 8063{
9a3c8263
SM
8064 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8065 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 8066
094b34ac 8067 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 8068}
348e048f 8069
f4dc4d17
DE
8070/* Allocate a hash table for type unit groups. */
8071
8072static htab_t
8073allocate_type_unit_groups_table (void)
8074{
8075 return htab_create_alloc_ex (3,
8076 hash_type_unit_group,
8077 eq_type_unit_group,
8078 NULL,
8079 &dwarf2_per_objfile->objfile->objfile_obstack,
8080 hashtab_obstack_allocate,
8081 dummy_obstack_deallocate);
8082}
dee91e82 8083
f4dc4d17
DE
8084/* Type units that don't have DW_AT_stmt_list are grouped into their own
8085 partial symtabs. We combine several TUs per psymtab to not let the size
8086 of any one psymtab grow too big. */
8087#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8088#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 8089
094b34ac 8090/* Helper routine for get_type_unit_group.
f4dc4d17
DE
8091 Create the type_unit_group object used to hold one or more TUs. */
8092
8093static struct type_unit_group *
094b34ac 8094create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
8095{
8096 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 8097 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 8098 struct type_unit_group *tu_group;
f4dc4d17
DE
8099
8100 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8101 struct type_unit_group);
094b34ac 8102 per_cu = &tu_group->per_cu;
f4dc4d17 8103 per_cu->objfile = objfile;
f4dc4d17 8104
094b34ac
DE
8105 if (dwarf2_per_objfile->using_index)
8106 {
8107 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8108 struct dwarf2_per_cu_quick_data);
094b34ac
DE
8109 }
8110 else
8111 {
9c541725 8112 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
8113 struct partial_symtab *pst;
8114 char *name;
8115
8116 /* Give the symtab a useful name for debug purposes. */
8117 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8118 name = xstrprintf ("<type_units_%d>",
8119 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8120 else
8121 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8122
8123 pst = create_partial_symtab (per_cu, name);
8124 pst->anonymous = 1;
f4dc4d17 8125
094b34ac
DE
8126 xfree (name);
8127 }
f4dc4d17 8128
094b34ac 8129 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 8130 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
8131
8132 return tu_group;
8133}
8134
094b34ac
DE
8135/* Look up the type_unit_group for type unit CU, and create it if necessary.
8136 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
8137
8138static struct type_unit_group *
ff39bb5e 8139get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
8140{
8141 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8142 struct type_unit_group *tu_group;
8143 void **slot;
8144 unsigned int line_offset;
8145 struct type_unit_group type_unit_group_for_lookup;
8146
8147 if (dwarf2_per_objfile->type_unit_groups == NULL)
8148 {
8149 dwarf2_per_objfile->type_unit_groups =
8150 allocate_type_unit_groups_table ();
8151 }
8152
8153 /* Do we need to create a new group, or can we use an existing one? */
8154
8155 if (stmt_list)
8156 {
8157 line_offset = DW_UNSND (stmt_list);
8158 ++tu_stats->nr_symtab_sharers;
8159 }
8160 else
8161 {
8162 /* Ugh, no stmt_list. Rare, but we have to handle it.
8163 We can do various things here like create one group per TU or
8164 spread them over multiple groups to split up the expansion work.
8165 To avoid worst case scenarios (too many groups or too large groups)
8166 we, umm, group them in bunches. */
8167 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8168 | (tu_stats->nr_stmt_less_type_units
8169 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8170 ++tu_stats->nr_stmt_less_type_units;
8171 }
8172
094b34ac 8173 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 8174 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
8175 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8176 &type_unit_group_for_lookup, INSERT);
8177 if (*slot != NULL)
8178 {
9a3c8263 8179 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
8180 gdb_assert (tu_group != NULL);
8181 }
8182 else
8183 {
9c541725 8184 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 8185 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
8186 *slot = tu_group;
8187 ++tu_stats->nr_symtabs;
8188 }
8189
8190 return tu_group;
8191}
0018ea6f
DE
8192\f
8193/* Partial symbol tables. */
8194
8195/* Create a psymtab named NAME and assign it to PER_CU.
8196
8197 The caller must fill in the following details:
8198 dirname, textlow, texthigh. */
8199
8200static struct partial_symtab *
8201create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8202{
8203 struct objfile *objfile = per_cu->objfile;
8204 struct partial_symtab *pst;
8205
18a94d75 8206 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
8207 objfile->global_psymbols,
8208 objfile->static_psymbols);
0018ea6f
DE
8209
8210 pst->psymtabs_addrmap_supported = 1;
8211
8212 /* This is the glue that links PST into GDB's symbol API. */
8213 pst->read_symtab_private = per_cu;
8214 pst->read_symtab = dwarf2_read_symtab;
8215 per_cu->v.psymtab = pst;
8216
8217 return pst;
8218}
8219
b93601f3
TT
8220/* The DATA object passed to process_psymtab_comp_unit_reader has this
8221 type. */
8222
8223struct process_psymtab_comp_unit_data
8224{
8225 /* True if we are reading a DW_TAG_partial_unit. */
8226
8227 int want_partial_unit;
8228
8229 /* The "pretend" language that is used if the CU doesn't declare a
8230 language. */
8231
8232 enum language pretend_language;
8233};
8234
0018ea6f
DE
8235/* die_reader_func for process_psymtab_comp_unit. */
8236
8237static void
8238process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8239 const gdb_byte *info_ptr,
0018ea6f
DE
8240 struct die_info *comp_unit_die,
8241 int has_children,
8242 void *data)
8243{
8244 struct dwarf2_cu *cu = reader->cu;
8245 struct objfile *objfile = cu->objfile;
3e29f34a 8246 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8247 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8248 CORE_ADDR baseaddr;
8249 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8250 struct partial_symtab *pst;
3a2b436a 8251 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8252 const char *filename;
9a3c8263
SM
8253 struct process_psymtab_comp_unit_data *info
8254 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 8255
b93601f3 8256 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
8257 return;
8258
8259 gdb_assert (! per_cu->is_debug_types);
8260
b93601f3 8261 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
8262
8263 cu->list_in_scope = &file_symbols;
8264
8265 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8266 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8267 if (filename == NULL)
0018ea6f 8268 filename = "";
0018ea6f
DE
8269
8270 pst = create_partial_symtab (per_cu, filename);
8271
8272 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8273 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
8274
8275 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8276
8277 dwarf2_find_base_address (comp_unit_die, cu);
8278
8279 /* Possibly set the default values of LOWPC and HIGHPC from
8280 `DW_AT_ranges'. */
3a2b436a
JK
8281 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8282 &best_highpc, cu, pst);
8283 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
8284 /* Store the contiguous range if it is not empty; it can be empty for
8285 CUs with no code. */
8286 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
8287 gdbarch_adjust_dwarf2_addr (gdbarch,
8288 best_lowpc + baseaddr),
8289 gdbarch_adjust_dwarf2_addr (gdbarch,
8290 best_highpc + baseaddr) - 1,
8291 pst);
0018ea6f
DE
8292
8293 /* Check if comp unit has_children.
8294 If so, read the rest of the partial symbols from this comp unit.
8295 If not, there's no more debug_info for this comp unit. */
8296 if (has_children)
8297 {
8298 struct partial_die_info *first_die;
8299 CORE_ADDR lowpc, highpc;
8300
8301 lowpc = ((CORE_ADDR) -1);
8302 highpc = ((CORE_ADDR) 0);
8303
8304 first_die = load_partial_dies (reader, info_ptr, 1);
8305
8306 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8307 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8308
8309 /* If we didn't find a lowpc, set it to highpc to avoid
8310 complaints from `maint check'. */
8311 if (lowpc == ((CORE_ADDR) -1))
8312 lowpc = highpc;
8313
8314 /* If the compilation unit didn't have an explicit address range,
8315 then use the information extracted from its child dies. */
e385593e 8316 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8317 {
8318 best_lowpc = lowpc;
8319 best_highpc = highpc;
8320 }
8321 }
3e29f34a
MR
8322 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8323 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 8324
8763cede 8325 end_psymtab_common (objfile, pst);
0018ea6f
DE
8326
8327 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8328 {
8329 int i;
8330 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8331 struct dwarf2_per_cu_data *iter;
8332
8333 /* Fill in 'dependencies' here; we fill in 'users' in a
8334 post-pass. */
8335 pst->number_of_dependencies = len;
8d749320
SM
8336 pst->dependencies =
8337 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
8338 for (i = 0;
8339 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8340 i, iter);
8341 ++i)
8342 pst->dependencies[i] = iter->v.psymtab;
8343
8344 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8345 }
8346
8347 /* Get the list of files included in the current compilation unit,
8348 and build a psymtab for each of them. */
8349 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8350
b4f54984 8351 if (dwarf_read_debug)
0018ea6f
DE
8352 {
8353 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8354
8355 fprintf_unfiltered (gdb_stdlog,
8356 "Psymtab for %s unit @0x%x: %s - %s"
8357 ", %d global, %d static syms\n",
8358 per_cu->is_debug_types ? "type" : "comp",
9c541725 8359 to_underlying (per_cu->sect_off),
0018ea6f
DE
8360 paddress (gdbarch, pst->textlow),
8361 paddress (gdbarch, pst->texthigh),
8362 pst->n_global_syms, pst->n_static_syms);
8363 }
8364}
8365
8366/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8367 Process compilation unit THIS_CU for a psymtab. */
8368
8369static void
8370process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8371 int want_partial_unit,
8372 enum language pretend_language)
0018ea6f
DE
8373{
8374 /* If this compilation unit was already read in, free the
8375 cached copy in order to read it in again. This is
8376 necessary because we skipped some symbols when we first
8377 read in the compilation unit (see load_partial_dies).
8378 This problem could be avoided, but the benefit is unclear. */
8379 if (this_cu->cu != NULL)
8380 free_one_cached_comp_unit (this_cu);
8381
f1902523
JK
8382 if (this_cu->is_debug_types)
8383 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8384 NULL);
8385 else
8386 {
8387 process_psymtab_comp_unit_data info;
8388 info.want_partial_unit = want_partial_unit;
8389 info.pretend_language = pretend_language;
8390 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8391 process_psymtab_comp_unit_reader, &info);
8392 }
0018ea6f
DE
8393
8394 /* Age out any secondary CUs. */
8395 age_cached_comp_units ();
8396}
f4dc4d17
DE
8397
8398/* Reader function for build_type_psymtabs. */
8399
8400static void
8401build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8402 const gdb_byte *info_ptr,
f4dc4d17
DE
8403 struct die_info *type_unit_die,
8404 int has_children,
8405 void *data)
8406{
8407 struct objfile *objfile = dwarf2_per_objfile->objfile;
8408 struct dwarf2_cu *cu = reader->cu;
8409 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8410 struct signatured_type *sig_type;
f4dc4d17
DE
8411 struct type_unit_group *tu_group;
8412 struct attribute *attr;
8413 struct partial_die_info *first_die;
8414 CORE_ADDR lowpc, highpc;
8415 struct partial_symtab *pst;
8416
8417 gdb_assert (data == NULL);
0186c6a7
DE
8418 gdb_assert (per_cu->is_debug_types);
8419 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8420
8421 if (! has_children)
8422 return;
8423
8424 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8425 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8426
0186c6a7 8427 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
8428
8429 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8430 cu->list_in_scope = &file_symbols;
8431 pst = create_partial_symtab (per_cu, "");
8432 pst->anonymous = 1;
8433
8434 first_die = load_partial_dies (reader, info_ptr, 1);
8435
8436 lowpc = (CORE_ADDR) -1;
8437 highpc = (CORE_ADDR) 0;
8438 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8439
8763cede 8440 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8441}
8442
73051182
DE
8443/* Struct used to sort TUs by their abbreviation table offset. */
8444
8445struct tu_abbrev_offset
8446{
8447 struct signatured_type *sig_type;
8448 sect_offset abbrev_offset;
8449};
8450
8451/* Helper routine for build_type_psymtabs_1, passed to qsort. */
8452
8453static int
8454sort_tu_by_abbrev_offset (const void *ap, const void *bp)
8455{
9a3c8263
SM
8456 const struct tu_abbrev_offset * const *a
8457 = (const struct tu_abbrev_offset * const*) ap;
8458 const struct tu_abbrev_offset * const *b
8459 = (const struct tu_abbrev_offset * const*) bp;
9c541725
PA
8460 sect_offset aoff = (*a)->abbrev_offset;
8461 sect_offset boff = (*b)->abbrev_offset;
73051182
DE
8462
8463 return (aoff > boff) - (aoff < boff);
8464}
8465
8466/* Efficiently read all the type units.
8467 This does the bulk of the work for build_type_psymtabs.
8468
8469 The efficiency is because we sort TUs by the abbrev table they use and
8470 only read each abbrev table once. In one program there are 200K TUs
8471 sharing 8K abbrev tables.
8472
8473 The main purpose of this function is to support building the
8474 dwarf2_per_objfile->type_unit_groups table.
8475 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8476 can collapse the search space by grouping them by stmt_list.
8477 The savings can be significant, in the same program from above the 200K TUs
8478 share 8K stmt_list tables.
8479
8480 FUNC is expected to call get_type_unit_group, which will create the
8481 struct type_unit_group if necessary and add it to
8482 dwarf2_per_objfile->type_unit_groups. */
8483
8484static void
8485build_type_psymtabs_1 (void)
8486{
73051182
DE
8487 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8488 struct cleanup *cleanups;
8489 struct abbrev_table *abbrev_table;
8490 sect_offset abbrev_offset;
8491 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
8492 int i;
8493
8494 /* It's up to the caller to not call us multiple times. */
8495 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8496
8497 if (dwarf2_per_objfile->n_type_units == 0)
8498 return;
8499
8500 /* TUs typically share abbrev tables, and there can be way more TUs than
8501 abbrev tables. Sort by abbrev table to reduce the number of times we
8502 read each abbrev table in.
8503 Alternatives are to punt or to maintain a cache of abbrev tables.
8504 This is simpler and efficient enough for now.
8505
8506 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8507 symtab to use). Typically TUs with the same abbrev offset have the same
8508 stmt_list value too so in practice this should work well.
8509
8510 The basic algorithm here is:
8511
8512 sort TUs by abbrev table
8513 for each TU with same abbrev table:
8514 read abbrev table if first user
8515 read TU top level DIE
8516 [IWBN if DWO skeletons had DW_AT_stmt_list]
8517 call FUNC */
8518
b4f54984 8519 if (dwarf_read_debug)
73051182
DE
8520 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8521
8522 /* Sort in a separate table to maintain the order of all_type_units
8523 for .gdb_index: TU indices directly index all_type_units. */
8524 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
8525 dwarf2_per_objfile->n_type_units);
8526 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8527 {
8528 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8529
8530 sorted_by_abbrev[i].sig_type = sig_type;
8531 sorted_by_abbrev[i].abbrev_offset =
8532 read_abbrev_offset (sig_type->per_cu.section,
9c541725 8533 sig_type->per_cu.sect_off);
73051182
DE
8534 }
8535 cleanups = make_cleanup (xfree, sorted_by_abbrev);
8536 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
8537 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
8538
9c541725 8539 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
8540 abbrev_table = NULL;
8541 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
8542
8543 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8544 {
8545 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8546
8547 /* Switch to the next abbrev table if necessary. */
8548 if (abbrev_table == NULL
9c541725 8549 || tu->abbrev_offset != abbrev_offset)
73051182
DE
8550 {
8551 if (abbrev_table != NULL)
8552 {
8553 abbrev_table_free (abbrev_table);
8554 /* Reset to NULL in case abbrev_table_read_table throws
8555 an error: abbrev_table_free_cleanup will get called. */
8556 abbrev_table = NULL;
8557 }
8558 abbrev_offset = tu->abbrev_offset;
8559 abbrev_table =
8560 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
8561 abbrev_offset);
8562 ++tu_stats->nr_uniq_abbrev_tables;
8563 }
8564
8565 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
8566 build_type_psymtabs_reader, NULL);
8567 }
8568
73051182 8569 do_cleanups (cleanups);
6aa5f3a6 8570}
73051182 8571
6aa5f3a6
DE
8572/* Print collected type unit statistics. */
8573
8574static void
8575print_tu_stats (void)
8576{
8577 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8578
8579 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8580 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8581 dwarf2_per_objfile->n_type_units);
8582 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8583 tu_stats->nr_uniq_abbrev_tables);
8584 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8585 tu_stats->nr_symtabs);
8586 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8587 tu_stats->nr_symtab_sharers);
8588 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8589 tu_stats->nr_stmt_less_type_units);
8590 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8591 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8592}
8593
f4dc4d17
DE
8594/* Traversal function for build_type_psymtabs. */
8595
8596static int
8597build_type_psymtab_dependencies (void **slot, void *info)
8598{
8599 struct objfile *objfile = dwarf2_per_objfile->objfile;
8600 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8601 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8602 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
8603 int len = VEC_length (sig_type_ptr, tu_group->tus);
8604 struct signatured_type *iter;
f4dc4d17
DE
8605 int i;
8606
8607 gdb_assert (len > 0);
0186c6a7 8608 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8609
8610 pst->number_of_dependencies = len;
8d749320
SM
8611 pst->dependencies =
8612 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 8613 for (i = 0;
0186c6a7 8614 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
8615 ++i)
8616 {
0186c6a7
DE
8617 gdb_assert (iter->per_cu.is_debug_types);
8618 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8619 iter->type_unit_group = tu_group;
f4dc4d17
DE
8620 }
8621
0186c6a7 8622 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
8623
8624 return 1;
8625}
8626
8627/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8628 Build partial symbol tables for the .debug_types comp-units. */
8629
8630static void
8631build_type_psymtabs (struct objfile *objfile)
8632{
0e50663e 8633 if (! create_all_type_units (objfile))
348e048f
DE
8634 return;
8635
73051182 8636 build_type_psymtabs_1 ();
6aa5f3a6 8637}
f4dc4d17 8638
6aa5f3a6
DE
8639/* Traversal function for process_skeletonless_type_unit.
8640 Read a TU in a DWO file and build partial symbols for it. */
8641
8642static int
8643process_skeletonless_type_unit (void **slot, void *info)
8644{
8645 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9a3c8263 8646 struct objfile *objfile = (struct objfile *) info;
6aa5f3a6
DE
8647 struct signatured_type find_entry, *entry;
8648
8649 /* If this TU doesn't exist in the global table, add it and read it in. */
8650
8651 if (dwarf2_per_objfile->signatured_types == NULL)
8652 {
8653 dwarf2_per_objfile->signatured_types
8654 = allocate_signatured_type_table (objfile);
8655 }
8656
8657 find_entry.signature = dwo_unit->signature;
8658 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8659 INSERT);
8660 /* If we've already seen this type there's nothing to do. What's happening
8661 is we're doing our own version of comdat-folding here. */
8662 if (*slot != NULL)
8663 return 1;
8664
8665 /* This does the job that create_all_type_units would have done for
8666 this TU. */
8667 entry = add_type_unit (dwo_unit->signature, slot);
8668 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
8669 *slot = entry;
8670
8671 /* This does the job that build_type_psymtabs_1 would have done. */
8672 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8673 build_type_psymtabs_reader, NULL);
8674
8675 return 1;
8676}
8677
8678/* Traversal function for process_skeletonless_type_units. */
8679
8680static int
8681process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8682{
8683 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8684
8685 if (dwo_file->tus != NULL)
8686 {
8687 htab_traverse_noresize (dwo_file->tus,
8688 process_skeletonless_type_unit, info);
8689 }
8690
8691 return 1;
8692}
8693
8694/* Scan all TUs of DWO files, verifying we've processed them.
8695 This is needed in case a TU was emitted without its skeleton.
8696 Note: This can't be done until we know what all the DWO files are. */
8697
8698static void
8699process_skeletonless_type_units (struct objfile *objfile)
8700{
8701 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8702 if (get_dwp_file () == NULL
8703 && dwarf2_per_objfile->dwo_files != NULL)
8704 {
8705 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8706 process_dwo_file_for_skeletonless_type_units,
8707 objfile);
8708 }
348e048f
DE
8709}
8710
95554aad
TT
8711/* Compute the 'user' field for each psymtab in OBJFILE. */
8712
8713static void
8714set_partial_user (struct objfile *objfile)
8715{
8716 int i;
8717
8718 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8719 {
8832e7e3 8720 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
95554aad
TT
8721 struct partial_symtab *pst = per_cu->v.psymtab;
8722 int j;
8723
36586728
TT
8724 if (pst == NULL)
8725 continue;
8726
95554aad
TT
8727 for (j = 0; j < pst->number_of_dependencies; ++j)
8728 {
8729 /* Set the 'user' field only if it is not already set. */
8730 if (pst->dependencies[j]->user == NULL)
8731 pst->dependencies[j]->user = pst;
8732 }
8733 }
8734}
8735
93311388
DE
8736/* Build the partial symbol table by doing a quick pass through the
8737 .debug_info and .debug_abbrev sections. */
72bf9492 8738
93311388 8739static void
c67a9c90 8740dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 8741{
791afaa2 8742 struct cleanup *back_to;
21b2bd31 8743 int i;
93311388 8744
b4f54984 8745 if (dwarf_read_debug)
45cfd468
DE
8746 {
8747 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8748 objfile_name (objfile));
45cfd468
DE
8749 }
8750
98bfdba5
PA
8751 dwarf2_per_objfile->reading_partial_symbols = 1;
8752
be391dca 8753 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8754
93311388
DE
8755 /* Any cached compilation units will be linked by the per-objfile
8756 read_in_chain. Make sure to free them when we're done. */
8757 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 8758
348e048f
DE
8759 build_type_psymtabs (objfile);
8760
93311388 8761 create_all_comp_units (objfile);
c906108c 8762
60606b2c
TT
8763 /* Create a temporary address map on a temporary obstack. We later
8764 copy this to the final obstack. */
8268c778 8765 auto_obstack temp_obstack;
791afaa2
TT
8766
8767 scoped_restore save_psymtabs_addrmap
8768 = make_scoped_restore (&objfile->psymtabs_addrmap,
8769 addrmap_create_mutable (&temp_obstack));
72bf9492 8770
21b2bd31 8771 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 8772 {
8832e7e3 8773 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
aaa75496 8774
b93601f3 8775 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 8776 }
ff013f42 8777
6aa5f3a6
DE
8778 /* This has to wait until we read the CUs, we need the list of DWOs. */
8779 process_skeletonless_type_units (objfile);
8780
8781 /* Now that all TUs have been processed we can fill in the dependencies. */
8782 if (dwarf2_per_objfile->type_unit_groups != NULL)
8783 {
8784 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8785 build_type_psymtab_dependencies, NULL);
8786 }
8787
b4f54984 8788 if (dwarf_read_debug)
6aa5f3a6
DE
8789 print_tu_stats ();
8790
95554aad
TT
8791 set_partial_user (objfile);
8792
ff013f42
JK
8793 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8794 &objfile->objfile_obstack);
791afaa2
TT
8795 /* At this point we want to keep the address map. */
8796 save_psymtabs_addrmap.release ();
ff013f42 8797
ae038cb0 8798 do_cleanups (back_to);
45cfd468 8799
b4f54984 8800 if (dwarf_read_debug)
45cfd468 8801 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8802 objfile_name (objfile));
ae038cb0
DJ
8803}
8804
3019eac3 8805/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8806
8807static void
dee91e82 8808load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8809 const gdb_byte *info_ptr,
dee91e82
DE
8810 struct die_info *comp_unit_die,
8811 int has_children,
8812 void *data)
ae038cb0 8813{
dee91e82 8814 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8815
95554aad 8816 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8817
ae038cb0
DJ
8818 /* Check if comp unit has_children.
8819 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8820 If not, there's no more debug_info for this comp unit. */
d85a05f0 8821 if (has_children)
dee91e82
DE
8822 load_partial_dies (reader, info_ptr, 0);
8823}
98bfdba5 8824
dee91e82
DE
8825/* Load the partial DIEs for a secondary CU into memory.
8826 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8827
dee91e82
DE
8828static void
8829load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8830{
f4dc4d17
DE
8831 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8832 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8833}
8834
ae038cb0 8835static void
36586728
TT
8836read_comp_units_from_section (struct objfile *objfile,
8837 struct dwarf2_section_info *section,
f1902523 8838 struct dwarf2_section_info *abbrev_section,
36586728
TT
8839 unsigned int is_dwz,
8840 int *n_allocated,
8841 int *n_comp_units,
8842 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 8843{
d521ce57 8844 const gdb_byte *info_ptr;
be391dca 8845
b4f54984 8846 if (dwarf_read_debug)
bf6af496 8847 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8848 get_section_name (section),
8849 get_section_file_name (section));
bf6af496 8850
36586728 8851 dwarf2_read_section (objfile, section);
ae038cb0 8852
36586728 8853 info_ptr = section->buffer;
6e70227d 8854
36586728 8855 while (info_ptr < section->buffer + section->size)
ae038cb0 8856 {
ae038cb0 8857 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8858
9c541725 8859 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8860
f1902523
JK
8861 comp_unit_head cu_header;
8862 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
8863 info_ptr, rcuh_kind::COMPILE);
ae038cb0
DJ
8864
8865 /* Save the compilation unit for later lookup. */
f1902523
JK
8866 if (cu_header.unit_type != DW_UT_type)
8867 {
8868 this_cu = XOBNEW (&objfile->objfile_obstack,
8869 struct dwarf2_per_cu_data);
8870 memset (this_cu, 0, sizeof (*this_cu));
8871 }
8872 else
8873 {
8874 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8875 struct signatured_type);
8876 memset (sig_type, 0, sizeof (*sig_type));
8877 sig_type->signature = cu_header.signature;
8878 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8879 this_cu = &sig_type->per_cu;
8880 }
8881 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8882 this_cu->sect_off = sect_off;
f1902523 8883 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8884 this_cu->is_dwz = is_dwz;
9291a0cd 8885 this_cu->objfile = objfile;
8a0459fd 8886 this_cu->section = section;
ae038cb0 8887
36586728 8888 if (*n_comp_units == *n_allocated)
ae038cb0 8889 {
36586728 8890 *n_allocated *= 2;
224c3ddb
SM
8891 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
8892 *all_comp_units, *n_allocated);
ae038cb0 8893 }
36586728
TT
8894 (*all_comp_units)[*n_comp_units] = this_cu;
8895 ++*n_comp_units;
ae038cb0
DJ
8896
8897 info_ptr = info_ptr + this_cu->length;
8898 }
36586728
TT
8899}
8900
8901/* Create a list of all compilation units in OBJFILE.
8902 This is only done for -readnow and building partial symtabs. */
8903
8904static void
8905create_all_comp_units (struct objfile *objfile)
8906{
8907 int n_allocated;
8908 int n_comp_units;
8909 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 8910 struct dwz_file *dwz;
36586728
TT
8911
8912 n_comp_units = 0;
8913 n_allocated = 10;
8d749320 8914 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728 8915
f1902523
JK
8916 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
8917 &dwarf2_per_objfile->abbrev, 0,
36586728
TT
8918 &n_allocated, &n_comp_units, &all_comp_units);
8919
4db1a1dc
TT
8920 dwz = dwarf2_get_dwz_file ();
8921 if (dwz != NULL)
f1902523 8922 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
4db1a1dc
TT
8923 &n_allocated, &n_comp_units,
8924 &all_comp_units);
ae038cb0 8925
8d749320
SM
8926 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
8927 struct dwarf2_per_cu_data *,
8928 n_comp_units);
ae038cb0
DJ
8929 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
8930 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
8931 xfree (all_comp_units);
8932 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
8933}
8934
5734ee8b 8935/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8936 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8937 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8938 DW_AT_ranges). See the comments of add_partial_subprogram on how
8939 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8940
72bf9492
DJ
8941static void
8942scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8943 CORE_ADDR *highpc, int set_addrmap,
8944 struct dwarf2_cu *cu)
c906108c 8945{
72bf9492 8946 struct partial_die_info *pdi;
c906108c 8947
91c24f0a
DC
8948 /* Now, march along the PDI's, descending into ones which have
8949 interesting children but skipping the children of the other ones,
8950 until we reach the end of the compilation unit. */
c906108c 8951
72bf9492 8952 pdi = first_die;
91c24f0a 8953
72bf9492
DJ
8954 while (pdi != NULL)
8955 {
8956 fixup_partial_die (pdi, cu);
c906108c 8957
f55ee35c 8958 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8959 children, so we need to look at them. Ditto for anonymous
8960 enums. */
933c6fe4 8961
72bf9492 8962 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
8963 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8964 || pdi->tag == DW_TAG_imported_unit)
c906108c 8965 {
72bf9492 8966 switch (pdi->tag)
c906108c
SS
8967 {
8968 case DW_TAG_subprogram:
cdc07690 8969 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8970 break;
72929c62 8971 case DW_TAG_constant:
c906108c
SS
8972 case DW_TAG_variable:
8973 case DW_TAG_typedef:
91c24f0a 8974 case DW_TAG_union_type:
72bf9492 8975 if (!pdi->is_declaration)
63d06c5c 8976 {
72bf9492 8977 add_partial_symbol (pdi, cu);
63d06c5c
DC
8978 }
8979 break;
c906108c 8980 case DW_TAG_class_type:
680b30c7 8981 case DW_TAG_interface_type:
c906108c 8982 case DW_TAG_structure_type:
72bf9492 8983 if (!pdi->is_declaration)
c906108c 8984 {
72bf9492 8985 add_partial_symbol (pdi, cu);
c906108c 8986 }
e98c9e7c
TT
8987 if (cu->language == language_rust && pdi->has_children)
8988 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8989 set_addrmap, cu);
c906108c 8990 break;
91c24f0a 8991 case DW_TAG_enumeration_type:
72bf9492
DJ
8992 if (!pdi->is_declaration)
8993 add_partial_enumeration (pdi, cu);
c906108c
SS
8994 break;
8995 case DW_TAG_base_type:
a02abb62 8996 case DW_TAG_subrange_type:
c906108c 8997 /* File scope base type definitions are added to the partial
c5aa993b 8998 symbol table. */
72bf9492 8999 add_partial_symbol (pdi, cu);
c906108c 9000 break;
d9fa45fe 9001 case DW_TAG_namespace:
cdc07690 9002 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 9003 break;
5d7cb8df 9004 case DW_TAG_module:
cdc07690 9005 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 9006 break;
95554aad
TT
9007 case DW_TAG_imported_unit:
9008 {
9009 struct dwarf2_per_cu_data *per_cu;
9010
f4dc4d17
DE
9011 /* For now we don't handle imported units in type units. */
9012 if (cu->per_cu->is_debug_types)
9013 {
9014 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9015 " supported in type units [in module %s]"),
4262abfb 9016 objfile_name (cu->objfile));
f4dc4d17
DE
9017 }
9018
9c541725 9019 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
36586728 9020 pdi->is_dwz,
95554aad
TT
9021 cu->objfile);
9022
9023 /* Go read the partial unit, if needed. */
9024 if (per_cu->v.psymtab == NULL)
b93601f3 9025 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 9026
f4dc4d17 9027 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 9028 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
9029 }
9030 break;
74921315
KS
9031 case DW_TAG_imported_declaration:
9032 add_partial_symbol (pdi, cu);
9033 break;
c906108c
SS
9034 default:
9035 break;
9036 }
9037 }
9038
72bf9492
DJ
9039 /* If the die has a sibling, skip to the sibling. */
9040
9041 pdi = pdi->die_sibling;
9042 }
9043}
9044
9045/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 9046
72bf9492 9047 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 9048 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
9049 Enumerators are an exception; they use the scope of their parent
9050 enumeration type, i.e. the name of the enumeration type is not
9051 prepended to the enumerator.
91c24f0a 9052
72bf9492
DJ
9053 There are two complexities. One is DW_AT_specification; in this
9054 case "parent" means the parent of the target of the specification,
9055 instead of the direct parent of the DIE. The other is compilers
9056 which do not emit DW_TAG_namespace; in this case we try to guess
9057 the fully qualified name of structure types from their members'
9058 linkage names. This must be done using the DIE's children rather
9059 than the children of any DW_AT_specification target. We only need
9060 to do this for structures at the top level, i.e. if the target of
9061 any DW_AT_specification (if any; otherwise the DIE itself) does not
9062 have a parent. */
9063
9064/* Compute the scope prefix associated with PDI's parent, in
9065 compilation unit CU. The result will be allocated on CU's
9066 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9067 field. NULL is returned if no prefix is necessary. */
15d034d0 9068static const char *
72bf9492
DJ
9069partial_die_parent_scope (struct partial_die_info *pdi,
9070 struct dwarf2_cu *cu)
9071{
15d034d0 9072 const char *grandparent_scope;
72bf9492 9073 struct partial_die_info *parent, *real_pdi;
91c24f0a 9074
72bf9492
DJ
9075 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9076 then this means the parent of the specification DIE. */
9077
9078 real_pdi = pdi;
72bf9492 9079 while (real_pdi->has_specification)
36586728
TT
9080 real_pdi = find_partial_die (real_pdi->spec_offset,
9081 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
9082
9083 parent = real_pdi->die_parent;
9084 if (parent == NULL)
9085 return NULL;
9086
9087 if (parent->scope_set)
9088 return parent->scope;
9089
9090 fixup_partial_die (parent, cu);
9091
10b3939b 9092 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 9093
acebe513
UW
9094 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9095 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9096 Work around this problem here. */
9097 if (cu->language == language_cplus
6e70227d 9098 && parent->tag == DW_TAG_namespace
acebe513
UW
9099 && strcmp (parent->name, "::") == 0
9100 && grandparent_scope == NULL)
9101 {
9102 parent->scope = NULL;
9103 parent->scope_set = 1;
9104 return NULL;
9105 }
9106
9c6c53f7
SA
9107 if (pdi->tag == DW_TAG_enumerator)
9108 /* Enumerators should not get the name of the enumeration as a prefix. */
9109 parent->scope = grandparent_scope;
9110 else if (parent->tag == DW_TAG_namespace
f55ee35c 9111 || parent->tag == DW_TAG_module
72bf9492
DJ
9112 || parent->tag == DW_TAG_structure_type
9113 || parent->tag == DW_TAG_class_type
680b30c7 9114 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
9115 || parent->tag == DW_TAG_union_type
9116 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
9117 {
9118 if (grandparent_scope == NULL)
9119 parent->scope = parent->name;
9120 else
3e43a32a
MS
9121 parent->scope = typename_concat (&cu->comp_unit_obstack,
9122 grandparent_scope,
f55ee35c 9123 parent->name, 0, cu);
72bf9492 9124 }
72bf9492
DJ
9125 else
9126 {
9127 /* FIXME drow/2004-04-01: What should we be doing with
9128 function-local names? For partial symbols, we should probably be
9129 ignoring them. */
9130 complaint (&symfile_complaints,
e2e0b3e5 9131 _("unhandled containing DIE tag %d for DIE at %d"),
9c541725 9132 parent->tag, to_underlying (pdi->sect_off));
72bf9492 9133 parent->scope = grandparent_scope;
c906108c
SS
9134 }
9135
72bf9492
DJ
9136 parent->scope_set = 1;
9137 return parent->scope;
9138}
9139
9140/* Return the fully scoped name associated with PDI, from compilation unit
9141 CU. The result will be allocated with malloc. */
4568ecf9 9142
72bf9492
DJ
9143static char *
9144partial_die_full_name (struct partial_die_info *pdi,
9145 struct dwarf2_cu *cu)
9146{
15d034d0 9147 const char *parent_scope;
72bf9492 9148
98bfdba5
PA
9149 /* If this is a template instantiation, we can not work out the
9150 template arguments from partial DIEs. So, unfortunately, we have
9151 to go through the full DIEs. At least any work we do building
9152 types here will be reused if full symbols are loaded later. */
9153 if (pdi->has_template_arguments)
9154 {
9155 fixup_partial_die (pdi, cu);
9156
9157 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9158 {
9159 struct die_info *die;
9160 struct attribute attr;
9161 struct dwarf2_cu *ref_cu = cu;
9162
b64f50a1 9163 /* DW_FORM_ref_addr is using section offset. */
b4069958 9164 attr.name = (enum dwarf_attribute) 0;
98bfdba5 9165 attr.form = DW_FORM_ref_addr;
9c541725 9166 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
9167 die = follow_die_ref (NULL, &attr, &ref_cu);
9168
9169 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9170 }
9171 }
9172
72bf9492
DJ
9173 parent_scope = partial_die_parent_scope (pdi, cu);
9174 if (parent_scope == NULL)
9175 return NULL;
9176 else
f55ee35c 9177 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
9178}
9179
9180static void
72bf9492 9181add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 9182{
e7c27a73 9183 struct objfile *objfile = cu->objfile;
3e29f34a 9184 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 9185 CORE_ADDR addr = 0;
15d034d0 9186 const char *actual_name = NULL;
e142c38c 9187 CORE_ADDR baseaddr;
15d034d0 9188 char *built_actual_name;
e142c38c
DJ
9189
9190 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9191
15d034d0
TT
9192 built_actual_name = partial_die_full_name (pdi, cu);
9193 if (built_actual_name != NULL)
9194 actual_name = built_actual_name;
63d06c5c 9195
72bf9492
DJ
9196 if (actual_name == NULL)
9197 actual_name = pdi->name;
9198
c906108c
SS
9199 switch (pdi->tag)
9200 {
9201 case DW_TAG_subprogram:
3e29f34a 9202 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 9203 if (pdi->is_external || cu->language == language_ada)
c906108c 9204 {
2cfa0c8d
JB
9205 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9206 of the global scope. But in Ada, we want to be able to access
9207 nested procedures globally. So all Ada subprograms are stored
9208 in the global scope. */
f47fb265 9209 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9210 built_actual_name != NULL,
f47fb265
MS
9211 VAR_DOMAIN, LOC_BLOCK,
9212 &objfile->global_psymbols,
1762568f 9213 addr, cu->language, objfile);
c906108c
SS
9214 }
9215 else
9216 {
f47fb265 9217 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9218 built_actual_name != NULL,
f47fb265
MS
9219 VAR_DOMAIN, LOC_BLOCK,
9220 &objfile->static_psymbols,
1762568f 9221 addr, cu->language, objfile);
c906108c 9222 }
0c1b455e
TT
9223
9224 if (pdi->main_subprogram && actual_name != NULL)
9225 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 9226 break;
72929c62
JB
9227 case DW_TAG_constant:
9228 {
af5bf4ad 9229 std::vector<partial_symbol *> *list;
72929c62
JB
9230
9231 if (pdi->is_external)
9232 list = &objfile->global_psymbols;
9233 else
9234 list = &objfile->static_psymbols;
f47fb265 9235 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9236 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 9237 list, 0, cu->language, objfile);
72929c62
JB
9238 }
9239 break;
c906108c 9240 case DW_TAG_variable:
95554aad
TT
9241 if (pdi->d.locdesc)
9242 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 9243
95554aad 9244 if (pdi->d.locdesc
caac4577
JG
9245 && addr == 0
9246 && !dwarf2_per_objfile->has_section_at_zero)
9247 {
9248 /* A global or static variable may also have been stripped
9249 out by the linker if unused, in which case its address
9250 will be nullified; do not add such variables into partial
9251 symbol table then. */
9252 }
9253 else if (pdi->is_external)
c906108c
SS
9254 {
9255 /* Global Variable.
9256 Don't enter into the minimal symbol tables as there is
9257 a minimal symbol table entry from the ELF symbols already.
9258 Enter into partial symbol table if it has a location
9259 descriptor or a type.
9260 If the location descriptor is missing, new_symbol will create
9261 a LOC_UNRESOLVED symbol, the address of the variable will then
9262 be determined from the minimal symbol table whenever the variable
9263 is referenced.
9264 The address for the partial symbol table entry is not
9265 used by GDB, but it comes in handy for debugging partial symbol
9266 table building. */
9267
95554aad 9268 if (pdi->d.locdesc || pdi->has_type)
f47fb265 9269 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9270 built_actual_name != NULL,
f47fb265
MS
9271 VAR_DOMAIN, LOC_STATIC,
9272 &objfile->global_psymbols,
1762568f 9273 addr + baseaddr,
f47fb265 9274 cu->language, objfile);
c906108c
SS
9275 }
9276 else
9277 {
ff908ebf
AW
9278 int has_loc = pdi->d.locdesc != NULL;
9279
9280 /* Static Variable. Skip symbols whose value we cannot know (those
9281 without location descriptors or constant values). */
9282 if (!has_loc && !pdi->has_const_value)
decbce07 9283 {
15d034d0 9284 xfree (built_actual_name);
decbce07
MS
9285 return;
9286 }
ff908ebf 9287
f47fb265 9288 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9289 built_actual_name != NULL,
f47fb265
MS
9290 VAR_DOMAIN, LOC_STATIC,
9291 &objfile->static_psymbols,
ff908ebf 9292 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 9293 cu->language, objfile);
c906108c
SS
9294 }
9295 break;
9296 case DW_TAG_typedef:
9297 case DW_TAG_base_type:
a02abb62 9298 case DW_TAG_subrange_type:
38d518c9 9299 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9300 built_actual_name != NULL,
176620f1 9301 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 9302 &objfile->static_psymbols,
1762568f 9303 0, cu->language, objfile);
c906108c 9304 break;
74921315 9305 case DW_TAG_imported_declaration:
72bf9492
DJ
9306 case DW_TAG_namespace:
9307 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9308 built_actual_name != NULL,
72bf9492
DJ
9309 VAR_DOMAIN, LOC_TYPEDEF,
9310 &objfile->global_psymbols,
1762568f 9311 0, cu->language, objfile);
72bf9492 9312 break;
530e8392
KB
9313 case DW_TAG_module:
9314 add_psymbol_to_list (actual_name, strlen (actual_name),
9315 built_actual_name != NULL,
9316 MODULE_DOMAIN, LOC_TYPEDEF,
9317 &objfile->global_psymbols,
1762568f 9318 0, cu->language, objfile);
530e8392 9319 break;
c906108c 9320 case DW_TAG_class_type:
680b30c7 9321 case DW_TAG_interface_type:
c906108c
SS
9322 case DW_TAG_structure_type:
9323 case DW_TAG_union_type:
9324 case DW_TAG_enumeration_type:
fa4028e9
JB
9325 /* Skip external references. The DWARF standard says in the section
9326 about "Structure, Union, and Class Type Entries": "An incomplete
9327 structure, union or class type is represented by a structure,
9328 union or class entry that does not have a byte size attribute
9329 and that has a DW_AT_declaration attribute." */
9330 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 9331 {
15d034d0 9332 xfree (built_actual_name);
decbce07
MS
9333 return;
9334 }
fa4028e9 9335
63d06c5c
DC
9336 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9337 static vs. global. */
38d518c9 9338 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9339 built_actual_name != NULL,
176620f1 9340 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 9341 cu->language == language_cplus
63d06c5c
DC
9342 ? &objfile->global_psymbols
9343 : &objfile->static_psymbols,
1762568f 9344 0, cu->language, objfile);
c906108c 9345
c906108c
SS
9346 break;
9347 case DW_TAG_enumerator:
38d518c9 9348 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9349 built_actual_name != NULL,
176620f1 9350 VAR_DOMAIN, LOC_CONST,
9c37b5ae 9351 cu->language == language_cplus
f6fe98ef
DJ
9352 ? &objfile->global_psymbols
9353 : &objfile->static_psymbols,
1762568f 9354 0, cu->language, objfile);
c906108c
SS
9355 break;
9356 default:
9357 break;
9358 }
5c4e30ca 9359
15d034d0 9360 xfree (built_actual_name);
c906108c
SS
9361}
9362
5c4e30ca
DC
9363/* Read a partial die corresponding to a namespace; also, add a symbol
9364 corresponding to that namespace to the symbol table. NAMESPACE is
9365 the name of the enclosing namespace. */
91c24f0a 9366
72bf9492
DJ
9367static void
9368add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9369 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9370 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9371{
72bf9492 9372 /* Add a symbol for the namespace. */
e7c27a73 9373
72bf9492 9374 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9375
9376 /* Now scan partial symbols in that namespace. */
9377
91c24f0a 9378 if (pdi->has_children)
cdc07690 9379 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9380}
9381
5d7cb8df
JK
9382/* Read a partial die corresponding to a Fortran module. */
9383
9384static void
9385add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9386 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9387{
530e8392
KB
9388 /* Add a symbol for the namespace. */
9389
9390 add_partial_symbol (pdi, cu);
9391
f55ee35c 9392 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9393
9394 if (pdi->has_children)
cdc07690 9395 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9396}
9397
bc30ff58
JB
9398/* Read a partial die corresponding to a subprogram and create a partial
9399 symbol for that subprogram. When the CU language allows it, this
9400 routine also defines a partial symbol for each nested subprogram
cdc07690 9401 that this subprogram contains. If SET_ADDRMAP is true, record the
428fc5fc
YQ
9402 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
9403 and highest PC values found in PDI.
6e70227d 9404
cdc07690
YQ
9405 PDI may also be a lexical block, in which case we simply search
9406 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9407 Again, this is only performed when the CU language allows this
9408 type of definitions. */
9409
9410static void
9411add_partial_subprogram (struct partial_die_info *pdi,
9412 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9413 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58
JB
9414{
9415 if (pdi->tag == DW_TAG_subprogram)
9416 {
9417 if (pdi->has_pc_info)
9418 {
9419 if (pdi->lowpc < *lowpc)
9420 *lowpc = pdi->lowpc;
9421 if (pdi->highpc > *highpc)
9422 *highpc = pdi->highpc;
cdc07690 9423 if (set_addrmap)
5734ee8b 9424 {
5734ee8b 9425 struct objfile *objfile = cu->objfile;
3e29f34a
MR
9426 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9427 CORE_ADDR baseaddr;
9428 CORE_ADDR highpc;
9429 CORE_ADDR lowpc;
5734ee8b
DJ
9430
9431 baseaddr = ANOFFSET (objfile->section_offsets,
9432 SECT_OFF_TEXT (objfile));
3e29f34a
MR
9433 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9434 pdi->lowpc + baseaddr);
9435 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9436 pdi->highpc + baseaddr);
9437 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 9438 cu->per_cu->v.psymtab);
5734ee8b 9439 }
481860b3
GB
9440 }
9441
9442 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9443 {
bc30ff58 9444 if (!pdi->is_declaration)
e8d05480
JB
9445 /* Ignore subprogram DIEs that do not have a name, they are
9446 illegal. Do not emit a complaint at this point, we will
9447 do so when we convert this psymtab into a symtab. */
9448 if (pdi->name)
9449 add_partial_symbol (pdi, cu);
bc30ff58
JB
9450 }
9451 }
6e70227d 9452
bc30ff58
JB
9453 if (! pdi->has_children)
9454 return;
9455
9456 if (cu->language == language_ada)
9457 {
9458 pdi = pdi->die_child;
9459 while (pdi != NULL)
9460 {
9461 fixup_partial_die (pdi, cu);
9462 if (pdi->tag == DW_TAG_subprogram
9463 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9464 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9465 pdi = pdi->die_sibling;
9466 }
9467 }
9468}
9469
91c24f0a
DC
9470/* Read a partial die corresponding to an enumeration type. */
9471
72bf9492
DJ
9472static void
9473add_partial_enumeration (struct partial_die_info *enum_pdi,
9474 struct dwarf2_cu *cu)
91c24f0a 9475{
72bf9492 9476 struct partial_die_info *pdi;
91c24f0a
DC
9477
9478 if (enum_pdi->name != NULL)
72bf9492
DJ
9479 add_partial_symbol (enum_pdi, cu);
9480
9481 pdi = enum_pdi->die_child;
9482 while (pdi)
91c24f0a 9483 {
72bf9492 9484 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 9485 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 9486 else
72bf9492
DJ
9487 add_partial_symbol (pdi, cu);
9488 pdi = pdi->die_sibling;
91c24f0a 9489 }
91c24f0a
DC
9490}
9491
6caca83c
CC
9492/* Return the initial uleb128 in the die at INFO_PTR. */
9493
9494static unsigned int
d521ce57 9495peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9496{
9497 unsigned int bytes_read;
9498
9499 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9500}
9501
4bb7a0a7
DJ
9502/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
9503 Return the corresponding abbrev, or NULL if the number is zero (indicating
9504 an empty DIE). In either case *BYTES_READ will be set to the length of
9505 the initial number. */
9506
9507static struct abbrev_info *
d521ce57 9508peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 9509 struct dwarf2_cu *cu)
4bb7a0a7
DJ
9510{
9511 bfd *abfd = cu->objfile->obfd;
9512 unsigned int abbrev_number;
9513 struct abbrev_info *abbrev;
9514
9515 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9516
9517 if (abbrev_number == 0)
9518 return NULL;
9519
433df2d4 9520 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
9521 if (!abbrev)
9522 {
422b9917
DE
9523 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9524 " at offset 0x%x [in module %s]"),
9525 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9c541725 9526 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9527 }
9528
9529 return abbrev;
9530}
9531
93311388
DE
9532/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9533 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9534 DIE. Any children of the skipped DIEs will also be skipped. */
9535
d521ce57
TT
9536static const gdb_byte *
9537skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9538{
dee91e82 9539 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
9540 struct abbrev_info *abbrev;
9541 unsigned int bytes_read;
9542
9543 while (1)
9544 {
9545 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9546 if (abbrev == NULL)
9547 return info_ptr + bytes_read;
9548 else
dee91e82 9549 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9550 }
9551}
9552
93311388
DE
9553/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9554 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9555 abbrev corresponding to that skipped uleb128 should be passed in
9556 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9557 children. */
9558
d521ce57
TT
9559static const gdb_byte *
9560skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9561 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9562{
9563 unsigned int bytes_read;
9564 struct attribute attr;
dee91e82
DE
9565 bfd *abfd = reader->abfd;
9566 struct dwarf2_cu *cu = reader->cu;
d521ce57 9567 const gdb_byte *buffer = reader->buffer;
f664829e 9568 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9569 unsigned int form, i;
9570
9571 for (i = 0; i < abbrev->num_attrs; i++)
9572 {
9573 /* The only abbrev we care about is DW_AT_sibling. */
9574 if (abbrev->attrs[i].name == DW_AT_sibling)
9575 {
dee91e82 9576 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9577 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9578 complaint (&symfile_complaints,
9579 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9580 else
b9502d3f 9581 {
9c541725
PA
9582 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9583 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9584
9585 if (sibling_ptr < info_ptr)
9586 complaint (&symfile_complaints,
9587 _("DW_AT_sibling points backwards"));
22869d73
KS
9588 else if (sibling_ptr > reader->buffer_end)
9589 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9590 else
9591 return sibling_ptr;
9592 }
4bb7a0a7
DJ
9593 }
9594
9595 /* If it isn't DW_AT_sibling, skip this attribute. */
9596 form = abbrev->attrs[i].form;
9597 skip_attribute:
9598 switch (form)
9599 {
4bb7a0a7 9600 case DW_FORM_ref_addr:
ae411497
TT
9601 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9602 and later it is offset sized. */
9603 if (cu->header.version == 2)
9604 info_ptr += cu->header.addr_size;
9605 else
9606 info_ptr += cu->header.offset_size;
9607 break;
36586728
TT
9608 case DW_FORM_GNU_ref_alt:
9609 info_ptr += cu->header.offset_size;
9610 break;
ae411497 9611 case DW_FORM_addr:
4bb7a0a7
DJ
9612 info_ptr += cu->header.addr_size;
9613 break;
9614 case DW_FORM_data1:
9615 case DW_FORM_ref1:
9616 case DW_FORM_flag:
9617 info_ptr += 1;
9618 break;
2dc7f7b3 9619 case DW_FORM_flag_present:
43988095 9620 case DW_FORM_implicit_const:
2dc7f7b3 9621 break;
4bb7a0a7
DJ
9622 case DW_FORM_data2:
9623 case DW_FORM_ref2:
9624 info_ptr += 2;
9625 break;
9626 case DW_FORM_data4:
9627 case DW_FORM_ref4:
9628 info_ptr += 4;
9629 break;
9630 case DW_FORM_data8:
9631 case DW_FORM_ref8:
55f1336d 9632 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9633 info_ptr += 8;
9634 break;
0224619f
JK
9635 case DW_FORM_data16:
9636 info_ptr += 16;
9637 break;
4bb7a0a7 9638 case DW_FORM_string:
9b1c24c8 9639 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9640 info_ptr += bytes_read;
9641 break;
2dc7f7b3 9642 case DW_FORM_sec_offset:
4bb7a0a7 9643 case DW_FORM_strp:
36586728 9644 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9645 info_ptr += cu->header.offset_size;
9646 break;
2dc7f7b3 9647 case DW_FORM_exprloc:
4bb7a0a7
DJ
9648 case DW_FORM_block:
9649 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9650 info_ptr += bytes_read;
9651 break;
9652 case DW_FORM_block1:
9653 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9654 break;
9655 case DW_FORM_block2:
9656 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9657 break;
9658 case DW_FORM_block4:
9659 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9660 break;
9661 case DW_FORM_sdata:
9662 case DW_FORM_udata:
9663 case DW_FORM_ref_udata:
3019eac3
DE
9664 case DW_FORM_GNU_addr_index:
9665 case DW_FORM_GNU_str_index:
d521ce57 9666 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9667 break;
9668 case DW_FORM_indirect:
9669 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9670 info_ptr += bytes_read;
9671 /* We need to continue parsing from here, so just go back to
9672 the top. */
9673 goto skip_attribute;
9674
9675 default:
3e43a32a
MS
9676 error (_("Dwarf Error: Cannot handle %s "
9677 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9678 dwarf_form_name (form),
9679 bfd_get_filename (abfd));
9680 }
9681 }
9682
9683 if (abbrev->has_children)
dee91e82 9684 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9685 else
9686 return info_ptr;
9687}
9688
93311388 9689/* Locate ORIG_PDI's sibling.
dee91e82 9690 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9691
d521ce57 9692static const gdb_byte *
dee91e82
DE
9693locate_pdi_sibling (const struct die_reader_specs *reader,
9694 struct partial_die_info *orig_pdi,
d521ce57 9695 const gdb_byte *info_ptr)
91c24f0a
DC
9696{
9697 /* Do we know the sibling already? */
72bf9492 9698
91c24f0a
DC
9699 if (orig_pdi->sibling)
9700 return orig_pdi->sibling;
9701
9702 /* Are there any children to deal with? */
9703
9704 if (!orig_pdi->has_children)
9705 return info_ptr;
9706
4bb7a0a7 9707 /* Skip the children the long way. */
91c24f0a 9708
dee91e82 9709 return skip_children (reader, info_ptr);
91c24f0a
DC
9710}
9711
257e7a09 9712/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9713 not NULL. */
c906108c
SS
9714
9715static void
257e7a09
YQ
9716dwarf2_read_symtab (struct partial_symtab *self,
9717 struct objfile *objfile)
c906108c 9718{
257e7a09 9719 if (self->readin)
c906108c 9720 {
442e4d9c 9721 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9722 self->filename);
442e4d9c
YQ
9723 }
9724 else
9725 {
9726 if (info_verbose)
c906108c 9727 {
442e4d9c 9728 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9729 self->filename);
442e4d9c 9730 gdb_flush (gdb_stdout);
c906108c 9731 }
c906108c 9732
442e4d9c 9733 /* Restore our global data. */
9a3c8263
SM
9734 dwarf2_per_objfile
9735 = (struct dwarf2_per_objfile *) objfile_data (objfile,
9736 dwarf2_objfile_data_key);
10b3939b 9737
442e4d9c
YQ
9738 /* If this psymtab is constructed from a debug-only objfile, the
9739 has_section_at_zero flag will not necessarily be correct. We
9740 can get the correct value for this flag by looking at the data
9741 associated with the (presumably stripped) associated objfile. */
9742 if (objfile->separate_debug_objfile_backlink)
9743 {
9744 struct dwarf2_per_objfile *dpo_backlink
9a3c8263
SM
9745 = ((struct dwarf2_per_objfile *)
9746 objfile_data (objfile->separate_debug_objfile_backlink,
9747 dwarf2_objfile_data_key));
9a619af0 9748
442e4d9c
YQ
9749 dwarf2_per_objfile->has_section_at_zero
9750 = dpo_backlink->has_section_at_zero;
9751 }
b2ab525c 9752
442e4d9c 9753 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9754
257e7a09 9755 psymtab_to_symtab_1 (self);
c906108c 9756
442e4d9c
YQ
9757 /* Finish up the debug error message. */
9758 if (info_verbose)
9759 printf_filtered (_("done.\n"));
c906108c 9760 }
95554aad
TT
9761
9762 process_cu_includes ();
c906108c 9763}
9cdd5dbd
DE
9764\f
9765/* Reading in full CUs. */
c906108c 9766
10b3939b
DJ
9767/* Add PER_CU to the queue. */
9768
9769static void
95554aad
TT
9770queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9771 enum language pretend_language)
10b3939b
DJ
9772{
9773 struct dwarf2_queue_item *item;
9774
9775 per_cu->queued = 1;
8d749320 9776 item = XNEW (struct dwarf2_queue_item);
10b3939b 9777 item->per_cu = per_cu;
95554aad 9778 item->pretend_language = pretend_language;
10b3939b
DJ
9779 item->next = NULL;
9780
9781 if (dwarf2_queue == NULL)
9782 dwarf2_queue = item;
9783 else
9784 dwarf2_queue_tail->next = item;
9785
9786 dwarf2_queue_tail = item;
9787}
9788
89e63ee4
DE
9789/* If PER_CU is not yet queued, add it to the queue.
9790 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9791 dependency.
0907af0c 9792 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9793 meaning either PER_CU is already queued or it is already loaded.
9794
9795 N.B. There is an invariant here that if a CU is queued then it is loaded.
9796 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9797
9798static int
89e63ee4 9799maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9800 struct dwarf2_per_cu_data *per_cu,
9801 enum language pretend_language)
9802{
9803 /* We may arrive here during partial symbol reading, if we need full
9804 DIEs to process an unusual case (e.g. template arguments). Do
9805 not queue PER_CU, just tell our caller to load its DIEs. */
9806 if (dwarf2_per_objfile->reading_partial_symbols)
9807 {
9808 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9809 return 1;
9810 return 0;
9811 }
9812
9813 /* Mark the dependence relation so that we don't flush PER_CU
9814 too early. */
89e63ee4
DE
9815 if (dependent_cu != NULL)
9816 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9817
9818 /* If it's already on the queue, we have nothing to do. */
9819 if (per_cu->queued)
9820 return 0;
9821
9822 /* If the compilation unit is already loaded, just mark it as
9823 used. */
9824 if (per_cu->cu != NULL)
9825 {
9826 per_cu->cu->last_used = 0;
9827 return 0;
9828 }
9829
9830 /* Add it to the queue. */
9831 queue_comp_unit (per_cu, pretend_language);
9832
9833 return 1;
9834}
9835
10b3939b
DJ
9836/* Process the queue. */
9837
9838static void
a0f42c21 9839process_queue (void)
10b3939b
DJ
9840{
9841 struct dwarf2_queue_item *item, *next_item;
9842
b4f54984 9843 if (dwarf_read_debug)
45cfd468
DE
9844 {
9845 fprintf_unfiltered (gdb_stdlog,
9846 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9847 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9848 }
9849
03dd20cc
DJ
9850 /* The queue starts out with one item, but following a DIE reference
9851 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9852 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9853 {
cc12ce38
DE
9854 if ((dwarf2_per_objfile->using_index
9855 ? !item->per_cu->v.quick->compunit_symtab
9856 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9857 /* Skip dummy CUs. */
9858 && item->per_cu->cu != NULL)
f4dc4d17
DE
9859 {
9860 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9861 unsigned int debug_print_threshold;
247f5c4f 9862 char buf[100];
f4dc4d17 9863
247f5c4f 9864 if (per_cu->is_debug_types)
f4dc4d17 9865 {
247f5c4f
DE
9866 struct signatured_type *sig_type =
9867 (struct signatured_type *) per_cu;
9868
9869 sprintf (buf, "TU %s at offset 0x%x",
73be47f5 9870 hex_string (sig_type->signature),
9c541725 9871 to_underlying (per_cu->sect_off));
73be47f5
DE
9872 /* There can be 100s of TUs.
9873 Only print them in verbose mode. */
9874 debug_print_threshold = 2;
f4dc4d17 9875 }
247f5c4f 9876 else
73be47f5 9877 {
9c541725
PA
9878 sprintf (buf, "CU at offset 0x%x",
9879 to_underlying (per_cu->sect_off));
73be47f5
DE
9880 debug_print_threshold = 1;
9881 }
247f5c4f 9882
b4f54984 9883 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9884 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9885
9886 if (per_cu->is_debug_types)
9887 process_full_type_unit (per_cu, item->pretend_language);
9888 else
9889 process_full_comp_unit (per_cu, item->pretend_language);
9890
b4f54984 9891 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9892 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9893 }
10b3939b
DJ
9894
9895 item->per_cu->queued = 0;
9896 next_item = item->next;
9897 xfree (item);
9898 }
9899
9900 dwarf2_queue_tail = NULL;
45cfd468 9901
b4f54984 9902 if (dwarf_read_debug)
45cfd468
DE
9903 {
9904 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9905 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9906 }
10b3939b
DJ
9907}
9908
9909/* Free all allocated queue entries. This function only releases anything if
9910 an error was thrown; if the queue was processed then it would have been
9911 freed as we went along. */
9912
9913static void
9914dwarf2_release_queue (void *dummy)
9915{
9916 struct dwarf2_queue_item *item, *last;
9917
9918 item = dwarf2_queue;
9919 while (item)
9920 {
9921 /* Anything still marked queued is likely to be in an
9922 inconsistent state, so discard it. */
9923 if (item->per_cu->queued)
9924 {
9925 if (item->per_cu->cu != NULL)
dee91e82 9926 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
9927 item->per_cu->queued = 0;
9928 }
9929
9930 last = item;
9931 item = item->next;
9932 xfree (last);
9933 }
9934
9935 dwarf2_queue = dwarf2_queue_tail = NULL;
9936}
9937
9938/* Read in full symbols for PST, and anything it depends on. */
9939
c906108c 9940static void
fba45db2 9941psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9942{
10b3939b 9943 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9944 int i;
9945
95554aad
TT
9946 if (pst->readin)
9947 return;
9948
aaa75496 9949 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9950 if (!pst->dependencies[i]->readin
9951 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9952 {
9953 /* Inform about additional files that need to be read in. */
9954 if (info_verbose)
9955 {
a3f17187 9956 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9957 fputs_filtered (" ", gdb_stdout);
9958 wrap_here ("");
9959 fputs_filtered ("and ", gdb_stdout);
9960 wrap_here ("");
9961 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9962 wrap_here (""); /* Flush output. */
aaa75496
JB
9963 gdb_flush (gdb_stdout);
9964 }
9965 psymtab_to_symtab_1 (pst->dependencies[i]);
9966 }
9967
9a3c8263 9968 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9969
9970 if (per_cu == NULL)
aaa75496
JB
9971 {
9972 /* It's an include file, no symbols to read for it.
9973 Everything is in the parent symtab. */
9974 pst->readin = 1;
9975 return;
9976 }
c906108c 9977
a0f42c21 9978 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
9979}
9980
dee91e82
DE
9981/* Trivial hash function for die_info: the hash value of a DIE
9982 is its offset in .debug_info for this objfile. */
10b3939b 9983
dee91e82
DE
9984static hashval_t
9985die_hash (const void *item)
10b3939b 9986{
9a3c8263 9987 const struct die_info *die = (const struct die_info *) item;
6502dd73 9988
9c541725 9989 return to_underlying (die->sect_off);
dee91e82 9990}
63d06c5c 9991
dee91e82
DE
9992/* Trivial comparison function for die_info structures: two DIEs
9993 are equal if they have the same offset. */
98bfdba5 9994
dee91e82
DE
9995static int
9996die_eq (const void *item_lhs, const void *item_rhs)
9997{
9a3c8263
SM
9998 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9999 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 10000
9c541725 10001 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 10002}
c906108c 10003
dee91e82
DE
10004/* die_reader_func for load_full_comp_unit.
10005 This is identical to read_signatured_type_reader,
10006 but is kept separate for now. */
c906108c 10007
dee91e82
DE
10008static void
10009load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 10010 const gdb_byte *info_ptr,
dee91e82
DE
10011 struct die_info *comp_unit_die,
10012 int has_children,
10013 void *data)
10014{
10015 struct dwarf2_cu *cu = reader->cu;
9a3c8263 10016 enum language *language_ptr = (enum language *) data;
6caca83c 10017
dee91e82
DE
10018 gdb_assert (cu->die_hash == NULL);
10019 cu->die_hash =
10020 htab_create_alloc_ex (cu->header.length / 12,
10021 die_hash,
10022 die_eq,
10023 NULL,
10024 &cu->comp_unit_obstack,
10025 hashtab_obstack_allocate,
10026 dummy_obstack_deallocate);
e142c38c 10027
dee91e82
DE
10028 if (has_children)
10029 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10030 &info_ptr, comp_unit_die);
10031 cu->dies = comp_unit_die;
10032 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
10033
10034 /* We try not to read any attributes in this function, because not
9cdd5dbd 10035 all CUs needed for references have been loaded yet, and symbol
10b3939b 10036 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
10037 or we won't be able to build types correctly.
10038 Similarly, if we do not read the producer, we can not apply
10039 producer-specific interpretation. */
95554aad 10040 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 10041}
10b3939b 10042
dee91e82 10043/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 10044
dee91e82 10045static void
95554aad
TT
10046load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10047 enum language pretend_language)
dee91e82 10048{
3019eac3 10049 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 10050
f4dc4d17
DE
10051 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10052 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
10053}
10054
3da10d80
KS
10055/* Add a DIE to the delayed physname list. */
10056
10057static void
10058add_to_method_list (struct type *type, int fnfield_index, int index,
10059 const char *name, struct die_info *die,
10060 struct dwarf2_cu *cu)
10061{
10062 struct delayed_method_info mi;
10063 mi.type = type;
10064 mi.fnfield_index = fnfield_index;
10065 mi.index = index;
10066 mi.name = name;
10067 mi.die = die;
10068 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
10069}
10070
10071/* A cleanup for freeing the delayed method list. */
10072
10073static void
10074free_delayed_list (void *ptr)
10075{
10076 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
10077 if (cu->method_list != NULL)
10078 {
10079 VEC_free (delayed_method_info, cu->method_list);
10080 cu->method_list = NULL;
10081 }
10082}
10083
3693fdb3
PA
10084/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10085 "const" / "volatile". If so, decrements LEN by the length of the
10086 modifier and return true. Otherwise return false. */
10087
10088template<size_t N>
10089static bool
10090check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10091{
10092 size_t mod_len = sizeof (mod) - 1;
10093 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10094 {
10095 len -= mod_len;
10096 return true;
10097 }
10098 return false;
10099}
10100
3da10d80
KS
10101/* Compute the physnames of any methods on the CU's method list.
10102
10103 The computation of method physnames is delayed in order to avoid the
10104 (bad) condition that one of the method's formal parameters is of an as yet
10105 incomplete type. */
10106
10107static void
10108compute_delayed_physnames (struct dwarf2_cu *cu)
10109{
10110 int i;
10111 struct delayed_method_info *mi;
3693fdb3
PA
10112
10113 /* Only C++ delays computing physnames. */
10114 if (VEC_empty (delayed_method_info, cu->method_list))
10115 return;
10116 gdb_assert (cu->language == language_cplus);
10117
3da10d80
KS
10118 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
10119 {
1d06ead6 10120 const char *physname;
3da10d80
KS
10121 struct fn_fieldlist *fn_flp
10122 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 10123 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
10124 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
10125 = physname ? physname : "";
3693fdb3
PA
10126
10127 /* Since there's no tag to indicate whether a method is a
10128 const/volatile overload, extract that information out of the
10129 demangled name. */
10130 if (physname != NULL)
10131 {
10132 size_t len = strlen (physname);
10133
10134 while (1)
10135 {
10136 if (physname[len] == ')') /* shortcut */
10137 break;
10138 else if (check_modifier (physname, len, " const"))
10139 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
10140 else if (check_modifier (physname, len, " volatile"))
10141 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
10142 else
10143 break;
10144 }
10145 }
3da10d80
KS
10146 }
10147}
10148
a766d390
DE
10149/* Go objects should be embedded in a DW_TAG_module DIE,
10150 and it's not clear if/how imported objects will appear.
10151 To keep Go support simple until that's worked out,
10152 go back through what we've read and create something usable.
10153 We could do this while processing each DIE, and feels kinda cleaner,
10154 but that way is more invasive.
10155 This is to, for example, allow the user to type "p var" or "b main"
10156 without having to specify the package name, and allow lookups
10157 of module.object to work in contexts that use the expression
10158 parser. */
10159
10160static void
10161fixup_go_packaging (struct dwarf2_cu *cu)
10162{
10163 char *package_name = NULL;
10164 struct pending *list;
10165 int i;
10166
10167 for (list = global_symbols; list != NULL; list = list->next)
10168 {
10169 for (i = 0; i < list->nsyms; ++i)
10170 {
10171 struct symbol *sym = list->symbol[i];
10172
10173 if (SYMBOL_LANGUAGE (sym) == language_go
10174 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10175 {
10176 char *this_package_name = go_symbol_package_name (sym);
10177
10178 if (this_package_name == NULL)
10179 continue;
10180 if (package_name == NULL)
10181 package_name = this_package_name;
10182 else
10183 {
10184 if (strcmp (package_name, this_package_name) != 0)
10185 complaint (&symfile_complaints,
10186 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
10187 (symbol_symtab (sym) != NULL
10188 ? symtab_to_filename_for_display
10189 (symbol_symtab (sym))
4262abfb 10190 : objfile_name (cu->objfile)),
a766d390
DE
10191 this_package_name, package_name);
10192 xfree (this_package_name);
10193 }
10194 }
10195 }
10196 }
10197
10198 if (package_name != NULL)
10199 {
10200 struct objfile *objfile = cu->objfile;
34a68019 10201 const char *saved_package_name
224c3ddb
SM
10202 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10203 package_name,
10204 strlen (package_name));
19f392bc
UW
10205 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10206 saved_package_name);
a766d390
DE
10207 struct symbol *sym;
10208
10209 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10210
e623cf5d 10211 sym = allocate_symbol (objfile);
f85f34ed 10212 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
10213 SYMBOL_SET_NAMES (sym, saved_package_name,
10214 strlen (saved_package_name), 0, objfile);
a766d390
DE
10215 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10216 e.g., "main" finds the "main" module and not C's main(). */
10217 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 10218 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
10219 SYMBOL_TYPE (sym) = type;
10220
10221 add_symbol_to_list (sym, &global_symbols);
10222
10223 xfree (package_name);
10224 }
10225}
10226
95554aad
TT
10227/* Return the symtab for PER_CU. This works properly regardless of
10228 whether we're using the index or psymtabs. */
10229
43f3e411
DE
10230static struct compunit_symtab *
10231get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad
TT
10232{
10233 return (dwarf2_per_objfile->using_index
43f3e411
DE
10234 ? per_cu->v.quick->compunit_symtab
10235 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10236}
10237
10238/* A helper function for computing the list of all symbol tables
10239 included by PER_CU. */
10240
10241static void
43f3e411 10242recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 10243 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10244 struct dwarf2_per_cu_data *per_cu,
43f3e411 10245 struct compunit_symtab *immediate_parent)
95554aad
TT
10246{
10247 void **slot;
10248 int ix;
43f3e411 10249 struct compunit_symtab *cust;
95554aad
TT
10250 struct dwarf2_per_cu_data *iter;
10251
10252 slot = htab_find_slot (all_children, per_cu, INSERT);
10253 if (*slot != NULL)
10254 {
10255 /* This inclusion and its children have been processed. */
10256 return;
10257 }
10258
10259 *slot = per_cu;
10260 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10261 cust = get_compunit_symtab (per_cu);
10262 if (cust != NULL)
ec94af83
DE
10263 {
10264 /* If this is a type unit only add its symbol table if we haven't
10265 seen it yet (type unit per_cu's can share symtabs). */
10266 if (per_cu->is_debug_types)
10267 {
43f3e411 10268 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10269 if (*slot == NULL)
10270 {
43f3e411
DE
10271 *slot = cust;
10272 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10273 if (cust->user == NULL)
10274 cust->user = immediate_parent;
ec94af83
DE
10275 }
10276 }
10277 else
f9125b6c 10278 {
43f3e411
DE
10279 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10280 if (cust->user == NULL)
10281 cust->user = immediate_parent;
f9125b6c 10282 }
ec94af83 10283 }
95554aad
TT
10284
10285 for (ix = 0;
796a7ff8 10286 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 10287 ++ix)
ec94af83
DE
10288 {
10289 recursively_compute_inclusions (result, all_children,
43f3e411 10290 all_type_symtabs, iter, cust);
ec94af83 10291 }
95554aad
TT
10292}
10293
43f3e411 10294/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10295 PER_CU. */
10296
10297static void
43f3e411 10298compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10299{
f4dc4d17
DE
10300 gdb_assert (! per_cu->is_debug_types);
10301
796a7ff8 10302 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
10303 {
10304 int ix, len;
ec94af83 10305 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
10306 struct compunit_symtab *compunit_symtab_iter;
10307 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 10308 htab_t all_children, all_type_symtabs;
43f3e411 10309 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10310
10311 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10312 if (cust == NULL)
95554aad
TT
10313 return;
10314
10315 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10316 NULL, xcalloc, xfree);
ec94af83
DE
10317 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10318 NULL, xcalloc, xfree);
95554aad
TT
10319
10320 for (ix = 0;
796a7ff8 10321 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 10322 ix, per_cu_iter);
95554aad 10323 ++ix)
ec94af83
DE
10324 {
10325 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 10326 all_type_symtabs, per_cu_iter,
43f3e411 10327 cust);
ec94af83 10328 }
95554aad 10329
ec94af83 10330 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
10331 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10332 cust->includes
8d749320
SM
10333 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
10334 struct compunit_symtab *, len + 1);
95554aad 10335 for (ix = 0;
43f3e411
DE
10336 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10337 compunit_symtab_iter);
95554aad 10338 ++ix)
43f3e411
DE
10339 cust->includes[ix] = compunit_symtab_iter;
10340 cust->includes[len] = NULL;
95554aad 10341
43f3e411 10342 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 10343 htab_delete (all_children);
ec94af83 10344 htab_delete (all_type_symtabs);
95554aad
TT
10345 }
10346}
10347
10348/* Compute the 'includes' field for the symtabs of all the CUs we just
10349 read. */
10350
10351static void
10352process_cu_includes (void)
10353{
10354 int ix;
10355 struct dwarf2_per_cu_data *iter;
10356
10357 for (ix = 0;
10358 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10359 ix, iter);
10360 ++ix)
f4dc4d17
DE
10361 {
10362 if (! iter->is_debug_types)
43f3e411 10363 compute_compunit_symtab_includes (iter);
f4dc4d17 10364 }
95554aad
TT
10365
10366 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10367}
10368
9cdd5dbd 10369/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10370 already been loaded into memory. */
10371
10372static void
95554aad
TT
10373process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10374 enum language pretend_language)
10b3939b 10375{
10b3939b 10376 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 10377 struct objfile *objfile = per_cu->objfile;
3e29f34a 10378 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10379 CORE_ADDR lowpc, highpc;
43f3e411 10380 struct compunit_symtab *cust;
33c7c59d 10381 struct cleanup *delayed_list_cleanup;
10b3939b 10382 CORE_ADDR baseaddr;
4359dff1 10383 struct block *static_block;
3e29f34a 10384 CORE_ADDR addr;
10b3939b
DJ
10385
10386 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10387
10b3939b 10388 buildsym_init ();
33c7c59d 10389 scoped_free_pendings free_pending;
3da10d80 10390 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
10391
10392 cu->list_in_scope = &file_symbols;
c906108c 10393
95554aad
TT
10394 cu->language = pretend_language;
10395 cu->language_defn = language_def (cu->language);
10396
c906108c 10397 /* Do line number decoding in read_file_scope () */
10b3939b 10398 process_die (cu->dies, cu);
c906108c 10399
a766d390
DE
10400 /* For now fudge the Go package. */
10401 if (cu->language == language_go)
10402 fixup_go_packaging (cu);
10403
3da10d80
KS
10404 /* Now that we have processed all the DIEs in the CU, all the types
10405 should be complete, and it should now be safe to compute all of the
10406 physnames. */
10407 compute_delayed_physnames (cu);
10408 do_cleanups (delayed_list_cleanup);
10409
fae299cd
DC
10410 /* Some compilers don't define a DW_AT_high_pc attribute for the
10411 compilation unit. If the DW_AT_high_pc is missing, synthesize
10412 it, by scanning the DIE's below the compilation unit. */
10b3939b 10413 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10414
3e29f34a
MR
10415 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10416 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10417
10418 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10419 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10420 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10421 addrmap to help ensure it has an accurate map of pc values belonging to
10422 this comp unit. */
10423 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10424
43f3e411
DE
10425 cust = end_symtab_from_static_block (static_block,
10426 SECT_OFF_TEXT (objfile), 0);
c906108c 10427
43f3e411 10428 if (cust != NULL)
c906108c 10429 {
df15bd07 10430 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10431
8be455d7
JK
10432 /* Set symtab language to language from DW_AT_language. If the
10433 compilation is from a C file generated by language preprocessors, do
10434 not set the language if it was already deduced by start_subfile. */
43f3e411 10435 if (!(cu->language == language_c
40e3ad0e 10436 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10437 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10438
10439 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10440 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10441 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10442 there were bugs in prologue debug info, fixed later in GCC-4.5
10443 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10444
10445 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10446 needed, it would be wrong due to missing DW_AT_producer there.
10447
10448 Still one can confuse GDB by using non-standard GCC compilation
10449 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10450 */
ab260dad 10451 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10452 cust->locations_valid = 1;
e0d00bc7
JK
10453
10454 if (gcc_4_minor >= 5)
43f3e411 10455 cust->epilogue_unwind_valid = 1;
96408a79 10456
43f3e411 10457 cust->call_site_htab = cu->call_site_htab;
c906108c 10458 }
9291a0cd
TT
10459
10460 if (dwarf2_per_objfile->using_index)
43f3e411 10461 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10462 else
10463 {
10464 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10465 pst->compunit_symtab = cust;
9291a0cd
TT
10466 pst->readin = 1;
10467 }
c906108c 10468
95554aad
TT
10469 /* Push it for inclusion processing later. */
10470 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
f4dc4d17 10471}
45cfd468 10472
f4dc4d17
DE
10473/* Generate full symbol information for type unit PER_CU, whose DIEs have
10474 already been loaded into memory. */
10475
10476static void
10477process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10478 enum language pretend_language)
10479{
10480 struct dwarf2_cu *cu = per_cu->cu;
10481 struct objfile *objfile = per_cu->objfile;
43f3e411 10482 struct compunit_symtab *cust;
33c7c59d 10483 struct cleanup *delayed_list_cleanup;
0186c6a7
DE
10484 struct signatured_type *sig_type;
10485
10486 gdb_assert (per_cu->is_debug_types);
10487 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
10488
10489 buildsym_init ();
33c7c59d 10490 scoped_free_pendings free_pending;
f4dc4d17
DE
10491 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10492
10493 cu->list_in_scope = &file_symbols;
10494
10495 cu->language = pretend_language;
10496 cu->language_defn = language_def (cu->language);
10497
10498 /* The symbol tables are set up in read_type_unit_scope. */
10499 process_die (cu->dies, cu);
10500
10501 /* For now fudge the Go package. */
10502 if (cu->language == language_go)
10503 fixup_go_packaging (cu);
10504
10505 /* Now that we have processed all the DIEs in the CU, all the types
10506 should be complete, and it should now be safe to compute all of the
10507 physnames. */
10508 compute_delayed_physnames (cu);
10509 do_cleanups (delayed_list_cleanup);
10510
10511 /* TUs share symbol tables.
10512 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10513 of it with end_expandable_symtab. Otherwise, complete the addition of
10514 this TU's symbols to the existing symtab. */
43f3e411 10515 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10516 {
43f3e411
DE
10517 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10518 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10519
43f3e411 10520 if (cust != NULL)
f4dc4d17
DE
10521 {
10522 /* Set symtab language to language from DW_AT_language. If the
10523 compilation is from a C file generated by language preprocessors,
10524 do not set the language if it was already deduced by
10525 start_subfile. */
43f3e411
DE
10526 if (!(cu->language == language_c
10527 && COMPUNIT_FILETABS (cust)->language != language_c))
10528 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10529 }
10530 }
10531 else
10532 {
0ab9ce85 10533 augment_type_symtab ();
43f3e411 10534 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10535 }
10536
10537 if (dwarf2_per_objfile->using_index)
43f3e411 10538 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10539 else
10540 {
10541 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10542 pst->compunit_symtab = cust;
f4dc4d17 10543 pst->readin = 1;
45cfd468 10544 }
c906108c
SS
10545}
10546
95554aad
TT
10547/* Process an imported unit DIE. */
10548
10549static void
10550process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10551{
10552 struct attribute *attr;
10553
f4dc4d17
DE
10554 /* For now we don't handle imported units in type units. */
10555 if (cu->per_cu->is_debug_types)
10556 {
10557 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10558 " supported in type units [in module %s]"),
4262abfb 10559 objfile_name (cu->objfile));
f4dc4d17
DE
10560 }
10561
95554aad
TT
10562 attr = dwarf2_attr (die, DW_AT_import, cu);
10563 if (attr != NULL)
10564 {
9c541725
PA
10565 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10566 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10567 dwarf2_per_cu_data *per_cu
10568 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
95554aad 10569
69d751e3 10570 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
10571 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10572 load_full_comp_unit (per_cu, cu->language);
10573
796a7ff8 10574 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
10575 per_cu);
10576 }
10577}
10578
4c8aa72d
PA
10579/* RAII object that represents a process_die scope: i.e.,
10580 starts/finishes processing a DIE. */
10581class process_die_scope
adde2bff 10582{
4c8aa72d
PA
10583public:
10584 process_die_scope (die_info *die, dwarf2_cu *cu)
10585 : m_die (die), m_cu (cu)
10586 {
10587 /* We should only be processing DIEs not already in process. */
10588 gdb_assert (!m_die->in_process);
10589 m_die->in_process = true;
10590 }
8c3cb9fa 10591
4c8aa72d
PA
10592 ~process_die_scope ()
10593 {
10594 m_die->in_process = false;
10595
10596 /* If we're done processing the DIE for the CU that owns the line
10597 header, we don't need the line header anymore. */
10598 if (m_cu->line_header_die_owner == m_die)
10599 {
10600 delete m_cu->line_header;
10601 m_cu->line_header = NULL;
10602 m_cu->line_header_die_owner = NULL;
10603 }
10604 }
10605
10606private:
10607 die_info *m_die;
10608 dwarf2_cu *m_cu;
10609};
adde2bff 10610
c906108c
SS
10611/* Process a die and its children. */
10612
10613static void
e7c27a73 10614process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10615{
4c8aa72d 10616 process_die_scope scope (die, cu);
adde2bff 10617
c906108c
SS
10618 switch (die->tag)
10619 {
10620 case DW_TAG_padding:
10621 break;
10622 case DW_TAG_compile_unit:
95554aad 10623 case DW_TAG_partial_unit:
e7c27a73 10624 read_file_scope (die, cu);
c906108c 10625 break;
348e048f
DE
10626 case DW_TAG_type_unit:
10627 read_type_unit_scope (die, cu);
10628 break;
c906108c 10629 case DW_TAG_subprogram:
c906108c 10630 case DW_TAG_inlined_subroutine:
edb3359d 10631 read_func_scope (die, cu);
c906108c
SS
10632 break;
10633 case DW_TAG_lexical_block:
14898363
L
10634 case DW_TAG_try_block:
10635 case DW_TAG_catch_block:
e7c27a73 10636 read_lexical_block_scope (die, cu);
c906108c 10637 break;
216f72a1 10638 case DW_TAG_call_site:
96408a79
SA
10639 case DW_TAG_GNU_call_site:
10640 read_call_site_scope (die, cu);
10641 break;
c906108c 10642 case DW_TAG_class_type:
680b30c7 10643 case DW_TAG_interface_type:
c906108c
SS
10644 case DW_TAG_structure_type:
10645 case DW_TAG_union_type:
134d01f1 10646 process_structure_scope (die, cu);
c906108c
SS
10647 break;
10648 case DW_TAG_enumeration_type:
134d01f1 10649 process_enumeration_scope (die, cu);
c906108c 10650 break;
134d01f1 10651
f792889a
DJ
10652 /* These dies have a type, but processing them does not create
10653 a symbol or recurse to process the children. Therefore we can
10654 read them on-demand through read_type_die. */
c906108c 10655 case DW_TAG_subroutine_type:
72019c9c 10656 case DW_TAG_set_type:
c906108c 10657 case DW_TAG_array_type:
c906108c 10658 case DW_TAG_pointer_type:
c906108c 10659 case DW_TAG_ptr_to_member_type:
c906108c 10660 case DW_TAG_reference_type:
4297a3f0 10661 case DW_TAG_rvalue_reference_type:
c906108c 10662 case DW_TAG_string_type:
c906108c 10663 break;
134d01f1 10664
c906108c 10665 case DW_TAG_base_type:
a02abb62 10666 case DW_TAG_subrange_type:
cb249c71 10667 case DW_TAG_typedef:
134d01f1
DJ
10668 /* Add a typedef symbol for the type definition, if it has a
10669 DW_AT_name. */
f792889a 10670 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10671 break;
c906108c 10672 case DW_TAG_common_block:
e7c27a73 10673 read_common_block (die, cu);
c906108c
SS
10674 break;
10675 case DW_TAG_common_inclusion:
10676 break;
d9fa45fe 10677 case DW_TAG_namespace:
4d4ec4e5 10678 cu->processing_has_namespace_info = 1;
e7c27a73 10679 read_namespace (die, cu);
d9fa45fe 10680 break;
5d7cb8df 10681 case DW_TAG_module:
4d4ec4e5 10682 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
10683 read_module (die, cu);
10684 break;
d9fa45fe 10685 case DW_TAG_imported_declaration:
74921315
KS
10686 cu->processing_has_namespace_info = 1;
10687 if (read_namespace_alias (die, cu))
10688 break;
10689 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 10690 case DW_TAG_imported_module:
4d4ec4e5 10691 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
10692 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10693 || cu->language != language_fortran))
10694 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10695 dwarf_tag_name (die->tag));
10696 read_import_statement (die, cu);
d9fa45fe 10697 break;
95554aad
TT
10698
10699 case DW_TAG_imported_unit:
10700 process_imported_unit_die (die, cu);
10701 break;
10702
71a3c369
TT
10703 case DW_TAG_variable:
10704 read_variable (die, cu);
10705 break;
10706
c906108c 10707 default:
e7c27a73 10708 new_symbol (die, NULL, cu);
c906108c
SS
10709 break;
10710 }
10711}
ca69b9e6
DE
10712\f
10713/* DWARF name computation. */
c906108c 10714
94af9270
KS
10715/* A helper function for dwarf2_compute_name which determines whether DIE
10716 needs to have the name of the scope prepended to the name listed in the
10717 die. */
10718
10719static int
10720die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10721{
1c809c68
TT
10722 struct attribute *attr;
10723
94af9270
KS
10724 switch (die->tag)
10725 {
10726 case DW_TAG_namespace:
10727 case DW_TAG_typedef:
10728 case DW_TAG_class_type:
10729 case DW_TAG_interface_type:
10730 case DW_TAG_structure_type:
10731 case DW_TAG_union_type:
10732 case DW_TAG_enumeration_type:
10733 case DW_TAG_enumerator:
10734 case DW_TAG_subprogram:
08a76f8a 10735 case DW_TAG_inlined_subroutine:
94af9270 10736 case DW_TAG_member:
74921315 10737 case DW_TAG_imported_declaration:
94af9270
KS
10738 return 1;
10739
10740 case DW_TAG_variable:
c2b0a229 10741 case DW_TAG_constant:
94af9270
KS
10742 /* We only need to prefix "globally" visible variables. These include
10743 any variable marked with DW_AT_external or any variable that
10744 lives in a namespace. [Variables in anonymous namespaces
10745 require prefixing, but they are not DW_AT_external.] */
10746
10747 if (dwarf2_attr (die, DW_AT_specification, cu))
10748 {
10749 struct dwarf2_cu *spec_cu = cu;
9a619af0 10750
94af9270
KS
10751 return die_needs_namespace (die_specification (die, &spec_cu),
10752 spec_cu);
10753 }
10754
1c809c68 10755 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10756 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10757 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10758 return 0;
10759 /* A variable in a lexical block of some kind does not need a
10760 namespace, even though in C++ such variables may be external
10761 and have a mangled name. */
10762 if (die->parent->tag == DW_TAG_lexical_block
10763 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10764 || die->parent->tag == DW_TAG_catch_block
10765 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10766 return 0;
10767 return 1;
94af9270
KS
10768
10769 default:
10770 return 0;
10771 }
10772}
10773
73b9be8b
KS
10774/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10775 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10776 defined for the given DIE. */
10777
10778static struct attribute *
10779dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10780{
10781 struct attribute *attr;
10782
10783 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10784 if (attr == NULL)
10785 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10786
10787 return attr;
10788}
10789
10790/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10791 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10792 defined for the given DIE. */
10793
10794static const char *
10795dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10796{
10797 const char *linkage_name;
10798
10799 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10800 if (linkage_name == NULL)
10801 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10802
10803 return linkage_name;
10804}
10805
94af9270 10806/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10807 compute the physname for the object, which include a method's:
9c37b5ae 10808 - formal parameters (C++),
a766d390 10809 - receiver type (Go),
a766d390
DE
10810
10811 The term "physname" is a bit confusing.
10812 For C++, for example, it is the demangled name.
10813 For Go, for example, it's the mangled name.
94af9270 10814
af6b7be1
JB
10815 For Ada, return the DIE's linkage name rather than the fully qualified
10816 name. PHYSNAME is ignored..
10817
94af9270
KS
10818 The result is allocated on the objfile_obstack and canonicalized. */
10819
10820static const char *
15d034d0
TT
10821dwarf2_compute_name (const char *name,
10822 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10823 int physname)
10824{
bb5ed363
DE
10825 struct objfile *objfile = cu->objfile;
10826
94af9270
KS
10827 if (name == NULL)
10828 name = dwarf2_name (die, cu);
10829
2ee7123e
DE
10830 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10831 but otherwise compute it by typename_concat inside GDB.
10832 FIXME: Actually this is not really true, or at least not always true.
10833 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10834 Fortran names because there is no mangling standard. So new_symbol_full
10835 will set the demangled name to the result of dwarf2_full_name, and it is
10836 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10837 if (cu->language == language_ada
10838 || (cu->language == language_fortran && physname))
10839 {
10840 /* For Ada unit, we prefer the linkage name over the name, as
10841 the former contains the exported name, which the user expects
10842 to be able to reference. Ideally, we want the user to be able
10843 to reference this entity using either natural or linkage name,
10844 but we haven't started looking at this enhancement yet. */
73b9be8b 10845 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10846
2ee7123e
DE
10847 if (linkage_name != NULL)
10848 return linkage_name;
f55ee35c
JK
10849 }
10850
94af9270
KS
10851 /* These are the only languages we know how to qualify names in. */
10852 if (name != NULL
9c37b5ae 10853 && (cu->language == language_cplus
c44af4eb
TT
10854 || cu->language == language_fortran || cu->language == language_d
10855 || cu->language == language_rust))
94af9270
KS
10856 {
10857 if (die_needs_namespace (die, cu))
10858 {
0d5cff50 10859 const char *prefix;
34a68019 10860 const char *canonical_name = NULL;
94af9270 10861
d7e74731
PA
10862 string_file buf;
10863
94af9270 10864 prefix = determine_prefix (die, cu);
94af9270
KS
10865 if (*prefix != '\0')
10866 {
f55ee35c
JK
10867 char *prefixed_name = typename_concat (NULL, prefix, name,
10868 physname, cu);
9a619af0 10869
d7e74731 10870 buf.puts (prefixed_name);
94af9270
KS
10871 xfree (prefixed_name);
10872 }
10873 else
d7e74731 10874 buf.puts (name);
94af9270 10875
98bfdba5
PA
10876 /* Template parameters may be specified in the DIE's DW_AT_name, or
10877 as children with DW_TAG_template_type_param or
10878 DW_TAG_value_type_param. If the latter, add them to the name
10879 here. If the name already has template parameters, then
10880 skip this step; some versions of GCC emit both, and
10881 it is more efficient to use the pre-computed name.
10882
10883 Something to keep in mind about this process: it is very
10884 unlikely, or in some cases downright impossible, to produce
10885 something that will match the mangled name of a function.
10886 If the definition of the function has the same debug info,
10887 we should be able to match up with it anyway. But fallbacks
10888 using the minimal symbol, for instance to find a method
10889 implemented in a stripped copy of libstdc++, will not work.
10890 If we do not have debug info for the definition, we will have to
10891 match them up some other way.
10892
10893 When we do name matching there is a related problem with function
10894 templates; two instantiated function templates are allowed to
10895 differ only by their return types, which we do not add here. */
10896
10897 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10898 {
10899 struct attribute *attr;
10900 struct die_info *child;
10901 int first = 1;
10902
10903 die->building_fullname = 1;
10904
10905 for (child = die->child; child != NULL; child = child->sibling)
10906 {
10907 struct type *type;
12df843f 10908 LONGEST value;
d521ce57 10909 const gdb_byte *bytes;
98bfdba5
PA
10910 struct dwarf2_locexpr_baton *baton;
10911 struct value *v;
10912
10913 if (child->tag != DW_TAG_template_type_param
10914 && child->tag != DW_TAG_template_value_param)
10915 continue;
10916
10917 if (first)
10918 {
d7e74731 10919 buf.puts ("<");
98bfdba5
PA
10920 first = 0;
10921 }
10922 else
d7e74731 10923 buf.puts (", ");
98bfdba5
PA
10924
10925 attr = dwarf2_attr (child, DW_AT_type, cu);
10926 if (attr == NULL)
10927 {
10928 complaint (&symfile_complaints,
10929 _("template parameter missing DW_AT_type"));
d7e74731 10930 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10931 continue;
10932 }
10933 type = die_type (child, cu);
10934
10935 if (child->tag == DW_TAG_template_type_param)
10936 {
d7e74731 10937 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
10938 continue;
10939 }
10940
10941 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10942 if (attr == NULL)
10943 {
10944 complaint (&symfile_complaints,
3e43a32a
MS
10945 _("template parameter missing "
10946 "DW_AT_const_value"));
d7e74731 10947 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10948 continue;
10949 }
10950
10951 dwarf2_const_value_attr (attr, type, name,
10952 &cu->comp_unit_obstack, cu,
10953 &value, &bytes, &baton);
10954
10955 if (TYPE_NOSIGN (type))
10956 /* GDB prints characters as NUMBER 'CHAR'. If that's
10957 changed, this can use value_print instead. */
d7e74731 10958 c_printchar (value, type, &buf);
98bfdba5
PA
10959 else
10960 {
10961 struct value_print_options opts;
10962
10963 if (baton != NULL)
10964 v = dwarf2_evaluate_loc_desc (type, NULL,
10965 baton->data,
10966 baton->size,
10967 baton->per_cu);
10968 else if (bytes != NULL)
10969 {
10970 v = allocate_value (type);
10971 memcpy (value_contents_writeable (v), bytes,
10972 TYPE_LENGTH (type));
10973 }
10974 else
10975 v = value_from_longest (type, value);
10976
3e43a32a
MS
10977 /* Specify decimal so that we do not depend on
10978 the radix. */
98bfdba5
PA
10979 get_formatted_print_options (&opts, 'd');
10980 opts.raw = 1;
d7e74731 10981 value_print (v, &buf, &opts);
98bfdba5
PA
10982 release_value (v);
10983 value_free (v);
10984 }
10985 }
10986
10987 die->building_fullname = 0;
10988
10989 if (!first)
10990 {
10991 /* Close the argument list, with a space if necessary
10992 (nested templates). */
d7e74731
PA
10993 if (!buf.empty () && buf.string ().back () == '>')
10994 buf.puts (" >");
98bfdba5 10995 else
d7e74731 10996 buf.puts (">");
98bfdba5
PA
10997 }
10998 }
10999
9c37b5ae 11000 /* For C++ methods, append formal parameter type
94af9270 11001 information, if PHYSNAME. */
6e70227d 11002
94af9270 11003 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 11004 && cu->language == language_cplus)
94af9270
KS
11005 {
11006 struct type *type = read_type_die (die, cu);
11007
d7e74731 11008 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 11009 &type_print_raw_options);
94af9270 11010
9c37b5ae 11011 if (cu->language == language_cplus)
94af9270 11012 {
60430eff
DJ
11013 /* Assume that an artificial first parameter is
11014 "this", but do not crash if it is not. RealView
11015 marks unnamed (and thus unused) parameters as
11016 artificial; there is no way to differentiate
11017 the two cases. */
94af9270
KS
11018 if (TYPE_NFIELDS (type) > 0
11019 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11020 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11021 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11022 0))))
d7e74731 11023 buf.puts (" const");
94af9270
KS
11024 }
11025 }
11026
d7e74731 11027 const std::string &intermediate_name = buf.string ();
94af9270
KS
11028
11029 if (cu->language == language_cplus)
34a68019 11030 canonical_name
322a8516 11031 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11032 &objfile->per_bfd->storage_obstack);
11033
11034 /* If we only computed INTERMEDIATE_NAME, or if
11035 INTERMEDIATE_NAME is already canonical, then we need to
11036 copy it to the appropriate obstack. */
322a8516 11037 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
11038 name = ((const char *)
11039 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
11040 intermediate_name.c_str (),
11041 intermediate_name.length ()));
34a68019
TT
11042 else
11043 name = canonical_name;
94af9270
KS
11044 }
11045 }
11046
11047 return name;
11048}
11049
0114d602
DJ
11050/* Return the fully qualified name of DIE, based on its DW_AT_name.
11051 If scope qualifiers are appropriate they will be added. The result
34a68019 11052 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11053 not have a name. NAME may either be from a previous call to
11054 dwarf2_name or NULL.
11055
9c37b5ae 11056 The output string will be canonicalized (if C++). */
0114d602
DJ
11057
11058static const char *
15d034d0 11059dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11060{
94af9270
KS
11061 return dwarf2_compute_name (name, die, cu, 0);
11062}
0114d602 11063
94af9270
KS
11064/* Construct a physname for the given DIE in CU. NAME may either be
11065 from a previous call to dwarf2_name or NULL. The result will be
11066 allocated on the objfile_objstack or NULL if the DIE does not have a
11067 name.
0114d602 11068
9c37b5ae 11069 The output string will be canonicalized (if C++). */
0114d602 11070
94af9270 11071static const char *
15d034d0 11072dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11073{
bb5ed363 11074 struct objfile *objfile = cu->objfile;
900e11f9 11075 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11076 int need_copy = 1;
11077
11078 /* In this case dwarf2_compute_name is just a shortcut not building anything
11079 on its own. */
11080 if (!die_needs_namespace (die, cu))
11081 return dwarf2_compute_name (name, die, cu, 1);
11082
73b9be8b 11083 mangled = dw2_linkage_name (die, cu);
900e11f9 11084
e98c9e7c
TT
11085 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11086 See https://github.com/rust-lang/rust/issues/32925. */
11087 if (cu->language == language_rust && mangled != NULL
11088 && strchr (mangled, '{') != NULL)
11089 mangled = NULL;
11090
900e11f9
JK
11091 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11092 has computed. */
791afaa2 11093 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11094 if (mangled != NULL)
900e11f9 11095 {
900e11f9
JK
11096 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
11097 type. It is easier for GDB users to search for such functions as
11098 `name(params)' than `long name(params)'. In such case the minimal
11099 symbol names do not match the full symbol names but for template
11100 functions there is never a need to look up their definition from their
11101 declaration so the only disadvantage remains the minimal symbol
11102 variant `long name(params)' does not have the proper inferior type.
11103 */
11104
a766d390
DE
11105 if (cu->language == language_go)
11106 {
11107 /* This is a lie, but we already lie to the caller new_symbol_full.
11108 new_symbol_full assumes we return the mangled name.
11109 This just undoes that lie until things are cleaned up. */
a766d390
DE
11110 }
11111 else
11112 {
791afaa2
TT
11113 demangled.reset (gdb_demangle (mangled,
11114 (DMGL_PARAMS | DMGL_ANSI
11115 | DMGL_RET_DROP)));
a766d390 11116 }
900e11f9 11117 if (demangled)
791afaa2 11118 canon = demangled.get ();
900e11f9
JK
11119 else
11120 {
11121 canon = mangled;
11122 need_copy = 0;
11123 }
11124 }
11125
11126 if (canon == NULL || check_physname)
11127 {
11128 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11129
11130 if (canon != NULL && strcmp (physname, canon) != 0)
11131 {
11132 /* It may not mean a bug in GDB. The compiler could also
11133 compute DW_AT_linkage_name incorrectly. But in such case
11134 GDB would need to be bug-to-bug compatible. */
11135
11136 complaint (&symfile_complaints,
11137 _("Computed physname <%s> does not match demangled <%s> "
11138 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9c541725 11139 physname, canon, mangled, to_underlying (die->sect_off),
4262abfb 11140 objfile_name (objfile));
900e11f9
JK
11141
11142 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11143 is available here - over computed PHYSNAME. It is safer
11144 against both buggy GDB and buggy compilers. */
11145
11146 retval = canon;
11147 }
11148 else
11149 {
11150 retval = physname;
11151 need_copy = 0;
11152 }
11153 }
11154 else
11155 retval = canon;
11156
11157 if (need_copy)
224c3ddb
SM
11158 retval = ((const char *)
11159 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11160 retval, strlen (retval)));
900e11f9 11161
900e11f9 11162 return retval;
0114d602
DJ
11163}
11164
74921315
KS
11165/* Inspect DIE in CU for a namespace alias. If one exists, record
11166 a new symbol for it.
11167
11168 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11169
11170static int
11171read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11172{
11173 struct attribute *attr;
11174
11175 /* If the die does not have a name, this is not a namespace
11176 alias. */
11177 attr = dwarf2_attr (die, DW_AT_name, cu);
11178 if (attr != NULL)
11179 {
11180 int num;
11181 struct die_info *d = die;
11182 struct dwarf2_cu *imported_cu = cu;
11183
11184 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11185 keep inspecting DIEs until we hit the underlying import. */
11186#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11187 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11188 {
11189 attr = dwarf2_attr (d, DW_AT_import, cu);
11190 if (attr == NULL)
11191 break;
11192
11193 d = follow_die_ref (d, attr, &imported_cu);
11194 if (d->tag != DW_TAG_imported_declaration)
11195 break;
11196 }
11197
11198 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11199 {
11200 complaint (&symfile_complaints,
11201 _("DIE at 0x%x has too many recursively imported "
9c541725 11202 "declarations"), to_underlying (d->sect_off));
74921315
KS
11203 return 0;
11204 }
11205
11206 if (attr != NULL)
11207 {
11208 struct type *type;
9c541725 11209 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11210
9c541725 11211 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11212 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11213 {
11214 /* This declaration is a global namespace alias. Add
11215 a symbol for it whose type is the aliased namespace. */
11216 new_symbol (die, type, cu);
11217 return 1;
11218 }
11219 }
11220 }
11221
11222 return 0;
11223}
11224
22cee43f
PMR
11225/* Return the using directives repository (global or local?) to use in the
11226 current context for LANGUAGE.
11227
11228 For Ada, imported declarations can materialize renamings, which *may* be
11229 global. However it is impossible (for now?) in DWARF to distinguish
11230 "external" imported declarations and "static" ones. As all imported
11231 declarations seem to be static in all other languages, make them all CU-wide
11232 global only in Ada. */
11233
11234static struct using_direct **
11235using_directives (enum language language)
11236{
11237 if (language == language_ada && context_stack_depth == 0)
11238 return &global_using_directives;
11239 else
11240 return &local_using_directives;
11241}
11242
27aa8d6a
SW
11243/* Read the import statement specified by the given die and record it. */
11244
11245static void
11246read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11247{
bb5ed363 11248 struct objfile *objfile = cu->objfile;
27aa8d6a 11249 struct attribute *import_attr;
32019081 11250 struct die_info *imported_die, *child_die;
de4affc9 11251 struct dwarf2_cu *imported_cu;
27aa8d6a 11252 const char *imported_name;
794684b6 11253 const char *imported_name_prefix;
13387711
SW
11254 const char *canonical_name;
11255 const char *import_alias;
11256 const char *imported_declaration = NULL;
794684b6 11257 const char *import_prefix;
eb1e02fd 11258 std::vector<const char *> excludes;
13387711 11259
27aa8d6a
SW
11260 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11261 if (import_attr == NULL)
11262 {
11263 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11264 dwarf_tag_name (die->tag));
11265 return;
11266 }
11267
de4affc9
CC
11268 imported_cu = cu;
11269 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11270 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11271 if (imported_name == NULL)
11272 {
11273 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11274
11275 The import in the following code:
11276 namespace A
11277 {
11278 typedef int B;
11279 }
11280
11281 int main ()
11282 {
11283 using A::B;
11284 B b;
11285 return b;
11286 }
11287
11288 ...
11289 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11290 <52> DW_AT_decl_file : 1
11291 <53> DW_AT_decl_line : 6
11292 <54> DW_AT_import : <0x75>
11293 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11294 <59> DW_AT_name : B
11295 <5b> DW_AT_decl_file : 1
11296 <5c> DW_AT_decl_line : 2
11297 <5d> DW_AT_type : <0x6e>
11298 ...
11299 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11300 <76> DW_AT_byte_size : 4
11301 <77> DW_AT_encoding : 5 (signed)
11302
11303 imports the wrong die ( 0x75 instead of 0x58 ).
11304 This case will be ignored until the gcc bug is fixed. */
11305 return;
11306 }
11307
82856980
SW
11308 /* Figure out the local name after import. */
11309 import_alias = dwarf2_name (die, cu);
27aa8d6a 11310
794684b6
SW
11311 /* Figure out where the statement is being imported to. */
11312 import_prefix = determine_prefix (die, cu);
11313
11314 /* Figure out what the scope of the imported die is and prepend it
11315 to the name of the imported die. */
de4affc9 11316 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11317
f55ee35c
JK
11318 if (imported_die->tag != DW_TAG_namespace
11319 && imported_die->tag != DW_TAG_module)
794684b6 11320 {
13387711
SW
11321 imported_declaration = imported_name;
11322 canonical_name = imported_name_prefix;
794684b6 11323 }
13387711 11324 else if (strlen (imported_name_prefix) > 0)
12aaed36 11325 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11326 imported_name_prefix,
11327 (cu->language == language_d ? "." : "::"),
11328 imported_name, (char *) NULL);
13387711
SW
11329 else
11330 canonical_name = imported_name;
794684b6 11331
32019081
JK
11332 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11333 for (child_die = die->child; child_die && child_die->tag;
11334 child_die = sibling_die (child_die))
11335 {
11336 /* DWARF-4: A Fortran use statement with a “rename list” may be
11337 represented by an imported module entry with an import attribute
11338 referring to the module and owned entries corresponding to those
11339 entities that are renamed as part of being imported. */
11340
11341 if (child_die->tag != DW_TAG_imported_declaration)
11342 {
11343 complaint (&symfile_complaints,
11344 _("child DW_TAG_imported_declaration expected "
11345 "- DIE at 0x%x [in module %s]"),
9c541725 11346 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
11347 continue;
11348 }
11349
11350 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11351 if (import_attr == NULL)
11352 {
11353 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11354 dwarf_tag_name (child_die->tag));
11355 continue;
11356 }
11357
11358 imported_cu = cu;
11359 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11360 &imported_cu);
11361 imported_name = dwarf2_name (imported_die, imported_cu);
11362 if (imported_name == NULL)
11363 {
11364 complaint (&symfile_complaints,
11365 _("child DW_TAG_imported_declaration has unknown "
11366 "imported name - DIE at 0x%x [in module %s]"),
9c541725 11367 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
11368 continue;
11369 }
11370
eb1e02fd 11371 excludes.push_back (imported_name);
32019081
JK
11372
11373 process_die (child_die, cu);
11374 }
11375
22cee43f
PMR
11376 add_using_directive (using_directives (cu->language),
11377 import_prefix,
11378 canonical_name,
11379 import_alias,
11380 imported_declaration,
11381 excludes,
11382 0,
11383 &objfile->objfile_obstack);
27aa8d6a
SW
11384}
11385
5230b05a
WT
11386/* ICC<14 does not output the required DW_AT_declaration on incomplete
11387 types, but gives them a size of zero. Starting with version 14,
11388 ICC is compatible with GCC. */
11389
11390static int
11391producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11392{
11393 if (!cu->checked_producer)
11394 check_producer (cu);
11395
11396 return cu->producer_is_icc_lt_14;
11397}
11398
1b80a9fa
JK
11399/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11400 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11401 this, it was first present in GCC release 4.3.0. */
11402
11403static int
11404producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11405{
11406 if (!cu->checked_producer)
11407 check_producer (cu);
11408
11409 return cu->producer_is_gcc_lt_4_3;
11410}
11411
d721ba37
PA
11412static file_and_directory
11413find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11414{
d721ba37
PA
11415 file_and_directory res;
11416
9291a0cd
TT
11417 /* Find the filename. Do not use dwarf2_name here, since the filename
11418 is not a source language identifier. */
d721ba37
PA
11419 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11420 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11421
d721ba37
PA
11422 if (res.comp_dir == NULL
11423 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11424 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11425 {
d721ba37
PA
11426 res.comp_dir_storage = ldirname (res.name);
11427 if (!res.comp_dir_storage.empty ())
11428 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11429 }
d721ba37 11430 if (res.comp_dir != NULL)
9291a0cd
TT
11431 {
11432 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11433 directory, get rid of it. */
d721ba37 11434 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11435
d721ba37
PA
11436 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11437 res.comp_dir = cp + 1;
9291a0cd
TT
11438 }
11439
d721ba37
PA
11440 if (res.name == NULL)
11441 res.name = "<unknown>";
11442
11443 return res;
9291a0cd
TT
11444}
11445
f4dc4d17
DE
11446/* Handle DW_AT_stmt_list for a compilation unit.
11447 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11448 COMP_DIR is the compilation directory. LOWPC is passed to
11449 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11450
11451static void
11452handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11453 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11454{
527f3840 11455 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11456 struct attribute *attr;
527f3840
JK
11457 struct line_header line_header_local;
11458 hashval_t line_header_local_hash;
527f3840
JK
11459 void **slot;
11460 int decode_mapping;
2ab95328 11461
f4dc4d17
DE
11462 gdb_assert (! cu->per_cu->is_debug_types);
11463
2ab95328 11464 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11465 if (attr == NULL)
11466 return;
11467
9c541725 11468 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11469
11470 /* The line header hash table is only created if needed (it exists to
11471 prevent redundant reading of the line table for partial_units).
11472 If we're given a partial_unit, we'll need it. If we're given a
11473 compile_unit, then use the line header hash table if it's already
11474 created, but don't create one just yet. */
11475
11476 if (dwarf2_per_objfile->line_header_hash == NULL
11477 && die->tag == DW_TAG_partial_unit)
2ab95328 11478 {
527f3840
JK
11479 dwarf2_per_objfile->line_header_hash
11480 = htab_create_alloc_ex (127, line_header_hash_voidp,
11481 line_header_eq_voidp,
11482 free_line_header_voidp,
11483 &objfile->objfile_obstack,
11484 hashtab_obstack_allocate,
11485 dummy_obstack_deallocate);
11486 }
2ab95328 11487
9c541725 11488 line_header_local.sect_off = line_offset;
527f3840
JK
11489 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11490 line_header_local_hash = line_header_hash (&line_header_local);
11491 if (dwarf2_per_objfile->line_header_hash != NULL)
11492 {
11493 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11494 &line_header_local,
11495 line_header_local_hash, NO_INSERT);
11496
11497 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11498 is not present in *SLOT (since if there is something in *SLOT then
11499 it will be for a partial_unit). */
11500 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11501 {
527f3840 11502 gdb_assert (*slot != NULL);
9a3c8263 11503 cu->line_header = (struct line_header *) *slot;
527f3840 11504 return;
dee91e82 11505 }
2ab95328 11506 }
527f3840
JK
11507
11508 /* dwarf_decode_line_header does not yet provide sufficient information.
11509 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11510 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11511 if (lh == NULL)
527f3840 11512 return;
4c8aa72d
PA
11513
11514 cu->line_header = lh.release ();
11515 cu->line_header_die_owner = die;
527f3840
JK
11516
11517 if (dwarf2_per_objfile->line_header_hash == NULL)
11518 slot = NULL;
11519 else
11520 {
11521 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11522 &line_header_local,
11523 line_header_local_hash, INSERT);
11524 gdb_assert (slot != NULL);
11525 }
11526 if (slot != NULL && *slot == NULL)
11527 {
11528 /* This newly decoded line number information unit will be owned
11529 by line_header_hash hash table. */
11530 *slot = cu->line_header;
4c8aa72d 11531 cu->line_header_die_owner = NULL;
527f3840
JK
11532 }
11533 else
11534 {
11535 /* We cannot free any current entry in (*slot) as that struct line_header
11536 may be already used by multiple CUs. Create only temporary decoded
11537 line_header for this CU - it may happen at most once for each line
11538 number information unit. And if we're not using line_header_hash
11539 then this is what we want as well. */
11540 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11541 }
11542 decode_mapping = (die->tag != DW_TAG_partial_unit);
11543 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11544 decode_mapping);
fff8551c 11545
2ab95328
TT
11546}
11547
95554aad 11548/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11549
c906108c 11550static void
e7c27a73 11551read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11552{
dee91e82 11553 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11554 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11555 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11556 CORE_ADDR highpc = ((CORE_ADDR) 0);
11557 struct attribute *attr;
c906108c 11558 struct die_info *child_die;
e142c38c 11559 CORE_ADDR baseaddr;
6e70227d 11560
e142c38c 11561 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11562
fae299cd 11563 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11564
11565 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11566 from finish_block. */
2acceee2 11567 if (lowpc == ((CORE_ADDR) -1))
c906108c 11568 lowpc = highpc;
3e29f34a 11569 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11570
d721ba37 11571 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11572
95554aad 11573 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 11574
f4b8a18d
KW
11575 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11576 standardised yet. As a workaround for the language detection we fall
11577 back to the DW_AT_producer string. */
11578 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11579 cu->language = language_opencl;
11580
3019eac3
DE
11581 /* Similar hack for Go. */
11582 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11583 set_cu_language (DW_LANG_Go, cu);
11584
d721ba37 11585 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11586
11587 /* Decode line number information if present. We do this before
11588 processing child DIEs, so that the line header table is available
11589 for DW_AT_decl_file. */
d721ba37 11590 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11591
11592 /* Process all dies in compilation unit. */
11593 if (die->child != NULL)
11594 {
11595 child_die = die->child;
11596 while (child_die && child_die->tag)
11597 {
11598 process_die (child_die, cu);
11599 child_die = sibling_die (child_die);
11600 }
11601 }
11602
11603 /* Decode macro information, if present. Dwarf 2 macro information
11604 refers to information in the line number info statement program
11605 header, so we can only read it if we've read the header
11606 successfully. */
0af92d60
JK
11607 attr = dwarf2_attr (die, DW_AT_macros, cu);
11608 if (attr == NULL)
11609 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11610 if (attr && cu->line_header)
11611 {
11612 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11613 complaint (&symfile_complaints,
0af92d60 11614 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11615
43f3e411 11616 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11617 }
11618 else
11619 {
11620 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11621 if (attr && cu->line_header)
11622 {
11623 unsigned int macro_offset = DW_UNSND (attr);
11624
43f3e411 11625 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11626 }
11627 }
3019eac3
DE
11628}
11629
f4dc4d17
DE
11630/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11631 Create the set of symtabs used by this TU, or if this TU is sharing
11632 symtabs with another TU and the symtabs have already been created
11633 then restore those symtabs in the line header.
11634 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
11635
11636static void
f4dc4d17 11637setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 11638{
f4dc4d17
DE
11639 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11640 struct type_unit_group *tu_group;
11641 int first_time;
3019eac3 11642 struct attribute *attr;
9c541725 11643 unsigned int i;
0186c6a7 11644 struct signatured_type *sig_type;
3019eac3 11645
f4dc4d17 11646 gdb_assert (per_cu->is_debug_types);
0186c6a7 11647 sig_type = (struct signatured_type *) per_cu;
3019eac3 11648
f4dc4d17 11649 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 11650
f4dc4d17 11651 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11652 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
11653 if (sig_type->type_unit_group == NULL)
11654 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11655 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11656
11657 /* If we've already processed this stmt_list there's no real need to
11658 do it again, we could fake it and just recreate the part we need
11659 (file name,index -> symtab mapping). If data shows this optimization
11660 is useful we can do it then. */
43f3e411 11661 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11662
11663 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11664 debug info. */
fff8551c 11665 line_header_up lh;
f4dc4d17 11666 if (attr != NULL)
3019eac3 11667 {
9c541725 11668 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
11669 lh = dwarf_decode_line_header (line_offset, cu);
11670 }
11671 if (lh == NULL)
11672 {
11673 if (first_time)
11674 dwarf2_start_symtab (cu, "", NULL, 0);
11675 else
11676 {
11677 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 11678 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11679 }
f4dc4d17 11680 return;
3019eac3
DE
11681 }
11682
4c8aa72d
PA
11683 cu->line_header = lh.release ();
11684 cu->line_header_die_owner = die;
3019eac3 11685
f4dc4d17
DE
11686 if (first_time)
11687 {
43f3e411 11688 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 11689
1fd60fc0
DE
11690 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11691 still initializing it, and our caller (a few levels up)
11692 process_full_type_unit still needs to know if this is the first
11693 time. */
11694
4c8aa72d
PA
11695 tu_group->num_symtabs = cu->line_header->file_names.size ();
11696 tu_group->symtabs = XNEWVEC (struct symtab *,
11697 cu->line_header->file_names.size ());
3019eac3 11698
4c8aa72d 11699 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11700 {
4c8aa72d 11701 file_entry &fe = cu->line_header->file_names[i];
3019eac3 11702
4c8aa72d 11703 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 11704
f4dc4d17
DE
11705 if (current_subfile->symtab == NULL)
11706 {
4c8aa72d
PA
11707 /* NOTE: start_subfile will recognize when it's been
11708 passed a file it has already seen. So we can't
11709 assume there's a simple mapping from
11710 cu->line_header->file_names to subfiles, plus
11711 cu->line_header->file_names may contain dups. */
43f3e411
DE
11712 current_subfile->symtab
11713 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
11714 }
11715
8c43009f
PA
11716 fe.symtab = current_subfile->symtab;
11717 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11718 }
11719 }
11720 else
3019eac3 11721 {
0ab9ce85 11722 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11723
4c8aa72d 11724 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11725 {
4c8aa72d 11726 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 11727
4c8aa72d 11728 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11729 }
3019eac3
DE
11730 }
11731
f4dc4d17
DE
11732 /* The main symtab is allocated last. Type units don't have DW_AT_name
11733 so they don't have a "real" (so to speak) symtab anyway.
11734 There is later code that will assign the main symtab to all symbols
11735 that don't have one. We need to handle the case of a symbol with a
11736 missing symtab (DW_AT_decl_file) anyway. */
11737}
3019eac3 11738
f4dc4d17
DE
11739/* Process DW_TAG_type_unit.
11740 For TUs we want to skip the first top level sibling if it's not the
11741 actual type being defined by this TU. In this case the first top
11742 level sibling is there to provide context only. */
3019eac3 11743
f4dc4d17
DE
11744static void
11745read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11746{
11747 struct die_info *child_die;
3019eac3 11748
f4dc4d17
DE
11749 prepare_one_comp_unit (cu, die, language_minimal);
11750
11751 /* Initialize (or reinitialize) the machinery for building symtabs.
11752 We do this before processing child DIEs, so that the line header table
11753 is available for DW_AT_decl_file. */
11754 setup_type_unit_groups (die, cu);
11755
11756 if (die->child != NULL)
11757 {
11758 child_die = die->child;
11759 while (child_die && child_die->tag)
11760 {
11761 process_die (child_die, cu);
11762 child_die = sibling_die (child_die);
11763 }
11764 }
3019eac3
DE
11765}
11766\f
80626a55
DE
11767/* DWO/DWP files.
11768
11769 http://gcc.gnu.org/wiki/DebugFission
11770 http://gcc.gnu.org/wiki/DebugFissionDWP
11771
11772 To simplify handling of both DWO files ("object" files with the DWARF info)
11773 and DWP files (a file with the DWOs packaged up into one file), we treat
11774 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11775
11776static hashval_t
11777hash_dwo_file (const void *item)
11778{
9a3c8263 11779 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11780 hashval_t hash;
3019eac3 11781
a2ce51a0
DE
11782 hash = htab_hash_string (dwo_file->dwo_name);
11783 if (dwo_file->comp_dir != NULL)
11784 hash += htab_hash_string (dwo_file->comp_dir);
11785 return hash;
3019eac3
DE
11786}
11787
11788static int
11789eq_dwo_file (const void *item_lhs, const void *item_rhs)
11790{
9a3c8263
SM
11791 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11792 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11793
a2ce51a0
DE
11794 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11795 return 0;
11796 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11797 return lhs->comp_dir == rhs->comp_dir;
11798 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11799}
11800
11801/* Allocate a hash table for DWO files. */
11802
11803static htab_t
11804allocate_dwo_file_hash_table (void)
11805{
11806 struct objfile *objfile = dwarf2_per_objfile->objfile;
11807
11808 return htab_create_alloc_ex (41,
11809 hash_dwo_file,
11810 eq_dwo_file,
11811 NULL,
11812 &objfile->objfile_obstack,
11813 hashtab_obstack_allocate,
11814 dummy_obstack_deallocate);
11815}
11816
80626a55
DE
11817/* Lookup DWO file DWO_NAME. */
11818
11819static void **
0ac5b59e 11820lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
11821{
11822 struct dwo_file find_entry;
11823 void **slot;
11824
11825 if (dwarf2_per_objfile->dwo_files == NULL)
11826 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
11827
11828 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
11829 find_entry.dwo_name = dwo_name;
11830 find_entry.comp_dir = comp_dir;
80626a55
DE
11831 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11832
11833 return slot;
11834}
11835
3019eac3
DE
11836static hashval_t
11837hash_dwo_unit (const void *item)
11838{
9a3c8263 11839 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11840
11841 /* This drops the top 32 bits of the id, but is ok for a hash. */
11842 return dwo_unit->signature;
11843}
11844
11845static int
11846eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11847{
9a3c8263
SM
11848 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11849 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11850
11851 /* The signature is assumed to be unique within the DWO file.
11852 So while object file CU dwo_id's always have the value zero,
11853 that's OK, assuming each object file DWO file has only one CU,
11854 and that's the rule for now. */
11855 return lhs->signature == rhs->signature;
11856}
11857
11858/* Allocate a hash table for DWO CUs,TUs.
11859 There is one of these tables for each of CUs,TUs for each DWO file. */
11860
11861static htab_t
11862allocate_dwo_unit_table (struct objfile *objfile)
11863{
11864 /* Start out with a pretty small number.
11865 Generally DWO files contain only one CU and maybe some TUs. */
11866 return htab_create_alloc_ex (3,
11867 hash_dwo_unit,
11868 eq_dwo_unit,
11869 NULL,
11870 &objfile->objfile_obstack,
11871 hashtab_obstack_allocate,
11872 dummy_obstack_deallocate);
11873}
11874
80626a55 11875/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11876
19c3d4c9 11877struct create_dwo_cu_data
3019eac3
DE
11878{
11879 struct dwo_file *dwo_file;
19c3d4c9 11880 struct dwo_unit dwo_unit;
3019eac3
DE
11881};
11882
19c3d4c9 11883/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11884
11885static void
19c3d4c9
DE
11886create_dwo_cu_reader (const struct die_reader_specs *reader,
11887 const gdb_byte *info_ptr,
11888 struct die_info *comp_unit_die,
11889 int has_children,
11890 void *datap)
3019eac3
DE
11891{
11892 struct dwarf2_cu *cu = reader->cu;
9c541725 11893 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11894 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11895 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11896 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11897 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11898 struct attribute *attr;
3019eac3
DE
11899
11900 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11901 if (attr == NULL)
11902 {
19c3d4c9
DE
11903 complaint (&symfile_complaints,
11904 _("Dwarf Error: debug entry at offset 0x%x is missing"
11905 " its dwo_id [in module %s]"),
9c541725 11906 to_underlying (sect_off), dwo_file->dwo_name);
3019eac3
DE
11907 return;
11908 }
11909
3019eac3
DE
11910 dwo_unit->dwo_file = dwo_file;
11911 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 11912 dwo_unit->section = section;
9c541725 11913 dwo_unit->sect_off = sect_off;
3019eac3
DE
11914 dwo_unit->length = cu->per_cu->length;
11915
b4f54984 11916 if (dwarf_read_debug)
4031ecc5 11917 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9c541725
PA
11918 to_underlying (sect_off),
11919 hex_string (dwo_unit->signature));
3019eac3
DE
11920}
11921
33c5cd75 11922/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11923 Note: This function processes DWO files only, not DWP files. */
3019eac3 11924
33c5cd75
DB
11925static void
11926create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
11927 htab_t &cus_htab)
3019eac3
DE
11928{
11929 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11930 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11931
33c5cd75
DB
11932 dwarf2_read_section (objfile, &section);
11933 info_ptr = section.buffer;
3019eac3
DE
11934
11935 if (info_ptr == NULL)
33c5cd75 11936 return;
3019eac3 11937
b4f54984 11938 if (dwarf_read_debug)
19c3d4c9
DE
11939 {
11940 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11941 get_section_name (&section),
11942 get_section_file_name (&section));
19c3d4c9 11943 }
3019eac3 11944
33c5cd75 11945 end_ptr = info_ptr + section.size;
3019eac3
DE
11946 while (info_ptr < end_ptr)
11947 {
11948 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
11949 struct create_dwo_cu_data create_dwo_cu_data;
11950 struct dwo_unit *dwo_unit;
11951 void **slot;
11952 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 11953
19c3d4c9
DE
11954 memset (&create_dwo_cu_data.dwo_unit, 0,
11955 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
11956 memset (&per_cu, 0, sizeof (per_cu));
11957 per_cu.objfile = objfile;
11958 per_cu.is_debug_types = 0;
33c5cd75
DB
11959 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11960 per_cu.section = &section;
c5ed0576 11961 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
11962
11963 init_cutu_and_read_dies_no_follow (
11964 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11965 info_ptr += per_cu.length;
11966
11967 // If the unit could not be parsed, skip it.
11968 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11969 continue;
3019eac3 11970
33c5cd75
DB
11971 if (cus_htab == NULL)
11972 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11973
33c5cd75
DB
11974 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11975 *dwo_unit = create_dwo_cu_data.dwo_unit;
11976 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11977 gdb_assert (slot != NULL);
11978 if (*slot != NULL)
19c3d4c9 11979 {
33c5cd75
DB
11980 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11981 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11982
33c5cd75
DB
11983 complaint (&symfile_complaints,
11984 _("debug cu entry at offset 0x%x is duplicate to"
11985 " the entry at offset 0x%x, signature %s"),
11986 to_underlying (sect_off), to_underlying (dup_sect_off),
11987 hex_string (dwo_unit->signature));
19c3d4c9 11988 }
33c5cd75 11989 *slot = (void *)dwo_unit;
3019eac3 11990 }
3019eac3
DE
11991}
11992
80626a55
DE
11993/* DWP file .debug_{cu,tu}_index section format:
11994 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11995
d2415c6c
DE
11996 DWP Version 1:
11997
80626a55
DE
11998 Both index sections have the same format, and serve to map a 64-bit
11999 signature to a set of section numbers. Each section begins with a header,
12000 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12001 indexes, and a pool of 32-bit section numbers. The index sections will be
12002 aligned at 8-byte boundaries in the file.
12003
d2415c6c
DE
12004 The index section header consists of:
12005
12006 V, 32 bit version number
12007 -, 32 bits unused
12008 N, 32 bit number of compilation units or type units in the index
12009 M, 32 bit number of slots in the hash table
80626a55 12010
d2415c6c 12011 Numbers are recorded using the byte order of the application binary.
80626a55 12012
d2415c6c
DE
12013 The hash table begins at offset 16 in the section, and consists of an array
12014 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12015 order of the application binary). Unused slots in the hash table are 0.
12016 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12017
d2415c6c
DE
12018 The parallel table begins immediately after the hash table
12019 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12020 array of 32-bit indexes (using the byte order of the application binary),
12021 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12022 table contains a 32-bit index into the pool of section numbers. For unused
12023 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12024
73869dc2
DE
12025 The pool of section numbers begins immediately following the hash table
12026 (at offset 16 + 12 * M from the beginning of the section). The pool of
12027 section numbers consists of an array of 32-bit words (using the byte order
12028 of the application binary). Each item in the array is indexed starting
12029 from 0. The hash table entry provides the index of the first section
12030 number in the set. Additional section numbers in the set follow, and the
12031 set is terminated by a 0 entry (section number 0 is not used in ELF).
12032
12033 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12034 section must be the first entry in the set, and the .debug_abbrev.dwo must
12035 be the second entry. Other members of the set may follow in any order.
12036
12037 ---
12038
12039 DWP Version 2:
12040
12041 DWP Version 2 combines all the .debug_info, etc. sections into one,
12042 and the entries in the index tables are now offsets into these sections.
12043 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12044 section.
12045
12046 Index Section Contents:
12047 Header
12048 Hash Table of Signatures dwp_hash_table.hash_table
12049 Parallel Table of Indices dwp_hash_table.unit_table
12050 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12051 Table of Section Sizes dwp_hash_table.v2.sizes
12052
12053 The index section header consists of:
12054
12055 V, 32 bit version number
12056 L, 32 bit number of columns in the table of section offsets
12057 N, 32 bit number of compilation units or type units in the index
12058 M, 32 bit number of slots in the hash table
12059
12060 Numbers are recorded using the byte order of the application binary.
12061
12062 The hash table has the same format as version 1.
12063 The parallel table of indices has the same format as version 1,
12064 except that the entries are origin-1 indices into the table of sections
12065 offsets and the table of section sizes.
12066
12067 The table of offsets begins immediately following the parallel table
12068 (at offset 16 + 12 * M from the beginning of the section). The table is
12069 a two-dimensional array of 32-bit words (using the byte order of the
12070 application binary), with L columns and N+1 rows, in row-major order.
12071 Each row in the array is indexed starting from 0. The first row provides
12072 a key to the remaining rows: each column in this row provides an identifier
12073 for a debug section, and the offsets in the same column of subsequent rows
12074 refer to that section. The section identifiers are:
12075
12076 DW_SECT_INFO 1 .debug_info.dwo
12077 DW_SECT_TYPES 2 .debug_types.dwo
12078 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12079 DW_SECT_LINE 4 .debug_line.dwo
12080 DW_SECT_LOC 5 .debug_loc.dwo
12081 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12082 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12083 DW_SECT_MACRO 8 .debug_macro.dwo
12084
12085 The offsets provided by the CU and TU index sections are the base offsets
12086 for the contributions made by each CU or TU to the corresponding section
12087 in the package file. Each CU and TU header contains an abbrev_offset
12088 field, used to find the abbreviations table for that CU or TU within the
12089 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12090 be interpreted as relative to the base offset given in the index section.
12091 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12092 should be interpreted as relative to the base offset for .debug_line.dwo,
12093 and offsets into other debug sections obtained from DWARF attributes should
12094 also be interpreted as relative to the corresponding base offset.
12095
12096 The table of sizes begins immediately following the table of offsets.
12097 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12098 with L columns and N rows, in row-major order. Each row in the array is
12099 indexed starting from 1 (row 0 is shared by the two tables).
12100
12101 ---
12102
12103 Hash table lookup is handled the same in version 1 and 2:
12104
12105 We assume that N and M will not exceed 2^32 - 1.
12106 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12107
d2415c6c
DE
12108 Given a 64-bit compilation unit signature or a type signature S, an entry
12109 in the hash table is located as follows:
80626a55 12110
d2415c6c
DE
12111 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12112 the low-order k bits all set to 1.
80626a55 12113
d2415c6c 12114 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12115
d2415c6c
DE
12116 3) If the hash table entry at index H matches the signature, use that
12117 entry. If the hash table entry at index H is unused (all zeroes),
12118 terminate the search: the signature is not present in the table.
80626a55 12119
d2415c6c 12120 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12121
d2415c6c 12122 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12123 to stop at an unused slot or find the match. */
80626a55
DE
12124
12125/* Create a hash table to map DWO IDs to their CU/TU entry in
12126 .debug_{info,types}.dwo in DWP_FILE.
12127 Returns NULL if there isn't one.
12128 Note: This function processes DWP files only, not DWO files. */
12129
12130static struct dwp_hash_table *
12131create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
12132{
12133 struct objfile *objfile = dwarf2_per_objfile->objfile;
12134 bfd *dbfd = dwp_file->dbfd;
948f8e3d 12135 const gdb_byte *index_ptr, *index_end;
80626a55 12136 struct dwarf2_section_info *index;
73869dc2 12137 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12138 struct dwp_hash_table *htab;
12139
12140 if (is_debug_types)
12141 index = &dwp_file->sections.tu_index;
12142 else
12143 index = &dwp_file->sections.cu_index;
12144
12145 if (dwarf2_section_empty_p (index))
12146 return NULL;
12147 dwarf2_read_section (objfile, index);
12148
12149 index_ptr = index->buffer;
12150 index_end = index_ptr + index->size;
12151
12152 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12153 index_ptr += 4;
12154 if (version == 2)
12155 nr_columns = read_4_bytes (dbfd, index_ptr);
12156 else
12157 nr_columns = 0;
12158 index_ptr += 4;
80626a55
DE
12159 nr_units = read_4_bytes (dbfd, index_ptr);
12160 index_ptr += 4;
12161 nr_slots = read_4_bytes (dbfd, index_ptr);
12162 index_ptr += 4;
12163
73869dc2 12164 if (version != 1 && version != 2)
80626a55 12165 {
21aa081e 12166 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12167 " [in module %s]"),
21aa081e 12168 pulongest (version), dwp_file->name);
80626a55
DE
12169 }
12170 if (nr_slots != (nr_slots & -nr_slots))
12171 {
21aa081e 12172 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12173 " is not power of 2 [in module %s]"),
21aa081e 12174 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12175 }
12176
12177 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12178 htab->version = version;
12179 htab->nr_columns = nr_columns;
80626a55
DE
12180 htab->nr_units = nr_units;
12181 htab->nr_slots = nr_slots;
12182 htab->hash_table = index_ptr;
12183 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12184
12185 /* Exit early if the table is empty. */
12186 if (nr_slots == 0 || nr_units == 0
12187 || (version == 2 && nr_columns == 0))
12188 {
12189 /* All must be zero. */
12190 if (nr_slots != 0 || nr_units != 0
12191 || (version == 2 && nr_columns != 0))
12192 {
12193 complaint (&symfile_complaints,
12194 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12195 " all zero [in modules %s]"),
12196 dwp_file->name);
12197 }
12198 return htab;
12199 }
12200
12201 if (version == 1)
12202 {
12203 htab->section_pool.v1.indices =
12204 htab->unit_table + sizeof (uint32_t) * nr_slots;
12205 /* It's harder to decide whether the section is too small in v1.
12206 V1 is deprecated anyway so we punt. */
12207 }
12208 else
12209 {
12210 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12211 int *ids = htab->section_pool.v2.section_ids;
12212 /* Reverse map for error checking. */
12213 int ids_seen[DW_SECT_MAX + 1];
12214 int i;
12215
12216 if (nr_columns < 2)
12217 {
12218 error (_("Dwarf Error: bad DWP hash table, too few columns"
12219 " in section table [in module %s]"),
12220 dwp_file->name);
12221 }
12222 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12223 {
12224 error (_("Dwarf Error: bad DWP hash table, too many columns"
12225 " in section table [in module %s]"),
12226 dwp_file->name);
12227 }
12228 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12229 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12230 for (i = 0; i < nr_columns; ++i)
12231 {
12232 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12233
12234 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12235 {
12236 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12237 " in section table [in module %s]"),
12238 id, dwp_file->name);
12239 }
12240 if (ids_seen[id] != -1)
12241 {
12242 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12243 " id %d in section table [in module %s]"),
12244 id, dwp_file->name);
12245 }
12246 ids_seen[id] = i;
12247 ids[i] = id;
12248 }
12249 /* Must have exactly one info or types section. */
12250 if (((ids_seen[DW_SECT_INFO] != -1)
12251 + (ids_seen[DW_SECT_TYPES] != -1))
12252 != 1)
12253 {
12254 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12255 " DWO info/types section [in module %s]"),
12256 dwp_file->name);
12257 }
12258 /* Must have an abbrev section. */
12259 if (ids_seen[DW_SECT_ABBREV] == -1)
12260 {
12261 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12262 " section [in module %s]"),
12263 dwp_file->name);
12264 }
12265 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12266 htab->section_pool.v2.sizes =
12267 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12268 * nr_units * nr_columns);
12269 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12270 * nr_units * nr_columns))
12271 > index_end)
12272 {
12273 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12274 " [in module %s]"),
12275 dwp_file->name);
12276 }
12277 }
80626a55
DE
12278
12279 return htab;
12280}
12281
12282/* Update SECTIONS with the data from SECTP.
12283
12284 This function is like the other "locate" section routines that are
12285 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12286 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12287
12288 The result is non-zero for success, or zero if an error was found. */
12289
12290static int
73869dc2
DE
12291locate_v1_virtual_dwo_sections (asection *sectp,
12292 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12293{
12294 const struct dwop_section_names *names = &dwop_section_names;
12295
12296 if (section_is_p (sectp->name, &names->abbrev_dwo))
12297 {
12298 /* There can be only one. */
049412e3 12299 if (sections->abbrev.s.section != NULL)
80626a55 12300 return 0;
049412e3 12301 sections->abbrev.s.section = sectp;
80626a55
DE
12302 sections->abbrev.size = bfd_get_section_size (sectp);
12303 }
12304 else if (section_is_p (sectp->name, &names->info_dwo)
12305 || section_is_p (sectp->name, &names->types_dwo))
12306 {
12307 /* There can be only one. */
049412e3 12308 if (sections->info_or_types.s.section != NULL)
80626a55 12309 return 0;
049412e3 12310 sections->info_or_types.s.section = sectp;
80626a55
DE
12311 sections->info_or_types.size = bfd_get_section_size (sectp);
12312 }
12313 else if (section_is_p (sectp->name, &names->line_dwo))
12314 {
12315 /* There can be only one. */
049412e3 12316 if (sections->line.s.section != NULL)
80626a55 12317 return 0;
049412e3 12318 sections->line.s.section = sectp;
80626a55
DE
12319 sections->line.size = bfd_get_section_size (sectp);
12320 }
12321 else if (section_is_p (sectp->name, &names->loc_dwo))
12322 {
12323 /* There can be only one. */
049412e3 12324 if (sections->loc.s.section != NULL)
80626a55 12325 return 0;
049412e3 12326 sections->loc.s.section = sectp;
80626a55
DE
12327 sections->loc.size = bfd_get_section_size (sectp);
12328 }
12329 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12330 {
12331 /* There can be only one. */
049412e3 12332 if (sections->macinfo.s.section != NULL)
80626a55 12333 return 0;
049412e3 12334 sections->macinfo.s.section = sectp;
80626a55
DE
12335 sections->macinfo.size = bfd_get_section_size (sectp);
12336 }
12337 else if (section_is_p (sectp->name, &names->macro_dwo))
12338 {
12339 /* There can be only one. */
049412e3 12340 if (sections->macro.s.section != NULL)
80626a55 12341 return 0;
049412e3 12342 sections->macro.s.section = sectp;
80626a55
DE
12343 sections->macro.size = bfd_get_section_size (sectp);
12344 }
12345 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12346 {
12347 /* There can be only one. */
049412e3 12348 if (sections->str_offsets.s.section != NULL)
80626a55 12349 return 0;
049412e3 12350 sections->str_offsets.s.section = sectp;
80626a55
DE
12351 sections->str_offsets.size = bfd_get_section_size (sectp);
12352 }
12353 else
12354 {
12355 /* No other kind of section is valid. */
12356 return 0;
12357 }
12358
12359 return 1;
12360}
12361
73869dc2
DE
12362/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12363 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12364 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12365 This is for DWP version 1 files. */
80626a55
DE
12366
12367static struct dwo_unit *
73869dc2
DE
12368create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
12369 uint32_t unit_index,
12370 const char *comp_dir,
12371 ULONGEST signature, int is_debug_types)
80626a55
DE
12372{
12373 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12374 const struct dwp_hash_table *dwp_htab =
12375 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
12376 bfd *dbfd = dwp_file->dbfd;
12377 const char *kind = is_debug_types ? "TU" : "CU";
12378 struct dwo_file *dwo_file;
12379 struct dwo_unit *dwo_unit;
73869dc2 12380 struct virtual_v1_dwo_sections sections;
80626a55 12381 void **dwo_file_slot;
80626a55
DE
12382 int i;
12383
73869dc2
DE
12384 gdb_assert (dwp_file->version == 1);
12385
b4f54984 12386 if (dwarf_read_debug)
80626a55 12387 {
73869dc2 12388 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12389 kind,
73869dc2 12390 pulongest (unit_index), hex_string (signature),
80626a55
DE
12391 dwp_file->name);
12392 }
12393
19ac8c2e 12394 /* Fetch the sections of this DWO unit.
80626a55
DE
12395 Put a limit on the number of sections we look for so that bad data
12396 doesn't cause us to loop forever. */
12397
73869dc2 12398#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12399 (1 /* .debug_info or .debug_types */ \
12400 + 1 /* .debug_abbrev */ \
12401 + 1 /* .debug_line */ \
12402 + 1 /* .debug_loc */ \
12403 + 1 /* .debug_str_offsets */ \
19ac8c2e 12404 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12405 + 1 /* trailing zero */)
12406
12407 memset (&sections, 0, sizeof (sections));
80626a55 12408
73869dc2 12409 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12410 {
12411 asection *sectp;
12412 uint32_t section_nr =
12413 read_4_bytes (dbfd,
73869dc2
DE
12414 dwp_htab->section_pool.v1.indices
12415 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12416
12417 if (section_nr == 0)
12418 break;
12419 if (section_nr >= dwp_file->num_sections)
12420 {
12421 error (_("Dwarf Error: bad DWP hash table, section number too large"
12422 " [in module %s]"),
12423 dwp_file->name);
12424 }
12425
12426 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12427 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12428 {
12429 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12430 " [in module %s]"),
12431 dwp_file->name);
12432 }
12433 }
12434
12435 if (i < 2
a32a8923
DE
12436 || dwarf2_section_empty_p (&sections.info_or_types)
12437 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12438 {
12439 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12440 " [in module %s]"),
12441 dwp_file->name);
12442 }
73869dc2 12443 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12444 {
12445 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12446 " [in module %s]"),
12447 dwp_file->name);
12448 }
12449
12450 /* It's easier for the rest of the code if we fake a struct dwo_file and
12451 have dwo_unit "live" in that. At least for now.
12452
12453 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12454 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12455 file, we can combine them back into a virtual DWO file to save space
12456 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12457 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12458
791afaa2
TT
12459 std::string virtual_dwo_name =
12460 string_printf ("virtual-dwo/%d-%d-%d-%d",
12461 get_section_id (&sections.abbrev),
12462 get_section_id (&sections.line),
12463 get_section_id (&sections.loc),
12464 get_section_id (&sections.str_offsets));
80626a55 12465 /* Can we use an existing virtual DWO file? */
791afaa2 12466 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
80626a55
DE
12467 /* Create one if necessary. */
12468 if (*dwo_file_slot == NULL)
12469 {
b4f54984 12470 if (dwarf_read_debug)
80626a55
DE
12471 {
12472 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12473 virtual_dwo_name.c_str ());
80626a55
DE
12474 }
12475 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12476 dwo_file->dwo_name
12477 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12478 virtual_dwo_name.c_str (),
12479 virtual_dwo_name.size ());
0ac5b59e 12480 dwo_file->comp_dir = comp_dir;
80626a55
DE
12481 dwo_file->sections.abbrev = sections.abbrev;
12482 dwo_file->sections.line = sections.line;
12483 dwo_file->sections.loc = sections.loc;
12484 dwo_file->sections.macinfo = sections.macinfo;
12485 dwo_file->sections.macro = sections.macro;
12486 dwo_file->sections.str_offsets = sections.str_offsets;
12487 /* The "str" section is global to the entire DWP file. */
12488 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12489 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12490 there's no need to record it in dwo_file.
12491 Also, we can't simply record type sections in dwo_file because
12492 we record a pointer into the vector in dwo_unit. As we collect more
12493 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12494 for it, invalidating all copies of pointers into the previous
12495 contents. */
80626a55
DE
12496 *dwo_file_slot = dwo_file;
12497 }
12498 else
12499 {
b4f54984 12500 if (dwarf_read_debug)
80626a55
DE
12501 {
12502 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12503 virtual_dwo_name.c_str ());
80626a55 12504 }
9a3c8263 12505 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12506 }
80626a55
DE
12507
12508 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12509 dwo_unit->dwo_file = dwo_file;
12510 dwo_unit->signature = signature;
8d749320
SM
12511 dwo_unit->section =
12512 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12513 *dwo_unit->section = sections.info_or_types;
57d63ce2 12514 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12515
12516 return dwo_unit;
12517}
12518
73869dc2
DE
12519/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12520 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12521 piece within that section used by a TU/CU, return a virtual section
12522 of just that piece. */
12523
12524static struct dwarf2_section_info
12525create_dwp_v2_section (struct dwarf2_section_info *section,
12526 bfd_size_type offset, bfd_size_type size)
12527{
12528 struct dwarf2_section_info result;
12529 asection *sectp;
12530
12531 gdb_assert (section != NULL);
12532 gdb_assert (!section->is_virtual);
12533
12534 memset (&result, 0, sizeof (result));
12535 result.s.containing_section = section;
12536 result.is_virtual = 1;
12537
12538 if (size == 0)
12539 return result;
12540
12541 sectp = get_section_bfd_section (section);
12542
12543 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12544 bounds of the real section. This is a pretty-rare event, so just
12545 flag an error (easier) instead of a warning and trying to cope. */
12546 if (sectp == NULL
12547 || offset + size > bfd_get_section_size (sectp))
12548 {
73869dc2
DE
12549 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12550 " in section %s [in module %s]"),
12551 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12552 objfile_name (dwarf2_per_objfile->objfile));
12553 }
12554
12555 result.virtual_offset = offset;
12556 result.size = size;
12557 return result;
12558}
12559
12560/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12561 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12562 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12563 This is for DWP version 2 files. */
12564
12565static struct dwo_unit *
12566create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
12567 uint32_t unit_index,
12568 const char *comp_dir,
12569 ULONGEST signature, int is_debug_types)
12570{
12571 struct objfile *objfile = dwarf2_per_objfile->objfile;
12572 const struct dwp_hash_table *dwp_htab =
12573 is_debug_types ? dwp_file->tus : dwp_file->cus;
12574 bfd *dbfd = dwp_file->dbfd;
12575 const char *kind = is_debug_types ? "TU" : "CU";
12576 struct dwo_file *dwo_file;
12577 struct dwo_unit *dwo_unit;
12578 struct virtual_v2_dwo_sections sections;
12579 void **dwo_file_slot;
73869dc2
DE
12580 int i;
12581
12582 gdb_assert (dwp_file->version == 2);
12583
b4f54984 12584 if (dwarf_read_debug)
73869dc2
DE
12585 {
12586 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12587 kind,
12588 pulongest (unit_index), hex_string (signature),
12589 dwp_file->name);
12590 }
12591
12592 /* Fetch the section offsets of this DWO unit. */
12593
12594 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12595
12596 for (i = 0; i < dwp_htab->nr_columns; ++i)
12597 {
12598 uint32_t offset = read_4_bytes (dbfd,
12599 dwp_htab->section_pool.v2.offsets
12600 + (((unit_index - 1) * dwp_htab->nr_columns
12601 + i)
12602 * sizeof (uint32_t)));
12603 uint32_t size = read_4_bytes (dbfd,
12604 dwp_htab->section_pool.v2.sizes
12605 + (((unit_index - 1) * dwp_htab->nr_columns
12606 + i)
12607 * sizeof (uint32_t)));
12608
12609 switch (dwp_htab->section_pool.v2.section_ids[i])
12610 {
12611 case DW_SECT_INFO:
12612 case DW_SECT_TYPES:
12613 sections.info_or_types_offset = offset;
12614 sections.info_or_types_size = size;
12615 break;
12616 case DW_SECT_ABBREV:
12617 sections.abbrev_offset = offset;
12618 sections.abbrev_size = size;
12619 break;
12620 case DW_SECT_LINE:
12621 sections.line_offset = offset;
12622 sections.line_size = size;
12623 break;
12624 case DW_SECT_LOC:
12625 sections.loc_offset = offset;
12626 sections.loc_size = size;
12627 break;
12628 case DW_SECT_STR_OFFSETS:
12629 sections.str_offsets_offset = offset;
12630 sections.str_offsets_size = size;
12631 break;
12632 case DW_SECT_MACINFO:
12633 sections.macinfo_offset = offset;
12634 sections.macinfo_size = size;
12635 break;
12636 case DW_SECT_MACRO:
12637 sections.macro_offset = offset;
12638 sections.macro_size = size;
12639 break;
12640 }
12641 }
12642
12643 /* It's easier for the rest of the code if we fake a struct dwo_file and
12644 have dwo_unit "live" in that. At least for now.
12645
12646 The DWP file can be made up of a random collection of CUs and TUs.
12647 However, for each CU + set of TUs that came from the same original DWO
12648 file, we can combine them back into a virtual DWO file to save space
12649 (fewer struct dwo_file objects to allocate). Remember that for really
12650 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12651
791afaa2
TT
12652 std::string virtual_dwo_name =
12653 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12654 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12655 (long) (sections.line_size ? sections.line_offset : 0),
12656 (long) (sections.loc_size ? sections.loc_offset : 0),
12657 (long) (sections.str_offsets_size
12658 ? sections.str_offsets_offset : 0));
73869dc2 12659 /* Can we use an existing virtual DWO file? */
791afaa2 12660 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
73869dc2
DE
12661 /* Create one if necessary. */
12662 if (*dwo_file_slot == NULL)
12663 {
b4f54984 12664 if (dwarf_read_debug)
73869dc2
DE
12665 {
12666 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12667 virtual_dwo_name.c_str ());
73869dc2
DE
12668 }
12669 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12670 dwo_file->dwo_name
12671 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12672 virtual_dwo_name.c_str (),
12673 virtual_dwo_name.size ());
73869dc2
DE
12674 dwo_file->comp_dir = comp_dir;
12675 dwo_file->sections.abbrev =
12676 create_dwp_v2_section (&dwp_file->sections.abbrev,
12677 sections.abbrev_offset, sections.abbrev_size);
12678 dwo_file->sections.line =
12679 create_dwp_v2_section (&dwp_file->sections.line,
12680 sections.line_offset, sections.line_size);
12681 dwo_file->sections.loc =
12682 create_dwp_v2_section (&dwp_file->sections.loc,
12683 sections.loc_offset, sections.loc_size);
12684 dwo_file->sections.macinfo =
12685 create_dwp_v2_section (&dwp_file->sections.macinfo,
12686 sections.macinfo_offset, sections.macinfo_size);
12687 dwo_file->sections.macro =
12688 create_dwp_v2_section (&dwp_file->sections.macro,
12689 sections.macro_offset, sections.macro_size);
12690 dwo_file->sections.str_offsets =
12691 create_dwp_v2_section (&dwp_file->sections.str_offsets,
12692 sections.str_offsets_offset,
12693 sections.str_offsets_size);
12694 /* The "str" section is global to the entire DWP file. */
12695 dwo_file->sections.str = dwp_file->sections.str;
12696 /* The info or types section is assigned below to dwo_unit,
12697 there's no need to record it in dwo_file.
12698 Also, we can't simply record type sections in dwo_file because
12699 we record a pointer into the vector in dwo_unit. As we collect more
12700 types we'll grow the vector and eventually have to reallocate space
12701 for it, invalidating all copies of pointers into the previous
12702 contents. */
12703 *dwo_file_slot = dwo_file;
12704 }
12705 else
12706 {
b4f54984 12707 if (dwarf_read_debug)
73869dc2
DE
12708 {
12709 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12710 virtual_dwo_name.c_str ());
73869dc2 12711 }
9a3c8263 12712 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12713 }
73869dc2
DE
12714
12715 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12716 dwo_unit->dwo_file = dwo_file;
12717 dwo_unit->signature = signature;
8d749320
SM
12718 dwo_unit->section =
12719 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
73869dc2
DE
12720 *dwo_unit->section = create_dwp_v2_section (is_debug_types
12721 ? &dwp_file->sections.types
12722 : &dwp_file->sections.info,
12723 sections.info_or_types_offset,
12724 sections.info_or_types_size);
12725 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12726
12727 return dwo_unit;
12728}
12729
57d63ce2
DE
12730/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12731 Returns NULL if the signature isn't found. */
80626a55
DE
12732
12733static struct dwo_unit *
57d63ce2
DE
12734lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
12735 ULONGEST signature, int is_debug_types)
80626a55 12736{
57d63ce2
DE
12737 const struct dwp_hash_table *dwp_htab =
12738 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 12739 bfd *dbfd = dwp_file->dbfd;
57d63ce2 12740 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12741 uint32_t hash = signature & mask;
12742 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12743 unsigned int i;
12744 void **slot;
870f88f7 12745 struct dwo_unit find_dwo_cu;
80626a55
DE
12746
12747 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12748 find_dwo_cu.signature = signature;
19ac8c2e
DE
12749 slot = htab_find_slot (is_debug_types
12750 ? dwp_file->loaded_tus
12751 : dwp_file->loaded_cus,
12752 &find_dwo_cu, INSERT);
80626a55
DE
12753
12754 if (*slot != NULL)
9a3c8263 12755 return (struct dwo_unit *) *slot;
80626a55
DE
12756
12757 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12758 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12759 {
12760 ULONGEST signature_in_table;
12761
12762 signature_in_table =
57d63ce2 12763 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12764 if (signature_in_table == signature)
12765 {
57d63ce2
DE
12766 uint32_t unit_index =
12767 read_4_bytes (dbfd,
12768 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12769
73869dc2
DE
12770 if (dwp_file->version == 1)
12771 {
12772 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
12773 comp_dir, signature,
12774 is_debug_types);
12775 }
12776 else
12777 {
12778 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
12779 comp_dir, signature,
12780 is_debug_types);
12781 }
9a3c8263 12782 return (struct dwo_unit *) *slot;
80626a55
DE
12783 }
12784 if (signature_in_table == 0)
12785 return NULL;
12786 hash = (hash + hash2) & mask;
12787 }
12788
12789 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12790 " [in module %s]"),
12791 dwp_file->name);
12792}
12793
ab5088bf 12794/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12795 Open the file specified by FILE_NAME and hand it off to BFD for
12796 preliminary analysis. Return a newly initialized bfd *, which
12797 includes a canonicalized copy of FILE_NAME.
80626a55 12798 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12799 SEARCH_CWD is true if the current directory is to be searched.
12800 It will be searched before debug-file-directory.
13aaf454
DE
12801 If successful, the file is added to the bfd include table of the
12802 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12803 If unable to find/open the file, return NULL.
3019eac3
DE
12804 NOTE: This function is derived from symfile_bfd_open. */
12805
192b62ce 12806static gdb_bfd_ref_ptr
6ac97d4c 12807try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3 12808{
80626a55 12809 int desc, flags;
3019eac3 12810 char *absolute_name;
9c02c129
DE
12811 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12812 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12813 to debug_file_directory. */
12814 char *search_path;
12815 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12816
6ac97d4c
DE
12817 if (search_cwd)
12818 {
12819 if (*debug_file_directory != '\0')
12820 search_path = concat (".", dirname_separator_string,
b36cec19 12821 debug_file_directory, (char *) NULL);
6ac97d4c
DE
12822 else
12823 search_path = xstrdup (".");
12824 }
9c02c129 12825 else
6ac97d4c 12826 search_path = xstrdup (debug_file_directory);
3019eac3 12827
492c0ab7 12828 flags = OPF_RETURN_REALPATH;
80626a55
DE
12829 if (is_dwp)
12830 flags |= OPF_SEARCH_IN_PATH;
9c02c129 12831 desc = openp (search_path, flags, file_name,
3019eac3 12832 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 12833 xfree (search_path);
3019eac3
DE
12834 if (desc < 0)
12835 return NULL;
12836
192b62ce 12837 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 12838 xfree (absolute_name);
9c02c129
DE
12839 if (sym_bfd == NULL)
12840 return NULL;
192b62ce 12841 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12842
192b62ce
TT
12843 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12844 return NULL;
3019eac3 12845
13aaf454
DE
12846 /* Success. Record the bfd as having been included by the objfile's bfd.
12847 This is important because things like demangled_names_hash lives in the
12848 objfile's per_bfd space and may have references to things like symbol
12849 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12850 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12851
3019eac3
DE
12852 return sym_bfd;
12853}
12854
ab5088bf 12855/* Try to open DWO file FILE_NAME.
3019eac3
DE
12856 COMP_DIR is the DW_AT_comp_dir attribute.
12857 The result is the bfd handle of the file.
12858 If there is a problem finding or opening the file, return NULL.
12859 Upon success, the canonicalized path of the file is stored in the bfd,
12860 same as symfile_bfd_open. */
12861
192b62ce 12862static gdb_bfd_ref_ptr
ab5088bf 12863open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3 12864{
80626a55 12865 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 12866 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12867
12868 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12869
12870 if (comp_dir != NULL)
12871 {
b36cec19
PA
12872 char *path_to_try = concat (comp_dir, SLASH_STRING,
12873 file_name, (char *) NULL);
3019eac3
DE
12874
12875 /* NOTE: If comp_dir is a relative path, this will also try the
12876 search path, which seems useful. */
192b62ce
TT
12877 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
12878 1 /*search_cwd*/));
3019eac3
DE
12879 xfree (path_to_try);
12880 if (abfd != NULL)
12881 return abfd;
12882 }
12883
12884 /* That didn't work, try debug-file-directory, which, despite its name,
12885 is a list of paths. */
12886
12887 if (*debug_file_directory == '\0')
12888 return NULL;
12889
6ac97d4c 12890 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12891}
12892
80626a55
DE
12893/* This function is mapped across the sections and remembers the offset and
12894 size of each of the DWO debugging sections we are interested in. */
12895
12896static void
12897dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12898{
9a3c8263 12899 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12900 const struct dwop_section_names *names = &dwop_section_names;
12901
12902 if (section_is_p (sectp->name, &names->abbrev_dwo))
12903 {
049412e3 12904 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
12905 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12906 }
12907 else if (section_is_p (sectp->name, &names->info_dwo))
12908 {
049412e3 12909 dwo_sections->info.s.section = sectp;
80626a55
DE
12910 dwo_sections->info.size = bfd_get_section_size (sectp);
12911 }
12912 else if (section_is_p (sectp->name, &names->line_dwo))
12913 {
049412e3 12914 dwo_sections->line.s.section = sectp;
80626a55
DE
12915 dwo_sections->line.size = bfd_get_section_size (sectp);
12916 }
12917 else if (section_is_p (sectp->name, &names->loc_dwo))
12918 {
049412e3 12919 dwo_sections->loc.s.section = sectp;
80626a55
DE
12920 dwo_sections->loc.size = bfd_get_section_size (sectp);
12921 }
12922 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12923 {
049412e3 12924 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
12925 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12926 }
12927 else if (section_is_p (sectp->name, &names->macro_dwo))
12928 {
049412e3 12929 dwo_sections->macro.s.section = sectp;
80626a55
DE
12930 dwo_sections->macro.size = bfd_get_section_size (sectp);
12931 }
12932 else if (section_is_p (sectp->name, &names->str_dwo))
12933 {
049412e3 12934 dwo_sections->str.s.section = sectp;
80626a55
DE
12935 dwo_sections->str.size = bfd_get_section_size (sectp);
12936 }
12937 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12938 {
049412e3 12939 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
12940 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12941 }
12942 else if (section_is_p (sectp->name, &names->types_dwo))
12943 {
12944 struct dwarf2_section_info type_section;
12945
12946 memset (&type_section, 0, sizeof (type_section));
049412e3 12947 type_section.s.section = sectp;
80626a55
DE
12948 type_section.size = bfd_get_section_size (sectp);
12949 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12950 &type_section);
12951 }
12952}
12953
ab5088bf 12954/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12955 by PER_CU. This is for the non-DWP case.
80626a55 12956 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12957
12958static struct dwo_file *
0ac5b59e
DE
12959open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12960 const char *dwo_name, const char *comp_dir)
3019eac3
DE
12961{
12962 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12963 struct dwo_file *dwo_file;
3019eac3
DE
12964 struct cleanup *cleanups;
12965
192b62ce 12966 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
80626a55
DE
12967 if (dbfd == NULL)
12968 {
b4f54984 12969 if (dwarf_read_debug)
80626a55
DE
12970 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12971 return NULL;
12972 }
12973 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
12974 dwo_file->dwo_name = dwo_name;
12975 dwo_file->comp_dir = comp_dir;
192b62ce 12976 dwo_file->dbfd = dbfd.release ();
3019eac3
DE
12977
12978 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
12979
192b62ce
TT
12980 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
12981 &dwo_file->sections);
3019eac3 12982
33c5cd75 12983 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
3019eac3 12984
78d4d2c5
JK
12985 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
12986 dwo_file->tus);
3019eac3
DE
12987
12988 discard_cleanups (cleanups);
12989
b4f54984 12990 if (dwarf_read_debug)
80626a55
DE
12991 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12992
3019eac3
DE
12993 return dwo_file;
12994}
12995
80626a55 12996/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12997 size of each of the DWP debugging sections common to version 1 and 2 that
12998 we are interested in. */
3019eac3 12999
80626a55 13000static void
73869dc2
DE
13001dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13002 void *dwp_file_ptr)
3019eac3 13003{
9a3c8263 13004 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13005 const struct dwop_section_names *names = &dwop_section_names;
13006 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13007
80626a55 13008 /* Record the ELF section number for later lookup: this is what the
73869dc2 13009 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13010 gdb_assert (elf_section_nr < dwp_file->num_sections);
13011 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13012
80626a55
DE
13013 /* Look for specific sections that we need. */
13014 if (section_is_p (sectp->name, &names->str_dwo))
13015 {
049412e3 13016 dwp_file->sections.str.s.section = sectp;
80626a55
DE
13017 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13018 }
13019 else if (section_is_p (sectp->name, &names->cu_index))
13020 {
049412e3 13021 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
13022 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13023 }
13024 else if (section_is_p (sectp->name, &names->tu_index))
13025 {
049412e3 13026 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
13027 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13028 }
13029}
3019eac3 13030
73869dc2
DE
13031/* This function is mapped across the sections and remembers the offset and
13032 size of each of the DWP version 2 debugging sections that we are interested
13033 in. This is split into a separate function because we don't know if we
13034 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13035
13036static void
13037dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13038{
9a3c8263 13039 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13040 const struct dwop_section_names *names = &dwop_section_names;
13041 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13042
13043 /* Record the ELF section number for later lookup: this is what the
13044 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13045 gdb_assert (elf_section_nr < dwp_file->num_sections);
13046 dwp_file->elf_sections[elf_section_nr] = sectp;
13047
13048 /* Look for specific sections that we need. */
13049 if (section_is_p (sectp->name, &names->abbrev_dwo))
13050 {
049412e3 13051 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
13052 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13053 }
13054 else if (section_is_p (sectp->name, &names->info_dwo))
13055 {
049412e3 13056 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
13057 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13058 }
13059 else if (section_is_p (sectp->name, &names->line_dwo))
13060 {
049412e3 13061 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
13062 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13063 }
13064 else if (section_is_p (sectp->name, &names->loc_dwo))
13065 {
049412e3 13066 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
13067 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13068 }
13069 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13070 {
049412e3 13071 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
13072 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13073 }
13074 else if (section_is_p (sectp->name, &names->macro_dwo))
13075 {
049412e3 13076 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
13077 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13078 }
13079 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13080 {
049412e3 13081 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
13082 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13083 }
13084 else if (section_is_p (sectp->name, &names->types_dwo))
13085 {
049412e3 13086 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
13087 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13088 }
13089}
13090
80626a55 13091/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13092
80626a55
DE
13093static hashval_t
13094hash_dwp_loaded_cutus (const void *item)
13095{
9a3c8263 13096 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13097
80626a55
DE
13098 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13099 return dwo_unit->signature;
3019eac3
DE
13100}
13101
80626a55 13102/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13103
80626a55
DE
13104static int
13105eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13106{
9a3c8263
SM
13107 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13108 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13109
80626a55
DE
13110 return dua->signature == dub->signature;
13111}
3019eac3 13112
80626a55 13113/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13114
80626a55
DE
13115static htab_t
13116allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13117{
13118 return htab_create_alloc_ex (3,
13119 hash_dwp_loaded_cutus,
13120 eq_dwp_loaded_cutus,
13121 NULL,
13122 &objfile->objfile_obstack,
13123 hashtab_obstack_allocate,
13124 dummy_obstack_deallocate);
13125}
3019eac3 13126
ab5088bf
DE
13127/* Try to open DWP file FILE_NAME.
13128 The result is the bfd handle of the file.
13129 If there is a problem finding or opening the file, return NULL.
13130 Upon success, the canonicalized path of the file is stored in the bfd,
13131 same as symfile_bfd_open. */
13132
192b62ce 13133static gdb_bfd_ref_ptr
ab5088bf
DE
13134open_dwp_file (const char *file_name)
13135{
192b62ce
TT
13136 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
13137 1 /*search_cwd*/));
6ac97d4c
DE
13138 if (abfd != NULL)
13139 return abfd;
13140
13141 /* Work around upstream bug 15652.
13142 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13143 [Whether that's a "bug" is debatable, but it is getting in our way.]
13144 We have no real idea where the dwp file is, because gdb's realpath-ing
13145 of the executable's path may have discarded the needed info.
13146 [IWBN if the dwp file name was recorded in the executable, akin to
13147 .gnu_debuglink, but that doesn't exist yet.]
13148 Strip the directory from FILE_NAME and search again. */
13149 if (*debug_file_directory != '\0')
13150 {
13151 /* Don't implicitly search the current directory here.
13152 If the user wants to search "." to handle this case,
13153 it must be added to debug-file-directory. */
13154 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
13155 0 /*search_cwd*/);
13156 }
13157
13158 return NULL;
ab5088bf
DE
13159}
13160
80626a55
DE
13161/* Initialize the use of the DWP file for the current objfile.
13162 By convention the name of the DWP file is ${objfile}.dwp.
13163 The result is NULL if it can't be found. */
a766d390 13164
80626a55 13165static struct dwp_file *
ab5088bf 13166open_and_init_dwp_file (void)
80626a55
DE
13167{
13168 struct objfile *objfile = dwarf2_per_objfile->objfile;
13169 struct dwp_file *dwp_file;
80626a55 13170
82bf32bc
JK
13171 /* Try to find first .dwp for the binary file before any symbolic links
13172 resolving. */
6c447423
DE
13173
13174 /* If the objfile is a debug file, find the name of the real binary
13175 file and get the name of dwp file from there. */
d721ba37 13176 std::string dwp_name;
6c447423
DE
13177 if (objfile->separate_debug_objfile_backlink != NULL)
13178 {
13179 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13180 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13181
d721ba37 13182 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13183 }
13184 else
d721ba37
PA
13185 dwp_name = objfile->original_name;
13186
13187 dwp_name += ".dwp";
80626a55 13188
d721ba37 13189 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
82bf32bc
JK
13190 if (dbfd == NULL
13191 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13192 {
13193 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13194 dwp_name = objfile_name (objfile);
13195 dwp_name += ".dwp";
13196 dbfd = open_dwp_file (dwp_name.c_str ());
82bf32bc
JK
13197 }
13198
80626a55
DE
13199 if (dbfd == NULL)
13200 {
b4f54984 13201 if (dwarf_read_debug)
d721ba37 13202 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 13203 return NULL;
3019eac3 13204 }
80626a55 13205 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
13206 dwp_file->name = bfd_get_filename (dbfd.get ());
13207 dwp_file->dbfd = dbfd.release ();
c906108c 13208
80626a55 13209 /* +1: section 0 is unused */
192b62ce 13210 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
13211 dwp_file->elf_sections =
13212 OBSTACK_CALLOC (&objfile->objfile_obstack,
13213 dwp_file->num_sections, asection *);
13214
192b62ce
TT
13215 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13216 dwp_file);
80626a55
DE
13217
13218 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
13219
13220 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
13221
73869dc2 13222 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13223 if (dwp_file->cus && dwp_file->tus
13224 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13225 {
13226 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13227 pretty bizarre. We use pulongest here because that's the established
4d65956b 13228 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13229 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13230 " TU version %s [in DWP file %s]"),
13231 pulongest (dwp_file->cus->version),
d721ba37 13232 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13233 }
08302ed2
DE
13234
13235 if (dwp_file->cus)
13236 dwp_file->version = dwp_file->cus->version;
13237 else if (dwp_file->tus)
13238 dwp_file->version = dwp_file->tus->version;
13239 else
13240 dwp_file->version = 2;
73869dc2
DE
13241
13242 if (dwp_file->version == 2)
192b62ce
TT
13243 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13244 dwp_file);
73869dc2 13245
19ac8c2e
DE
13246 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13247 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13248
b4f54984 13249 if (dwarf_read_debug)
80626a55
DE
13250 {
13251 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13252 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13253 " %s CUs, %s TUs\n",
13254 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13255 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13256 }
13257
13258 return dwp_file;
3019eac3 13259}
c906108c 13260
ab5088bf
DE
13261/* Wrapper around open_and_init_dwp_file, only open it once. */
13262
13263static struct dwp_file *
13264get_dwp_file (void)
13265{
13266 if (! dwarf2_per_objfile->dwp_checked)
13267 {
13268 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
13269 dwarf2_per_objfile->dwp_checked = 1;
13270 }
13271 return dwarf2_per_objfile->dwp_file;
13272}
13273
80626a55
DE
13274/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13275 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13276 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13277 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13278 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13279
13280 This is called, for example, when wanting to read a variable with a
13281 complex location. Therefore we don't want to do file i/o for every call.
13282 Therefore we don't want to look for a DWO file on every call.
13283 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13284 then we check if we've already seen DWO_NAME, and only THEN do we check
13285 for a DWO file.
13286
1c658ad5 13287 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13288 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13289
3019eac3 13290static struct dwo_unit *
80626a55
DE
13291lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13292 const char *dwo_name, const char *comp_dir,
13293 ULONGEST signature, int is_debug_types)
3019eac3
DE
13294{
13295 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13296 const char *kind = is_debug_types ? "TU" : "CU";
13297 void **dwo_file_slot;
3019eac3 13298 struct dwo_file *dwo_file;
80626a55 13299 struct dwp_file *dwp_file;
cb1df416 13300
6a506a2d
DE
13301 /* First see if there's a DWP file.
13302 If we have a DWP file but didn't find the DWO inside it, don't
13303 look for the original DWO file. It makes gdb behave differently
13304 depending on whether one is debugging in the build tree. */
cf2c3c16 13305
ab5088bf 13306 dwp_file = get_dwp_file ();
80626a55 13307 if (dwp_file != NULL)
cf2c3c16 13308 {
80626a55
DE
13309 const struct dwp_hash_table *dwp_htab =
13310 is_debug_types ? dwp_file->tus : dwp_file->cus;
13311
13312 if (dwp_htab != NULL)
13313 {
13314 struct dwo_unit *dwo_cutu =
57d63ce2
DE
13315 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
13316 signature, is_debug_types);
80626a55
DE
13317
13318 if (dwo_cutu != NULL)
13319 {
b4f54984 13320 if (dwarf_read_debug)
80626a55
DE
13321 {
13322 fprintf_unfiltered (gdb_stdlog,
13323 "Virtual DWO %s %s found: @%s\n",
13324 kind, hex_string (signature),
13325 host_address_to_string (dwo_cutu));
13326 }
13327 return dwo_cutu;
13328 }
13329 }
13330 }
6a506a2d 13331 else
80626a55 13332 {
6a506a2d 13333 /* No DWP file, look for the DWO file. */
80626a55 13334
6a506a2d
DE
13335 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
13336 if (*dwo_file_slot == NULL)
80626a55 13337 {
6a506a2d
DE
13338 /* Read in the file and build a table of the CUs/TUs it contains. */
13339 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13340 }
6a506a2d 13341 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13342 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13343
6a506a2d 13344 if (dwo_file != NULL)
19c3d4c9 13345 {
6a506a2d
DE
13346 struct dwo_unit *dwo_cutu = NULL;
13347
13348 if (is_debug_types && dwo_file->tus)
13349 {
13350 struct dwo_unit find_dwo_cutu;
13351
13352 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13353 find_dwo_cutu.signature = signature;
9a3c8263
SM
13354 dwo_cutu
13355 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13356 }
33c5cd75 13357 else if (!is_debug_types && dwo_file->cus)
80626a55 13358 {
33c5cd75
DB
13359 struct dwo_unit find_dwo_cutu;
13360
13361 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13362 find_dwo_cutu.signature = signature;
13363 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13364 &find_dwo_cutu);
6a506a2d
DE
13365 }
13366
13367 if (dwo_cutu != NULL)
13368 {
b4f54984 13369 if (dwarf_read_debug)
6a506a2d
DE
13370 {
13371 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13372 kind, dwo_name, hex_string (signature),
13373 host_address_to_string (dwo_cutu));
13374 }
13375 return dwo_cutu;
80626a55
DE
13376 }
13377 }
2e276125 13378 }
9cdd5dbd 13379
80626a55
DE
13380 /* We didn't find it. This could mean a dwo_id mismatch, or
13381 someone deleted the DWO/DWP file, or the search path isn't set up
13382 correctly to find the file. */
13383
b4f54984 13384 if (dwarf_read_debug)
80626a55
DE
13385 {
13386 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13387 kind, dwo_name, hex_string (signature));
13388 }
3019eac3 13389
6656a72d
DE
13390 /* This is a warning and not a complaint because it can be caused by
13391 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13392 {
13393 /* Print the name of the DWP file if we looked there, helps the user
13394 better diagnose the problem. */
791afaa2 13395 std::string dwp_text;
43942612
DE
13396
13397 if (dwp_file != NULL)
791afaa2
TT
13398 dwp_text = string_printf (" [in DWP file %s]",
13399 lbasename (dwp_file->name));
43942612
DE
13400
13401 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
13402 " [in module %s]"),
13403 kind, dwo_name, hex_string (signature),
791afaa2 13404 dwp_text.c_str (),
43942612 13405 this_unit->is_debug_types ? "TU" : "CU",
9c541725 13406 to_underlying (this_unit->sect_off), objfile_name (objfile));
43942612 13407 }
3019eac3 13408 return NULL;
5fb290d7
DJ
13409}
13410
80626a55
DE
13411/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13412 See lookup_dwo_cutu_unit for details. */
13413
13414static struct dwo_unit *
13415lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13416 const char *dwo_name, const char *comp_dir,
13417 ULONGEST signature)
13418{
13419 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13420}
13421
13422/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13423 See lookup_dwo_cutu_unit for details. */
13424
13425static struct dwo_unit *
13426lookup_dwo_type_unit (struct signatured_type *this_tu,
13427 const char *dwo_name, const char *comp_dir)
13428{
13429 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13430}
13431
89e63ee4
DE
13432/* Traversal function for queue_and_load_all_dwo_tus. */
13433
13434static int
13435queue_and_load_dwo_tu (void **slot, void *info)
13436{
13437 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13438 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13439 ULONGEST signature = dwo_unit->signature;
13440 struct signatured_type *sig_type =
13441 lookup_dwo_signatured_type (per_cu->cu, signature);
13442
13443 if (sig_type != NULL)
13444 {
13445 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13446
13447 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13448 a real dependency of PER_CU on SIG_TYPE. That is detected later
13449 while processing PER_CU. */
13450 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13451 load_full_type_unit (sig_cu);
13452 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13453 }
13454
13455 return 1;
13456}
13457
13458/* Queue all TUs contained in the DWO of PER_CU to be read in.
13459 The DWO may have the only definition of the type, though it may not be
13460 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13461 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13462
13463static void
13464queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13465{
13466 struct dwo_unit *dwo_unit;
13467 struct dwo_file *dwo_file;
13468
13469 gdb_assert (!per_cu->is_debug_types);
13470 gdb_assert (get_dwp_file () == NULL);
13471 gdb_assert (per_cu->cu != NULL);
13472
13473 dwo_unit = per_cu->cu->dwo_unit;
13474 gdb_assert (dwo_unit != NULL);
13475
13476 dwo_file = dwo_unit->dwo_file;
13477 if (dwo_file->tus != NULL)
13478 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13479}
13480
3019eac3
DE
13481/* Free all resources associated with DWO_FILE.
13482 Close the DWO file and munmap the sections.
13483 All memory should be on the objfile obstack. */
348e048f
DE
13484
13485static void
3019eac3 13486free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 13487{
348e048f 13488
5c6fa7ab 13489 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 13490 gdb_bfd_unref (dwo_file->dbfd);
348e048f 13491
3019eac3
DE
13492 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13493}
348e048f 13494
3019eac3 13495/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 13496
3019eac3
DE
13497static void
13498free_dwo_file_cleanup (void *arg)
13499{
13500 struct dwo_file *dwo_file = (struct dwo_file *) arg;
13501 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 13502
3019eac3
DE
13503 free_dwo_file (dwo_file, objfile);
13504}
348e048f 13505
3019eac3 13506/* Traversal function for free_dwo_files. */
2ab95328 13507
3019eac3
DE
13508static int
13509free_dwo_file_from_slot (void **slot, void *info)
13510{
13511 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13512 struct objfile *objfile = (struct objfile *) info;
348e048f 13513
3019eac3 13514 free_dwo_file (dwo_file, objfile);
348e048f 13515
3019eac3
DE
13516 return 1;
13517}
348e048f 13518
3019eac3 13519/* Free all resources associated with DWO_FILES. */
348e048f 13520
3019eac3
DE
13521static void
13522free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13523{
13524 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 13525}
3019eac3
DE
13526\f
13527/* Read in various DIEs. */
348e048f 13528
d389af10 13529/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13530 Inherit only the children of the DW_AT_abstract_origin DIE not being
13531 already referenced by DW_AT_abstract_origin from the children of the
13532 current DIE. */
d389af10
JK
13533
13534static void
13535inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13536{
13537 struct die_info *child_die;
791afaa2 13538 sect_offset *offsetp;
d389af10
JK
13539 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13540 struct die_info *origin_die;
13541 /* Iterator of the ORIGIN_DIE children. */
13542 struct die_info *origin_child_die;
d389af10 13543 struct attribute *attr;
cd02d79d
PA
13544 struct dwarf2_cu *origin_cu;
13545 struct pending **origin_previous_list_in_scope;
d389af10
JK
13546
13547 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13548 if (!attr)
13549 return;
13550
cd02d79d
PA
13551 /* Note that following die references may follow to a die in a
13552 different cu. */
13553
13554 origin_cu = cu;
13555 origin_die = follow_die_ref (die, attr, &origin_cu);
13556
13557 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13558 symbols in. */
13559 origin_previous_list_in_scope = origin_cu->list_in_scope;
13560 origin_cu->list_in_scope = cu->list_in_scope;
13561
edb3359d
DJ
13562 if (die->tag != origin_die->tag
13563 && !(die->tag == DW_TAG_inlined_subroutine
13564 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
13565 complaint (&symfile_complaints,
13566 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9c541725
PA
13567 to_underlying (die->sect_off),
13568 to_underlying (origin_die->sect_off));
d389af10 13569
791afaa2 13570 std::vector<sect_offset> offsets;
d389af10 13571
3ea89b92
PMR
13572 for (child_die = die->child;
13573 child_die && child_die->tag;
13574 child_die = sibling_die (child_die))
13575 {
13576 struct die_info *child_origin_die;
13577 struct dwarf2_cu *child_origin_cu;
13578
13579 /* We are trying to process concrete instance entries:
216f72a1 13580 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13581 it's not relevant to our analysis here. i.e. detecting DIEs that are
13582 present in the abstract instance but not referenced in the concrete
13583 one. */
216f72a1
JK
13584 if (child_die->tag == DW_TAG_call_site
13585 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13586 continue;
13587
c38f313d
DJ
13588 /* For each CHILD_DIE, find the corresponding child of
13589 ORIGIN_DIE. If there is more than one layer of
13590 DW_AT_abstract_origin, follow them all; there shouldn't be,
13591 but GCC versions at least through 4.4 generate this (GCC PR
13592 40573). */
3ea89b92
PMR
13593 child_origin_die = child_die;
13594 child_origin_cu = cu;
c38f313d
DJ
13595 while (1)
13596 {
cd02d79d
PA
13597 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13598 child_origin_cu);
c38f313d
DJ
13599 if (attr == NULL)
13600 break;
cd02d79d
PA
13601 child_origin_die = follow_die_ref (child_origin_die, attr,
13602 &child_origin_cu);
c38f313d
DJ
13603 }
13604
d389af10
JK
13605 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13606 counterpart may exist. */
c38f313d 13607 if (child_origin_die != child_die)
d389af10 13608 {
edb3359d
DJ
13609 if (child_die->tag != child_origin_die->tag
13610 && !(child_die->tag == DW_TAG_inlined_subroutine
13611 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
13612 complaint (&symfile_complaints,
13613 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
13614 "different tags"),
13615 to_underlying (child_die->sect_off),
13616 to_underlying (child_origin_die->sect_off));
c38f313d
DJ
13617 if (child_origin_die->parent != origin_die)
13618 complaint (&symfile_complaints,
13619 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
13620 "different parents"),
13621 to_underlying (child_die->sect_off),
13622 to_underlying (child_origin_die->sect_off));
c38f313d 13623 else
791afaa2 13624 offsets.push_back (child_origin_die->sect_off);
d389af10 13625 }
d389af10 13626 }
791afaa2
TT
13627 std::sort (offsets.begin (), offsets.end ());
13628 sect_offset *offsets_end = offsets.data () + offsets.size ();
13629 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13630 if (offsetp[-1] == *offsetp)
3e43a32a
MS
13631 complaint (&symfile_complaints,
13632 _("Multiple children of DIE 0x%x refer "
13633 "to DIE 0x%x as their abstract origin"),
9c541725 13634 to_underlying (die->sect_off), to_underlying (*offsetp));
d389af10 13635
791afaa2 13636 offsetp = offsets.data ();
d389af10
JK
13637 origin_child_die = origin_die->child;
13638 while (origin_child_die && origin_child_die->tag)
13639 {
13640 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13641 while (offsetp < offsets_end
9c541725 13642 && *offsetp < origin_child_die->sect_off)
d389af10 13643 offsetp++;
b64f50a1 13644 if (offsetp >= offsets_end
9c541725 13645 || *offsetp > origin_child_die->sect_off)
d389af10 13646 {
adde2bff
DE
13647 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13648 Check whether we're already processing ORIGIN_CHILD_DIE.
13649 This can happen with mutually referenced abstract_origins.
13650 PR 16581. */
13651 if (!origin_child_die->in_process)
13652 process_die (origin_child_die, origin_cu);
d389af10
JK
13653 }
13654 origin_child_die = sibling_die (origin_child_die);
13655 }
cd02d79d 13656 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
13657}
13658
c906108c 13659static void
e7c27a73 13660read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13661{
e7c27a73 13662 struct objfile *objfile = cu->objfile;
3e29f34a 13663 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13664 struct context_stack *newobj;
c906108c
SS
13665 CORE_ADDR lowpc;
13666 CORE_ADDR highpc;
13667 struct die_info *child_die;
edb3359d 13668 struct attribute *attr, *call_line, *call_file;
15d034d0 13669 const char *name;
e142c38c 13670 CORE_ADDR baseaddr;
801e3a5b 13671 struct block *block;
edb3359d 13672 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13673 std::vector<struct symbol *> template_args;
34eaf542 13674 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13675
13676 if (inlined_func)
13677 {
13678 /* If we do not have call site information, we can't show the
13679 caller of this inlined function. That's too confusing, so
13680 only use the scope for local variables. */
13681 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13682 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13683 if (call_line == NULL || call_file == NULL)
13684 {
13685 read_lexical_block_scope (die, cu);
13686 return;
13687 }
13688 }
c906108c 13689
e142c38c
DJ
13690 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13691
94af9270 13692 name = dwarf2_name (die, cu);
c906108c 13693
e8d05480
JB
13694 /* Ignore functions with missing or empty names. These are actually
13695 illegal according to the DWARF standard. */
13696 if (name == NULL)
13697 {
13698 complaint (&symfile_complaints,
b64f50a1 13699 _("missing name for subprogram DIE at %d"),
9c541725 13700 to_underlying (die->sect_off));
e8d05480
JB
13701 return;
13702 }
13703
13704 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13705 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13706 <= PC_BOUNDS_INVALID)
e8d05480 13707 {
ae4d0c03
PM
13708 attr = dwarf2_attr (die, DW_AT_external, cu);
13709 if (!attr || !DW_UNSND (attr))
13710 complaint (&symfile_complaints,
3e43a32a
MS
13711 _("cannot get low and high bounds "
13712 "for subprogram DIE at %d"),
9c541725 13713 to_underlying (die->sect_off));
e8d05480
JB
13714 return;
13715 }
c906108c 13716
3e29f34a
MR
13717 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13718 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13719
34eaf542
TT
13720 /* If we have any template arguments, then we must allocate a
13721 different sort of symbol. */
13722 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13723 {
13724 if (child_die->tag == DW_TAG_template_type_param
13725 || child_die->tag == DW_TAG_template_value_param)
13726 {
e623cf5d 13727 templ_func = allocate_template_symbol (objfile);
cf724bc9 13728 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13729 break;
13730 }
13731 }
13732
fe978cb0
PA
13733 newobj = push_context (0, lowpc);
13734 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 13735 (struct symbol *) templ_func);
4c2df51b 13736
4cecd739
DJ
13737 /* If there is a location expression for DW_AT_frame_base, record
13738 it. */
e142c38c 13739 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 13740 if (attr)
fe978cb0 13741 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13742
63e43d3a
PMR
13743 /* If there is a location for the static link, record it. */
13744 newobj->static_link = NULL;
13745 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13746 if (attr)
13747 {
224c3ddb
SM
13748 newobj->static_link
13749 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
13750 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13751 }
13752
e142c38c 13753 cu->list_in_scope = &local_symbols;
c906108c 13754
639d11d3 13755 if (die->child != NULL)
c906108c 13756 {
639d11d3 13757 child_die = die->child;
c906108c
SS
13758 while (child_die && child_die->tag)
13759 {
34eaf542
TT
13760 if (child_die->tag == DW_TAG_template_type_param
13761 || child_die->tag == DW_TAG_template_value_param)
13762 {
13763 struct symbol *arg = new_symbol (child_die, NULL, cu);
13764
f1078f66 13765 if (arg != NULL)
2f4732b0 13766 template_args.push_back (arg);
34eaf542
TT
13767 }
13768 else
13769 process_die (child_die, cu);
c906108c
SS
13770 child_die = sibling_die (child_die);
13771 }
13772 }
13773
d389af10
JK
13774 inherit_abstract_dies (die, cu);
13775
4a811a97
UW
13776 /* If we have a DW_AT_specification, we might need to import using
13777 directives from the context of the specification DIE. See the
13778 comment in determine_prefix. */
13779 if (cu->language == language_cplus
13780 && dwarf2_attr (die, DW_AT_specification, cu))
13781 {
13782 struct dwarf2_cu *spec_cu = cu;
13783 struct die_info *spec_die = die_specification (die, &spec_cu);
13784
13785 while (spec_die)
13786 {
13787 child_die = spec_die->child;
13788 while (child_die && child_die->tag)
13789 {
13790 if (child_die->tag == DW_TAG_imported_module)
13791 process_die (child_die, spec_cu);
13792 child_die = sibling_die (child_die);
13793 }
13794
13795 /* In some cases, GCC generates specification DIEs that
13796 themselves contain DW_AT_specification attributes. */
13797 spec_die = die_specification (spec_die, &spec_cu);
13798 }
13799 }
13800
fe978cb0 13801 newobj = pop_context ();
c906108c 13802 /* Make a block for the local symbols within. */
fe978cb0 13803 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 13804 newobj->static_link, lowpc, highpc);
801e3a5b 13805
df8a16a1 13806 /* For C++, set the block's scope. */
45280282
IB
13807 if ((cu->language == language_cplus
13808 || cu->language == language_fortran
c44af4eb
TT
13809 || cu->language == language_d
13810 || cu->language == language_rust)
4d4ec4e5 13811 && cu->processing_has_namespace_info)
195a3f6c
TT
13812 block_set_scope (block, determine_prefix (die, cu),
13813 &objfile->objfile_obstack);
df8a16a1 13814
801e3a5b
JB
13815 /* If we have address ranges, record them. */
13816 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13817
fe978cb0 13818 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 13819
34eaf542 13820 /* Attach template arguments to function. */
2f4732b0 13821 if (!template_args.empty ())
34eaf542
TT
13822 {
13823 gdb_assert (templ_func != NULL);
13824
2f4732b0 13825 templ_func->n_template_arguments = template_args.size ();
34eaf542 13826 templ_func->template_arguments
8d749320
SM
13827 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13828 templ_func->n_template_arguments);
34eaf542 13829 memcpy (templ_func->template_arguments,
2f4732b0 13830 template_args.data (),
34eaf542 13831 (templ_func->n_template_arguments * sizeof (struct symbol *)));
34eaf542
TT
13832 }
13833
208d8187
JB
13834 /* In C++, we can have functions nested inside functions (e.g., when
13835 a function declares a class that has methods). This means that
13836 when we finish processing a function scope, we may need to go
13837 back to building a containing block's symbol lists. */
fe978cb0 13838 local_symbols = newobj->locals;
22cee43f 13839 local_using_directives = newobj->local_using_directives;
208d8187 13840
921e78cf
JB
13841 /* If we've finished processing a top-level function, subsequent
13842 symbols go in the file symbol list. */
13843 if (outermost_context_p ())
e142c38c 13844 cu->list_in_scope = &file_symbols;
c906108c
SS
13845}
13846
13847/* Process all the DIES contained within a lexical block scope. Start
13848 a new scope, process the dies, and then close the scope. */
13849
13850static void
e7c27a73 13851read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13852{
e7c27a73 13853 struct objfile *objfile = cu->objfile;
3e29f34a 13854 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13855 struct context_stack *newobj;
c906108c
SS
13856 CORE_ADDR lowpc, highpc;
13857 struct die_info *child_die;
e142c38c
DJ
13858 CORE_ADDR baseaddr;
13859
13860 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13861
13862 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13863 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13864 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13865 be nasty. Might be easier to properly extend generic blocks to
af34e669 13866 describe ranges. */
e385593e
JK
13867 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13868 {
13869 case PC_BOUNDS_NOT_PRESENT:
13870 /* DW_TAG_lexical_block has no attributes, process its children as if
13871 there was no wrapping by that DW_TAG_lexical_block.
13872 GCC does no longer produces such DWARF since GCC r224161. */
13873 for (child_die = die->child;
13874 child_die != NULL && child_die->tag;
13875 child_die = sibling_die (child_die))
13876 process_die (child_die, cu);
13877 return;
13878 case PC_BOUNDS_INVALID:
13879 return;
13880 }
3e29f34a
MR
13881 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13882 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
13883
13884 push_context (0, lowpc);
639d11d3 13885 if (die->child != NULL)
c906108c 13886 {
639d11d3 13887 child_die = die->child;
c906108c
SS
13888 while (child_die && child_die->tag)
13889 {
e7c27a73 13890 process_die (child_die, cu);
c906108c
SS
13891 child_die = sibling_die (child_die);
13892 }
13893 }
3ea89b92 13894 inherit_abstract_dies (die, cu);
fe978cb0 13895 newobj = pop_context ();
c906108c 13896
22cee43f 13897 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 13898 {
801e3a5b 13899 struct block *block
63e43d3a 13900 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 13901 newobj->start_addr, highpc);
801e3a5b
JB
13902
13903 /* Note that recording ranges after traversing children, as we
13904 do here, means that recording a parent's ranges entails
13905 walking across all its children's ranges as they appear in
13906 the address map, which is quadratic behavior.
13907
13908 It would be nicer to record the parent's ranges before
13909 traversing its children, simply overriding whatever you find
13910 there. But since we don't even decide whether to create a
13911 block until after we've traversed its children, that's hard
13912 to do. */
13913 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13914 }
fe978cb0 13915 local_symbols = newobj->locals;
22cee43f 13916 local_using_directives = newobj->local_using_directives;
c906108c
SS
13917}
13918
216f72a1 13919/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13920
13921static void
13922read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13923{
13924 struct objfile *objfile = cu->objfile;
13925 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13926 CORE_ADDR pc, baseaddr;
13927 struct attribute *attr;
13928 struct call_site *call_site, call_site_local;
13929 void **slot;
13930 int nparams;
13931 struct die_info *child_die;
13932
13933 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13934
216f72a1
JK
13935 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13936 if (attr == NULL)
13937 {
13938 /* This was a pre-DWARF-5 GNU extension alias
13939 for DW_AT_call_return_pc. */
13940 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13941 }
96408a79
SA
13942 if (!attr)
13943 {
13944 complaint (&symfile_complaints,
216f72a1 13945 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 13946 "DIE 0x%x [in module %s]"),
9c541725 13947 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
13948 return;
13949 }
31aa7e4e 13950 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13951 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13952
13953 if (cu->call_site_htab == NULL)
13954 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13955 NULL, &objfile->objfile_obstack,
13956 hashtab_obstack_allocate, NULL);
13957 call_site_local.pc = pc;
13958 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13959 if (*slot != NULL)
13960 {
13961 complaint (&symfile_complaints,
216f72a1 13962 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 13963 "DIE 0x%x [in module %s]"),
9c541725 13964 paddress (gdbarch, pc), to_underlying (die->sect_off),
4262abfb 13965 objfile_name (objfile));
96408a79
SA
13966 return;
13967 }
13968
13969 /* Count parameters at the caller. */
13970
13971 nparams = 0;
13972 for (child_die = die->child; child_die && child_die->tag;
13973 child_die = sibling_die (child_die))
13974 {
216f72a1
JK
13975 if (child_die->tag != DW_TAG_call_site_parameter
13976 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13977 {
13978 complaint (&symfile_complaints,
216f72a1
JK
13979 _("Tag %d is not DW_TAG_call_site_parameter in "
13980 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 13981 child_die->tag, to_underlying (child_die->sect_off),
4262abfb 13982 objfile_name (objfile));
96408a79
SA
13983 continue;
13984 }
13985
13986 nparams++;
13987 }
13988
224c3ddb
SM
13989 call_site
13990 = ((struct call_site *)
13991 obstack_alloc (&objfile->objfile_obstack,
13992 sizeof (*call_site)
13993 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13994 *slot = call_site;
13995 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13996 call_site->pc = pc;
13997
216f72a1
JK
13998 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13999 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
14000 {
14001 struct die_info *func_die;
14002
14003 /* Skip also over DW_TAG_inlined_subroutine. */
14004 for (func_die = die->parent;
14005 func_die && func_die->tag != DW_TAG_subprogram
14006 && func_die->tag != DW_TAG_subroutine_type;
14007 func_die = func_die->parent);
14008
216f72a1
JK
14009 /* DW_AT_call_all_calls is a superset
14010 of DW_AT_call_all_tail_calls. */
96408a79 14011 if (func_die
216f72a1 14012 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14013 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14014 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14015 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14016 {
14017 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14018 not complete. But keep CALL_SITE for look ups via call_site_htab,
14019 both the initial caller containing the real return address PC and
14020 the final callee containing the current PC of a chain of tail
14021 calls do not need to have the tail call list complete. But any
14022 function candidate for a virtual tail call frame searched via
14023 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14024 determined unambiguously. */
14025 }
14026 else
14027 {
14028 struct type *func_type = NULL;
14029
14030 if (func_die)
14031 func_type = get_die_type (func_die, cu);
14032 if (func_type != NULL)
14033 {
14034 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14035
14036 /* Enlist this call site to the function. */
14037 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14038 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14039 }
14040 else
14041 complaint (&symfile_complaints,
216f72a1 14042 _("Cannot find function owning DW_TAG_call_site "
96408a79 14043 "DIE 0x%x [in module %s]"),
9c541725 14044 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
14045 }
14046 }
14047
216f72a1
JK
14048 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14049 if (attr == NULL)
14050 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14051 if (attr == NULL)
14052 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14053 if (attr == NULL)
216f72a1
JK
14054 {
14055 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14056 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14057 }
96408a79
SA
14058 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14059 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14060 /* Keep NULL DWARF_BLOCK. */;
14061 else if (attr_form_is_block (attr))
14062 {
14063 struct dwarf2_locexpr_baton *dlbaton;
14064
8d749320 14065 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14066 dlbaton->data = DW_BLOCK (attr)->data;
14067 dlbaton->size = DW_BLOCK (attr)->size;
14068 dlbaton->per_cu = cu->per_cu;
14069
14070 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14071 }
7771576e 14072 else if (attr_form_is_ref (attr))
96408a79 14073 {
96408a79
SA
14074 struct dwarf2_cu *target_cu = cu;
14075 struct die_info *target_die;
14076
ac9ec31b 14077 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
14078 gdb_assert (target_cu->objfile == objfile);
14079 if (die_is_declaration (target_die, target_cu))
14080 {
7d45c7c3 14081 const char *target_physname;
9112db09
JK
14082
14083 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14084 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14085 if (target_physname == NULL)
9112db09 14086 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
14087 if (target_physname == NULL)
14088 complaint (&symfile_complaints,
216f72a1 14089 _("DW_AT_call_target target DIE has invalid "
96408a79 14090 "physname, for referencing DIE 0x%x [in module %s]"),
9c541725 14091 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 14092 else
7d455152 14093 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14094 }
14095 else
14096 {
14097 CORE_ADDR lowpc;
14098
14099 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14100 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14101 <= PC_BOUNDS_INVALID)
96408a79 14102 complaint (&symfile_complaints,
216f72a1 14103 _("DW_AT_call_target target DIE has invalid "
96408a79 14104 "low pc, for referencing DIE 0x%x [in module %s]"),
9c541725 14105 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 14106 else
3e29f34a
MR
14107 {
14108 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14109 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14110 }
96408a79
SA
14111 }
14112 }
14113 else
14114 complaint (&symfile_complaints,
216f72a1 14115 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 14116 "block nor reference, for DIE 0x%x [in module %s]"),
9c541725 14117 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
14118
14119 call_site->per_cu = cu->per_cu;
14120
14121 for (child_die = die->child;
14122 child_die && child_die->tag;
14123 child_die = sibling_die (child_die))
14124 {
96408a79 14125 struct call_site_parameter *parameter;
1788b2d3 14126 struct attribute *loc, *origin;
96408a79 14127
216f72a1
JK
14128 if (child_die->tag != DW_TAG_call_site_parameter
14129 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14130 {
14131 /* Already printed the complaint above. */
14132 continue;
14133 }
14134
14135 gdb_assert (call_site->parameter_count < nparams);
14136 parameter = &call_site->parameter[call_site->parameter_count];
14137
1788b2d3
JK
14138 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14139 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14140 register is contained in DW_AT_call_value. */
96408a79 14141
24c5c679 14142 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14143 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14144 if (origin == NULL)
14145 {
14146 /* This was a pre-DWARF-5 GNU extension alias
14147 for DW_AT_call_parameter. */
14148 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14149 }
7771576e 14150 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14151 {
1788b2d3 14152 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14153
14154 sect_offset sect_off
14155 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14156 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14157 {
14158 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14159 binding can be done only inside one CU. Such referenced DIE
14160 therefore cannot be even moved to DW_TAG_partial_unit. */
14161 complaint (&symfile_complaints,
216f72a1
JK
14162 _("DW_AT_call_parameter offset is not in CU for "
14163 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
14164 to_underlying (child_die->sect_off),
14165 objfile_name (objfile));
d76b7dbc
JK
14166 continue;
14167 }
9c541725
PA
14168 parameter->u.param_cu_off
14169 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14170 }
14171 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
14172 {
14173 complaint (&symfile_complaints,
14174 _("No DW_FORM_block* DW_AT_location for "
216f72a1 14175 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 14176 to_underlying (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14177 continue;
14178 }
24c5c679 14179 else
96408a79 14180 {
24c5c679
JK
14181 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14182 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14183 if (parameter->u.dwarf_reg != -1)
14184 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14185 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14186 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14187 &parameter->u.fb_offset))
14188 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14189 else
14190 {
14191 complaint (&symfile_complaints,
14192 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14193 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 14194 "DW_TAG_call_site child DIE 0x%x "
24c5c679 14195 "[in module %s]"),
9c541725
PA
14196 to_underlying (child_die->sect_off),
14197 objfile_name (objfile));
24c5c679
JK
14198 continue;
14199 }
96408a79
SA
14200 }
14201
216f72a1
JK
14202 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14203 if (attr == NULL)
14204 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14205 if (!attr_form_is_block (attr))
14206 {
14207 complaint (&symfile_complaints,
216f72a1
JK
14208 _("No DW_FORM_block* DW_AT_call_value for "
14209 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
14210 to_underlying (child_die->sect_off),
14211 objfile_name (objfile));
96408a79
SA
14212 continue;
14213 }
14214 parameter->value = DW_BLOCK (attr)->data;
14215 parameter->value_size = DW_BLOCK (attr)->size;
14216
14217 /* Parameters are not pre-cleared by memset above. */
14218 parameter->data_value = NULL;
14219 parameter->data_value_size = 0;
14220 call_site->parameter_count++;
14221
216f72a1
JK
14222 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14223 if (attr == NULL)
14224 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14225 if (attr)
14226 {
14227 if (!attr_form_is_block (attr))
14228 complaint (&symfile_complaints,
216f72a1
JK
14229 _("No DW_FORM_block* DW_AT_call_data_value for "
14230 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
14231 to_underlying (child_die->sect_off),
14232 objfile_name (objfile));
96408a79
SA
14233 else
14234 {
14235 parameter->data_value = DW_BLOCK (attr)->data;
14236 parameter->data_value_size = DW_BLOCK (attr)->size;
14237 }
14238 }
14239 }
14240}
14241
71a3c369
TT
14242/* Helper function for read_variable. If DIE represents a virtual
14243 table, then return the type of the concrete object that is
14244 associated with the virtual table. Otherwise, return NULL. */
14245
14246static struct type *
14247rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14248{
14249 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14250 if (attr == NULL)
14251 return NULL;
14252
14253 /* Find the type DIE. */
14254 struct die_info *type_die = NULL;
14255 struct dwarf2_cu *type_cu = cu;
14256
14257 if (attr_form_is_ref (attr))
14258 type_die = follow_die_ref (die, attr, &type_cu);
14259 if (type_die == NULL)
14260 return NULL;
14261
14262 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14263 return NULL;
14264 return die_containing_type (type_die, type_cu);
14265}
14266
14267/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14268
14269static void
14270read_variable (struct die_info *die, struct dwarf2_cu *cu)
14271{
14272 struct rust_vtable_symbol *storage = NULL;
14273
14274 if (cu->language == language_rust)
14275 {
14276 struct type *containing_type = rust_containing_type (die, cu);
14277
14278 if (containing_type != NULL)
14279 {
14280 struct objfile *objfile = cu->objfile;
14281
14282 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14283 struct rust_vtable_symbol);
14284 initialize_objfile_symbol (storage);
14285 storage->concrete_type = containing_type;
cf724bc9 14286 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14287 }
14288 }
14289
14290 new_symbol_full (die, NULL, cu, storage);
14291}
14292
43988095
JK
14293/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14294 reading .debug_rnglists.
14295 Callback's type should be:
14296 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14297 Return true if the attributes are present and valid, otherwise,
14298 return false. */
14299
14300template <typename Callback>
14301static bool
14302dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14303 Callback &&callback)
14304{
14305 struct objfile *objfile = cu->objfile;
43988095 14306 bfd *obfd = objfile->obfd;
43988095
JK
14307 /* Base address selection entry. */
14308 CORE_ADDR base;
14309 int found_base;
43988095 14310 const gdb_byte *buffer;
43988095
JK
14311 CORE_ADDR baseaddr;
14312 bool overflow = false;
14313
14314 found_base = cu->base_known;
14315 base = cu->base_address;
14316
14317 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14318 if (offset >= dwarf2_per_objfile->rnglists.size)
14319 {
14320 complaint (&symfile_complaints,
14321 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14322 offset);
14323 return false;
14324 }
14325 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14326
14327 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14328
14329 while (1)
14330 {
7814882a
JK
14331 /* Initialize it due to a false compiler warning. */
14332 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14333 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14334 + dwarf2_per_objfile->rnglists.size);
14335 unsigned int bytes_read;
14336
14337 if (buffer == buf_end)
14338 {
14339 overflow = true;
14340 break;
14341 }
14342 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14343 switch (rlet)
14344 {
14345 case DW_RLE_end_of_list:
14346 break;
14347 case DW_RLE_base_address:
14348 if (buffer + cu->header.addr_size > buf_end)
14349 {
14350 overflow = true;
14351 break;
14352 }
14353 base = read_address (obfd, buffer, cu, &bytes_read);
14354 found_base = 1;
14355 buffer += bytes_read;
14356 break;
14357 case DW_RLE_start_length:
14358 if (buffer + cu->header.addr_size > buf_end)
14359 {
14360 overflow = true;
14361 break;
14362 }
14363 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14364 buffer += bytes_read;
14365 range_end = (range_beginning
14366 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14367 buffer += bytes_read;
14368 if (buffer > buf_end)
14369 {
14370 overflow = true;
14371 break;
14372 }
14373 break;
14374 case DW_RLE_offset_pair:
14375 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14376 buffer += bytes_read;
14377 if (buffer > buf_end)
14378 {
14379 overflow = true;
14380 break;
14381 }
14382 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14383 buffer += bytes_read;
14384 if (buffer > buf_end)
14385 {
14386 overflow = true;
14387 break;
14388 }
14389 break;
14390 case DW_RLE_start_end:
14391 if (buffer + 2 * cu->header.addr_size > buf_end)
14392 {
14393 overflow = true;
14394 break;
14395 }
14396 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14397 buffer += bytes_read;
14398 range_end = read_address (obfd, buffer, cu, &bytes_read);
14399 buffer += bytes_read;
14400 break;
14401 default:
14402 complaint (&symfile_complaints,
14403 _("Invalid .debug_rnglists data (no base address)"));
14404 return false;
14405 }
14406 if (rlet == DW_RLE_end_of_list || overflow)
14407 break;
14408 if (rlet == DW_RLE_base_address)
14409 continue;
14410
14411 if (!found_base)
14412 {
14413 /* We have no valid base address for the ranges
14414 data. */
14415 complaint (&symfile_complaints,
14416 _("Invalid .debug_rnglists data (no base address)"));
14417 return false;
14418 }
14419
14420 if (range_beginning > range_end)
14421 {
14422 /* Inverted range entries are invalid. */
14423 complaint (&symfile_complaints,
14424 _("Invalid .debug_rnglists data (inverted range)"));
14425 return false;
14426 }
14427
14428 /* Empty range entries have no effect. */
14429 if (range_beginning == range_end)
14430 continue;
14431
14432 range_beginning += base;
14433 range_end += base;
14434
14435 /* A not-uncommon case of bad debug info.
14436 Don't pollute the addrmap with bad data. */
14437 if (range_beginning + baseaddr == 0
14438 && !dwarf2_per_objfile->has_section_at_zero)
14439 {
14440 complaint (&symfile_complaints,
14441 _(".debug_rnglists entry has start address of zero"
14442 " [in module %s]"), objfile_name (objfile));
14443 continue;
14444 }
14445
14446 callback (range_beginning, range_end);
14447 }
14448
14449 if (overflow)
14450 {
14451 complaint (&symfile_complaints,
14452 _("Offset %d is not terminated "
14453 "for DW_AT_ranges attribute"),
14454 offset);
14455 return false;
14456 }
14457
14458 return true;
14459}
14460
14461/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14462 Callback's type should be:
14463 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14464 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14465
43988095 14466template <typename Callback>
43039443 14467static int
5f46c5a5 14468dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14469 Callback &&callback)
43039443
JK
14470{
14471 struct objfile *objfile = cu->objfile;
14472 struct comp_unit_head *cu_header = &cu->header;
14473 bfd *obfd = objfile->obfd;
14474 unsigned int addr_size = cu_header->addr_size;
14475 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14476 /* Base address selection entry. */
14477 CORE_ADDR base;
14478 int found_base;
14479 unsigned int dummy;
d521ce57 14480 const gdb_byte *buffer;
ff013f42 14481 CORE_ADDR baseaddr;
43039443 14482
43988095
JK
14483 if (cu_header->version >= 5)
14484 return dwarf2_rnglists_process (offset, cu, callback);
14485
d00adf39
DE
14486 found_base = cu->base_known;
14487 base = cu->base_address;
43039443 14488
be391dca 14489 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14490 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
14491 {
14492 complaint (&symfile_complaints,
14493 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14494 offset);
14495 return 0;
14496 }
dce234bc 14497 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14498
e7030f15 14499 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14500
43039443
JK
14501 while (1)
14502 {
14503 CORE_ADDR range_beginning, range_end;
14504
14505 range_beginning = read_address (obfd, buffer, cu, &dummy);
14506 buffer += addr_size;
14507 range_end = read_address (obfd, buffer, cu, &dummy);
14508 buffer += addr_size;
14509 offset += 2 * addr_size;
14510
14511 /* An end of list marker is a pair of zero addresses. */
14512 if (range_beginning == 0 && range_end == 0)
14513 /* Found the end of list entry. */
14514 break;
14515
14516 /* Each base address selection entry is a pair of 2 values.
14517 The first is the largest possible address, the second is
14518 the base address. Check for a base address here. */
14519 if ((range_beginning & mask) == mask)
14520 {
28d2bfb9
AB
14521 /* If we found the largest possible address, then we already
14522 have the base address in range_end. */
14523 base = range_end;
43039443
JK
14524 found_base = 1;
14525 continue;
14526 }
14527
14528 if (!found_base)
14529 {
14530 /* We have no valid base address for the ranges
14531 data. */
14532 complaint (&symfile_complaints,
14533 _("Invalid .debug_ranges data (no base address)"));
14534 return 0;
14535 }
14536
9277c30c
UW
14537 if (range_beginning > range_end)
14538 {
14539 /* Inverted range entries are invalid. */
14540 complaint (&symfile_complaints,
14541 _("Invalid .debug_ranges data (inverted range)"));
14542 return 0;
14543 }
14544
14545 /* Empty range entries have no effect. */
14546 if (range_beginning == range_end)
14547 continue;
14548
43039443
JK
14549 range_beginning += base;
14550 range_end += base;
14551
01093045
DE
14552 /* A not-uncommon case of bad debug info.
14553 Don't pollute the addrmap with bad data. */
14554 if (range_beginning + baseaddr == 0
14555 && !dwarf2_per_objfile->has_section_at_zero)
14556 {
14557 complaint (&symfile_complaints,
14558 _(".debug_ranges entry has start address of zero"
4262abfb 14559 " [in module %s]"), objfile_name (objfile));
01093045
DE
14560 continue;
14561 }
14562
5f46c5a5
JK
14563 callback (range_beginning, range_end);
14564 }
14565
14566 return 1;
14567}
14568
14569/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14570 Return 1 if the attributes are present and valid, otherwise, return 0.
14571 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14572
14573static int
14574dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14575 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14576 struct partial_symtab *ranges_pst)
14577{
14578 struct objfile *objfile = cu->objfile;
14579 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14580 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14581 SECT_OFF_TEXT (objfile));
14582 int low_set = 0;
14583 CORE_ADDR low = 0;
14584 CORE_ADDR high = 0;
14585 int retval;
14586
14587 retval = dwarf2_ranges_process (offset, cu,
14588 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14589 {
9277c30c 14590 if (ranges_pst != NULL)
3e29f34a
MR
14591 {
14592 CORE_ADDR lowpc;
14593 CORE_ADDR highpc;
14594
14595 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14596 range_beginning + baseaddr);
14597 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14598 range_end + baseaddr);
14599 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14600 ranges_pst);
14601 }
ff013f42 14602
43039443
JK
14603 /* FIXME: This is recording everything as a low-high
14604 segment of consecutive addresses. We should have a
14605 data structure for discontiguous block ranges
14606 instead. */
14607 if (! low_set)
14608 {
14609 low = range_beginning;
14610 high = range_end;
14611 low_set = 1;
14612 }
14613 else
14614 {
14615 if (range_beginning < low)
14616 low = range_beginning;
14617 if (range_end > high)
14618 high = range_end;
14619 }
5f46c5a5
JK
14620 });
14621 if (!retval)
14622 return 0;
43039443
JK
14623
14624 if (! low_set)
14625 /* If the first entry is an end-of-list marker, the range
14626 describes an empty scope, i.e. no instructions. */
14627 return 0;
14628
14629 if (low_return)
14630 *low_return = low;
14631 if (high_return)
14632 *high_return = high;
14633 return 1;
14634}
14635
3a2b436a
JK
14636/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14637 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14638 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14639
3a2b436a 14640static enum pc_bounds_kind
af34e669 14641dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14642 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14643 struct partial_symtab *pst)
c906108c
SS
14644{
14645 struct attribute *attr;
91da1414 14646 struct attribute *attr_high;
af34e669
DJ
14647 CORE_ADDR low = 0;
14648 CORE_ADDR high = 0;
e385593e 14649 enum pc_bounds_kind ret;
c906108c 14650
91da1414
MW
14651 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14652 if (attr_high)
af34e669 14653 {
e142c38c 14654 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 14655 if (attr)
91da1414 14656 {
31aa7e4e
JB
14657 low = attr_value_as_address (attr);
14658 high = attr_value_as_address (attr_high);
14659 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14660 high += low;
91da1414 14661 }
af34e669
DJ
14662 else
14663 /* Found high w/o low attribute. */
e385593e 14664 return PC_BOUNDS_INVALID;
af34e669
DJ
14665
14666 /* Found consecutive range of addresses. */
3a2b436a 14667 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14668 }
c906108c 14669 else
af34e669 14670 {
e142c38c 14671 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14672 if (attr != NULL)
14673 {
ab435259
DE
14674 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14675 We take advantage of the fact that DW_AT_ranges does not appear
14676 in DW_TAG_compile_unit of DWO files. */
14677 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14678 unsigned int ranges_offset = (DW_UNSND (attr)
14679 + (need_ranges_base
14680 ? cu->ranges_base
14681 : 0));
2e3cf129 14682
af34e669 14683 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14684 .debug_ranges section. */
2e3cf129 14685 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14686 return PC_BOUNDS_INVALID;
43039443 14687 /* Found discontinuous range of addresses. */
3a2b436a 14688 ret = PC_BOUNDS_RANGES;
af34e669 14689 }
e385593e
JK
14690 else
14691 return PC_BOUNDS_NOT_PRESENT;
af34e669 14692 }
c906108c 14693
9373cf26
JK
14694 /* read_partial_die has also the strict LOW < HIGH requirement. */
14695 if (high <= low)
e385593e 14696 return PC_BOUNDS_INVALID;
c906108c
SS
14697
14698 /* When using the GNU linker, .gnu.linkonce. sections are used to
14699 eliminate duplicate copies of functions and vtables and such.
14700 The linker will arbitrarily choose one and discard the others.
14701 The AT_*_pc values for such functions refer to local labels in
14702 these sections. If the section from that file was discarded, the
14703 labels are not in the output, so the relocs get a value of 0.
14704 If this is a discarded function, mark the pc bounds as invalid,
14705 so that GDB will ignore it. */
72dca2f5 14706 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14707 return PC_BOUNDS_INVALID;
c906108c
SS
14708
14709 *lowpc = low;
96408a79
SA
14710 if (highpc)
14711 *highpc = high;
af34e669 14712 return ret;
c906108c
SS
14713}
14714
b084d499
JB
14715/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14716 its low and high PC addresses. Do nothing if these addresses could not
14717 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14718 and HIGHPC to the high address if greater than HIGHPC. */
14719
14720static void
14721dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14722 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14723 struct dwarf2_cu *cu)
14724{
14725 CORE_ADDR low, high;
14726 struct die_info *child = die->child;
14727
e385593e 14728 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14729 {
325fac50
PA
14730 *lowpc = std::min (*lowpc, low);
14731 *highpc = std::max (*highpc, high);
b084d499
JB
14732 }
14733
14734 /* If the language does not allow nested subprograms (either inside
14735 subprograms or lexical blocks), we're done. */
14736 if (cu->language != language_ada)
14737 return;
6e70227d 14738
b084d499
JB
14739 /* Check all the children of the given DIE. If it contains nested
14740 subprograms, then check their pc bounds. Likewise, we need to
14741 check lexical blocks as well, as they may also contain subprogram
14742 definitions. */
14743 while (child && child->tag)
14744 {
14745 if (child->tag == DW_TAG_subprogram
14746 || child->tag == DW_TAG_lexical_block)
14747 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14748 child = sibling_die (child);
14749 }
14750}
14751
fae299cd
DC
14752/* Get the low and high pc's represented by the scope DIE, and store
14753 them in *LOWPC and *HIGHPC. If the correct values can't be
14754 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14755
14756static void
14757get_scope_pc_bounds (struct die_info *die,
14758 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14759 struct dwarf2_cu *cu)
14760{
14761 CORE_ADDR best_low = (CORE_ADDR) -1;
14762 CORE_ADDR best_high = (CORE_ADDR) 0;
14763 CORE_ADDR current_low, current_high;
14764
3a2b436a 14765 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14766 >= PC_BOUNDS_RANGES)
fae299cd
DC
14767 {
14768 best_low = current_low;
14769 best_high = current_high;
14770 }
14771 else
14772 {
14773 struct die_info *child = die->child;
14774
14775 while (child && child->tag)
14776 {
14777 switch (child->tag) {
14778 case DW_TAG_subprogram:
b084d499 14779 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14780 break;
14781 case DW_TAG_namespace:
f55ee35c 14782 case DW_TAG_module:
fae299cd
DC
14783 /* FIXME: carlton/2004-01-16: Should we do this for
14784 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14785 that current GCC's always emit the DIEs corresponding
14786 to definitions of methods of classes as children of a
14787 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14788 the DIEs giving the declarations, which could be
14789 anywhere). But I don't see any reason why the
14790 standards says that they have to be there. */
14791 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14792
14793 if (current_low != ((CORE_ADDR) -1))
14794 {
325fac50
PA
14795 best_low = std::min (best_low, current_low);
14796 best_high = std::max (best_high, current_high);
fae299cd
DC
14797 }
14798 break;
14799 default:
0963b4bd 14800 /* Ignore. */
fae299cd
DC
14801 break;
14802 }
14803
14804 child = sibling_die (child);
14805 }
14806 }
14807
14808 *lowpc = best_low;
14809 *highpc = best_high;
14810}
14811
801e3a5b
JB
14812/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14813 in DIE. */
380bca97 14814
801e3a5b
JB
14815static void
14816dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14817 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14818{
bb5ed363 14819 struct objfile *objfile = cu->objfile;
3e29f34a 14820 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14821 struct attribute *attr;
91da1414 14822 struct attribute *attr_high;
801e3a5b 14823
91da1414
MW
14824 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14825 if (attr_high)
801e3a5b 14826 {
801e3a5b
JB
14827 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14828 if (attr)
14829 {
31aa7e4e
JB
14830 CORE_ADDR low = attr_value_as_address (attr);
14831 CORE_ADDR high = attr_value_as_address (attr_high);
14832
14833 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14834 high += low;
9a619af0 14835
3e29f34a
MR
14836 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14837 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14838 record_block_range (block, low, high - 1);
801e3a5b
JB
14839 }
14840 }
14841
14842 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14843 if (attr)
14844 {
ab435259
DE
14845 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14846 We take advantage of the fact that DW_AT_ranges does not appear
14847 in DW_TAG_compile_unit of DWO files. */
14848 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14849
14850 /* The value of the DW_AT_ranges attribute is the offset of the
14851 address range list in the .debug_ranges section. */
ab435259
DE
14852 unsigned long offset = (DW_UNSND (attr)
14853 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 14854 const gdb_byte *buffer;
801e3a5b
JB
14855
14856 /* For some target architectures, but not others, the
14857 read_address function sign-extends the addresses it returns.
14858 To recognize base address selection entries, we need a
14859 mask. */
14860 unsigned int addr_size = cu->header.addr_size;
14861 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14862
14863 /* The base address, to which the next pair is relative. Note
14864 that this 'base' is a DWARF concept: most entries in a range
14865 list are relative, to reduce the number of relocs against the
14866 debugging information. This is separate from this function's
14867 'baseaddr' argument, which GDB uses to relocate debugging
14868 information from a shared library based on the address at
14869 which the library was loaded. */
d00adf39
DE
14870 CORE_ADDR base = cu->base_address;
14871 int base_known = cu->base_known;
801e3a5b 14872
5f46c5a5
JK
14873 dwarf2_ranges_process (offset, cu,
14874 [&] (CORE_ADDR start, CORE_ADDR end)
14875 {
58fdfd2c
JK
14876 start += baseaddr;
14877 end += baseaddr;
5f46c5a5
JK
14878 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14879 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14880 record_block_range (block, start, end - 1);
14881 });
801e3a5b
JB
14882 }
14883}
14884
685b1105
JK
14885/* Check whether the producer field indicates either of GCC < 4.6, or the
14886 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14887
685b1105
JK
14888static void
14889check_producer (struct dwarf2_cu *cu)
60d5a603 14890{
38360086 14891 int major, minor;
60d5a603
JK
14892
14893 if (cu->producer == NULL)
14894 {
14895 /* For unknown compilers expect their behavior is DWARF version
14896 compliant.
14897
14898 GCC started to support .debug_types sections by -gdwarf-4 since
14899 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14900 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14901 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14902 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14903 }
b1ffba5a 14904 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14905 {
38360086
MW
14906 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14907 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14908 }
5230b05a
WT
14909 else if (producer_is_icc (cu->producer, &major, &minor))
14910 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
14911 else
14912 {
14913 /* For other non-GCC compilers, expect their behavior is DWARF version
14914 compliant. */
60d5a603
JK
14915 }
14916
ba919b58 14917 cu->checked_producer = 1;
685b1105 14918}
ba919b58 14919
685b1105
JK
14920/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14921 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14922 during 4.6.0 experimental. */
14923
14924static int
14925producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14926{
14927 if (!cu->checked_producer)
14928 check_producer (cu);
14929
14930 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14931}
14932
14933/* Return the default accessibility type if it is not overriden by
14934 DW_AT_accessibility. */
14935
14936static enum dwarf_access_attribute
14937dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14938{
14939 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14940 {
14941 /* The default DWARF 2 accessibility for members is public, the default
14942 accessibility for inheritance is private. */
14943
14944 if (die->tag != DW_TAG_inheritance)
14945 return DW_ACCESS_public;
14946 else
14947 return DW_ACCESS_private;
14948 }
14949 else
14950 {
14951 /* DWARF 3+ defines the default accessibility a different way. The same
14952 rules apply now for DW_TAG_inheritance as for the members and it only
14953 depends on the container kind. */
14954
14955 if (die->parent->tag == DW_TAG_class_type)
14956 return DW_ACCESS_private;
14957 else
14958 return DW_ACCESS_public;
14959 }
14960}
14961
74ac6d43
TT
14962/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14963 offset. If the attribute was not found return 0, otherwise return
14964 1. If it was found but could not properly be handled, set *OFFSET
14965 to 0. */
14966
14967static int
14968handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14969 LONGEST *offset)
14970{
14971 struct attribute *attr;
14972
14973 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14974 if (attr != NULL)
14975 {
14976 *offset = 0;
14977
14978 /* Note that we do not check for a section offset first here.
14979 This is because DW_AT_data_member_location is new in DWARF 4,
14980 so if we see it, we can assume that a constant form is really
14981 a constant and not a section offset. */
14982 if (attr_form_is_constant (attr))
14983 *offset = dwarf2_get_attr_constant_value (attr, 0);
14984 else if (attr_form_is_section_offset (attr))
14985 dwarf2_complex_location_expr_complaint ();
14986 else if (attr_form_is_block (attr))
14987 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14988 else
14989 dwarf2_complex_location_expr_complaint ();
14990
14991 return 1;
14992 }
14993
14994 return 0;
14995}
14996
c906108c
SS
14997/* Add an aggregate field to the field list. */
14998
14999static void
107d2387 15000dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15001 struct dwarf2_cu *cu)
6e70227d 15002{
e7c27a73 15003 struct objfile *objfile = cu->objfile;
5e2b427d 15004 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15005 struct nextfield *new_field;
15006 struct attribute *attr;
15007 struct field *fp;
15d034d0 15008 const char *fieldname = "";
c906108c
SS
15009
15010 /* Allocate a new field list entry and link it in. */
8d749320 15011 new_field = XNEW (struct nextfield);
b8c9b27d 15012 make_cleanup (xfree, new_field);
c906108c 15013 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
15014
15015 if (die->tag == DW_TAG_inheritance)
15016 {
15017 new_field->next = fip->baseclasses;
15018 fip->baseclasses = new_field;
15019 }
15020 else
15021 {
15022 new_field->next = fip->fields;
15023 fip->fields = new_field;
15024 }
c906108c
SS
15025 fip->nfields++;
15026
e142c38c 15027 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
15028 if (attr)
15029 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15030 else
15031 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15032 if (new_field->accessibility != DW_ACCESS_public)
15033 fip->non_public_fields = 1;
60d5a603 15034
e142c38c 15035 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
15036 if (attr)
15037 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15038 else
15039 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15040
15041 fp = &new_field->field;
a9a9bd0f 15042
e142c38c 15043 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15044 {
74ac6d43
TT
15045 LONGEST offset;
15046
a9a9bd0f 15047 /* Data member other than a C++ static data member. */
6e70227d 15048
c906108c 15049 /* Get type of field. */
e7c27a73 15050 fp->type = die_type (die, cu);
c906108c 15051
d6a843b5 15052 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15053
c906108c 15054 /* Get bit size of field (zero if none). */
e142c38c 15055 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
15056 if (attr)
15057 {
15058 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15059 }
15060 else
15061 {
15062 FIELD_BITSIZE (*fp) = 0;
15063 }
15064
15065 /* Get bit offset of field. */
74ac6d43
TT
15066 if (handle_data_member_location (die, cu, &offset))
15067 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15068 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
15069 if (attr)
15070 {
5e2b427d 15071 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
15072 {
15073 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15074 additional bit offset from the MSB of the containing
15075 anonymous object to the MSB of the field. We don't
15076 have to do anything special since we don't need to
15077 know the size of the anonymous object. */
f41f5e61 15078 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15079 }
15080 else
15081 {
15082 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15083 MSB of the anonymous object, subtract off the number of
15084 bits from the MSB of the field to the MSB of the
15085 object, and then subtract off the number of bits of
15086 the field itself. The result is the bit offset of
15087 the LSB of the field. */
c906108c
SS
15088 int anonymous_size;
15089 int bit_offset = DW_UNSND (attr);
15090
e142c38c 15091 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15092 if (attr)
15093 {
15094 /* The size of the anonymous object containing
15095 the bit field is explicit, so use the
15096 indicated size (in bytes). */
15097 anonymous_size = DW_UNSND (attr);
15098 }
15099 else
15100 {
15101 /* The size of the anonymous object containing
15102 the bit field must be inferred from the type
15103 attribute of the data member containing the
15104 bit field. */
15105 anonymous_size = TYPE_LENGTH (fp->type);
15106 }
f41f5e61
PA
15107 SET_FIELD_BITPOS (*fp,
15108 (FIELD_BITPOS (*fp)
15109 + anonymous_size * bits_per_byte
15110 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15111 }
15112 }
da5b30da
AA
15113 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15114 if (attr != NULL)
15115 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15116 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15117
15118 /* Get name of field. */
39cbfefa
DJ
15119 fieldname = dwarf2_name (die, cu);
15120 if (fieldname == NULL)
15121 fieldname = "";
d8151005
DJ
15122
15123 /* The name is already allocated along with this objfile, so we don't
15124 need to duplicate it for the type. */
15125 fp->name = fieldname;
c906108c
SS
15126
15127 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15128 pointer or virtual base class pointer) to private. */
e142c38c 15129 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15130 {
d48cc9dd 15131 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15132 new_field->accessibility = DW_ACCESS_private;
15133 fip->non_public_fields = 1;
15134 }
15135 }
a9a9bd0f 15136 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15137 {
a9a9bd0f
DC
15138 /* C++ static member. */
15139
15140 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15141 is a declaration, but all versions of G++ as of this writing
15142 (so through at least 3.2.1) incorrectly generate
15143 DW_TAG_variable tags. */
6e70227d 15144
ff355380 15145 const char *physname;
c906108c 15146
a9a9bd0f 15147 /* Get name of field. */
39cbfefa
DJ
15148 fieldname = dwarf2_name (die, cu);
15149 if (fieldname == NULL)
c906108c
SS
15150 return;
15151
254e6b9e 15152 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15153 if (attr
15154 /* Only create a symbol if this is an external value.
15155 new_symbol checks this and puts the value in the global symbol
15156 table, which we want. If it is not external, new_symbol
15157 will try to put the value in cu->list_in_scope which is wrong. */
15158 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15159 {
15160 /* A static const member, not much different than an enum as far as
15161 we're concerned, except that we can support more types. */
15162 new_symbol (die, NULL, cu);
15163 }
15164
2df3850c 15165 /* Get physical name. */
ff355380 15166 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15167
d8151005
DJ
15168 /* The name is already allocated along with this objfile, so we don't
15169 need to duplicate it for the type. */
15170 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15171 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15172 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15173 }
15174 else if (die->tag == DW_TAG_inheritance)
15175 {
74ac6d43 15176 LONGEST offset;
d4b96c9a 15177
74ac6d43
TT
15178 /* C++ base class field. */
15179 if (handle_data_member_location (die, cu, &offset))
15180 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15181 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15182 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
15183 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15184 fip->nbaseclasses++;
15185 }
15186}
15187
883fd55a
KS
15188/* Can the type given by DIE define another type? */
15189
15190static bool
15191type_can_define_types (const struct die_info *die)
15192{
15193 switch (die->tag)
15194 {
15195 case DW_TAG_typedef:
15196 case DW_TAG_class_type:
15197 case DW_TAG_structure_type:
15198 case DW_TAG_union_type:
15199 case DW_TAG_enumeration_type:
15200 return true;
15201
15202 default:
15203 return false;
15204 }
15205}
15206
15207/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15208
15209static void
883fd55a
KS
15210dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15211 struct dwarf2_cu *cu)
6e70227d 15212{
883fd55a
KS
15213 struct decl_field_list *new_field;
15214 struct decl_field *fp;
98751a41
JK
15215
15216 /* Allocate a new field list entry and link it in. */
883fd55a 15217 new_field = XCNEW (struct decl_field_list);
98751a41
JK
15218 make_cleanup (xfree, new_field);
15219
883fd55a 15220 gdb_assert (type_can_define_types (die));
98751a41
JK
15221
15222 fp = &new_field->field;
15223
883fd55a 15224 /* Get name of field. NULL is okay here, meaning an anonymous type. */
98751a41 15225 fp->name = dwarf2_name (die, cu);
98751a41
JK
15226 fp->type = read_type_die (die, cu);
15227
c191a687
KS
15228 /* Save accessibility. */
15229 enum dwarf_access_attribute accessibility;
15230 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15231 if (attr != NULL)
15232 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15233 else
15234 accessibility = dwarf2_default_access_attribute (die, cu);
15235 switch (accessibility)
15236 {
15237 case DW_ACCESS_public:
15238 /* The assumed value if neither private nor protected. */
15239 break;
15240 case DW_ACCESS_private:
15241 fp->is_private = 1;
15242 break;
15243 case DW_ACCESS_protected:
15244 fp->is_protected = 1;
15245 break;
15246 default:
37534686
KS
15247 complaint (&symfile_complaints,
15248 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15249 }
15250
883fd55a
KS
15251 if (die->tag == DW_TAG_typedef)
15252 {
15253 new_field->next = fip->typedef_field_list;
15254 fip->typedef_field_list = new_field;
15255 fip->typedef_field_list_count++;
15256 }
15257 else
15258 {
15259 new_field->next = fip->nested_types_list;
15260 fip->nested_types_list = new_field;
15261 fip->nested_types_list_count++;
15262 }
98751a41
JK
15263}
15264
c906108c
SS
15265/* Create the vector of fields, and attach it to the type. */
15266
15267static void
fba45db2 15268dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15269 struct dwarf2_cu *cu)
c906108c
SS
15270{
15271 int nfields = fip->nfields;
15272
15273 /* Record the field count, allocate space for the array of fields,
15274 and create blank accessibility bitfields if necessary. */
15275 TYPE_NFIELDS (type) = nfields;
15276 TYPE_FIELDS (type) = (struct field *)
15277 TYPE_ALLOC (type, sizeof (struct field) * nfields);
15278 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
15279
b4ba55a1 15280 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15281 {
15282 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15283
15284 TYPE_FIELD_PRIVATE_BITS (type) =
15285 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15286 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15287
15288 TYPE_FIELD_PROTECTED_BITS (type) =
15289 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15290 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15291
774b6a14
TT
15292 TYPE_FIELD_IGNORE_BITS (type) =
15293 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15294 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15295 }
15296
15297 /* If the type has baseclasses, allocate and clear a bit vector for
15298 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 15299 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
15300 {
15301 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 15302 unsigned char *pointer;
c906108c
SS
15303
15304 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15305 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15306 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
15307 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
15308 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
15309 }
15310
3e43a32a
MS
15311 /* Copy the saved-up fields into the field vector. Start from the head of
15312 the list, adding to the tail of the field array, so that they end up in
15313 the same order in the array in which they were added to the list. */
c906108c
SS
15314 while (nfields-- > 0)
15315 {
7d0ccb61
DJ
15316 struct nextfield *fieldp;
15317
15318 if (fip->fields)
15319 {
15320 fieldp = fip->fields;
15321 fip->fields = fieldp->next;
15322 }
15323 else
15324 {
15325 fieldp = fip->baseclasses;
15326 fip->baseclasses = fieldp->next;
15327 }
15328
15329 TYPE_FIELD (type, nfields) = fieldp->field;
15330 switch (fieldp->accessibility)
c906108c 15331 {
c5aa993b 15332 case DW_ACCESS_private:
b4ba55a1
JB
15333 if (cu->language != language_ada)
15334 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 15335 break;
c906108c 15336
c5aa993b 15337 case DW_ACCESS_protected:
b4ba55a1
JB
15338 if (cu->language != language_ada)
15339 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 15340 break;
c906108c 15341
c5aa993b
JM
15342 case DW_ACCESS_public:
15343 break;
c906108c 15344
c5aa993b
JM
15345 default:
15346 /* Unknown accessibility. Complain and treat it as public. */
15347 {
e2e0b3e5 15348 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 15349 fieldp->accessibility);
c5aa993b
JM
15350 }
15351 break;
c906108c
SS
15352 }
15353 if (nfields < fip->nbaseclasses)
15354 {
7d0ccb61 15355 switch (fieldp->virtuality)
c906108c 15356 {
c5aa993b
JM
15357 case DW_VIRTUALITY_virtual:
15358 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15359 if (cu->language == language_ada)
a73c6dcd 15360 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
15361 SET_TYPE_FIELD_VIRTUAL (type, nfields);
15362 break;
c906108c
SS
15363 }
15364 }
c906108c
SS
15365 }
15366}
15367
7d27a96d
TT
15368/* Return true if this member function is a constructor, false
15369 otherwise. */
15370
15371static int
15372dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15373{
15374 const char *fieldname;
fe978cb0 15375 const char *type_name;
7d27a96d
TT
15376 int len;
15377
15378 if (die->parent == NULL)
15379 return 0;
15380
15381 if (die->parent->tag != DW_TAG_structure_type
15382 && die->parent->tag != DW_TAG_union_type
15383 && die->parent->tag != DW_TAG_class_type)
15384 return 0;
15385
15386 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15387 type_name = dwarf2_name (die->parent, cu);
15388 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15389 return 0;
15390
15391 len = strlen (fieldname);
fe978cb0
PA
15392 return (strncmp (fieldname, type_name, len) == 0
15393 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15394}
15395
c906108c
SS
15396/* Add a member function to the proper fieldlist. */
15397
15398static void
107d2387 15399dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15400 struct type *type, struct dwarf2_cu *cu)
c906108c 15401{
e7c27a73 15402 struct objfile *objfile = cu->objfile;
c906108c
SS
15403 struct attribute *attr;
15404 struct fnfieldlist *flp;
15405 int i;
15406 struct fn_field *fnp;
15d034d0 15407 const char *fieldname;
c906108c 15408 struct nextfnfield *new_fnfield;
f792889a 15409 struct type *this_type;
60d5a603 15410 enum dwarf_access_attribute accessibility;
c906108c 15411
b4ba55a1 15412 if (cu->language == language_ada)
a73c6dcd 15413 error (_("unexpected member function in Ada type"));
b4ba55a1 15414
2df3850c 15415 /* Get name of member function. */
39cbfefa
DJ
15416 fieldname = dwarf2_name (die, cu);
15417 if (fieldname == NULL)
2df3850c 15418 return;
c906108c 15419
c906108c
SS
15420 /* Look up member function name in fieldlist. */
15421 for (i = 0; i < fip->nfnfields; i++)
15422 {
27bfe10e 15423 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
15424 break;
15425 }
15426
15427 /* Create new list element if necessary. */
15428 if (i < fip->nfnfields)
15429 flp = &fip->fnfieldlists[i];
15430 else
15431 {
15432 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
15433 {
15434 fip->fnfieldlists = (struct fnfieldlist *)
15435 xrealloc (fip->fnfieldlists,
15436 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 15437 * sizeof (struct fnfieldlist));
c906108c 15438 if (fip->nfnfields == 0)
c13c43fd 15439 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
15440 }
15441 flp = &fip->fnfieldlists[fip->nfnfields];
15442 flp->name = fieldname;
15443 flp->length = 0;
15444 flp->head = NULL;
3da10d80 15445 i = fip->nfnfields++;
c906108c
SS
15446 }
15447
15448 /* Create a new member function field and chain it to the field list
0963b4bd 15449 entry. */
8d749320 15450 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 15451 make_cleanup (xfree, new_fnfield);
c906108c
SS
15452 memset (new_fnfield, 0, sizeof (struct nextfnfield));
15453 new_fnfield->next = flp->head;
15454 flp->head = new_fnfield;
15455 flp->length++;
15456
15457 /* Fill in the member function field info. */
15458 fnp = &new_fnfield->fnfield;
3da10d80
KS
15459
15460 /* Delay processing of the physname until later. */
9c37b5ae 15461 if (cu->language == language_cplus)
3da10d80
KS
15462 {
15463 add_to_method_list (type, i, flp->length - 1, fieldname,
15464 die, cu);
15465 }
15466 else
15467 {
1d06ead6 15468 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15469 fnp->physname = physname ? physname : "";
15470 }
15471
c906108c 15472 fnp->type = alloc_type (objfile);
f792889a
DJ
15473 this_type = read_type_die (die, cu);
15474 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15475 {
f792889a 15476 int nparams = TYPE_NFIELDS (this_type);
c906108c 15477
f792889a 15478 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15479 of the method itself (TYPE_CODE_METHOD). */
15480 smash_to_method_type (fnp->type, type,
f792889a
DJ
15481 TYPE_TARGET_TYPE (this_type),
15482 TYPE_FIELDS (this_type),
15483 TYPE_NFIELDS (this_type),
15484 TYPE_VARARGS (this_type));
c906108c
SS
15485
15486 /* Handle static member functions.
c5aa993b 15487 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15488 member functions. G++ helps GDB by marking the first
15489 parameter for non-static member functions (which is the this
15490 pointer) as artificial. We obtain this information from
15491 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15492 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15493 fnp->voffset = VOFFSET_STATIC;
15494 }
15495 else
e2e0b3e5 15496 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 15497 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15498
15499 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15500 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15501 fnp->fcontext = die_containing_type (die, cu);
c906108c 15502
3e43a32a
MS
15503 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15504 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15505
15506 /* Get accessibility. */
e142c38c 15507 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15508 if (attr)
aead7601 15509 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15510 else
15511 accessibility = dwarf2_default_access_attribute (die, cu);
15512 switch (accessibility)
c906108c 15513 {
60d5a603
JK
15514 case DW_ACCESS_private:
15515 fnp->is_private = 1;
15516 break;
15517 case DW_ACCESS_protected:
15518 fnp->is_protected = 1;
15519 break;
c906108c
SS
15520 }
15521
b02dede2 15522 /* Check for artificial methods. */
e142c38c 15523 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15524 if (attr && DW_UNSND (attr) != 0)
15525 fnp->is_artificial = 1;
15526
7d27a96d
TT
15527 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15528
0d564a31 15529 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15530 function. For older versions of GCC, this is an offset in the
15531 appropriate virtual table, as specified by DW_AT_containing_type.
15532 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15533 to the object address. */
15534
e142c38c 15535 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15536 if (attr)
8e19ed76 15537 {
aec5aa8b 15538 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15539 {
aec5aa8b
TT
15540 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15541 {
15542 /* Old-style GCC. */
15543 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15544 }
15545 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15546 || (DW_BLOCK (attr)->size > 1
15547 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15548 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15549 {
aec5aa8b
TT
15550 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15551 if ((fnp->voffset % cu->header.addr_size) != 0)
15552 dwarf2_complex_location_expr_complaint ();
15553 else
15554 fnp->voffset /= cu->header.addr_size;
15555 fnp->voffset += 2;
15556 }
15557 else
15558 dwarf2_complex_location_expr_complaint ();
15559
15560 if (!fnp->fcontext)
7e993ebf
KS
15561 {
15562 /* If there is no `this' field and no DW_AT_containing_type,
15563 we cannot actually find a base class context for the
15564 vtable! */
15565 if (TYPE_NFIELDS (this_type) == 0
15566 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15567 {
15568 complaint (&symfile_complaints,
15569 _("cannot determine context for virtual member "
15570 "function \"%s\" (offset %d)"),
9c541725 15571 fieldname, to_underlying (die->sect_off));
7e993ebf
KS
15572 }
15573 else
15574 {
15575 fnp->fcontext
15576 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15577 }
15578 }
aec5aa8b 15579 }
3690dd37 15580 else if (attr_form_is_section_offset (attr))
8e19ed76 15581 {
4d3c2250 15582 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15583 }
15584 else
15585 {
4d3c2250
KB
15586 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15587 fieldname);
8e19ed76 15588 }
0d564a31 15589 }
d48cc9dd
DJ
15590 else
15591 {
15592 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15593 if (attr && DW_UNSND (attr))
15594 {
15595 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15596 complaint (&symfile_complaints,
3e43a32a
MS
15597 _("Member function \"%s\" (offset %d) is virtual "
15598 "but the vtable offset is not specified"),
9c541725 15599 fieldname, to_underlying (die->sect_off));
9655fd1a 15600 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15601 TYPE_CPLUS_DYNAMIC (type) = 1;
15602 }
15603 }
c906108c
SS
15604}
15605
15606/* Create the vector of member function fields, and attach it to the type. */
15607
15608static void
fba45db2 15609dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15610 struct dwarf2_cu *cu)
c906108c
SS
15611{
15612 struct fnfieldlist *flp;
c906108c
SS
15613 int i;
15614
b4ba55a1 15615 if (cu->language == language_ada)
a73c6dcd 15616 error (_("unexpected member functions in Ada type"));
b4ba55a1 15617
c906108c
SS
15618 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15619 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15620 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
15621
15622 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
15623 {
15624 struct nextfnfield *nfp = flp->head;
15625 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15626 int k;
15627
15628 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
15629 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
15630 fn_flp->fn_fields = (struct fn_field *)
15631 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
15632 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 15633 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
15634 }
15635
15636 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
15637}
15638
1168df01
JB
15639/* Returns non-zero if NAME is the name of a vtable member in CU's
15640 language, zero otherwise. */
15641static int
15642is_vtable_name (const char *name, struct dwarf2_cu *cu)
15643{
15644 static const char vptr[] = "_vptr";
15645
9c37b5ae
TT
15646 /* Look for the C++ form of the vtable. */
15647 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15648 return 1;
15649
15650 return 0;
15651}
15652
c0dd20ea 15653/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15654 functions, with the ABI-specified layout. If TYPE describes
15655 such a structure, smash it into a member function type.
61049d3b
DJ
15656
15657 GCC shouldn't do this; it should just output pointer to member DIEs.
15658 This is GCC PR debug/28767. */
c0dd20ea 15659
0b92b5bb
TT
15660static void
15661quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15662{
09e2d7c7 15663 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15664
15665 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15666 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15667 return;
c0dd20ea
DJ
15668
15669 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15670 if (TYPE_FIELD_NAME (type, 0) == NULL
15671 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15672 || TYPE_FIELD_NAME (type, 1) == NULL
15673 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15674 return;
c0dd20ea
DJ
15675
15676 /* Find the type of the method. */
0b92b5bb 15677 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15678 if (pfn_type == NULL
15679 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15680 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15681 return;
c0dd20ea
DJ
15682
15683 /* Look for the "this" argument. */
15684 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15685 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15686 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15687 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15688 return;
c0dd20ea 15689
09e2d7c7 15690 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15691 new_type = alloc_type (objfile);
09e2d7c7 15692 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15693 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15694 TYPE_VARARGS (pfn_type));
0b92b5bb 15695 smash_to_methodptr_type (type, new_type);
c0dd20ea 15696}
1168df01 15697
685b1105 15698
c906108c 15699/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15700 (definition) to create a type for the structure or union. Fill in
15701 the type's name and general properties; the members will not be
83655187
DE
15702 processed until process_structure_scope. A symbol table entry for
15703 the type will also not be done until process_structure_scope (assuming
15704 the type has a name).
c906108c 15705
c767944b
DJ
15706 NOTE: we need to call these functions regardless of whether or not the
15707 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15708 structure or union. This gets the type entered into our set of
83655187 15709 user defined types. */
c906108c 15710
f792889a 15711static struct type *
134d01f1 15712read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15713{
e7c27a73 15714 struct objfile *objfile = cu->objfile;
c906108c
SS
15715 struct type *type;
15716 struct attribute *attr;
15d034d0 15717 const char *name;
c906108c 15718
348e048f
DE
15719 /* If the definition of this type lives in .debug_types, read that type.
15720 Don't follow DW_AT_specification though, that will take us back up
15721 the chain and we want to go down. */
45e58e77 15722 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
15723 if (attr)
15724 {
ac9ec31b 15725 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15726
ac9ec31b 15727 /* The type's CU may not be the same as CU.
02142a6c 15728 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15729 return set_die_type (die, type, cu);
15730 }
15731
c0dd20ea 15732 type = alloc_type (objfile);
c906108c 15733 INIT_CPLUS_SPECIFIC (type);
93311388 15734
39cbfefa
DJ
15735 name = dwarf2_name (die, cu);
15736 if (name != NULL)
c906108c 15737 {
987504bb 15738 if (cu->language == language_cplus
c44af4eb
TT
15739 || cu->language == language_d
15740 || cu->language == language_rust)
63d06c5c 15741 {
15d034d0 15742 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15743
15744 /* dwarf2_full_name might have already finished building the DIE's
15745 type. If so, there is no need to continue. */
15746 if (get_die_type (die, cu) != NULL)
15747 return get_die_type (die, cu);
15748
15749 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
15750 if (die->tag == DW_TAG_structure_type
15751 || die->tag == DW_TAG_class_type)
15752 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
15753 }
15754 else
15755 {
d8151005
DJ
15756 /* The name is already allocated along with this objfile, so
15757 we don't need to duplicate it for the type. */
7d455152 15758 TYPE_TAG_NAME (type) = name;
94af9270
KS
15759 if (die->tag == DW_TAG_class_type)
15760 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 15761 }
c906108c
SS
15762 }
15763
15764 if (die->tag == DW_TAG_structure_type)
15765 {
15766 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15767 }
15768 else if (die->tag == DW_TAG_union_type)
15769 {
15770 TYPE_CODE (type) = TYPE_CODE_UNION;
15771 }
15772 else
15773 {
4753d33b 15774 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15775 }
15776
0cc2414c
TT
15777 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15778 TYPE_DECLARED_CLASS (type) = 1;
15779
e142c38c 15780 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15781 if (attr)
15782 {
155bfbd3
JB
15783 if (attr_form_is_constant (attr))
15784 TYPE_LENGTH (type) = DW_UNSND (attr);
15785 else
15786 {
15787 /* For the moment, dynamic type sizes are not supported
15788 by GDB's struct type. The actual size is determined
15789 on-demand when resolving the type of a given object,
15790 so set the type's length to zero for now. Otherwise,
15791 we record an expression as the length, and that expression
15792 could lead to a very large value, which could eventually
15793 lead to us trying to allocate that much memory when creating
15794 a value of that type. */
15795 TYPE_LENGTH (type) = 0;
15796 }
c906108c
SS
15797 }
15798 else
15799 {
15800 TYPE_LENGTH (type) = 0;
15801 }
15802
5230b05a 15803 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15804 {
5230b05a
WT
15805 /* ICC<14 does not output the required DW_AT_declaration on
15806 incomplete types, but gives them a size of zero. */
422b1cb0 15807 TYPE_STUB (type) = 1;
685b1105
JK
15808 }
15809 else
15810 TYPE_STUB_SUPPORTED (type) = 1;
15811
dc718098 15812 if (die_is_declaration (die, cu))
876cecd0 15813 TYPE_STUB (type) = 1;
a6c727b2
DJ
15814 else if (attr == NULL && die->child == NULL
15815 && producer_is_realview (cu->producer))
15816 /* RealView does not output the required DW_AT_declaration
15817 on incomplete types. */
15818 TYPE_STUB (type) = 1;
dc718098 15819
c906108c
SS
15820 /* We need to add the type field to the die immediately so we don't
15821 infinitely recurse when dealing with pointers to the structure
0963b4bd 15822 type within the structure itself. */
1c379e20 15823 set_die_type (die, type, cu);
c906108c 15824
7e314c57
JK
15825 /* set_die_type should be already done. */
15826 set_descriptive_type (type, die, cu);
15827
c767944b
DJ
15828 return type;
15829}
15830
15831/* Finish creating a structure or union type, including filling in
15832 its members and creating a symbol for it. */
15833
15834static void
15835process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15836{
15837 struct objfile *objfile = cu->objfile;
ca040673 15838 struct die_info *child_die;
c767944b
DJ
15839 struct type *type;
15840
15841 type = get_die_type (die, cu);
15842 if (type == NULL)
15843 type = read_structure_type (die, cu);
15844
e142c38c 15845 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15846 {
15847 struct field_info fi;
2f4732b0 15848 std::vector<struct symbol *> template_args;
c767944b 15849 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
15850
15851 memset (&fi, 0, sizeof (struct field_info));
15852
639d11d3 15853 child_die = die->child;
c906108c
SS
15854
15855 while (child_die && child_die->tag)
15856 {
a9a9bd0f
DC
15857 if (child_die->tag == DW_TAG_member
15858 || child_die->tag == DW_TAG_variable)
c906108c 15859 {
a9a9bd0f
DC
15860 /* NOTE: carlton/2002-11-05: A C++ static data member
15861 should be a DW_TAG_member that is a declaration, but
15862 all versions of G++ as of this writing (so through at
15863 least 3.2.1) incorrectly generate DW_TAG_variable
15864 tags for them instead. */
e7c27a73 15865 dwarf2_add_field (&fi, child_die, cu);
c906108c 15866 }
8713b1b1 15867 else if (child_die->tag == DW_TAG_subprogram)
c906108c 15868 {
e98c9e7c
TT
15869 /* Rust doesn't have member functions in the C++ sense.
15870 However, it does emit ordinary functions as children
15871 of a struct DIE. */
15872 if (cu->language == language_rust)
15873 read_func_scope (child_die, cu);
15874 else
15875 {
15876 /* C++ member function. */
15877 dwarf2_add_member_fn (&fi, child_die, type, cu);
15878 }
c906108c
SS
15879 }
15880 else if (child_die->tag == DW_TAG_inheritance)
15881 {
15882 /* C++ base class field. */
e7c27a73 15883 dwarf2_add_field (&fi, child_die, cu);
c906108c 15884 }
883fd55a
KS
15885 else if (type_can_define_types (child_die))
15886 dwarf2_add_type_defn (&fi, child_die, cu);
34eaf542
TT
15887 else if (child_die->tag == DW_TAG_template_type_param
15888 || child_die->tag == DW_TAG_template_value_param)
15889 {
15890 struct symbol *arg = new_symbol (child_die, NULL, cu);
15891
f1078f66 15892 if (arg != NULL)
2f4732b0 15893 template_args.push_back (arg);
34eaf542
TT
15894 }
15895
c906108c
SS
15896 child_die = sibling_die (child_die);
15897 }
15898
34eaf542 15899 /* Attach template arguments to type. */
2f4732b0 15900 if (!template_args.empty ())
34eaf542
TT
15901 {
15902 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15903 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15904 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15905 = XOBNEWVEC (&objfile->objfile_obstack,
15906 struct symbol *,
15907 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15908 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15909 template_args.data (),
34eaf542
TT
15910 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15911 * sizeof (struct symbol *)));
34eaf542
TT
15912 }
15913
c906108c
SS
15914 /* Attach fields and member functions to the type. */
15915 if (fi.nfields)
e7c27a73 15916 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
15917 if (fi.nfnfields)
15918 {
e7c27a73 15919 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15920
c5aa993b 15921 /* Get the type which refers to the base class (possibly this
c906108c 15922 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15923 class from the DW_AT_containing_type attribute. This use of
15924 DW_AT_containing_type is a GNU extension. */
c906108c 15925
e142c38c 15926 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15927 {
e7c27a73 15928 struct type *t = die_containing_type (die, cu);
c906108c 15929
ae6ae975 15930 set_type_vptr_basetype (type, t);
c906108c
SS
15931 if (type == t)
15932 {
c906108c
SS
15933 int i;
15934
15935 /* Our own class provides vtbl ptr. */
15936 for (i = TYPE_NFIELDS (t) - 1;
15937 i >= TYPE_N_BASECLASSES (t);
15938 --i)
15939 {
0d5cff50 15940 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15941
1168df01 15942 if (is_vtable_name (fieldname, cu))
c906108c 15943 {
ae6ae975 15944 set_type_vptr_fieldno (type, i);
c906108c
SS
15945 break;
15946 }
15947 }
15948
15949 /* Complain if virtual function table field not found. */
15950 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 15951 complaint (&symfile_complaints,
3e43a32a
MS
15952 _("virtual function table pointer "
15953 "not found when defining class '%s'"),
4d3c2250
KB
15954 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
15955 "");
c906108c
SS
15956 }
15957 else
15958 {
ae6ae975 15959 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15960 }
15961 }
f6235d4c 15962 else if (cu->producer
61012eef 15963 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15964 {
15965 /* The IBM XLC compiler does not provide direct indication
15966 of the containing type, but the vtable pointer is
15967 always named __vfp. */
15968
15969 int i;
15970
15971 for (i = TYPE_NFIELDS (type) - 1;
15972 i >= TYPE_N_BASECLASSES (type);
15973 --i)
15974 {
15975 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15976 {
ae6ae975
DE
15977 set_type_vptr_fieldno (type, i);
15978 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15979 break;
15980 }
15981 }
15982 }
c906108c 15983 }
98751a41
JK
15984
15985 /* Copy fi.typedef_field_list linked list elements content into the
15986 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15987 if (fi.typedef_field_list)
15988 {
15989 int i = fi.typedef_field_list_count;
15990
a0d7a4ff 15991 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15992 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15993 = ((struct decl_field *)
224c3ddb 15994 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
15995 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
15996
15997 /* Reverse the list order to keep the debug info elements order. */
15998 while (--i >= 0)
15999 {
883fd55a 16000 struct decl_field *dest, *src;
6e70227d 16001
98751a41
JK
16002 dest = &TYPE_TYPEDEF_FIELD (type, i);
16003 src = &fi.typedef_field_list->field;
16004 fi.typedef_field_list = fi.typedef_field_list->next;
16005 *dest = *src;
16006 }
16007 }
c767944b 16008
883fd55a
KS
16009 /* Copy fi.nested_types_list linked list elements content into the
16010 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16011 if (fi.nested_types_list != NULL && cu->language != language_ada)
16012 {
16013 int i = fi.nested_types_list_count;
16014
16015 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16016 TYPE_NESTED_TYPES_ARRAY (type)
16017 = ((struct decl_field *)
16018 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
16019 TYPE_NESTED_TYPES_COUNT (type) = i;
16020
16021 /* Reverse the list order to keep the debug info elements order. */
16022 while (--i >= 0)
16023 {
16024 struct decl_field *dest, *src;
16025
16026 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
16027 src = &fi.nested_types_list->field;
16028 fi.nested_types_list = fi.nested_types_list->next;
16029 *dest = *src;
16030 }
16031 }
16032
c767944b 16033 do_cleanups (back_to);
c906108c 16034 }
63d06c5c 16035
bb5ed363 16036 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 16037
90aeadfc
DC
16038 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16039 snapshots) has been known to create a die giving a declaration
16040 for a class that has, as a child, a die giving a definition for a
16041 nested class. So we have to process our children even if the
16042 current die is a declaration. Normally, of course, a declaration
16043 won't have any children at all. */
134d01f1 16044
ca040673
DE
16045 child_die = die->child;
16046
90aeadfc
DC
16047 while (child_die != NULL && child_die->tag)
16048 {
16049 if (child_die->tag == DW_TAG_member
16050 || child_die->tag == DW_TAG_variable
34eaf542
TT
16051 || child_die->tag == DW_TAG_inheritance
16052 || child_die->tag == DW_TAG_template_value_param
16053 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16054 {
90aeadfc 16055 /* Do nothing. */
134d01f1 16056 }
90aeadfc
DC
16057 else
16058 process_die (child_die, cu);
134d01f1 16059
90aeadfc 16060 child_die = sibling_die (child_die);
134d01f1
DJ
16061 }
16062
fa4028e9
JB
16063 /* Do not consider external references. According to the DWARF standard,
16064 these DIEs are identified by the fact that they have no byte_size
16065 attribute, and a declaration attribute. */
16066 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16067 || !die_is_declaration (die, cu))
c767944b 16068 new_symbol (die, type, cu);
134d01f1
DJ
16069}
16070
55426c9d
JB
16071/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16072 update TYPE using some information only available in DIE's children. */
16073
16074static void
16075update_enumeration_type_from_children (struct die_info *die,
16076 struct type *type,
16077 struct dwarf2_cu *cu)
16078{
60f7655a 16079 struct die_info *child_die;
55426c9d
JB
16080 int unsigned_enum = 1;
16081 int flag_enum = 1;
16082 ULONGEST mask = 0;
55426c9d 16083
8268c778 16084 auto_obstack obstack;
55426c9d 16085
60f7655a
DE
16086 for (child_die = die->child;
16087 child_die != NULL && child_die->tag;
16088 child_die = sibling_die (child_die))
55426c9d
JB
16089 {
16090 struct attribute *attr;
16091 LONGEST value;
16092 const gdb_byte *bytes;
16093 struct dwarf2_locexpr_baton *baton;
16094 const char *name;
60f7655a 16095
55426c9d
JB
16096 if (child_die->tag != DW_TAG_enumerator)
16097 continue;
16098
16099 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16100 if (attr == NULL)
16101 continue;
16102
16103 name = dwarf2_name (child_die, cu);
16104 if (name == NULL)
16105 name = "<anonymous enumerator>";
16106
16107 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16108 &value, &bytes, &baton);
16109 if (value < 0)
16110 {
16111 unsigned_enum = 0;
16112 flag_enum = 0;
16113 }
16114 else if ((mask & value) != 0)
16115 flag_enum = 0;
16116 else
16117 mask |= value;
16118
16119 /* If we already know that the enum type is neither unsigned, nor
16120 a flag type, no need to look at the rest of the enumerates. */
16121 if (!unsigned_enum && !flag_enum)
16122 break;
55426c9d
JB
16123 }
16124
16125 if (unsigned_enum)
16126 TYPE_UNSIGNED (type) = 1;
16127 if (flag_enum)
16128 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16129}
16130
134d01f1
DJ
16131/* Given a DW_AT_enumeration_type die, set its type. We do not
16132 complete the type's fields yet, or create any symbols. */
c906108c 16133
f792889a 16134static struct type *
134d01f1 16135read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16136{
e7c27a73 16137 struct objfile *objfile = cu->objfile;
c906108c 16138 struct type *type;
c906108c 16139 struct attribute *attr;
0114d602 16140 const char *name;
134d01f1 16141
348e048f
DE
16142 /* If the definition of this type lives in .debug_types, read that type.
16143 Don't follow DW_AT_specification though, that will take us back up
16144 the chain and we want to go down. */
45e58e77 16145 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16146 if (attr)
16147 {
ac9ec31b 16148 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16149
ac9ec31b 16150 /* The type's CU may not be the same as CU.
02142a6c 16151 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16152 return set_die_type (die, type, cu);
16153 }
16154
c906108c
SS
16155 type = alloc_type (objfile);
16156
16157 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16158 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16159 if (name != NULL)
7d455152 16160 TYPE_TAG_NAME (type) = name;
c906108c 16161
0626fc76
TT
16162 attr = dwarf2_attr (die, DW_AT_type, cu);
16163 if (attr != NULL)
16164 {
16165 struct type *underlying_type = die_type (die, cu);
16166
16167 TYPE_TARGET_TYPE (type) = underlying_type;
16168 }
16169
e142c38c 16170 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16171 if (attr)
16172 {
16173 TYPE_LENGTH (type) = DW_UNSND (attr);
16174 }
16175 else
16176 {
16177 TYPE_LENGTH (type) = 0;
16178 }
16179
137033e9
JB
16180 /* The enumeration DIE can be incomplete. In Ada, any type can be
16181 declared as private in the package spec, and then defined only
16182 inside the package body. Such types are known as Taft Amendment
16183 Types. When another package uses such a type, an incomplete DIE
16184 may be generated by the compiler. */
02eb380e 16185 if (die_is_declaration (die, cu))
876cecd0 16186 TYPE_STUB (type) = 1;
02eb380e 16187
0626fc76
TT
16188 /* Finish the creation of this type by using the enum's children.
16189 We must call this even when the underlying type has been provided
16190 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16191 update_enumeration_type_from_children (die, type, cu);
16192
0626fc76
TT
16193 /* If this type has an underlying type that is not a stub, then we
16194 may use its attributes. We always use the "unsigned" attribute
16195 in this situation, because ordinarily we guess whether the type
16196 is unsigned -- but the guess can be wrong and the underlying type
16197 can tell us the reality. However, we defer to a local size
16198 attribute if one exists, because this lets the compiler override
16199 the underlying type if needed. */
16200 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16201 {
16202 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16203 if (TYPE_LENGTH (type) == 0)
16204 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16205 }
16206
3d567982
TT
16207 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16208
f792889a 16209 return set_die_type (die, type, cu);
134d01f1
DJ
16210}
16211
16212/* Given a pointer to a die which begins an enumeration, process all
16213 the dies that define the members of the enumeration, and create the
16214 symbol for the enumeration type.
16215
16216 NOTE: We reverse the order of the element list. */
16217
16218static void
16219process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16220{
f792889a 16221 struct type *this_type;
134d01f1 16222
f792889a
DJ
16223 this_type = get_die_type (die, cu);
16224 if (this_type == NULL)
16225 this_type = read_enumeration_type (die, cu);
9dc481d3 16226
639d11d3 16227 if (die->child != NULL)
c906108c 16228 {
9dc481d3
DE
16229 struct die_info *child_die;
16230 struct symbol *sym;
16231 struct field *fields = NULL;
16232 int num_fields = 0;
15d034d0 16233 const char *name;
9dc481d3 16234
639d11d3 16235 child_die = die->child;
c906108c
SS
16236 while (child_die && child_die->tag)
16237 {
16238 if (child_die->tag != DW_TAG_enumerator)
16239 {
e7c27a73 16240 process_die (child_die, cu);
c906108c
SS
16241 }
16242 else
16243 {
39cbfefa
DJ
16244 name = dwarf2_name (child_die, cu);
16245 if (name)
c906108c 16246 {
f792889a 16247 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16248
16249 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16250 {
16251 fields = (struct field *)
16252 xrealloc (fields,
16253 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16254 * sizeof (struct field));
c906108c
SS
16255 }
16256
3567439c 16257 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16258 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16259 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16260 FIELD_BITSIZE (fields[num_fields]) = 0;
16261
16262 num_fields++;
16263 }
16264 }
16265
16266 child_die = sibling_die (child_die);
16267 }
16268
16269 if (num_fields)
16270 {
f792889a
DJ
16271 TYPE_NFIELDS (this_type) = num_fields;
16272 TYPE_FIELDS (this_type) = (struct field *)
16273 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16274 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16275 sizeof (struct field) * num_fields);
b8c9b27d 16276 xfree (fields);
c906108c 16277 }
c906108c 16278 }
134d01f1 16279
6c83ed52
TT
16280 /* If we are reading an enum from a .debug_types unit, and the enum
16281 is a declaration, and the enum is not the signatured type in the
16282 unit, then we do not want to add a symbol for it. Adding a
16283 symbol would in some cases obscure the true definition of the
16284 enum, giving users an incomplete type when the definition is
16285 actually available. Note that we do not want to do this for all
16286 enums which are just declarations, because C++0x allows forward
16287 enum declarations. */
3019eac3 16288 if (cu->per_cu->is_debug_types
6c83ed52
TT
16289 && die_is_declaration (die, cu))
16290 {
52dc124a 16291 struct signatured_type *sig_type;
6c83ed52 16292
c0f78cd4 16293 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16294 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16295 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16296 return;
16297 }
16298
f792889a 16299 new_symbol (die, this_type, cu);
c906108c
SS
16300}
16301
16302/* Extract all information from a DW_TAG_array_type DIE and put it in
16303 the DIE's type field. For now, this only handles one dimensional
16304 arrays. */
16305
f792889a 16306static struct type *
e7c27a73 16307read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16308{
e7c27a73 16309 struct objfile *objfile = cu->objfile;
c906108c 16310 struct die_info *child_die;
7e314c57 16311 struct type *type;
c906108c 16312 struct type *element_type, *range_type, *index_type;
c906108c 16313 struct attribute *attr;
15d034d0 16314 const char *name;
dc53a7ad 16315 unsigned int bit_stride = 0;
c906108c 16316
e7c27a73 16317 element_type = die_type (die, cu);
c906108c 16318
7e314c57
JK
16319 /* The die_type call above may have already set the type for this DIE. */
16320 type = get_die_type (die, cu);
16321 if (type)
16322 return type;
16323
dc53a7ad
JB
16324 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16325 if (attr != NULL)
16326 bit_stride = DW_UNSND (attr) * 8;
16327
16328 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16329 if (attr != NULL)
16330 bit_stride = DW_UNSND (attr);
16331
c906108c
SS
16332 /* Irix 6.2 native cc creates array types without children for
16333 arrays with unspecified length. */
639d11d3 16334 if (die->child == NULL)
c906108c 16335 {
46bf5051 16336 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16337 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
16338 type = create_array_type_with_stride (NULL, element_type, range_type,
16339 bit_stride);
f792889a 16340 return set_die_type (die, type, cu);
c906108c
SS
16341 }
16342
791afaa2 16343 std::vector<struct type *> range_types;
639d11d3 16344 child_die = die->child;
c906108c
SS
16345 while (child_die && child_die->tag)
16346 {
16347 if (child_die->tag == DW_TAG_subrange_type)
16348 {
f792889a 16349 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16350
f792889a 16351 if (child_type != NULL)
a02abb62 16352 {
0963b4bd
MS
16353 /* The range type was succesfully read. Save it for the
16354 array type creation. */
791afaa2 16355 range_types.push_back (child_type);
a02abb62 16356 }
c906108c
SS
16357 }
16358 child_die = sibling_die (child_die);
16359 }
16360
16361 /* Dwarf2 dimensions are output from left to right, create the
16362 necessary array types in backwards order. */
7ca2d3a3 16363
c906108c 16364 type = element_type;
7ca2d3a3
DL
16365
16366 if (read_array_order (die, cu) == DW_ORD_col_major)
16367 {
16368 int i = 0;
9a619af0 16369
791afaa2 16370 while (i < range_types.size ())
dc53a7ad
JB
16371 type = create_array_type_with_stride (NULL, type, range_types[i++],
16372 bit_stride);
7ca2d3a3
DL
16373 }
16374 else
16375 {
791afaa2 16376 size_t ndim = range_types.size ();
7ca2d3a3 16377 while (ndim-- > 0)
dc53a7ad
JB
16378 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16379 bit_stride);
7ca2d3a3 16380 }
c906108c 16381
f5f8a009
EZ
16382 /* Understand Dwarf2 support for vector types (like they occur on
16383 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16384 array type. This is not part of the Dwarf2/3 standard yet, but a
16385 custom vendor extension. The main difference between a regular
16386 array and the vector variant is that vectors are passed by value
16387 to functions. */
e142c38c 16388 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16389 if (attr)
ea37ba09 16390 make_vector_type (type);
f5f8a009 16391
dbc98a8b
KW
16392 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16393 implementation may choose to implement triple vectors using this
16394 attribute. */
16395 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16396 if (attr)
16397 {
16398 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16399 TYPE_LENGTH (type) = DW_UNSND (attr);
16400 else
3e43a32a
MS
16401 complaint (&symfile_complaints,
16402 _("DW_AT_byte_size for array type smaller "
16403 "than the total size of elements"));
dbc98a8b
KW
16404 }
16405
39cbfefa
DJ
16406 name = dwarf2_name (die, cu);
16407 if (name)
16408 TYPE_NAME (type) = name;
6e70227d 16409
0963b4bd 16410 /* Install the type in the die. */
7e314c57
JK
16411 set_die_type (die, type, cu);
16412
16413 /* set_die_type should be already done. */
b4ba55a1
JB
16414 set_descriptive_type (type, die, cu);
16415
7e314c57 16416 return type;
c906108c
SS
16417}
16418
7ca2d3a3 16419static enum dwarf_array_dim_ordering
6e70227d 16420read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16421{
16422 struct attribute *attr;
16423
16424 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16425
aead7601
SM
16426 if (attr)
16427 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16428
0963b4bd
MS
16429 /* GNU F77 is a special case, as at 08/2004 array type info is the
16430 opposite order to the dwarf2 specification, but data is still
16431 laid out as per normal fortran.
7ca2d3a3 16432
0963b4bd
MS
16433 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16434 version checking. */
7ca2d3a3 16435
905e0470
PM
16436 if (cu->language == language_fortran
16437 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16438 {
16439 return DW_ORD_row_major;
16440 }
16441
6e70227d 16442 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16443 {
16444 case array_column_major:
16445 return DW_ORD_col_major;
16446 case array_row_major:
16447 default:
16448 return DW_ORD_row_major;
16449 };
16450}
16451
72019c9c 16452/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16453 the DIE's type field. */
72019c9c 16454
f792889a 16455static struct type *
72019c9c
GM
16456read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16457{
7e314c57
JK
16458 struct type *domain_type, *set_type;
16459 struct attribute *attr;
f792889a 16460
7e314c57
JK
16461 domain_type = die_type (die, cu);
16462
16463 /* The die_type call above may have already set the type for this DIE. */
16464 set_type = get_die_type (die, cu);
16465 if (set_type)
16466 return set_type;
16467
16468 set_type = create_set_type (NULL, domain_type);
16469
16470 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16471 if (attr)
16472 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16473
f792889a 16474 return set_die_type (die, set_type, cu);
72019c9c 16475}
7ca2d3a3 16476
0971de02
TT
16477/* A helper for read_common_block that creates a locexpr baton.
16478 SYM is the symbol which we are marking as computed.
16479 COMMON_DIE is the DIE for the common block.
16480 COMMON_LOC is the location expression attribute for the common
16481 block itself.
16482 MEMBER_LOC is the location expression attribute for the particular
16483 member of the common block that we are processing.
16484 CU is the CU from which the above come. */
16485
16486static void
16487mark_common_block_symbol_computed (struct symbol *sym,
16488 struct die_info *common_die,
16489 struct attribute *common_loc,
16490 struct attribute *member_loc,
16491 struct dwarf2_cu *cu)
16492{
16493 struct objfile *objfile = dwarf2_per_objfile->objfile;
16494 struct dwarf2_locexpr_baton *baton;
16495 gdb_byte *ptr;
16496 unsigned int cu_off;
16497 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16498 LONGEST offset = 0;
16499
16500 gdb_assert (common_loc && member_loc);
16501 gdb_assert (attr_form_is_block (common_loc));
16502 gdb_assert (attr_form_is_block (member_loc)
16503 || attr_form_is_constant (member_loc));
16504
8d749320 16505 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16506 baton->per_cu = cu->per_cu;
16507 gdb_assert (baton->per_cu);
16508
16509 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16510
16511 if (attr_form_is_constant (member_loc))
16512 {
16513 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16514 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16515 }
16516 else
16517 baton->size += DW_BLOCK (member_loc)->size;
16518
224c3ddb 16519 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16520 baton->data = ptr;
16521
16522 *ptr++ = DW_OP_call4;
9c541725 16523 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16524 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16525 ptr += 4;
16526
16527 if (attr_form_is_constant (member_loc))
16528 {
16529 *ptr++ = DW_OP_addr;
16530 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16531 ptr += cu->header.addr_size;
16532 }
16533 else
16534 {
16535 /* We have to copy the data here, because DW_OP_call4 will only
16536 use a DW_AT_location attribute. */
16537 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16538 ptr += DW_BLOCK (member_loc)->size;
16539 }
16540
16541 *ptr++ = DW_OP_plus;
16542 gdb_assert (ptr - baton->data == baton->size);
16543
0971de02 16544 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16545 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16546}
16547
4357ac6c
TT
16548/* Create appropriate locally-scoped variables for all the
16549 DW_TAG_common_block entries. Also create a struct common_block
16550 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16551 is used to sepate the common blocks name namespace from regular
16552 variable names. */
c906108c
SS
16553
16554static void
e7c27a73 16555read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16556{
0971de02
TT
16557 struct attribute *attr;
16558
16559 attr = dwarf2_attr (die, DW_AT_location, cu);
16560 if (attr)
16561 {
16562 /* Support the .debug_loc offsets. */
16563 if (attr_form_is_block (attr))
16564 {
16565 /* Ok. */
16566 }
16567 else if (attr_form_is_section_offset (attr))
16568 {
16569 dwarf2_complex_location_expr_complaint ();
16570 attr = NULL;
16571 }
16572 else
16573 {
16574 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16575 "common block member");
16576 attr = NULL;
16577 }
16578 }
16579
639d11d3 16580 if (die->child != NULL)
c906108c 16581 {
4357ac6c
TT
16582 struct objfile *objfile = cu->objfile;
16583 struct die_info *child_die;
16584 size_t n_entries = 0, size;
16585 struct common_block *common_block;
16586 struct symbol *sym;
74ac6d43 16587
4357ac6c
TT
16588 for (child_die = die->child;
16589 child_die && child_die->tag;
16590 child_die = sibling_die (child_die))
16591 ++n_entries;
16592
16593 size = (sizeof (struct common_block)
16594 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16595 common_block
16596 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16597 size);
4357ac6c
TT
16598 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16599 common_block->n_entries = 0;
16600
16601 for (child_die = die->child;
16602 child_die && child_die->tag;
16603 child_die = sibling_die (child_die))
16604 {
16605 /* Create the symbol in the DW_TAG_common_block block in the current
16606 symbol scope. */
e7c27a73 16607 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16608 if (sym != NULL)
16609 {
16610 struct attribute *member_loc;
16611
16612 common_block->contents[common_block->n_entries++] = sym;
16613
16614 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16615 cu);
16616 if (member_loc)
16617 {
16618 /* GDB has handled this for a long time, but it is
16619 not specified by DWARF. It seems to have been
16620 emitted by gfortran at least as recently as:
16621 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16622 complaint (&symfile_complaints,
16623 _("Variable in common block has "
16624 "DW_AT_data_member_location "
16625 "- DIE at 0x%x [in module %s]"),
9c541725 16626 to_underlying (child_die->sect_off),
4262abfb 16627 objfile_name (cu->objfile));
0971de02
TT
16628
16629 if (attr_form_is_section_offset (member_loc))
16630 dwarf2_complex_location_expr_complaint ();
16631 else if (attr_form_is_constant (member_loc)
16632 || attr_form_is_block (member_loc))
16633 {
16634 if (attr)
16635 mark_common_block_symbol_computed (sym, die, attr,
16636 member_loc, cu);
16637 }
16638 else
16639 dwarf2_complex_location_expr_complaint ();
16640 }
16641 }
c906108c 16642 }
4357ac6c
TT
16643
16644 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16645 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16646 }
16647}
16648
0114d602 16649/* Create a type for a C++ namespace. */
d9fa45fe 16650
0114d602
DJ
16651static struct type *
16652read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16653{
e7c27a73 16654 struct objfile *objfile = cu->objfile;
0114d602 16655 const char *previous_prefix, *name;
9219021c 16656 int is_anonymous;
0114d602
DJ
16657 struct type *type;
16658
16659 /* For extensions, reuse the type of the original namespace. */
16660 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16661 {
16662 struct die_info *ext_die;
16663 struct dwarf2_cu *ext_cu = cu;
9a619af0 16664
0114d602
DJ
16665 ext_die = dwarf2_extension (die, &ext_cu);
16666 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16667
16668 /* EXT_CU may not be the same as CU.
02142a6c 16669 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16670 return set_die_type (die, type, cu);
16671 }
9219021c 16672
e142c38c 16673 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16674
16675 /* Now build the name of the current namespace. */
16676
0114d602
DJ
16677 previous_prefix = determine_prefix (die, cu);
16678 if (previous_prefix[0] != '\0')
16679 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16680 previous_prefix, name, 0, cu);
0114d602
DJ
16681
16682 /* Create the type. */
19f392bc 16683 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
16684 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16685
60531b24 16686 return set_die_type (die, type, cu);
0114d602
DJ
16687}
16688
22cee43f 16689/* Read a namespace scope. */
0114d602
DJ
16690
16691static void
16692read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16693{
16694 struct objfile *objfile = cu->objfile;
0114d602 16695 int is_anonymous;
9219021c 16696
5c4e30ca
DC
16697 /* Add a symbol associated to this if we haven't seen the namespace
16698 before. Also, add a using directive if it's an anonymous
16699 namespace. */
9219021c 16700
f2f0e013 16701 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16702 {
16703 struct type *type;
16704
0114d602 16705 type = read_type_die (die, cu);
e7c27a73 16706 new_symbol (die, type, cu);
5c4e30ca 16707
e8e80198 16708 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16709 if (is_anonymous)
0114d602
DJ
16710 {
16711 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16712
eb1e02fd 16713 std::vector<const char *> excludes;
22cee43f
PMR
16714 add_using_directive (using_directives (cu->language),
16715 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16716 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16717 }
5c4e30ca 16718 }
9219021c 16719
639d11d3 16720 if (die->child != NULL)
d9fa45fe 16721 {
639d11d3 16722 struct die_info *child_die = die->child;
6e70227d 16723
d9fa45fe
DC
16724 while (child_die && child_die->tag)
16725 {
e7c27a73 16726 process_die (child_die, cu);
d9fa45fe
DC
16727 child_die = sibling_die (child_die);
16728 }
16729 }
38d518c9
EZ
16730}
16731
f55ee35c
JK
16732/* Read a Fortran module as type. This DIE can be only a declaration used for
16733 imported module. Still we need that type as local Fortran "use ... only"
16734 declaration imports depend on the created type in determine_prefix. */
16735
16736static struct type *
16737read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16738{
16739 struct objfile *objfile = cu->objfile;
15d034d0 16740 const char *module_name;
f55ee35c
JK
16741 struct type *type;
16742
16743 module_name = dwarf2_name (die, cu);
16744 if (!module_name)
3e43a32a
MS
16745 complaint (&symfile_complaints,
16746 _("DW_TAG_module has no name, offset 0x%x"),
9c541725 16747 to_underlying (die->sect_off));
19f392bc 16748 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
16749
16750 /* determine_prefix uses TYPE_TAG_NAME. */
16751 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16752
16753 return set_die_type (die, type, cu);
16754}
16755
5d7cb8df
JK
16756/* Read a Fortran module. */
16757
16758static void
16759read_module (struct die_info *die, struct dwarf2_cu *cu)
16760{
16761 struct die_info *child_die = die->child;
530e8392
KB
16762 struct type *type;
16763
16764 type = read_type_die (die, cu);
16765 new_symbol (die, type, cu);
5d7cb8df 16766
5d7cb8df
JK
16767 while (child_die && child_die->tag)
16768 {
16769 process_die (child_die, cu);
16770 child_die = sibling_die (child_die);
16771 }
16772}
16773
38d518c9
EZ
16774/* Return the name of the namespace represented by DIE. Set
16775 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16776 namespace. */
16777
16778static const char *
e142c38c 16779namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16780{
16781 struct die_info *current_die;
16782 const char *name = NULL;
16783
16784 /* Loop through the extensions until we find a name. */
16785
16786 for (current_die = die;
16787 current_die != NULL;
f2f0e013 16788 current_die = dwarf2_extension (die, &cu))
38d518c9 16789 {
96553a0c
DE
16790 /* We don't use dwarf2_name here so that we can detect the absence
16791 of a name -> anonymous namespace. */
7d45c7c3 16792 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16793
38d518c9
EZ
16794 if (name != NULL)
16795 break;
16796 }
16797
16798 /* Is it an anonymous namespace? */
16799
16800 *is_anonymous = (name == NULL);
16801 if (*is_anonymous)
2b1dbab0 16802 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16803
16804 return name;
d9fa45fe
DC
16805}
16806
c906108c
SS
16807/* Extract all information from a DW_TAG_pointer_type DIE and add to
16808 the user defined type vector. */
16809
f792889a 16810static struct type *
e7c27a73 16811read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16812{
5e2b427d 16813 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 16814 struct comp_unit_head *cu_header = &cu->header;
c906108c 16815 struct type *type;
8b2dbe47
KB
16816 struct attribute *attr_byte_size;
16817 struct attribute *attr_address_class;
16818 int byte_size, addr_class;
7e314c57
JK
16819 struct type *target_type;
16820
16821 target_type = die_type (die, cu);
c906108c 16822
7e314c57
JK
16823 /* The die_type call above may have already set the type for this DIE. */
16824 type = get_die_type (die, cu);
16825 if (type)
16826 return type;
16827
16828 type = lookup_pointer_type (target_type);
8b2dbe47 16829
e142c38c 16830 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16831 if (attr_byte_size)
16832 byte_size = DW_UNSND (attr_byte_size);
c906108c 16833 else
8b2dbe47
KB
16834 byte_size = cu_header->addr_size;
16835
e142c38c 16836 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16837 if (attr_address_class)
16838 addr_class = DW_UNSND (attr_address_class);
16839 else
16840 addr_class = DW_ADDR_none;
16841
16842 /* If the pointer size or address class is different than the
16843 default, create a type variant marked as such and set the
16844 length accordingly. */
16845 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 16846 {
5e2b427d 16847 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16848 {
16849 int type_flags;
16850
849957d9 16851 type_flags = gdbarch_address_class_type_flags
5e2b427d 16852 (gdbarch, byte_size, addr_class);
876cecd0
TT
16853 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16854 == 0);
8b2dbe47
KB
16855 type = make_type_with_address_space (type, type_flags);
16856 }
16857 else if (TYPE_LENGTH (type) != byte_size)
16858 {
3e43a32a
MS
16859 complaint (&symfile_complaints,
16860 _("invalid pointer size %d"), byte_size);
8b2dbe47 16861 }
6e70227d 16862 else
9a619af0
MS
16863 {
16864 /* Should we also complain about unhandled address classes? */
16865 }
c906108c 16866 }
8b2dbe47
KB
16867
16868 TYPE_LENGTH (type) = byte_size;
f792889a 16869 return set_die_type (die, type, cu);
c906108c
SS
16870}
16871
16872/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16873 the user defined type vector. */
16874
f792889a 16875static struct type *
e7c27a73 16876read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16877{
16878 struct type *type;
16879 struct type *to_type;
16880 struct type *domain;
16881
e7c27a73
DJ
16882 to_type = die_type (die, cu);
16883 domain = die_containing_type (die, cu);
0d5de010 16884
7e314c57
JK
16885 /* The calls above may have already set the type for this DIE. */
16886 type = get_die_type (die, cu);
16887 if (type)
16888 return type;
16889
0d5de010
DJ
16890 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16891 type = lookup_methodptr_type (to_type);
7078baeb
TT
16892 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16893 {
16894 struct type *new_type = alloc_type (cu->objfile);
16895
16896 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16897 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16898 TYPE_VARARGS (to_type));
16899 type = lookup_methodptr_type (new_type);
16900 }
0d5de010
DJ
16901 else
16902 type = lookup_memberptr_type (to_type, domain);
c906108c 16903
f792889a 16904 return set_die_type (die, type, cu);
c906108c
SS
16905}
16906
4297a3f0 16907/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16908 the user defined type vector. */
16909
f792889a 16910static struct type *
4297a3f0
AV
16911read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16912 enum type_code refcode)
c906108c 16913{
e7c27a73 16914 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16915 struct type *type, *target_type;
c906108c
SS
16916 struct attribute *attr;
16917
4297a3f0
AV
16918 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16919
7e314c57
JK
16920 target_type = die_type (die, cu);
16921
16922 /* The die_type call above may have already set the type for this DIE. */
16923 type = get_die_type (die, cu);
16924 if (type)
16925 return type;
16926
4297a3f0 16927 type = lookup_reference_type (target_type, refcode);
e142c38c 16928 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16929 if (attr)
16930 {
16931 TYPE_LENGTH (type) = DW_UNSND (attr);
16932 }
16933 else
16934 {
107d2387 16935 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16936 }
f792889a 16937 return set_die_type (die, type, cu);
c906108c
SS
16938}
16939
cf363f18
MW
16940/* Add the given cv-qualifiers to the element type of the array. GCC
16941 outputs DWARF type qualifiers that apply to an array, not the
16942 element type. But GDB relies on the array element type to carry
16943 the cv-qualifiers. This mimics section 6.7.3 of the C99
16944 specification. */
16945
16946static struct type *
16947add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16948 struct type *base_type, int cnst, int voltl)
16949{
16950 struct type *el_type, *inner_array;
16951
16952 base_type = copy_type (base_type);
16953 inner_array = base_type;
16954
16955 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16956 {
16957 TYPE_TARGET_TYPE (inner_array) =
16958 copy_type (TYPE_TARGET_TYPE (inner_array));
16959 inner_array = TYPE_TARGET_TYPE (inner_array);
16960 }
16961
16962 el_type = TYPE_TARGET_TYPE (inner_array);
16963 cnst |= TYPE_CONST (el_type);
16964 voltl |= TYPE_VOLATILE (el_type);
16965 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16966
16967 return set_die_type (die, base_type, cu);
16968}
16969
f792889a 16970static struct type *
e7c27a73 16971read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16972{
f792889a 16973 struct type *base_type, *cv_type;
c906108c 16974
e7c27a73 16975 base_type = die_type (die, cu);
7e314c57
JK
16976
16977 /* The die_type call above may have already set the type for this DIE. */
16978 cv_type = get_die_type (die, cu);
16979 if (cv_type)
16980 return cv_type;
16981
2f608a3a
KW
16982 /* In case the const qualifier is applied to an array type, the element type
16983 is so qualified, not the array type (section 6.7.3 of C99). */
16984 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16985 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16986
f792889a
DJ
16987 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16988 return set_die_type (die, cv_type, cu);
c906108c
SS
16989}
16990
f792889a 16991static struct type *
e7c27a73 16992read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16993{
f792889a 16994 struct type *base_type, *cv_type;
c906108c 16995
e7c27a73 16996 base_type = die_type (die, cu);
7e314c57
JK
16997
16998 /* The die_type call above may have already set the type for this DIE. */
16999 cv_type = get_die_type (die, cu);
17000 if (cv_type)
17001 return cv_type;
17002
cf363f18
MW
17003 /* In case the volatile qualifier is applied to an array type, the
17004 element type is so qualified, not the array type (section 6.7.3
17005 of C99). */
17006 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17007 return add_array_cv_type (die, cu, base_type, 0, 1);
17008
f792889a
DJ
17009 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17010 return set_die_type (die, cv_type, cu);
c906108c
SS
17011}
17012
06d66ee9
TT
17013/* Handle DW_TAG_restrict_type. */
17014
17015static struct type *
17016read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17017{
17018 struct type *base_type, *cv_type;
17019
17020 base_type = die_type (die, cu);
17021
17022 /* The die_type call above may have already set the type for this DIE. */
17023 cv_type = get_die_type (die, cu);
17024 if (cv_type)
17025 return cv_type;
17026
17027 cv_type = make_restrict_type (base_type);
17028 return set_die_type (die, cv_type, cu);
17029}
17030
a2c2acaf
MW
17031/* Handle DW_TAG_atomic_type. */
17032
17033static struct type *
17034read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17035{
17036 struct type *base_type, *cv_type;
17037
17038 base_type = die_type (die, cu);
17039
17040 /* The die_type call above may have already set the type for this DIE. */
17041 cv_type = get_die_type (die, cu);
17042 if (cv_type)
17043 return cv_type;
17044
17045 cv_type = make_atomic_type (base_type);
17046 return set_die_type (die, cv_type, cu);
17047}
17048
c906108c
SS
17049/* Extract all information from a DW_TAG_string_type DIE and add to
17050 the user defined type vector. It isn't really a user defined type,
17051 but it behaves like one, with other DIE's using an AT_user_def_type
17052 attribute to reference it. */
17053
f792889a 17054static struct type *
e7c27a73 17055read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17056{
e7c27a73 17057 struct objfile *objfile = cu->objfile;
3b7538c0 17058 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17059 struct type *type, *range_type, *index_type, *char_type;
17060 struct attribute *attr;
17061 unsigned int length;
17062
e142c38c 17063 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17064 if (attr)
17065 {
17066 length = DW_UNSND (attr);
17067 }
17068 else
17069 {
0963b4bd 17070 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17071 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17072 if (attr)
17073 {
17074 length = DW_UNSND (attr);
17075 }
17076 else
17077 {
17078 length = 1;
17079 }
c906108c 17080 }
6ccb9162 17081
46bf5051 17082 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17083 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17084 char_type = language_string_char_type (cu->language_defn, gdbarch);
17085 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17086
f792889a 17087 return set_die_type (die, type, cu);
c906108c
SS
17088}
17089
4d804846
JB
17090/* Assuming that DIE corresponds to a function, returns nonzero
17091 if the function is prototyped. */
17092
17093static int
17094prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17095{
17096 struct attribute *attr;
17097
17098 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17099 if (attr && (DW_UNSND (attr) != 0))
17100 return 1;
17101
17102 /* The DWARF standard implies that the DW_AT_prototyped attribute
17103 is only meaninful for C, but the concept also extends to other
17104 languages that allow unprototyped functions (Eg: Objective C).
17105 For all other languages, assume that functions are always
17106 prototyped. */
17107 if (cu->language != language_c
17108 && cu->language != language_objc
17109 && cu->language != language_opencl)
17110 return 1;
17111
17112 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17113 prototyped and unprototyped functions; default to prototyped,
17114 since that is more common in modern code (and RealView warns
17115 about unprototyped functions). */
17116 if (producer_is_realview (cu->producer))
17117 return 1;
17118
17119 return 0;
17120}
17121
c906108c
SS
17122/* Handle DIES due to C code like:
17123
17124 struct foo
c5aa993b
JM
17125 {
17126 int (*funcp)(int a, long l);
17127 int b;
17128 };
c906108c 17129
0963b4bd 17130 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17131
f792889a 17132static struct type *
e7c27a73 17133read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17134{
bb5ed363 17135 struct objfile *objfile = cu->objfile;
0963b4bd
MS
17136 struct type *type; /* Type that this function returns. */
17137 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17138 struct attribute *attr;
17139
e7c27a73 17140 type = die_type (die, cu);
7e314c57
JK
17141
17142 /* The die_type call above may have already set the type for this DIE. */
17143 ftype = get_die_type (die, cu);
17144 if (ftype)
17145 return ftype;
17146
0c8b41f1 17147 ftype = lookup_function_type (type);
c906108c 17148
4d804846 17149 if (prototyped_function_p (die, cu))
a6c727b2 17150 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17151
c055b101
CV
17152 /* Store the calling convention in the type if it's available in
17153 the subroutine die. Otherwise set the calling convention to
17154 the default value DW_CC_normal. */
17155 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17156 if (attr)
17157 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17158 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17159 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17160 else
17161 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17162
743649fd
MW
17163 /* Record whether the function returns normally to its caller or not
17164 if the DWARF producer set that information. */
17165 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17166 if (attr && (DW_UNSND (attr) != 0))
17167 TYPE_NO_RETURN (ftype) = 1;
17168
76c10ea2
GM
17169 /* We need to add the subroutine type to the die immediately so
17170 we don't infinitely recurse when dealing with parameters
0963b4bd 17171 declared as the same subroutine type. */
76c10ea2 17172 set_die_type (die, ftype, cu);
6e70227d 17173
639d11d3 17174 if (die->child != NULL)
c906108c 17175 {
bb5ed363 17176 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17177 struct die_info *child_die;
8072405b 17178 int nparams, iparams;
c906108c
SS
17179
17180 /* Count the number of parameters.
17181 FIXME: GDB currently ignores vararg functions, but knows about
17182 vararg member functions. */
8072405b 17183 nparams = 0;
639d11d3 17184 child_die = die->child;
c906108c
SS
17185 while (child_die && child_die->tag)
17186 {
17187 if (child_die->tag == DW_TAG_formal_parameter)
17188 nparams++;
17189 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17190 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17191 child_die = sibling_die (child_die);
17192 }
17193
17194 /* Allocate storage for parameters and fill them in. */
17195 TYPE_NFIELDS (ftype) = nparams;
17196 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17197 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17198
8072405b
JK
17199 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17200 even if we error out during the parameters reading below. */
17201 for (iparams = 0; iparams < nparams; iparams++)
17202 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17203
17204 iparams = 0;
639d11d3 17205 child_die = die->child;
c906108c
SS
17206 while (child_die && child_die->tag)
17207 {
17208 if (child_die->tag == DW_TAG_formal_parameter)
17209 {
3ce3b1ba
PA
17210 struct type *arg_type;
17211
17212 /* DWARF version 2 has no clean way to discern C++
17213 static and non-static member functions. G++ helps
17214 GDB by marking the first parameter for non-static
17215 member functions (which is the this pointer) as
17216 artificial. We pass this information to
17217 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17218
17219 DWARF version 3 added DW_AT_object_pointer, which GCC
17220 4.5 does not yet generate. */
e142c38c 17221 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17222 if (attr)
17223 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17224 else
9c37b5ae 17225 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17226 arg_type = die_type (child_die, cu);
17227
17228 /* RealView does not mark THIS as const, which the testsuite
17229 expects. GCC marks THIS as const in method definitions,
17230 but not in the class specifications (GCC PR 43053). */
17231 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17232 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17233 {
17234 int is_this = 0;
17235 struct dwarf2_cu *arg_cu = cu;
17236 const char *name = dwarf2_name (child_die, cu);
17237
17238 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17239 if (attr)
17240 {
17241 /* If the compiler emits this, use it. */
17242 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17243 is_this = 1;
17244 }
17245 else if (name && strcmp (name, "this") == 0)
17246 /* Function definitions will have the argument names. */
17247 is_this = 1;
17248 else if (name == NULL && iparams == 0)
17249 /* Declarations may not have the names, so like
17250 elsewhere in GDB, assume an artificial first
17251 argument is "this". */
17252 is_this = 1;
17253
17254 if (is_this)
17255 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17256 arg_type, 0);
17257 }
17258
17259 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17260 iparams++;
17261 }
17262 child_die = sibling_die (child_die);
17263 }
17264 }
17265
76c10ea2 17266 return ftype;
c906108c
SS
17267}
17268
f792889a 17269static struct type *
e7c27a73 17270read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17271{
e7c27a73 17272 struct objfile *objfile = cu->objfile;
0114d602 17273 const char *name = NULL;
3c8e0968 17274 struct type *this_type, *target_type;
c906108c 17275
94af9270 17276 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17277 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17278 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17279 set_die_type (die, this_type, cu);
3c8e0968
DE
17280 target_type = die_type (die, cu);
17281 if (target_type != this_type)
17282 TYPE_TARGET_TYPE (this_type) = target_type;
17283 else
17284 {
17285 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17286 spec and cause infinite loops in GDB. */
17287 complaint (&symfile_complaints,
17288 _("Self-referential DW_TAG_typedef "
17289 "- DIE at 0x%x [in module %s]"),
9c541725 17290 to_underlying (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17291 TYPE_TARGET_TYPE (this_type) = NULL;
17292 }
f792889a 17293 return this_type;
c906108c
SS
17294}
17295
9b790ce7
UW
17296/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17297 (which may be different from NAME) to the architecture back-end to allow
17298 it to guess the correct format if necessary. */
17299
17300static struct type *
17301dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17302 const char *name_hint)
17303{
17304 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17305 const struct floatformat **format;
17306 struct type *type;
17307
17308 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17309 if (format)
17310 type = init_float_type (objfile, bits, name, format);
17311 else
77b7c781 17312 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17313
17314 return type;
17315}
17316
c906108c
SS
17317/* Find a representation of a given base type and install
17318 it in the TYPE field of the die. */
17319
f792889a 17320static struct type *
e7c27a73 17321read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17322{
e7c27a73 17323 struct objfile *objfile = cu->objfile;
c906108c
SS
17324 struct type *type;
17325 struct attribute *attr;
19f392bc 17326 int encoding = 0, bits = 0;
15d034d0 17327 const char *name;
c906108c 17328
e142c38c 17329 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17330 if (attr)
17331 {
17332 encoding = DW_UNSND (attr);
17333 }
e142c38c 17334 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17335 if (attr)
17336 {
19f392bc 17337 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17338 }
39cbfefa 17339 name = dwarf2_name (die, cu);
6ccb9162 17340 if (!name)
c906108c 17341 {
6ccb9162
UW
17342 complaint (&symfile_complaints,
17343 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17344 }
6ccb9162
UW
17345
17346 switch (encoding)
c906108c 17347 {
6ccb9162
UW
17348 case DW_ATE_address:
17349 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17350 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17351 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17352 break;
17353 case DW_ATE_boolean:
19f392bc 17354 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17355 break;
17356 case DW_ATE_complex_float:
9b790ce7 17357 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 17358 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17359 break;
17360 case DW_ATE_decimal_float:
19f392bc 17361 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17362 break;
17363 case DW_ATE_float:
9b790ce7 17364 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17365 break;
17366 case DW_ATE_signed:
19f392bc 17367 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17368 break;
17369 case DW_ATE_unsigned:
3b2b8fea
TT
17370 if (cu->language == language_fortran
17371 && name
61012eef 17372 && startswith (name, "character("))
19f392bc
UW
17373 type = init_character_type (objfile, bits, 1, name);
17374 else
17375 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
17376 break;
17377 case DW_ATE_signed_char:
6e70227d 17378 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17379 || cu->language == language_pascal
17380 || cu->language == language_fortran)
19f392bc
UW
17381 type = init_character_type (objfile, bits, 0, name);
17382 else
17383 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17384 break;
17385 case DW_ATE_unsigned_char:
868a0084 17386 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17387 || cu->language == language_pascal
c44af4eb
TT
17388 || cu->language == language_fortran
17389 || cu->language == language_rust)
19f392bc
UW
17390 type = init_character_type (objfile, bits, 1, name);
17391 else
17392 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 17393 break;
75079b2b 17394 case DW_ATE_UTF:
53e710ac
PA
17395 {
17396 gdbarch *arch = get_objfile_arch (objfile);
17397
17398 if (bits == 16)
17399 type = builtin_type (arch)->builtin_char16;
17400 else if (bits == 32)
17401 type = builtin_type (arch)->builtin_char32;
17402 else
17403 {
17404 complaint (&symfile_complaints,
17405 _("unsupported DW_ATE_UTF bit size: '%d'"),
17406 bits);
17407 type = init_integer_type (objfile, bits, 1, name);
17408 }
17409 return set_die_type (die, type, cu);
17410 }
75079b2b
TT
17411 break;
17412
6ccb9162
UW
17413 default:
17414 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17415 dwarf_type_encoding_name (encoding));
77b7c781 17416 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17417 break;
c906108c 17418 }
6ccb9162 17419
0114d602 17420 if (name && strcmp (name, "char") == 0)
876cecd0 17421 TYPE_NOSIGN (type) = 1;
0114d602 17422
f792889a 17423 return set_die_type (die, type, cu);
c906108c
SS
17424}
17425
80180f79
SA
17426/* Parse dwarf attribute if it's a block, reference or constant and put the
17427 resulting value of the attribute into struct bound_prop.
17428 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17429
17430static int
17431attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17432 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17433{
17434 struct dwarf2_property_baton *baton;
17435 struct obstack *obstack = &cu->objfile->objfile_obstack;
17436
17437 if (attr == NULL || prop == NULL)
17438 return 0;
17439
17440 if (attr_form_is_block (attr))
17441 {
8d749320 17442 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
17443 baton->referenced_type = NULL;
17444 baton->locexpr.per_cu = cu->per_cu;
17445 baton->locexpr.size = DW_BLOCK (attr)->size;
17446 baton->locexpr.data = DW_BLOCK (attr)->data;
17447 prop->data.baton = baton;
17448 prop->kind = PROP_LOCEXPR;
17449 gdb_assert (prop->data.baton != NULL);
17450 }
17451 else if (attr_form_is_ref (attr))
17452 {
17453 struct dwarf2_cu *target_cu = cu;
17454 struct die_info *target_die;
17455 struct attribute *target_attr;
17456
17457 target_die = follow_die_ref (die, attr, &target_cu);
17458 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17459 if (target_attr == NULL)
17460 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17461 target_cu);
80180f79
SA
17462 if (target_attr == NULL)
17463 return 0;
17464
df25ebbd 17465 switch (target_attr->name)
80180f79 17466 {
df25ebbd
JB
17467 case DW_AT_location:
17468 if (attr_form_is_section_offset (target_attr))
17469 {
8d749320 17470 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17471 baton->referenced_type = die_type (target_die, target_cu);
17472 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17473 prop->data.baton = baton;
17474 prop->kind = PROP_LOCLIST;
17475 gdb_assert (prop->data.baton != NULL);
17476 }
17477 else if (attr_form_is_block (target_attr))
17478 {
8d749320 17479 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17480 baton->referenced_type = die_type (target_die, target_cu);
17481 baton->locexpr.per_cu = cu->per_cu;
17482 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17483 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17484 prop->data.baton = baton;
17485 prop->kind = PROP_LOCEXPR;
17486 gdb_assert (prop->data.baton != NULL);
17487 }
17488 else
17489 {
17490 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17491 "dynamic property");
17492 return 0;
17493 }
17494 break;
17495 case DW_AT_data_member_location:
17496 {
17497 LONGEST offset;
17498
17499 if (!handle_data_member_location (target_die, target_cu,
17500 &offset))
17501 return 0;
17502
8d749320 17503 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
17504 baton->referenced_type = read_type_die (target_die->parent,
17505 target_cu);
df25ebbd
JB
17506 baton->offset_info.offset = offset;
17507 baton->offset_info.type = die_type (target_die, target_cu);
17508 prop->data.baton = baton;
17509 prop->kind = PROP_ADDR_OFFSET;
17510 break;
17511 }
80180f79
SA
17512 }
17513 }
17514 else if (attr_form_is_constant (attr))
17515 {
17516 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17517 prop->kind = PROP_CONST;
17518 }
17519 else
17520 {
17521 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17522 dwarf2_name (die, cu));
17523 return 0;
17524 }
17525
17526 return 1;
17527}
17528
a02abb62
JB
17529/* Read the given DW_AT_subrange DIE. */
17530
f792889a 17531static struct type *
a02abb62
JB
17532read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17533{
4c9ad8c2 17534 struct type *base_type, *orig_base_type;
a02abb62
JB
17535 struct type *range_type;
17536 struct attribute *attr;
729efb13 17537 struct dynamic_prop low, high;
4fae6e18 17538 int low_default_is_valid;
c451ebe5 17539 int high_bound_is_count = 0;
15d034d0 17540 const char *name;
43bbcdc2 17541 LONGEST negative_mask;
e77813c8 17542
4c9ad8c2
TT
17543 orig_base_type = die_type (die, cu);
17544 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17545 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17546 creating the range type, but we use the result of check_typedef
17547 when examining properties of the type. */
17548 base_type = check_typedef (orig_base_type);
a02abb62 17549
7e314c57
JK
17550 /* The die_type call above may have already set the type for this DIE. */
17551 range_type = get_die_type (die, cu);
17552 if (range_type)
17553 return range_type;
17554
729efb13
SA
17555 low.kind = PROP_CONST;
17556 high.kind = PROP_CONST;
17557 high.data.const_val = 0;
17558
4fae6e18
JK
17559 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17560 omitting DW_AT_lower_bound. */
17561 switch (cu->language)
6e70227d 17562 {
4fae6e18
JK
17563 case language_c:
17564 case language_cplus:
729efb13 17565 low.data.const_val = 0;
4fae6e18
JK
17566 low_default_is_valid = 1;
17567 break;
17568 case language_fortran:
729efb13 17569 low.data.const_val = 1;
4fae6e18
JK
17570 low_default_is_valid = 1;
17571 break;
17572 case language_d:
4fae6e18 17573 case language_objc:
c44af4eb 17574 case language_rust:
729efb13 17575 low.data.const_val = 0;
4fae6e18
JK
17576 low_default_is_valid = (cu->header.version >= 4);
17577 break;
17578 case language_ada:
17579 case language_m2:
17580 case language_pascal:
729efb13 17581 low.data.const_val = 1;
4fae6e18
JK
17582 low_default_is_valid = (cu->header.version >= 4);
17583 break;
17584 default:
729efb13 17585 low.data.const_val = 0;
4fae6e18
JK
17586 low_default_is_valid = 0;
17587 break;
a02abb62
JB
17588 }
17589
e142c38c 17590 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 17591 if (attr)
11c1ba78 17592 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
17593 else if (!low_default_is_valid)
17594 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17595 "- DIE at 0x%x [in module %s]"),
9c541725 17596 to_underlying (die->sect_off), objfile_name (cu->objfile));
a02abb62 17597
e142c38c 17598 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 17599 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
17600 {
17601 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 17602 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 17603 {
c451ebe5
SA
17604 /* If bounds are constant do the final calculation here. */
17605 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17606 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17607 else
17608 high_bound_is_count = 1;
c2ff108b 17609 }
e77813c8
PM
17610 }
17611
17612 /* Dwarf-2 specifications explicitly allows to create subrange types
17613 without specifying a base type.
17614 In that case, the base type must be set to the type of
17615 the lower bound, upper bound or count, in that order, if any of these
17616 three attributes references an object that has a type.
17617 If no base type is found, the Dwarf-2 specifications say that
17618 a signed integer type of size equal to the size of an address should
17619 be used.
17620 For the following C code: `extern char gdb_int [];'
17621 GCC produces an empty range DIE.
17622 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 17623 high bound or count are not yet handled by this code. */
e77813c8
PM
17624 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17625 {
17626 struct objfile *objfile = cu->objfile;
17627 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17628 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17629 struct type *int_type = objfile_type (objfile)->builtin_int;
17630
17631 /* Test "int", "long int", and "long long int" objfile types,
17632 and select the first one having a size above or equal to the
17633 architecture address size. */
17634 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17635 base_type = int_type;
17636 else
17637 {
17638 int_type = objfile_type (objfile)->builtin_long;
17639 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17640 base_type = int_type;
17641 else
17642 {
17643 int_type = objfile_type (objfile)->builtin_long_long;
17644 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17645 base_type = int_type;
17646 }
17647 }
17648 }
a02abb62 17649
dbb9c2b1
JB
17650 /* Normally, the DWARF producers are expected to use a signed
17651 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17652 But this is unfortunately not always the case, as witnessed
17653 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17654 is used instead. To work around that ambiguity, we treat
17655 the bounds as signed, and thus sign-extend their values, when
17656 the base type is signed. */
6e70227d 17657 negative_mask =
66c6502d 17658 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17659 if (low.kind == PROP_CONST
17660 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17661 low.data.const_val |= negative_mask;
17662 if (high.kind == PROP_CONST
17663 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17664 high.data.const_val |= negative_mask;
43bbcdc2 17665
729efb13 17666 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 17667
c451ebe5
SA
17668 if (high_bound_is_count)
17669 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17670
c2ff108b
JK
17671 /* Ada expects an empty array on no boundary attributes. */
17672 if (attr == NULL && cu->language != language_ada)
729efb13 17673 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17674
39cbfefa
DJ
17675 name = dwarf2_name (die, cu);
17676 if (name)
17677 TYPE_NAME (range_type) = name;
6e70227d 17678
e142c38c 17679 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
17680 if (attr)
17681 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17682
7e314c57
JK
17683 set_die_type (die, range_type, cu);
17684
17685 /* set_die_type should be already done. */
b4ba55a1
JB
17686 set_descriptive_type (range_type, die, cu);
17687
7e314c57 17688 return range_type;
a02abb62 17689}
6e70227d 17690
f792889a 17691static struct type *
81a17f79
JB
17692read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17693{
17694 struct type *type;
81a17f79 17695
19f392bc 17696 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 17697 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17698
74a2f8ff
JB
17699 /* In Ada, an unspecified type is typically used when the description
17700 of the type is defered to a different unit. When encountering
17701 such a type, we treat it as a stub, and try to resolve it later on,
17702 when needed. */
17703 if (cu->language == language_ada)
17704 TYPE_STUB (type) = 1;
17705
f792889a 17706 return set_die_type (die, type, cu);
81a17f79 17707}
a02abb62 17708
639d11d3
DC
17709/* Read a single die and all its descendents. Set the die's sibling
17710 field to NULL; set other fields in the die correctly, and set all
17711 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17712 location of the info_ptr after reading all of those dies. PARENT
17713 is the parent of the die in question. */
17714
17715static struct die_info *
dee91e82 17716read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17717 const gdb_byte *info_ptr,
17718 const gdb_byte **new_info_ptr,
dee91e82 17719 struct die_info *parent)
639d11d3
DC
17720{
17721 struct die_info *die;
d521ce57 17722 const gdb_byte *cur_ptr;
639d11d3
DC
17723 int has_children;
17724
bf6af496 17725 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
17726 if (die == NULL)
17727 {
17728 *new_info_ptr = cur_ptr;
17729 return NULL;
17730 }
93311388 17731 store_in_ref_table (die, reader->cu);
639d11d3
DC
17732
17733 if (has_children)
bf6af496 17734 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17735 else
17736 {
17737 die->child = NULL;
17738 *new_info_ptr = cur_ptr;
17739 }
17740
17741 die->sibling = NULL;
17742 die->parent = parent;
17743 return die;
17744}
17745
17746/* Read a die, all of its descendents, and all of its siblings; set
17747 all of the fields of all of the dies correctly. Arguments are as
17748 in read_die_and_children. */
17749
17750static struct die_info *
bf6af496 17751read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17752 const gdb_byte *info_ptr,
17753 const gdb_byte **new_info_ptr,
bf6af496 17754 struct die_info *parent)
639d11d3
DC
17755{
17756 struct die_info *first_die, *last_sibling;
d521ce57 17757 const gdb_byte *cur_ptr;
639d11d3 17758
c906108c 17759 cur_ptr = info_ptr;
639d11d3
DC
17760 first_die = last_sibling = NULL;
17761
17762 while (1)
c906108c 17763 {
639d11d3 17764 struct die_info *die
dee91e82 17765 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17766
1d325ec1 17767 if (die == NULL)
c906108c 17768 {
639d11d3
DC
17769 *new_info_ptr = cur_ptr;
17770 return first_die;
c906108c 17771 }
1d325ec1
DJ
17772
17773 if (!first_die)
17774 first_die = die;
c906108c 17775 else
1d325ec1
DJ
17776 last_sibling->sibling = die;
17777
17778 last_sibling = die;
c906108c 17779 }
c906108c
SS
17780}
17781
bf6af496
DE
17782/* Read a die, all of its descendents, and all of its siblings; set
17783 all of the fields of all of the dies correctly. Arguments are as
17784 in read_die_and_children.
17785 This the main entry point for reading a DIE and all its children. */
17786
17787static struct die_info *
17788read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17789 const gdb_byte *info_ptr,
17790 const gdb_byte **new_info_ptr,
bf6af496
DE
17791 struct die_info *parent)
17792{
17793 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17794 new_info_ptr, parent);
17795
b4f54984 17796 if (dwarf_die_debug)
bf6af496
DE
17797 {
17798 fprintf_unfiltered (gdb_stdlog,
17799 "Read die from %s@0x%x of %s:\n",
a32a8923 17800 get_section_name (reader->die_section),
bf6af496
DE
17801 (unsigned) (info_ptr - reader->die_section->buffer),
17802 bfd_get_filename (reader->abfd));
b4f54984 17803 dump_die (die, dwarf_die_debug);
bf6af496
DE
17804 }
17805
17806 return die;
17807}
17808
3019eac3
DE
17809/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17810 attributes.
17811 The caller is responsible for filling in the extra attributes
17812 and updating (*DIEP)->num_attrs.
17813 Set DIEP to point to a newly allocated die with its information,
17814 except for its child, sibling, and parent fields.
17815 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 17816
d521ce57 17817static const gdb_byte *
3019eac3 17818read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17819 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 17820 int *has_children, int num_extra_attrs)
93311388 17821{
b64f50a1 17822 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17823 struct abbrev_info *abbrev;
17824 struct die_info *die;
17825 struct dwarf2_cu *cu = reader->cu;
17826 bfd *abfd = reader->abfd;
17827
9c541725 17828 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17829 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17830 info_ptr += bytes_read;
17831 if (!abbrev_number)
17832 {
17833 *diep = NULL;
17834 *has_children = 0;
17835 return info_ptr;
17836 }
17837
433df2d4 17838 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 17839 if (!abbrev)
348e048f
DE
17840 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17841 abbrev_number,
17842 bfd_get_filename (abfd));
17843
3019eac3 17844 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17845 die->sect_off = sect_off;
93311388
DE
17846 die->tag = abbrev->tag;
17847 die->abbrev = abbrev_number;
17848
3019eac3
DE
17849 /* Make the result usable.
17850 The caller needs to update num_attrs after adding the extra
17851 attributes. */
93311388
DE
17852 die->num_attrs = abbrev->num_attrs;
17853
17854 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
17855 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17856 info_ptr);
93311388
DE
17857
17858 *diep = die;
17859 *has_children = abbrev->has_children;
17860 return info_ptr;
17861}
17862
3019eac3
DE
17863/* Read a die and all its attributes.
17864 Set DIEP to point to a newly allocated die with its information,
17865 except for its child, sibling, and parent fields.
17866 Set HAS_CHILDREN to tell whether the die has children or not. */
17867
d521ce57 17868static const gdb_byte *
3019eac3 17869read_full_die (const struct die_reader_specs *reader,
d521ce57 17870 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
17871 int *has_children)
17872{
d521ce57 17873 const gdb_byte *result;
bf6af496
DE
17874
17875 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17876
b4f54984 17877 if (dwarf_die_debug)
bf6af496
DE
17878 {
17879 fprintf_unfiltered (gdb_stdlog,
17880 "Read die from %s@0x%x of %s:\n",
a32a8923 17881 get_section_name (reader->die_section),
bf6af496
DE
17882 (unsigned) (info_ptr - reader->die_section->buffer),
17883 bfd_get_filename (reader->abfd));
b4f54984 17884 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17885 }
17886
17887 return result;
3019eac3 17888}
433df2d4
DE
17889\f
17890/* Abbreviation tables.
3019eac3 17891
433df2d4 17892 In DWARF version 2, the description of the debugging information is
c906108c
SS
17893 stored in a separate .debug_abbrev section. Before we read any
17894 dies from a section we read in all abbreviations and install them
433df2d4
DE
17895 in a hash table. */
17896
17897/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17898
17899static struct abbrev_info *
17900abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
17901{
17902 struct abbrev_info *abbrev;
17903
8d749320 17904 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 17905 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 17906
433df2d4
DE
17907 return abbrev;
17908}
17909
17910/* Add an abbreviation to the table. */
c906108c
SS
17911
17912static void
433df2d4
DE
17913abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
17914 unsigned int abbrev_number,
17915 struct abbrev_info *abbrev)
17916{
17917 unsigned int hash_number;
17918
17919 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17920 abbrev->next = abbrev_table->abbrevs[hash_number];
17921 abbrev_table->abbrevs[hash_number] = abbrev;
17922}
dee91e82 17923
433df2d4
DE
17924/* Look up an abbrev in the table.
17925 Returns NULL if the abbrev is not found. */
17926
17927static struct abbrev_info *
17928abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
17929 unsigned int abbrev_number)
c906108c 17930{
433df2d4
DE
17931 unsigned int hash_number;
17932 struct abbrev_info *abbrev;
17933
17934 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17935 abbrev = abbrev_table->abbrevs[hash_number];
17936
17937 while (abbrev)
17938 {
17939 if (abbrev->number == abbrev_number)
17940 return abbrev;
17941 abbrev = abbrev->next;
17942 }
17943 return NULL;
17944}
17945
17946/* Read in an abbrev table. */
17947
17948static struct abbrev_table *
17949abbrev_table_read_table (struct dwarf2_section_info *section,
9c541725 17950 sect_offset sect_off)
433df2d4
DE
17951{
17952 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 17953 bfd *abfd = get_section_bfd_owner (section);
433df2d4 17954 struct abbrev_table *abbrev_table;
d521ce57 17955 const gdb_byte *abbrev_ptr;
c906108c
SS
17956 struct abbrev_info *cur_abbrev;
17957 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 17958 unsigned int abbrev_form;
f3dd6933
DJ
17959 struct attr_abbrev *cur_attrs;
17960 unsigned int allocated_attrs;
c906108c 17961
70ba0933 17962 abbrev_table = XNEW (struct abbrev_table);
9c541725 17963 abbrev_table->sect_off = sect_off;
433df2d4 17964 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
17965 abbrev_table->abbrevs =
17966 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
17967 ABBREV_HASH_SIZE);
433df2d4
DE
17968 memset (abbrev_table->abbrevs, 0,
17969 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 17970
433df2d4 17971 dwarf2_read_section (objfile, section);
9c541725 17972 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
17973 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17974 abbrev_ptr += bytes_read;
17975
f3dd6933 17976 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 17977 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 17978
0963b4bd 17979 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
17980 while (abbrev_number)
17981 {
433df2d4 17982 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
17983
17984 /* read in abbrev header */
17985 cur_abbrev->number = abbrev_number;
aead7601
SM
17986 cur_abbrev->tag
17987 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
17988 abbrev_ptr += bytes_read;
17989 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
17990 abbrev_ptr += 1;
17991
17992 /* now read in declarations */
22d2f3ab 17993 for (;;)
c906108c 17994 {
43988095
JK
17995 LONGEST implicit_const;
17996
22d2f3ab
JK
17997 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17998 abbrev_ptr += bytes_read;
17999 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18000 abbrev_ptr += bytes_read;
43988095
JK
18001 if (abbrev_form == DW_FORM_implicit_const)
18002 {
18003 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18004 &bytes_read);
18005 abbrev_ptr += bytes_read;
18006 }
18007 else
18008 {
18009 /* Initialize it due to a false compiler warning. */
18010 implicit_const = -1;
18011 }
22d2f3ab
JK
18012
18013 if (abbrev_name == 0)
18014 break;
18015
f3dd6933 18016 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18017 {
f3dd6933
DJ
18018 allocated_attrs += ATTR_ALLOC_CHUNK;
18019 cur_attrs
224c3ddb 18020 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18021 }
ae038cb0 18022
aead7601
SM
18023 cur_attrs[cur_abbrev->num_attrs].name
18024 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18025 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18026 = (enum dwarf_form) abbrev_form;
43988095 18027 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18028 ++cur_abbrev->num_attrs;
c906108c
SS
18029 }
18030
8d749320
SM
18031 cur_abbrev->attrs =
18032 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18033 cur_abbrev->num_attrs);
f3dd6933
DJ
18034 memcpy (cur_abbrev->attrs, cur_attrs,
18035 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18036
433df2d4 18037 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
18038
18039 /* Get next abbreviation.
18040 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18041 always properly terminated with an abbrev number of 0.
18042 Exit loop if we encounter an abbreviation which we have
18043 already read (which means we are about to read the abbreviations
18044 for the next compile unit) or if the end of the abbreviation
18045 table is reached. */
433df2d4 18046 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18047 break;
18048 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18049 abbrev_ptr += bytes_read;
433df2d4 18050 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
18051 break;
18052 }
f3dd6933
DJ
18053
18054 xfree (cur_attrs);
433df2d4 18055 return abbrev_table;
c906108c
SS
18056}
18057
433df2d4 18058/* Free the resources held by ABBREV_TABLE. */
c906108c 18059
c906108c 18060static void
433df2d4 18061abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 18062{
433df2d4
DE
18063 obstack_free (&abbrev_table->abbrev_obstack, NULL);
18064 xfree (abbrev_table);
c906108c
SS
18065}
18066
f4dc4d17
DE
18067/* Same as abbrev_table_free but as a cleanup.
18068 We pass in a pointer to the pointer to the table so that we can
18069 set the pointer to NULL when we're done. It also simplifies
73051182 18070 build_type_psymtabs_1. */
f4dc4d17
DE
18071
18072static void
18073abbrev_table_free_cleanup (void *table_ptr)
18074{
9a3c8263 18075 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
18076
18077 if (*abbrev_table_ptr != NULL)
18078 abbrev_table_free (*abbrev_table_ptr);
18079 *abbrev_table_ptr = NULL;
18080}
18081
433df2d4
DE
18082/* Read the abbrev table for CU from ABBREV_SECTION. */
18083
18084static void
18085dwarf2_read_abbrevs (struct dwarf2_cu *cu,
18086 struct dwarf2_section_info *abbrev_section)
c906108c 18087{
433df2d4 18088 cu->abbrev_table =
9c541725 18089 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
433df2d4 18090}
c906108c 18091
433df2d4 18092/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 18093
433df2d4
DE
18094static void
18095dwarf2_free_abbrev_table (void *ptr_to_cu)
18096{
9a3c8263 18097 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 18098
a2ce51a0
DE
18099 if (cu->abbrev_table != NULL)
18100 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
18101 /* Set this to NULL so that we SEGV if we try to read it later,
18102 and also because free_comp_unit verifies this is NULL. */
18103 cu->abbrev_table = NULL;
18104}
18105\f
72bf9492
DJ
18106/* Returns nonzero if TAG represents a type that we might generate a partial
18107 symbol for. */
18108
18109static int
18110is_type_tag_for_partial (int tag)
18111{
18112 switch (tag)
18113 {
18114#if 0
18115 /* Some types that would be reasonable to generate partial symbols for,
18116 that we don't at present. */
18117 case DW_TAG_array_type:
18118 case DW_TAG_file_type:
18119 case DW_TAG_ptr_to_member_type:
18120 case DW_TAG_set_type:
18121 case DW_TAG_string_type:
18122 case DW_TAG_subroutine_type:
18123#endif
18124 case DW_TAG_base_type:
18125 case DW_TAG_class_type:
680b30c7 18126 case DW_TAG_interface_type:
72bf9492
DJ
18127 case DW_TAG_enumeration_type:
18128 case DW_TAG_structure_type:
18129 case DW_TAG_subrange_type:
18130 case DW_TAG_typedef:
18131 case DW_TAG_union_type:
18132 return 1;
18133 default:
18134 return 0;
18135 }
18136}
18137
18138/* Load all DIEs that are interesting for partial symbols into memory. */
18139
18140static struct partial_die_info *
dee91e82 18141load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18142 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18143{
dee91e82 18144 struct dwarf2_cu *cu = reader->cu;
bb5ed363 18145 struct objfile *objfile = cu->objfile;
72bf9492
DJ
18146 struct partial_die_info *part_die;
18147 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18148 struct abbrev_info *abbrev;
18149 unsigned int bytes_read;
5afb4e99 18150 unsigned int load_all = 0;
72bf9492
DJ
18151 int nesting_level = 1;
18152
18153 parent_die = NULL;
18154 last_die = NULL;
18155
7adf1e79
DE
18156 gdb_assert (cu->per_cu != NULL);
18157 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18158 load_all = 1;
18159
72bf9492
DJ
18160 cu->partial_dies
18161 = htab_create_alloc_ex (cu->header.length / 12,
18162 partial_die_hash,
18163 partial_die_eq,
18164 NULL,
18165 &cu->comp_unit_obstack,
18166 hashtab_obstack_allocate,
18167 dummy_obstack_deallocate);
18168
8d749320 18169 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
18170
18171 while (1)
18172 {
18173 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
18174
18175 /* A NULL abbrev means the end of a series of children. */
18176 if (abbrev == NULL)
18177 {
18178 if (--nesting_level == 0)
18179 {
18180 /* PART_DIE was probably the last thing allocated on the
18181 comp_unit_obstack, so we could call obstack_free
18182 here. We don't do that because the waste is small,
18183 and will be cleaned up when we're done with this
18184 compilation unit. This way, we're also more robust
18185 against other users of the comp_unit_obstack. */
18186 return first_die;
18187 }
18188 info_ptr += bytes_read;
18189 last_die = parent_die;
18190 parent_die = parent_die->die_parent;
18191 continue;
18192 }
18193
98bfdba5
PA
18194 /* Check for template arguments. We never save these; if
18195 they're seen, we just mark the parent, and go on our way. */
18196 if (parent_die != NULL
18197 && cu->language == language_cplus
18198 && (abbrev->tag == DW_TAG_template_type_param
18199 || abbrev->tag == DW_TAG_template_value_param))
18200 {
18201 parent_die->has_template_arguments = 1;
18202
18203 if (!load_all)
18204 {
18205 /* We don't need a partial DIE for the template argument. */
dee91e82 18206 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18207 continue;
18208 }
18209 }
18210
0d99eb77 18211 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18212 Skip their other children. */
18213 if (!load_all
18214 && cu->language == language_cplus
18215 && parent_die != NULL
18216 && parent_die->tag == DW_TAG_subprogram)
18217 {
dee91e82 18218 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18219 continue;
18220 }
18221
5afb4e99
DJ
18222 /* Check whether this DIE is interesting enough to save. Normally
18223 we would not be interested in members here, but there may be
18224 later variables referencing them via DW_AT_specification (for
18225 static members). */
18226 if (!load_all
18227 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18228 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18229 && abbrev->tag != DW_TAG_enumerator
18230 && abbrev->tag != DW_TAG_subprogram
bc30ff58 18231 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18232 && abbrev->tag != DW_TAG_variable
5afb4e99 18233 && abbrev->tag != DW_TAG_namespace
f55ee35c 18234 && abbrev->tag != DW_TAG_module
95554aad 18235 && abbrev->tag != DW_TAG_member
74921315
KS
18236 && abbrev->tag != DW_TAG_imported_unit
18237 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18238 {
18239 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18240 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18241 continue;
18242 }
18243
dee91e82
DE
18244 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
18245 info_ptr);
72bf9492
DJ
18246
18247 /* This two-pass algorithm for processing partial symbols has a
18248 high cost in cache pressure. Thus, handle some simple cases
18249 here which cover the majority of C partial symbols. DIEs
18250 which neither have specification tags in them, nor could have
18251 specification tags elsewhere pointing at them, can simply be
18252 processed and discarded.
18253
18254 This segment is also optional; scan_partial_symbols and
18255 add_partial_symbol will handle these DIEs if we chain
18256 them in normally. When compilers which do not emit large
18257 quantities of duplicate debug information are more common,
18258 this code can probably be removed. */
18259
18260 /* Any complete simple types at the top level (pretty much all
18261 of them, for a language without namespaces), can be processed
18262 directly. */
18263 if (parent_die == NULL
18264 && part_die->has_specification == 0
18265 && part_die->is_declaration == 0
d8228535 18266 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
18267 || part_die->tag == DW_TAG_base_type
18268 || part_die->tag == DW_TAG_subrange_type))
18269 {
18270 if (building_psymtab && part_die->name != NULL)
04a679b8 18271 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 18272 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 18273 &objfile->static_psymbols,
1762568f 18274 0, cu->language, objfile);
dee91e82 18275 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
18276 continue;
18277 }
18278
d8228535
JK
18279 /* The exception for DW_TAG_typedef with has_children above is
18280 a workaround of GCC PR debug/47510. In the case of this complaint
18281 type_name_no_tag_or_error will error on such types later.
18282
18283 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18284 it could not find the child DIEs referenced later, this is checked
18285 above. In correct DWARF DW_TAG_typedef should have no children. */
18286
18287 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
18288 complaint (&symfile_complaints,
18289 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18290 "- DIE at 0x%x [in module %s]"),
9c541725 18291 to_underlying (part_die->sect_off), objfile_name (objfile));
d8228535 18292
72bf9492
DJ
18293 /* If we're at the second level, and we're an enumerator, and
18294 our parent has no specification (meaning possibly lives in a
18295 namespace elsewhere), then we can add the partial symbol now
18296 instead of queueing it. */
18297 if (part_die->tag == DW_TAG_enumerator
18298 && parent_die != NULL
18299 && parent_die->die_parent == NULL
18300 && parent_die->tag == DW_TAG_enumeration_type
18301 && parent_die->has_specification == 0)
18302 {
18303 if (part_die->name == NULL)
3e43a32a
MS
18304 complaint (&symfile_complaints,
18305 _("malformed enumerator DIE ignored"));
72bf9492 18306 else if (building_psymtab)
04a679b8 18307 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 18308 VAR_DOMAIN, LOC_CONST,
9c37b5ae 18309 cu->language == language_cplus
bb5ed363
DE
18310 ? &objfile->global_psymbols
18311 : &objfile->static_psymbols,
1762568f 18312 0, cu->language, objfile);
72bf9492 18313
dee91e82 18314 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
18315 continue;
18316 }
18317
18318 /* We'll save this DIE so link it in. */
18319 part_die->die_parent = parent_die;
18320 part_die->die_sibling = NULL;
18321 part_die->die_child = NULL;
18322
18323 if (last_die && last_die == parent_die)
18324 last_die->die_child = part_die;
18325 else if (last_die)
18326 last_die->die_sibling = part_die;
18327
18328 last_die = part_die;
18329
18330 if (first_die == NULL)
18331 first_die = part_die;
18332
18333 /* Maybe add the DIE to the hash table. Not all DIEs that we
18334 find interesting need to be in the hash table, because we
18335 also have the parent/sibling/child chains; only those that we
18336 might refer to by offset later during partial symbol reading.
18337
18338 For now this means things that might have be the target of a
18339 DW_AT_specification, DW_AT_abstract_origin, or
18340 DW_AT_extension. DW_AT_extension will refer only to
18341 namespaces; DW_AT_abstract_origin refers to functions (and
18342 many things under the function DIE, but we do not recurse
18343 into function DIEs during partial symbol reading) and
18344 possibly variables as well; DW_AT_specification refers to
18345 declarations. Declarations ought to have the DW_AT_declaration
18346 flag. It happens that GCC forgets to put it in sometimes, but
18347 only for functions, not for types.
18348
18349 Adding more things than necessary to the hash table is harmless
18350 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18351 wasted time in find_partial_die, when we reread the compilation
18352 unit with load_all_dies set. */
72bf9492 18353
5afb4e99 18354 if (load_all
72929c62 18355 || abbrev->tag == DW_TAG_constant
5afb4e99 18356 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18357 || abbrev->tag == DW_TAG_variable
18358 || abbrev->tag == DW_TAG_namespace
18359 || part_die->is_declaration)
18360 {
18361 void **slot;
18362
18363 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18364 to_underlying (part_die->sect_off),
18365 INSERT);
72bf9492
DJ
18366 *slot = part_die;
18367 }
18368
8d749320 18369 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
18370
18371 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18372 we have no reason to follow the children of structures; for other
98bfdba5
PA
18373 languages we have to, so that we can get at method physnames
18374 to infer fully qualified class names, for DW_AT_specification,
18375 and for C++ template arguments. For C++, we also look one level
18376 inside functions to find template arguments (if the name of the
18377 function does not already contain the template arguments).
bc30ff58
JB
18378
18379 For Ada, we need to scan the children of subprograms and lexical
18380 blocks as well because Ada allows the definition of nested
18381 entities that could be interesting for the debugger, such as
18382 nested subprograms for instance. */
72bf9492 18383 if (last_die->has_children
5afb4e99
DJ
18384 && (load_all
18385 || last_die->tag == DW_TAG_namespace
f55ee35c 18386 || last_die->tag == DW_TAG_module
72bf9492 18387 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18388 || (cu->language == language_cplus
18389 && last_die->tag == DW_TAG_subprogram
18390 && (last_die->name == NULL
18391 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18392 || (cu->language != language_c
18393 && (last_die->tag == DW_TAG_class_type
680b30c7 18394 || last_die->tag == DW_TAG_interface_type
72bf9492 18395 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
18396 || last_die->tag == DW_TAG_union_type))
18397 || (cu->language == language_ada
18398 && (last_die->tag == DW_TAG_subprogram
18399 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18400 {
18401 nesting_level++;
18402 parent_die = last_die;
18403 continue;
18404 }
18405
18406 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18407 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18408
18409 /* Back to the top, do it again. */
18410 }
18411}
18412
c906108c
SS
18413/* Read a minimal amount of information into the minimal die structure. */
18414
d521ce57 18415static const gdb_byte *
dee91e82
DE
18416read_partial_die (const struct die_reader_specs *reader,
18417 struct partial_die_info *part_die,
18418 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 18419 const gdb_byte *info_ptr)
c906108c 18420{
dee91e82 18421 struct dwarf2_cu *cu = reader->cu;
bb5ed363 18422 struct objfile *objfile = cu->objfile;
d521ce57 18423 const gdb_byte *buffer = reader->buffer;
fa238c03 18424 unsigned int i;
c906108c 18425 struct attribute attr;
c5aa993b 18426 int has_low_pc_attr = 0;
c906108c 18427 int has_high_pc_attr = 0;
91da1414 18428 int high_pc_relative = 0;
c906108c 18429
72bf9492 18430 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 18431
9c541725 18432 part_die->sect_off = (sect_offset) (info_ptr - buffer);
72bf9492
DJ
18433
18434 info_ptr += abbrev_len;
18435
18436 if (abbrev == NULL)
18437 return info_ptr;
18438
c906108c
SS
18439 part_die->tag = abbrev->tag;
18440 part_die->has_children = abbrev->has_children;
c906108c
SS
18441
18442 for (i = 0; i < abbrev->num_attrs; ++i)
18443 {
dee91e82 18444 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
18445
18446 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18447 partial symbol table. */
c906108c
SS
18448 switch (attr.name)
18449 {
18450 case DW_AT_name:
71c25dea
TT
18451 switch (part_die->tag)
18452 {
18453 case DW_TAG_compile_unit:
95554aad 18454 case DW_TAG_partial_unit:
348e048f 18455 case DW_TAG_type_unit:
71c25dea
TT
18456 /* Compilation units have a DW_AT_name that is a filename, not
18457 a source language identifier. */
18458 case DW_TAG_enumeration_type:
18459 case DW_TAG_enumerator:
18460 /* These tags always have simple identifiers already; no need
18461 to canonicalize them. */
18462 part_die->name = DW_STRING (&attr);
18463 break;
18464 default:
18465 part_die->name
18466 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 18467 &objfile->per_bfd->storage_obstack);
71c25dea
TT
18468 break;
18469 }
c906108c 18470 break;
31ef98ae 18471 case DW_AT_linkage_name:
c906108c 18472 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18473 /* Note that both forms of linkage name might appear. We
18474 assume they will be the same, and we only store the last
18475 one we see. */
94af9270
KS
18476 if (cu->language == language_ada)
18477 part_die->name = DW_STRING (&attr);
abc72ce4 18478 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
18479 break;
18480 case DW_AT_low_pc:
18481 has_low_pc_attr = 1;
31aa7e4e 18482 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
18483 break;
18484 case DW_AT_high_pc:
18485 has_high_pc_attr = 1;
31aa7e4e
JB
18486 part_die->highpc = attr_value_as_address (&attr);
18487 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18488 high_pc_relative = 1;
c906108c
SS
18489 break;
18490 case DW_AT_location:
0963b4bd 18491 /* Support the .debug_loc offsets. */
8e19ed76
PS
18492 if (attr_form_is_block (&attr))
18493 {
95554aad 18494 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 18495 }
3690dd37 18496 else if (attr_form_is_section_offset (&attr))
8e19ed76 18497 {
4d3c2250 18498 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18499 }
18500 else
18501 {
4d3c2250
KB
18502 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18503 "partial symbol information");
8e19ed76 18504 }
c906108c 18505 break;
c906108c
SS
18506 case DW_AT_external:
18507 part_die->is_external = DW_UNSND (&attr);
18508 break;
18509 case DW_AT_declaration:
18510 part_die->is_declaration = DW_UNSND (&attr);
18511 break;
18512 case DW_AT_type:
18513 part_die->has_type = 1;
18514 break;
18515 case DW_AT_abstract_origin:
18516 case DW_AT_specification:
72bf9492
DJ
18517 case DW_AT_extension:
18518 part_die->has_specification = 1;
c764a876 18519 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
18520 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18521 || cu->per_cu->is_dwz);
c906108c
SS
18522 break;
18523 case DW_AT_sibling:
18524 /* Ignore absolute siblings, they might point outside of
18525 the current compile unit. */
18526 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
18527 complaint (&symfile_complaints,
18528 _("ignoring absolute DW_AT_sibling"));
c906108c 18529 else
b9502d3f 18530 {
9c541725
PA
18531 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18532 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18533
18534 if (sibling_ptr < info_ptr)
18535 complaint (&symfile_complaints,
18536 _("DW_AT_sibling points backwards"));
22869d73
KS
18537 else if (sibling_ptr > reader->buffer_end)
18538 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
18539 else
18540 part_die->sibling = sibling_ptr;
18541 }
c906108c 18542 break;
fa4028e9
JB
18543 case DW_AT_byte_size:
18544 part_die->has_byte_size = 1;
18545 break;
ff908ebf
AW
18546 case DW_AT_const_value:
18547 part_die->has_const_value = 1;
18548 break;
68511cec
CES
18549 case DW_AT_calling_convention:
18550 /* DWARF doesn't provide a way to identify a program's source-level
18551 entry point. DW_AT_calling_convention attributes are only meant
18552 to describe functions' calling conventions.
18553
18554 However, because it's a necessary piece of information in
0c1b455e
TT
18555 Fortran, and before DWARF 4 DW_CC_program was the only
18556 piece of debugging information whose definition refers to
18557 a 'main program' at all, several compilers marked Fortran
18558 main programs with DW_CC_program --- even when those
18559 functions use the standard calling conventions.
18560
18561 Although DWARF now specifies a way to provide this
18562 information, we support this practice for backward
18563 compatibility. */
68511cec 18564 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
18565 && cu->language == language_fortran)
18566 part_die->main_subprogram = 1;
68511cec 18567 break;
481860b3
GB
18568 case DW_AT_inline:
18569 if (DW_UNSND (&attr) == DW_INL_inlined
18570 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18571 part_die->may_be_inlined = 1;
18572 break;
95554aad
TT
18573
18574 case DW_AT_import:
18575 if (part_die->tag == DW_TAG_imported_unit)
36586728 18576 {
9c541725 18577 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
36586728
TT
18578 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18579 || cu->per_cu->is_dwz);
18580 }
95554aad
TT
18581 break;
18582
0c1b455e
TT
18583 case DW_AT_main_subprogram:
18584 part_die->main_subprogram = DW_UNSND (&attr);
18585 break;
18586
c906108c
SS
18587 default:
18588 break;
18589 }
18590 }
18591
91da1414
MW
18592 if (high_pc_relative)
18593 part_die->highpc += part_die->lowpc;
18594
9373cf26
JK
18595 if (has_low_pc_attr && has_high_pc_attr)
18596 {
18597 /* When using the GNU linker, .gnu.linkonce. sections are used to
18598 eliminate duplicate copies of functions and vtables and such.
18599 The linker will arbitrarily choose one and discard the others.
18600 The AT_*_pc values for such functions refer to local labels in
18601 these sections. If the section from that file was discarded, the
18602 labels are not in the output, so the relocs get a value of 0.
18603 If this is a discarded function, mark the pc bounds as invalid,
18604 so that GDB will ignore it. */
18605 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18606 {
bb5ed363 18607 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
18608
18609 complaint (&symfile_complaints,
18610 _("DW_AT_low_pc %s is zero "
18611 "for DIE at 0x%x [in module %s]"),
18612 paddress (gdbarch, part_die->lowpc),
9c541725 18613 to_underlying (part_die->sect_off), objfile_name (objfile));
9373cf26
JK
18614 }
18615 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18616 else if (part_die->lowpc >= part_die->highpc)
18617 {
bb5ed363 18618 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
18619
18620 complaint (&symfile_complaints,
18621 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18622 "for DIE at 0x%x [in module %s]"),
18623 paddress (gdbarch, part_die->lowpc),
18624 paddress (gdbarch, part_die->highpc),
9c541725
PA
18625 to_underlying (part_die->sect_off),
18626 objfile_name (objfile));
9373cf26
JK
18627 }
18628 else
18629 part_die->has_pc_info = 1;
18630 }
85cbf3d3 18631
c906108c
SS
18632 return info_ptr;
18633}
18634
72bf9492
DJ
18635/* Find a cached partial DIE at OFFSET in CU. */
18636
18637static struct partial_die_info *
9c541725 18638find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
72bf9492
DJ
18639{
18640 struct partial_die_info *lookup_die = NULL;
18641 struct partial_die_info part_die;
18642
9c541725 18643 part_die.sect_off = sect_off;
9a3c8263
SM
18644 lookup_die = ((struct partial_die_info *)
18645 htab_find_with_hash (cu->partial_dies, &part_die,
9c541725 18646 to_underlying (sect_off)));
72bf9492 18647
72bf9492
DJ
18648 return lookup_die;
18649}
18650
348e048f
DE
18651/* Find a partial DIE at OFFSET, which may or may not be in CU,
18652 except in the case of .debug_types DIEs which do not reference
18653 outside their CU (they do however referencing other types via
55f1336d 18654 DW_FORM_ref_sig8). */
72bf9492
DJ
18655
18656static struct partial_die_info *
9c541725 18657find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18658{
bb5ed363 18659 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
18660 struct dwarf2_per_cu_data *per_cu = NULL;
18661 struct partial_die_info *pd = NULL;
72bf9492 18662
36586728 18663 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18664 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18665 {
9c541725 18666 pd = find_partial_die_in_comp_unit (sect_off, cu);
5afb4e99
DJ
18667 if (pd != NULL)
18668 return pd;
0d99eb77
DE
18669 /* We missed recording what we needed.
18670 Load all dies and try again. */
18671 per_cu = cu->per_cu;
5afb4e99 18672 }
0d99eb77
DE
18673 else
18674 {
18675 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18676 if (cu->per_cu->is_debug_types)
0d99eb77 18677 {
9c541725
PA
18678 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
18679 " external reference to offset 0x%x [in module %s].\n"),
18680 to_underlying (cu->header.sect_off), to_underlying (sect_off),
0d99eb77
DE
18681 bfd_get_filename (objfile->obfd));
18682 }
9c541725 18683 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 18684 objfile);
72bf9492 18685
0d99eb77
DE
18686 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18687 load_partial_comp_unit (per_cu);
ae038cb0 18688
0d99eb77 18689 per_cu->cu->last_used = 0;
9c541725 18690 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
0d99eb77 18691 }
5afb4e99 18692
dee91e82
DE
18693 /* If we didn't find it, and not all dies have been loaded,
18694 load them all and try again. */
18695
5afb4e99
DJ
18696 if (pd == NULL && per_cu->load_all_dies == 0)
18697 {
5afb4e99 18698 per_cu->load_all_dies = 1;
fd820528
DE
18699
18700 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18701 THIS_CU->cu may already be in use. So we can't just free it and
18702 replace its DIEs with the ones we read in. Instead, we leave those
18703 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18704 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18705 set. */
dee91e82 18706 load_partial_comp_unit (per_cu);
5afb4e99 18707
9c541725 18708 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
5afb4e99
DJ
18709 }
18710
18711 if (pd == NULL)
18712 internal_error (__FILE__, __LINE__,
3e43a32a
MS
18713 _("could not find partial DIE 0x%x "
18714 "in cache [from module %s]\n"),
9c541725 18715 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 18716 return pd;
72bf9492
DJ
18717}
18718
abc72ce4
DE
18719/* See if we can figure out if the class lives in a namespace. We do
18720 this by looking for a member function; its demangled name will
18721 contain namespace info, if there is any. */
18722
18723static void
18724guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18725 struct dwarf2_cu *cu)
18726{
18727 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18728 what template types look like, because the demangler
18729 frequently doesn't give the same name as the debug info. We
18730 could fix this by only using the demangled name to get the
18731 prefix (but see comment in read_structure_type). */
18732
18733 struct partial_die_info *real_pdi;
18734 struct partial_die_info *child_pdi;
18735
18736 /* If this DIE (this DIE's specification, if any) has a parent, then
18737 we should not do this. We'll prepend the parent's fully qualified
18738 name when we create the partial symbol. */
18739
18740 real_pdi = struct_pdi;
18741 while (real_pdi->has_specification)
36586728
TT
18742 real_pdi = find_partial_die (real_pdi->spec_offset,
18743 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
18744
18745 if (real_pdi->die_parent != NULL)
18746 return;
18747
18748 for (child_pdi = struct_pdi->die_child;
18749 child_pdi != NULL;
18750 child_pdi = child_pdi->die_sibling)
18751 {
18752 if (child_pdi->tag == DW_TAG_subprogram
18753 && child_pdi->linkage_name != NULL)
18754 {
18755 char *actual_class_name
18756 = language_class_name_from_physname (cu->language_defn,
18757 child_pdi->linkage_name);
18758 if (actual_class_name != NULL)
18759 {
18760 struct_pdi->name
224c3ddb
SM
18761 = ((const char *)
18762 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
18763 actual_class_name,
18764 strlen (actual_class_name)));
abc72ce4
DE
18765 xfree (actual_class_name);
18766 }
18767 break;
18768 }
18769 }
18770}
18771
72bf9492
DJ
18772/* Adjust PART_DIE before generating a symbol for it. This function
18773 may set the is_external flag or change the DIE's name. */
18774
18775static void
18776fixup_partial_die (struct partial_die_info *part_die,
18777 struct dwarf2_cu *cu)
18778{
abc72ce4
DE
18779 /* Once we've fixed up a die, there's no point in doing so again.
18780 This also avoids a memory leak if we were to call
18781 guess_partial_die_structure_name multiple times. */
18782 if (part_die->fixup_called)
18783 return;
18784
72bf9492
DJ
18785 /* If we found a reference attribute and the DIE has no name, try
18786 to find a name in the referred to DIE. */
18787
18788 if (part_die->name == NULL && part_die->has_specification)
18789 {
18790 struct partial_die_info *spec_die;
72bf9492 18791
36586728
TT
18792 spec_die = find_partial_die (part_die->spec_offset,
18793 part_die->spec_is_dwz, cu);
72bf9492 18794
10b3939b 18795 fixup_partial_die (spec_die, cu);
72bf9492
DJ
18796
18797 if (spec_die->name)
18798 {
18799 part_die->name = spec_die->name;
18800
18801 /* Copy DW_AT_external attribute if it is set. */
18802 if (spec_die->is_external)
18803 part_die->is_external = spec_die->is_external;
18804 }
18805 }
18806
18807 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
18808
18809 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 18810 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18811
abc72ce4
DE
18812 /* If there is no parent die to provide a namespace, and there are
18813 children, see if we can determine the namespace from their linkage
122d1940 18814 name. */
abc72ce4 18815 if (cu->language == language_cplus
8b70b953 18816 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
18817 && part_die->die_parent == NULL
18818 && part_die->has_children
18819 && (part_die->tag == DW_TAG_class_type
18820 || part_die->tag == DW_TAG_structure_type
18821 || part_die->tag == DW_TAG_union_type))
18822 guess_partial_die_structure_name (part_die, cu);
18823
53832f31
TT
18824 /* GCC might emit a nameless struct or union that has a linkage
18825 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18826 if (part_die->name == NULL
96408a79
SA
18827 && (part_die->tag == DW_TAG_class_type
18828 || part_die->tag == DW_TAG_interface_type
18829 || part_die->tag == DW_TAG_structure_type
18830 || part_die->tag == DW_TAG_union_type)
53832f31
TT
18831 && part_die->linkage_name != NULL)
18832 {
18833 char *demangled;
18834
8de20a37 18835 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
18836 if (demangled)
18837 {
96408a79
SA
18838 const char *base;
18839
18840 /* Strip any leading namespaces/classes, keep only the base name.
18841 DW_AT_name for named DIEs does not contain the prefixes. */
18842 base = strrchr (demangled, ':');
18843 if (base && base > demangled && base[-1] == ':')
18844 base++;
18845 else
18846 base = demangled;
18847
34a68019 18848 part_die->name
224c3ddb
SM
18849 = ((const char *)
18850 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
18851 base, strlen (base)));
53832f31
TT
18852 xfree (demangled);
18853 }
18854 }
18855
abc72ce4 18856 part_die->fixup_called = 1;
72bf9492
DJ
18857}
18858
a8329558 18859/* Read an attribute value described by an attribute form. */
c906108c 18860
d521ce57 18861static const gdb_byte *
dee91e82
DE
18862read_attribute_value (const struct die_reader_specs *reader,
18863 struct attribute *attr, unsigned form,
43988095 18864 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 18865{
dee91e82 18866 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
18867 struct objfile *objfile = cu->objfile;
18868 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18869 bfd *abfd = reader->abfd;
e7c27a73 18870 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18871 unsigned int bytes_read;
18872 struct dwarf_block *blk;
18873
aead7601 18874 attr->form = (enum dwarf_form) form;
a8329558 18875 switch (form)
c906108c 18876 {
c906108c 18877 case DW_FORM_ref_addr:
ae411497 18878 if (cu->header.version == 2)
4568ecf9 18879 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 18880 else
4568ecf9
DE
18881 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18882 &cu->header, &bytes_read);
ae411497
TT
18883 info_ptr += bytes_read;
18884 break;
36586728
TT
18885 case DW_FORM_GNU_ref_alt:
18886 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18887 info_ptr += bytes_read;
18888 break;
ae411497 18889 case DW_FORM_addr:
e7c27a73 18890 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 18891 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18892 info_ptr += bytes_read;
c906108c
SS
18893 break;
18894 case DW_FORM_block2:
7b5a2f43 18895 blk = dwarf_alloc_block (cu);
c906108c
SS
18896 blk->size = read_2_bytes (abfd, info_ptr);
18897 info_ptr += 2;
18898 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18899 info_ptr += blk->size;
18900 DW_BLOCK (attr) = blk;
18901 break;
18902 case DW_FORM_block4:
7b5a2f43 18903 blk = dwarf_alloc_block (cu);
c906108c
SS
18904 blk->size = read_4_bytes (abfd, info_ptr);
18905 info_ptr += 4;
18906 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18907 info_ptr += blk->size;
18908 DW_BLOCK (attr) = blk;
18909 break;
18910 case DW_FORM_data2:
18911 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18912 info_ptr += 2;
18913 break;
18914 case DW_FORM_data4:
18915 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18916 info_ptr += 4;
18917 break;
18918 case DW_FORM_data8:
18919 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18920 info_ptr += 8;
18921 break;
0224619f
JK
18922 case DW_FORM_data16:
18923 blk = dwarf_alloc_block (cu);
18924 blk->size = 16;
18925 blk->data = read_n_bytes (abfd, info_ptr, 16);
18926 info_ptr += 16;
18927 DW_BLOCK (attr) = blk;
18928 break;
2dc7f7b3
TT
18929 case DW_FORM_sec_offset:
18930 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18931 info_ptr += bytes_read;
18932 break;
c906108c 18933 case DW_FORM_string:
9b1c24c8 18934 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18935 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18936 info_ptr += bytes_read;
18937 break;
4bdf3d34 18938 case DW_FORM_strp:
36586728
TT
18939 if (!cu->per_cu->is_dwz)
18940 {
18941 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
18942 &bytes_read);
18943 DW_STRING_IS_CANONICAL (attr) = 0;
18944 info_ptr += bytes_read;
18945 break;
18946 }
18947 /* FALLTHROUGH */
43988095
JK
18948 case DW_FORM_line_strp:
18949 if (!cu->per_cu->is_dwz)
18950 {
18951 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
18952 cu_header, &bytes_read);
18953 DW_STRING_IS_CANONICAL (attr) = 0;
18954 info_ptr += bytes_read;
18955 break;
18956 }
18957 /* FALLTHROUGH */
36586728
TT
18958 case DW_FORM_GNU_strp_alt:
18959 {
18960 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18961 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18962 &bytes_read);
18963
18964 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
18965 DW_STRING_IS_CANONICAL (attr) = 0;
18966 info_ptr += bytes_read;
18967 }
4bdf3d34 18968 break;
2dc7f7b3 18969 case DW_FORM_exprloc:
c906108c 18970 case DW_FORM_block:
7b5a2f43 18971 blk = dwarf_alloc_block (cu);
c906108c
SS
18972 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18973 info_ptr += bytes_read;
18974 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18975 info_ptr += blk->size;
18976 DW_BLOCK (attr) = blk;
18977 break;
18978 case DW_FORM_block1:
7b5a2f43 18979 blk = dwarf_alloc_block (cu);
c906108c
SS
18980 blk->size = read_1_byte (abfd, info_ptr);
18981 info_ptr += 1;
18982 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18983 info_ptr += blk->size;
18984 DW_BLOCK (attr) = blk;
18985 break;
18986 case DW_FORM_data1:
18987 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18988 info_ptr += 1;
18989 break;
18990 case DW_FORM_flag:
18991 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18992 info_ptr += 1;
18993 break;
2dc7f7b3
TT
18994 case DW_FORM_flag_present:
18995 DW_UNSND (attr) = 1;
18996 break;
c906108c
SS
18997 case DW_FORM_sdata:
18998 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18999 info_ptr += bytes_read;
19000 break;
19001 case DW_FORM_udata:
19002 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19003 info_ptr += bytes_read;
19004 break;
19005 case DW_FORM_ref1:
9c541725 19006 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19007 + read_1_byte (abfd, info_ptr));
c906108c
SS
19008 info_ptr += 1;
19009 break;
19010 case DW_FORM_ref2:
9c541725 19011 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19012 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19013 info_ptr += 2;
19014 break;
19015 case DW_FORM_ref4:
9c541725 19016 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19017 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19018 info_ptr += 4;
19019 break;
613e1657 19020 case DW_FORM_ref8:
9c541725 19021 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19022 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19023 info_ptr += 8;
19024 break;
55f1336d 19025 case DW_FORM_ref_sig8:
ac9ec31b 19026 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19027 info_ptr += 8;
19028 break;
c906108c 19029 case DW_FORM_ref_udata:
9c541725 19030 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19031 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19032 info_ptr += bytes_read;
19033 break;
c906108c 19034 case DW_FORM_indirect:
a8329558
KW
19035 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19036 info_ptr += bytes_read;
43988095
JK
19037 if (form == DW_FORM_implicit_const)
19038 {
19039 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19040 info_ptr += bytes_read;
19041 }
19042 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19043 info_ptr);
19044 break;
19045 case DW_FORM_implicit_const:
19046 DW_SND (attr) = implicit_const;
a8329558 19047 break;
3019eac3
DE
19048 case DW_FORM_GNU_addr_index:
19049 if (reader->dwo_file == NULL)
19050 {
19051 /* For now flag a hard error.
19052 Later we can turn this into a complaint. */
19053 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19054 dwarf_form_name (form),
19055 bfd_get_filename (abfd));
19056 }
19057 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19058 info_ptr += bytes_read;
19059 break;
19060 case DW_FORM_GNU_str_index:
19061 if (reader->dwo_file == NULL)
19062 {
19063 /* For now flag a hard error.
19064 Later we can turn this into a complaint if warranted. */
19065 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19066 dwarf_form_name (form),
19067 bfd_get_filename (abfd));
19068 }
19069 {
19070 ULONGEST str_index =
19071 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19072
342587c4 19073 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
19074 DW_STRING_IS_CANONICAL (attr) = 0;
19075 info_ptr += bytes_read;
19076 }
19077 break;
c906108c 19078 default:
8a3fe4f8 19079 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19080 dwarf_form_name (form),
19081 bfd_get_filename (abfd));
c906108c 19082 }
28e94949 19083
36586728 19084 /* Super hack. */
7771576e 19085 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19086 attr->form = DW_FORM_GNU_ref_alt;
19087
28e94949
JB
19088 /* We have seen instances where the compiler tried to emit a byte
19089 size attribute of -1 which ended up being encoded as an unsigned
19090 0xffffffff. Although 0xffffffff is technically a valid size value,
19091 an object of this size seems pretty unlikely so we can relatively
19092 safely treat these cases as if the size attribute was invalid and
19093 treat them as zero by default. */
19094 if (attr->name == DW_AT_byte_size
19095 && form == DW_FORM_data4
19096 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19097 {
19098 complaint
19099 (&symfile_complaints,
43bbcdc2
PH
19100 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19101 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19102 DW_UNSND (attr) = 0;
19103 }
28e94949 19104
c906108c
SS
19105 return info_ptr;
19106}
19107
a8329558
KW
19108/* Read an attribute described by an abbreviated attribute. */
19109
d521ce57 19110static const gdb_byte *
dee91e82
DE
19111read_attribute (const struct die_reader_specs *reader,
19112 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19113 const gdb_byte *info_ptr)
a8329558
KW
19114{
19115 attr->name = abbrev->name;
43988095
JK
19116 return read_attribute_value (reader, attr, abbrev->form,
19117 abbrev->implicit_const, info_ptr);
a8329558
KW
19118}
19119
0963b4bd 19120/* Read dwarf information from a buffer. */
c906108c
SS
19121
19122static unsigned int
a1855c1d 19123read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19124{
fe1b8b76 19125 return bfd_get_8 (abfd, buf);
c906108c
SS
19126}
19127
19128static int
a1855c1d 19129read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19130{
fe1b8b76 19131 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19132}
19133
19134static unsigned int
a1855c1d 19135read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19136{
fe1b8b76 19137 return bfd_get_16 (abfd, buf);
c906108c
SS
19138}
19139
21ae7a4d 19140static int
a1855c1d 19141read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19142{
19143 return bfd_get_signed_16 (abfd, buf);
19144}
19145
c906108c 19146static unsigned int
a1855c1d 19147read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19148{
fe1b8b76 19149 return bfd_get_32 (abfd, buf);
c906108c
SS
19150}
19151
21ae7a4d 19152static int
a1855c1d 19153read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19154{
19155 return bfd_get_signed_32 (abfd, buf);
19156}
19157
93311388 19158static ULONGEST
a1855c1d 19159read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19160{
fe1b8b76 19161 return bfd_get_64 (abfd, buf);
c906108c
SS
19162}
19163
19164static CORE_ADDR
d521ce57 19165read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19166 unsigned int *bytes_read)
c906108c 19167{
e7c27a73 19168 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19169 CORE_ADDR retval = 0;
19170
107d2387 19171 if (cu_header->signed_addr_p)
c906108c 19172 {
107d2387
AC
19173 switch (cu_header->addr_size)
19174 {
19175 case 2:
fe1b8b76 19176 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19177 break;
19178 case 4:
fe1b8b76 19179 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19180 break;
19181 case 8:
fe1b8b76 19182 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19183 break;
19184 default:
8e65ff28 19185 internal_error (__FILE__, __LINE__,
e2e0b3e5 19186 _("read_address: bad switch, signed [in module %s]"),
659b0389 19187 bfd_get_filename (abfd));
107d2387
AC
19188 }
19189 }
19190 else
19191 {
19192 switch (cu_header->addr_size)
19193 {
19194 case 2:
fe1b8b76 19195 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19196 break;
19197 case 4:
fe1b8b76 19198 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19199 break;
19200 case 8:
fe1b8b76 19201 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19202 break;
19203 default:
8e65ff28 19204 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19205 _("read_address: bad switch, "
19206 "unsigned [in module %s]"),
659b0389 19207 bfd_get_filename (abfd));
107d2387 19208 }
c906108c 19209 }
64367e0a 19210
107d2387
AC
19211 *bytes_read = cu_header->addr_size;
19212 return retval;
c906108c
SS
19213}
19214
f7ef9339 19215/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19216 specification allows the initial length to take up either 4 bytes
19217 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19218 bytes describe the length and all offsets will be 8 bytes in length
19219 instead of 4.
19220
f7ef9339
KB
19221 An older, non-standard 64-bit format is also handled by this
19222 function. The older format in question stores the initial length
19223 as an 8-byte quantity without an escape value. Lengths greater
19224 than 2^32 aren't very common which means that the initial 4 bytes
19225 is almost always zero. Since a length value of zero doesn't make
19226 sense for the 32-bit format, this initial zero can be considered to
19227 be an escape value which indicates the presence of the older 64-bit
19228 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19229 greater than 4GB. If it becomes necessary to handle lengths
19230 somewhat larger than 4GB, we could allow other small values (such
19231 as the non-sensical values of 1, 2, and 3) to also be used as
19232 escape values indicating the presence of the old format.
f7ef9339 19233
917c78fc
MK
19234 The value returned via bytes_read should be used to increment the
19235 relevant pointer after calling read_initial_length().
c764a876 19236
613e1657
KB
19237 [ Note: read_initial_length() and read_offset() are based on the
19238 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19239 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19240 from:
19241
f7ef9339 19242 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19243
613e1657
KB
19244 This document is only a draft and is subject to change. (So beware.)
19245
f7ef9339 19246 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19247 determined empirically by examining 64-bit ELF files produced by
19248 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19249
19250 - Kevin, July 16, 2002
613e1657
KB
19251 ] */
19252
19253static LONGEST
d521ce57 19254read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19255{
fe1b8b76 19256 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19257
dd373385 19258 if (length == 0xffffffff)
613e1657 19259 {
fe1b8b76 19260 length = bfd_get_64 (abfd, buf + 4);
613e1657 19261 *bytes_read = 12;
613e1657 19262 }
dd373385 19263 else if (length == 0)
f7ef9339 19264 {
dd373385 19265 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19266 length = bfd_get_64 (abfd, buf);
f7ef9339 19267 *bytes_read = 8;
f7ef9339 19268 }
613e1657
KB
19269 else
19270 {
19271 *bytes_read = 4;
613e1657
KB
19272 }
19273
c764a876
DE
19274 return length;
19275}
dd373385 19276
c764a876
DE
19277/* Cover function for read_initial_length.
19278 Returns the length of the object at BUF, and stores the size of the
19279 initial length in *BYTES_READ and stores the size that offsets will be in
19280 *OFFSET_SIZE.
19281 If the initial length size is not equivalent to that specified in
19282 CU_HEADER then issue a complaint.
19283 This is useful when reading non-comp-unit headers. */
dd373385 19284
c764a876 19285static LONGEST
d521ce57 19286read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19287 const struct comp_unit_head *cu_header,
19288 unsigned int *bytes_read,
19289 unsigned int *offset_size)
19290{
19291 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19292
19293 gdb_assert (cu_header->initial_length_size == 4
19294 || cu_header->initial_length_size == 8
19295 || cu_header->initial_length_size == 12);
19296
19297 if (cu_header->initial_length_size != *bytes_read)
19298 complaint (&symfile_complaints,
19299 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19300
c764a876 19301 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19302 return length;
613e1657
KB
19303}
19304
19305/* Read an offset from the data stream. The size of the offset is
917c78fc 19306 given by cu_header->offset_size. */
613e1657
KB
19307
19308static LONGEST
d521ce57
TT
19309read_offset (bfd *abfd, const gdb_byte *buf,
19310 const struct comp_unit_head *cu_header,
891d2f0b 19311 unsigned int *bytes_read)
c764a876
DE
19312{
19313 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19314
c764a876
DE
19315 *bytes_read = cu_header->offset_size;
19316 return offset;
19317}
19318
19319/* Read an offset from the data stream. */
19320
19321static LONGEST
d521ce57 19322read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19323{
19324 LONGEST retval = 0;
19325
c764a876 19326 switch (offset_size)
613e1657
KB
19327 {
19328 case 4:
fe1b8b76 19329 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19330 break;
19331 case 8:
fe1b8b76 19332 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19333 break;
19334 default:
8e65ff28 19335 internal_error (__FILE__, __LINE__,
c764a876 19336 _("read_offset_1: bad switch [in module %s]"),
659b0389 19337 bfd_get_filename (abfd));
613e1657
KB
19338 }
19339
917c78fc 19340 return retval;
613e1657
KB
19341}
19342
d521ce57
TT
19343static const gdb_byte *
19344read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19345{
19346 /* If the size of a host char is 8 bits, we can return a pointer
19347 to the buffer, otherwise we have to copy the data to a buffer
19348 allocated on the temporary obstack. */
4bdf3d34 19349 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19350 return buf;
c906108c
SS
19351}
19352
d521ce57
TT
19353static const char *
19354read_direct_string (bfd *abfd, const gdb_byte *buf,
19355 unsigned int *bytes_read_ptr)
c906108c
SS
19356{
19357 /* If the size of a host char is 8 bits, we can return a pointer
19358 to the string, otherwise we have to copy the string to a buffer
19359 allocated on the temporary obstack. */
4bdf3d34 19360 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19361 if (*buf == '\0')
19362 {
19363 *bytes_read_ptr = 1;
19364 return NULL;
19365 }
d521ce57
TT
19366 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19367 return (const char *) buf;
4bdf3d34
JJ
19368}
19369
43988095
JK
19370/* Return pointer to string at section SECT offset STR_OFFSET with error
19371 reporting strings FORM_NAME and SECT_NAME. */
19372
d521ce57 19373static const char *
43988095
JK
19374read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
19375 struct dwarf2_section_info *sect,
19376 const char *form_name,
19377 const char *sect_name)
19378{
19379 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
19380 if (sect->buffer == NULL)
19381 error (_("%s used without %s section [in module %s]"),
19382 form_name, sect_name, bfd_get_filename (abfd));
19383 if (str_offset >= sect->size)
19384 error (_("%s pointing outside of %s section [in module %s]"),
19385 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19386 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19387 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19388 return NULL;
43988095
JK
19389 return (const char *) (sect->buffer + str_offset);
19390}
19391
19392/* Return pointer to string at .debug_str offset STR_OFFSET. */
19393
19394static const char *
19395read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
19396{
19397 return read_indirect_string_at_offset_from (abfd, str_offset,
19398 &dwarf2_per_objfile->str,
19399 "DW_FORM_strp", ".debug_str");
19400}
19401
19402/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19403
19404static const char *
19405read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
19406{
19407 return read_indirect_string_at_offset_from (abfd, str_offset,
19408 &dwarf2_per_objfile->line_str,
19409 "DW_FORM_line_strp",
19410 ".debug_line_str");
c906108c
SS
19411}
19412
36586728
TT
19413/* Read a string at offset STR_OFFSET in the .debug_str section from
19414 the .dwz file DWZ. Throw an error if the offset is too large. If
19415 the string consists of a single NUL byte, return NULL; otherwise
19416 return a pointer to the string. */
19417
d521ce57 19418static const char *
36586728
TT
19419read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
19420{
19421 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
19422
19423 if (dwz->str.buffer == NULL)
19424 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19425 "section [in module %s]"),
19426 bfd_get_filename (dwz->dwz_bfd));
19427 if (str_offset >= dwz->str.size)
19428 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19429 ".debug_str section [in module %s]"),
19430 bfd_get_filename (dwz->dwz_bfd));
19431 gdb_assert (HOST_CHAR_BIT == 8);
19432 if (dwz->str.buffer[str_offset] == '\0')
19433 return NULL;
d521ce57 19434 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19435}
19436
43988095
JK
19437/* Return pointer to string at .debug_str offset as read from BUF.
19438 BUF is assumed to be in a compilation unit described by CU_HEADER.
19439 Return *BYTES_READ_PTR count of bytes read from BUF. */
19440
d521ce57
TT
19441static const char *
19442read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
19443 const struct comp_unit_head *cu_header,
19444 unsigned int *bytes_read_ptr)
19445{
19446 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19447
19448 return read_indirect_string_at_offset (abfd, str_offset);
19449}
19450
43988095
JK
19451/* Return pointer to string at .debug_line_str offset as read from BUF.
19452 BUF is assumed to be in a compilation unit described by CU_HEADER.
19453 Return *BYTES_READ_PTR count of bytes read from BUF. */
19454
19455static const char *
19456read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
19457 const struct comp_unit_head *cu_header,
19458 unsigned int *bytes_read_ptr)
19459{
19460 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19461
19462 return read_indirect_line_string_at_offset (abfd, str_offset);
19463}
19464
19465ULONGEST
d521ce57 19466read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19467 unsigned int *bytes_read_ptr)
c906108c 19468{
12df843f 19469 ULONGEST result;
ce5d95e1 19470 unsigned int num_read;
870f88f7 19471 int shift;
c906108c
SS
19472 unsigned char byte;
19473
19474 result = 0;
19475 shift = 0;
19476 num_read = 0;
c906108c
SS
19477 while (1)
19478 {
fe1b8b76 19479 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19480 buf++;
19481 num_read++;
12df843f 19482 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19483 if ((byte & 128) == 0)
19484 {
19485 break;
19486 }
19487 shift += 7;
19488 }
19489 *bytes_read_ptr = num_read;
19490 return result;
19491}
19492
12df843f 19493static LONGEST
d521ce57
TT
19494read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19495 unsigned int *bytes_read_ptr)
c906108c 19496{
12df843f 19497 LONGEST result;
870f88f7 19498 int shift, num_read;
c906108c
SS
19499 unsigned char byte;
19500
19501 result = 0;
19502 shift = 0;
c906108c 19503 num_read = 0;
c906108c
SS
19504 while (1)
19505 {
fe1b8b76 19506 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19507 buf++;
19508 num_read++;
12df843f 19509 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
19510 shift += 7;
19511 if ((byte & 128) == 0)
19512 {
19513 break;
19514 }
19515 }
77e0b926 19516 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 19517 result |= -(((LONGEST) 1) << shift);
c906108c
SS
19518 *bytes_read_ptr = num_read;
19519 return result;
19520}
19521
3019eac3
DE
19522/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19523 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19524 ADDR_SIZE is the size of addresses from the CU header. */
19525
19526static CORE_ADDR
19527read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
19528{
19529 struct objfile *objfile = dwarf2_per_objfile->objfile;
19530 bfd *abfd = objfile->obfd;
19531 const gdb_byte *info_ptr;
19532
19533 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19534 if (dwarf2_per_objfile->addr.buffer == NULL)
19535 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19536 objfile_name (objfile));
3019eac3
DE
19537 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19538 error (_("DW_FORM_addr_index pointing outside of "
19539 ".debug_addr section [in module %s]"),
4262abfb 19540 objfile_name (objfile));
3019eac3
DE
19541 info_ptr = (dwarf2_per_objfile->addr.buffer
19542 + addr_base + addr_index * addr_size);
19543 if (addr_size == 4)
19544 return bfd_get_32 (abfd, info_ptr);
19545 else
19546 return bfd_get_64 (abfd, info_ptr);
19547}
19548
19549/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19550
19551static CORE_ADDR
19552read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19553{
19554 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
19555}
19556
19557/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19558
19559static CORE_ADDR
d521ce57 19560read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19561 unsigned int *bytes_read)
19562{
19563 bfd *abfd = cu->objfile->obfd;
19564 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19565
19566 return read_addr_index (cu, addr_index);
19567}
19568
19569/* Data structure to pass results from dwarf2_read_addr_index_reader
19570 back to dwarf2_read_addr_index. */
19571
19572struct dwarf2_read_addr_index_data
19573{
19574 ULONGEST addr_base;
19575 int addr_size;
19576};
19577
19578/* die_reader_func for dwarf2_read_addr_index. */
19579
19580static void
19581dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19582 const gdb_byte *info_ptr,
3019eac3
DE
19583 struct die_info *comp_unit_die,
19584 int has_children,
19585 void *data)
19586{
19587 struct dwarf2_cu *cu = reader->cu;
19588 struct dwarf2_read_addr_index_data *aidata =
19589 (struct dwarf2_read_addr_index_data *) data;
19590
19591 aidata->addr_base = cu->addr_base;
19592 aidata->addr_size = cu->header.addr_size;
19593}
19594
19595/* Given an index in .debug_addr, fetch the value.
19596 NOTE: This can be called during dwarf expression evaluation,
19597 long after the debug information has been read, and thus per_cu->cu
19598 may no longer exist. */
19599
19600CORE_ADDR
19601dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19602 unsigned int addr_index)
19603{
19604 struct objfile *objfile = per_cu->objfile;
19605 struct dwarf2_cu *cu = per_cu->cu;
19606 ULONGEST addr_base;
19607 int addr_size;
19608
19609 /* This is intended to be called from outside this file. */
19610 dw2_setup (objfile);
19611
19612 /* We need addr_base and addr_size.
19613 If we don't have PER_CU->cu, we have to get it.
19614 Nasty, but the alternative is storing the needed info in PER_CU,
19615 which at this point doesn't seem justified: it's not clear how frequently
19616 it would get used and it would increase the size of every PER_CU.
19617 Entry points like dwarf2_per_cu_addr_size do a similar thing
19618 so we're not in uncharted territory here.
19619 Alas we need to be a bit more complicated as addr_base is contained
19620 in the DIE.
19621
19622 We don't need to read the entire CU(/TU).
19623 We just need the header and top level die.
a1b64ce1 19624
3019eac3 19625 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19626 For now we skip this optimization. */
3019eac3
DE
19627
19628 if (cu != NULL)
19629 {
19630 addr_base = cu->addr_base;
19631 addr_size = cu->header.addr_size;
19632 }
19633 else
19634 {
19635 struct dwarf2_read_addr_index_data aidata;
19636
a1b64ce1
DE
19637 /* Note: We can't use init_cutu_and_read_dies_simple here,
19638 we need addr_base. */
19639 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19640 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
19641 addr_base = aidata.addr_base;
19642 addr_size = aidata.addr_size;
19643 }
19644
19645 return read_addr_index_1 (addr_index, addr_base, addr_size);
19646}
19647
57d63ce2
DE
19648/* Given a DW_FORM_GNU_str_index, fetch the string.
19649 This is only used by the Fission support. */
3019eac3 19650
d521ce57 19651static const char *
342587c4 19652read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
19653{
19654 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19655 const char *objf_name = objfile_name (objfile);
3019eac3 19656 bfd *abfd = objfile->obfd;
342587c4 19657 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
19658 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19659 struct dwarf2_section_info *str_offsets_section =
19660 &reader->dwo_file->sections.str_offsets;
d521ce57 19661 const gdb_byte *info_ptr;
3019eac3 19662 ULONGEST str_offset;
57d63ce2 19663 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 19664
73869dc2
DE
19665 dwarf2_read_section (objfile, str_section);
19666 dwarf2_read_section (objfile, str_offsets_section);
19667 if (str_section->buffer == NULL)
57d63ce2 19668 error (_("%s used without .debug_str.dwo section"
9c541725
PA
19669 " in CU at offset 0x%x [in module %s]"),
19670 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19671 if (str_offsets_section->buffer == NULL)
57d63ce2 19672 error (_("%s used without .debug_str_offsets.dwo section"
9c541725
PA
19673 " in CU at offset 0x%x [in module %s]"),
19674 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19675 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 19676 error (_("%s pointing outside of .debug_str_offsets.dwo"
9c541725
PA
19677 " section in CU at offset 0x%x [in module %s]"),
19678 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19679 info_ptr = (str_offsets_section->buffer
3019eac3
DE
19680 + str_index * cu->header.offset_size);
19681 if (cu->header.offset_size == 4)
19682 str_offset = bfd_get_32 (abfd, info_ptr);
19683 else
19684 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19685 if (str_offset >= str_section->size)
57d63ce2 19686 error (_("Offset from %s pointing outside of"
9c541725
PA
19687 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
19688 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19689 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19690}
19691
3019eac3
DE
19692/* Return the length of an LEB128 number in BUF. */
19693
19694static int
19695leb128_size (const gdb_byte *buf)
19696{
19697 const gdb_byte *begin = buf;
19698 gdb_byte byte;
19699
19700 while (1)
19701 {
19702 byte = *buf++;
19703 if ((byte & 128) == 0)
19704 return buf - begin;
19705 }
19706}
19707
c906108c 19708static void
e142c38c 19709set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19710{
19711 switch (lang)
19712 {
19713 case DW_LANG_C89:
76bee0cc 19714 case DW_LANG_C99:
0cfd832f 19715 case DW_LANG_C11:
c906108c 19716 case DW_LANG_C:
d1be3247 19717 case DW_LANG_UPC:
e142c38c 19718 cu->language = language_c;
c906108c 19719 break;
9c37b5ae 19720 case DW_LANG_Java:
c906108c 19721 case DW_LANG_C_plus_plus:
0cfd832f
MW
19722 case DW_LANG_C_plus_plus_11:
19723 case DW_LANG_C_plus_plus_14:
e142c38c 19724 cu->language = language_cplus;
c906108c 19725 break;
6aecb9c2
JB
19726 case DW_LANG_D:
19727 cu->language = language_d;
19728 break;
c906108c
SS
19729 case DW_LANG_Fortran77:
19730 case DW_LANG_Fortran90:
b21b22e0 19731 case DW_LANG_Fortran95:
f7de9aab
MW
19732 case DW_LANG_Fortran03:
19733 case DW_LANG_Fortran08:
e142c38c 19734 cu->language = language_fortran;
c906108c 19735 break;
a766d390
DE
19736 case DW_LANG_Go:
19737 cu->language = language_go;
19738 break;
c906108c 19739 case DW_LANG_Mips_Assembler:
e142c38c 19740 cu->language = language_asm;
c906108c
SS
19741 break;
19742 case DW_LANG_Ada83:
8aaf0b47 19743 case DW_LANG_Ada95:
bc5f45f8
JB
19744 cu->language = language_ada;
19745 break;
72019c9c
GM
19746 case DW_LANG_Modula2:
19747 cu->language = language_m2;
19748 break;
fe8e67fd
PM
19749 case DW_LANG_Pascal83:
19750 cu->language = language_pascal;
19751 break;
22566fbd
DJ
19752 case DW_LANG_ObjC:
19753 cu->language = language_objc;
19754 break;
c44af4eb
TT
19755 case DW_LANG_Rust:
19756 case DW_LANG_Rust_old:
19757 cu->language = language_rust;
19758 break;
c906108c
SS
19759 case DW_LANG_Cobol74:
19760 case DW_LANG_Cobol85:
c906108c 19761 default:
e142c38c 19762 cu->language = language_minimal;
c906108c
SS
19763 break;
19764 }
e142c38c 19765 cu->language_defn = language_def (cu->language);
c906108c
SS
19766}
19767
19768/* Return the named attribute or NULL if not there. */
19769
19770static struct attribute *
e142c38c 19771dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19772{
a48e046c 19773 for (;;)
c906108c 19774 {
a48e046c
TT
19775 unsigned int i;
19776 struct attribute *spec = NULL;
19777
19778 for (i = 0; i < die->num_attrs; ++i)
19779 {
19780 if (die->attrs[i].name == name)
19781 return &die->attrs[i];
19782 if (die->attrs[i].name == DW_AT_specification
19783 || die->attrs[i].name == DW_AT_abstract_origin)
19784 spec = &die->attrs[i];
19785 }
19786
19787 if (!spec)
19788 break;
c906108c 19789
f2f0e013 19790 die = follow_die_ref (die, spec, &cu);
f2f0e013 19791 }
c5aa993b 19792
c906108c
SS
19793 return NULL;
19794}
19795
348e048f
DE
19796/* Return the named attribute or NULL if not there,
19797 but do not follow DW_AT_specification, etc.
19798 This is for use in contexts where we're reading .debug_types dies.
19799 Following DW_AT_specification, DW_AT_abstract_origin will take us
19800 back up the chain, and we want to go down. */
19801
19802static struct attribute *
45e58e77 19803dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19804{
19805 unsigned int i;
19806
19807 for (i = 0; i < die->num_attrs; ++i)
19808 if (die->attrs[i].name == name)
19809 return &die->attrs[i];
19810
19811 return NULL;
19812}
19813
7d45c7c3
KB
19814/* Return the string associated with a string-typed attribute, or NULL if it
19815 is either not found or is of an incorrect type. */
19816
19817static const char *
19818dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19819{
19820 struct attribute *attr;
19821 const char *str = NULL;
19822
19823 attr = dwarf2_attr (die, name, cu);
19824
19825 if (attr != NULL)
19826 {
43988095 19827 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
19828 || attr->form == DW_FORM_string
19829 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19830 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19831 str = DW_STRING (attr);
19832 else
19833 complaint (&symfile_complaints,
19834 _("string type expected for attribute %s for "
19835 "DIE at 0x%x in module %s"),
9c541725 19836 dwarf_attr_name (name), to_underlying (die->sect_off),
7d45c7c3
KB
19837 objfile_name (cu->objfile));
19838 }
19839
19840 return str;
19841}
19842
05cf31d1
JB
19843/* Return non-zero iff the attribute NAME is defined for the given DIE,
19844 and holds a non-zero value. This function should only be used for
2dc7f7b3 19845 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19846
19847static int
19848dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19849{
19850 struct attribute *attr = dwarf2_attr (die, name, cu);
19851
19852 return (attr && DW_UNSND (attr));
19853}
19854
3ca72b44 19855static int
e142c38c 19856die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19857{
05cf31d1
JB
19858 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19859 which value is non-zero. However, we have to be careful with
19860 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19861 (via dwarf2_flag_true_p) follows this attribute. So we may
19862 end up accidently finding a declaration attribute that belongs
19863 to a different DIE referenced by the specification attribute,
19864 even though the given DIE does not have a declaration attribute. */
19865 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19866 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19867}
19868
63d06c5c 19869/* Return the die giving the specification for DIE, if there is
f2f0e013 19870 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19871 containing the return value on output. If there is no
19872 specification, but there is an abstract origin, that is
19873 returned. */
63d06c5c
DC
19874
19875static struct die_info *
f2f0e013 19876die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19877{
f2f0e013
DJ
19878 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19879 *spec_cu);
63d06c5c 19880
edb3359d
DJ
19881 if (spec_attr == NULL)
19882 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19883
63d06c5c
DC
19884 if (spec_attr == NULL)
19885 return NULL;
19886 else
f2f0e013 19887 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19888}
c906108c 19889
527f3840
JK
19890/* Stub for free_line_header to match void * callback types. */
19891
19892static void
19893free_line_header_voidp (void *arg)
19894{
9a3c8263 19895 struct line_header *lh = (struct line_header *) arg;
527f3840 19896
fff8551c 19897 delete lh;
527f3840
JK
19898}
19899
fff8551c
PA
19900void
19901line_header::add_include_dir (const char *include_dir)
c906108c 19902{
27e0867f 19903 if (dwarf_line_debug >= 2)
fff8551c
PA
19904 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19905 include_dirs.size () + 1, include_dir);
27e0867f 19906
fff8551c 19907 include_dirs.push_back (include_dir);
debd256d 19908}
6e70227d 19909
fff8551c
PA
19910void
19911line_header::add_file_name (const char *name,
ecfb656c 19912 dir_index d_index,
fff8551c
PA
19913 unsigned int mod_time,
19914 unsigned int length)
debd256d 19915{
27e0867f
DE
19916 if (dwarf_line_debug >= 2)
19917 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 19918 (unsigned) file_names.size () + 1, name);
27e0867f 19919
ecfb656c 19920 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 19921}
6e70227d 19922
83769d0b 19923/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19924
19925static struct dwarf2_section_info *
19926get_debug_line_section (struct dwarf2_cu *cu)
19927{
19928 struct dwarf2_section_info *section;
19929
19930 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19931 DWO file. */
19932 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19933 section = &cu->dwo_unit->dwo_file->sections.line;
19934 else if (cu->per_cu->is_dwz)
19935 {
19936 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19937
19938 section = &dwz->line;
19939 }
19940 else
19941 section = &dwarf2_per_objfile->line;
19942
19943 return section;
19944}
19945
43988095
JK
19946/* Read directory or file name entry format, starting with byte of
19947 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19948 entries count and the entries themselves in the described entry
19949 format. */
19950
19951static void
19952read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
19953 struct line_header *lh,
19954 const struct comp_unit_head *cu_header,
19955 void (*callback) (struct line_header *lh,
19956 const char *name,
ecfb656c 19957 dir_index d_index,
43988095
JK
19958 unsigned int mod_time,
19959 unsigned int length))
19960{
19961 gdb_byte format_count, formati;
19962 ULONGEST data_count, datai;
19963 const gdb_byte *buf = *bufp;
19964 const gdb_byte *format_header_data;
43988095
JK
19965 unsigned int bytes_read;
19966
19967 format_count = read_1_byte (abfd, buf);
19968 buf += 1;
19969 format_header_data = buf;
19970 for (formati = 0; formati < format_count; formati++)
19971 {
19972 read_unsigned_leb128 (abfd, buf, &bytes_read);
19973 buf += bytes_read;
19974 read_unsigned_leb128 (abfd, buf, &bytes_read);
19975 buf += bytes_read;
19976 }
19977
19978 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19979 buf += bytes_read;
19980 for (datai = 0; datai < data_count; datai++)
19981 {
19982 const gdb_byte *format = format_header_data;
19983 struct file_entry fe;
19984
43988095
JK
19985 for (formati = 0; formati < format_count; formati++)
19986 {
ecfb656c 19987 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19988 format += bytes_read;
43988095 19989
ecfb656c 19990 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19991 format += bytes_read;
ecfb656c
PA
19992
19993 gdb::optional<const char *> string;
19994 gdb::optional<unsigned int> uint;
19995
43988095
JK
19996 switch (form)
19997 {
19998 case DW_FORM_string:
ecfb656c 19999 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20000 buf += bytes_read;
20001 break;
20002
20003 case DW_FORM_line_strp:
ecfb656c
PA
20004 string.emplace (read_indirect_line_string (abfd, buf,
20005 cu_header,
20006 &bytes_read));
43988095
JK
20007 buf += bytes_read;
20008 break;
20009
20010 case DW_FORM_data1:
ecfb656c 20011 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20012 buf += 1;
20013 break;
20014
20015 case DW_FORM_data2:
ecfb656c 20016 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20017 buf += 2;
20018 break;
20019
20020 case DW_FORM_data4:
ecfb656c 20021 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20022 buf += 4;
20023 break;
20024
20025 case DW_FORM_data8:
ecfb656c 20026 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20027 buf += 8;
20028 break;
20029
20030 case DW_FORM_udata:
ecfb656c 20031 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20032 buf += bytes_read;
20033 break;
20034
20035 case DW_FORM_block:
20036 /* It is valid only for DW_LNCT_timestamp which is ignored by
20037 current GDB. */
20038 break;
20039 }
ecfb656c
PA
20040
20041 switch (content_type)
20042 {
20043 case DW_LNCT_path:
20044 if (string.has_value ())
20045 fe.name = *string;
20046 break;
20047 case DW_LNCT_directory_index:
20048 if (uint.has_value ())
20049 fe.d_index = (dir_index) *uint;
20050 break;
20051 case DW_LNCT_timestamp:
20052 if (uint.has_value ())
20053 fe.mod_time = *uint;
20054 break;
20055 case DW_LNCT_size:
20056 if (uint.has_value ())
20057 fe.length = *uint;
20058 break;
20059 case DW_LNCT_MD5:
20060 break;
20061 default:
20062 complaint (&symfile_complaints,
20063 _("Unknown format content type %s"),
20064 pulongest (content_type));
20065 }
43988095
JK
20066 }
20067
ecfb656c 20068 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20069 }
20070
20071 *bufp = buf;
20072}
20073
debd256d 20074/* Read the statement program header starting at OFFSET in
3019eac3 20075 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20076 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20077 Returns NULL if there is a problem reading the header, e.g., if it
20078 has a version we don't understand.
debd256d
JB
20079
20080 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20081 the returned object point into the dwarf line section buffer,
20082 and must not be freed. */
ae2de4f8 20083
fff8551c 20084static line_header_up
9c541725 20085dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20086{
d521ce57 20087 const gdb_byte *line_ptr;
c764a876 20088 unsigned int bytes_read, offset_size;
debd256d 20089 int i;
d521ce57 20090 const char *cur_dir, *cur_file;
3019eac3
DE
20091 struct dwarf2_section_info *section;
20092 bfd *abfd;
20093
36586728 20094 section = get_debug_line_section (cu);
3019eac3
DE
20095 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20096 if (section->buffer == NULL)
debd256d 20097 {
3019eac3
DE
20098 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20099 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20100 else
20101 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
20102 return 0;
20103 }
20104
fceca515
DE
20105 /* We can't do this until we know the section is non-empty.
20106 Only then do we know we have such a section. */
a32a8923 20107 abfd = get_section_bfd_owner (section);
fceca515 20108
a738430d
MK
20109 /* Make sure that at least there's room for the total_length field.
20110 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20111 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20112 {
4d3c2250 20113 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20114 return 0;
20115 }
20116
fff8551c 20117 line_header_up lh (new line_header ());
debd256d 20118
9c541725 20119 lh->sect_off = sect_off;
527f3840
JK
20120 lh->offset_in_dwz = cu->per_cu->is_dwz;
20121
9c541725 20122 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20123
a738430d 20124 /* Read in the header. */
6e70227d 20125 lh->total_length =
c764a876
DE
20126 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20127 &bytes_read, &offset_size);
debd256d 20128 line_ptr += bytes_read;
3019eac3 20129 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20130 {
4d3c2250 20131 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20132 return 0;
20133 }
20134 lh->statement_program_end = line_ptr + lh->total_length;
20135 lh->version = read_2_bytes (abfd, line_ptr);
20136 line_ptr += 2;
43988095 20137 if (lh->version > 5)
cd366ee8
DE
20138 {
20139 /* This is a version we don't understand. The format could have
20140 changed in ways we don't handle properly so just punt. */
20141 complaint (&symfile_complaints,
20142 _("unsupported version in .debug_line section"));
20143 return NULL;
20144 }
43988095
JK
20145 if (lh->version >= 5)
20146 {
20147 gdb_byte segment_selector_size;
20148
20149 /* Skip address size. */
20150 read_1_byte (abfd, line_ptr);
20151 line_ptr += 1;
20152
20153 segment_selector_size = read_1_byte (abfd, line_ptr);
20154 line_ptr += 1;
20155 if (segment_selector_size != 0)
20156 {
20157 complaint (&symfile_complaints,
20158 _("unsupported segment selector size %u "
20159 "in .debug_line section"),
20160 segment_selector_size);
20161 return NULL;
20162 }
20163 }
c764a876
DE
20164 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20165 line_ptr += offset_size;
debd256d
JB
20166 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20167 line_ptr += 1;
2dc7f7b3
TT
20168 if (lh->version >= 4)
20169 {
20170 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20171 line_ptr += 1;
20172 }
20173 else
20174 lh->maximum_ops_per_instruction = 1;
20175
20176 if (lh->maximum_ops_per_instruction == 0)
20177 {
20178 lh->maximum_ops_per_instruction = 1;
20179 complaint (&symfile_complaints,
3e43a32a
MS
20180 _("invalid maximum_ops_per_instruction "
20181 "in `.debug_line' section"));
2dc7f7b3
TT
20182 }
20183
debd256d
JB
20184 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20185 line_ptr += 1;
20186 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20187 line_ptr += 1;
20188 lh->line_range = read_1_byte (abfd, line_ptr);
20189 line_ptr += 1;
20190 lh->opcode_base = read_1_byte (abfd, line_ptr);
20191 line_ptr += 1;
fff8551c 20192 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20193
20194 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20195 for (i = 1; i < lh->opcode_base; ++i)
20196 {
20197 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20198 line_ptr += 1;
20199 }
20200
43988095 20201 if (lh->version >= 5)
debd256d 20202 {
43988095 20203 /* Read directory table. */
fff8551c
PA
20204 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
20205 [] (struct line_header *lh, const char *name,
ecfb656c 20206 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20207 unsigned int length)
20208 {
20209 lh->add_include_dir (name);
20210 });
debd256d 20211
43988095 20212 /* Read file name table. */
fff8551c
PA
20213 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
20214 [] (struct line_header *lh, const char *name,
ecfb656c 20215 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20216 unsigned int length)
20217 {
ecfb656c 20218 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 20219 });
43988095
JK
20220 }
20221 else
debd256d 20222 {
43988095
JK
20223 /* Read directory table. */
20224 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20225 {
20226 line_ptr += bytes_read;
fff8551c 20227 lh->add_include_dir (cur_dir);
43988095 20228 }
debd256d
JB
20229 line_ptr += bytes_read;
20230
43988095
JK
20231 /* Read file name table. */
20232 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20233 {
ecfb656c
PA
20234 unsigned int mod_time, length;
20235 dir_index d_index;
43988095
JK
20236
20237 line_ptr += bytes_read;
ecfb656c 20238 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20239 line_ptr += bytes_read;
20240 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20241 line_ptr += bytes_read;
20242 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20243 line_ptr += bytes_read;
20244
ecfb656c 20245 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20246 }
20247 line_ptr += bytes_read;
debd256d 20248 }
6e70227d 20249 lh->statement_program_start = line_ptr;
debd256d 20250
3019eac3 20251 if (line_ptr > (section->buffer + section->size))
4d3c2250 20252 complaint (&symfile_complaints,
3e43a32a
MS
20253 _("line number info header doesn't "
20254 "fit in `.debug_line' section"));
debd256d 20255
debd256d
JB
20256 return lh;
20257}
c906108c 20258
c6da4cef
DE
20259/* Subroutine of dwarf_decode_lines to simplify it.
20260 Return the file name of the psymtab for included file FILE_INDEX
20261 in line header LH of PST.
20262 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20263 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
20264 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
20265
20266 The function creates dangling cleanup registration. */
c6da4cef 20267
d521ce57 20268static const char *
c6da4cef
DE
20269psymtab_include_file_name (const struct line_header *lh, int file_index,
20270 const struct partial_symtab *pst,
20271 const char *comp_dir)
20272{
8c43009f 20273 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20274 const char *include_name = fe.name;
20275 const char *include_name_to_compare = include_name;
72b9f47f
TT
20276 const char *pst_filename;
20277 char *copied_name = NULL;
c6da4cef
DE
20278 int file_is_pst;
20279
8c43009f 20280 const char *dir_name = fe.include_dir (lh);
c6da4cef
DE
20281
20282 if (!IS_ABSOLUTE_PATH (include_name)
20283 && (dir_name != NULL || comp_dir != NULL))
20284 {
20285 /* Avoid creating a duplicate psymtab for PST.
20286 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20287 Before we do the comparison, however, we need to account
20288 for DIR_NAME and COMP_DIR.
20289 First prepend dir_name (if non-NULL). If we still don't
20290 have an absolute path prepend comp_dir (if non-NULL).
20291 However, the directory we record in the include-file's
20292 psymtab does not contain COMP_DIR (to match the
20293 corresponding symtab(s)).
20294
20295 Example:
20296
20297 bash$ cd /tmp
20298 bash$ gcc -g ./hello.c
20299 include_name = "hello.c"
20300 dir_name = "."
20301 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20302 DW_AT_name = "./hello.c"
20303
20304 */
c6da4cef
DE
20305
20306 if (dir_name != NULL)
20307 {
d521ce57
TT
20308 char *tem = concat (dir_name, SLASH_STRING,
20309 include_name, (char *)NULL);
20310
20311 make_cleanup (xfree, tem);
20312 include_name = tem;
c6da4cef 20313 include_name_to_compare = include_name;
c6da4cef
DE
20314 }
20315 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20316 {
d521ce57
TT
20317 char *tem = concat (comp_dir, SLASH_STRING,
20318 include_name, (char *)NULL);
20319
20320 make_cleanup (xfree, tem);
20321 include_name_to_compare = tem;
c6da4cef
DE
20322 }
20323 }
20324
20325 pst_filename = pst->filename;
20326 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20327 {
72b9f47f
TT
20328 copied_name = concat (pst->dirname, SLASH_STRING,
20329 pst_filename, (char *)NULL);
20330 pst_filename = copied_name;
c6da4cef
DE
20331 }
20332
1e3fad37 20333 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20334
72b9f47f
TT
20335 if (copied_name != NULL)
20336 xfree (copied_name);
c6da4cef
DE
20337
20338 if (file_is_pst)
20339 return NULL;
20340 return include_name;
20341}
20342
d9b3de22
DE
20343/* State machine to track the state of the line number program. */
20344
6f77053d 20345class lnp_state_machine
d9b3de22 20346{
6f77053d
PA
20347public:
20348 /* Initialize a machine state for the start of a line number
20349 program. */
20350 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20351
8c43009f
PA
20352 file_entry *current_file ()
20353 {
20354 /* lh->file_names is 0-based, but the file name numbers in the
20355 statement program are 1-based. */
6f77053d
PA
20356 return m_line_header->file_name_at (m_file);
20357 }
20358
20359 /* Record the line in the state machine. END_SEQUENCE is true if
20360 we're processing the end of a sequence. */
20361 void record_line (bool end_sequence);
20362
20363 /* Check address and if invalid nop-out the rest of the lines in this
20364 sequence. */
20365 void check_line_address (struct dwarf2_cu *cu,
20366 const gdb_byte *line_ptr,
20367 CORE_ADDR lowpc, CORE_ADDR address);
20368
20369 void handle_set_discriminator (unsigned int discriminator)
20370 {
20371 m_discriminator = discriminator;
20372 m_line_has_non_zero_discriminator |= discriminator != 0;
20373 }
20374
20375 /* Handle DW_LNE_set_address. */
20376 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20377 {
20378 m_op_index = 0;
20379 address += baseaddr;
20380 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20381 }
20382
20383 /* Handle DW_LNS_advance_pc. */
20384 void handle_advance_pc (CORE_ADDR adjust);
20385
20386 /* Handle a special opcode. */
20387 void handle_special_opcode (unsigned char op_code);
20388
20389 /* Handle DW_LNS_advance_line. */
20390 void handle_advance_line (int line_delta)
20391 {
20392 advance_line (line_delta);
20393 }
20394
20395 /* Handle DW_LNS_set_file. */
20396 void handle_set_file (file_name_index file);
20397
20398 /* Handle DW_LNS_negate_stmt. */
20399 void handle_negate_stmt ()
20400 {
20401 m_is_stmt = !m_is_stmt;
20402 }
20403
20404 /* Handle DW_LNS_const_add_pc. */
20405 void handle_const_add_pc ();
20406
20407 /* Handle DW_LNS_fixed_advance_pc. */
20408 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20409 {
20410 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20411 m_op_index = 0;
20412 }
20413
20414 /* Handle DW_LNS_copy. */
20415 void handle_copy ()
20416 {
20417 record_line (false);
20418 m_discriminator = 0;
20419 }
20420
20421 /* Handle DW_LNE_end_sequence. */
20422 void handle_end_sequence ()
20423 {
20424 m_record_line_callback = ::record_line;
20425 }
20426
20427private:
20428 /* Advance the line by LINE_DELTA. */
20429 void advance_line (int line_delta)
20430 {
20431 m_line += line_delta;
20432
20433 if (line_delta != 0)
20434 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20435 }
20436
6f77053d
PA
20437 gdbarch *m_gdbarch;
20438
20439 /* True if we're recording lines.
20440 Otherwise we're building partial symtabs and are just interested in
20441 finding include files mentioned by the line number program. */
20442 bool m_record_lines_p;
20443
8c43009f 20444 /* The line number header. */
6f77053d 20445 line_header *m_line_header;
8c43009f 20446
6f77053d
PA
20447 /* These are part of the standard DWARF line number state machine,
20448 and initialized according to the DWARF spec. */
d9b3de22 20449
6f77053d 20450 unsigned char m_op_index = 0;
8c43009f 20451 /* The line table index (1-based) of the current file. */
6f77053d
PA
20452 file_name_index m_file = (file_name_index) 1;
20453 unsigned int m_line = 1;
20454
20455 /* These are initialized in the constructor. */
20456
20457 CORE_ADDR m_address;
20458 bool m_is_stmt;
20459 unsigned int m_discriminator;
d9b3de22
DE
20460
20461 /* Additional bits of state we need to track. */
20462
20463 /* The last file that we called dwarf2_start_subfile for.
20464 This is only used for TLLs. */
6f77053d 20465 unsigned int m_last_file = 0;
d9b3de22 20466 /* The last file a line number was recorded for. */
6f77053d 20467 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
20468
20469 /* The function to call to record a line. */
6f77053d 20470 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
20471
20472 /* The last line number that was recorded, used to coalesce
20473 consecutive entries for the same line. This can happen, for
20474 example, when discriminators are present. PR 17276. */
6f77053d
PA
20475 unsigned int m_last_line = 0;
20476 bool m_line_has_non_zero_discriminator = false;
8c43009f 20477};
d9b3de22 20478
6f77053d
PA
20479void
20480lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20481{
20482 CORE_ADDR addr_adj = (((m_op_index + adjust)
20483 / m_line_header->maximum_ops_per_instruction)
20484 * m_line_header->minimum_instruction_length);
20485 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20486 m_op_index = ((m_op_index + adjust)
20487 % m_line_header->maximum_ops_per_instruction);
20488}
d9b3de22 20489
6f77053d
PA
20490void
20491lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20492{
6f77053d
PA
20493 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20494 CORE_ADDR addr_adj = (((m_op_index
20495 + (adj_opcode / m_line_header->line_range))
20496 / m_line_header->maximum_ops_per_instruction)
20497 * m_line_header->minimum_instruction_length);
20498 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20499 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20500 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20501
6f77053d
PA
20502 int line_delta = (m_line_header->line_base
20503 + (adj_opcode % m_line_header->line_range));
20504 advance_line (line_delta);
20505 record_line (false);
20506 m_discriminator = 0;
20507}
d9b3de22 20508
6f77053d
PA
20509void
20510lnp_state_machine::handle_set_file (file_name_index file)
20511{
20512 m_file = file;
20513
20514 const file_entry *fe = current_file ();
20515 if (fe == NULL)
20516 dwarf2_debug_line_missing_file_complaint ();
20517 else if (m_record_lines_p)
20518 {
20519 const char *dir = fe->include_dir (m_line_header);
20520
20521 m_last_subfile = current_subfile;
20522 m_line_has_non_zero_discriminator = m_discriminator != 0;
20523 dwarf2_start_subfile (fe->name, dir);
20524 }
20525}
20526
20527void
20528lnp_state_machine::handle_const_add_pc ()
20529{
20530 CORE_ADDR adjust
20531 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20532
20533 CORE_ADDR addr_adj
20534 = (((m_op_index + adjust)
20535 / m_line_header->maximum_ops_per_instruction)
20536 * m_line_header->minimum_instruction_length);
20537
20538 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20539 m_op_index = ((m_op_index + adjust)
20540 % m_line_header->maximum_ops_per_instruction);
20541}
d9b3de22 20542
c91513d8
PP
20543/* Ignore this record_line request. */
20544
20545static void
20546noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20547{
20548 return;
20549}
20550
a05a36a5
DE
20551/* Return non-zero if we should add LINE to the line number table.
20552 LINE is the line to add, LAST_LINE is the last line that was added,
20553 LAST_SUBFILE is the subfile for LAST_LINE.
20554 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20555 had a non-zero discriminator.
20556
20557 We have to be careful in the presence of discriminators.
20558 E.g., for this line:
20559
20560 for (i = 0; i < 100000; i++);
20561
20562 clang can emit four line number entries for that one line,
20563 each with a different discriminator.
20564 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20565
20566 However, we want gdb to coalesce all four entries into one.
20567 Otherwise the user could stepi into the middle of the line and
20568 gdb would get confused about whether the pc really was in the
20569 middle of the line.
20570
20571 Things are further complicated by the fact that two consecutive
20572 line number entries for the same line is a heuristic used by gcc
20573 to denote the end of the prologue. So we can't just discard duplicate
20574 entries, we have to be selective about it. The heuristic we use is
20575 that we only collapse consecutive entries for the same line if at least
20576 one of those entries has a non-zero discriminator. PR 17276.
20577
20578 Note: Addresses in the line number state machine can never go backwards
20579 within one sequence, thus this coalescing is ok. */
20580
20581static int
20582dwarf_record_line_p (unsigned int line, unsigned int last_line,
20583 int line_has_non_zero_discriminator,
20584 struct subfile *last_subfile)
20585{
20586 if (current_subfile != last_subfile)
20587 return 1;
20588 if (line != last_line)
20589 return 1;
20590 /* Same line for the same file that we've seen already.
20591 As a last check, for pr 17276, only record the line if the line
20592 has never had a non-zero discriminator. */
20593 if (!line_has_non_zero_discriminator)
20594 return 1;
20595 return 0;
20596}
20597
252a6764
DE
20598/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20599 in the line table of subfile SUBFILE. */
20600
20601static void
d9b3de22
DE
20602dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20603 unsigned int line, CORE_ADDR address,
20604 record_line_ftype p_record_line)
252a6764
DE
20605{
20606 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20607
27e0867f
DE
20608 if (dwarf_line_debug)
20609 {
20610 fprintf_unfiltered (gdb_stdlog,
20611 "Recording line %u, file %s, address %s\n",
20612 line, lbasename (subfile->name),
20613 paddress (gdbarch, address));
20614 }
20615
d5962de5 20616 (*p_record_line) (subfile, line, addr);
252a6764
DE
20617}
20618
20619/* Subroutine of dwarf_decode_lines_1 to simplify it.
20620 Mark the end of a set of line number records.
d9b3de22 20621 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20622 If SUBFILE is NULL the request is ignored. */
20623
20624static void
20625dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20626 CORE_ADDR address, record_line_ftype p_record_line)
20627{
27e0867f
DE
20628 if (subfile == NULL)
20629 return;
20630
20631 if (dwarf_line_debug)
20632 {
20633 fprintf_unfiltered (gdb_stdlog,
20634 "Finishing current line, file %s, address %s\n",
20635 lbasename (subfile->name),
20636 paddress (gdbarch, address));
20637 }
20638
d9b3de22
DE
20639 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20640}
20641
6f77053d
PA
20642void
20643lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20644{
d9b3de22
DE
20645 if (dwarf_line_debug)
20646 {
20647 fprintf_unfiltered (gdb_stdlog,
20648 "Processing actual line %u: file %u,"
20649 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
20650 m_line, to_underlying (m_file),
20651 paddress (m_gdbarch, m_address),
20652 m_is_stmt, m_discriminator);
d9b3de22
DE
20653 }
20654
6f77053d 20655 file_entry *fe = current_file ();
8c43009f
PA
20656
20657 if (fe == NULL)
d9b3de22
DE
20658 dwarf2_debug_line_missing_file_complaint ();
20659 /* For now we ignore lines not starting on an instruction boundary.
20660 But not when processing end_sequence for compatibility with the
20661 previous version of the code. */
6f77053d 20662 else if (m_op_index == 0 || end_sequence)
d9b3de22 20663 {
8c43009f 20664 fe->included_p = 1;
6f77053d 20665 if (m_record_lines_p && m_is_stmt)
d9b3de22 20666 {
6f77053d 20667 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 20668 {
6f77053d
PA
20669 dwarf_finish_line (m_gdbarch, m_last_subfile,
20670 m_address, m_record_line_callback);
d9b3de22
DE
20671 }
20672
20673 if (!end_sequence)
20674 {
6f77053d
PA
20675 if (dwarf_record_line_p (m_line, m_last_line,
20676 m_line_has_non_zero_discriminator,
20677 m_last_subfile))
d9b3de22 20678 {
6f77053d
PA
20679 dwarf_record_line_1 (m_gdbarch, current_subfile,
20680 m_line, m_address,
20681 m_record_line_callback);
d9b3de22 20682 }
6f77053d
PA
20683 m_last_subfile = current_subfile;
20684 m_last_line = m_line;
d9b3de22
DE
20685 }
20686 }
20687 }
20688}
20689
6f77053d
PA
20690lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20691 bool record_lines_p)
d9b3de22 20692{
6f77053d
PA
20693 m_gdbarch = arch;
20694 m_record_lines_p = record_lines_p;
20695 m_line_header = lh;
d9b3de22 20696
6f77053d 20697 m_record_line_callback = ::record_line;
d9b3de22 20698
d9b3de22
DE
20699 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20700 was a line entry for it so that the backend has a chance to adjust it
20701 and also record it in case it needs it. This is currently used by MIPS
20702 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20703 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20704 m_is_stmt = lh->default_is_stmt;
20705 m_discriminator = 0;
252a6764
DE
20706}
20707
6f77053d
PA
20708void
20709lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20710 const gdb_byte *line_ptr,
20711 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
20712{
20713 /* If address < lowpc then it's not a usable value, it's outside the
20714 pc range of the CU. However, we restrict the test to only address
20715 values of zero to preserve GDB's previous behaviour which is to
20716 handle the specific case of a function being GC'd by the linker. */
20717
20718 if (address == 0 && address < lowpc)
20719 {
20720 /* This line table is for a function which has been
20721 GCd by the linker. Ignore it. PR gdb/12528 */
20722
20723 struct objfile *objfile = cu->objfile;
20724 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20725
20726 complaint (&symfile_complaints,
20727 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20728 line_offset, objfile_name (objfile));
6f77053d
PA
20729 m_record_line_callback = noop_record_line;
20730 /* Note: record_line_callback is left as noop_record_line until
20731 we see DW_LNE_end_sequence. */
924c2928
DE
20732 }
20733}
20734
f3f5162e 20735/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20736 Process the line number information in LH.
20737 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20738 program in order to set included_p for every referenced header. */
debd256d 20739
c906108c 20740static void
43f3e411
DE
20741dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20742 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20743{
d521ce57
TT
20744 const gdb_byte *line_ptr, *extended_end;
20745 const gdb_byte *line_end;
a8c50c1f 20746 unsigned int bytes_read, extended_len;
699ca60a 20747 unsigned char op_code, extended_op;
e142c38c
DJ
20748 CORE_ADDR baseaddr;
20749 struct objfile *objfile = cu->objfile;
f3f5162e 20750 bfd *abfd = objfile->obfd;
fbf65064 20751 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20752 /* True if we're recording line info (as opposed to building partial
20753 symtabs and just interested in finding include files mentioned by
20754 the line number program). */
20755 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
20756
20757 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 20758
debd256d
JB
20759 line_ptr = lh->statement_program_start;
20760 line_end = lh->statement_program_end;
c906108c
SS
20761
20762 /* Read the statement sequences until there's nothing left. */
20763 while (line_ptr < line_end)
20764 {
6f77053d
PA
20765 /* The DWARF line number program state machine. Reset the state
20766 machine at the start of each sequence. */
20767 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20768 bool end_sequence = false;
d9b3de22 20769
8c43009f 20770 if (record_lines_p)
c906108c 20771 {
8c43009f
PA
20772 /* Start a subfile for the current file of the state
20773 machine. */
20774 const file_entry *fe = state_machine.current_file ();
20775
20776 if (fe != NULL)
20777 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
20778 }
20779
a738430d 20780 /* Decode the table. */
d9b3de22 20781 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20782 {
20783 op_code = read_1_byte (abfd, line_ptr);
20784 line_ptr += 1;
9aa1fe7e 20785
debd256d 20786 if (op_code >= lh->opcode_base)
6e70227d 20787 {
8e07a239 20788 /* Special opcode. */
6f77053d 20789 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20790 }
20791 else switch (op_code)
c906108c
SS
20792 {
20793 case DW_LNS_extended_op:
3e43a32a
MS
20794 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20795 &bytes_read);
473b7be6 20796 line_ptr += bytes_read;
a8c50c1f 20797 extended_end = line_ptr + extended_len;
c906108c
SS
20798 extended_op = read_1_byte (abfd, line_ptr);
20799 line_ptr += 1;
20800 switch (extended_op)
20801 {
20802 case DW_LNE_end_sequence:
6f77053d
PA
20803 state_machine.handle_end_sequence ();
20804 end_sequence = true;
c906108c
SS
20805 break;
20806 case DW_LNE_set_address:
d9b3de22
DE
20807 {
20808 CORE_ADDR address
20809 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 20810 line_ptr += bytes_read;
6f77053d
PA
20811
20812 state_machine.check_line_address (cu, line_ptr,
20813 lowpc, address);
20814 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20815 }
c906108c
SS
20816 break;
20817 case DW_LNE_define_file:
debd256d 20818 {
d521ce57 20819 const char *cur_file;
ecfb656c
PA
20820 unsigned int mod_time, length;
20821 dir_index dindex;
6e70227d 20822
3e43a32a
MS
20823 cur_file = read_direct_string (abfd, line_ptr,
20824 &bytes_read);
debd256d 20825 line_ptr += bytes_read;
ecfb656c 20826 dindex = (dir_index)
debd256d
JB
20827 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20828 line_ptr += bytes_read;
20829 mod_time =
20830 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20831 line_ptr += bytes_read;
20832 length =
20833 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20834 line_ptr += bytes_read;
ecfb656c 20835 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20836 }
c906108c 20837 break;
d0c6ba3d 20838 case DW_LNE_set_discriminator:
6f77053d
PA
20839 {
20840 /* The discriminator is not interesting to the
20841 debugger; just ignore it. We still need to
20842 check its value though:
20843 if there are consecutive entries for the same
20844 (non-prologue) line we want to coalesce them.
20845 PR 17276. */
20846 unsigned int discr
20847 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20848 line_ptr += bytes_read;
20849
20850 state_machine.handle_set_discriminator (discr);
20851 }
d0c6ba3d 20852 break;
c906108c 20853 default:
4d3c2250 20854 complaint (&symfile_complaints,
e2e0b3e5 20855 _("mangled .debug_line section"));
debd256d 20856 return;
c906108c 20857 }
a8c50c1f
DJ
20858 /* Make sure that we parsed the extended op correctly. If e.g.
20859 we expected a different address size than the producer used,
20860 we may have read the wrong number of bytes. */
20861 if (line_ptr != extended_end)
20862 {
20863 complaint (&symfile_complaints,
20864 _("mangled .debug_line section"));
20865 return;
20866 }
c906108c
SS
20867 break;
20868 case DW_LNS_copy:
6f77053d 20869 state_machine.handle_copy ();
c906108c
SS
20870 break;
20871 case DW_LNS_advance_pc:
2dc7f7b3
TT
20872 {
20873 CORE_ADDR adjust
20874 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20875 line_ptr += bytes_read;
6f77053d
PA
20876
20877 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20878 }
c906108c
SS
20879 break;
20880 case DW_LNS_advance_line:
a05a36a5
DE
20881 {
20882 int line_delta
20883 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20884 line_ptr += bytes_read;
6f77053d
PA
20885
20886 state_machine.handle_advance_line (line_delta);
a05a36a5 20887 }
c906108c
SS
20888 break;
20889 case DW_LNS_set_file:
d9b3de22 20890 {
6f77053d 20891 file_name_index file
ecfb656c
PA
20892 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20893 &bytes_read);
d9b3de22 20894 line_ptr += bytes_read;
8c43009f 20895
6f77053d 20896 state_machine.handle_set_file (file);
d9b3de22 20897 }
c906108c
SS
20898 break;
20899 case DW_LNS_set_column:
0ad93d4f 20900 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20901 line_ptr += bytes_read;
20902 break;
20903 case DW_LNS_negate_stmt:
6f77053d 20904 state_machine.handle_negate_stmt ();
c906108c
SS
20905 break;
20906 case DW_LNS_set_basic_block:
c906108c 20907 break;
c2c6d25f
JM
20908 /* Add to the address register of the state machine the
20909 address increment value corresponding to special opcode
a738430d
MK
20910 255. I.e., this value is scaled by the minimum
20911 instruction length since special opcode 255 would have
b021a221 20912 scaled the increment. */
c906108c 20913 case DW_LNS_const_add_pc:
6f77053d 20914 state_machine.handle_const_add_pc ();
c906108c
SS
20915 break;
20916 case DW_LNS_fixed_advance_pc:
3e29f34a 20917 {
6f77053d 20918 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20919 line_ptr += 2;
6f77053d
PA
20920
20921 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20922 }
c906108c 20923 break;
9aa1fe7e 20924 default:
a738430d
MK
20925 {
20926 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20927 int i;
a738430d 20928
debd256d 20929 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20930 {
20931 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20932 line_ptr += bytes_read;
20933 }
20934 }
c906108c
SS
20935 }
20936 }
d9b3de22
DE
20937
20938 if (!end_sequence)
20939 dwarf2_debug_line_missing_end_sequence_complaint ();
20940
20941 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20942 in which case we still finish recording the last line). */
6f77053d 20943 state_machine.record_line (true);
c906108c 20944 }
f3f5162e
DE
20945}
20946
20947/* Decode the Line Number Program (LNP) for the given line_header
20948 structure and CU. The actual information extracted and the type
20949 of structures created from the LNP depends on the value of PST.
20950
20951 1. If PST is NULL, then this procedure uses the data from the program
20952 to create all necessary symbol tables, and their linetables.
20953
20954 2. If PST is not NULL, this procedure reads the program to determine
20955 the list of files included by the unit represented by PST, and
20956 builds all the associated partial symbol tables.
20957
20958 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20959 It is used for relative paths in the line table.
20960 NOTE: When processing partial symtabs (pst != NULL),
20961 comp_dir == pst->dirname.
20962
20963 NOTE: It is important that psymtabs have the same file name (via strcmp)
20964 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20965 symtab we don't use it in the name of the psymtabs we create.
20966 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20967 A good testcase for this is mb-inline.exp.
20968
527f3840
JK
20969 LOWPC is the lowest address in CU (or 0 if not known).
20970
20971 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20972 for its PC<->lines mapping information. Otherwise only the filename
20973 table is read in. */
f3f5162e
DE
20974
20975static void
20976dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 20977 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 20978 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
20979{
20980 struct objfile *objfile = cu->objfile;
20981 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20982
527f3840
JK
20983 if (decode_mapping)
20984 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20985
20986 if (decode_for_pst_p)
20987 {
20988 int file_index;
20989
20990 /* Now that we're done scanning the Line Header Program, we can
20991 create the psymtab of each included file. */
fff8551c 20992 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
20993 if (lh->file_names[file_index].included_p == 1)
20994 {
d521ce57 20995 const char *include_name =
c6da4cef
DE
20996 psymtab_include_file_name (lh, file_index, pst, comp_dir);
20997 if (include_name != NULL)
aaa75496
JB
20998 dwarf2_create_include_psymtab (include_name, pst, objfile);
20999 }
21000 }
cb1df416
DJ
21001 else
21002 {
21003 /* Make sure a symtab is created for every file, even files
21004 which contain only variables (i.e. no code with associated
21005 line numbers). */
43f3e411 21006 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 21007 int i;
cb1df416 21008
fff8551c 21009 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 21010 {
8c43009f 21011 file_entry &fe = lh->file_names[i];
9a619af0 21012
8c43009f 21013 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 21014
cb1df416 21015 if (current_subfile->symtab == NULL)
43f3e411
DE
21016 {
21017 current_subfile->symtab
21018 = allocate_symtab (cust, current_subfile->name);
21019 }
8c43009f 21020 fe.symtab = current_subfile->symtab;
cb1df416
DJ
21021 }
21022 }
c906108c
SS
21023}
21024
21025/* Start a subfile for DWARF. FILENAME is the name of the file and
21026 DIRNAME the name of the source directory which contains FILENAME
4d663531 21027 or NULL if not known.
c906108c
SS
21028 This routine tries to keep line numbers from identical absolute and
21029 relative file names in a common subfile.
21030
21031 Using the `list' example from the GDB testsuite, which resides in
21032 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21033 of /srcdir/list0.c yields the following debugging information for list0.c:
21034
c5aa993b 21035 DW_AT_name: /srcdir/list0.c
4d663531 21036 DW_AT_comp_dir: /compdir
357e46e7 21037 files.files[0].name: list0.h
c5aa993b 21038 files.files[0].dir: /srcdir
357e46e7 21039 files.files[1].name: list0.c
c5aa993b 21040 files.files[1].dir: /srcdir
c906108c
SS
21041
21042 The line number information for list0.c has to end up in a single
4f1520fb
FR
21043 subfile, so that `break /srcdir/list0.c:1' works as expected.
21044 start_subfile will ensure that this happens provided that we pass the
21045 concatenation of files.files[1].dir and files.files[1].name as the
21046 subfile's name. */
c906108c
SS
21047
21048static void
4d663531 21049dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 21050{
d521ce57 21051 char *copy = NULL;
4f1520fb 21052
4d663531 21053 /* In order not to lose the line information directory,
4f1520fb
FR
21054 we concatenate it to the filename when it makes sense.
21055 Note that the Dwarf3 standard says (speaking of filenames in line
21056 information): ``The directory index is ignored for file names
21057 that represent full path names''. Thus ignoring dirname in the
21058 `else' branch below isn't an issue. */
c906108c 21059
d5166ae1 21060 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21061 {
21062 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21063 filename = copy;
21064 }
c906108c 21065
4d663531 21066 start_subfile (filename);
4f1520fb 21067
d521ce57
TT
21068 if (copy != NULL)
21069 xfree (copy);
c906108c
SS
21070}
21071
f4dc4d17
DE
21072/* Start a symtab for DWARF.
21073 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21074
43f3e411 21075static struct compunit_symtab *
f4dc4d17 21076dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 21077 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 21078{
43f3e411 21079 struct compunit_symtab *cust
5ffa0793 21080 = start_symtab (cu->objfile, name, comp_dir, low_pc, cu->language);
43f3e411 21081
f4dc4d17
DE
21082 record_debugformat ("DWARF 2");
21083 record_producer (cu->producer);
21084
21085 /* We assume that we're processing GCC output. */
21086 processing_gcc_compilation = 2;
21087
4d4ec4e5 21088 cu->processing_has_namespace_info = 0;
43f3e411
DE
21089
21090 return cust;
f4dc4d17
DE
21091}
21092
4c2df51b
DJ
21093static void
21094var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21095 struct dwarf2_cu *cu)
4c2df51b 21096{
e7c27a73
DJ
21097 struct objfile *objfile = cu->objfile;
21098 struct comp_unit_head *cu_header = &cu->header;
21099
4c2df51b
DJ
21100 /* NOTE drow/2003-01-30: There used to be a comment and some special
21101 code here to turn a symbol with DW_AT_external and a
21102 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21103 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21104 with some versions of binutils) where shared libraries could have
21105 relocations against symbols in their debug information - the
21106 minimal symbol would have the right address, but the debug info
21107 would not. It's no longer necessary, because we will explicitly
21108 apply relocations when we read in the debug information now. */
21109
21110 /* A DW_AT_location attribute with no contents indicates that a
21111 variable has been optimized away. */
21112 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21113 {
f1e6e072 21114 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21115 return;
21116 }
21117
21118 /* Handle one degenerate form of location expression specially, to
21119 preserve GDB's previous behavior when section offsets are
3019eac3
DE
21120 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21121 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21122
21123 if (attr_form_is_block (attr)
3019eac3
DE
21124 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21125 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21126 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21127 && (DW_BLOCK (attr)->size
21128 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21129 {
891d2f0b 21130 unsigned int dummy;
4c2df51b 21131
3019eac3
DE
21132 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21133 SYMBOL_VALUE_ADDRESS (sym) =
21134 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21135 else
21136 SYMBOL_VALUE_ADDRESS (sym) =
21137 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 21138 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
21139 fixup_symbol_section (sym, objfile);
21140 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21141 SYMBOL_SECTION (sym));
4c2df51b
DJ
21142 return;
21143 }
21144
21145 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21146 expression evaluator, and use LOC_COMPUTED only when necessary
21147 (i.e. when the value of a register or memory location is
21148 referenced, or a thread-local block, etc.). Then again, it might
21149 not be worthwhile. I'm assuming that it isn't unless performance
21150 or memory numbers show me otherwise. */
21151
f1e6e072 21152 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21153
f1e6e072 21154 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 21155 cu->has_loclist = 1;
4c2df51b
DJ
21156}
21157
c906108c
SS
21158/* Given a pointer to a DWARF information entry, figure out if we need
21159 to make a symbol table entry for it, and if so, create a new entry
21160 and return a pointer to it.
21161 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21162 used the passed type.
21163 If SPACE is not NULL, use it to hold the new symbol. If it is
21164 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21165
21166static struct symbol *
34eaf542
TT
21167new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21168 struct symbol *space)
c906108c 21169{
e7c27a73 21170 struct objfile *objfile = cu->objfile;
3e29f34a 21171 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21172 struct symbol *sym = NULL;
15d034d0 21173 const char *name;
c906108c
SS
21174 struct attribute *attr = NULL;
21175 struct attribute *attr2 = NULL;
e142c38c 21176 CORE_ADDR baseaddr;
e37fd15a
SW
21177 struct pending **list_to_add = NULL;
21178
edb3359d 21179 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21180
21181 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21182
94af9270 21183 name = dwarf2_name (die, cu);
c906108c
SS
21184 if (name)
21185 {
94af9270 21186 const char *linkagename;
34eaf542 21187 int suppress_add = 0;
94af9270 21188
34eaf542
TT
21189 if (space)
21190 sym = space;
21191 else
e623cf5d 21192 sym = allocate_symbol (objfile);
c906108c 21193 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21194
21195 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21196 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21197 linkagename = dwarf2_physname (name, die, cu);
21198 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21199
f55ee35c
JK
21200 /* Fortran does not have mangling standard and the mangling does differ
21201 between gfortran, iFort etc. */
21202 if (cu->language == language_fortran
b250c185 21203 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21204 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21205 dwarf2_full_name (name, die, cu),
29df156d 21206 NULL);
f55ee35c 21207
c906108c 21208 /* Default assumptions.
c5aa993b 21209 Use the passed type or decode it from the die. */
176620f1 21210 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21211 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21212 if (type != NULL)
21213 SYMBOL_TYPE (sym) = type;
21214 else
e7c27a73 21215 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21216 attr = dwarf2_attr (die,
21217 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21218 cu);
c906108c
SS
21219 if (attr)
21220 {
21221 SYMBOL_LINE (sym) = DW_UNSND (attr);
21222 }
cb1df416 21223
edb3359d
DJ
21224 attr = dwarf2_attr (die,
21225 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21226 cu);
cb1df416
DJ
21227 if (attr)
21228 {
ecfb656c 21229 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21230 struct file_entry *fe;
9a619af0 21231
ecfb656c
PA
21232 if (cu->line_header != NULL)
21233 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21234 else
21235 fe = NULL;
21236
21237 if (fe == NULL)
cb1df416
DJ
21238 complaint (&symfile_complaints,
21239 _("file index out of range"));
8c43009f
PA
21240 else
21241 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21242 }
21243
c906108c
SS
21244 switch (die->tag)
21245 {
21246 case DW_TAG_label:
e142c38c 21247 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21248 if (attr)
3e29f34a
MR
21249 {
21250 CORE_ADDR addr;
21251
21252 addr = attr_value_as_address (attr);
21253 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21254 SYMBOL_VALUE_ADDRESS (sym) = addr;
21255 }
0f5238ed
TT
21256 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21257 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21258 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 21259 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21260 break;
21261 case DW_TAG_subprogram:
21262 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21263 finish_block. */
f1e6e072 21264 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21265 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
21266 if ((attr2 && (DW_UNSND (attr2) != 0))
21267 || cu->language == language_ada)
c906108c 21268 {
2cfa0c8d
JB
21269 /* Subprograms marked external are stored as a global symbol.
21270 Ada subprograms, whether marked external or not, are always
21271 stored as a global symbol, because we want to be able to
21272 access them globally. For instance, we want to be able
21273 to break on a nested subprogram without having to
21274 specify the context. */
e37fd15a 21275 list_to_add = &global_symbols;
c906108c
SS
21276 }
21277 else
21278 {
e37fd15a 21279 list_to_add = cu->list_in_scope;
c906108c
SS
21280 }
21281 break;
edb3359d
DJ
21282 case DW_TAG_inlined_subroutine:
21283 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21284 finish_block. */
f1e6e072 21285 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21286 SYMBOL_INLINED (sym) = 1;
481860b3 21287 list_to_add = cu->list_in_scope;
edb3359d 21288 break;
34eaf542
TT
21289 case DW_TAG_template_value_param:
21290 suppress_add = 1;
21291 /* Fall through. */
72929c62 21292 case DW_TAG_constant:
c906108c 21293 case DW_TAG_variable:
254e6b9e 21294 case DW_TAG_member:
0963b4bd
MS
21295 /* Compilation with minimal debug info may result in
21296 variables with missing type entries. Change the
21297 misleading `void' type to something sensible. */
c906108c 21298 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21299 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21300
e142c38c 21301 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21302 /* In the case of DW_TAG_member, we should only be called for
21303 static const members. */
21304 if (die->tag == DW_TAG_member)
21305 {
3863f96c
DE
21306 /* dwarf2_add_field uses die_is_declaration,
21307 so we do the same. */
254e6b9e
DE
21308 gdb_assert (die_is_declaration (die, cu));
21309 gdb_assert (attr);
21310 }
c906108c
SS
21311 if (attr)
21312 {
e7c27a73 21313 dwarf2_const_value (attr, sym, cu);
e142c38c 21314 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21315 if (!suppress_add)
34eaf542
TT
21316 {
21317 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 21318 list_to_add = &global_symbols;
34eaf542 21319 else
e37fd15a 21320 list_to_add = cu->list_in_scope;
34eaf542 21321 }
c906108c
SS
21322 break;
21323 }
e142c38c 21324 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21325 if (attr)
21326 {
e7c27a73 21327 var_decode_location (attr, sym, cu);
e142c38c 21328 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21329
21330 /* Fortran explicitly imports any global symbols to the local
21331 scope by DW_TAG_common_block. */
21332 if (cu->language == language_fortran && die->parent
21333 && die->parent->tag == DW_TAG_common_block)
21334 attr2 = NULL;
21335
caac4577
JG
21336 if (SYMBOL_CLASS (sym) == LOC_STATIC
21337 && SYMBOL_VALUE_ADDRESS (sym) == 0
21338 && !dwarf2_per_objfile->has_section_at_zero)
21339 {
21340 /* When a static variable is eliminated by the linker,
21341 the corresponding debug information is not stripped
21342 out, but the variable address is set to null;
21343 do not add such variables into symbol table. */
21344 }
21345 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21346 {
f55ee35c
JK
21347 /* Workaround gfortran PR debug/40040 - it uses
21348 DW_AT_location for variables in -fPIC libraries which may
21349 get overriden by other libraries/executable and get
21350 a different address. Resolve it by the minimal symbol
21351 which may come from inferior's executable using copy
21352 relocation. Make this workaround only for gfortran as for
21353 other compilers GDB cannot guess the minimal symbol
21354 Fortran mangling kind. */
21355 if (cu->language == language_fortran && die->parent
21356 && die->parent->tag == DW_TAG_module
21357 && cu->producer
28586665 21358 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 21359 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 21360
1c809c68
TT
21361 /* A variable with DW_AT_external is never static,
21362 but it may be block-scoped. */
21363 list_to_add = (cu->list_in_scope == &file_symbols
21364 ? &global_symbols : cu->list_in_scope);
1c809c68 21365 }
c906108c 21366 else
e37fd15a 21367 list_to_add = cu->list_in_scope;
c906108c
SS
21368 }
21369 else
21370 {
21371 /* We do not know the address of this symbol.
c5aa993b
JM
21372 If it is an external symbol and we have type information
21373 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21374 The address of the variable will then be determined from
21375 the minimal symbol table whenever the variable is
21376 referenced. */
e142c38c 21377 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21378
21379 /* Fortran explicitly imports any global symbols to the local
21380 scope by DW_TAG_common_block. */
21381 if (cu->language == language_fortran && die->parent
21382 && die->parent->tag == DW_TAG_common_block)
21383 {
21384 /* SYMBOL_CLASS doesn't matter here because
21385 read_common_block is going to reset it. */
21386 if (!suppress_add)
21387 list_to_add = cu->list_in_scope;
21388 }
21389 else if (attr2 && (DW_UNSND (attr2) != 0)
21390 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21391 {
0fe7935b
DJ
21392 /* A variable with DW_AT_external is never static, but it
21393 may be block-scoped. */
21394 list_to_add = (cu->list_in_scope == &file_symbols
21395 ? &global_symbols : cu->list_in_scope);
21396
f1e6e072 21397 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21398 }
442ddf59
JK
21399 else if (!die_is_declaration (die, cu))
21400 {
21401 /* Use the default LOC_OPTIMIZED_OUT class. */
21402 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21403 if (!suppress_add)
21404 list_to_add = cu->list_in_scope;
442ddf59 21405 }
c906108c
SS
21406 }
21407 break;
21408 case DW_TAG_formal_parameter:
edb3359d
DJ
21409 /* If we are inside a function, mark this as an argument. If
21410 not, we might be looking at an argument to an inlined function
21411 when we do not have enough information to show inlined frames;
21412 pretend it's a local variable in that case so that the user can
21413 still see it. */
21414 if (context_stack_depth > 0
21415 && context_stack[context_stack_depth - 1].name != NULL)
21416 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 21417 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21418 if (attr)
21419 {
e7c27a73 21420 var_decode_location (attr, sym, cu);
c906108c 21421 }
e142c38c 21422 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21423 if (attr)
21424 {
e7c27a73 21425 dwarf2_const_value (attr, sym, cu);
c906108c 21426 }
f346a30d 21427
e37fd15a 21428 list_to_add = cu->list_in_scope;
c906108c
SS
21429 break;
21430 case DW_TAG_unspecified_parameters:
21431 /* From varargs functions; gdb doesn't seem to have any
21432 interest in this information, so just ignore it for now.
21433 (FIXME?) */
21434 break;
34eaf542
TT
21435 case DW_TAG_template_type_param:
21436 suppress_add = 1;
21437 /* Fall through. */
c906108c 21438 case DW_TAG_class_type:
680b30c7 21439 case DW_TAG_interface_type:
c906108c
SS
21440 case DW_TAG_structure_type:
21441 case DW_TAG_union_type:
72019c9c 21442 case DW_TAG_set_type:
c906108c 21443 case DW_TAG_enumeration_type:
f1e6e072 21444 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21445 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21446
63d06c5c 21447 {
9c37b5ae 21448 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21449 really ever be static objects: otherwise, if you try
21450 to, say, break of a class's method and you're in a file
21451 which doesn't mention that class, it won't work unless
21452 the check for all static symbols in lookup_symbol_aux
21453 saves you. See the OtherFileClass tests in
21454 gdb.c++/namespace.exp. */
21455
e37fd15a 21456 if (!suppress_add)
34eaf542 21457 {
34eaf542 21458 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21459 && cu->language == language_cplus
34eaf542 21460 ? &global_symbols : cu->list_in_scope);
63d06c5c 21461
64382290 21462 /* The semantics of C++ state that "struct foo {
9c37b5ae 21463 ... }" also defines a typedef for "foo". */
64382290 21464 if (cu->language == language_cplus
45280282 21465 || cu->language == language_ada
c44af4eb
TT
21466 || cu->language == language_d
21467 || cu->language == language_rust)
64382290
TT
21468 {
21469 /* The symbol's name is already allocated along
21470 with this objfile, so we don't need to
21471 duplicate it for the type. */
21472 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21473 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21474 }
63d06c5c
DC
21475 }
21476 }
c906108c
SS
21477 break;
21478 case DW_TAG_typedef:
f1e6e072 21479 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21480 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21481 list_to_add = cu->list_in_scope;
63d06c5c 21482 break;
c906108c 21483 case DW_TAG_base_type:
a02abb62 21484 case DW_TAG_subrange_type:
f1e6e072 21485 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21486 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21487 list_to_add = cu->list_in_scope;
c906108c
SS
21488 break;
21489 case DW_TAG_enumerator:
e142c38c 21490 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21491 if (attr)
21492 {
e7c27a73 21493 dwarf2_const_value (attr, sym, cu);
c906108c 21494 }
63d06c5c
DC
21495 {
21496 /* NOTE: carlton/2003-11-10: See comment above in the
21497 DW_TAG_class_type, etc. block. */
21498
e142c38c 21499 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21500 && cu->language == language_cplus
e142c38c 21501 ? &global_symbols : cu->list_in_scope);
63d06c5c 21502 }
c906108c 21503 break;
74921315 21504 case DW_TAG_imported_declaration:
5c4e30ca 21505 case DW_TAG_namespace:
f1e6e072 21506 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 21507 list_to_add = &global_symbols;
5c4e30ca 21508 break;
530e8392
KB
21509 case DW_TAG_module:
21510 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21511 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21512 list_to_add = &global_symbols;
21513 break;
4357ac6c 21514 case DW_TAG_common_block:
f1e6e072 21515 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
21516 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21517 add_symbol_to_list (sym, cu->list_in_scope);
21518 break;
c906108c
SS
21519 default:
21520 /* Not a tag we recognize. Hopefully we aren't processing
21521 trash data, but since we must specifically ignore things
21522 we don't recognize, there is nothing else we should do at
0963b4bd 21523 this point. */
e2e0b3e5 21524 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 21525 dwarf_tag_name (die->tag));
c906108c
SS
21526 break;
21527 }
df8a16a1 21528
e37fd15a
SW
21529 if (suppress_add)
21530 {
21531 sym->hash_next = objfile->template_symbols;
21532 objfile->template_symbols = sym;
21533 list_to_add = NULL;
21534 }
21535
21536 if (list_to_add != NULL)
21537 add_symbol_to_list (sym, list_to_add);
21538
df8a16a1
DJ
21539 /* For the benefit of old versions of GCC, check for anonymous
21540 namespaces based on the demangled name. */
4d4ec4e5 21541 if (!cu->processing_has_namespace_info
94af9270 21542 && cu->language == language_cplus)
a10964d1 21543 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
21544 }
21545 return (sym);
21546}
21547
34eaf542
TT
21548/* A wrapper for new_symbol_full that always allocates a new symbol. */
21549
21550static struct symbol *
21551new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
21552{
21553 return new_symbol_full (die, type, cu, NULL);
21554}
21555
98bfdba5
PA
21556/* Given an attr with a DW_FORM_dataN value in host byte order,
21557 zero-extend it as appropriate for the symbol's type. The DWARF
21558 standard (v4) is not entirely clear about the meaning of using
21559 DW_FORM_dataN for a constant with a signed type, where the type is
21560 wider than the data. The conclusion of a discussion on the DWARF
21561 list was that this is unspecified. We choose to always zero-extend
21562 because that is the interpretation long in use by GCC. */
c906108c 21563
98bfdba5 21564static gdb_byte *
ff39bb5e 21565dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21566 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21567{
e7c27a73 21568 struct objfile *objfile = cu->objfile;
e17a4113
UW
21569 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21570 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21571 LONGEST l = DW_UNSND (attr);
21572
21573 if (bits < sizeof (*value) * 8)
21574 {
21575 l &= ((LONGEST) 1 << bits) - 1;
21576 *value = l;
21577 }
21578 else if (bits == sizeof (*value) * 8)
21579 *value = l;
21580 else
21581 {
224c3ddb 21582 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21583 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21584 return bytes;
21585 }
21586
21587 return NULL;
21588}
21589
21590/* Read a constant value from an attribute. Either set *VALUE, or if
21591 the value does not fit in *VALUE, set *BYTES - either already
21592 allocated on the objfile obstack, or newly allocated on OBSTACK,
21593 or, set *BATON, if we translated the constant to a location
21594 expression. */
21595
21596static void
ff39bb5e 21597dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21598 const char *name, struct obstack *obstack,
21599 struct dwarf2_cu *cu,
d521ce57 21600 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21601 struct dwarf2_locexpr_baton **baton)
21602{
21603 struct objfile *objfile = cu->objfile;
21604 struct comp_unit_head *cu_header = &cu->header;
c906108c 21605 struct dwarf_block *blk;
98bfdba5
PA
21606 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21607 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21608
21609 *value = 0;
21610 *bytes = NULL;
21611 *baton = NULL;
c906108c
SS
21612
21613 switch (attr->form)
21614 {
21615 case DW_FORM_addr:
3019eac3 21616 case DW_FORM_GNU_addr_index:
ac56253d 21617 {
ac56253d
TT
21618 gdb_byte *data;
21619
98bfdba5
PA
21620 if (TYPE_LENGTH (type) != cu_header->addr_size)
21621 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21622 cu_header->addr_size,
98bfdba5 21623 TYPE_LENGTH (type));
ac56253d
TT
21624 /* Symbols of this form are reasonably rare, so we just
21625 piggyback on the existing location code rather than writing
21626 a new implementation of symbol_computed_ops. */
8d749320 21627 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21628 (*baton)->per_cu = cu->per_cu;
21629 gdb_assert ((*baton)->per_cu);
ac56253d 21630
98bfdba5 21631 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21632 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21633 (*baton)->data = data;
ac56253d
TT
21634
21635 data[0] = DW_OP_addr;
21636 store_unsigned_integer (&data[1], cu_header->addr_size,
21637 byte_order, DW_ADDR (attr));
21638 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21639 }
c906108c 21640 break;
4ac36638 21641 case DW_FORM_string:
93b5768b 21642 case DW_FORM_strp:
3019eac3 21643 case DW_FORM_GNU_str_index:
36586728 21644 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21645 /* DW_STRING is already allocated on the objfile obstack, point
21646 directly to it. */
d521ce57 21647 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21648 break;
c906108c
SS
21649 case DW_FORM_block1:
21650 case DW_FORM_block2:
21651 case DW_FORM_block4:
21652 case DW_FORM_block:
2dc7f7b3 21653 case DW_FORM_exprloc:
0224619f 21654 case DW_FORM_data16:
c906108c 21655 blk = DW_BLOCK (attr);
98bfdba5
PA
21656 if (TYPE_LENGTH (type) != blk->size)
21657 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21658 TYPE_LENGTH (type));
21659 *bytes = blk->data;
c906108c 21660 break;
2df3850c
JM
21661
21662 /* The DW_AT_const_value attributes are supposed to carry the
21663 symbol's value "represented as it would be on the target
21664 architecture." By the time we get here, it's already been
21665 converted to host endianness, so we just need to sign- or
21666 zero-extend it as appropriate. */
21667 case DW_FORM_data1:
3aef2284 21668 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21669 break;
c906108c 21670 case DW_FORM_data2:
3aef2284 21671 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21672 break;
c906108c 21673 case DW_FORM_data4:
3aef2284 21674 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21675 break;
c906108c 21676 case DW_FORM_data8:
3aef2284 21677 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21678 break;
21679
c906108c 21680 case DW_FORM_sdata:
663c44ac 21681 case DW_FORM_implicit_const:
98bfdba5 21682 *value = DW_SND (attr);
2df3850c
JM
21683 break;
21684
c906108c 21685 case DW_FORM_udata:
98bfdba5 21686 *value = DW_UNSND (attr);
c906108c 21687 break;
2df3850c 21688
c906108c 21689 default:
4d3c2250 21690 complaint (&symfile_complaints,
e2e0b3e5 21691 _("unsupported const value attribute form: '%s'"),
4d3c2250 21692 dwarf_form_name (attr->form));
98bfdba5 21693 *value = 0;
c906108c
SS
21694 break;
21695 }
21696}
21697
2df3850c 21698
98bfdba5
PA
21699/* Copy constant value from an attribute to a symbol. */
21700
2df3850c 21701static void
ff39bb5e 21702dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21703 struct dwarf2_cu *cu)
2df3850c 21704{
98bfdba5 21705 struct objfile *objfile = cu->objfile;
12df843f 21706 LONGEST value;
d521ce57 21707 const gdb_byte *bytes;
98bfdba5 21708 struct dwarf2_locexpr_baton *baton;
2df3850c 21709
98bfdba5
PA
21710 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21711 SYMBOL_PRINT_NAME (sym),
21712 &objfile->objfile_obstack, cu,
21713 &value, &bytes, &baton);
2df3850c 21714
98bfdba5
PA
21715 if (baton != NULL)
21716 {
98bfdba5 21717 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21718 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21719 }
21720 else if (bytes != NULL)
21721 {
21722 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21723 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21724 }
21725 else
21726 {
21727 SYMBOL_VALUE (sym) = value;
f1e6e072 21728 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21729 }
2df3850c
JM
21730}
21731
c906108c
SS
21732/* Return the type of the die in question using its DW_AT_type attribute. */
21733
21734static struct type *
e7c27a73 21735die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21736{
c906108c 21737 struct attribute *type_attr;
c906108c 21738
e142c38c 21739 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21740 if (!type_attr)
21741 {
21742 /* A missing DW_AT_type represents a void type. */
46bf5051 21743 return objfile_type (cu->objfile)->builtin_void;
c906108c 21744 }
348e048f 21745
673bfd45 21746 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21747}
21748
b4ba55a1
JB
21749/* True iff CU's producer generates GNAT Ada auxiliary information
21750 that allows to find parallel types through that information instead
21751 of having to do expensive parallel lookups by type name. */
21752
21753static int
21754need_gnat_info (struct dwarf2_cu *cu)
21755{
21756 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
21757 of GNAT produces this auxiliary information, without any indication
21758 that it is produced. Part of enhancing the FSF version of GNAT
21759 to produce that information will be to put in place an indicator
21760 that we can use in order to determine whether the descriptive type
21761 info is available or not. One suggestion that has been made is
21762 to use a new attribute, attached to the CU die. For now, assume
21763 that the descriptive type info is not available. */
21764 return 0;
21765}
21766
b4ba55a1
JB
21767/* Return the auxiliary type of the die in question using its
21768 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21769 attribute is not present. */
21770
21771static struct type *
21772die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21773{
b4ba55a1 21774 struct attribute *type_attr;
b4ba55a1
JB
21775
21776 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21777 if (!type_attr)
21778 return NULL;
21779
673bfd45 21780 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21781}
21782
21783/* If DIE has a descriptive_type attribute, then set the TYPE's
21784 descriptive type accordingly. */
21785
21786static void
21787set_descriptive_type (struct type *type, struct die_info *die,
21788 struct dwarf2_cu *cu)
21789{
21790 struct type *descriptive_type = die_descriptive_type (die, cu);
21791
21792 if (descriptive_type)
21793 {
21794 ALLOCATE_GNAT_AUX_TYPE (type);
21795 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21796 }
21797}
21798
c906108c
SS
21799/* Return the containing type of the die in question using its
21800 DW_AT_containing_type attribute. */
21801
21802static struct type *
e7c27a73 21803die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21804{
c906108c 21805 struct attribute *type_attr;
c906108c 21806
e142c38c 21807 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21808 if (!type_attr)
21809 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 21810 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 21811
673bfd45 21812 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21813}
21814
ac9ec31b
DE
21815/* Return an error marker type to use for the ill formed type in DIE/CU. */
21816
21817static struct type *
21818build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21819{
21820 struct objfile *objfile = dwarf2_per_objfile->objfile;
21821 char *message, *saved;
21822
21823 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 21824 objfile_name (objfile),
9c541725
PA
21825 to_underlying (cu->header.sect_off),
21826 to_underlying (die->sect_off));
224c3ddb
SM
21827 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21828 message, strlen (message));
ac9ec31b
DE
21829 xfree (message);
21830
19f392bc 21831 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21832}
21833
673bfd45 21834/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21835 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21836 DW_AT_containing_type.
673bfd45
DE
21837 If there is no type substitute an error marker. */
21838
c906108c 21839static struct type *
ff39bb5e 21840lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21841 struct dwarf2_cu *cu)
c906108c 21842{
bb5ed363 21843 struct objfile *objfile = cu->objfile;
f792889a
DJ
21844 struct type *this_type;
21845
ac9ec31b
DE
21846 gdb_assert (attr->name == DW_AT_type
21847 || attr->name == DW_AT_GNAT_descriptive_type
21848 || attr->name == DW_AT_containing_type);
21849
673bfd45
DE
21850 /* First see if we have it cached. */
21851
36586728
TT
21852 if (attr->form == DW_FORM_GNU_ref_alt)
21853 {
21854 struct dwarf2_per_cu_data *per_cu;
9c541725 21855 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21856
9c541725
PA
21857 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
21858 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21859 }
7771576e 21860 else if (attr_form_is_ref (attr))
673bfd45 21861 {
9c541725 21862 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21863
9c541725 21864 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21865 }
55f1336d 21866 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21867 {
ac9ec31b 21868 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21869
ac9ec31b 21870 return get_signatured_type (die, signature, cu);
673bfd45
DE
21871 }
21872 else
21873 {
ac9ec31b
DE
21874 complaint (&symfile_complaints,
21875 _("Dwarf Error: Bad type attribute %s in DIE"
21876 " at 0x%x [in module %s]"),
9c541725 21877 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
4262abfb 21878 objfile_name (objfile));
ac9ec31b 21879 return build_error_marker_type (cu, die);
673bfd45
DE
21880 }
21881
21882 /* If not cached we need to read it in. */
21883
21884 if (this_type == NULL)
21885 {
ac9ec31b 21886 struct die_info *type_die = NULL;
673bfd45
DE
21887 struct dwarf2_cu *type_cu = cu;
21888
7771576e 21889 if (attr_form_is_ref (attr))
ac9ec31b
DE
21890 type_die = follow_die_ref (die, attr, &type_cu);
21891 if (type_die == NULL)
21892 return build_error_marker_type (cu, die);
21893 /* If we find the type now, it's probably because the type came
3019eac3
DE
21894 from an inter-CU reference and the type's CU got expanded before
21895 ours. */
ac9ec31b 21896 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21897 }
21898
21899 /* If we still don't have a type use an error marker. */
21900
21901 if (this_type == NULL)
ac9ec31b 21902 return build_error_marker_type (cu, die);
673bfd45 21903
f792889a 21904 return this_type;
c906108c
SS
21905}
21906
673bfd45
DE
21907/* Return the type in DIE, CU.
21908 Returns NULL for invalid types.
21909
02142a6c 21910 This first does a lookup in die_type_hash,
673bfd45
DE
21911 and only reads the die in if necessary.
21912
21913 NOTE: This can be called when reading in partial or full symbols. */
21914
f792889a 21915static struct type *
e7c27a73 21916read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21917{
f792889a
DJ
21918 struct type *this_type;
21919
21920 this_type = get_die_type (die, cu);
21921 if (this_type)
21922 return this_type;
21923
673bfd45
DE
21924 return read_type_die_1 (die, cu);
21925}
21926
21927/* Read the type in DIE, CU.
21928 Returns NULL for invalid types. */
21929
21930static struct type *
21931read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21932{
21933 struct type *this_type = NULL;
21934
c906108c
SS
21935 switch (die->tag)
21936 {
21937 case DW_TAG_class_type:
680b30c7 21938 case DW_TAG_interface_type:
c906108c
SS
21939 case DW_TAG_structure_type:
21940 case DW_TAG_union_type:
f792889a 21941 this_type = read_structure_type (die, cu);
c906108c
SS
21942 break;
21943 case DW_TAG_enumeration_type:
f792889a 21944 this_type = read_enumeration_type (die, cu);
c906108c
SS
21945 break;
21946 case DW_TAG_subprogram:
21947 case DW_TAG_subroutine_type:
edb3359d 21948 case DW_TAG_inlined_subroutine:
f792889a 21949 this_type = read_subroutine_type (die, cu);
c906108c
SS
21950 break;
21951 case DW_TAG_array_type:
f792889a 21952 this_type = read_array_type (die, cu);
c906108c 21953 break;
72019c9c 21954 case DW_TAG_set_type:
f792889a 21955 this_type = read_set_type (die, cu);
72019c9c 21956 break;
c906108c 21957 case DW_TAG_pointer_type:
f792889a 21958 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21959 break;
21960 case DW_TAG_ptr_to_member_type:
f792889a 21961 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21962 break;
21963 case DW_TAG_reference_type:
4297a3f0
AV
21964 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21965 break;
21966 case DW_TAG_rvalue_reference_type:
21967 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21968 break;
21969 case DW_TAG_const_type:
f792889a 21970 this_type = read_tag_const_type (die, cu);
c906108c
SS
21971 break;
21972 case DW_TAG_volatile_type:
f792889a 21973 this_type = read_tag_volatile_type (die, cu);
c906108c 21974 break;
06d66ee9
TT
21975 case DW_TAG_restrict_type:
21976 this_type = read_tag_restrict_type (die, cu);
21977 break;
c906108c 21978 case DW_TAG_string_type:
f792889a 21979 this_type = read_tag_string_type (die, cu);
c906108c
SS
21980 break;
21981 case DW_TAG_typedef:
f792889a 21982 this_type = read_typedef (die, cu);
c906108c 21983 break;
a02abb62 21984 case DW_TAG_subrange_type:
f792889a 21985 this_type = read_subrange_type (die, cu);
a02abb62 21986 break;
c906108c 21987 case DW_TAG_base_type:
f792889a 21988 this_type = read_base_type (die, cu);
c906108c 21989 break;
81a17f79 21990 case DW_TAG_unspecified_type:
f792889a 21991 this_type = read_unspecified_type (die, cu);
81a17f79 21992 break;
0114d602
DJ
21993 case DW_TAG_namespace:
21994 this_type = read_namespace_type (die, cu);
21995 break;
f55ee35c
JK
21996 case DW_TAG_module:
21997 this_type = read_module_type (die, cu);
21998 break;
a2c2acaf
MW
21999 case DW_TAG_atomic_type:
22000 this_type = read_tag_atomic_type (die, cu);
22001 break;
c906108c 22002 default:
3e43a32a
MS
22003 complaint (&symfile_complaints,
22004 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 22005 dwarf_tag_name (die->tag));
c906108c
SS
22006 break;
22007 }
63d06c5c 22008
f792889a 22009 return this_type;
63d06c5c
DC
22010}
22011
abc72ce4
DE
22012/* See if we can figure out if the class lives in a namespace. We do
22013 this by looking for a member function; its demangled name will
22014 contain namespace info, if there is any.
22015 Return the computed name or NULL.
22016 Space for the result is allocated on the objfile's obstack.
22017 This is the full-die version of guess_partial_die_structure_name.
22018 In this case we know DIE has no useful parent. */
22019
22020static char *
22021guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22022{
22023 struct die_info *spec_die;
22024 struct dwarf2_cu *spec_cu;
22025 struct die_info *child;
22026
22027 spec_cu = cu;
22028 spec_die = die_specification (die, &spec_cu);
22029 if (spec_die != NULL)
22030 {
22031 die = spec_die;
22032 cu = spec_cu;
22033 }
22034
22035 for (child = die->child;
22036 child != NULL;
22037 child = child->sibling)
22038 {
22039 if (child->tag == DW_TAG_subprogram)
22040 {
73b9be8b 22041 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22042
7d45c7c3 22043 if (linkage_name != NULL)
abc72ce4
DE
22044 {
22045 char *actual_name
22046 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22047 linkage_name);
abc72ce4
DE
22048 char *name = NULL;
22049
22050 if (actual_name != NULL)
22051 {
15d034d0 22052 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22053
22054 if (die_name != NULL
22055 && strcmp (die_name, actual_name) != 0)
22056 {
22057 /* Strip off the class name from the full name.
22058 We want the prefix. */
22059 int die_name_len = strlen (die_name);
22060 int actual_name_len = strlen (actual_name);
22061
22062 /* Test for '::' as a sanity check. */
22063 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22064 && actual_name[actual_name_len
22065 - die_name_len - 1] == ':')
224c3ddb
SM
22066 name = (char *) obstack_copy0 (
22067 &cu->objfile->per_bfd->storage_obstack,
22068 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22069 }
22070 }
22071 xfree (actual_name);
22072 return name;
22073 }
22074 }
22075 }
22076
22077 return NULL;
22078}
22079
96408a79
SA
22080/* GCC might emit a nameless typedef that has a linkage name. Determine the
22081 prefix part in such case. See
22082 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22083
a121b7c1 22084static const char *
96408a79
SA
22085anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22086{
22087 struct attribute *attr;
e6a959d6 22088 const char *base;
96408a79
SA
22089
22090 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22091 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22092 return NULL;
22093
7d45c7c3 22094 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22095 return NULL;
22096
73b9be8b 22097 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22098 if (attr == NULL || DW_STRING (attr) == NULL)
22099 return NULL;
22100
22101 /* dwarf2_name had to be already called. */
22102 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22103
22104 /* Strip the base name, keep any leading namespaces/classes. */
22105 base = strrchr (DW_STRING (attr), ':');
22106 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22107 return "";
22108
224c3ddb
SM
22109 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
22110 DW_STRING (attr),
22111 &base[-1] - DW_STRING (attr));
96408a79
SA
22112}
22113
fdde2d81 22114/* Return the name of the namespace/class that DIE is defined within,
0114d602 22115 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22116
0114d602
DJ
22117 For example, if we're within the method foo() in the following
22118 code:
22119
22120 namespace N {
22121 class C {
22122 void foo () {
22123 }
22124 };
22125 }
22126
22127 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22128
0d5cff50 22129static const char *
e142c38c 22130determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22131{
0114d602
DJ
22132 struct die_info *parent, *spec_die;
22133 struct dwarf2_cu *spec_cu;
22134 struct type *parent_type;
a121b7c1 22135 const char *retval;
63d06c5c 22136
9c37b5ae 22137 if (cu->language != language_cplus
c44af4eb
TT
22138 && cu->language != language_fortran && cu->language != language_d
22139 && cu->language != language_rust)
0114d602
DJ
22140 return "";
22141
96408a79
SA
22142 retval = anonymous_struct_prefix (die, cu);
22143 if (retval)
22144 return retval;
22145
0114d602
DJ
22146 /* We have to be careful in the presence of DW_AT_specification.
22147 For example, with GCC 3.4, given the code
22148
22149 namespace N {
22150 void foo() {
22151 // Definition of N::foo.
22152 }
22153 }
22154
22155 then we'll have a tree of DIEs like this:
22156
22157 1: DW_TAG_compile_unit
22158 2: DW_TAG_namespace // N
22159 3: DW_TAG_subprogram // declaration of N::foo
22160 4: DW_TAG_subprogram // definition of N::foo
22161 DW_AT_specification // refers to die #3
22162
22163 Thus, when processing die #4, we have to pretend that we're in
22164 the context of its DW_AT_specification, namely the contex of die
22165 #3. */
22166 spec_cu = cu;
22167 spec_die = die_specification (die, &spec_cu);
22168 if (spec_die == NULL)
22169 parent = die->parent;
22170 else
63d06c5c 22171 {
0114d602
DJ
22172 parent = spec_die->parent;
22173 cu = spec_cu;
63d06c5c 22174 }
0114d602
DJ
22175
22176 if (parent == NULL)
22177 return "";
98bfdba5
PA
22178 else if (parent->building_fullname)
22179 {
22180 const char *name;
22181 const char *parent_name;
22182
22183 /* It has been seen on RealView 2.2 built binaries,
22184 DW_TAG_template_type_param types actually _defined_ as
22185 children of the parent class:
22186
22187 enum E {};
22188 template class <class Enum> Class{};
22189 Class<enum E> class_e;
22190
22191 1: DW_TAG_class_type (Class)
22192 2: DW_TAG_enumeration_type (E)
22193 3: DW_TAG_enumerator (enum1:0)
22194 3: DW_TAG_enumerator (enum2:1)
22195 ...
22196 2: DW_TAG_template_type_param
22197 DW_AT_type DW_FORM_ref_udata (E)
22198
22199 Besides being broken debug info, it can put GDB into an
22200 infinite loop. Consider:
22201
22202 When we're building the full name for Class<E>, we'll start
22203 at Class, and go look over its template type parameters,
22204 finding E. We'll then try to build the full name of E, and
22205 reach here. We're now trying to build the full name of E,
22206 and look over the parent DIE for containing scope. In the
22207 broken case, if we followed the parent DIE of E, we'd again
22208 find Class, and once again go look at its template type
22209 arguments, etc., etc. Simply don't consider such parent die
22210 as source-level parent of this die (it can't be, the language
22211 doesn't allow it), and break the loop here. */
22212 name = dwarf2_name (die, cu);
22213 parent_name = dwarf2_name (parent, cu);
22214 complaint (&symfile_complaints,
22215 _("template param type '%s' defined within parent '%s'"),
22216 name ? name : "<unknown>",
22217 parent_name ? parent_name : "<unknown>");
22218 return "";
22219 }
63d06c5c 22220 else
0114d602
DJ
22221 switch (parent->tag)
22222 {
63d06c5c 22223 case DW_TAG_namespace:
0114d602 22224 parent_type = read_type_die (parent, cu);
acebe513
UW
22225 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22226 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22227 Work around this problem here. */
22228 if (cu->language == language_cplus
22229 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22230 return "";
0114d602
DJ
22231 /* We give a name to even anonymous namespaces. */
22232 return TYPE_TAG_NAME (parent_type);
63d06c5c 22233 case DW_TAG_class_type:
680b30c7 22234 case DW_TAG_interface_type:
63d06c5c 22235 case DW_TAG_structure_type:
0114d602 22236 case DW_TAG_union_type:
f55ee35c 22237 case DW_TAG_module:
0114d602
DJ
22238 parent_type = read_type_die (parent, cu);
22239 if (TYPE_TAG_NAME (parent_type) != NULL)
22240 return TYPE_TAG_NAME (parent_type);
22241 else
22242 /* An anonymous structure is only allowed non-static data
22243 members; no typedefs, no member functions, et cetera.
22244 So it does not need a prefix. */
22245 return "";
abc72ce4 22246 case DW_TAG_compile_unit:
95554aad 22247 case DW_TAG_partial_unit:
abc72ce4
DE
22248 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22249 if (cu->language == language_cplus
8b70b953 22250 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
22251 && die->child != NULL
22252 && (die->tag == DW_TAG_class_type
22253 || die->tag == DW_TAG_structure_type
22254 || die->tag == DW_TAG_union_type))
22255 {
22256 char *name = guess_full_die_structure_name (die, cu);
22257 if (name != NULL)
22258 return name;
22259 }
22260 return "";
3d567982
TT
22261 case DW_TAG_enumeration_type:
22262 parent_type = read_type_die (parent, cu);
22263 if (TYPE_DECLARED_CLASS (parent_type))
22264 {
22265 if (TYPE_TAG_NAME (parent_type) != NULL)
22266 return TYPE_TAG_NAME (parent_type);
22267 return "";
22268 }
22269 /* Fall through. */
63d06c5c 22270 default:
8176b9b8 22271 return determine_prefix (parent, cu);
63d06c5c 22272 }
63d06c5c
DC
22273}
22274
3e43a32a
MS
22275/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22276 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22277 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22278 an obconcat, otherwise allocate storage for the result. The CU argument is
22279 used to determine the language and hence, the appropriate separator. */
987504bb 22280
f55ee35c 22281#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22282
22283static char *
f55ee35c
JK
22284typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22285 int physname, struct dwarf2_cu *cu)
63d06c5c 22286{
f55ee35c 22287 const char *lead = "";
5c315b68 22288 const char *sep;
63d06c5c 22289
3e43a32a
MS
22290 if (suffix == NULL || suffix[0] == '\0'
22291 || prefix == NULL || prefix[0] == '\0')
987504bb 22292 sep = "";
45280282
IB
22293 else if (cu->language == language_d)
22294 {
22295 /* For D, the 'main' function could be defined in any module, but it
22296 should never be prefixed. */
22297 if (strcmp (suffix, "D main") == 0)
22298 {
22299 prefix = "";
22300 sep = "";
22301 }
22302 else
22303 sep = ".";
22304 }
f55ee35c
JK
22305 else if (cu->language == language_fortran && physname)
22306 {
22307 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22308 DW_AT_MIPS_linkage_name is preferred and used instead. */
22309
22310 lead = "__";
22311 sep = "_MOD_";
22312 }
987504bb
JJ
22313 else
22314 sep = "::";
63d06c5c 22315
6dd47d34
DE
22316 if (prefix == NULL)
22317 prefix = "";
22318 if (suffix == NULL)
22319 suffix = "";
22320
987504bb
JJ
22321 if (obs == NULL)
22322 {
3e43a32a 22323 char *retval
224c3ddb
SM
22324 = ((char *)
22325 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22326
f55ee35c
JK
22327 strcpy (retval, lead);
22328 strcat (retval, prefix);
6dd47d34
DE
22329 strcat (retval, sep);
22330 strcat (retval, suffix);
63d06c5c
DC
22331 return retval;
22332 }
987504bb
JJ
22333 else
22334 {
22335 /* We have an obstack. */
f55ee35c 22336 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22337 }
63d06c5c
DC
22338}
22339
c906108c
SS
22340/* Return sibling of die, NULL if no sibling. */
22341
f9aca02d 22342static struct die_info *
fba45db2 22343sibling_die (struct die_info *die)
c906108c 22344{
639d11d3 22345 return die->sibling;
c906108c
SS
22346}
22347
71c25dea
TT
22348/* Get name of a die, return NULL if not found. */
22349
15d034d0
TT
22350static const char *
22351dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22352 struct obstack *obstack)
22353{
22354 if (name && cu->language == language_cplus)
22355 {
2f408ecb 22356 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22357
2f408ecb 22358 if (!canon_name.empty ())
71c25dea 22359 {
2f408ecb
PA
22360 if (canon_name != name)
22361 name = (const char *) obstack_copy0 (obstack,
22362 canon_name.c_str (),
22363 canon_name.length ());
71c25dea
TT
22364 }
22365 }
22366
22367 return name;
c906108c
SS
22368}
22369
96553a0c
DE
22370/* Get name of a die, return NULL if not found.
22371 Anonymous namespaces are converted to their magic string. */
9219021c 22372
15d034d0 22373static const char *
e142c38c 22374dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22375{
22376 struct attribute *attr;
22377
e142c38c 22378 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22379 if ((!attr || !DW_STRING (attr))
96553a0c 22380 && die->tag != DW_TAG_namespace
53832f31
TT
22381 && die->tag != DW_TAG_class_type
22382 && die->tag != DW_TAG_interface_type
22383 && die->tag != DW_TAG_structure_type
22384 && die->tag != DW_TAG_union_type)
71c25dea
TT
22385 return NULL;
22386
22387 switch (die->tag)
22388 {
22389 case DW_TAG_compile_unit:
95554aad 22390 case DW_TAG_partial_unit:
71c25dea
TT
22391 /* Compilation units have a DW_AT_name that is a filename, not
22392 a source language identifier. */
22393 case DW_TAG_enumeration_type:
22394 case DW_TAG_enumerator:
22395 /* These tags always have simple identifiers already; no need
22396 to canonicalize them. */
22397 return DW_STRING (attr);
907af001 22398
96553a0c
DE
22399 case DW_TAG_namespace:
22400 if (attr != NULL && DW_STRING (attr) != NULL)
22401 return DW_STRING (attr);
22402 return CP_ANONYMOUS_NAMESPACE_STR;
22403
907af001
UW
22404 case DW_TAG_class_type:
22405 case DW_TAG_interface_type:
22406 case DW_TAG_structure_type:
22407 case DW_TAG_union_type:
22408 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22409 structures or unions. These were of the form "._%d" in GCC 4.1,
22410 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22411 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22412 if (attr && DW_STRING (attr)
61012eef
GB
22413 && (startswith (DW_STRING (attr), "._")
22414 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22415 return NULL;
53832f31
TT
22416
22417 /* GCC might emit a nameless typedef that has a linkage name. See
22418 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22419 if (!attr || DW_STRING (attr) == NULL)
22420 {
df5c6c50 22421 char *demangled = NULL;
53832f31 22422
73b9be8b 22423 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22424 if (attr == NULL || DW_STRING (attr) == NULL)
22425 return NULL;
22426
df5c6c50
JK
22427 /* Avoid demangling DW_STRING (attr) the second time on a second
22428 call for the same DIE. */
22429 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22430 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22431
22432 if (demangled)
22433 {
e6a959d6 22434 const char *base;
96408a79 22435
53832f31 22436 /* FIXME: we already did this for the partial symbol... */
34a68019 22437 DW_STRING (attr)
224c3ddb
SM
22438 = ((const char *)
22439 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
22440 demangled, strlen (demangled)));
53832f31
TT
22441 DW_STRING_IS_CANONICAL (attr) = 1;
22442 xfree (demangled);
96408a79
SA
22443
22444 /* Strip any leading namespaces/classes, keep only the base name.
22445 DW_AT_name for named DIEs does not contain the prefixes. */
22446 base = strrchr (DW_STRING (attr), ':');
22447 if (base && base > DW_STRING (attr) && base[-1] == ':')
22448 return &base[1];
22449 else
22450 return DW_STRING (attr);
53832f31
TT
22451 }
22452 }
907af001
UW
22453 break;
22454
71c25dea 22455 default:
907af001
UW
22456 break;
22457 }
22458
22459 if (!DW_STRING_IS_CANONICAL (attr))
22460 {
22461 DW_STRING (attr)
22462 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 22463 &cu->objfile->per_bfd->storage_obstack);
907af001 22464 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22465 }
907af001 22466 return DW_STRING (attr);
9219021c
DC
22467}
22468
22469/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22470 is none. *EXT_CU is the CU containing DIE on input, and the CU
22471 containing the return value on output. */
9219021c
DC
22472
22473static struct die_info *
f2f0e013 22474dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22475{
22476 struct attribute *attr;
9219021c 22477
f2f0e013 22478 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22479 if (attr == NULL)
22480 return NULL;
22481
f2f0e013 22482 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22483}
22484
c906108c
SS
22485/* Convert a DIE tag into its string name. */
22486
f39c6ffd 22487static const char *
aa1ee363 22488dwarf_tag_name (unsigned tag)
c906108c 22489{
f39c6ffd
TT
22490 const char *name = get_DW_TAG_name (tag);
22491
22492 if (name == NULL)
22493 return "DW_TAG_<unknown>";
22494
22495 return name;
c906108c
SS
22496}
22497
22498/* Convert a DWARF attribute code into its string name. */
22499
f39c6ffd 22500static const char *
aa1ee363 22501dwarf_attr_name (unsigned attr)
c906108c 22502{
f39c6ffd
TT
22503 const char *name;
22504
c764a876 22505#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22506 if (attr == DW_AT_MIPS_fde)
22507 return "DW_AT_MIPS_fde";
22508#else
22509 if (attr == DW_AT_HP_block_index)
22510 return "DW_AT_HP_block_index";
c764a876 22511#endif
f39c6ffd
TT
22512
22513 name = get_DW_AT_name (attr);
22514
22515 if (name == NULL)
22516 return "DW_AT_<unknown>";
22517
22518 return name;
c906108c
SS
22519}
22520
22521/* Convert a DWARF value form code into its string name. */
22522
f39c6ffd 22523static const char *
aa1ee363 22524dwarf_form_name (unsigned form)
c906108c 22525{
f39c6ffd
TT
22526 const char *name = get_DW_FORM_name (form);
22527
22528 if (name == NULL)
22529 return "DW_FORM_<unknown>";
22530
22531 return name;
c906108c
SS
22532}
22533
a121b7c1 22534static const char *
fba45db2 22535dwarf_bool_name (unsigned mybool)
c906108c
SS
22536{
22537 if (mybool)
22538 return "TRUE";
22539 else
22540 return "FALSE";
22541}
22542
22543/* Convert a DWARF type code into its string name. */
22544
f39c6ffd 22545static const char *
aa1ee363 22546dwarf_type_encoding_name (unsigned enc)
c906108c 22547{
f39c6ffd 22548 const char *name = get_DW_ATE_name (enc);
c906108c 22549
f39c6ffd
TT
22550 if (name == NULL)
22551 return "DW_ATE_<unknown>";
c906108c 22552
f39c6ffd 22553 return name;
c906108c 22554}
c906108c 22555
f9aca02d 22556static void
d97bc12b 22557dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22558{
22559 unsigned int i;
22560
d97bc12b
DE
22561 print_spaces (indent, f);
22562 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
9c541725
PA
22563 dwarf_tag_name (die->tag), die->abbrev,
22564 to_underlying (die->sect_off));
d97bc12b
DE
22565
22566 if (die->parent != NULL)
22567 {
22568 print_spaces (indent, f);
22569 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9c541725 22570 to_underlying (die->parent->sect_off));
d97bc12b
DE
22571 }
22572
22573 print_spaces (indent, f);
22574 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22575 dwarf_bool_name (die->child != NULL));
c906108c 22576
d97bc12b
DE
22577 print_spaces (indent, f);
22578 fprintf_unfiltered (f, " attributes:\n");
22579
c906108c
SS
22580 for (i = 0; i < die->num_attrs; ++i)
22581 {
d97bc12b
DE
22582 print_spaces (indent, f);
22583 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22584 dwarf_attr_name (die->attrs[i].name),
22585 dwarf_form_name (die->attrs[i].form));
d97bc12b 22586
c906108c
SS
22587 switch (die->attrs[i].form)
22588 {
c906108c 22589 case DW_FORM_addr:
3019eac3 22590 case DW_FORM_GNU_addr_index:
d97bc12b 22591 fprintf_unfiltered (f, "address: ");
5af949e3 22592 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22593 break;
22594 case DW_FORM_block2:
22595 case DW_FORM_block4:
22596 case DW_FORM_block:
22597 case DW_FORM_block1:
56eb65bd
SP
22598 fprintf_unfiltered (f, "block: size %s",
22599 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22600 break;
2dc7f7b3 22601 case DW_FORM_exprloc:
56eb65bd
SP
22602 fprintf_unfiltered (f, "expression: size %s",
22603 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22604 break;
0224619f
JK
22605 case DW_FORM_data16:
22606 fprintf_unfiltered (f, "constant of 16 bytes");
22607 break;
4568ecf9
DE
22608 case DW_FORM_ref_addr:
22609 fprintf_unfiltered (f, "ref address: ");
22610 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22611 break;
36586728
TT
22612 case DW_FORM_GNU_ref_alt:
22613 fprintf_unfiltered (f, "alt ref address: ");
22614 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22615 break;
10b3939b
DJ
22616 case DW_FORM_ref1:
22617 case DW_FORM_ref2:
22618 case DW_FORM_ref4:
4568ecf9
DE
22619 case DW_FORM_ref8:
22620 case DW_FORM_ref_udata:
d97bc12b 22621 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22622 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22623 break;
c906108c
SS
22624 case DW_FORM_data1:
22625 case DW_FORM_data2:
22626 case DW_FORM_data4:
ce5d95e1 22627 case DW_FORM_data8:
c906108c
SS
22628 case DW_FORM_udata:
22629 case DW_FORM_sdata:
43bbcdc2
PH
22630 fprintf_unfiltered (f, "constant: %s",
22631 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22632 break;
2dc7f7b3
TT
22633 case DW_FORM_sec_offset:
22634 fprintf_unfiltered (f, "section offset: %s",
22635 pulongest (DW_UNSND (&die->attrs[i])));
22636 break;
55f1336d 22637 case DW_FORM_ref_sig8:
ac9ec31b
DE
22638 fprintf_unfiltered (f, "signature: %s",
22639 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22640 break;
c906108c 22641 case DW_FORM_string:
4bdf3d34 22642 case DW_FORM_strp:
43988095 22643 case DW_FORM_line_strp:
3019eac3 22644 case DW_FORM_GNU_str_index:
36586728 22645 case DW_FORM_GNU_strp_alt:
8285870a 22646 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22647 DW_STRING (&die->attrs[i])
8285870a
JK
22648 ? DW_STRING (&die->attrs[i]) : "",
22649 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22650 break;
22651 case DW_FORM_flag:
22652 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22653 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22654 else
d97bc12b 22655 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22656 break;
2dc7f7b3
TT
22657 case DW_FORM_flag_present:
22658 fprintf_unfiltered (f, "flag: TRUE");
22659 break;
a8329558 22660 case DW_FORM_indirect:
0963b4bd
MS
22661 /* The reader will have reduced the indirect form to
22662 the "base form" so this form should not occur. */
3e43a32a
MS
22663 fprintf_unfiltered (f,
22664 "unexpected attribute form: DW_FORM_indirect");
a8329558 22665 break;
663c44ac
JK
22666 case DW_FORM_implicit_const:
22667 fprintf_unfiltered (f, "constant: %s",
22668 plongest (DW_SND (&die->attrs[i])));
22669 break;
c906108c 22670 default:
d97bc12b 22671 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22672 die->attrs[i].form);
d97bc12b 22673 break;
c906108c 22674 }
d97bc12b 22675 fprintf_unfiltered (f, "\n");
c906108c
SS
22676 }
22677}
22678
f9aca02d 22679static void
d97bc12b 22680dump_die_for_error (struct die_info *die)
c906108c 22681{
d97bc12b
DE
22682 dump_die_shallow (gdb_stderr, 0, die);
22683}
22684
22685static void
22686dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22687{
22688 int indent = level * 4;
22689
22690 gdb_assert (die != NULL);
22691
22692 if (level >= max_level)
22693 return;
22694
22695 dump_die_shallow (f, indent, die);
22696
22697 if (die->child != NULL)
c906108c 22698 {
d97bc12b
DE
22699 print_spaces (indent, f);
22700 fprintf_unfiltered (f, " Children:");
22701 if (level + 1 < max_level)
22702 {
22703 fprintf_unfiltered (f, "\n");
22704 dump_die_1 (f, level + 1, max_level, die->child);
22705 }
22706 else
22707 {
3e43a32a
MS
22708 fprintf_unfiltered (f,
22709 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22710 }
22711 }
22712
22713 if (die->sibling != NULL && level > 0)
22714 {
22715 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22716 }
22717}
22718
d97bc12b
DE
22719/* This is called from the pdie macro in gdbinit.in.
22720 It's not static so gcc will keep a copy callable from gdb. */
22721
22722void
22723dump_die (struct die_info *die, int max_level)
22724{
22725 dump_die_1 (gdb_stdlog, 0, max_level, die);
22726}
22727
f9aca02d 22728static void
51545339 22729store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22730{
51545339 22731 void **slot;
c906108c 22732
9c541725
PA
22733 slot = htab_find_slot_with_hash (cu->die_hash, die,
22734 to_underlying (die->sect_off),
b64f50a1 22735 INSERT);
51545339
DJ
22736
22737 *slot = die;
c906108c
SS
22738}
22739
b64f50a1
JK
22740/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22741 required kind. */
22742
22743static sect_offset
ff39bb5e 22744dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22745{
7771576e 22746 if (attr_form_is_ref (attr))
9c541725 22747 return (sect_offset) DW_UNSND (attr);
93311388
DE
22748
22749 complaint (&symfile_complaints,
22750 _("unsupported die ref attribute form: '%s'"),
22751 dwarf_form_name (attr->form));
9c541725 22752 return {};
c906108c
SS
22753}
22754
43bbcdc2
PH
22755/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22756 * the value held by the attribute is not constant. */
a02abb62 22757
43bbcdc2 22758static LONGEST
ff39bb5e 22759dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22760{
663c44ac 22761 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22762 return DW_SND (attr);
22763 else if (attr->form == DW_FORM_udata
22764 || attr->form == DW_FORM_data1
22765 || attr->form == DW_FORM_data2
22766 || attr->form == DW_FORM_data4
22767 || attr->form == DW_FORM_data8)
22768 return DW_UNSND (attr);
22769 else
22770 {
0224619f 22771 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
22772 complaint (&symfile_complaints,
22773 _("Attribute value is not a constant (%s)"),
a02abb62
JB
22774 dwarf_form_name (attr->form));
22775 return default_value;
22776 }
22777}
22778
348e048f
DE
22779/* Follow reference or signature attribute ATTR of SRC_DIE.
22780 On entry *REF_CU is the CU of SRC_DIE.
22781 On exit *REF_CU is the CU of the result. */
22782
22783static struct die_info *
ff39bb5e 22784follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22785 struct dwarf2_cu **ref_cu)
22786{
22787 struct die_info *die;
22788
7771576e 22789 if (attr_form_is_ref (attr))
348e048f 22790 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22791 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22792 die = follow_die_sig (src_die, attr, ref_cu);
22793 else
22794 {
22795 dump_die_for_error (src_die);
22796 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 22797 objfile_name ((*ref_cu)->objfile));
348e048f
DE
22798 }
22799
22800 return die;
03dd20cc
DJ
22801}
22802
5c631832 22803/* Follow reference OFFSET.
673bfd45
DE
22804 On entry *REF_CU is the CU of the source die referencing OFFSET.
22805 On exit *REF_CU is the CU of the result.
22806 Returns NULL if OFFSET is invalid. */
f504f079 22807
f9aca02d 22808static struct die_info *
9c541725 22809follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22810 struct dwarf2_cu **ref_cu)
c906108c 22811{
10b3939b 22812 struct die_info temp_die;
f2f0e013 22813 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 22814
348e048f
DE
22815 gdb_assert (cu->per_cu != NULL);
22816
98bfdba5
PA
22817 target_cu = cu;
22818
3019eac3 22819 if (cu->per_cu->is_debug_types)
348e048f
DE
22820 {
22821 /* .debug_types CUs cannot reference anything outside their CU.
22822 If they need to, they have to reference a signatured type via
55f1336d 22823 DW_FORM_ref_sig8. */
9c541725 22824 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 22825 return NULL;
348e048f 22826 }
36586728 22827 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 22828 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
22829 {
22830 struct dwarf2_per_cu_data *per_cu;
9a619af0 22831
9c541725 22832 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 22833 cu->objfile);
03dd20cc
DJ
22834
22835 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
22836 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22837 load_full_comp_unit (per_cu, cu->language);
03dd20cc 22838
10b3939b
DJ
22839 target_cu = per_cu->cu;
22840 }
98bfdba5
PA
22841 else if (cu->dies == NULL)
22842 {
22843 /* We're loading full DIEs during partial symbol reading. */
22844 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 22845 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 22846 }
c906108c 22847
f2f0e013 22848 *ref_cu = target_cu;
9c541725 22849 temp_die.sect_off = sect_off;
9a3c8263 22850 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22851 &temp_die,
22852 to_underlying (sect_off));
5c631832 22853}
10b3939b 22854
5c631832
JK
22855/* Follow reference attribute ATTR of SRC_DIE.
22856 On entry *REF_CU is the CU of SRC_DIE.
22857 On exit *REF_CU is the CU of the result. */
22858
22859static struct die_info *
ff39bb5e 22860follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22861 struct dwarf2_cu **ref_cu)
22862{
9c541725 22863 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22864 struct dwarf2_cu *cu = *ref_cu;
22865 struct die_info *die;
22866
9c541725 22867 die = follow_die_offset (sect_off,
36586728
TT
22868 (attr->form == DW_FORM_GNU_ref_alt
22869 || cu->per_cu->is_dwz),
22870 ref_cu);
5c631832
JK
22871 if (!die)
22872 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
22873 "at 0x%x [in module %s]"),
9c541725 22874 to_underlying (sect_off), to_underlying (src_die->sect_off),
4262abfb 22875 objfile_name (cu->objfile));
348e048f 22876
5c631832
JK
22877 return die;
22878}
22879
9c541725 22880/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b
JK
22881 Returned value is intended for DW_OP_call*. Returned
22882 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
22883
22884struct dwarf2_locexpr_baton
9c541725 22885dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
22886 struct dwarf2_per_cu_data *per_cu,
22887 CORE_ADDR (*get_frame_pc) (void *baton),
22888 void *baton)
5c631832 22889{
918dd910 22890 struct dwarf2_cu *cu;
5c631832
JK
22891 struct die_info *die;
22892 struct attribute *attr;
22893 struct dwarf2_locexpr_baton retval;
22894
8cf6f0b1
TT
22895 dw2_setup (per_cu->objfile);
22896
918dd910
JK
22897 if (per_cu->cu == NULL)
22898 load_cu (per_cu);
22899 cu = per_cu->cu;
cc12ce38
DE
22900 if (cu == NULL)
22901 {
22902 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22903 Instead just throw an error, not much else we can do. */
22904 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 22905 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 22906 }
918dd910 22907
9c541725 22908 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832
JK
22909 if (!die)
22910 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 22911 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
22912
22913 attr = dwarf2_attr (die, DW_AT_location, cu);
22914 if (!attr)
22915 {
e103e986
JK
22916 /* DWARF: "If there is no such attribute, then there is no effect.".
22917 DATA is ignored if SIZE is 0. */
5c631832 22918
e103e986 22919 retval.data = NULL;
5c631832
JK
22920 retval.size = 0;
22921 }
8cf6f0b1
TT
22922 else if (attr_form_is_section_offset (attr))
22923 {
22924 struct dwarf2_loclist_baton loclist_baton;
22925 CORE_ADDR pc = (*get_frame_pc) (baton);
22926 size_t size;
22927
22928 fill_in_loclist_baton (cu, &loclist_baton, attr);
22929
22930 retval.data = dwarf2_find_location_expression (&loclist_baton,
22931 &size, pc);
22932 retval.size = size;
22933 }
5c631832
JK
22934 else
22935 {
22936 if (!attr_form_is_block (attr))
22937 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
22938 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9c541725 22939 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
22940
22941 retval.data = DW_BLOCK (attr)->data;
22942 retval.size = DW_BLOCK (attr)->size;
22943 }
22944 retval.per_cu = cu->per_cu;
918dd910 22945
918dd910
JK
22946 age_cached_comp_units ();
22947
5c631832 22948 return retval;
348e048f
DE
22949}
22950
8b9737bf
TT
22951/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22952 offset. */
22953
22954struct dwarf2_locexpr_baton
22955dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22956 struct dwarf2_per_cu_data *per_cu,
22957 CORE_ADDR (*get_frame_pc) (void *baton),
22958 void *baton)
22959{
9c541725 22960 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22961
9c541725 22962 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22963}
22964
b6807d98
TT
22965/* Write a constant of a given type as target-ordered bytes into
22966 OBSTACK. */
22967
22968static const gdb_byte *
22969write_constant_as_bytes (struct obstack *obstack,
22970 enum bfd_endian byte_order,
22971 struct type *type,
22972 ULONGEST value,
22973 LONGEST *len)
22974{
22975 gdb_byte *result;
22976
22977 *len = TYPE_LENGTH (type);
224c3ddb 22978 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22979 store_unsigned_integer (result, *len, byte_order, value);
22980
22981 return result;
22982}
22983
22984/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22985 pointer to the constant bytes and set LEN to the length of the
22986 data. If memory is needed, allocate it on OBSTACK. If the DIE
22987 does not have a DW_AT_const_value, return NULL. */
22988
22989const gdb_byte *
9c541725 22990dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
22991 struct dwarf2_per_cu_data *per_cu,
22992 struct obstack *obstack,
22993 LONGEST *len)
22994{
22995 struct dwarf2_cu *cu;
22996 struct die_info *die;
22997 struct attribute *attr;
22998 const gdb_byte *result = NULL;
22999 struct type *type;
23000 LONGEST value;
23001 enum bfd_endian byte_order;
23002
23003 dw2_setup (per_cu->objfile);
23004
23005 if (per_cu->cu == NULL)
23006 load_cu (per_cu);
23007 cu = per_cu->cu;
cc12ce38
DE
23008 if (cu == NULL)
23009 {
23010 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23011 Instead just throw an error, not much else we can do. */
23012 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 23013 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 23014 }
b6807d98 23015
9c541725 23016 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98
TT
23017 if (!die)
23018 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 23019 to_underlying (sect_off), objfile_name (per_cu->objfile));
b6807d98
TT
23020
23021
23022 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23023 if (attr == NULL)
23024 return NULL;
23025
23026 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
23027 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23028
23029 switch (attr->form)
23030 {
23031 case DW_FORM_addr:
23032 case DW_FORM_GNU_addr_index:
23033 {
23034 gdb_byte *tem;
23035
23036 *len = cu->header.addr_size;
224c3ddb 23037 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23038 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23039 result = tem;
23040 }
23041 break;
23042 case DW_FORM_string:
23043 case DW_FORM_strp:
23044 case DW_FORM_GNU_str_index:
23045 case DW_FORM_GNU_strp_alt:
23046 /* DW_STRING is already allocated on the objfile obstack, point
23047 directly to it. */
23048 result = (const gdb_byte *) DW_STRING (attr);
23049 *len = strlen (DW_STRING (attr));
23050 break;
23051 case DW_FORM_block1:
23052 case DW_FORM_block2:
23053 case DW_FORM_block4:
23054 case DW_FORM_block:
23055 case DW_FORM_exprloc:
0224619f 23056 case DW_FORM_data16:
b6807d98
TT
23057 result = DW_BLOCK (attr)->data;
23058 *len = DW_BLOCK (attr)->size;
23059 break;
23060
23061 /* The DW_AT_const_value attributes are supposed to carry the
23062 symbol's value "represented as it would be on the target
23063 architecture." By the time we get here, it's already been
23064 converted to host endianness, so we just need to sign- or
23065 zero-extend it as appropriate. */
23066 case DW_FORM_data1:
23067 type = die_type (die, cu);
23068 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23069 if (result == NULL)
23070 result = write_constant_as_bytes (obstack, byte_order,
23071 type, value, len);
23072 break;
23073 case DW_FORM_data2:
23074 type = die_type (die, cu);
23075 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23076 if (result == NULL)
23077 result = write_constant_as_bytes (obstack, byte_order,
23078 type, value, len);
23079 break;
23080 case DW_FORM_data4:
23081 type = die_type (die, cu);
23082 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23083 if (result == NULL)
23084 result = write_constant_as_bytes (obstack, byte_order,
23085 type, value, len);
23086 break;
23087 case DW_FORM_data8:
23088 type = die_type (die, cu);
23089 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23090 if (result == NULL)
23091 result = write_constant_as_bytes (obstack, byte_order,
23092 type, value, len);
23093 break;
23094
23095 case DW_FORM_sdata:
663c44ac 23096 case DW_FORM_implicit_const:
b6807d98
TT
23097 type = die_type (die, cu);
23098 result = write_constant_as_bytes (obstack, byte_order,
23099 type, DW_SND (attr), len);
23100 break;
23101
23102 case DW_FORM_udata:
23103 type = die_type (die, cu);
23104 result = write_constant_as_bytes (obstack, byte_order,
23105 type, DW_UNSND (attr), len);
23106 break;
23107
23108 default:
23109 complaint (&symfile_complaints,
23110 _("unsupported const value attribute form: '%s'"),
23111 dwarf_form_name (attr->form));
23112 break;
23113 }
23114
23115 return result;
23116}
23117
7942e96e
AA
23118/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23119 valid type for this die is found. */
23120
23121struct type *
9c541725 23122dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23123 struct dwarf2_per_cu_data *per_cu)
23124{
23125 struct dwarf2_cu *cu;
23126 struct die_info *die;
23127
23128 dw2_setup (per_cu->objfile);
23129
23130 if (per_cu->cu == NULL)
23131 load_cu (per_cu);
23132 cu = per_cu->cu;
23133 if (!cu)
23134 return NULL;
23135
9c541725 23136 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23137 if (!die)
23138 return NULL;
23139
23140 return die_type (die, cu);
23141}
23142
8a9b8146
TT
23143/* Return the type of the DIE at DIE_OFFSET in the CU named by
23144 PER_CU. */
23145
23146struct type *
b64f50a1 23147dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23148 struct dwarf2_per_cu_data *per_cu)
23149{
8a9b8146 23150 dw2_setup (per_cu->objfile);
b64f50a1 23151
9c541725 23152 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23153 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23154}
23155
ac9ec31b 23156/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23157 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23158 On exit *REF_CU is the CU of the result.
23159 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23160
23161static struct die_info *
ac9ec31b
DE
23162follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23163 struct dwarf2_cu **ref_cu)
348e048f 23164{
348e048f 23165 struct die_info temp_die;
348e048f
DE
23166 struct dwarf2_cu *sig_cu;
23167 struct die_info *die;
23168
ac9ec31b
DE
23169 /* While it might be nice to assert sig_type->type == NULL here,
23170 we can get here for DW_AT_imported_declaration where we need
23171 the DIE not the type. */
348e048f
DE
23172
23173 /* If necessary, add it to the queue and load its DIEs. */
23174
95554aad 23175 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23176 read_signatured_type (sig_type);
348e048f 23177
348e048f 23178 sig_cu = sig_type->per_cu.cu;
69d751e3 23179 gdb_assert (sig_cu != NULL);
9c541725
PA
23180 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23181 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23182 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23183 to_underlying (temp_die.sect_off));
348e048f
DE
23184 if (die)
23185 {
796a7ff8
DE
23186 /* For .gdb_index version 7 keep track of included TUs.
23187 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23188 if (dwarf2_per_objfile->index_table != NULL
23189 && dwarf2_per_objfile->index_table->version <= 7)
23190 {
23191 VEC_safe_push (dwarf2_per_cu_ptr,
23192 (*ref_cu)->per_cu->imported_symtabs,
23193 sig_cu->per_cu);
23194 }
23195
348e048f
DE
23196 *ref_cu = sig_cu;
23197 return die;
23198 }
23199
ac9ec31b
DE
23200 return NULL;
23201}
23202
23203/* Follow signatured type referenced by ATTR in SRC_DIE.
23204 On entry *REF_CU is the CU of SRC_DIE.
23205 On exit *REF_CU is the CU of the result.
23206 The result is the DIE of the type.
23207 If the referenced type cannot be found an error is thrown. */
23208
23209static struct die_info *
ff39bb5e 23210follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23211 struct dwarf2_cu **ref_cu)
23212{
23213 ULONGEST signature = DW_SIGNATURE (attr);
23214 struct signatured_type *sig_type;
23215 struct die_info *die;
23216
23217 gdb_assert (attr->form == DW_FORM_ref_sig8);
23218
a2ce51a0 23219 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23220 /* sig_type will be NULL if the signatured type is missing from
23221 the debug info. */
23222 if (sig_type == NULL)
23223 {
23224 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23225 " from DIE at 0x%x [in module %s]"),
9c541725 23226 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 23227 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
23228 }
23229
23230 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23231 if (die == NULL)
23232 {
23233 dump_die_for_error (src_die);
23234 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23235 " from DIE at 0x%x [in module %s]"),
9c541725 23236 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 23237 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
23238 }
23239
23240 return die;
23241}
23242
23243/* Get the type specified by SIGNATURE referenced in DIE/CU,
23244 reading in and processing the type unit if necessary. */
23245
23246static struct type *
23247get_signatured_type (struct die_info *die, ULONGEST signature,
23248 struct dwarf2_cu *cu)
23249{
23250 struct signatured_type *sig_type;
23251 struct dwarf2_cu *type_cu;
23252 struct die_info *type_die;
23253 struct type *type;
23254
a2ce51a0 23255 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23256 /* sig_type will be NULL if the signatured type is missing from
23257 the debug info. */
23258 if (sig_type == NULL)
23259 {
23260 complaint (&symfile_complaints,
23261 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23262 " from DIE at 0x%x [in module %s]"),
9c541725 23263 hex_string (signature), to_underlying (die->sect_off),
4262abfb 23264 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23265 return build_error_marker_type (cu, die);
23266 }
23267
23268 /* If we already know the type we're done. */
23269 if (sig_type->type != NULL)
23270 return sig_type->type;
23271
23272 type_cu = cu;
23273 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23274 if (type_die != NULL)
23275 {
23276 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23277 is created. This is important, for example, because for c++ classes
23278 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23279 type = read_type_die (type_die, type_cu);
23280 if (type == NULL)
23281 {
23282 complaint (&symfile_complaints,
23283 _("Dwarf Error: Cannot build signatured type %s"
23284 " referenced from DIE at 0x%x [in module %s]"),
9c541725 23285 hex_string (signature), to_underlying (die->sect_off),
4262abfb 23286 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23287 type = build_error_marker_type (cu, die);
23288 }
23289 }
23290 else
23291 {
23292 complaint (&symfile_complaints,
23293 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23294 " from DIE at 0x%x [in module %s]"),
9c541725 23295 hex_string (signature), to_underlying (die->sect_off),
4262abfb 23296 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23297 type = build_error_marker_type (cu, die);
23298 }
23299 sig_type->type = type;
23300
23301 return type;
23302}
23303
23304/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23305 reading in and processing the type unit if necessary. */
23306
23307static struct type *
ff39bb5e 23308get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23309 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23310{
23311 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23312 if (attr_form_is_ref (attr))
ac9ec31b
DE
23313 {
23314 struct dwarf2_cu *type_cu = cu;
23315 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23316
23317 return read_type_die (type_die, type_cu);
23318 }
23319 else if (attr->form == DW_FORM_ref_sig8)
23320 {
23321 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23322 }
23323 else
23324 {
23325 complaint (&symfile_complaints,
23326 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23327 " at 0x%x [in module %s]"),
9c541725 23328 dwarf_form_name (attr->form), to_underlying (die->sect_off),
4262abfb 23329 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23330 return build_error_marker_type (cu, die);
23331 }
348e048f
DE
23332}
23333
e5fe5e75 23334/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23335
23336static void
e5fe5e75 23337load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23338{
52dc124a 23339 struct signatured_type *sig_type;
348e048f 23340
f4dc4d17
DE
23341 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23342 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23343
6721b2ec
DE
23344 /* We have the per_cu, but we need the signatured_type.
23345 Fortunately this is an easy translation. */
23346 gdb_assert (per_cu->is_debug_types);
23347 sig_type = (struct signatured_type *) per_cu;
348e048f 23348
6721b2ec 23349 gdb_assert (per_cu->cu == NULL);
348e048f 23350
52dc124a 23351 read_signatured_type (sig_type);
348e048f 23352
6721b2ec 23353 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23354}
23355
dee91e82
DE
23356/* die_reader_func for read_signatured_type.
23357 This is identical to load_full_comp_unit_reader,
23358 but is kept separate for now. */
348e048f
DE
23359
23360static void
dee91e82 23361read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23362 const gdb_byte *info_ptr,
dee91e82
DE
23363 struct die_info *comp_unit_die,
23364 int has_children,
23365 void *data)
348e048f 23366{
dee91e82 23367 struct dwarf2_cu *cu = reader->cu;
348e048f 23368
dee91e82
DE
23369 gdb_assert (cu->die_hash == NULL);
23370 cu->die_hash =
23371 htab_create_alloc_ex (cu->header.length / 12,
23372 die_hash,
23373 die_eq,
23374 NULL,
23375 &cu->comp_unit_obstack,
23376 hashtab_obstack_allocate,
23377 dummy_obstack_deallocate);
348e048f 23378
dee91e82
DE
23379 if (has_children)
23380 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23381 &info_ptr, comp_unit_die);
23382 cu->dies = comp_unit_die;
23383 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23384
23385 /* We try not to read any attributes in this function, because not
9cdd5dbd 23386 all CUs needed for references have been loaded yet, and symbol
348e048f 23387 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23388 or we won't be able to build types correctly.
23389 Similarly, if we do not read the producer, we can not apply
23390 producer-specific interpretation. */
95554aad 23391 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23392}
348e048f 23393
3019eac3
DE
23394/* Read in a signatured type and build its CU and DIEs.
23395 If the type is a stub for the real type in a DWO file,
23396 read in the real type from the DWO file as well. */
dee91e82
DE
23397
23398static void
23399read_signatured_type (struct signatured_type *sig_type)
23400{
23401 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23402
3019eac3 23403 gdb_assert (per_cu->is_debug_types);
dee91e82 23404 gdb_assert (per_cu->cu == NULL);
348e048f 23405
f4dc4d17
DE
23406 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23407 read_signatured_type_reader, NULL);
7ee85ab1 23408 sig_type->per_cu.tu_read = 1;
c906108c
SS
23409}
23410
c906108c
SS
23411/* Decode simple location descriptions.
23412 Given a pointer to a dwarf block that defines a location, compute
23413 the location and return the value.
23414
4cecd739
DJ
23415 NOTE drow/2003-11-18: This function is called in two situations
23416 now: for the address of static or global variables (partial symbols
23417 only) and for offsets into structures which are expected to be
23418 (more or less) constant. The partial symbol case should go away,
23419 and only the constant case should remain. That will let this
23420 function complain more accurately. A few special modes are allowed
23421 without complaint for global variables (for instance, global
23422 register values and thread-local values).
c906108c
SS
23423
23424 A location description containing no operations indicates that the
4cecd739 23425 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23426 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23427 callers will only want a very basic result and this can become a
21ae7a4d
JK
23428 complaint.
23429
23430 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23431
23432static CORE_ADDR
e7c27a73 23433decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23434{
e7c27a73 23435 struct objfile *objfile = cu->objfile;
56eb65bd
SP
23436 size_t i;
23437 size_t size = blk->size;
d521ce57 23438 const gdb_byte *data = blk->data;
21ae7a4d
JK
23439 CORE_ADDR stack[64];
23440 int stacki;
23441 unsigned int bytes_read, unsnd;
23442 gdb_byte op;
c906108c 23443
21ae7a4d
JK
23444 i = 0;
23445 stacki = 0;
23446 stack[stacki] = 0;
23447 stack[++stacki] = 0;
23448
23449 while (i < size)
23450 {
23451 op = data[i++];
23452 switch (op)
23453 {
23454 case DW_OP_lit0:
23455 case DW_OP_lit1:
23456 case DW_OP_lit2:
23457 case DW_OP_lit3:
23458 case DW_OP_lit4:
23459 case DW_OP_lit5:
23460 case DW_OP_lit6:
23461 case DW_OP_lit7:
23462 case DW_OP_lit8:
23463 case DW_OP_lit9:
23464 case DW_OP_lit10:
23465 case DW_OP_lit11:
23466 case DW_OP_lit12:
23467 case DW_OP_lit13:
23468 case DW_OP_lit14:
23469 case DW_OP_lit15:
23470 case DW_OP_lit16:
23471 case DW_OP_lit17:
23472 case DW_OP_lit18:
23473 case DW_OP_lit19:
23474 case DW_OP_lit20:
23475 case DW_OP_lit21:
23476 case DW_OP_lit22:
23477 case DW_OP_lit23:
23478 case DW_OP_lit24:
23479 case DW_OP_lit25:
23480 case DW_OP_lit26:
23481 case DW_OP_lit27:
23482 case DW_OP_lit28:
23483 case DW_OP_lit29:
23484 case DW_OP_lit30:
23485 case DW_OP_lit31:
23486 stack[++stacki] = op - DW_OP_lit0;
23487 break;
f1bea926 23488
21ae7a4d
JK
23489 case DW_OP_reg0:
23490 case DW_OP_reg1:
23491 case DW_OP_reg2:
23492 case DW_OP_reg3:
23493 case DW_OP_reg4:
23494 case DW_OP_reg5:
23495 case DW_OP_reg6:
23496 case DW_OP_reg7:
23497 case DW_OP_reg8:
23498 case DW_OP_reg9:
23499 case DW_OP_reg10:
23500 case DW_OP_reg11:
23501 case DW_OP_reg12:
23502 case DW_OP_reg13:
23503 case DW_OP_reg14:
23504 case DW_OP_reg15:
23505 case DW_OP_reg16:
23506 case DW_OP_reg17:
23507 case DW_OP_reg18:
23508 case DW_OP_reg19:
23509 case DW_OP_reg20:
23510 case DW_OP_reg21:
23511 case DW_OP_reg22:
23512 case DW_OP_reg23:
23513 case DW_OP_reg24:
23514 case DW_OP_reg25:
23515 case DW_OP_reg26:
23516 case DW_OP_reg27:
23517 case DW_OP_reg28:
23518 case DW_OP_reg29:
23519 case DW_OP_reg30:
23520 case DW_OP_reg31:
23521 stack[++stacki] = op - DW_OP_reg0;
23522 if (i < size)
23523 dwarf2_complex_location_expr_complaint ();
23524 break;
c906108c 23525
21ae7a4d
JK
23526 case DW_OP_regx:
23527 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23528 i += bytes_read;
23529 stack[++stacki] = unsnd;
23530 if (i < size)
23531 dwarf2_complex_location_expr_complaint ();
23532 break;
c906108c 23533
21ae7a4d
JK
23534 case DW_OP_addr:
23535 stack[++stacki] = read_address (objfile->obfd, &data[i],
23536 cu, &bytes_read);
23537 i += bytes_read;
23538 break;
d53d4ac5 23539
21ae7a4d
JK
23540 case DW_OP_const1u:
23541 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23542 i += 1;
23543 break;
23544
23545 case DW_OP_const1s:
23546 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23547 i += 1;
23548 break;
23549
23550 case DW_OP_const2u:
23551 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23552 i += 2;
23553 break;
23554
23555 case DW_OP_const2s:
23556 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23557 i += 2;
23558 break;
d53d4ac5 23559
21ae7a4d
JK
23560 case DW_OP_const4u:
23561 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23562 i += 4;
23563 break;
23564
23565 case DW_OP_const4s:
23566 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23567 i += 4;
23568 break;
23569
585861ea
JK
23570 case DW_OP_const8u:
23571 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23572 i += 8;
23573 break;
23574
21ae7a4d
JK
23575 case DW_OP_constu:
23576 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23577 &bytes_read);
23578 i += bytes_read;
23579 break;
23580
23581 case DW_OP_consts:
23582 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23583 i += bytes_read;
23584 break;
23585
23586 case DW_OP_dup:
23587 stack[stacki + 1] = stack[stacki];
23588 stacki++;
23589 break;
23590
23591 case DW_OP_plus:
23592 stack[stacki - 1] += stack[stacki];
23593 stacki--;
23594 break;
23595
23596 case DW_OP_plus_uconst:
23597 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23598 &bytes_read);
23599 i += bytes_read;
23600 break;
23601
23602 case DW_OP_minus:
23603 stack[stacki - 1] -= stack[stacki];
23604 stacki--;
23605 break;
23606
23607 case DW_OP_deref:
23608 /* If we're not the last op, then we definitely can't encode
23609 this using GDB's address_class enum. This is valid for partial
23610 global symbols, although the variable's address will be bogus
23611 in the psymtab. */
23612 if (i < size)
23613 dwarf2_complex_location_expr_complaint ();
23614 break;
23615
23616 case DW_OP_GNU_push_tls_address:
4aa4e28b 23617 case DW_OP_form_tls_address:
21ae7a4d
JK
23618 /* The top of the stack has the offset from the beginning
23619 of the thread control block at which the variable is located. */
23620 /* Nothing should follow this operator, so the top of stack would
23621 be returned. */
23622 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23623 address will be bogus in the psymtab. Make it always at least
23624 non-zero to not look as a variable garbage collected by linker
23625 which have DW_OP_addr 0. */
21ae7a4d
JK
23626 if (i < size)
23627 dwarf2_complex_location_expr_complaint ();
585861ea 23628 stack[stacki]++;
21ae7a4d
JK
23629 break;
23630
23631 case DW_OP_GNU_uninit:
23632 break;
23633
3019eac3 23634 case DW_OP_GNU_addr_index:
49f6c839 23635 case DW_OP_GNU_const_index:
3019eac3
DE
23636 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23637 &bytes_read);
23638 i += bytes_read;
23639 break;
23640
21ae7a4d
JK
23641 default:
23642 {
f39c6ffd 23643 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23644
23645 if (name)
23646 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23647 name);
23648 else
23649 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23650 op);
23651 }
23652
23653 return (stack[stacki]);
d53d4ac5 23654 }
3c6e0cb3 23655
21ae7a4d
JK
23656 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23657 outside of the allocated space. Also enforce minimum>0. */
23658 if (stacki >= ARRAY_SIZE (stack) - 1)
23659 {
23660 complaint (&symfile_complaints,
23661 _("location description stack overflow"));
23662 return 0;
23663 }
23664
23665 if (stacki <= 0)
23666 {
23667 complaint (&symfile_complaints,
23668 _("location description stack underflow"));
23669 return 0;
23670 }
23671 }
23672 return (stack[stacki]);
c906108c
SS
23673}
23674
23675/* memory allocation interface */
23676
c906108c 23677static struct dwarf_block *
7b5a2f43 23678dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23679{
8d749320 23680 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23681}
23682
c906108c 23683static struct die_info *
b60c80d6 23684dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23685{
23686 struct die_info *die;
b60c80d6
DJ
23687 size_t size = sizeof (struct die_info);
23688
23689 if (num_attrs > 1)
23690 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23691
b60c80d6 23692 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23693 memset (die, 0, sizeof (struct die_info));
23694 return (die);
23695}
2e276125
JB
23696
23697\f
23698/* Macro support. */
23699
233d95b5
JK
23700/* Return file name relative to the compilation directory of file number I in
23701 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 23702 responsible for freeing it. */
233d95b5 23703
2e276125 23704static char *
233d95b5 23705file_file_name (int file, struct line_header *lh)
2e276125 23706{
6a83a1e6
EZ
23707 /* Is the file number a valid index into the line header's file name
23708 table? Remember that file numbers start with one, not zero. */
fff8551c 23709 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 23710 {
8c43009f 23711 const file_entry &fe = lh->file_names[file - 1];
6e70227d 23712
8c43009f
PA
23713 if (!IS_ABSOLUTE_PATH (fe.name))
23714 {
23715 const char *dir = fe.include_dir (lh);
23716 if (dir != NULL)
23717 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23718 }
23719 return xstrdup (fe.name);
6a83a1e6 23720 }
2e276125
JB
23721 else
23722 {
6a83a1e6
EZ
23723 /* The compiler produced a bogus file number. We can at least
23724 record the macro definitions made in the file, even if we
23725 won't be able to find the file by name. */
23726 char fake_name[80];
9a619af0 23727
8c042590
PM
23728 xsnprintf (fake_name, sizeof (fake_name),
23729 "<bad macro file number %d>", file);
2e276125 23730
6e70227d 23731 complaint (&symfile_complaints,
6a83a1e6
EZ
23732 _("bad file number in macro information (%d)"),
23733 file);
2e276125 23734
6a83a1e6 23735 return xstrdup (fake_name);
2e276125
JB
23736 }
23737}
23738
233d95b5
JK
23739/* Return the full name of file number I in *LH's file name table.
23740 Use COMP_DIR as the name of the current directory of the
23741 compilation. The result is allocated using xmalloc; the caller is
23742 responsible for freeing it. */
23743static char *
23744file_full_name (int file, struct line_header *lh, const char *comp_dir)
23745{
23746 /* Is the file number a valid index into the line header's file name
23747 table? Remember that file numbers start with one, not zero. */
fff8551c 23748 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
23749 {
23750 char *relative = file_file_name (file, lh);
23751
23752 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23753 return relative;
b36cec19
PA
23754 return reconcat (relative, comp_dir, SLASH_STRING,
23755 relative, (char *) NULL);
233d95b5
JK
23756 }
23757 else
23758 return file_file_name (file, lh);
23759}
23760
2e276125
JB
23761
23762static struct macro_source_file *
23763macro_start_file (int file, int line,
23764 struct macro_source_file *current_file,
43f3e411 23765 struct line_header *lh)
2e276125 23766{
233d95b5
JK
23767 /* File name relative to the compilation directory of this source file. */
23768 char *file_name = file_file_name (file, lh);
2e276125 23769
2e276125 23770 if (! current_file)
abc9d0dc 23771 {
fc474241
DE
23772 /* Note: We don't create a macro table for this compilation unit
23773 at all until we actually get a filename. */
43f3e411 23774 struct macro_table *macro_table = get_macro_table ();
fc474241 23775
abc9d0dc
TT
23776 /* If we have no current file, then this must be the start_file
23777 directive for the compilation unit's main source file. */
fc474241
DE
23778 current_file = macro_set_main (macro_table, file_name);
23779 macro_define_special (macro_table);
abc9d0dc 23780 }
2e276125 23781 else
233d95b5 23782 current_file = macro_include (current_file, line, file_name);
2e276125 23783
233d95b5 23784 xfree (file_name);
6e70227d 23785
2e276125
JB
23786 return current_file;
23787}
23788
2e276125
JB
23789static const char *
23790consume_improper_spaces (const char *p, const char *body)
23791{
23792 if (*p == ' ')
23793 {
4d3c2250 23794 complaint (&symfile_complaints,
3e43a32a
MS
23795 _("macro definition contains spaces "
23796 "in formal argument list:\n`%s'"),
4d3c2250 23797 body);
2e276125
JB
23798
23799 while (*p == ' ')
23800 p++;
23801 }
23802
23803 return p;
23804}
23805
23806
23807static void
23808parse_macro_definition (struct macro_source_file *file, int line,
23809 const char *body)
23810{
23811 const char *p;
23812
23813 /* The body string takes one of two forms. For object-like macro
23814 definitions, it should be:
23815
23816 <macro name> " " <definition>
23817
23818 For function-like macro definitions, it should be:
23819
23820 <macro name> "() " <definition>
23821 or
23822 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23823
23824 Spaces may appear only where explicitly indicated, and in the
23825 <definition>.
23826
23827 The Dwarf 2 spec says that an object-like macro's name is always
23828 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23829 the space when the macro's definition is the empty string.
2e276125
JB
23830
23831 The Dwarf 2 spec says that there should be no spaces between the
23832 formal arguments in a function-like macro's formal argument list,
23833 but versions of GCC around March 2002 include spaces after the
23834 commas. */
23835
23836
23837 /* Find the extent of the macro name. The macro name is terminated
23838 by either a space or null character (for an object-like macro) or
23839 an opening paren (for a function-like macro). */
23840 for (p = body; *p; p++)
23841 if (*p == ' ' || *p == '(')
23842 break;
23843
23844 if (*p == ' ' || *p == '\0')
23845 {
23846 /* It's an object-like macro. */
23847 int name_len = p - body;
3f8a7804 23848 char *name = savestring (body, name_len);
2e276125
JB
23849 const char *replacement;
23850
23851 if (*p == ' ')
23852 replacement = body + name_len + 1;
23853 else
23854 {
4d3c2250 23855 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23856 replacement = body + name_len;
23857 }
6e70227d 23858
2e276125
JB
23859 macro_define_object (file, line, name, replacement);
23860
23861 xfree (name);
23862 }
23863 else if (*p == '(')
23864 {
23865 /* It's a function-like macro. */
3f8a7804 23866 char *name = savestring (body, p - body);
2e276125
JB
23867 int argc = 0;
23868 int argv_size = 1;
8d749320 23869 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23870
23871 p++;
23872
23873 p = consume_improper_spaces (p, body);
23874
23875 /* Parse the formal argument list. */
23876 while (*p && *p != ')')
23877 {
23878 /* Find the extent of the current argument name. */
23879 const char *arg_start = p;
23880
23881 while (*p && *p != ',' && *p != ')' && *p != ' ')
23882 p++;
23883
23884 if (! *p || p == arg_start)
4d3c2250 23885 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23886 else
23887 {
23888 /* Make sure argv has room for the new argument. */
23889 if (argc >= argv_size)
23890 {
23891 argv_size *= 2;
224c3ddb 23892 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
23893 }
23894
3f8a7804 23895 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
23896 }
23897
23898 p = consume_improper_spaces (p, body);
23899
23900 /* Consume the comma, if present. */
23901 if (*p == ',')
23902 {
23903 p++;
23904
23905 p = consume_improper_spaces (p, body);
23906 }
23907 }
23908
23909 if (*p == ')')
23910 {
23911 p++;
23912
23913 if (*p == ' ')
23914 /* Perfectly formed definition, no complaints. */
23915 macro_define_function (file, line, name,
6e70227d 23916 argc, (const char **) argv,
2e276125
JB
23917 p + 1);
23918 else if (*p == '\0')
23919 {
23920 /* Complain, but do define it. */
4d3c2250 23921 dwarf2_macro_malformed_definition_complaint (body);
2e276125 23922 macro_define_function (file, line, name,
6e70227d 23923 argc, (const char **) argv,
2e276125
JB
23924 p);
23925 }
23926 else
23927 /* Just complain. */
4d3c2250 23928 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23929 }
23930 else
23931 /* Just complain. */
4d3c2250 23932 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23933
23934 xfree (name);
23935 {
23936 int i;
23937
23938 for (i = 0; i < argc; i++)
23939 xfree (argv[i]);
23940 }
23941 xfree (argv);
23942 }
23943 else
4d3c2250 23944 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23945}
23946
cf2c3c16
TT
23947/* Skip some bytes from BYTES according to the form given in FORM.
23948 Returns the new pointer. */
2e276125 23949
d521ce57
TT
23950static const gdb_byte *
23951skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
23952 enum dwarf_form form,
23953 unsigned int offset_size,
23954 struct dwarf2_section_info *section)
2e276125 23955{
cf2c3c16 23956 unsigned int bytes_read;
2e276125 23957
cf2c3c16 23958 switch (form)
2e276125 23959 {
cf2c3c16
TT
23960 case DW_FORM_data1:
23961 case DW_FORM_flag:
23962 ++bytes;
23963 break;
23964
23965 case DW_FORM_data2:
23966 bytes += 2;
23967 break;
23968
23969 case DW_FORM_data4:
23970 bytes += 4;
23971 break;
23972
23973 case DW_FORM_data8:
23974 bytes += 8;
23975 break;
23976
0224619f
JK
23977 case DW_FORM_data16:
23978 bytes += 16;
23979 break;
23980
cf2c3c16
TT
23981 case DW_FORM_string:
23982 read_direct_string (abfd, bytes, &bytes_read);
23983 bytes += bytes_read;
23984 break;
23985
23986 case DW_FORM_sec_offset:
23987 case DW_FORM_strp:
36586728 23988 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
23989 bytes += offset_size;
23990 break;
23991
23992 case DW_FORM_block:
23993 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23994 bytes += bytes_read;
23995 break;
23996
23997 case DW_FORM_block1:
23998 bytes += 1 + read_1_byte (abfd, bytes);
23999 break;
24000 case DW_FORM_block2:
24001 bytes += 2 + read_2_bytes (abfd, bytes);
24002 break;
24003 case DW_FORM_block4:
24004 bytes += 4 + read_4_bytes (abfd, bytes);
24005 break;
24006
24007 case DW_FORM_sdata:
24008 case DW_FORM_udata:
3019eac3
DE
24009 case DW_FORM_GNU_addr_index:
24010 case DW_FORM_GNU_str_index:
d521ce57 24011 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24012 if (bytes == NULL)
24013 {
24014 dwarf2_section_buffer_overflow_complaint (section);
24015 return NULL;
24016 }
cf2c3c16
TT
24017 break;
24018
663c44ac
JK
24019 case DW_FORM_implicit_const:
24020 break;
24021
cf2c3c16
TT
24022 default:
24023 {
cf2c3c16
TT
24024 complaint (&symfile_complaints,
24025 _("invalid form 0x%x in `%s'"),
a32a8923 24026 form, get_section_name (section));
cf2c3c16
TT
24027 return NULL;
24028 }
2e276125
JB
24029 }
24030
cf2c3c16
TT
24031 return bytes;
24032}
757a13d0 24033
cf2c3c16
TT
24034/* A helper for dwarf_decode_macros that handles skipping an unknown
24035 opcode. Returns an updated pointer to the macro data buffer; or,
24036 on error, issues a complaint and returns NULL. */
757a13d0 24037
d521ce57 24038static const gdb_byte *
cf2c3c16 24039skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24040 const gdb_byte **opcode_definitions,
24041 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24042 bfd *abfd,
24043 unsigned int offset_size,
24044 struct dwarf2_section_info *section)
24045{
24046 unsigned int bytes_read, i;
24047 unsigned long arg;
d521ce57 24048 const gdb_byte *defn;
2e276125 24049
cf2c3c16 24050 if (opcode_definitions[opcode] == NULL)
2e276125 24051 {
cf2c3c16
TT
24052 complaint (&symfile_complaints,
24053 _("unrecognized DW_MACFINO opcode 0x%x"),
24054 opcode);
24055 return NULL;
24056 }
2e276125 24057
cf2c3c16
TT
24058 defn = opcode_definitions[opcode];
24059 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24060 defn += bytes_read;
2e276125 24061
cf2c3c16
TT
24062 for (i = 0; i < arg; ++i)
24063 {
aead7601
SM
24064 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24065 (enum dwarf_form) defn[i], offset_size,
f664829e 24066 section);
cf2c3c16
TT
24067 if (mac_ptr == NULL)
24068 {
24069 /* skip_form_bytes already issued the complaint. */
24070 return NULL;
24071 }
24072 }
757a13d0 24073
cf2c3c16
TT
24074 return mac_ptr;
24075}
757a13d0 24076
cf2c3c16
TT
24077/* A helper function which parses the header of a macro section.
24078 If the macro section is the extended (for now called "GNU") type,
24079 then this updates *OFFSET_SIZE. Returns a pointer to just after
24080 the header, or issues a complaint and returns NULL on error. */
757a13d0 24081
d521ce57
TT
24082static const gdb_byte *
24083dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24084 bfd *abfd,
d521ce57 24085 const gdb_byte *mac_ptr,
cf2c3c16
TT
24086 unsigned int *offset_size,
24087 int section_is_gnu)
24088{
24089 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24090
cf2c3c16
TT
24091 if (section_is_gnu)
24092 {
24093 unsigned int version, flags;
757a13d0 24094
cf2c3c16 24095 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24096 if (version != 4 && version != 5)
cf2c3c16
TT
24097 {
24098 complaint (&symfile_complaints,
24099 _("unrecognized version `%d' in .debug_macro section"),
24100 version);
24101 return NULL;
24102 }
24103 mac_ptr += 2;
757a13d0 24104
cf2c3c16
TT
24105 flags = read_1_byte (abfd, mac_ptr);
24106 ++mac_ptr;
24107 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24108
cf2c3c16
TT
24109 if ((flags & 2) != 0)
24110 /* We don't need the line table offset. */
24111 mac_ptr += *offset_size;
757a13d0 24112
cf2c3c16
TT
24113 /* Vendor opcode descriptions. */
24114 if ((flags & 4) != 0)
24115 {
24116 unsigned int i, count;
757a13d0 24117
cf2c3c16
TT
24118 count = read_1_byte (abfd, mac_ptr);
24119 ++mac_ptr;
24120 for (i = 0; i < count; ++i)
24121 {
24122 unsigned int opcode, bytes_read;
24123 unsigned long arg;
24124
24125 opcode = read_1_byte (abfd, mac_ptr);
24126 ++mac_ptr;
24127 opcode_definitions[opcode] = mac_ptr;
24128 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24129 mac_ptr += bytes_read;
24130 mac_ptr += arg;
24131 }
757a13d0 24132 }
cf2c3c16 24133 }
757a13d0 24134
cf2c3c16
TT
24135 return mac_ptr;
24136}
757a13d0 24137
cf2c3c16 24138/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24139 including DW_MACRO_import. */
cf2c3c16
TT
24140
24141static void
d521ce57
TT
24142dwarf_decode_macro_bytes (bfd *abfd,
24143 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24144 struct macro_source_file *current_file,
43f3e411 24145 struct line_header *lh,
cf2c3c16 24146 struct dwarf2_section_info *section,
36586728 24147 int section_is_gnu, int section_is_dwz,
cf2c3c16 24148 unsigned int offset_size,
8fc3fc34 24149 htab_t include_hash)
cf2c3c16 24150{
4d663531 24151 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24152 enum dwarf_macro_record_type macinfo_type;
24153 int at_commandline;
d521ce57 24154 const gdb_byte *opcode_definitions[256];
757a13d0 24155
cf2c3c16
TT
24156 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24157 &offset_size, section_is_gnu);
24158 if (mac_ptr == NULL)
24159 {
24160 /* We already issued a complaint. */
24161 return;
24162 }
757a13d0
JK
24163
24164 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24165 GDB is still reading the definitions from command line. First
24166 DW_MACINFO_start_file will need to be ignored as it was already executed
24167 to create CURRENT_FILE for the main source holding also the command line
24168 definitions. On first met DW_MACINFO_start_file this flag is reset to
24169 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24170
24171 at_commandline = 1;
24172
24173 do
24174 {
24175 /* Do we at least have room for a macinfo type byte? */
24176 if (mac_ptr >= mac_end)
24177 {
f664829e 24178 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24179 break;
24180 }
24181
aead7601 24182 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24183 mac_ptr++;
24184
cf2c3c16
TT
24185 /* Note that we rely on the fact that the corresponding GNU and
24186 DWARF constants are the same. */
132448f8
SM
24187 DIAGNOSTIC_PUSH
24188 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24189 switch (macinfo_type)
24190 {
24191 /* A zero macinfo type indicates the end of the macro
24192 information. */
24193 case 0:
24194 break;
2e276125 24195
0af92d60
JK
24196 case DW_MACRO_define:
24197 case DW_MACRO_undef:
24198 case DW_MACRO_define_strp:
24199 case DW_MACRO_undef_strp:
24200 case DW_MACRO_define_sup:
24201 case DW_MACRO_undef_sup:
2e276125 24202 {
891d2f0b 24203 unsigned int bytes_read;
2e276125 24204 int line;
d521ce57 24205 const char *body;
cf2c3c16 24206 int is_define;
2e276125 24207
cf2c3c16
TT
24208 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24209 mac_ptr += bytes_read;
24210
0af92d60
JK
24211 if (macinfo_type == DW_MACRO_define
24212 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24213 {
24214 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24215 mac_ptr += bytes_read;
24216 }
24217 else
24218 {
24219 LONGEST str_offset;
24220
24221 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24222 mac_ptr += offset_size;
2e276125 24223
0af92d60
JK
24224 if (macinfo_type == DW_MACRO_define_sup
24225 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24226 || section_is_dwz)
36586728
TT
24227 {
24228 struct dwz_file *dwz = dwarf2_get_dwz_file ();
24229
24230 body = read_indirect_string_from_dwz (dwz, str_offset);
24231 }
24232 else
24233 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
24234 }
24235
0af92d60
JK
24236 is_define = (macinfo_type == DW_MACRO_define
24237 || macinfo_type == DW_MACRO_define_strp
24238 || macinfo_type == DW_MACRO_define_sup);
2e276125 24239 if (! current_file)
757a13d0
JK
24240 {
24241 /* DWARF violation as no main source is present. */
24242 complaint (&symfile_complaints,
24243 _("debug info with no main source gives macro %s "
24244 "on line %d: %s"),
cf2c3c16
TT
24245 is_define ? _("definition") : _("undefinition"),
24246 line, body);
757a13d0
JK
24247 break;
24248 }
3e43a32a
MS
24249 if ((line == 0 && !at_commandline)
24250 || (line != 0 && at_commandline))
4d3c2250 24251 complaint (&symfile_complaints,
757a13d0
JK
24252 _("debug info gives %s macro %s with %s line %d: %s"),
24253 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24254 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24255 line == 0 ? _("zero") : _("non-zero"), line, body);
24256
cf2c3c16 24257 if (is_define)
757a13d0 24258 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24259 else
24260 {
0af92d60
JK
24261 gdb_assert (macinfo_type == DW_MACRO_undef
24262 || macinfo_type == DW_MACRO_undef_strp
24263 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24264 macro_undef (current_file, line, body);
24265 }
2e276125
JB
24266 }
24267 break;
24268
0af92d60 24269 case DW_MACRO_start_file:
2e276125 24270 {
891d2f0b 24271 unsigned int bytes_read;
2e276125
JB
24272 int line, file;
24273
24274 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24275 mac_ptr += bytes_read;
24276 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24277 mac_ptr += bytes_read;
24278
3e43a32a
MS
24279 if ((line == 0 && !at_commandline)
24280 || (line != 0 && at_commandline))
757a13d0
JK
24281 complaint (&symfile_complaints,
24282 _("debug info gives source %d included "
24283 "from %s at %s line %d"),
24284 file, at_commandline ? _("command-line") : _("file"),
24285 line == 0 ? _("zero") : _("non-zero"), line);
24286
24287 if (at_commandline)
24288 {
0af92d60 24289 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24290 pass one. */
757a13d0
JK
24291 at_commandline = 0;
24292 }
24293 else
43f3e411 24294 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
24295 }
24296 break;
24297
0af92d60 24298 case DW_MACRO_end_file:
2e276125 24299 if (! current_file)
4d3c2250 24300 complaint (&symfile_complaints,
3e43a32a
MS
24301 _("macro debug info has an unmatched "
24302 "`close_file' directive"));
2e276125
JB
24303 else
24304 {
24305 current_file = current_file->included_by;
24306 if (! current_file)
24307 {
cf2c3c16 24308 enum dwarf_macro_record_type next_type;
2e276125
JB
24309
24310 /* GCC circa March 2002 doesn't produce the zero
24311 type byte marking the end of the compilation
24312 unit. Complain if it's not there, but exit no
24313 matter what. */
24314
24315 /* Do we at least have room for a macinfo type byte? */
24316 if (mac_ptr >= mac_end)
24317 {
f664829e 24318 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24319 return;
24320 }
24321
24322 /* We don't increment mac_ptr here, so this is just
24323 a look-ahead. */
aead7601
SM
24324 next_type
24325 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24326 mac_ptr);
2e276125 24327 if (next_type != 0)
4d3c2250 24328 complaint (&symfile_complaints,
3e43a32a
MS
24329 _("no terminating 0-type entry for "
24330 "macros in `.debug_macinfo' section"));
2e276125
JB
24331
24332 return;
24333 }
24334 }
24335 break;
24336
0af92d60
JK
24337 case DW_MACRO_import:
24338 case DW_MACRO_import_sup:
cf2c3c16
TT
24339 {
24340 LONGEST offset;
8fc3fc34 24341 void **slot;
a036ba48
TT
24342 bfd *include_bfd = abfd;
24343 struct dwarf2_section_info *include_section = section;
d521ce57 24344 const gdb_byte *include_mac_end = mac_end;
a036ba48 24345 int is_dwz = section_is_dwz;
d521ce57 24346 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24347
24348 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24349 mac_ptr += offset_size;
24350
0af92d60 24351 if (macinfo_type == DW_MACRO_import_sup)
a036ba48
TT
24352 {
24353 struct dwz_file *dwz = dwarf2_get_dwz_file ();
24354
4d663531 24355 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24356
a036ba48 24357 include_section = &dwz->macro;
a32a8923 24358 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24359 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24360 is_dwz = 1;
24361 }
24362
24363 new_mac_ptr = include_section->buffer + offset;
24364 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24365
8fc3fc34
TT
24366 if (*slot != NULL)
24367 {
24368 /* This has actually happened; see
24369 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24370 complaint (&symfile_complaints,
0af92d60 24371 _("recursive DW_MACRO_import in "
8fc3fc34
TT
24372 ".debug_macro section"));
24373 }
24374 else
24375 {
d521ce57 24376 *slot = (void *) new_mac_ptr;
36586728 24377
a036ba48 24378 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 24379 include_mac_end, current_file, lh,
36586728 24380 section, section_is_gnu, is_dwz,
4d663531 24381 offset_size, include_hash);
8fc3fc34 24382
d521ce57 24383 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24384 }
cf2c3c16
TT
24385 }
24386 break;
24387
2e276125 24388 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24389 if (!section_is_gnu)
24390 {
24391 unsigned int bytes_read;
2e276125 24392
ac298888
TT
24393 /* This reads the constant, but since we don't recognize
24394 any vendor extensions, we ignore it. */
24395 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24396 mac_ptr += bytes_read;
24397 read_direct_string (abfd, mac_ptr, &bytes_read);
24398 mac_ptr += bytes_read;
2e276125 24399
cf2c3c16
TT
24400 /* We don't recognize any vendor extensions. */
24401 break;
24402 }
24403 /* FALLTHROUGH */
24404
24405 default:
24406 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24407 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24408 section);
24409 if (mac_ptr == NULL)
24410 return;
24411 break;
2e276125 24412 }
132448f8 24413 DIAGNOSTIC_POP
757a13d0 24414 } while (macinfo_type != 0);
2e276125 24415}
8e19ed76 24416
cf2c3c16 24417static void
09262596 24418dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24419 int section_is_gnu)
cf2c3c16 24420{
bb5ed363 24421 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24422 struct line_header *lh = cu->line_header;
24423 bfd *abfd;
d521ce57 24424 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24425 struct macro_source_file *current_file = 0;
24426 enum dwarf_macro_record_type macinfo_type;
24427 unsigned int offset_size = cu->header.offset_size;
d521ce57 24428 const gdb_byte *opcode_definitions[256];
8fc3fc34 24429 void **slot;
09262596
DE
24430 struct dwarf2_section_info *section;
24431 const char *section_name;
24432
24433 if (cu->dwo_unit != NULL)
24434 {
24435 if (section_is_gnu)
24436 {
24437 section = &cu->dwo_unit->dwo_file->sections.macro;
24438 section_name = ".debug_macro.dwo";
24439 }
24440 else
24441 {
24442 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24443 section_name = ".debug_macinfo.dwo";
24444 }
24445 }
24446 else
24447 {
24448 if (section_is_gnu)
24449 {
24450 section = &dwarf2_per_objfile->macro;
24451 section_name = ".debug_macro";
24452 }
24453 else
24454 {
24455 section = &dwarf2_per_objfile->macinfo;
24456 section_name = ".debug_macinfo";
24457 }
24458 }
cf2c3c16 24459
bb5ed363 24460 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24461 if (section->buffer == NULL)
24462 {
fceca515 24463 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
24464 return;
24465 }
a32a8923 24466 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24467
24468 /* First pass: Find the name of the base filename.
24469 This filename is needed in order to process all macros whose definition
24470 (or undefinition) comes from the command line. These macros are defined
24471 before the first DW_MACINFO_start_file entry, and yet still need to be
24472 associated to the base file.
24473
24474 To determine the base file name, we scan the macro definitions until we
24475 reach the first DW_MACINFO_start_file entry. We then initialize
24476 CURRENT_FILE accordingly so that any macro definition found before the
24477 first DW_MACINFO_start_file can still be associated to the base file. */
24478
24479 mac_ptr = section->buffer + offset;
24480 mac_end = section->buffer + section->size;
24481
24482 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24483 &offset_size, section_is_gnu);
24484 if (mac_ptr == NULL)
24485 {
24486 /* We already issued a complaint. */
24487 return;
24488 }
24489
24490 do
24491 {
24492 /* Do we at least have room for a macinfo type byte? */
24493 if (mac_ptr >= mac_end)
24494 {
24495 /* Complaint is printed during the second pass as GDB will probably
24496 stop the first pass earlier upon finding
24497 DW_MACINFO_start_file. */
24498 break;
24499 }
24500
aead7601 24501 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24502 mac_ptr++;
24503
24504 /* Note that we rely on the fact that the corresponding GNU and
24505 DWARF constants are the same. */
132448f8
SM
24506 DIAGNOSTIC_PUSH
24507 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
24508 switch (macinfo_type)
24509 {
24510 /* A zero macinfo type indicates the end of the macro
24511 information. */
24512 case 0:
24513 break;
24514
0af92d60
JK
24515 case DW_MACRO_define:
24516 case DW_MACRO_undef:
cf2c3c16
TT
24517 /* Only skip the data by MAC_PTR. */
24518 {
24519 unsigned int bytes_read;
24520
24521 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24522 mac_ptr += bytes_read;
24523 read_direct_string (abfd, mac_ptr, &bytes_read);
24524 mac_ptr += bytes_read;
24525 }
24526 break;
24527
0af92d60 24528 case DW_MACRO_start_file:
cf2c3c16
TT
24529 {
24530 unsigned int bytes_read;
24531 int line, file;
24532
24533 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24534 mac_ptr += bytes_read;
24535 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24536 mac_ptr += bytes_read;
24537
43f3e411 24538 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
24539 }
24540 break;
24541
0af92d60 24542 case DW_MACRO_end_file:
cf2c3c16
TT
24543 /* No data to skip by MAC_PTR. */
24544 break;
24545
0af92d60
JK
24546 case DW_MACRO_define_strp:
24547 case DW_MACRO_undef_strp:
24548 case DW_MACRO_define_sup:
24549 case DW_MACRO_undef_sup:
cf2c3c16
TT
24550 {
24551 unsigned int bytes_read;
24552
24553 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24554 mac_ptr += bytes_read;
24555 mac_ptr += offset_size;
24556 }
24557 break;
24558
0af92d60
JK
24559 case DW_MACRO_import:
24560 case DW_MACRO_import_sup:
cf2c3c16 24561 /* Note that, according to the spec, a transparent include
0af92d60 24562 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
24563 skip this opcode. */
24564 mac_ptr += offset_size;
24565 break;
24566
24567 case DW_MACINFO_vendor_ext:
24568 /* Only skip the data by MAC_PTR. */
24569 if (!section_is_gnu)
24570 {
24571 unsigned int bytes_read;
24572
24573 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24574 mac_ptr += bytes_read;
24575 read_direct_string (abfd, mac_ptr, &bytes_read);
24576 mac_ptr += bytes_read;
24577 }
24578 /* FALLTHROUGH */
24579
24580 default:
24581 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24582 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24583 section);
24584 if (mac_ptr == NULL)
24585 return;
24586 break;
24587 }
132448f8 24588 DIAGNOSTIC_POP
cf2c3c16
TT
24589 } while (macinfo_type != 0 && current_file == NULL);
24590
24591 /* Second pass: Process all entries.
24592
24593 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24594 command-line macro definitions/undefinitions. This flag is unset when we
24595 reach the first DW_MACINFO_start_file entry. */
24596
fc4007c9
TT
24597 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24598 htab_eq_pointer,
24599 NULL, xcalloc, xfree));
8fc3fc34 24600 mac_ptr = section->buffer + offset;
fc4007c9 24601 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 24602 *slot = (void *) mac_ptr;
8fc3fc34 24603 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 24604 current_file, lh, section,
fc4007c9
TT
24605 section_is_gnu, 0, offset_size,
24606 include_hash.get ());
cf2c3c16
TT
24607}
24608
8e19ed76 24609/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 24610 if so return true else false. */
380bca97 24611
8e19ed76 24612static int
6e5a29e1 24613attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
24614{
24615 return (attr == NULL ? 0 :
24616 attr->form == DW_FORM_block1
24617 || attr->form == DW_FORM_block2
24618 || attr->form == DW_FORM_block4
2dc7f7b3
TT
24619 || attr->form == DW_FORM_block
24620 || attr->form == DW_FORM_exprloc);
8e19ed76 24621}
4c2df51b 24622
c6a0999f
JB
24623/* Return non-zero if ATTR's value is a section offset --- classes
24624 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24625 You may use DW_UNSND (attr) to retrieve such offsets.
24626
24627 Section 7.5.4, "Attribute Encodings", explains that no attribute
24628 may have a value that belongs to more than one of these classes; it
24629 would be ambiguous if we did, because we use the same forms for all
24630 of them. */
380bca97 24631
3690dd37 24632static int
6e5a29e1 24633attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
24634{
24635 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
24636 || attr->form == DW_FORM_data8
24637 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
24638}
24639
3690dd37
JB
24640/* Return non-zero if ATTR's value falls in the 'constant' class, or
24641 zero otherwise. When this function returns true, you can apply
24642 dwarf2_get_attr_constant_value to it.
24643
24644 However, note that for some attributes you must check
24645 attr_form_is_section_offset before using this test. DW_FORM_data4
24646 and DW_FORM_data8 are members of both the constant class, and of
24647 the classes that contain offsets into other debug sections
24648 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24649 that, if an attribute's can be either a constant or one of the
24650 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
24651 taken as section offsets, not constants.
24652
24653 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24654 cannot handle that. */
380bca97 24655
3690dd37 24656static int
6e5a29e1 24657attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
24658{
24659 switch (attr->form)
24660 {
24661 case DW_FORM_sdata:
24662 case DW_FORM_udata:
24663 case DW_FORM_data1:
24664 case DW_FORM_data2:
24665 case DW_FORM_data4:
24666 case DW_FORM_data8:
663c44ac 24667 case DW_FORM_implicit_const:
3690dd37
JB
24668 return 1;
24669 default:
24670 return 0;
24671 }
24672}
24673
7771576e
SA
24674
24675/* DW_ADDR is always stored already as sect_offset; despite for the forms
24676 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24677
24678static int
6e5a29e1 24679attr_form_is_ref (const struct attribute *attr)
7771576e
SA
24680{
24681 switch (attr->form)
24682 {
24683 case DW_FORM_ref_addr:
24684 case DW_FORM_ref1:
24685 case DW_FORM_ref2:
24686 case DW_FORM_ref4:
24687 case DW_FORM_ref8:
24688 case DW_FORM_ref_udata:
24689 case DW_FORM_GNU_ref_alt:
24690 return 1;
24691 default:
24692 return 0;
24693 }
24694}
24695
3019eac3
DE
24696/* Return the .debug_loc section to use for CU.
24697 For DWO files use .debug_loc.dwo. */
24698
24699static struct dwarf2_section_info *
24700cu_debug_loc_section (struct dwarf2_cu *cu)
24701{
24702 if (cu->dwo_unit)
43988095
JK
24703 {
24704 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24705
24706 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24707 }
24708 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24709 : &dwarf2_per_objfile->loc);
3019eac3
DE
24710}
24711
8cf6f0b1
TT
24712/* A helper function that fills in a dwarf2_loclist_baton. */
24713
24714static void
24715fill_in_loclist_baton (struct dwarf2_cu *cu,
24716 struct dwarf2_loclist_baton *baton,
ff39bb5e 24717 const struct attribute *attr)
8cf6f0b1 24718{
3019eac3
DE
24719 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24720
24721 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
24722
24723 baton->per_cu = cu->per_cu;
24724 gdb_assert (baton->per_cu);
24725 /* We don't know how long the location list is, but make sure we
24726 don't run off the edge of the section. */
3019eac3
DE
24727 baton->size = section->size - DW_UNSND (attr);
24728 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 24729 baton->base_address = cu->base_address;
f664829e 24730 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24731}
24732
4c2df51b 24733static void
ff39bb5e 24734dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24735 struct dwarf2_cu *cu, int is_block)
4c2df51b 24736{
bb5ed363 24737 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 24738 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24739
3690dd37 24740 if (attr_form_is_section_offset (attr)
3019eac3 24741 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24742 the section. If so, fall through to the complaint in the
24743 other branch. */
3019eac3 24744 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 24745 {
0d53c4c4 24746 struct dwarf2_loclist_baton *baton;
4c2df51b 24747
8d749320 24748 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24749
8cf6f0b1 24750 fill_in_loclist_baton (cu, baton, attr);
be391dca 24751
d00adf39 24752 if (cu->base_known == 0)
0d53c4c4 24753 complaint (&symfile_complaints,
3e43a32a
MS
24754 _("Location list used without "
24755 "specifying the CU base address."));
4c2df51b 24756
f1e6e072
TT
24757 SYMBOL_ACLASS_INDEX (sym) = (is_block
24758 ? dwarf2_loclist_block_index
24759 : dwarf2_loclist_index);
0d53c4c4
DJ
24760 SYMBOL_LOCATION_BATON (sym) = baton;
24761 }
24762 else
24763 {
24764 struct dwarf2_locexpr_baton *baton;
24765
8d749320 24766 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
24767 baton->per_cu = cu->per_cu;
24768 gdb_assert (baton->per_cu);
0d53c4c4
DJ
24769
24770 if (attr_form_is_block (attr))
24771 {
24772 /* Note that we're just copying the block's data pointer
24773 here, not the actual data. We're still pointing into the
6502dd73
DJ
24774 info_buffer for SYM's objfile; right now we never release
24775 that buffer, but when we do clean up properly this may
24776 need to change. */
0d53c4c4
DJ
24777 baton->size = DW_BLOCK (attr)->size;
24778 baton->data = DW_BLOCK (attr)->data;
24779 }
24780 else
24781 {
24782 dwarf2_invalid_attrib_class_complaint ("location description",
24783 SYMBOL_NATURAL_NAME (sym));
24784 baton->size = 0;
0d53c4c4 24785 }
6e70227d 24786
f1e6e072
TT
24787 SYMBOL_ACLASS_INDEX (sym) = (is_block
24788 ? dwarf2_locexpr_block_index
24789 : dwarf2_locexpr_index);
0d53c4c4
DJ
24790 SYMBOL_LOCATION_BATON (sym) = baton;
24791 }
4c2df51b 24792}
6502dd73 24793
9aa1f1e3
TT
24794/* Return the OBJFILE associated with the compilation unit CU. If CU
24795 came from a separate debuginfo file, then the master objfile is
24796 returned. */
ae0d2f24
UW
24797
24798struct objfile *
24799dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24800{
9291a0cd 24801 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
24802
24803 /* Return the master objfile, so that we can report and look up the
24804 correct file containing this variable. */
24805 if (objfile->separate_debug_objfile_backlink)
24806 objfile = objfile->separate_debug_objfile_backlink;
24807
24808 return objfile;
24809}
24810
96408a79
SA
24811/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24812 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24813 CU_HEADERP first. */
24814
24815static const struct comp_unit_head *
24816per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24817 struct dwarf2_per_cu_data *per_cu)
24818{
d521ce57 24819 const gdb_byte *info_ptr;
96408a79
SA
24820
24821 if (per_cu->cu)
24822 return &per_cu->cu->header;
24823
9c541725 24824 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24825
24826 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24827 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24828 rcuh_kind::COMPILE);
96408a79
SA
24829
24830 return cu_headerp;
24831}
24832
ae0d2f24
UW
24833/* Return the address size given in the compilation unit header for CU. */
24834
98714339 24835int
ae0d2f24
UW
24836dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24837{
96408a79
SA
24838 struct comp_unit_head cu_header_local;
24839 const struct comp_unit_head *cu_headerp;
c471e790 24840
96408a79
SA
24841 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24842
24843 return cu_headerp->addr_size;
ae0d2f24
UW
24844}
24845
9eae7c52
TT
24846/* Return the offset size given in the compilation unit header for CU. */
24847
24848int
24849dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24850{
96408a79
SA
24851 struct comp_unit_head cu_header_local;
24852 const struct comp_unit_head *cu_headerp;
9c6c53f7 24853
96408a79
SA
24854 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24855
24856 return cu_headerp->offset_size;
24857}
24858
24859/* See its dwarf2loc.h declaration. */
24860
24861int
24862dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24863{
24864 struct comp_unit_head cu_header_local;
24865 const struct comp_unit_head *cu_headerp;
24866
24867 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24868
24869 if (cu_headerp->version == 2)
24870 return cu_headerp->addr_size;
24871 else
24872 return cu_headerp->offset_size;
181cebd4
JK
24873}
24874
9aa1f1e3
TT
24875/* Return the text offset of the CU. The returned offset comes from
24876 this CU's objfile. If this objfile came from a separate debuginfo
24877 file, then the offset may be different from the corresponding
24878 offset in the parent objfile. */
24879
24880CORE_ADDR
24881dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24882{
bb3fa9d0 24883 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
24884
24885 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24886}
24887
43988095
JK
24888/* Return DWARF version number of PER_CU. */
24889
24890short
24891dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24892{
24893 return per_cu->dwarf_version;
24894}
24895
348e048f
DE
24896/* Locate the .debug_info compilation unit from CU's objfile which contains
24897 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
24898
24899static struct dwarf2_per_cu_data *
9c541725 24900dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 24901 unsigned int offset_in_dwz,
ae038cb0
DJ
24902 struct objfile *objfile)
24903{
24904 struct dwarf2_per_cu_data *this_cu;
24905 int low, high;
36586728 24906 const sect_offset *cu_off;
ae038cb0 24907
ae038cb0
DJ
24908 low = 0;
24909 high = dwarf2_per_objfile->n_comp_units - 1;
24910 while (high > low)
24911 {
36586728 24912 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24913 int mid = low + (high - low) / 2;
9a619af0 24914
36586728 24915 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 24916 cu_off = &mid_cu->sect_off;
36586728 24917 if (mid_cu->is_dwz > offset_in_dwz
9c541725 24918 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
24919 high = mid;
24920 else
24921 low = mid + 1;
24922 }
24923 gdb_assert (low == high);
36586728 24924 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
24925 cu_off = &this_cu->sect_off;
24926 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 24927 {
36586728 24928 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24929 error (_("Dwarf Error: could not find partial DIE containing "
9c541725
PA
24930 "offset 0x%x [in module %s]"),
24931 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
10b3939b 24932
9c541725
PA
24933 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24934 <= sect_off);
ae038cb0
DJ
24935 return dwarf2_per_objfile->all_comp_units[low-1];
24936 }
24937 else
24938 {
24939 this_cu = dwarf2_per_objfile->all_comp_units[low];
24940 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725
PA
24941 && sect_off >= this_cu->sect_off + this_cu->length)
24942 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
24943 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24944 return this_cu;
24945 }
24946}
24947
23745b47 24948/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 24949
9816fde3 24950static void
23745b47 24951init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 24952{
9816fde3 24953 memset (cu, 0, sizeof (*cu));
23745b47
DE
24954 per_cu->cu = cu;
24955 cu->per_cu = per_cu;
24956 cu->objfile = per_cu->objfile;
93311388 24957 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
24958}
24959
24960/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24961
24962static void
95554aad
TT
24963prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24964 enum language pretend_language)
9816fde3
JK
24965{
24966 struct attribute *attr;
24967
24968 /* Set the language we're debugging. */
24969 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24970 if (attr)
24971 set_cu_language (DW_UNSND (attr), cu);
24972 else
9cded63f 24973 {
95554aad 24974 cu->language = pretend_language;
9cded63f
TT
24975 cu->language_defn = language_def (cu->language);
24976 }
dee91e82 24977
7d45c7c3 24978 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24979}
24980
ae038cb0
DJ
24981/* Release one cached compilation unit, CU. We unlink it from the tree
24982 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
24983 the caller is responsible for that.
24984 NOTE: DATA is a void * because this function is also used as a
24985 cleanup routine. */
ae038cb0
DJ
24986
24987static void
68dc6402 24988free_heap_comp_unit (void *data)
ae038cb0 24989{
9a3c8263 24990 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 24991
23745b47
DE
24992 gdb_assert (cu->per_cu != NULL);
24993 cu->per_cu->cu = NULL;
ae038cb0
DJ
24994 cu->per_cu = NULL;
24995
24996 obstack_free (&cu->comp_unit_obstack, NULL);
24997
24998 xfree (cu);
24999}
25000
72bf9492 25001/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 25002 when we're finished with it. We can't free the pointer itself, but be
dee91e82 25003 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
25004
25005static void
25006free_stack_comp_unit (void *data)
25007{
9a3c8263 25008 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 25009
23745b47
DE
25010 gdb_assert (cu->per_cu != NULL);
25011 cu->per_cu->cu = NULL;
25012 cu->per_cu = NULL;
25013
72bf9492
DJ
25014 obstack_free (&cu->comp_unit_obstack, NULL);
25015 cu->partial_dies = NULL;
ae038cb0
DJ
25016}
25017
25018/* Free all cached compilation units. */
25019
25020static void
25021free_cached_comp_units (void *data)
25022{
330cdd98 25023 dwarf2_per_objfile->free_cached_comp_units ();
ae038cb0
DJ
25024}
25025
25026/* Increase the age counter on each cached compilation unit, and free
25027 any that are too old. */
25028
25029static void
25030age_cached_comp_units (void)
25031{
25032 struct dwarf2_per_cu_data *per_cu, **last_chain;
25033
25034 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25035 per_cu = dwarf2_per_objfile->read_in_chain;
25036 while (per_cu != NULL)
25037 {
25038 per_cu->cu->last_used ++;
b4f54984 25039 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25040 dwarf2_mark (per_cu->cu);
25041 per_cu = per_cu->cu->read_in_chain;
25042 }
25043
25044 per_cu = dwarf2_per_objfile->read_in_chain;
25045 last_chain = &dwarf2_per_objfile->read_in_chain;
25046 while (per_cu != NULL)
25047 {
25048 struct dwarf2_per_cu_data *next_cu;
25049
25050 next_cu = per_cu->cu->read_in_chain;
25051
25052 if (!per_cu->cu->mark)
25053 {
68dc6402 25054 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
25055 *last_chain = next_cu;
25056 }
25057 else
25058 last_chain = &per_cu->cu->read_in_chain;
25059
25060 per_cu = next_cu;
25061 }
25062}
25063
25064/* Remove a single compilation unit from the cache. */
25065
25066static void
dee91e82 25067free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25068{
25069 struct dwarf2_per_cu_data *per_cu, **last_chain;
25070
25071 per_cu = dwarf2_per_objfile->read_in_chain;
25072 last_chain = &dwarf2_per_objfile->read_in_chain;
25073 while (per_cu != NULL)
25074 {
25075 struct dwarf2_per_cu_data *next_cu;
25076
25077 next_cu = per_cu->cu->read_in_chain;
25078
dee91e82 25079 if (per_cu == target_per_cu)
ae038cb0 25080 {
68dc6402 25081 free_heap_comp_unit (per_cu->cu);
dee91e82 25082 per_cu->cu = NULL;
ae038cb0
DJ
25083 *last_chain = next_cu;
25084 break;
25085 }
25086 else
25087 last_chain = &per_cu->cu->read_in_chain;
25088
25089 per_cu = next_cu;
25090 }
25091}
25092
fe3e1990
DJ
25093/* Release all extra memory associated with OBJFILE. */
25094
25095void
25096dwarf2_free_objfile (struct objfile *objfile)
25097{
9a3c8263
SM
25098 dwarf2_per_objfile
25099 = (struct dwarf2_per_objfile *) objfile_data (objfile,
25100 dwarf2_objfile_data_key);
fe3e1990
DJ
25101
25102 if (dwarf2_per_objfile == NULL)
25103 return;
25104
330cdd98 25105 dwarf2_per_objfile->~dwarf2_per_objfile ();
fe3e1990
DJ
25106}
25107
dee91e82
DE
25108/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25109 We store these in a hash table separate from the DIEs, and preserve them
25110 when the DIEs are flushed out of cache.
25111
25112 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25113 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25114 or the type may come from a DWO file. Furthermore, while it's more logical
25115 to use per_cu->section+offset, with Fission the section with the data is in
25116 the DWO file but we don't know that section at the point we need it.
25117 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25118 because we can enter the lookup routine, get_die_type_at_offset, from
25119 outside this file, and thus won't necessarily have PER_CU->cu.
25120 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25121
dee91e82 25122struct dwarf2_per_cu_offset_and_type
1c379e20 25123{
dee91e82 25124 const struct dwarf2_per_cu_data *per_cu;
9c541725 25125 sect_offset sect_off;
1c379e20
DJ
25126 struct type *type;
25127};
25128
dee91e82 25129/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25130
25131static hashval_t
dee91e82 25132per_cu_offset_and_type_hash (const void *item)
1c379e20 25133{
9a3c8263
SM
25134 const struct dwarf2_per_cu_offset_and_type *ofs
25135 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25136
9c541725 25137 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25138}
25139
dee91e82 25140/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25141
25142static int
dee91e82 25143per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25144{
9a3c8263
SM
25145 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25146 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25147 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25148 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25149
dee91e82 25150 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25151 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25152}
25153
25154/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25155 table if necessary. For convenience, return TYPE.
25156
25157 The DIEs reading must have careful ordering to:
25158 * Not cause infite loops trying to read in DIEs as a prerequisite for
25159 reading current DIE.
25160 * Not trying to dereference contents of still incompletely read in types
25161 while reading in other DIEs.
25162 * Enable referencing still incompletely read in types just by a pointer to
25163 the type without accessing its fields.
25164
25165 Therefore caller should follow these rules:
25166 * Try to fetch any prerequisite types we may need to build this DIE type
25167 before building the type and calling set_die_type.
e71ec853 25168 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25169 possible before fetching more types to complete the current type.
25170 * Make the type as complete as possible before fetching more types. */
1c379e20 25171
f792889a 25172static struct type *
1c379e20
DJ
25173set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25174{
dee91e82 25175 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 25176 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
25177 struct attribute *attr;
25178 struct dynamic_prop prop;
1c379e20 25179
b4ba55a1
JB
25180 /* For Ada types, make sure that the gnat-specific data is always
25181 initialized (if not already set). There are a few types where
25182 we should not be doing so, because the type-specific area is
25183 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25184 where the type-specific area is used to store the floatformat).
25185 But this is not a problem, because the gnat-specific information
25186 is actually not needed for these types. */
25187 if (need_gnat_info (cu)
25188 && TYPE_CODE (type) != TYPE_CODE_FUNC
25189 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25190 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25191 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25192 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25193 && !HAVE_GNAT_AUX_INFO (type))
25194 INIT_GNAT_SPECIFIC (type);
25195
3f2f83dd
KB
25196 /* Read DW_AT_allocated and set in type. */
25197 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25198 if (attr_form_is_block (attr))
25199 {
25200 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25201 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
25202 }
25203 else if (attr != NULL)
25204 {
25205 complaint (&symfile_complaints,
9c541725
PA
25206 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
25207 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25208 to_underlying (die->sect_off));
3f2f83dd
KB
25209 }
25210
25211 /* Read DW_AT_associated and set in type. */
25212 attr = dwarf2_attr (die, DW_AT_associated, cu);
25213 if (attr_form_is_block (attr))
25214 {
25215 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25216 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
25217 }
25218 else if (attr != NULL)
25219 {
25220 complaint (&symfile_complaints,
9c541725
PA
25221 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
25222 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25223 to_underlying (die->sect_off));
3f2f83dd
KB
25224 }
25225
3cdcd0ce
JB
25226 /* Read DW_AT_data_location and set in type. */
25227 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25228 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 25229 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 25230
dee91e82 25231 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25232 {
dee91e82
DE
25233 dwarf2_per_objfile->die_type_hash =
25234 htab_create_alloc_ex (127,
25235 per_cu_offset_and_type_hash,
25236 per_cu_offset_and_type_eq,
25237 NULL,
25238 &objfile->objfile_obstack,
25239 hashtab_obstack_allocate,
25240 dummy_obstack_deallocate);
f792889a 25241 }
1c379e20 25242
dee91e82 25243 ofs.per_cu = cu->per_cu;
9c541725 25244 ofs.sect_off = die->sect_off;
1c379e20 25245 ofs.type = type;
dee91e82
DE
25246 slot = (struct dwarf2_per_cu_offset_and_type **)
25247 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
25248 if (*slot)
25249 complaint (&symfile_complaints,
25250 _("A problem internal to GDB: DIE 0x%x has type already set"),
9c541725 25251 to_underlying (die->sect_off));
8d749320
SM
25252 *slot = XOBNEW (&objfile->objfile_obstack,
25253 struct dwarf2_per_cu_offset_and_type);
1c379e20 25254 **slot = ofs;
f792889a 25255 return type;
1c379e20
DJ
25256}
25257
9c541725 25258/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25259 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25260
25261static struct type *
9c541725 25262get_die_type_at_offset (sect_offset sect_off,
673bfd45 25263 struct dwarf2_per_cu_data *per_cu)
1c379e20 25264{
dee91e82 25265 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 25266
dee91e82 25267 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25268 return NULL;
1c379e20 25269
dee91e82 25270 ofs.per_cu = per_cu;
9c541725 25271 ofs.sect_off = sect_off;
9a3c8263
SM
25272 slot = ((struct dwarf2_per_cu_offset_and_type *)
25273 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25274 if (slot)
25275 return slot->type;
25276 else
25277 return NULL;
25278}
25279
02142a6c 25280/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25281 or return NULL if DIE does not have a saved type. */
25282
25283static struct type *
25284get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25285{
9c541725 25286 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25287}
25288
10b3939b
DJ
25289/* Add a dependence relationship from CU to REF_PER_CU. */
25290
25291static void
25292dwarf2_add_dependence (struct dwarf2_cu *cu,
25293 struct dwarf2_per_cu_data *ref_per_cu)
25294{
25295 void **slot;
25296
25297 if (cu->dependencies == NULL)
25298 cu->dependencies
25299 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25300 NULL, &cu->comp_unit_obstack,
25301 hashtab_obstack_allocate,
25302 dummy_obstack_deallocate);
25303
25304 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25305 if (*slot == NULL)
25306 *slot = ref_per_cu;
25307}
1c379e20 25308
f504f079
DE
25309/* Subroutine of dwarf2_mark to pass to htab_traverse.
25310 Set the mark field in every compilation unit in the
ae038cb0
DJ
25311 cache that we must keep because we are keeping CU. */
25312
10b3939b
DJ
25313static int
25314dwarf2_mark_helper (void **slot, void *data)
25315{
25316 struct dwarf2_per_cu_data *per_cu;
25317
25318 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25319
25320 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25321 reading of the chain. As such dependencies remain valid it is not much
25322 useful to track and undo them during QUIT cleanups. */
25323 if (per_cu->cu == NULL)
25324 return 1;
25325
10b3939b
DJ
25326 if (per_cu->cu->mark)
25327 return 1;
25328 per_cu->cu->mark = 1;
25329
25330 if (per_cu->cu->dependencies != NULL)
25331 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25332
25333 return 1;
25334}
25335
f504f079
DE
25336/* Set the mark field in CU and in every other compilation unit in the
25337 cache that we must keep because we are keeping CU. */
25338
ae038cb0
DJ
25339static void
25340dwarf2_mark (struct dwarf2_cu *cu)
25341{
25342 if (cu->mark)
25343 return;
25344 cu->mark = 1;
10b3939b
DJ
25345 if (cu->dependencies != NULL)
25346 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25347}
25348
25349static void
25350dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25351{
25352 while (per_cu)
25353 {
25354 per_cu->cu->mark = 0;
25355 per_cu = per_cu->cu->read_in_chain;
25356 }
72bf9492
DJ
25357}
25358
72bf9492
DJ
25359/* Trivial hash function for partial_die_info: the hash value of a DIE
25360 is its offset in .debug_info for this objfile. */
25361
25362static hashval_t
25363partial_die_hash (const void *item)
25364{
9a3c8263
SM
25365 const struct partial_die_info *part_die
25366 = (const struct partial_die_info *) item;
9a619af0 25367
9c541725 25368 return to_underlying (part_die->sect_off);
72bf9492
DJ
25369}
25370
25371/* Trivial comparison function for partial_die_info structures: two DIEs
25372 are equal if they have the same offset. */
25373
25374static int
25375partial_die_eq (const void *item_lhs, const void *item_rhs)
25376{
9a3c8263
SM
25377 const struct partial_die_info *part_die_lhs
25378 = (const struct partial_die_info *) item_lhs;
25379 const struct partial_die_info *part_die_rhs
25380 = (const struct partial_die_info *) item_rhs;
9a619af0 25381
9c541725 25382 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25383}
25384
b4f54984
DE
25385static struct cmd_list_element *set_dwarf_cmdlist;
25386static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25387
25388static void
981a3fb3 25389set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25390{
b4f54984 25391 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25392 gdb_stdout);
ae038cb0
DJ
25393}
25394
25395static void
981a3fb3 25396show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25397{
b4f54984 25398 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25399}
25400
4bf44c1c 25401/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
25402
25403static void
c1bd65d0 25404dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 25405{
9a3c8263 25406 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 25407 int ix;
8b70b953 25408
626f2d1c
TT
25409 /* Make sure we don't accidentally use dwarf2_per_objfile while
25410 cleaning up. */
25411 dwarf2_per_objfile = NULL;
25412
59b0c7c1
JB
25413 for (ix = 0; ix < data->n_comp_units; ++ix)
25414 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 25415
59b0c7c1 25416 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 25417 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
25418 data->all_type_units[ix]->per_cu.imported_symtabs);
25419 xfree (data->all_type_units);
95554aad 25420
8b70b953 25421 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
25422
25423 if (data->dwo_files)
25424 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
25425 if (data->dwp_file)
25426 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
25427
25428 if (data->dwz_file && data->dwz_file->dwz_bfd)
25429 gdb_bfd_unref (data->dwz_file->dwz_bfd);
3f563c84
PA
25430
25431 if (data->index_table != NULL)
25432 data->index_table->~mapped_index ();
9291a0cd
TT
25433}
25434
25435\f
ae2de4f8 25436/* The "save gdb-index" command. */
9291a0cd 25437
437afbb8
JK
25438/* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25439 error checking. */
25440
25441static void
25442file_write (FILE *file, const void *data, size_t size)
25443{
25444 if (fwrite (data, 1, size, file) != size)
25445 error (_("couldn't data write to file"));
25446}
25447
25448/* Write the contents of VEC to FILE, with error checking. */
25449
25450template<typename Elem, typename Alloc>
25451static void
25452file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25453{
25454 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25455}
25456
bc8f2430
JK
25457/* In-memory buffer to prepare data to be written later to a file. */
25458class data_buf
9291a0cd 25459{
bc8f2430 25460public:
bc8f2430
JK
25461 /* Copy DATA to the end of the buffer. */
25462 template<typename T>
25463 void append_data (const T &data)
25464 {
25465 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25466 reinterpret_cast<const gdb_byte *> (&data + 1),
c2f134ac 25467 grow (sizeof (data)));
bc8f2430 25468 }
b89be57b 25469
c2f134ac
PA
25470 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25471 terminating zero is appended too. */
bc8f2430
JK
25472 void append_cstr0 (const char *cstr)
25473 {
25474 const size_t size = strlen (cstr) + 1;
c2f134ac
PA
25475 std::copy (cstr, cstr + size, grow (size));
25476 }
25477
437afbb8
JK
25478 /* Store INPUT as ULEB128 to the end of buffer. */
25479 void append_unsigned_leb128 (ULONGEST input)
25480 {
25481 for (;;)
25482 {
25483 gdb_byte output = input & 0x7f;
25484 input >>= 7;
25485 if (input)
25486 output |= 0x80;
25487 append_data (output);
25488 if (input == 0)
25489 break;
25490 }
25491 }
25492
c2f134ac
PA
25493 /* Accept a host-format integer in VAL and append it to the buffer
25494 as a target-format integer which is LEN bytes long. */
25495 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25496 {
25497 ::store_unsigned_integer (grow (len), len, byte_order, val);
bc8f2430 25498 }
9291a0cd 25499
bc8f2430
JK
25500 /* Return the size of the buffer. */
25501 size_t size () const
25502 {
25503 return m_vec.size ();
25504 }
25505
437afbb8
JK
25506 /* Return true iff the buffer is empty. */
25507 bool empty () const
25508 {
25509 return m_vec.empty ();
25510 }
25511
bc8f2430
JK
25512 /* Write the buffer to FILE. */
25513 void file_write (FILE *file) const
25514 {
437afbb8 25515 ::file_write (file, m_vec);
bc8f2430
JK
25516 }
25517
25518private:
c2f134ac
PA
25519 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25520 the start of the new block. */
25521 gdb_byte *grow (size_t size)
25522 {
25523 m_vec.resize (m_vec.size () + size);
25524 return &*m_vec.end () - size;
25525 }
25526
d5722aa2 25527 gdb::byte_vector m_vec;
bc8f2430 25528};
9291a0cd
TT
25529
25530/* An entry in the symbol table. */
25531struct symtab_index_entry
25532{
25533 /* The name of the symbol. */
25534 const char *name;
25535 /* The offset of the name in the constant pool. */
25536 offset_type index_offset;
25537 /* A sorted vector of the indices of all the CUs that hold an object
25538 of this name. */
bc8f2430 25539 std::vector<offset_type> cu_indices;
9291a0cd
TT
25540};
25541
25542/* The symbol table. This is a power-of-2-sized hash table. */
25543struct mapped_symtab
25544{
bc8f2430
JK
25545 mapped_symtab ()
25546 {
25547 data.resize (1024);
25548 }
b89be57b 25549
bc8f2430 25550 offset_type n_elements = 0;
4b76cda9 25551 std::vector<symtab_index_entry> data;
bc8f2430 25552};
9291a0cd 25553
bc8f2430 25554/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
559a7a62
JK
25555 the slot.
25556
25557 Function is used only during write_hash_table so no index format backward
25558 compatibility is needed. */
b89be57b 25559
4b76cda9 25560static symtab_index_entry &
9291a0cd
TT
25561find_slot (struct mapped_symtab *symtab, const char *name)
25562{
559a7a62 25563 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd 25564
bc8f2430
JK
25565 index = hash & (symtab->data.size () - 1);
25566 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
9291a0cd
TT
25567
25568 for (;;)
25569 {
4b76cda9
PA
25570 if (symtab->data[index].name == NULL
25571 || strcmp (name, symtab->data[index].name) == 0)
bc8f2430
JK
25572 return symtab->data[index];
25573 index = (index + step) & (symtab->data.size () - 1);
9291a0cd
TT
25574 }
25575}
25576
25577/* Expand SYMTAB's hash table. */
b89be57b 25578
9291a0cd
TT
25579static void
25580hash_expand (struct mapped_symtab *symtab)
25581{
bc8f2430 25582 auto old_entries = std::move (symtab->data);
9291a0cd 25583
bc8f2430
JK
25584 symtab->data.clear ();
25585 symtab->data.resize (old_entries.size () * 2);
9291a0cd 25586
bc8f2430 25587 for (auto &it : old_entries)
4b76cda9 25588 if (it.name != NULL)
bc8f2430 25589 {
4b76cda9 25590 auto &ref = find_slot (symtab, it.name);
bc8f2430
JK
25591 ref = std::move (it);
25592 }
9291a0cd
TT
25593}
25594
156942c7
DE
25595/* Add an entry to SYMTAB. NAME is the name of the symbol.
25596 CU_INDEX is the index of the CU in which the symbol appears.
25597 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 25598
9291a0cd
TT
25599static void
25600add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 25601 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
25602 offset_type cu_index)
25603{
156942c7 25604 offset_type cu_index_and_attrs;
9291a0cd
TT
25605
25606 ++symtab->n_elements;
bc8f2430 25607 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
9291a0cd
TT
25608 hash_expand (symtab);
25609
4b76cda9
PA
25610 symtab_index_entry &slot = find_slot (symtab, name);
25611 if (slot.name == NULL)
9291a0cd 25612 {
4b76cda9 25613 slot.name = name;
156942c7 25614 /* index_offset is set later. */
9291a0cd 25615 }
156942c7
DE
25616
25617 cu_index_and_attrs = 0;
25618 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
25619 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
25620 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
25621
25622 /* We don't want to record an index value twice as we want to avoid the
25623 duplication.
25624 We process all global symbols and then all static symbols
25625 (which would allow us to avoid the duplication by only having to check
25626 the last entry pushed), but a symbol could have multiple kinds in one CU.
25627 To keep things simple we don't worry about the duplication here and
25628 sort and uniqufy the list after we've processed all symbols. */
4b76cda9 25629 slot.cu_indices.push_back (cu_index_and_attrs);
156942c7
DE
25630}
25631
25632/* Sort and remove duplicates of all symbols' cu_indices lists. */
25633
25634static void
25635uniquify_cu_indices (struct mapped_symtab *symtab)
25636{
4b76cda9 25637 for (auto &entry : symtab->data)
156942c7 25638 {
4b76cda9 25639 if (entry.name != NULL && !entry.cu_indices.empty ())
156942c7 25640 {
4b76cda9 25641 auto &cu_indices = entry.cu_indices;
6fd931f2
PA
25642 std::sort (cu_indices.begin (), cu_indices.end ());
25643 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
25644 cu_indices.erase (from, cu_indices.end ());
156942c7
DE
25645 }
25646 }
9291a0cd
TT
25647}
25648
bc8f2430
JK
25649/* A form of 'const char *' suitable for container keys. Only the
25650 pointer is stored. The strings themselves are compared, not the
25651 pointers. */
25652class c_str_view
9291a0cd 25653{
bc8f2430
JK
25654public:
25655 c_str_view (const char *cstr)
25656 : m_cstr (cstr)
25657 {}
9291a0cd 25658
bc8f2430
JK
25659 bool operator== (const c_str_view &other) const
25660 {
25661 return strcmp (m_cstr, other.m_cstr) == 0;
25662 }
9291a0cd 25663
437afbb8
JK
25664 /* Return the underlying C string. Note, the returned string is
25665 only a reference with lifetime of this object. */
25666 const char *c_str () const
25667 {
25668 return m_cstr;
25669 }
25670
bc8f2430
JK
25671private:
25672 friend class c_str_view_hasher;
25673 const char *const m_cstr;
25674};
9291a0cd 25675
bc8f2430
JK
25676/* A std::unordered_map::hasher for c_str_view that uses the right
25677 hash function for strings in a mapped index. */
25678class c_str_view_hasher
25679{
25680public:
25681 size_t operator () (const c_str_view &x) const
25682 {
25683 return mapped_index_string_hash (INT_MAX, x.m_cstr);
25684 }
25685};
b89be57b 25686
bc8f2430
JK
25687/* A std::unordered_map::hasher for std::vector<>. */
25688template<typename T>
25689class vector_hasher
9291a0cd 25690{
bc8f2430
JK
25691public:
25692 size_t operator () (const std::vector<T> &key) const
25693 {
25694 return iterative_hash (key.data (),
25695 sizeof (key.front ()) * key.size (), 0);
25696 }
25697};
9291a0cd 25698
bc8f2430
JK
25699/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
25700 constant pool entries going into the data buffer CPOOL. */
3876f04e 25701
bc8f2430
JK
25702static void
25703write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
25704{
25705 {
25706 /* Elements are sorted vectors of the indices of all the CUs that
25707 hold an object of this name. */
25708 std::unordered_map<std::vector<offset_type>, offset_type,
25709 vector_hasher<offset_type>>
25710 symbol_hash_table;
25711
25712 /* We add all the index vectors to the constant pool first, to
25713 ensure alignment is ok. */
4b76cda9 25714 for (symtab_index_entry &entry : symtab->data)
bc8f2430 25715 {
4b76cda9 25716 if (entry.name == NULL)
bc8f2430 25717 continue;
4b76cda9 25718 gdb_assert (entry.index_offset == 0);
70a1152b
PA
25719
25720 /* Finding before inserting is faster than always trying to
25721 insert, because inserting always allocates a node, does the
25722 lookup, and then destroys the new node if another node
25723 already had the same key. C++17 try_emplace will avoid
25724 this. */
25725 const auto found
4b76cda9 25726 = symbol_hash_table.find (entry.cu_indices);
70a1152b
PA
25727 if (found != symbol_hash_table.end ())
25728 {
4b76cda9 25729 entry.index_offset = found->second;
70a1152b
PA
25730 continue;
25731 }
25732
4b76cda9
PA
25733 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
25734 entry.index_offset = cpool.size ();
25735 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
25736 for (const auto index : entry.cu_indices)
25737 cpool.append_data (MAYBE_SWAP (index));
bc8f2430
JK
25738 }
25739 }
9291a0cd
TT
25740
25741 /* Now write out the hash table. */
bc8f2430 25742 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
4b76cda9 25743 for (const auto &entry : symtab->data)
9291a0cd
TT
25744 {
25745 offset_type str_off, vec_off;
25746
4b76cda9 25747 if (entry.name != NULL)
9291a0cd 25748 {
4b76cda9 25749 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
bc8f2430 25750 if (insertpair.second)
4b76cda9 25751 cpool.append_cstr0 (entry.name);
bc8f2430 25752 str_off = insertpair.first->second;
4b76cda9 25753 vec_off = entry.index_offset;
9291a0cd
TT
25754 }
25755 else
25756 {
25757 /* While 0 is a valid constant pool index, it is not valid
25758 to have 0 for both offsets. */
25759 str_off = 0;
25760 vec_off = 0;
25761 }
25762
bc8f2430
JK
25763 output.append_data (MAYBE_SWAP (str_off));
25764 output.append_data (MAYBE_SWAP (vec_off));
9291a0cd 25765 }
9291a0cd
TT
25766}
25767
bc8f2430 25768typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
0a5429f6
DE
25769
25770/* Helper struct for building the address table. */
25771struct addrmap_index_data
25772{
bc8f2430
JK
25773 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
25774 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
25775 {}
25776
0a5429f6 25777 struct objfile *objfile;
bc8f2430
JK
25778 data_buf &addr_vec;
25779 psym_index_map &cu_index_htab;
0a5429f6
DE
25780
25781 /* Non-zero if the previous_* fields are valid.
25782 We can't write an entry until we see the next entry (since it is only then
25783 that we know the end of the entry). */
25784 int previous_valid;
25785 /* Index of the CU in the table of all CUs in the index file. */
25786 unsigned int previous_cu_index;
0963b4bd 25787 /* Start address of the CU. */
0a5429f6
DE
25788 CORE_ADDR previous_cu_start;
25789};
25790
bc8f2430 25791/* Write an address entry to ADDR_VEC. */
b89be57b 25792
9291a0cd 25793static void
bc8f2430 25794add_address_entry (struct objfile *objfile, data_buf &addr_vec,
0a5429f6 25795 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 25796{
9291a0cd
TT
25797 CORE_ADDR baseaddr;
25798
25799 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25800
c2f134ac
PA
25801 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
25802 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
bc8f2430 25803 addr_vec.append_data (MAYBE_SWAP (cu_index));
0a5429f6
DE
25804}
25805
25806/* Worker function for traversing an addrmap to build the address table. */
25807
25808static int
25809add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
25810{
9a3c8263
SM
25811 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
25812 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
25813
25814 if (data->previous_valid)
bc8f2430 25815 add_address_entry (data->objfile, data->addr_vec,
0a5429f6
DE
25816 data->previous_cu_start, start_addr,
25817 data->previous_cu_index);
25818
25819 data->previous_cu_start = start_addr;
25820 if (pst != NULL)
25821 {
bc8f2430
JK
25822 const auto it = data->cu_index_htab.find (pst);
25823 gdb_assert (it != data->cu_index_htab.cend ());
25824 data->previous_cu_index = it->second;
0a5429f6
DE
25825 data->previous_valid = 1;
25826 }
25827 else
bc8f2430 25828 data->previous_valid = 0;
0a5429f6
DE
25829
25830 return 0;
25831}
25832
bc8f2430 25833/* Write OBJFILE's address map to ADDR_VEC.
0a5429f6
DE
25834 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
25835 in the index file. */
25836
25837static void
bc8f2430
JK
25838write_address_map (struct objfile *objfile, data_buf &addr_vec,
25839 psym_index_map &cu_index_htab)
0a5429f6 25840{
bc8f2430 25841 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
0a5429f6
DE
25842
25843 /* When writing the address table, we have to cope with the fact that
25844 the addrmap iterator only provides the start of a region; we have to
25845 wait until the next invocation to get the start of the next region. */
25846
25847 addrmap_index_data.objfile = objfile;
0a5429f6
DE
25848 addrmap_index_data.previous_valid = 0;
25849
25850 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
25851 &addrmap_index_data);
25852
25853 /* It's highly unlikely the last entry (end address = 0xff...ff)
25854 is valid, but we should still handle it.
25855 The end address is recorded as the start of the next region, but that
25856 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
25857 anyway. */
25858 if (addrmap_index_data.previous_valid)
bc8f2430 25859 add_address_entry (objfile, addr_vec,
0a5429f6
DE
25860 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
25861 addrmap_index_data.previous_cu_index);
9291a0cd
TT
25862}
25863
156942c7
DE
25864/* Return the symbol kind of PSYM. */
25865
25866static gdb_index_symbol_kind
25867symbol_kind (struct partial_symbol *psym)
25868{
25869 domain_enum domain = PSYMBOL_DOMAIN (psym);
25870 enum address_class aclass = PSYMBOL_CLASS (psym);
25871
25872 switch (domain)
25873 {
25874 case VAR_DOMAIN:
25875 switch (aclass)
25876 {
25877 case LOC_BLOCK:
25878 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
25879 case LOC_TYPEDEF:
25880 return GDB_INDEX_SYMBOL_KIND_TYPE;
25881 case LOC_COMPUTED:
25882 case LOC_CONST_BYTES:
25883 case LOC_OPTIMIZED_OUT:
25884 case LOC_STATIC:
25885 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25886 case LOC_CONST:
25887 /* Note: It's currently impossible to recognize psyms as enum values
25888 short of reading the type info. For now punt. */
25889 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25890 default:
25891 /* There are other LOC_FOO values that one might want to classify
25892 as variables, but dwarf2read.c doesn't currently use them. */
25893 return GDB_INDEX_SYMBOL_KIND_OTHER;
25894 }
25895 case STRUCT_DOMAIN:
25896 return GDB_INDEX_SYMBOL_KIND_TYPE;
25897 default:
25898 return GDB_INDEX_SYMBOL_KIND_OTHER;
25899 }
25900}
25901
9291a0cd 25902/* Add a list of partial symbols to SYMTAB. */
b89be57b 25903
9291a0cd
TT
25904static void
25905write_psymbols (struct mapped_symtab *symtab,
bc8f2430 25906 std::unordered_set<partial_symbol *> &psyms_seen,
9291a0cd
TT
25907 struct partial_symbol **psymp,
25908 int count,
987d643c
TT
25909 offset_type cu_index,
25910 int is_static)
9291a0cd
TT
25911{
25912 for (; count-- > 0; ++psymp)
25913 {
156942c7 25914 struct partial_symbol *psym = *psymp;
987d643c 25915
156942c7 25916 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 25917 error (_("Ada is not currently supported by the index"));
987d643c 25918
987d643c 25919 /* Only add a given psymbol once. */
bc8f2430 25920 if (psyms_seen.insert (psym).second)
987d643c 25921 {
156942c7
DE
25922 gdb_index_symbol_kind kind = symbol_kind (psym);
25923
156942c7
DE
25924 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
25925 is_static, kind, cu_index);
987d643c 25926 }
9291a0cd
TT
25927 }
25928}
25929
1fd400ff
TT
25930/* A helper struct used when iterating over debug_types. */
25931struct signatured_type_index_data
25932{
bc8f2430
JK
25933 signatured_type_index_data (data_buf &types_list_,
25934 std::unordered_set<partial_symbol *> &psyms_seen_)
25935 : types_list (types_list_), psyms_seen (psyms_seen_)
25936 {}
25937
1fd400ff
TT
25938 struct objfile *objfile;
25939 struct mapped_symtab *symtab;
bc8f2430
JK
25940 data_buf &types_list;
25941 std::unordered_set<partial_symbol *> &psyms_seen;
1fd400ff
TT
25942 int cu_index;
25943};
25944
25945/* A helper function that writes a single signatured_type to an
25946 obstack. */
b89be57b 25947
1fd400ff
TT
25948static int
25949write_one_signatured_type (void **slot, void *d)
25950{
9a3c8263
SM
25951 struct signatured_type_index_data *info
25952 = (struct signatured_type_index_data *) d;
1fd400ff 25953 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 25954 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
25955
25956 write_psymbols (info->symtab,
987d643c 25957 info->psyms_seen,
af5bf4ad 25958 &info->objfile->global_psymbols[psymtab->globals_offset],
987d643c
TT
25959 psymtab->n_global_syms, info->cu_index,
25960 0);
1fd400ff 25961 write_psymbols (info->symtab,
987d643c 25962 info->psyms_seen,
af5bf4ad 25963 &info->objfile->static_psymbols[psymtab->statics_offset],
987d643c
TT
25964 psymtab->n_static_syms, info->cu_index,
25965 1);
1fd400ff 25966
c2f134ac
PA
25967 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
25968 to_underlying (entry->per_cu.sect_off));
25969 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
25970 to_underlying (entry->type_offset_in_tu));
25971 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
1fd400ff
TT
25972
25973 ++info->cu_index;
25974
25975 return 1;
25976}
25977
e8f8bcb3
PA
25978/* Recurse into all "included" dependencies and count their symbols as
25979 if they appeared in this psymtab. */
25980
25981static void
25982recursively_count_psymbols (struct partial_symtab *psymtab,
25983 size_t &psyms_seen)
25984{
25985 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
25986 if (psymtab->dependencies[i]->user != NULL)
25987 recursively_count_psymbols (psymtab->dependencies[i],
25988 psyms_seen);
25989
25990 psyms_seen += psymtab->n_global_syms;
25991 psyms_seen += psymtab->n_static_syms;
25992}
25993
95554aad
TT
25994/* Recurse into all "included" dependencies and write their symbols as
25995 if they appeared in this psymtab. */
25996
25997static void
25998recursively_write_psymbols (struct objfile *objfile,
25999 struct partial_symtab *psymtab,
26000 struct mapped_symtab *symtab,
bc8f2430 26001 std::unordered_set<partial_symbol *> &psyms_seen,
95554aad
TT
26002 offset_type cu_index)
26003{
26004 int i;
26005
26006 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26007 if (psymtab->dependencies[i]->user != NULL)
26008 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26009 symtab, psyms_seen, cu_index);
26010
26011 write_psymbols (symtab,
26012 psyms_seen,
af5bf4ad 26013 &objfile->global_psymbols[psymtab->globals_offset],
95554aad
TT
26014 psymtab->n_global_syms, cu_index,
26015 0);
26016 write_psymbols (symtab,
26017 psyms_seen,
af5bf4ad 26018 &objfile->static_psymbols[psymtab->statics_offset],
95554aad
TT
26019 psymtab->n_static_syms, cu_index,
26020 1);
26021}
26022
437afbb8
JK
26023/* DWARF-5 .debug_names builder. */
26024class debug_names
26025{
26026public:
26027 debug_names (bool is_dwarf64, bfd_endian dwarf5_byte_order)
26028 : m_dwarf5_byte_order (dwarf5_byte_order),
26029 m_dwarf32 (dwarf5_byte_order),
26030 m_dwarf64 (dwarf5_byte_order),
26031 m_dwarf (is_dwarf64
26032 ? static_cast<dwarf &> (m_dwarf64)
26033 : static_cast<dwarf &> (m_dwarf32)),
26034 m_name_table_string_offs (m_dwarf.name_table_string_offs),
26035 m_name_table_entry_offs (m_dwarf.name_table_entry_offs)
26036 {}
9291a0cd 26037
8af5c486
JK
26038 int dwarf5_offset_size () const
26039 {
26040 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26041 return dwarf5_is_dwarf64 ? 8 : 4;
26042 }
26043
26044 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26045 enum class unit_kind { cu, tu };
26046
437afbb8 26047 /* Insert one symbol. */
8af5c486
JK
26048 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26049 unit_kind kind)
437afbb8
JK
26050 {
26051 const int dwarf_tag = psymbol_tag (psym);
26052 if (dwarf_tag == 0)
26053 return;
26054 const char *const name = SYMBOL_SEARCH_NAME (psym);
26055 const auto insertpair
26056 = m_name_to_value_set.emplace (c_str_view (name),
26057 std::set<symbol_value> ());
26058 std::set<symbol_value> &value_set = insertpair.first->second;
8af5c486 26059 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
437afbb8 26060 }
9291a0cd 26061
437afbb8
JK
26062 /* Build all the tables. All symbols must be already inserted.
26063 This function does not call file_write, caller has to do it
26064 afterwards. */
26065 void build ()
26066 {
26067 /* Verify the build method has not be called twice. */
26068 gdb_assert (m_abbrev_table.empty ());
26069 const size_t name_count = m_name_to_value_set.size ();
26070 m_bucket_table.resize
26071 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26072 m_hash_table.reserve (name_count);
26073 m_name_table_string_offs.reserve (name_count);
26074 m_name_table_entry_offs.reserve (name_count);
26075
26076 /* Map each hash of symbol to its name and value. */
26077 struct hash_it_pair
26078 {
26079 uint32_t hash;
26080 decltype (m_name_to_value_set)::const_iterator it;
26081 };
26082 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26083 bucket_hash.resize (m_bucket_table.size ());
26084 for (decltype (m_name_to_value_set)::const_iterator it
26085 = m_name_to_value_set.cbegin ();
26086 it != m_name_to_value_set.cend ();
26087 ++it)
26088 {
26089 const char *const name = it->first.c_str ();
26090 const uint32_t hash = dwarf5_djb_hash (name);
26091 hash_it_pair hashitpair;
26092 hashitpair.hash = hash;
26093 hashitpair.it = it;
26094 auto &slot = bucket_hash[hash % bucket_hash.size()];
26095 slot.push_front (std::move (hashitpair));
26096 }
26097 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26098 {
26099 const std::forward_list<hash_it_pair> &hashitlist
26100 = bucket_hash[bucket_ix];
26101 if (hashitlist.empty ())
26102 continue;
26103 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26104 /* The hashes array is indexed starting at 1. */
26105 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26106 sizeof (bucket_slot), m_dwarf5_byte_order,
26107 m_hash_table.size () + 1);
26108 for (const hash_it_pair &hashitpair : hashitlist)
26109 {
26110 m_hash_table.push_back (0);
26111 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26112 (&m_hash_table.back ()),
26113 sizeof (m_hash_table.back ()),
26114 m_dwarf5_byte_order, hashitpair.hash);
26115 const c_str_view &name = hashitpair.it->first;
26116 const std::set<symbol_value> &value_set = hashitpair.it->second;
26117 m_name_table_string_offs.push_back_reorder
26118 (m_debugstrlookup.lookup (name.c_str ()));
26119 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26120 gdb_assert (!value_set.empty ());
26121 for (const symbol_value &value : value_set)
26122 {
26123 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
8af5c486
JK
26124 value.is_static,
26125 value.kind)];
437afbb8
JK
26126 if (idx == 0)
26127 {
26128 idx = m_idx_next++;
26129 m_abbrev_table.append_unsigned_leb128 (idx);
26130 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
8af5c486
JK
26131 m_abbrev_table.append_unsigned_leb128
26132 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26133 : DW_IDX_type_unit);
437afbb8
JK
26134 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26135 m_abbrev_table.append_unsigned_leb128 (value.is_static
26136 ? DW_IDX_GNU_internal
26137 : DW_IDX_GNU_external);
26138 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26139
26140 /* Terminate attributes list. */
26141 m_abbrev_table.append_unsigned_leb128 (0);
26142 m_abbrev_table.append_unsigned_leb128 (0);
26143 }
9291a0cd 26144
437afbb8
JK
26145 m_entry_pool.append_unsigned_leb128 (idx);
26146 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26147 }
9291a0cd 26148
437afbb8
JK
26149 /* Terminate the list of CUs. */
26150 m_entry_pool.append_unsigned_leb128 (0);
26151 }
26152 }
26153 gdb_assert (m_hash_table.size () == name_count);
987d643c 26154
437afbb8
JK
26155 /* Terminate tags list. */
26156 m_abbrev_table.append_unsigned_leb128 (0);
26157 }
0a5429f6 26158
437afbb8
JK
26159 /* Return .debug_names bucket count. This must be called only after
26160 calling the build method. */
26161 uint32_t bucket_count () const
26162 {
26163 /* Verify the build method has been already called. */
26164 gdb_assert (!m_abbrev_table.empty ());
26165 const uint32_t retval = m_bucket_table.size ();
26166
26167 /* Check for overflow. */
26168 gdb_assert (retval == m_bucket_table.size ());
26169 return retval;
26170 }
26171
26172 /* Return .debug_names names count. This must be called only after
26173 calling the build method. */
26174 uint32_t name_count () const
26175 {
26176 /* Verify the build method has been already called. */
26177 gdb_assert (!m_abbrev_table.empty ());
26178 const uint32_t retval = m_hash_table.size ();
26179
26180 /* Check for overflow. */
26181 gdb_assert (retval == m_hash_table.size ());
26182 return retval;
26183 }
26184
26185 /* Return number of bytes of .debug_names abbreviation table. This
26186 must be called only after calling the build method. */
26187 uint32_t abbrev_table_bytes () const
26188 {
26189 gdb_assert (!m_abbrev_table.empty ());
26190 return m_abbrev_table.size ();
26191 }
26192
26193 /* Recurse into all "included" dependencies and store their symbols
26194 as if they appeared in this psymtab. */
26195 void recursively_write_psymbols
26196 (struct objfile *objfile,
26197 struct partial_symtab *psymtab,
26198 std::unordered_set<partial_symbol *> &psyms_seen,
26199 int cu_index)
26200 {
26201 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26202 if (psymtab->dependencies[i]->user != NULL)
26203 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26204 psyms_seen, cu_index);
26205
26206 write_psymbols (psyms_seen,
26207 &objfile->global_psymbols[psymtab->globals_offset],
8af5c486 26208 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
437afbb8
JK
26209 write_psymbols (psyms_seen,
26210 &objfile->static_psymbols[psymtab->statics_offset],
8af5c486 26211 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
437afbb8
JK
26212 }
26213
26214 /* Return number of bytes the .debug_names section will have. This
26215 must be called only after calling the build method. */
26216 size_t bytes () const
26217 {
26218 /* Verify the build method has been already called. */
26219 gdb_assert (!m_abbrev_table.empty ());
26220 size_t expected_bytes = 0;
26221 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26222 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26223 expected_bytes += m_name_table_string_offs.bytes ();
26224 expected_bytes += m_name_table_entry_offs.bytes ();
26225 expected_bytes += m_abbrev_table.size ();
26226 expected_bytes += m_entry_pool.size ();
26227 return expected_bytes;
26228 }
26229
26230 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26231 FILE_STR. This must be called only after calling the build
26232 method. */
26233 void file_write (FILE *file_names, FILE *file_str) const
26234 {
26235 /* Verify the build method has been already called. */
26236 gdb_assert (!m_abbrev_table.empty ());
26237 ::file_write (file_names, m_bucket_table);
26238 ::file_write (file_names, m_hash_table);
26239 m_name_table_string_offs.file_write (file_names);
26240 m_name_table_entry_offs.file_write (file_names);
26241 m_abbrev_table.file_write (file_names);
26242 m_entry_pool.file_write (file_names);
26243 m_debugstrlookup.file_write (file_str);
26244 }
26245
8af5c486
JK
26246 /* A helper user data for write_one_signatured_type. */
26247 class write_one_signatured_type_data
26248 {
26249 public:
26250 write_one_signatured_type_data (debug_names &nametable_,
26251 signatured_type_index_data &&info_)
26252 : nametable (nametable_), info (std::move (info_))
26253 {}
26254 debug_names &nametable;
26255 struct signatured_type_index_data info;
26256 };
26257
26258 /* A helper function to pass write_one_signatured_type to
26259 htab_traverse_noresize. */
26260 static int
26261 write_one_signatured_type (void **slot, void *d)
26262 {
26263 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26264 struct signatured_type_index_data *info = &data->info;
26265 struct signatured_type *entry = (struct signatured_type *) *slot;
26266
26267 data->nametable.write_one_signatured_type (entry, info);
26268
26269 return 1;
26270 }
26271
437afbb8
JK
26272private:
26273
26274 /* Storage for symbol names mapping them to their .debug_str section
26275 offsets. */
26276 class debug_str_lookup
26277 {
26278 public:
26279
26280 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26281 All .debug_str section strings are automatically stored. */
26282 debug_str_lookup ()
26283 : m_abfd (dwarf2_per_objfile->objfile->obfd)
26284 {
26285 dwarf2_read_section (dwarf2_per_objfile->objfile,
26286 &dwarf2_per_objfile->str);
26287 if (dwarf2_per_objfile->str.buffer == NULL)
26288 return;
26289 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26290 data < (dwarf2_per_objfile->str.buffer
26291 + dwarf2_per_objfile->str.size);)
26292 {
26293 const char *const s = reinterpret_cast<const char *> (data);
26294 const auto insertpair
26295 = m_str_table.emplace (c_str_view (s),
26296 data - dwarf2_per_objfile->str.buffer);
26297 if (!insertpair.second)
26298 complaint (&symfile_complaints,
26299 _("Duplicate string \"%s\" in "
26300 ".debug_str section [in module %s]"),
26301 s, bfd_get_filename (m_abfd));
26302 data += strlen (s) + 1;
26303 }
26304 }
26305
26306 /* Return offset of symbol name S in the .debug_str section. Add
26307 such symbol to the section's end if it does not exist there
26308 yet. */
26309 size_t lookup (const char *s)
26310 {
26311 const auto it = m_str_table.find (c_str_view (s));
26312 if (it != m_str_table.end ())
26313 return it->second;
26314 const size_t offset = (dwarf2_per_objfile->str.size
26315 + m_str_add_buf.size ());
26316 m_str_table.emplace (c_str_view (s), offset);
26317 m_str_add_buf.append_cstr0 (s);
26318 return offset;
26319 }
26320
26321 /* Append the end of the .debug_str section to FILE. */
26322 void file_write (FILE *file) const
26323 {
26324 m_str_add_buf.file_write (file);
26325 }
26326
26327 private:
26328 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26329 bfd *const m_abfd;
26330
26331 /* Data to add at the end of .debug_str for new needed symbol names. */
26332 data_buf m_str_add_buf;
26333 };
26334
26335 /* Container to map used DWARF tags to their .debug_names abbreviation
26336 tags. */
26337 class index_key
26338 {
26339 public:
8af5c486
JK
26340 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26341 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
437afbb8
JK
26342 {
26343 }
26344
26345 bool
26346 operator== (const index_key &other) const
26347 {
8af5c486
JK
26348 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26349 && kind == other.kind);
437afbb8
JK
26350 }
26351
26352 const int dwarf_tag;
26353 const bool is_static;
8af5c486 26354 const unit_kind kind;
437afbb8
JK
26355 };
26356
26357 /* Provide std::unordered_map::hasher for index_key. */
26358 class index_key_hasher
26359 {
26360 public:
26361 size_t
26362 operator () (const index_key &key) const
26363 {
26364 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26365 }
26366 };
26367
26368 /* Parameters of one symbol entry. */
26369 class symbol_value
26370 {
26371 public:
26372 const int dwarf_tag, cu_index;
26373 const bool is_static;
8af5c486 26374 const unit_kind kind;
437afbb8 26375
8af5c486
JK
26376 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26377 unit_kind kind_)
26378 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26379 kind (kind_)
437afbb8
JK
26380 {}
26381
26382 bool
26383 operator< (const symbol_value &other) const
26384 {
26385#define X(n) \
26386 do \
26387 { \
26388 if (n < other.n) \
26389 return true; \
26390 if (n > other.n) \
26391 return false; \
26392 } \
26393 while (0)
26394 X (dwarf_tag);
26395 X (is_static);
8af5c486 26396 X (kind);
437afbb8
JK
26397 X (cu_index);
26398#undef X
26399 return false;
26400 }
26401 };
26402
26403 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26404 output. */
26405 class offset_vec
26406 {
26407 protected:
26408 const bfd_endian dwarf5_byte_order;
26409 public:
26410 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26411 : dwarf5_byte_order (dwarf5_byte_order_)
26412 {}
26413
26414 /* Call std::vector::reserve for NELEM elements. */
26415 virtual void reserve (size_t nelem) = 0;
26416
26417 /* Call std::vector::push_back with store_unsigned_integer byte
26418 reordering for ELEM. */
26419 virtual void push_back_reorder (size_t elem) = 0;
26420
26421 /* Return expected output size in bytes. */
26422 virtual size_t bytes () const = 0;
26423
26424 /* Write name table to FILE. */
26425 virtual void file_write (FILE *file) const = 0;
26426 };
26427
26428 /* Template to unify DWARF-32 and DWARF-64 output. */
26429 template<typename OffsetSize>
26430 class offset_vec_tmpl : public offset_vec
26431 {
26432 public:
26433 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26434 : offset_vec (dwarf5_byte_order_)
26435 {}
26436
26437 /* Implement offset_vec::reserve. */
26438 void reserve (size_t nelem) override
26439 {
26440 m_vec.reserve (nelem);
26441 }
26442
26443 /* Implement offset_vec::push_back_reorder. */
26444 void push_back_reorder (size_t elem) override
26445 {
26446 m_vec.push_back (elem);
26447 /* Check for overflow. */
26448 gdb_assert (m_vec.back () == elem);
26449 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26450 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26451 }
26452
26453 /* Implement offset_vec::bytes. */
26454 size_t bytes () const override
26455 {
26456 return m_vec.size () * sizeof (m_vec[0]);
26457 }
26458
26459 /* Implement offset_vec::file_write. */
26460 void file_write (FILE *file) const override
26461 {
26462 ::file_write (file, m_vec);
26463 }
26464
26465 private:
26466 std::vector<OffsetSize> m_vec;
26467 };
26468
26469 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26470 respecting name table width. */
26471 class dwarf
26472 {
26473 public:
26474 offset_vec &name_table_string_offs, &name_table_entry_offs;
26475
26476 dwarf (offset_vec &name_table_string_offs_,
26477 offset_vec &name_table_entry_offs_)
26478 : name_table_string_offs (name_table_string_offs_),
26479 name_table_entry_offs (name_table_entry_offs_)
26480 {
26481 }
26482 };
e8f8bcb3 26483
437afbb8
JK
26484 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26485 respecting name table width. */
26486 template<typename OffsetSize>
26487 class dwarf_tmpl : public dwarf
26488 {
26489 public:
26490 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26491 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26492 m_name_table_string_offs (dwarf5_byte_order_),
26493 m_name_table_entry_offs (dwarf5_byte_order_)
26494 {}
26495
26496 private:
26497 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26498 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26499 };
26500
26501 /* Try to reconstruct original DWARF tag for given partial_symbol.
26502 This function is not DWARF-5 compliant but it is sufficient for
26503 GDB as a DWARF-5 index consumer. */
26504 static int psymbol_tag (const struct partial_symbol *psym)
26505 {
26506 domain_enum domain = PSYMBOL_DOMAIN (psym);
26507 enum address_class aclass = PSYMBOL_CLASS (psym);
26508
26509 switch (domain)
26510 {
26511 case VAR_DOMAIN:
26512 switch (aclass)
26513 {
26514 case LOC_BLOCK:
26515 return DW_TAG_subprogram;
26516 case LOC_TYPEDEF:
26517 return DW_TAG_typedef;
26518 case LOC_COMPUTED:
26519 case LOC_CONST_BYTES:
26520 case LOC_OPTIMIZED_OUT:
26521 case LOC_STATIC:
26522 return DW_TAG_variable;
26523 case LOC_CONST:
26524 /* Note: It's currently impossible to recognize psyms as enum values
26525 short of reading the type info. For now punt. */
26526 return DW_TAG_variable;
26527 default:
26528 /* There are other LOC_FOO values that one might want to classify
26529 as variables, but dwarf2read.c doesn't currently use them. */
26530 return DW_TAG_variable;
26531 }
26532 case STRUCT_DOMAIN:
26533 return DW_TAG_structure_type;
26534 default:
26535 return 0;
26536 }
26537 }
26538
26539 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26540 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26541 struct partial_symbol **psymp, int count, int cu_index,
8af5c486 26542 bool is_static, unit_kind kind)
437afbb8
JK
26543 {
26544 for (; count-- > 0; ++psymp)
26545 {
26546 struct partial_symbol *psym = *psymp;
26547
26548 if (SYMBOL_LANGUAGE (psym) == language_ada)
26549 error (_("Ada is not currently supported by the index"));
26550
26551 /* Only add a given psymbol once. */
26552 if (psyms_seen.insert (psym).second)
8af5c486 26553 insert (psym, cu_index, is_static, kind);
437afbb8
JK
26554 }
26555 }
26556
8af5c486
JK
26557 /* A helper function that writes a single signatured_type
26558 to a debug_names. */
26559 void
26560 write_one_signatured_type (struct signatured_type *entry,
26561 struct signatured_type_index_data *info)
26562 {
26563 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26564
26565 write_psymbols (info->psyms_seen,
26566 &info->objfile->global_psymbols[psymtab->globals_offset],
26567 psymtab->n_global_syms, info->cu_index, false,
26568 unit_kind::tu);
26569 write_psymbols (info->psyms_seen,
26570 &info->objfile->static_psymbols[psymtab->statics_offset],
26571 psymtab->n_static_syms, info->cu_index, true,
26572 unit_kind::tu);
26573
26574 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
26575 to_underlying (entry->per_cu.sect_off));
26576
26577 ++info->cu_index;
26578 }
26579
437afbb8
JK
26580 /* Store value of each symbol. */
26581 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
26582 m_name_to_value_set;
26583
26584 /* Tables of DWARF-5 .debug_names. They are in object file byte
26585 order. */
26586 std::vector<uint32_t> m_bucket_table;
26587 std::vector<uint32_t> m_hash_table;
26588
26589 const bfd_endian m_dwarf5_byte_order;
26590 dwarf_tmpl<uint32_t> m_dwarf32;
26591 dwarf_tmpl<uint64_t> m_dwarf64;
26592 dwarf &m_dwarf;
26593 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
26594 debug_str_lookup m_debugstrlookup;
26595
26596 /* Map each used .debug_names abbreviation tag parameter to its
26597 index value. */
26598 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
26599
26600 /* Next unused .debug_names abbreviation tag for
26601 m_indexkey_to_idx. */
26602 int m_idx_next = 1;
26603
26604 /* .debug_names abbreviation table. */
26605 data_buf m_abbrev_table;
26606
26607 /* .debug_names entry pool. */
26608 data_buf m_entry_pool;
26609};
26610
26611/* Return iff any of the needed offsets does not fit into 32-bit
26612 .debug_names section. */
26613
26614static bool
26615check_dwarf64_offsets ()
26616{
26617 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26618 {
26619 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
26620
26621 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26622 return true;
26623 }
26624 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26625 {
26626 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26627 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26628
26629 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26630 return true;
26631 }
26632 return false;
26633}
26634
26635/* The psyms_seen set is potentially going to be largish (~40k
26636 elements when indexing a -g3 build of GDB itself). Estimate the
26637 number of elements in order to avoid too many rehashes, which
26638 require rebuilding buckets and thus many trips to
26639 malloc/free. */
26640
26641static size_t
26642psyms_seen_size ()
26643{
e8f8bcb3
PA
26644 size_t psyms_count = 0;
26645 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26646 {
26647 struct dwarf2_per_cu_data *per_cu
26648 = dwarf2_per_objfile->all_comp_units[i];
26649 struct partial_symtab *psymtab = per_cu->v.psymtab;
26650
26651 if (psymtab != NULL && psymtab->user == NULL)
26652 recursively_count_psymbols (psymtab, psyms_count);
26653 }
26654 /* Generating an index for gdb itself shows a ratio of
26655 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
437afbb8
JK
26656 return psyms_count / 4;
26657}
26658
26659/* Write new .gdb_index section for OBJFILE into OUT_FILE.
26660 Return how many bytes were expected to be written into OUT_FILE. */
26661
26662static size_t
26663write_gdbindex (struct objfile *objfile, FILE *out_file)
26664{
26665 mapped_symtab symtab;
26666 data_buf cu_list;
26667
26668 /* While we're scanning CU's create a table that maps a psymtab pointer
26669 (which is what addrmap records) to its index (which is what is recorded
26670 in the index file). This will later be needed to write the address
26671 table. */
26672 psym_index_map cu_index_htab;
26673 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
26674
26675 /* The CU list is already sorted, so we don't need to do additional
26676 work here. Also, the debug_types entries do not appear in
26677 all_comp_units, but only in their own hash table. */
26678
26679 std::unordered_set<partial_symbol *> psyms_seen (psyms_seen_size ());
bc8f2430 26680 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 26681 {
3e43a32a
MS
26682 struct dwarf2_per_cu_data *per_cu
26683 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 26684 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 26685
92fac807
JK
26686 /* CU of a shared file from 'dwz -m' may be unused by this main file.
26687 It may be referenced from a local scope but in such case it does not
26688 need to be present in .gdb_index. */
26689 if (psymtab == NULL)
26690 continue;
26691
95554aad 26692 if (psymtab->user == NULL)
bc8f2430
JK
26693 recursively_write_psymbols (objfile, psymtab, &symtab,
26694 psyms_seen, i);
9291a0cd 26695
bc8f2430
JK
26696 const auto insertpair = cu_index_htab.emplace (psymtab, i);
26697 gdb_assert (insertpair.second);
9291a0cd 26698
c2f134ac
PA
26699 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
26700 to_underlying (per_cu->sect_off));
26701 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
26702 }
26703
0a5429f6 26704 /* Dump the address map. */
bc8f2430
JK
26705 data_buf addr_vec;
26706 write_address_map (objfile, addr_vec, cu_index_htab);
0a5429f6 26707
1fd400ff 26708 /* Write out the .debug_type entries, if any. */
bc8f2430 26709 data_buf types_cu_list;
1fd400ff
TT
26710 if (dwarf2_per_objfile->signatured_types)
26711 {
bc8f2430
JK
26712 signatured_type_index_data sig_data (types_cu_list,
26713 psyms_seen);
1fd400ff
TT
26714
26715 sig_data.objfile = objfile;
bc8f2430 26716 sig_data.symtab = &symtab;
1fd400ff
TT
26717 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
26718 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26719 write_one_signatured_type, &sig_data);
26720 }
26721
156942c7
DE
26722 /* Now that we've processed all symbols we can shrink their cu_indices
26723 lists. */
bc8f2430 26724 uniquify_cu_indices (&symtab);
156942c7 26725
bc8f2430
JK
26726 data_buf symtab_vec, constant_pool;
26727 write_hash_table (&symtab, symtab_vec, constant_pool);
9291a0cd 26728
bc8f2430
JK
26729 data_buf contents;
26730 const offset_type size_of_contents = 6 * sizeof (offset_type);
26731 offset_type total_len = size_of_contents;
9291a0cd
TT
26732
26733 /* The version number. */
bc8f2430 26734 contents.append_data (MAYBE_SWAP (8));
9291a0cd
TT
26735
26736 /* The offset of the CU list from the start of the file. */
bc8f2430
JK
26737 contents.append_data (MAYBE_SWAP (total_len));
26738 total_len += cu_list.size ();
9291a0cd 26739
1fd400ff 26740 /* The offset of the types CU list from the start of the file. */
bc8f2430
JK
26741 contents.append_data (MAYBE_SWAP (total_len));
26742 total_len += types_cu_list.size ();
1fd400ff 26743
9291a0cd 26744 /* The offset of the address table from the start of the file. */
bc8f2430
JK
26745 contents.append_data (MAYBE_SWAP (total_len));
26746 total_len += addr_vec.size ();
9291a0cd
TT
26747
26748 /* The offset of the symbol table from the start of the file. */
bc8f2430
JK
26749 contents.append_data (MAYBE_SWAP (total_len));
26750 total_len += symtab_vec.size ();
9291a0cd
TT
26751
26752 /* The offset of the constant pool from the start of the file. */
bc8f2430
JK
26753 contents.append_data (MAYBE_SWAP (total_len));
26754 total_len += constant_pool.size ();
9291a0cd 26755
bc8f2430 26756 gdb_assert (contents.size () == size_of_contents);
9291a0cd 26757
bc8f2430
JK
26758 contents.file_write (out_file);
26759 cu_list.file_write (out_file);
26760 types_cu_list.file_write (out_file);
26761 addr_vec.file_write (out_file);
26762 symtab_vec.file_write (out_file);
26763 constant_pool.file_write (out_file);
9291a0cd 26764
437afbb8
JK
26765 return total_len;
26766}
26767
26768/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
26769static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
26770
26771/* Write a new .debug_names section for OBJFILE into OUT_FILE, write
26772 needed addition to .debug_str section to OUT_FILE_STR. Return how
26773 many bytes were expected to be written into OUT_FILE. */
26774
26775static size_t
26776write_debug_names (struct objfile *objfile, FILE *out_file, FILE *out_file_str)
26777{
26778 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets ();
437afbb8
JK
26779 const enum bfd_endian dwarf5_byte_order
26780 = gdbarch_byte_order (get_objfile_arch (objfile));
26781
26782 /* The CU list is already sorted, so we don't need to do additional
26783 work here. Also, the debug_types entries do not appear in
26784 all_comp_units, but only in their own hash table. */
26785 data_buf cu_list;
26786 debug_names nametable (dwarf5_is_dwarf64, dwarf5_byte_order);
26787 std::unordered_set<partial_symbol *> psyms_seen (psyms_seen_size ());
26788 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26789 {
26790 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
26791 partial_symtab *psymtab = per_cu->v.psymtab;
26792
26793 /* CU of a shared file from 'dwz -m' may be unused by this main
26794 file. It may be referenced from a local scope but in such
26795 case it does not need to be present in .debug_names. */
26796 if (psymtab == NULL)
26797 continue;
26798
26799 if (psymtab->user == NULL)
26800 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
26801
8af5c486 26802 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
437afbb8
JK
26803 to_underlying (per_cu->sect_off));
26804 }
437afbb8 26805
8af5c486 26806 /* Write out the .debug_type entries, if any. */
437afbb8 26807 data_buf types_cu_list;
8af5c486 26808 if (dwarf2_per_objfile->signatured_types)
437afbb8 26809 {
8af5c486
JK
26810 debug_names::write_one_signatured_type_data sig_data (nametable,
26811 signatured_type_index_data (types_cu_list, psyms_seen));
437afbb8 26812
8af5c486
JK
26813 sig_data.info.objfile = objfile;
26814 /* It is used only for gdb_index. */
26815 sig_data.info.symtab = nullptr;
26816 sig_data.info.cu_index = 0;
26817 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26818 debug_names::write_one_signatured_type,
26819 &sig_data);
437afbb8
JK
26820 }
26821
8af5c486
JK
26822 nametable.build ();
26823
26824 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
26825
437afbb8
JK
26826 const offset_type bytes_of_header
26827 = ((dwarf5_is_dwarf64 ? 12 : 4)
26828 + 2 + 2 + 7 * 4
26829 + sizeof (dwarf5_gdb_augmentation));
26830 size_t expected_bytes = 0;
26831 expected_bytes += bytes_of_header;
26832 expected_bytes += cu_list.size ();
26833 expected_bytes += types_cu_list.size ();
26834 expected_bytes += nametable.bytes ();
26835 data_buf header;
26836
26837 if (!dwarf5_is_dwarf64)
26838 {
26839 const uint64_t size64 = expected_bytes - 4;
26840 gdb_assert (size64 < 0xfffffff0);
26841 header.append_uint (4, dwarf5_byte_order, size64);
26842 }
26843 else
26844 {
26845 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
26846 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
26847 }
26848
26849 /* The version number. */
26850 header.append_uint (2, dwarf5_byte_order, 5);
26851
26852 /* Padding. */
26853 header.append_uint (2, dwarf5_byte_order, 0);
26854
26855 /* comp_unit_count - The number of CUs in the CU list. */
26856 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
26857
26858 /* local_type_unit_count - The number of TUs in the local TU
26859 list. */
26860 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
26861
26862 /* foreign_type_unit_count - The number of TUs in the foreign TU
26863 list. */
26864 header.append_uint (4, dwarf5_byte_order, 0);
26865
26866 /* bucket_count - The number of hash buckets in the hash lookup
26867 table. */
26868 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
26869
26870 /* name_count - The number of unique names in the index. */
26871 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
26872
26873 /* abbrev_table_size - The size in bytes of the abbreviations
26874 table. */
26875 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
26876
26877 /* augmentation_string_size - The size in bytes of the augmentation
26878 string. This value is rounded up to a multiple of 4. */
26879 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
26880 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
26881 header.append_data (dwarf5_gdb_augmentation);
26882
26883 gdb_assert (header.size () == bytes_of_header);
26884
26885 header.file_write (out_file);
26886 cu_list.file_write (out_file);
26887 types_cu_list.file_write (out_file);
26888 nametable.file_write (out_file, out_file_str);
26889
26890 return expected_bytes;
26891}
26892
26893/* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
26894 position is at the end of the file. */
26895
26896static void
26897assert_file_size (FILE *file, const char *filename, size_t expected_size)
26898{
26899 const auto file_size = ftell (file);
26900 if (file_size == -1)
26901 error (_("Can't get `%s' size"), filename);
26902 gdb_assert (file_size == expected_size);
26903}
26904
437afbb8
JK
26905/* Create an index file for OBJFILE in the directory DIR. */
26906
26907static void
26908write_psymtabs_to_index (struct objfile *objfile, const char *dir,
26909 dw_index_kind index_kind)
26910{
26911 if (dwarf2_per_objfile->using_index)
26912 error (_("Cannot use an index to create the index"));
26913
26914 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
26915 error (_("Cannot make an index when the file has multiple .debug_types sections"));
26916
26917 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
26918 return;
26919
26920 struct stat st;
26921 if (stat (objfile_name (objfile), &st) < 0)
26922 perror_with_name (objfile_name (objfile));
26923
26924 std::string filename (std::string (dir) + SLASH_STRING
26925 + lbasename (objfile_name (objfile))
26926 + (index_kind == dw_index_kind::DEBUG_NAMES
26927 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
26928
26929 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
26930 if (!out_file)
26931 error (_("Can't open `%s' for writing"), filename.c_str ());
26932
26933 /* Order matters here; we want FILE to be closed before FILENAME is
26934 unlinked, because on MS-Windows one cannot delete a file that is
26935 still open. (Don't call anything here that might throw until
26936 file_closer is created.) */
26937 gdb::unlinker unlink_file (filename.c_str ());
26938 gdb_file_up close_out_file (out_file);
26939
26940 if (index_kind == dw_index_kind::DEBUG_NAMES)
26941 {
26942 std::string filename_str (std::string (dir) + SLASH_STRING
26943 + lbasename (objfile_name (objfile))
26944 + DEBUG_STR_SUFFIX);
26945 FILE *out_file_str
26946 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
26947 if (!out_file_str)
26948 error (_("Can't open `%s' for writing"), filename_str.c_str ());
26949 gdb::unlinker unlink_file_str (filename_str.c_str ());
26950 gdb_file_up close_out_file_str (out_file_str);
26951
26952 const size_t total_len
26953 = write_debug_names (objfile, out_file, out_file_str);
26954 assert_file_size (out_file, filename.c_str (), total_len);
26955
26956 /* We want to keep the file .debug_str file too. */
26957 unlink_file_str.keep ();
26958 }
26959 else
26960 {
26961 const size_t total_len
26962 = write_gdbindex (objfile, out_file);
26963 assert_file_size (out_file, filename.c_str (), total_len);
26964 }
26965
bef155c3
TT
26966 /* We want to keep the file. */
26967 unlink_file.keep ();
9291a0cd
TT
26968}
26969
90476074
TT
26970/* Implementation of the `save gdb-index' command.
26971
437afbb8
JK
26972 Note that the .gdb_index file format used by this command is
26973 documented in the GDB manual. Any changes here must be documented
26974 there. */
11570e71 26975
9291a0cd 26976static void
8384c356 26977save_gdb_index_command (const char *arg, int from_tty)
9291a0cd
TT
26978{
26979 struct objfile *objfile;
437afbb8
JK
26980 const char dwarf5space[] = "-dwarf-5 ";
26981 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
26982
26983 if (!arg)
26984 arg = "";
26985
26986 arg = skip_spaces (arg);
26987 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
26988 {
26989 index_kind = dw_index_kind::DEBUG_NAMES;
26990 arg += strlen (dwarf5space);
26991 arg = skip_spaces (arg);
26992 }
9291a0cd 26993
437afbb8
JK
26994 if (!*arg)
26995 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
9291a0cd
TT
26996
26997 ALL_OBJFILES (objfile)
26998 {
26999 struct stat st;
27000
27001 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 27002 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
27003 continue;
27004
9a3c8263
SM
27005 dwarf2_per_objfile
27006 = (struct dwarf2_per_objfile *) objfile_data (objfile,
27007 dwarf2_objfile_data_key);
9291a0cd
TT
27008 if (dwarf2_per_objfile)
27009 {
9291a0cd 27010
492d29ea 27011 TRY
9291a0cd 27012 {
437afbb8 27013 write_psymtabs_to_index (objfile, arg, index_kind);
9291a0cd 27014 }
492d29ea
PA
27015 CATCH (except, RETURN_MASK_ERROR)
27016 {
27017 exception_fprintf (gdb_stderr, except,
27018 _("Error while writing index for `%s': "),
27019 objfile_name (objfile));
27020 }
27021 END_CATCH
9291a0cd
TT
27022 }
27023 }
dce234bc
PP
27024}
27025
9291a0cd
TT
27026\f
27027
b4f54984 27028int dwarf_always_disassemble;
9eae7c52
TT
27029
27030static void
b4f54984
DE
27031show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27032 struct cmd_list_element *c, const char *value)
9eae7c52 27033{
3e43a32a
MS
27034 fprintf_filtered (file,
27035 _("Whether to always disassemble "
27036 "DWARF expressions is %s.\n"),
9eae7c52
TT
27037 value);
27038}
27039
900e11f9
JK
27040static void
27041show_check_physname (struct ui_file *file, int from_tty,
27042 struct cmd_list_element *c, const char *value)
27043{
27044 fprintf_filtered (file,
27045 _("Whether to check \"physname\" is %s.\n"),
27046 value);
27047}
27048
6502dd73
DJ
27049void
27050_initialize_dwarf2_read (void)
27051{
96d19272
JK
27052 struct cmd_list_element *c;
27053
dce234bc 27054 dwarf2_objfile_data_key
c1bd65d0 27055 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 27056
b4f54984
DE
27057 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27058Set DWARF specific variables.\n\
27059Configure DWARF variables such as the cache size"),
27060 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
27061 0/*allow-unknown*/, &maintenance_set_cmdlist);
27062
b4f54984
DE
27063 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27064Show DWARF specific variables\n\
27065Show DWARF variables such as the cache size"),
27066 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
27067 0/*allow-unknown*/, &maintenance_show_cmdlist);
27068
27069 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
27070 &dwarf_max_cache_age, _("\
27071Set the upper bound on the age of cached DWARF compilation units."), _("\
27072Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
27073A higher limit means that cached compilation units will be stored\n\
27074in memory longer, and more total memory will be used. Zero disables\n\
27075caching, which can slow down startup."),
2c5b56ce 27076 NULL,
b4f54984
DE
27077 show_dwarf_max_cache_age,
27078 &set_dwarf_cmdlist,
27079 &show_dwarf_cmdlist);
d97bc12b 27080
9eae7c52 27081 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 27082 &dwarf_always_disassemble, _("\
9eae7c52
TT
27083Set whether `info address' always disassembles DWARF expressions."), _("\
27084Show whether `info address' always disassembles DWARF expressions."), _("\
27085When enabled, DWARF expressions are always printed in an assembly-like\n\
27086syntax. When disabled, expressions will be printed in a more\n\
27087conversational style, when possible."),
27088 NULL,
b4f54984
DE
27089 show_dwarf_always_disassemble,
27090 &set_dwarf_cmdlist,
27091 &show_dwarf_cmdlist);
27092
27093 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27094Set debugging of the DWARF reader."), _("\
27095Show debugging of the DWARF reader."), _("\
27096When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
27097reading and symtab expansion. A value of 1 (one) provides basic\n\
27098information. A value greater than 1 provides more verbose information."),
45cfd468
DE
27099 NULL,
27100 NULL,
27101 &setdebuglist, &showdebuglist);
27102
b4f54984
DE
27103 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27104Set debugging of the DWARF DIE reader."), _("\
27105Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
27106When enabled (non-zero), DIEs are dumped after they are read in.\n\
27107The value is the maximum depth to print."),
ccce17b0
YQ
27108 NULL,
27109 NULL,
27110 &setdebuglist, &showdebuglist);
9291a0cd 27111
27e0867f
DE
27112 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27113Set debugging of the dwarf line reader."), _("\
27114Show debugging of the dwarf line reader."), _("\
27115When enabled (non-zero), line number entries are dumped as they are read in.\n\
27116A value of 1 (one) provides basic information.\n\
27117A value greater than 1 provides more verbose information."),
27118 NULL,
27119 NULL,
27120 &setdebuglist, &showdebuglist);
27121
900e11f9
JK
27122 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27123Set cross-checking of \"physname\" code against demangler."), _("\
27124Show cross-checking of \"physname\" code against demangler."), _("\
27125When enabled, GDB's internal \"physname\" code is checked against\n\
27126the demangler."),
27127 NULL, show_check_physname,
27128 &setdebuglist, &showdebuglist);
27129
e615022a
DE
27130 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27131 no_class, &use_deprecated_index_sections, _("\
27132Set whether to use deprecated gdb_index sections."), _("\
27133Show whether to use deprecated gdb_index sections."), _("\
27134When enabled, deprecated .gdb_index sections are used anyway.\n\
27135Normally they are ignored either because of a missing feature or\n\
27136performance issue.\n\
27137Warning: This option must be enabled before gdb reads the file."),
27138 NULL,
27139 NULL,
27140 &setlist, &showlist);
27141
96d19272 27142 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 27143 _("\
fc1a9d6e 27144Save a gdb-index file.\n\
437afbb8
JK
27145Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27146\n\
27147No options create one file with .gdb-index extension for pre-DWARF-5\n\
27148compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27149extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
96d19272
JK
27150 &save_cmdlist);
27151 set_cmd_completer (c, filename_completer);
f1e6e072
TT
27152
27153 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27154 &dwarf2_locexpr_funcs);
27155 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27156 &dwarf2_loclist_funcs);
27157
27158 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27159 &dwarf2_block_frame_base_locexpr_funcs);
27160 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27161 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
27162
27163#if GDB_SELF_TEST
27164 selftests::register_test ("dw2_expand_symtabs_matching",
27165 selftests::dw2_expand_symtabs_matching::run_test);
27166#endif
6502dd73 27167}
This page took 6.099114 seconds and 4 git commands to generate.