Support wildmatching in .debug_names too.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
61baf725 3 Copyright (C) 1994-2017 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. */
44ed8f3e 280struct mapped_index : 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. */
44ed8f3e 321struct mapped_debug_names : 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
4841/* A wrapper around mapped_index that builds a mock mapped_index, from
4842 the symbol list passed as parameter to the constructor. */
4843class mock_mapped_index
4844{
4845public:
4846 template<size_t N>
4847 mock_mapped_index (const char *(&symbols)[N])
4848 : mock_mapped_index (symbols, N)
4849 {}
4850
4851 /* Access the built index. */
4852 mapped_index &index ()
4853 { return m_index; }
4854
4855 /* Disable copy. */
4856 mock_mapped_index(const mock_mapped_index &) = delete;
4857 void operator= (const mock_mapped_index &) = delete;
4858
4859private:
4860 mock_mapped_index (const char **symbols, size_t symbols_size)
4861 {
4862 /* No string can live at offset zero. Add a dummy entry. */
4863 obstack_grow_str0 (&m_constant_pool, "");
4864
4865 for (size_t i = 0; i < symbols_size; i++)
4866 {
4867 const char *sym = symbols[i];
4868 size_t offset = obstack_object_size (&m_constant_pool);
4869 obstack_grow_str0 (&m_constant_pool, sym);
f00a2de2 4870 m_symbol_table.push_back ({offset, 0});
c62446b1
PA
4871 };
4872
4873 m_index.constant_pool = (const char *) obstack_base (&m_constant_pool);
f00a2de2 4874 m_index.symbol_table = m_symbol_table;
c62446b1
PA
4875 }
4876
4877public:
4878 /* The built mapped_index. */
4879 mapped_index m_index{};
4880
4881 /* The storage that the built mapped_index uses for symbol and
4882 constant pool tables. */
f00a2de2 4883 std::vector<mapped_index::symbol_table_slot> m_symbol_table;
c62446b1
PA
4884 auto_obstack m_constant_pool;
4885};
4886
4887/* Convenience function that converts a NULL pointer to a "<null>"
4888 string, to pass to print routines. */
4889
4890static const char *
4891string_or_null (const char *str)
4892{
4893 return str != NULL ? str : "<null>";
4894}
4895
4896/* Check if a lookup_name_info built from
4897 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4898 index. EXPECTED_LIST is the list of expected matches, in expected
4899 matching order. If no match expected, then an empty list is
4900 specified. Returns true on success. On failure prints a warning
4901 indicating the file:line that failed, and returns false. */
4902
4903static bool
4904check_match (const char *file, int line,
4905 mock_mapped_index &mock_index,
4906 const char *name, symbol_name_match_type match_type,
4907 bool completion_mode,
4908 std::initializer_list<const char *> expected_list)
4909{
4910 lookup_name_info lookup_name (name, match_type, completion_mode);
4911
4912 bool matched = true;
4913
4914 auto mismatch = [&] (const char *expected_str,
4915 const char *got)
4916 {
4917 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4918 "expected=\"%s\", got=\"%s\"\n"),
4919 file, line,
4920 (match_type == symbol_name_match_type::FULL
4921 ? "FULL" : "WILD"),
4922 name, string_or_null (expected_str), string_or_null (got));
4923 matched = false;
4924 };
4925
4926 auto expected_it = expected_list.begin ();
4927 auto expected_end = expected_list.end ();
4928
4929 dw2_expand_symtabs_matching_symbol (mock_index.index (), lookup_name,
4930 NULL, ALL_DOMAIN,
4931 [&] (offset_type idx)
4932 {
4933 const char *matched_name = mock_index.index ().symbol_name_at (idx);
4934 const char *expected_str
4935 = expected_it == expected_end ? NULL : *expected_it++;
4936
4937 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4938 mismatch (expected_str, matched_name);
4939 });
4940
4941 const char *expected_str
4942 = expected_it == expected_end ? NULL : *expected_it++;
4943 if (expected_str != NULL)
4944 mismatch (expected_str, NULL);
4945
4946 return matched;
4947}
4948
4949/* The symbols added to the mock mapped_index for testing (in
4950 canonical form). */
4951static const char *test_symbols[] = {
4952 "function",
4953 "std::bar",
4954 "std::zfunction",
4955 "std::zfunction2",
4956 "w1::w2",
4957 "ns::foo<char*>",
4958 "ns::foo<int>",
4959 "ns::foo<long>",
a20714ff
PA
4960 "ns2::tmpl<int>::foo2",
4961 "(anonymous namespace)::A::B::C",
c62446b1 4962
e1ef7d7a
PA
4963 /* These are used to check that the increment-last-char in the
4964 matching algorithm for completion doesn't match "t1_fund" when
4965 completing "t1_func". */
4966 "t1_func",
4967 "t1_func1",
4968 "t1_fund",
4969 "t1_fund1",
4970
4971 /* A UTF-8 name with multi-byte sequences to make sure that
4972 cp-name-parser understands this as a single identifier ("função"
4973 is "function" in PT). */
4974 u8"u8função",
4975
4976 /* \377 (0xff) is Latin1 'ÿ'. */
4977 "yfunc\377",
4978
4979 /* \377 (0xff) is Latin1 'ÿ'. */
4980 "\377",
4981 "\377\377123",
4982
c62446b1
PA
4983 /* A name with all sorts of complications. Starts with "z" to make
4984 it easier for the completion tests below. */
4985#define Z_SYM_NAME \
4986 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4987 "::tuple<(anonymous namespace)::ui*, " \
4988 "std::default_delete<(anonymous namespace)::ui>, void>"
4989
4990 Z_SYM_NAME
4991};
4992
5c58de74
PA
4993/* Returns true if the mapped_index::find_name_component_bounds method
4994 finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME, in
4995 completion mode. */
4996
4997static bool
4998check_find_bounds_finds (mapped_index &index,
4999 const char *search_name,
5000 gdb::array_view<const char *> expected_syms)
5001{
5002 lookup_name_info lookup_name (search_name,
5003 symbol_name_match_type::FULL, true);
5004
5005 auto bounds = index.find_name_components_bounds (lookup_name);
5006
5007 size_t distance = std::distance (bounds.first, bounds.second);
5008 if (distance != expected_syms.size ())
5009 return false;
5010
5011 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
5012 {
5013 auto nc_elem = bounds.first + exp_elem;
5014 const char *qualified = index.symbol_name_at (nc_elem->idx);
5015 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
5016 return false;
5017 }
5018
5019 return true;
5020}
5021
5022/* Test the lower-level mapped_index::find_name_component_bounds
5023 method. */
5024
c62446b1 5025static void
5c58de74
PA
5026test_mapped_index_find_name_component_bounds ()
5027{
5028 mock_mapped_index mock_index (test_symbols);
5029
5030 mock_index.index ().build_name_components ();
5031
5032 /* Test the lower-level mapped_index::find_name_component_bounds
5033 method in completion mode. */
5034 {
5035 static const char *expected_syms[] = {
5036 "t1_func",
5037 "t1_func1",
5c58de74
PA
5038 };
5039
5040 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
5041 "t1_func", expected_syms));
5042 }
5043
5044 /* Check that the increment-last-char in the name matching algorithm
5045 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5046 {
5047 static const char *expected_syms1[] = {
5048 "\377",
5049 "\377\377123",
5050 };
5051 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
5052 "\377", expected_syms1));
5053
5054 static const char *expected_syms2[] = {
5055 "\377\377123",
5056 };
5057 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
5058 "\377\377", expected_syms2));
5059 }
5060}
5061
5062/* Test dw2_expand_symtabs_matching_symbol. */
5063
5064static void
5065test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
5066{
5067 mock_mapped_index mock_index (test_symbols);
5068
5069 /* We let all tests run until the end even if some fails, for debug
5070 convenience. */
5071 bool any_mismatch = false;
5072
5073 /* Create the expected symbols list (an initializer_list). Needed
5074 because lists have commas, and we need to pass them to CHECK,
5075 which is a macro. */
5076#define EXPECT(...) { __VA_ARGS__ }
5077
5078 /* Wrapper for check_match that passes down the current
5079 __FILE__/__LINE__. */
5080#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5081 any_mismatch |= !check_match (__FILE__, __LINE__, \
5082 mock_index, \
5083 NAME, MATCH_TYPE, COMPLETION_MODE, \
5084 EXPECTED_LIST)
5085
5086 /* Identity checks. */
5087 for (const char *sym : test_symbols)
5088 {
5089 /* Should be able to match all existing symbols. */
5090 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5091 EXPECT (sym));
5092
5093 /* Should be able to match all existing symbols with
5094 parameters. */
5095 std::string with_params = std::string (sym) + "(int)";
5096 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5097 EXPECT (sym));
5098
5099 /* Should be able to match all existing symbols with
5100 parameters and qualifiers. */
5101 with_params = std::string (sym) + " ( int ) const";
5102 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5103 EXPECT (sym));
5104
5105 /* This should really find sym, but cp-name-parser.y doesn't
5106 know about lvalue/rvalue qualifiers yet. */
5107 with_params = std::string (sym) + " ( int ) &&";
5108 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5109 {});
5110 }
5111
e1ef7d7a
PA
5112 /* Check that the name matching algorithm for completion doesn't get
5113 confused with Latin1 'ÿ' / 0xff. */
5114 {
5115 static const char str[] = "\377";
5116 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5117 EXPECT ("\377", "\377\377123"));
5118 }
5119
5120 /* Check that the increment-last-char in the matching algorithm for
5121 completion doesn't match "t1_fund" when completing "t1_func". */
5122 {
5123 static const char str[] = "t1_func";
5124 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5125 EXPECT ("t1_func", "t1_func1"));
5126 }
5127
c62446b1
PA
5128 /* Check that completion mode works at each prefix of the expected
5129 symbol name. */
5130 {
5131 static const char str[] = "function(int)";
5132 size_t len = strlen (str);
5133 std::string lookup;
5134
5135 for (size_t i = 1; i < len; i++)
5136 {
5137 lookup.assign (str, i);
5138 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5139 EXPECT ("function"));
5140 }
5141 }
5142
5143 /* While "w" is a prefix of both components, the match function
5144 should still only be called once. */
5145 {
5146 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5147 EXPECT ("w1::w2"));
a20714ff
PA
5148 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5149 EXPECT ("w1::w2"));
c62446b1
PA
5150 }
5151
5152 /* Same, with a "complicated" symbol. */
5153 {
5154 static const char str[] = Z_SYM_NAME;
5155 size_t len = strlen (str);
5156 std::string lookup;
5157
5158 for (size_t i = 1; i < len; i++)
5159 {
5160 lookup.assign (str, i);
5161 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5162 EXPECT (Z_SYM_NAME));
5163 }
5164 }
5165
5166 /* In FULL mode, an incomplete symbol doesn't match. */
5167 {
5168 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5169 {});
5170 }
5171
5172 /* A complete symbol with parameters matches any overload, since the
5173 index has no overload info. */
5174 {
5175 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5176 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
5177 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5178 EXPECT ("std::zfunction", "std::zfunction2"));
5179 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5180 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
5181 }
5182
5183 /* Check that whitespace is ignored appropriately. A symbol with a
5184 template argument list. */
5185 {
5186 static const char expected[] = "ns::foo<int>";
5187 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5188 EXPECT (expected));
a20714ff
PA
5189 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5190 EXPECT (expected));
c62446b1
PA
5191 }
5192
5193 /* Check that whitespace is ignored appropriately. A symbol with a
5194 template argument list that includes a pointer. */
5195 {
5196 static const char expected[] = "ns::foo<char*>";
5197 /* Try both completion and non-completion modes. */
5198 static const bool completion_mode[2] = {false, true};
5199 for (size_t i = 0; i < 2; i++)
5200 {
5201 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5202 completion_mode[i], EXPECT (expected));
a20714ff
PA
5203 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5204 completion_mode[i], EXPECT (expected));
c62446b1
PA
5205
5206 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5207 completion_mode[i], EXPECT (expected));
a20714ff
PA
5208 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5209 completion_mode[i], EXPECT (expected));
c62446b1
PA
5210 }
5211 }
5212
5213 {
5214 /* Check method qualifiers are ignored. */
5215 static const char expected[] = "ns::foo<char*>";
5216 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5217 symbol_name_match_type::FULL, true, EXPECT (expected));
5218 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5219 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
5220 CHECK_MATCH ("foo < char * > ( int ) const",
5221 symbol_name_match_type::WILD, true, EXPECT (expected));
5222 CHECK_MATCH ("foo < char * > ( int ) &&",
5223 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
5224 }
5225
5226 /* Test lookup names that don't match anything. */
5227 {
a20714ff
PA
5228 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5229 {});
5230
c62446b1
PA
5231 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5232 {});
5233 }
5234
a20714ff
PA
5235 /* Some wild matching tests, exercising "(anonymous namespace)",
5236 which should not be confused with a parameter list. */
5237 {
5238 static const char *syms[] = {
5239 "A::B::C",
5240 "B::C",
5241 "C",
5242 "A :: B :: C ( int )",
5243 "B :: C ( int )",
5244 "C ( int )",
5245 };
5246
5247 for (const char *s : syms)
5248 {
5249 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5250 EXPECT ("(anonymous namespace)::A::B::C"));
5251 }
5252 }
5253
5254 {
5255 static const char expected[] = "ns2::tmpl<int>::foo2";
5256 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5257 EXPECT (expected));
5258 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5259 EXPECT (expected));
5260 }
5261
c62446b1
PA
5262 SELF_CHECK (!any_mismatch);
5263
5264#undef EXPECT
5265#undef CHECK_MATCH
5266}
5267
5c58de74
PA
5268static void
5269run_test ()
5270{
5271 test_mapped_index_find_name_component_bounds ();
5272 test_dw2_expand_symtabs_matching_symbol ();
5273}
5274
c62446b1
PA
5275}} // namespace selftests::dw2_expand_symtabs_matching
5276
5277#endif /* GDB_SELF_TEST */
5278
4b514bc8
JK
5279/* If FILE_MATCHER is NULL or if PER_CU has
5280 dwarf2_per_cu_quick_data::MARK set (see
5281 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5282 EXPANSION_NOTIFY on it. */
5283
5284static void
5285dw2_expand_symtabs_matching_one
5286 (struct dwarf2_per_cu_data *per_cu,
5287 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5288 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5289{
5290 if (file_matcher == NULL || per_cu->v.quick->mark)
5291 {
5292 bool symtab_was_null
5293 = (per_cu->v.quick->compunit_symtab == NULL);
5294
5295 dw2_instantiate_symtab (per_cu);
5296
5297 if (expansion_notify != NULL
5298 && symtab_was_null
5299 && per_cu->v.quick->compunit_symtab != NULL)
5300 expansion_notify (per_cu->v.quick->compunit_symtab);
5301 }
5302}
5303
3f563c84
PA
5304/* Helper for dw2_expand_matching symtabs. Called on each symbol
5305 matched, to expand corresponding CUs that were marked. IDX is the
5306 index of the symbol name that matched. */
5307
5308static void
5309dw2_expand_marked_cus
5310 (mapped_index &index, offset_type idx,
5311 struct objfile *objfile,
5312 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5313 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5314 search_domain kind)
5315{
3f563c84
PA
5316 offset_type *vec, vec_len, vec_idx;
5317 bool global_seen = false;
5318
61920122 5319 vec = (offset_type *) (index.constant_pool
f00a2de2 5320 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5321 vec_len = MAYBE_SWAP (vec[0]);
5322 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5323 {
5324 struct dwarf2_per_cu_data *per_cu;
5325 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5326 /* This value is only valid for index versions >= 7. */
5327 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5328 gdb_index_symbol_kind symbol_kind =
5329 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5330 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5331 /* Only check the symbol attributes if they're present.
5332 Indices prior to version 7 don't record them,
5333 and indices >= 7 may elide them for certain symbols
5334 (gold does this). */
5335 int attrs_valid =
5336 (index.version >= 7
5337 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5338
5339 /* Work around gold/15646. */
5340 if (attrs_valid)
9291a0cd 5341 {
61920122
PA
5342 if (!is_static && global_seen)
5343 continue;
5344 if (!is_static)
5345 global_seen = true;
5346 }
3190f0c6 5347
61920122
PA
5348 /* Only check the symbol's kind if it has one. */
5349 if (attrs_valid)
5350 {
5351 switch (kind)
8943b874 5352 {
61920122
PA
5353 case VARIABLES_DOMAIN:
5354 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5355 continue;
5356 break;
5357 case FUNCTIONS_DOMAIN:
5358 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5359 continue;
61920122
PA
5360 break;
5361 case TYPES_DOMAIN:
5362 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5363 continue;
5364 break;
5365 default:
5366 break;
8943b874 5367 }
61920122 5368 }
8943b874 5369
61920122
PA
5370 /* Don't crash on bad data. */
5371 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5372 + dwarf2_per_objfile->n_type_units))
5373 {
5374 complaint (&symfile_complaints,
5375 _(".gdb_index entry has bad CU index"
5376 " [in module %s]"), objfile_name (objfile));
5377 continue;
5378 }
5379
5380 per_cu = dw2_get_cutu (cu_index);
4b514bc8
JK
5381 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5382 expansion_notify);
61920122
PA
5383 }
5384}
5385
4b514bc8
JK
5386/* If FILE_MATCHER is non-NULL, set all the
5387 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5388 that match FILE_MATCHER. */
5389
61920122 5390static void
4b514bc8
JK
5391dw_expand_symtabs_matching_file_matcher
5392 (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5393{
4b514bc8 5394 if (file_matcher == NULL)
61920122
PA
5395 return;
5396
4b514bc8
JK
5397 objfile *const objfile = dwarf2_per_objfile->objfile;
5398
5399 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5400 htab_eq_pointer,
5401 NULL, xcalloc, xfree));
5402 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5403 htab_eq_pointer,
5404 NULL, xcalloc, xfree));
61920122 5405
4b514bc8
JK
5406 /* The rule is CUs specify all the files, including those used by
5407 any TU, so there's no need to scan TUs here. */
61920122 5408
927aa2e7
JK
5409 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5410 {
5411 int j;
5412 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5413 struct quick_file_names *file_data;
5414 void **slot;
5415
5416 QUIT;
5417
5418 per_cu->v.quick->mark = 0;
5419
5420 /* We only need to look at symtabs not already expanded. */
5421 if (per_cu->v.quick->compunit_symtab)
5422 continue;
5423
5424 file_data = dw2_get_file_names (per_cu);
5425 if (file_data == NULL)
5426 continue;
5427
5428 if (htab_find (visited_not_found.get (), file_data) != NULL)
5429 continue;
5430 else if (htab_find (visited_found.get (), file_data) != NULL)
5431 {
5432 per_cu->v.quick->mark = 1;
5433 continue;
5434 }
5435
5436 for (j = 0; j < file_data->num_file_names; ++j)
5437 {
5438 const char *this_real_name;
5439
5440 if (file_matcher (file_data->file_names[j], false))
5441 {
5442 per_cu->v.quick->mark = 1;
5443 break;
5444 }
5445
5446 /* Before we invoke realpath, which can get expensive when many
5447 files are involved, do a quick comparison of the basenames. */
5448 if (!basenames_may_differ
5449 && !file_matcher (lbasename (file_data->file_names[j]),
5450 true))
5451 continue;
5452
5453 this_real_name = dw2_get_real_path (objfile, file_data, j);
5454 if (file_matcher (this_real_name, false))
5455 {
5456 per_cu->v.quick->mark = 1;
5457 break;
5458 }
5459 }
5460
5461 slot = htab_find_slot (per_cu->v.quick->mark
5462 ? visited_found.get ()
5463 : visited_not_found.get (),
5464 file_data, INSERT);
5465 *slot = file_data;
5466 }
5467}
5468
5469static void
5470dw2_expand_symtabs_matching
5471 (struct objfile *objfile,
5472 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5473 const lookup_name_info &lookup_name,
5474 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5475 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5476 enum search_domain kind)
5477{
927aa2e7
JK
5478 dw2_setup (objfile);
5479
5480 /* index_table is NULL if OBJF_READNOW. */
5481 if (!dwarf2_per_objfile->index_table)
5482 return;
5483
5484 dw_expand_symtabs_matching_file_matcher (file_matcher);
5485
5486 mapped_index &index = *dwarf2_per_objfile->index_table;
5487
5488 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5489 symbol_matcher,
5490 kind, [&] (offset_type idx)
5491 {
5492 dw2_expand_marked_cus (index, idx, objfile, file_matcher,
5493 expansion_notify, kind);
5494 });
5495}
5496
5497/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5498 symtab. */
5499
5500static struct compunit_symtab *
5501recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5502 CORE_ADDR pc)
5503{
5504 int i;
5505
5506 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5507 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5508 return cust;
5509
5510 if (cust->includes == NULL)
5511 return NULL;
5512
5513 for (i = 0; cust->includes[i]; ++i)
5514 {
5515 struct compunit_symtab *s = cust->includes[i];
5516
5517 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5518 if (s != NULL)
5519 return s;
5520 }
5521
5522 return NULL;
5523}
5524
5525static struct compunit_symtab *
5526dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5527 struct bound_minimal_symbol msymbol,
5528 CORE_ADDR pc,
5529 struct obj_section *section,
5530 int warn_if_readin)
5531{
5532 struct dwarf2_per_cu_data *data;
5533 struct compunit_symtab *result;
5534
5535 dw2_setup (objfile);
5536
5537 if (!objfile->psymtabs_addrmap)
5538 return NULL;
5539
5540 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5541 pc);
5542 if (!data)
5543 return NULL;
5544
5545 if (warn_if_readin && data->v.quick->compunit_symtab)
5546 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5547 paddress (get_objfile_arch (objfile), pc));
5548
5549 result
5550 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5551 pc);
5552 gdb_assert (result != NULL);
5553 return result;
5554}
5555
5556static void
5557dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5558 void *data, int need_fullname)
5559{
5560 dw2_setup (objfile);
5561
5562 if (!dwarf2_per_objfile->filenames_cache)
5563 {
5564 dwarf2_per_objfile->filenames_cache.emplace ();
5565
5566 htab_up visited (htab_create_alloc (10,
5567 htab_hash_pointer, htab_eq_pointer,
5568 NULL, xcalloc, xfree));
5569
5570 /* The rule is CUs specify all the files, including those used
5571 by any TU, so there's no need to scan TUs here. We can
5572 ignore file names coming from already-expanded CUs. */
5573
5574 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5575 {
5576 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
5577
5578 if (per_cu->v.quick->compunit_symtab)
5579 {
5580 void **slot = htab_find_slot (visited.get (),
5581 per_cu->v.quick->file_names,
5582 INSERT);
5583
5584 *slot = per_cu->v.quick->file_names;
5585 }
5586 }
5587
5588 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5589 {
5590 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5591 struct quick_file_names *file_data;
5592 void **slot;
5593
5594 /* We only need to look at symtabs not already expanded. */
5595 if (per_cu->v.quick->compunit_symtab)
5596 continue;
5597
5598 file_data = dw2_get_file_names (per_cu);
5599 if (file_data == NULL)
5600 continue;
5601
5602 slot = htab_find_slot (visited.get (), file_data, INSERT);
5603 if (*slot)
5604 {
5605 /* Already visited. */
5606 continue;
5607 }
5608 *slot = file_data;
5609
5610 for (int j = 0; j < file_data->num_file_names; ++j)
5611 {
5612 const char *filename = file_data->file_names[j];
5613 dwarf2_per_objfile->filenames_cache->seen (filename);
5614 }
5615 }
5616 }
5617
5618 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5619 {
5620 gdb::unique_xmalloc_ptr<char> this_real_name;
5621
5622 if (need_fullname)
5623 this_real_name = gdb_realpath (filename);
5624 (*fun) (filename, this_real_name.get (), data);
5625 });
5626}
5627
5628static int
5629dw2_has_symbols (struct objfile *objfile)
5630{
5631 return 1;
5632}
5633
5634const struct quick_symbol_functions dwarf2_gdb_index_functions =
5635{
5636 dw2_has_symbols,
5637 dw2_find_last_source_symtab,
5638 dw2_forget_cached_source_info,
5639 dw2_map_symtabs_matching_filename,
5640 dw2_lookup_symbol,
5641 dw2_print_stats,
5642 dw2_dump,
5643 dw2_relocate,
5644 dw2_expand_symtabs_for_function,
5645 dw2_expand_all_symtabs,
5646 dw2_expand_symtabs_with_fullname,
5647 dw2_map_matching_symbols,
5648 dw2_expand_symtabs_matching,
5649 dw2_find_pc_sect_compunit_symtab,
5650 NULL,
5651 dw2_map_symbol_filenames
5652};
5653
5654/* DWARF-5 debug_names reader. */
5655
5656/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5657static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5658
5659/* A helper function that reads the .debug_names section in SECTION
5660 and fills in MAP. FILENAME is the name of the file containing the
5661 section; it is used for error reporting.
5662
5663 Returns true if all went well, false otherwise. */
5664
5665static bool
5666read_debug_names_from_section (struct objfile *objfile,
5667 const char *filename,
5668 struct dwarf2_section_info *section,
5669 mapped_debug_names &map)
5670{
5671 if (dwarf2_section_empty_p (section))
5672 return false;
5673
5674 /* Older elfutils strip versions could keep the section in the main
5675 executable while splitting it for the separate debug info file. */
5676 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5677 return false;
5678
5679 dwarf2_read_section (objfile, section);
5680
5681 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5682
5683 const gdb_byte *addr = section->buffer;
5684
5685 bfd *const abfd = get_section_bfd_owner (section);
5686
5687 unsigned int bytes_read;
5688 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5689 addr += bytes_read;
5690
5691 map.dwarf5_is_dwarf64 = bytes_read != 4;
5692 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5693 if (bytes_read + length != section->size)
5694 {
5695 /* There may be multiple per-CU indices. */
5696 warning (_("Section .debug_names in %s length %s does not match "
5697 "section length %s, ignoring .debug_names."),
5698 filename, plongest (bytes_read + length),
5699 pulongest (section->size));
5700 return false;
5701 }
5702
5703 /* The version number. */
5704 uint16_t version = read_2_bytes (abfd, addr);
5705 addr += 2;
5706 if (version != 5)
5707 {
5708 warning (_("Section .debug_names in %s has unsupported version %d, "
5709 "ignoring .debug_names."),
5710 filename, version);
5711 return false;
5712 }
5713
5714 /* Padding. */
5715 uint16_t padding = read_2_bytes (abfd, addr);
5716 addr += 2;
5717 if (padding != 0)
5718 {
5719 warning (_("Section .debug_names in %s has unsupported padding %d, "
5720 "ignoring .debug_names."),
5721 filename, padding);
5722 return false;
5723 }
5724
5725 /* comp_unit_count - The number of CUs in the CU list. */
5726 map.cu_count = read_4_bytes (abfd, addr);
5727 addr += 4;
5728
5729 /* local_type_unit_count - The number of TUs in the local TU
5730 list. */
5731 map.tu_count = read_4_bytes (abfd, addr);
5732 addr += 4;
5733
5734 /* foreign_type_unit_count - The number of TUs in the foreign TU
5735 list. */
5736 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5737 addr += 4;
5738 if (foreign_tu_count != 0)
5739 {
5740 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5741 "ignoring .debug_names."),
5742 filename, static_cast<unsigned long> (foreign_tu_count));
5743 return false;
5744 }
5745
5746 /* bucket_count - The number of hash buckets in the hash lookup
5747 table. */
5748 map.bucket_count = read_4_bytes (abfd, addr);
5749 addr += 4;
5750
5751 /* name_count - The number of unique names in the index. */
5752 map.name_count = read_4_bytes (abfd, addr);
5753 addr += 4;
5754
5755 /* abbrev_table_size - The size in bytes of the abbreviations
5756 table. */
5757 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5758 addr += 4;
5759
5760 /* augmentation_string_size - The size in bytes of the augmentation
5761 string. This value is rounded up to a multiple of 4. */
5762 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5763 addr += 4;
5764 map.augmentation_is_gdb = ((augmentation_string_size
5765 == sizeof (dwarf5_augmentation))
5766 && memcmp (addr, dwarf5_augmentation,
5767 sizeof (dwarf5_augmentation)) == 0);
5768 augmentation_string_size += (-augmentation_string_size) & 3;
5769 addr += augmentation_string_size;
5770
5771 /* List of CUs */
5772 map.cu_table_reordered = addr;
5773 addr += map.cu_count * map.offset_size;
5774
5775 /* List of Local TUs */
5776 map.tu_table_reordered = addr;
5777 addr += map.tu_count * map.offset_size;
5778
5779 /* Hash Lookup Table */
5780 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5781 addr += map.bucket_count * 4;
5782 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5783 addr += map.name_count * 4;
5784
5785 /* Name Table */
5786 map.name_table_string_offs_reordered = addr;
5787 addr += map.name_count * map.offset_size;
5788 map.name_table_entry_offs_reordered = addr;
5789 addr += map.name_count * map.offset_size;
5790
5791 const gdb_byte *abbrev_table_start = addr;
5792 for (;;)
5793 {
5794 unsigned int bytes_read;
5795 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5796 addr += bytes_read;
5797 if (index_num == 0)
5798 break;
5799
5800 const auto insertpair
5801 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5802 if (!insertpair.second)
5803 {
5804 warning (_("Section .debug_names in %s has duplicate index %s, "
5805 "ignoring .debug_names."),
5806 filename, pulongest (index_num));
5807 return false;
5808 }
5809 mapped_debug_names::index_val &indexval = insertpair.first->second;
5810 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5811 addr += bytes_read;
5812
5813 for (;;)
5814 {
5815 mapped_debug_names::index_val::attr attr;
5816 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5817 addr += bytes_read;
5818 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5819 addr += bytes_read;
5820 if (attr.form == DW_FORM_implicit_const)
5821 {
5822 attr.implicit_const = read_signed_leb128 (abfd, addr,
5823 &bytes_read);
5824 addr += bytes_read;
5825 }
5826 if (attr.dw_idx == 0 && attr.form == 0)
5827 break;
5828 indexval.attr_vec.push_back (std::move (attr));
5829 }
5830 }
5831 if (addr != abbrev_table_start + abbrev_table_size)
5832 {
5833 warning (_("Section .debug_names in %s has abbreviation_table "
5834 "of size %zu vs. written as %u, ignoring .debug_names."),
5835 filename, addr - abbrev_table_start, abbrev_table_size);
5836 return false;
5837 }
5838 map.entry_pool = addr;
5839
5840 return true;
5841}
5842
5843/* A helper for create_cus_from_debug_names that handles the MAP's CU
5844 list. */
5845
5846static void
5847create_cus_from_debug_names_list (struct objfile *objfile,
5848 const mapped_debug_names &map,
5849 dwarf2_section_info &section,
5850 bool is_dwz, int base_offset)
5851{
5852 sect_offset sect_off_prev;
5853 for (uint32_t i = 0; i <= map.cu_count; ++i)
5854 {
5855 sect_offset sect_off_next;
5856 if (i < map.cu_count)
5857 {
5858 sect_off_next
5859 = (sect_offset) (extract_unsigned_integer
5860 (map.cu_table_reordered + i * map.offset_size,
5861 map.offset_size,
5862 map.dwarf5_byte_order));
5863 }
5864 else
5865 sect_off_next = (sect_offset) section.size;
5866 if (i >= 1)
5867 {
5868 const ULONGEST length = sect_off_next - sect_off_prev;
5869 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
5870 = create_cu_from_index_list (objfile, &section, is_dwz,
5871 sect_off_prev, length);
5872 }
5873 sect_off_prev = sect_off_next;
5874 }
5875}
5876
5877/* Read the CU list from the mapped index, and use it to create all
5878 the CU objects for this objfile. */
5879
5880static void
5881create_cus_from_debug_names (struct objfile *objfile,
5882 const mapped_debug_names &map,
5883 const mapped_debug_names &dwz_map)
5884{
5885
5886 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
5887 dwarf2_per_objfile->all_comp_units
5888 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
5889 dwarf2_per_objfile->n_comp_units);
5890
5891 create_cus_from_debug_names_list (objfile, map, dwarf2_per_objfile->info,
5892 false /* is_dwz */,
5893 0 /* base_offset */);
5894
5895 if (dwz_map.cu_count == 0)
5896 return;
5897
5898 dwz_file *dwz = dwarf2_get_dwz_file ();
5899 create_cus_from_debug_names_list (objfile, dwz_map, dwz->info,
5900 true /* is_dwz */,
5901 map.cu_count /* base_offset */);
5902}
5903
5904/* Read .debug_names. If everything went ok, initialize the "quick"
5905 elements of all the CUs and return true. Otherwise, return false. */
5906
5907static bool
5908dwarf2_read_debug_names (struct objfile *objfile)
5909{
5910 mapped_debug_names local_map, dwz_map;
5911
5912 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5913 &dwarf2_per_objfile->debug_names,
5914 local_map))
5915 return false;
5916
5917 /* Don't use the index if it's empty. */
5918 if (local_map.name_count == 0)
5919 return false;
5920
5921 /* If there is a .dwz file, read it so we can get its CU list as
5922 well. */
5923 dwz_file *dwz = dwarf2_get_dwz_file ();
5924 if (dwz != NULL)
5925 {
5926 if (!read_debug_names_from_section (objfile,
5927 bfd_get_filename (dwz->dwz_bfd),
5928 &dwz->debug_names, dwz_map))
5929 {
5930 warning (_("could not read '.debug_names' section from %s; skipping"),
5931 bfd_get_filename (dwz->dwz_bfd));
5932 return false;
5933 }
5934 }
5935
5936 create_cus_from_debug_names (objfile, local_map, dwz_map);
5937
5938 if (local_map.tu_count != 0)
5939 {
5940 /* We can only handle a single .debug_types when we have an
5941 index. */
5942 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5943 return false;
5944
5945 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5946 dwarf2_per_objfile->types, 0);
5947
5948 create_signatured_type_table_from_debug_names
5949 (objfile, local_map, section, &dwarf2_per_objfile->abbrev);
5950 }
5951
5952 create_addrmap_from_aranges (objfile, &dwarf2_per_objfile->debug_aranges);
5953
5954 dwarf2_per_objfile->debug_names_table.reset (new mapped_debug_names);
5955 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5956 dwarf2_per_objfile->using_index = 1;
5957 dwarf2_per_objfile->quick_file_names_table =
5958 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5959
5960 return true;
5961}
5962
5963/* Symbol name hashing function as specified by DWARF-5. */
5964
5965static uint32_t
5966dwarf5_djb_hash (const char *str_)
5967{
5968 const unsigned char *str = (const unsigned char *) str_;
5969
5970 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
5971 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
5972
5973 uint32_t hash = 5381;
5974 while (int c = *str++)
5975 hash = hash * 33 + tolower (c);
5976 return hash;
5977}
5978
5979/* Type used to manage iterating over all CUs looking for a symbol for
5980 .debug_names. */
5981
5982class dw2_debug_names_iterator
5983{
5984public:
5985 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5986 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5987 dw2_debug_names_iterator (const mapped_debug_names &map,
5988 bool want_specific_block,
5989 block_enum block_index, domain_enum domain,
5990 const char *name)
5991 : m_map (map), m_want_specific_block (want_specific_block),
5992 m_block_index (block_index), m_domain (domain),
5993 m_addr (find_vec_in_debug_names (map, name))
5994 {}
5995
5996 dw2_debug_names_iterator (const mapped_debug_names &map,
5997 search_domain search, uint32_t namei)
5998 : m_map (map),
5999 m_search (search),
6000 m_addr (find_vec_in_debug_names (map, namei))
6001 {}
6002
6003 /* Return the next matching CU or NULL if there are no more. */
6004 dwarf2_per_cu_data *next ();
6005
6006private:
6007 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6008 const char *name);
6009 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6010 uint32_t namei);
6011
6012 /* The internalized form of .debug_names. */
6013 const mapped_debug_names &m_map;
6014
6015 /* If true, only look for symbols that match BLOCK_INDEX. */
6016 const bool m_want_specific_block = false;
6017
6018 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6019 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6020 value. */
6021 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6022
6023 /* The kind of symbol we're looking for. */
6024 const domain_enum m_domain = UNDEF_DOMAIN;
6025 const search_domain m_search = ALL_DOMAIN;
6026
6027 /* The list of CUs from the index entry of the symbol, or NULL if
6028 not found. */
6029 const gdb_byte *m_addr;
6030};
6031
6032const char *
6033mapped_debug_names::namei_to_name (uint32_t namei) const
6034{
6035 const ULONGEST namei_string_offs
6036 = extract_unsigned_integer ((name_table_string_offs_reordered
6037 + namei * offset_size),
6038 offset_size,
6039 dwarf5_byte_order);
6040 return read_indirect_string_at_offset
6041 (dwarf2_per_objfile->objfile->obfd, namei_string_offs);
6042}
6043
6044/* Find a slot in .debug_names for the object named NAME. If NAME is
6045 found, return pointer to its pool data. If NAME cannot be found,
6046 return NULL. */
6047
6048const gdb_byte *
6049dw2_debug_names_iterator::find_vec_in_debug_names
6050 (const mapped_debug_names &map, const char *name)
6051{
6052 int (*cmp) (const char *, const char *);
6053
6054 if (current_language->la_language == language_cplus
6055 || current_language->la_language == language_fortran
6056 || current_language->la_language == language_d)
6057 {
6058 /* NAME is already canonical. Drop any qualifiers as
6059 .debug_names does not contain any. */
6060
6061 if (strchr (name, '(') != NULL)
6062 {
6063 gdb::unique_xmalloc_ptr<char> without_params
6064 = cp_remove_params (name);
6065
6066 if (without_params != NULL)
6067 {
6068 name = without_params.get();
6069 }
6070 }
6071 }
6072
6073 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6074
6075 const uint32_t full_hash = dwarf5_djb_hash (name);
6076 uint32_t namei
6077 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6078 (map.bucket_table_reordered
6079 + (full_hash % map.bucket_count)), 4,
6080 map.dwarf5_byte_order);
6081 if (namei == 0)
6082 return NULL;
6083 --namei;
6084 if (namei >= map.name_count)
6085 {
6086 complaint (&symfile_complaints,
6087 _("Wrong .debug_names with name index %u but name_count=%u "
6088 "[in module %s]"),
6089 namei, map.name_count,
6090 objfile_name (dwarf2_per_objfile->objfile));
6091 return NULL;
6092 }
6093
6094 for (;;)
6095 {
6096 const uint32_t namei_full_hash
6097 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6098 (map.hash_table_reordered + namei), 4,
6099 map.dwarf5_byte_order);
6100 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6101 return NULL;
6102
6103 if (full_hash == namei_full_hash)
6104 {
6105 const char *const namei_string = map.namei_to_name (namei);
6106
6107#if 0 /* An expensive sanity check. */
6108 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6109 {
6110 complaint (&symfile_complaints,
6111 _("Wrong .debug_names hash for string at index %u "
6112 "[in module %s]"),
6113 namei, objfile_name (dwarf2_per_objfile->objfile));
6114 return NULL;
6115 }
6116#endif
6117
6118 if (cmp (namei_string, name) == 0)
6119 {
6120 const ULONGEST namei_entry_offs
6121 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6122 + namei * map.offset_size),
6123 map.offset_size, map.dwarf5_byte_order);
6124 return map.entry_pool + namei_entry_offs;
6125 }
6126 }
6127
6128 ++namei;
6129 if (namei >= map.name_count)
6130 return NULL;
6131 }
6132}
6133
6134const gdb_byte *
6135dw2_debug_names_iterator::find_vec_in_debug_names
6136 (const mapped_debug_names &map, uint32_t namei)
6137{
6138 if (namei >= map.name_count)
6139 {
6140 complaint (&symfile_complaints,
6141 _("Wrong .debug_names with name index %u but name_count=%u "
6142 "[in module %s]"),
6143 namei, map.name_count,
6144 objfile_name (dwarf2_per_objfile->objfile));
6145 return NULL;
6146 }
6147
6148 const ULONGEST namei_entry_offs
6149 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6150 + namei * map.offset_size),
6151 map.offset_size, map.dwarf5_byte_order);
6152 return map.entry_pool + namei_entry_offs;
6153}
6154
6155/* See dw2_debug_names_iterator. */
6156
6157dwarf2_per_cu_data *
6158dw2_debug_names_iterator::next ()
6159{
6160 if (m_addr == NULL)
6161 return NULL;
6162
6163 bfd *const abfd = dwarf2_per_objfile->objfile->obfd;
6164
6165 again:
6166
6167 unsigned int bytes_read;
6168 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6169 m_addr += bytes_read;
6170 if (abbrev == 0)
6171 return NULL;
6172
6173 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6174 if (indexval_it == m_map.abbrev_map.cend ())
6175 {
6176 complaint (&symfile_complaints,
6177 _("Wrong .debug_names undefined abbrev code %s "
6178 "[in module %s]"),
6179 pulongest (abbrev), objfile_name (dwarf2_per_objfile->objfile));
6180 return NULL;
6181 }
6182 const mapped_debug_names::index_val &indexval = indexval_it->second;
6183 bool have_is_static = false;
6184 bool is_static;
6185 dwarf2_per_cu_data *per_cu = NULL;
6186 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6187 {
6188 ULONGEST ull;
6189 switch (attr.form)
6190 {
6191 case DW_FORM_implicit_const:
6192 ull = attr.implicit_const;
6193 break;
6194 case DW_FORM_flag_present:
6195 ull = 1;
6196 break;
6197 case DW_FORM_udata:
6198 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6199 m_addr += bytes_read;
6200 break;
6201 default:
6202 complaint (&symfile_complaints,
6203 _("Unsupported .debug_names form %s [in module %s]"),
6204 dwarf_form_name (attr.form),
6205 objfile_name (dwarf2_per_objfile->objfile));
6206 return NULL;
6207 }
6208 switch (attr.dw_idx)
6209 {
6210 case DW_IDX_compile_unit:
6211 /* Don't crash on bad data. */
6212 if (ull >= (dwarf2_per_objfile->n_comp_units
6213 + dwarf2_per_objfile->n_type_units))
6214 {
6215 complaint (&symfile_complaints,
6216 _(".debug_names entry has bad CU index %s"
6217 " [in module %s]"),
6218 pulongest (ull),
6219 objfile_name (dwarf2_per_objfile->objfile));
6220 continue;
6221 }
6222 per_cu = dw2_get_cutu (ull);
6223 break;
6224 case DW_IDX_GNU_internal:
6225 if (!m_map.augmentation_is_gdb)
6226 break;
6227 have_is_static = true;
6228 is_static = true;
6229 break;
6230 case DW_IDX_GNU_external:
6231 if (!m_map.augmentation_is_gdb)
6232 break;
6233 have_is_static = true;
6234 is_static = false;
6235 break;
6236 }
6237 }
6238
6239 /* Skip if already read in. */
6240 if (per_cu->v.quick->compunit_symtab)
6241 goto again;
6242
6243 /* Check static vs global. */
6244 if (have_is_static)
6245 {
6246 const bool want_static = m_block_index != GLOBAL_BLOCK;
6247 if (m_want_specific_block && want_static != is_static)
6248 goto again;
6249 }
6250
6251 /* Match dw2_symtab_iter_next, symbol_kind
6252 and debug_names::psymbol_tag. */
6253 switch (m_domain)
6254 {
6255 case VAR_DOMAIN:
6256 switch (indexval.dwarf_tag)
6257 {
6258 case DW_TAG_variable:
6259 case DW_TAG_subprogram:
6260 /* Some types are also in VAR_DOMAIN. */
6261 case DW_TAG_typedef:
6262 case DW_TAG_structure_type:
6263 break;
6264 default:
6265 goto again;
6266 }
6267 break;
6268 case STRUCT_DOMAIN:
6269 switch (indexval.dwarf_tag)
6270 {
6271 case DW_TAG_typedef:
6272 case DW_TAG_structure_type:
6273 break;
6274 default:
6275 goto again;
6276 }
6277 break;
6278 case LABEL_DOMAIN:
6279 switch (indexval.dwarf_tag)
6280 {
6281 case 0:
6282 case DW_TAG_variable:
6283 break;
6284 default:
6285 goto again;
6286 }
6287 break;
6288 default:
6289 break;
6290 }
6291
6292 /* Match dw2_expand_symtabs_matching, symbol_kind and
6293 debug_names::psymbol_tag. */
6294 switch (m_search)
4b514bc8 6295 {
927aa2e7
JK
6296 case VARIABLES_DOMAIN:
6297 switch (indexval.dwarf_tag)
4b514bc8 6298 {
927aa2e7
JK
6299 case DW_TAG_variable:
6300 break;
6301 default:
6302 goto again;
4b514bc8 6303 }
927aa2e7
JK
6304 break;
6305 case FUNCTIONS_DOMAIN:
6306 switch (indexval.dwarf_tag)
4b514bc8 6307 {
927aa2e7
JK
6308 case DW_TAG_subprogram:
6309 break;
6310 default:
6311 goto again;
4b514bc8 6312 }
927aa2e7
JK
6313 break;
6314 case TYPES_DOMAIN:
6315 switch (indexval.dwarf_tag)
6316 {
6317 case DW_TAG_typedef:
6318 case DW_TAG_structure_type:
6319 break;
6320 default:
6321 goto again;
6322 }
6323 break;
6324 default:
6325 break;
4b514bc8 6326 }
927aa2e7
JK
6327
6328 return per_cu;
4b514bc8 6329}
61920122 6330
927aa2e7
JK
6331static struct compunit_symtab *
6332dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6333 const char *name, domain_enum domain)
4b514bc8 6334{
927aa2e7 6335 const block_enum block_index = static_cast<block_enum> (block_index_int);
4b514bc8 6336 dw2_setup (objfile);
61920122 6337
927aa2e7
JK
6338 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6339 if (!mapp)
61920122 6340 {
927aa2e7
JK
6341 /* index is NULL if OBJF_READNOW. */
6342 return NULL;
6343 }
6344 const auto &map = *mapp;
9291a0cd 6345
927aa2e7
JK
6346 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6347 block_index, domain, name);
9703b513 6348
927aa2e7
JK
6349 struct compunit_symtab *stab_best = NULL;
6350 struct dwarf2_per_cu_data *per_cu;
6351 while ((per_cu = iter.next ()) != NULL)
6352 {
6353 struct symbol *sym, *with_opaque = NULL;
6354 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6355 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6356 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6357
927aa2e7
JK
6358 sym = block_find_symbol (block, name, domain,
6359 block_find_non_opaque_type_preferred,
6360 &with_opaque);
9703b513 6361
927aa2e7
JK
6362 /* Some caution must be observed with overloaded functions and
6363 methods, since the index will not contain any overload
6364 information (but NAME might contain it). */
a3ec0bb1 6365
927aa2e7
JK
6366 if (sym != NULL
6367 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6368 return stab;
6369 if (with_opaque != NULL
6370 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6371 stab_best = stab;
9703b513 6372
927aa2e7 6373 /* Keep looking through other CUs. */
9703b513
TT
6374 }
6375
927aa2e7 6376 return stab_best;
9703b513
TT
6377}
6378
927aa2e7
JK
6379/* This dumps minimal information about .debug_names. It is called
6380 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6381 uses this to verify that .debug_names has been loaded. */
9291a0cd 6382
927aa2e7
JK
6383static void
6384dw2_debug_names_dump (struct objfile *objfile)
6385{
9291a0cd 6386 dw2_setup (objfile);
927aa2e7
JK
6387 gdb_assert (dwarf2_per_objfile->using_index);
6388 printf_filtered (".debug_names:");
6389 if (dwarf2_per_objfile->debug_names_table)
6390 printf_filtered (" exists\n");
6391 else
6392 printf_filtered (" faked for \"readnow\"\n");
6393 printf_filtered ("\n");
9291a0cd
TT
6394}
6395
9291a0cd 6396static void
927aa2e7
JK
6397dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6398 const char *func_name)
9291a0cd 6399{
9291a0cd 6400 dw2_setup (objfile);
ae2de4f8 6401
927aa2e7
JK
6402 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6403 if (dwarf2_per_objfile->debug_names_table)
24c79950 6404 {
927aa2e7 6405 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6406
927aa2e7
JK
6407 /* Note: It doesn't matter what we pass for block_index here. */
6408 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6409 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
24c79950 6410
927aa2e7
JK
6411 struct dwarf2_per_cu_data *per_cu;
6412 while ((per_cu = iter.next ()) != NULL)
6413 dw2_instantiate_symtab (per_cu);
6414 }
6415}
24c79950 6416
927aa2e7
JK
6417static void
6418dw2_debug_names_expand_symtabs_matching
6419 (struct objfile *objfile,
6420 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6421 const lookup_name_info &lookup_name,
6422 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6423 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6424 enum search_domain kind)
6425{
6426 dw2_setup (objfile);
9291a0cd 6427
927aa2e7
JK
6428 /* debug_names_table is NULL if OBJF_READNOW. */
6429 if (!dwarf2_per_objfile->debug_names_table)
6430 return;
9291a0cd 6431
927aa2e7 6432 dw_expand_symtabs_matching_file_matcher (file_matcher);
24c79950 6433
44ed8f3e 6434 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6435
44ed8f3e
PA
6436 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6437 symbol_matcher,
6438 kind, [&] (offset_type namei)
927aa2e7 6439 {
927aa2e7
JK
6440 /* The name was matched, now expand corresponding CUs that were
6441 marked. */
6442 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6443
927aa2e7
JK
6444 struct dwarf2_per_cu_data *per_cu;
6445 while ((per_cu = iter.next ()) != NULL)
6446 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6447 expansion_notify);
44ed8f3e 6448 });
9291a0cd
TT
6449}
6450
927aa2e7 6451const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6452{
6453 dw2_has_symbols,
6454 dw2_find_last_source_symtab,
6455 dw2_forget_cached_source_info,
f8eba3c6 6456 dw2_map_symtabs_matching_filename,
927aa2e7 6457 dw2_debug_names_lookup_symbol,
9291a0cd 6458 dw2_print_stats,
927aa2e7 6459 dw2_debug_names_dump,
9291a0cd 6460 dw2_relocate,
927aa2e7 6461 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6462 dw2_expand_all_symtabs,
652a8996 6463 dw2_expand_symtabs_with_fullname,
40658b94 6464 dw2_map_matching_symbols,
927aa2e7 6465 dw2_debug_names_expand_symtabs_matching,
43f3e411 6466 dw2_find_pc_sect_compunit_symtab,
71a3c369 6467 NULL,
9291a0cd
TT
6468 dw2_map_symbol_filenames
6469};
6470
6471/* Initialize for reading DWARF for this objfile. Return 0 if this
6472 file will use psymtabs, or 1 if using the GNU index. */
6473
abccd1e7 6474const sym_fns &
9291a0cd
TT
6475dwarf2_initialize_objfile (struct objfile *objfile)
6476{
6477 /* If we're about to read full symbols, don't bother with the
6478 indices. In this case we also don't care if some other debug
6479 format is making psymtabs, because they are all about to be
6480 expanded anyway. */
6481 if ((objfile->flags & OBJF_READNOW))
6482 {
6483 int i;
6484
6485 dwarf2_per_objfile->using_index = 1;
6486 create_all_comp_units (objfile);
0e50663e 6487 create_all_type_units (objfile);
7b9f3c50
DE
6488 dwarf2_per_objfile->quick_file_names_table =
6489 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 6490
1fd400ff 6491 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 6492 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 6493 {
8832e7e3 6494 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 6495
e254ef6a
DE
6496 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6497 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6498 }
6499
6500 /* Return 1 so that gdb sees the "quick" functions. However,
6501 these functions will be no-ops because we will have expanded
6502 all symtabs. */
abccd1e7 6503 return elf_sym_fns_gdb_index;
9291a0cd
TT
6504 }
6505
927aa2e7
JK
6506 if (dwarf2_read_debug_names (objfile))
6507 return elf_sym_fns_debug_names;
6508
9291a0cd 6509 if (dwarf2_read_index (objfile))
abccd1e7 6510 return elf_sym_fns_gdb_index;
9291a0cd 6511
abccd1e7 6512 return elf_sym_fns_lazy_psyms;
9291a0cd
TT
6513}
6514
6515\f
6516
dce234bc
PP
6517/* Build a partial symbol table. */
6518
6519void
f29dff0a 6520dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6521{
c9bf0622 6522
af5bf4ad
SM
6523 if (objfile->global_psymbols.capacity () == 0
6524 && objfile->static_psymbols.capacity () == 0)
6525 init_psymbol_list (objfile, 1024);
c906108c 6526
492d29ea 6527 TRY
c9bf0622
TT
6528 {
6529 /* This isn't really ideal: all the data we allocate on the
6530 objfile's obstack is still uselessly kept around. However,
6531 freeing it seems unsafe. */
906768f9 6532 psymtab_discarder psymtabs (objfile);
c9bf0622 6533 dwarf2_build_psymtabs_hard (objfile);
906768f9 6534 psymtabs.keep ();
c9bf0622 6535 }
492d29ea
PA
6536 CATCH (except, RETURN_MASK_ERROR)
6537 {
6538 exception_print (gdb_stderr, except);
6539 }
6540 END_CATCH
c906108c 6541}
c906108c 6542
1ce1cefd
DE
6543/* Return the total length of the CU described by HEADER. */
6544
6545static unsigned int
6546get_cu_length (const struct comp_unit_head *header)
6547{
6548 return header->initial_length_size + header->length;
6549}
6550
9c541725 6551/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6552
9c541725
PA
6553static inline bool
6554offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6555{
9c541725
PA
6556 sect_offset bottom = cu_header->sect_off;
6557 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6558
9c541725 6559 return sect_off >= bottom && sect_off < top;
45452591
DE
6560}
6561
3b80fe9b
DE
6562/* Find the base address of the compilation unit for range lists and
6563 location lists. It will normally be specified by DW_AT_low_pc.
6564 In DWARF-3 draft 4, the base address could be overridden by
6565 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6566 compilation units with discontinuous ranges. */
6567
6568static void
6569dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6570{
6571 struct attribute *attr;
6572
6573 cu->base_known = 0;
6574 cu->base_address = 0;
6575
6576 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6577 if (attr)
6578 {
31aa7e4e 6579 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6580 cu->base_known = 1;
6581 }
6582 else
6583 {
6584 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6585 if (attr)
6586 {
31aa7e4e 6587 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6588 cu->base_known = 1;
6589 }
6590 }
6591}
6592
93311388 6593/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6594 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6595 NOTE: This leaves members offset, first_die_offset to be filled in
6596 by the caller. */
107d2387 6597
d521ce57 6598static const gdb_byte *
107d2387 6599read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6600 const gdb_byte *info_ptr,
6601 struct dwarf2_section_info *section,
6602 rcuh_kind section_kind)
107d2387
AC
6603{
6604 int signed_addr;
891d2f0b 6605 unsigned int bytes_read;
43988095
JK
6606 const char *filename = get_section_file_name (section);
6607 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6608
6609 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6610 cu_header->initial_length_size = bytes_read;
6611 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6612 info_ptr += bytes_read;
107d2387
AC
6613 cu_header->version = read_2_bytes (abfd, info_ptr);
6614 info_ptr += 2;
43988095
JK
6615 if (cu_header->version < 5)
6616 switch (section_kind)
6617 {
6618 case rcuh_kind::COMPILE:
6619 cu_header->unit_type = DW_UT_compile;
6620 break;
6621 case rcuh_kind::TYPE:
6622 cu_header->unit_type = DW_UT_type;
6623 break;
6624 default:
6625 internal_error (__FILE__, __LINE__,
6626 _("read_comp_unit_head: invalid section_kind"));
6627 }
6628 else
6629 {
6630 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6631 (read_1_byte (abfd, info_ptr));
6632 info_ptr += 1;
6633 switch (cu_header->unit_type)
6634 {
6635 case DW_UT_compile:
6636 if (section_kind != rcuh_kind::COMPILE)
6637 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6638 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6639 filename);
6640 break;
6641 case DW_UT_type:
6642 section_kind = rcuh_kind::TYPE;
6643 break;
6644 default:
6645 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6646 "(is %d, should be %d or %d) [in module %s]"),
6647 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6648 }
6649
6650 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6651 info_ptr += 1;
6652 }
9c541725
PA
6653 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6654 cu_header,
6655 &bytes_read);
613e1657 6656 info_ptr += bytes_read;
43988095
JK
6657 if (cu_header->version < 5)
6658 {
6659 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6660 info_ptr += 1;
6661 }
107d2387
AC
6662 signed_addr = bfd_get_sign_extend_vma (abfd);
6663 if (signed_addr < 0)
8e65ff28 6664 internal_error (__FILE__, __LINE__,
e2e0b3e5 6665 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6666 cu_header->signed_addr_p = signed_addr;
c764a876 6667
43988095
JK
6668 if (section_kind == rcuh_kind::TYPE)
6669 {
6670 LONGEST type_offset;
6671
6672 cu_header->signature = read_8_bytes (abfd, info_ptr);
6673 info_ptr += 8;
6674
6675 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6676 info_ptr += bytes_read;
9c541725
PA
6677 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6678 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6679 error (_("Dwarf Error: Too big type_offset in compilation unit "
6680 "header (is %s) [in module %s]"), plongest (type_offset),
6681 filename);
6682 }
6683
107d2387
AC
6684 return info_ptr;
6685}
6686
36586728
TT
6687/* Helper function that returns the proper abbrev section for
6688 THIS_CU. */
6689
6690static struct dwarf2_section_info *
6691get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6692{
6693 struct dwarf2_section_info *abbrev;
6694
6695 if (this_cu->is_dwz)
6696 abbrev = &dwarf2_get_dwz_file ()->abbrev;
6697 else
6698 abbrev = &dwarf2_per_objfile->abbrev;
6699
6700 return abbrev;
6701}
6702
9ff913ba
DE
6703/* Subroutine of read_and_check_comp_unit_head and
6704 read_and_check_type_unit_head to simplify them.
6705 Perform various error checking on the header. */
6706
6707static void
6708error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
6709 struct dwarf2_section_info *section,
6710 struct dwarf2_section_info *abbrev_section)
9ff913ba 6711{
a32a8923 6712 const char *filename = get_section_file_name (section);
9ff913ba 6713
43988095 6714 if (header->version < 2 || header->version > 5)
9ff913ba 6715 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 6716 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
6717 filename);
6718
9c541725 6719 if (to_underlying (header->abbrev_sect_off)
36586728 6720 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9c541725
PA
6721 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
6722 "(offset 0x%x + 6) [in module %s]"),
6723 to_underlying (header->abbrev_sect_off),
6724 to_underlying (header->sect_off),
9ff913ba
DE
6725 filename);
6726
9c541725 6727 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6728 avoid potential 32-bit overflow. */
9c541725 6729 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6730 > section->size)
9c541725
PA
6731 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6732 "(offset 0x%x + 0) [in module %s]"),
6733 header->length, to_underlying (header->sect_off),
9ff913ba
DE
6734 filename);
6735}
6736
6737/* Read in a CU/TU header and perform some basic error checking.
6738 The contents of the header are stored in HEADER.
6739 The result is a pointer to the start of the first DIE. */
adabb602 6740
d521ce57 6741static const gdb_byte *
9ff913ba
DE
6742read_and_check_comp_unit_head (struct comp_unit_head *header,
6743 struct dwarf2_section_info *section,
4bdcc0c1 6744 struct dwarf2_section_info *abbrev_section,
d521ce57 6745 const gdb_byte *info_ptr,
43988095 6746 rcuh_kind section_kind)
72bf9492 6747{
d521ce57 6748 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6749
9c541725 6750 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6751
43988095 6752 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6753
9c541725 6754 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6755
4bdcc0c1 6756 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
6757
6758 return info_ptr;
348e048f
DE
6759}
6760
f4dc4d17
DE
6761/* Fetch the abbreviation table offset from a comp or type unit header. */
6762
6763static sect_offset
6764read_abbrev_offset (struct dwarf2_section_info *section,
9c541725 6765 sect_offset sect_off)
f4dc4d17 6766{
a32a8923 6767 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6768 const gdb_byte *info_ptr;
ac298888 6769 unsigned int initial_length_size, offset_size;
43988095 6770 uint16_t version;
f4dc4d17
DE
6771
6772 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6773 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6774 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6775 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6776 info_ptr += initial_length_size;
6777
6778 version = read_2_bytes (abfd, info_ptr);
6779 info_ptr += 2;
6780 if (version >= 5)
6781 {
6782 /* Skip unit type and address size. */
6783 info_ptr += 2;
6784 }
6785
9c541725 6786 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6787}
6788
aaa75496
JB
6789/* Allocate a new partial symtab for file named NAME and mark this new
6790 partial symtab as being an include of PST. */
6791
6792static void
d521ce57 6793dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6794 struct objfile *objfile)
6795{
6796 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6797
fbd9ab74
JK
6798 if (!IS_ABSOLUTE_PATH (subpst->filename))
6799 {
6800 /* It shares objfile->objfile_obstack. */
6801 subpst->dirname = pst->dirname;
6802 }
6803
aaa75496
JB
6804 subpst->textlow = 0;
6805 subpst->texthigh = 0;
6806
8d749320
SM
6807 subpst->dependencies
6808 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
6809 subpst->dependencies[0] = pst;
6810 subpst->number_of_dependencies = 1;
6811
6812 subpst->globals_offset = 0;
6813 subpst->n_global_syms = 0;
6814 subpst->statics_offset = 0;
6815 subpst->n_static_syms = 0;
43f3e411 6816 subpst->compunit_symtab = NULL;
aaa75496
JB
6817 subpst->read_symtab = pst->read_symtab;
6818 subpst->readin = 0;
6819
6820 /* No private part is necessary for include psymtabs. This property
6821 can be used to differentiate between such include psymtabs and
10b3939b 6822 the regular ones. */
58a9656e 6823 subpst->read_symtab_private = NULL;
aaa75496
JB
6824}
6825
6826/* Read the Line Number Program data and extract the list of files
6827 included by the source file represented by PST. Build an include
d85a05f0 6828 partial symtab for each of these included files. */
aaa75496
JB
6829
6830static void
6831dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6832 struct die_info *die,
6833 struct partial_symtab *pst)
aaa75496 6834{
fff8551c 6835 line_header_up lh;
d85a05f0 6836 struct attribute *attr;
aaa75496 6837
d85a05f0
DJ
6838 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6839 if (attr)
9c541725 6840 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6841 if (lh == NULL)
6842 return; /* No linetable, so no includes. */
6843
c6da4cef 6844 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 6845 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
6846}
6847
348e048f 6848static hashval_t
52dc124a 6849hash_signatured_type (const void *item)
348e048f 6850{
9a3c8263
SM
6851 const struct signatured_type *sig_type
6852 = (const struct signatured_type *) item;
9a619af0 6853
348e048f 6854 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6855 return sig_type->signature;
348e048f
DE
6856}
6857
6858static int
52dc124a 6859eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6860{
9a3c8263
SM
6861 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6862 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6863
348e048f
DE
6864 return lhs->signature == rhs->signature;
6865}
6866
1fd400ff
TT
6867/* Allocate a hash table for signatured types. */
6868
6869static htab_t
673bfd45 6870allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6871{
6872 return htab_create_alloc_ex (41,
52dc124a
DE
6873 hash_signatured_type,
6874 eq_signatured_type,
1fd400ff
TT
6875 NULL,
6876 &objfile->objfile_obstack,
6877 hashtab_obstack_allocate,
6878 dummy_obstack_deallocate);
6879}
6880
d467dd73 6881/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6882
6883static int
d467dd73 6884add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6885{
9a3c8263
SM
6886 struct signatured_type *sigt = (struct signatured_type *) *slot;
6887 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 6888
b4dd5633 6889 **datap = sigt;
1fd400ff
TT
6890 ++*datap;
6891
6892 return 1;
6893}
6894
78d4d2c5 6895/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6896 and fill them into TYPES_HTAB. It will process only type units,
6897 therefore DW_UT_type. */
c88ee1f0 6898
78d4d2c5
JK
6899static void
6900create_debug_type_hash_table (struct dwo_file *dwo_file,
43988095
JK
6901 dwarf2_section_info *section, htab_t &types_htab,
6902 rcuh_kind section_kind)
348e048f 6903{
3019eac3 6904 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6905 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6906 bfd *abfd;
6907 const gdb_byte *info_ptr, *end_ptr;
348e048f 6908
4bdcc0c1
DE
6909 abbrev_section = (dwo_file != NULL
6910 ? &dwo_file->sections.abbrev
6911 : &dwarf2_per_objfile->abbrev);
6912
b4f54984 6913 if (dwarf_read_debug)
43988095
JK
6914 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6915 get_section_name (section),
a32a8923 6916 get_section_file_name (abbrev_section));
09406207 6917
78d4d2c5
JK
6918 dwarf2_read_section (objfile, section);
6919 info_ptr = section->buffer;
348e048f 6920
78d4d2c5
JK
6921 if (info_ptr == NULL)
6922 return;
348e048f 6923
78d4d2c5
JK
6924 /* We can't set abfd until now because the section may be empty or
6925 not present, in which case the bfd is unknown. */
6926 abfd = get_section_bfd_owner (section);
348e048f 6927
78d4d2c5
JK
6928 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6929 because we don't need to read any dies: the signature is in the
6930 header. */
3019eac3 6931
78d4d2c5
JK
6932 end_ptr = info_ptr + section->size;
6933 while (info_ptr < end_ptr)
6934 {
78d4d2c5
JK
6935 struct signatured_type *sig_type;
6936 struct dwo_unit *dwo_tu;
6937 void **slot;
6938 const gdb_byte *ptr = info_ptr;
6939 struct comp_unit_head header;
6940 unsigned int length;
8b70b953 6941
9c541725 6942 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6943
a49dd8dd
JK
6944 /* Initialize it due to a false compiler warning. */
6945 header.signature = -1;
9c541725 6946 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6947
78d4d2c5
JK
6948 /* We need to read the type's signature in order to build the hash
6949 table, but we don't need anything else just yet. */
348e048f 6950
43988095
JK
6951 ptr = read_and_check_comp_unit_head (&header, section,
6952 abbrev_section, ptr, section_kind);
348e048f 6953
78d4d2c5 6954 length = get_cu_length (&header);
6caca83c 6955
78d4d2c5
JK
6956 /* Skip dummy type units. */
6957 if (ptr >= info_ptr + length
43988095
JK
6958 || peek_abbrev_code (abfd, ptr) == 0
6959 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6960 {
6961 info_ptr += length;
6962 continue;
6963 }
dee91e82 6964
78d4d2c5
JK
6965 if (types_htab == NULL)
6966 {
6967 if (dwo_file)
6968 types_htab = allocate_dwo_unit_table (objfile);
6969 else
6970 types_htab = allocate_signatured_type_table (objfile);
6971 }
8b70b953 6972
78d4d2c5
JK
6973 if (dwo_file)
6974 {
6975 sig_type = NULL;
6976 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6977 struct dwo_unit);
6978 dwo_tu->dwo_file = dwo_file;
43988095 6979 dwo_tu->signature = header.signature;
9c541725 6980 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6981 dwo_tu->section = section;
9c541725 6982 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6983 dwo_tu->length = length;
6984 }
6985 else
6986 {
6987 /* N.B.: type_offset is not usable if this type uses a DWO file.
6988 The real type_offset is in the DWO file. */
6989 dwo_tu = NULL;
6990 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6991 struct signatured_type);
43988095 6992 sig_type->signature = header.signature;
9c541725 6993 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6994 sig_type->per_cu.objfile = objfile;
6995 sig_type->per_cu.is_debug_types = 1;
6996 sig_type->per_cu.section = section;
9c541725 6997 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6998 sig_type->per_cu.length = length;
6999 }
7000
7001 slot = htab_find_slot (types_htab,
7002 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7003 INSERT);
7004 gdb_assert (slot != NULL);
7005 if (*slot != NULL)
7006 {
9c541725 7007 sect_offset dup_sect_off;
0349ea22 7008
3019eac3
DE
7009 if (dwo_file)
7010 {
78d4d2c5
JK
7011 const struct dwo_unit *dup_tu
7012 = (const struct dwo_unit *) *slot;
7013
9c541725 7014 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
7015 }
7016 else
7017 {
78d4d2c5
JK
7018 const struct signatured_type *dup_tu
7019 = (const struct signatured_type *) *slot;
7020
9c541725 7021 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 7022 }
8b70b953 7023
78d4d2c5
JK
7024 complaint (&symfile_complaints,
7025 _("debug type entry at offset 0x%x is duplicate to"
7026 " the entry at offset 0x%x, signature %s"),
9c541725 7027 to_underlying (sect_off), to_underlying (dup_sect_off),
43988095 7028 hex_string (header.signature));
78d4d2c5
JK
7029 }
7030 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 7031
78d4d2c5
JK
7032 if (dwarf_read_debug > 1)
7033 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
9c541725 7034 to_underlying (sect_off),
43988095 7035 hex_string (header.signature));
3019eac3 7036
78d4d2c5
JK
7037 info_ptr += length;
7038 }
7039}
3019eac3 7040
78d4d2c5
JK
7041/* Create the hash table of all entries in the .debug_types
7042 (or .debug_types.dwo) section(s).
7043 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7044 otherwise it is NULL.
b3c8eb43 7045
78d4d2c5 7046 The result is a pointer to the hash table or NULL if there are no types.
348e048f 7047
78d4d2c5 7048 Note: This function processes DWO files only, not DWP files. */
348e048f 7049
78d4d2c5
JK
7050static void
7051create_debug_types_hash_table (struct dwo_file *dwo_file,
7052 VEC (dwarf2_section_info_def) *types,
7053 htab_t &types_htab)
7054{
7055 int ix;
7056 struct dwarf2_section_info *section;
7057
7058 if (VEC_empty (dwarf2_section_info_def, types))
7059 return;
348e048f 7060
78d4d2c5
JK
7061 for (ix = 0;
7062 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7063 ++ix)
43988095
JK
7064 create_debug_type_hash_table (dwo_file, section, types_htab,
7065 rcuh_kind::TYPE);
3019eac3
DE
7066}
7067
7068/* Create the hash table of all entries in the .debug_types section,
7069 and initialize all_type_units.
7070 The result is zero if there is an error (e.g. missing .debug_types section),
7071 otherwise non-zero. */
7072
7073static int
7074create_all_type_units (struct objfile *objfile)
7075{
78d4d2c5 7076 htab_t types_htab = NULL;
b4dd5633 7077 struct signatured_type **iter;
3019eac3 7078
43988095
JK
7079 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
7080 rcuh_kind::COMPILE);
78d4d2c5 7081 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
3019eac3
DE
7082 if (types_htab == NULL)
7083 {
7084 dwarf2_per_objfile->signatured_types = NULL;
7085 return 0;
7086 }
7087
348e048f
DE
7088 dwarf2_per_objfile->signatured_types = types_htab;
7089
6aa5f3a6
DE
7090 dwarf2_per_objfile->n_type_units
7091 = dwarf2_per_objfile->n_allocated_type_units
7092 = htab_elements (types_htab);
8d749320
SM
7093 dwarf2_per_objfile->all_type_units =
7094 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
7095 iter = &dwarf2_per_objfile->all_type_units[0];
7096 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7097 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7098 == dwarf2_per_objfile->n_type_units);
1fd400ff 7099
348e048f
DE
7100 return 1;
7101}
7102
6aa5f3a6
DE
7103/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7104 If SLOT is non-NULL, it is the entry to use in the hash table.
7105 Otherwise we find one. */
7106
7107static struct signatured_type *
7108add_type_unit (ULONGEST sig, void **slot)
7109{
7110 struct objfile *objfile = dwarf2_per_objfile->objfile;
7111 int n_type_units = dwarf2_per_objfile->n_type_units;
7112 struct signatured_type *sig_type;
7113
7114 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7115 ++n_type_units;
7116 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7117 {
7118 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7119 dwarf2_per_objfile->n_allocated_type_units = 1;
7120 dwarf2_per_objfile->n_allocated_type_units *= 2;
7121 dwarf2_per_objfile->all_type_units
224c3ddb
SM
7122 = XRESIZEVEC (struct signatured_type *,
7123 dwarf2_per_objfile->all_type_units,
7124 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
7125 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7126 }
7127 dwarf2_per_objfile->n_type_units = n_type_units;
7128
7129 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7130 struct signatured_type);
7131 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7132 sig_type->signature = sig;
7133 sig_type->per_cu.is_debug_types = 1;
7134 if (dwarf2_per_objfile->using_index)
7135 {
7136 sig_type->per_cu.v.quick =
7137 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7138 struct dwarf2_per_cu_quick_data);
7139 }
7140
7141 if (slot == NULL)
7142 {
7143 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7144 sig_type, INSERT);
7145 }
7146 gdb_assert (*slot == NULL);
7147 *slot = sig_type;
7148 /* The rest of sig_type must be filled in by the caller. */
7149 return sig_type;
7150}
7151
a2ce51a0
DE
7152/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7153 Fill in SIG_ENTRY with DWO_ENTRY. */
7154
7155static void
7156fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
7157 struct signatured_type *sig_entry,
7158 struct dwo_unit *dwo_entry)
7159{
7ee85ab1 7160 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
7161 gdb_assert (! sig_entry->per_cu.queued);
7162 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
7163 if (dwarf2_per_objfile->using_index)
7164 {
7165 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 7166 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
7167 }
7168 else
7169 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 7170 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 7171 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 7172 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
7173 gdb_assert (sig_entry->dwo_unit == NULL);
7174
7175 sig_entry->per_cu.section = dwo_entry->section;
9c541725 7176 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
7177 sig_entry->per_cu.length = dwo_entry->length;
7178 sig_entry->per_cu.reading_dwo_directly = 1;
7179 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
7180 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7181 sig_entry->dwo_unit = dwo_entry;
7182}
7183
7184/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
7185 If we haven't read the TU yet, create the signatured_type data structure
7186 for a TU to be read in directly from a DWO file, bypassing the stub.
7187 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7188 using .gdb_index, then when reading a CU we want to stay in the DWO file
7189 containing that CU. Otherwise we could end up reading several other DWO
7190 files (due to comdat folding) to process the transitive closure of all the
7191 mentioned TUs, and that can be slow. The current DWO file will have every
7192 type signature that it needs.
a2ce51a0
DE
7193 We only do this for .gdb_index because in the psymtab case we already have
7194 to read all the DWOs to build the type unit groups. */
7195
7196static struct signatured_type *
7197lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7198{
7199 struct objfile *objfile = dwarf2_per_objfile->objfile;
7200 struct dwo_file *dwo_file;
7201 struct dwo_unit find_dwo_entry, *dwo_entry;
7202 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7203 void **slot;
a2ce51a0
DE
7204
7205 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7206
6aa5f3a6
DE
7207 /* If TU skeletons have been removed then we may not have read in any
7208 TUs yet. */
7209 if (dwarf2_per_objfile->signatured_types == NULL)
7210 {
7211 dwarf2_per_objfile->signatured_types
7212 = allocate_signatured_type_table (objfile);
7213 }
a2ce51a0
DE
7214
7215 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7216 Use the global signatured_types array to do our own comdat-folding
7217 of types. If this is the first time we're reading this TU, and
7218 the TU has an entry in .gdb_index, replace the recorded data from
7219 .gdb_index with this TU. */
a2ce51a0 7220
a2ce51a0 7221 find_sig_entry.signature = sig;
6aa5f3a6
DE
7222 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7223 &find_sig_entry, INSERT);
9a3c8263 7224 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7225
7226 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7227 read. Don't reassign the global entry to point to this DWO if that's
7228 the case. Also note that if the TU is already being read, it may not
7229 have come from a DWO, the program may be a mix of Fission-compiled
7230 code and non-Fission-compiled code. */
7231
7232 /* Have we already tried to read this TU?
7233 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7234 needn't exist in the global table yet). */
7235 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7236 return sig_entry;
7237
6aa5f3a6
DE
7238 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7239 dwo_unit of the TU itself. */
7240 dwo_file = cu->dwo_unit->dwo_file;
7241
a2ce51a0
DE
7242 /* Ok, this is the first time we're reading this TU. */
7243 if (dwo_file->tus == NULL)
7244 return NULL;
7245 find_dwo_entry.signature = sig;
9a3c8263 7246 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7247 if (dwo_entry == NULL)
7248 return NULL;
7249
6aa5f3a6
DE
7250 /* If the global table doesn't have an entry for this TU, add one. */
7251 if (sig_entry == NULL)
7252 sig_entry = add_type_unit (sig, slot);
7253
a2ce51a0 7254 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 7255 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7256 return sig_entry;
7257}
7258
a2ce51a0
DE
7259/* Subroutine of lookup_signatured_type.
7260 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7261 then try the DWP file. If the TU stub (skeleton) has been removed then
7262 it won't be in .gdb_index. */
a2ce51a0
DE
7263
7264static struct signatured_type *
7265lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7266{
7267 struct objfile *objfile = dwarf2_per_objfile->objfile;
7268 struct dwp_file *dwp_file = get_dwp_file ();
7269 struct dwo_unit *dwo_entry;
7270 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7271 void **slot;
a2ce51a0
DE
7272
7273 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7274 gdb_assert (dwp_file != NULL);
7275
6aa5f3a6
DE
7276 /* If TU skeletons have been removed then we may not have read in any
7277 TUs yet. */
7278 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7279 {
6aa5f3a6
DE
7280 dwarf2_per_objfile->signatured_types
7281 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7282 }
7283
6aa5f3a6
DE
7284 find_sig_entry.signature = sig;
7285 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7286 &find_sig_entry, INSERT);
9a3c8263 7287 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7288
7289 /* Have we already tried to read this TU?
7290 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7291 needn't exist in the global table yet). */
7292 if (sig_entry != NULL)
7293 return sig_entry;
7294
a2ce51a0
DE
7295 if (dwp_file->tus == NULL)
7296 return NULL;
57d63ce2
DE
7297 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
7298 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7299 if (dwo_entry == NULL)
7300 return NULL;
7301
6aa5f3a6 7302 sig_entry = add_type_unit (sig, slot);
a2ce51a0
DE
7303 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
7304
a2ce51a0
DE
7305 return sig_entry;
7306}
7307
380bca97 7308/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7309 Returns NULL if signature SIG is not present in the table.
7310 It is up to the caller to complain about this. */
348e048f
DE
7311
7312static struct signatured_type *
a2ce51a0 7313lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7314{
a2ce51a0
DE
7315 if (cu->dwo_unit
7316 && dwarf2_per_objfile->using_index)
7317 {
7318 /* We're in a DWO/DWP file, and we're using .gdb_index.
7319 These cases require special processing. */
7320 if (get_dwp_file () == NULL)
7321 return lookup_dwo_signatured_type (cu, sig);
7322 else
7323 return lookup_dwp_signatured_type (cu, sig);
7324 }
7325 else
7326 {
7327 struct signatured_type find_entry, *entry;
348e048f 7328
a2ce51a0
DE
7329 if (dwarf2_per_objfile->signatured_types == NULL)
7330 return NULL;
7331 find_entry.signature = sig;
9a3c8263
SM
7332 entry = ((struct signatured_type *)
7333 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7334 return entry;
7335 }
348e048f 7336}
42e7ad6c
DE
7337\f
7338/* Low level DIE reading support. */
348e048f 7339
d85a05f0
DJ
7340/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7341
7342static void
7343init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7344 struct dwarf2_cu *cu,
3019eac3
DE
7345 struct dwarf2_section_info *section,
7346 struct dwo_file *dwo_file)
d85a05f0 7347{
fceca515 7348 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7349 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7350 reader->cu = cu;
3019eac3 7351 reader->dwo_file = dwo_file;
dee91e82
DE
7352 reader->die_section = section;
7353 reader->buffer = section->buffer;
f664829e 7354 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7355 reader->comp_dir = NULL;
d85a05f0
DJ
7356}
7357
b0c7bfa9
DE
7358/* Subroutine of init_cutu_and_read_dies to simplify it.
7359 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7360 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7361 already.
7362
7363 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7364 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7365 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7366 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7367 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7368 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7369 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7370 are filled in with the info of the DIE from the DWO file.
7371 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
7372 provided an abbrev table to use.
7373 The result is non-zero if a valid (non-dummy) DIE was found. */
7374
7375static int
7376read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7377 struct dwo_unit *dwo_unit,
7378 int abbrev_table_provided,
7379 struct die_info *stub_comp_unit_die,
a2ce51a0 7380 const char *stub_comp_dir,
b0c7bfa9 7381 struct die_reader_specs *result_reader,
d521ce57 7382 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
7383 struct die_info **result_comp_unit_die,
7384 int *result_has_children)
7385{
7386 struct objfile *objfile = dwarf2_per_objfile->objfile;
7387 struct dwarf2_cu *cu = this_cu->cu;
7388 struct dwarf2_section_info *section;
7389 bfd *abfd;
d521ce57 7390 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7391 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7392 int i,num_extra_attrs;
7393 struct dwarf2_section_info *dwo_abbrev_section;
7394 struct attribute *attr;
7395 struct die_info *comp_unit_die;
7396
b0aeadb3
DE
7397 /* At most one of these may be provided. */
7398 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7399
b0c7bfa9
DE
7400 /* These attributes aren't processed until later:
7401 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7402 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7403 referenced later. However, these attributes are found in the stub
7404 which we won't have later. In order to not impose this complication
7405 on the rest of the code, we read them here and copy them to the
7406 DWO CU/TU die. */
b0c7bfa9
DE
7407
7408 stmt_list = NULL;
7409 low_pc = NULL;
7410 high_pc = NULL;
7411 ranges = NULL;
7412 comp_dir = NULL;
7413
7414 if (stub_comp_unit_die != NULL)
7415 {
7416 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7417 DWO file. */
7418 if (! this_cu->is_debug_types)
7419 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7420 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7421 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7422 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7423 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7424
7425 /* There should be a DW_AT_addr_base attribute here (if needed).
7426 We need the value before we can process DW_FORM_GNU_addr_index. */
7427 cu->addr_base = 0;
7428 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7429 if (attr)
7430 cu->addr_base = DW_UNSND (attr);
7431
7432 /* There should be a DW_AT_ranges_base attribute here (if needed).
7433 We need the value before we can process DW_AT_ranges. */
7434 cu->ranges_base = 0;
7435 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7436 if (attr)
7437 cu->ranges_base = DW_UNSND (attr);
7438 }
a2ce51a0
DE
7439 else if (stub_comp_dir != NULL)
7440 {
7441 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7442 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7443 comp_dir->name = DW_AT_comp_dir;
7444 comp_dir->form = DW_FORM_string;
7445 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7446 DW_STRING (comp_dir) = stub_comp_dir;
7447 }
b0c7bfa9
DE
7448
7449 /* Set up for reading the DWO CU/TU. */
7450 cu->dwo_unit = dwo_unit;
7451 section = dwo_unit->section;
7452 dwarf2_read_section (objfile, section);
a32a8923 7453 abfd = get_section_bfd_owner (section);
9c541725
PA
7454 begin_info_ptr = info_ptr = (section->buffer
7455 + to_underlying (dwo_unit->sect_off));
b0c7bfa9
DE
7456 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7457 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
7458
7459 if (this_cu->is_debug_types)
7460 {
b0c7bfa9
DE
7461 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7462
43988095 7463 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
b0c7bfa9 7464 dwo_abbrev_section,
43988095 7465 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7466 /* This is not an assert because it can be caused by bad debug info. */
43988095 7467 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7468 {
7469 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7470 " TU at offset 0x%x [in module %s]"),
7471 hex_string (sig_type->signature),
43988095 7472 hex_string (cu->header.signature),
9c541725 7473 to_underlying (dwo_unit->sect_off),
a2ce51a0
DE
7474 bfd_get_filename (abfd));
7475 }
9c541725 7476 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7477 /* For DWOs coming from DWP files, we don't know the CU length
7478 nor the type's offset in the TU until now. */
7479 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7480 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7481
7482 /* Establish the type offset that can be used to lookup the type.
7483 For DWO files, we don't know it until now. */
9c541725
PA
7484 sig_type->type_offset_in_section
7485 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7486 }
7487 else
7488 {
7489 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
7490 dwo_abbrev_section,
43988095 7491 info_ptr, rcuh_kind::COMPILE);
9c541725 7492 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7493 /* For DWOs coming from DWP files, we don't know the CU length
7494 until now. */
7495 dwo_unit->length = get_cu_length (&cu->header);
7496 }
7497
02142a6c
DE
7498 /* Replace the CU's original abbrev table with the DWO's.
7499 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
7500 if (abbrev_table_provided)
7501 {
7502 /* Don't free the provided abbrev table, the caller of
7503 init_cutu_and_read_dies owns it. */
7504 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 7505 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
7506 make_cleanup (dwarf2_free_abbrev_table, cu);
7507 }
7508 else
7509 {
7510 dwarf2_free_abbrev_table (cu);
7511 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 7512 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
7513 }
7514
7515 /* Read in the die, but leave space to copy over the attributes
7516 from the stub. This has the benefit of simplifying the rest of
7517 the code - all the work to maintain the illusion of a single
7518 DW_TAG_{compile,type}_unit DIE is done here. */
7519 num_extra_attrs = ((stmt_list != NULL)
7520 + (low_pc != NULL)
7521 + (high_pc != NULL)
7522 + (ranges != NULL)
7523 + (comp_dir != NULL));
7524 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7525 result_has_children, num_extra_attrs);
7526
7527 /* Copy over the attributes from the stub to the DIE we just read in. */
7528 comp_unit_die = *result_comp_unit_die;
7529 i = comp_unit_die->num_attrs;
7530 if (stmt_list != NULL)
7531 comp_unit_die->attrs[i++] = *stmt_list;
7532 if (low_pc != NULL)
7533 comp_unit_die->attrs[i++] = *low_pc;
7534 if (high_pc != NULL)
7535 comp_unit_die->attrs[i++] = *high_pc;
7536 if (ranges != NULL)
7537 comp_unit_die->attrs[i++] = *ranges;
7538 if (comp_dir != NULL)
7539 comp_unit_die->attrs[i++] = *comp_dir;
7540 comp_unit_die->num_attrs += num_extra_attrs;
7541
b4f54984 7542 if (dwarf_die_debug)
bf6af496
DE
7543 {
7544 fprintf_unfiltered (gdb_stdlog,
7545 "Read die from %s@0x%x of %s:\n",
a32a8923 7546 get_section_name (section),
bf6af496
DE
7547 (unsigned) (begin_info_ptr - section->buffer),
7548 bfd_get_filename (abfd));
b4f54984 7549 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7550 }
7551
a2ce51a0
DE
7552 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7553 TUs by skipping the stub and going directly to the entry in the DWO file.
7554 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7555 to get it via circuitous means. Blech. */
7556 if (comp_dir != NULL)
7557 result_reader->comp_dir = DW_STRING (comp_dir);
7558
b0c7bfa9
DE
7559 /* Skip dummy compilation units. */
7560 if (info_ptr >= begin_info_ptr + dwo_unit->length
7561 || peek_abbrev_code (abfd, info_ptr) == 0)
7562 return 0;
7563
7564 *result_info_ptr = info_ptr;
7565 return 1;
7566}
7567
7568/* Subroutine of init_cutu_and_read_dies to simplify it.
7569 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7570 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7571
7572static struct dwo_unit *
7573lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7574 struct die_info *comp_unit_die)
7575{
7576 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7577 ULONGEST signature;
7578 struct dwo_unit *dwo_unit;
7579 const char *comp_dir, *dwo_name;
7580
a2ce51a0
DE
7581 gdb_assert (cu != NULL);
7582
b0c7bfa9 7583 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
7584 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7585 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7586
7587 if (this_cu->is_debug_types)
7588 {
7589 struct signatured_type *sig_type;
7590
7591 /* Since this_cu is the first member of struct signatured_type,
7592 we can go from a pointer to one to a pointer to the other. */
7593 sig_type = (struct signatured_type *) this_cu;
7594 signature = sig_type->signature;
7595 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7596 }
7597 else
7598 {
7599 struct attribute *attr;
7600
7601 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7602 if (! attr)
7603 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7604 " [in module %s]"),
4262abfb 7605 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
7606 signature = DW_UNSND (attr);
7607 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7608 signature);
7609 }
7610
b0c7bfa9
DE
7611 return dwo_unit;
7612}
7613
a2ce51a0 7614/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6
DE
7615 See it for a description of the parameters.
7616 Read a TU directly from a DWO file, bypassing the stub.
7617
7618 Note: This function could be a little bit simpler if we shared cleanups
7619 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
7620 to do, so we keep this function self-contained. Or we could move this
7621 into our caller, but it's complex enough already. */
a2ce51a0
DE
7622
7623static void
6aa5f3a6
DE
7624init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7625 int use_existing_cu, int keep,
a2ce51a0
DE
7626 die_reader_func_ftype *die_reader_func,
7627 void *data)
7628{
7629 struct dwarf2_cu *cu;
7630 struct signatured_type *sig_type;
6aa5f3a6 7631 struct cleanup *cleanups, *free_cu_cleanup = NULL;
a2ce51a0
DE
7632 struct die_reader_specs reader;
7633 const gdb_byte *info_ptr;
7634 struct die_info *comp_unit_die;
7635 int has_children;
7636
7637 /* Verify we can do the following downcast, and that we have the
7638 data we need. */
7639 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7640 sig_type = (struct signatured_type *) this_cu;
7641 gdb_assert (sig_type->dwo_unit != NULL);
7642
7643 cleanups = make_cleanup (null_cleanup, NULL);
7644
6aa5f3a6
DE
7645 if (use_existing_cu && this_cu->cu != NULL)
7646 {
7647 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7648 cu = this_cu->cu;
7649 /* There's no need to do the rereading_dwo_cu handling that
7650 init_cutu_and_read_dies does since we don't read the stub. */
7651 }
7652 else
7653 {
7654 /* If !use_existing_cu, this_cu->cu must be NULL. */
7655 gdb_assert (this_cu->cu == NULL);
8d749320 7656 cu = XNEW (struct dwarf2_cu);
6aa5f3a6
DE
7657 init_one_comp_unit (cu, this_cu);
7658 /* If an error occurs while loading, release our storage. */
7659 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
7660 }
7661
7662 /* A future optimization, if needed, would be to use an existing
7663 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7664 could share abbrev tables. */
a2ce51a0
DE
7665
7666 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7667 0 /* abbrev_table_provided */,
7668 NULL /* stub_comp_unit_die */,
7669 sig_type->dwo_unit->dwo_file->comp_dir,
7670 &reader, &info_ptr,
7671 &comp_unit_die, &has_children) == 0)
7672 {
7673 /* Dummy die. */
7674 do_cleanups (cleanups);
7675 return;
7676 }
7677
7678 /* All the "real" work is done here. */
7679 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7680
6aa5f3a6 7681 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7682 but the alternative is making the latter more complex.
7683 This function is only for the special case of using DWO files directly:
7684 no point in overly complicating the general case just to handle this. */
6aa5f3a6 7685 if (free_cu_cleanup != NULL)
a2ce51a0 7686 {
6aa5f3a6
DE
7687 if (keep)
7688 {
7689 /* We've successfully allocated this compilation unit. Let our
7690 caller clean it up when finished with it. */
7691 discard_cleanups (free_cu_cleanup);
a2ce51a0 7692
6aa5f3a6
DE
7693 /* We can only discard free_cu_cleanup and all subsequent cleanups.
7694 So we have to manually free the abbrev table. */
7695 dwarf2_free_abbrev_table (cu);
a2ce51a0 7696
6aa5f3a6
DE
7697 /* Link this CU into read_in_chain. */
7698 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7699 dwarf2_per_objfile->read_in_chain = this_cu;
7700 }
7701 else
7702 do_cleanups (free_cu_cleanup);
a2ce51a0 7703 }
a2ce51a0
DE
7704
7705 do_cleanups (cleanups);
7706}
7707
fd820528 7708/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7709 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7710
f4dc4d17
DE
7711 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7712 Otherwise the table specified in the comp unit header is read in and used.
7713 This is an optimization for when we already have the abbrev table.
7714
dee91e82
DE
7715 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7716 Otherwise, a new CU is allocated with xmalloc.
7717
7718 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7719 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7720
7721 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7722 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7723
70221824 7724static void
fd820528 7725init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7726 struct abbrev_table *abbrev_table,
fd820528
DE
7727 int use_existing_cu, int keep,
7728 die_reader_func_ftype *die_reader_func,
7729 void *data)
c906108c 7730{
dee91e82 7731 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7732 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7733 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7734 struct dwarf2_cu *cu;
d521ce57 7735 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7736 struct die_reader_specs reader;
d85a05f0 7737 struct die_info *comp_unit_die;
dee91e82 7738 int has_children;
d85a05f0 7739 struct attribute *attr;
365156ad 7740 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 7741 struct signatured_type *sig_type = NULL;
4bdcc0c1 7742 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7743 /* Non-zero if CU currently points to a DWO file and we need to
7744 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7745 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7746 int rereading_dwo_cu = 0;
c906108c 7747
b4f54984 7748 if (dwarf_die_debug)
09406207
DE
7749 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7750 this_cu->is_debug_types ? "type" : "comp",
9c541725 7751 to_underlying (this_cu->sect_off));
09406207 7752
dee91e82
DE
7753 if (use_existing_cu)
7754 gdb_assert (keep);
23745b47 7755
a2ce51a0
DE
7756 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7757 file (instead of going through the stub), short-circuit all of this. */
7758 if (this_cu->reading_dwo_directly)
7759 {
7760 /* Narrow down the scope of possibilities to have to understand. */
7761 gdb_assert (this_cu->is_debug_types);
7762 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7763 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7764 die_reader_func, data);
a2ce51a0
DE
7765 return;
7766 }
7767
dee91e82
DE
7768 cleanups = make_cleanup (null_cleanup, NULL);
7769
7770 /* This is cheap if the section is already read in. */
7771 dwarf2_read_section (objfile, section);
7772
9c541725 7773 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7774
7775 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
7776
7777 if (use_existing_cu && this_cu->cu != NULL)
7778 {
7779 cu = this_cu->cu;
42e7ad6c
DE
7780 /* If this CU is from a DWO file we need to start over, we need to
7781 refetch the attributes from the skeleton CU.
7782 This could be optimized by retrieving those attributes from when we
7783 were here the first time: the previous comp_unit_die was stored in
7784 comp_unit_obstack. But there's no data yet that we need this
7785 optimization. */
7786 if (cu->dwo_unit != NULL)
7787 rereading_dwo_cu = 1;
dee91e82
DE
7788 }
7789 else
7790 {
7791 /* If !use_existing_cu, this_cu->cu must be NULL. */
7792 gdb_assert (this_cu->cu == NULL);
8d749320 7793 cu = XNEW (struct dwarf2_cu);
dee91e82 7794 init_one_comp_unit (cu, this_cu);
dee91e82 7795 /* If an error occurs while loading, release our storage. */
365156ad 7796 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 7797 }
dee91e82 7798
b0c7bfa9 7799 /* Get the header. */
9c541725 7800 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7801 {
7802 /* We already have the header, there's no need to read it in again. */
9c541725 7803 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7804 }
7805 else
7806 {
3019eac3 7807 if (this_cu->is_debug_types)
dee91e82 7808 {
43988095 7809 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4bdcc0c1 7810 abbrev_section, info_ptr,
43988095 7811 rcuh_kind::TYPE);
dee91e82 7812
42e7ad6c
DE
7813 /* Since per_cu is the first member of struct signatured_type,
7814 we can go from a pointer to one to a pointer to the other. */
7815 sig_type = (struct signatured_type *) this_cu;
43988095 7816 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7817 gdb_assert (sig_type->type_offset_in_tu
7818 == cu->header.type_cu_offset_in_tu);
7819 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7820
42e7ad6c
DE
7821 /* LENGTH has not been set yet for type units if we're
7822 using .gdb_index. */
1ce1cefd 7823 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7824
7825 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7826 sig_type->type_offset_in_section =
7827 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7828
7829 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7830 }
7831 else
7832 {
4bdcc0c1
DE
7833 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
7834 abbrev_section,
43988095
JK
7835 info_ptr,
7836 rcuh_kind::COMPILE);
dee91e82 7837
9c541725 7838 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7839 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7840 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7841 }
7842 }
10b3939b 7843
6caca83c 7844 /* Skip dummy compilation units. */
dee91e82 7845 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
7846 || peek_abbrev_code (abfd, info_ptr) == 0)
7847 {
dee91e82 7848 do_cleanups (cleanups);
21b2bd31 7849 return;
6caca83c
CC
7850 }
7851
433df2d4
DE
7852 /* If we don't have them yet, read the abbrevs for this compilation unit.
7853 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
7854 done. Note that it's important that if the CU had an abbrev table
7855 on entry we don't free it when we're done: Somewhere up the call stack
7856 it may be in use. */
f4dc4d17
DE
7857 if (abbrev_table != NULL)
7858 {
7859 gdb_assert (cu->abbrev_table == NULL);
9c541725 7860 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
f4dc4d17
DE
7861 cu->abbrev_table = abbrev_table;
7862 }
7863 else if (cu->abbrev_table == NULL)
dee91e82 7864 {
4bdcc0c1 7865 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
7866 make_cleanup (dwarf2_free_abbrev_table, cu);
7867 }
42e7ad6c
DE
7868 else if (rereading_dwo_cu)
7869 {
7870 dwarf2_free_abbrev_table (cu);
7871 dwarf2_read_abbrevs (cu, abbrev_section);
7872 }
af703f96 7873
dee91e82 7874 /* Read the top level CU/TU die. */
3019eac3 7875 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 7876 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7877
b0c7bfa9
DE
7878 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7879 from the DWO file.
7880 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7881 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
7882 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7883 if (attr)
7884 {
3019eac3 7885 struct dwo_unit *dwo_unit;
b0c7bfa9 7886 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7887
7888 if (has_children)
6a506a2d
DE
7889 {
7890 complaint (&symfile_complaints,
7891 _("compilation unit with DW_AT_GNU_dwo_name"
7892 " has children (offset 0x%x) [in module %s]"),
9c541725 7893 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6a506a2d 7894 }
b0c7bfa9 7895 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7896 if (dwo_unit != NULL)
3019eac3 7897 {
6a506a2d
DE
7898 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7899 abbrev_table != NULL,
a2ce51a0 7900 comp_unit_die, NULL,
6a506a2d
DE
7901 &reader, &info_ptr,
7902 &dwo_comp_unit_die, &has_children) == 0)
7903 {
7904 /* Dummy die. */
7905 do_cleanups (cleanups);
7906 return;
7907 }
7908 comp_unit_die = dwo_comp_unit_die;
7909 }
7910 else
7911 {
7912 /* Yikes, we couldn't find the rest of the DIE, we only have
7913 the stub. A complaint has already been logged. There's
7914 not much more we can do except pass on the stub DIE to
7915 die_reader_func. We don't want to throw an error on bad
7916 debug info. */
3019eac3
DE
7917 }
7918 }
7919
b0c7bfa9 7920 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7921 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7922
b0c7bfa9 7923 /* Done, clean up. */
365156ad 7924 if (free_cu_cleanup != NULL)
348e048f 7925 {
365156ad
TT
7926 if (keep)
7927 {
7928 /* We've successfully allocated this compilation unit. Let our
7929 caller clean it up when finished with it. */
7930 discard_cleanups (free_cu_cleanup);
dee91e82 7931
365156ad
TT
7932 /* We can only discard free_cu_cleanup and all subsequent cleanups.
7933 So we have to manually free the abbrev table. */
7934 dwarf2_free_abbrev_table (cu);
dee91e82 7935
365156ad
TT
7936 /* Link this CU into read_in_chain. */
7937 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7938 dwarf2_per_objfile->read_in_chain = this_cu;
7939 }
7940 else
7941 do_cleanups (free_cu_cleanup);
348e048f 7942 }
365156ad
TT
7943
7944 do_cleanups (cleanups);
dee91e82
DE
7945}
7946
33e80786
DE
7947/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7948 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7949 to have already done the lookup to find the DWO file).
dee91e82
DE
7950
7951 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7952 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7953
7954 We fill in THIS_CU->length.
7955
7956 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7957 linker) then DIE_READER_FUNC will not get called.
7958
7959 THIS_CU->cu is always freed when done.
3019eac3
DE
7960 This is done in order to not leave THIS_CU->cu in a state where we have
7961 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7962
7963static void
7964init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7965 struct dwo_file *dwo_file,
dee91e82
DE
7966 die_reader_func_ftype *die_reader_func,
7967 void *data)
7968{
7969 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7970 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7971 bfd *abfd = get_section_bfd_owner (section);
33e80786 7972 struct dwarf2_section_info *abbrev_section;
dee91e82 7973 struct dwarf2_cu cu;
d521ce57 7974 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
7975 struct die_reader_specs reader;
7976 struct cleanup *cleanups;
7977 struct die_info *comp_unit_die;
7978 int has_children;
7979
b4f54984 7980 if (dwarf_die_debug)
09406207
DE
7981 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7982 this_cu->is_debug_types ? "type" : "comp",
9c541725 7983 to_underlying (this_cu->sect_off));
09406207 7984
dee91e82
DE
7985 gdb_assert (this_cu->cu == NULL);
7986
33e80786
DE
7987 abbrev_section = (dwo_file != NULL
7988 ? &dwo_file->sections.abbrev
7989 : get_abbrev_section_for_cu (this_cu));
7990
dee91e82
DE
7991 /* This is cheap if the section is already read in. */
7992 dwarf2_read_section (objfile, section);
7993
7994 init_one_comp_unit (&cu, this_cu);
7995
7996 cleanups = make_cleanup (free_stack_comp_unit, &cu);
7997
9c541725 7998 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4bdcc0c1
DE
7999 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
8000 abbrev_section, info_ptr,
43988095
JK
8001 (this_cu->is_debug_types
8002 ? rcuh_kind::TYPE
8003 : rcuh_kind::COMPILE));
dee91e82 8004
1ce1cefd 8005 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
8006
8007 /* Skip dummy compilation units. */
8008 if (info_ptr >= begin_info_ptr + this_cu->length
8009 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 8010 {
dee91e82 8011 do_cleanups (cleanups);
21b2bd31 8012 return;
93311388 8013 }
72bf9492 8014
dee91e82
DE
8015 dwarf2_read_abbrevs (&cu, abbrev_section);
8016 make_cleanup (dwarf2_free_abbrev_table, &cu);
8017
3019eac3 8018 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
8019 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8020
8021 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8022
8023 do_cleanups (cleanups);
8024}
8025
3019eac3
DE
8026/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8027 does not lookup the specified DWO file.
8028 This cannot be used to read DWO files.
dee91e82
DE
8029
8030 THIS_CU->cu is always freed when done.
3019eac3
DE
8031 This is done in order to not leave THIS_CU->cu in a state where we have
8032 to care whether it refers to the "main" CU or the DWO CU.
8033 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
8034
8035static void
8036init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8037 die_reader_func_ftype *die_reader_func,
8038 void *data)
8039{
33e80786 8040 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 8041}
0018ea6f
DE
8042\f
8043/* Type Unit Groups.
dee91e82 8044
0018ea6f
DE
8045 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8046 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8047 so that all types coming from the same compilation (.o file) are grouped
8048 together. A future step could be to put the types in the same symtab as
8049 the CU the types ultimately came from. */
ff013f42 8050
f4dc4d17
DE
8051static hashval_t
8052hash_type_unit_group (const void *item)
8053{
9a3c8263
SM
8054 const struct type_unit_group *tu_group
8055 = (const struct type_unit_group *) item;
f4dc4d17 8056
094b34ac 8057 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 8058}
348e048f
DE
8059
8060static int
f4dc4d17 8061eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 8062{
9a3c8263
SM
8063 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8064 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 8065
094b34ac 8066 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 8067}
348e048f 8068
f4dc4d17
DE
8069/* Allocate a hash table for type unit groups. */
8070
8071static htab_t
8072allocate_type_unit_groups_table (void)
8073{
8074 return htab_create_alloc_ex (3,
8075 hash_type_unit_group,
8076 eq_type_unit_group,
8077 NULL,
8078 &dwarf2_per_objfile->objfile->objfile_obstack,
8079 hashtab_obstack_allocate,
8080 dummy_obstack_deallocate);
8081}
dee91e82 8082
f4dc4d17
DE
8083/* Type units that don't have DW_AT_stmt_list are grouped into their own
8084 partial symtabs. We combine several TUs per psymtab to not let the size
8085 of any one psymtab grow too big. */
8086#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8087#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 8088
094b34ac 8089/* Helper routine for get_type_unit_group.
f4dc4d17
DE
8090 Create the type_unit_group object used to hold one or more TUs. */
8091
8092static struct type_unit_group *
094b34ac 8093create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
8094{
8095 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 8096 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 8097 struct type_unit_group *tu_group;
f4dc4d17
DE
8098
8099 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8100 struct type_unit_group);
094b34ac 8101 per_cu = &tu_group->per_cu;
f4dc4d17 8102 per_cu->objfile = objfile;
f4dc4d17 8103
094b34ac
DE
8104 if (dwarf2_per_objfile->using_index)
8105 {
8106 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8107 struct dwarf2_per_cu_quick_data);
094b34ac
DE
8108 }
8109 else
8110 {
9c541725 8111 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
8112 struct partial_symtab *pst;
8113 char *name;
8114
8115 /* Give the symtab a useful name for debug purposes. */
8116 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8117 name = xstrprintf ("<type_units_%d>",
8118 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8119 else
8120 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8121
8122 pst = create_partial_symtab (per_cu, name);
8123 pst->anonymous = 1;
f4dc4d17 8124
094b34ac
DE
8125 xfree (name);
8126 }
f4dc4d17 8127
094b34ac 8128 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 8129 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
8130
8131 return tu_group;
8132}
8133
094b34ac
DE
8134/* Look up the type_unit_group for type unit CU, and create it if necessary.
8135 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
8136
8137static struct type_unit_group *
ff39bb5e 8138get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
8139{
8140 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8141 struct type_unit_group *tu_group;
8142 void **slot;
8143 unsigned int line_offset;
8144 struct type_unit_group type_unit_group_for_lookup;
8145
8146 if (dwarf2_per_objfile->type_unit_groups == NULL)
8147 {
8148 dwarf2_per_objfile->type_unit_groups =
8149 allocate_type_unit_groups_table ();
8150 }
8151
8152 /* Do we need to create a new group, or can we use an existing one? */
8153
8154 if (stmt_list)
8155 {
8156 line_offset = DW_UNSND (stmt_list);
8157 ++tu_stats->nr_symtab_sharers;
8158 }
8159 else
8160 {
8161 /* Ugh, no stmt_list. Rare, but we have to handle it.
8162 We can do various things here like create one group per TU or
8163 spread them over multiple groups to split up the expansion work.
8164 To avoid worst case scenarios (too many groups or too large groups)
8165 we, umm, group them in bunches. */
8166 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8167 | (tu_stats->nr_stmt_less_type_units
8168 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8169 ++tu_stats->nr_stmt_less_type_units;
8170 }
8171
094b34ac 8172 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 8173 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
8174 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8175 &type_unit_group_for_lookup, INSERT);
8176 if (*slot != NULL)
8177 {
9a3c8263 8178 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
8179 gdb_assert (tu_group != NULL);
8180 }
8181 else
8182 {
9c541725 8183 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 8184 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
8185 *slot = tu_group;
8186 ++tu_stats->nr_symtabs;
8187 }
8188
8189 return tu_group;
8190}
0018ea6f
DE
8191\f
8192/* Partial symbol tables. */
8193
8194/* Create a psymtab named NAME and assign it to PER_CU.
8195
8196 The caller must fill in the following details:
8197 dirname, textlow, texthigh. */
8198
8199static struct partial_symtab *
8200create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8201{
8202 struct objfile *objfile = per_cu->objfile;
8203 struct partial_symtab *pst;
8204
18a94d75 8205 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
8206 objfile->global_psymbols,
8207 objfile->static_psymbols);
0018ea6f
DE
8208
8209 pst->psymtabs_addrmap_supported = 1;
8210
8211 /* This is the glue that links PST into GDB's symbol API. */
8212 pst->read_symtab_private = per_cu;
8213 pst->read_symtab = dwarf2_read_symtab;
8214 per_cu->v.psymtab = pst;
8215
8216 return pst;
8217}
8218
b93601f3
TT
8219/* The DATA object passed to process_psymtab_comp_unit_reader has this
8220 type. */
8221
8222struct process_psymtab_comp_unit_data
8223{
8224 /* True if we are reading a DW_TAG_partial_unit. */
8225
8226 int want_partial_unit;
8227
8228 /* The "pretend" language that is used if the CU doesn't declare a
8229 language. */
8230
8231 enum language pretend_language;
8232};
8233
0018ea6f
DE
8234/* die_reader_func for process_psymtab_comp_unit. */
8235
8236static void
8237process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8238 const gdb_byte *info_ptr,
0018ea6f
DE
8239 struct die_info *comp_unit_die,
8240 int has_children,
8241 void *data)
8242{
8243 struct dwarf2_cu *cu = reader->cu;
8244 struct objfile *objfile = cu->objfile;
3e29f34a 8245 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8246 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8247 CORE_ADDR baseaddr;
8248 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8249 struct partial_symtab *pst;
3a2b436a 8250 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8251 const char *filename;
9a3c8263
SM
8252 struct process_psymtab_comp_unit_data *info
8253 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 8254
b93601f3 8255 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
8256 return;
8257
8258 gdb_assert (! per_cu->is_debug_types);
8259
b93601f3 8260 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
8261
8262 cu->list_in_scope = &file_symbols;
8263
8264 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8265 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8266 if (filename == NULL)
0018ea6f 8267 filename = "";
0018ea6f
DE
8268
8269 pst = create_partial_symtab (per_cu, filename);
8270
8271 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8272 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
8273
8274 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8275
8276 dwarf2_find_base_address (comp_unit_die, cu);
8277
8278 /* Possibly set the default values of LOWPC and HIGHPC from
8279 `DW_AT_ranges'. */
3a2b436a
JK
8280 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8281 &best_highpc, cu, pst);
8282 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
8283 /* Store the contiguous range if it is not empty; it can be empty for
8284 CUs with no code. */
8285 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
8286 gdbarch_adjust_dwarf2_addr (gdbarch,
8287 best_lowpc + baseaddr),
8288 gdbarch_adjust_dwarf2_addr (gdbarch,
8289 best_highpc + baseaddr) - 1,
8290 pst);
0018ea6f
DE
8291
8292 /* Check if comp unit has_children.
8293 If so, read the rest of the partial symbols from this comp unit.
8294 If not, there's no more debug_info for this comp unit. */
8295 if (has_children)
8296 {
8297 struct partial_die_info *first_die;
8298 CORE_ADDR lowpc, highpc;
8299
8300 lowpc = ((CORE_ADDR) -1);
8301 highpc = ((CORE_ADDR) 0);
8302
8303 first_die = load_partial_dies (reader, info_ptr, 1);
8304
8305 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8306 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8307
8308 /* If we didn't find a lowpc, set it to highpc to avoid
8309 complaints from `maint check'. */
8310 if (lowpc == ((CORE_ADDR) -1))
8311 lowpc = highpc;
8312
8313 /* If the compilation unit didn't have an explicit address range,
8314 then use the information extracted from its child dies. */
e385593e 8315 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8316 {
8317 best_lowpc = lowpc;
8318 best_highpc = highpc;
8319 }
8320 }
3e29f34a
MR
8321 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8322 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 8323
8763cede 8324 end_psymtab_common (objfile, pst);
0018ea6f
DE
8325
8326 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8327 {
8328 int i;
8329 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8330 struct dwarf2_per_cu_data *iter;
8331
8332 /* Fill in 'dependencies' here; we fill in 'users' in a
8333 post-pass. */
8334 pst->number_of_dependencies = len;
8d749320
SM
8335 pst->dependencies =
8336 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
8337 for (i = 0;
8338 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8339 i, iter);
8340 ++i)
8341 pst->dependencies[i] = iter->v.psymtab;
8342
8343 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8344 }
8345
8346 /* Get the list of files included in the current compilation unit,
8347 and build a psymtab for each of them. */
8348 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8349
b4f54984 8350 if (dwarf_read_debug)
0018ea6f
DE
8351 {
8352 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8353
8354 fprintf_unfiltered (gdb_stdlog,
8355 "Psymtab for %s unit @0x%x: %s - %s"
8356 ", %d global, %d static syms\n",
8357 per_cu->is_debug_types ? "type" : "comp",
9c541725 8358 to_underlying (per_cu->sect_off),
0018ea6f
DE
8359 paddress (gdbarch, pst->textlow),
8360 paddress (gdbarch, pst->texthigh),
8361 pst->n_global_syms, pst->n_static_syms);
8362 }
8363}
8364
8365/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8366 Process compilation unit THIS_CU for a psymtab. */
8367
8368static void
8369process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8370 int want_partial_unit,
8371 enum language pretend_language)
0018ea6f
DE
8372{
8373 /* If this compilation unit was already read in, free the
8374 cached copy in order to read it in again. This is
8375 necessary because we skipped some symbols when we first
8376 read in the compilation unit (see load_partial_dies).
8377 This problem could be avoided, but the benefit is unclear. */
8378 if (this_cu->cu != NULL)
8379 free_one_cached_comp_unit (this_cu);
8380
f1902523
JK
8381 if (this_cu->is_debug_types)
8382 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8383 NULL);
8384 else
8385 {
8386 process_psymtab_comp_unit_data info;
8387 info.want_partial_unit = want_partial_unit;
8388 info.pretend_language = pretend_language;
8389 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8390 process_psymtab_comp_unit_reader, &info);
8391 }
0018ea6f
DE
8392
8393 /* Age out any secondary CUs. */
8394 age_cached_comp_units ();
8395}
f4dc4d17
DE
8396
8397/* Reader function for build_type_psymtabs. */
8398
8399static void
8400build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8401 const gdb_byte *info_ptr,
f4dc4d17
DE
8402 struct die_info *type_unit_die,
8403 int has_children,
8404 void *data)
8405{
8406 struct objfile *objfile = dwarf2_per_objfile->objfile;
8407 struct dwarf2_cu *cu = reader->cu;
8408 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8409 struct signatured_type *sig_type;
f4dc4d17
DE
8410 struct type_unit_group *tu_group;
8411 struct attribute *attr;
8412 struct partial_die_info *first_die;
8413 CORE_ADDR lowpc, highpc;
8414 struct partial_symtab *pst;
8415
8416 gdb_assert (data == NULL);
0186c6a7
DE
8417 gdb_assert (per_cu->is_debug_types);
8418 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8419
8420 if (! has_children)
8421 return;
8422
8423 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8424 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8425
0186c6a7 8426 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
8427
8428 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8429 cu->list_in_scope = &file_symbols;
8430 pst = create_partial_symtab (per_cu, "");
8431 pst->anonymous = 1;
8432
8433 first_die = load_partial_dies (reader, info_ptr, 1);
8434
8435 lowpc = (CORE_ADDR) -1;
8436 highpc = (CORE_ADDR) 0;
8437 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8438
8763cede 8439 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8440}
8441
73051182
DE
8442/* Struct used to sort TUs by their abbreviation table offset. */
8443
8444struct tu_abbrev_offset
8445{
8446 struct signatured_type *sig_type;
8447 sect_offset abbrev_offset;
8448};
8449
8450/* Helper routine for build_type_psymtabs_1, passed to qsort. */
8451
8452static int
8453sort_tu_by_abbrev_offset (const void *ap, const void *bp)
8454{
9a3c8263
SM
8455 const struct tu_abbrev_offset * const *a
8456 = (const struct tu_abbrev_offset * const*) ap;
8457 const struct tu_abbrev_offset * const *b
8458 = (const struct tu_abbrev_offset * const*) bp;
9c541725
PA
8459 sect_offset aoff = (*a)->abbrev_offset;
8460 sect_offset boff = (*b)->abbrev_offset;
73051182
DE
8461
8462 return (aoff > boff) - (aoff < boff);
8463}
8464
8465/* Efficiently read all the type units.
8466 This does the bulk of the work for build_type_psymtabs.
8467
8468 The efficiency is because we sort TUs by the abbrev table they use and
8469 only read each abbrev table once. In one program there are 200K TUs
8470 sharing 8K abbrev tables.
8471
8472 The main purpose of this function is to support building the
8473 dwarf2_per_objfile->type_unit_groups table.
8474 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8475 can collapse the search space by grouping them by stmt_list.
8476 The savings can be significant, in the same program from above the 200K TUs
8477 share 8K stmt_list tables.
8478
8479 FUNC is expected to call get_type_unit_group, which will create the
8480 struct type_unit_group if necessary and add it to
8481 dwarf2_per_objfile->type_unit_groups. */
8482
8483static void
8484build_type_psymtabs_1 (void)
8485{
73051182
DE
8486 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8487 struct cleanup *cleanups;
8488 struct abbrev_table *abbrev_table;
8489 sect_offset abbrev_offset;
8490 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
8491 int i;
8492
8493 /* It's up to the caller to not call us multiple times. */
8494 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8495
8496 if (dwarf2_per_objfile->n_type_units == 0)
8497 return;
8498
8499 /* TUs typically share abbrev tables, and there can be way more TUs than
8500 abbrev tables. Sort by abbrev table to reduce the number of times we
8501 read each abbrev table in.
8502 Alternatives are to punt or to maintain a cache of abbrev tables.
8503 This is simpler and efficient enough for now.
8504
8505 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8506 symtab to use). Typically TUs with the same abbrev offset have the same
8507 stmt_list value too so in practice this should work well.
8508
8509 The basic algorithm here is:
8510
8511 sort TUs by abbrev table
8512 for each TU with same abbrev table:
8513 read abbrev table if first user
8514 read TU top level DIE
8515 [IWBN if DWO skeletons had DW_AT_stmt_list]
8516 call FUNC */
8517
b4f54984 8518 if (dwarf_read_debug)
73051182
DE
8519 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8520
8521 /* Sort in a separate table to maintain the order of all_type_units
8522 for .gdb_index: TU indices directly index all_type_units. */
8523 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
8524 dwarf2_per_objfile->n_type_units);
8525 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8526 {
8527 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8528
8529 sorted_by_abbrev[i].sig_type = sig_type;
8530 sorted_by_abbrev[i].abbrev_offset =
8531 read_abbrev_offset (sig_type->per_cu.section,
9c541725 8532 sig_type->per_cu.sect_off);
73051182
DE
8533 }
8534 cleanups = make_cleanup (xfree, sorted_by_abbrev);
8535 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
8536 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
8537
9c541725 8538 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
8539 abbrev_table = NULL;
8540 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
8541
8542 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8543 {
8544 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8545
8546 /* Switch to the next abbrev table if necessary. */
8547 if (abbrev_table == NULL
9c541725 8548 || tu->abbrev_offset != abbrev_offset)
73051182
DE
8549 {
8550 if (abbrev_table != NULL)
8551 {
8552 abbrev_table_free (abbrev_table);
8553 /* Reset to NULL in case abbrev_table_read_table throws
8554 an error: abbrev_table_free_cleanup will get called. */
8555 abbrev_table = NULL;
8556 }
8557 abbrev_offset = tu->abbrev_offset;
8558 abbrev_table =
8559 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
8560 abbrev_offset);
8561 ++tu_stats->nr_uniq_abbrev_tables;
8562 }
8563
8564 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
8565 build_type_psymtabs_reader, NULL);
8566 }
8567
73051182 8568 do_cleanups (cleanups);
6aa5f3a6 8569}
73051182 8570
6aa5f3a6
DE
8571/* Print collected type unit statistics. */
8572
8573static void
8574print_tu_stats (void)
8575{
8576 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8577
8578 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8579 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8580 dwarf2_per_objfile->n_type_units);
8581 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8582 tu_stats->nr_uniq_abbrev_tables);
8583 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8584 tu_stats->nr_symtabs);
8585 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8586 tu_stats->nr_symtab_sharers);
8587 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8588 tu_stats->nr_stmt_less_type_units);
8589 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8590 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8591}
8592
f4dc4d17
DE
8593/* Traversal function for build_type_psymtabs. */
8594
8595static int
8596build_type_psymtab_dependencies (void **slot, void *info)
8597{
8598 struct objfile *objfile = dwarf2_per_objfile->objfile;
8599 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8600 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8601 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
8602 int len = VEC_length (sig_type_ptr, tu_group->tus);
8603 struct signatured_type *iter;
f4dc4d17
DE
8604 int i;
8605
8606 gdb_assert (len > 0);
0186c6a7 8607 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8608
8609 pst->number_of_dependencies = len;
8d749320
SM
8610 pst->dependencies =
8611 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 8612 for (i = 0;
0186c6a7 8613 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
8614 ++i)
8615 {
0186c6a7
DE
8616 gdb_assert (iter->per_cu.is_debug_types);
8617 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8618 iter->type_unit_group = tu_group;
f4dc4d17
DE
8619 }
8620
0186c6a7 8621 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
8622
8623 return 1;
8624}
8625
8626/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8627 Build partial symbol tables for the .debug_types comp-units. */
8628
8629static void
8630build_type_psymtabs (struct objfile *objfile)
8631{
0e50663e 8632 if (! create_all_type_units (objfile))
348e048f
DE
8633 return;
8634
73051182 8635 build_type_psymtabs_1 ();
6aa5f3a6 8636}
f4dc4d17 8637
6aa5f3a6
DE
8638/* Traversal function for process_skeletonless_type_unit.
8639 Read a TU in a DWO file and build partial symbols for it. */
8640
8641static int
8642process_skeletonless_type_unit (void **slot, void *info)
8643{
8644 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9a3c8263 8645 struct objfile *objfile = (struct objfile *) info;
6aa5f3a6
DE
8646 struct signatured_type find_entry, *entry;
8647
8648 /* If this TU doesn't exist in the global table, add it and read it in. */
8649
8650 if (dwarf2_per_objfile->signatured_types == NULL)
8651 {
8652 dwarf2_per_objfile->signatured_types
8653 = allocate_signatured_type_table (objfile);
8654 }
8655
8656 find_entry.signature = dwo_unit->signature;
8657 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8658 INSERT);
8659 /* If we've already seen this type there's nothing to do. What's happening
8660 is we're doing our own version of comdat-folding here. */
8661 if (*slot != NULL)
8662 return 1;
8663
8664 /* This does the job that create_all_type_units would have done for
8665 this TU. */
8666 entry = add_type_unit (dwo_unit->signature, slot);
8667 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
8668 *slot = entry;
8669
8670 /* This does the job that build_type_psymtabs_1 would have done. */
8671 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8672 build_type_psymtabs_reader, NULL);
8673
8674 return 1;
8675}
8676
8677/* Traversal function for process_skeletonless_type_units. */
8678
8679static int
8680process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8681{
8682 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8683
8684 if (dwo_file->tus != NULL)
8685 {
8686 htab_traverse_noresize (dwo_file->tus,
8687 process_skeletonless_type_unit, info);
8688 }
8689
8690 return 1;
8691}
8692
8693/* Scan all TUs of DWO files, verifying we've processed them.
8694 This is needed in case a TU was emitted without its skeleton.
8695 Note: This can't be done until we know what all the DWO files are. */
8696
8697static void
8698process_skeletonless_type_units (struct objfile *objfile)
8699{
8700 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8701 if (get_dwp_file () == NULL
8702 && dwarf2_per_objfile->dwo_files != NULL)
8703 {
8704 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8705 process_dwo_file_for_skeletonless_type_units,
8706 objfile);
8707 }
348e048f
DE
8708}
8709
95554aad
TT
8710/* Compute the 'user' field for each psymtab in OBJFILE. */
8711
8712static void
8713set_partial_user (struct objfile *objfile)
8714{
8715 int i;
8716
8717 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8718 {
8832e7e3 8719 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
95554aad
TT
8720 struct partial_symtab *pst = per_cu->v.psymtab;
8721 int j;
8722
36586728
TT
8723 if (pst == NULL)
8724 continue;
8725
95554aad
TT
8726 for (j = 0; j < pst->number_of_dependencies; ++j)
8727 {
8728 /* Set the 'user' field only if it is not already set. */
8729 if (pst->dependencies[j]->user == NULL)
8730 pst->dependencies[j]->user = pst;
8731 }
8732 }
8733}
8734
93311388
DE
8735/* Build the partial symbol table by doing a quick pass through the
8736 .debug_info and .debug_abbrev sections. */
72bf9492 8737
93311388 8738static void
c67a9c90 8739dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 8740{
791afaa2 8741 struct cleanup *back_to;
21b2bd31 8742 int i;
93311388 8743
b4f54984 8744 if (dwarf_read_debug)
45cfd468
DE
8745 {
8746 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8747 objfile_name (objfile));
45cfd468
DE
8748 }
8749
98bfdba5
PA
8750 dwarf2_per_objfile->reading_partial_symbols = 1;
8751
be391dca 8752 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8753
93311388
DE
8754 /* Any cached compilation units will be linked by the per-objfile
8755 read_in_chain. Make sure to free them when we're done. */
8756 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 8757
348e048f
DE
8758 build_type_psymtabs (objfile);
8759
93311388 8760 create_all_comp_units (objfile);
c906108c 8761
60606b2c
TT
8762 /* Create a temporary address map on a temporary obstack. We later
8763 copy this to the final obstack. */
8268c778 8764 auto_obstack temp_obstack;
791afaa2
TT
8765
8766 scoped_restore save_psymtabs_addrmap
8767 = make_scoped_restore (&objfile->psymtabs_addrmap,
8768 addrmap_create_mutable (&temp_obstack));
72bf9492 8769
21b2bd31 8770 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 8771 {
8832e7e3 8772 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
aaa75496 8773
b93601f3 8774 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 8775 }
ff013f42 8776
6aa5f3a6
DE
8777 /* This has to wait until we read the CUs, we need the list of DWOs. */
8778 process_skeletonless_type_units (objfile);
8779
8780 /* Now that all TUs have been processed we can fill in the dependencies. */
8781 if (dwarf2_per_objfile->type_unit_groups != NULL)
8782 {
8783 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8784 build_type_psymtab_dependencies, NULL);
8785 }
8786
b4f54984 8787 if (dwarf_read_debug)
6aa5f3a6
DE
8788 print_tu_stats ();
8789
95554aad
TT
8790 set_partial_user (objfile);
8791
ff013f42
JK
8792 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8793 &objfile->objfile_obstack);
791afaa2
TT
8794 /* At this point we want to keep the address map. */
8795 save_psymtabs_addrmap.release ();
ff013f42 8796
ae038cb0 8797 do_cleanups (back_to);
45cfd468 8798
b4f54984 8799 if (dwarf_read_debug)
45cfd468 8800 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8801 objfile_name (objfile));
ae038cb0
DJ
8802}
8803
3019eac3 8804/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8805
8806static void
dee91e82 8807load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8808 const gdb_byte *info_ptr,
dee91e82
DE
8809 struct die_info *comp_unit_die,
8810 int has_children,
8811 void *data)
ae038cb0 8812{
dee91e82 8813 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8814
95554aad 8815 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8816
ae038cb0
DJ
8817 /* Check if comp unit has_children.
8818 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8819 If not, there's no more debug_info for this comp unit. */
d85a05f0 8820 if (has_children)
dee91e82
DE
8821 load_partial_dies (reader, info_ptr, 0);
8822}
98bfdba5 8823
dee91e82
DE
8824/* Load the partial DIEs for a secondary CU into memory.
8825 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8826
dee91e82
DE
8827static void
8828load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8829{
f4dc4d17
DE
8830 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8831 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8832}
8833
ae038cb0 8834static void
36586728
TT
8835read_comp_units_from_section (struct objfile *objfile,
8836 struct dwarf2_section_info *section,
f1902523 8837 struct dwarf2_section_info *abbrev_section,
36586728
TT
8838 unsigned int is_dwz,
8839 int *n_allocated,
8840 int *n_comp_units,
8841 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 8842{
d521ce57 8843 const gdb_byte *info_ptr;
be391dca 8844
b4f54984 8845 if (dwarf_read_debug)
bf6af496 8846 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8847 get_section_name (section),
8848 get_section_file_name (section));
bf6af496 8849
36586728 8850 dwarf2_read_section (objfile, section);
ae038cb0 8851
36586728 8852 info_ptr = section->buffer;
6e70227d 8853
36586728 8854 while (info_ptr < section->buffer + section->size)
ae038cb0 8855 {
ae038cb0 8856 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8857
9c541725 8858 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8859
f1902523
JK
8860 comp_unit_head cu_header;
8861 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
8862 info_ptr, rcuh_kind::COMPILE);
ae038cb0
DJ
8863
8864 /* Save the compilation unit for later lookup. */
f1902523
JK
8865 if (cu_header.unit_type != DW_UT_type)
8866 {
8867 this_cu = XOBNEW (&objfile->objfile_obstack,
8868 struct dwarf2_per_cu_data);
8869 memset (this_cu, 0, sizeof (*this_cu));
8870 }
8871 else
8872 {
8873 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8874 struct signatured_type);
8875 memset (sig_type, 0, sizeof (*sig_type));
8876 sig_type->signature = cu_header.signature;
8877 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8878 this_cu = &sig_type->per_cu;
8879 }
8880 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8881 this_cu->sect_off = sect_off;
f1902523 8882 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8883 this_cu->is_dwz = is_dwz;
9291a0cd 8884 this_cu->objfile = objfile;
8a0459fd 8885 this_cu->section = section;
ae038cb0 8886
36586728 8887 if (*n_comp_units == *n_allocated)
ae038cb0 8888 {
36586728 8889 *n_allocated *= 2;
224c3ddb
SM
8890 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
8891 *all_comp_units, *n_allocated);
ae038cb0 8892 }
36586728
TT
8893 (*all_comp_units)[*n_comp_units] = this_cu;
8894 ++*n_comp_units;
ae038cb0
DJ
8895
8896 info_ptr = info_ptr + this_cu->length;
8897 }
36586728
TT
8898}
8899
8900/* Create a list of all compilation units in OBJFILE.
8901 This is only done for -readnow and building partial symtabs. */
8902
8903static void
8904create_all_comp_units (struct objfile *objfile)
8905{
8906 int n_allocated;
8907 int n_comp_units;
8908 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 8909 struct dwz_file *dwz;
36586728
TT
8910
8911 n_comp_units = 0;
8912 n_allocated = 10;
8d749320 8913 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728 8914
f1902523
JK
8915 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
8916 &dwarf2_per_objfile->abbrev, 0,
36586728
TT
8917 &n_allocated, &n_comp_units, &all_comp_units);
8918
4db1a1dc
TT
8919 dwz = dwarf2_get_dwz_file ();
8920 if (dwz != NULL)
f1902523 8921 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
4db1a1dc
TT
8922 &n_allocated, &n_comp_units,
8923 &all_comp_units);
ae038cb0 8924
8d749320
SM
8925 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
8926 struct dwarf2_per_cu_data *,
8927 n_comp_units);
ae038cb0
DJ
8928 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
8929 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
8930 xfree (all_comp_units);
8931 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
8932}
8933
5734ee8b 8934/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8935 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8936 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8937 DW_AT_ranges). See the comments of add_partial_subprogram on how
8938 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8939
72bf9492
DJ
8940static void
8941scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8942 CORE_ADDR *highpc, int set_addrmap,
8943 struct dwarf2_cu *cu)
c906108c 8944{
72bf9492 8945 struct partial_die_info *pdi;
c906108c 8946
91c24f0a
DC
8947 /* Now, march along the PDI's, descending into ones which have
8948 interesting children but skipping the children of the other ones,
8949 until we reach the end of the compilation unit. */
c906108c 8950
72bf9492 8951 pdi = first_die;
91c24f0a 8952
72bf9492
DJ
8953 while (pdi != NULL)
8954 {
8955 fixup_partial_die (pdi, cu);
c906108c 8956
f55ee35c 8957 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8958 children, so we need to look at them. Ditto for anonymous
8959 enums. */
933c6fe4 8960
72bf9492 8961 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
8962 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8963 || pdi->tag == DW_TAG_imported_unit)
c906108c 8964 {
72bf9492 8965 switch (pdi->tag)
c906108c
SS
8966 {
8967 case DW_TAG_subprogram:
cdc07690 8968 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8969 break;
72929c62 8970 case DW_TAG_constant:
c906108c
SS
8971 case DW_TAG_variable:
8972 case DW_TAG_typedef:
91c24f0a 8973 case DW_TAG_union_type:
72bf9492 8974 if (!pdi->is_declaration)
63d06c5c 8975 {
72bf9492 8976 add_partial_symbol (pdi, cu);
63d06c5c
DC
8977 }
8978 break;
c906108c 8979 case DW_TAG_class_type:
680b30c7 8980 case DW_TAG_interface_type:
c906108c 8981 case DW_TAG_structure_type:
72bf9492 8982 if (!pdi->is_declaration)
c906108c 8983 {
72bf9492 8984 add_partial_symbol (pdi, cu);
c906108c 8985 }
e98c9e7c
TT
8986 if (cu->language == language_rust && pdi->has_children)
8987 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8988 set_addrmap, cu);
c906108c 8989 break;
91c24f0a 8990 case DW_TAG_enumeration_type:
72bf9492
DJ
8991 if (!pdi->is_declaration)
8992 add_partial_enumeration (pdi, cu);
c906108c
SS
8993 break;
8994 case DW_TAG_base_type:
a02abb62 8995 case DW_TAG_subrange_type:
c906108c 8996 /* File scope base type definitions are added to the partial
c5aa993b 8997 symbol table. */
72bf9492 8998 add_partial_symbol (pdi, cu);
c906108c 8999 break;
d9fa45fe 9000 case DW_TAG_namespace:
cdc07690 9001 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 9002 break;
5d7cb8df 9003 case DW_TAG_module:
cdc07690 9004 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 9005 break;
95554aad
TT
9006 case DW_TAG_imported_unit:
9007 {
9008 struct dwarf2_per_cu_data *per_cu;
9009
f4dc4d17
DE
9010 /* For now we don't handle imported units in type units. */
9011 if (cu->per_cu->is_debug_types)
9012 {
9013 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9014 " supported in type units [in module %s]"),
4262abfb 9015 objfile_name (cu->objfile));
f4dc4d17
DE
9016 }
9017
9c541725 9018 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
36586728 9019 pdi->is_dwz,
95554aad
TT
9020 cu->objfile);
9021
9022 /* Go read the partial unit, if needed. */
9023 if (per_cu->v.psymtab == NULL)
b93601f3 9024 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 9025
f4dc4d17 9026 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 9027 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
9028 }
9029 break;
74921315
KS
9030 case DW_TAG_imported_declaration:
9031 add_partial_symbol (pdi, cu);
9032 break;
c906108c
SS
9033 default:
9034 break;
9035 }
9036 }
9037
72bf9492
DJ
9038 /* If the die has a sibling, skip to the sibling. */
9039
9040 pdi = pdi->die_sibling;
9041 }
9042}
9043
9044/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 9045
72bf9492 9046 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 9047 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
9048 Enumerators are an exception; they use the scope of their parent
9049 enumeration type, i.e. the name of the enumeration type is not
9050 prepended to the enumerator.
91c24f0a 9051
72bf9492
DJ
9052 There are two complexities. One is DW_AT_specification; in this
9053 case "parent" means the parent of the target of the specification,
9054 instead of the direct parent of the DIE. The other is compilers
9055 which do not emit DW_TAG_namespace; in this case we try to guess
9056 the fully qualified name of structure types from their members'
9057 linkage names. This must be done using the DIE's children rather
9058 than the children of any DW_AT_specification target. We only need
9059 to do this for structures at the top level, i.e. if the target of
9060 any DW_AT_specification (if any; otherwise the DIE itself) does not
9061 have a parent. */
9062
9063/* Compute the scope prefix associated with PDI's parent, in
9064 compilation unit CU. The result will be allocated on CU's
9065 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9066 field. NULL is returned if no prefix is necessary. */
15d034d0 9067static const char *
72bf9492
DJ
9068partial_die_parent_scope (struct partial_die_info *pdi,
9069 struct dwarf2_cu *cu)
9070{
15d034d0 9071 const char *grandparent_scope;
72bf9492 9072 struct partial_die_info *parent, *real_pdi;
91c24f0a 9073
72bf9492
DJ
9074 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9075 then this means the parent of the specification DIE. */
9076
9077 real_pdi = pdi;
72bf9492 9078 while (real_pdi->has_specification)
36586728
TT
9079 real_pdi = find_partial_die (real_pdi->spec_offset,
9080 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
9081
9082 parent = real_pdi->die_parent;
9083 if (parent == NULL)
9084 return NULL;
9085
9086 if (parent->scope_set)
9087 return parent->scope;
9088
9089 fixup_partial_die (parent, cu);
9090
10b3939b 9091 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 9092
acebe513
UW
9093 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9094 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9095 Work around this problem here. */
9096 if (cu->language == language_cplus
6e70227d 9097 && parent->tag == DW_TAG_namespace
acebe513
UW
9098 && strcmp (parent->name, "::") == 0
9099 && grandparent_scope == NULL)
9100 {
9101 parent->scope = NULL;
9102 parent->scope_set = 1;
9103 return NULL;
9104 }
9105
9c6c53f7
SA
9106 if (pdi->tag == DW_TAG_enumerator)
9107 /* Enumerators should not get the name of the enumeration as a prefix. */
9108 parent->scope = grandparent_scope;
9109 else if (parent->tag == DW_TAG_namespace
f55ee35c 9110 || parent->tag == DW_TAG_module
72bf9492
DJ
9111 || parent->tag == DW_TAG_structure_type
9112 || parent->tag == DW_TAG_class_type
680b30c7 9113 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
9114 || parent->tag == DW_TAG_union_type
9115 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
9116 {
9117 if (grandparent_scope == NULL)
9118 parent->scope = parent->name;
9119 else
3e43a32a
MS
9120 parent->scope = typename_concat (&cu->comp_unit_obstack,
9121 grandparent_scope,
f55ee35c 9122 parent->name, 0, cu);
72bf9492 9123 }
72bf9492
DJ
9124 else
9125 {
9126 /* FIXME drow/2004-04-01: What should we be doing with
9127 function-local names? For partial symbols, we should probably be
9128 ignoring them. */
9129 complaint (&symfile_complaints,
e2e0b3e5 9130 _("unhandled containing DIE tag %d for DIE at %d"),
9c541725 9131 parent->tag, to_underlying (pdi->sect_off));
72bf9492 9132 parent->scope = grandparent_scope;
c906108c
SS
9133 }
9134
72bf9492
DJ
9135 parent->scope_set = 1;
9136 return parent->scope;
9137}
9138
9139/* Return the fully scoped name associated with PDI, from compilation unit
9140 CU. The result will be allocated with malloc. */
4568ecf9 9141
72bf9492
DJ
9142static char *
9143partial_die_full_name (struct partial_die_info *pdi,
9144 struct dwarf2_cu *cu)
9145{
15d034d0 9146 const char *parent_scope;
72bf9492 9147
98bfdba5
PA
9148 /* If this is a template instantiation, we can not work out the
9149 template arguments from partial DIEs. So, unfortunately, we have
9150 to go through the full DIEs. At least any work we do building
9151 types here will be reused if full symbols are loaded later. */
9152 if (pdi->has_template_arguments)
9153 {
9154 fixup_partial_die (pdi, cu);
9155
9156 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9157 {
9158 struct die_info *die;
9159 struct attribute attr;
9160 struct dwarf2_cu *ref_cu = cu;
9161
b64f50a1 9162 /* DW_FORM_ref_addr is using section offset. */
b4069958 9163 attr.name = (enum dwarf_attribute) 0;
98bfdba5 9164 attr.form = DW_FORM_ref_addr;
9c541725 9165 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
9166 die = follow_die_ref (NULL, &attr, &ref_cu);
9167
9168 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9169 }
9170 }
9171
72bf9492
DJ
9172 parent_scope = partial_die_parent_scope (pdi, cu);
9173 if (parent_scope == NULL)
9174 return NULL;
9175 else
f55ee35c 9176 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
9177}
9178
9179static void
72bf9492 9180add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 9181{
e7c27a73 9182 struct objfile *objfile = cu->objfile;
3e29f34a 9183 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 9184 CORE_ADDR addr = 0;
15d034d0 9185 const char *actual_name = NULL;
e142c38c 9186 CORE_ADDR baseaddr;
15d034d0 9187 char *built_actual_name;
e142c38c
DJ
9188
9189 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9190
15d034d0
TT
9191 built_actual_name = partial_die_full_name (pdi, cu);
9192 if (built_actual_name != NULL)
9193 actual_name = built_actual_name;
63d06c5c 9194
72bf9492
DJ
9195 if (actual_name == NULL)
9196 actual_name = pdi->name;
9197
c906108c
SS
9198 switch (pdi->tag)
9199 {
9200 case DW_TAG_subprogram:
3e29f34a 9201 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 9202 if (pdi->is_external || cu->language == language_ada)
c906108c 9203 {
2cfa0c8d
JB
9204 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9205 of the global scope. But in Ada, we want to be able to access
9206 nested procedures globally. So all Ada subprograms are stored
9207 in the global scope. */
f47fb265 9208 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9209 built_actual_name != NULL,
f47fb265
MS
9210 VAR_DOMAIN, LOC_BLOCK,
9211 &objfile->global_psymbols,
1762568f 9212 addr, cu->language, objfile);
c906108c
SS
9213 }
9214 else
9215 {
f47fb265 9216 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9217 built_actual_name != NULL,
f47fb265
MS
9218 VAR_DOMAIN, LOC_BLOCK,
9219 &objfile->static_psymbols,
1762568f 9220 addr, cu->language, objfile);
c906108c 9221 }
0c1b455e
TT
9222
9223 if (pdi->main_subprogram && actual_name != NULL)
9224 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 9225 break;
72929c62
JB
9226 case DW_TAG_constant:
9227 {
af5bf4ad 9228 std::vector<partial_symbol *> *list;
72929c62
JB
9229
9230 if (pdi->is_external)
9231 list = &objfile->global_psymbols;
9232 else
9233 list = &objfile->static_psymbols;
f47fb265 9234 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9235 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 9236 list, 0, cu->language, objfile);
72929c62
JB
9237 }
9238 break;
c906108c 9239 case DW_TAG_variable:
95554aad
TT
9240 if (pdi->d.locdesc)
9241 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 9242
95554aad 9243 if (pdi->d.locdesc
caac4577
JG
9244 && addr == 0
9245 && !dwarf2_per_objfile->has_section_at_zero)
9246 {
9247 /* A global or static variable may also have been stripped
9248 out by the linker if unused, in which case its address
9249 will be nullified; do not add such variables into partial
9250 symbol table then. */
9251 }
9252 else if (pdi->is_external)
c906108c
SS
9253 {
9254 /* Global Variable.
9255 Don't enter into the minimal symbol tables as there is
9256 a minimal symbol table entry from the ELF symbols already.
9257 Enter into partial symbol table if it has a location
9258 descriptor or a type.
9259 If the location descriptor is missing, new_symbol will create
9260 a LOC_UNRESOLVED symbol, the address of the variable will then
9261 be determined from the minimal symbol table whenever the variable
9262 is referenced.
9263 The address for the partial symbol table entry is not
9264 used by GDB, but it comes in handy for debugging partial symbol
9265 table building. */
9266
95554aad 9267 if (pdi->d.locdesc || pdi->has_type)
f47fb265 9268 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9269 built_actual_name != NULL,
f47fb265
MS
9270 VAR_DOMAIN, LOC_STATIC,
9271 &objfile->global_psymbols,
1762568f 9272 addr + baseaddr,
f47fb265 9273 cu->language, objfile);
c906108c
SS
9274 }
9275 else
9276 {
ff908ebf
AW
9277 int has_loc = pdi->d.locdesc != NULL;
9278
9279 /* Static Variable. Skip symbols whose value we cannot know (those
9280 without location descriptors or constant values). */
9281 if (!has_loc && !pdi->has_const_value)
decbce07 9282 {
15d034d0 9283 xfree (built_actual_name);
decbce07
MS
9284 return;
9285 }
ff908ebf 9286
f47fb265 9287 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9288 built_actual_name != NULL,
f47fb265
MS
9289 VAR_DOMAIN, LOC_STATIC,
9290 &objfile->static_psymbols,
ff908ebf 9291 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 9292 cu->language, objfile);
c906108c
SS
9293 }
9294 break;
9295 case DW_TAG_typedef:
9296 case DW_TAG_base_type:
a02abb62 9297 case DW_TAG_subrange_type:
38d518c9 9298 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9299 built_actual_name != NULL,
176620f1 9300 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 9301 &objfile->static_psymbols,
1762568f 9302 0, cu->language, objfile);
c906108c 9303 break;
74921315 9304 case DW_TAG_imported_declaration:
72bf9492
DJ
9305 case DW_TAG_namespace:
9306 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9307 built_actual_name != NULL,
72bf9492
DJ
9308 VAR_DOMAIN, LOC_TYPEDEF,
9309 &objfile->global_psymbols,
1762568f 9310 0, cu->language, objfile);
72bf9492 9311 break;
530e8392
KB
9312 case DW_TAG_module:
9313 add_psymbol_to_list (actual_name, strlen (actual_name),
9314 built_actual_name != NULL,
9315 MODULE_DOMAIN, LOC_TYPEDEF,
9316 &objfile->global_psymbols,
1762568f 9317 0, cu->language, objfile);
530e8392 9318 break;
c906108c 9319 case DW_TAG_class_type:
680b30c7 9320 case DW_TAG_interface_type:
c906108c
SS
9321 case DW_TAG_structure_type:
9322 case DW_TAG_union_type:
9323 case DW_TAG_enumeration_type:
fa4028e9
JB
9324 /* Skip external references. The DWARF standard says in the section
9325 about "Structure, Union, and Class Type Entries": "An incomplete
9326 structure, union or class type is represented by a structure,
9327 union or class entry that does not have a byte size attribute
9328 and that has a DW_AT_declaration attribute." */
9329 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 9330 {
15d034d0 9331 xfree (built_actual_name);
decbce07
MS
9332 return;
9333 }
fa4028e9 9334
63d06c5c
DC
9335 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9336 static vs. global. */
38d518c9 9337 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9338 built_actual_name != NULL,
176620f1 9339 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 9340 cu->language == language_cplus
63d06c5c
DC
9341 ? &objfile->global_psymbols
9342 : &objfile->static_psymbols,
1762568f 9343 0, cu->language, objfile);
c906108c 9344
c906108c
SS
9345 break;
9346 case DW_TAG_enumerator:
38d518c9 9347 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9348 built_actual_name != NULL,
176620f1 9349 VAR_DOMAIN, LOC_CONST,
9c37b5ae 9350 cu->language == language_cplus
f6fe98ef
DJ
9351 ? &objfile->global_psymbols
9352 : &objfile->static_psymbols,
1762568f 9353 0, cu->language, objfile);
c906108c
SS
9354 break;
9355 default:
9356 break;
9357 }
5c4e30ca 9358
15d034d0 9359 xfree (built_actual_name);
c906108c
SS
9360}
9361
5c4e30ca
DC
9362/* Read a partial die corresponding to a namespace; also, add a symbol
9363 corresponding to that namespace to the symbol table. NAMESPACE is
9364 the name of the enclosing namespace. */
91c24f0a 9365
72bf9492
DJ
9366static void
9367add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9368 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9369 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9370{
72bf9492 9371 /* Add a symbol for the namespace. */
e7c27a73 9372
72bf9492 9373 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9374
9375 /* Now scan partial symbols in that namespace. */
9376
91c24f0a 9377 if (pdi->has_children)
cdc07690 9378 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9379}
9380
5d7cb8df
JK
9381/* Read a partial die corresponding to a Fortran module. */
9382
9383static void
9384add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9385 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9386{
530e8392
KB
9387 /* Add a symbol for the namespace. */
9388
9389 add_partial_symbol (pdi, cu);
9390
f55ee35c 9391 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9392
9393 if (pdi->has_children)
cdc07690 9394 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9395}
9396
bc30ff58
JB
9397/* Read a partial die corresponding to a subprogram and create a partial
9398 symbol for that subprogram. When the CU language allows it, this
9399 routine also defines a partial symbol for each nested subprogram
cdc07690 9400 that this subprogram contains. If SET_ADDRMAP is true, record the
428fc5fc
YQ
9401 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
9402 and highest PC values found in PDI.
6e70227d 9403
cdc07690
YQ
9404 PDI may also be a lexical block, in which case we simply search
9405 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9406 Again, this is only performed when the CU language allows this
9407 type of definitions. */
9408
9409static void
9410add_partial_subprogram (struct partial_die_info *pdi,
9411 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9412 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58
JB
9413{
9414 if (pdi->tag == DW_TAG_subprogram)
9415 {
9416 if (pdi->has_pc_info)
9417 {
9418 if (pdi->lowpc < *lowpc)
9419 *lowpc = pdi->lowpc;
9420 if (pdi->highpc > *highpc)
9421 *highpc = pdi->highpc;
cdc07690 9422 if (set_addrmap)
5734ee8b 9423 {
5734ee8b 9424 struct objfile *objfile = cu->objfile;
3e29f34a
MR
9425 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9426 CORE_ADDR baseaddr;
9427 CORE_ADDR highpc;
9428 CORE_ADDR lowpc;
5734ee8b
DJ
9429
9430 baseaddr = ANOFFSET (objfile->section_offsets,
9431 SECT_OFF_TEXT (objfile));
3e29f34a
MR
9432 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9433 pdi->lowpc + baseaddr);
9434 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9435 pdi->highpc + baseaddr);
9436 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 9437 cu->per_cu->v.psymtab);
5734ee8b 9438 }
481860b3
GB
9439 }
9440
9441 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9442 {
bc30ff58 9443 if (!pdi->is_declaration)
e8d05480
JB
9444 /* Ignore subprogram DIEs that do not have a name, they are
9445 illegal. Do not emit a complaint at this point, we will
9446 do so when we convert this psymtab into a symtab. */
9447 if (pdi->name)
9448 add_partial_symbol (pdi, cu);
bc30ff58
JB
9449 }
9450 }
6e70227d 9451
bc30ff58
JB
9452 if (! pdi->has_children)
9453 return;
9454
9455 if (cu->language == language_ada)
9456 {
9457 pdi = pdi->die_child;
9458 while (pdi != NULL)
9459 {
9460 fixup_partial_die (pdi, cu);
9461 if (pdi->tag == DW_TAG_subprogram
9462 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9463 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9464 pdi = pdi->die_sibling;
9465 }
9466 }
9467}
9468
91c24f0a
DC
9469/* Read a partial die corresponding to an enumeration type. */
9470
72bf9492
DJ
9471static void
9472add_partial_enumeration (struct partial_die_info *enum_pdi,
9473 struct dwarf2_cu *cu)
91c24f0a 9474{
72bf9492 9475 struct partial_die_info *pdi;
91c24f0a
DC
9476
9477 if (enum_pdi->name != NULL)
72bf9492
DJ
9478 add_partial_symbol (enum_pdi, cu);
9479
9480 pdi = enum_pdi->die_child;
9481 while (pdi)
91c24f0a 9482 {
72bf9492 9483 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 9484 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 9485 else
72bf9492
DJ
9486 add_partial_symbol (pdi, cu);
9487 pdi = pdi->die_sibling;
91c24f0a 9488 }
91c24f0a
DC
9489}
9490
6caca83c
CC
9491/* Return the initial uleb128 in the die at INFO_PTR. */
9492
9493static unsigned int
d521ce57 9494peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9495{
9496 unsigned int bytes_read;
9497
9498 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9499}
9500
4bb7a0a7
DJ
9501/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
9502 Return the corresponding abbrev, or NULL if the number is zero (indicating
9503 an empty DIE). In either case *BYTES_READ will be set to the length of
9504 the initial number. */
9505
9506static struct abbrev_info *
d521ce57 9507peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 9508 struct dwarf2_cu *cu)
4bb7a0a7
DJ
9509{
9510 bfd *abfd = cu->objfile->obfd;
9511 unsigned int abbrev_number;
9512 struct abbrev_info *abbrev;
9513
9514 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9515
9516 if (abbrev_number == 0)
9517 return NULL;
9518
433df2d4 9519 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
9520 if (!abbrev)
9521 {
422b9917
DE
9522 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9523 " at offset 0x%x [in module %s]"),
9524 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9c541725 9525 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9526 }
9527
9528 return abbrev;
9529}
9530
93311388
DE
9531/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9532 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9533 DIE. Any children of the skipped DIEs will also be skipped. */
9534
d521ce57
TT
9535static const gdb_byte *
9536skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9537{
dee91e82 9538 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
9539 struct abbrev_info *abbrev;
9540 unsigned int bytes_read;
9541
9542 while (1)
9543 {
9544 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9545 if (abbrev == NULL)
9546 return info_ptr + bytes_read;
9547 else
dee91e82 9548 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9549 }
9550}
9551
93311388
DE
9552/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9553 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9554 abbrev corresponding to that skipped uleb128 should be passed in
9555 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9556 children. */
9557
d521ce57
TT
9558static const gdb_byte *
9559skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9560 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9561{
9562 unsigned int bytes_read;
9563 struct attribute attr;
dee91e82
DE
9564 bfd *abfd = reader->abfd;
9565 struct dwarf2_cu *cu = reader->cu;
d521ce57 9566 const gdb_byte *buffer = reader->buffer;
f664829e 9567 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9568 unsigned int form, i;
9569
9570 for (i = 0; i < abbrev->num_attrs; i++)
9571 {
9572 /* The only abbrev we care about is DW_AT_sibling. */
9573 if (abbrev->attrs[i].name == DW_AT_sibling)
9574 {
dee91e82 9575 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9576 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9577 complaint (&symfile_complaints,
9578 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9579 else
b9502d3f 9580 {
9c541725
PA
9581 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9582 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9583
9584 if (sibling_ptr < info_ptr)
9585 complaint (&symfile_complaints,
9586 _("DW_AT_sibling points backwards"));
22869d73
KS
9587 else if (sibling_ptr > reader->buffer_end)
9588 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9589 else
9590 return sibling_ptr;
9591 }
4bb7a0a7
DJ
9592 }
9593
9594 /* If it isn't DW_AT_sibling, skip this attribute. */
9595 form = abbrev->attrs[i].form;
9596 skip_attribute:
9597 switch (form)
9598 {
4bb7a0a7 9599 case DW_FORM_ref_addr:
ae411497
TT
9600 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9601 and later it is offset sized. */
9602 if (cu->header.version == 2)
9603 info_ptr += cu->header.addr_size;
9604 else
9605 info_ptr += cu->header.offset_size;
9606 break;
36586728
TT
9607 case DW_FORM_GNU_ref_alt:
9608 info_ptr += cu->header.offset_size;
9609 break;
ae411497 9610 case DW_FORM_addr:
4bb7a0a7
DJ
9611 info_ptr += cu->header.addr_size;
9612 break;
9613 case DW_FORM_data1:
9614 case DW_FORM_ref1:
9615 case DW_FORM_flag:
9616 info_ptr += 1;
9617 break;
2dc7f7b3 9618 case DW_FORM_flag_present:
43988095 9619 case DW_FORM_implicit_const:
2dc7f7b3 9620 break;
4bb7a0a7
DJ
9621 case DW_FORM_data2:
9622 case DW_FORM_ref2:
9623 info_ptr += 2;
9624 break;
9625 case DW_FORM_data4:
9626 case DW_FORM_ref4:
9627 info_ptr += 4;
9628 break;
9629 case DW_FORM_data8:
9630 case DW_FORM_ref8:
55f1336d 9631 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9632 info_ptr += 8;
9633 break;
0224619f
JK
9634 case DW_FORM_data16:
9635 info_ptr += 16;
9636 break;
4bb7a0a7 9637 case DW_FORM_string:
9b1c24c8 9638 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9639 info_ptr += bytes_read;
9640 break;
2dc7f7b3 9641 case DW_FORM_sec_offset:
4bb7a0a7 9642 case DW_FORM_strp:
36586728 9643 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9644 info_ptr += cu->header.offset_size;
9645 break;
2dc7f7b3 9646 case DW_FORM_exprloc:
4bb7a0a7
DJ
9647 case DW_FORM_block:
9648 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9649 info_ptr += bytes_read;
9650 break;
9651 case DW_FORM_block1:
9652 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9653 break;
9654 case DW_FORM_block2:
9655 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9656 break;
9657 case DW_FORM_block4:
9658 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9659 break;
9660 case DW_FORM_sdata:
9661 case DW_FORM_udata:
9662 case DW_FORM_ref_udata:
3019eac3
DE
9663 case DW_FORM_GNU_addr_index:
9664 case DW_FORM_GNU_str_index:
d521ce57 9665 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9666 break;
9667 case DW_FORM_indirect:
9668 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9669 info_ptr += bytes_read;
9670 /* We need to continue parsing from here, so just go back to
9671 the top. */
9672 goto skip_attribute;
9673
9674 default:
3e43a32a
MS
9675 error (_("Dwarf Error: Cannot handle %s "
9676 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9677 dwarf_form_name (form),
9678 bfd_get_filename (abfd));
9679 }
9680 }
9681
9682 if (abbrev->has_children)
dee91e82 9683 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9684 else
9685 return info_ptr;
9686}
9687
93311388 9688/* Locate ORIG_PDI's sibling.
dee91e82 9689 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9690
d521ce57 9691static const gdb_byte *
dee91e82
DE
9692locate_pdi_sibling (const struct die_reader_specs *reader,
9693 struct partial_die_info *orig_pdi,
d521ce57 9694 const gdb_byte *info_ptr)
91c24f0a
DC
9695{
9696 /* Do we know the sibling already? */
72bf9492 9697
91c24f0a
DC
9698 if (orig_pdi->sibling)
9699 return orig_pdi->sibling;
9700
9701 /* Are there any children to deal with? */
9702
9703 if (!orig_pdi->has_children)
9704 return info_ptr;
9705
4bb7a0a7 9706 /* Skip the children the long way. */
91c24f0a 9707
dee91e82 9708 return skip_children (reader, info_ptr);
91c24f0a
DC
9709}
9710
257e7a09 9711/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9712 not NULL. */
c906108c
SS
9713
9714static void
257e7a09
YQ
9715dwarf2_read_symtab (struct partial_symtab *self,
9716 struct objfile *objfile)
c906108c 9717{
257e7a09 9718 if (self->readin)
c906108c 9719 {
442e4d9c 9720 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9721 self->filename);
442e4d9c
YQ
9722 }
9723 else
9724 {
9725 if (info_verbose)
c906108c 9726 {
442e4d9c 9727 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9728 self->filename);
442e4d9c 9729 gdb_flush (gdb_stdout);
c906108c 9730 }
c906108c 9731
442e4d9c 9732 /* Restore our global data. */
9a3c8263
SM
9733 dwarf2_per_objfile
9734 = (struct dwarf2_per_objfile *) objfile_data (objfile,
9735 dwarf2_objfile_data_key);
10b3939b 9736
442e4d9c
YQ
9737 /* If this psymtab is constructed from a debug-only objfile, the
9738 has_section_at_zero flag will not necessarily be correct. We
9739 can get the correct value for this flag by looking at the data
9740 associated with the (presumably stripped) associated objfile. */
9741 if (objfile->separate_debug_objfile_backlink)
9742 {
9743 struct dwarf2_per_objfile *dpo_backlink
9a3c8263
SM
9744 = ((struct dwarf2_per_objfile *)
9745 objfile_data (objfile->separate_debug_objfile_backlink,
9746 dwarf2_objfile_data_key));
9a619af0 9747
442e4d9c
YQ
9748 dwarf2_per_objfile->has_section_at_zero
9749 = dpo_backlink->has_section_at_zero;
9750 }
b2ab525c 9751
442e4d9c 9752 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9753
257e7a09 9754 psymtab_to_symtab_1 (self);
c906108c 9755
442e4d9c
YQ
9756 /* Finish up the debug error message. */
9757 if (info_verbose)
9758 printf_filtered (_("done.\n"));
c906108c 9759 }
95554aad
TT
9760
9761 process_cu_includes ();
c906108c 9762}
9cdd5dbd
DE
9763\f
9764/* Reading in full CUs. */
c906108c 9765
10b3939b
DJ
9766/* Add PER_CU to the queue. */
9767
9768static void
95554aad
TT
9769queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9770 enum language pretend_language)
10b3939b
DJ
9771{
9772 struct dwarf2_queue_item *item;
9773
9774 per_cu->queued = 1;
8d749320 9775 item = XNEW (struct dwarf2_queue_item);
10b3939b 9776 item->per_cu = per_cu;
95554aad 9777 item->pretend_language = pretend_language;
10b3939b
DJ
9778 item->next = NULL;
9779
9780 if (dwarf2_queue == NULL)
9781 dwarf2_queue = item;
9782 else
9783 dwarf2_queue_tail->next = item;
9784
9785 dwarf2_queue_tail = item;
9786}
9787
89e63ee4
DE
9788/* If PER_CU is not yet queued, add it to the queue.
9789 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9790 dependency.
0907af0c 9791 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9792 meaning either PER_CU is already queued or it is already loaded.
9793
9794 N.B. There is an invariant here that if a CU is queued then it is loaded.
9795 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9796
9797static int
89e63ee4 9798maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9799 struct dwarf2_per_cu_data *per_cu,
9800 enum language pretend_language)
9801{
9802 /* We may arrive here during partial symbol reading, if we need full
9803 DIEs to process an unusual case (e.g. template arguments). Do
9804 not queue PER_CU, just tell our caller to load its DIEs. */
9805 if (dwarf2_per_objfile->reading_partial_symbols)
9806 {
9807 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9808 return 1;
9809 return 0;
9810 }
9811
9812 /* Mark the dependence relation so that we don't flush PER_CU
9813 too early. */
89e63ee4
DE
9814 if (dependent_cu != NULL)
9815 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9816
9817 /* If it's already on the queue, we have nothing to do. */
9818 if (per_cu->queued)
9819 return 0;
9820
9821 /* If the compilation unit is already loaded, just mark it as
9822 used. */
9823 if (per_cu->cu != NULL)
9824 {
9825 per_cu->cu->last_used = 0;
9826 return 0;
9827 }
9828
9829 /* Add it to the queue. */
9830 queue_comp_unit (per_cu, pretend_language);
9831
9832 return 1;
9833}
9834
10b3939b
DJ
9835/* Process the queue. */
9836
9837static void
a0f42c21 9838process_queue (void)
10b3939b
DJ
9839{
9840 struct dwarf2_queue_item *item, *next_item;
9841
b4f54984 9842 if (dwarf_read_debug)
45cfd468
DE
9843 {
9844 fprintf_unfiltered (gdb_stdlog,
9845 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9846 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9847 }
9848
03dd20cc
DJ
9849 /* The queue starts out with one item, but following a DIE reference
9850 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9851 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9852 {
cc12ce38
DE
9853 if ((dwarf2_per_objfile->using_index
9854 ? !item->per_cu->v.quick->compunit_symtab
9855 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9856 /* Skip dummy CUs. */
9857 && item->per_cu->cu != NULL)
f4dc4d17
DE
9858 {
9859 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9860 unsigned int debug_print_threshold;
247f5c4f 9861 char buf[100];
f4dc4d17 9862
247f5c4f 9863 if (per_cu->is_debug_types)
f4dc4d17 9864 {
247f5c4f
DE
9865 struct signatured_type *sig_type =
9866 (struct signatured_type *) per_cu;
9867
9868 sprintf (buf, "TU %s at offset 0x%x",
73be47f5 9869 hex_string (sig_type->signature),
9c541725 9870 to_underlying (per_cu->sect_off));
73be47f5
DE
9871 /* There can be 100s of TUs.
9872 Only print them in verbose mode. */
9873 debug_print_threshold = 2;
f4dc4d17 9874 }
247f5c4f 9875 else
73be47f5 9876 {
9c541725
PA
9877 sprintf (buf, "CU at offset 0x%x",
9878 to_underlying (per_cu->sect_off));
73be47f5
DE
9879 debug_print_threshold = 1;
9880 }
247f5c4f 9881
b4f54984 9882 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9883 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9884
9885 if (per_cu->is_debug_types)
9886 process_full_type_unit (per_cu, item->pretend_language);
9887 else
9888 process_full_comp_unit (per_cu, item->pretend_language);
9889
b4f54984 9890 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9891 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9892 }
10b3939b
DJ
9893
9894 item->per_cu->queued = 0;
9895 next_item = item->next;
9896 xfree (item);
9897 }
9898
9899 dwarf2_queue_tail = NULL;
45cfd468 9900
b4f54984 9901 if (dwarf_read_debug)
45cfd468
DE
9902 {
9903 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9904 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9905 }
10b3939b
DJ
9906}
9907
9908/* Free all allocated queue entries. This function only releases anything if
9909 an error was thrown; if the queue was processed then it would have been
9910 freed as we went along. */
9911
9912static void
9913dwarf2_release_queue (void *dummy)
9914{
9915 struct dwarf2_queue_item *item, *last;
9916
9917 item = dwarf2_queue;
9918 while (item)
9919 {
9920 /* Anything still marked queued is likely to be in an
9921 inconsistent state, so discard it. */
9922 if (item->per_cu->queued)
9923 {
9924 if (item->per_cu->cu != NULL)
dee91e82 9925 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
9926 item->per_cu->queued = 0;
9927 }
9928
9929 last = item;
9930 item = item->next;
9931 xfree (last);
9932 }
9933
9934 dwarf2_queue = dwarf2_queue_tail = NULL;
9935}
9936
9937/* Read in full symbols for PST, and anything it depends on. */
9938
c906108c 9939static void
fba45db2 9940psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9941{
10b3939b 9942 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9943 int i;
9944
95554aad
TT
9945 if (pst->readin)
9946 return;
9947
aaa75496 9948 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9949 if (!pst->dependencies[i]->readin
9950 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9951 {
9952 /* Inform about additional files that need to be read in. */
9953 if (info_verbose)
9954 {
a3f17187 9955 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9956 fputs_filtered (" ", gdb_stdout);
9957 wrap_here ("");
9958 fputs_filtered ("and ", gdb_stdout);
9959 wrap_here ("");
9960 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9961 wrap_here (""); /* Flush output. */
aaa75496
JB
9962 gdb_flush (gdb_stdout);
9963 }
9964 psymtab_to_symtab_1 (pst->dependencies[i]);
9965 }
9966
9a3c8263 9967 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9968
9969 if (per_cu == NULL)
aaa75496
JB
9970 {
9971 /* It's an include file, no symbols to read for it.
9972 Everything is in the parent symtab. */
9973 pst->readin = 1;
9974 return;
9975 }
c906108c 9976
a0f42c21 9977 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
9978}
9979
dee91e82
DE
9980/* Trivial hash function for die_info: the hash value of a DIE
9981 is its offset in .debug_info for this objfile. */
10b3939b 9982
dee91e82
DE
9983static hashval_t
9984die_hash (const void *item)
10b3939b 9985{
9a3c8263 9986 const struct die_info *die = (const struct die_info *) item;
6502dd73 9987
9c541725 9988 return to_underlying (die->sect_off);
dee91e82 9989}
63d06c5c 9990
dee91e82
DE
9991/* Trivial comparison function for die_info structures: two DIEs
9992 are equal if they have the same offset. */
98bfdba5 9993
dee91e82
DE
9994static int
9995die_eq (const void *item_lhs, const void *item_rhs)
9996{
9a3c8263
SM
9997 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9998 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9999
9c541725 10000 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 10001}
c906108c 10002
dee91e82
DE
10003/* die_reader_func for load_full_comp_unit.
10004 This is identical to read_signatured_type_reader,
10005 but is kept separate for now. */
c906108c 10006
dee91e82
DE
10007static void
10008load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 10009 const gdb_byte *info_ptr,
dee91e82
DE
10010 struct die_info *comp_unit_die,
10011 int has_children,
10012 void *data)
10013{
10014 struct dwarf2_cu *cu = reader->cu;
9a3c8263 10015 enum language *language_ptr = (enum language *) data;
6caca83c 10016
dee91e82
DE
10017 gdb_assert (cu->die_hash == NULL);
10018 cu->die_hash =
10019 htab_create_alloc_ex (cu->header.length / 12,
10020 die_hash,
10021 die_eq,
10022 NULL,
10023 &cu->comp_unit_obstack,
10024 hashtab_obstack_allocate,
10025 dummy_obstack_deallocate);
e142c38c 10026
dee91e82
DE
10027 if (has_children)
10028 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10029 &info_ptr, comp_unit_die);
10030 cu->dies = comp_unit_die;
10031 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
10032
10033 /* We try not to read any attributes in this function, because not
9cdd5dbd 10034 all CUs needed for references have been loaded yet, and symbol
10b3939b 10035 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
10036 or we won't be able to build types correctly.
10037 Similarly, if we do not read the producer, we can not apply
10038 producer-specific interpretation. */
95554aad 10039 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 10040}
10b3939b 10041
dee91e82 10042/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 10043
dee91e82 10044static void
95554aad
TT
10045load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10046 enum language pretend_language)
dee91e82 10047{
3019eac3 10048 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 10049
f4dc4d17
DE
10050 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10051 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
10052}
10053
3da10d80
KS
10054/* Add a DIE to the delayed physname list. */
10055
10056static void
10057add_to_method_list (struct type *type, int fnfield_index, int index,
10058 const char *name, struct die_info *die,
10059 struct dwarf2_cu *cu)
10060{
10061 struct delayed_method_info mi;
10062 mi.type = type;
10063 mi.fnfield_index = fnfield_index;
10064 mi.index = index;
10065 mi.name = name;
10066 mi.die = die;
10067 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
10068}
10069
10070/* A cleanup for freeing the delayed method list. */
10071
10072static void
10073free_delayed_list (void *ptr)
10074{
10075 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
10076 if (cu->method_list != NULL)
10077 {
10078 VEC_free (delayed_method_info, cu->method_list);
10079 cu->method_list = NULL;
10080 }
10081}
10082
3693fdb3
PA
10083/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10084 "const" / "volatile". If so, decrements LEN by the length of the
10085 modifier and return true. Otherwise return false. */
10086
10087template<size_t N>
10088static bool
10089check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10090{
10091 size_t mod_len = sizeof (mod) - 1;
10092 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10093 {
10094 len -= mod_len;
10095 return true;
10096 }
10097 return false;
10098}
10099
3da10d80
KS
10100/* Compute the physnames of any methods on the CU's method list.
10101
10102 The computation of method physnames is delayed in order to avoid the
10103 (bad) condition that one of the method's formal parameters is of an as yet
10104 incomplete type. */
10105
10106static void
10107compute_delayed_physnames (struct dwarf2_cu *cu)
10108{
10109 int i;
10110 struct delayed_method_info *mi;
3693fdb3
PA
10111
10112 /* Only C++ delays computing physnames. */
10113 if (VEC_empty (delayed_method_info, cu->method_list))
10114 return;
10115 gdb_assert (cu->language == language_cplus);
10116
3da10d80
KS
10117 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
10118 {
1d06ead6 10119 const char *physname;
3da10d80
KS
10120 struct fn_fieldlist *fn_flp
10121 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 10122 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
10123 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
10124 = physname ? physname : "";
3693fdb3
PA
10125
10126 /* Since there's no tag to indicate whether a method is a
10127 const/volatile overload, extract that information out of the
10128 demangled name. */
10129 if (physname != NULL)
10130 {
10131 size_t len = strlen (physname);
10132
10133 while (1)
10134 {
10135 if (physname[len] == ')') /* shortcut */
10136 break;
10137 else if (check_modifier (physname, len, " const"))
10138 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
10139 else if (check_modifier (physname, len, " volatile"))
10140 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
10141 else
10142 break;
10143 }
10144 }
3da10d80
KS
10145 }
10146}
10147
a766d390
DE
10148/* Go objects should be embedded in a DW_TAG_module DIE,
10149 and it's not clear if/how imported objects will appear.
10150 To keep Go support simple until that's worked out,
10151 go back through what we've read and create something usable.
10152 We could do this while processing each DIE, and feels kinda cleaner,
10153 but that way is more invasive.
10154 This is to, for example, allow the user to type "p var" or "b main"
10155 without having to specify the package name, and allow lookups
10156 of module.object to work in contexts that use the expression
10157 parser. */
10158
10159static void
10160fixup_go_packaging (struct dwarf2_cu *cu)
10161{
10162 char *package_name = NULL;
10163 struct pending *list;
10164 int i;
10165
10166 for (list = global_symbols; list != NULL; list = list->next)
10167 {
10168 for (i = 0; i < list->nsyms; ++i)
10169 {
10170 struct symbol *sym = list->symbol[i];
10171
10172 if (SYMBOL_LANGUAGE (sym) == language_go
10173 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10174 {
10175 char *this_package_name = go_symbol_package_name (sym);
10176
10177 if (this_package_name == NULL)
10178 continue;
10179 if (package_name == NULL)
10180 package_name = this_package_name;
10181 else
10182 {
10183 if (strcmp (package_name, this_package_name) != 0)
10184 complaint (&symfile_complaints,
10185 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
10186 (symbol_symtab (sym) != NULL
10187 ? symtab_to_filename_for_display
10188 (symbol_symtab (sym))
4262abfb 10189 : objfile_name (cu->objfile)),
a766d390
DE
10190 this_package_name, package_name);
10191 xfree (this_package_name);
10192 }
10193 }
10194 }
10195 }
10196
10197 if (package_name != NULL)
10198 {
10199 struct objfile *objfile = cu->objfile;
34a68019 10200 const char *saved_package_name
224c3ddb
SM
10201 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10202 package_name,
10203 strlen (package_name));
19f392bc
UW
10204 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10205 saved_package_name);
a766d390
DE
10206 struct symbol *sym;
10207
10208 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10209
e623cf5d 10210 sym = allocate_symbol (objfile);
f85f34ed 10211 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
10212 SYMBOL_SET_NAMES (sym, saved_package_name,
10213 strlen (saved_package_name), 0, objfile);
a766d390
DE
10214 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10215 e.g., "main" finds the "main" module and not C's main(). */
10216 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 10217 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
10218 SYMBOL_TYPE (sym) = type;
10219
10220 add_symbol_to_list (sym, &global_symbols);
10221
10222 xfree (package_name);
10223 }
10224}
10225
95554aad
TT
10226/* Return the symtab for PER_CU. This works properly regardless of
10227 whether we're using the index or psymtabs. */
10228
43f3e411
DE
10229static struct compunit_symtab *
10230get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad
TT
10231{
10232 return (dwarf2_per_objfile->using_index
43f3e411
DE
10233 ? per_cu->v.quick->compunit_symtab
10234 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10235}
10236
10237/* A helper function for computing the list of all symbol tables
10238 included by PER_CU. */
10239
10240static void
43f3e411 10241recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 10242 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10243 struct dwarf2_per_cu_data *per_cu,
43f3e411 10244 struct compunit_symtab *immediate_parent)
95554aad
TT
10245{
10246 void **slot;
10247 int ix;
43f3e411 10248 struct compunit_symtab *cust;
95554aad
TT
10249 struct dwarf2_per_cu_data *iter;
10250
10251 slot = htab_find_slot (all_children, per_cu, INSERT);
10252 if (*slot != NULL)
10253 {
10254 /* This inclusion and its children have been processed. */
10255 return;
10256 }
10257
10258 *slot = per_cu;
10259 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10260 cust = get_compunit_symtab (per_cu);
10261 if (cust != NULL)
ec94af83
DE
10262 {
10263 /* If this is a type unit only add its symbol table if we haven't
10264 seen it yet (type unit per_cu's can share symtabs). */
10265 if (per_cu->is_debug_types)
10266 {
43f3e411 10267 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10268 if (*slot == NULL)
10269 {
43f3e411
DE
10270 *slot = cust;
10271 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10272 if (cust->user == NULL)
10273 cust->user = immediate_parent;
ec94af83
DE
10274 }
10275 }
10276 else
f9125b6c 10277 {
43f3e411
DE
10278 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10279 if (cust->user == NULL)
10280 cust->user = immediate_parent;
f9125b6c 10281 }
ec94af83 10282 }
95554aad
TT
10283
10284 for (ix = 0;
796a7ff8 10285 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 10286 ++ix)
ec94af83
DE
10287 {
10288 recursively_compute_inclusions (result, all_children,
43f3e411 10289 all_type_symtabs, iter, cust);
ec94af83 10290 }
95554aad
TT
10291}
10292
43f3e411 10293/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10294 PER_CU. */
10295
10296static void
43f3e411 10297compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10298{
f4dc4d17
DE
10299 gdb_assert (! per_cu->is_debug_types);
10300
796a7ff8 10301 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
10302 {
10303 int ix, len;
ec94af83 10304 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
10305 struct compunit_symtab *compunit_symtab_iter;
10306 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 10307 htab_t all_children, all_type_symtabs;
43f3e411 10308 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10309
10310 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10311 if (cust == NULL)
95554aad
TT
10312 return;
10313
10314 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10315 NULL, xcalloc, xfree);
ec94af83
DE
10316 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10317 NULL, xcalloc, xfree);
95554aad
TT
10318
10319 for (ix = 0;
796a7ff8 10320 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 10321 ix, per_cu_iter);
95554aad 10322 ++ix)
ec94af83
DE
10323 {
10324 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 10325 all_type_symtabs, per_cu_iter,
43f3e411 10326 cust);
ec94af83 10327 }
95554aad 10328
ec94af83 10329 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
10330 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10331 cust->includes
8d749320
SM
10332 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
10333 struct compunit_symtab *, len + 1);
95554aad 10334 for (ix = 0;
43f3e411
DE
10335 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10336 compunit_symtab_iter);
95554aad 10337 ++ix)
43f3e411
DE
10338 cust->includes[ix] = compunit_symtab_iter;
10339 cust->includes[len] = NULL;
95554aad 10340
43f3e411 10341 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 10342 htab_delete (all_children);
ec94af83 10343 htab_delete (all_type_symtabs);
95554aad
TT
10344 }
10345}
10346
10347/* Compute the 'includes' field for the symtabs of all the CUs we just
10348 read. */
10349
10350static void
10351process_cu_includes (void)
10352{
10353 int ix;
10354 struct dwarf2_per_cu_data *iter;
10355
10356 for (ix = 0;
10357 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10358 ix, iter);
10359 ++ix)
f4dc4d17
DE
10360 {
10361 if (! iter->is_debug_types)
43f3e411 10362 compute_compunit_symtab_includes (iter);
f4dc4d17 10363 }
95554aad
TT
10364
10365 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10366}
10367
9cdd5dbd 10368/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10369 already been loaded into memory. */
10370
10371static void
95554aad
TT
10372process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10373 enum language pretend_language)
10b3939b 10374{
10b3939b 10375 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 10376 struct objfile *objfile = per_cu->objfile;
3e29f34a 10377 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10378 CORE_ADDR lowpc, highpc;
43f3e411 10379 struct compunit_symtab *cust;
33c7c59d 10380 struct cleanup *delayed_list_cleanup;
10b3939b 10381 CORE_ADDR baseaddr;
4359dff1 10382 struct block *static_block;
3e29f34a 10383 CORE_ADDR addr;
10b3939b
DJ
10384
10385 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10386
10b3939b 10387 buildsym_init ();
33c7c59d 10388 scoped_free_pendings free_pending;
3da10d80 10389 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
10390
10391 cu->list_in_scope = &file_symbols;
c906108c 10392
95554aad
TT
10393 cu->language = pretend_language;
10394 cu->language_defn = language_def (cu->language);
10395
c906108c 10396 /* Do line number decoding in read_file_scope () */
10b3939b 10397 process_die (cu->dies, cu);
c906108c 10398
a766d390
DE
10399 /* For now fudge the Go package. */
10400 if (cu->language == language_go)
10401 fixup_go_packaging (cu);
10402
3da10d80
KS
10403 /* Now that we have processed all the DIEs in the CU, all the types
10404 should be complete, and it should now be safe to compute all of the
10405 physnames. */
10406 compute_delayed_physnames (cu);
10407 do_cleanups (delayed_list_cleanup);
10408
fae299cd
DC
10409 /* Some compilers don't define a DW_AT_high_pc attribute for the
10410 compilation unit. If the DW_AT_high_pc is missing, synthesize
10411 it, by scanning the DIE's below the compilation unit. */
10b3939b 10412 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10413
3e29f34a
MR
10414 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10415 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10416
10417 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10418 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10419 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10420 addrmap to help ensure it has an accurate map of pc values belonging to
10421 this comp unit. */
10422 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10423
43f3e411
DE
10424 cust = end_symtab_from_static_block (static_block,
10425 SECT_OFF_TEXT (objfile), 0);
c906108c 10426
43f3e411 10427 if (cust != NULL)
c906108c 10428 {
df15bd07 10429 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10430
8be455d7
JK
10431 /* Set symtab language to language from DW_AT_language. If the
10432 compilation is from a C file generated by language preprocessors, do
10433 not set the language if it was already deduced by start_subfile. */
43f3e411 10434 if (!(cu->language == language_c
40e3ad0e 10435 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10436 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10437
10438 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10439 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10440 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10441 there were bugs in prologue debug info, fixed later in GCC-4.5
10442 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10443
10444 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10445 needed, it would be wrong due to missing DW_AT_producer there.
10446
10447 Still one can confuse GDB by using non-standard GCC compilation
10448 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10449 */
ab260dad 10450 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10451 cust->locations_valid = 1;
e0d00bc7
JK
10452
10453 if (gcc_4_minor >= 5)
43f3e411 10454 cust->epilogue_unwind_valid = 1;
96408a79 10455
43f3e411 10456 cust->call_site_htab = cu->call_site_htab;
c906108c 10457 }
9291a0cd
TT
10458
10459 if (dwarf2_per_objfile->using_index)
43f3e411 10460 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10461 else
10462 {
10463 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10464 pst->compunit_symtab = cust;
9291a0cd
TT
10465 pst->readin = 1;
10466 }
c906108c 10467
95554aad
TT
10468 /* Push it for inclusion processing later. */
10469 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
f4dc4d17 10470}
45cfd468 10471
f4dc4d17
DE
10472/* Generate full symbol information for type unit PER_CU, whose DIEs have
10473 already been loaded into memory. */
10474
10475static void
10476process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10477 enum language pretend_language)
10478{
10479 struct dwarf2_cu *cu = per_cu->cu;
10480 struct objfile *objfile = per_cu->objfile;
43f3e411 10481 struct compunit_symtab *cust;
33c7c59d 10482 struct cleanup *delayed_list_cleanup;
0186c6a7
DE
10483 struct signatured_type *sig_type;
10484
10485 gdb_assert (per_cu->is_debug_types);
10486 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
10487
10488 buildsym_init ();
33c7c59d 10489 scoped_free_pendings free_pending;
f4dc4d17
DE
10490 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10491
10492 cu->list_in_scope = &file_symbols;
10493
10494 cu->language = pretend_language;
10495 cu->language_defn = language_def (cu->language);
10496
10497 /* The symbol tables are set up in read_type_unit_scope. */
10498 process_die (cu->dies, cu);
10499
10500 /* For now fudge the Go package. */
10501 if (cu->language == language_go)
10502 fixup_go_packaging (cu);
10503
10504 /* Now that we have processed all the DIEs in the CU, all the types
10505 should be complete, and it should now be safe to compute all of the
10506 physnames. */
10507 compute_delayed_physnames (cu);
10508 do_cleanups (delayed_list_cleanup);
10509
10510 /* TUs share symbol tables.
10511 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10512 of it with end_expandable_symtab. Otherwise, complete the addition of
10513 this TU's symbols to the existing symtab. */
43f3e411 10514 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10515 {
43f3e411
DE
10516 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10517 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10518
43f3e411 10519 if (cust != NULL)
f4dc4d17
DE
10520 {
10521 /* Set symtab language to language from DW_AT_language. If the
10522 compilation is from a C file generated by language preprocessors,
10523 do not set the language if it was already deduced by
10524 start_subfile. */
43f3e411
DE
10525 if (!(cu->language == language_c
10526 && COMPUNIT_FILETABS (cust)->language != language_c))
10527 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10528 }
10529 }
10530 else
10531 {
0ab9ce85 10532 augment_type_symtab ();
43f3e411 10533 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10534 }
10535
10536 if (dwarf2_per_objfile->using_index)
43f3e411 10537 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10538 else
10539 {
10540 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10541 pst->compunit_symtab = cust;
f4dc4d17 10542 pst->readin = 1;
45cfd468 10543 }
c906108c
SS
10544}
10545
95554aad
TT
10546/* Process an imported unit DIE. */
10547
10548static void
10549process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10550{
10551 struct attribute *attr;
10552
f4dc4d17
DE
10553 /* For now we don't handle imported units in type units. */
10554 if (cu->per_cu->is_debug_types)
10555 {
10556 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10557 " supported in type units [in module %s]"),
4262abfb 10558 objfile_name (cu->objfile));
f4dc4d17
DE
10559 }
10560
95554aad
TT
10561 attr = dwarf2_attr (die, DW_AT_import, cu);
10562 if (attr != NULL)
10563 {
9c541725
PA
10564 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10565 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10566 dwarf2_per_cu_data *per_cu
10567 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
95554aad 10568
69d751e3 10569 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
10570 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10571 load_full_comp_unit (per_cu, cu->language);
10572
796a7ff8 10573 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
10574 per_cu);
10575 }
10576}
10577
4c8aa72d
PA
10578/* RAII object that represents a process_die scope: i.e.,
10579 starts/finishes processing a DIE. */
10580class process_die_scope
adde2bff 10581{
4c8aa72d
PA
10582public:
10583 process_die_scope (die_info *die, dwarf2_cu *cu)
10584 : m_die (die), m_cu (cu)
10585 {
10586 /* We should only be processing DIEs not already in process. */
10587 gdb_assert (!m_die->in_process);
10588 m_die->in_process = true;
10589 }
8c3cb9fa 10590
4c8aa72d
PA
10591 ~process_die_scope ()
10592 {
10593 m_die->in_process = false;
10594
10595 /* If we're done processing the DIE for the CU that owns the line
10596 header, we don't need the line header anymore. */
10597 if (m_cu->line_header_die_owner == m_die)
10598 {
10599 delete m_cu->line_header;
10600 m_cu->line_header = NULL;
10601 m_cu->line_header_die_owner = NULL;
10602 }
10603 }
10604
10605private:
10606 die_info *m_die;
10607 dwarf2_cu *m_cu;
10608};
adde2bff 10609
c906108c
SS
10610/* Process a die and its children. */
10611
10612static void
e7c27a73 10613process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10614{
4c8aa72d 10615 process_die_scope scope (die, cu);
adde2bff 10616
c906108c
SS
10617 switch (die->tag)
10618 {
10619 case DW_TAG_padding:
10620 break;
10621 case DW_TAG_compile_unit:
95554aad 10622 case DW_TAG_partial_unit:
e7c27a73 10623 read_file_scope (die, cu);
c906108c 10624 break;
348e048f
DE
10625 case DW_TAG_type_unit:
10626 read_type_unit_scope (die, cu);
10627 break;
c906108c 10628 case DW_TAG_subprogram:
c906108c 10629 case DW_TAG_inlined_subroutine:
edb3359d 10630 read_func_scope (die, cu);
c906108c
SS
10631 break;
10632 case DW_TAG_lexical_block:
14898363
L
10633 case DW_TAG_try_block:
10634 case DW_TAG_catch_block:
e7c27a73 10635 read_lexical_block_scope (die, cu);
c906108c 10636 break;
216f72a1 10637 case DW_TAG_call_site:
96408a79
SA
10638 case DW_TAG_GNU_call_site:
10639 read_call_site_scope (die, cu);
10640 break;
c906108c 10641 case DW_TAG_class_type:
680b30c7 10642 case DW_TAG_interface_type:
c906108c
SS
10643 case DW_TAG_structure_type:
10644 case DW_TAG_union_type:
134d01f1 10645 process_structure_scope (die, cu);
c906108c
SS
10646 break;
10647 case DW_TAG_enumeration_type:
134d01f1 10648 process_enumeration_scope (die, cu);
c906108c 10649 break;
134d01f1 10650
f792889a
DJ
10651 /* These dies have a type, but processing them does not create
10652 a symbol or recurse to process the children. Therefore we can
10653 read them on-demand through read_type_die. */
c906108c 10654 case DW_TAG_subroutine_type:
72019c9c 10655 case DW_TAG_set_type:
c906108c 10656 case DW_TAG_array_type:
c906108c 10657 case DW_TAG_pointer_type:
c906108c 10658 case DW_TAG_ptr_to_member_type:
c906108c 10659 case DW_TAG_reference_type:
4297a3f0 10660 case DW_TAG_rvalue_reference_type:
c906108c 10661 case DW_TAG_string_type:
c906108c 10662 break;
134d01f1 10663
c906108c 10664 case DW_TAG_base_type:
a02abb62 10665 case DW_TAG_subrange_type:
cb249c71 10666 case DW_TAG_typedef:
134d01f1
DJ
10667 /* Add a typedef symbol for the type definition, if it has a
10668 DW_AT_name. */
f792889a 10669 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10670 break;
c906108c 10671 case DW_TAG_common_block:
e7c27a73 10672 read_common_block (die, cu);
c906108c
SS
10673 break;
10674 case DW_TAG_common_inclusion:
10675 break;
d9fa45fe 10676 case DW_TAG_namespace:
4d4ec4e5 10677 cu->processing_has_namespace_info = 1;
e7c27a73 10678 read_namespace (die, cu);
d9fa45fe 10679 break;
5d7cb8df 10680 case DW_TAG_module:
4d4ec4e5 10681 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
10682 read_module (die, cu);
10683 break;
d9fa45fe 10684 case DW_TAG_imported_declaration:
74921315
KS
10685 cu->processing_has_namespace_info = 1;
10686 if (read_namespace_alias (die, cu))
10687 break;
10688 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 10689 case DW_TAG_imported_module:
4d4ec4e5 10690 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
10691 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10692 || cu->language != language_fortran))
10693 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10694 dwarf_tag_name (die->tag));
10695 read_import_statement (die, cu);
d9fa45fe 10696 break;
95554aad
TT
10697
10698 case DW_TAG_imported_unit:
10699 process_imported_unit_die (die, cu);
10700 break;
10701
71a3c369
TT
10702 case DW_TAG_variable:
10703 read_variable (die, cu);
10704 break;
10705
c906108c 10706 default:
e7c27a73 10707 new_symbol (die, NULL, cu);
c906108c
SS
10708 break;
10709 }
10710}
ca69b9e6
DE
10711\f
10712/* DWARF name computation. */
c906108c 10713
94af9270
KS
10714/* A helper function for dwarf2_compute_name which determines whether DIE
10715 needs to have the name of the scope prepended to the name listed in the
10716 die. */
10717
10718static int
10719die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10720{
1c809c68
TT
10721 struct attribute *attr;
10722
94af9270
KS
10723 switch (die->tag)
10724 {
10725 case DW_TAG_namespace:
10726 case DW_TAG_typedef:
10727 case DW_TAG_class_type:
10728 case DW_TAG_interface_type:
10729 case DW_TAG_structure_type:
10730 case DW_TAG_union_type:
10731 case DW_TAG_enumeration_type:
10732 case DW_TAG_enumerator:
10733 case DW_TAG_subprogram:
08a76f8a 10734 case DW_TAG_inlined_subroutine:
94af9270 10735 case DW_TAG_member:
74921315 10736 case DW_TAG_imported_declaration:
94af9270
KS
10737 return 1;
10738
10739 case DW_TAG_variable:
c2b0a229 10740 case DW_TAG_constant:
94af9270
KS
10741 /* We only need to prefix "globally" visible variables. These include
10742 any variable marked with DW_AT_external or any variable that
10743 lives in a namespace. [Variables in anonymous namespaces
10744 require prefixing, but they are not DW_AT_external.] */
10745
10746 if (dwarf2_attr (die, DW_AT_specification, cu))
10747 {
10748 struct dwarf2_cu *spec_cu = cu;
9a619af0 10749
94af9270
KS
10750 return die_needs_namespace (die_specification (die, &spec_cu),
10751 spec_cu);
10752 }
10753
1c809c68 10754 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10755 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10756 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10757 return 0;
10758 /* A variable in a lexical block of some kind does not need a
10759 namespace, even though in C++ such variables may be external
10760 and have a mangled name. */
10761 if (die->parent->tag == DW_TAG_lexical_block
10762 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10763 || die->parent->tag == DW_TAG_catch_block
10764 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10765 return 0;
10766 return 1;
94af9270
KS
10767
10768 default:
10769 return 0;
10770 }
10771}
10772
73b9be8b
KS
10773/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10774 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10775 defined for the given DIE. */
10776
10777static struct attribute *
10778dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10779{
10780 struct attribute *attr;
10781
10782 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10783 if (attr == NULL)
10784 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10785
10786 return attr;
10787}
10788
10789/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10790 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10791 defined for the given DIE. */
10792
10793static const char *
10794dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10795{
10796 const char *linkage_name;
10797
10798 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10799 if (linkage_name == NULL)
10800 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10801
10802 return linkage_name;
10803}
10804
94af9270 10805/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10806 compute the physname for the object, which include a method's:
9c37b5ae 10807 - formal parameters (C++),
a766d390 10808 - receiver type (Go),
a766d390
DE
10809
10810 The term "physname" is a bit confusing.
10811 For C++, for example, it is the demangled name.
10812 For Go, for example, it's the mangled name.
94af9270 10813
af6b7be1
JB
10814 For Ada, return the DIE's linkage name rather than the fully qualified
10815 name. PHYSNAME is ignored..
10816
94af9270
KS
10817 The result is allocated on the objfile_obstack and canonicalized. */
10818
10819static const char *
15d034d0
TT
10820dwarf2_compute_name (const char *name,
10821 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10822 int physname)
10823{
bb5ed363
DE
10824 struct objfile *objfile = cu->objfile;
10825
94af9270
KS
10826 if (name == NULL)
10827 name = dwarf2_name (die, cu);
10828
2ee7123e
DE
10829 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10830 but otherwise compute it by typename_concat inside GDB.
10831 FIXME: Actually this is not really true, or at least not always true.
10832 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10833 Fortran names because there is no mangling standard. So new_symbol_full
10834 will set the demangled name to the result of dwarf2_full_name, and it is
10835 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10836 if (cu->language == language_ada
10837 || (cu->language == language_fortran && physname))
10838 {
10839 /* For Ada unit, we prefer the linkage name over the name, as
10840 the former contains the exported name, which the user expects
10841 to be able to reference. Ideally, we want the user to be able
10842 to reference this entity using either natural or linkage name,
10843 but we haven't started looking at this enhancement yet. */
73b9be8b 10844 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10845
2ee7123e
DE
10846 if (linkage_name != NULL)
10847 return linkage_name;
f55ee35c
JK
10848 }
10849
94af9270
KS
10850 /* These are the only languages we know how to qualify names in. */
10851 if (name != NULL
9c37b5ae 10852 && (cu->language == language_cplus
c44af4eb
TT
10853 || cu->language == language_fortran || cu->language == language_d
10854 || cu->language == language_rust))
94af9270
KS
10855 {
10856 if (die_needs_namespace (die, cu))
10857 {
0d5cff50 10858 const char *prefix;
34a68019 10859 const char *canonical_name = NULL;
94af9270 10860
d7e74731
PA
10861 string_file buf;
10862
94af9270 10863 prefix = determine_prefix (die, cu);
94af9270
KS
10864 if (*prefix != '\0')
10865 {
f55ee35c
JK
10866 char *prefixed_name = typename_concat (NULL, prefix, name,
10867 physname, cu);
9a619af0 10868
d7e74731 10869 buf.puts (prefixed_name);
94af9270
KS
10870 xfree (prefixed_name);
10871 }
10872 else
d7e74731 10873 buf.puts (name);
94af9270 10874
98bfdba5
PA
10875 /* Template parameters may be specified in the DIE's DW_AT_name, or
10876 as children with DW_TAG_template_type_param or
10877 DW_TAG_value_type_param. If the latter, add them to the name
10878 here. If the name already has template parameters, then
10879 skip this step; some versions of GCC emit both, and
10880 it is more efficient to use the pre-computed name.
10881
10882 Something to keep in mind about this process: it is very
10883 unlikely, or in some cases downright impossible, to produce
10884 something that will match the mangled name of a function.
10885 If the definition of the function has the same debug info,
10886 we should be able to match up with it anyway. But fallbacks
10887 using the minimal symbol, for instance to find a method
10888 implemented in a stripped copy of libstdc++, will not work.
10889 If we do not have debug info for the definition, we will have to
10890 match them up some other way.
10891
10892 When we do name matching there is a related problem with function
10893 templates; two instantiated function templates are allowed to
10894 differ only by their return types, which we do not add here. */
10895
10896 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10897 {
10898 struct attribute *attr;
10899 struct die_info *child;
10900 int first = 1;
10901
10902 die->building_fullname = 1;
10903
10904 for (child = die->child; child != NULL; child = child->sibling)
10905 {
10906 struct type *type;
12df843f 10907 LONGEST value;
d521ce57 10908 const gdb_byte *bytes;
98bfdba5
PA
10909 struct dwarf2_locexpr_baton *baton;
10910 struct value *v;
10911
10912 if (child->tag != DW_TAG_template_type_param
10913 && child->tag != DW_TAG_template_value_param)
10914 continue;
10915
10916 if (first)
10917 {
d7e74731 10918 buf.puts ("<");
98bfdba5
PA
10919 first = 0;
10920 }
10921 else
d7e74731 10922 buf.puts (", ");
98bfdba5
PA
10923
10924 attr = dwarf2_attr (child, DW_AT_type, cu);
10925 if (attr == NULL)
10926 {
10927 complaint (&symfile_complaints,
10928 _("template parameter missing DW_AT_type"));
d7e74731 10929 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10930 continue;
10931 }
10932 type = die_type (child, cu);
10933
10934 if (child->tag == DW_TAG_template_type_param)
10935 {
d7e74731 10936 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
10937 continue;
10938 }
10939
10940 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10941 if (attr == NULL)
10942 {
10943 complaint (&symfile_complaints,
3e43a32a
MS
10944 _("template parameter missing "
10945 "DW_AT_const_value"));
d7e74731 10946 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10947 continue;
10948 }
10949
10950 dwarf2_const_value_attr (attr, type, name,
10951 &cu->comp_unit_obstack, cu,
10952 &value, &bytes, &baton);
10953
10954 if (TYPE_NOSIGN (type))
10955 /* GDB prints characters as NUMBER 'CHAR'. If that's
10956 changed, this can use value_print instead. */
d7e74731 10957 c_printchar (value, type, &buf);
98bfdba5
PA
10958 else
10959 {
10960 struct value_print_options opts;
10961
10962 if (baton != NULL)
10963 v = dwarf2_evaluate_loc_desc (type, NULL,
10964 baton->data,
10965 baton->size,
10966 baton->per_cu);
10967 else if (bytes != NULL)
10968 {
10969 v = allocate_value (type);
10970 memcpy (value_contents_writeable (v), bytes,
10971 TYPE_LENGTH (type));
10972 }
10973 else
10974 v = value_from_longest (type, value);
10975
3e43a32a
MS
10976 /* Specify decimal so that we do not depend on
10977 the radix. */
98bfdba5
PA
10978 get_formatted_print_options (&opts, 'd');
10979 opts.raw = 1;
d7e74731 10980 value_print (v, &buf, &opts);
98bfdba5
PA
10981 release_value (v);
10982 value_free (v);
10983 }
10984 }
10985
10986 die->building_fullname = 0;
10987
10988 if (!first)
10989 {
10990 /* Close the argument list, with a space if necessary
10991 (nested templates). */
d7e74731
PA
10992 if (!buf.empty () && buf.string ().back () == '>')
10993 buf.puts (" >");
98bfdba5 10994 else
d7e74731 10995 buf.puts (">");
98bfdba5
PA
10996 }
10997 }
10998
9c37b5ae 10999 /* For C++ methods, append formal parameter type
94af9270 11000 information, if PHYSNAME. */
6e70227d 11001
94af9270 11002 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 11003 && cu->language == language_cplus)
94af9270
KS
11004 {
11005 struct type *type = read_type_die (die, cu);
11006
d7e74731 11007 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 11008 &type_print_raw_options);
94af9270 11009
9c37b5ae 11010 if (cu->language == language_cplus)
94af9270 11011 {
60430eff
DJ
11012 /* Assume that an artificial first parameter is
11013 "this", but do not crash if it is not. RealView
11014 marks unnamed (and thus unused) parameters as
11015 artificial; there is no way to differentiate
11016 the two cases. */
94af9270
KS
11017 if (TYPE_NFIELDS (type) > 0
11018 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11019 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11020 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11021 0))))
d7e74731 11022 buf.puts (" const");
94af9270
KS
11023 }
11024 }
11025
d7e74731 11026 const std::string &intermediate_name = buf.string ();
94af9270
KS
11027
11028 if (cu->language == language_cplus)
34a68019 11029 canonical_name
322a8516 11030 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11031 &objfile->per_bfd->storage_obstack);
11032
11033 /* If we only computed INTERMEDIATE_NAME, or if
11034 INTERMEDIATE_NAME is already canonical, then we need to
11035 copy it to the appropriate obstack. */
322a8516 11036 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
11037 name = ((const char *)
11038 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
11039 intermediate_name.c_str (),
11040 intermediate_name.length ()));
34a68019
TT
11041 else
11042 name = canonical_name;
94af9270
KS
11043 }
11044 }
11045
11046 return name;
11047}
11048
0114d602
DJ
11049/* Return the fully qualified name of DIE, based on its DW_AT_name.
11050 If scope qualifiers are appropriate they will be added. The result
34a68019 11051 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11052 not have a name. NAME may either be from a previous call to
11053 dwarf2_name or NULL.
11054
9c37b5ae 11055 The output string will be canonicalized (if C++). */
0114d602
DJ
11056
11057static const char *
15d034d0 11058dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11059{
94af9270
KS
11060 return dwarf2_compute_name (name, die, cu, 0);
11061}
0114d602 11062
94af9270
KS
11063/* Construct a physname for the given DIE in CU. NAME may either be
11064 from a previous call to dwarf2_name or NULL. The result will be
11065 allocated on the objfile_objstack or NULL if the DIE does not have a
11066 name.
0114d602 11067
9c37b5ae 11068 The output string will be canonicalized (if C++). */
0114d602 11069
94af9270 11070static const char *
15d034d0 11071dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11072{
bb5ed363 11073 struct objfile *objfile = cu->objfile;
900e11f9 11074 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11075 int need_copy = 1;
11076
11077 /* In this case dwarf2_compute_name is just a shortcut not building anything
11078 on its own. */
11079 if (!die_needs_namespace (die, cu))
11080 return dwarf2_compute_name (name, die, cu, 1);
11081
73b9be8b 11082 mangled = dw2_linkage_name (die, cu);
900e11f9 11083
e98c9e7c
TT
11084 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11085 See https://github.com/rust-lang/rust/issues/32925. */
11086 if (cu->language == language_rust && mangled != NULL
11087 && strchr (mangled, '{') != NULL)
11088 mangled = NULL;
11089
900e11f9
JK
11090 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11091 has computed. */
791afaa2 11092 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11093 if (mangled != NULL)
900e11f9 11094 {
900e11f9
JK
11095 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
11096 type. It is easier for GDB users to search for such functions as
11097 `name(params)' than `long name(params)'. In such case the minimal
11098 symbol names do not match the full symbol names but for template
11099 functions there is never a need to look up their definition from their
11100 declaration so the only disadvantage remains the minimal symbol
11101 variant `long name(params)' does not have the proper inferior type.
11102 */
11103
a766d390
DE
11104 if (cu->language == language_go)
11105 {
11106 /* This is a lie, but we already lie to the caller new_symbol_full.
11107 new_symbol_full assumes we return the mangled name.
11108 This just undoes that lie until things are cleaned up. */
a766d390
DE
11109 }
11110 else
11111 {
791afaa2
TT
11112 demangled.reset (gdb_demangle (mangled,
11113 (DMGL_PARAMS | DMGL_ANSI
11114 | DMGL_RET_DROP)));
a766d390 11115 }
900e11f9 11116 if (demangled)
791afaa2 11117 canon = demangled.get ();
900e11f9
JK
11118 else
11119 {
11120 canon = mangled;
11121 need_copy = 0;
11122 }
11123 }
11124
11125 if (canon == NULL || check_physname)
11126 {
11127 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11128
11129 if (canon != NULL && strcmp (physname, canon) != 0)
11130 {
11131 /* It may not mean a bug in GDB. The compiler could also
11132 compute DW_AT_linkage_name incorrectly. But in such case
11133 GDB would need to be bug-to-bug compatible. */
11134
11135 complaint (&symfile_complaints,
11136 _("Computed physname <%s> does not match demangled <%s> "
11137 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9c541725 11138 physname, canon, mangled, to_underlying (die->sect_off),
4262abfb 11139 objfile_name (objfile));
900e11f9
JK
11140
11141 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11142 is available here - over computed PHYSNAME. It is safer
11143 against both buggy GDB and buggy compilers. */
11144
11145 retval = canon;
11146 }
11147 else
11148 {
11149 retval = physname;
11150 need_copy = 0;
11151 }
11152 }
11153 else
11154 retval = canon;
11155
11156 if (need_copy)
224c3ddb
SM
11157 retval = ((const char *)
11158 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11159 retval, strlen (retval)));
900e11f9 11160
900e11f9 11161 return retval;
0114d602
DJ
11162}
11163
74921315
KS
11164/* Inspect DIE in CU for a namespace alias. If one exists, record
11165 a new symbol for it.
11166
11167 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11168
11169static int
11170read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11171{
11172 struct attribute *attr;
11173
11174 /* If the die does not have a name, this is not a namespace
11175 alias. */
11176 attr = dwarf2_attr (die, DW_AT_name, cu);
11177 if (attr != NULL)
11178 {
11179 int num;
11180 struct die_info *d = die;
11181 struct dwarf2_cu *imported_cu = cu;
11182
11183 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11184 keep inspecting DIEs until we hit the underlying import. */
11185#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11186 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11187 {
11188 attr = dwarf2_attr (d, DW_AT_import, cu);
11189 if (attr == NULL)
11190 break;
11191
11192 d = follow_die_ref (d, attr, &imported_cu);
11193 if (d->tag != DW_TAG_imported_declaration)
11194 break;
11195 }
11196
11197 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11198 {
11199 complaint (&symfile_complaints,
11200 _("DIE at 0x%x has too many recursively imported "
9c541725 11201 "declarations"), to_underlying (d->sect_off));
74921315
KS
11202 return 0;
11203 }
11204
11205 if (attr != NULL)
11206 {
11207 struct type *type;
9c541725 11208 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11209
9c541725 11210 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11211 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11212 {
11213 /* This declaration is a global namespace alias. Add
11214 a symbol for it whose type is the aliased namespace. */
11215 new_symbol (die, type, cu);
11216 return 1;
11217 }
11218 }
11219 }
11220
11221 return 0;
11222}
11223
22cee43f
PMR
11224/* Return the using directives repository (global or local?) to use in the
11225 current context for LANGUAGE.
11226
11227 For Ada, imported declarations can materialize renamings, which *may* be
11228 global. However it is impossible (for now?) in DWARF to distinguish
11229 "external" imported declarations and "static" ones. As all imported
11230 declarations seem to be static in all other languages, make them all CU-wide
11231 global only in Ada. */
11232
11233static struct using_direct **
11234using_directives (enum language language)
11235{
11236 if (language == language_ada && context_stack_depth == 0)
11237 return &global_using_directives;
11238 else
11239 return &local_using_directives;
11240}
11241
27aa8d6a
SW
11242/* Read the import statement specified by the given die and record it. */
11243
11244static void
11245read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11246{
bb5ed363 11247 struct objfile *objfile = cu->objfile;
27aa8d6a 11248 struct attribute *import_attr;
32019081 11249 struct die_info *imported_die, *child_die;
de4affc9 11250 struct dwarf2_cu *imported_cu;
27aa8d6a 11251 const char *imported_name;
794684b6 11252 const char *imported_name_prefix;
13387711
SW
11253 const char *canonical_name;
11254 const char *import_alias;
11255 const char *imported_declaration = NULL;
794684b6 11256 const char *import_prefix;
eb1e02fd 11257 std::vector<const char *> excludes;
13387711 11258
27aa8d6a
SW
11259 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11260 if (import_attr == NULL)
11261 {
11262 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11263 dwarf_tag_name (die->tag));
11264 return;
11265 }
11266
de4affc9
CC
11267 imported_cu = cu;
11268 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11269 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11270 if (imported_name == NULL)
11271 {
11272 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11273
11274 The import in the following code:
11275 namespace A
11276 {
11277 typedef int B;
11278 }
11279
11280 int main ()
11281 {
11282 using A::B;
11283 B b;
11284 return b;
11285 }
11286
11287 ...
11288 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11289 <52> DW_AT_decl_file : 1
11290 <53> DW_AT_decl_line : 6
11291 <54> DW_AT_import : <0x75>
11292 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11293 <59> DW_AT_name : B
11294 <5b> DW_AT_decl_file : 1
11295 <5c> DW_AT_decl_line : 2
11296 <5d> DW_AT_type : <0x6e>
11297 ...
11298 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11299 <76> DW_AT_byte_size : 4
11300 <77> DW_AT_encoding : 5 (signed)
11301
11302 imports the wrong die ( 0x75 instead of 0x58 ).
11303 This case will be ignored until the gcc bug is fixed. */
11304 return;
11305 }
11306
82856980
SW
11307 /* Figure out the local name after import. */
11308 import_alias = dwarf2_name (die, cu);
27aa8d6a 11309
794684b6
SW
11310 /* Figure out where the statement is being imported to. */
11311 import_prefix = determine_prefix (die, cu);
11312
11313 /* Figure out what the scope of the imported die is and prepend it
11314 to the name of the imported die. */
de4affc9 11315 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11316
f55ee35c
JK
11317 if (imported_die->tag != DW_TAG_namespace
11318 && imported_die->tag != DW_TAG_module)
794684b6 11319 {
13387711
SW
11320 imported_declaration = imported_name;
11321 canonical_name = imported_name_prefix;
794684b6 11322 }
13387711 11323 else if (strlen (imported_name_prefix) > 0)
12aaed36 11324 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11325 imported_name_prefix,
11326 (cu->language == language_d ? "." : "::"),
11327 imported_name, (char *) NULL);
13387711
SW
11328 else
11329 canonical_name = imported_name;
794684b6 11330
32019081
JK
11331 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11332 for (child_die = die->child; child_die && child_die->tag;
11333 child_die = sibling_die (child_die))
11334 {
11335 /* DWARF-4: A Fortran use statement with a “rename list” may be
11336 represented by an imported module entry with an import attribute
11337 referring to the module and owned entries corresponding to those
11338 entities that are renamed as part of being imported. */
11339
11340 if (child_die->tag != DW_TAG_imported_declaration)
11341 {
11342 complaint (&symfile_complaints,
11343 _("child DW_TAG_imported_declaration expected "
11344 "- DIE at 0x%x [in module %s]"),
9c541725 11345 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
11346 continue;
11347 }
11348
11349 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11350 if (import_attr == NULL)
11351 {
11352 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11353 dwarf_tag_name (child_die->tag));
11354 continue;
11355 }
11356
11357 imported_cu = cu;
11358 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11359 &imported_cu);
11360 imported_name = dwarf2_name (imported_die, imported_cu);
11361 if (imported_name == NULL)
11362 {
11363 complaint (&symfile_complaints,
11364 _("child DW_TAG_imported_declaration has unknown "
11365 "imported name - DIE at 0x%x [in module %s]"),
9c541725 11366 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
11367 continue;
11368 }
11369
eb1e02fd 11370 excludes.push_back (imported_name);
32019081
JK
11371
11372 process_die (child_die, cu);
11373 }
11374
22cee43f
PMR
11375 add_using_directive (using_directives (cu->language),
11376 import_prefix,
11377 canonical_name,
11378 import_alias,
11379 imported_declaration,
11380 excludes,
11381 0,
11382 &objfile->objfile_obstack);
27aa8d6a
SW
11383}
11384
5230b05a
WT
11385/* ICC<14 does not output the required DW_AT_declaration on incomplete
11386 types, but gives them a size of zero. Starting with version 14,
11387 ICC is compatible with GCC. */
11388
11389static int
11390producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11391{
11392 if (!cu->checked_producer)
11393 check_producer (cu);
11394
11395 return cu->producer_is_icc_lt_14;
11396}
11397
1b80a9fa
JK
11398/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11399 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11400 this, it was first present in GCC release 4.3.0. */
11401
11402static int
11403producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11404{
11405 if (!cu->checked_producer)
11406 check_producer (cu);
11407
11408 return cu->producer_is_gcc_lt_4_3;
11409}
11410
d721ba37
PA
11411static file_and_directory
11412find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11413{
d721ba37
PA
11414 file_and_directory res;
11415
9291a0cd
TT
11416 /* Find the filename. Do not use dwarf2_name here, since the filename
11417 is not a source language identifier. */
d721ba37
PA
11418 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11419 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11420
d721ba37
PA
11421 if (res.comp_dir == NULL
11422 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11423 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11424 {
d721ba37
PA
11425 res.comp_dir_storage = ldirname (res.name);
11426 if (!res.comp_dir_storage.empty ())
11427 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11428 }
d721ba37 11429 if (res.comp_dir != NULL)
9291a0cd
TT
11430 {
11431 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11432 directory, get rid of it. */
d721ba37 11433 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11434
d721ba37
PA
11435 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11436 res.comp_dir = cp + 1;
9291a0cd
TT
11437 }
11438
d721ba37
PA
11439 if (res.name == NULL)
11440 res.name = "<unknown>";
11441
11442 return res;
9291a0cd
TT
11443}
11444
f4dc4d17
DE
11445/* Handle DW_AT_stmt_list for a compilation unit.
11446 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11447 COMP_DIR is the compilation directory. LOWPC is passed to
11448 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11449
11450static void
11451handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11452 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11453{
527f3840 11454 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11455 struct attribute *attr;
527f3840
JK
11456 struct line_header line_header_local;
11457 hashval_t line_header_local_hash;
527f3840
JK
11458 void **slot;
11459 int decode_mapping;
2ab95328 11460
f4dc4d17
DE
11461 gdb_assert (! cu->per_cu->is_debug_types);
11462
2ab95328 11463 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11464 if (attr == NULL)
11465 return;
11466
9c541725 11467 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11468
11469 /* The line header hash table is only created if needed (it exists to
11470 prevent redundant reading of the line table for partial_units).
11471 If we're given a partial_unit, we'll need it. If we're given a
11472 compile_unit, then use the line header hash table if it's already
11473 created, but don't create one just yet. */
11474
11475 if (dwarf2_per_objfile->line_header_hash == NULL
11476 && die->tag == DW_TAG_partial_unit)
2ab95328 11477 {
527f3840
JK
11478 dwarf2_per_objfile->line_header_hash
11479 = htab_create_alloc_ex (127, line_header_hash_voidp,
11480 line_header_eq_voidp,
11481 free_line_header_voidp,
11482 &objfile->objfile_obstack,
11483 hashtab_obstack_allocate,
11484 dummy_obstack_deallocate);
11485 }
2ab95328 11486
9c541725 11487 line_header_local.sect_off = line_offset;
527f3840
JK
11488 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11489 line_header_local_hash = line_header_hash (&line_header_local);
11490 if (dwarf2_per_objfile->line_header_hash != NULL)
11491 {
11492 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11493 &line_header_local,
11494 line_header_local_hash, NO_INSERT);
11495
11496 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11497 is not present in *SLOT (since if there is something in *SLOT then
11498 it will be for a partial_unit). */
11499 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11500 {
527f3840 11501 gdb_assert (*slot != NULL);
9a3c8263 11502 cu->line_header = (struct line_header *) *slot;
527f3840 11503 return;
dee91e82 11504 }
2ab95328 11505 }
527f3840
JK
11506
11507 /* dwarf_decode_line_header does not yet provide sufficient information.
11508 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11509 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11510 if (lh == NULL)
527f3840 11511 return;
4c8aa72d
PA
11512
11513 cu->line_header = lh.release ();
11514 cu->line_header_die_owner = die;
527f3840
JK
11515
11516 if (dwarf2_per_objfile->line_header_hash == NULL)
11517 slot = NULL;
11518 else
11519 {
11520 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11521 &line_header_local,
11522 line_header_local_hash, INSERT);
11523 gdb_assert (slot != NULL);
11524 }
11525 if (slot != NULL && *slot == NULL)
11526 {
11527 /* This newly decoded line number information unit will be owned
11528 by line_header_hash hash table. */
11529 *slot = cu->line_header;
4c8aa72d 11530 cu->line_header_die_owner = NULL;
527f3840
JK
11531 }
11532 else
11533 {
11534 /* We cannot free any current entry in (*slot) as that struct line_header
11535 may be already used by multiple CUs. Create only temporary decoded
11536 line_header for this CU - it may happen at most once for each line
11537 number information unit. And if we're not using line_header_hash
11538 then this is what we want as well. */
11539 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11540 }
11541 decode_mapping = (die->tag != DW_TAG_partial_unit);
11542 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11543 decode_mapping);
fff8551c 11544
2ab95328
TT
11545}
11546
95554aad 11547/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11548
c906108c 11549static void
e7c27a73 11550read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11551{
dee91e82 11552 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11553 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11554 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11555 CORE_ADDR highpc = ((CORE_ADDR) 0);
11556 struct attribute *attr;
c906108c 11557 struct die_info *child_die;
e142c38c 11558 CORE_ADDR baseaddr;
6e70227d 11559
e142c38c 11560 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11561
fae299cd 11562 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11563
11564 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11565 from finish_block. */
2acceee2 11566 if (lowpc == ((CORE_ADDR) -1))
c906108c 11567 lowpc = highpc;
3e29f34a 11568 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11569
d721ba37 11570 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11571
95554aad 11572 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 11573
f4b8a18d
KW
11574 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11575 standardised yet. As a workaround for the language detection we fall
11576 back to the DW_AT_producer string. */
11577 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11578 cu->language = language_opencl;
11579
3019eac3
DE
11580 /* Similar hack for Go. */
11581 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11582 set_cu_language (DW_LANG_Go, cu);
11583
d721ba37 11584 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11585
11586 /* Decode line number information if present. We do this before
11587 processing child DIEs, so that the line header table is available
11588 for DW_AT_decl_file. */
d721ba37 11589 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11590
11591 /* Process all dies in compilation unit. */
11592 if (die->child != NULL)
11593 {
11594 child_die = die->child;
11595 while (child_die && child_die->tag)
11596 {
11597 process_die (child_die, cu);
11598 child_die = sibling_die (child_die);
11599 }
11600 }
11601
11602 /* Decode macro information, if present. Dwarf 2 macro information
11603 refers to information in the line number info statement program
11604 header, so we can only read it if we've read the header
11605 successfully. */
0af92d60
JK
11606 attr = dwarf2_attr (die, DW_AT_macros, cu);
11607 if (attr == NULL)
11608 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11609 if (attr && cu->line_header)
11610 {
11611 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11612 complaint (&symfile_complaints,
0af92d60 11613 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11614
43f3e411 11615 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11616 }
11617 else
11618 {
11619 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11620 if (attr && cu->line_header)
11621 {
11622 unsigned int macro_offset = DW_UNSND (attr);
11623
43f3e411 11624 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11625 }
11626 }
3019eac3
DE
11627}
11628
f4dc4d17
DE
11629/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11630 Create the set of symtabs used by this TU, or if this TU is sharing
11631 symtabs with another TU and the symtabs have already been created
11632 then restore those symtabs in the line header.
11633 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
11634
11635static void
f4dc4d17 11636setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 11637{
f4dc4d17
DE
11638 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11639 struct type_unit_group *tu_group;
11640 int first_time;
3019eac3 11641 struct attribute *attr;
9c541725 11642 unsigned int i;
0186c6a7 11643 struct signatured_type *sig_type;
3019eac3 11644
f4dc4d17 11645 gdb_assert (per_cu->is_debug_types);
0186c6a7 11646 sig_type = (struct signatured_type *) per_cu;
3019eac3 11647
f4dc4d17 11648 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 11649
f4dc4d17 11650 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11651 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
11652 if (sig_type->type_unit_group == NULL)
11653 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11654 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11655
11656 /* If we've already processed this stmt_list there's no real need to
11657 do it again, we could fake it and just recreate the part we need
11658 (file name,index -> symtab mapping). If data shows this optimization
11659 is useful we can do it then. */
43f3e411 11660 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11661
11662 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11663 debug info. */
fff8551c 11664 line_header_up lh;
f4dc4d17 11665 if (attr != NULL)
3019eac3 11666 {
9c541725 11667 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
11668 lh = dwarf_decode_line_header (line_offset, cu);
11669 }
11670 if (lh == NULL)
11671 {
11672 if (first_time)
11673 dwarf2_start_symtab (cu, "", NULL, 0);
11674 else
11675 {
11676 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 11677 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11678 }
f4dc4d17 11679 return;
3019eac3
DE
11680 }
11681
4c8aa72d
PA
11682 cu->line_header = lh.release ();
11683 cu->line_header_die_owner = die;
3019eac3 11684
f4dc4d17
DE
11685 if (first_time)
11686 {
43f3e411 11687 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 11688
1fd60fc0
DE
11689 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11690 still initializing it, and our caller (a few levels up)
11691 process_full_type_unit still needs to know if this is the first
11692 time. */
11693
4c8aa72d
PA
11694 tu_group->num_symtabs = cu->line_header->file_names.size ();
11695 tu_group->symtabs = XNEWVEC (struct symtab *,
11696 cu->line_header->file_names.size ());
3019eac3 11697
4c8aa72d 11698 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11699 {
4c8aa72d 11700 file_entry &fe = cu->line_header->file_names[i];
3019eac3 11701
4c8aa72d 11702 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 11703
f4dc4d17
DE
11704 if (current_subfile->symtab == NULL)
11705 {
4c8aa72d
PA
11706 /* NOTE: start_subfile will recognize when it's been
11707 passed a file it has already seen. So we can't
11708 assume there's a simple mapping from
11709 cu->line_header->file_names to subfiles, plus
11710 cu->line_header->file_names may contain dups. */
43f3e411
DE
11711 current_subfile->symtab
11712 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
11713 }
11714
8c43009f
PA
11715 fe.symtab = current_subfile->symtab;
11716 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11717 }
11718 }
11719 else
3019eac3 11720 {
0ab9ce85 11721 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11722
4c8aa72d 11723 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11724 {
4c8aa72d 11725 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 11726
4c8aa72d 11727 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11728 }
3019eac3
DE
11729 }
11730
f4dc4d17
DE
11731 /* The main symtab is allocated last. Type units don't have DW_AT_name
11732 so they don't have a "real" (so to speak) symtab anyway.
11733 There is later code that will assign the main symtab to all symbols
11734 that don't have one. We need to handle the case of a symbol with a
11735 missing symtab (DW_AT_decl_file) anyway. */
11736}
3019eac3 11737
f4dc4d17
DE
11738/* Process DW_TAG_type_unit.
11739 For TUs we want to skip the first top level sibling if it's not the
11740 actual type being defined by this TU. In this case the first top
11741 level sibling is there to provide context only. */
3019eac3 11742
f4dc4d17
DE
11743static void
11744read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11745{
11746 struct die_info *child_die;
3019eac3 11747
f4dc4d17
DE
11748 prepare_one_comp_unit (cu, die, language_minimal);
11749
11750 /* Initialize (or reinitialize) the machinery for building symtabs.
11751 We do this before processing child DIEs, so that the line header table
11752 is available for DW_AT_decl_file. */
11753 setup_type_unit_groups (die, cu);
11754
11755 if (die->child != NULL)
11756 {
11757 child_die = die->child;
11758 while (child_die && child_die->tag)
11759 {
11760 process_die (child_die, cu);
11761 child_die = sibling_die (child_die);
11762 }
11763 }
3019eac3
DE
11764}
11765\f
80626a55
DE
11766/* DWO/DWP files.
11767
11768 http://gcc.gnu.org/wiki/DebugFission
11769 http://gcc.gnu.org/wiki/DebugFissionDWP
11770
11771 To simplify handling of both DWO files ("object" files with the DWARF info)
11772 and DWP files (a file with the DWOs packaged up into one file), we treat
11773 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11774
11775static hashval_t
11776hash_dwo_file (const void *item)
11777{
9a3c8263 11778 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11779 hashval_t hash;
3019eac3 11780
a2ce51a0
DE
11781 hash = htab_hash_string (dwo_file->dwo_name);
11782 if (dwo_file->comp_dir != NULL)
11783 hash += htab_hash_string (dwo_file->comp_dir);
11784 return hash;
3019eac3
DE
11785}
11786
11787static int
11788eq_dwo_file (const void *item_lhs, const void *item_rhs)
11789{
9a3c8263
SM
11790 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11791 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11792
a2ce51a0
DE
11793 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11794 return 0;
11795 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11796 return lhs->comp_dir == rhs->comp_dir;
11797 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11798}
11799
11800/* Allocate a hash table for DWO files. */
11801
11802static htab_t
11803allocate_dwo_file_hash_table (void)
11804{
11805 struct objfile *objfile = dwarf2_per_objfile->objfile;
11806
11807 return htab_create_alloc_ex (41,
11808 hash_dwo_file,
11809 eq_dwo_file,
11810 NULL,
11811 &objfile->objfile_obstack,
11812 hashtab_obstack_allocate,
11813 dummy_obstack_deallocate);
11814}
11815
80626a55
DE
11816/* Lookup DWO file DWO_NAME. */
11817
11818static void **
0ac5b59e 11819lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
11820{
11821 struct dwo_file find_entry;
11822 void **slot;
11823
11824 if (dwarf2_per_objfile->dwo_files == NULL)
11825 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
11826
11827 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
11828 find_entry.dwo_name = dwo_name;
11829 find_entry.comp_dir = comp_dir;
80626a55
DE
11830 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11831
11832 return slot;
11833}
11834
3019eac3
DE
11835static hashval_t
11836hash_dwo_unit (const void *item)
11837{
9a3c8263 11838 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11839
11840 /* This drops the top 32 bits of the id, but is ok for a hash. */
11841 return dwo_unit->signature;
11842}
11843
11844static int
11845eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11846{
9a3c8263
SM
11847 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11848 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11849
11850 /* The signature is assumed to be unique within the DWO file.
11851 So while object file CU dwo_id's always have the value zero,
11852 that's OK, assuming each object file DWO file has only one CU,
11853 and that's the rule for now. */
11854 return lhs->signature == rhs->signature;
11855}
11856
11857/* Allocate a hash table for DWO CUs,TUs.
11858 There is one of these tables for each of CUs,TUs for each DWO file. */
11859
11860static htab_t
11861allocate_dwo_unit_table (struct objfile *objfile)
11862{
11863 /* Start out with a pretty small number.
11864 Generally DWO files contain only one CU and maybe some TUs. */
11865 return htab_create_alloc_ex (3,
11866 hash_dwo_unit,
11867 eq_dwo_unit,
11868 NULL,
11869 &objfile->objfile_obstack,
11870 hashtab_obstack_allocate,
11871 dummy_obstack_deallocate);
11872}
11873
80626a55 11874/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11875
19c3d4c9 11876struct create_dwo_cu_data
3019eac3
DE
11877{
11878 struct dwo_file *dwo_file;
19c3d4c9 11879 struct dwo_unit dwo_unit;
3019eac3
DE
11880};
11881
19c3d4c9 11882/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11883
11884static void
19c3d4c9
DE
11885create_dwo_cu_reader (const struct die_reader_specs *reader,
11886 const gdb_byte *info_ptr,
11887 struct die_info *comp_unit_die,
11888 int has_children,
11889 void *datap)
3019eac3
DE
11890{
11891 struct dwarf2_cu *cu = reader->cu;
9c541725 11892 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11893 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11894 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11895 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11896 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11897 struct attribute *attr;
3019eac3
DE
11898
11899 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11900 if (attr == NULL)
11901 {
19c3d4c9
DE
11902 complaint (&symfile_complaints,
11903 _("Dwarf Error: debug entry at offset 0x%x is missing"
11904 " its dwo_id [in module %s]"),
9c541725 11905 to_underlying (sect_off), dwo_file->dwo_name);
3019eac3
DE
11906 return;
11907 }
11908
3019eac3
DE
11909 dwo_unit->dwo_file = dwo_file;
11910 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 11911 dwo_unit->section = section;
9c541725 11912 dwo_unit->sect_off = sect_off;
3019eac3
DE
11913 dwo_unit->length = cu->per_cu->length;
11914
b4f54984 11915 if (dwarf_read_debug)
4031ecc5 11916 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9c541725
PA
11917 to_underlying (sect_off),
11918 hex_string (dwo_unit->signature));
3019eac3
DE
11919}
11920
33c5cd75 11921/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11922 Note: This function processes DWO files only, not DWP files. */
3019eac3 11923
33c5cd75
DB
11924static void
11925create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
11926 htab_t &cus_htab)
3019eac3
DE
11927{
11928 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11929 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11930
33c5cd75
DB
11931 dwarf2_read_section (objfile, &section);
11932 info_ptr = section.buffer;
3019eac3
DE
11933
11934 if (info_ptr == NULL)
33c5cd75 11935 return;
3019eac3 11936
b4f54984 11937 if (dwarf_read_debug)
19c3d4c9
DE
11938 {
11939 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11940 get_section_name (&section),
11941 get_section_file_name (&section));
19c3d4c9 11942 }
3019eac3 11943
33c5cd75 11944 end_ptr = info_ptr + section.size;
3019eac3
DE
11945 while (info_ptr < end_ptr)
11946 {
11947 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
11948 struct create_dwo_cu_data create_dwo_cu_data;
11949 struct dwo_unit *dwo_unit;
11950 void **slot;
11951 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 11952
19c3d4c9
DE
11953 memset (&create_dwo_cu_data.dwo_unit, 0,
11954 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
11955 memset (&per_cu, 0, sizeof (per_cu));
11956 per_cu.objfile = objfile;
11957 per_cu.is_debug_types = 0;
33c5cd75
DB
11958 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11959 per_cu.section = &section;
c5ed0576 11960 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
11961
11962 init_cutu_and_read_dies_no_follow (
11963 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11964 info_ptr += per_cu.length;
11965
11966 // If the unit could not be parsed, skip it.
11967 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11968 continue;
3019eac3 11969
33c5cd75
DB
11970 if (cus_htab == NULL)
11971 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11972
33c5cd75
DB
11973 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11974 *dwo_unit = create_dwo_cu_data.dwo_unit;
11975 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11976 gdb_assert (slot != NULL);
11977 if (*slot != NULL)
19c3d4c9 11978 {
33c5cd75
DB
11979 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11980 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11981
33c5cd75
DB
11982 complaint (&symfile_complaints,
11983 _("debug cu entry at offset 0x%x is duplicate to"
11984 " the entry at offset 0x%x, signature %s"),
11985 to_underlying (sect_off), to_underlying (dup_sect_off),
11986 hex_string (dwo_unit->signature));
19c3d4c9 11987 }
33c5cd75 11988 *slot = (void *)dwo_unit;
3019eac3 11989 }
3019eac3
DE
11990}
11991
80626a55
DE
11992/* DWP file .debug_{cu,tu}_index section format:
11993 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11994
d2415c6c
DE
11995 DWP Version 1:
11996
80626a55
DE
11997 Both index sections have the same format, and serve to map a 64-bit
11998 signature to a set of section numbers. Each section begins with a header,
11999 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12000 indexes, and a pool of 32-bit section numbers. The index sections will be
12001 aligned at 8-byte boundaries in the file.
12002
d2415c6c
DE
12003 The index section header consists of:
12004
12005 V, 32 bit version number
12006 -, 32 bits unused
12007 N, 32 bit number of compilation units or type units in the index
12008 M, 32 bit number of slots in the hash table
80626a55 12009
d2415c6c 12010 Numbers are recorded using the byte order of the application binary.
80626a55 12011
d2415c6c
DE
12012 The hash table begins at offset 16 in the section, and consists of an array
12013 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12014 order of the application binary). Unused slots in the hash table are 0.
12015 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12016
d2415c6c
DE
12017 The parallel table begins immediately after the hash table
12018 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12019 array of 32-bit indexes (using the byte order of the application binary),
12020 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12021 table contains a 32-bit index into the pool of section numbers. For unused
12022 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12023
73869dc2
DE
12024 The pool of section numbers begins immediately following the hash table
12025 (at offset 16 + 12 * M from the beginning of the section). The pool of
12026 section numbers consists of an array of 32-bit words (using the byte order
12027 of the application binary). Each item in the array is indexed starting
12028 from 0. The hash table entry provides the index of the first section
12029 number in the set. Additional section numbers in the set follow, and the
12030 set is terminated by a 0 entry (section number 0 is not used in ELF).
12031
12032 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12033 section must be the first entry in the set, and the .debug_abbrev.dwo must
12034 be the second entry. Other members of the set may follow in any order.
12035
12036 ---
12037
12038 DWP Version 2:
12039
12040 DWP Version 2 combines all the .debug_info, etc. sections into one,
12041 and the entries in the index tables are now offsets into these sections.
12042 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12043 section.
12044
12045 Index Section Contents:
12046 Header
12047 Hash Table of Signatures dwp_hash_table.hash_table
12048 Parallel Table of Indices dwp_hash_table.unit_table
12049 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12050 Table of Section Sizes dwp_hash_table.v2.sizes
12051
12052 The index section header consists of:
12053
12054 V, 32 bit version number
12055 L, 32 bit number of columns in the table of section offsets
12056 N, 32 bit number of compilation units or type units in the index
12057 M, 32 bit number of slots in the hash table
12058
12059 Numbers are recorded using the byte order of the application binary.
12060
12061 The hash table has the same format as version 1.
12062 The parallel table of indices has the same format as version 1,
12063 except that the entries are origin-1 indices into the table of sections
12064 offsets and the table of section sizes.
12065
12066 The table of offsets begins immediately following the parallel table
12067 (at offset 16 + 12 * M from the beginning of the section). The table is
12068 a two-dimensional array of 32-bit words (using the byte order of the
12069 application binary), with L columns and N+1 rows, in row-major order.
12070 Each row in the array is indexed starting from 0. The first row provides
12071 a key to the remaining rows: each column in this row provides an identifier
12072 for a debug section, and the offsets in the same column of subsequent rows
12073 refer to that section. The section identifiers are:
12074
12075 DW_SECT_INFO 1 .debug_info.dwo
12076 DW_SECT_TYPES 2 .debug_types.dwo
12077 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12078 DW_SECT_LINE 4 .debug_line.dwo
12079 DW_SECT_LOC 5 .debug_loc.dwo
12080 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12081 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12082 DW_SECT_MACRO 8 .debug_macro.dwo
12083
12084 The offsets provided by the CU and TU index sections are the base offsets
12085 for the contributions made by each CU or TU to the corresponding section
12086 in the package file. Each CU and TU header contains an abbrev_offset
12087 field, used to find the abbreviations table for that CU or TU within the
12088 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12089 be interpreted as relative to the base offset given in the index section.
12090 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12091 should be interpreted as relative to the base offset for .debug_line.dwo,
12092 and offsets into other debug sections obtained from DWARF attributes should
12093 also be interpreted as relative to the corresponding base offset.
12094
12095 The table of sizes begins immediately following the table of offsets.
12096 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12097 with L columns and N rows, in row-major order. Each row in the array is
12098 indexed starting from 1 (row 0 is shared by the two tables).
12099
12100 ---
12101
12102 Hash table lookup is handled the same in version 1 and 2:
12103
12104 We assume that N and M will not exceed 2^32 - 1.
12105 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12106
d2415c6c
DE
12107 Given a 64-bit compilation unit signature or a type signature S, an entry
12108 in the hash table is located as follows:
80626a55 12109
d2415c6c
DE
12110 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12111 the low-order k bits all set to 1.
80626a55 12112
d2415c6c 12113 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12114
d2415c6c
DE
12115 3) If the hash table entry at index H matches the signature, use that
12116 entry. If the hash table entry at index H is unused (all zeroes),
12117 terminate the search: the signature is not present in the table.
80626a55 12118
d2415c6c 12119 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12120
d2415c6c 12121 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12122 to stop at an unused slot or find the match. */
80626a55
DE
12123
12124/* Create a hash table to map DWO IDs to their CU/TU entry in
12125 .debug_{info,types}.dwo in DWP_FILE.
12126 Returns NULL if there isn't one.
12127 Note: This function processes DWP files only, not DWO files. */
12128
12129static struct dwp_hash_table *
12130create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
12131{
12132 struct objfile *objfile = dwarf2_per_objfile->objfile;
12133 bfd *dbfd = dwp_file->dbfd;
948f8e3d 12134 const gdb_byte *index_ptr, *index_end;
80626a55 12135 struct dwarf2_section_info *index;
73869dc2 12136 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12137 struct dwp_hash_table *htab;
12138
12139 if (is_debug_types)
12140 index = &dwp_file->sections.tu_index;
12141 else
12142 index = &dwp_file->sections.cu_index;
12143
12144 if (dwarf2_section_empty_p (index))
12145 return NULL;
12146 dwarf2_read_section (objfile, index);
12147
12148 index_ptr = index->buffer;
12149 index_end = index_ptr + index->size;
12150
12151 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12152 index_ptr += 4;
12153 if (version == 2)
12154 nr_columns = read_4_bytes (dbfd, index_ptr);
12155 else
12156 nr_columns = 0;
12157 index_ptr += 4;
80626a55
DE
12158 nr_units = read_4_bytes (dbfd, index_ptr);
12159 index_ptr += 4;
12160 nr_slots = read_4_bytes (dbfd, index_ptr);
12161 index_ptr += 4;
12162
73869dc2 12163 if (version != 1 && version != 2)
80626a55 12164 {
21aa081e 12165 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12166 " [in module %s]"),
21aa081e 12167 pulongest (version), dwp_file->name);
80626a55
DE
12168 }
12169 if (nr_slots != (nr_slots & -nr_slots))
12170 {
21aa081e 12171 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12172 " is not power of 2 [in module %s]"),
21aa081e 12173 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12174 }
12175
12176 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12177 htab->version = version;
12178 htab->nr_columns = nr_columns;
80626a55
DE
12179 htab->nr_units = nr_units;
12180 htab->nr_slots = nr_slots;
12181 htab->hash_table = index_ptr;
12182 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12183
12184 /* Exit early if the table is empty. */
12185 if (nr_slots == 0 || nr_units == 0
12186 || (version == 2 && nr_columns == 0))
12187 {
12188 /* All must be zero. */
12189 if (nr_slots != 0 || nr_units != 0
12190 || (version == 2 && nr_columns != 0))
12191 {
12192 complaint (&symfile_complaints,
12193 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12194 " all zero [in modules %s]"),
12195 dwp_file->name);
12196 }
12197 return htab;
12198 }
12199
12200 if (version == 1)
12201 {
12202 htab->section_pool.v1.indices =
12203 htab->unit_table + sizeof (uint32_t) * nr_slots;
12204 /* It's harder to decide whether the section is too small in v1.
12205 V1 is deprecated anyway so we punt. */
12206 }
12207 else
12208 {
12209 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12210 int *ids = htab->section_pool.v2.section_ids;
12211 /* Reverse map for error checking. */
12212 int ids_seen[DW_SECT_MAX + 1];
12213 int i;
12214
12215 if (nr_columns < 2)
12216 {
12217 error (_("Dwarf Error: bad DWP hash table, too few columns"
12218 " in section table [in module %s]"),
12219 dwp_file->name);
12220 }
12221 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12222 {
12223 error (_("Dwarf Error: bad DWP hash table, too many columns"
12224 " in section table [in module %s]"),
12225 dwp_file->name);
12226 }
12227 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12228 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12229 for (i = 0; i < nr_columns; ++i)
12230 {
12231 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12232
12233 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12234 {
12235 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12236 " in section table [in module %s]"),
12237 id, dwp_file->name);
12238 }
12239 if (ids_seen[id] != -1)
12240 {
12241 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12242 " id %d in section table [in module %s]"),
12243 id, dwp_file->name);
12244 }
12245 ids_seen[id] = i;
12246 ids[i] = id;
12247 }
12248 /* Must have exactly one info or types section. */
12249 if (((ids_seen[DW_SECT_INFO] != -1)
12250 + (ids_seen[DW_SECT_TYPES] != -1))
12251 != 1)
12252 {
12253 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12254 " DWO info/types section [in module %s]"),
12255 dwp_file->name);
12256 }
12257 /* Must have an abbrev section. */
12258 if (ids_seen[DW_SECT_ABBREV] == -1)
12259 {
12260 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12261 " section [in module %s]"),
12262 dwp_file->name);
12263 }
12264 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12265 htab->section_pool.v2.sizes =
12266 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12267 * nr_units * nr_columns);
12268 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12269 * nr_units * nr_columns))
12270 > index_end)
12271 {
12272 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12273 " [in module %s]"),
12274 dwp_file->name);
12275 }
12276 }
80626a55
DE
12277
12278 return htab;
12279}
12280
12281/* Update SECTIONS with the data from SECTP.
12282
12283 This function is like the other "locate" section routines that are
12284 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12285 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12286
12287 The result is non-zero for success, or zero if an error was found. */
12288
12289static int
73869dc2
DE
12290locate_v1_virtual_dwo_sections (asection *sectp,
12291 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12292{
12293 const struct dwop_section_names *names = &dwop_section_names;
12294
12295 if (section_is_p (sectp->name, &names->abbrev_dwo))
12296 {
12297 /* There can be only one. */
049412e3 12298 if (sections->abbrev.s.section != NULL)
80626a55 12299 return 0;
049412e3 12300 sections->abbrev.s.section = sectp;
80626a55
DE
12301 sections->abbrev.size = bfd_get_section_size (sectp);
12302 }
12303 else if (section_is_p (sectp->name, &names->info_dwo)
12304 || section_is_p (sectp->name, &names->types_dwo))
12305 {
12306 /* There can be only one. */
049412e3 12307 if (sections->info_or_types.s.section != NULL)
80626a55 12308 return 0;
049412e3 12309 sections->info_or_types.s.section = sectp;
80626a55
DE
12310 sections->info_or_types.size = bfd_get_section_size (sectp);
12311 }
12312 else if (section_is_p (sectp->name, &names->line_dwo))
12313 {
12314 /* There can be only one. */
049412e3 12315 if (sections->line.s.section != NULL)
80626a55 12316 return 0;
049412e3 12317 sections->line.s.section = sectp;
80626a55
DE
12318 sections->line.size = bfd_get_section_size (sectp);
12319 }
12320 else if (section_is_p (sectp->name, &names->loc_dwo))
12321 {
12322 /* There can be only one. */
049412e3 12323 if (sections->loc.s.section != NULL)
80626a55 12324 return 0;
049412e3 12325 sections->loc.s.section = sectp;
80626a55
DE
12326 sections->loc.size = bfd_get_section_size (sectp);
12327 }
12328 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12329 {
12330 /* There can be only one. */
049412e3 12331 if (sections->macinfo.s.section != NULL)
80626a55 12332 return 0;
049412e3 12333 sections->macinfo.s.section = sectp;
80626a55
DE
12334 sections->macinfo.size = bfd_get_section_size (sectp);
12335 }
12336 else if (section_is_p (sectp->name, &names->macro_dwo))
12337 {
12338 /* There can be only one. */
049412e3 12339 if (sections->macro.s.section != NULL)
80626a55 12340 return 0;
049412e3 12341 sections->macro.s.section = sectp;
80626a55
DE
12342 sections->macro.size = bfd_get_section_size (sectp);
12343 }
12344 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12345 {
12346 /* There can be only one. */
049412e3 12347 if (sections->str_offsets.s.section != NULL)
80626a55 12348 return 0;
049412e3 12349 sections->str_offsets.s.section = sectp;
80626a55
DE
12350 sections->str_offsets.size = bfd_get_section_size (sectp);
12351 }
12352 else
12353 {
12354 /* No other kind of section is valid. */
12355 return 0;
12356 }
12357
12358 return 1;
12359}
12360
73869dc2
DE
12361/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12362 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12363 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12364 This is for DWP version 1 files. */
80626a55
DE
12365
12366static struct dwo_unit *
73869dc2
DE
12367create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
12368 uint32_t unit_index,
12369 const char *comp_dir,
12370 ULONGEST signature, int is_debug_types)
80626a55
DE
12371{
12372 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12373 const struct dwp_hash_table *dwp_htab =
12374 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
12375 bfd *dbfd = dwp_file->dbfd;
12376 const char *kind = is_debug_types ? "TU" : "CU";
12377 struct dwo_file *dwo_file;
12378 struct dwo_unit *dwo_unit;
73869dc2 12379 struct virtual_v1_dwo_sections sections;
80626a55 12380 void **dwo_file_slot;
80626a55
DE
12381 int i;
12382
73869dc2
DE
12383 gdb_assert (dwp_file->version == 1);
12384
b4f54984 12385 if (dwarf_read_debug)
80626a55 12386 {
73869dc2 12387 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12388 kind,
73869dc2 12389 pulongest (unit_index), hex_string (signature),
80626a55
DE
12390 dwp_file->name);
12391 }
12392
19ac8c2e 12393 /* Fetch the sections of this DWO unit.
80626a55
DE
12394 Put a limit on the number of sections we look for so that bad data
12395 doesn't cause us to loop forever. */
12396
73869dc2 12397#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12398 (1 /* .debug_info or .debug_types */ \
12399 + 1 /* .debug_abbrev */ \
12400 + 1 /* .debug_line */ \
12401 + 1 /* .debug_loc */ \
12402 + 1 /* .debug_str_offsets */ \
19ac8c2e 12403 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12404 + 1 /* trailing zero */)
12405
12406 memset (&sections, 0, sizeof (sections));
80626a55 12407
73869dc2 12408 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12409 {
12410 asection *sectp;
12411 uint32_t section_nr =
12412 read_4_bytes (dbfd,
73869dc2
DE
12413 dwp_htab->section_pool.v1.indices
12414 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12415
12416 if (section_nr == 0)
12417 break;
12418 if (section_nr >= dwp_file->num_sections)
12419 {
12420 error (_("Dwarf Error: bad DWP hash table, section number too large"
12421 " [in module %s]"),
12422 dwp_file->name);
12423 }
12424
12425 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12426 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12427 {
12428 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12429 " [in module %s]"),
12430 dwp_file->name);
12431 }
12432 }
12433
12434 if (i < 2
a32a8923
DE
12435 || dwarf2_section_empty_p (&sections.info_or_types)
12436 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12437 {
12438 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12439 " [in module %s]"),
12440 dwp_file->name);
12441 }
73869dc2 12442 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12443 {
12444 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12445 " [in module %s]"),
12446 dwp_file->name);
12447 }
12448
12449 /* It's easier for the rest of the code if we fake a struct dwo_file and
12450 have dwo_unit "live" in that. At least for now.
12451
12452 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12453 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12454 file, we can combine them back into a virtual DWO file to save space
12455 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12456 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12457
791afaa2
TT
12458 std::string virtual_dwo_name =
12459 string_printf ("virtual-dwo/%d-%d-%d-%d",
12460 get_section_id (&sections.abbrev),
12461 get_section_id (&sections.line),
12462 get_section_id (&sections.loc),
12463 get_section_id (&sections.str_offsets));
80626a55 12464 /* Can we use an existing virtual DWO file? */
791afaa2 12465 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
80626a55
DE
12466 /* Create one if necessary. */
12467 if (*dwo_file_slot == NULL)
12468 {
b4f54984 12469 if (dwarf_read_debug)
80626a55
DE
12470 {
12471 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12472 virtual_dwo_name.c_str ());
80626a55
DE
12473 }
12474 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12475 dwo_file->dwo_name
12476 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12477 virtual_dwo_name.c_str (),
12478 virtual_dwo_name.size ());
0ac5b59e 12479 dwo_file->comp_dir = comp_dir;
80626a55
DE
12480 dwo_file->sections.abbrev = sections.abbrev;
12481 dwo_file->sections.line = sections.line;
12482 dwo_file->sections.loc = sections.loc;
12483 dwo_file->sections.macinfo = sections.macinfo;
12484 dwo_file->sections.macro = sections.macro;
12485 dwo_file->sections.str_offsets = sections.str_offsets;
12486 /* The "str" section is global to the entire DWP file. */
12487 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12488 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12489 there's no need to record it in dwo_file.
12490 Also, we can't simply record type sections in dwo_file because
12491 we record a pointer into the vector in dwo_unit. As we collect more
12492 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12493 for it, invalidating all copies of pointers into the previous
12494 contents. */
80626a55
DE
12495 *dwo_file_slot = dwo_file;
12496 }
12497 else
12498 {
b4f54984 12499 if (dwarf_read_debug)
80626a55
DE
12500 {
12501 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12502 virtual_dwo_name.c_str ());
80626a55 12503 }
9a3c8263 12504 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12505 }
80626a55
DE
12506
12507 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12508 dwo_unit->dwo_file = dwo_file;
12509 dwo_unit->signature = signature;
8d749320
SM
12510 dwo_unit->section =
12511 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12512 *dwo_unit->section = sections.info_or_types;
57d63ce2 12513 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12514
12515 return dwo_unit;
12516}
12517
73869dc2
DE
12518/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12519 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12520 piece within that section used by a TU/CU, return a virtual section
12521 of just that piece. */
12522
12523static struct dwarf2_section_info
12524create_dwp_v2_section (struct dwarf2_section_info *section,
12525 bfd_size_type offset, bfd_size_type size)
12526{
12527 struct dwarf2_section_info result;
12528 asection *sectp;
12529
12530 gdb_assert (section != NULL);
12531 gdb_assert (!section->is_virtual);
12532
12533 memset (&result, 0, sizeof (result));
12534 result.s.containing_section = section;
12535 result.is_virtual = 1;
12536
12537 if (size == 0)
12538 return result;
12539
12540 sectp = get_section_bfd_section (section);
12541
12542 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12543 bounds of the real section. This is a pretty-rare event, so just
12544 flag an error (easier) instead of a warning and trying to cope. */
12545 if (sectp == NULL
12546 || offset + size > bfd_get_section_size (sectp))
12547 {
73869dc2
DE
12548 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12549 " in section %s [in module %s]"),
12550 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12551 objfile_name (dwarf2_per_objfile->objfile));
12552 }
12553
12554 result.virtual_offset = offset;
12555 result.size = size;
12556 return result;
12557}
12558
12559/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12560 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12561 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12562 This is for DWP version 2 files. */
12563
12564static struct dwo_unit *
12565create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
12566 uint32_t unit_index,
12567 const char *comp_dir,
12568 ULONGEST signature, int is_debug_types)
12569{
12570 struct objfile *objfile = dwarf2_per_objfile->objfile;
12571 const struct dwp_hash_table *dwp_htab =
12572 is_debug_types ? dwp_file->tus : dwp_file->cus;
12573 bfd *dbfd = dwp_file->dbfd;
12574 const char *kind = is_debug_types ? "TU" : "CU";
12575 struct dwo_file *dwo_file;
12576 struct dwo_unit *dwo_unit;
12577 struct virtual_v2_dwo_sections sections;
12578 void **dwo_file_slot;
73869dc2
DE
12579 int i;
12580
12581 gdb_assert (dwp_file->version == 2);
12582
b4f54984 12583 if (dwarf_read_debug)
73869dc2
DE
12584 {
12585 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12586 kind,
12587 pulongest (unit_index), hex_string (signature),
12588 dwp_file->name);
12589 }
12590
12591 /* Fetch the section offsets of this DWO unit. */
12592
12593 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12594
12595 for (i = 0; i < dwp_htab->nr_columns; ++i)
12596 {
12597 uint32_t offset = read_4_bytes (dbfd,
12598 dwp_htab->section_pool.v2.offsets
12599 + (((unit_index - 1) * dwp_htab->nr_columns
12600 + i)
12601 * sizeof (uint32_t)));
12602 uint32_t size = read_4_bytes (dbfd,
12603 dwp_htab->section_pool.v2.sizes
12604 + (((unit_index - 1) * dwp_htab->nr_columns
12605 + i)
12606 * sizeof (uint32_t)));
12607
12608 switch (dwp_htab->section_pool.v2.section_ids[i])
12609 {
12610 case DW_SECT_INFO:
12611 case DW_SECT_TYPES:
12612 sections.info_or_types_offset = offset;
12613 sections.info_or_types_size = size;
12614 break;
12615 case DW_SECT_ABBREV:
12616 sections.abbrev_offset = offset;
12617 sections.abbrev_size = size;
12618 break;
12619 case DW_SECT_LINE:
12620 sections.line_offset = offset;
12621 sections.line_size = size;
12622 break;
12623 case DW_SECT_LOC:
12624 sections.loc_offset = offset;
12625 sections.loc_size = size;
12626 break;
12627 case DW_SECT_STR_OFFSETS:
12628 sections.str_offsets_offset = offset;
12629 sections.str_offsets_size = size;
12630 break;
12631 case DW_SECT_MACINFO:
12632 sections.macinfo_offset = offset;
12633 sections.macinfo_size = size;
12634 break;
12635 case DW_SECT_MACRO:
12636 sections.macro_offset = offset;
12637 sections.macro_size = size;
12638 break;
12639 }
12640 }
12641
12642 /* It's easier for the rest of the code if we fake a struct dwo_file and
12643 have dwo_unit "live" in that. At least for now.
12644
12645 The DWP file can be made up of a random collection of CUs and TUs.
12646 However, for each CU + set of TUs that came from the same original DWO
12647 file, we can combine them back into a virtual DWO file to save space
12648 (fewer struct dwo_file objects to allocate). Remember that for really
12649 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12650
791afaa2
TT
12651 std::string virtual_dwo_name =
12652 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12653 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12654 (long) (sections.line_size ? sections.line_offset : 0),
12655 (long) (sections.loc_size ? sections.loc_offset : 0),
12656 (long) (sections.str_offsets_size
12657 ? sections.str_offsets_offset : 0));
73869dc2 12658 /* Can we use an existing virtual DWO file? */
791afaa2 12659 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
73869dc2
DE
12660 /* Create one if necessary. */
12661 if (*dwo_file_slot == NULL)
12662 {
b4f54984 12663 if (dwarf_read_debug)
73869dc2
DE
12664 {
12665 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12666 virtual_dwo_name.c_str ());
73869dc2
DE
12667 }
12668 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12669 dwo_file->dwo_name
12670 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12671 virtual_dwo_name.c_str (),
12672 virtual_dwo_name.size ());
73869dc2
DE
12673 dwo_file->comp_dir = comp_dir;
12674 dwo_file->sections.abbrev =
12675 create_dwp_v2_section (&dwp_file->sections.abbrev,
12676 sections.abbrev_offset, sections.abbrev_size);
12677 dwo_file->sections.line =
12678 create_dwp_v2_section (&dwp_file->sections.line,
12679 sections.line_offset, sections.line_size);
12680 dwo_file->sections.loc =
12681 create_dwp_v2_section (&dwp_file->sections.loc,
12682 sections.loc_offset, sections.loc_size);
12683 dwo_file->sections.macinfo =
12684 create_dwp_v2_section (&dwp_file->sections.macinfo,
12685 sections.macinfo_offset, sections.macinfo_size);
12686 dwo_file->sections.macro =
12687 create_dwp_v2_section (&dwp_file->sections.macro,
12688 sections.macro_offset, sections.macro_size);
12689 dwo_file->sections.str_offsets =
12690 create_dwp_v2_section (&dwp_file->sections.str_offsets,
12691 sections.str_offsets_offset,
12692 sections.str_offsets_size);
12693 /* The "str" section is global to the entire DWP file. */
12694 dwo_file->sections.str = dwp_file->sections.str;
12695 /* The info or types section is assigned below to dwo_unit,
12696 there's no need to record it in dwo_file.
12697 Also, we can't simply record type sections in dwo_file because
12698 we record a pointer into the vector in dwo_unit. As we collect more
12699 types we'll grow the vector and eventually have to reallocate space
12700 for it, invalidating all copies of pointers into the previous
12701 contents. */
12702 *dwo_file_slot = dwo_file;
12703 }
12704 else
12705 {
b4f54984 12706 if (dwarf_read_debug)
73869dc2
DE
12707 {
12708 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12709 virtual_dwo_name.c_str ());
73869dc2 12710 }
9a3c8263 12711 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12712 }
73869dc2
DE
12713
12714 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12715 dwo_unit->dwo_file = dwo_file;
12716 dwo_unit->signature = signature;
8d749320
SM
12717 dwo_unit->section =
12718 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
73869dc2
DE
12719 *dwo_unit->section = create_dwp_v2_section (is_debug_types
12720 ? &dwp_file->sections.types
12721 : &dwp_file->sections.info,
12722 sections.info_or_types_offset,
12723 sections.info_or_types_size);
12724 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12725
12726 return dwo_unit;
12727}
12728
57d63ce2
DE
12729/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12730 Returns NULL if the signature isn't found. */
80626a55
DE
12731
12732static struct dwo_unit *
57d63ce2
DE
12733lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
12734 ULONGEST signature, int is_debug_types)
80626a55 12735{
57d63ce2
DE
12736 const struct dwp_hash_table *dwp_htab =
12737 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 12738 bfd *dbfd = dwp_file->dbfd;
57d63ce2 12739 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12740 uint32_t hash = signature & mask;
12741 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12742 unsigned int i;
12743 void **slot;
870f88f7 12744 struct dwo_unit find_dwo_cu;
80626a55
DE
12745
12746 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12747 find_dwo_cu.signature = signature;
19ac8c2e
DE
12748 slot = htab_find_slot (is_debug_types
12749 ? dwp_file->loaded_tus
12750 : dwp_file->loaded_cus,
12751 &find_dwo_cu, INSERT);
80626a55
DE
12752
12753 if (*slot != NULL)
9a3c8263 12754 return (struct dwo_unit *) *slot;
80626a55
DE
12755
12756 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12757 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12758 {
12759 ULONGEST signature_in_table;
12760
12761 signature_in_table =
57d63ce2 12762 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12763 if (signature_in_table == signature)
12764 {
57d63ce2
DE
12765 uint32_t unit_index =
12766 read_4_bytes (dbfd,
12767 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12768
73869dc2
DE
12769 if (dwp_file->version == 1)
12770 {
12771 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
12772 comp_dir, signature,
12773 is_debug_types);
12774 }
12775 else
12776 {
12777 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
12778 comp_dir, signature,
12779 is_debug_types);
12780 }
9a3c8263 12781 return (struct dwo_unit *) *slot;
80626a55
DE
12782 }
12783 if (signature_in_table == 0)
12784 return NULL;
12785 hash = (hash + hash2) & mask;
12786 }
12787
12788 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12789 " [in module %s]"),
12790 dwp_file->name);
12791}
12792
ab5088bf 12793/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12794 Open the file specified by FILE_NAME and hand it off to BFD for
12795 preliminary analysis. Return a newly initialized bfd *, which
12796 includes a canonicalized copy of FILE_NAME.
80626a55 12797 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12798 SEARCH_CWD is true if the current directory is to be searched.
12799 It will be searched before debug-file-directory.
13aaf454
DE
12800 If successful, the file is added to the bfd include table of the
12801 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12802 If unable to find/open the file, return NULL.
3019eac3
DE
12803 NOTE: This function is derived from symfile_bfd_open. */
12804
192b62ce 12805static gdb_bfd_ref_ptr
6ac97d4c 12806try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3 12807{
80626a55 12808 int desc, flags;
3019eac3 12809 char *absolute_name;
9c02c129
DE
12810 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12811 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12812 to debug_file_directory. */
12813 char *search_path;
12814 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12815
6ac97d4c
DE
12816 if (search_cwd)
12817 {
12818 if (*debug_file_directory != '\0')
12819 search_path = concat (".", dirname_separator_string,
b36cec19 12820 debug_file_directory, (char *) NULL);
6ac97d4c
DE
12821 else
12822 search_path = xstrdup (".");
12823 }
9c02c129 12824 else
6ac97d4c 12825 search_path = xstrdup (debug_file_directory);
3019eac3 12826
492c0ab7 12827 flags = OPF_RETURN_REALPATH;
80626a55
DE
12828 if (is_dwp)
12829 flags |= OPF_SEARCH_IN_PATH;
9c02c129 12830 desc = openp (search_path, flags, file_name,
3019eac3 12831 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 12832 xfree (search_path);
3019eac3
DE
12833 if (desc < 0)
12834 return NULL;
12835
192b62ce 12836 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 12837 xfree (absolute_name);
9c02c129
DE
12838 if (sym_bfd == NULL)
12839 return NULL;
192b62ce 12840 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12841
192b62ce
TT
12842 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12843 return NULL;
3019eac3 12844
13aaf454
DE
12845 /* Success. Record the bfd as having been included by the objfile's bfd.
12846 This is important because things like demangled_names_hash lives in the
12847 objfile's per_bfd space and may have references to things like symbol
12848 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12849 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12850
3019eac3
DE
12851 return sym_bfd;
12852}
12853
ab5088bf 12854/* Try to open DWO file FILE_NAME.
3019eac3
DE
12855 COMP_DIR is the DW_AT_comp_dir attribute.
12856 The result is the bfd handle of the file.
12857 If there is a problem finding or opening the file, return NULL.
12858 Upon success, the canonicalized path of the file is stored in the bfd,
12859 same as symfile_bfd_open. */
12860
192b62ce 12861static gdb_bfd_ref_ptr
ab5088bf 12862open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3 12863{
80626a55 12864 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 12865 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12866
12867 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12868
12869 if (comp_dir != NULL)
12870 {
b36cec19
PA
12871 char *path_to_try = concat (comp_dir, SLASH_STRING,
12872 file_name, (char *) NULL);
3019eac3
DE
12873
12874 /* NOTE: If comp_dir is a relative path, this will also try the
12875 search path, which seems useful. */
192b62ce
TT
12876 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
12877 1 /*search_cwd*/));
3019eac3
DE
12878 xfree (path_to_try);
12879 if (abfd != NULL)
12880 return abfd;
12881 }
12882
12883 /* That didn't work, try debug-file-directory, which, despite its name,
12884 is a list of paths. */
12885
12886 if (*debug_file_directory == '\0')
12887 return NULL;
12888
6ac97d4c 12889 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12890}
12891
80626a55
DE
12892/* This function is mapped across the sections and remembers the offset and
12893 size of each of the DWO debugging sections we are interested in. */
12894
12895static void
12896dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12897{
9a3c8263 12898 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12899 const struct dwop_section_names *names = &dwop_section_names;
12900
12901 if (section_is_p (sectp->name, &names->abbrev_dwo))
12902 {
049412e3 12903 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
12904 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12905 }
12906 else if (section_is_p (sectp->name, &names->info_dwo))
12907 {
049412e3 12908 dwo_sections->info.s.section = sectp;
80626a55
DE
12909 dwo_sections->info.size = bfd_get_section_size (sectp);
12910 }
12911 else if (section_is_p (sectp->name, &names->line_dwo))
12912 {
049412e3 12913 dwo_sections->line.s.section = sectp;
80626a55
DE
12914 dwo_sections->line.size = bfd_get_section_size (sectp);
12915 }
12916 else if (section_is_p (sectp->name, &names->loc_dwo))
12917 {
049412e3 12918 dwo_sections->loc.s.section = sectp;
80626a55
DE
12919 dwo_sections->loc.size = bfd_get_section_size (sectp);
12920 }
12921 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12922 {
049412e3 12923 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
12924 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12925 }
12926 else if (section_is_p (sectp->name, &names->macro_dwo))
12927 {
049412e3 12928 dwo_sections->macro.s.section = sectp;
80626a55
DE
12929 dwo_sections->macro.size = bfd_get_section_size (sectp);
12930 }
12931 else if (section_is_p (sectp->name, &names->str_dwo))
12932 {
049412e3 12933 dwo_sections->str.s.section = sectp;
80626a55
DE
12934 dwo_sections->str.size = bfd_get_section_size (sectp);
12935 }
12936 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12937 {
049412e3 12938 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
12939 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12940 }
12941 else if (section_is_p (sectp->name, &names->types_dwo))
12942 {
12943 struct dwarf2_section_info type_section;
12944
12945 memset (&type_section, 0, sizeof (type_section));
049412e3 12946 type_section.s.section = sectp;
80626a55
DE
12947 type_section.size = bfd_get_section_size (sectp);
12948 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12949 &type_section);
12950 }
12951}
12952
ab5088bf 12953/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12954 by PER_CU. This is for the non-DWP case.
80626a55 12955 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12956
12957static struct dwo_file *
0ac5b59e
DE
12958open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12959 const char *dwo_name, const char *comp_dir)
3019eac3
DE
12960{
12961 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12962 struct dwo_file *dwo_file;
3019eac3
DE
12963 struct cleanup *cleanups;
12964
192b62ce 12965 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
80626a55
DE
12966 if (dbfd == NULL)
12967 {
b4f54984 12968 if (dwarf_read_debug)
80626a55
DE
12969 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12970 return NULL;
12971 }
12972 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
12973 dwo_file->dwo_name = dwo_name;
12974 dwo_file->comp_dir = comp_dir;
192b62ce 12975 dwo_file->dbfd = dbfd.release ();
3019eac3
DE
12976
12977 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
12978
192b62ce
TT
12979 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
12980 &dwo_file->sections);
3019eac3 12981
33c5cd75 12982 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
3019eac3 12983
78d4d2c5
JK
12984 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
12985 dwo_file->tus);
3019eac3
DE
12986
12987 discard_cleanups (cleanups);
12988
b4f54984 12989 if (dwarf_read_debug)
80626a55
DE
12990 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12991
3019eac3
DE
12992 return dwo_file;
12993}
12994
80626a55 12995/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12996 size of each of the DWP debugging sections common to version 1 and 2 that
12997 we are interested in. */
3019eac3 12998
80626a55 12999static void
73869dc2
DE
13000dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13001 void *dwp_file_ptr)
3019eac3 13002{
9a3c8263 13003 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13004 const struct dwop_section_names *names = &dwop_section_names;
13005 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13006
80626a55 13007 /* Record the ELF section number for later lookup: this is what the
73869dc2 13008 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13009 gdb_assert (elf_section_nr < dwp_file->num_sections);
13010 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13011
80626a55
DE
13012 /* Look for specific sections that we need. */
13013 if (section_is_p (sectp->name, &names->str_dwo))
13014 {
049412e3 13015 dwp_file->sections.str.s.section = sectp;
80626a55
DE
13016 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13017 }
13018 else if (section_is_p (sectp->name, &names->cu_index))
13019 {
049412e3 13020 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
13021 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13022 }
13023 else if (section_is_p (sectp->name, &names->tu_index))
13024 {
049412e3 13025 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
13026 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13027 }
13028}
3019eac3 13029
73869dc2
DE
13030/* This function is mapped across the sections and remembers the offset and
13031 size of each of the DWP version 2 debugging sections that we are interested
13032 in. This is split into a separate function because we don't know if we
13033 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13034
13035static void
13036dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13037{
9a3c8263 13038 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13039 const struct dwop_section_names *names = &dwop_section_names;
13040 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13041
13042 /* Record the ELF section number for later lookup: this is what the
13043 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13044 gdb_assert (elf_section_nr < dwp_file->num_sections);
13045 dwp_file->elf_sections[elf_section_nr] = sectp;
13046
13047 /* Look for specific sections that we need. */
13048 if (section_is_p (sectp->name, &names->abbrev_dwo))
13049 {
049412e3 13050 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
13051 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13052 }
13053 else if (section_is_p (sectp->name, &names->info_dwo))
13054 {
049412e3 13055 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
13056 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13057 }
13058 else if (section_is_p (sectp->name, &names->line_dwo))
13059 {
049412e3 13060 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
13061 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13062 }
13063 else if (section_is_p (sectp->name, &names->loc_dwo))
13064 {
049412e3 13065 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
13066 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13067 }
13068 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13069 {
049412e3 13070 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
13071 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13072 }
13073 else if (section_is_p (sectp->name, &names->macro_dwo))
13074 {
049412e3 13075 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
13076 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13077 }
13078 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13079 {
049412e3 13080 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
13081 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13082 }
13083 else if (section_is_p (sectp->name, &names->types_dwo))
13084 {
049412e3 13085 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
13086 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13087 }
13088}
13089
80626a55 13090/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13091
80626a55
DE
13092static hashval_t
13093hash_dwp_loaded_cutus (const void *item)
13094{
9a3c8263 13095 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13096
80626a55
DE
13097 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13098 return dwo_unit->signature;
3019eac3
DE
13099}
13100
80626a55 13101/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13102
80626a55
DE
13103static int
13104eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13105{
9a3c8263
SM
13106 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13107 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13108
80626a55
DE
13109 return dua->signature == dub->signature;
13110}
3019eac3 13111
80626a55 13112/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13113
80626a55
DE
13114static htab_t
13115allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13116{
13117 return htab_create_alloc_ex (3,
13118 hash_dwp_loaded_cutus,
13119 eq_dwp_loaded_cutus,
13120 NULL,
13121 &objfile->objfile_obstack,
13122 hashtab_obstack_allocate,
13123 dummy_obstack_deallocate);
13124}
3019eac3 13125
ab5088bf
DE
13126/* Try to open DWP file FILE_NAME.
13127 The result is the bfd handle of the file.
13128 If there is a problem finding or opening the file, return NULL.
13129 Upon success, the canonicalized path of the file is stored in the bfd,
13130 same as symfile_bfd_open. */
13131
192b62ce 13132static gdb_bfd_ref_ptr
ab5088bf
DE
13133open_dwp_file (const char *file_name)
13134{
192b62ce
TT
13135 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
13136 1 /*search_cwd*/));
6ac97d4c
DE
13137 if (abfd != NULL)
13138 return abfd;
13139
13140 /* Work around upstream bug 15652.
13141 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13142 [Whether that's a "bug" is debatable, but it is getting in our way.]
13143 We have no real idea where the dwp file is, because gdb's realpath-ing
13144 of the executable's path may have discarded the needed info.
13145 [IWBN if the dwp file name was recorded in the executable, akin to
13146 .gnu_debuglink, but that doesn't exist yet.]
13147 Strip the directory from FILE_NAME and search again. */
13148 if (*debug_file_directory != '\0')
13149 {
13150 /* Don't implicitly search the current directory here.
13151 If the user wants to search "." to handle this case,
13152 it must be added to debug-file-directory. */
13153 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
13154 0 /*search_cwd*/);
13155 }
13156
13157 return NULL;
ab5088bf
DE
13158}
13159
80626a55
DE
13160/* Initialize the use of the DWP file for the current objfile.
13161 By convention the name of the DWP file is ${objfile}.dwp.
13162 The result is NULL if it can't be found. */
a766d390 13163
80626a55 13164static struct dwp_file *
ab5088bf 13165open_and_init_dwp_file (void)
80626a55
DE
13166{
13167 struct objfile *objfile = dwarf2_per_objfile->objfile;
13168 struct dwp_file *dwp_file;
80626a55 13169
82bf32bc
JK
13170 /* Try to find first .dwp for the binary file before any symbolic links
13171 resolving. */
6c447423
DE
13172
13173 /* If the objfile is a debug file, find the name of the real binary
13174 file and get the name of dwp file from there. */
d721ba37 13175 std::string dwp_name;
6c447423
DE
13176 if (objfile->separate_debug_objfile_backlink != NULL)
13177 {
13178 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13179 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13180
d721ba37 13181 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13182 }
13183 else
d721ba37
PA
13184 dwp_name = objfile->original_name;
13185
13186 dwp_name += ".dwp";
80626a55 13187
d721ba37 13188 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
82bf32bc
JK
13189 if (dbfd == NULL
13190 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13191 {
13192 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13193 dwp_name = objfile_name (objfile);
13194 dwp_name += ".dwp";
13195 dbfd = open_dwp_file (dwp_name.c_str ());
82bf32bc
JK
13196 }
13197
80626a55
DE
13198 if (dbfd == NULL)
13199 {
b4f54984 13200 if (dwarf_read_debug)
d721ba37 13201 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 13202 return NULL;
3019eac3 13203 }
80626a55 13204 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
13205 dwp_file->name = bfd_get_filename (dbfd.get ());
13206 dwp_file->dbfd = dbfd.release ();
c906108c 13207
80626a55 13208 /* +1: section 0 is unused */
192b62ce 13209 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
13210 dwp_file->elf_sections =
13211 OBSTACK_CALLOC (&objfile->objfile_obstack,
13212 dwp_file->num_sections, asection *);
13213
192b62ce
TT
13214 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13215 dwp_file);
80626a55
DE
13216
13217 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
13218
13219 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
13220
73869dc2 13221 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13222 if (dwp_file->cus && dwp_file->tus
13223 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13224 {
13225 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13226 pretty bizarre. We use pulongest here because that's the established
4d65956b 13227 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13228 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13229 " TU version %s [in DWP file %s]"),
13230 pulongest (dwp_file->cus->version),
d721ba37 13231 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13232 }
08302ed2
DE
13233
13234 if (dwp_file->cus)
13235 dwp_file->version = dwp_file->cus->version;
13236 else if (dwp_file->tus)
13237 dwp_file->version = dwp_file->tus->version;
13238 else
13239 dwp_file->version = 2;
73869dc2
DE
13240
13241 if (dwp_file->version == 2)
192b62ce
TT
13242 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13243 dwp_file);
73869dc2 13244
19ac8c2e
DE
13245 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13246 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13247
b4f54984 13248 if (dwarf_read_debug)
80626a55
DE
13249 {
13250 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13251 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13252 " %s CUs, %s TUs\n",
13253 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13254 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13255 }
13256
13257 return dwp_file;
3019eac3 13258}
c906108c 13259
ab5088bf
DE
13260/* Wrapper around open_and_init_dwp_file, only open it once. */
13261
13262static struct dwp_file *
13263get_dwp_file (void)
13264{
13265 if (! dwarf2_per_objfile->dwp_checked)
13266 {
13267 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
13268 dwarf2_per_objfile->dwp_checked = 1;
13269 }
13270 return dwarf2_per_objfile->dwp_file;
13271}
13272
80626a55
DE
13273/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13274 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13275 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13276 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13277 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13278
13279 This is called, for example, when wanting to read a variable with a
13280 complex location. Therefore we don't want to do file i/o for every call.
13281 Therefore we don't want to look for a DWO file on every call.
13282 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13283 then we check if we've already seen DWO_NAME, and only THEN do we check
13284 for a DWO file.
13285
1c658ad5 13286 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13287 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13288
3019eac3 13289static struct dwo_unit *
80626a55
DE
13290lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13291 const char *dwo_name, const char *comp_dir,
13292 ULONGEST signature, int is_debug_types)
3019eac3
DE
13293{
13294 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13295 const char *kind = is_debug_types ? "TU" : "CU";
13296 void **dwo_file_slot;
3019eac3 13297 struct dwo_file *dwo_file;
80626a55 13298 struct dwp_file *dwp_file;
cb1df416 13299
6a506a2d
DE
13300 /* First see if there's a DWP file.
13301 If we have a DWP file but didn't find the DWO inside it, don't
13302 look for the original DWO file. It makes gdb behave differently
13303 depending on whether one is debugging in the build tree. */
cf2c3c16 13304
ab5088bf 13305 dwp_file = get_dwp_file ();
80626a55 13306 if (dwp_file != NULL)
cf2c3c16 13307 {
80626a55
DE
13308 const struct dwp_hash_table *dwp_htab =
13309 is_debug_types ? dwp_file->tus : dwp_file->cus;
13310
13311 if (dwp_htab != NULL)
13312 {
13313 struct dwo_unit *dwo_cutu =
57d63ce2
DE
13314 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
13315 signature, is_debug_types);
80626a55
DE
13316
13317 if (dwo_cutu != NULL)
13318 {
b4f54984 13319 if (dwarf_read_debug)
80626a55
DE
13320 {
13321 fprintf_unfiltered (gdb_stdlog,
13322 "Virtual DWO %s %s found: @%s\n",
13323 kind, hex_string (signature),
13324 host_address_to_string (dwo_cutu));
13325 }
13326 return dwo_cutu;
13327 }
13328 }
13329 }
6a506a2d 13330 else
80626a55 13331 {
6a506a2d 13332 /* No DWP file, look for the DWO file. */
80626a55 13333
6a506a2d
DE
13334 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
13335 if (*dwo_file_slot == NULL)
80626a55 13336 {
6a506a2d
DE
13337 /* Read in the file and build a table of the CUs/TUs it contains. */
13338 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13339 }
6a506a2d 13340 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13341 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13342
6a506a2d 13343 if (dwo_file != NULL)
19c3d4c9 13344 {
6a506a2d
DE
13345 struct dwo_unit *dwo_cutu = NULL;
13346
13347 if (is_debug_types && dwo_file->tus)
13348 {
13349 struct dwo_unit find_dwo_cutu;
13350
13351 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13352 find_dwo_cutu.signature = signature;
9a3c8263
SM
13353 dwo_cutu
13354 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13355 }
33c5cd75 13356 else if (!is_debug_types && dwo_file->cus)
80626a55 13357 {
33c5cd75
DB
13358 struct dwo_unit find_dwo_cutu;
13359
13360 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13361 find_dwo_cutu.signature = signature;
13362 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13363 &find_dwo_cutu);
6a506a2d
DE
13364 }
13365
13366 if (dwo_cutu != NULL)
13367 {
b4f54984 13368 if (dwarf_read_debug)
6a506a2d
DE
13369 {
13370 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13371 kind, dwo_name, hex_string (signature),
13372 host_address_to_string (dwo_cutu));
13373 }
13374 return dwo_cutu;
80626a55
DE
13375 }
13376 }
2e276125 13377 }
9cdd5dbd 13378
80626a55
DE
13379 /* We didn't find it. This could mean a dwo_id mismatch, or
13380 someone deleted the DWO/DWP file, or the search path isn't set up
13381 correctly to find the file. */
13382
b4f54984 13383 if (dwarf_read_debug)
80626a55
DE
13384 {
13385 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13386 kind, dwo_name, hex_string (signature));
13387 }
3019eac3 13388
6656a72d
DE
13389 /* This is a warning and not a complaint because it can be caused by
13390 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13391 {
13392 /* Print the name of the DWP file if we looked there, helps the user
13393 better diagnose the problem. */
791afaa2 13394 std::string dwp_text;
43942612
DE
13395
13396 if (dwp_file != NULL)
791afaa2
TT
13397 dwp_text = string_printf (" [in DWP file %s]",
13398 lbasename (dwp_file->name));
43942612
DE
13399
13400 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
13401 " [in module %s]"),
13402 kind, dwo_name, hex_string (signature),
791afaa2 13403 dwp_text.c_str (),
43942612 13404 this_unit->is_debug_types ? "TU" : "CU",
9c541725 13405 to_underlying (this_unit->sect_off), objfile_name (objfile));
43942612 13406 }
3019eac3 13407 return NULL;
5fb290d7
DJ
13408}
13409
80626a55
DE
13410/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13411 See lookup_dwo_cutu_unit for details. */
13412
13413static struct dwo_unit *
13414lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13415 const char *dwo_name, const char *comp_dir,
13416 ULONGEST signature)
13417{
13418 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13419}
13420
13421/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13422 See lookup_dwo_cutu_unit for details. */
13423
13424static struct dwo_unit *
13425lookup_dwo_type_unit (struct signatured_type *this_tu,
13426 const char *dwo_name, const char *comp_dir)
13427{
13428 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13429}
13430
89e63ee4
DE
13431/* Traversal function for queue_and_load_all_dwo_tus. */
13432
13433static int
13434queue_and_load_dwo_tu (void **slot, void *info)
13435{
13436 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13437 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13438 ULONGEST signature = dwo_unit->signature;
13439 struct signatured_type *sig_type =
13440 lookup_dwo_signatured_type (per_cu->cu, signature);
13441
13442 if (sig_type != NULL)
13443 {
13444 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13445
13446 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13447 a real dependency of PER_CU on SIG_TYPE. That is detected later
13448 while processing PER_CU. */
13449 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13450 load_full_type_unit (sig_cu);
13451 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13452 }
13453
13454 return 1;
13455}
13456
13457/* Queue all TUs contained in the DWO of PER_CU to be read in.
13458 The DWO may have the only definition of the type, though it may not be
13459 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13460 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13461
13462static void
13463queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13464{
13465 struct dwo_unit *dwo_unit;
13466 struct dwo_file *dwo_file;
13467
13468 gdb_assert (!per_cu->is_debug_types);
13469 gdb_assert (get_dwp_file () == NULL);
13470 gdb_assert (per_cu->cu != NULL);
13471
13472 dwo_unit = per_cu->cu->dwo_unit;
13473 gdb_assert (dwo_unit != NULL);
13474
13475 dwo_file = dwo_unit->dwo_file;
13476 if (dwo_file->tus != NULL)
13477 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13478}
13479
3019eac3
DE
13480/* Free all resources associated with DWO_FILE.
13481 Close the DWO file and munmap the sections.
13482 All memory should be on the objfile obstack. */
348e048f
DE
13483
13484static void
3019eac3 13485free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 13486{
348e048f 13487
5c6fa7ab 13488 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 13489 gdb_bfd_unref (dwo_file->dbfd);
348e048f 13490
3019eac3
DE
13491 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13492}
348e048f 13493
3019eac3 13494/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 13495
3019eac3
DE
13496static void
13497free_dwo_file_cleanup (void *arg)
13498{
13499 struct dwo_file *dwo_file = (struct dwo_file *) arg;
13500 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 13501
3019eac3
DE
13502 free_dwo_file (dwo_file, objfile);
13503}
348e048f 13504
3019eac3 13505/* Traversal function for free_dwo_files. */
2ab95328 13506
3019eac3
DE
13507static int
13508free_dwo_file_from_slot (void **slot, void *info)
13509{
13510 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13511 struct objfile *objfile = (struct objfile *) info;
348e048f 13512
3019eac3 13513 free_dwo_file (dwo_file, objfile);
348e048f 13514
3019eac3
DE
13515 return 1;
13516}
348e048f 13517
3019eac3 13518/* Free all resources associated with DWO_FILES. */
348e048f 13519
3019eac3
DE
13520static void
13521free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13522{
13523 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 13524}
3019eac3
DE
13525\f
13526/* Read in various DIEs. */
348e048f 13527
d389af10 13528/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13529 Inherit only the children of the DW_AT_abstract_origin DIE not being
13530 already referenced by DW_AT_abstract_origin from the children of the
13531 current DIE. */
d389af10
JK
13532
13533static void
13534inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13535{
13536 struct die_info *child_die;
791afaa2 13537 sect_offset *offsetp;
d389af10
JK
13538 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13539 struct die_info *origin_die;
13540 /* Iterator of the ORIGIN_DIE children. */
13541 struct die_info *origin_child_die;
d389af10 13542 struct attribute *attr;
cd02d79d
PA
13543 struct dwarf2_cu *origin_cu;
13544 struct pending **origin_previous_list_in_scope;
d389af10
JK
13545
13546 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13547 if (!attr)
13548 return;
13549
cd02d79d
PA
13550 /* Note that following die references may follow to a die in a
13551 different cu. */
13552
13553 origin_cu = cu;
13554 origin_die = follow_die_ref (die, attr, &origin_cu);
13555
13556 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13557 symbols in. */
13558 origin_previous_list_in_scope = origin_cu->list_in_scope;
13559 origin_cu->list_in_scope = cu->list_in_scope;
13560
edb3359d
DJ
13561 if (die->tag != origin_die->tag
13562 && !(die->tag == DW_TAG_inlined_subroutine
13563 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
13564 complaint (&symfile_complaints,
13565 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9c541725
PA
13566 to_underlying (die->sect_off),
13567 to_underlying (origin_die->sect_off));
d389af10 13568
791afaa2 13569 std::vector<sect_offset> offsets;
d389af10 13570
3ea89b92
PMR
13571 for (child_die = die->child;
13572 child_die && child_die->tag;
13573 child_die = sibling_die (child_die))
13574 {
13575 struct die_info *child_origin_die;
13576 struct dwarf2_cu *child_origin_cu;
13577
13578 /* We are trying to process concrete instance entries:
216f72a1 13579 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13580 it's not relevant to our analysis here. i.e. detecting DIEs that are
13581 present in the abstract instance but not referenced in the concrete
13582 one. */
216f72a1
JK
13583 if (child_die->tag == DW_TAG_call_site
13584 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13585 continue;
13586
c38f313d
DJ
13587 /* For each CHILD_DIE, find the corresponding child of
13588 ORIGIN_DIE. If there is more than one layer of
13589 DW_AT_abstract_origin, follow them all; there shouldn't be,
13590 but GCC versions at least through 4.4 generate this (GCC PR
13591 40573). */
3ea89b92
PMR
13592 child_origin_die = child_die;
13593 child_origin_cu = cu;
c38f313d
DJ
13594 while (1)
13595 {
cd02d79d
PA
13596 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13597 child_origin_cu);
c38f313d
DJ
13598 if (attr == NULL)
13599 break;
cd02d79d
PA
13600 child_origin_die = follow_die_ref (child_origin_die, attr,
13601 &child_origin_cu);
c38f313d
DJ
13602 }
13603
d389af10
JK
13604 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13605 counterpart may exist. */
c38f313d 13606 if (child_origin_die != child_die)
d389af10 13607 {
edb3359d
DJ
13608 if (child_die->tag != child_origin_die->tag
13609 && !(child_die->tag == DW_TAG_inlined_subroutine
13610 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
13611 complaint (&symfile_complaints,
13612 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
13613 "different tags"),
13614 to_underlying (child_die->sect_off),
13615 to_underlying (child_origin_die->sect_off));
c38f313d
DJ
13616 if (child_origin_die->parent != origin_die)
13617 complaint (&symfile_complaints,
13618 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
13619 "different parents"),
13620 to_underlying (child_die->sect_off),
13621 to_underlying (child_origin_die->sect_off));
c38f313d 13622 else
791afaa2 13623 offsets.push_back (child_origin_die->sect_off);
d389af10 13624 }
d389af10 13625 }
791afaa2
TT
13626 std::sort (offsets.begin (), offsets.end ());
13627 sect_offset *offsets_end = offsets.data () + offsets.size ();
13628 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13629 if (offsetp[-1] == *offsetp)
3e43a32a
MS
13630 complaint (&symfile_complaints,
13631 _("Multiple children of DIE 0x%x refer "
13632 "to DIE 0x%x as their abstract origin"),
9c541725 13633 to_underlying (die->sect_off), to_underlying (*offsetp));
d389af10 13634
791afaa2 13635 offsetp = offsets.data ();
d389af10
JK
13636 origin_child_die = origin_die->child;
13637 while (origin_child_die && origin_child_die->tag)
13638 {
13639 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13640 while (offsetp < offsets_end
9c541725 13641 && *offsetp < origin_child_die->sect_off)
d389af10 13642 offsetp++;
b64f50a1 13643 if (offsetp >= offsets_end
9c541725 13644 || *offsetp > origin_child_die->sect_off)
d389af10 13645 {
adde2bff
DE
13646 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13647 Check whether we're already processing ORIGIN_CHILD_DIE.
13648 This can happen with mutually referenced abstract_origins.
13649 PR 16581. */
13650 if (!origin_child_die->in_process)
13651 process_die (origin_child_die, origin_cu);
d389af10
JK
13652 }
13653 origin_child_die = sibling_die (origin_child_die);
13654 }
cd02d79d 13655 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
13656}
13657
c906108c 13658static void
e7c27a73 13659read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13660{
e7c27a73 13661 struct objfile *objfile = cu->objfile;
3e29f34a 13662 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13663 struct context_stack *newobj;
c906108c
SS
13664 CORE_ADDR lowpc;
13665 CORE_ADDR highpc;
13666 struct die_info *child_die;
edb3359d 13667 struct attribute *attr, *call_line, *call_file;
15d034d0 13668 const char *name;
e142c38c 13669 CORE_ADDR baseaddr;
801e3a5b 13670 struct block *block;
edb3359d 13671 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13672 std::vector<struct symbol *> template_args;
34eaf542 13673 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13674
13675 if (inlined_func)
13676 {
13677 /* If we do not have call site information, we can't show the
13678 caller of this inlined function. That's too confusing, so
13679 only use the scope for local variables. */
13680 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13681 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13682 if (call_line == NULL || call_file == NULL)
13683 {
13684 read_lexical_block_scope (die, cu);
13685 return;
13686 }
13687 }
c906108c 13688
e142c38c
DJ
13689 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13690
94af9270 13691 name = dwarf2_name (die, cu);
c906108c 13692
e8d05480
JB
13693 /* Ignore functions with missing or empty names. These are actually
13694 illegal according to the DWARF standard. */
13695 if (name == NULL)
13696 {
13697 complaint (&symfile_complaints,
b64f50a1 13698 _("missing name for subprogram DIE at %d"),
9c541725 13699 to_underlying (die->sect_off));
e8d05480
JB
13700 return;
13701 }
13702
13703 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13704 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13705 <= PC_BOUNDS_INVALID)
e8d05480 13706 {
ae4d0c03
PM
13707 attr = dwarf2_attr (die, DW_AT_external, cu);
13708 if (!attr || !DW_UNSND (attr))
13709 complaint (&symfile_complaints,
3e43a32a
MS
13710 _("cannot get low and high bounds "
13711 "for subprogram DIE at %d"),
9c541725 13712 to_underlying (die->sect_off));
e8d05480
JB
13713 return;
13714 }
c906108c 13715
3e29f34a
MR
13716 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13717 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13718
34eaf542
TT
13719 /* If we have any template arguments, then we must allocate a
13720 different sort of symbol. */
13721 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13722 {
13723 if (child_die->tag == DW_TAG_template_type_param
13724 || child_die->tag == DW_TAG_template_value_param)
13725 {
e623cf5d 13726 templ_func = allocate_template_symbol (objfile);
cf724bc9 13727 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13728 break;
13729 }
13730 }
13731
fe978cb0
PA
13732 newobj = push_context (0, lowpc);
13733 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 13734 (struct symbol *) templ_func);
4c2df51b 13735
4cecd739
DJ
13736 /* If there is a location expression for DW_AT_frame_base, record
13737 it. */
e142c38c 13738 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 13739 if (attr)
fe978cb0 13740 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13741
63e43d3a
PMR
13742 /* If there is a location for the static link, record it. */
13743 newobj->static_link = NULL;
13744 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13745 if (attr)
13746 {
224c3ddb
SM
13747 newobj->static_link
13748 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
13749 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13750 }
13751
e142c38c 13752 cu->list_in_scope = &local_symbols;
c906108c 13753
639d11d3 13754 if (die->child != NULL)
c906108c 13755 {
639d11d3 13756 child_die = die->child;
c906108c
SS
13757 while (child_die && child_die->tag)
13758 {
34eaf542
TT
13759 if (child_die->tag == DW_TAG_template_type_param
13760 || child_die->tag == DW_TAG_template_value_param)
13761 {
13762 struct symbol *arg = new_symbol (child_die, NULL, cu);
13763
f1078f66 13764 if (arg != NULL)
2f4732b0 13765 template_args.push_back (arg);
34eaf542
TT
13766 }
13767 else
13768 process_die (child_die, cu);
c906108c
SS
13769 child_die = sibling_die (child_die);
13770 }
13771 }
13772
d389af10
JK
13773 inherit_abstract_dies (die, cu);
13774
4a811a97
UW
13775 /* If we have a DW_AT_specification, we might need to import using
13776 directives from the context of the specification DIE. See the
13777 comment in determine_prefix. */
13778 if (cu->language == language_cplus
13779 && dwarf2_attr (die, DW_AT_specification, cu))
13780 {
13781 struct dwarf2_cu *spec_cu = cu;
13782 struct die_info *spec_die = die_specification (die, &spec_cu);
13783
13784 while (spec_die)
13785 {
13786 child_die = spec_die->child;
13787 while (child_die && child_die->tag)
13788 {
13789 if (child_die->tag == DW_TAG_imported_module)
13790 process_die (child_die, spec_cu);
13791 child_die = sibling_die (child_die);
13792 }
13793
13794 /* In some cases, GCC generates specification DIEs that
13795 themselves contain DW_AT_specification attributes. */
13796 spec_die = die_specification (spec_die, &spec_cu);
13797 }
13798 }
13799
fe978cb0 13800 newobj = pop_context ();
c906108c 13801 /* Make a block for the local symbols within. */
fe978cb0 13802 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 13803 newobj->static_link, lowpc, highpc);
801e3a5b 13804
df8a16a1 13805 /* For C++, set the block's scope. */
45280282
IB
13806 if ((cu->language == language_cplus
13807 || cu->language == language_fortran
c44af4eb
TT
13808 || cu->language == language_d
13809 || cu->language == language_rust)
4d4ec4e5 13810 && cu->processing_has_namespace_info)
195a3f6c
TT
13811 block_set_scope (block, determine_prefix (die, cu),
13812 &objfile->objfile_obstack);
df8a16a1 13813
801e3a5b
JB
13814 /* If we have address ranges, record them. */
13815 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13816
fe978cb0 13817 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 13818
34eaf542 13819 /* Attach template arguments to function. */
2f4732b0 13820 if (!template_args.empty ())
34eaf542
TT
13821 {
13822 gdb_assert (templ_func != NULL);
13823
2f4732b0 13824 templ_func->n_template_arguments = template_args.size ();
34eaf542 13825 templ_func->template_arguments
8d749320
SM
13826 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13827 templ_func->n_template_arguments);
34eaf542 13828 memcpy (templ_func->template_arguments,
2f4732b0 13829 template_args.data (),
34eaf542 13830 (templ_func->n_template_arguments * sizeof (struct symbol *)));
34eaf542
TT
13831 }
13832
208d8187
JB
13833 /* In C++, we can have functions nested inside functions (e.g., when
13834 a function declares a class that has methods). This means that
13835 when we finish processing a function scope, we may need to go
13836 back to building a containing block's symbol lists. */
fe978cb0 13837 local_symbols = newobj->locals;
22cee43f 13838 local_using_directives = newobj->local_using_directives;
208d8187 13839
921e78cf
JB
13840 /* If we've finished processing a top-level function, subsequent
13841 symbols go in the file symbol list. */
13842 if (outermost_context_p ())
e142c38c 13843 cu->list_in_scope = &file_symbols;
c906108c
SS
13844}
13845
13846/* Process all the DIES contained within a lexical block scope. Start
13847 a new scope, process the dies, and then close the scope. */
13848
13849static void
e7c27a73 13850read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13851{
e7c27a73 13852 struct objfile *objfile = cu->objfile;
3e29f34a 13853 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13854 struct context_stack *newobj;
c906108c
SS
13855 CORE_ADDR lowpc, highpc;
13856 struct die_info *child_die;
e142c38c
DJ
13857 CORE_ADDR baseaddr;
13858
13859 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13860
13861 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13862 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13863 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13864 be nasty. Might be easier to properly extend generic blocks to
af34e669 13865 describe ranges. */
e385593e
JK
13866 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13867 {
13868 case PC_BOUNDS_NOT_PRESENT:
13869 /* DW_TAG_lexical_block has no attributes, process its children as if
13870 there was no wrapping by that DW_TAG_lexical_block.
13871 GCC does no longer produces such DWARF since GCC r224161. */
13872 for (child_die = die->child;
13873 child_die != NULL && child_die->tag;
13874 child_die = sibling_die (child_die))
13875 process_die (child_die, cu);
13876 return;
13877 case PC_BOUNDS_INVALID:
13878 return;
13879 }
3e29f34a
MR
13880 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13881 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
13882
13883 push_context (0, lowpc);
639d11d3 13884 if (die->child != NULL)
c906108c 13885 {
639d11d3 13886 child_die = die->child;
c906108c
SS
13887 while (child_die && child_die->tag)
13888 {
e7c27a73 13889 process_die (child_die, cu);
c906108c
SS
13890 child_die = sibling_die (child_die);
13891 }
13892 }
3ea89b92 13893 inherit_abstract_dies (die, cu);
fe978cb0 13894 newobj = pop_context ();
c906108c 13895
22cee43f 13896 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 13897 {
801e3a5b 13898 struct block *block
63e43d3a 13899 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 13900 newobj->start_addr, highpc);
801e3a5b
JB
13901
13902 /* Note that recording ranges after traversing children, as we
13903 do here, means that recording a parent's ranges entails
13904 walking across all its children's ranges as they appear in
13905 the address map, which is quadratic behavior.
13906
13907 It would be nicer to record the parent's ranges before
13908 traversing its children, simply overriding whatever you find
13909 there. But since we don't even decide whether to create a
13910 block until after we've traversed its children, that's hard
13911 to do. */
13912 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13913 }
fe978cb0 13914 local_symbols = newobj->locals;
22cee43f 13915 local_using_directives = newobj->local_using_directives;
c906108c
SS
13916}
13917
216f72a1 13918/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13919
13920static void
13921read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13922{
13923 struct objfile *objfile = cu->objfile;
13924 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13925 CORE_ADDR pc, baseaddr;
13926 struct attribute *attr;
13927 struct call_site *call_site, call_site_local;
13928 void **slot;
13929 int nparams;
13930 struct die_info *child_die;
13931
13932 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13933
216f72a1
JK
13934 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13935 if (attr == NULL)
13936 {
13937 /* This was a pre-DWARF-5 GNU extension alias
13938 for DW_AT_call_return_pc. */
13939 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13940 }
96408a79
SA
13941 if (!attr)
13942 {
13943 complaint (&symfile_complaints,
216f72a1 13944 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 13945 "DIE 0x%x [in module %s]"),
9c541725 13946 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
13947 return;
13948 }
31aa7e4e 13949 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13950 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13951
13952 if (cu->call_site_htab == NULL)
13953 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13954 NULL, &objfile->objfile_obstack,
13955 hashtab_obstack_allocate, NULL);
13956 call_site_local.pc = pc;
13957 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13958 if (*slot != NULL)
13959 {
13960 complaint (&symfile_complaints,
216f72a1 13961 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 13962 "DIE 0x%x [in module %s]"),
9c541725 13963 paddress (gdbarch, pc), to_underlying (die->sect_off),
4262abfb 13964 objfile_name (objfile));
96408a79
SA
13965 return;
13966 }
13967
13968 /* Count parameters at the caller. */
13969
13970 nparams = 0;
13971 for (child_die = die->child; child_die && child_die->tag;
13972 child_die = sibling_die (child_die))
13973 {
216f72a1
JK
13974 if (child_die->tag != DW_TAG_call_site_parameter
13975 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13976 {
13977 complaint (&symfile_complaints,
216f72a1
JK
13978 _("Tag %d is not DW_TAG_call_site_parameter in "
13979 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 13980 child_die->tag, to_underlying (child_die->sect_off),
4262abfb 13981 objfile_name (objfile));
96408a79
SA
13982 continue;
13983 }
13984
13985 nparams++;
13986 }
13987
224c3ddb
SM
13988 call_site
13989 = ((struct call_site *)
13990 obstack_alloc (&objfile->objfile_obstack,
13991 sizeof (*call_site)
13992 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13993 *slot = call_site;
13994 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13995 call_site->pc = pc;
13996
216f72a1
JK
13997 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13998 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13999 {
14000 struct die_info *func_die;
14001
14002 /* Skip also over DW_TAG_inlined_subroutine. */
14003 for (func_die = die->parent;
14004 func_die && func_die->tag != DW_TAG_subprogram
14005 && func_die->tag != DW_TAG_subroutine_type;
14006 func_die = func_die->parent);
14007
216f72a1
JK
14008 /* DW_AT_call_all_calls is a superset
14009 of DW_AT_call_all_tail_calls. */
96408a79 14010 if (func_die
216f72a1 14011 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14012 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14013 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14014 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14015 {
14016 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14017 not complete. But keep CALL_SITE for look ups via call_site_htab,
14018 both the initial caller containing the real return address PC and
14019 the final callee containing the current PC of a chain of tail
14020 calls do not need to have the tail call list complete. But any
14021 function candidate for a virtual tail call frame searched via
14022 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14023 determined unambiguously. */
14024 }
14025 else
14026 {
14027 struct type *func_type = NULL;
14028
14029 if (func_die)
14030 func_type = get_die_type (func_die, cu);
14031 if (func_type != NULL)
14032 {
14033 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14034
14035 /* Enlist this call site to the function. */
14036 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14037 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14038 }
14039 else
14040 complaint (&symfile_complaints,
216f72a1 14041 _("Cannot find function owning DW_TAG_call_site "
96408a79 14042 "DIE 0x%x [in module %s]"),
9c541725 14043 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
14044 }
14045 }
14046
216f72a1
JK
14047 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14048 if (attr == NULL)
14049 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14050 if (attr == NULL)
14051 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14052 if (attr == NULL)
216f72a1
JK
14053 {
14054 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14055 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14056 }
96408a79
SA
14057 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14058 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14059 /* Keep NULL DWARF_BLOCK. */;
14060 else if (attr_form_is_block (attr))
14061 {
14062 struct dwarf2_locexpr_baton *dlbaton;
14063
8d749320 14064 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14065 dlbaton->data = DW_BLOCK (attr)->data;
14066 dlbaton->size = DW_BLOCK (attr)->size;
14067 dlbaton->per_cu = cu->per_cu;
14068
14069 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14070 }
7771576e 14071 else if (attr_form_is_ref (attr))
96408a79 14072 {
96408a79
SA
14073 struct dwarf2_cu *target_cu = cu;
14074 struct die_info *target_die;
14075
ac9ec31b 14076 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
14077 gdb_assert (target_cu->objfile == objfile);
14078 if (die_is_declaration (target_die, target_cu))
14079 {
7d45c7c3 14080 const char *target_physname;
9112db09
JK
14081
14082 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14083 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14084 if (target_physname == NULL)
9112db09 14085 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
14086 if (target_physname == NULL)
14087 complaint (&symfile_complaints,
216f72a1 14088 _("DW_AT_call_target target DIE has invalid "
96408a79 14089 "physname, for referencing DIE 0x%x [in module %s]"),
9c541725 14090 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 14091 else
7d455152 14092 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14093 }
14094 else
14095 {
14096 CORE_ADDR lowpc;
14097
14098 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14099 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14100 <= PC_BOUNDS_INVALID)
96408a79 14101 complaint (&symfile_complaints,
216f72a1 14102 _("DW_AT_call_target target DIE has invalid "
96408a79 14103 "low pc, for referencing DIE 0x%x [in module %s]"),
9c541725 14104 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 14105 else
3e29f34a
MR
14106 {
14107 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14108 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14109 }
96408a79
SA
14110 }
14111 }
14112 else
14113 complaint (&symfile_complaints,
216f72a1 14114 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 14115 "block nor reference, for DIE 0x%x [in module %s]"),
9c541725 14116 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
14117
14118 call_site->per_cu = cu->per_cu;
14119
14120 for (child_die = die->child;
14121 child_die && child_die->tag;
14122 child_die = sibling_die (child_die))
14123 {
96408a79 14124 struct call_site_parameter *parameter;
1788b2d3 14125 struct attribute *loc, *origin;
96408a79 14126
216f72a1
JK
14127 if (child_die->tag != DW_TAG_call_site_parameter
14128 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14129 {
14130 /* Already printed the complaint above. */
14131 continue;
14132 }
14133
14134 gdb_assert (call_site->parameter_count < nparams);
14135 parameter = &call_site->parameter[call_site->parameter_count];
14136
1788b2d3
JK
14137 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14138 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14139 register is contained in DW_AT_call_value. */
96408a79 14140
24c5c679 14141 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14142 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14143 if (origin == NULL)
14144 {
14145 /* This was a pre-DWARF-5 GNU extension alias
14146 for DW_AT_call_parameter. */
14147 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14148 }
7771576e 14149 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14150 {
1788b2d3 14151 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14152
14153 sect_offset sect_off
14154 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14155 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14156 {
14157 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14158 binding can be done only inside one CU. Such referenced DIE
14159 therefore cannot be even moved to DW_TAG_partial_unit. */
14160 complaint (&symfile_complaints,
216f72a1
JK
14161 _("DW_AT_call_parameter offset is not in CU for "
14162 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
14163 to_underlying (child_die->sect_off),
14164 objfile_name (objfile));
d76b7dbc
JK
14165 continue;
14166 }
9c541725
PA
14167 parameter->u.param_cu_off
14168 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14169 }
14170 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
14171 {
14172 complaint (&symfile_complaints,
14173 _("No DW_FORM_block* DW_AT_location for "
216f72a1 14174 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 14175 to_underlying (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14176 continue;
14177 }
24c5c679 14178 else
96408a79 14179 {
24c5c679
JK
14180 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14181 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14182 if (parameter->u.dwarf_reg != -1)
14183 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14184 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14185 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14186 &parameter->u.fb_offset))
14187 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14188 else
14189 {
14190 complaint (&symfile_complaints,
14191 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14192 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 14193 "DW_TAG_call_site child DIE 0x%x "
24c5c679 14194 "[in module %s]"),
9c541725
PA
14195 to_underlying (child_die->sect_off),
14196 objfile_name (objfile));
24c5c679
JK
14197 continue;
14198 }
96408a79
SA
14199 }
14200
216f72a1
JK
14201 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14202 if (attr == NULL)
14203 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14204 if (!attr_form_is_block (attr))
14205 {
14206 complaint (&symfile_complaints,
216f72a1
JK
14207 _("No DW_FORM_block* DW_AT_call_value for "
14208 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
14209 to_underlying (child_die->sect_off),
14210 objfile_name (objfile));
96408a79
SA
14211 continue;
14212 }
14213 parameter->value = DW_BLOCK (attr)->data;
14214 parameter->value_size = DW_BLOCK (attr)->size;
14215
14216 /* Parameters are not pre-cleared by memset above. */
14217 parameter->data_value = NULL;
14218 parameter->data_value_size = 0;
14219 call_site->parameter_count++;
14220
216f72a1
JK
14221 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14222 if (attr == NULL)
14223 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14224 if (attr)
14225 {
14226 if (!attr_form_is_block (attr))
14227 complaint (&symfile_complaints,
216f72a1
JK
14228 _("No DW_FORM_block* DW_AT_call_data_value for "
14229 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
14230 to_underlying (child_die->sect_off),
14231 objfile_name (objfile));
96408a79
SA
14232 else
14233 {
14234 parameter->data_value = DW_BLOCK (attr)->data;
14235 parameter->data_value_size = DW_BLOCK (attr)->size;
14236 }
14237 }
14238 }
14239}
14240
71a3c369
TT
14241/* Helper function for read_variable. If DIE represents a virtual
14242 table, then return the type of the concrete object that is
14243 associated with the virtual table. Otherwise, return NULL. */
14244
14245static struct type *
14246rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14247{
14248 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14249 if (attr == NULL)
14250 return NULL;
14251
14252 /* Find the type DIE. */
14253 struct die_info *type_die = NULL;
14254 struct dwarf2_cu *type_cu = cu;
14255
14256 if (attr_form_is_ref (attr))
14257 type_die = follow_die_ref (die, attr, &type_cu);
14258 if (type_die == NULL)
14259 return NULL;
14260
14261 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14262 return NULL;
14263 return die_containing_type (type_die, type_cu);
14264}
14265
14266/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14267
14268static void
14269read_variable (struct die_info *die, struct dwarf2_cu *cu)
14270{
14271 struct rust_vtable_symbol *storage = NULL;
14272
14273 if (cu->language == language_rust)
14274 {
14275 struct type *containing_type = rust_containing_type (die, cu);
14276
14277 if (containing_type != NULL)
14278 {
14279 struct objfile *objfile = cu->objfile;
14280
14281 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14282 struct rust_vtable_symbol);
14283 initialize_objfile_symbol (storage);
14284 storage->concrete_type = containing_type;
cf724bc9 14285 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14286 }
14287 }
14288
14289 new_symbol_full (die, NULL, cu, storage);
14290}
14291
43988095
JK
14292/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14293 reading .debug_rnglists.
14294 Callback's type should be:
14295 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14296 Return true if the attributes are present and valid, otherwise,
14297 return false. */
14298
14299template <typename Callback>
14300static bool
14301dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14302 Callback &&callback)
14303{
14304 struct objfile *objfile = cu->objfile;
43988095 14305 bfd *obfd = objfile->obfd;
43988095
JK
14306 /* Base address selection entry. */
14307 CORE_ADDR base;
14308 int found_base;
43988095 14309 const gdb_byte *buffer;
43988095
JK
14310 CORE_ADDR baseaddr;
14311 bool overflow = false;
14312
14313 found_base = cu->base_known;
14314 base = cu->base_address;
14315
14316 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14317 if (offset >= dwarf2_per_objfile->rnglists.size)
14318 {
14319 complaint (&symfile_complaints,
14320 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14321 offset);
14322 return false;
14323 }
14324 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14325
14326 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14327
14328 while (1)
14329 {
7814882a
JK
14330 /* Initialize it due to a false compiler warning. */
14331 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14332 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14333 + dwarf2_per_objfile->rnglists.size);
14334 unsigned int bytes_read;
14335
14336 if (buffer == buf_end)
14337 {
14338 overflow = true;
14339 break;
14340 }
14341 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14342 switch (rlet)
14343 {
14344 case DW_RLE_end_of_list:
14345 break;
14346 case DW_RLE_base_address:
14347 if (buffer + cu->header.addr_size > buf_end)
14348 {
14349 overflow = true;
14350 break;
14351 }
14352 base = read_address (obfd, buffer, cu, &bytes_read);
14353 found_base = 1;
14354 buffer += bytes_read;
14355 break;
14356 case DW_RLE_start_length:
14357 if (buffer + cu->header.addr_size > buf_end)
14358 {
14359 overflow = true;
14360 break;
14361 }
14362 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14363 buffer += bytes_read;
14364 range_end = (range_beginning
14365 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14366 buffer += bytes_read;
14367 if (buffer > buf_end)
14368 {
14369 overflow = true;
14370 break;
14371 }
14372 break;
14373 case DW_RLE_offset_pair:
14374 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14375 buffer += bytes_read;
14376 if (buffer > buf_end)
14377 {
14378 overflow = true;
14379 break;
14380 }
14381 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14382 buffer += bytes_read;
14383 if (buffer > buf_end)
14384 {
14385 overflow = true;
14386 break;
14387 }
14388 break;
14389 case DW_RLE_start_end:
14390 if (buffer + 2 * cu->header.addr_size > buf_end)
14391 {
14392 overflow = true;
14393 break;
14394 }
14395 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14396 buffer += bytes_read;
14397 range_end = read_address (obfd, buffer, cu, &bytes_read);
14398 buffer += bytes_read;
14399 break;
14400 default:
14401 complaint (&symfile_complaints,
14402 _("Invalid .debug_rnglists data (no base address)"));
14403 return false;
14404 }
14405 if (rlet == DW_RLE_end_of_list || overflow)
14406 break;
14407 if (rlet == DW_RLE_base_address)
14408 continue;
14409
14410 if (!found_base)
14411 {
14412 /* We have no valid base address for the ranges
14413 data. */
14414 complaint (&symfile_complaints,
14415 _("Invalid .debug_rnglists data (no base address)"));
14416 return false;
14417 }
14418
14419 if (range_beginning > range_end)
14420 {
14421 /* Inverted range entries are invalid. */
14422 complaint (&symfile_complaints,
14423 _("Invalid .debug_rnglists data (inverted range)"));
14424 return false;
14425 }
14426
14427 /* Empty range entries have no effect. */
14428 if (range_beginning == range_end)
14429 continue;
14430
14431 range_beginning += base;
14432 range_end += base;
14433
14434 /* A not-uncommon case of bad debug info.
14435 Don't pollute the addrmap with bad data. */
14436 if (range_beginning + baseaddr == 0
14437 && !dwarf2_per_objfile->has_section_at_zero)
14438 {
14439 complaint (&symfile_complaints,
14440 _(".debug_rnglists entry has start address of zero"
14441 " [in module %s]"), objfile_name (objfile));
14442 continue;
14443 }
14444
14445 callback (range_beginning, range_end);
14446 }
14447
14448 if (overflow)
14449 {
14450 complaint (&symfile_complaints,
14451 _("Offset %d is not terminated "
14452 "for DW_AT_ranges attribute"),
14453 offset);
14454 return false;
14455 }
14456
14457 return true;
14458}
14459
14460/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14461 Callback's type should be:
14462 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14463 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14464
43988095 14465template <typename Callback>
43039443 14466static int
5f46c5a5 14467dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14468 Callback &&callback)
43039443
JK
14469{
14470 struct objfile *objfile = cu->objfile;
14471 struct comp_unit_head *cu_header = &cu->header;
14472 bfd *obfd = objfile->obfd;
14473 unsigned int addr_size = cu_header->addr_size;
14474 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14475 /* Base address selection entry. */
14476 CORE_ADDR base;
14477 int found_base;
14478 unsigned int dummy;
d521ce57 14479 const gdb_byte *buffer;
ff013f42 14480 CORE_ADDR baseaddr;
43039443 14481
43988095
JK
14482 if (cu_header->version >= 5)
14483 return dwarf2_rnglists_process (offset, cu, callback);
14484
d00adf39
DE
14485 found_base = cu->base_known;
14486 base = cu->base_address;
43039443 14487
be391dca 14488 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14489 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
14490 {
14491 complaint (&symfile_complaints,
14492 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14493 offset);
14494 return 0;
14495 }
dce234bc 14496 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14497
e7030f15 14498 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14499
43039443
JK
14500 while (1)
14501 {
14502 CORE_ADDR range_beginning, range_end;
14503
14504 range_beginning = read_address (obfd, buffer, cu, &dummy);
14505 buffer += addr_size;
14506 range_end = read_address (obfd, buffer, cu, &dummy);
14507 buffer += addr_size;
14508 offset += 2 * addr_size;
14509
14510 /* An end of list marker is a pair of zero addresses. */
14511 if (range_beginning == 0 && range_end == 0)
14512 /* Found the end of list entry. */
14513 break;
14514
14515 /* Each base address selection entry is a pair of 2 values.
14516 The first is the largest possible address, the second is
14517 the base address. Check for a base address here. */
14518 if ((range_beginning & mask) == mask)
14519 {
28d2bfb9
AB
14520 /* If we found the largest possible address, then we already
14521 have the base address in range_end. */
14522 base = range_end;
43039443
JK
14523 found_base = 1;
14524 continue;
14525 }
14526
14527 if (!found_base)
14528 {
14529 /* We have no valid base address for the ranges
14530 data. */
14531 complaint (&symfile_complaints,
14532 _("Invalid .debug_ranges data (no base address)"));
14533 return 0;
14534 }
14535
9277c30c
UW
14536 if (range_beginning > range_end)
14537 {
14538 /* Inverted range entries are invalid. */
14539 complaint (&symfile_complaints,
14540 _("Invalid .debug_ranges data (inverted range)"));
14541 return 0;
14542 }
14543
14544 /* Empty range entries have no effect. */
14545 if (range_beginning == range_end)
14546 continue;
14547
43039443
JK
14548 range_beginning += base;
14549 range_end += base;
14550
01093045
DE
14551 /* A not-uncommon case of bad debug info.
14552 Don't pollute the addrmap with bad data. */
14553 if (range_beginning + baseaddr == 0
14554 && !dwarf2_per_objfile->has_section_at_zero)
14555 {
14556 complaint (&symfile_complaints,
14557 _(".debug_ranges entry has start address of zero"
4262abfb 14558 " [in module %s]"), objfile_name (objfile));
01093045
DE
14559 continue;
14560 }
14561
5f46c5a5
JK
14562 callback (range_beginning, range_end);
14563 }
14564
14565 return 1;
14566}
14567
14568/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14569 Return 1 if the attributes are present and valid, otherwise, return 0.
14570 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14571
14572static int
14573dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14574 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14575 struct partial_symtab *ranges_pst)
14576{
14577 struct objfile *objfile = cu->objfile;
14578 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14579 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14580 SECT_OFF_TEXT (objfile));
14581 int low_set = 0;
14582 CORE_ADDR low = 0;
14583 CORE_ADDR high = 0;
14584 int retval;
14585
14586 retval = dwarf2_ranges_process (offset, cu,
14587 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14588 {
9277c30c 14589 if (ranges_pst != NULL)
3e29f34a
MR
14590 {
14591 CORE_ADDR lowpc;
14592 CORE_ADDR highpc;
14593
14594 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14595 range_beginning + baseaddr);
14596 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14597 range_end + baseaddr);
14598 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14599 ranges_pst);
14600 }
ff013f42 14601
43039443
JK
14602 /* FIXME: This is recording everything as a low-high
14603 segment of consecutive addresses. We should have a
14604 data structure for discontiguous block ranges
14605 instead. */
14606 if (! low_set)
14607 {
14608 low = range_beginning;
14609 high = range_end;
14610 low_set = 1;
14611 }
14612 else
14613 {
14614 if (range_beginning < low)
14615 low = range_beginning;
14616 if (range_end > high)
14617 high = range_end;
14618 }
5f46c5a5
JK
14619 });
14620 if (!retval)
14621 return 0;
43039443
JK
14622
14623 if (! low_set)
14624 /* If the first entry is an end-of-list marker, the range
14625 describes an empty scope, i.e. no instructions. */
14626 return 0;
14627
14628 if (low_return)
14629 *low_return = low;
14630 if (high_return)
14631 *high_return = high;
14632 return 1;
14633}
14634
3a2b436a
JK
14635/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14636 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14637 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14638
3a2b436a 14639static enum pc_bounds_kind
af34e669 14640dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14641 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14642 struct partial_symtab *pst)
c906108c
SS
14643{
14644 struct attribute *attr;
91da1414 14645 struct attribute *attr_high;
af34e669
DJ
14646 CORE_ADDR low = 0;
14647 CORE_ADDR high = 0;
e385593e 14648 enum pc_bounds_kind ret;
c906108c 14649
91da1414
MW
14650 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14651 if (attr_high)
af34e669 14652 {
e142c38c 14653 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 14654 if (attr)
91da1414 14655 {
31aa7e4e
JB
14656 low = attr_value_as_address (attr);
14657 high = attr_value_as_address (attr_high);
14658 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14659 high += low;
91da1414 14660 }
af34e669
DJ
14661 else
14662 /* Found high w/o low attribute. */
e385593e 14663 return PC_BOUNDS_INVALID;
af34e669
DJ
14664
14665 /* Found consecutive range of addresses. */
3a2b436a 14666 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14667 }
c906108c 14668 else
af34e669 14669 {
e142c38c 14670 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14671 if (attr != NULL)
14672 {
ab435259
DE
14673 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14674 We take advantage of the fact that DW_AT_ranges does not appear
14675 in DW_TAG_compile_unit of DWO files. */
14676 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14677 unsigned int ranges_offset = (DW_UNSND (attr)
14678 + (need_ranges_base
14679 ? cu->ranges_base
14680 : 0));
2e3cf129 14681
af34e669 14682 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14683 .debug_ranges section. */
2e3cf129 14684 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14685 return PC_BOUNDS_INVALID;
43039443 14686 /* Found discontinuous range of addresses. */
3a2b436a 14687 ret = PC_BOUNDS_RANGES;
af34e669 14688 }
e385593e
JK
14689 else
14690 return PC_BOUNDS_NOT_PRESENT;
af34e669 14691 }
c906108c 14692
9373cf26
JK
14693 /* read_partial_die has also the strict LOW < HIGH requirement. */
14694 if (high <= low)
e385593e 14695 return PC_BOUNDS_INVALID;
c906108c
SS
14696
14697 /* When using the GNU linker, .gnu.linkonce. sections are used to
14698 eliminate duplicate copies of functions and vtables and such.
14699 The linker will arbitrarily choose one and discard the others.
14700 The AT_*_pc values for such functions refer to local labels in
14701 these sections. If the section from that file was discarded, the
14702 labels are not in the output, so the relocs get a value of 0.
14703 If this is a discarded function, mark the pc bounds as invalid,
14704 so that GDB will ignore it. */
72dca2f5 14705 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14706 return PC_BOUNDS_INVALID;
c906108c
SS
14707
14708 *lowpc = low;
96408a79
SA
14709 if (highpc)
14710 *highpc = high;
af34e669 14711 return ret;
c906108c
SS
14712}
14713
b084d499
JB
14714/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14715 its low and high PC addresses. Do nothing if these addresses could not
14716 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14717 and HIGHPC to the high address if greater than HIGHPC. */
14718
14719static void
14720dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14721 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14722 struct dwarf2_cu *cu)
14723{
14724 CORE_ADDR low, high;
14725 struct die_info *child = die->child;
14726
e385593e 14727 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14728 {
325fac50
PA
14729 *lowpc = std::min (*lowpc, low);
14730 *highpc = std::max (*highpc, high);
b084d499
JB
14731 }
14732
14733 /* If the language does not allow nested subprograms (either inside
14734 subprograms or lexical blocks), we're done. */
14735 if (cu->language != language_ada)
14736 return;
6e70227d 14737
b084d499
JB
14738 /* Check all the children of the given DIE. If it contains nested
14739 subprograms, then check their pc bounds. Likewise, we need to
14740 check lexical blocks as well, as they may also contain subprogram
14741 definitions. */
14742 while (child && child->tag)
14743 {
14744 if (child->tag == DW_TAG_subprogram
14745 || child->tag == DW_TAG_lexical_block)
14746 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14747 child = sibling_die (child);
14748 }
14749}
14750
fae299cd
DC
14751/* Get the low and high pc's represented by the scope DIE, and store
14752 them in *LOWPC and *HIGHPC. If the correct values can't be
14753 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14754
14755static void
14756get_scope_pc_bounds (struct die_info *die,
14757 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14758 struct dwarf2_cu *cu)
14759{
14760 CORE_ADDR best_low = (CORE_ADDR) -1;
14761 CORE_ADDR best_high = (CORE_ADDR) 0;
14762 CORE_ADDR current_low, current_high;
14763
3a2b436a 14764 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14765 >= PC_BOUNDS_RANGES)
fae299cd
DC
14766 {
14767 best_low = current_low;
14768 best_high = current_high;
14769 }
14770 else
14771 {
14772 struct die_info *child = die->child;
14773
14774 while (child && child->tag)
14775 {
14776 switch (child->tag) {
14777 case DW_TAG_subprogram:
b084d499 14778 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14779 break;
14780 case DW_TAG_namespace:
f55ee35c 14781 case DW_TAG_module:
fae299cd
DC
14782 /* FIXME: carlton/2004-01-16: Should we do this for
14783 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14784 that current GCC's always emit the DIEs corresponding
14785 to definitions of methods of classes as children of a
14786 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14787 the DIEs giving the declarations, which could be
14788 anywhere). But I don't see any reason why the
14789 standards says that they have to be there. */
14790 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14791
14792 if (current_low != ((CORE_ADDR) -1))
14793 {
325fac50
PA
14794 best_low = std::min (best_low, current_low);
14795 best_high = std::max (best_high, current_high);
fae299cd
DC
14796 }
14797 break;
14798 default:
0963b4bd 14799 /* Ignore. */
fae299cd
DC
14800 break;
14801 }
14802
14803 child = sibling_die (child);
14804 }
14805 }
14806
14807 *lowpc = best_low;
14808 *highpc = best_high;
14809}
14810
801e3a5b
JB
14811/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14812 in DIE. */
380bca97 14813
801e3a5b
JB
14814static void
14815dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14816 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14817{
bb5ed363 14818 struct objfile *objfile = cu->objfile;
3e29f34a 14819 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14820 struct attribute *attr;
91da1414 14821 struct attribute *attr_high;
801e3a5b 14822
91da1414
MW
14823 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14824 if (attr_high)
801e3a5b 14825 {
801e3a5b
JB
14826 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14827 if (attr)
14828 {
31aa7e4e
JB
14829 CORE_ADDR low = attr_value_as_address (attr);
14830 CORE_ADDR high = attr_value_as_address (attr_high);
14831
14832 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14833 high += low;
9a619af0 14834
3e29f34a
MR
14835 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14836 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14837 record_block_range (block, low, high - 1);
801e3a5b
JB
14838 }
14839 }
14840
14841 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14842 if (attr)
14843 {
ab435259
DE
14844 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14845 We take advantage of the fact that DW_AT_ranges does not appear
14846 in DW_TAG_compile_unit of DWO files. */
14847 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14848
14849 /* The value of the DW_AT_ranges attribute is the offset of the
14850 address range list in the .debug_ranges section. */
ab435259
DE
14851 unsigned long offset = (DW_UNSND (attr)
14852 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 14853 const gdb_byte *buffer;
801e3a5b
JB
14854
14855 /* For some target architectures, but not others, the
14856 read_address function sign-extends the addresses it returns.
14857 To recognize base address selection entries, we need a
14858 mask. */
14859 unsigned int addr_size = cu->header.addr_size;
14860 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14861
14862 /* The base address, to which the next pair is relative. Note
14863 that this 'base' is a DWARF concept: most entries in a range
14864 list are relative, to reduce the number of relocs against the
14865 debugging information. This is separate from this function's
14866 'baseaddr' argument, which GDB uses to relocate debugging
14867 information from a shared library based on the address at
14868 which the library was loaded. */
d00adf39
DE
14869 CORE_ADDR base = cu->base_address;
14870 int base_known = cu->base_known;
801e3a5b 14871
5f46c5a5
JK
14872 dwarf2_ranges_process (offset, cu,
14873 [&] (CORE_ADDR start, CORE_ADDR end)
14874 {
58fdfd2c
JK
14875 start += baseaddr;
14876 end += baseaddr;
5f46c5a5
JK
14877 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14878 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14879 record_block_range (block, start, end - 1);
14880 });
801e3a5b
JB
14881 }
14882}
14883
685b1105
JK
14884/* Check whether the producer field indicates either of GCC < 4.6, or the
14885 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14886
685b1105
JK
14887static void
14888check_producer (struct dwarf2_cu *cu)
60d5a603 14889{
38360086 14890 int major, minor;
60d5a603
JK
14891
14892 if (cu->producer == NULL)
14893 {
14894 /* For unknown compilers expect their behavior is DWARF version
14895 compliant.
14896
14897 GCC started to support .debug_types sections by -gdwarf-4 since
14898 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14899 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14900 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14901 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14902 }
b1ffba5a 14903 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14904 {
38360086
MW
14905 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14906 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14907 }
5230b05a
WT
14908 else if (producer_is_icc (cu->producer, &major, &minor))
14909 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
14910 else
14911 {
14912 /* For other non-GCC compilers, expect their behavior is DWARF version
14913 compliant. */
60d5a603
JK
14914 }
14915
ba919b58 14916 cu->checked_producer = 1;
685b1105 14917}
ba919b58 14918
685b1105
JK
14919/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14920 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14921 during 4.6.0 experimental. */
14922
14923static int
14924producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14925{
14926 if (!cu->checked_producer)
14927 check_producer (cu);
14928
14929 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14930}
14931
14932/* Return the default accessibility type if it is not overriden by
14933 DW_AT_accessibility. */
14934
14935static enum dwarf_access_attribute
14936dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14937{
14938 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14939 {
14940 /* The default DWARF 2 accessibility for members is public, the default
14941 accessibility for inheritance is private. */
14942
14943 if (die->tag != DW_TAG_inheritance)
14944 return DW_ACCESS_public;
14945 else
14946 return DW_ACCESS_private;
14947 }
14948 else
14949 {
14950 /* DWARF 3+ defines the default accessibility a different way. The same
14951 rules apply now for DW_TAG_inheritance as for the members and it only
14952 depends on the container kind. */
14953
14954 if (die->parent->tag == DW_TAG_class_type)
14955 return DW_ACCESS_private;
14956 else
14957 return DW_ACCESS_public;
14958 }
14959}
14960
74ac6d43
TT
14961/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14962 offset. If the attribute was not found return 0, otherwise return
14963 1. If it was found but could not properly be handled, set *OFFSET
14964 to 0. */
14965
14966static int
14967handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14968 LONGEST *offset)
14969{
14970 struct attribute *attr;
14971
14972 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14973 if (attr != NULL)
14974 {
14975 *offset = 0;
14976
14977 /* Note that we do not check for a section offset first here.
14978 This is because DW_AT_data_member_location is new in DWARF 4,
14979 so if we see it, we can assume that a constant form is really
14980 a constant and not a section offset. */
14981 if (attr_form_is_constant (attr))
14982 *offset = dwarf2_get_attr_constant_value (attr, 0);
14983 else if (attr_form_is_section_offset (attr))
14984 dwarf2_complex_location_expr_complaint ();
14985 else if (attr_form_is_block (attr))
14986 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14987 else
14988 dwarf2_complex_location_expr_complaint ();
14989
14990 return 1;
14991 }
14992
14993 return 0;
14994}
14995
c906108c
SS
14996/* Add an aggregate field to the field list. */
14997
14998static void
107d2387 14999dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15000 struct dwarf2_cu *cu)
6e70227d 15001{
e7c27a73 15002 struct objfile *objfile = cu->objfile;
5e2b427d 15003 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15004 struct nextfield *new_field;
15005 struct attribute *attr;
15006 struct field *fp;
15d034d0 15007 const char *fieldname = "";
c906108c
SS
15008
15009 /* Allocate a new field list entry and link it in. */
8d749320 15010 new_field = XNEW (struct nextfield);
b8c9b27d 15011 make_cleanup (xfree, new_field);
c906108c 15012 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
15013
15014 if (die->tag == DW_TAG_inheritance)
15015 {
15016 new_field->next = fip->baseclasses;
15017 fip->baseclasses = new_field;
15018 }
15019 else
15020 {
15021 new_field->next = fip->fields;
15022 fip->fields = new_field;
15023 }
c906108c
SS
15024 fip->nfields++;
15025
e142c38c 15026 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
15027 if (attr)
15028 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15029 else
15030 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15031 if (new_field->accessibility != DW_ACCESS_public)
15032 fip->non_public_fields = 1;
60d5a603 15033
e142c38c 15034 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
15035 if (attr)
15036 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15037 else
15038 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15039
15040 fp = &new_field->field;
a9a9bd0f 15041
e142c38c 15042 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15043 {
74ac6d43
TT
15044 LONGEST offset;
15045
a9a9bd0f 15046 /* Data member other than a C++ static data member. */
6e70227d 15047
c906108c 15048 /* Get type of field. */
e7c27a73 15049 fp->type = die_type (die, cu);
c906108c 15050
d6a843b5 15051 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15052
c906108c 15053 /* Get bit size of field (zero if none). */
e142c38c 15054 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
15055 if (attr)
15056 {
15057 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15058 }
15059 else
15060 {
15061 FIELD_BITSIZE (*fp) = 0;
15062 }
15063
15064 /* Get bit offset of field. */
74ac6d43
TT
15065 if (handle_data_member_location (die, cu, &offset))
15066 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15067 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
15068 if (attr)
15069 {
5e2b427d 15070 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
15071 {
15072 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15073 additional bit offset from the MSB of the containing
15074 anonymous object to the MSB of the field. We don't
15075 have to do anything special since we don't need to
15076 know the size of the anonymous object. */
f41f5e61 15077 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15078 }
15079 else
15080 {
15081 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15082 MSB of the anonymous object, subtract off the number of
15083 bits from the MSB of the field to the MSB of the
15084 object, and then subtract off the number of bits of
15085 the field itself. The result is the bit offset of
15086 the LSB of the field. */
c906108c
SS
15087 int anonymous_size;
15088 int bit_offset = DW_UNSND (attr);
15089
e142c38c 15090 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15091 if (attr)
15092 {
15093 /* The size of the anonymous object containing
15094 the bit field is explicit, so use the
15095 indicated size (in bytes). */
15096 anonymous_size = DW_UNSND (attr);
15097 }
15098 else
15099 {
15100 /* The size of the anonymous object containing
15101 the bit field must be inferred from the type
15102 attribute of the data member containing the
15103 bit field. */
15104 anonymous_size = TYPE_LENGTH (fp->type);
15105 }
f41f5e61
PA
15106 SET_FIELD_BITPOS (*fp,
15107 (FIELD_BITPOS (*fp)
15108 + anonymous_size * bits_per_byte
15109 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15110 }
15111 }
da5b30da
AA
15112 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15113 if (attr != NULL)
15114 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15115 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15116
15117 /* Get name of field. */
39cbfefa
DJ
15118 fieldname = dwarf2_name (die, cu);
15119 if (fieldname == NULL)
15120 fieldname = "";
d8151005
DJ
15121
15122 /* The name is already allocated along with this objfile, so we don't
15123 need to duplicate it for the type. */
15124 fp->name = fieldname;
c906108c
SS
15125
15126 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15127 pointer or virtual base class pointer) to private. */
e142c38c 15128 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15129 {
d48cc9dd 15130 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15131 new_field->accessibility = DW_ACCESS_private;
15132 fip->non_public_fields = 1;
15133 }
15134 }
a9a9bd0f 15135 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15136 {
a9a9bd0f
DC
15137 /* C++ static member. */
15138
15139 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15140 is a declaration, but all versions of G++ as of this writing
15141 (so through at least 3.2.1) incorrectly generate
15142 DW_TAG_variable tags. */
6e70227d 15143
ff355380 15144 const char *physname;
c906108c 15145
a9a9bd0f 15146 /* Get name of field. */
39cbfefa
DJ
15147 fieldname = dwarf2_name (die, cu);
15148 if (fieldname == NULL)
c906108c
SS
15149 return;
15150
254e6b9e 15151 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15152 if (attr
15153 /* Only create a symbol if this is an external value.
15154 new_symbol checks this and puts the value in the global symbol
15155 table, which we want. If it is not external, new_symbol
15156 will try to put the value in cu->list_in_scope which is wrong. */
15157 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15158 {
15159 /* A static const member, not much different than an enum as far as
15160 we're concerned, except that we can support more types. */
15161 new_symbol (die, NULL, cu);
15162 }
15163
2df3850c 15164 /* Get physical name. */
ff355380 15165 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15166
d8151005
DJ
15167 /* The name is already allocated along with this objfile, so we don't
15168 need to duplicate it for the type. */
15169 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15170 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15171 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15172 }
15173 else if (die->tag == DW_TAG_inheritance)
15174 {
74ac6d43 15175 LONGEST offset;
d4b96c9a 15176
74ac6d43
TT
15177 /* C++ base class field. */
15178 if (handle_data_member_location (die, cu, &offset))
15179 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15180 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15181 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
15182 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15183 fip->nbaseclasses++;
15184 }
15185}
15186
883fd55a
KS
15187/* Can the type given by DIE define another type? */
15188
15189static bool
15190type_can_define_types (const struct die_info *die)
15191{
15192 switch (die->tag)
15193 {
15194 case DW_TAG_typedef:
15195 case DW_TAG_class_type:
15196 case DW_TAG_structure_type:
15197 case DW_TAG_union_type:
15198 case DW_TAG_enumeration_type:
15199 return true;
15200
15201 default:
15202 return false;
15203 }
15204}
15205
15206/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15207
15208static void
883fd55a
KS
15209dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15210 struct dwarf2_cu *cu)
6e70227d 15211{
883fd55a
KS
15212 struct decl_field_list *new_field;
15213 struct decl_field *fp;
98751a41
JK
15214
15215 /* Allocate a new field list entry and link it in. */
883fd55a 15216 new_field = XCNEW (struct decl_field_list);
98751a41
JK
15217 make_cleanup (xfree, new_field);
15218
883fd55a 15219 gdb_assert (type_can_define_types (die));
98751a41
JK
15220
15221 fp = &new_field->field;
15222
883fd55a 15223 /* Get name of field. NULL is okay here, meaning an anonymous type. */
98751a41 15224 fp->name = dwarf2_name (die, cu);
98751a41
JK
15225 fp->type = read_type_die (die, cu);
15226
c191a687
KS
15227 /* Save accessibility. */
15228 enum dwarf_access_attribute accessibility;
15229 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15230 if (attr != NULL)
15231 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15232 else
15233 accessibility = dwarf2_default_access_attribute (die, cu);
15234 switch (accessibility)
15235 {
15236 case DW_ACCESS_public:
15237 /* The assumed value if neither private nor protected. */
15238 break;
15239 case DW_ACCESS_private:
15240 fp->is_private = 1;
15241 break;
15242 case DW_ACCESS_protected:
15243 fp->is_protected = 1;
15244 break;
15245 default:
37534686
KS
15246 complaint (&symfile_complaints,
15247 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15248 }
15249
883fd55a
KS
15250 if (die->tag == DW_TAG_typedef)
15251 {
15252 new_field->next = fip->typedef_field_list;
15253 fip->typedef_field_list = new_field;
15254 fip->typedef_field_list_count++;
15255 }
15256 else
15257 {
15258 new_field->next = fip->nested_types_list;
15259 fip->nested_types_list = new_field;
15260 fip->nested_types_list_count++;
15261 }
98751a41
JK
15262}
15263
c906108c
SS
15264/* Create the vector of fields, and attach it to the type. */
15265
15266static void
fba45db2 15267dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15268 struct dwarf2_cu *cu)
c906108c
SS
15269{
15270 int nfields = fip->nfields;
15271
15272 /* Record the field count, allocate space for the array of fields,
15273 and create blank accessibility bitfields if necessary. */
15274 TYPE_NFIELDS (type) = nfields;
15275 TYPE_FIELDS (type) = (struct field *)
15276 TYPE_ALLOC (type, sizeof (struct field) * nfields);
15277 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
15278
b4ba55a1 15279 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15280 {
15281 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15282
15283 TYPE_FIELD_PRIVATE_BITS (type) =
15284 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15285 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15286
15287 TYPE_FIELD_PROTECTED_BITS (type) =
15288 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15289 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15290
774b6a14
TT
15291 TYPE_FIELD_IGNORE_BITS (type) =
15292 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15293 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15294 }
15295
15296 /* If the type has baseclasses, allocate and clear a bit vector for
15297 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 15298 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
15299 {
15300 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 15301 unsigned char *pointer;
c906108c
SS
15302
15303 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15304 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15305 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
15306 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
15307 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
15308 }
15309
3e43a32a
MS
15310 /* Copy the saved-up fields into the field vector. Start from the head of
15311 the list, adding to the tail of the field array, so that they end up in
15312 the same order in the array in which they were added to the list. */
c906108c
SS
15313 while (nfields-- > 0)
15314 {
7d0ccb61
DJ
15315 struct nextfield *fieldp;
15316
15317 if (fip->fields)
15318 {
15319 fieldp = fip->fields;
15320 fip->fields = fieldp->next;
15321 }
15322 else
15323 {
15324 fieldp = fip->baseclasses;
15325 fip->baseclasses = fieldp->next;
15326 }
15327
15328 TYPE_FIELD (type, nfields) = fieldp->field;
15329 switch (fieldp->accessibility)
c906108c 15330 {
c5aa993b 15331 case DW_ACCESS_private:
b4ba55a1
JB
15332 if (cu->language != language_ada)
15333 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 15334 break;
c906108c 15335
c5aa993b 15336 case DW_ACCESS_protected:
b4ba55a1
JB
15337 if (cu->language != language_ada)
15338 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 15339 break;
c906108c 15340
c5aa993b
JM
15341 case DW_ACCESS_public:
15342 break;
c906108c 15343
c5aa993b
JM
15344 default:
15345 /* Unknown accessibility. Complain and treat it as public. */
15346 {
e2e0b3e5 15347 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 15348 fieldp->accessibility);
c5aa993b
JM
15349 }
15350 break;
c906108c
SS
15351 }
15352 if (nfields < fip->nbaseclasses)
15353 {
7d0ccb61 15354 switch (fieldp->virtuality)
c906108c 15355 {
c5aa993b
JM
15356 case DW_VIRTUALITY_virtual:
15357 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15358 if (cu->language == language_ada)
a73c6dcd 15359 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
15360 SET_TYPE_FIELD_VIRTUAL (type, nfields);
15361 break;
c906108c
SS
15362 }
15363 }
c906108c
SS
15364 }
15365}
15366
7d27a96d
TT
15367/* Return true if this member function is a constructor, false
15368 otherwise. */
15369
15370static int
15371dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15372{
15373 const char *fieldname;
fe978cb0 15374 const char *type_name;
7d27a96d
TT
15375 int len;
15376
15377 if (die->parent == NULL)
15378 return 0;
15379
15380 if (die->parent->tag != DW_TAG_structure_type
15381 && die->parent->tag != DW_TAG_union_type
15382 && die->parent->tag != DW_TAG_class_type)
15383 return 0;
15384
15385 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15386 type_name = dwarf2_name (die->parent, cu);
15387 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15388 return 0;
15389
15390 len = strlen (fieldname);
fe978cb0
PA
15391 return (strncmp (fieldname, type_name, len) == 0
15392 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15393}
15394
c906108c
SS
15395/* Add a member function to the proper fieldlist. */
15396
15397static void
107d2387 15398dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15399 struct type *type, struct dwarf2_cu *cu)
c906108c 15400{
e7c27a73 15401 struct objfile *objfile = cu->objfile;
c906108c
SS
15402 struct attribute *attr;
15403 struct fnfieldlist *flp;
15404 int i;
15405 struct fn_field *fnp;
15d034d0 15406 const char *fieldname;
c906108c 15407 struct nextfnfield *new_fnfield;
f792889a 15408 struct type *this_type;
60d5a603 15409 enum dwarf_access_attribute accessibility;
c906108c 15410
b4ba55a1 15411 if (cu->language == language_ada)
a73c6dcd 15412 error (_("unexpected member function in Ada type"));
b4ba55a1 15413
2df3850c 15414 /* Get name of member function. */
39cbfefa
DJ
15415 fieldname = dwarf2_name (die, cu);
15416 if (fieldname == NULL)
2df3850c 15417 return;
c906108c 15418
c906108c
SS
15419 /* Look up member function name in fieldlist. */
15420 for (i = 0; i < fip->nfnfields; i++)
15421 {
27bfe10e 15422 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
15423 break;
15424 }
15425
15426 /* Create new list element if necessary. */
15427 if (i < fip->nfnfields)
15428 flp = &fip->fnfieldlists[i];
15429 else
15430 {
15431 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
15432 {
15433 fip->fnfieldlists = (struct fnfieldlist *)
15434 xrealloc (fip->fnfieldlists,
15435 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 15436 * sizeof (struct fnfieldlist));
c906108c 15437 if (fip->nfnfields == 0)
c13c43fd 15438 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
15439 }
15440 flp = &fip->fnfieldlists[fip->nfnfields];
15441 flp->name = fieldname;
15442 flp->length = 0;
15443 flp->head = NULL;
3da10d80 15444 i = fip->nfnfields++;
c906108c
SS
15445 }
15446
15447 /* Create a new member function field and chain it to the field list
0963b4bd 15448 entry. */
8d749320 15449 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 15450 make_cleanup (xfree, new_fnfield);
c906108c
SS
15451 memset (new_fnfield, 0, sizeof (struct nextfnfield));
15452 new_fnfield->next = flp->head;
15453 flp->head = new_fnfield;
15454 flp->length++;
15455
15456 /* Fill in the member function field info. */
15457 fnp = &new_fnfield->fnfield;
3da10d80
KS
15458
15459 /* Delay processing of the physname until later. */
9c37b5ae 15460 if (cu->language == language_cplus)
3da10d80
KS
15461 {
15462 add_to_method_list (type, i, flp->length - 1, fieldname,
15463 die, cu);
15464 }
15465 else
15466 {
1d06ead6 15467 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15468 fnp->physname = physname ? physname : "";
15469 }
15470
c906108c 15471 fnp->type = alloc_type (objfile);
f792889a
DJ
15472 this_type = read_type_die (die, cu);
15473 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15474 {
f792889a 15475 int nparams = TYPE_NFIELDS (this_type);
c906108c 15476
f792889a 15477 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15478 of the method itself (TYPE_CODE_METHOD). */
15479 smash_to_method_type (fnp->type, type,
f792889a
DJ
15480 TYPE_TARGET_TYPE (this_type),
15481 TYPE_FIELDS (this_type),
15482 TYPE_NFIELDS (this_type),
15483 TYPE_VARARGS (this_type));
c906108c
SS
15484
15485 /* Handle static member functions.
c5aa993b 15486 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15487 member functions. G++ helps GDB by marking the first
15488 parameter for non-static member functions (which is the this
15489 pointer) as artificial. We obtain this information from
15490 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15491 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15492 fnp->voffset = VOFFSET_STATIC;
15493 }
15494 else
e2e0b3e5 15495 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 15496 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15497
15498 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15499 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15500 fnp->fcontext = die_containing_type (die, cu);
c906108c 15501
3e43a32a
MS
15502 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15503 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15504
15505 /* Get accessibility. */
e142c38c 15506 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15507 if (attr)
aead7601 15508 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15509 else
15510 accessibility = dwarf2_default_access_attribute (die, cu);
15511 switch (accessibility)
c906108c 15512 {
60d5a603
JK
15513 case DW_ACCESS_private:
15514 fnp->is_private = 1;
15515 break;
15516 case DW_ACCESS_protected:
15517 fnp->is_protected = 1;
15518 break;
c906108c
SS
15519 }
15520
b02dede2 15521 /* Check for artificial methods. */
e142c38c 15522 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15523 if (attr && DW_UNSND (attr) != 0)
15524 fnp->is_artificial = 1;
15525
7d27a96d
TT
15526 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15527
0d564a31 15528 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15529 function. For older versions of GCC, this is an offset in the
15530 appropriate virtual table, as specified by DW_AT_containing_type.
15531 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15532 to the object address. */
15533
e142c38c 15534 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15535 if (attr)
8e19ed76 15536 {
aec5aa8b 15537 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15538 {
aec5aa8b
TT
15539 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15540 {
15541 /* Old-style GCC. */
15542 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15543 }
15544 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15545 || (DW_BLOCK (attr)->size > 1
15546 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15547 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15548 {
aec5aa8b
TT
15549 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15550 if ((fnp->voffset % cu->header.addr_size) != 0)
15551 dwarf2_complex_location_expr_complaint ();
15552 else
15553 fnp->voffset /= cu->header.addr_size;
15554 fnp->voffset += 2;
15555 }
15556 else
15557 dwarf2_complex_location_expr_complaint ();
15558
15559 if (!fnp->fcontext)
7e993ebf
KS
15560 {
15561 /* If there is no `this' field and no DW_AT_containing_type,
15562 we cannot actually find a base class context for the
15563 vtable! */
15564 if (TYPE_NFIELDS (this_type) == 0
15565 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15566 {
15567 complaint (&symfile_complaints,
15568 _("cannot determine context for virtual member "
15569 "function \"%s\" (offset %d)"),
9c541725 15570 fieldname, to_underlying (die->sect_off));
7e993ebf
KS
15571 }
15572 else
15573 {
15574 fnp->fcontext
15575 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15576 }
15577 }
aec5aa8b 15578 }
3690dd37 15579 else if (attr_form_is_section_offset (attr))
8e19ed76 15580 {
4d3c2250 15581 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15582 }
15583 else
15584 {
4d3c2250
KB
15585 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15586 fieldname);
8e19ed76 15587 }
0d564a31 15588 }
d48cc9dd
DJ
15589 else
15590 {
15591 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15592 if (attr && DW_UNSND (attr))
15593 {
15594 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15595 complaint (&symfile_complaints,
3e43a32a
MS
15596 _("Member function \"%s\" (offset %d) is virtual "
15597 "but the vtable offset is not specified"),
9c541725 15598 fieldname, to_underlying (die->sect_off));
9655fd1a 15599 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15600 TYPE_CPLUS_DYNAMIC (type) = 1;
15601 }
15602 }
c906108c
SS
15603}
15604
15605/* Create the vector of member function fields, and attach it to the type. */
15606
15607static void
fba45db2 15608dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15609 struct dwarf2_cu *cu)
c906108c
SS
15610{
15611 struct fnfieldlist *flp;
c906108c
SS
15612 int i;
15613
b4ba55a1 15614 if (cu->language == language_ada)
a73c6dcd 15615 error (_("unexpected member functions in Ada type"));
b4ba55a1 15616
c906108c
SS
15617 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15618 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15619 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
15620
15621 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
15622 {
15623 struct nextfnfield *nfp = flp->head;
15624 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15625 int k;
15626
15627 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
15628 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
15629 fn_flp->fn_fields = (struct fn_field *)
15630 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
15631 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 15632 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
15633 }
15634
15635 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
15636}
15637
1168df01
JB
15638/* Returns non-zero if NAME is the name of a vtable member in CU's
15639 language, zero otherwise. */
15640static int
15641is_vtable_name (const char *name, struct dwarf2_cu *cu)
15642{
15643 static const char vptr[] = "_vptr";
15644
9c37b5ae
TT
15645 /* Look for the C++ form of the vtable. */
15646 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15647 return 1;
15648
15649 return 0;
15650}
15651
c0dd20ea 15652/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15653 functions, with the ABI-specified layout. If TYPE describes
15654 such a structure, smash it into a member function type.
61049d3b
DJ
15655
15656 GCC shouldn't do this; it should just output pointer to member DIEs.
15657 This is GCC PR debug/28767. */
c0dd20ea 15658
0b92b5bb
TT
15659static void
15660quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15661{
09e2d7c7 15662 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15663
15664 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15665 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15666 return;
c0dd20ea
DJ
15667
15668 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15669 if (TYPE_FIELD_NAME (type, 0) == NULL
15670 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15671 || TYPE_FIELD_NAME (type, 1) == NULL
15672 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15673 return;
c0dd20ea
DJ
15674
15675 /* Find the type of the method. */
0b92b5bb 15676 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15677 if (pfn_type == NULL
15678 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15679 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15680 return;
c0dd20ea
DJ
15681
15682 /* Look for the "this" argument. */
15683 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15684 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15685 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15686 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15687 return;
c0dd20ea 15688
09e2d7c7 15689 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15690 new_type = alloc_type (objfile);
09e2d7c7 15691 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15692 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15693 TYPE_VARARGS (pfn_type));
0b92b5bb 15694 smash_to_methodptr_type (type, new_type);
c0dd20ea 15695}
1168df01 15696
685b1105 15697
c906108c 15698/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15699 (definition) to create a type for the structure or union. Fill in
15700 the type's name and general properties; the members will not be
83655187
DE
15701 processed until process_structure_scope. A symbol table entry for
15702 the type will also not be done until process_structure_scope (assuming
15703 the type has a name).
c906108c 15704
c767944b
DJ
15705 NOTE: we need to call these functions regardless of whether or not the
15706 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15707 structure or union. This gets the type entered into our set of
83655187 15708 user defined types. */
c906108c 15709
f792889a 15710static struct type *
134d01f1 15711read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15712{
e7c27a73 15713 struct objfile *objfile = cu->objfile;
c906108c
SS
15714 struct type *type;
15715 struct attribute *attr;
15d034d0 15716 const char *name;
c906108c 15717
348e048f
DE
15718 /* If the definition of this type lives in .debug_types, read that type.
15719 Don't follow DW_AT_specification though, that will take us back up
15720 the chain and we want to go down. */
45e58e77 15721 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
15722 if (attr)
15723 {
ac9ec31b 15724 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15725
ac9ec31b 15726 /* The type's CU may not be the same as CU.
02142a6c 15727 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15728 return set_die_type (die, type, cu);
15729 }
15730
c0dd20ea 15731 type = alloc_type (objfile);
c906108c 15732 INIT_CPLUS_SPECIFIC (type);
93311388 15733
39cbfefa
DJ
15734 name = dwarf2_name (die, cu);
15735 if (name != NULL)
c906108c 15736 {
987504bb 15737 if (cu->language == language_cplus
c44af4eb
TT
15738 || cu->language == language_d
15739 || cu->language == language_rust)
63d06c5c 15740 {
15d034d0 15741 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15742
15743 /* dwarf2_full_name might have already finished building the DIE's
15744 type. If so, there is no need to continue. */
15745 if (get_die_type (die, cu) != NULL)
15746 return get_die_type (die, cu);
15747
15748 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
15749 if (die->tag == DW_TAG_structure_type
15750 || die->tag == DW_TAG_class_type)
15751 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
15752 }
15753 else
15754 {
d8151005
DJ
15755 /* The name is already allocated along with this objfile, so
15756 we don't need to duplicate it for the type. */
7d455152 15757 TYPE_TAG_NAME (type) = name;
94af9270
KS
15758 if (die->tag == DW_TAG_class_type)
15759 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 15760 }
c906108c
SS
15761 }
15762
15763 if (die->tag == DW_TAG_structure_type)
15764 {
15765 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15766 }
15767 else if (die->tag == DW_TAG_union_type)
15768 {
15769 TYPE_CODE (type) = TYPE_CODE_UNION;
15770 }
15771 else
15772 {
4753d33b 15773 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15774 }
15775
0cc2414c
TT
15776 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15777 TYPE_DECLARED_CLASS (type) = 1;
15778
e142c38c 15779 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15780 if (attr)
15781 {
155bfbd3
JB
15782 if (attr_form_is_constant (attr))
15783 TYPE_LENGTH (type) = DW_UNSND (attr);
15784 else
15785 {
15786 /* For the moment, dynamic type sizes are not supported
15787 by GDB's struct type. The actual size is determined
15788 on-demand when resolving the type of a given object,
15789 so set the type's length to zero for now. Otherwise,
15790 we record an expression as the length, and that expression
15791 could lead to a very large value, which could eventually
15792 lead to us trying to allocate that much memory when creating
15793 a value of that type. */
15794 TYPE_LENGTH (type) = 0;
15795 }
c906108c
SS
15796 }
15797 else
15798 {
15799 TYPE_LENGTH (type) = 0;
15800 }
15801
5230b05a 15802 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15803 {
5230b05a
WT
15804 /* ICC<14 does not output the required DW_AT_declaration on
15805 incomplete types, but gives them a size of zero. */
422b1cb0 15806 TYPE_STUB (type) = 1;
685b1105
JK
15807 }
15808 else
15809 TYPE_STUB_SUPPORTED (type) = 1;
15810
dc718098 15811 if (die_is_declaration (die, cu))
876cecd0 15812 TYPE_STUB (type) = 1;
a6c727b2
DJ
15813 else if (attr == NULL && die->child == NULL
15814 && producer_is_realview (cu->producer))
15815 /* RealView does not output the required DW_AT_declaration
15816 on incomplete types. */
15817 TYPE_STUB (type) = 1;
dc718098 15818
c906108c
SS
15819 /* We need to add the type field to the die immediately so we don't
15820 infinitely recurse when dealing with pointers to the structure
0963b4bd 15821 type within the structure itself. */
1c379e20 15822 set_die_type (die, type, cu);
c906108c 15823
7e314c57
JK
15824 /* set_die_type should be already done. */
15825 set_descriptive_type (type, die, cu);
15826
c767944b
DJ
15827 return type;
15828}
15829
15830/* Finish creating a structure or union type, including filling in
15831 its members and creating a symbol for it. */
15832
15833static void
15834process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15835{
15836 struct objfile *objfile = cu->objfile;
ca040673 15837 struct die_info *child_die;
c767944b
DJ
15838 struct type *type;
15839
15840 type = get_die_type (die, cu);
15841 if (type == NULL)
15842 type = read_structure_type (die, cu);
15843
e142c38c 15844 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15845 {
15846 struct field_info fi;
2f4732b0 15847 std::vector<struct symbol *> template_args;
c767944b 15848 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
15849
15850 memset (&fi, 0, sizeof (struct field_info));
15851
639d11d3 15852 child_die = die->child;
c906108c
SS
15853
15854 while (child_die && child_die->tag)
15855 {
a9a9bd0f
DC
15856 if (child_die->tag == DW_TAG_member
15857 || child_die->tag == DW_TAG_variable)
c906108c 15858 {
a9a9bd0f
DC
15859 /* NOTE: carlton/2002-11-05: A C++ static data member
15860 should be a DW_TAG_member that is a declaration, but
15861 all versions of G++ as of this writing (so through at
15862 least 3.2.1) incorrectly generate DW_TAG_variable
15863 tags for them instead. */
e7c27a73 15864 dwarf2_add_field (&fi, child_die, cu);
c906108c 15865 }
8713b1b1 15866 else if (child_die->tag == DW_TAG_subprogram)
c906108c 15867 {
e98c9e7c
TT
15868 /* Rust doesn't have member functions in the C++ sense.
15869 However, it does emit ordinary functions as children
15870 of a struct DIE. */
15871 if (cu->language == language_rust)
15872 read_func_scope (child_die, cu);
15873 else
15874 {
15875 /* C++ member function. */
15876 dwarf2_add_member_fn (&fi, child_die, type, cu);
15877 }
c906108c
SS
15878 }
15879 else if (child_die->tag == DW_TAG_inheritance)
15880 {
15881 /* C++ base class field. */
e7c27a73 15882 dwarf2_add_field (&fi, child_die, cu);
c906108c 15883 }
883fd55a
KS
15884 else if (type_can_define_types (child_die))
15885 dwarf2_add_type_defn (&fi, child_die, cu);
34eaf542
TT
15886 else if (child_die->tag == DW_TAG_template_type_param
15887 || child_die->tag == DW_TAG_template_value_param)
15888 {
15889 struct symbol *arg = new_symbol (child_die, NULL, cu);
15890
f1078f66 15891 if (arg != NULL)
2f4732b0 15892 template_args.push_back (arg);
34eaf542
TT
15893 }
15894
c906108c
SS
15895 child_die = sibling_die (child_die);
15896 }
15897
34eaf542 15898 /* Attach template arguments to type. */
2f4732b0 15899 if (!template_args.empty ())
34eaf542
TT
15900 {
15901 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15902 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15903 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15904 = XOBNEWVEC (&objfile->objfile_obstack,
15905 struct symbol *,
15906 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15907 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15908 template_args.data (),
34eaf542
TT
15909 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15910 * sizeof (struct symbol *)));
34eaf542
TT
15911 }
15912
c906108c
SS
15913 /* Attach fields and member functions to the type. */
15914 if (fi.nfields)
e7c27a73 15915 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
15916 if (fi.nfnfields)
15917 {
e7c27a73 15918 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15919
c5aa993b 15920 /* Get the type which refers to the base class (possibly this
c906108c 15921 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15922 class from the DW_AT_containing_type attribute. This use of
15923 DW_AT_containing_type is a GNU extension. */
c906108c 15924
e142c38c 15925 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15926 {
e7c27a73 15927 struct type *t = die_containing_type (die, cu);
c906108c 15928
ae6ae975 15929 set_type_vptr_basetype (type, t);
c906108c
SS
15930 if (type == t)
15931 {
c906108c
SS
15932 int i;
15933
15934 /* Our own class provides vtbl ptr. */
15935 for (i = TYPE_NFIELDS (t) - 1;
15936 i >= TYPE_N_BASECLASSES (t);
15937 --i)
15938 {
0d5cff50 15939 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15940
1168df01 15941 if (is_vtable_name (fieldname, cu))
c906108c 15942 {
ae6ae975 15943 set_type_vptr_fieldno (type, i);
c906108c
SS
15944 break;
15945 }
15946 }
15947
15948 /* Complain if virtual function table field not found. */
15949 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 15950 complaint (&symfile_complaints,
3e43a32a
MS
15951 _("virtual function table pointer "
15952 "not found when defining class '%s'"),
4d3c2250
KB
15953 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
15954 "");
c906108c
SS
15955 }
15956 else
15957 {
ae6ae975 15958 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15959 }
15960 }
f6235d4c 15961 else if (cu->producer
61012eef 15962 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15963 {
15964 /* The IBM XLC compiler does not provide direct indication
15965 of the containing type, but the vtable pointer is
15966 always named __vfp. */
15967
15968 int i;
15969
15970 for (i = TYPE_NFIELDS (type) - 1;
15971 i >= TYPE_N_BASECLASSES (type);
15972 --i)
15973 {
15974 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15975 {
ae6ae975
DE
15976 set_type_vptr_fieldno (type, i);
15977 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15978 break;
15979 }
15980 }
15981 }
c906108c 15982 }
98751a41
JK
15983
15984 /* Copy fi.typedef_field_list linked list elements content into the
15985 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15986 if (fi.typedef_field_list)
15987 {
15988 int i = fi.typedef_field_list_count;
15989
a0d7a4ff 15990 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15991 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15992 = ((struct decl_field *)
224c3ddb 15993 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
15994 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
15995
15996 /* Reverse the list order to keep the debug info elements order. */
15997 while (--i >= 0)
15998 {
883fd55a 15999 struct decl_field *dest, *src;
6e70227d 16000
98751a41
JK
16001 dest = &TYPE_TYPEDEF_FIELD (type, i);
16002 src = &fi.typedef_field_list->field;
16003 fi.typedef_field_list = fi.typedef_field_list->next;
16004 *dest = *src;
16005 }
16006 }
c767944b 16007
883fd55a
KS
16008 /* Copy fi.nested_types_list linked list elements content into the
16009 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16010 if (fi.nested_types_list != NULL && cu->language != language_ada)
16011 {
16012 int i = fi.nested_types_list_count;
16013
16014 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16015 TYPE_NESTED_TYPES_ARRAY (type)
16016 = ((struct decl_field *)
16017 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
16018 TYPE_NESTED_TYPES_COUNT (type) = i;
16019
16020 /* Reverse the list order to keep the debug info elements order. */
16021 while (--i >= 0)
16022 {
16023 struct decl_field *dest, *src;
16024
16025 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
16026 src = &fi.nested_types_list->field;
16027 fi.nested_types_list = fi.nested_types_list->next;
16028 *dest = *src;
16029 }
16030 }
16031
c767944b 16032 do_cleanups (back_to);
c906108c 16033 }
63d06c5c 16034
bb5ed363 16035 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 16036
90aeadfc
DC
16037 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16038 snapshots) has been known to create a die giving a declaration
16039 for a class that has, as a child, a die giving a definition for a
16040 nested class. So we have to process our children even if the
16041 current die is a declaration. Normally, of course, a declaration
16042 won't have any children at all. */
134d01f1 16043
ca040673
DE
16044 child_die = die->child;
16045
90aeadfc
DC
16046 while (child_die != NULL && child_die->tag)
16047 {
16048 if (child_die->tag == DW_TAG_member
16049 || child_die->tag == DW_TAG_variable
34eaf542
TT
16050 || child_die->tag == DW_TAG_inheritance
16051 || child_die->tag == DW_TAG_template_value_param
16052 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16053 {
90aeadfc 16054 /* Do nothing. */
134d01f1 16055 }
90aeadfc
DC
16056 else
16057 process_die (child_die, cu);
134d01f1 16058
90aeadfc 16059 child_die = sibling_die (child_die);
134d01f1
DJ
16060 }
16061
fa4028e9
JB
16062 /* Do not consider external references. According to the DWARF standard,
16063 these DIEs are identified by the fact that they have no byte_size
16064 attribute, and a declaration attribute. */
16065 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16066 || !die_is_declaration (die, cu))
c767944b 16067 new_symbol (die, type, cu);
134d01f1
DJ
16068}
16069
55426c9d
JB
16070/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16071 update TYPE using some information only available in DIE's children. */
16072
16073static void
16074update_enumeration_type_from_children (struct die_info *die,
16075 struct type *type,
16076 struct dwarf2_cu *cu)
16077{
60f7655a 16078 struct die_info *child_die;
55426c9d
JB
16079 int unsigned_enum = 1;
16080 int flag_enum = 1;
16081 ULONGEST mask = 0;
55426c9d 16082
8268c778 16083 auto_obstack obstack;
55426c9d 16084
60f7655a
DE
16085 for (child_die = die->child;
16086 child_die != NULL && child_die->tag;
16087 child_die = sibling_die (child_die))
55426c9d
JB
16088 {
16089 struct attribute *attr;
16090 LONGEST value;
16091 const gdb_byte *bytes;
16092 struct dwarf2_locexpr_baton *baton;
16093 const char *name;
60f7655a 16094
55426c9d
JB
16095 if (child_die->tag != DW_TAG_enumerator)
16096 continue;
16097
16098 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16099 if (attr == NULL)
16100 continue;
16101
16102 name = dwarf2_name (child_die, cu);
16103 if (name == NULL)
16104 name = "<anonymous enumerator>";
16105
16106 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16107 &value, &bytes, &baton);
16108 if (value < 0)
16109 {
16110 unsigned_enum = 0;
16111 flag_enum = 0;
16112 }
16113 else if ((mask & value) != 0)
16114 flag_enum = 0;
16115 else
16116 mask |= value;
16117
16118 /* If we already know that the enum type is neither unsigned, nor
16119 a flag type, no need to look at the rest of the enumerates. */
16120 if (!unsigned_enum && !flag_enum)
16121 break;
55426c9d
JB
16122 }
16123
16124 if (unsigned_enum)
16125 TYPE_UNSIGNED (type) = 1;
16126 if (flag_enum)
16127 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16128}
16129
134d01f1
DJ
16130/* Given a DW_AT_enumeration_type die, set its type. We do not
16131 complete the type's fields yet, or create any symbols. */
c906108c 16132
f792889a 16133static struct type *
134d01f1 16134read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16135{
e7c27a73 16136 struct objfile *objfile = cu->objfile;
c906108c 16137 struct type *type;
c906108c 16138 struct attribute *attr;
0114d602 16139 const char *name;
134d01f1 16140
348e048f
DE
16141 /* If the definition of this type lives in .debug_types, read that type.
16142 Don't follow DW_AT_specification though, that will take us back up
16143 the chain and we want to go down. */
45e58e77 16144 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16145 if (attr)
16146 {
ac9ec31b 16147 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16148
ac9ec31b 16149 /* The type's CU may not be the same as CU.
02142a6c 16150 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16151 return set_die_type (die, type, cu);
16152 }
16153
c906108c
SS
16154 type = alloc_type (objfile);
16155
16156 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16157 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16158 if (name != NULL)
7d455152 16159 TYPE_TAG_NAME (type) = name;
c906108c 16160
0626fc76
TT
16161 attr = dwarf2_attr (die, DW_AT_type, cu);
16162 if (attr != NULL)
16163 {
16164 struct type *underlying_type = die_type (die, cu);
16165
16166 TYPE_TARGET_TYPE (type) = underlying_type;
16167 }
16168
e142c38c 16169 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16170 if (attr)
16171 {
16172 TYPE_LENGTH (type) = DW_UNSND (attr);
16173 }
16174 else
16175 {
16176 TYPE_LENGTH (type) = 0;
16177 }
16178
137033e9
JB
16179 /* The enumeration DIE can be incomplete. In Ada, any type can be
16180 declared as private in the package spec, and then defined only
16181 inside the package body. Such types are known as Taft Amendment
16182 Types. When another package uses such a type, an incomplete DIE
16183 may be generated by the compiler. */
02eb380e 16184 if (die_is_declaration (die, cu))
876cecd0 16185 TYPE_STUB (type) = 1;
02eb380e 16186
0626fc76
TT
16187 /* Finish the creation of this type by using the enum's children.
16188 We must call this even when the underlying type has been provided
16189 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16190 update_enumeration_type_from_children (die, type, cu);
16191
0626fc76
TT
16192 /* If this type has an underlying type that is not a stub, then we
16193 may use its attributes. We always use the "unsigned" attribute
16194 in this situation, because ordinarily we guess whether the type
16195 is unsigned -- but the guess can be wrong and the underlying type
16196 can tell us the reality. However, we defer to a local size
16197 attribute if one exists, because this lets the compiler override
16198 the underlying type if needed. */
16199 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16200 {
16201 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16202 if (TYPE_LENGTH (type) == 0)
16203 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16204 }
16205
3d567982
TT
16206 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16207
f792889a 16208 return set_die_type (die, type, cu);
134d01f1
DJ
16209}
16210
16211/* Given a pointer to a die which begins an enumeration, process all
16212 the dies that define the members of the enumeration, and create the
16213 symbol for the enumeration type.
16214
16215 NOTE: We reverse the order of the element list. */
16216
16217static void
16218process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16219{
f792889a 16220 struct type *this_type;
134d01f1 16221
f792889a
DJ
16222 this_type = get_die_type (die, cu);
16223 if (this_type == NULL)
16224 this_type = read_enumeration_type (die, cu);
9dc481d3 16225
639d11d3 16226 if (die->child != NULL)
c906108c 16227 {
9dc481d3
DE
16228 struct die_info *child_die;
16229 struct symbol *sym;
16230 struct field *fields = NULL;
16231 int num_fields = 0;
15d034d0 16232 const char *name;
9dc481d3 16233
639d11d3 16234 child_die = die->child;
c906108c
SS
16235 while (child_die && child_die->tag)
16236 {
16237 if (child_die->tag != DW_TAG_enumerator)
16238 {
e7c27a73 16239 process_die (child_die, cu);
c906108c
SS
16240 }
16241 else
16242 {
39cbfefa
DJ
16243 name = dwarf2_name (child_die, cu);
16244 if (name)
c906108c 16245 {
f792889a 16246 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16247
16248 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16249 {
16250 fields = (struct field *)
16251 xrealloc (fields,
16252 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16253 * sizeof (struct field));
c906108c
SS
16254 }
16255
3567439c 16256 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16257 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16258 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16259 FIELD_BITSIZE (fields[num_fields]) = 0;
16260
16261 num_fields++;
16262 }
16263 }
16264
16265 child_die = sibling_die (child_die);
16266 }
16267
16268 if (num_fields)
16269 {
f792889a
DJ
16270 TYPE_NFIELDS (this_type) = num_fields;
16271 TYPE_FIELDS (this_type) = (struct field *)
16272 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16273 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16274 sizeof (struct field) * num_fields);
b8c9b27d 16275 xfree (fields);
c906108c 16276 }
c906108c 16277 }
134d01f1 16278
6c83ed52
TT
16279 /* If we are reading an enum from a .debug_types unit, and the enum
16280 is a declaration, and the enum is not the signatured type in the
16281 unit, then we do not want to add a symbol for it. Adding a
16282 symbol would in some cases obscure the true definition of the
16283 enum, giving users an incomplete type when the definition is
16284 actually available. Note that we do not want to do this for all
16285 enums which are just declarations, because C++0x allows forward
16286 enum declarations. */
3019eac3 16287 if (cu->per_cu->is_debug_types
6c83ed52
TT
16288 && die_is_declaration (die, cu))
16289 {
52dc124a 16290 struct signatured_type *sig_type;
6c83ed52 16291
c0f78cd4 16292 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16293 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16294 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16295 return;
16296 }
16297
f792889a 16298 new_symbol (die, this_type, cu);
c906108c
SS
16299}
16300
16301/* Extract all information from a DW_TAG_array_type DIE and put it in
16302 the DIE's type field. For now, this only handles one dimensional
16303 arrays. */
16304
f792889a 16305static struct type *
e7c27a73 16306read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16307{
e7c27a73 16308 struct objfile *objfile = cu->objfile;
c906108c 16309 struct die_info *child_die;
7e314c57 16310 struct type *type;
c906108c 16311 struct type *element_type, *range_type, *index_type;
c906108c 16312 struct attribute *attr;
15d034d0 16313 const char *name;
dc53a7ad 16314 unsigned int bit_stride = 0;
c906108c 16315
e7c27a73 16316 element_type = die_type (die, cu);
c906108c 16317
7e314c57
JK
16318 /* The die_type call above may have already set the type for this DIE. */
16319 type = get_die_type (die, cu);
16320 if (type)
16321 return type;
16322
dc53a7ad
JB
16323 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16324 if (attr != NULL)
16325 bit_stride = DW_UNSND (attr) * 8;
16326
16327 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16328 if (attr != NULL)
16329 bit_stride = DW_UNSND (attr);
16330
c906108c
SS
16331 /* Irix 6.2 native cc creates array types without children for
16332 arrays with unspecified length. */
639d11d3 16333 if (die->child == NULL)
c906108c 16334 {
46bf5051 16335 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16336 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
16337 type = create_array_type_with_stride (NULL, element_type, range_type,
16338 bit_stride);
f792889a 16339 return set_die_type (die, type, cu);
c906108c
SS
16340 }
16341
791afaa2 16342 std::vector<struct type *> range_types;
639d11d3 16343 child_die = die->child;
c906108c
SS
16344 while (child_die && child_die->tag)
16345 {
16346 if (child_die->tag == DW_TAG_subrange_type)
16347 {
f792889a 16348 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16349
f792889a 16350 if (child_type != NULL)
a02abb62 16351 {
0963b4bd
MS
16352 /* The range type was succesfully read. Save it for the
16353 array type creation. */
791afaa2 16354 range_types.push_back (child_type);
a02abb62 16355 }
c906108c
SS
16356 }
16357 child_die = sibling_die (child_die);
16358 }
16359
16360 /* Dwarf2 dimensions are output from left to right, create the
16361 necessary array types in backwards order. */
7ca2d3a3 16362
c906108c 16363 type = element_type;
7ca2d3a3
DL
16364
16365 if (read_array_order (die, cu) == DW_ORD_col_major)
16366 {
16367 int i = 0;
9a619af0 16368
791afaa2 16369 while (i < range_types.size ())
dc53a7ad
JB
16370 type = create_array_type_with_stride (NULL, type, range_types[i++],
16371 bit_stride);
7ca2d3a3
DL
16372 }
16373 else
16374 {
791afaa2 16375 size_t ndim = range_types.size ();
7ca2d3a3 16376 while (ndim-- > 0)
dc53a7ad
JB
16377 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16378 bit_stride);
7ca2d3a3 16379 }
c906108c 16380
f5f8a009
EZ
16381 /* Understand Dwarf2 support for vector types (like they occur on
16382 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16383 array type. This is not part of the Dwarf2/3 standard yet, but a
16384 custom vendor extension. The main difference between a regular
16385 array and the vector variant is that vectors are passed by value
16386 to functions. */
e142c38c 16387 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16388 if (attr)
ea37ba09 16389 make_vector_type (type);
f5f8a009 16390
dbc98a8b
KW
16391 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16392 implementation may choose to implement triple vectors using this
16393 attribute. */
16394 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16395 if (attr)
16396 {
16397 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16398 TYPE_LENGTH (type) = DW_UNSND (attr);
16399 else
3e43a32a
MS
16400 complaint (&symfile_complaints,
16401 _("DW_AT_byte_size for array type smaller "
16402 "than the total size of elements"));
dbc98a8b
KW
16403 }
16404
39cbfefa
DJ
16405 name = dwarf2_name (die, cu);
16406 if (name)
16407 TYPE_NAME (type) = name;
6e70227d 16408
0963b4bd 16409 /* Install the type in the die. */
7e314c57
JK
16410 set_die_type (die, type, cu);
16411
16412 /* set_die_type should be already done. */
b4ba55a1
JB
16413 set_descriptive_type (type, die, cu);
16414
7e314c57 16415 return type;
c906108c
SS
16416}
16417
7ca2d3a3 16418static enum dwarf_array_dim_ordering
6e70227d 16419read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16420{
16421 struct attribute *attr;
16422
16423 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16424
aead7601
SM
16425 if (attr)
16426 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16427
0963b4bd
MS
16428 /* GNU F77 is a special case, as at 08/2004 array type info is the
16429 opposite order to the dwarf2 specification, but data is still
16430 laid out as per normal fortran.
7ca2d3a3 16431
0963b4bd
MS
16432 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16433 version checking. */
7ca2d3a3 16434
905e0470
PM
16435 if (cu->language == language_fortran
16436 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16437 {
16438 return DW_ORD_row_major;
16439 }
16440
6e70227d 16441 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16442 {
16443 case array_column_major:
16444 return DW_ORD_col_major;
16445 case array_row_major:
16446 default:
16447 return DW_ORD_row_major;
16448 };
16449}
16450
72019c9c 16451/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16452 the DIE's type field. */
72019c9c 16453
f792889a 16454static struct type *
72019c9c
GM
16455read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16456{
7e314c57
JK
16457 struct type *domain_type, *set_type;
16458 struct attribute *attr;
f792889a 16459
7e314c57
JK
16460 domain_type = die_type (die, cu);
16461
16462 /* The die_type call above may have already set the type for this DIE. */
16463 set_type = get_die_type (die, cu);
16464 if (set_type)
16465 return set_type;
16466
16467 set_type = create_set_type (NULL, domain_type);
16468
16469 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16470 if (attr)
16471 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16472
f792889a 16473 return set_die_type (die, set_type, cu);
72019c9c 16474}
7ca2d3a3 16475
0971de02
TT
16476/* A helper for read_common_block that creates a locexpr baton.
16477 SYM is the symbol which we are marking as computed.
16478 COMMON_DIE is the DIE for the common block.
16479 COMMON_LOC is the location expression attribute for the common
16480 block itself.
16481 MEMBER_LOC is the location expression attribute for the particular
16482 member of the common block that we are processing.
16483 CU is the CU from which the above come. */
16484
16485static void
16486mark_common_block_symbol_computed (struct symbol *sym,
16487 struct die_info *common_die,
16488 struct attribute *common_loc,
16489 struct attribute *member_loc,
16490 struct dwarf2_cu *cu)
16491{
16492 struct objfile *objfile = dwarf2_per_objfile->objfile;
16493 struct dwarf2_locexpr_baton *baton;
16494 gdb_byte *ptr;
16495 unsigned int cu_off;
16496 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16497 LONGEST offset = 0;
16498
16499 gdb_assert (common_loc && member_loc);
16500 gdb_assert (attr_form_is_block (common_loc));
16501 gdb_assert (attr_form_is_block (member_loc)
16502 || attr_form_is_constant (member_loc));
16503
8d749320 16504 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16505 baton->per_cu = cu->per_cu;
16506 gdb_assert (baton->per_cu);
16507
16508 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16509
16510 if (attr_form_is_constant (member_loc))
16511 {
16512 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16513 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16514 }
16515 else
16516 baton->size += DW_BLOCK (member_loc)->size;
16517
224c3ddb 16518 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16519 baton->data = ptr;
16520
16521 *ptr++ = DW_OP_call4;
9c541725 16522 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16523 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16524 ptr += 4;
16525
16526 if (attr_form_is_constant (member_loc))
16527 {
16528 *ptr++ = DW_OP_addr;
16529 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16530 ptr += cu->header.addr_size;
16531 }
16532 else
16533 {
16534 /* We have to copy the data here, because DW_OP_call4 will only
16535 use a DW_AT_location attribute. */
16536 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16537 ptr += DW_BLOCK (member_loc)->size;
16538 }
16539
16540 *ptr++ = DW_OP_plus;
16541 gdb_assert (ptr - baton->data == baton->size);
16542
0971de02 16543 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16544 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16545}
16546
4357ac6c
TT
16547/* Create appropriate locally-scoped variables for all the
16548 DW_TAG_common_block entries. Also create a struct common_block
16549 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16550 is used to sepate the common blocks name namespace from regular
16551 variable names. */
c906108c
SS
16552
16553static void
e7c27a73 16554read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16555{
0971de02
TT
16556 struct attribute *attr;
16557
16558 attr = dwarf2_attr (die, DW_AT_location, cu);
16559 if (attr)
16560 {
16561 /* Support the .debug_loc offsets. */
16562 if (attr_form_is_block (attr))
16563 {
16564 /* Ok. */
16565 }
16566 else if (attr_form_is_section_offset (attr))
16567 {
16568 dwarf2_complex_location_expr_complaint ();
16569 attr = NULL;
16570 }
16571 else
16572 {
16573 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16574 "common block member");
16575 attr = NULL;
16576 }
16577 }
16578
639d11d3 16579 if (die->child != NULL)
c906108c 16580 {
4357ac6c
TT
16581 struct objfile *objfile = cu->objfile;
16582 struct die_info *child_die;
16583 size_t n_entries = 0, size;
16584 struct common_block *common_block;
16585 struct symbol *sym;
74ac6d43 16586
4357ac6c
TT
16587 for (child_die = die->child;
16588 child_die && child_die->tag;
16589 child_die = sibling_die (child_die))
16590 ++n_entries;
16591
16592 size = (sizeof (struct common_block)
16593 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16594 common_block
16595 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16596 size);
4357ac6c
TT
16597 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16598 common_block->n_entries = 0;
16599
16600 for (child_die = die->child;
16601 child_die && child_die->tag;
16602 child_die = sibling_die (child_die))
16603 {
16604 /* Create the symbol in the DW_TAG_common_block block in the current
16605 symbol scope. */
e7c27a73 16606 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16607 if (sym != NULL)
16608 {
16609 struct attribute *member_loc;
16610
16611 common_block->contents[common_block->n_entries++] = sym;
16612
16613 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16614 cu);
16615 if (member_loc)
16616 {
16617 /* GDB has handled this for a long time, but it is
16618 not specified by DWARF. It seems to have been
16619 emitted by gfortran at least as recently as:
16620 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16621 complaint (&symfile_complaints,
16622 _("Variable in common block has "
16623 "DW_AT_data_member_location "
16624 "- DIE at 0x%x [in module %s]"),
9c541725 16625 to_underlying (child_die->sect_off),
4262abfb 16626 objfile_name (cu->objfile));
0971de02
TT
16627
16628 if (attr_form_is_section_offset (member_loc))
16629 dwarf2_complex_location_expr_complaint ();
16630 else if (attr_form_is_constant (member_loc)
16631 || attr_form_is_block (member_loc))
16632 {
16633 if (attr)
16634 mark_common_block_symbol_computed (sym, die, attr,
16635 member_loc, cu);
16636 }
16637 else
16638 dwarf2_complex_location_expr_complaint ();
16639 }
16640 }
c906108c 16641 }
4357ac6c
TT
16642
16643 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16644 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16645 }
16646}
16647
0114d602 16648/* Create a type for a C++ namespace. */
d9fa45fe 16649
0114d602
DJ
16650static struct type *
16651read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16652{
e7c27a73 16653 struct objfile *objfile = cu->objfile;
0114d602 16654 const char *previous_prefix, *name;
9219021c 16655 int is_anonymous;
0114d602
DJ
16656 struct type *type;
16657
16658 /* For extensions, reuse the type of the original namespace. */
16659 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16660 {
16661 struct die_info *ext_die;
16662 struct dwarf2_cu *ext_cu = cu;
9a619af0 16663
0114d602
DJ
16664 ext_die = dwarf2_extension (die, &ext_cu);
16665 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16666
16667 /* EXT_CU may not be the same as CU.
02142a6c 16668 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16669 return set_die_type (die, type, cu);
16670 }
9219021c 16671
e142c38c 16672 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16673
16674 /* Now build the name of the current namespace. */
16675
0114d602
DJ
16676 previous_prefix = determine_prefix (die, cu);
16677 if (previous_prefix[0] != '\0')
16678 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16679 previous_prefix, name, 0, cu);
0114d602
DJ
16680
16681 /* Create the type. */
19f392bc 16682 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
16683 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16684
60531b24 16685 return set_die_type (die, type, cu);
0114d602
DJ
16686}
16687
22cee43f 16688/* Read a namespace scope. */
0114d602
DJ
16689
16690static void
16691read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16692{
16693 struct objfile *objfile = cu->objfile;
0114d602 16694 int is_anonymous;
9219021c 16695
5c4e30ca
DC
16696 /* Add a symbol associated to this if we haven't seen the namespace
16697 before. Also, add a using directive if it's an anonymous
16698 namespace. */
9219021c 16699
f2f0e013 16700 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16701 {
16702 struct type *type;
16703
0114d602 16704 type = read_type_die (die, cu);
e7c27a73 16705 new_symbol (die, type, cu);
5c4e30ca 16706
e8e80198 16707 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16708 if (is_anonymous)
0114d602
DJ
16709 {
16710 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16711
eb1e02fd 16712 std::vector<const char *> excludes;
22cee43f
PMR
16713 add_using_directive (using_directives (cu->language),
16714 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16715 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16716 }
5c4e30ca 16717 }
9219021c 16718
639d11d3 16719 if (die->child != NULL)
d9fa45fe 16720 {
639d11d3 16721 struct die_info *child_die = die->child;
6e70227d 16722
d9fa45fe
DC
16723 while (child_die && child_die->tag)
16724 {
e7c27a73 16725 process_die (child_die, cu);
d9fa45fe
DC
16726 child_die = sibling_die (child_die);
16727 }
16728 }
38d518c9
EZ
16729}
16730
f55ee35c
JK
16731/* Read a Fortran module as type. This DIE can be only a declaration used for
16732 imported module. Still we need that type as local Fortran "use ... only"
16733 declaration imports depend on the created type in determine_prefix. */
16734
16735static struct type *
16736read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16737{
16738 struct objfile *objfile = cu->objfile;
15d034d0 16739 const char *module_name;
f55ee35c
JK
16740 struct type *type;
16741
16742 module_name = dwarf2_name (die, cu);
16743 if (!module_name)
3e43a32a
MS
16744 complaint (&symfile_complaints,
16745 _("DW_TAG_module has no name, offset 0x%x"),
9c541725 16746 to_underlying (die->sect_off));
19f392bc 16747 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
16748
16749 /* determine_prefix uses TYPE_TAG_NAME. */
16750 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16751
16752 return set_die_type (die, type, cu);
16753}
16754
5d7cb8df
JK
16755/* Read a Fortran module. */
16756
16757static void
16758read_module (struct die_info *die, struct dwarf2_cu *cu)
16759{
16760 struct die_info *child_die = die->child;
530e8392
KB
16761 struct type *type;
16762
16763 type = read_type_die (die, cu);
16764 new_symbol (die, type, cu);
5d7cb8df 16765
5d7cb8df
JK
16766 while (child_die && child_die->tag)
16767 {
16768 process_die (child_die, cu);
16769 child_die = sibling_die (child_die);
16770 }
16771}
16772
38d518c9
EZ
16773/* Return the name of the namespace represented by DIE. Set
16774 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16775 namespace. */
16776
16777static const char *
e142c38c 16778namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16779{
16780 struct die_info *current_die;
16781 const char *name = NULL;
16782
16783 /* Loop through the extensions until we find a name. */
16784
16785 for (current_die = die;
16786 current_die != NULL;
f2f0e013 16787 current_die = dwarf2_extension (die, &cu))
38d518c9 16788 {
96553a0c
DE
16789 /* We don't use dwarf2_name here so that we can detect the absence
16790 of a name -> anonymous namespace. */
7d45c7c3 16791 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16792
38d518c9
EZ
16793 if (name != NULL)
16794 break;
16795 }
16796
16797 /* Is it an anonymous namespace? */
16798
16799 *is_anonymous = (name == NULL);
16800 if (*is_anonymous)
2b1dbab0 16801 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16802
16803 return name;
d9fa45fe
DC
16804}
16805
c906108c
SS
16806/* Extract all information from a DW_TAG_pointer_type DIE and add to
16807 the user defined type vector. */
16808
f792889a 16809static struct type *
e7c27a73 16810read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16811{
5e2b427d 16812 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 16813 struct comp_unit_head *cu_header = &cu->header;
c906108c 16814 struct type *type;
8b2dbe47
KB
16815 struct attribute *attr_byte_size;
16816 struct attribute *attr_address_class;
16817 int byte_size, addr_class;
7e314c57
JK
16818 struct type *target_type;
16819
16820 target_type = die_type (die, cu);
c906108c 16821
7e314c57
JK
16822 /* The die_type call above may have already set the type for this DIE. */
16823 type = get_die_type (die, cu);
16824 if (type)
16825 return type;
16826
16827 type = lookup_pointer_type (target_type);
8b2dbe47 16828
e142c38c 16829 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16830 if (attr_byte_size)
16831 byte_size = DW_UNSND (attr_byte_size);
c906108c 16832 else
8b2dbe47
KB
16833 byte_size = cu_header->addr_size;
16834
e142c38c 16835 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16836 if (attr_address_class)
16837 addr_class = DW_UNSND (attr_address_class);
16838 else
16839 addr_class = DW_ADDR_none;
16840
16841 /* If the pointer size or address class is different than the
16842 default, create a type variant marked as such and set the
16843 length accordingly. */
16844 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 16845 {
5e2b427d 16846 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16847 {
16848 int type_flags;
16849
849957d9 16850 type_flags = gdbarch_address_class_type_flags
5e2b427d 16851 (gdbarch, byte_size, addr_class);
876cecd0
TT
16852 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16853 == 0);
8b2dbe47
KB
16854 type = make_type_with_address_space (type, type_flags);
16855 }
16856 else if (TYPE_LENGTH (type) != byte_size)
16857 {
3e43a32a
MS
16858 complaint (&symfile_complaints,
16859 _("invalid pointer size %d"), byte_size);
8b2dbe47 16860 }
6e70227d 16861 else
9a619af0
MS
16862 {
16863 /* Should we also complain about unhandled address classes? */
16864 }
c906108c 16865 }
8b2dbe47
KB
16866
16867 TYPE_LENGTH (type) = byte_size;
f792889a 16868 return set_die_type (die, type, cu);
c906108c
SS
16869}
16870
16871/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16872 the user defined type vector. */
16873
f792889a 16874static struct type *
e7c27a73 16875read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16876{
16877 struct type *type;
16878 struct type *to_type;
16879 struct type *domain;
16880
e7c27a73
DJ
16881 to_type = die_type (die, cu);
16882 domain = die_containing_type (die, cu);
0d5de010 16883
7e314c57
JK
16884 /* The calls above may have already set the type for this DIE. */
16885 type = get_die_type (die, cu);
16886 if (type)
16887 return type;
16888
0d5de010
DJ
16889 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16890 type = lookup_methodptr_type (to_type);
7078baeb
TT
16891 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16892 {
16893 struct type *new_type = alloc_type (cu->objfile);
16894
16895 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16896 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16897 TYPE_VARARGS (to_type));
16898 type = lookup_methodptr_type (new_type);
16899 }
0d5de010
DJ
16900 else
16901 type = lookup_memberptr_type (to_type, domain);
c906108c 16902
f792889a 16903 return set_die_type (die, type, cu);
c906108c
SS
16904}
16905
4297a3f0 16906/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16907 the user defined type vector. */
16908
f792889a 16909static struct type *
4297a3f0
AV
16910read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16911 enum type_code refcode)
c906108c 16912{
e7c27a73 16913 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16914 struct type *type, *target_type;
c906108c
SS
16915 struct attribute *attr;
16916
4297a3f0
AV
16917 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16918
7e314c57
JK
16919 target_type = die_type (die, cu);
16920
16921 /* The die_type call above may have already set the type for this DIE. */
16922 type = get_die_type (die, cu);
16923 if (type)
16924 return type;
16925
4297a3f0 16926 type = lookup_reference_type (target_type, refcode);
e142c38c 16927 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16928 if (attr)
16929 {
16930 TYPE_LENGTH (type) = DW_UNSND (attr);
16931 }
16932 else
16933 {
107d2387 16934 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16935 }
f792889a 16936 return set_die_type (die, type, cu);
c906108c
SS
16937}
16938
cf363f18
MW
16939/* Add the given cv-qualifiers to the element type of the array. GCC
16940 outputs DWARF type qualifiers that apply to an array, not the
16941 element type. But GDB relies on the array element type to carry
16942 the cv-qualifiers. This mimics section 6.7.3 of the C99
16943 specification. */
16944
16945static struct type *
16946add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16947 struct type *base_type, int cnst, int voltl)
16948{
16949 struct type *el_type, *inner_array;
16950
16951 base_type = copy_type (base_type);
16952 inner_array = base_type;
16953
16954 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16955 {
16956 TYPE_TARGET_TYPE (inner_array) =
16957 copy_type (TYPE_TARGET_TYPE (inner_array));
16958 inner_array = TYPE_TARGET_TYPE (inner_array);
16959 }
16960
16961 el_type = TYPE_TARGET_TYPE (inner_array);
16962 cnst |= TYPE_CONST (el_type);
16963 voltl |= TYPE_VOLATILE (el_type);
16964 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16965
16966 return set_die_type (die, base_type, cu);
16967}
16968
f792889a 16969static struct type *
e7c27a73 16970read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16971{
f792889a 16972 struct type *base_type, *cv_type;
c906108c 16973
e7c27a73 16974 base_type = die_type (die, cu);
7e314c57
JK
16975
16976 /* The die_type call above may have already set the type for this DIE. */
16977 cv_type = get_die_type (die, cu);
16978 if (cv_type)
16979 return cv_type;
16980
2f608a3a
KW
16981 /* In case the const qualifier is applied to an array type, the element type
16982 is so qualified, not the array type (section 6.7.3 of C99). */
16983 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16984 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16985
f792889a
DJ
16986 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16987 return set_die_type (die, cv_type, cu);
c906108c
SS
16988}
16989
f792889a 16990static struct type *
e7c27a73 16991read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16992{
f792889a 16993 struct type *base_type, *cv_type;
c906108c 16994
e7c27a73 16995 base_type = die_type (die, cu);
7e314c57
JK
16996
16997 /* The die_type call above may have already set the type for this DIE. */
16998 cv_type = get_die_type (die, cu);
16999 if (cv_type)
17000 return cv_type;
17001
cf363f18
MW
17002 /* In case the volatile qualifier is applied to an array type, the
17003 element type is so qualified, not the array type (section 6.7.3
17004 of C99). */
17005 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17006 return add_array_cv_type (die, cu, base_type, 0, 1);
17007
f792889a
DJ
17008 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17009 return set_die_type (die, cv_type, cu);
c906108c
SS
17010}
17011
06d66ee9
TT
17012/* Handle DW_TAG_restrict_type. */
17013
17014static struct type *
17015read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17016{
17017 struct type *base_type, *cv_type;
17018
17019 base_type = die_type (die, cu);
17020
17021 /* The die_type call above may have already set the type for this DIE. */
17022 cv_type = get_die_type (die, cu);
17023 if (cv_type)
17024 return cv_type;
17025
17026 cv_type = make_restrict_type (base_type);
17027 return set_die_type (die, cv_type, cu);
17028}
17029
a2c2acaf
MW
17030/* Handle DW_TAG_atomic_type. */
17031
17032static struct type *
17033read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17034{
17035 struct type *base_type, *cv_type;
17036
17037 base_type = die_type (die, cu);
17038
17039 /* The die_type call above may have already set the type for this DIE. */
17040 cv_type = get_die_type (die, cu);
17041 if (cv_type)
17042 return cv_type;
17043
17044 cv_type = make_atomic_type (base_type);
17045 return set_die_type (die, cv_type, cu);
17046}
17047
c906108c
SS
17048/* Extract all information from a DW_TAG_string_type DIE and add to
17049 the user defined type vector. It isn't really a user defined type,
17050 but it behaves like one, with other DIE's using an AT_user_def_type
17051 attribute to reference it. */
17052
f792889a 17053static struct type *
e7c27a73 17054read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17055{
e7c27a73 17056 struct objfile *objfile = cu->objfile;
3b7538c0 17057 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17058 struct type *type, *range_type, *index_type, *char_type;
17059 struct attribute *attr;
17060 unsigned int length;
17061
e142c38c 17062 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17063 if (attr)
17064 {
17065 length = DW_UNSND (attr);
17066 }
17067 else
17068 {
0963b4bd 17069 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17070 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17071 if (attr)
17072 {
17073 length = DW_UNSND (attr);
17074 }
17075 else
17076 {
17077 length = 1;
17078 }
c906108c 17079 }
6ccb9162 17080
46bf5051 17081 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17082 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17083 char_type = language_string_char_type (cu->language_defn, gdbarch);
17084 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17085
f792889a 17086 return set_die_type (die, type, cu);
c906108c
SS
17087}
17088
4d804846
JB
17089/* Assuming that DIE corresponds to a function, returns nonzero
17090 if the function is prototyped. */
17091
17092static int
17093prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17094{
17095 struct attribute *attr;
17096
17097 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17098 if (attr && (DW_UNSND (attr) != 0))
17099 return 1;
17100
17101 /* The DWARF standard implies that the DW_AT_prototyped attribute
17102 is only meaninful for C, but the concept also extends to other
17103 languages that allow unprototyped functions (Eg: Objective C).
17104 For all other languages, assume that functions are always
17105 prototyped. */
17106 if (cu->language != language_c
17107 && cu->language != language_objc
17108 && cu->language != language_opencl)
17109 return 1;
17110
17111 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17112 prototyped and unprototyped functions; default to prototyped,
17113 since that is more common in modern code (and RealView warns
17114 about unprototyped functions). */
17115 if (producer_is_realview (cu->producer))
17116 return 1;
17117
17118 return 0;
17119}
17120
c906108c
SS
17121/* Handle DIES due to C code like:
17122
17123 struct foo
c5aa993b
JM
17124 {
17125 int (*funcp)(int a, long l);
17126 int b;
17127 };
c906108c 17128
0963b4bd 17129 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17130
f792889a 17131static struct type *
e7c27a73 17132read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17133{
bb5ed363 17134 struct objfile *objfile = cu->objfile;
0963b4bd
MS
17135 struct type *type; /* Type that this function returns. */
17136 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17137 struct attribute *attr;
17138
e7c27a73 17139 type = die_type (die, cu);
7e314c57
JK
17140
17141 /* The die_type call above may have already set the type for this DIE. */
17142 ftype = get_die_type (die, cu);
17143 if (ftype)
17144 return ftype;
17145
0c8b41f1 17146 ftype = lookup_function_type (type);
c906108c 17147
4d804846 17148 if (prototyped_function_p (die, cu))
a6c727b2 17149 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17150
c055b101
CV
17151 /* Store the calling convention in the type if it's available in
17152 the subroutine die. Otherwise set the calling convention to
17153 the default value DW_CC_normal. */
17154 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17155 if (attr)
17156 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17157 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17158 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17159 else
17160 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17161
743649fd
MW
17162 /* Record whether the function returns normally to its caller or not
17163 if the DWARF producer set that information. */
17164 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17165 if (attr && (DW_UNSND (attr) != 0))
17166 TYPE_NO_RETURN (ftype) = 1;
17167
76c10ea2
GM
17168 /* We need to add the subroutine type to the die immediately so
17169 we don't infinitely recurse when dealing with parameters
0963b4bd 17170 declared as the same subroutine type. */
76c10ea2 17171 set_die_type (die, ftype, cu);
6e70227d 17172
639d11d3 17173 if (die->child != NULL)
c906108c 17174 {
bb5ed363 17175 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17176 struct die_info *child_die;
8072405b 17177 int nparams, iparams;
c906108c
SS
17178
17179 /* Count the number of parameters.
17180 FIXME: GDB currently ignores vararg functions, but knows about
17181 vararg member functions. */
8072405b 17182 nparams = 0;
639d11d3 17183 child_die = die->child;
c906108c
SS
17184 while (child_die && child_die->tag)
17185 {
17186 if (child_die->tag == DW_TAG_formal_parameter)
17187 nparams++;
17188 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17189 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17190 child_die = sibling_die (child_die);
17191 }
17192
17193 /* Allocate storage for parameters and fill them in. */
17194 TYPE_NFIELDS (ftype) = nparams;
17195 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17196 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17197
8072405b
JK
17198 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17199 even if we error out during the parameters reading below. */
17200 for (iparams = 0; iparams < nparams; iparams++)
17201 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17202
17203 iparams = 0;
639d11d3 17204 child_die = die->child;
c906108c
SS
17205 while (child_die && child_die->tag)
17206 {
17207 if (child_die->tag == DW_TAG_formal_parameter)
17208 {
3ce3b1ba
PA
17209 struct type *arg_type;
17210
17211 /* DWARF version 2 has no clean way to discern C++
17212 static and non-static member functions. G++ helps
17213 GDB by marking the first parameter for non-static
17214 member functions (which is the this pointer) as
17215 artificial. We pass this information to
17216 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17217
17218 DWARF version 3 added DW_AT_object_pointer, which GCC
17219 4.5 does not yet generate. */
e142c38c 17220 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17221 if (attr)
17222 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17223 else
9c37b5ae 17224 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17225 arg_type = die_type (child_die, cu);
17226
17227 /* RealView does not mark THIS as const, which the testsuite
17228 expects. GCC marks THIS as const in method definitions,
17229 but not in the class specifications (GCC PR 43053). */
17230 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17231 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17232 {
17233 int is_this = 0;
17234 struct dwarf2_cu *arg_cu = cu;
17235 const char *name = dwarf2_name (child_die, cu);
17236
17237 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17238 if (attr)
17239 {
17240 /* If the compiler emits this, use it. */
17241 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17242 is_this = 1;
17243 }
17244 else if (name && strcmp (name, "this") == 0)
17245 /* Function definitions will have the argument names. */
17246 is_this = 1;
17247 else if (name == NULL && iparams == 0)
17248 /* Declarations may not have the names, so like
17249 elsewhere in GDB, assume an artificial first
17250 argument is "this". */
17251 is_this = 1;
17252
17253 if (is_this)
17254 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17255 arg_type, 0);
17256 }
17257
17258 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17259 iparams++;
17260 }
17261 child_die = sibling_die (child_die);
17262 }
17263 }
17264
76c10ea2 17265 return ftype;
c906108c
SS
17266}
17267
f792889a 17268static struct type *
e7c27a73 17269read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17270{
e7c27a73 17271 struct objfile *objfile = cu->objfile;
0114d602 17272 const char *name = NULL;
3c8e0968 17273 struct type *this_type, *target_type;
c906108c 17274
94af9270 17275 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17276 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17277 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17278 set_die_type (die, this_type, cu);
3c8e0968
DE
17279 target_type = die_type (die, cu);
17280 if (target_type != this_type)
17281 TYPE_TARGET_TYPE (this_type) = target_type;
17282 else
17283 {
17284 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17285 spec and cause infinite loops in GDB. */
17286 complaint (&symfile_complaints,
17287 _("Self-referential DW_TAG_typedef "
17288 "- DIE at 0x%x [in module %s]"),
9c541725 17289 to_underlying (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17290 TYPE_TARGET_TYPE (this_type) = NULL;
17291 }
f792889a 17292 return this_type;
c906108c
SS
17293}
17294
9b790ce7
UW
17295/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17296 (which may be different from NAME) to the architecture back-end to allow
17297 it to guess the correct format if necessary. */
17298
17299static struct type *
17300dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17301 const char *name_hint)
17302{
17303 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17304 const struct floatformat **format;
17305 struct type *type;
17306
17307 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17308 if (format)
17309 type = init_float_type (objfile, bits, name, format);
17310 else
77b7c781 17311 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17312
17313 return type;
17314}
17315
c906108c
SS
17316/* Find a representation of a given base type and install
17317 it in the TYPE field of the die. */
17318
f792889a 17319static struct type *
e7c27a73 17320read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17321{
e7c27a73 17322 struct objfile *objfile = cu->objfile;
c906108c
SS
17323 struct type *type;
17324 struct attribute *attr;
19f392bc 17325 int encoding = 0, bits = 0;
15d034d0 17326 const char *name;
c906108c 17327
e142c38c 17328 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17329 if (attr)
17330 {
17331 encoding = DW_UNSND (attr);
17332 }
e142c38c 17333 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17334 if (attr)
17335 {
19f392bc 17336 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17337 }
39cbfefa 17338 name = dwarf2_name (die, cu);
6ccb9162 17339 if (!name)
c906108c 17340 {
6ccb9162
UW
17341 complaint (&symfile_complaints,
17342 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17343 }
6ccb9162
UW
17344
17345 switch (encoding)
c906108c 17346 {
6ccb9162
UW
17347 case DW_ATE_address:
17348 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17349 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17350 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17351 break;
17352 case DW_ATE_boolean:
19f392bc 17353 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17354 break;
17355 case DW_ATE_complex_float:
9b790ce7 17356 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 17357 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17358 break;
17359 case DW_ATE_decimal_float:
19f392bc 17360 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17361 break;
17362 case DW_ATE_float:
9b790ce7 17363 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17364 break;
17365 case DW_ATE_signed:
19f392bc 17366 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17367 break;
17368 case DW_ATE_unsigned:
3b2b8fea
TT
17369 if (cu->language == language_fortran
17370 && name
61012eef 17371 && startswith (name, "character("))
19f392bc
UW
17372 type = init_character_type (objfile, bits, 1, name);
17373 else
17374 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
17375 break;
17376 case DW_ATE_signed_char:
6e70227d 17377 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17378 || cu->language == language_pascal
17379 || cu->language == language_fortran)
19f392bc
UW
17380 type = init_character_type (objfile, bits, 0, name);
17381 else
17382 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17383 break;
17384 case DW_ATE_unsigned_char:
868a0084 17385 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17386 || cu->language == language_pascal
c44af4eb
TT
17387 || cu->language == language_fortran
17388 || cu->language == language_rust)
19f392bc
UW
17389 type = init_character_type (objfile, bits, 1, name);
17390 else
17391 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 17392 break;
75079b2b 17393 case DW_ATE_UTF:
53e710ac
PA
17394 {
17395 gdbarch *arch = get_objfile_arch (objfile);
17396
17397 if (bits == 16)
17398 type = builtin_type (arch)->builtin_char16;
17399 else if (bits == 32)
17400 type = builtin_type (arch)->builtin_char32;
17401 else
17402 {
17403 complaint (&symfile_complaints,
17404 _("unsupported DW_ATE_UTF bit size: '%d'"),
17405 bits);
17406 type = init_integer_type (objfile, bits, 1, name);
17407 }
17408 return set_die_type (die, type, cu);
17409 }
75079b2b
TT
17410 break;
17411
6ccb9162
UW
17412 default:
17413 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17414 dwarf_type_encoding_name (encoding));
77b7c781 17415 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17416 break;
c906108c 17417 }
6ccb9162 17418
0114d602 17419 if (name && strcmp (name, "char") == 0)
876cecd0 17420 TYPE_NOSIGN (type) = 1;
0114d602 17421
f792889a 17422 return set_die_type (die, type, cu);
c906108c
SS
17423}
17424
80180f79
SA
17425/* Parse dwarf attribute if it's a block, reference or constant and put the
17426 resulting value of the attribute into struct bound_prop.
17427 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17428
17429static int
17430attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17431 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17432{
17433 struct dwarf2_property_baton *baton;
17434 struct obstack *obstack = &cu->objfile->objfile_obstack;
17435
17436 if (attr == NULL || prop == NULL)
17437 return 0;
17438
17439 if (attr_form_is_block (attr))
17440 {
8d749320 17441 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
17442 baton->referenced_type = NULL;
17443 baton->locexpr.per_cu = cu->per_cu;
17444 baton->locexpr.size = DW_BLOCK (attr)->size;
17445 baton->locexpr.data = DW_BLOCK (attr)->data;
17446 prop->data.baton = baton;
17447 prop->kind = PROP_LOCEXPR;
17448 gdb_assert (prop->data.baton != NULL);
17449 }
17450 else if (attr_form_is_ref (attr))
17451 {
17452 struct dwarf2_cu *target_cu = cu;
17453 struct die_info *target_die;
17454 struct attribute *target_attr;
17455
17456 target_die = follow_die_ref (die, attr, &target_cu);
17457 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17458 if (target_attr == NULL)
17459 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17460 target_cu);
80180f79
SA
17461 if (target_attr == NULL)
17462 return 0;
17463
df25ebbd 17464 switch (target_attr->name)
80180f79 17465 {
df25ebbd
JB
17466 case DW_AT_location:
17467 if (attr_form_is_section_offset (target_attr))
17468 {
8d749320 17469 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17470 baton->referenced_type = die_type (target_die, target_cu);
17471 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17472 prop->data.baton = baton;
17473 prop->kind = PROP_LOCLIST;
17474 gdb_assert (prop->data.baton != NULL);
17475 }
17476 else if (attr_form_is_block (target_attr))
17477 {
8d749320 17478 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17479 baton->referenced_type = die_type (target_die, target_cu);
17480 baton->locexpr.per_cu = cu->per_cu;
17481 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17482 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17483 prop->data.baton = baton;
17484 prop->kind = PROP_LOCEXPR;
17485 gdb_assert (prop->data.baton != NULL);
17486 }
17487 else
17488 {
17489 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17490 "dynamic property");
17491 return 0;
17492 }
17493 break;
17494 case DW_AT_data_member_location:
17495 {
17496 LONGEST offset;
17497
17498 if (!handle_data_member_location (target_die, target_cu,
17499 &offset))
17500 return 0;
17501
8d749320 17502 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
17503 baton->referenced_type = read_type_die (target_die->parent,
17504 target_cu);
df25ebbd
JB
17505 baton->offset_info.offset = offset;
17506 baton->offset_info.type = die_type (target_die, target_cu);
17507 prop->data.baton = baton;
17508 prop->kind = PROP_ADDR_OFFSET;
17509 break;
17510 }
80180f79
SA
17511 }
17512 }
17513 else if (attr_form_is_constant (attr))
17514 {
17515 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17516 prop->kind = PROP_CONST;
17517 }
17518 else
17519 {
17520 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17521 dwarf2_name (die, cu));
17522 return 0;
17523 }
17524
17525 return 1;
17526}
17527
a02abb62
JB
17528/* Read the given DW_AT_subrange DIE. */
17529
f792889a 17530static struct type *
a02abb62
JB
17531read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17532{
4c9ad8c2 17533 struct type *base_type, *orig_base_type;
a02abb62
JB
17534 struct type *range_type;
17535 struct attribute *attr;
729efb13 17536 struct dynamic_prop low, high;
4fae6e18 17537 int low_default_is_valid;
c451ebe5 17538 int high_bound_is_count = 0;
15d034d0 17539 const char *name;
43bbcdc2 17540 LONGEST negative_mask;
e77813c8 17541
4c9ad8c2
TT
17542 orig_base_type = die_type (die, cu);
17543 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17544 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17545 creating the range type, but we use the result of check_typedef
17546 when examining properties of the type. */
17547 base_type = check_typedef (orig_base_type);
a02abb62 17548
7e314c57
JK
17549 /* The die_type call above may have already set the type for this DIE. */
17550 range_type = get_die_type (die, cu);
17551 if (range_type)
17552 return range_type;
17553
729efb13
SA
17554 low.kind = PROP_CONST;
17555 high.kind = PROP_CONST;
17556 high.data.const_val = 0;
17557
4fae6e18
JK
17558 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17559 omitting DW_AT_lower_bound. */
17560 switch (cu->language)
6e70227d 17561 {
4fae6e18
JK
17562 case language_c:
17563 case language_cplus:
729efb13 17564 low.data.const_val = 0;
4fae6e18
JK
17565 low_default_is_valid = 1;
17566 break;
17567 case language_fortran:
729efb13 17568 low.data.const_val = 1;
4fae6e18
JK
17569 low_default_is_valid = 1;
17570 break;
17571 case language_d:
4fae6e18 17572 case language_objc:
c44af4eb 17573 case language_rust:
729efb13 17574 low.data.const_val = 0;
4fae6e18
JK
17575 low_default_is_valid = (cu->header.version >= 4);
17576 break;
17577 case language_ada:
17578 case language_m2:
17579 case language_pascal:
729efb13 17580 low.data.const_val = 1;
4fae6e18
JK
17581 low_default_is_valid = (cu->header.version >= 4);
17582 break;
17583 default:
729efb13 17584 low.data.const_val = 0;
4fae6e18
JK
17585 low_default_is_valid = 0;
17586 break;
a02abb62
JB
17587 }
17588
e142c38c 17589 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 17590 if (attr)
11c1ba78 17591 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
17592 else if (!low_default_is_valid)
17593 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17594 "- DIE at 0x%x [in module %s]"),
9c541725 17595 to_underlying (die->sect_off), objfile_name (cu->objfile));
a02abb62 17596
e142c38c 17597 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 17598 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
17599 {
17600 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 17601 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 17602 {
c451ebe5
SA
17603 /* If bounds are constant do the final calculation here. */
17604 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17605 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17606 else
17607 high_bound_is_count = 1;
c2ff108b 17608 }
e77813c8
PM
17609 }
17610
17611 /* Dwarf-2 specifications explicitly allows to create subrange types
17612 without specifying a base type.
17613 In that case, the base type must be set to the type of
17614 the lower bound, upper bound or count, in that order, if any of these
17615 three attributes references an object that has a type.
17616 If no base type is found, the Dwarf-2 specifications say that
17617 a signed integer type of size equal to the size of an address should
17618 be used.
17619 For the following C code: `extern char gdb_int [];'
17620 GCC produces an empty range DIE.
17621 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 17622 high bound or count are not yet handled by this code. */
e77813c8
PM
17623 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17624 {
17625 struct objfile *objfile = cu->objfile;
17626 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17627 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17628 struct type *int_type = objfile_type (objfile)->builtin_int;
17629
17630 /* Test "int", "long int", and "long long int" objfile types,
17631 and select the first one having a size above or equal to the
17632 architecture address size. */
17633 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17634 base_type = int_type;
17635 else
17636 {
17637 int_type = objfile_type (objfile)->builtin_long;
17638 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17639 base_type = int_type;
17640 else
17641 {
17642 int_type = objfile_type (objfile)->builtin_long_long;
17643 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17644 base_type = int_type;
17645 }
17646 }
17647 }
a02abb62 17648
dbb9c2b1
JB
17649 /* Normally, the DWARF producers are expected to use a signed
17650 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17651 But this is unfortunately not always the case, as witnessed
17652 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17653 is used instead. To work around that ambiguity, we treat
17654 the bounds as signed, and thus sign-extend their values, when
17655 the base type is signed. */
6e70227d 17656 negative_mask =
66c6502d 17657 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17658 if (low.kind == PROP_CONST
17659 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17660 low.data.const_val |= negative_mask;
17661 if (high.kind == PROP_CONST
17662 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17663 high.data.const_val |= negative_mask;
43bbcdc2 17664
729efb13 17665 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 17666
c451ebe5
SA
17667 if (high_bound_is_count)
17668 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17669
c2ff108b
JK
17670 /* Ada expects an empty array on no boundary attributes. */
17671 if (attr == NULL && cu->language != language_ada)
729efb13 17672 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17673
39cbfefa
DJ
17674 name = dwarf2_name (die, cu);
17675 if (name)
17676 TYPE_NAME (range_type) = name;
6e70227d 17677
e142c38c 17678 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
17679 if (attr)
17680 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17681
7e314c57
JK
17682 set_die_type (die, range_type, cu);
17683
17684 /* set_die_type should be already done. */
b4ba55a1
JB
17685 set_descriptive_type (range_type, die, cu);
17686
7e314c57 17687 return range_type;
a02abb62 17688}
6e70227d 17689
f792889a 17690static struct type *
81a17f79
JB
17691read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17692{
17693 struct type *type;
81a17f79 17694
81a17f79
JB
17695 /* For now, we only support the C meaning of an unspecified type: void. */
17696
19f392bc 17697 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 17698 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17699
f792889a 17700 return set_die_type (die, type, cu);
81a17f79 17701}
a02abb62 17702
639d11d3
DC
17703/* Read a single die and all its descendents. Set the die's sibling
17704 field to NULL; set other fields in the die correctly, and set all
17705 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17706 location of the info_ptr after reading all of those dies. PARENT
17707 is the parent of the die in question. */
17708
17709static struct die_info *
dee91e82 17710read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17711 const gdb_byte *info_ptr,
17712 const gdb_byte **new_info_ptr,
dee91e82 17713 struct die_info *parent)
639d11d3
DC
17714{
17715 struct die_info *die;
d521ce57 17716 const gdb_byte *cur_ptr;
639d11d3
DC
17717 int has_children;
17718
bf6af496 17719 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
17720 if (die == NULL)
17721 {
17722 *new_info_ptr = cur_ptr;
17723 return NULL;
17724 }
93311388 17725 store_in_ref_table (die, reader->cu);
639d11d3
DC
17726
17727 if (has_children)
bf6af496 17728 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17729 else
17730 {
17731 die->child = NULL;
17732 *new_info_ptr = cur_ptr;
17733 }
17734
17735 die->sibling = NULL;
17736 die->parent = parent;
17737 return die;
17738}
17739
17740/* Read a die, all of its descendents, and all of its siblings; set
17741 all of the fields of all of the dies correctly. Arguments are as
17742 in read_die_and_children. */
17743
17744static struct die_info *
bf6af496 17745read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17746 const gdb_byte *info_ptr,
17747 const gdb_byte **new_info_ptr,
bf6af496 17748 struct die_info *parent)
639d11d3
DC
17749{
17750 struct die_info *first_die, *last_sibling;
d521ce57 17751 const gdb_byte *cur_ptr;
639d11d3 17752
c906108c 17753 cur_ptr = info_ptr;
639d11d3
DC
17754 first_die = last_sibling = NULL;
17755
17756 while (1)
c906108c 17757 {
639d11d3 17758 struct die_info *die
dee91e82 17759 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17760
1d325ec1 17761 if (die == NULL)
c906108c 17762 {
639d11d3
DC
17763 *new_info_ptr = cur_ptr;
17764 return first_die;
c906108c 17765 }
1d325ec1
DJ
17766
17767 if (!first_die)
17768 first_die = die;
c906108c 17769 else
1d325ec1
DJ
17770 last_sibling->sibling = die;
17771
17772 last_sibling = die;
c906108c 17773 }
c906108c
SS
17774}
17775
bf6af496
DE
17776/* Read a die, all of its descendents, and all of its siblings; set
17777 all of the fields of all of the dies correctly. Arguments are as
17778 in read_die_and_children.
17779 This the main entry point for reading a DIE and all its children. */
17780
17781static struct die_info *
17782read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17783 const gdb_byte *info_ptr,
17784 const gdb_byte **new_info_ptr,
bf6af496
DE
17785 struct die_info *parent)
17786{
17787 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17788 new_info_ptr, parent);
17789
b4f54984 17790 if (dwarf_die_debug)
bf6af496
DE
17791 {
17792 fprintf_unfiltered (gdb_stdlog,
17793 "Read die from %s@0x%x of %s:\n",
a32a8923 17794 get_section_name (reader->die_section),
bf6af496
DE
17795 (unsigned) (info_ptr - reader->die_section->buffer),
17796 bfd_get_filename (reader->abfd));
b4f54984 17797 dump_die (die, dwarf_die_debug);
bf6af496
DE
17798 }
17799
17800 return die;
17801}
17802
3019eac3
DE
17803/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17804 attributes.
17805 The caller is responsible for filling in the extra attributes
17806 and updating (*DIEP)->num_attrs.
17807 Set DIEP to point to a newly allocated die with its information,
17808 except for its child, sibling, and parent fields.
17809 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 17810
d521ce57 17811static const gdb_byte *
3019eac3 17812read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17813 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 17814 int *has_children, int num_extra_attrs)
93311388 17815{
b64f50a1 17816 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17817 struct abbrev_info *abbrev;
17818 struct die_info *die;
17819 struct dwarf2_cu *cu = reader->cu;
17820 bfd *abfd = reader->abfd;
17821
9c541725 17822 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17823 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17824 info_ptr += bytes_read;
17825 if (!abbrev_number)
17826 {
17827 *diep = NULL;
17828 *has_children = 0;
17829 return info_ptr;
17830 }
17831
433df2d4 17832 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 17833 if (!abbrev)
348e048f
DE
17834 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17835 abbrev_number,
17836 bfd_get_filename (abfd));
17837
3019eac3 17838 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17839 die->sect_off = sect_off;
93311388
DE
17840 die->tag = abbrev->tag;
17841 die->abbrev = abbrev_number;
17842
3019eac3
DE
17843 /* Make the result usable.
17844 The caller needs to update num_attrs after adding the extra
17845 attributes. */
93311388
DE
17846 die->num_attrs = abbrev->num_attrs;
17847
17848 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
17849 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17850 info_ptr);
93311388
DE
17851
17852 *diep = die;
17853 *has_children = abbrev->has_children;
17854 return info_ptr;
17855}
17856
3019eac3
DE
17857/* Read a die and all its attributes.
17858 Set DIEP to point to a newly allocated die with its information,
17859 except for its child, sibling, and parent fields.
17860 Set HAS_CHILDREN to tell whether the die has children or not. */
17861
d521ce57 17862static const gdb_byte *
3019eac3 17863read_full_die (const struct die_reader_specs *reader,
d521ce57 17864 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
17865 int *has_children)
17866{
d521ce57 17867 const gdb_byte *result;
bf6af496
DE
17868
17869 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17870
b4f54984 17871 if (dwarf_die_debug)
bf6af496
DE
17872 {
17873 fprintf_unfiltered (gdb_stdlog,
17874 "Read die from %s@0x%x of %s:\n",
a32a8923 17875 get_section_name (reader->die_section),
bf6af496
DE
17876 (unsigned) (info_ptr - reader->die_section->buffer),
17877 bfd_get_filename (reader->abfd));
b4f54984 17878 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17879 }
17880
17881 return result;
3019eac3 17882}
433df2d4
DE
17883\f
17884/* Abbreviation tables.
3019eac3 17885
433df2d4 17886 In DWARF version 2, the description of the debugging information is
c906108c
SS
17887 stored in a separate .debug_abbrev section. Before we read any
17888 dies from a section we read in all abbreviations and install them
433df2d4
DE
17889 in a hash table. */
17890
17891/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17892
17893static struct abbrev_info *
17894abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
17895{
17896 struct abbrev_info *abbrev;
17897
8d749320 17898 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 17899 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 17900
433df2d4
DE
17901 return abbrev;
17902}
17903
17904/* Add an abbreviation to the table. */
c906108c
SS
17905
17906static void
433df2d4
DE
17907abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
17908 unsigned int abbrev_number,
17909 struct abbrev_info *abbrev)
17910{
17911 unsigned int hash_number;
17912
17913 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17914 abbrev->next = abbrev_table->abbrevs[hash_number];
17915 abbrev_table->abbrevs[hash_number] = abbrev;
17916}
dee91e82 17917
433df2d4
DE
17918/* Look up an abbrev in the table.
17919 Returns NULL if the abbrev is not found. */
17920
17921static struct abbrev_info *
17922abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
17923 unsigned int abbrev_number)
c906108c 17924{
433df2d4
DE
17925 unsigned int hash_number;
17926 struct abbrev_info *abbrev;
17927
17928 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17929 abbrev = abbrev_table->abbrevs[hash_number];
17930
17931 while (abbrev)
17932 {
17933 if (abbrev->number == abbrev_number)
17934 return abbrev;
17935 abbrev = abbrev->next;
17936 }
17937 return NULL;
17938}
17939
17940/* Read in an abbrev table. */
17941
17942static struct abbrev_table *
17943abbrev_table_read_table (struct dwarf2_section_info *section,
9c541725 17944 sect_offset sect_off)
433df2d4
DE
17945{
17946 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 17947 bfd *abfd = get_section_bfd_owner (section);
433df2d4 17948 struct abbrev_table *abbrev_table;
d521ce57 17949 const gdb_byte *abbrev_ptr;
c906108c
SS
17950 struct abbrev_info *cur_abbrev;
17951 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 17952 unsigned int abbrev_form;
f3dd6933
DJ
17953 struct attr_abbrev *cur_attrs;
17954 unsigned int allocated_attrs;
c906108c 17955
70ba0933 17956 abbrev_table = XNEW (struct abbrev_table);
9c541725 17957 abbrev_table->sect_off = sect_off;
433df2d4 17958 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
17959 abbrev_table->abbrevs =
17960 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
17961 ABBREV_HASH_SIZE);
433df2d4
DE
17962 memset (abbrev_table->abbrevs, 0,
17963 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 17964
433df2d4 17965 dwarf2_read_section (objfile, section);
9c541725 17966 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
17967 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17968 abbrev_ptr += bytes_read;
17969
f3dd6933 17970 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 17971 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 17972
0963b4bd 17973 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
17974 while (abbrev_number)
17975 {
433df2d4 17976 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
17977
17978 /* read in abbrev header */
17979 cur_abbrev->number = abbrev_number;
aead7601
SM
17980 cur_abbrev->tag
17981 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
17982 abbrev_ptr += bytes_read;
17983 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
17984 abbrev_ptr += 1;
17985
17986 /* now read in declarations */
22d2f3ab 17987 for (;;)
c906108c 17988 {
43988095
JK
17989 LONGEST implicit_const;
17990
22d2f3ab
JK
17991 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17992 abbrev_ptr += bytes_read;
17993 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17994 abbrev_ptr += bytes_read;
43988095
JK
17995 if (abbrev_form == DW_FORM_implicit_const)
17996 {
17997 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
17998 &bytes_read);
17999 abbrev_ptr += bytes_read;
18000 }
18001 else
18002 {
18003 /* Initialize it due to a false compiler warning. */
18004 implicit_const = -1;
18005 }
22d2f3ab
JK
18006
18007 if (abbrev_name == 0)
18008 break;
18009
f3dd6933 18010 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18011 {
f3dd6933
DJ
18012 allocated_attrs += ATTR_ALLOC_CHUNK;
18013 cur_attrs
224c3ddb 18014 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18015 }
ae038cb0 18016
aead7601
SM
18017 cur_attrs[cur_abbrev->num_attrs].name
18018 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18019 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18020 = (enum dwarf_form) abbrev_form;
43988095 18021 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18022 ++cur_abbrev->num_attrs;
c906108c
SS
18023 }
18024
8d749320
SM
18025 cur_abbrev->attrs =
18026 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18027 cur_abbrev->num_attrs);
f3dd6933
DJ
18028 memcpy (cur_abbrev->attrs, cur_attrs,
18029 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18030
433df2d4 18031 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
18032
18033 /* Get next abbreviation.
18034 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18035 always properly terminated with an abbrev number of 0.
18036 Exit loop if we encounter an abbreviation which we have
18037 already read (which means we are about to read the abbreviations
18038 for the next compile unit) or if the end of the abbreviation
18039 table is reached. */
433df2d4 18040 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18041 break;
18042 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18043 abbrev_ptr += bytes_read;
433df2d4 18044 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
18045 break;
18046 }
f3dd6933
DJ
18047
18048 xfree (cur_attrs);
433df2d4 18049 return abbrev_table;
c906108c
SS
18050}
18051
433df2d4 18052/* Free the resources held by ABBREV_TABLE. */
c906108c 18053
c906108c 18054static void
433df2d4 18055abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 18056{
433df2d4
DE
18057 obstack_free (&abbrev_table->abbrev_obstack, NULL);
18058 xfree (abbrev_table);
c906108c
SS
18059}
18060
f4dc4d17
DE
18061/* Same as abbrev_table_free but as a cleanup.
18062 We pass in a pointer to the pointer to the table so that we can
18063 set the pointer to NULL when we're done. It also simplifies
73051182 18064 build_type_psymtabs_1. */
f4dc4d17
DE
18065
18066static void
18067abbrev_table_free_cleanup (void *table_ptr)
18068{
9a3c8263 18069 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
18070
18071 if (*abbrev_table_ptr != NULL)
18072 abbrev_table_free (*abbrev_table_ptr);
18073 *abbrev_table_ptr = NULL;
18074}
18075
433df2d4
DE
18076/* Read the abbrev table for CU from ABBREV_SECTION. */
18077
18078static void
18079dwarf2_read_abbrevs (struct dwarf2_cu *cu,
18080 struct dwarf2_section_info *abbrev_section)
c906108c 18081{
433df2d4 18082 cu->abbrev_table =
9c541725 18083 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
433df2d4 18084}
c906108c 18085
433df2d4 18086/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 18087
433df2d4
DE
18088static void
18089dwarf2_free_abbrev_table (void *ptr_to_cu)
18090{
9a3c8263 18091 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 18092
a2ce51a0
DE
18093 if (cu->abbrev_table != NULL)
18094 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
18095 /* Set this to NULL so that we SEGV if we try to read it later,
18096 and also because free_comp_unit verifies this is NULL. */
18097 cu->abbrev_table = NULL;
18098}
18099\f
72bf9492
DJ
18100/* Returns nonzero if TAG represents a type that we might generate a partial
18101 symbol for. */
18102
18103static int
18104is_type_tag_for_partial (int tag)
18105{
18106 switch (tag)
18107 {
18108#if 0
18109 /* Some types that would be reasonable to generate partial symbols for,
18110 that we don't at present. */
18111 case DW_TAG_array_type:
18112 case DW_TAG_file_type:
18113 case DW_TAG_ptr_to_member_type:
18114 case DW_TAG_set_type:
18115 case DW_TAG_string_type:
18116 case DW_TAG_subroutine_type:
18117#endif
18118 case DW_TAG_base_type:
18119 case DW_TAG_class_type:
680b30c7 18120 case DW_TAG_interface_type:
72bf9492
DJ
18121 case DW_TAG_enumeration_type:
18122 case DW_TAG_structure_type:
18123 case DW_TAG_subrange_type:
18124 case DW_TAG_typedef:
18125 case DW_TAG_union_type:
18126 return 1;
18127 default:
18128 return 0;
18129 }
18130}
18131
18132/* Load all DIEs that are interesting for partial symbols into memory. */
18133
18134static struct partial_die_info *
dee91e82 18135load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18136 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18137{
dee91e82 18138 struct dwarf2_cu *cu = reader->cu;
bb5ed363 18139 struct objfile *objfile = cu->objfile;
72bf9492
DJ
18140 struct partial_die_info *part_die;
18141 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18142 struct abbrev_info *abbrev;
18143 unsigned int bytes_read;
5afb4e99 18144 unsigned int load_all = 0;
72bf9492
DJ
18145 int nesting_level = 1;
18146
18147 parent_die = NULL;
18148 last_die = NULL;
18149
7adf1e79
DE
18150 gdb_assert (cu->per_cu != NULL);
18151 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18152 load_all = 1;
18153
72bf9492
DJ
18154 cu->partial_dies
18155 = htab_create_alloc_ex (cu->header.length / 12,
18156 partial_die_hash,
18157 partial_die_eq,
18158 NULL,
18159 &cu->comp_unit_obstack,
18160 hashtab_obstack_allocate,
18161 dummy_obstack_deallocate);
18162
8d749320 18163 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
18164
18165 while (1)
18166 {
18167 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
18168
18169 /* A NULL abbrev means the end of a series of children. */
18170 if (abbrev == NULL)
18171 {
18172 if (--nesting_level == 0)
18173 {
18174 /* PART_DIE was probably the last thing allocated on the
18175 comp_unit_obstack, so we could call obstack_free
18176 here. We don't do that because the waste is small,
18177 and will be cleaned up when we're done with this
18178 compilation unit. This way, we're also more robust
18179 against other users of the comp_unit_obstack. */
18180 return first_die;
18181 }
18182 info_ptr += bytes_read;
18183 last_die = parent_die;
18184 parent_die = parent_die->die_parent;
18185 continue;
18186 }
18187
98bfdba5
PA
18188 /* Check for template arguments. We never save these; if
18189 they're seen, we just mark the parent, and go on our way. */
18190 if (parent_die != NULL
18191 && cu->language == language_cplus
18192 && (abbrev->tag == DW_TAG_template_type_param
18193 || abbrev->tag == DW_TAG_template_value_param))
18194 {
18195 parent_die->has_template_arguments = 1;
18196
18197 if (!load_all)
18198 {
18199 /* We don't need a partial DIE for the template argument. */
dee91e82 18200 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18201 continue;
18202 }
18203 }
18204
0d99eb77 18205 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18206 Skip their other children. */
18207 if (!load_all
18208 && cu->language == language_cplus
18209 && parent_die != NULL
18210 && parent_die->tag == DW_TAG_subprogram)
18211 {
dee91e82 18212 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18213 continue;
18214 }
18215
5afb4e99
DJ
18216 /* Check whether this DIE is interesting enough to save. Normally
18217 we would not be interested in members here, but there may be
18218 later variables referencing them via DW_AT_specification (for
18219 static members). */
18220 if (!load_all
18221 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18222 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18223 && abbrev->tag != DW_TAG_enumerator
18224 && abbrev->tag != DW_TAG_subprogram
bc30ff58 18225 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18226 && abbrev->tag != DW_TAG_variable
5afb4e99 18227 && abbrev->tag != DW_TAG_namespace
f55ee35c 18228 && abbrev->tag != DW_TAG_module
95554aad 18229 && abbrev->tag != DW_TAG_member
74921315
KS
18230 && abbrev->tag != DW_TAG_imported_unit
18231 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18232 {
18233 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18234 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18235 continue;
18236 }
18237
dee91e82
DE
18238 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
18239 info_ptr);
72bf9492
DJ
18240
18241 /* This two-pass algorithm for processing partial symbols has a
18242 high cost in cache pressure. Thus, handle some simple cases
18243 here which cover the majority of C partial symbols. DIEs
18244 which neither have specification tags in them, nor could have
18245 specification tags elsewhere pointing at them, can simply be
18246 processed and discarded.
18247
18248 This segment is also optional; scan_partial_symbols and
18249 add_partial_symbol will handle these DIEs if we chain
18250 them in normally. When compilers which do not emit large
18251 quantities of duplicate debug information are more common,
18252 this code can probably be removed. */
18253
18254 /* Any complete simple types at the top level (pretty much all
18255 of them, for a language without namespaces), can be processed
18256 directly. */
18257 if (parent_die == NULL
18258 && part_die->has_specification == 0
18259 && part_die->is_declaration == 0
d8228535 18260 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
18261 || part_die->tag == DW_TAG_base_type
18262 || part_die->tag == DW_TAG_subrange_type))
18263 {
18264 if (building_psymtab && part_die->name != NULL)
04a679b8 18265 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 18266 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 18267 &objfile->static_psymbols,
1762568f 18268 0, cu->language, objfile);
dee91e82 18269 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
18270 continue;
18271 }
18272
d8228535
JK
18273 /* The exception for DW_TAG_typedef with has_children above is
18274 a workaround of GCC PR debug/47510. In the case of this complaint
18275 type_name_no_tag_or_error will error on such types later.
18276
18277 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18278 it could not find the child DIEs referenced later, this is checked
18279 above. In correct DWARF DW_TAG_typedef should have no children. */
18280
18281 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
18282 complaint (&symfile_complaints,
18283 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18284 "- DIE at 0x%x [in module %s]"),
9c541725 18285 to_underlying (part_die->sect_off), objfile_name (objfile));
d8228535 18286
72bf9492
DJ
18287 /* If we're at the second level, and we're an enumerator, and
18288 our parent has no specification (meaning possibly lives in a
18289 namespace elsewhere), then we can add the partial symbol now
18290 instead of queueing it. */
18291 if (part_die->tag == DW_TAG_enumerator
18292 && parent_die != NULL
18293 && parent_die->die_parent == NULL
18294 && parent_die->tag == DW_TAG_enumeration_type
18295 && parent_die->has_specification == 0)
18296 {
18297 if (part_die->name == NULL)
3e43a32a
MS
18298 complaint (&symfile_complaints,
18299 _("malformed enumerator DIE ignored"));
72bf9492 18300 else if (building_psymtab)
04a679b8 18301 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 18302 VAR_DOMAIN, LOC_CONST,
9c37b5ae 18303 cu->language == language_cplus
bb5ed363
DE
18304 ? &objfile->global_psymbols
18305 : &objfile->static_psymbols,
1762568f 18306 0, cu->language, objfile);
72bf9492 18307
dee91e82 18308 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
18309 continue;
18310 }
18311
18312 /* We'll save this DIE so link it in. */
18313 part_die->die_parent = parent_die;
18314 part_die->die_sibling = NULL;
18315 part_die->die_child = NULL;
18316
18317 if (last_die && last_die == parent_die)
18318 last_die->die_child = part_die;
18319 else if (last_die)
18320 last_die->die_sibling = part_die;
18321
18322 last_die = part_die;
18323
18324 if (first_die == NULL)
18325 first_die = part_die;
18326
18327 /* Maybe add the DIE to the hash table. Not all DIEs that we
18328 find interesting need to be in the hash table, because we
18329 also have the parent/sibling/child chains; only those that we
18330 might refer to by offset later during partial symbol reading.
18331
18332 For now this means things that might have be the target of a
18333 DW_AT_specification, DW_AT_abstract_origin, or
18334 DW_AT_extension. DW_AT_extension will refer only to
18335 namespaces; DW_AT_abstract_origin refers to functions (and
18336 many things under the function DIE, but we do not recurse
18337 into function DIEs during partial symbol reading) and
18338 possibly variables as well; DW_AT_specification refers to
18339 declarations. Declarations ought to have the DW_AT_declaration
18340 flag. It happens that GCC forgets to put it in sometimes, but
18341 only for functions, not for types.
18342
18343 Adding more things than necessary to the hash table is harmless
18344 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18345 wasted time in find_partial_die, when we reread the compilation
18346 unit with load_all_dies set. */
72bf9492 18347
5afb4e99 18348 if (load_all
72929c62 18349 || abbrev->tag == DW_TAG_constant
5afb4e99 18350 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18351 || abbrev->tag == DW_TAG_variable
18352 || abbrev->tag == DW_TAG_namespace
18353 || part_die->is_declaration)
18354 {
18355 void **slot;
18356
18357 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18358 to_underlying (part_die->sect_off),
18359 INSERT);
72bf9492
DJ
18360 *slot = part_die;
18361 }
18362
8d749320 18363 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
18364
18365 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18366 we have no reason to follow the children of structures; for other
98bfdba5
PA
18367 languages we have to, so that we can get at method physnames
18368 to infer fully qualified class names, for DW_AT_specification,
18369 and for C++ template arguments. For C++, we also look one level
18370 inside functions to find template arguments (if the name of the
18371 function does not already contain the template arguments).
bc30ff58
JB
18372
18373 For Ada, we need to scan the children of subprograms and lexical
18374 blocks as well because Ada allows the definition of nested
18375 entities that could be interesting for the debugger, such as
18376 nested subprograms for instance. */
72bf9492 18377 if (last_die->has_children
5afb4e99
DJ
18378 && (load_all
18379 || last_die->tag == DW_TAG_namespace
f55ee35c 18380 || last_die->tag == DW_TAG_module
72bf9492 18381 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18382 || (cu->language == language_cplus
18383 && last_die->tag == DW_TAG_subprogram
18384 && (last_die->name == NULL
18385 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18386 || (cu->language != language_c
18387 && (last_die->tag == DW_TAG_class_type
680b30c7 18388 || last_die->tag == DW_TAG_interface_type
72bf9492 18389 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
18390 || last_die->tag == DW_TAG_union_type))
18391 || (cu->language == language_ada
18392 && (last_die->tag == DW_TAG_subprogram
18393 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18394 {
18395 nesting_level++;
18396 parent_die = last_die;
18397 continue;
18398 }
18399
18400 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18401 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18402
18403 /* Back to the top, do it again. */
18404 }
18405}
18406
c906108c
SS
18407/* Read a minimal amount of information into the minimal die structure. */
18408
d521ce57 18409static const gdb_byte *
dee91e82
DE
18410read_partial_die (const struct die_reader_specs *reader,
18411 struct partial_die_info *part_die,
18412 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 18413 const gdb_byte *info_ptr)
c906108c 18414{
dee91e82 18415 struct dwarf2_cu *cu = reader->cu;
bb5ed363 18416 struct objfile *objfile = cu->objfile;
d521ce57 18417 const gdb_byte *buffer = reader->buffer;
fa238c03 18418 unsigned int i;
c906108c 18419 struct attribute attr;
c5aa993b 18420 int has_low_pc_attr = 0;
c906108c 18421 int has_high_pc_attr = 0;
91da1414 18422 int high_pc_relative = 0;
c906108c 18423
72bf9492 18424 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 18425
9c541725 18426 part_die->sect_off = (sect_offset) (info_ptr - buffer);
72bf9492
DJ
18427
18428 info_ptr += abbrev_len;
18429
18430 if (abbrev == NULL)
18431 return info_ptr;
18432
c906108c
SS
18433 part_die->tag = abbrev->tag;
18434 part_die->has_children = abbrev->has_children;
c906108c
SS
18435
18436 for (i = 0; i < abbrev->num_attrs; ++i)
18437 {
dee91e82 18438 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
18439
18440 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18441 partial symbol table. */
c906108c
SS
18442 switch (attr.name)
18443 {
18444 case DW_AT_name:
71c25dea
TT
18445 switch (part_die->tag)
18446 {
18447 case DW_TAG_compile_unit:
95554aad 18448 case DW_TAG_partial_unit:
348e048f 18449 case DW_TAG_type_unit:
71c25dea
TT
18450 /* Compilation units have a DW_AT_name that is a filename, not
18451 a source language identifier. */
18452 case DW_TAG_enumeration_type:
18453 case DW_TAG_enumerator:
18454 /* These tags always have simple identifiers already; no need
18455 to canonicalize them. */
18456 part_die->name = DW_STRING (&attr);
18457 break;
18458 default:
18459 part_die->name
18460 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 18461 &objfile->per_bfd->storage_obstack);
71c25dea
TT
18462 break;
18463 }
c906108c 18464 break;
31ef98ae 18465 case DW_AT_linkage_name:
c906108c 18466 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18467 /* Note that both forms of linkage name might appear. We
18468 assume they will be the same, and we only store the last
18469 one we see. */
94af9270
KS
18470 if (cu->language == language_ada)
18471 part_die->name = DW_STRING (&attr);
abc72ce4 18472 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
18473 break;
18474 case DW_AT_low_pc:
18475 has_low_pc_attr = 1;
31aa7e4e 18476 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
18477 break;
18478 case DW_AT_high_pc:
18479 has_high_pc_attr = 1;
31aa7e4e
JB
18480 part_die->highpc = attr_value_as_address (&attr);
18481 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18482 high_pc_relative = 1;
c906108c
SS
18483 break;
18484 case DW_AT_location:
0963b4bd 18485 /* Support the .debug_loc offsets. */
8e19ed76
PS
18486 if (attr_form_is_block (&attr))
18487 {
95554aad 18488 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 18489 }
3690dd37 18490 else if (attr_form_is_section_offset (&attr))
8e19ed76 18491 {
4d3c2250 18492 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18493 }
18494 else
18495 {
4d3c2250
KB
18496 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18497 "partial symbol information");
8e19ed76 18498 }
c906108c 18499 break;
c906108c
SS
18500 case DW_AT_external:
18501 part_die->is_external = DW_UNSND (&attr);
18502 break;
18503 case DW_AT_declaration:
18504 part_die->is_declaration = DW_UNSND (&attr);
18505 break;
18506 case DW_AT_type:
18507 part_die->has_type = 1;
18508 break;
18509 case DW_AT_abstract_origin:
18510 case DW_AT_specification:
72bf9492
DJ
18511 case DW_AT_extension:
18512 part_die->has_specification = 1;
c764a876 18513 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
18514 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18515 || cu->per_cu->is_dwz);
c906108c
SS
18516 break;
18517 case DW_AT_sibling:
18518 /* Ignore absolute siblings, they might point outside of
18519 the current compile unit. */
18520 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
18521 complaint (&symfile_complaints,
18522 _("ignoring absolute DW_AT_sibling"));
c906108c 18523 else
b9502d3f 18524 {
9c541725
PA
18525 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18526 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18527
18528 if (sibling_ptr < info_ptr)
18529 complaint (&symfile_complaints,
18530 _("DW_AT_sibling points backwards"));
22869d73
KS
18531 else if (sibling_ptr > reader->buffer_end)
18532 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
18533 else
18534 part_die->sibling = sibling_ptr;
18535 }
c906108c 18536 break;
fa4028e9
JB
18537 case DW_AT_byte_size:
18538 part_die->has_byte_size = 1;
18539 break;
ff908ebf
AW
18540 case DW_AT_const_value:
18541 part_die->has_const_value = 1;
18542 break;
68511cec
CES
18543 case DW_AT_calling_convention:
18544 /* DWARF doesn't provide a way to identify a program's source-level
18545 entry point. DW_AT_calling_convention attributes are only meant
18546 to describe functions' calling conventions.
18547
18548 However, because it's a necessary piece of information in
0c1b455e
TT
18549 Fortran, and before DWARF 4 DW_CC_program was the only
18550 piece of debugging information whose definition refers to
18551 a 'main program' at all, several compilers marked Fortran
18552 main programs with DW_CC_program --- even when those
18553 functions use the standard calling conventions.
18554
18555 Although DWARF now specifies a way to provide this
18556 information, we support this practice for backward
18557 compatibility. */
68511cec 18558 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
18559 && cu->language == language_fortran)
18560 part_die->main_subprogram = 1;
68511cec 18561 break;
481860b3
GB
18562 case DW_AT_inline:
18563 if (DW_UNSND (&attr) == DW_INL_inlined
18564 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18565 part_die->may_be_inlined = 1;
18566 break;
95554aad
TT
18567
18568 case DW_AT_import:
18569 if (part_die->tag == DW_TAG_imported_unit)
36586728 18570 {
9c541725 18571 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
36586728
TT
18572 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18573 || cu->per_cu->is_dwz);
18574 }
95554aad
TT
18575 break;
18576
0c1b455e
TT
18577 case DW_AT_main_subprogram:
18578 part_die->main_subprogram = DW_UNSND (&attr);
18579 break;
18580
c906108c
SS
18581 default:
18582 break;
18583 }
18584 }
18585
91da1414
MW
18586 if (high_pc_relative)
18587 part_die->highpc += part_die->lowpc;
18588
9373cf26
JK
18589 if (has_low_pc_attr && has_high_pc_attr)
18590 {
18591 /* When using the GNU linker, .gnu.linkonce. sections are used to
18592 eliminate duplicate copies of functions and vtables and such.
18593 The linker will arbitrarily choose one and discard the others.
18594 The AT_*_pc values for such functions refer to local labels in
18595 these sections. If the section from that file was discarded, the
18596 labels are not in the output, so the relocs get a value of 0.
18597 If this is a discarded function, mark the pc bounds as invalid,
18598 so that GDB will ignore it. */
18599 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18600 {
bb5ed363 18601 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
18602
18603 complaint (&symfile_complaints,
18604 _("DW_AT_low_pc %s is zero "
18605 "for DIE at 0x%x [in module %s]"),
18606 paddress (gdbarch, part_die->lowpc),
9c541725 18607 to_underlying (part_die->sect_off), objfile_name (objfile));
9373cf26
JK
18608 }
18609 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18610 else if (part_die->lowpc >= part_die->highpc)
18611 {
bb5ed363 18612 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
18613
18614 complaint (&symfile_complaints,
18615 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18616 "for DIE at 0x%x [in module %s]"),
18617 paddress (gdbarch, part_die->lowpc),
18618 paddress (gdbarch, part_die->highpc),
9c541725
PA
18619 to_underlying (part_die->sect_off),
18620 objfile_name (objfile));
9373cf26
JK
18621 }
18622 else
18623 part_die->has_pc_info = 1;
18624 }
85cbf3d3 18625
c906108c
SS
18626 return info_ptr;
18627}
18628
72bf9492
DJ
18629/* Find a cached partial DIE at OFFSET in CU. */
18630
18631static struct partial_die_info *
9c541725 18632find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
72bf9492
DJ
18633{
18634 struct partial_die_info *lookup_die = NULL;
18635 struct partial_die_info part_die;
18636
9c541725 18637 part_die.sect_off = sect_off;
9a3c8263
SM
18638 lookup_die = ((struct partial_die_info *)
18639 htab_find_with_hash (cu->partial_dies, &part_die,
9c541725 18640 to_underlying (sect_off)));
72bf9492 18641
72bf9492
DJ
18642 return lookup_die;
18643}
18644
348e048f
DE
18645/* Find a partial DIE at OFFSET, which may or may not be in CU,
18646 except in the case of .debug_types DIEs which do not reference
18647 outside their CU (they do however referencing other types via
55f1336d 18648 DW_FORM_ref_sig8). */
72bf9492
DJ
18649
18650static struct partial_die_info *
9c541725 18651find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18652{
bb5ed363 18653 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
18654 struct dwarf2_per_cu_data *per_cu = NULL;
18655 struct partial_die_info *pd = NULL;
72bf9492 18656
36586728 18657 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18658 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18659 {
9c541725 18660 pd = find_partial_die_in_comp_unit (sect_off, cu);
5afb4e99
DJ
18661 if (pd != NULL)
18662 return pd;
0d99eb77
DE
18663 /* We missed recording what we needed.
18664 Load all dies and try again. */
18665 per_cu = cu->per_cu;
5afb4e99 18666 }
0d99eb77
DE
18667 else
18668 {
18669 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18670 if (cu->per_cu->is_debug_types)
0d99eb77 18671 {
9c541725
PA
18672 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
18673 " external reference to offset 0x%x [in module %s].\n"),
18674 to_underlying (cu->header.sect_off), to_underlying (sect_off),
0d99eb77
DE
18675 bfd_get_filename (objfile->obfd));
18676 }
9c541725 18677 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 18678 objfile);
72bf9492 18679
0d99eb77
DE
18680 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18681 load_partial_comp_unit (per_cu);
ae038cb0 18682
0d99eb77 18683 per_cu->cu->last_used = 0;
9c541725 18684 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
0d99eb77 18685 }
5afb4e99 18686
dee91e82
DE
18687 /* If we didn't find it, and not all dies have been loaded,
18688 load them all and try again. */
18689
5afb4e99
DJ
18690 if (pd == NULL && per_cu->load_all_dies == 0)
18691 {
5afb4e99 18692 per_cu->load_all_dies = 1;
fd820528
DE
18693
18694 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18695 THIS_CU->cu may already be in use. So we can't just free it and
18696 replace its DIEs with the ones we read in. Instead, we leave those
18697 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18698 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18699 set. */
dee91e82 18700 load_partial_comp_unit (per_cu);
5afb4e99 18701
9c541725 18702 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
5afb4e99
DJ
18703 }
18704
18705 if (pd == NULL)
18706 internal_error (__FILE__, __LINE__,
3e43a32a
MS
18707 _("could not find partial DIE 0x%x "
18708 "in cache [from module %s]\n"),
9c541725 18709 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 18710 return pd;
72bf9492
DJ
18711}
18712
abc72ce4
DE
18713/* See if we can figure out if the class lives in a namespace. We do
18714 this by looking for a member function; its demangled name will
18715 contain namespace info, if there is any. */
18716
18717static void
18718guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18719 struct dwarf2_cu *cu)
18720{
18721 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18722 what template types look like, because the demangler
18723 frequently doesn't give the same name as the debug info. We
18724 could fix this by only using the demangled name to get the
18725 prefix (but see comment in read_structure_type). */
18726
18727 struct partial_die_info *real_pdi;
18728 struct partial_die_info *child_pdi;
18729
18730 /* If this DIE (this DIE's specification, if any) has a parent, then
18731 we should not do this. We'll prepend the parent's fully qualified
18732 name when we create the partial symbol. */
18733
18734 real_pdi = struct_pdi;
18735 while (real_pdi->has_specification)
36586728
TT
18736 real_pdi = find_partial_die (real_pdi->spec_offset,
18737 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
18738
18739 if (real_pdi->die_parent != NULL)
18740 return;
18741
18742 for (child_pdi = struct_pdi->die_child;
18743 child_pdi != NULL;
18744 child_pdi = child_pdi->die_sibling)
18745 {
18746 if (child_pdi->tag == DW_TAG_subprogram
18747 && child_pdi->linkage_name != NULL)
18748 {
18749 char *actual_class_name
18750 = language_class_name_from_physname (cu->language_defn,
18751 child_pdi->linkage_name);
18752 if (actual_class_name != NULL)
18753 {
18754 struct_pdi->name
224c3ddb
SM
18755 = ((const char *)
18756 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
18757 actual_class_name,
18758 strlen (actual_class_name)));
abc72ce4
DE
18759 xfree (actual_class_name);
18760 }
18761 break;
18762 }
18763 }
18764}
18765
72bf9492
DJ
18766/* Adjust PART_DIE before generating a symbol for it. This function
18767 may set the is_external flag or change the DIE's name. */
18768
18769static void
18770fixup_partial_die (struct partial_die_info *part_die,
18771 struct dwarf2_cu *cu)
18772{
abc72ce4
DE
18773 /* Once we've fixed up a die, there's no point in doing so again.
18774 This also avoids a memory leak if we were to call
18775 guess_partial_die_structure_name multiple times. */
18776 if (part_die->fixup_called)
18777 return;
18778
72bf9492
DJ
18779 /* If we found a reference attribute and the DIE has no name, try
18780 to find a name in the referred to DIE. */
18781
18782 if (part_die->name == NULL && part_die->has_specification)
18783 {
18784 struct partial_die_info *spec_die;
72bf9492 18785
36586728
TT
18786 spec_die = find_partial_die (part_die->spec_offset,
18787 part_die->spec_is_dwz, cu);
72bf9492 18788
10b3939b 18789 fixup_partial_die (spec_die, cu);
72bf9492
DJ
18790
18791 if (spec_die->name)
18792 {
18793 part_die->name = spec_die->name;
18794
18795 /* Copy DW_AT_external attribute if it is set. */
18796 if (spec_die->is_external)
18797 part_die->is_external = spec_die->is_external;
18798 }
18799 }
18800
18801 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
18802
18803 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 18804 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18805
abc72ce4
DE
18806 /* If there is no parent die to provide a namespace, and there are
18807 children, see if we can determine the namespace from their linkage
122d1940 18808 name. */
abc72ce4 18809 if (cu->language == language_cplus
8b70b953 18810 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
18811 && part_die->die_parent == NULL
18812 && part_die->has_children
18813 && (part_die->tag == DW_TAG_class_type
18814 || part_die->tag == DW_TAG_structure_type
18815 || part_die->tag == DW_TAG_union_type))
18816 guess_partial_die_structure_name (part_die, cu);
18817
53832f31
TT
18818 /* GCC might emit a nameless struct or union that has a linkage
18819 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18820 if (part_die->name == NULL
96408a79
SA
18821 && (part_die->tag == DW_TAG_class_type
18822 || part_die->tag == DW_TAG_interface_type
18823 || part_die->tag == DW_TAG_structure_type
18824 || part_die->tag == DW_TAG_union_type)
53832f31
TT
18825 && part_die->linkage_name != NULL)
18826 {
18827 char *demangled;
18828
8de20a37 18829 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
18830 if (demangled)
18831 {
96408a79
SA
18832 const char *base;
18833
18834 /* Strip any leading namespaces/classes, keep only the base name.
18835 DW_AT_name for named DIEs does not contain the prefixes. */
18836 base = strrchr (demangled, ':');
18837 if (base && base > demangled && base[-1] == ':')
18838 base++;
18839 else
18840 base = demangled;
18841
34a68019 18842 part_die->name
224c3ddb
SM
18843 = ((const char *)
18844 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
18845 base, strlen (base)));
53832f31
TT
18846 xfree (demangled);
18847 }
18848 }
18849
abc72ce4 18850 part_die->fixup_called = 1;
72bf9492
DJ
18851}
18852
a8329558 18853/* Read an attribute value described by an attribute form. */
c906108c 18854
d521ce57 18855static const gdb_byte *
dee91e82
DE
18856read_attribute_value (const struct die_reader_specs *reader,
18857 struct attribute *attr, unsigned form,
43988095 18858 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 18859{
dee91e82 18860 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
18861 struct objfile *objfile = cu->objfile;
18862 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18863 bfd *abfd = reader->abfd;
e7c27a73 18864 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18865 unsigned int bytes_read;
18866 struct dwarf_block *blk;
18867
aead7601 18868 attr->form = (enum dwarf_form) form;
a8329558 18869 switch (form)
c906108c 18870 {
c906108c 18871 case DW_FORM_ref_addr:
ae411497 18872 if (cu->header.version == 2)
4568ecf9 18873 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 18874 else
4568ecf9
DE
18875 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18876 &cu->header, &bytes_read);
ae411497
TT
18877 info_ptr += bytes_read;
18878 break;
36586728
TT
18879 case DW_FORM_GNU_ref_alt:
18880 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18881 info_ptr += bytes_read;
18882 break;
ae411497 18883 case DW_FORM_addr:
e7c27a73 18884 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 18885 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18886 info_ptr += bytes_read;
c906108c
SS
18887 break;
18888 case DW_FORM_block2:
7b5a2f43 18889 blk = dwarf_alloc_block (cu);
c906108c
SS
18890 blk->size = read_2_bytes (abfd, info_ptr);
18891 info_ptr += 2;
18892 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18893 info_ptr += blk->size;
18894 DW_BLOCK (attr) = blk;
18895 break;
18896 case DW_FORM_block4:
7b5a2f43 18897 blk = dwarf_alloc_block (cu);
c906108c
SS
18898 blk->size = read_4_bytes (abfd, info_ptr);
18899 info_ptr += 4;
18900 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18901 info_ptr += blk->size;
18902 DW_BLOCK (attr) = blk;
18903 break;
18904 case DW_FORM_data2:
18905 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18906 info_ptr += 2;
18907 break;
18908 case DW_FORM_data4:
18909 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18910 info_ptr += 4;
18911 break;
18912 case DW_FORM_data8:
18913 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18914 info_ptr += 8;
18915 break;
0224619f
JK
18916 case DW_FORM_data16:
18917 blk = dwarf_alloc_block (cu);
18918 blk->size = 16;
18919 blk->data = read_n_bytes (abfd, info_ptr, 16);
18920 info_ptr += 16;
18921 DW_BLOCK (attr) = blk;
18922 break;
2dc7f7b3
TT
18923 case DW_FORM_sec_offset:
18924 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18925 info_ptr += bytes_read;
18926 break;
c906108c 18927 case DW_FORM_string:
9b1c24c8 18928 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18929 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18930 info_ptr += bytes_read;
18931 break;
4bdf3d34 18932 case DW_FORM_strp:
36586728
TT
18933 if (!cu->per_cu->is_dwz)
18934 {
18935 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
18936 &bytes_read);
18937 DW_STRING_IS_CANONICAL (attr) = 0;
18938 info_ptr += bytes_read;
18939 break;
18940 }
18941 /* FALLTHROUGH */
43988095
JK
18942 case DW_FORM_line_strp:
18943 if (!cu->per_cu->is_dwz)
18944 {
18945 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
18946 cu_header, &bytes_read);
18947 DW_STRING_IS_CANONICAL (attr) = 0;
18948 info_ptr += bytes_read;
18949 break;
18950 }
18951 /* FALLTHROUGH */
36586728
TT
18952 case DW_FORM_GNU_strp_alt:
18953 {
18954 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18955 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18956 &bytes_read);
18957
18958 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
18959 DW_STRING_IS_CANONICAL (attr) = 0;
18960 info_ptr += bytes_read;
18961 }
4bdf3d34 18962 break;
2dc7f7b3 18963 case DW_FORM_exprloc:
c906108c 18964 case DW_FORM_block:
7b5a2f43 18965 blk = dwarf_alloc_block (cu);
c906108c
SS
18966 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18967 info_ptr += bytes_read;
18968 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18969 info_ptr += blk->size;
18970 DW_BLOCK (attr) = blk;
18971 break;
18972 case DW_FORM_block1:
7b5a2f43 18973 blk = dwarf_alloc_block (cu);
c906108c
SS
18974 blk->size = read_1_byte (abfd, info_ptr);
18975 info_ptr += 1;
18976 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18977 info_ptr += blk->size;
18978 DW_BLOCK (attr) = blk;
18979 break;
18980 case DW_FORM_data1:
18981 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18982 info_ptr += 1;
18983 break;
18984 case DW_FORM_flag:
18985 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18986 info_ptr += 1;
18987 break;
2dc7f7b3
TT
18988 case DW_FORM_flag_present:
18989 DW_UNSND (attr) = 1;
18990 break;
c906108c
SS
18991 case DW_FORM_sdata:
18992 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18993 info_ptr += bytes_read;
18994 break;
18995 case DW_FORM_udata:
18996 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18997 info_ptr += bytes_read;
18998 break;
18999 case DW_FORM_ref1:
9c541725 19000 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19001 + read_1_byte (abfd, info_ptr));
c906108c
SS
19002 info_ptr += 1;
19003 break;
19004 case DW_FORM_ref2:
9c541725 19005 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19006 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19007 info_ptr += 2;
19008 break;
19009 case DW_FORM_ref4:
9c541725 19010 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19011 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19012 info_ptr += 4;
19013 break;
613e1657 19014 case DW_FORM_ref8:
9c541725 19015 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19016 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19017 info_ptr += 8;
19018 break;
55f1336d 19019 case DW_FORM_ref_sig8:
ac9ec31b 19020 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19021 info_ptr += 8;
19022 break;
c906108c 19023 case DW_FORM_ref_udata:
9c541725 19024 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19025 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19026 info_ptr += bytes_read;
19027 break;
c906108c 19028 case DW_FORM_indirect:
a8329558
KW
19029 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19030 info_ptr += bytes_read;
43988095
JK
19031 if (form == DW_FORM_implicit_const)
19032 {
19033 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19034 info_ptr += bytes_read;
19035 }
19036 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19037 info_ptr);
19038 break;
19039 case DW_FORM_implicit_const:
19040 DW_SND (attr) = implicit_const;
a8329558 19041 break;
3019eac3
DE
19042 case DW_FORM_GNU_addr_index:
19043 if (reader->dwo_file == NULL)
19044 {
19045 /* For now flag a hard error.
19046 Later we can turn this into a complaint. */
19047 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19048 dwarf_form_name (form),
19049 bfd_get_filename (abfd));
19050 }
19051 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19052 info_ptr += bytes_read;
19053 break;
19054 case DW_FORM_GNU_str_index:
19055 if (reader->dwo_file == NULL)
19056 {
19057 /* For now flag a hard error.
19058 Later we can turn this into a complaint if warranted. */
19059 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19060 dwarf_form_name (form),
19061 bfd_get_filename (abfd));
19062 }
19063 {
19064 ULONGEST str_index =
19065 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19066
342587c4 19067 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
19068 DW_STRING_IS_CANONICAL (attr) = 0;
19069 info_ptr += bytes_read;
19070 }
19071 break;
c906108c 19072 default:
8a3fe4f8 19073 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19074 dwarf_form_name (form),
19075 bfd_get_filename (abfd));
c906108c 19076 }
28e94949 19077
36586728 19078 /* Super hack. */
7771576e 19079 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19080 attr->form = DW_FORM_GNU_ref_alt;
19081
28e94949
JB
19082 /* We have seen instances where the compiler tried to emit a byte
19083 size attribute of -1 which ended up being encoded as an unsigned
19084 0xffffffff. Although 0xffffffff is technically a valid size value,
19085 an object of this size seems pretty unlikely so we can relatively
19086 safely treat these cases as if the size attribute was invalid and
19087 treat them as zero by default. */
19088 if (attr->name == DW_AT_byte_size
19089 && form == DW_FORM_data4
19090 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19091 {
19092 complaint
19093 (&symfile_complaints,
43bbcdc2
PH
19094 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19095 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19096 DW_UNSND (attr) = 0;
19097 }
28e94949 19098
c906108c
SS
19099 return info_ptr;
19100}
19101
a8329558
KW
19102/* Read an attribute described by an abbreviated attribute. */
19103
d521ce57 19104static const gdb_byte *
dee91e82
DE
19105read_attribute (const struct die_reader_specs *reader,
19106 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19107 const gdb_byte *info_ptr)
a8329558
KW
19108{
19109 attr->name = abbrev->name;
43988095
JK
19110 return read_attribute_value (reader, attr, abbrev->form,
19111 abbrev->implicit_const, info_ptr);
a8329558
KW
19112}
19113
0963b4bd 19114/* Read dwarf information from a buffer. */
c906108c
SS
19115
19116static unsigned int
a1855c1d 19117read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19118{
fe1b8b76 19119 return bfd_get_8 (abfd, buf);
c906108c
SS
19120}
19121
19122static int
a1855c1d 19123read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19124{
fe1b8b76 19125 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19126}
19127
19128static unsigned int
a1855c1d 19129read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19130{
fe1b8b76 19131 return bfd_get_16 (abfd, buf);
c906108c
SS
19132}
19133
21ae7a4d 19134static int
a1855c1d 19135read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19136{
19137 return bfd_get_signed_16 (abfd, buf);
19138}
19139
c906108c 19140static unsigned int
a1855c1d 19141read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19142{
fe1b8b76 19143 return bfd_get_32 (abfd, buf);
c906108c
SS
19144}
19145
21ae7a4d 19146static int
a1855c1d 19147read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19148{
19149 return bfd_get_signed_32 (abfd, buf);
19150}
19151
93311388 19152static ULONGEST
a1855c1d 19153read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19154{
fe1b8b76 19155 return bfd_get_64 (abfd, buf);
c906108c
SS
19156}
19157
19158static CORE_ADDR
d521ce57 19159read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19160 unsigned int *bytes_read)
c906108c 19161{
e7c27a73 19162 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19163 CORE_ADDR retval = 0;
19164
107d2387 19165 if (cu_header->signed_addr_p)
c906108c 19166 {
107d2387
AC
19167 switch (cu_header->addr_size)
19168 {
19169 case 2:
fe1b8b76 19170 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19171 break;
19172 case 4:
fe1b8b76 19173 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19174 break;
19175 case 8:
fe1b8b76 19176 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19177 break;
19178 default:
8e65ff28 19179 internal_error (__FILE__, __LINE__,
e2e0b3e5 19180 _("read_address: bad switch, signed [in module %s]"),
659b0389 19181 bfd_get_filename (abfd));
107d2387
AC
19182 }
19183 }
19184 else
19185 {
19186 switch (cu_header->addr_size)
19187 {
19188 case 2:
fe1b8b76 19189 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19190 break;
19191 case 4:
fe1b8b76 19192 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19193 break;
19194 case 8:
fe1b8b76 19195 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19196 break;
19197 default:
8e65ff28 19198 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19199 _("read_address: bad switch, "
19200 "unsigned [in module %s]"),
659b0389 19201 bfd_get_filename (abfd));
107d2387 19202 }
c906108c 19203 }
64367e0a 19204
107d2387
AC
19205 *bytes_read = cu_header->addr_size;
19206 return retval;
c906108c
SS
19207}
19208
f7ef9339 19209/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19210 specification allows the initial length to take up either 4 bytes
19211 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19212 bytes describe the length and all offsets will be 8 bytes in length
19213 instead of 4.
19214
f7ef9339
KB
19215 An older, non-standard 64-bit format is also handled by this
19216 function. The older format in question stores the initial length
19217 as an 8-byte quantity without an escape value. Lengths greater
19218 than 2^32 aren't very common which means that the initial 4 bytes
19219 is almost always zero. Since a length value of zero doesn't make
19220 sense for the 32-bit format, this initial zero can be considered to
19221 be an escape value which indicates the presence of the older 64-bit
19222 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19223 greater than 4GB. If it becomes necessary to handle lengths
19224 somewhat larger than 4GB, we could allow other small values (such
19225 as the non-sensical values of 1, 2, and 3) to also be used as
19226 escape values indicating the presence of the old format.
f7ef9339 19227
917c78fc
MK
19228 The value returned via bytes_read should be used to increment the
19229 relevant pointer after calling read_initial_length().
c764a876 19230
613e1657
KB
19231 [ Note: read_initial_length() and read_offset() are based on the
19232 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19233 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19234 from:
19235
f7ef9339 19236 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19237
613e1657
KB
19238 This document is only a draft and is subject to change. (So beware.)
19239
f7ef9339 19240 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19241 determined empirically by examining 64-bit ELF files produced by
19242 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19243
19244 - Kevin, July 16, 2002
613e1657
KB
19245 ] */
19246
19247static LONGEST
d521ce57 19248read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19249{
fe1b8b76 19250 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19251
dd373385 19252 if (length == 0xffffffff)
613e1657 19253 {
fe1b8b76 19254 length = bfd_get_64 (abfd, buf + 4);
613e1657 19255 *bytes_read = 12;
613e1657 19256 }
dd373385 19257 else if (length == 0)
f7ef9339 19258 {
dd373385 19259 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19260 length = bfd_get_64 (abfd, buf);
f7ef9339 19261 *bytes_read = 8;
f7ef9339 19262 }
613e1657
KB
19263 else
19264 {
19265 *bytes_read = 4;
613e1657
KB
19266 }
19267
c764a876
DE
19268 return length;
19269}
dd373385 19270
c764a876
DE
19271/* Cover function for read_initial_length.
19272 Returns the length of the object at BUF, and stores the size of the
19273 initial length in *BYTES_READ and stores the size that offsets will be in
19274 *OFFSET_SIZE.
19275 If the initial length size is not equivalent to that specified in
19276 CU_HEADER then issue a complaint.
19277 This is useful when reading non-comp-unit headers. */
dd373385 19278
c764a876 19279static LONGEST
d521ce57 19280read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19281 const struct comp_unit_head *cu_header,
19282 unsigned int *bytes_read,
19283 unsigned int *offset_size)
19284{
19285 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19286
19287 gdb_assert (cu_header->initial_length_size == 4
19288 || cu_header->initial_length_size == 8
19289 || cu_header->initial_length_size == 12);
19290
19291 if (cu_header->initial_length_size != *bytes_read)
19292 complaint (&symfile_complaints,
19293 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19294
c764a876 19295 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19296 return length;
613e1657
KB
19297}
19298
19299/* Read an offset from the data stream. The size of the offset is
917c78fc 19300 given by cu_header->offset_size. */
613e1657
KB
19301
19302static LONGEST
d521ce57
TT
19303read_offset (bfd *abfd, const gdb_byte *buf,
19304 const struct comp_unit_head *cu_header,
891d2f0b 19305 unsigned int *bytes_read)
c764a876
DE
19306{
19307 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19308
c764a876
DE
19309 *bytes_read = cu_header->offset_size;
19310 return offset;
19311}
19312
19313/* Read an offset from the data stream. */
19314
19315static LONGEST
d521ce57 19316read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19317{
19318 LONGEST retval = 0;
19319
c764a876 19320 switch (offset_size)
613e1657
KB
19321 {
19322 case 4:
fe1b8b76 19323 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19324 break;
19325 case 8:
fe1b8b76 19326 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19327 break;
19328 default:
8e65ff28 19329 internal_error (__FILE__, __LINE__,
c764a876 19330 _("read_offset_1: bad switch [in module %s]"),
659b0389 19331 bfd_get_filename (abfd));
613e1657
KB
19332 }
19333
917c78fc 19334 return retval;
613e1657
KB
19335}
19336
d521ce57
TT
19337static const gdb_byte *
19338read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19339{
19340 /* If the size of a host char is 8 bits, we can return a pointer
19341 to the buffer, otherwise we have to copy the data to a buffer
19342 allocated on the temporary obstack. */
4bdf3d34 19343 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19344 return buf;
c906108c
SS
19345}
19346
d521ce57
TT
19347static const char *
19348read_direct_string (bfd *abfd, const gdb_byte *buf,
19349 unsigned int *bytes_read_ptr)
c906108c
SS
19350{
19351 /* If the size of a host char is 8 bits, we can return a pointer
19352 to the string, otherwise we have to copy the string to a buffer
19353 allocated on the temporary obstack. */
4bdf3d34 19354 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19355 if (*buf == '\0')
19356 {
19357 *bytes_read_ptr = 1;
19358 return NULL;
19359 }
d521ce57
TT
19360 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19361 return (const char *) buf;
4bdf3d34
JJ
19362}
19363
43988095
JK
19364/* Return pointer to string at section SECT offset STR_OFFSET with error
19365 reporting strings FORM_NAME and SECT_NAME. */
19366
d521ce57 19367static const char *
43988095
JK
19368read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
19369 struct dwarf2_section_info *sect,
19370 const char *form_name,
19371 const char *sect_name)
19372{
19373 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
19374 if (sect->buffer == NULL)
19375 error (_("%s used without %s section [in module %s]"),
19376 form_name, sect_name, bfd_get_filename (abfd));
19377 if (str_offset >= sect->size)
19378 error (_("%s pointing outside of %s section [in module %s]"),
19379 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19380 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19381 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19382 return NULL;
43988095
JK
19383 return (const char *) (sect->buffer + str_offset);
19384}
19385
19386/* Return pointer to string at .debug_str offset STR_OFFSET. */
19387
19388static const char *
19389read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
19390{
19391 return read_indirect_string_at_offset_from (abfd, str_offset,
19392 &dwarf2_per_objfile->str,
19393 "DW_FORM_strp", ".debug_str");
19394}
19395
19396/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19397
19398static const char *
19399read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
19400{
19401 return read_indirect_string_at_offset_from (abfd, str_offset,
19402 &dwarf2_per_objfile->line_str,
19403 "DW_FORM_line_strp",
19404 ".debug_line_str");
c906108c
SS
19405}
19406
36586728
TT
19407/* Read a string at offset STR_OFFSET in the .debug_str section from
19408 the .dwz file DWZ. Throw an error if the offset is too large. If
19409 the string consists of a single NUL byte, return NULL; otherwise
19410 return a pointer to the string. */
19411
d521ce57 19412static const char *
36586728
TT
19413read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
19414{
19415 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
19416
19417 if (dwz->str.buffer == NULL)
19418 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19419 "section [in module %s]"),
19420 bfd_get_filename (dwz->dwz_bfd));
19421 if (str_offset >= dwz->str.size)
19422 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19423 ".debug_str section [in module %s]"),
19424 bfd_get_filename (dwz->dwz_bfd));
19425 gdb_assert (HOST_CHAR_BIT == 8);
19426 if (dwz->str.buffer[str_offset] == '\0')
19427 return NULL;
d521ce57 19428 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19429}
19430
43988095
JK
19431/* Return pointer to string at .debug_str offset as read from BUF.
19432 BUF is assumed to be in a compilation unit described by CU_HEADER.
19433 Return *BYTES_READ_PTR count of bytes read from BUF. */
19434
d521ce57
TT
19435static const char *
19436read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
19437 const struct comp_unit_head *cu_header,
19438 unsigned int *bytes_read_ptr)
19439{
19440 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19441
19442 return read_indirect_string_at_offset (abfd, str_offset);
19443}
19444
43988095
JK
19445/* Return pointer to string at .debug_line_str offset as read from BUF.
19446 BUF is assumed to be in a compilation unit described by CU_HEADER.
19447 Return *BYTES_READ_PTR count of bytes read from BUF. */
19448
19449static const char *
19450read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
19451 const struct comp_unit_head *cu_header,
19452 unsigned int *bytes_read_ptr)
19453{
19454 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19455
19456 return read_indirect_line_string_at_offset (abfd, str_offset);
19457}
19458
19459ULONGEST
d521ce57 19460read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19461 unsigned int *bytes_read_ptr)
c906108c 19462{
12df843f 19463 ULONGEST result;
ce5d95e1 19464 unsigned int num_read;
870f88f7 19465 int shift;
c906108c
SS
19466 unsigned char byte;
19467
19468 result = 0;
19469 shift = 0;
19470 num_read = 0;
c906108c
SS
19471 while (1)
19472 {
fe1b8b76 19473 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19474 buf++;
19475 num_read++;
12df843f 19476 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19477 if ((byte & 128) == 0)
19478 {
19479 break;
19480 }
19481 shift += 7;
19482 }
19483 *bytes_read_ptr = num_read;
19484 return result;
19485}
19486
12df843f 19487static LONGEST
d521ce57
TT
19488read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19489 unsigned int *bytes_read_ptr)
c906108c 19490{
12df843f 19491 LONGEST result;
870f88f7 19492 int shift, num_read;
c906108c
SS
19493 unsigned char byte;
19494
19495 result = 0;
19496 shift = 0;
c906108c 19497 num_read = 0;
c906108c
SS
19498 while (1)
19499 {
fe1b8b76 19500 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19501 buf++;
19502 num_read++;
12df843f 19503 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
19504 shift += 7;
19505 if ((byte & 128) == 0)
19506 {
19507 break;
19508 }
19509 }
77e0b926 19510 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 19511 result |= -(((LONGEST) 1) << shift);
c906108c
SS
19512 *bytes_read_ptr = num_read;
19513 return result;
19514}
19515
3019eac3
DE
19516/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19517 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19518 ADDR_SIZE is the size of addresses from the CU header. */
19519
19520static CORE_ADDR
19521read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
19522{
19523 struct objfile *objfile = dwarf2_per_objfile->objfile;
19524 bfd *abfd = objfile->obfd;
19525 const gdb_byte *info_ptr;
19526
19527 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19528 if (dwarf2_per_objfile->addr.buffer == NULL)
19529 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19530 objfile_name (objfile));
3019eac3
DE
19531 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19532 error (_("DW_FORM_addr_index pointing outside of "
19533 ".debug_addr section [in module %s]"),
4262abfb 19534 objfile_name (objfile));
3019eac3
DE
19535 info_ptr = (dwarf2_per_objfile->addr.buffer
19536 + addr_base + addr_index * addr_size);
19537 if (addr_size == 4)
19538 return bfd_get_32 (abfd, info_ptr);
19539 else
19540 return bfd_get_64 (abfd, info_ptr);
19541}
19542
19543/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19544
19545static CORE_ADDR
19546read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19547{
19548 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
19549}
19550
19551/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19552
19553static CORE_ADDR
d521ce57 19554read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19555 unsigned int *bytes_read)
19556{
19557 bfd *abfd = cu->objfile->obfd;
19558 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19559
19560 return read_addr_index (cu, addr_index);
19561}
19562
19563/* Data structure to pass results from dwarf2_read_addr_index_reader
19564 back to dwarf2_read_addr_index. */
19565
19566struct dwarf2_read_addr_index_data
19567{
19568 ULONGEST addr_base;
19569 int addr_size;
19570};
19571
19572/* die_reader_func for dwarf2_read_addr_index. */
19573
19574static void
19575dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19576 const gdb_byte *info_ptr,
3019eac3
DE
19577 struct die_info *comp_unit_die,
19578 int has_children,
19579 void *data)
19580{
19581 struct dwarf2_cu *cu = reader->cu;
19582 struct dwarf2_read_addr_index_data *aidata =
19583 (struct dwarf2_read_addr_index_data *) data;
19584
19585 aidata->addr_base = cu->addr_base;
19586 aidata->addr_size = cu->header.addr_size;
19587}
19588
19589/* Given an index in .debug_addr, fetch the value.
19590 NOTE: This can be called during dwarf expression evaluation,
19591 long after the debug information has been read, and thus per_cu->cu
19592 may no longer exist. */
19593
19594CORE_ADDR
19595dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19596 unsigned int addr_index)
19597{
19598 struct objfile *objfile = per_cu->objfile;
19599 struct dwarf2_cu *cu = per_cu->cu;
19600 ULONGEST addr_base;
19601 int addr_size;
19602
19603 /* This is intended to be called from outside this file. */
19604 dw2_setup (objfile);
19605
19606 /* We need addr_base and addr_size.
19607 If we don't have PER_CU->cu, we have to get it.
19608 Nasty, but the alternative is storing the needed info in PER_CU,
19609 which at this point doesn't seem justified: it's not clear how frequently
19610 it would get used and it would increase the size of every PER_CU.
19611 Entry points like dwarf2_per_cu_addr_size do a similar thing
19612 so we're not in uncharted territory here.
19613 Alas we need to be a bit more complicated as addr_base is contained
19614 in the DIE.
19615
19616 We don't need to read the entire CU(/TU).
19617 We just need the header and top level die.
a1b64ce1 19618
3019eac3 19619 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19620 For now we skip this optimization. */
3019eac3
DE
19621
19622 if (cu != NULL)
19623 {
19624 addr_base = cu->addr_base;
19625 addr_size = cu->header.addr_size;
19626 }
19627 else
19628 {
19629 struct dwarf2_read_addr_index_data aidata;
19630
a1b64ce1
DE
19631 /* Note: We can't use init_cutu_and_read_dies_simple here,
19632 we need addr_base. */
19633 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19634 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
19635 addr_base = aidata.addr_base;
19636 addr_size = aidata.addr_size;
19637 }
19638
19639 return read_addr_index_1 (addr_index, addr_base, addr_size);
19640}
19641
57d63ce2
DE
19642/* Given a DW_FORM_GNU_str_index, fetch the string.
19643 This is only used by the Fission support. */
3019eac3 19644
d521ce57 19645static const char *
342587c4 19646read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
19647{
19648 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19649 const char *objf_name = objfile_name (objfile);
3019eac3 19650 bfd *abfd = objfile->obfd;
342587c4 19651 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
19652 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19653 struct dwarf2_section_info *str_offsets_section =
19654 &reader->dwo_file->sections.str_offsets;
d521ce57 19655 const gdb_byte *info_ptr;
3019eac3 19656 ULONGEST str_offset;
57d63ce2 19657 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 19658
73869dc2
DE
19659 dwarf2_read_section (objfile, str_section);
19660 dwarf2_read_section (objfile, str_offsets_section);
19661 if (str_section->buffer == NULL)
57d63ce2 19662 error (_("%s used without .debug_str.dwo section"
9c541725
PA
19663 " in CU at offset 0x%x [in module %s]"),
19664 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19665 if (str_offsets_section->buffer == NULL)
57d63ce2 19666 error (_("%s used without .debug_str_offsets.dwo section"
9c541725
PA
19667 " in CU at offset 0x%x [in module %s]"),
19668 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19669 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 19670 error (_("%s pointing outside of .debug_str_offsets.dwo"
9c541725
PA
19671 " section in CU at offset 0x%x [in module %s]"),
19672 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19673 info_ptr = (str_offsets_section->buffer
3019eac3
DE
19674 + str_index * cu->header.offset_size);
19675 if (cu->header.offset_size == 4)
19676 str_offset = bfd_get_32 (abfd, info_ptr);
19677 else
19678 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19679 if (str_offset >= str_section->size)
57d63ce2 19680 error (_("Offset from %s pointing outside of"
9c541725
PA
19681 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
19682 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19683 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19684}
19685
3019eac3
DE
19686/* Return the length of an LEB128 number in BUF. */
19687
19688static int
19689leb128_size (const gdb_byte *buf)
19690{
19691 const gdb_byte *begin = buf;
19692 gdb_byte byte;
19693
19694 while (1)
19695 {
19696 byte = *buf++;
19697 if ((byte & 128) == 0)
19698 return buf - begin;
19699 }
19700}
19701
c906108c 19702static void
e142c38c 19703set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19704{
19705 switch (lang)
19706 {
19707 case DW_LANG_C89:
76bee0cc 19708 case DW_LANG_C99:
0cfd832f 19709 case DW_LANG_C11:
c906108c 19710 case DW_LANG_C:
d1be3247 19711 case DW_LANG_UPC:
e142c38c 19712 cu->language = language_c;
c906108c 19713 break;
9c37b5ae 19714 case DW_LANG_Java:
c906108c 19715 case DW_LANG_C_plus_plus:
0cfd832f
MW
19716 case DW_LANG_C_plus_plus_11:
19717 case DW_LANG_C_plus_plus_14:
e142c38c 19718 cu->language = language_cplus;
c906108c 19719 break;
6aecb9c2
JB
19720 case DW_LANG_D:
19721 cu->language = language_d;
19722 break;
c906108c
SS
19723 case DW_LANG_Fortran77:
19724 case DW_LANG_Fortran90:
b21b22e0 19725 case DW_LANG_Fortran95:
f7de9aab
MW
19726 case DW_LANG_Fortran03:
19727 case DW_LANG_Fortran08:
e142c38c 19728 cu->language = language_fortran;
c906108c 19729 break;
a766d390
DE
19730 case DW_LANG_Go:
19731 cu->language = language_go;
19732 break;
c906108c 19733 case DW_LANG_Mips_Assembler:
e142c38c 19734 cu->language = language_asm;
c906108c
SS
19735 break;
19736 case DW_LANG_Ada83:
8aaf0b47 19737 case DW_LANG_Ada95:
bc5f45f8
JB
19738 cu->language = language_ada;
19739 break;
72019c9c
GM
19740 case DW_LANG_Modula2:
19741 cu->language = language_m2;
19742 break;
fe8e67fd
PM
19743 case DW_LANG_Pascal83:
19744 cu->language = language_pascal;
19745 break;
22566fbd
DJ
19746 case DW_LANG_ObjC:
19747 cu->language = language_objc;
19748 break;
c44af4eb
TT
19749 case DW_LANG_Rust:
19750 case DW_LANG_Rust_old:
19751 cu->language = language_rust;
19752 break;
c906108c
SS
19753 case DW_LANG_Cobol74:
19754 case DW_LANG_Cobol85:
c906108c 19755 default:
e142c38c 19756 cu->language = language_minimal;
c906108c
SS
19757 break;
19758 }
e142c38c 19759 cu->language_defn = language_def (cu->language);
c906108c
SS
19760}
19761
19762/* Return the named attribute or NULL if not there. */
19763
19764static struct attribute *
e142c38c 19765dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19766{
a48e046c 19767 for (;;)
c906108c 19768 {
a48e046c
TT
19769 unsigned int i;
19770 struct attribute *spec = NULL;
19771
19772 for (i = 0; i < die->num_attrs; ++i)
19773 {
19774 if (die->attrs[i].name == name)
19775 return &die->attrs[i];
19776 if (die->attrs[i].name == DW_AT_specification
19777 || die->attrs[i].name == DW_AT_abstract_origin)
19778 spec = &die->attrs[i];
19779 }
19780
19781 if (!spec)
19782 break;
c906108c 19783
f2f0e013 19784 die = follow_die_ref (die, spec, &cu);
f2f0e013 19785 }
c5aa993b 19786
c906108c
SS
19787 return NULL;
19788}
19789
348e048f
DE
19790/* Return the named attribute or NULL if not there,
19791 but do not follow DW_AT_specification, etc.
19792 This is for use in contexts where we're reading .debug_types dies.
19793 Following DW_AT_specification, DW_AT_abstract_origin will take us
19794 back up the chain, and we want to go down. */
19795
19796static struct attribute *
45e58e77 19797dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19798{
19799 unsigned int i;
19800
19801 for (i = 0; i < die->num_attrs; ++i)
19802 if (die->attrs[i].name == name)
19803 return &die->attrs[i];
19804
19805 return NULL;
19806}
19807
7d45c7c3
KB
19808/* Return the string associated with a string-typed attribute, or NULL if it
19809 is either not found or is of an incorrect type. */
19810
19811static const char *
19812dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19813{
19814 struct attribute *attr;
19815 const char *str = NULL;
19816
19817 attr = dwarf2_attr (die, name, cu);
19818
19819 if (attr != NULL)
19820 {
43988095 19821 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
19822 || attr->form == DW_FORM_string
19823 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19824 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19825 str = DW_STRING (attr);
19826 else
19827 complaint (&symfile_complaints,
19828 _("string type expected for attribute %s for "
19829 "DIE at 0x%x in module %s"),
9c541725 19830 dwarf_attr_name (name), to_underlying (die->sect_off),
7d45c7c3
KB
19831 objfile_name (cu->objfile));
19832 }
19833
19834 return str;
19835}
19836
05cf31d1
JB
19837/* Return non-zero iff the attribute NAME is defined for the given DIE,
19838 and holds a non-zero value. This function should only be used for
2dc7f7b3 19839 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19840
19841static int
19842dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19843{
19844 struct attribute *attr = dwarf2_attr (die, name, cu);
19845
19846 return (attr && DW_UNSND (attr));
19847}
19848
3ca72b44 19849static int
e142c38c 19850die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19851{
05cf31d1
JB
19852 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19853 which value is non-zero. However, we have to be careful with
19854 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19855 (via dwarf2_flag_true_p) follows this attribute. So we may
19856 end up accidently finding a declaration attribute that belongs
19857 to a different DIE referenced by the specification attribute,
19858 even though the given DIE does not have a declaration attribute. */
19859 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19860 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19861}
19862
63d06c5c 19863/* Return the die giving the specification for DIE, if there is
f2f0e013 19864 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19865 containing the return value on output. If there is no
19866 specification, but there is an abstract origin, that is
19867 returned. */
63d06c5c
DC
19868
19869static struct die_info *
f2f0e013 19870die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19871{
f2f0e013
DJ
19872 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19873 *spec_cu);
63d06c5c 19874
edb3359d
DJ
19875 if (spec_attr == NULL)
19876 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19877
63d06c5c
DC
19878 if (spec_attr == NULL)
19879 return NULL;
19880 else
f2f0e013 19881 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19882}
c906108c 19883
527f3840
JK
19884/* Stub for free_line_header to match void * callback types. */
19885
19886static void
19887free_line_header_voidp (void *arg)
19888{
9a3c8263 19889 struct line_header *lh = (struct line_header *) arg;
527f3840 19890
fff8551c 19891 delete lh;
527f3840
JK
19892}
19893
fff8551c
PA
19894void
19895line_header::add_include_dir (const char *include_dir)
c906108c 19896{
27e0867f 19897 if (dwarf_line_debug >= 2)
fff8551c
PA
19898 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19899 include_dirs.size () + 1, include_dir);
27e0867f 19900
fff8551c 19901 include_dirs.push_back (include_dir);
debd256d 19902}
6e70227d 19903
fff8551c
PA
19904void
19905line_header::add_file_name (const char *name,
ecfb656c 19906 dir_index d_index,
fff8551c
PA
19907 unsigned int mod_time,
19908 unsigned int length)
debd256d 19909{
27e0867f
DE
19910 if (dwarf_line_debug >= 2)
19911 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 19912 (unsigned) file_names.size () + 1, name);
27e0867f 19913
ecfb656c 19914 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 19915}
6e70227d 19916
83769d0b 19917/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19918
19919static struct dwarf2_section_info *
19920get_debug_line_section (struct dwarf2_cu *cu)
19921{
19922 struct dwarf2_section_info *section;
19923
19924 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19925 DWO file. */
19926 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19927 section = &cu->dwo_unit->dwo_file->sections.line;
19928 else if (cu->per_cu->is_dwz)
19929 {
19930 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19931
19932 section = &dwz->line;
19933 }
19934 else
19935 section = &dwarf2_per_objfile->line;
19936
19937 return section;
19938}
19939
43988095
JK
19940/* Read directory or file name entry format, starting with byte of
19941 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19942 entries count and the entries themselves in the described entry
19943 format. */
19944
19945static void
19946read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
19947 struct line_header *lh,
19948 const struct comp_unit_head *cu_header,
19949 void (*callback) (struct line_header *lh,
19950 const char *name,
ecfb656c 19951 dir_index d_index,
43988095
JK
19952 unsigned int mod_time,
19953 unsigned int length))
19954{
19955 gdb_byte format_count, formati;
19956 ULONGEST data_count, datai;
19957 const gdb_byte *buf = *bufp;
19958 const gdb_byte *format_header_data;
43988095
JK
19959 unsigned int bytes_read;
19960
19961 format_count = read_1_byte (abfd, buf);
19962 buf += 1;
19963 format_header_data = buf;
19964 for (formati = 0; formati < format_count; formati++)
19965 {
19966 read_unsigned_leb128 (abfd, buf, &bytes_read);
19967 buf += bytes_read;
19968 read_unsigned_leb128 (abfd, buf, &bytes_read);
19969 buf += bytes_read;
19970 }
19971
19972 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19973 buf += bytes_read;
19974 for (datai = 0; datai < data_count; datai++)
19975 {
19976 const gdb_byte *format = format_header_data;
19977 struct file_entry fe;
19978
43988095
JK
19979 for (formati = 0; formati < format_count; formati++)
19980 {
ecfb656c 19981 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19982 format += bytes_read;
43988095 19983
ecfb656c 19984 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19985 format += bytes_read;
ecfb656c
PA
19986
19987 gdb::optional<const char *> string;
19988 gdb::optional<unsigned int> uint;
19989
43988095
JK
19990 switch (form)
19991 {
19992 case DW_FORM_string:
ecfb656c 19993 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
19994 buf += bytes_read;
19995 break;
19996
19997 case DW_FORM_line_strp:
ecfb656c
PA
19998 string.emplace (read_indirect_line_string (abfd, buf,
19999 cu_header,
20000 &bytes_read));
43988095
JK
20001 buf += bytes_read;
20002 break;
20003
20004 case DW_FORM_data1:
ecfb656c 20005 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20006 buf += 1;
20007 break;
20008
20009 case DW_FORM_data2:
ecfb656c 20010 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20011 buf += 2;
20012 break;
20013
20014 case DW_FORM_data4:
ecfb656c 20015 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20016 buf += 4;
20017 break;
20018
20019 case DW_FORM_data8:
ecfb656c 20020 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20021 buf += 8;
20022 break;
20023
20024 case DW_FORM_udata:
ecfb656c 20025 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20026 buf += bytes_read;
20027 break;
20028
20029 case DW_FORM_block:
20030 /* It is valid only for DW_LNCT_timestamp which is ignored by
20031 current GDB. */
20032 break;
20033 }
ecfb656c
PA
20034
20035 switch (content_type)
20036 {
20037 case DW_LNCT_path:
20038 if (string.has_value ())
20039 fe.name = *string;
20040 break;
20041 case DW_LNCT_directory_index:
20042 if (uint.has_value ())
20043 fe.d_index = (dir_index) *uint;
20044 break;
20045 case DW_LNCT_timestamp:
20046 if (uint.has_value ())
20047 fe.mod_time = *uint;
20048 break;
20049 case DW_LNCT_size:
20050 if (uint.has_value ())
20051 fe.length = *uint;
20052 break;
20053 case DW_LNCT_MD5:
20054 break;
20055 default:
20056 complaint (&symfile_complaints,
20057 _("Unknown format content type %s"),
20058 pulongest (content_type));
20059 }
43988095
JK
20060 }
20061
ecfb656c 20062 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20063 }
20064
20065 *bufp = buf;
20066}
20067
debd256d 20068/* Read the statement program header starting at OFFSET in
3019eac3 20069 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20070 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20071 Returns NULL if there is a problem reading the header, e.g., if it
20072 has a version we don't understand.
debd256d
JB
20073
20074 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20075 the returned object point into the dwarf line section buffer,
20076 and must not be freed. */
ae2de4f8 20077
fff8551c 20078static line_header_up
9c541725 20079dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20080{
d521ce57 20081 const gdb_byte *line_ptr;
c764a876 20082 unsigned int bytes_read, offset_size;
debd256d 20083 int i;
d521ce57 20084 const char *cur_dir, *cur_file;
3019eac3
DE
20085 struct dwarf2_section_info *section;
20086 bfd *abfd;
20087
36586728 20088 section = get_debug_line_section (cu);
3019eac3
DE
20089 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20090 if (section->buffer == NULL)
debd256d 20091 {
3019eac3
DE
20092 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20093 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20094 else
20095 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
20096 return 0;
20097 }
20098
fceca515
DE
20099 /* We can't do this until we know the section is non-empty.
20100 Only then do we know we have such a section. */
a32a8923 20101 abfd = get_section_bfd_owner (section);
fceca515 20102
a738430d
MK
20103 /* Make sure that at least there's room for the total_length field.
20104 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20105 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20106 {
4d3c2250 20107 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20108 return 0;
20109 }
20110
fff8551c 20111 line_header_up lh (new line_header ());
debd256d 20112
9c541725 20113 lh->sect_off = sect_off;
527f3840
JK
20114 lh->offset_in_dwz = cu->per_cu->is_dwz;
20115
9c541725 20116 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20117
a738430d 20118 /* Read in the header. */
6e70227d 20119 lh->total_length =
c764a876
DE
20120 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20121 &bytes_read, &offset_size);
debd256d 20122 line_ptr += bytes_read;
3019eac3 20123 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20124 {
4d3c2250 20125 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20126 return 0;
20127 }
20128 lh->statement_program_end = line_ptr + lh->total_length;
20129 lh->version = read_2_bytes (abfd, line_ptr);
20130 line_ptr += 2;
43988095 20131 if (lh->version > 5)
cd366ee8
DE
20132 {
20133 /* This is a version we don't understand. The format could have
20134 changed in ways we don't handle properly so just punt. */
20135 complaint (&symfile_complaints,
20136 _("unsupported version in .debug_line section"));
20137 return NULL;
20138 }
43988095
JK
20139 if (lh->version >= 5)
20140 {
20141 gdb_byte segment_selector_size;
20142
20143 /* Skip address size. */
20144 read_1_byte (abfd, line_ptr);
20145 line_ptr += 1;
20146
20147 segment_selector_size = read_1_byte (abfd, line_ptr);
20148 line_ptr += 1;
20149 if (segment_selector_size != 0)
20150 {
20151 complaint (&symfile_complaints,
20152 _("unsupported segment selector size %u "
20153 "in .debug_line section"),
20154 segment_selector_size);
20155 return NULL;
20156 }
20157 }
c764a876
DE
20158 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20159 line_ptr += offset_size;
debd256d
JB
20160 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20161 line_ptr += 1;
2dc7f7b3
TT
20162 if (lh->version >= 4)
20163 {
20164 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20165 line_ptr += 1;
20166 }
20167 else
20168 lh->maximum_ops_per_instruction = 1;
20169
20170 if (lh->maximum_ops_per_instruction == 0)
20171 {
20172 lh->maximum_ops_per_instruction = 1;
20173 complaint (&symfile_complaints,
3e43a32a
MS
20174 _("invalid maximum_ops_per_instruction "
20175 "in `.debug_line' section"));
2dc7f7b3
TT
20176 }
20177
debd256d
JB
20178 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20179 line_ptr += 1;
20180 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20181 line_ptr += 1;
20182 lh->line_range = read_1_byte (abfd, line_ptr);
20183 line_ptr += 1;
20184 lh->opcode_base = read_1_byte (abfd, line_ptr);
20185 line_ptr += 1;
fff8551c 20186 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20187
20188 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20189 for (i = 1; i < lh->opcode_base; ++i)
20190 {
20191 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20192 line_ptr += 1;
20193 }
20194
43988095 20195 if (lh->version >= 5)
debd256d 20196 {
43988095 20197 /* Read directory table. */
fff8551c
PA
20198 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
20199 [] (struct line_header *lh, const char *name,
ecfb656c 20200 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20201 unsigned int length)
20202 {
20203 lh->add_include_dir (name);
20204 });
debd256d 20205
43988095 20206 /* Read file name table. */
fff8551c
PA
20207 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
20208 [] (struct line_header *lh, const char *name,
ecfb656c 20209 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20210 unsigned int length)
20211 {
ecfb656c 20212 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 20213 });
43988095
JK
20214 }
20215 else
debd256d 20216 {
43988095
JK
20217 /* Read directory table. */
20218 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20219 {
20220 line_ptr += bytes_read;
fff8551c 20221 lh->add_include_dir (cur_dir);
43988095 20222 }
debd256d
JB
20223 line_ptr += bytes_read;
20224
43988095
JK
20225 /* Read file name table. */
20226 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20227 {
ecfb656c
PA
20228 unsigned int mod_time, length;
20229 dir_index d_index;
43988095
JK
20230
20231 line_ptr += bytes_read;
ecfb656c 20232 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20233 line_ptr += bytes_read;
20234 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20235 line_ptr += bytes_read;
20236 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20237 line_ptr += bytes_read;
20238
ecfb656c 20239 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20240 }
20241 line_ptr += bytes_read;
debd256d 20242 }
6e70227d 20243 lh->statement_program_start = line_ptr;
debd256d 20244
3019eac3 20245 if (line_ptr > (section->buffer + section->size))
4d3c2250 20246 complaint (&symfile_complaints,
3e43a32a
MS
20247 _("line number info header doesn't "
20248 "fit in `.debug_line' section"));
debd256d 20249
debd256d
JB
20250 return lh;
20251}
c906108c 20252
c6da4cef
DE
20253/* Subroutine of dwarf_decode_lines to simplify it.
20254 Return the file name of the psymtab for included file FILE_INDEX
20255 in line header LH of PST.
20256 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20257 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
20258 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
20259
20260 The function creates dangling cleanup registration. */
c6da4cef 20261
d521ce57 20262static const char *
c6da4cef
DE
20263psymtab_include_file_name (const struct line_header *lh, int file_index,
20264 const struct partial_symtab *pst,
20265 const char *comp_dir)
20266{
8c43009f 20267 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20268 const char *include_name = fe.name;
20269 const char *include_name_to_compare = include_name;
72b9f47f
TT
20270 const char *pst_filename;
20271 char *copied_name = NULL;
c6da4cef
DE
20272 int file_is_pst;
20273
8c43009f 20274 const char *dir_name = fe.include_dir (lh);
c6da4cef
DE
20275
20276 if (!IS_ABSOLUTE_PATH (include_name)
20277 && (dir_name != NULL || comp_dir != NULL))
20278 {
20279 /* Avoid creating a duplicate psymtab for PST.
20280 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20281 Before we do the comparison, however, we need to account
20282 for DIR_NAME and COMP_DIR.
20283 First prepend dir_name (if non-NULL). If we still don't
20284 have an absolute path prepend comp_dir (if non-NULL).
20285 However, the directory we record in the include-file's
20286 psymtab does not contain COMP_DIR (to match the
20287 corresponding symtab(s)).
20288
20289 Example:
20290
20291 bash$ cd /tmp
20292 bash$ gcc -g ./hello.c
20293 include_name = "hello.c"
20294 dir_name = "."
20295 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20296 DW_AT_name = "./hello.c"
20297
20298 */
c6da4cef
DE
20299
20300 if (dir_name != NULL)
20301 {
d521ce57
TT
20302 char *tem = concat (dir_name, SLASH_STRING,
20303 include_name, (char *)NULL);
20304
20305 make_cleanup (xfree, tem);
20306 include_name = tem;
c6da4cef 20307 include_name_to_compare = include_name;
c6da4cef
DE
20308 }
20309 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20310 {
d521ce57
TT
20311 char *tem = concat (comp_dir, SLASH_STRING,
20312 include_name, (char *)NULL);
20313
20314 make_cleanup (xfree, tem);
20315 include_name_to_compare = tem;
c6da4cef
DE
20316 }
20317 }
20318
20319 pst_filename = pst->filename;
20320 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20321 {
72b9f47f
TT
20322 copied_name = concat (pst->dirname, SLASH_STRING,
20323 pst_filename, (char *)NULL);
20324 pst_filename = copied_name;
c6da4cef
DE
20325 }
20326
1e3fad37 20327 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20328
72b9f47f
TT
20329 if (copied_name != NULL)
20330 xfree (copied_name);
c6da4cef
DE
20331
20332 if (file_is_pst)
20333 return NULL;
20334 return include_name;
20335}
20336
d9b3de22
DE
20337/* State machine to track the state of the line number program. */
20338
6f77053d 20339class lnp_state_machine
d9b3de22 20340{
6f77053d
PA
20341public:
20342 /* Initialize a machine state for the start of a line number
20343 program. */
20344 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20345
8c43009f
PA
20346 file_entry *current_file ()
20347 {
20348 /* lh->file_names is 0-based, but the file name numbers in the
20349 statement program are 1-based. */
6f77053d
PA
20350 return m_line_header->file_name_at (m_file);
20351 }
20352
20353 /* Record the line in the state machine. END_SEQUENCE is true if
20354 we're processing the end of a sequence. */
20355 void record_line (bool end_sequence);
20356
20357 /* Check address and if invalid nop-out the rest of the lines in this
20358 sequence. */
20359 void check_line_address (struct dwarf2_cu *cu,
20360 const gdb_byte *line_ptr,
20361 CORE_ADDR lowpc, CORE_ADDR address);
20362
20363 void handle_set_discriminator (unsigned int discriminator)
20364 {
20365 m_discriminator = discriminator;
20366 m_line_has_non_zero_discriminator |= discriminator != 0;
20367 }
20368
20369 /* Handle DW_LNE_set_address. */
20370 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20371 {
20372 m_op_index = 0;
20373 address += baseaddr;
20374 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20375 }
20376
20377 /* Handle DW_LNS_advance_pc. */
20378 void handle_advance_pc (CORE_ADDR adjust);
20379
20380 /* Handle a special opcode. */
20381 void handle_special_opcode (unsigned char op_code);
20382
20383 /* Handle DW_LNS_advance_line. */
20384 void handle_advance_line (int line_delta)
20385 {
20386 advance_line (line_delta);
20387 }
20388
20389 /* Handle DW_LNS_set_file. */
20390 void handle_set_file (file_name_index file);
20391
20392 /* Handle DW_LNS_negate_stmt. */
20393 void handle_negate_stmt ()
20394 {
20395 m_is_stmt = !m_is_stmt;
20396 }
20397
20398 /* Handle DW_LNS_const_add_pc. */
20399 void handle_const_add_pc ();
20400
20401 /* Handle DW_LNS_fixed_advance_pc. */
20402 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20403 {
20404 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20405 m_op_index = 0;
20406 }
20407
20408 /* Handle DW_LNS_copy. */
20409 void handle_copy ()
20410 {
20411 record_line (false);
20412 m_discriminator = 0;
20413 }
20414
20415 /* Handle DW_LNE_end_sequence. */
20416 void handle_end_sequence ()
20417 {
20418 m_record_line_callback = ::record_line;
20419 }
20420
20421private:
20422 /* Advance the line by LINE_DELTA. */
20423 void advance_line (int line_delta)
20424 {
20425 m_line += line_delta;
20426
20427 if (line_delta != 0)
20428 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20429 }
20430
6f77053d
PA
20431 gdbarch *m_gdbarch;
20432
20433 /* True if we're recording lines.
20434 Otherwise we're building partial symtabs and are just interested in
20435 finding include files mentioned by the line number program. */
20436 bool m_record_lines_p;
20437
8c43009f 20438 /* The line number header. */
6f77053d 20439 line_header *m_line_header;
8c43009f 20440
6f77053d
PA
20441 /* These are part of the standard DWARF line number state machine,
20442 and initialized according to the DWARF spec. */
d9b3de22 20443
6f77053d 20444 unsigned char m_op_index = 0;
8c43009f 20445 /* The line table index (1-based) of the current file. */
6f77053d
PA
20446 file_name_index m_file = (file_name_index) 1;
20447 unsigned int m_line = 1;
20448
20449 /* These are initialized in the constructor. */
20450
20451 CORE_ADDR m_address;
20452 bool m_is_stmt;
20453 unsigned int m_discriminator;
d9b3de22
DE
20454
20455 /* Additional bits of state we need to track. */
20456
20457 /* The last file that we called dwarf2_start_subfile for.
20458 This is only used for TLLs. */
6f77053d 20459 unsigned int m_last_file = 0;
d9b3de22 20460 /* The last file a line number was recorded for. */
6f77053d 20461 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
20462
20463 /* The function to call to record a line. */
6f77053d 20464 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
20465
20466 /* The last line number that was recorded, used to coalesce
20467 consecutive entries for the same line. This can happen, for
20468 example, when discriminators are present. PR 17276. */
6f77053d
PA
20469 unsigned int m_last_line = 0;
20470 bool m_line_has_non_zero_discriminator = false;
8c43009f 20471};
d9b3de22 20472
6f77053d
PA
20473void
20474lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20475{
20476 CORE_ADDR addr_adj = (((m_op_index + adjust)
20477 / m_line_header->maximum_ops_per_instruction)
20478 * m_line_header->minimum_instruction_length);
20479 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20480 m_op_index = ((m_op_index + adjust)
20481 % m_line_header->maximum_ops_per_instruction);
20482}
d9b3de22 20483
6f77053d
PA
20484void
20485lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20486{
6f77053d
PA
20487 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20488 CORE_ADDR addr_adj = (((m_op_index
20489 + (adj_opcode / m_line_header->line_range))
20490 / m_line_header->maximum_ops_per_instruction)
20491 * m_line_header->minimum_instruction_length);
20492 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20493 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20494 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20495
6f77053d
PA
20496 int line_delta = (m_line_header->line_base
20497 + (adj_opcode % m_line_header->line_range));
20498 advance_line (line_delta);
20499 record_line (false);
20500 m_discriminator = 0;
20501}
d9b3de22 20502
6f77053d
PA
20503void
20504lnp_state_machine::handle_set_file (file_name_index file)
20505{
20506 m_file = file;
20507
20508 const file_entry *fe = current_file ();
20509 if (fe == NULL)
20510 dwarf2_debug_line_missing_file_complaint ();
20511 else if (m_record_lines_p)
20512 {
20513 const char *dir = fe->include_dir (m_line_header);
20514
20515 m_last_subfile = current_subfile;
20516 m_line_has_non_zero_discriminator = m_discriminator != 0;
20517 dwarf2_start_subfile (fe->name, dir);
20518 }
20519}
20520
20521void
20522lnp_state_machine::handle_const_add_pc ()
20523{
20524 CORE_ADDR adjust
20525 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20526
20527 CORE_ADDR addr_adj
20528 = (((m_op_index + adjust)
20529 / m_line_header->maximum_ops_per_instruction)
20530 * m_line_header->minimum_instruction_length);
20531
20532 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20533 m_op_index = ((m_op_index + adjust)
20534 % m_line_header->maximum_ops_per_instruction);
20535}
d9b3de22 20536
c91513d8
PP
20537/* Ignore this record_line request. */
20538
20539static void
20540noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20541{
20542 return;
20543}
20544
a05a36a5
DE
20545/* Return non-zero if we should add LINE to the line number table.
20546 LINE is the line to add, LAST_LINE is the last line that was added,
20547 LAST_SUBFILE is the subfile for LAST_LINE.
20548 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20549 had a non-zero discriminator.
20550
20551 We have to be careful in the presence of discriminators.
20552 E.g., for this line:
20553
20554 for (i = 0; i < 100000; i++);
20555
20556 clang can emit four line number entries for that one line,
20557 each with a different discriminator.
20558 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20559
20560 However, we want gdb to coalesce all four entries into one.
20561 Otherwise the user could stepi into the middle of the line and
20562 gdb would get confused about whether the pc really was in the
20563 middle of the line.
20564
20565 Things are further complicated by the fact that two consecutive
20566 line number entries for the same line is a heuristic used by gcc
20567 to denote the end of the prologue. So we can't just discard duplicate
20568 entries, we have to be selective about it. The heuristic we use is
20569 that we only collapse consecutive entries for the same line if at least
20570 one of those entries has a non-zero discriminator. PR 17276.
20571
20572 Note: Addresses in the line number state machine can never go backwards
20573 within one sequence, thus this coalescing is ok. */
20574
20575static int
20576dwarf_record_line_p (unsigned int line, unsigned int last_line,
20577 int line_has_non_zero_discriminator,
20578 struct subfile *last_subfile)
20579{
20580 if (current_subfile != last_subfile)
20581 return 1;
20582 if (line != last_line)
20583 return 1;
20584 /* Same line for the same file that we've seen already.
20585 As a last check, for pr 17276, only record the line if the line
20586 has never had a non-zero discriminator. */
20587 if (!line_has_non_zero_discriminator)
20588 return 1;
20589 return 0;
20590}
20591
252a6764
DE
20592/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20593 in the line table of subfile SUBFILE. */
20594
20595static void
d9b3de22
DE
20596dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20597 unsigned int line, CORE_ADDR address,
20598 record_line_ftype p_record_line)
252a6764
DE
20599{
20600 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20601
27e0867f
DE
20602 if (dwarf_line_debug)
20603 {
20604 fprintf_unfiltered (gdb_stdlog,
20605 "Recording line %u, file %s, address %s\n",
20606 line, lbasename (subfile->name),
20607 paddress (gdbarch, address));
20608 }
20609
d5962de5 20610 (*p_record_line) (subfile, line, addr);
252a6764
DE
20611}
20612
20613/* Subroutine of dwarf_decode_lines_1 to simplify it.
20614 Mark the end of a set of line number records.
d9b3de22 20615 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20616 If SUBFILE is NULL the request is ignored. */
20617
20618static void
20619dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20620 CORE_ADDR address, record_line_ftype p_record_line)
20621{
27e0867f
DE
20622 if (subfile == NULL)
20623 return;
20624
20625 if (dwarf_line_debug)
20626 {
20627 fprintf_unfiltered (gdb_stdlog,
20628 "Finishing current line, file %s, address %s\n",
20629 lbasename (subfile->name),
20630 paddress (gdbarch, address));
20631 }
20632
d9b3de22
DE
20633 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20634}
20635
6f77053d
PA
20636void
20637lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20638{
d9b3de22
DE
20639 if (dwarf_line_debug)
20640 {
20641 fprintf_unfiltered (gdb_stdlog,
20642 "Processing actual line %u: file %u,"
20643 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
20644 m_line, to_underlying (m_file),
20645 paddress (m_gdbarch, m_address),
20646 m_is_stmt, m_discriminator);
d9b3de22
DE
20647 }
20648
6f77053d 20649 file_entry *fe = current_file ();
8c43009f
PA
20650
20651 if (fe == NULL)
d9b3de22
DE
20652 dwarf2_debug_line_missing_file_complaint ();
20653 /* For now we ignore lines not starting on an instruction boundary.
20654 But not when processing end_sequence for compatibility with the
20655 previous version of the code. */
6f77053d 20656 else if (m_op_index == 0 || end_sequence)
d9b3de22 20657 {
8c43009f 20658 fe->included_p = 1;
6f77053d 20659 if (m_record_lines_p && m_is_stmt)
d9b3de22 20660 {
6f77053d 20661 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 20662 {
6f77053d
PA
20663 dwarf_finish_line (m_gdbarch, m_last_subfile,
20664 m_address, m_record_line_callback);
d9b3de22
DE
20665 }
20666
20667 if (!end_sequence)
20668 {
6f77053d
PA
20669 if (dwarf_record_line_p (m_line, m_last_line,
20670 m_line_has_non_zero_discriminator,
20671 m_last_subfile))
d9b3de22 20672 {
6f77053d
PA
20673 dwarf_record_line_1 (m_gdbarch, current_subfile,
20674 m_line, m_address,
20675 m_record_line_callback);
d9b3de22 20676 }
6f77053d
PA
20677 m_last_subfile = current_subfile;
20678 m_last_line = m_line;
d9b3de22
DE
20679 }
20680 }
20681 }
20682}
20683
6f77053d
PA
20684lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20685 bool record_lines_p)
d9b3de22 20686{
6f77053d
PA
20687 m_gdbarch = arch;
20688 m_record_lines_p = record_lines_p;
20689 m_line_header = lh;
d9b3de22 20690
6f77053d 20691 m_record_line_callback = ::record_line;
d9b3de22 20692
d9b3de22
DE
20693 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20694 was a line entry for it so that the backend has a chance to adjust it
20695 and also record it in case it needs it. This is currently used by MIPS
20696 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20697 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20698 m_is_stmt = lh->default_is_stmt;
20699 m_discriminator = 0;
252a6764
DE
20700}
20701
6f77053d
PA
20702void
20703lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20704 const gdb_byte *line_ptr,
20705 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
20706{
20707 /* If address < lowpc then it's not a usable value, it's outside the
20708 pc range of the CU. However, we restrict the test to only address
20709 values of zero to preserve GDB's previous behaviour which is to
20710 handle the specific case of a function being GC'd by the linker. */
20711
20712 if (address == 0 && address < lowpc)
20713 {
20714 /* This line table is for a function which has been
20715 GCd by the linker. Ignore it. PR gdb/12528 */
20716
20717 struct objfile *objfile = cu->objfile;
20718 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20719
20720 complaint (&symfile_complaints,
20721 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20722 line_offset, objfile_name (objfile));
6f77053d
PA
20723 m_record_line_callback = noop_record_line;
20724 /* Note: record_line_callback is left as noop_record_line until
20725 we see DW_LNE_end_sequence. */
924c2928
DE
20726 }
20727}
20728
f3f5162e 20729/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20730 Process the line number information in LH.
20731 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20732 program in order to set included_p for every referenced header. */
debd256d 20733
c906108c 20734static void
43f3e411
DE
20735dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20736 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20737{
d521ce57
TT
20738 const gdb_byte *line_ptr, *extended_end;
20739 const gdb_byte *line_end;
a8c50c1f 20740 unsigned int bytes_read, extended_len;
699ca60a 20741 unsigned char op_code, extended_op;
e142c38c
DJ
20742 CORE_ADDR baseaddr;
20743 struct objfile *objfile = cu->objfile;
f3f5162e 20744 bfd *abfd = objfile->obfd;
fbf65064 20745 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20746 /* True if we're recording line info (as opposed to building partial
20747 symtabs and just interested in finding include files mentioned by
20748 the line number program). */
20749 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
20750
20751 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 20752
debd256d
JB
20753 line_ptr = lh->statement_program_start;
20754 line_end = lh->statement_program_end;
c906108c
SS
20755
20756 /* Read the statement sequences until there's nothing left. */
20757 while (line_ptr < line_end)
20758 {
6f77053d
PA
20759 /* The DWARF line number program state machine. Reset the state
20760 machine at the start of each sequence. */
20761 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20762 bool end_sequence = false;
d9b3de22 20763
8c43009f 20764 if (record_lines_p)
c906108c 20765 {
8c43009f
PA
20766 /* Start a subfile for the current file of the state
20767 machine. */
20768 const file_entry *fe = state_machine.current_file ();
20769
20770 if (fe != NULL)
20771 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
20772 }
20773
a738430d 20774 /* Decode the table. */
d9b3de22 20775 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20776 {
20777 op_code = read_1_byte (abfd, line_ptr);
20778 line_ptr += 1;
9aa1fe7e 20779
debd256d 20780 if (op_code >= lh->opcode_base)
6e70227d 20781 {
8e07a239 20782 /* Special opcode. */
6f77053d 20783 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20784 }
20785 else switch (op_code)
c906108c
SS
20786 {
20787 case DW_LNS_extended_op:
3e43a32a
MS
20788 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20789 &bytes_read);
473b7be6 20790 line_ptr += bytes_read;
a8c50c1f 20791 extended_end = line_ptr + extended_len;
c906108c
SS
20792 extended_op = read_1_byte (abfd, line_ptr);
20793 line_ptr += 1;
20794 switch (extended_op)
20795 {
20796 case DW_LNE_end_sequence:
6f77053d
PA
20797 state_machine.handle_end_sequence ();
20798 end_sequence = true;
c906108c
SS
20799 break;
20800 case DW_LNE_set_address:
d9b3de22
DE
20801 {
20802 CORE_ADDR address
20803 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 20804 line_ptr += bytes_read;
6f77053d
PA
20805
20806 state_machine.check_line_address (cu, line_ptr,
20807 lowpc, address);
20808 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20809 }
c906108c
SS
20810 break;
20811 case DW_LNE_define_file:
debd256d 20812 {
d521ce57 20813 const char *cur_file;
ecfb656c
PA
20814 unsigned int mod_time, length;
20815 dir_index dindex;
6e70227d 20816
3e43a32a
MS
20817 cur_file = read_direct_string (abfd, line_ptr,
20818 &bytes_read);
debd256d 20819 line_ptr += bytes_read;
ecfb656c 20820 dindex = (dir_index)
debd256d
JB
20821 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20822 line_ptr += bytes_read;
20823 mod_time =
20824 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20825 line_ptr += bytes_read;
20826 length =
20827 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20828 line_ptr += bytes_read;
ecfb656c 20829 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20830 }
c906108c 20831 break;
d0c6ba3d 20832 case DW_LNE_set_discriminator:
6f77053d
PA
20833 {
20834 /* The discriminator is not interesting to the
20835 debugger; just ignore it. We still need to
20836 check its value though:
20837 if there are consecutive entries for the same
20838 (non-prologue) line we want to coalesce them.
20839 PR 17276. */
20840 unsigned int discr
20841 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20842 line_ptr += bytes_read;
20843
20844 state_machine.handle_set_discriminator (discr);
20845 }
d0c6ba3d 20846 break;
c906108c 20847 default:
4d3c2250 20848 complaint (&symfile_complaints,
e2e0b3e5 20849 _("mangled .debug_line section"));
debd256d 20850 return;
c906108c 20851 }
a8c50c1f
DJ
20852 /* Make sure that we parsed the extended op correctly. If e.g.
20853 we expected a different address size than the producer used,
20854 we may have read the wrong number of bytes. */
20855 if (line_ptr != extended_end)
20856 {
20857 complaint (&symfile_complaints,
20858 _("mangled .debug_line section"));
20859 return;
20860 }
c906108c
SS
20861 break;
20862 case DW_LNS_copy:
6f77053d 20863 state_machine.handle_copy ();
c906108c
SS
20864 break;
20865 case DW_LNS_advance_pc:
2dc7f7b3
TT
20866 {
20867 CORE_ADDR adjust
20868 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20869 line_ptr += bytes_read;
6f77053d
PA
20870
20871 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20872 }
c906108c
SS
20873 break;
20874 case DW_LNS_advance_line:
a05a36a5
DE
20875 {
20876 int line_delta
20877 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20878 line_ptr += bytes_read;
6f77053d
PA
20879
20880 state_machine.handle_advance_line (line_delta);
a05a36a5 20881 }
c906108c
SS
20882 break;
20883 case DW_LNS_set_file:
d9b3de22 20884 {
6f77053d 20885 file_name_index file
ecfb656c
PA
20886 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20887 &bytes_read);
d9b3de22 20888 line_ptr += bytes_read;
8c43009f 20889
6f77053d 20890 state_machine.handle_set_file (file);
d9b3de22 20891 }
c906108c
SS
20892 break;
20893 case DW_LNS_set_column:
0ad93d4f 20894 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20895 line_ptr += bytes_read;
20896 break;
20897 case DW_LNS_negate_stmt:
6f77053d 20898 state_machine.handle_negate_stmt ();
c906108c
SS
20899 break;
20900 case DW_LNS_set_basic_block:
c906108c 20901 break;
c2c6d25f
JM
20902 /* Add to the address register of the state machine the
20903 address increment value corresponding to special opcode
a738430d
MK
20904 255. I.e., this value is scaled by the minimum
20905 instruction length since special opcode 255 would have
b021a221 20906 scaled the increment. */
c906108c 20907 case DW_LNS_const_add_pc:
6f77053d 20908 state_machine.handle_const_add_pc ();
c906108c
SS
20909 break;
20910 case DW_LNS_fixed_advance_pc:
3e29f34a 20911 {
6f77053d 20912 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20913 line_ptr += 2;
6f77053d
PA
20914
20915 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20916 }
c906108c 20917 break;
9aa1fe7e 20918 default:
a738430d
MK
20919 {
20920 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20921 int i;
a738430d 20922
debd256d 20923 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20924 {
20925 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20926 line_ptr += bytes_read;
20927 }
20928 }
c906108c
SS
20929 }
20930 }
d9b3de22
DE
20931
20932 if (!end_sequence)
20933 dwarf2_debug_line_missing_end_sequence_complaint ();
20934
20935 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20936 in which case we still finish recording the last line). */
6f77053d 20937 state_machine.record_line (true);
c906108c 20938 }
f3f5162e
DE
20939}
20940
20941/* Decode the Line Number Program (LNP) for the given line_header
20942 structure and CU. The actual information extracted and the type
20943 of structures created from the LNP depends on the value of PST.
20944
20945 1. If PST is NULL, then this procedure uses the data from the program
20946 to create all necessary symbol tables, and their linetables.
20947
20948 2. If PST is not NULL, this procedure reads the program to determine
20949 the list of files included by the unit represented by PST, and
20950 builds all the associated partial symbol tables.
20951
20952 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20953 It is used for relative paths in the line table.
20954 NOTE: When processing partial symtabs (pst != NULL),
20955 comp_dir == pst->dirname.
20956
20957 NOTE: It is important that psymtabs have the same file name (via strcmp)
20958 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20959 symtab we don't use it in the name of the psymtabs we create.
20960 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20961 A good testcase for this is mb-inline.exp.
20962
527f3840
JK
20963 LOWPC is the lowest address in CU (or 0 if not known).
20964
20965 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20966 for its PC<->lines mapping information. Otherwise only the filename
20967 table is read in. */
f3f5162e
DE
20968
20969static void
20970dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 20971 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 20972 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
20973{
20974 struct objfile *objfile = cu->objfile;
20975 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20976
527f3840
JK
20977 if (decode_mapping)
20978 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20979
20980 if (decode_for_pst_p)
20981 {
20982 int file_index;
20983
20984 /* Now that we're done scanning the Line Header Program, we can
20985 create the psymtab of each included file. */
fff8551c 20986 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
20987 if (lh->file_names[file_index].included_p == 1)
20988 {
d521ce57 20989 const char *include_name =
c6da4cef
DE
20990 psymtab_include_file_name (lh, file_index, pst, comp_dir);
20991 if (include_name != NULL)
aaa75496
JB
20992 dwarf2_create_include_psymtab (include_name, pst, objfile);
20993 }
20994 }
cb1df416
DJ
20995 else
20996 {
20997 /* Make sure a symtab is created for every file, even files
20998 which contain only variables (i.e. no code with associated
20999 line numbers). */
43f3e411 21000 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 21001 int i;
cb1df416 21002
fff8551c 21003 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 21004 {
8c43009f 21005 file_entry &fe = lh->file_names[i];
9a619af0 21006
8c43009f 21007 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 21008
cb1df416 21009 if (current_subfile->symtab == NULL)
43f3e411
DE
21010 {
21011 current_subfile->symtab
21012 = allocate_symtab (cust, current_subfile->name);
21013 }
8c43009f 21014 fe.symtab = current_subfile->symtab;
cb1df416
DJ
21015 }
21016 }
c906108c
SS
21017}
21018
21019/* Start a subfile for DWARF. FILENAME is the name of the file and
21020 DIRNAME the name of the source directory which contains FILENAME
4d663531 21021 or NULL if not known.
c906108c
SS
21022 This routine tries to keep line numbers from identical absolute and
21023 relative file names in a common subfile.
21024
21025 Using the `list' example from the GDB testsuite, which resides in
21026 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21027 of /srcdir/list0.c yields the following debugging information for list0.c:
21028
c5aa993b 21029 DW_AT_name: /srcdir/list0.c
4d663531 21030 DW_AT_comp_dir: /compdir
357e46e7 21031 files.files[0].name: list0.h
c5aa993b 21032 files.files[0].dir: /srcdir
357e46e7 21033 files.files[1].name: list0.c
c5aa993b 21034 files.files[1].dir: /srcdir
c906108c
SS
21035
21036 The line number information for list0.c has to end up in a single
4f1520fb
FR
21037 subfile, so that `break /srcdir/list0.c:1' works as expected.
21038 start_subfile will ensure that this happens provided that we pass the
21039 concatenation of files.files[1].dir and files.files[1].name as the
21040 subfile's name. */
c906108c
SS
21041
21042static void
4d663531 21043dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 21044{
d521ce57 21045 char *copy = NULL;
4f1520fb 21046
4d663531 21047 /* In order not to lose the line information directory,
4f1520fb
FR
21048 we concatenate it to the filename when it makes sense.
21049 Note that the Dwarf3 standard says (speaking of filenames in line
21050 information): ``The directory index is ignored for file names
21051 that represent full path names''. Thus ignoring dirname in the
21052 `else' branch below isn't an issue. */
c906108c 21053
d5166ae1 21054 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21055 {
21056 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21057 filename = copy;
21058 }
c906108c 21059
4d663531 21060 start_subfile (filename);
4f1520fb 21061
d521ce57
TT
21062 if (copy != NULL)
21063 xfree (copy);
c906108c
SS
21064}
21065
f4dc4d17
DE
21066/* Start a symtab for DWARF.
21067 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21068
43f3e411 21069static struct compunit_symtab *
f4dc4d17 21070dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 21071 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 21072{
43f3e411 21073 struct compunit_symtab *cust
5ffa0793 21074 = start_symtab (cu->objfile, name, comp_dir, low_pc, cu->language);
43f3e411 21075
f4dc4d17
DE
21076 record_debugformat ("DWARF 2");
21077 record_producer (cu->producer);
21078
21079 /* We assume that we're processing GCC output. */
21080 processing_gcc_compilation = 2;
21081
4d4ec4e5 21082 cu->processing_has_namespace_info = 0;
43f3e411
DE
21083
21084 return cust;
f4dc4d17
DE
21085}
21086
4c2df51b
DJ
21087static void
21088var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21089 struct dwarf2_cu *cu)
4c2df51b 21090{
e7c27a73
DJ
21091 struct objfile *objfile = cu->objfile;
21092 struct comp_unit_head *cu_header = &cu->header;
21093
4c2df51b
DJ
21094 /* NOTE drow/2003-01-30: There used to be a comment and some special
21095 code here to turn a symbol with DW_AT_external and a
21096 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21097 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21098 with some versions of binutils) where shared libraries could have
21099 relocations against symbols in their debug information - the
21100 minimal symbol would have the right address, but the debug info
21101 would not. It's no longer necessary, because we will explicitly
21102 apply relocations when we read in the debug information now. */
21103
21104 /* A DW_AT_location attribute with no contents indicates that a
21105 variable has been optimized away. */
21106 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21107 {
f1e6e072 21108 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21109 return;
21110 }
21111
21112 /* Handle one degenerate form of location expression specially, to
21113 preserve GDB's previous behavior when section offsets are
3019eac3
DE
21114 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21115 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21116
21117 if (attr_form_is_block (attr)
3019eac3
DE
21118 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21119 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21120 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21121 && (DW_BLOCK (attr)->size
21122 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21123 {
891d2f0b 21124 unsigned int dummy;
4c2df51b 21125
3019eac3
DE
21126 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21127 SYMBOL_VALUE_ADDRESS (sym) =
21128 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21129 else
21130 SYMBOL_VALUE_ADDRESS (sym) =
21131 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 21132 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
21133 fixup_symbol_section (sym, objfile);
21134 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21135 SYMBOL_SECTION (sym));
4c2df51b
DJ
21136 return;
21137 }
21138
21139 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21140 expression evaluator, and use LOC_COMPUTED only when necessary
21141 (i.e. when the value of a register or memory location is
21142 referenced, or a thread-local block, etc.). Then again, it might
21143 not be worthwhile. I'm assuming that it isn't unless performance
21144 or memory numbers show me otherwise. */
21145
f1e6e072 21146 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21147
f1e6e072 21148 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 21149 cu->has_loclist = 1;
4c2df51b
DJ
21150}
21151
c906108c
SS
21152/* Given a pointer to a DWARF information entry, figure out if we need
21153 to make a symbol table entry for it, and if so, create a new entry
21154 and return a pointer to it.
21155 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21156 used the passed type.
21157 If SPACE is not NULL, use it to hold the new symbol. If it is
21158 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21159
21160static struct symbol *
34eaf542
TT
21161new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21162 struct symbol *space)
c906108c 21163{
e7c27a73 21164 struct objfile *objfile = cu->objfile;
3e29f34a 21165 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21166 struct symbol *sym = NULL;
15d034d0 21167 const char *name;
c906108c
SS
21168 struct attribute *attr = NULL;
21169 struct attribute *attr2 = NULL;
e142c38c 21170 CORE_ADDR baseaddr;
e37fd15a
SW
21171 struct pending **list_to_add = NULL;
21172
edb3359d 21173 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21174
21175 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21176
94af9270 21177 name = dwarf2_name (die, cu);
c906108c
SS
21178 if (name)
21179 {
94af9270 21180 const char *linkagename;
34eaf542 21181 int suppress_add = 0;
94af9270 21182
34eaf542
TT
21183 if (space)
21184 sym = space;
21185 else
e623cf5d 21186 sym = allocate_symbol (objfile);
c906108c 21187 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21188
21189 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21190 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21191 linkagename = dwarf2_physname (name, die, cu);
21192 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21193
f55ee35c
JK
21194 /* Fortran does not have mangling standard and the mangling does differ
21195 between gfortran, iFort etc. */
21196 if (cu->language == language_fortran
b250c185 21197 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21198 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21199 dwarf2_full_name (name, die, cu),
29df156d 21200 NULL);
f55ee35c 21201
c906108c 21202 /* Default assumptions.
c5aa993b 21203 Use the passed type or decode it from the die. */
176620f1 21204 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21205 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21206 if (type != NULL)
21207 SYMBOL_TYPE (sym) = type;
21208 else
e7c27a73 21209 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21210 attr = dwarf2_attr (die,
21211 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21212 cu);
c906108c
SS
21213 if (attr)
21214 {
21215 SYMBOL_LINE (sym) = DW_UNSND (attr);
21216 }
cb1df416 21217
edb3359d
DJ
21218 attr = dwarf2_attr (die,
21219 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21220 cu);
cb1df416
DJ
21221 if (attr)
21222 {
ecfb656c 21223 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21224 struct file_entry *fe;
9a619af0 21225
ecfb656c
PA
21226 if (cu->line_header != NULL)
21227 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21228 else
21229 fe = NULL;
21230
21231 if (fe == NULL)
cb1df416
DJ
21232 complaint (&symfile_complaints,
21233 _("file index out of range"));
8c43009f
PA
21234 else
21235 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21236 }
21237
c906108c
SS
21238 switch (die->tag)
21239 {
21240 case DW_TAG_label:
e142c38c 21241 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21242 if (attr)
3e29f34a
MR
21243 {
21244 CORE_ADDR addr;
21245
21246 addr = attr_value_as_address (attr);
21247 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21248 SYMBOL_VALUE_ADDRESS (sym) = addr;
21249 }
0f5238ed
TT
21250 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21251 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21252 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 21253 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21254 break;
21255 case DW_TAG_subprogram:
21256 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21257 finish_block. */
f1e6e072 21258 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21259 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
21260 if ((attr2 && (DW_UNSND (attr2) != 0))
21261 || cu->language == language_ada)
c906108c 21262 {
2cfa0c8d
JB
21263 /* Subprograms marked external are stored as a global symbol.
21264 Ada subprograms, whether marked external or not, are always
21265 stored as a global symbol, because we want to be able to
21266 access them globally. For instance, we want to be able
21267 to break on a nested subprogram without having to
21268 specify the context. */
e37fd15a 21269 list_to_add = &global_symbols;
c906108c
SS
21270 }
21271 else
21272 {
e37fd15a 21273 list_to_add = cu->list_in_scope;
c906108c
SS
21274 }
21275 break;
edb3359d
DJ
21276 case DW_TAG_inlined_subroutine:
21277 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21278 finish_block. */
f1e6e072 21279 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21280 SYMBOL_INLINED (sym) = 1;
481860b3 21281 list_to_add = cu->list_in_scope;
edb3359d 21282 break;
34eaf542
TT
21283 case DW_TAG_template_value_param:
21284 suppress_add = 1;
21285 /* Fall through. */
72929c62 21286 case DW_TAG_constant:
c906108c 21287 case DW_TAG_variable:
254e6b9e 21288 case DW_TAG_member:
0963b4bd
MS
21289 /* Compilation with minimal debug info may result in
21290 variables with missing type entries. Change the
21291 misleading `void' type to something sensible. */
c906108c 21292 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21293 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21294
e142c38c 21295 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21296 /* In the case of DW_TAG_member, we should only be called for
21297 static const members. */
21298 if (die->tag == DW_TAG_member)
21299 {
3863f96c
DE
21300 /* dwarf2_add_field uses die_is_declaration,
21301 so we do the same. */
254e6b9e
DE
21302 gdb_assert (die_is_declaration (die, cu));
21303 gdb_assert (attr);
21304 }
c906108c
SS
21305 if (attr)
21306 {
e7c27a73 21307 dwarf2_const_value (attr, sym, cu);
e142c38c 21308 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21309 if (!suppress_add)
34eaf542
TT
21310 {
21311 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 21312 list_to_add = &global_symbols;
34eaf542 21313 else
e37fd15a 21314 list_to_add = cu->list_in_scope;
34eaf542 21315 }
c906108c
SS
21316 break;
21317 }
e142c38c 21318 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21319 if (attr)
21320 {
e7c27a73 21321 var_decode_location (attr, sym, cu);
e142c38c 21322 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21323
21324 /* Fortran explicitly imports any global symbols to the local
21325 scope by DW_TAG_common_block. */
21326 if (cu->language == language_fortran && die->parent
21327 && die->parent->tag == DW_TAG_common_block)
21328 attr2 = NULL;
21329
caac4577
JG
21330 if (SYMBOL_CLASS (sym) == LOC_STATIC
21331 && SYMBOL_VALUE_ADDRESS (sym) == 0
21332 && !dwarf2_per_objfile->has_section_at_zero)
21333 {
21334 /* When a static variable is eliminated by the linker,
21335 the corresponding debug information is not stripped
21336 out, but the variable address is set to null;
21337 do not add such variables into symbol table. */
21338 }
21339 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21340 {
f55ee35c
JK
21341 /* Workaround gfortran PR debug/40040 - it uses
21342 DW_AT_location for variables in -fPIC libraries which may
21343 get overriden by other libraries/executable and get
21344 a different address. Resolve it by the minimal symbol
21345 which may come from inferior's executable using copy
21346 relocation. Make this workaround only for gfortran as for
21347 other compilers GDB cannot guess the minimal symbol
21348 Fortran mangling kind. */
21349 if (cu->language == language_fortran && die->parent
21350 && die->parent->tag == DW_TAG_module
21351 && cu->producer
28586665 21352 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 21353 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 21354
1c809c68
TT
21355 /* A variable with DW_AT_external is never static,
21356 but it may be block-scoped. */
21357 list_to_add = (cu->list_in_scope == &file_symbols
21358 ? &global_symbols : cu->list_in_scope);
1c809c68 21359 }
c906108c 21360 else
e37fd15a 21361 list_to_add = cu->list_in_scope;
c906108c
SS
21362 }
21363 else
21364 {
21365 /* We do not know the address of this symbol.
c5aa993b
JM
21366 If it is an external symbol and we have type information
21367 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21368 The address of the variable will then be determined from
21369 the minimal symbol table whenever the variable is
21370 referenced. */
e142c38c 21371 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21372
21373 /* Fortran explicitly imports any global symbols to the local
21374 scope by DW_TAG_common_block. */
21375 if (cu->language == language_fortran && die->parent
21376 && die->parent->tag == DW_TAG_common_block)
21377 {
21378 /* SYMBOL_CLASS doesn't matter here because
21379 read_common_block is going to reset it. */
21380 if (!suppress_add)
21381 list_to_add = cu->list_in_scope;
21382 }
21383 else if (attr2 && (DW_UNSND (attr2) != 0)
21384 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21385 {
0fe7935b
DJ
21386 /* A variable with DW_AT_external is never static, but it
21387 may be block-scoped. */
21388 list_to_add = (cu->list_in_scope == &file_symbols
21389 ? &global_symbols : cu->list_in_scope);
21390
f1e6e072 21391 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21392 }
442ddf59
JK
21393 else if (!die_is_declaration (die, cu))
21394 {
21395 /* Use the default LOC_OPTIMIZED_OUT class. */
21396 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21397 if (!suppress_add)
21398 list_to_add = cu->list_in_scope;
442ddf59 21399 }
c906108c
SS
21400 }
21401 break;
21402 case DW_TAG_formal_parameter:
edb3359d
DJ
21403 /* If we are inside a function, mark this as an argument. If
21404 not, we might be looking at an argument to an inlined function
21405 when we do not have enough information to show inlined frames;
21406 pretend it's a local variable in that case so that the user can
21407 still see it. */
21408 if (context_stack_depth > 0
21409 && context_stack[context_stack_depth - 1].name != NULL)
21410 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 21411 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21412 if (attr)
21413 {
e7c27a73 21414 var_decode_location (attr, sym, cu);
c906108c 21415 }
e142c38c 21416 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21417 if (attr)
21418 {
e7c27a73 21419 dwarf2_const_value (attr, sym, cu);
c906108c 21420 }
f346a30d 21421
e37fd15a 21422 list_to_add = cu->list_in_scope;
c906108c
SS
21423 break;
21424 case DW_TAG_unspecified_parameters:
21425 /* From varargs functions; gdb doesn't seem to have any
21426 interest in this information, so just ignore it for now.
21427 (FIXME?) */
21428 break;
34eaf542
TT
21429 case DW_TAG_template_type_param:
21430 suppress_add = 1;
21431 /* Fall through. */
c906108c 21432 case DW_TAG_class_type:
680b30c7 21433 case DW_TAG_interface_type:
c906108c
SS
21434 case DW_TAG_structure_type:
21435 case DW_TAG_union_type:
72019c9c 21436 case DW_TAG_set_type:
c906108c 21437 case DW_TAG_enumeration_type:
f1e6e072 21438 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21439 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21440
63d06c5c 21441 {
9c37b5ae 21442 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21443 really ever be static objects: otherwise, if you try
21444 to, say, break of a class's method and you're in a file
21445 which doesn't mention that class, it won't work unless
21446 the check for all static symbols in lookup_symbol_aux
21447 saves you. See the OtherFileClass tests in
21448 gdb.c++/namespace.exp. */
21449
e37fd15a 21450 if (!suppress_add)
34eaf542 21451 {
34eaf542 21452 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21453 && cu->language == language_cplus
34eaf542 21454 ? &global_symbols : cu->list_in_scope);
63d06c5c 21455
64382290 21456 /* The semantics of C++ state that "struct foo {
9c37b5ae 21457 ... }" also defines a typedef for "foo". */
64382290 21458 if (cu->language == language_cplus
45280282 21459 || cu->language == language_ada
c44af4eb
TT
21460 || cu->language == language_d
21461 || cu->language == language_rust)
64382290
TT
21462 {
21463 /* The symbol's name is already allocated along
21464 with this objfile, so we don't need to
21465 duplicate it for the type. */
21466 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21467 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21468 }
63d06c5c
DC
21469 }
21470 }
c906108c
SS
21471 break;
21472 case DW_TAG_typedef:
f1e6e072 21473 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21474 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21475 list_to_add = cu->list_in_scope;
63d06c5c 21476 break;
c906108c 21477 case DW_TAG_base_type:
a02abb62 21478 case DW_TAG_subrange_type:
f1e6e072 21479 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21480 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21481 list_to_add = cu->list_in_scope;
c906108c
SS
21482 break;
21483 case DW_TAG_enumerator:
e142c38c 21484 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21485 if (attr)
21486 {
e7c27a73 21487 dwarf2_const_value (attr, sym, cu);
c906108c 21488 }
63d06c5c
DC
21489 {
21490 /* NOTE: carlton/2003-11-10: See comment above in the
21491 DW_TAG_class_type, etc. block. */
21492
e142c38c 21493 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21494 && cu->language == language_cplus
e142c38c 21495 ? &global_symbols : cu->list_in_scope);
63d06c5c 21496 }
c906108c 21497 break;
74921315 21498 case DW_TAG_imported_declaration:
5c4e30ca 21499 case DW_TAG_namespace:
f1e6e072 21500 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 21501 list_to_add = &global_symbols;
5c4e30ca 21502 break;
530e8392
KB
21503 case DW_TAG_module:
21504 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21505 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21506 list_to_add = &global_symbols;
21507 break;
4357ac6c 21508 case DW_TAG_common_block:
f1e6e072 21509 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
21510 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21511 add_symbol_to_list (sym, cu->list_in_scope);
21512 break;
c906108c
SS
21513 default:
21514 /* Not a tag we recognize. Hopefully we aren't processing
21515 trash data, but since we must specifically ignore things
21516 we don't recognize, there is nothing else we should do at
0963b4bd 21517 this point. */
e2e0b3e5 21518 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 21519 dwarf_tag_name (die->tag));
c906108c
SS
21520 break;
21521 }
df8a16a1 21522
e37fd15a
SW
21523 if (suppress_add)
21524 {
21525 sym->hash_next = objfile->template_symbols;
21526 objfile->template_symbols = sym;
21527 list_to_add = NULL;
21528 }
21529
21530 if (list_to_add != NULL)
21531 add_symbol_to_list (sym, list_to_add);
21532
df8a16a1
DJ
21533 /* For the benefit of old versions of GCC, check for anonymous
21534 namespaces based on the demangled name. */
4d4ec4e5 21535 if (!cu->processing_has_namespace_info
94af9270 21536 && cu->language == language_cplus)
a10964d1 21537 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
21538 }
21539 return (sym);
21540}
21541
34eaf542
TT
21542/* A wrapper for new_symbol_full that always allocates a new symbol. */
21543
21544static struct symbol *
21545new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
21546{
21547 return new_symbol_full (die, type, cu, NULL);
21548}
21549
98bfdba5
PA
21550/* Given an attr with a DW_FORM_dataN value in host byte order,
21551 zero-extend it as appropriate for the symbol's type. The DWARF
21552 standard (v4) is not entirely clear about the meaning of using
21553 DW_FORM_dataN for a constant with a signed type, where the type is
21554 wider than the data. The conclusion of a discussion on the DWARF
21555 list was that this is unspecified. We choose to always zero-extend
21556 because that is the interpretation long in use by GCC. */
c906108c 21557
98bfdba5 21558static gdb_byte *
ff39bb5e 21559dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21560 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21561{
e7c27a73 21562 struct objfile *objfile = cu->objfile;
e17a4113
UW
21563 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21564 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21565 LONGEST l = DW_UNSND (attr);
21566
21567 if (bits < sizeof (*value) * 8)
21568 {
21569 l &= ((LONGEST) 1 << bits) - 1;
21570 *value = l;
21571 }
21572 else if (bits == sizeof (*value) * 8)
21573 *value = l;
21574 else
21575 {
224c3ddb 21576 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21577 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21578 return bytes;
21579 }
21580
21581 return NULL;
21582}
21583
21584/* Read a constant value from an attribute. Either set *VALUE, or if
21585 the value does not fit in *VALUE, set *BYTES - either already
21586 allocated on the objfile obstack, or newly allocated on OBSTACK,
21587 or, set *BATON, if we translated the constant to a location
21588 expression. */
21589
21590static void
ff39bb5e 21591dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21592 const char *name, struct obstack *obstack,
21593 struct dwarf2_cu *cu,
d521ce57 21594 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21595 struct dwarf2_locexpr_baton **baton)
21596{
21597 struct objfile *objfile = cu->objfile;
21598 struct comp_unit_head *cu_header = &cu->header;
c906108c 21599 struct dwarf_block *blk;
98bfdba5
PA
21600 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21601 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21602
21603 *value = 0;
21604 *bytes = NULL;
21605 *baton = NULL;
c906108c
SS
21606
21607 switch (attr->form)
21608 {
21609 case DW_FORM_addr:
3019eac3 21610 case DW_FORM_GNU_addr_index:
ac56253d 21611 {
ac56253d
TT
21612 gdb_byte *data;
21613
98bfdba5
PA
21614 if (TYPE_LENGTH (type) != cu_header->addr_size)
21615 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21616 cu_header->addr_size,
98bfdba5 21617 TYPE_LENGTH (type));
ac56253d
TT
21618 /* Symbols of this form are reasonably rare, so we just
21619 piggyback on the existing location code rather than writing
21620 a new implementation of symbol_computed_ops. */
8d749320 21621 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21622 (*baton)->per_cu = cu->per_cu;
21623 gdb_assert ((*baton)->per_cu);
ac56253d 21624
98bfdba5 21625 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21626 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21627 (*baton)->data = data;
ac56253d
TT
21628
21629 data[0] = DW_OP_addr;
21630 store_unsigned_integer (&data[1], cu_header->addr_size,
21631 byte_order, DW_ADDR (attr));
21632 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21633 }
c906108c 21634 break;
4ac36638 21635 case DW_FORM_string:
93b5768b 21636 case DW_FORM_strp:
3019eac3 21637 case DW_FORM_GNU_str_index:
36586728 21638 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21639 /* DW_STRING is already allocated on the objfile obstack, point
21640 directly to it. */
d521ce57 21641 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21642 break;
c906108c
SS
21643 case DW_FORM_block1:
21644 case DW_FORM_block2:
21645 case DW_FORM_block4:
21646 case DW_FORM_block:
2dc7f7b3 21647 case DW_FORM_exprloc:
0224619f 21648 case DW_FORM_data16:
c906108c 21649 blk = DW_BLOCK (attr);
98bfdba5
PA
21650 if (TYPE_LENGTH (type) != blk->size)
21651 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21652 TYPE_LENGTH (type));
21653 *bytes = blk->data;
c906108c 21654 break;
2df3850c
JM
21655
21656 /* The DW_AT_const_value attributes are supposed to carry the
21657 symbol's value "represented as it would be on the target
21658 architecture." By the time we get here, it's already been
21659 converted to host endianness, so we just need to sign- or
21660 zero-extend it as appropriate. */
21661 case DW_FORM_data1:
3aef2284 21662 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21663 break;
c906108c 21664 case DW_FORM_data2:
3aef2284 21665 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21666 break;
c906108c 21667 case DW_FORM_data4:
3aef2284 21668 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21669 break;
c906108c 21670 case DW_FORM_data8:
3aef2284 21671 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21672 break;
21673
c906108c 21674 case DW_FORM_sdata:
663c44ac 21675 case DW_FORM_implicit_const:
98bfdba5 21676 *value = DW_SND (attr);
2df3850c
JM
21677 break;
21678
c906108c 21679 case DW_FORM_udata:
98bfdba5 21680 *value = DW_UNSND (attr);
c906108c 21681 break;
2df3850c 21682
c906108c 21683 default:
4d3c2250 21684 complaint (&symfile_complaints,
e2e0b3e5 21685 _("unsupported const value attribute form: '%s'"),
4d3c2250 21686 dwarf_form_name (attr->form));
98bfdba5 21687 *value = 0;
c906108c
SS
21688 break;
21689 }
21690}
21691
2df3850c 21692
98bfdba5
PA
21693/* Copy constant value from an attribute to a symbol. */
21694
2df3850c 21695static void
ff39bb5e 21696dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21697 struct dwarf2_cu *cu)
2df3850c 21698{
98bfdba5 21699 struct objfile *objfile = cu->objfile;
12df843f 21700 LONGEST value;
d521ce57 21701 const gdb_byte *bytes;
98bfdba5 21702 struct dwarf2_locexpr_baton *baton;
2df3850c 21703
98bfdba5
PA
21704 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21705 SYMBOL_PRINT_NAME (sym),
21706 &objfile->objfile_obstack, cu,
21707 &value, &bytes, &baton);
2df3850c 21708
98bfdba5
PA
21709 if (baton != NULL)
21710 {
98bfdba5 21711 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21712 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21713 }
21714 else if (bytes != NULL)
21715 {
21716 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21717 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21718 }
21719 else
21720 {
21721 SYMBOL_VALUE (sym) = value;
f1e6e072 21722 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21723 }
2df3850c
JM
21724}
21725
c906108c
SS
21726/* Return the type of the die in question using its DW_AT_type attribute. */
21727
21728static struct type *
e7c27a73 21729die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21730{
c906108c 21731 struct attribute *type_attr;
c906108c 21732
e142c38c 21733 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21734 if (!type_attr)
21735 {
21736 /* A missing DW_AT_type represents a void type. */
46bf5051 21737 return objfile_type (cu->objfile)->builtin_void;
c906108c 21738 }
348e048f 21739
673bfd45 21740 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21741}
21742
b4ba55a1
JB
21743/* True iff CU's producer generates GNAT Ada auxiliary information
21744 that allows to find parallel types through that information instead
21745 of having to do expensive parallel lookups by type name. */
21746
21747static int
21748need_gnat_info (struct dwarf2_cu *cu)
21749{
21750 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
21751 of GNAT produces this auxiliary information, without any indication
21752 that it is produced. Part of enhancing the FSF version of GNAT
21753 to produce that information will be to put in place an indicator
21754 that we can use in order to determine whether the descriptive type
21755 info is available or not. One suggestion that has been made is
21756 to use a new attribute, attached to the CU die. For now, assume
21757 that the descriptive type info is not available. */
21758 return 0;
21759}
21760
b4ba55a1
JB
21761/* Return the auxiliary type of the die in question using its
21762 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21763 attribute is not present. */
21764
21765static struct type *
21766die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21767{
b4ba55a1 21768 struct attribute *type_attr;
b4ba55a1
JB
21769
21770 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21771 if (!type_attr)
21772 return NULL;
21773
673bfd45 21774 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21775}
21776
21777/* If DIE has a descriptive_type attribute, then set the TYPE's
21778 descriptive type accordingly. */
21779
21780static void
21781set_descriptive_type (struct type *type, struct die_info *die,
21782 struct dwarf2_cu *cu)
21783{
21784 struct type *descriptive_type = die_descriptive_type (die, cu);
21785
21786 if (descriptive_type)
21787 {
21788 ALLOCATE_GNAT_AUX_TYPE (type);
21789 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21790 }
21791}
21792
c906108c
SS
21793/* Return the containing type of the die in question using its
21794 DW_AT_containing_type attribute. */
21795
21796static struct type *
e7c27a73 21797die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21798{
c906108c 21799 struct attribute *type_attr;
c906108c 21800
e142c38c 21801 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21802 if (!type_attr)
21803 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 21804 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 21805
673bfd45 21806 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21807}
21808
ac9ec31b
DE
21809/* Return an error marker type to use for the ill formed type in DIE/CU. */
21810
21811static struct type *
21812build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21813{
21814 struct objfile *objfile = dwarf2_per_objfile->objfile;
21815 char *message, *saved;
21816
21817 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 21818 objfile_name (objfile),
9c541725
PA
21819 to_underlying (cu->header.sect_off),
21820 to_underlying (die->sect_off));
224c3ddb
SM
21821 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21822 message, strlen (message));
ac9ec31b
DE
21823 xfree (message);
21824
19f392bc 21825 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21826}
21827
673bfd45 21828/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21829 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21830 DW_AT_containing_type.
673bfd45
DE
21831 If there is no type substitute an error marker. */
21832
c906108c 21833static struct type *
ff39bb5e 21834lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21835 struct dwarf2_cu *cu)
c906108c 21836{
bb5ed363 21837 struct objfile *objfile = cu->objfile;
f792889a
DJ
21838 struct type *this_type;
21839
ac9ec31b
DE
21840 gdb_assert (attr->name == DW_AT_type
21841 || attr->name == DW_AT_GNAT_descriptive_type
21842 || attr->name == DW_AT_containing_type);
21843
673bfd45
DE
21844 /* First see if we have it cached. */
21845
36586728
TT
21846 if (attr->form == DW_FORM_GNU_ref_alt)
21847 {
21848 struct dwarf2_per_cu_data *per_cu;
9c541725 21849 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21850
9c541725
PA
21851 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
21852 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21853 }
7771576e 21854 else if (attr_form_is_ref (attr))
673bfd45 21855 {
9c541725 21856 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21857
9c541725 21858 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21859 }
55f1336d 21860 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21861 {
ac9ec31b 21862 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21863
ac9ec31b 21864 return get_signatured_type (die, signature, cu);
673bfd45
DE
21865 }
21866 else
21867 {
ac9ec31b
DE
21868 complaint (&symfile_complaints,
21869 _("Dwarf Error: Bad type attribute %s in DIE"
21870 " at 0x%x [in module %s]"),
9c541725 21871 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
4262abfb 21872 objfile_name (objfile));
ac9ec31b 21873 return build_error_marker_type (cu, die);
673bfd45
DE
21874 }
21875
21876 /* If not cached we need to read it in. */
21877
21878 if (this_type == NULL)
21879 {
ac9ec31b 21880 struct die_info *type_die = NULL;
673bfd45
DE
21881 struct dwarf2_cu *type_cu = cu;
21882
7771576e 21883 if (attr_form_is_ref (attr))
ac9ec31b
DE
21884 type_die = follow_die_ref (die, attr, &type_cu);
21885 if (type_die == NULL)
21886 return build_error_marker_type (cu, die);
21887 /* If we find the type now, it's probably because the type came
3019eac3
DE
21888 from an inter-CU reference and the type's CU got expanded before
21889 ours. */
ac9ec31b 21890 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21891 }
21892
21893 /* If we still don't have a type use an error marker. */
21894
21895 if (this_type == NULL)
ac9ec31b 21896 return build_error_marker_type (cu, die);
673bfd45 21897
f792889a 21898 return this_type;
c906108c
SS
21899}
21900
673bfd45
DE
21901/* Return the type in DIE, CU.
21902 Returns NULL for invalid types.
21903
02142a6c 21904 This first does a lookup in die_type_hash,
673bfd45
DE
21905 and only reads the die in if necessary.
21906
21907 NOTE: This can be called when reading in partial or full symbols. */
21908
f792889a 21909static struct type *
e7c27a73 21910read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21911{
f792889a
DJ
21912 struct type *this_type;
21913
21914 this_type = get_die_type (die, cu);
21915 if (this_type)
21916 return this_type;
21917
673bfd45
DE
21918 return read_type_die_1 (die, cu);
21919}
21920
21921/* Read the type in DIE, CU.
21922 Returns NULL for invalid types. */
21923
21924static struct type *
21925read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21926{
21927 struct type *this_type = NULL;
21928
c906108c
SS
21929 switch (die->tag)
21930 {
21931 case DW_TAG_class_type:
680b30c7 21932 case DW_TAG_interface_type:
c906108c
SS
21933 case DW_TAG_structure_type:
21934 case DW_TAG_union_type:
f792889a 21935 this_type = read_structure_type (die, cu);
c906108c
SS
21936 break;
21937 case DW_TAG_enumeration_type:
f792889a 21938 this_type = read_enumeration_type (die, cu);
c906108c
SS
21939 break;
21940 case DW_TAG_subprogram:
21941 case DW_TAG_subroutine_type:
edb3359d 21942 case DW_TAG_inlined_subroutine:
f792889a 21943 this_type = read_subroutine_type (die, cu);
c906108c
SS
21944 break;
21945 case DW_TAG_array_type:
f792889a 21946 this_type = read_array_type (die, cu);
c906108c 21947 break;
72019c9c 21948 case DW_TAG_set_type:
f792889a 21949 this_type = read_set_type (die, cu);
72019c9c 21950 break;
c906108c 21951 case DW_TAG_pointer_type:
f792889a 21952 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21953 break;
21954 case DW_TAG_ptr_to_member_type:
f792889a 21955 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21956 break;
21957 case DW_TAG_reference_type:
4297a3f0
AV
21958 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21959 break;
21960 case DW_TAG_rvalue_reference_type:
21961 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21962 break;
21963 case DW_TAG_const_type:
f792889a 21964 this_type = read_tag_const_type (die, cu);
c906108c
SS
21965 break;
21966 case DW_TAG_volatile_type:
f792889a 21967 this_type = read_tag_volatile_type (die, cu);
c906108c 21968 break;
06d66ee9
TT
21969 case DW_TAG_restrict_type:
21970 this_type = read_tag_restrict_type (die, cu);
21971 break;
c906108c 21972 case DW_TAG_string_type:
f792889a 21973 this_type = read_tag_string_type (die, cu);
c906108c
SS
21974 break;
21975 case DW_TAG_typedef:
f792889a 21976 this_type = read_typedef (die, cu);
c906108c 21977 break;
a02abb62 21978 case DW_TAG_subrange_type:
f792889a 21979 this_type = read_subrange_type (die, cu);
a02abb62 21980 break;
c906108c 21981 case DW_TAG_base_type:
f792889a 21982 this_type = read_base_type (die, cu);
c906108c 21983 break;
81a17f79 21984 case DW_TAG_unspecified_type:
f792889a 21985 this_type = read_unspecified_type (die, cu);
81a17f79 21986 break;
0114d602
DJ
21987 case DW_TAG_namespace:
21988 this_type = read_namespace_type (die, cu);
21989 break;
f55ee35c
JK
21990 case DW_TAG_module:
21991 this_type = read_module_type (die, cu);
21992 break;
a2c2acaf
MW
21993 case DW_TAG_atomic_type:
21994 this_type = read_tag_atomic_type (die, cu);
21995 break;
c906108c 21996 default:
3e43a32a
MS
21997 complaint (&symfile_complaints,
21998 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 21999 dwarf_tag_name (die->tag));
c906108c
SS
22000 break;
22001 }
63d06c5c 22002
f792889a 22003 return this_type;
63d06c5c
DC
22004}
22005
abc72ce4
DE
22006/* See if we can figure out if the class lives in a namespace. We do
22007 this by looking for a member function; its demangled name will
22008 contain namespace info, if there is any.
22009 Return the computed name or NULL.
22010 Space for the result is allocated on the objfile's obstack.
22011 This is the full-die version of guess_partial_die_structure_name.
22012 In this case we know DIE has no useful parent. */
22013
22014static char *
22015guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22016{
22017 struct die_info *spec_die;
22018 struct dwarf2_cu *spec_cu;
22019 struct die_info *child;
22020
22021 spec_cu = cu;
22022 spec_die = die_specification (die, &spec_cu);
22023 if (spec_die != NULL)
22024 {
22025 die = spec_die;
22026 cu = spec_cu;
22027 }
22028
22029 for (child = die->child;
22030 child != NULL;
22031 child = child->sibling)
22032 {
22033 if (child->tag == DW_TAG_subprogram)
22034 {
73b9be8b 22035 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22036
7d45c7c3 22037 if (linkage_name != NULL)
abc72ce4
DE
22038 {
22039 char *actual_name
22040 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22041 linkage_name);
abc72ce4
DE
22042 char *name = NULL;
22043
22044 if (actual_name != NULL)
22045 {
15d034d0 22046 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22047
22048 if (die_name != NULL
22049 && strcmp (die_name, actual_name) != 0)
22050 {
22051 /* Strip off the class name from the full name.
22052 We want the prefix. */
22053 int die_name_len = strlen (die_name);
22054 int actual_name_len = strlen (actual_name);
22055
22056 /* Test for '::' as a sanity check. */
22057 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22058 && actual_name[actual_name_len
22059 - die_name_len - 1] == ':')
224c3ddb
SM
22060 name = (char *) obstack_copy0 (
22061 &cu->objfile->per_bfd->storage_obstack,
22062 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22063 }
22064 }
22065 xfree (actual_name);
22066 return name;
22067 }
22068 }
22069 }
22070
22071 return NULL;
22072}
22073
96408a79
SA
22074/* GCC might emit a nameless typedef that has a linkage name. Determine the
22075 prefix part in such case. See
22076 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22077
a121b7c1 22078static const char *
96408a79
SA
22079anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22080{
22081 struct attribute *attr;
e6a959d6 22082 const char *base;
96408a79
SA
22083
22084 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22085 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22086 return NULL;
22087
7d45c7c3 22088 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22089 return NULL;
22090
73b9be8b 22091 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22092 if (attr == NULL || DW_STRING (attr) == NULL)
22093 return NULL;
22094
22095 /* dwarf2_name had to be already called. */
22096 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22097
22098 /* Strip the base name, keep any leading namespaces/classes. */
22099 base = strrchr (DW_STRING (attr), ':');
22100 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22101 return "";
22102
224c3ddb
SM
22103 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
22104 DW_STRING (attr),
22105 &base[-1] - DW_STRING (attr));
96408a79
SA
22106}
22107
fdde2d81 22108/* Return the name of the namespace/class that DIE is defined within,
0114d602 22109 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22110
0114d602
DJ
22111 For example, if we're within the method foo() in the following
22112 code:
22113
22114 namespace N {
22115 class C {
22116 void foo () {
22117 }
22118 };
22119 }
22120
22121 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22122
0d5cff50 22123static const char *
e142c38c 22124determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22125{
0114d602
DJ
22126 struct die_info *parent, *spec_die;
22127 struct dwarf2_cu *spec_cu;
22128 struct type *parent_type;
a121b7c1 22129 const char *retval;
63d06c5c 22130
9c37b5ae 22131 if (cu->language != language_cplus
c44af4eb
TT
22132 && cu->language != language_fortran && cu->language != language_d
22133 && cu->language != language_rust)
0114d602
DJ
22134 return "";
22135
96408a79
SA
22136 retval = anonymous_struct_prefix (die, cu);
22137 if (retval)
22138 return retval;
22139
0114d602
DJ
22140 /* We have to be careful in the presence of DW_AT_specification.
22141 For example, with GCC 3.4, given the code
22142
22143 namespace N {
22144 void foo() {
22145 // Definition of N::foo.
22146 }
22147 }
22148
22149 then we'll have a tree of DIEs like this:
22150
22151 1: DW_TAG_compile_unit
22152 2: DW_TAG_namespace // N
22153 3: DW_TAG_subprogram // declaration of N::foo
22154 4: DW_TAG_subprogram // definition of N::foo
22155 DW_AT_specification // refers to die #3
22156
22157 Thus, when processing die #4, we have to pretend that we're in
22158 the context of its DW_AT_specification, namely the contex of die
22159 #3. */
22160 spec_cu = cu;
22161 spec_die = die_specification (die, &spec_cu);
22162 if (spec_die == NULL)
22163 parent = die->parent;
22164 else
63d06c5c 22165 {
0114d602
DJ
22166 parent = spec_die->parent;
22167 cu = spec_cu;
63d06c5c 22168 }
0114d602
DJ
22169
22170 if (parent == NULL)
22171 return "";
98bfdba5
PA
22172 else if (parent->building_fullname)
22173 {
22174 const char *name;
22175 const char *parent_name;
22176
22177 /* It has been seen on RealView 2.2 built binaries,
22178 DW_TAG_template_type_param types actually _defined_ as
22179 children of the parent class:
22180
22181 enum E {};
22182 template class <class Enum> Class{};
22183 Class<enum E> class_e;
22184
22185 1: DW_TAG_class_type (Class)
22186 2: DW_TAG_enumeration_type (E)
22187 3: DW_TAG_enumerator (enum1:0)
22188 3: DW_TAG_enumerator (enum2:1)
22189 ...
22190 2: DW_TAG_template_type_param
22191 DW_AT_type DW_FORM_ref_udata (E)
22192
22193 Besides being broken debug info, it can put GDB into an
22194 infinite loop. Consider:
22195
22196 When we're building the full name for Class<E>, we'll start
22197 at Class, and go look over its template type parameters,
22198 finding E. We'll then try to build the full name of E, and
22199 reach here. We're now trying to build the full name of E,
22200 and look over the parent DIE for containing scope. In the
22201 broken case, if we followed the parent DIE of E, we'd again
22202 find Class, and once again go look at its template type
22203 arguments, etc., etc. Simply don't consider such parent die
22204 as source-level parent of this die (it can't be, the language
22205 doesn't allow it), and break the loop here. */
22206 name = dwarf2_name (die, cu);
22207 parent_name = dwarf2_name (parent, cu);
22208 complaint (&symfile_complaints,
22209 _("template param type '%s' defined within parent '%s'"),
22210 name ? name : "<unknown>",
22211 parent_name ? parent_name : "<unknown>");
22212 return "";
22213 }
63d06c5c 22214 else
0114d602
DJ
22215 switch (parent->tag)
22216 {
63d06c5c 22217 case DW_TAG_namespace:
0114d602 22218 parent_type = read_type_die (parent, cu);
acebe513
UW
22219 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22220 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22221 Work around this problem here. */
22222 if (cu->language == language_cplus
22223 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22224 return "";
0114d602
DJ
22225 /* We give a name to even anonymous namespaces. */
22226 return TYPE_TAG_NAME (parent_type);
63d06c5c 22227 case DW_TAG_class_type:
680b30c7 22228 case DW_TAG_interface_type:
63d06c5c 22229 case DW_TAG_structure_type:
0114d602 22230 case DW_TAG_union_type:
f55ee35c 22231 case DW_TAG_module:
0114d602
DJ
22232 parent_type = read_type_die (parent, cu);
22233 if (TYPE_TAG_NAME (parent_type) != NULL)
22234 return TYPE_TAG_NAME (parent_type);
22235 else
22236 /* An anonymous structure is only allowed non-static data
22237 members; no typedefs, no member functions, et cetera.
22238 So it does not need a prefix. */
22239 return "";
abc72ce4 22240 case DW_TAG_compile_unit:
95554aad 22241 case DW_TAG_partial_unit:
abc72ce4
DE
22242 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22243 if (cu->language == language_cplus
8b70b953 22244 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
22245 && die->child != NULL
22246 && (die->tag == DW_TAG_class_type
22247 || die->tag == DW_TAG_structure_type
22248 || die->tag == DW_TAG_union_type))
22249 {
22250 char *name = guess_full_die_structure_name (die, cu);
22251 if (name != NULL)
22252 return name;
22253 }
22254 return "";
3d567982
TT
22255 case DW_TAG_enumeration_type:
22256 parent_type = read_type_die (parent, cu);
22257 if (TYPE_DECLARED_CLASS (parent_type))
22258 {
22259 if (TYPE_TAG_NAME (parent_type) != NULL)
22260 return TYPE_TAG_NAME (parent_type);
22261 return "";
22262 }
22263 /* Fall through. */
63d06c5c 22264 default:
8176b9b8 22265 return determine_prefix (parent, cu);
63d06c5c 22266 }
63d06c5c
DC
22267}
22268
3e43a32a
MS
22269/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22270 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22271 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22272 an obconcat, otherwise allocate storage for the result. The CU argument is
22273 used to determine the language and hence, the appropriate separator. */
987504bb 22274
f55ee35c 22275#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22276
22277static char *
f55ee35c
JK
22278typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22279 int physname, struct dwarf2_cu *cu)
63d06c5c 22280{
f55ee35c 22281 const char *lead = "";
5c315b68 22282 const char *sep;
63d06c5c 22283
3e43a32a
MS
22284 if (suffix == NULL || suffix[0] == '\0'
22285 || prefix == NULL || prefix[0] == '\0')
987504bb 22286 sep = "";
45280282
IB
22287 else if (cu->language == language_d)
22288 {
22289 /* For D, the 'main' function could be defined in any module, but it
22290 should never be prefixed. */
22291 if (strcmp (suffix, "D main") == 0)
22292 {
22293 prefix = "";
22294 sep = "";
22295 }
22296 else
22297 sep = ".";
22298 }
f55ee35c
JK
22299 else if (cu->language == language_fortran && physname)
22300 {
22301 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22302 DW_AT_MIPS_linkage_name is preferred and used instead. */
22303
22304 lead = "__";
22305 sep = "_MOD_";
22306 }
987504bb
JJ
22307 else
22308 sep = "::";
63d06c5c 22309
6dd47d34
DE
22310 if (prefix == NULL)
22311 prefix = "";
22312 if (suffix == NULL)
22313 suffix = "";
22314
987504bb
JJ
22315 if (obs == NULL)
22316 {
3e43a32a 22317 char *retval
224c3ddb
SM
22318 = ((char *)
22319 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22320
f55ee35c
JK
22321 strcpy (retval, lead);
22322 strcat (retval, prefix);
6dd47d34
DE
22323 strcat (retval, sep);
22324 strcat (retval, suffix);
63d06c5c
DC
22325 return retval;
22326 }
987504bb
JJ
22327 else
22328 {
22329 /* We have an obstack. */
f55ee35c 22330 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22331 }
63d06c5c
DC
22332}
22333
c906108c
SS
22334/* Return sibling of die, NULL if no sibling. */
22335
f9aca02d 22336static struct die_info *
fba45db2 22337sibling_die (struct die_info *die)
c906108c 22338{
639d11d3 22339 return die->sibling;
c906108c
SS
22340}
22341
71c25dea
TT
22342/* Get name of a die, return NULL if not found. */
22343
15d034d0
TT
22344static const char *
22345dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22346 struct obstack *obstack)
22347{
22348 if (name && cu->language == language_cplus)
22349 {
2f408ecb 22350 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22351
2f408ecb 22352 if (!canon_name.empty ())
71c25dea 22353 {
2f408ecb
PA
22354 if (canon_name != name)
22355 name = (const char *) obstack_copy0 (obstack,
22356 canon_name.c_str (),
22357 canon_name.length ());
71c25dea
TT
22358 }
22359 }
22360
22361 return name;
c906108c
SS
22362}
22363
96553a0c
DE
22364/* Get name of a die, return NULL if not found.
22365 Anonymous namespaces are converted to their magic string. */
9219021c 22366
15d034d0 22367static const char *
e142c38c 22368dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22369{
22370 struct attribute *attr;
22371
e142c38c 22372 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22373 if ((!attr || !DW_STRING (attr))
96553a0c 22374 && die->tag != DW_TAG_namespace
53832f31
TT
22375 && die->tag != DW_TAG_class_type
22376 && die->tag != DW_TAG_interface_type
22377 && die->tag != DW_TAG_structure_type
22378 && die->tag != DW_TAG_union_type)
71c25dea
TT
22379 return NULL;
22380
22381 switch (die->tag)
22382 {
22383 case DW_TAG_compile_unit:
95554aad 22384 case DW_TAG_partial_unit:
71c25dea
TT
22385 /* Compilation units have a DW_AT_name that is a filename, not
22386 a source language identifier. */
22387 case DW_TAG_enumeration_type:
22388 case DW_TAG_enumerator:
22389 /* These tags always have simple identifiers already; no need
22390 to canonicalize them. */
22391 return DW_STRING (attr);
907af001 22392
96553a0c
DE
22393 case DW_TAG_namespace:
22394 if (attr != NULL && DW_STRING (attr) != NULL)
22395 return DW_STRING (attr);
22396 return CP_ANONYMOUS_NAMESPACE_STR;
22397
907af001
UW
22398 case DW_TAG_class_type:
22399 case DW_TAG_interface_type:
22400 case DW_TAG_structure_type:
22401 case DW_TAG_union_type:
22402 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22403 structures or unions. These were of the form "._%d" in GCC 4.1,
22404 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22405 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22406 if (attr && DW_STRING (attr)
61012eef
GB
22407 && (startswith (DW_STRING (attr), "._")
22408 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22409 return NULL;
53832f31
TT
22410
22411 /* GCC might emit a nameless typedef that has a linkage name. See
22412 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22413 if (!attr || DW_STRING (attr) == NULL)
22414 {
df5c6c50 22415 char *demangled = NULL;
53832f31 22416
73b9be8b 22417 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22418 if (attr == NULL || DW_STRING (attr) == NULL)
22419 return NULL;
22420
df5c6c50
JK
22421 /* Avoid demangling DW_STRING (attr) the second time on a second
22422 call for the same DIE. */
22423 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22424 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22425
22426 if (demangled)
22427 {
e6a959d6 22428 const char *base;
96408a79 22429
53832f31 22430 /* FIXME: we already did this for the partial symbol... */
34a68019 22431 DW_STRING (attr)
224c3ddb
SM
22432 = ((const char *)
22433 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
22434 demangled, strlen (demangled)));
53832f31
TT
22435 DW_STRING_IS_CANONICAL (attr) = 1;
22436 xfree (demangled);
96408a79
SA
22437
22438 /* Strip any leading namespaces/classes, keep only the base name.
22439 DW_AT_name for named DIEs does not contain the prefixes. */
22440 base = strrchr (DW_STRING (attr), ':');
22441 if (base && base > DW_STRING (attr) && base[-1] == ':')
22442 return &base[1];
22443 else
22444 return DW_STRING (attr);
53832f31
TT
22445 }
22446 }
907af001
UW
22447 break;
22448
71c25dea 22449 default:
907af001
UW
22450 break;
22451 }
22452
22453 if (!DW_STRING_IS_CANONICAL (attr))
22454 {
22455 DW_STRING (attr)
22456 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 22457 &cu->objfile->per_bfd->storage_obstack);
907af001 22458 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22459 }
907af001 22460 return DW_STRING (attr);
9219021c
DC
22461}
22462
22463/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22464 is none. *EXT_CU is the CU containing DIE on input, and the CU
22465 containing the return value on output. */
9219021c
DC
22466
22467static struct die_info *
f2f0e013 22468dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22469{
22470 struct attribute *attr;
9219021c 22471
f2f0e013 22472 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22473 if (attr == NULL)
22474 return NULL;
22475
f2f0e013 22476 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22477}
22478
c906108c
SS
22479/* Convert a DIE tag into its string name. */
22480
f39c6ffd 22481static const char *
aa1ee363 22482dwarf_tag_name (unsigned tag)
c906108c 22483{
f39c6ffd
TT
22484 const char *name = get_DW_TAG_name (tag);
22485
22486 if (name == NULL)
22487 return "DW_TAG_<unknown>";
22488
22489 return name;
c906108c
SS
22490}
22491
22492/* Convert a DWARF attribute code into its string name. */
22493
f39c6ffd 22494static const char *
aa1ee363 22495dwarf_attr_name (unsigned attr)
c906108c 22496{
f39c6ffd
TT
22497 const char *name;
22498
c764a876 22499#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22500 if (attr == DW_AT_MIPS_fde)
22501 return "DW_AT_MIPS_fde";
22502#else
22503 if (attr == DW_AT_HP_block_index)
22504 return "DW_AT_HP_block_index";
c764a876 22505#endif
f39c6ffd
TT
22506
22507 name = get_DW_AT_name (attr);
22508
22509 if (name == NULL)
22510 return "DW_AT_<unknown>";
22511
22512 return name;
c906108c
SS
22513}
22514
22515/* Convert a DWARF value form code into its string name. */
22516
f39c6ffd 22517static const char *
aa1ee363 22518dwarf_form_name (unsigned form)
c906108c 22519{
f39c6ffd
TT
22520 const char *name = get_DW_FORM_name (form);
22521
22522 if (name == NULL)
22523 return "DW_FORM_<unknown>";
22524
22525 return name;
c906108c
SS
22526}
22527
a121b7c1 22528static const char *
fba45db2 22529dwarf_bool_name (unsigned mybool)
c906108c
SS
22530{
22531 if (mybool)
22532 return "TRUE";
22533 else
22534 return "FALSE";
22535}
22536
22537/* Convert a DWARF type code into its string name. */
22538
f39c6ffd 22539static const char *
aa1ee363 22540dwarf_type_encoding_name (unsigned enc)
c906108c 22541{
f39c6ffd 22542 const char *name = get_DW_ATE_name (enc);
c906108c 22543
f39c6ffd
TT
22544 if (name == NULL)
22545 return "DW_ATE_<unknown>";
c906108c 22546
f39c6ffd 22547 return name;
c906108c 22548}
c906108c 22549
f9aca02d 22550static void
d97bc12b 22551dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22552{
22553 unsigned int i;
22554
d97bc12b
DE
22555 print_spaces (indent, f);
22556 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
9c541725
PA
22557 dwarf_tag_name (die->tag), die->abbrev,
22558 to_underlying (die->sect_off));
d97bc12b
DE
22559
22560 if (die->parent != NULL)
22561 {
22562 print_spaces (indent, f);
22563 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9c541725 22564 to_underlying (die->parent->sect_off));
d97bc12b
DE
22565 }
22566
22567 print_spaces (indent, f);
22568 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22569 dwarf_bool_name (die->child != NULL));
c906108c 22570
d97bc12b
DE
22571 print_spaces (indent, f);
22572 fprintf_unfiltered (f, " attributes:\n");
22573
c906108c
SS
22574 for (i = 0; i < die->num_attrs; ++i)
22575 {
d97bc12b
DE
22576 print_spaces (indent, f);
22577 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22578 dwarf_attr_name (die->attrs[i].name),
22579 dwarf_form_name (die->attrs[i].form));
d97bc12b 22580
c906108c
SS
22581 switch (die->attrs[i].form)
22582 {
c906108c 22583 case DW_FORM_addr:
3019eac3 22584 case DW_FORM_GNU_addr_index:
d97bc12b 22585 fprintf_unfiltered (f, "address: ");
5af949e3 22586 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22587 break;
22588 case DW_FORM_block2:
22589 case DW_FORM_block4:
22590 case DW_FORM_block:
22591 case DW_FORM_block1:
56eb65bd
SP
22592 fprintf_unfiltered (f, "block: size %s",
22593 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22594 break;
2dc7f7b3 22595 case DW_FORM_exprloc:
56eb65bd
SP
22596 fprintf_unfiltered (f, "expression: size %s",
22597 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22598 break;
0224619f
JK
22599 case DW_FORM_data16:
22600 fprintf_unfiltered (f, "constant of 16 bytes");
22601 break;
4568ecf9
DE
22602 case DW_FORM_ref_addr:
22603 fprintf_unfiltered (f, "ref address: ");
22604 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22605 break;
36586728
TT
22606 case DW_FORM_GNU_ref_alt:
22607 fprintf_unfiltered (f, "alt ref address: ");
22608 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22609 break;
10b3939b
DJ
22610 case DW_FORM_ref1:
22611 case DW_FORM_ref2:
22612 case DW_FORM_ref4:
4568ecf9
DE
22613 case DW_FORM_ref8:
22614 case DW_FORM_ref_udata:
d97bc12b 22615 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22616 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22617 break;
c906108c
SS
22618 case DW_FORM_data1:
22619 case DW_FORM_data2:
22620 case DW_FORM_data4:
ce5d95e1 22621 case DW_FORM_data8:
c906108c
SS
22622 case DW_FORM_udata:
22623 case DW_FORM_sdata:
43bbcdc2
PH
22624 fprintf_unfiltered (f, "constant: %s",
22625 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22626 break;
2dc7f7b3
TT
22627 case DW_FORM_sec_offset:
22628 fprintf_unfiltered (f, "section offset: %s",
22629 pulongest (DW_UNSND (&die->attrs[i])));
22630 break;
55f1336d 22631 case DW_FORM_ref_sig8:
ac9ec31b
DE
22632 fprintf_unfiltered (f, "signature: %s",
22633 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22634 break;
c906108c 22635 case DW_FORM_string:
4bdf3d34 22636 case DW_FORM_strp:
43988095 22637 case DW_FORM_line_strp:
3019eac3 22638 case DW_FORM_GNU_str_index:
36586728 22639 case DW_FORM_GNU_strp_alt:
8285870a 22640 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22641 DW_STRING (&die->attrs[i])
8285870a
JK
22642 ? DW_STRING (&die->attrs[i]) : "",
22643 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22644 break;
22645 case DW_FORM_flag:
22646 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22647 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22648 else
d97bc12b 22649 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22650 break;
2dc7f7b3
TT
22651 case DW_FORM_flag_present:
22652 fprintf_unfiltered (f, "flag: TRUE");
22653 break;
a8329558 22654 case DW_FORM_indirect:
0963b4bd
MS
22655 /* The reader will have reduced the indirect form to
22656 the "base form" so this form should not occur. */
3e43a32a
MS
22657 fprintf_unfiltered (f,
22658 "unexpected attribute form: DW_FORM_indirect");
a8329558 22659 break;
663c44ac
JK
22660 case DW_FORM_implicit_const:
22661 fprintf_unfiltered (f, "constant: %s",
22662 plongest (DW_SND (&die->attrs[i])));
22663 break;
c906108c 22664 default:
d97bc12b 22665 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22666 die->attrs[i].form);
d97bc12b 22667 break;
c906108c 22668 }
d97bc12b 22669 fprintf_unfiltered (f, "\n");
c906108c
SS
22670 }
22671}
22672
f9aca02d 22673static void
d97bc12b 22674dump_die_for_error (struct die_info *die)
c906108c 22675{
d97bc12b
DE
22676 dump_die_shallow (gdb_stderr, 0, die);
22677}
22678
22679static void
22680dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22681{
22682 int indent = level * 4;
22683
22684 gdb_assert (die != NULL);
22685
22686 if (level >= max_level)
22687 return;
22688
22689 dump_die_shallow (f, indent, die);
22690
22691 if (die->child != NULL)
c906108c 22692 {
d97bc12b
DE
22693 print_spaces (indent, f);
22694 fprintf_unfiltered (f, " Children:");
22695 if (level + 1 < max_level)
22696 {
22697 fprintf_unfiltered (f, "\n");
22698 dump_die_1 (f, level + 1, max_level, die->child);
22699 }
22700 else
22701 {
3e43a32a
MS
22702 fprintf_unfiltered (f,
22703 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22704 }
22705 }
22706
22707 if (die->sibling != NULL && level > 0)
22708 {
22709 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22710 }
22711}
22712
d97bc12b
DE
22713/* This is called from the pdie macro in gdbinit.in.
22714 It's not static so gcc will keep a copy callable from gdb. */
22715
22716void
22717dump_die (struct die_info *die, int max_level)
22718{
22719 dump_die_1 (gdb_stdlog, 0, max_level, die);
22720}
22721
f9aca02d 22722static void
51545339 22723store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22724{
51545339 22725 void **slot;
c906108c 22726
9c541725
PA
22727 slot = htab_find_slot_with_hash (cu->die_hash, die,
22728 to_underlying (die->sect_off),
b64f50a1 22729 INSERT);
51545339
DJ
22730
22731 *slot = die;
c906108c
SS
22732}
22733
b64f50a1
JK
22734/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22735 required kind. */
22736
22737static sect_offset
ff39bb5e 22738dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22739{
7771576e 22740 if (attr_form_is_ref (attr))
9c541725 22741 return (sect_offset) DW_UNSND (attr);
93311388
DE
22742
22743 complaint (&symfile_complaints,
22744 _("unsupported die ref attribute form: '%s'"),
22745 dwarf_form_name (attr->form));
9c541725 22746 return {};
c906108c
SS
22747}
22748
43bbcdc2
PH
22749/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22750 * the value held by the attribute is not constant. */
a02abb62 22751
43bbcdc2 22752static LONGEST
ff39bb5e 22753dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22754{
663c44ac 22755 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22756 return DW_SND (attr);
22757 else if (attr->form == DW_FORM_udata
22758 || attr->form == DW_FORM_data1
22759 || attr->form == DW_FORM_data2
22760 || attr->form == DW_FORM_data4
22761 || attr->form == DW_FORM_data8)
22762 return DW_UNSND (attr);
22763 else
22764 {
0224619f 22765 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
22766 complaint (&symfile_complaints,
22767 _("Attribute value is not a constant (%s)"),
a02abb62
JB
22768 dwarf_form_name (attr->form));
22769 return default_value;
22770 }
22771}
22772
348e048f
DE
22773/* Follow reference or signature attribute ATTR of SRC_DIE.
22774 On entry *REF_CU is the CU of SRC_DIE.
22775 On exit *REF_CU is the CU of the result. */
22776
22777static struct die_info *
ff39bb5e 22778follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22779 struct dwarf2_cu **ref_cu)
22780{
22781 struct die_info *die;
22782
7771576e 22783 if (attr_form_is_ref (attr))
348e048f 22784 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22785 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22786 die = follow_die_sig (src_die, attr, ref_cu);
22787 else
22788 {
22789 dump_die_for_error (src_die);
22790 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 22791 objfile_name ((*ref_cu)->objfile));
348e048f
DE
22792 }
22793
22794 return die;
03dd20cc
DJ
22795}
22796
5c631832 22797/* Follow reference OFFSET.
673bfd45
DE
22798 On entry *REF_CU is the CU of the source die referencing OFFSET.
22799 On exit *REF_CU is the CU of the result.
22800 Returns NULL if OFFSET is invalid. */
f504f079 22801
f9aca02d 22802static struct die_info *
9c541725 22803follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22804 struct dwarf2_cu **ref_cu)
c906108c 22805{
10b3939b 22806 struct die_info temp_die;
f2f0e013 22807 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 22808
348e048f
DE
22809 gdb_assert (cu->per_cu != NULL);
22810
98bfdba5
PA
22811 target_cu = cu;
22812
3019eac3 22813 if (cu->per_cu->is_debug_types)
348e048f
DE
22814 {
22815 /* .debug_types CUs cannot reference anything outside their CU.
22816 If they need to, they have to reference a signatured type via
55f1336d 22817 DW_FORM_ref_sig8. */
9c541725 22818 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 22819 return NULL;
348e048f 22820 }
36586728 22821 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 22822 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
22823 {
22824 struct dwarf2_per_cu_data *per_cu;
9a619af0 22825
9c541725 22826 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 22827 cu->objfile);
03dd20cc
DJ
22828
22829 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
22830 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22831 load_full_comp_unit (per_cu, cu->language);
03dd20cc 22832
10b3939b
DJ
22833 target_cu = per_cu->cu;
22834 }
98bfdba5
PA
22835 else if (cu->dies == NULL)
22836 {
22837 /* We're loading full DIEs during partial symbol reading. */
22838 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 22839 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 22840 }
c906108c 22841
f2f0e013 22842 *ref_cu = target_cu;
9c541725 22843 temp_die.sect_off = sect_off;
9a3c8263 22844 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22845 &temp_die,
22846 to_underlying (sect_off));
5c631832 22847}
10b3939b 22848
5c631832
JK
22849/* Follow reference attribute ATTR of SRC_DIE.
22850 On entry *REF_CU is the CU of SRC_DIE.
22851 On exit *REF_CU is the CU of the result. */
22852
22853static struct die_info *
ff39bb5e 22854follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22855 struct dwarf2_cu **ref_cu)
22856{
9c541725 22857 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22858 struct dwarf2_cu *cu = *ref_cu;
22859 struct die_info *die;
22860
9c541725 22861 die = follow_die_offset (sect_off,
36586728
TT
22862 (attr->form == DW_FORM_GNU_ref_alt
22863 || cu->per_cu->is_dwz),
22864 ref_cu);
5c631832
JK
22865 if (!die)
22866 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
22867 "at 0x%x [in module %s]"),
9c541725 22868 to_underlying (sect_off), to_underlying (src_die->sect_off),
4262abfb 22869 objfile_name (cu->objfile));
348e048f 22870
5c631832
JK
22871 return die;
22872}
22873
9c541725 22874/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b
JK
22875 Returned value is intended for DW_OP_call*. Returned
22876 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
22877
22878struct dwarf2_locexpr_baton
9c541725 22879dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
22880 struct dwarf2_per_cu_data *per_cu,
22881 CORE_ADDR (*get_frame_pc) (void *baton),
22882 void *baton)
5c631832 22883{
918dd910 22884 struct dwarf2_cu *cu;
5c631832
JK
22885 struct die_info *die;
22886 struct attribute *attr;
22887 struct dwarf2_locexpr_baton retval;
22888
8cf6f0b1
TT
22889 dw2_setup (per_cu->objfile);
22890
918dd910
JK
22891 if (per_cu->cu == NULL)
22892 load_cu (per_cu);
22893 cu = per_cu->cu;
cc12ce38
DE
22894 if (cu == NULL)
22895 {
22896 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22897 Instead just throw an error, not much else we can do. */
22898 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 22899 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 22900 }
918dd910 22901
9c541725 22902 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832
JK
22903 if (!die)
22904 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 22905 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
22906
22907 attr = dwarf2_attr (die, DW_AT_location, cu);
22908 if (!attr)
22909 {
e103e986
JK
22910 /* DWARF: "If there is no such attribute, then there is no effect.".
22911 DATA is ignored if SIZE is 0. */
5c631832 22912
e103e986 22913 retval.data = NULL;
5c631832
JK
22914 retval.size = 0;
22915 }
8cf6f0b1
TT
22916 else if (attr_form_is_section_offset (attr))
22917 {
22918 struct dwarf2_loclist_baton loclist_baton;
22919 CORE_ADDR pc = (*get_frame_pc) (baton);
22920 size_t size;
22921
22922 fill_in_loclist_baton (cu, &loclist_baton, attr);
22923
22924 retval.data = dwarf2_find_location_expression (&loclist_baton,
22925 &size, pc);
22926 retval.size = size;
22927 }
5c631832
JK
22928 else
22929 {
22930 if (!attr_form_is_block (attr))
22931 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
22932 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9c541725 22933 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
22934
22935 retval.data = DW_BLOCK (attr)->data;
22936 retval.size = DW_BLOCK (attr)->size;
22937 }
22938 retval.per_cu = cu->per_cu;
918dd910 22939
918dd910
JK
22940 age_cached_comp_units ();
22941
5c631832 22942 return retval;
348e048f
DE
22943}
22944
8b9737bf
TT
22945/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22946 offset. */
22947
22948struct dwarf2_locexpr_baton
22949dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22950 struct dwarf2_per_cu_data *per_cu,
22951 CORE_ADDR (*get_frame_pc) (void *baton),
22952 void *baton)
22953{
9c541725 22954 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22955
9c541725 22956 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22957}
22958
b6807d98
TT
22959/* Write a constant of a given type as target-ordered bytes into
22960 OBSTACK. */
22961
22962static const gdb_byte *
22963write_constant_as_bytes (struct obstack *obstack,
22964 enum bfd_endian byte_order,
22965 struct type *type,
22966 ULONGEST value,
22967 LONGEST *len)
22968{
22969 gdb_byte *result;
22970
22971 *len = TYPE_LENGTH (type);
224c3ddb 22972 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22973 store_unsigned_integer (result, *len, byte_order, value);
22974
22975 return result;
22976}
22977
22978/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22979 pointer to the constant bytes and set LEN to the length of the
22980 data. If memory is needed, allocate it on OBSTACK. If the DIE
22981 does not have a DW_AT_const_value, return NULL. */
22982
22983const gdb_byte *
9c541725 22984dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
22985 struct dwarf2_per_cu_data *per_cu,
22986 struct obstack *obstack,
22987 LONGEST *len)
22988{
22989 struct dwarf2_cu *cu;
22990 struct die_info *die;
22991 struct attribute *attr;
22992 const gdb_byte *result = NULL;
22993 struct type *type;
22994 LONGEST value;
22995 enum bfd_endian byte_order;
22996
22997 dw2_setup (per_cu->objfile);
22998
22999 if (per_cu->cu == NULL)
23000 load_cu (per_cu);
23001 cu = per_cu->cu;
cc12ce38
DE
23002 if (cu == NULL)
23003 {
23004 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23005 Instead just throw an error, not much else we can do. */
23006 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 23007 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 23008 }
b6807d98 23009
9c541725 23010 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98
TT
23011 if (!die)
23012 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 23013 to_underlying (sect_off), objfile_name (per_cu->objfile));
b6807d98
TT
23014
23015
23016 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23017 if (attr == NULL)
23018 return NULL;
23019
23020 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
23021 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23022
23023 switch (attr->form)
23024 {
23025 case DW_FORM_addr:
23026 case DW_FORM_GNU_addr_index:
23027 {
23028 gdb_byte *tem;
23029
23030 *len = cu->header.addr_size;
224c3ddb 23031 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23032 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23033 result = tem;
23034 }
23035 break;
23036 case DW_FORM_string:
23037 case DW_FORM_strp:
23038 case DW_FORM_GNU_str_index:
23039 case DW_FORM_GNU_strp_alt:
23040 /* DW_STRING is already allocated on the objfile obstack, point
23041 directly to it. */
23042 result = (const gdb_byte *) DW_STRING (attr);
23043 *len = strlen (DW_STRING (attr));
23044 break;
23045 case DW_FORM_block1:
23046 case DW_FORM_block2:
23047 case DW_FORM_block4:
23048 case DW_FORM_block:
23049 case DW_FORM_exprloc:
0224619f 23050 case DW_FORM_data16:
b6807d98
TT
23051 result = DW_BLOCK (attr)->data;
23052 *len = DW_BLOCK (attr)->size;
23053 break;
23054
23055 /* The DW_AT_const_value attributes are supposed to carry the
23056 symbol's value "represented as it would be on the target
23057 architecture." By the time we get here, it's already been
23058 converted to host endianness, so we just need to sign- or
23059 zero-extend it as appropriate. */
23060 case DW_FORM_data1:
23061 type = die_type (die, cu);
23062 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23063 if (result == NULL)
23064 result = write_constant_as_bytes (obstack, byte_order,
23065 type, value, len);
23066 break;
23067 case DW_FORM_data2:
23068 type = die_type (die, cu);
23069 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23070 if (result == NULL)
23071 result = write_constant_as_bytes (obstack, byte_order,
23072 type, value, len);
23073 break;
23074 case DW_FORM_data4:
23075 type = die_type (die, cu);
23076 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23077 if (result == NULL)
23078 result = write_constant_as_bytes (obstack, byte_order,
23079 type, value, len);
23080 break;
23081 case DW_FORM_data8:
23082 type = die_type (die, cu);
23083 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23084 if (result == NULL)
23085 result = write_constant_as_bytes (obstack, byte_order,
23086 type, value, len);
23087 break;
23088
23089 case DW_FORM_sdata:
663c44ac 23090 case DW_FORM_implicit_const:
b6807d98
TT
23091 type = die_type (die, cu);
23092 result = write_constant_as_bytes (obstack, byte_order,
23093 type, DW_SND (attr), len);
23094 break;
23095
23096 case DW_FORM_udata:
23097 type = die_type (die, cu);
23098 result = write_constant_as_bytes (obstack, byte_order,
23099 type, DW_UNSND (attr), len);
23100 break;
23101
23102 default:
23103 complaint (&symfile_complaints,
23104 _("unsupported const value attribute form: '%s'"),
23105 dwarf_form_name (attr->form));
23106 break;
23107 }
23108
23109 return result;
23110}
23111
7942e96e
AA
23112/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23113 valid type for this die is found. */
23114
23115struct type *
9c541725 23116dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23117 struct dwarf2_per_cu_data *per_cu)
23118{
23119 struct dwarf2_cu *cu;
23120 struct die_info *die;
23121
23122 dw2_setup (per_cu->objfile);
23123
23124 if (per_cu->cu == NULL)
23125 load_cu (per_cu);
23126 cu = per_cu->cu;
23127 if (!cu)
23128 return NULL;
23129
9c541725 23130 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23131 if (!die)
23132 return NULL;
23133
23134 return die_type (die, cu);
23135}
23136
8a9b8146
TT
23137/* Return the type of the DIE at DIE_OFFSET in the CU named by
23138 PER_CU. */
23139
23140struct type *
b64f50a1 23141dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23142 struct dwarf2_per_cu_data *per_cu)
23143{
8a9b8146 23144 dw2_setup (per_cu->objfile);
b64f50a1 23145
9c541725 23146 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23147 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23148}
23149
ac9ec31b 23150/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23151 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23152 On exit *REF_CU is the CU of the result.
23153 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23154
23155static struct die_info *
ac9ec31b
DE
23156follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23157 struct dwarf2_cu **ref_cu)
348e048f 23158{
348e048f 23159 struct die_info temp_die;
348e048f
DE
23160 struct dwarf2_cu *sig_cu;
23161 struct die_info *die;
23162
ac9ec31b
DE
23163 /* While it might be nice to assert sig_type->type == NULL here,
23164 we can get here for DW_AT_imported_declaration where we need
23165 the DIE not the type. */
348e048f
DE
23166
23167 /* If necessary, add it to the queue and load its DIEs. */
23168
95554aad 23169 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23170 read_signatured_type (sig_type);
348e048f 23171
348e048f 23172 sig_cu = sig_type->per_cu.cu;
69d751e3 23173 gdb_assert (sig_cu != NULL);
9c541725
PA
23174 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23175 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23176 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23177 to_underlying (temp_die.sect_off));
348e048f
DE
23178 if (die)
23179 {
796a7ff8
DE
23180 /* For .gdb_index version 7 keep track of included TUs.
23181 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23182 if (dwarf2_per_objfile->index_table != NULL
23183 && dwarf2_per_objfile->index_table->version <= 7)
23184 {
23185 VEC_safe_push (dwarf2_per_cu_ptr,
23186 (*ref_cu)->per_cu->imported_symtabs,
23187 sig_cu->per_cu);
23188 }
23189
348e048f
DE
23190 *ref_cu = sig_cu;
23191 return die;
23192 }
23193
ac9ec31b
DE
23194 return NULL;
23195}
23196
23197/* Follow signatured type referenced by ATTR in SRC_DIE.
23198 On entry *REF_CU is the CU of SRC_DIE.
23199 On exit *REF_CU is the CU of the result.
23200 The result is the DIE of the type.
23201 If the referenced type cannot be found an error is thrown. */
23202
23203static struct die_info *
ff39bb5e 23204follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23205 struct dwarf2_cu **ref_cu)
23206{
23207 ULONGEST signature = DW_SIGNATURE (attr);
23208 struct signatured_type *sig_type;
23209 struct die_info *die;
23210
23211 gdb_assert (attr->form == DW_FORM_ref_sig8);
23212
a2ce51a0 23213 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23214 /* sig_type will be NULL if the signatured type is missing from
23215 the debug info. */
23216 if (sig_type == NULL)
23217 {
23218 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23219 " from DIE at 0x%x [in module %s]"),
9c541725 23220 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 23221 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
23222 }
23223
23224 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23225 if (die == NULL)
23226 {
23227 dump_die_for_error (src_die);
23228 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23229 " from DIE at 0x%x [in module %s]"),
9c541725 23230 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 23231 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
23232 }
23233
23234 return die;
23235}
23236
23237/* Get the type specified by SIGNATURE referenced in DIE/CU,
23238 reading in and processing the type unit if necessary. */
23239
23240static struct type *
23241get_signatured_type (struct die_info *die, ULONGEST signature,
23242 struct dwarf2_cu *cu)
23243{
23244 struct signatured_type *sig_type;
23245 struct dwarf2_cu *type_cu;
23246 struct die_info *type_die;
23247 struct type *type;
23248
a2ce51a0 23249 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23250 /* sig_type will be NULL if the signatured type is missing from
23251 the debug info. */
23252 if (sig_type == NULL)
23253 {
23254 complaint (&symfile_complaints,
23255 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23256 " from DIE at 0x%x [in module %s]"),
9c541725 23257 hex_string (signature), to_underlying (die->sect_off),
4262abfb 23258 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23259 return build_error_marker_type (cu, die);
23260 }
23261
23262 /* If we already know the type we're done. */
23263 if (sig_type->type != NULL)
23264 return sig_type->type;
23265
23266 type_cu = cu;
23267 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23268 if (type_die != NULL)
23269 {
23270 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23271 is created. This is important, for example, because for c++ classes
23272 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23273 type = read_type_die (type_die, type_cu);
23274 if (type == NULL)
23275 {
23276 complaint (&symfile_complaints,
23277 _("Dwarf Error: Cannot build signatured type %s"
23278 " referenced from DIE at 0x%x [in module %s]"),
9c541725 23279 hex_string (signature), to_underlying (die->sect_off),
4262abfb 23280 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23281 type = build_error_marker_type (cu, die);
23282 }
23283 }
23284 else
23285 {
23286 complaint (&symfile_complaints,
23287 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23288 " from DIE at 0x%x [in module %s]"),
9c541725 23289 hex_string (signature), to_underlying (die->sect_off),
4262abfb 23290 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23291 type = build_error_marker_type (cu, die);
23292 }
23293 sig_type->type = type;
23294
23295 return type;
23296}
23297
23298/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23299 reading in and processing the type unit if necessary. */
23300
23301static struct type *
ff39bb5e 23302get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23303 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23304{
23305 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23306 if (attr_form_is_ref (attr))
ac9ec31b
DE
23307 {
23308 struct dwarf2_cu *type_cu = cu;
23309 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23310
23311 return read_type_die (type_die, type_cu);
23312 }
23313 else if (attr->form == DW_FORM_ref_sig8)
23314 {
23315 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23316 }
23317 else
23318 {
23319 complaint (&symfile_complaints,
23320 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23321 " at 0x%x [in module %s]"),
9c541725 23322 dwarf_form_name (attr->form), to_underlying (die->sect_off),
4262abfb 23323 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23324 return build_error_marker_type (cu, die);
23325 }
348e048f
DE
23326}
23327
e5fe5e75 23328/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23329
23330static void
e5fe5e75 23331load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23332{
52dc124a 23333 struct signatured_type *sig_type;
348e048f 23334
f4dc4d17
DE
23335 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23336 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23337
6721b2ec
DE
23338 /* We have the per_cu, but we need the signatured_type.
23339 Fortunately this is an easy translation. */
23340 gdb_assert (per_cu->is_debug_types);
23341 sig_type = (struct signatured_type *) per_cu;
348e048f 23342
6721b2ec 23343 gdb_assert (per_cu->cu == NULL);
348e048f 23344
52dc124a 23345 read_signatured_type (sig_type);
348e048f 23346
6721b2ec 23347 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23348}
23349
dee91e82
DE
23350/* die_reader_func for read_signatured_type.
23351 This is identical to load_full_comp_unit_reader,
23352 but is kept separate for now. */
348e048f
DE
23353
23354static void
dee91e82 23355read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23356 const gdb_byte *info_ptr,
dee91e82
DE
23357 struct die_info *comp_unit_die,
23358 int has_children,
23359 void *data)
348e048f 23360{
dee91e82 23361 struct dwarf2_cu *cu = reader->cu;
348e048f 23362
dee91e82
DE
23363 gdb_assert (cu->die_hash == NULL);
23364 cu->die_hash =
23365 htab_create_alloc_ex (cu->header.length / 12,
23366 die_hash,
23367 die_eq,
23368 NULL,
23369 &cu->comp_unit_obstack,
23370 hashtab_obstack_allocate,
23371 dummy_obstack_deallocate);
348e048f 23372
dee91e82
DE
23373 if (has_children)
23374 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23375 &info_ptr, comp_unit_die);
23376 cu->dies = comp_unit_die;
23377 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23378
23379 /* We try not to read any attributes in this function, because not
9cdd5dbd 23380 all CUs needed for references have been loaded yet, and symbol
348e048f 23381 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23382 or we won't be able to build types correctly.
23383 Similarly, if we do not read the producer, we can not apply
23384 producer-specific interpretation. */
95554aad 23385 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23386}
348e048f 23387
3019eac3
DE
23388/* Read in a signatured type and build its CU and DIEs.
23389 If the type is a stub for the real type in a DWO file,
23390 read in the real type from the DWO file as well. */
dee91e82
DE
23391
23392static void
23393read_signatured_type (struct signatured_type *sig_type)
23394{
23395 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23396
3019eac3 23397 gdb_assert (per_cu->is_debug_types);
dee91e82 23398 gdb_assert (per_cu->cu == NULL);
348e048f 23399
f4dc4d17
DE
23400 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23401 read_signatured_type_reader, NULL);
7ee85ab1 23402 sig_type->per_cu.tu_read = 1;
c906108c
SS
23403}
23404
c906108c
SS
23405/* Decode simple location descriptions.
23406 Given a pointer to a dwarf block that defines a location, compute
23407 the location and return the value.
23408
4cecd739
DJ
23409 NOTE drow/2003-11-18: This function is called in two situations
23410 now: for the address of static or global variables (partial symbols
23411 only) and for offsets into structures which are expected to be
23412 (more or less) constant. The partial symbol case should go away,
23413 and only the constant case should remain. That will let this
23414 function complain more accurately. A few special modes are allowed
23415 without complaint for global variables (for instance, global
23416 register values and thread-local values).
c906108c
SS
23417
23418 A location description containing no operations indicates that the
4cecd739 23419 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23420 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23421 callers will only want a very basic result and this can become a
21ae7a4d
JK
23422 complaint.
23423
23424 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23425
23426static CORE_ADDR
e7c27a73 23427decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23428{
e7c27a73 23429 struct objfile *objfile = cu->objfile;
56eb65bd
SP
23430 size_t i;
23431 size_t size = blk->size;
d521ce57 23432 const gdb_byte *data = blk->data;
21ae7a4d
JK
23433 CORE_ADDR stack[64];
23434 int stacki;
23435 unsigned int bytes_read, unsnd;
23436 gdb_byte op;
c906108c 23437
21ae7a4d
JK
23438 i = 0;
23439 stacki = 0;
23440 stack[stacki] = 0;
23441 stack[++stacki] = 0;
23442
23443 while (i < size)
23444 {
23445 op = data[i++];
23446 switch (op)
23447 {
23448 case DW_OP_lit0:
23449 case DW_OP_lit1:
23450 case DW_OP_lit2:
23451 case DW_OP_lit3:
23452 case DW_OP_lit4:
23453 case DW_OP_lit5:
23454 case DW_OP_lit6:
23455 case DW_OP_lit7:
23456 case DW_OP_lit8:
23457 case DW_OP_lit9:
23458 case DW_OP_lit10:
23459 case DW_OP_lit11:
23460 case DW_OP_lit12:
23461 case DW_OP_lit13:
23462 case DW_OP_lit14:
23463 case DW_OP_lit15:
23464 case DW_OP_lit16:
23465 case DW_OP_lit17:
23466 case DW_OP_lit18:
23467 case DW_OP_lit19:
23468 case DW_OP_lit20:
23469 case DW_OP_lit21:
23470 case DW_OP_lit22:
23471 case DW_OP_lit23:
23472 case DW_OP_lit24:
23473 case DW_OP_lit25:
23474 case DW_OP_lit26:
23475 case DW_OP_lit27:
23476 case DW_OP_lit28:
23477 case DW_OP_lit29:
23478 case DW_OP_lit30:
23479 case DW_OP_lit31:
23480 stack[++stacki] = op - DW_OP_lit0;
23481 break;
f1bea926 23482
21ae7a4d
JK
23483 case DW_OP_reg0:
23484 case DW_OP_reg1:
23485 case DW_OP_reg2:
23486 case DW_OP_reg3:
23487 case DW_OP_reg4:
23488 case DW_OP_reg5:
23489 case DW_OP_reg6:
23490 case DW_OP_reg7:
23491 case DW_OP_reg8:
23492 case DW_OP_reg9:
23493 case DW_OP_reg10:
23494 case DW_OP_reg11:
23495 case DW_OP_reg12:
23496 case DW_OP_reg13:
23497 case DW_OP_reg14:
23498 case DW_OP_reg15:
23499 case DW_OP_reg16:
23500 case DW_OP_reg17:
23501 case DW_OP_reg18:
23502 case DW_OP_reg19:
23503 case DW_OP_reg20:
23504 case DW_OP_reg21:
23505 case DW_OP_reg22:
23506 case DW_OP_reg23:
23507 case DW_OP_reg24:
23508 case DW_OP_reg25:
23509 case DW_OP_reg26:
23510 case DW_OP_reg27:
23511 case DW_OP_reg28:
23512 case DW_OP_reg29:
23513 case DW_OP_reg30:
23514 case DW_OP_reg31:
23515 stack[++stacki] = op - DW_OP_reg0;
23516 if (i < size)
23517 dwarf2_complex_location_expr_complaint ();
23518 break;
c906108c 23519
21ae7a4d
JK
23520 case DW_OP_regx:
23521 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23522 i += bytes_read;
23523 stack[++stacki] = unsnd;
23524 if (i < size)
23525 dwarf2_complex_location_expr_complaint ();
23526 break;
c906108c 23527
21ae7a4d
JK
23528 case DW_OP_addr:
23529 stack[++stacki] = read_address (objfile->obfd, &data[i],
23530 cu, &bytes_read);
23531 i += bytes_read;
23532 break;
d53d4ac5 23533
21ae7a4d
JK
23534 case DW_OP_const1u:
23535 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23536 i += 1;
23537 break;
23538
23539 case DW_OP_const1s:
23540 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23541 i += 1;
23542 break;
23543
23544 case DW_OP_const2u:
23545 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23546 i += 2;
23547 break;
23548
23549 case DW_OP_const2s:
23550 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23551 i += 2;
23552 break;
d53d4ac5 23553
21ae7a4d
JK
23554 case DW_OP_const4u:
23555 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23556 i += 4;
23557 break;
23558
23559 case DW_OP_const4s:
23560 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23561 i += 4;
23562 break;
23563
585861ea
JK
23564 case DW_OP_const8u:
23565 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23566 i += 8;
23567 break;
23568
21ae7a4d
JK
23569 case DW_OP_constu:
23570 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23571 &bytes_read);
23572 i += bytes_read;
23573 break;
23574
23575 case DW_OP_consts:
23576 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23577 i += bytes_read;
23578 break;
23579
23580 case DW_OP_dup:
23581 stack[stacki + 1] = stack[stacki];
23582 stacki++;
23583 break;
23584
23585 case DW_OP_plus:
23586 stack[stacki - 1] += stack[stacki];
23587 stacki--;
23588 break;
23589
23590 case DW_OP_plus_uconst:
23591 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23592 &bytes_read);
23593 i += bytes_read;
23594 break;
23595
23596 case DW_OP_minus:
23597 stack[stacki - 1] -= stack[stacki];
23598 stacki--;
23599 break;
23600
23601 case DW_OP_deref:
23602 /* If we're not the last op, then we definitely can't encode
23603 this using GDB's address_class enum. This is valid for partial
23604 global symbols, although the variable's address will be bogus
23605 in the psymtab. */
23606 if (i < size)
23607 dwarf2_complex_location_expr_complaint ();
23608 break;
23609
23610 case DW_OP_GNU_push_tls_address:
4aa4e28b 23611 case DW_OP_form_tls_address:
21ae7a4d
JK
23612 /* The top of the stack has the offset from the beginning
23613 of the thread control block at which the variable is located. */
23614 /* Nothing should follow this operator, so the top of stack would
23615 be returned. */
23616 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23617 address will be bogus in the psymtab. Make it always at least
23618 non-zero to not look as a variable garbage collected by linker
23619 which have DW_OP_addr 0. */
21ae7a4d
JK
23620 if (i < size)
23621 dwarf2_complex_location_expr_complaint ();
585861ea 23622 stack[stacki]++;
21ae7a4d
JK
23623 break;
23624
23625 case DW_OP_GNU_uninit:
23626 break;
23627
3019eac3 23628 case DW_OP_GNU_addr_index:
49f6c839 23629 case DW_OP_GNU_const_index:
3019eac3
DE
23630 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23631 &bytes_read);
23632 i += bytes_read;
23633 break;
23634
21ae7a4d
JK
23635 default:
23636 {
f39c6ffd 23637 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23638
23639 if (name)
23640 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23641 name);
23642 else
23643 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23644 op);
23645 }
23646
23647 return (stack[stacki]);
d53d4ac5 23648 }
3c6e0cb3 23649
21ae7a4d
JK
23650 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23651 outside of the allocated space. Also enforce minimum>0. */
23652 if (stacki >= ARRAY_SIZE (stack) - 1)
23653 {
23654 complaint (&symfile_complaints,
23655 _("location description stack overflow"));
23656 return 0;
23657 }
23658
23659 if (stacki <= 0)
23660 {
23661 complaint (&symfile_complaints,
23662 _("location description stack underflow"));
23663 return 0;
23664 }
23665 }
23666 return (stack[stacki]);
c906108c
SS
23667}
23668
23669/* memory allocation interface */
23670
c906108c 23671static struct dwarf_block *
7b5a2f43 23672dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23673{
8d749320 23674 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23675}
23676
c906108c 23677static struct die_info *
b60c80d6 23678dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23679{
23680 struct die_info *die;
b60c80d6
DJ
23681 size_t size = sizeof (struct die_info);
23682
23683 if (num_attrs > 1)
23684 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23685
b60c80d6 23686 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23687 memset (die, 0, sizeof (struct die_info));
23688 return (die);
23689}
2e276125
JB
23690
23691\f
23692/* Macro support. */
23693
233d95b5
JK
23694/* Return file name relative to the compilation directory of file number I in
23695 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 23696 responsible for freeing it. */
233d95b5 23697
2e276125 23698static char *
233d95b5 23699file_file_name (int file, struct line_header *lh)
2e276125 23700{
6a83a1e6
EZ
23701 /* Is the file number a valid index into the line header's file name
23702 table? Remember that file numbers start with one, not zero. */
fff8551c 23703 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 23704 {
8c43009f 23705 const file_entry &fe = lh->file_names[file - 1];
6e70227d 23706
8c43009f
PA
23707 if (!IS_ABSOLUTE_PATH (fe.name))
23708 {
23709 const char *dir = fe.include_dir (lh);
23710 if (dir != NULL)
23711 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23712 }
23713 return xstrdup (fe.name);
6a83a1e6 23714 }
2e276125
JB
23715 else
23716 {
6a83a1e6
EZ
23717 /* The compiler produced a bogus file number. We can at least
23718 record the macro definitions made in the file, even if we
23719 won't be able to find the file by name. */
23720 char fake_name[80];
9a619af0 23721
8c042590
PM
23722 xsnprintf (fake_name, sizeof (fake_name),
23723 "<bad macro file number %d>", file);
2e276125 23724
6e70227d 23725 complaint (&symfile_complaints,
6a83a1e6
EZ
23726 _("bad file number in macro information (%d)"),
23727 file);
2e276125 23728
6a83a1e6 23729 return xstrdup (fake_name);
2e276125
JB
23730 }
23731}
23732
233d95b5
JK
23733/* Return the full name of file number I in *LH's file name table.
23734 Use COMP_DIR as the name of the current directory of the
23735 compilation. The result is allocated using xmalloc; the caller is
23736 responsible for freeing it. */
23737static char *
23738file_full_name (int file, struct line_header *lh, const char *comp_dir)
23739{
23740 /* Is the file number a valid index into the line header's file name
23741 table? Remember that file numbers start with one, not zero. */
fff8551c 23742 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
23743 {
23744 char *relative = file_file_name (file, lh);
23745
23746 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23747 return relative;
b36cec19
PA
23748 return reconcat (relative, comp_dir, SLASH_STRING,
23749 relative, (char *) NULL);
233d95b5
JK
23750 }
23751 else
23752 return file_file_name (file, lh);
23753}
23754
2e276125
JB
23755
23756static struct macro_source_file *
23757macro_start_file (int file, int line,
23758 struct macro_source_file *current_file,
43f3e411 23759 struct line_header *lh)
2e276125 23760{
233d95b5
JK
23761 /* File name relative to the compilation directory of this source file. */
23762 char *file_name = file_file_name (file, lh);
2e276125 23763
2e276125 23764 if (! current_file)
abc9d0dc 23765 {
fc474241
DE
23766 /* Note: We don't create a macro table for this compilation unit
23767 at all until we actually get a filename. */
43f3e411 23768 struct macro_table *macro_table = get_macro_table ();
fc474241 23769
abc9d0dc
TT
23770 /* If we have no current file, then this must be the start_file
23771 directive for the compilation unit's main source file. */
fc474241
DE
23772 current_file = macro_set_main (macro_table, file_name);
23773 macro_define_special (macro_table);
abc9d0dc 23774 }
2e276125 23775 else
233d95b5 23776 current_file = macro_include (current_file, line, file_name);
2e276125 23777
233d95b5 23778 xfree (file_name);
6e70227d 23779
2e276125
JB
23780 return current_file;
23781}
23782
2e276125
JB
23783static const char *
23784consume_improper_spaces (const char *p, const char *body)
23785{
23786 if (*p == ' ')
23787 {
4d3c2250 23788 complaint (&symfile_complaints,
3e43a32a
MS
23789 _("macro definition contains spaces "
23790 "in formal argument list:\n`%s'"),
4d3c2250 23791 body);
2e276125
JB
23792
23793 while (*p == ' ')
23794 p++;
23795 }
23796
23797 return p;
23798}
23799
23800
23801static void
23802parse_macro_definition (struct macro_source_file *file, int line,
23803 const char *body)
23804{
23805 const char *p;
23806
23807 /* The body string takes one of two forms. For object-like macro
23808 definitions, it should be:
23809
23810 <macro name> " " <definition>
23811
23812 For function-like macro definitions, it should be:
23813
23814 <macro name> "() " <definition>
23815 or
23816 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23817
23818 Spaces may appear only where explicitly indicated, and in the
23819 <definition>.
23820
23821 The Dwarf 2 spec says that an object-like macro's name is always
23822 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23823 the space when the macro's definition is the empty string.
2e276125
JB
23824
23825 The Dwarf 2 spec says that there should be no spaces between the
23826 formal arguments in a function-like macro's formal argument list,
23827 but versions of GCC around March 2002 include spaces after the
23828 commas. */
23829
23830
23831 /* Find the extent of the macro name. The macro name is terminated
23832 by either a space or null character (for an object-like macro) or
23833 an opening paren (for a function-like macro). */
23834 for (p = body; *p; p++)
23835 if (*p == ' ' || *p == '(')
23836 break;
23837
23838 if (*p == ' ' || *p == '\0')
23839 {
23840 /* It's an object-like macro. */
23841 int name_len = p - body;
3f8a7804 23842 char *name = savestring (body, name_len);
2e276125
JB
23843 const char *replacement;
23844
23845 if (*p == ' ')
23846 replacement = body + name_len + 1;
23847 else
23848 {
4d3c2250 23849 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23850 replacement = body + name_len;
23851 }
6e70227d 23852
2e276125
JB
23853 macro_define_object (file, line, name, replacement);
23854
23855 xfree (name);
23856 }
23857 else if (*p == '(')
23858 {
23859 /* It's a function-like macro. */
3f8a7804 23860 char *name = savestring (body, p - body);
2e276125
JB
23861 int argc = 0;
23862 int argv_size = 1;
8d749320 23863 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23864
23865 p++;
23866
23867 p = consume_improper_spaces (p, body);
23868
23869 /* Parse the formal argument list. */
23870 while (*p && *p != ')')
23871 {
23872 /* Find the extent of the current argument name. */
23873 const char *arg_start = p;
23874
23875 while (*p && *p != ',' && *p != ')' && *p != ' ')
23876 p++;
23877
23878 if (! *p || p == arg_start)
4d3c2250 23879 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23880 else
23881 {
23882 /* Make sure argv has room for the new argument. */
23883 if (argc >= argv_size)
23884 {
23885 argv_size *= 2;
224c3ddb 23886 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
23887 }
23888
3f8a7804 23889 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
23890 }
23891
23892 p = consume_improper_spaces (p, body);
23893
23894 /* Consume the comma, if present. */
23895 if (*p == ',')
23896 {
23897 p++;
23898
23899 p = consume_improper_spaces (p, body);
23900 }
23901 }
23902
23903 if (*p == ')')
23904 {
23905 p++;
23906
23907 if (*p == ' ')
23908 /* Perfectly formed definition, no complaints. */
23909 macro_define_function (file, line, name,
6e70227d 23910 argc, (const char **) argv,
2e276125
JB
23911 p + 1);
23912 else if (*p == '\0')
23913 {
23914 /* Complain, but do define it. */
4d3c2250 23915 dwarf2_macro_malformed_definition_complaint (body);
2e276125 23916 macro_define_function (file, line, name,
6e70227d 23917 argc, (const char **) argv,
2e276125
JB
23918 p);
23919 }
23920 else
23921 /* Just complain. */
4d3c2250 23922 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23923 }
23924 else
23925 /* Just complain. */
4d3c2250 23926 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23927
23928 xfree (name);
23929 {
23930 int i;
23931
23932 for (i = 0; i < argc; i++)
23933 xfree (argv[i]);
23934 }
23935 xfree (argv);
23936 }
23937 else
4d3c2250 23938 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23939}
23940
cf2c3c16
TT
23941/* Skip some bytes from BYTES according to the form given in FORM.
23942 Returns the new pointer. */
2e276125 23943
d521ce57
TT
23944static const gdb_byte *
23945skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
23946 enum dwarf_form form,
23947 unsigned int offset_size,
23948 struct dwarf2_section_info *section)
2e276125 23949{
cf2c3c16 23950 unsigned int bytes_read;
2e276125 23951
cf2c3c16 23952 switch (form)
2e276125 23953 {
cf2c3c16
TT
23954 case DW_FORM_data1:
23955 case DW_FORM_flag:
23956 ++bytes;
23957 break;
23958
23959 case DW_FORM_data2:
23960 bytes += 2;
23961 break;
23962
23963 case DW_FORM_data4:
23964 bytes += 4;
23965 break;
23966
23967 case DW_FORM_data8:
23968 bytes += 8;
23969 break;
23970
0224619f
JK
23971 case DW_FORM_data16:
23972 bytes += 16;
23973 break;
23974
cf2c3c16
TT
23975 case DW_FORM_string:
23976 read_direct_string (abfd, bytes, &bytes_read);
23977 bytes += bytes_read;
23978 break;
23979
23980 case DW_FORM_sec_offset:
23981 case DW_FORM_strp:
36586728 23982 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
23983 bytes += offset_size;
23984 break;
23985
23986 case DW_FORM_block:
23987 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23988 bytes += bytes_read;
23989 break;
23990
23991 case DW_FORM_block1:
23992 bytes += 1 + read_1_byte (abfd, bytes);
23993 break;
23994 case DW_FORM_block2:
23995 bytes += 2 + read_2_bytes (abfd, bytes);
23996 break;
23997 case DW_FORM_block4:
23998 bytes += 4 + read_4_bytes (abfd, bytes);
23999 break;
24000
24001 case DW_FORM_sdata:
24002 case DW_FORM_udata:
3019eac3
DE
24003 case DW_FORM_GNU_addr_index:
24004 case DW_FORM_GNU_str_index:
d521ce57 24005 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24006 if (bytes == NULL)
24007 {
24008 dwarf2_section_buffer_overflow_complaint (section);
24009 return NULL;
24010 }
cf2c3c16
TT
24011 break;
24012
663c44ac
JK
24013 case DW_FORM_implicit_const:
24014 break;
24015
cf2c3c16
TT
24016 default:
24017 {
cf2c3c16
TT
24018 complaint (&symfile_complaints,
24019 _("invalid form 0x%x in `%s'"),
a32a8923 24020 form, get_section_name (section));
cf2c3c16
TT
24021 return NULL;
24022 }
2e276125
JB
24023 }
24024
cf2c3c16
TT
24025 return bytes;
24026}
757a13d0 24027
cf2c3c16
TT
24028/* A helper for dwarf_decode_macros that handles skipping an unknown
24029 opcode. Returns an updated pointer to the macro data buffer; or,
24030 on error, issues a complaint and returns NULL. */
757a13d0 24031
d521ce57 24032static const gdb_byte *
cf2c3c16 24033skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24034 const gdb_byte **opcode_definitions,
24035 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24036 bfd *abfd,
24037 unsigned int offset_size,
24038 struct dwarf2_section_info *section)
24039{
24040 unsigned int bytes_read, i;
24041 unsigned long arg;
d521ce57 24042 const gdb_byte *defn;
2e276125 24043
cf2c3c16 24044 if (opcode_definitions[opcode] == NULL)
2e276125 24045 {
cf2c3c16
TT
24046 complaint (&symfile_complaints,
24047 _("unrecognized DW_MACFINO opcode 0x%x"),
24048 opcode);
24049 return NULL;
24050 }
2e276125 24051
cf2c3c16
TT
24052 defn = opcode_definitions[opcode];
24053 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24054 defn += bytes_read;
2e276125 24055
cf2c3c16
TT
24056 for (i = 0; i < arg; ++i)
24057 {
aead7601
SM
24058 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24059 (enum dwarf_form) defn[i], offset_size,
f664829e 24060 section);
cf2c3c16
TT
24061 if (mac_ptr == NULL)
24062 {
24063 /* skip_form_bytes already issued the complaint. */
24064 return NULL;
24065 }
24066 }
757a13d0 24067
cf2c3c16
TT
24068 return mac_ptr;
24069}
757a13d0 24070
cf2c3c16
TT
24071/* A helper function which parses the header of a macro section.
24072 If the macro section is the extended (for now called "GNU") type,
24073 then this updates *OFFSET_SIZE. Returns a pointer to just after
24074 the header, or issues a complaint and returns NULL on error. */
757a13d0 24075
d521ce57
TT
24076static const gdb_byte *
24077dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24078 bfd *abfd,
d521ce57 24079 const gdb_byte *mac_ptr,
cf2c3c16
TT
24080 unsigned int *offset_size,
24081 int section_is_gnu)
24082{
24083 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24084
cf2c3c16
TT
24085 if (section_is_gnu)
24086 {
24087 unsigned int version, flags;
757a13d0 24088
cf2c3c16 24089 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24090 if (version != 4 && version != 5)
cf2c3c16
TT
24091 {
24092 complaint (&symfile_complaints,
24093 _("unrecognized version `%d' in .debug_macro section"),
24094 version);
24095 return NULL;
24096 }
24097 mac_ptr += 2;
757a13d0 24098
cf2c3c16
TT
24099 flags = read_1_byte (abfd, mac_ptr);
24100 ++mac_ptr;
24101 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24102
cf2c3c16
TT
24103 if ((flags & 2) != 0)
24104 /* We don't need the line table offset. */
24105 mac_ptr += *offset_size;
757a13d0 24106
cf2c3c16
TT
24107 /* Vendor opcode descriptions. */
24108 if ((flags & 4) != 0)
24109 {
24110 unsigned int i, count;
757a13d0 24111
cf2c3c16
TT
24112 count = read_1_byte (abfd, mac_ptr);
24113 ++mac_ptr;
24114 for (i = 0; i < count; ++i)
24115 {
24116 unsigned int opcode, bytes_read;
24117 unsigned long arg;
24118
24119 opcode = read_1_byte (abfd, mac_ptr);
24120 ++mac_ptr;
24121 opcode_definitions[opcode] = mac_ptr;
24122 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24123 mac_ptr += bytes_read;
24124 mac_ptr += arg;
24125 }
757a13d0 24126 }
cf2c3c16 24127 }
757a13d0 24128
cf2c3c16
TT
24129 return mac_ptr;
24130}
757a13d0 24131
cf2c3c16 24132/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24133 including DW_MACRO_import. */
cf2c3c16
TT
24134
24135static void
d521ce57
TT
24136dwarf_decode_macro_bytes (bfd *abfd,
24137 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24138 struct macro_source_file *current_file,
43f3e411 24139 struct line_header *lh,
cf2c3c16 24140 struct dwarf2_section_info *section,
36586728 24141 int section_is_gnu, int section_is_dwz,
cf2c3c16 24142 unsigned int offset_size,
8fc3fc34 24143 htab_t include_hash)
cf2c3c16 24144{
4d663531 24145 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24146 enum dwarf_macro_record_type macinfo_type;
24147 int at_commandline;
d521ce57 24148 const gdb_byte *opcode_definitions[256];
757a13d0 24149
cf2c3c16
TT
24150 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24151 &offset_size, section_is_gnu);
24152 if (mac_ptr == NULL)
24153 {
24154 /* We already issued a complaint. */
24155 return;
24156 }
757a13d0
JK
24157
24158 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24159 GDB is still reading the definitions from command line. First
24160 DW_MACINFO_start_file will need to be ignored as it was already executed
24161 to create CURRENT_FILE for the main source holding also the command line
24162 definitions. On first met DW_MACINFO_start_file this flag is reset to
24163 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24164
24165 at_commandline = 1;
24166
24167 do
24168 {
24169 /* Do we at least have room for a macinfo type byte? */
24170 if (mac_ptr >= mac_end)
24171 {
f664829e 24172 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24173 break;
24174 }
24175
aead7601 24176 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24177 mac_ptr++;
24178
cf2c3c16
TT
24179 /* Note that we rely on the fact that the corresponding GNU and
24180 DWARF constants are the same. */
757a13d0
JK
24181 switch (macinfo_type)
24182 {
24183 /* A zero macinfo type indicates the end of the macro
24184 information. */
24185 case 0:
24186 break;
2e276125 24187
0af92d60
JK
24188 case DW_MACRO_define:
24189 case DW_MACRO_undef:
24190 case DW_MACRO_define_strp:
24191 case DW_MACRO_undef_strp:
24192 case DW_MACRO_define_sup:
24193 case DW_MACRO_undef_sup:
2e276125 24194 {
891d2f0b 24195 unsigned int bytes_read;
2e276125 24196 int line;
d521ce57 24197 const char *body;
cf2c3c16 24198 int is_define;
2e276125 24199
cf2c3c16
TT
24200 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24201 mac_ptr += bytes_read;
24202
0af92d60
JK
24203 if (macinfo_type == DW_MACRO_define
24204 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24205 {
24206 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24207 mac_ptr += bytes_read;
24208 }
24209 else
24210 {
24211 LONGEST str_offset;
24212
24213 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24214 mac_ptr += offset_size;
2e276125 24215
0af92d60
JK
24216 if (macinfo_type == DW_MACRO_define_sup
24217 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24218 || section_is_dwz)
36586728
TT
24219 {
24220 struct dwz_file *dwz = dwarf2_get_dwz_file ();
24221
24222 body = read_indirect_string_from_dwz (dwz, str_offset);
24223 }
24224 else
24225 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
24226 }
24227
0af92d60
JK
24228 is_define = (macinfo_type == DW_MACRO_define
24229 || macinfo_type == DW_MACRO_define_strp
24230 || macinfo_type == DW_MACRO_define_sup);
2e276125 24231 if (! current_file)
757a13d0
JK
24232 {
24233 /* DWARF violation as no main source is present. */
24234 complaint (&symfile_complaints,
24235 _("debug info with no main source gives macro %s "
24236 "on line %d: %s"),
cf2c3c16
TT
24237 is_define ? _("definition") : _("undefinition"),
24238 line, body);
757a13d0
JK
24239 break;
24240 }
3e43a32a
MS
24241 if ((line == 0 && !at_commandline)
24242 || (line != 0 && at_commandline))
4d3c2250 24243 complaint (&symfile_complaints,
757a13d0
JK
24244 _("debug info gives %s macro %s with %s line %d: %s"),
24245 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24246 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24247 line == 0 ? _("zero") : _("non-zero"), line, body);
24248
cf2c3c16 24249 if (is_define)
757a13d0 24250 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24251 else
24252 {
0af92d60
JK
24253 gdb_assert (macinfo_type == DW_MACRO_undef
24254 || macinfo_type == DW_MACRO_undef_strp
24255 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24256 macro_undef (current_file, line, body);
24257 }
2e276125
JB
24258 }
24259 break;
24260
0af92d60 24261 case DW_MACRO_start_file:
2e276125 24262 {
891d2f0b 24263 unsigned int bytes_read;
2e276125
JB
24264 int line, file;
24265
24266 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24267 mac_ptr += bytes_read;
24268 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24269 mac_ptr += bytes_read;
24270
3e43a32a
MS
24271 if ((line == 0 && !at_commandline)
24272 || (line != 0 && at_commandline))
757a13d0
JK
24273 complaint (&symfile_complaints,
24274 _("debug info gives source %d included "
24275 "from %s at %s line %d"),
24276 file, at_commandline ? _("command-line") : _("file"),
24277 line == 0 ? _("zero") : _("non-zero"), line);
24278
24279 if (at_commandline)
24280 {
0af92d60 24281 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24282 pass one. */
757a13d0
JK
24283 at_commandline = 0;
24284 }
24285 else
43f3e411 24286 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
24287 }
24288 break;
24289
0af92d60 24290 case DW_MACRO_end_file:
2e276125 24291 if (! current_file)
4d3c2250 24292 complaint (&symfile_complaints,
3e43a32a
MS
24293 _("macro debug info has an unmatched "
24294 "`close_file' directive"));
2e276125
JB
24295 else
24296 {
24297 current_file = current_file->included_by;
24298 if (! current_file)
24299 {
cf2c3c16 24300 enum dwarf_macro_record_type next_type;
2e276125
JB
24301
24302 /* GCC circa March 2002 doesn't produce the zero
24303 type byte marking the end of the compilation
24304 unit. Complain if it's not there, but exit no
24305 matter what. */
24306
24307 /* Do we at least have room for a macinfo type byte? */
24308 if (mac_ptr >= mac_end)
24309 {
f664829e 24310 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24311 return;
24312 }
24313
24314 /* We don't increment mac_ptr here, so this is just
24315 a look-ahead. */
aead7601
SM
24316 next_type
24317 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24318 mac_ptr);
2e276125 24319 if (next_type != 0)
4d3c2250 24320 complaint (&symfile_complaints,
3e43a32a
MS
24321 _("no terminating 0-type entry for "
24322 "macros in `.debug_macinfo' section"));
2e276125
JB
24323
24324 return;
24325 }
24326 }
24327 break;
24328
0af92d60
JK
24329 case DW_MACRO_import:
24330 case DW_MACRO_import_sup:
cf2c3c16
TT
24331 {
24332 LONGEST offset;
8fc3fc34 24333 void **slot;
a036ba48
TT
24334 bfd *include_bfd = abfd;
24335 struct dwarf2_section_info *include_section = section;
d521ce57 24336 const gdb_byte *include_mac_end = mac_end;
a036ba48 24337 int is_dwz = section_is_dwz;
d521ce57 24338 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24339
24340 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24341 mac_ptr += offset_size;
24342
0af92d60 24343 if (macinfo_type == DW_MACRO_import_sup)
a036ba48
TT
24344 {
24345 struct dwz_file *dwz = dwarf2_get_dwz_file ();
24346
4d663531 24347 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24348
a036ba48 24349 include_section = &dwz->macro;
a32a8923 24350 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24351 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24352 is_dwz = 1;
24353 }
24354
24355 new_mac_ptr = include_section->buffer + offset;
24356 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24357
8fc3fc34
TT
24358 if (*slot != NULL)
24359 {
24360 /* This has actually happened; see
24361 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24362 complaint (&symfile_complaints,
0af92d60 24363 _("recursive DW_MACRO_import in "
8fc3fc34
TT
24364 ".debug_macro section"));
24365 }
24366 else
24367 {
d521ce57 24368 *slot = (void *) new_mac_ptr;
36586728 24369
a036ba48 24370 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 24371 include_mac_end, current_file, lh,
36586728 24372 section, section_is_gnu, is_dwz,
4d663531 24373 offset_size, include_hash);
8fc3fc34 24374
d521ce57 24375 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24376 }
cf2c3c16
TT
24377 }
24378 break;
24379
2e276125 24380 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24381 if (!section_is_gnu)
24382 {
24383 unsigned int bytes_read;
2e276125 24384
ac298888
TT
24385 /* This reads the constant, but since we don't recognize
24386 any vendor extensions, we ignore it. */
24387 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24388 mac_ptr += bytes_read;
24389 read_direct_string (abfd, mac_ptr, &bytes_read);
24390 mac_ptr += bytes_read;
2e276125 24391
cf2c3c16
TT
24392 /* We don't recognize any vendor extensions. */
24393 break;
24394 }
24395 /* FALLTHROUGH */
24396
24397 default:
24398 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24399 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24400 section);
24401 if (mac_ptr == NULL)
24402 return;
24403 break;
2e276125 24404 }
757a13d0 24405 } while (macinfo_type != 0);
2e276125 24406}
8e19ed76 24407
cf2c3c16 24408static void
09262596 24409dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24410 int section_is_gnu)
cf2c3c16 24411{
bb5ed363 24412 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24413 struct line_header *lh = cu->line_header;
24414 bfd *abfd;
d521ce57 24415 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24416 struct macro_source_file *current_file = 0;
24417 enum dwarf_macro_record_type macinfo_type;
24418 unsigned int offset_size = cu->header.offset_size;
d521ce57 24419 const gdb_byte *opcode_definitions[256];
8fc3fc34 24420 void **slot;
09262596
DE
24421 struct dwarf2_section_info *section;
24422 const char *section_name;
24423
24424 if (cu->dwo_unit != NULL)
24425 {
24426 if (section_is_gnu)
24427 {
24428 section = &cu->dwo_unit->dwo_file->sections.macro;
24429 section_name = ".debug_macro.dwo";
24430 }
24431 else
24432 {
24433 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24434 section_name = ".debug_macinfo.dwo";
24435 }
24436 }
24437 else
24438 {
24439 if (section_is_gnu)
24440 {
24441 section = &dwarf2_per_objfile->macro;
24442 section_name = ".debug_macro";
24443 }
24444 else
24445 {
24446 section = &dwarf2_per_objfile->macinfo;
24447 section_name = ".debug_macinfo";
24448 }
24449 }
cf2c3c16 24450
bb5ed363 24451 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24452 if (section->buffer == NULL)
24453 {
fceca515 24454 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
24455 return;
24456 }
a32a8923 24457 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24458
24459 /* First pass: Find the name of the base filename.
24460 This filename is needed in order to process all macros whose definition
24461 (or undefinition) comes from the command line. These macros are defined
24462 before the first DW_MACINFO_start_file entry, and yet still need to be
24463 associated to the base file.
24464
24465 To determine the base file name, we scan the macro definitions until we
24466 reach the first DW_MACINFO_start_file entry. We then initialize
24467 CURRENT_FILE accordingly so that any macro definition found before the
24468 first DW_MACINFO_start_file can still be associated to the base file. */
24469
24470 mac_ptr = section->buffer + offset;
24471 mac_end = section->buffer + section->size;
24472
24473 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24474 &offset_size, section_is_gnu);
24475 if (mac_ptr == NULL)
24476 {
24477 /* We already issued a complaint. */
24478 return;
24479 }
24480
24481 do
24482 {
24483 /* Do we at least have room for a macinfo type byte? */
24484 if (mac_ptr >= mac_end)
24485 {
24486 /* Complaint is printed during the second pass as GDB will probably
24487 stop the first pass earlier upon finding
24488 DW_MACINFO_start_file. */
24489 break;
24490 }
24491
aead7601 24492 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24493 mac_ptr++;
24494
24495 /* Note that we rely on the fact that the corresponding GNU and
24496 DWARF constants are the same. */
24497 switch (macinfo_type)
24498 {
24499 /* A zero macinfo type indicates the end of the macro
24500 information. */
24501 case 0:
24502 break;
24503
0af92d60
JK
24504 case DW_MACRO_define:
24505 case DW_MACRO_undef:
cf2c3c16
TT
24506 /* Only skip the data by MAC_PTR. */
24507 {
24508 unsigned int bytes_read;
24509
24510 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24511 mac_ptr += bytes_read;
24512 read_direct_string (abfd, mac_ptr, &bytes_read);
24513 mac_ptr += bytes_read;
24514 }
24515 break;
24516
0af92d60 24517 case DW_MACRO_start_file:
cf2c3c16
TT
24518 {
24519 unsigned int bytes_read;
24520 int line, file;
24521
24522 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24523 mac_ptr += bytes_read;
24524 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24525 mac_ptr += bytes_read;
24526
43f3e411 24527 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
24528 }
24529 break;
24530
0af92d60 24531 case DW_MACRO_end_file:
cf2c3c16
TT
24532 /* No data to skip by MAC_PTR. */
24533 break;
24534
0af92d60
JK
24535 case DW_MACRO_define_strp:
24536 case DW_MACRO_undef_strp:
24537 case DW_MACRO_define_sup:
24538 case DW_MACRO_undef_sup:
cf2c3c16
TT
24539 {
24540 unsigned int bytes_read;
24541
24542 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24543 mac_ptr += bytes_read;
24544 mac_ptr += offset_size;
24545 }
24546 break;
24547
0af92d60
JK
24548 case DW_MACRO_import:
24549 case DW_MACRO_import_sup:
cf2c3c16 24550 /* Note that, according to the spec, a transparent include
0af92d60 24551 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
24552 skip this opcode. */
24553 mac_ptr += offset_size;
24554 break;
24555
24556 case DW_MACINFO_vendor_ext:
24557 /* Only skip the data by MAC_PTR. */
24558 if (!section_is_gnu)
24559 {
24560 unsigned int bytes_read;
24561
24562 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24563 mac_ptr += bytes_read;
24564 read_direct_string (abfd, mac_ptr, &bytes_read);
24565 mac_ptr += bytes_read;
24566 }
24567 /* FALLTHROUGH */
24568
24569 default:
24570 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24571 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24572 section);
24573 if (mac_ptr == NULL)
24574 return;
24575 break;
24576 }
24577 } while (macinfo_type != 0 && current_file == NULL);
24578
24579 /* Second pass: Process all entries.
24580
24581 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24582 command-line macro definitions/undefinitions. This flag is unset when we
24583 reach the first DW_MACINFO_start_file entry. */
24584
fc4007c9
TT
24585 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24586 htab_eq_pointer,
24587 NULL, xcalloc, xfree));
8fc3fc34 24588 mac_ptr = section->buffer + offset;
fc4007c9 24589 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 24590 *slot = (void *) mac_ptr;
8fc3fc34 24591 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 24592 current_file, lh, section,
fc4007c9
TT
24593 section_is_gnu, 0, offset_size,
24594 include_hash.get ());
cf2c3c16
TT
24595}
24596
8e19ed76 24597/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 24598 if so return true else false. */
380bca97 24599
8e19ed76 24600static int
6e5a29e1 24601attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
24602{
24603 return (attr == NULL ? 0 :
24604 attr->form == DW_FORM_block1
24605 || attr->form == DW_FORM_block2
24606 || attr->form == DW_FORM_block4
2dc7f7b3
TT
24607 || attr->form == DW_FORM_block
24608 || attr->form == DW_FORM_exprloc);
8e19ed76 24609}
4c2df51b 24610
c6a0999f
JB
24611/* Return non-zero if ATTR's value is a section offset --- classes
24612 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24613 You may use DW_UNSND (attr) to retrieve such offsets.
24614
24615 Section 7.5.4, "Attribute Encodings", explains that no attribute
24616 may have a value that belongs to more than one of these classes; it
24617 would be ambiguous if we did, because we use the same forms for all
24618 of them. */
380bca97 24619
3690dd37 24620static int
6e5a29e1 24621attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
24622{
24623 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
24624 || attr->form == DW_FORM_data8
24625 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
24626}
24627
3690dd37
JB
24628/* Return non-zero if ATTR's value falls in the 'constant' class, or
24629 zero otherwise. When this function returns true, you can apply
24630 dwarf2_get_attr_constant_value to it.
24631
24632 However, note that for some attributes you must check
24633 attr_form_is_section_offset before using this test. DW_FORM_data4
24634 and DW_FORM_data8 are members of both the constant class, and of
24635 the classes that contain offsets into other debug sections
24636 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24637 that, if an attribute's can be either a constant or one of the
24638 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
24639 taken as section offsets, not constants.
24640
24641 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24642 cannot handle that. */
380bca97 24643
3690dd37 24644static int
6e5a29e1 24645attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
24646{
24647 switch (attr->form)
24648 {
24649 case DW_FORM_sdata:
24650 case DW_FORM_udata:
24651 case DW_FORM_data1:
24652 case DW_FORM_data2:
24653 case DW_FORM_data4:
24654 case DW_FORM_data8:
663c44ac 24655 case DW_FORM_implicit_const:
3690dd37
JB
24656 return 1;
24657 default:
24658 return 0;
24659 }
24660}
24661
7771576e
SA
24662
24663/* DW_ADDR is always stored already as sect_offset; despite for the forms
24664 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24665
24666static int
6e5a29e1 24667attr_form_is_ref (const struct attribute *attr)
7771576e
SA
24668{
24669 switch (attr->form)
24670 {
24671 case DW_FORM_ref_addr:
24672 case DW_FORM_ref1:
24673 case DW_FORM_ref2:
24674 case DW_FORM_ref4:
24675 case DW_FORM_ref8:
24676 case DW_FORM_ref_udata:
24677 case DW_FORM_GNU_ref_alt:
24678 return 1;
24679 default:
24680 return 0;
24681 }
24682}
24683
3019eac3
DE
24684/* Return the .debug_loc section to use for CU.
24685 For DWO files use .debug_loc.dwo. */
24686
24687static struct dwarf2_section_info *
24688cu_debug_loc_section (struct dwarf2_cu *cu)
24689{
24690 if (cu->dwo_unit)
43988095
JK
24691 {
24692 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24693
24694 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24695 }
24696 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24697 : &dwarf2_per_objfile->loc);
3019eac3
DE
24698}
24699
8cf6f0b1
TT
24700/* A helper function that fills in a dwarf2_loclist_baton. */
24701
24702static void
24703fill_in_loclist_baton (struct dwarf2_cu *cu,
24704 struct dwarf2_loclist_baton *baton,
ff39bb5e 24705 const struct attribute *attr)
8cf6f0b1 24706{
3019eac3
DE
24707 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24708
24709 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
24710
24711 baton->per_cu = cu->per_cu;
24712 gdb_assert (baton->per_cu);
24713 /* We don't know how long the location list is, but make sure we
24714 don't run off the edge of the section. */
3019eac3
DE
24715 baton->size = section->size - DW_UNSND (attr);
24716 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 24717 baton->base_address = cu->base_address;
f664829e 24718 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24719}
24720
4c2df51b 24721static void
ff39bb5e 24722dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24723 struct dwarf2_cu *cu, int is_block)
4c2df51b 24724{
bb5ed363 24725 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 24726 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24727
3690dd37 24728 if (attr_form_is_section_offset (attr)
3019eac3 24729 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24730 the section. If so, fall through to the complaint in the
24731 other branch. */
3019eac3 24732 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 24733 {
0d53c4c4 24734 struct dwarf2_loclist_baton *baton;
4c2df51b 24735
8d749320 24736 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24737
8cf6f0b1 24738 fill_in_loclist_baton (cu, baton, attr);
be391dca 24739
d00adf39 24740 if (cu->base_known == 0)
0d53c4c4 24741 complaint (&symfile_complaints,
3e43a32a
MS
24742 _("Location list used without "
24743 "specifying the CU base address."));
4c2df51b 24744
f1e6e072
TT
24745 SYMBOL_ACLASS_INDEX (sym) = (is_block
24746 ? dwarf2_loclist_block_index
24747 : dwarf2_loclist_index);
0d53c4c4
DJ
24748 SYMBOL_LOCATION_BATON (sym) = baton;
24749 }
24750 else
24751 {
24752 struct dwarf2_locexpr_baton *baton;
24753
8d749320 24754 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
24755 baton->per_cu = cu->per_cu;
24756 gdb_assert (baton->per_cu);
0d53c4c4
DJ
24757
24758 if (attr_form_is_block (attr))
24759 {
24760 /* Note that we're just copying the block's data pointer
24761 here, not the actual data. We're still pointing into the
6502dd73
DJ
24762 info_buffer for SYM's objfile; right now we never release
24763 that buffer, but when we do clean up properly this may
24764 need to change. */
0d53c4c4
DJ
24765 baton->size = DW_BLOCK (attr)->size;
24766 baton->data = DW_BLOCK (attr)->data;
24767 }
24768 else
24769 {
24770 dwarf2_invalid_attrib_class_complaint ("location description",
24771 SYMBOL_NATURAL_NAME (sym));
24772 baton->size = 0;
0d53c4c4 24773 }
6e70227d 24774
f1e6e072
TT
24775 SYMBOL_ACLASS_INDEX (sym) = (is_block
24776 ? dwarf2_locexpr_block_index
24777 : dwarf2_locexpr_index);
0d53c4c4
DJ
24778 SYMBOL_LOCATION_BATON (sym) = baton;
24779 }
4c2df51b 24780}
6502dd73 24781
9aa1f1e3
TT
24782/* Return the OBJFILE associated with the compilation unit CU. If CU
24783 came from a separate debuginfo file, then the master objfile is
24784 returned. */
ae0d2f24
UW
24785
24786struct objfile *
24787dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24788{
9291a0cd 24789 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
24790
24791 /* Return the master objfile, so that we can report and look up the
24792 correct file containing this variable. */
24793 if (objfile->separate_debug_objfile_backlink)
24794 objfile = objfile->separate_debug_objfile_backlink;
24795
24796 return objfile;
24797}
24798
96408a79
SA
24799/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24800 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24801 CU_HEADERP first. */
24802
24803static const struct comp_unit_head *
24804per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24805 struct dwarf2_per_cu_data *per_cu)
24806{
d521ce57 24807 const gdb_byte *info_ptr;
96408a79
SA
24808
24809 if (per_cu->cu)
24810 return &per_cu->cu->header;
24811
9c541725 24812 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24813
24814 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24815 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24816 rcuh_kind::COMPILE);
96408a79
SA
24817
24818 return cu_headerp;
24819}
24820
ae0d2f24
UW
24821/* Return the address size given in the compilation unit header for CU. */
24822
98714339 24823int
ae0d2f24
UW
24824dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24825{
96408a79
SA
24826 struct comp_unit_head cu_header_local;
24827 const struct comp_unit_head *cu_headerp;
c471e790 24828
96408a79
SA
24829 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24830
24831 return cu_headerp->addr_size;
ae0d2f24
UW
24832}
24833
9eae7c52
TT
24834/* Return the offset size given in the compilation unit header for CU. */
24835
24836int
24837dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24838{
96408a79
SA
24839 struct comp_unit_head cu_header_local;
24840 const struct comp_unit_head *cu_headerp;
9c6c53f7 24841
96408a79
SA
24842 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24843
24844 return cu_headerp->offset_size;
24845}
24846
24847/* See its dwarf2loc.h declaration. */
24848
24849int
24850dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24851{
24852 struct comp_unit_head cu_header_local;
24853 const struct comp_unit_head *cu_headerp;
24854
24855 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24856
24857 if (cu_headerp->version == 2)
24858 return cu_headerp->addr_size;
24859 else
24860 return cu_headerp->offset_size;
181cebd4
JK
24861}
24862
9aa1f1e3
TT
24863/* Return the text offset of the CU. The returned offset comes from
24864 this CU's objfile. If this objfile came from a separate debuginfo
24865 file, then the offset may be different from the corresponding
24866 offset in the parent objfile. */
24867
24868CORE_ADDR
24869dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24870{
bb3fa9d0 24871 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
24872
24873 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24874}
24875
43988095
JK
24876/* Return DWARF version number of PER_CU. */
24877
24878short
24879dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24880{
24881 return per_cu->dwarf_version;
24882}
24883
348e048f
DE
24884/* Locate the .debug_info compilation unit from CU's objfile which contains
24885 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
24886
24887static struct dwarf2_per_cu_data *
9c541725 24888dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 24889 unsigned int offset_in_dwz,
ae038cb0
DJ
24890 struct objfile *objfile)
24891{
24892 struct dwarf2_per_cu_data *this_cu;
24893 int low, high;
36586728 24894 const sect_offset *cu_off;
ae038cb0 24895
ae038cb0
DJ
24896 low = 0;
24897 high = dwarf2_per_objfile->n_comp_units - 1;
24898 while (high > low)
24899 {
36586728 24900 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24901 int mid = low + (high - low) / 2;
9a619af0 24902
36586728 24903 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 24904 cu_off = &mid_cu->sect_off;
36586728 24905 if (mid_cu->is_dwz > offset_in_dwz
9c541725 24906 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
24907 high = mid;
24908 else
24909 low = mid + 1;
24910 }
24911 gdb_assert (low == high);
36586728 24912 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
24913 cu_off = &this_cu->sect_off;
24914 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 24915 {
36586728 24916 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24917 error (_("Dwarf Error: could not find partial DIE containing "
9c541725
PA
24918 "offset 0x%x [in module %s]"),
24919 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
10b3939b 24920
9c541725
PA
24921 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24922 <= sect_off);
ae038cb0
DJ
24923 return dwarf2_per_objfile->all_comp_units[low-1];
24924 }
24925 else
24926 {
24927 this_cu = dwarf2_per_objfile->all_comp_units[low];
24928 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725
PA
24929 && sect_off >= this_cu->sect_off + this_cu->length)
24930 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
24931 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24932 return this_cu;
24933 }
24934}
24935
23745b47 24936/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 24937
9816fde3 24938static void
23745b47 24939init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 24940{
9816fde3 24941 memset (cu, 0, sizeof (*cu));
23745b47
DE
24942 per_cu->cu = cu;
24943 cu->per_cu = per_cu;
24944 cu->objfile = per_cu->objfile;
93311388 24945 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
24946}
24947
24948/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24949
24950static void
95554aad
TT
24951prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24952 enum language pretend_language)
9816fde3
JK
24953{
24954 struct attribute *attr;
24955
24956 /* Set the language we're debugging. */
24957 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24958 if (attr)
24959 set_cu_language (DW_UNSND (attr), cu);
24960 else
9cded63f 24961 {
95554aad 24962 cu->language = pretend_language;
9cded63f
TT
24963 cu->language_defn = language_def (cu->language);
24964 }
dee91e82 24965
7d45c7c3 24966 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24967}
24968
ae038cb0
DJ
24969/* Release one cached compilation unit, CU. We unlink it from the tree
24970 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
24971 the caller is responsible for that.
24972 NOTE: DATA is a void * because this function is also used as a
24973 cleanup routine. */
ae038cb0
DJ
24974
24975static void
68dc6402 24976free_heap_comp_unit (void *data)
ae038cb0 24977{
9a3c8263 24978 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 24979
23745b47
DE
24980 gdb_assert (cu->per_cu != NULL);
24981 cu->per_cu->cu = NULL;
ae038cb0
DJ
24982 cu->per_cu = NULL;
24983
24984 obstack_free (&cu->comp_unit_obstack, NULL);
24985
24986 xfree (cu);
24987}
24988
72bf9492 24989/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 24990 when we're finished with it. We can't free the pointer itself, but be
dee91e82 24991 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
24992
24993static void
24994free_stack_comp_unit (void *data)
24995{
9a3c8263 24996 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 24997
23745b47
DE
24998 gdb_assert (cu->per_cu != NULL);
24999 cu->per_cu->cu = NULL;
25000 cu->per_cu = NULL;
25001
72bf9492
DJ
25002 obstack_free (&cu->comp_unit_obstack, NULL);
25003 cu->partial_dies = NULL;
ae038cb0
DJ
25004}
25005
25006/* Free all cached compilation units. */
25007
25008static void
25009free_cached_comp_units (void *data)
25010{
330cdd98 25011 dwarf2_per_objfile->free_cached_comp_units ();
ae038cb0
DJ
25012}
25013
25014/* Increase the age counter on each cached compilation unit, and free
25015 any that are too old. */
25016
25017static void
25018age_cached_comp_units (void)
25019{
25020 struct dwarf2_per_cu_data *per_cu, **last_chain;
25021
25022 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25023 per_cu = dwarf2_per_objfile->read_in_chain;
25024 while (per_cu != NULL)
25025 {
25026 per_cu->cu->last_used ++;
b4f54984 25027 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25028 dwarf2_mark (per_cu->cu);
25029 per_cu = per_cu->cu->read_in_chain;
25030 }
25031
25032 per_cu = dwarf2_per_objfile->read_in_chain;
25033 last_chain = &dwarf2_per_objfile->read_in_chain;
25034 while (per_cu != NULL)
25035 {
25036 struct dwarf2_per_cu_data *next_cu;
25037
25038 next_cu = per_cu->cu->read_in_chain;
25039
25040 if (!per_cu->cu->mark)
25041 {
68dc6402 25042 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
25043 *last_chain = next_cu;
25044 }
25045 else
25046 last_chain = &per_cu->cu->read_in_chain;
25047
25048 per_cu = next_cu;
25049 }
25050}
25051
25052/* Remove a single compilation unit from the cache. */
25053
25054static void
dee91e82 25055free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25056{
25057 struct dwarf2_per_cu_data *per_cu, **last_chain;
25058
25059 per_cu = dwarf2_per_objfile->read_in_chain;
25060 last_chain = &dwarf2_per_objfile->read_in_chain;
25061 while (per_cu != NULL)
25062 {
25063 struct dwarf2_per_cu_data *next_cu;
25064
25065 next_cu = per_cu->cu->read_in_chain;
25066
dee91e82 25067 if (per_cu == target_per_cu)
ae038cb0 25068 {
68dc6402 25069 free_heap_comp_unit (per_cu->cu);
dee91e82 25070 per_cu->cu = NULL;
ae038cb0
DJ
25071 *last_chain = next_cu;
25072 break;
25073 }
25074 else
25075 last_chain = &per_cu->cu->read_in_chain;
25076
25077 per_cu = next_cu;
25078 }
25079}
25080
fe3e1990
DJ
25081/* Release all extra memory associated with OBJFILE. */
25082
25083void
25084dwarf2_free_objfile (struct objfile *objfile)
25085{
9a3c8263
SM
25086 dwarf2_per_objfile
25087 = (struct dwarf2_per_objfile *) objfile_data (objfile,
25088 dwarf2_objfile_data_key);
fe3e1990
DJ
25089
25090 if (dwarf2_per_objfile == NULL)
25091 return;
25092
330cdd98 25093 dwarf2_per_objfile->~dwarf2_per_objfile ();
fe3e1990
DJ
25094}
25095
dee91e82
DE
25096/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25097 We store these in a hash table separate from the DIEs, and preserve them
25098 when the DIEs are flushed out of cache.
25099
25100 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25101 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25102 or the type may come from a DWO file. Furthermore, while it's more logical
25103 to use per_cu->section+offset, with Fission the section with the data is in
25104 the DWO file but we don't know that section at the point we need it.
25105 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25106 because we can enter the lookup routine, get_die_type_at_offset, from
25107 outside this file, and thus won't necessarily have PER_CU->cu.
25108 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25109
dee91e82 25110struct dwarf2_per_cu_offset_and_type
1c379e20 25111{
dee91e82 25112 const struct dwarf2_per_cu_data *per_cu;
9c541725 25113 sect_offset sect_off;
1c379e20
DJ
25114 struct type *type;
25115};
25116
dee91e82 25117/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25118
25119static hashval_t
dee91e82 25120per_cu_offset_and_type_hash (const void *item)
1c379e20 25121{
9a3c8263
SM
25122 const struct dwarf2_per_cu_offset_and_type *ofs
25123 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25124
9c541725 25125 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25126}
25127
dee91e82 25128/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25129
25130static int
dee91e82 25131per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25132{
9a3c8263
SM
25133 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25134 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25135 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25136 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25137
dee91e82 25138 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25139 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25140}
25141
25142/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25143 table if necessary. For convenience, return TYPE.
25144
25145 The DIEs reading must have careful ordering to:
25146 * Not cause infite loops trying to read in DIEs as a prerequisite for
25147 reading current DIE.
25148 * Not trying to dereference contents of still incompletely read in types
25149 while reading in other DIEs.
25150 * Enable referencing still incompletely read in types just by a pointer to
25151 the type without accessing its fields.
25152
25153 Therefore caller should follow these rules:
25154 * Try to fetch any prerequisite types we may need to build this DIE type
25155 before building the type and calling set_die_type.
e71ec853 25156 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25157 possible before fetching more types to complete the current type.
25158 * Make the type as complete as possible before fetching more types. */
1c379e20 25159
f792889a 25160static struct type *
1c379e20
DJ
25161set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25162{
dee91e82 25163 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 25164 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
25165 struct attribute *attr;
25166 struct dynamic_prop prop;
1c379e20 25167
b4ba55a1
JB
25168 /* For Ada types, make sure that the gnat-specific data is always
25169 initialized (if not already set). There are a few types where
25170 we should not be doing so, because the type-specific area is
25171 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25172 where the type-specific area is used to store the floatformat).
25173 But this is not a problem, because the gnat-specific information
25174 is actually not needed for these types. */
25175 if (need_gnat_info (cu)
25176 && TYPE_CODE (type) != TYPE_CODE_FUNC
25177 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25178 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25179 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25180 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25181 && !HAVE_GNAT_AUX_INFO (type))
25182 INIT_GNAT_SPECIFIC (type);
25183
3f2f83dd
KB
25184 /* Read DW_AT_allocated and set in type. */
25185 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25186 if (attr_form_is_block (attr))
25187 {
25188 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25189 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
25190 }
25191 else if (attr != NULL)
25192 {
25193 complaint (&symfile_complaints,
9c541725
PA
25194 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
25195 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25196 to_underlying (die->sect_off));
3f2f83dd
KB
25197 }
25198
25199 /* Read DW_AT_associated and set in type. */
25200 attr = dwarf2_attr (die, DW_AT_associated, cu);
25201 if (attr_form_is_block (attr))
25202 {
25203 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25204 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
25205 }
25206 else if (attr != NULL)
25207 {
25208 complaint (&symfile_complaints,
9c541725
PA
25209 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
25210 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25211 to_underlying (die->sect_off));
3f2f83dd
KB
25212 }
25213
3cdcd0ce
JB
25214 /* Read DW_AT_data_location and set in type. */
25215 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25216 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 25217 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 25218
dee91e82 25219 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25220 {
dee91e82
DE
25221 dwarf2_per_objfile->die_type_hash =
25222 htab_create_alloc_ex (127,
25223 per_cu_offset_and_type_hash,
25224 per_cu_offset_and_type_eq,
25225 NULL,
25226 &objfile->objfile_obstack,
25227 hashtab_obstack_allocate,
25228 dummy_obstack_deallocate);
f792889a 25229 }
1c379e20 25230
dee91e82 25231 ofs.per_cu = cu->per_cu;
9c541725 25232 ofs.sect_off = die->sect_off;
1c379e20 25233 ofs.type = type;
dee91e82
DE
25234 slot = (struct dwarf2_per_cu_offset_and_type **)
25235 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
25236 if (*slot)
25237 complaint (&symfile_complaints,
25238 _("A problem internal to GDB: DIE 0x%x has type already set"),
9c541725 25239 to_underlying (die->sect_off));
8d749320
SM
25240 *slot = XOBNEW (&objfile->objfile_obstack,
25241 struct dwarf2_per_cu_offset_and_type);
1c379e20 25242 **slot = ofs;
f792889a 25243 return type;
1c379e20
DJ
25244}
25245
9c541725 25246/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25247 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25248
25249static struct type *
9c541725 25250get_die_type_at_offset (sect_offset sect_off,
673bfd45 25251 struct dwarf2_per_cu_data *per_cu)
1c379e20 25252{
dee91e82 25253 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 25254
dee91e82 25255 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25256 return NULL;
1c379e20 25257
dee91e82 25258 ofs.per_cu = per_cu;
9c541725 25259 ofs.sect_off = sect_off;
9a3c8263
SM
25260 slot = ((struct dwarf2_per_cu_offset_and_type *)
25261 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25262 if (slot)
25263 return slot->type;
25264 else
25265 return NULL;
25266}
25267
02142a6c 25268/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25269 or return NULL if DIE does not have a saved type. */
25270
25271static struct type *
25272get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25273{
9c541725 25274 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25275}
25276
10b3939b
DJ
25277/* Add a dependence relationship from CU to REF_PER_CU. */
25278
25279static void
25280dwarf2_add_dependence (struct dwarf2_cu *cu,
25281 struct dwarf2_per_cu_data *ref_per_cu)
25282{
25283 void **slot;
25284
25285 if (cu->dependencies == NULL)
25286 cu->dependencies
25287 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25288 NULL, &cu->comp_unit_obstack,
25289 hashtab_obstack_allocate,
25290 dummy_obstack_deallocate);
25291
25292 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25293 if (*slot == NULL)
25294 *slot = ref_per_cu;
25295}
1c379e20 25296
f504f079
DE
25297/* Subroutine of dwarf2_mark to pass to htab_traverse.
25298 Set the mark field in every compilation unit in the
ae038cb0
DJ
25299 cache that we must keep because we are keeping CU. */
25300
10b3939b
DJ
25301static int
25302dwarf2_mark_helper (void **slot, void *data)
25303{
25304 struct dwarf2_per_cu_data *per_cu;
25305
25306 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25307
25308 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25309 reading of the chain. As such dependencies remain valid it is not much
25310 useful to track and undo them during QUIT cleanups. */
25311 if (per_cu->cu == NULL)
25312 return 1;
25313
10b3939b
DJ
25314 if (per_cu->cu->mark)
25315 return 1;
25316 per_cu->cu->mark = 1;
25317
25318 if (per_cu->cu->dependencies != NULL)
25319 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25320
25321 return 1;
25322}
25323
f504f079
DE
25324/* Set the mark field in CU and in every other compilation unit in the
25325 cache that we must keep because we are keeping CU. */
25326
ae038cb0
DJ
25327static void
25328dwarf2_mark (struct dwarf2_cu *cu)
25329{
25330 if (cu->mark)
25331 return;
25332 cu->mark = 1;
10b3939b
DJ
25333 if (cu->dependencies != NULL)
25334 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25335}
25336
25337static void
25338dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25339{
25340 while (per_cu)
25341 {
25342 per_cu->cu->mark = 0;
25343 per_cu = per_cu->cu->read_in_chain;
25344 }
72bf9492
DJ
25345}
25346
72bf9492
DJ
25347/* Trivial hash function for partial_die_info: the hash value of a DIE
25348 is its offset in .debug_info for this objfile. */
25349
25350static hashval_t
25351partial_die_hash (const void *item)
25352{
9a3c8263
SM
25353 const struct partial_die_info *part_die
25354 = (const struct partial_die_info *) item;
9a619af0 25355
9c541725 25356 return to_underlying (part_die->sect_off);
72bf9492
DJ
25357}
25358
25359/* Trivial comparison function for partial_die_info structures: two DIEs
25360 are equal if they have the same offset. */
25361
25362static int
25363partial_die_eq (const void *item_lhs, const void *item_rhs)
25364{
9a3c8263
SM
25365 const struct partial_die_info *part_die_lhs
25366 = (const struct partial_die_info *) item_lhs;
25367 const struct partial_die_info *part_die_rhs
25368 = (const struct partial_die_info *) item_rhs;
9a619af0 25369
9c541725 25370 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25371}
25372
b4f54984
DE
25373static struct cmd_list_element *set_dwarf_cmdlist;
25374static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25375
25376static void
981a3fb3 25377set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25378{
b4f54984 25379 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25380 gdb_stdout);
ae038cb0
DJ
25381}
25382
25383static void
981a3fb3 25384show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25385{
b4f54984 25386 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25387}
25388
4bf44c1c 25389/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
25390
25391static void
c1bd65d0 25392dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 25393{
9a3c8263 25394 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 25395 int ix;
8b70b953 25396
626f2d1c
TT
25397 /* Make sure we don't accidentally use dwarf2_per_objfile while
25398 cleaning up. */
25399 dwarf2_per_objfile = NULL;
25400
59b0c7c1
JB
25401 for (ix = 0; ix < data->n_comp_units; ++ix)
25402 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 25403
59b0c7c1 25404 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 25405 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
25406 data->all_type_units[ix]->per_cu.imported_symtabs);
25407 xfree (data->all_type_units);
95554aad 25408
8b70b953 25409 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
25410
25411 if (data->dwo_files)
25412 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
25413 if (data->dwp_file)
25414 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
25415
25416 if (data->dwz_file && data->dwz_file->dwz_bfd)
25417 gdb_bfd_unref (data->dwz_file->dwz_bfd);
3f563c84
PA
25418
25419 if (data->index_table != NULL)
25420 data->index_table->~mapped_index ();
9291a0cd
TT
25421}
25422
25423\f
ae2de4f8 25424/* The "save gdb-index" command. */
9291a0cd 25425
437afbb8
JK
25426/* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25427 error checking. */
25428
25429static void
25430file_write (FILE *file, const void *data, size_t size)
25431{
25432 if (fwrite (data, 1, size, file) != size)
25433 error (_("couldn't data write to file"));
25434}
25435
25436/* Write the contents of VEC to FILE, with error checking. */
25437
25438template<typename Elem, typename Alloc>
25439static void
25440file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25441{
25442 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25443}
25444
bc8f2430
JK
25445/* In-memory buffer to prepare data to be written later to a file. */
25446class data_buf
9291a0cd 25447{
bc8f2430 25448public:
bc8f2430
JK
25449 /* Copy DATA to the end of the buffer. */
25450 template<typename T>
25451 void append_data (const T &data)
25452 {
25453 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25454 reinterpret_cast<const gdb_byte *> (&data + 1),
c2f134ac 25455 grow (sizeof (data)));
bc8f2430 25456 }
b89be57b 25457
c2f134ac
PA
25458 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25459 terminating zero is appended too. */
bc8f2430
JK
25460 void append_cstr0 (const char *cstr)
25461 {
25462 const size_t size = strlen (cstr) + 1;
c2f134ac
PA
25463 std::copy (cstr, cstr + size, grow (size));
25464 }
25465
437afbb8
JK
25466 /* Store INPUT as ULEB128 to the end of buffer. */
25467 void append_unsigned_leb128 (ULONGEST input)
25468 {
25469 for (;;)
25470 {
25471 gdb_byte output = input & 0x7f;
25472 input >>= 7;
25473 if (input)
25474 output |= 0x80;
25475 append_data (output);
25476 if (input == 0)
25477 break;
25478 }
25479 }
25480
c2f134ac
PA
25481 /* Accept a host-format integer in VAL and append it to the buffer
25482 as a target-format integer which is LEN bytes long. */
25483 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25484 {
25485 ::store_unsigned_integer (grow (len), len, byte_order, val);
bc8f2430 25486 }
9291a0cd 25487
bc8f2430
JK
25488 /* Return the size of the buffer. */
25489 size_t size () const
25490 {
25491 return m_vec.size ();
25492 }
25493
437afbb8
JK
25494 /* Return true iff the buffer is empty. */
25495 bool empty () const
25496 {
25497 return m_vec.empty ();
25498 }
25499
bc8f2430
JK
25500 /* Write the buffer to FILE. */
25501 void file_write (FILE *file) const
25502 {
437afbb8 25503 ::file_write (file, m_vec);
bc8f2430
JK
25504 }
25505
25506private:
c2f134ac
PA
25507 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25508 the start of the new block. */
25509 gdb_byte *grow (size_t size)
25510 {
25511 m_vec.resize (m_vec.size () + size);
25512 return &*m_vec.end () - size;
25513 }
25514
d5722aa2 25515 gdb::byte_vector m_vec;
bc8f2430 25516};
9291a0cd
TT
25517
25518/* An entry in the symbol table. */
25519struct symtab_index_entry
25520{
25521 /* The name of the symbol. */
25522 const char *name;
25523 /* The offset of the name in the constant pool. */
25524 offset_type index_offset;
25525 /* A sorted vector of the indices of all the CUs that hold an object
25526 of this name. */
bc8f2430 25527 std::vector<offset_type> cu_indices;
9291a0cd
TT
25528};
25529
25530/* The symbol table. This is a power-of-2-sized hash table. */
25531struct mapped_symtab
25532{
bc8f2430
JK
25533 mapped_symtab ()
25534 {
25535 data.resize (1024);
25536 }
b89be57b 25537
bc8f2430 25538 offset_type n_elements = 0;
4b76cda9 25539 std::vector<symtab_index_entry> data;
bc8f2430 25540};
9291a0cd 25541
bc8f2430 25542/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
559a7a62
JK
25543 the slot.
25544
25545 Function is used only during write_hash_table so no index format backward
25546 compatibility is needed. */
b89be57b 25547
4b76cda9 25548static symtab_index_entry &
9291a0cd
TT
25549find_slot (struct mapped_symtab *symtab, const char *name)
25550{
559a7a62 25551 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd 25552
bc8f2430
JK
25553 index = hash & (symtab->data.size () - 1);
25554 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
9291a0cd
TT
25555
25556 for (;;)
25557 {
4b76cda9
PA
25558 if (symtab->data[index].name == NULL
25559 || strcmp (name, symtab->data[index].name) == 0)
bc8f2430
JK
25560 return symtab->data[index];
25561 index = (index + step) & (symtab->data.size () - 1);
9291a0cd
TT
25562 }
25563}
25564
25565/* Expand SYMTAB's hash table. */
b89be57b 25566
9291a0cd
TT
25567static void
25568hash_expand (struct mapped_symtab *symtab)
25569{
bc8f2430 25570 auto old_entries = std::move (symtab->data);
9291a0cd 25571
bc8f2430
JK
25572 symtab->data.clear ();
25573 symtab->data.resize (old_entries.size () * 2);
9291a0cd 25574
bc8f2430 25575 for (auto &it : old_entries)
4b76cda9 25576 if (it.name != NULL)
bc8f2430 25577 {
4b76cda9 25578 auto &ref = find_slot (symtab, it.name);
bc8f2430
JK
25579 ref = std::move (it);
25580 }
9291a0cd
TT
25581}
25582
156942c7
DE
25583/* Add an entry to SYMTAB. NAME is the name of the symbol.
25584 CU_INDEX is the index of the CU in which the symbol appears.
25585 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 25586
9291a0cd
TT
25587static void
25588add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 25589 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
25590 offset_type cu_index)
25591{
156942c7 25592 offset_type cu_index_and_attrs;
9291a0cd
TT
25593
25594 ++symtab->n_elements;
bc8f2430 25595 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
9291a0cd
TT
25596 hash_expand (symtab);
25597
4b76cda9
PA
25598 symtab_index_entry &slot = find_slot (symtab, name);
25599 if (slot.name == NULL)
9291a0cd 25600 {
4b76cda9 25601 slot.name = name;
156942c7 25602 /* index_offset is set later. */
9291a0cd 25603 }
156942c7
DE
25604
25605 cu_index_and_attrs = 0;
25606 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
25607 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
25608 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
25609
25610 /* We don't want to record an index value twice as we want to avoid the
25611 duplication.
25612 We process all global symbols and then all static symbols
25613 (which would allow us to avoid the duplication by only having to check
25614 the last entry pushed), but a symbol could have multiple kinds in one CU.
25615 To keep things simple we don't worry about the duplication here and
25616 sort and uniqufy the list after we've processed all symbols. */
4b76cda9 25617 slot.cu_indices.push_back (cu_index_and_attrs);
156942c7
DE
25618}
25619
25620/* Sort and remove duplicates of all symbols' cu_indices lists. */
25621
25622static void
25623uniquify_cu_indices (struct mapped_symtab *symtab)
25624{
4b76cda9 25625 for (auto &entry : symtab->data)
156942c7 25626 {
4b76cda9 25627 if (entry.name != NULL && !entry.cu_indices.empty ())
156942c7 25628 {
4b76cda9 25629 auto &cu_indices = entry.cu_indices;
6fd931f2
PA
25630 std::sort (cu_indices.begin (), cu_indices.end ());
25631 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
25632 cu_indices.erase (from, cu_indices.end ());
156942c7
DE
25633 }
25634 }
9291a0cd
TT
25635}
25636
bc8f2430
JK
25637/* A form of 'const char *' suitable for container keys. Only the
25638 pointer is stored. The strings themselves are compared, not the
25639 pointers. */
25640class c_str_view
9291a0cd 25641{
bc8f2430
JK
25642public:
25643 c_str_view (const char *cstr)
25644 : m_cstr (cstr)
25645 {}
9291a0cd 25646
bc8f2430
JK
25647 bool operator== (const c_str_view &other) const
25648 {
25649 return strcmp (m_cstr, other.m_cstr) == 0;
25650 }
9291a0cd 25651
437afbb8
JK
25652 /* Return the underlying C string. Note, the returned string is
25653 only a reference with lifetime of this object. */
25654 const char *c_str () const
25655 {
25656 return m_cstr;
25657 }
25658
bc8f2430
JK
25659private:
25660 friend class c_str_view_hasher;
25661 const char *const m_cstr;
25662};
9291a0cd 25663
bc8f2430
JK
25664/* A std::unordered_map::hasher for c_str_view that uses the right
25665 hash function for strings in a mapped index. */
25666class c_str_view_hasher
25667{
25668public:
25669 size_t operator () (const c_str_view &x) const
25670 {
25671 return mapped_index_string_hash (INT_MAX, x.m_cstr);
25672 }
25673};
b89be57b 25674
bc8f2430
JK
25675/* A std::unordered_map::hasher for std::vector<>. */
25676template<typename T>
25677class vector_hasher
9291a0cd 25678{
bc8f2430
JK
25679public:
25680 size_t operator () (const std::vector<T> &key) const
25681 {
25682 return iterative_hash (key.data (),
25683 sizeof (key.front ()) * key.size (), 0);
25684 }
25685};
9291a0cd 25686
bc8f2430
JK
25687/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
25688 constant pool entries going into the data buffer CPOOL. */
3876f04e 25689
bc8f2430
JK
25690static void
25691write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
25692{
25693 {
25694 /* Elements are sorted vectors of the indices of all the CUs that
25695 hold an object of this name. */
25696 std::unordered_map<std::vector<offset_type>, offset_type,
25697 vector_hasher<offset_type>>
25698 symbol_hash_table;
25699
25700 /* We add all the index vectors to the constant pool first, to
25701 ensure alignment is ok. */
4b76cda9 25702 for (symtab_index_entry &entry : symtab->data)
bc8f2430 25703 {
4b76cda9 25704 if (entry.name == NULL)
bc8f2430 25705 continue;
4b76cda9 25706 gdb_assert (entry.index_offset == 0);
70a1152b
PA
25707
25708 /* Finding before inserting is faster than always trying to
25709 insert, because inserting always allocates a node, does the
25710 lookup, and then destroys the new node if another node
25711 already had the same key. C++17 try_emplace will avoid
25712 this. */
25713 const auto found
4b76cda9 25714 = symbol_hash_table.find (entry.cu_indices);
70a1152b
PA
25715 if (found != symbol_hash_table.end ())
25716 {
4b76cda9 25717 entry.index_offset = found->second;
70a1152b
PA
25718 continue;
25719 }
25720
4b76cda9
PA
25721 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
25722 entry.index_offset = cpool.size ();
25723 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
25724 for (const auto index : entry.cu_indices)
25725 cpool.append_data (MAYBE_SWAP (index));
bc8f2430
JK
25726 }
25727 }
9291a0cd
TT
25728
25729 /* Now write out the hash table. */
bc8f2430 25730 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
4b76cda9 25731 for (const auto &entry : symtab->data)
9291a0cd
TT
25732 {
25733 offset_type str_off, vec_off;
25734
4b76cda9 25735 if (entry.name != NULL)
9291a0cd 25736 {
4b76cda9 25737 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
bc8f2430 25738 if (insertpair.second)
4b76cda9 25739 cpool.append_cstr0 (entry.name);
bc8f2430 25740 str_off = insertpair.first->second;
4b76cda9 25741 vec_off = entry.index_offset;
9291a0cd
TT
25742 }
25743 else
25744 {
25745 /* While 0 is a valid constant pool index, it is not valid
25746 to have 0 for both offsets. */
25747 str_off = 0;
25748 vec_off = 0;
25749 }
25750
bc8f2430
JK
25751 output.append_data (MAYBE_SWAP (str_off));
25752 output.append_data (MAYBE_SWAP (vec_off));
9291a0cd 25753 }
9291a0cd
TT
25754}
25755
bc8f2430 25756typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
0a5429f6
DE
25757
25758/* Helper struct for building the address table. */
25759struct addrmap_index_data
25760{
bc8f2430
JK
25761 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
25762 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
25763 {}
25764
0a5429f6 25765 struct objfile *objfile;
bc8f2430
JK
25766 data_buf &addr_vec;
25767 psym_index_map &cu_index_htab;
0a5429f6
DE
25768
25769 /* Non-zero if the previous_* fields are valid.
25770 We can't write an entry until we see the next entry (since it is only then
25771 that we know the end of the entry). */
25772 int previous_valid;
25773 /* Index of the CU in the table of all CUs in the index file. */
25774 unsigned int previous_cu_index;
0963b4bd 25775 /* Start address of the CU. */
0a5429f6
DE
25776 CORE_ADDR previous_cu_start;
25777};
25778
bc8f2430 25779/* Write an address entry to ADDR_VEC. */
b89be57b 25780
9291a0cd 25781static void
bc8f2430 25782add_address_entry (struct objfile *objfile, data_buf &addr_vec,
0a5429f6 25783 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 25784{
9291a0cd
TT
25785 CORE_ADDR baseaddr;
25786
25787 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25788
c2f134ac
PA
25789 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
25790 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
bc8f2430 25791 addr_vec.append_data (MAYBE_SWAP (cu_index));
0a5429f6
DE
25792}
25793
25794/* Worker function for traversing an addrmap to build the address table. */
25795
25796static int
25797add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
25798{
9a3c8263
SM
25799 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
25800 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
25801
25802 if (data->previous_valid)
bc8f2430 25803 add_address_entry (data->objfile, data->addr_vec,
0a5429f6
DE
25804 data->previous_cu_start, start_addr,
25805 data->previous_cu_index);
25806
25807 data->previous_cu_start = start_addr;
25808 if (pst != NULL)
25809 {
bc8f2430
JK
25810 const auto it = data->cu_index_htab.find (pst);
25811 gdb_assert (it != data->cu_index_htab.cend ());
25812 data->previous_cu_index = it->second;
0a5429f6
DE
25813 data->previous_valid = 1;
25814 }
25815 else
bc8f2430 25816 data->previous_valid = 0;
0a5429f6
DE
25817
25818 return 0;
25819}
25820
bc8f2430 25821/* Write OBJFILE's address map to ADDR_VEC.
0a5429f6
DE
25822 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
25823 in the index file. */
25824
25825static void
bc8f2430
JK
25826write_address_map (struct objfile *objfile, data_buf &addr_vec,
25827 psym_index_map &cu_index_htab)
0a5429f6 25828{
bc8f2430 25829 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
0a5429f6
DE
25830
25831 /* When writing the address table, we have to cope with the fact that
25832 the addrmap iterator only provides the start of a region; we have to
25833 wait until the next invocation to get the start of the next region. */
25834
25835 addrmap_index_data.objfile = objfile;
0a5429f6
DE
25836 addrmap_index_data.previous_valid = 0;
25837
25838 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
25839 &addrmap_index_data);
25840
25841 /* It's highly unlikely the last entry (end address = 0xff...ff)
25842 is valid, but we should still handle it.
25843 The end address is recorded as the start of the next region, but that
25844 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
25845 anyway. */
25846 if (addrmap_index_data.previous_valid)
bc8f2430 25847 add_address_entry (objfile, addr_vec,
0a5429f6
DE
25848 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
25849 addrmap_index_data.previous_cu_index);
9291a0cd
TT
25850}
25851
156942c7
DE
25852/* Return the symbol kind of PSYM. */
25853
25854static gdb_index_symbol_kind
25855symbol_kind (struct partial_symbol *psym)
25856{
25857 domain_enum domain = PSYMBOL_DOMAIN (psym);
25858 enum address_class aclass = PSYMBOL_CLASS (psym);
25859
25860 switch (domain)
25861 {
25862 case VAR_DOMAIN:
25863 switch (aclass)
25864 {
25865 case LOC_BLOCK:
25866 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
25867 case LOC_TYPEDEF:
25868 return GDB_INDEX_SYMBOL_KIND_TYPE;
25869 case LOC_COMPUTED:
25870 case LOC_CONST_BYTES:
25871 case LOC_OPTIMIZED_OUT:
25872 case LOC_STATIC:
25873 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25874 case LOC_CONST:
25875 /* Note: It's currently impossible to recognize psyms as enum values
25876 short of reading the type info. For now punt. */
25877 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25878 default:
25879 /* There are other LOC_FOO values that one might want to classify
25880 as variables, but dwarf2read.c doesn't currently use them. */
25881 return GDB_INDEX_SYMBOL_KIND_OTHER;
25882 }
25883 case STRUCT_DOMAIN:
25884 return GDB_INDEX_SYMBOL_KIND_TYPE;
25885 default:
25886 return GDB_INDEX_SYMBOL_KIND_OTHER;
25887 }
25888}
25889
9291a0cd 25890/* Add a list of partial symbols to SYMTAB. */
b89be57b 25891
9291a0cd
TT
25892static void
25893write_psymbols (struct mapped_symtab *symtab,
bc8f2430 25894 std::unordered_set<partial_symbol *> &psyms_seen,
9291a0cd
TT
25895 struct partial_symbol **psymp,
25896 int count,
987d643c
TT
25897 offset_type cu_index,
25898 int is_static)
9291a0cd
TT
25899{
25900 for (; count-- > 0; ++psymp)
25901 {
156942c7 25902 struct partial_symbol *psym = *psymp;
987d643c 25903
156942c7 25904 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 25905 error (_("Ada is not currently supported by the index"));
987d643c 25906
987d643c 25907 /* Only add a given psymbol once. */
bc8f2430 25908 if (psyms_seen.insert (psym).second)
987d643c 25909 {
156942c7
DE
25910 gdb_index_symbol_kind kind = symbol_kind (psym);
25911
156942c7
DE
25912 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
25913 is_static, kind, cu_index);
987d643c 25914 }
9291a0cd
TT
25915 }
25916}
25917
1fd400ff
TT
25918/* A helper struct used when iterating over debug_types. */
25919struct signatured_type_index_data
25920{
bc8f2430
JK
25921 signatured_type_index_data (data_buf &types_list_,
25922 std::unordered_set<partial_symbol *> &psyms_seen_)
25923 : types_list (types_list_), psyms_seen (psyms_seen_)
25924 {}
25925
1fd400ff
TT
25926 struct objfile *objfile;
25927 struct mapped_symtab *symtab;
bc8f2430
JK
25928 data_buf &types_list;
25929 std::unordered_set<partial_symbol *> &psyms_seen;
1fd400ff
TT
25930 int cu_index;
25931};
25932
25933/* A helper function that writes a single signatured_type to an
25934 obstack. */
b89be57b 25935
1fd400ff
TT
25936static int
25937write_one_signatured_type (void **slot, void *d)
25938{
9a3c8263
SM
25939 struct signatured_type_index_data *info
25940 = (struct signatured_type_index_data *) d;
1fd400ff 25941 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 25942 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
25943
25944 write_psymbols (info->symtab,
987d643c 25945 info->psyms_seen,
af5bf4ad 25946 &info->objfile->global_psymbols[psymtab->globals_offset],
987d643c
TT
25947 psymtab->n_global_syms, info->cu_index,
25948 0);
1fd400ff 25949 write_psymbols (info->symtab,
987d643c 25950 info->psyms_seen,
af5bf4ad 25951 &info->objfile->static_psymbols[psymtab->statics_offset],
987d643c
TT
25952 psymtab->n_static_syms, info->cu_index,
25953 1);
1fd400ff 25954
c2f134ac
PA
25955 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
25956 to_underlying (entry->per_cu.sect_off));
25957 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
25958 to_underlying (entry->type_offset_in_tu));
25959 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
1fd400ff
TT
25960
25961 ++info->cu_index;
25962
25963 return 1;
25964}
25965
e8f8bcb3
PA
25966/* Recurse into all "included" dependencies and count their symbols as
25967 if they appeared in this psymtab. */
25968
25969static void
25970recursively_count_psymbols (struct partial_symtab *psymtab,
25971 size_t &psyms_seen)
25972{
25973 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
25974 if (psymtab->dependencies[i]->user != NULL)
25975 recursively_count_psymbols (psymtab->dependencies[i],
25976 psyms_seen);
25977
25978 psyms_seen += psymtab->n_global_syms;
25979 psyms_seen += psymtab->n_static_syms;
25980}
25981
95554aad
TT
25982/* Recurse into all "included" dependencies and write their symbols as
25983 if they appeared in this psymtab. */
25984
25985static void
25986recursively_write_psymbols (struct objfile *objfile,
25987 struct partial_symtab *psymtab,
25988 struct mapped_symtab *symtab,
bc8f2430 25989 std::unordered_set<partial_symbol *> &psyms_seen,
95554aad
TT
25990 offset_type cu_index)
25991{
25992 int i;
25993
25994 for (i = 0; i < psymtab->number_of_dependencies; ++i)
25995 if (psymtab->dependencies[i]->user != NULL)
25996 recursively_write_psymbols (objfile, psymtab->dependencies[i],
25997 symtab, psyms_seen, cu_index);
25998
25999 write_psymbols (symtab,
26000 psyms_seen,
af5bf4ad 26001 &objfile->global_psymbols[psymtab->globals_offset],
95554aad
TT
26002 psymtab->n_global_syms, cu_index,
26003 0);
26004 write_psymbols (symtab,
26005 psyms_seen,
af5bf4ad 26006 &objfile->static_psymbols[psymtab->statics_offset],
95554aad
TT
26007 psymtab->n_static_syms, cu_index,
26008 1);
26009}
26010
437afbb8
JK
26011/* DWARF-5 .debug_names builder. */
26012class debug_names
26013{
26014public:
26015 debug_names (bool is_dwarf64, bfd_endian dwarf5_byte_order)
26016 : m_dwarf5_byte_order (dwarf5_byte_order),
26017 m_dwarf32 (dwarf5_byte_order),
26018 m_dwarf64 (dwarf5_byte_order),
26019 m_dwarf (is_dwarf64
26020 ? static_cast<dwarf &> (m_dwarf64)
26021 : static_cast<dwarf &> (m_dwarf32)),
26022 m_name_table_string_offs (m_dwarf.name_table_string_offs),
26023 m_name_table_entry_offs (m_dwarf.name_table_entry_offs)
26024 {}
9291a0cd 26025
437afbb8
JK
26026 /* Insert one symbol. */
26027 void insert (const partial_symbol *psym, int cu_index, bool is_static)
26028 {
26029 const int dwarf_tag = psymbol_tag (psym);
26030 if (dwarf_tag == 0)
26031 return;
26032 const char *const name = SYMBOL_SEARCH_NAME (psym);
26033 const auto insertpair
26034 = m_name_to_value_set.emplace (c_str_view (name),
26035 std::set<symbol_value> ());
26036 std::set<symbol_value> &value_set = insertpair.first->second;
26037 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static));
26038 }
9291a0cd 26039
437afbb8
JK
26040 /* Build all the tables. All symbols must be already inserted.
26041 This function does not call file_write, caller has to do it
26042 afterwards. */
26043 void build ()
26044 {
26045 /* Verify the build method has not be called twice. */
26046 gdb_assert (m_abbrev_table.empty ());
26047 const size_t name_count = m_name_to_value_set.size ();
26048 m_bucket_table.resize
26049 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26050 m_hash_table.reserve (name_count);
26051 m_name_table_string_offs.reserve (name_count);
26052 m_name_table_entry_offs.reserve (name_count);
26053
26054 /* Map each hash of symbol to its name and value. */
26055 struct hash_it_pair
26056 {
26057 uint32_t hash;
26058 decltype (m_name_to_value_set)::const_iterator it;
26059 };
26060 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26061 bucket_hash.resize (m_bucket_table.size ());
26062 for (decltype (m_name_to_value_set)::const_iterator it
26063 = m_name_to_value_set.cbegin ();
26064 it != m_name_to_value_set.cend ();
26065 ++it)
26066 {
26067 const char *const name = it->first.c_str ();
26068 const uint32_t hash = dwarf5_djb_hash (name);
26069 hash_it_pair hashitpair;
26070 hashitpair.hash = hash;
26071 hashitpair.it = it;
26072 auto &slot = bucket_hash[hash % bucket_hash.size()];
26073 slot.push_front (std::move (hashitpair));
26074 }
26075 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26076 {
26077 const std::forward_list<hash_it_pair> &hashitlist
26078 = bucket_hash[bucket_ix];
26079 if (hashitlist.empty ())
26080 continue;
26081 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26082 /* The hashes array is indexed starting at 1. */
26083 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26084 sizeof (bucket_slot), m_dwarf5_byte_order,
26085 m_hash_table.size () + 1);
26086 for (const hash_it_pair &hashitpair : hashitlist)
26087 {
26088 m_hash_table.push_back (0);
26089 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26090 (&m_hash_table.back ()),
26091 sizeof (m_hash_table.back ()),
26092 m_dwarf5_byte_order, hashitpair.hash);
26093 const c_str_view &name = hashitpair.it->first;
26094 const std::set<symbol_value> &value_set = hashitpair.it->second;
26095 m_name_table_string_offs.push_back_reorder
26096 (m_debugstrlookup.lookup (name.c_str ()));
26097 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26098 gdb_assert (!value_set.empty ());
26099 for (const symbol_value &value : value_set)
26100 {
26101 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
26102 value.is_static)];
26103 if (idx == 0)
26104 {
26105 idx = m_idx_next++;
26106 m_abbrev_table.append_unsigned_leb128 (idx);
26107 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
26108 m_abbrev_table.append_unsigned_leb128 (DW_IDX_compile_unit);
26109 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26110 m_abbrev_table.append_unsigned_leb128 (value.is_static
26111 ? DW_IDX_GNU_internal
26112 : DW_IDX_GNU_external);
26113 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26114
26115 /* Terminate attributes list. */
26116 m_abbrev_table.append_unsigned_leb128 (0);
26117 m_abbrev_table.append_unsigned_leb128 (0);
26118 }
9291a0cd 26119
437afbb8
JK
26120 m_entry_pool.append_unsigned_leb128 (idx);
26121 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26122 }
9291a0cd 26123
437afbb8
JK
26124 /* Terminate the list of CUs. */
26125 m_entry_pool.append_unsigned_leb128 (0);
26126 }
26127 }
26128 gdb_assert (m_hash_table.size () == name_count);
987d643c 26129
437afbb8
JK
26130 /* Terminate tags list. */
26131 m_abbrev_table.append_unsigned_leb128 (0);
26132 }
0a5429f6 26133
437afbb8
JK
26134 /* Return .debug_names bucket count. This must be called only after
26135 calling the build method. */
26136 uint32_t bucket_count () const
26137 {
26138 /* Verify the build method has been already called. */
26139 gdb_assert (!m_abbrev_table.empty ());
26140 const uint32_t retval = m_bucket_table.size ();
26141
26142 /* Check for overflow. */
26143 gdb_assert (retval == m_bucket_table.size ());
26144 return retval;
26145 }
26146
26147 /* Return .debug_names names count. This must be called only after
26148 calling the build method. */
26149 uint32_t name_count () const
26150 {
26151 /* Verify the build method has been already called. */
26152 gdb_assert (!m_abbrev_table.empty ());
26153 const uint32_t retval = m_hash_table.size ();
26154
26155 /* Check for overflow. */
26156 gdb_assert (retval == m_hash_table.size ());
26157 return retval;
26158 }
26159
26160 /* Return number of bytes of .debug_names abbreviation table. This
26161 must be called only after calling the build method. */
26162 uint32_t abbrev_table_bytes () const
26163 {
26164 gdb_assert (!m_abbrev_table.empty ());
26165 return m_abbrev_table.size ();
26166 }
26167
26168 /* Recurse into all "included" dependencies and store their symbols
26169 as if they appeared in this psymtab. */
26170 void recursively_write_psymbols
26171 (struct objfile *objfile,
26172 struct partial_symtab *psymtab,
26173 std::unordered_set<partial_symbol *> &psyms_seen,
26174 int cu_index)
26175 {
26176 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26177 if (psymtab->dependencies[i]->user != NULL)
26178 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26179 psyms_seen, cu_index);
26180
26181 write_psymbols (psyms_seen,
26182 &objfile->global_psymbols[psymtab->globals_offset],
26183 psymtab->n_global_syms, cu_index, false);
26184 write_psymbols (psyms_seen,
26185 &objfile->static_psymbols[psymtab->statics_offset],
26186 psymtab->n_static_syms, cu_index, true);
26187 }
26188
26189 /* Return number of bytes the .debug_names section will have. This
26190 must be called only after calling the build method. */
26191 size_t bytes () const
26192 {
26193 /* Verify the build method has been already called. */
26194 gdb_assert (!m_abbrev_table.empty ());
26195 size_t expected_bytes = 0;
26196 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26197 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26198 expected_bytes += m_name_table_string_offs.bytes ();
26199 expected_bytes += m_name_table_entry_offs.bytes ();
26200 expected_bytes += m_abbrev_table.size ();
26201 expected_bytes += m_entry_pool.size ();
26202 return expected_bytes;
26203 }
26204
26205 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26206 FILE_STR. This must be called only after calling the build
26207 method. */
26208 void file_write (FILE *file_names, FILE *file_str) const
26209 {
26210 /* Verify the build method has been already called. */
26211 gdb_assert (!m_abbrev_table.empty ());
26212 ::file_write (file_names, m_bucket_table);
26213 ::file_write (file_names, m_hash_table);
26214 m_name_table_string_offs.file_write (file_names);
26215 m_name_table_entry_offs.file_write (file_names);
26216 m_abbrev_table.file_write (file_names);
26217 m_entry_pool.file_write (file_names);
26218 m_debugstrlookup.file_write (file_str);
26219 }
26220
26221private:
26222
26223 /* Storage for symbol names mapping them to their .debug_str section
26224 offsets. */
26225 class debug_str_lookup
26226 {
26227 public:
26228
26229 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26230 All .debug_str section strings are automatically stored. */
26231 debug_str_lookup ()
26232 : m_abfd (dwarf2_per_objfile->objfile->obfd)
26233 {
26234 dwarf2_read_section (dwarf2_per_objfile->objfile,
26235 &dwarf2_per_objfile->str);
26236 if (dwarf2_per_objfile->str.buffer == NULL)
26237 return;
26238 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26239 data < (dwarf2_per_objfile->str.buffer
26240 + dwarf2_per_objfile->str.size);)
26241 {
26242 const char *const s = reinterpret_cast<const char *> (data);
26243 const auto insertpair
26244 = m_str_table.emplace (c_str_view (s),
26245 data - dwarf2_per_objfile->str.buffer);
26246 if (!insertpair.second)
26247 complaint (&symfile_complaints,
26248 _("Duplicate string \"%s\" in "
26249 ".debug_str section [in module %s]"),
26250 s, bfd_get_filename (m_abfd));
26251 data += strlen (s) + 1;
26252 }
26253 }
26254
26255 /* Return offset of symbol name S in the .debug_str section. Add
26256 such symbol to the section's end if it does not exist there
26257 yet. */
26258 size_t lookup (const char *s)
26259 {
26260 const auto it = m_str_table.find (c_str_view (s));
26261 if (it != m_str_table.end ())
26262 return it->second;
26263 const size_t offset = (dwarf2_per_objfile->str.size
26264 + m_str_add_buf.size ());
26265 m_str_table.emplace (c_str_view (s), offset);
26266 m_str_add_buf.append_cstr0 (s);
26267 return offset;
26268 }
26269
26270 /* Append the end of the .debug_str section to FILE. */
26271 void file_write (FILE *file) const
26272 {
26273 m_str_add_buf.file_write (file);
26274 }
26275
26276 private:
26277 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26278 bfd *const m_abfd;
26279
26280 /* Data to add at the end of .debug_str for new needed symbol names. */
26281 data_buf m_str_add_buf;
26282 };
26283
26284 /* Container to map used DWARF tags to their .debug_names abbreviation
26285 tags. */
26286 class index_key
26287 {
26288 public:
26289 index_key (int dwarf_tag_, bool is_static_)
26290 : dwarf_tag (dwarf_tag_), is_static (is_static_)
26291 {
26292 }
26293
26294 bool
26295 operator== (const index_key &other) const
26296 {
26297 return dwarf_tag == other.dwarf_tag && is_static == other.is_static;
26298 }
26299
26300 const int dwarf_tag;
26301 const bool is_static;
26302 };
26303
26304 /* Provide std::unordered_map::hasher for index_key. */
26305 class index_key_hasher
26306 {
26307 public:
26308 size_t
26309 operator () (const index_key &key) const
26310 {
26311 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26312 }
26313 };
26314
26315 /* Parameters of one symbol entry. */
26316 class symbol_value
26317 {
26318 public:
26319 const int dwarf_tag, cu_index;
26320 const bool is_static;
26321
26322 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_)
26323 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_)
26324 {}
26325
26326 bool
26327 operator< (const symbol_value &other) const
26328 {
26329#define X(n) \
26330 do \
26331 { \
26332 if (n < other.n) \
26333 return true; \
26334 if (n > other.n) \
26335 return false; \
26336 } \
26337 while (0)
26338 X (dwarf_tag);
26339 X (is_static);
26340 X (cu_index);
26341#undef X
26342 return false;
26343 }
26344 };
26345
26346 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26347 output. */
26348 class offset_vec
26349 {
26350 protected:
26351 const bfd_endian dwarf5_byte_order;
26352 public:
26353 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26354 : dwarf5_byte_order (dwarf5_byte_order_)
26355 {}
26356
26357 /* Call std::vector::reserve for NELEM elements. */
26358 virtual void reserve (size_t nelem) = 0;
26359
26360 /* Call std::vector::push_back with store_unsigned_integer byte
26361 reordering for ELEM. */
26362 virtual void push_back_reorder (size_t elem) = 0;
26363
26364 /* Return expected output size in bytes. */
26365 virtual size_t bytes () const = 0;
26366
26367 /* Write name table to FILE. */
26368 virtual void file_write (FILE *file) const = 0;
26369 };
26370
26371 /* Template to unify DWARF-32 and DWARF-64 output. */
26372 template<typename OffsetSize>
26373 class offset_vec_tmpl : public offset_vec
26374 {
26375 public:
26376 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26377 : offset_vec (dwarf5_byte_order_)
26378 {}
26379
26380 /* Implement offset_vec::reserve. */
26381 void reserve (size_t nelem) override
26382 {
26383 m_vec.reserve (nelem);
26384 }
26385
26386 /* Implement offset_vec::push_back_reorder. */
26387 void push_back_reorder (size_t elem) override
26388 {
26389 m_vec.push_back (elem);
26390 /* Check for overflow. */
26391 gdb_assert (m_vec.back () == elem);
26392 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26393 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26394 }
26395
26396 /* Implement offset_vec::bytes. */
26397 size_t bytes () const override
26398 {
26399 return m_vec.size () * sizeof (m_vec[0]);
26400 }
26401
26402 /* Implement offset_vec::file_write. */
26403 void file_write (FILE *file) const override
26404 {
26405 ::file_write (file, m_vec);
26406 }
26407
26408 private:
26409 std::vector<OffsetSize> m_vec;
26410 };
26411
26412 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26413 respecting name table width. */
26414 class dwarf
26415 {
26416 public:
26417 offset_vec &name_table_string_offs, &name_table_entry_offs;
26418
26419 dwarf (offset_vec &name_table_string_offs_,
26420 offset_vec &name_table_entry_offs_)
26421 : name_table_string_offs (name_table_string_offs_),
26422 name_table_entry_offs (name_table_entry_offs_)
26423 {
26424 }
26425 };
e8f8bcb3 26426
437afbb8
JK
26427 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26428 respecting name table width. */
26429 template<typename OffsetSize>
26430 class dwarf_tmpl : public dwarf
26431 {
26432 public:
26433 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26434 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26435 m_name_table_string_offs (dwarf5_byte_order_),
26436 m_name_table_entry_offs (dwarf5_byte_order_)
26437 {}
26438
26439 private:
26440 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26441 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26442 };
26443
26444 /* Try to reconstruct original DWARF tag for given partial_symbol.
26445 This function is not DWARF-5 compliant but it is sufficient for
26446 GDB as a DWARF-5 index consumer. */
26447 static int psymbol_tag (const struct partial_symbol *psym)
26448 {
26449 domain_enum domain = PSYMBOL_DOMAIN (psym);
26450 enum address_class aclass = PSYMBOL_CLASS (psym);
26451
26452 switch (domain)
26453 {
26454 case VAR_DOMAIN:
26455 switch (aclass)
26456 {
26457 case LOC_BLOCK:
26458 return DW_TAG_subprogram;
26459 case LOC_TYPEDEF:
26460 return DW_TAG_typedef;
26461 case LOC_COMPUTED:
26462 case LOC_CONST_BYTES:
26463 case LOC_OPTIMIZED_OUT:
26464 case LOC_STATIC:
26465 return DW_TAG_variable;
26466 case LOC_CONST:
26467 /* Note: It's currently impossible to recognize psyms as enum values
26468 short of reading the type info. For now punt. */
26469 return DW_TAG_variable;
26470 default:
26471 /* There are other LOC_FOO values that one might want to classify
26472 as variables, but dwarf2read.c doesn't currently use them. */
26473 return DW_TAG_variable;
26474 }
26475 case STRUCT_DOMAIN:
26476 return DW_TAG_structure_type;
26477 default:
26478 return 0;
26479 }
26480 }
26481
26482 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26483 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26484 struct partial_symbol **psymp, int count, int cu_index,
26485 bool is_static)
26486 {
26487 for (; count-- > 0; ++psymp)
26488 {
26489 struct partial_symbol *psym = *psymp;
26490
26491 if (SYMBOL_LANGUAGE (psym) == language_ada)
26492 error (_("Ada is not currently supported by the index"));
26493
26494 /* Only add a given psymbol once. */
26495 if (psyms_seen.insert (psym).second)
26496 insert (psym, cu_index, is_static);
26497 }
26498 }
26499
26500 /* Store value of each symbol. */
26501 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
26502 m_name_to_value_set;
26503
26504 /* Tables of DWARF-5 .debug_names. They are in object file byte
26505 order. */
26506 std::vector<uint32_t> m_bucket_table;
26507 std::vector<uint32_t> m_hash_table;
26508
26509 const bfd_endian m_dwarf5_byte_order;
26510 dwarf_tmpl<uint32_t> m_dwarf32;
26511 dwarf_tmpl<uint64_t> m_dwarf64;
26512 dwarf &m_dwarf;
26513 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
26514 debug_str_lookup m_debugstrlookup;
26515
26516 /* Map each used .debug_names abbreviation tag parameter to its
26517 index value. */
26518 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
26519
26520 /* Next unused .debug_names abbreviation tag for
26521 m_indexkey_to_idx. */
26522 int m_idx_next = 1;
26523
26524 /* .debug_names abbreviation table. */
26525 data_buf m_abbrev_table;
26526
26527 /* .debug_names entry pool. */
26528 data_buf m_entry_pool;
26529};
26530
26531/* Return iff any of the needed offsets does not fit into 32-bit
26532 .debug_names section. */
26533
26534static bool
26535check_dwarf64_offsets ()
26536{
26537 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26538 {
26539 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
26540
26541 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26542 return true;
26543 }
26544 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26545 {
26546 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26547 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26548
26549 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26550 return true;
26551 }
26552 return false;
26553}
26554
26555/* The psyms_seen set is potentially going to be largish (~40k
26556 elements when indexing a -g3 build of GDB itself). Estimate the
26557 number of elements in order to avoid too many rehashes, which
26558 require rebuilding buckets and thus many trips to
26559 malloc/free. */
26560
26561static size_t
26562psyms_seen_size ()
26563{
e8f8bcb3
PA
26564 size_t psyms_count = 0;
26565 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26566 {
26567 struct dwarf2_per_cu_data *per_cu
26568 = dwarf2_per_objfile->all_comp_units[i];
26569 struct partial_symtab *psymtab = per_cu->v.psymtab;
26570
26571 if (psymtab != NULL && psymtab->user == NULL)
26572 recursively_count_psymbols (psymtab, psyms_count);
26573 }
26574 /* Generating an index for gdb itself shows a ratio of
26575 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
437afbb8
JK
26576 return psyms_count / 4;
26577}
26578
26579/* Write new .gdb_index section for OBJFILE into OUT_FILE.
26580 Return how many bytes were expected to be written into OUT_FILE. */
26581
26582static size_t
26583write_gdbindex (struct objfile *objfile, FILE *out_file)
26584{
26585 mapped_symtab symtab;
26586 data_buf cu_list;
26587
26588 /* While we're scanning CU's create a table that maps a psymtab pointer
26589 (which is what addrmap records) to its index (which is what is recorded
26590 in the index file). This will later be needed to write the address
26591 table. */
26592 psym_index_map cu_index_htab;
26593 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
26594
26595 /* The CU list is already sorted, so we don't need to do additional
26596 work here. Also, the debug_types entries do not appear in
26597 all_comp_units, but only in their own hash table. */
26598
26599 std::unordered_set<partial_symbol *> psyms_seen (psyms_seen_size ());
bc8f2430 26600 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 26601 {
3e43a32a
MS
26602 struct dwarf2_per_cu_data *per_cu
26603 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 26604 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 26605
92fac807
JK
26606 /* CU of a shared file from 'dwz -m' may be unused by this main file.
26607 It may be referenced from a local scope but in such case it does not
26608 need to be present in .gdb_index. */
26609 if (psymtab == NULL)
26610 continue;
26611
95554aad 26612 if (psymtab->user == NULL)
bc8f2430
JK
26613 recursively_write_psymbols (objfile, psymtab, &symtab,
26614 psyms_seen, i);
9291a0cd 26615
bc8f2430
JK
26616 const auto insertpair = cu_index_htab.emplace (psymtab, i);
26617 gdb_assert (insertpair.second);
9291a0cd 26618
c2f134ac
PA
26619 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
26620 to_underlying (per_cu->sect_off));
26621 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
26622 }
26623
0a5429f6 26624 /* Dump the address map. */
bc8f2430
JK
26625 data_buf addr_vec;
26626 write_address_map (objfile, addr_vec, cu_index_htab);
0a5429f6 26627
1fd400ff 26628 /* Write out the .debug_type entries, if any. */
bc8f2430 26629 data_buf types_cu_list;
1fd400ff
TT
26630 if (dwarf2_per_objfile->signatured_types)
26631 {
bc8f2430
JK
26632 signatured_type_index_data sig_data (types_cu_list,
26633 psyms_seen);
1fd400ff
TT
26634
26635 sig_data.objfile = objfile;
bc8f2430 26636 sig_data.symtab = &symtab;
1fd400ff
TT
26637 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
26638 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26639 write_one_signatured_type, &sig_data);
26640 }
26641
156942c7
DE
26642 /* Now that we've processed all symbols we can shrink their cu_indices
26643 lists. */
bc8f2430 26644 uniquify_cu_indices (&symtab);
156942c7 26645
bc8f2430
JK
26646 data_buf symtab_vec, constant_pool;
26647 write_hash_table (&symtab, symtab_vec, constant_pool);
9291a0cd 26648
bc8f2430
JK
26649 data_buf contents;
26650 const offset_type size_of_contents = 6 * sizeof (offset_type);
26651 offset_type total_len = size_of_contents;
9291a0cd
TT
26652
26653 /* The version number. */
bc8f2430 26654 contents.append_data (MAYBE_SWAP (8));
9291a0cd
TT
26655
26656 /* The offset of the CU list from the start of the file. */
bc8f2430
JK
26657 contents.append_data (MAYBE_SWAP (total_len));
26658 total_len += cu_list.size ();
9291a0cd 26659
1fd400ff 26660 /* The offset of the types CU list from the start of the file. */
bc8f2430
JK
26661 contents.append_data (MAYBE_SWAP (total_len));
26662 total_len += types_cu_list.size ();
1fd400ff 26663
9291a0cd 26664 /* The offset of the address table from the start of the file. */
bc8f2430
JK
26665 contents.append_data (MAYBE_SWAP (total_len));
26666 total_len += addr_vec.size ();
9291a0cd
TT
26667
26668 /* The offset of the symbol table from the start of the file. */
bc8f2430
JK
26669 contents.append_data (MAYBE_SWAP (total_len));
26670 total_len += symtab_vec.size ();
9291a0cd
TT
26671
26672 /* The offset of the constant pool from the start of the file. */
bc8f2430
JK
26673 contents.append_data (MAYBE_SWAP (total_len));
26674 total_len += constant_pool.size ();
9291a0cd 26675
bc8f2430 26676 gdb_assert (contents.size () == size_of_contents);
9291a0cd 26677
bc8f2430
JK
26678 contents.file_write (out_file);
26679 cu_list.file_write (out_file);
26680 types_cu_list.file_write (out_file);
26681 addr_vec.file_write (out_file);
26682 symtab_vec.file_write (out_file);
26683 constant_pool.file_write (out_file);
9291a0cd 26684
437afbb8
JK
26685 return total_len;
26686}
26687
26688/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
26689static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
26690
26691/* Write a new .debug_names section for OBJFILE into OUT_FILE, write
26692 needed addition to .debug_str section to OUT_FILE_STR. Return how
26693 many bytes were expected to be written into OUT_FILE. */
26694
26695static size_t
26696write_debug_names (struct objfile *objfile, FILE *out_file, FILE *out_file_str)
26697{
26698 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets ();
26699 const int dwarf5_offset_size = dwarf5_is_dwarf64 ? 8 : 4;
26700 const enum bfd_endian dwarf5_byte_order
26701 = gdbarch_byte_order (get_objfile_arch (objfile));
26702
26703 /* The CU list is already sorted, so we don't need to do additional
26704 work here. Also, the debug_types entries do not appear in
26705 all_comp_units, but only in their own hash table. */
26706 data_buf cu_list;
26707 debug_names nametable (dwarf5_is_dwarf64, dwarf5_byte_order);
26708 std::unordered_set<partial_symbol *> psyms_seen (psyms_seen_size ());
26709 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26710 {
26711 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
26712 partial_symtab *psymtab = per_cu->v.psymtab;
26713
26714 /* CU of a shared file from 'dwz -m' may be unused by this main
26715 file. It may be referenced from a local scope but in such
26716 case it does not need to be present in .debug_names. */
26717 if (psymtab == NULL)
26718 continue;
26719
26720 if (psymtab->user == NULL)
26721 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
26722
26723 cu_list.append_uint (dwarf5_offset_size, dwarf5_byte_order,
26724 to_underlying (per_cu->sect_off));
26725 }
26726 nametable.build ();
26727
26728 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
26729
26730 data_buf types_cu_list;
26731 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26732 {
26733 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26734 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26735
26736 types_cu_list.append_uint (dwarf5_offset_size, dwarf5_byte_order,
26737 to_underlying (per_cu.sect_off));
26738 }
26739
26740 const offset_type bytes_of_header
26741 = ((dwarf5_is_dwarf64 ? 12 : 4)
26742 + 2 + 2 + 7 * 4
26743 + sizeof (dwarf5_gdb_augmentation));
26744 size_t expected_bytes = 0;
26745 expected_bytes += bytes_of_header;
26746 expected_bytes += cu_list.size ();
26747 expected_bytes += types_cu_list.size ();
26748 expected_bytes += nametable.bytes ();
26749 data_buf header;
26750
26751 if (!dwarf5_is_dwarf64)
26752 {
26753 const uint64_t size64 = expected_bytes - 4;
26754 gdb_assert (size64 < 0xfffffff0);
26755 header.append_uint (4, dwarf5_byte_order, size64);
26756 }
26757 else
26758 {
26759 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
26760 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
26761 }
26762
26763 /* The version number. */
26764 header.append_uint (2, dwarf5_byte_order, 5);
26765
26766 /* Padding. */
26767 header.append_uint (2, dwarf5_byte_order, 0);
26768
26769 /* comp_unit_count - The number of CUs in the CU list. */
26770 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
26771
26772 /* local_type_unit_count - The number of TUs in the local TU
26773 list. */
26774 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
26775
26776 /* foreign_type_unit_count - The number of TUs in the foreign TU
26777 list. */
26778 header.append_uint (4, dwarf5_byte_order, 0);
26779
26780 /* bucket_count - The number of hash buckets in the hash lookup
26781 table. */
26782 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
26783
26784 /* name_count - The number of unique names in the index. */
26785 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
26786
26787 /* abbrev_table_size - The size in bytes of the abbreviations
26788 table. */
26789 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
26790
26791 /* augmentation_string_size - The size in bytes of the augmentation
26792 string. This value is rounded up to a multiple of 4. */
26793 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
26794 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
26795 header.append_data (dwarf5_gdb_augmentation);
26796
26797 gdb_assert (header.size () == bytes_of_header);
26798
26799 header.file_write (out_file);
26800 cu_list.file_write (out_file);
26801 types_cu_list.file_write (out_file);
26802 nametable.file_write (out_file, out_file_str);
26803
26804 return expected_bytes;
26805}
26806
26807/* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
26808 position is at the end of the file. */
26809
26810static void
26811assert_file_size (FILE *file, const char *filename, size_t expected_size)
26812{
26813 const auto file_size = ftell (file);
26814 if (file_size == -1)
26815 error (_("Can't get `%s' size"), filename);
26816 gdb_assert (file_size == expected_size);
26817}
26818
26819/* An index variant. */
26820enum dw_index_kind
26821{
26822 /* GDB's own .gdb_index format. */
26823 GDB_INDEX,
26824
26825 /* DWARF5 .debug_names. */
26826 DEBUG_NAMES,
26827};
26828
26829/* Create an index file for OBJFILE in the directory DIR. */
26830
26831static void
26832write_psymtabs_to_index (struct objfile *objfile, const char *dir,
26833 dw_index_kind index_kind)
26834{
26835 if (dwarf2_per_objfile->using_index)
26836 error (_("Cannot use an index to create the index"));
26837
26838 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
26839 error (_("Cannot make an index when the file has multiple .debug_types sections"));
26840
26841 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
26842 return;
26843
26844 struct stat st;
26845 if (stat (objfile_name (objfile), &st) < 0)
26846 perror_with_name (objfile_name (objfile));
26847
26848 std::string filename (std::string (dir) + SLASH_STRING
26849 + lbasename (objfile_name (objfile))
26850 + (index_kind == dw_index_kind::DEBUG_NAMES
26851 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
26852
26853 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
26854 if (!out_file)
26855 error (_("Can't open `%s' for writing"), filename.c_str ());
26856
26857 /* Order matters here; we want FILE to be closed before FILENAME is
26858 unlinked, because on MS-Windows one cannot delete a file that is
26859 still open. (Don't call anything here that might throw until
26860 file_closer is created.) */
26861 gdb::unlinker unlink_file (filename.c_str ());
26862 gdb_file_up close_out_file (out_file);
26863
26864 if (index_kind == dw_index_kind::DEBUG_NAMES)
26865 {
26866 std::string filename_str (std::string (dir) + SLASH_STRING
26867 + lbasename (objfile_name (objfile))
26868 + DEBUG_STR_SUFFIX);
26869 FILE *out_file_str
26870 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
26871 if (!out_file_str)
26872 error (_("Can't open `%s' for writing"), filename_str.c_str ());
26873 gdb::unlinker unlink_file_str (filename_str.c_str ());
26874 gdb_file_up close_out_file_str (out_file_str);
26875
26876 const size_t total_len
26877 = write_debug_names (objfile, out_file, out_file_str);
26878 assert_file_size (out_file, filename.c_str (), total_len);
26879
26880 /* We want to keep the file .debug_str file too. */
26881 unlink_file_str.keep ();
26882 }
26883 else
26884 {
26885 const size_t total_len
26886 = write_gdbindex (objfile, out_file);
26887 assert_file_size (out_file, filename.c_str (), total_len);
26888 }
26889
bef155c3
TT
26890 /* We want to keep the file. */
26891 unlink_file.keep ();
9291a0cd
TT
26892}
26893
90476074
TT
26894/* Implementation of the `save gdb-index' command.
26895
437afbb8
JK
26896 Note that the .gdb_index file format used by this command is
26897 documented in the GDB manual. Any changes here must be documented
26898 there. */
11570e71 26899
9291a0cd 26900static void
8384c356 26901save_gdb_index_command (const char *arg, int from_tty)
9291a0cd
TT
26902{
26903 struct objfile *objfile;
437afbb8
JK
26904 const char dwarf5space[] = "-dwarf-5 ";
26905 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
26906
26907 if (!arg)
26908 arg = "";
26909
26910 arg = skip_spaces (arg);
26911 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
26912 {
26913 index_kind = dw_index_kind::DEBUG_NAMES;
26914 arg += strlen (dwarf5space);
26915 arg = skip_spaces (arg);
26916 }
9291a0cd 26917
437afbb8
JK
26918 if (!*arg)
26919 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
9291a0cd
TT
26920
26921 ALL_OBJFILES (objfile)
26922 {
26923 struct stat st;
26924
26925 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 26926 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
26927 continue;
26928
9a3c8263
SM
26929 dwarf2_per_objfile
26930 = (struct dwarf2_per_objfile *) objfile_data (objfile,
26931 dwarf2_objfile_data_key);
9291a0cd
TT
26932 if (dwarf2_per_objfile)
26933 {
9291a0cd 26934
492d29ea 26935 TRY
9291a0cd 26936 {
437afbb8 26937 write_psymtabs_to_index (objfile, arg, index_kind);
9291a0cd 26938 }
492d29ea
PA
26939 CATCH (except, RETURN_MASK_ERROR)
26940 {
26941 exception_fprintf (gdb_stderr, except,
26942 _("Error while writing index for `%s': "),
26943 objfile_name (objfile));
26944 }
26945 END_CATCH
9291a0cd
TT
26946 }
26947 }
dce234bc
PP
26948}
26949
9291a0cd
TT
26950\f
26951
b4f54984 26952int dwarf_always_disassemble;
9eae7c52
TT
26953
26954static void
b4f54984
DE
26955show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26956 struct cmd_list_element *c, const char *value)
9eae7c52 26957{
3e43a32a
MS
26958 fprintf_filtered (file,
26959 _("Whether to always disassemble "
26960 "DWARF expressions is %s.\n"),
9eae7c52
TT
26961 value);
26962}
26963
900e11f9
JK
26964static void
26965show_check_physname (struct ui_file *file, int from_tty,
26966 struct cmd_list_element *c, const char *value)
26967{
26968 fprintf_filtered (file,
26969 _("Whether to check \"physname\" is %s.\n"),
26970 value);
26971}
26972
6502dd73
DJ
26973void
26974_initialize_dwarf2_read (void)
26975{
96d19272
JK
26976 struct cmd_list_element *c;
26977
dce234bc 26978 dwarf2_objfile_data_key
c1bd65d0 26979 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 26980
b4f54984
DE
26981 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26982Set DWARF specific variables.\n\
26983Configure DWARF variables such as the cache size"),
26984 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
26985 0/*allow-unknown*/, &maintenance_set_cmdlist);
26986
b4f54984
DE
26987 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
26988Show DWARF specific variables\n\
26989Show DWARF variables such as the cache size"),
26990 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
26991 0/*allow-unknown*/, &maintenance_show_cmdlist);
26992
26993 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
26994 &dwarf_max_cache_age, _("\
26995Set the upper bound on the age of cached DWARF compilation units."), _("\
26996Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
26997A higher limit means that cached compilation units will be stored\n\
26998in memory longer, and more total memory will be used. Zero disables\n\
26999caching, which can slow down startup."),
2c5b56ce 27000 NULL,
b4f54984
DE
27001 show_dwarf_max_cache_age,
27002 &set_dwarf_cmdlist,
27003 &show_dwarf_cmdlist);
d97bc12b 27004
9eae7c52 27005 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 27006 &dwarf_always_disassemble, _("\
9eae7c52
TT
27007Set whether `info address' always disassembles DWARF expressions."), _("\
27008Show whether `info address' always disassembles DWARF expressions."), _("\
27009When enabled, DWARF expressions are always printed in an assembly-like\n\
27010syntax. When disabled, expressions will be printed in a more\n\
27011conversational style, when possible."),
27012 NULL,
b4f54984
DE
27013 show_dwarf_always_disassemble,
27014 &set_dwarf_cmdlist,
27015 &show_dwarf_cmdlist);
27016
27017 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27018Set debugging of the DWARF reader."), _("\
27019Show debugging of the DWARF reader."), _("\
27020When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
27021reading and symtab expansion. A value of 1 (one) provides basic\n\
27022information. A value greater than 1 provides more verbose information."),
45cfd468
DE
27023 NULL,
27024 NULL,
27025 &setdebuglist, &showdebuglist);
27026
b4f54984
DE
27027 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27028Set debugging of the DWARF DIE reader."), _("\
27029Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
27030When enabled (non-zero), DIEs are dumped after they are read in.\n\
27031The value is the maximum depth to print."),
ccce17b0
YQ
27032 NULL,
27033 NULL,
27034 &setdebuglist, &showdebuglist);
9291a0cd 27035
27e0867f
DE
27036 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27037Set debugging of the dwarf line reader."), _("\
27038Show debugging of the dwarf line reader."), _("\
27039When enabled (non-zero), line number entries are dumped as they are read in.\n\
27040A value of 1 (one) provides basic information.\n\
27041A value greater than 1 provides more verbose information."),
27042 NULL,
27043 NULL,
27044 &setdebuglist, &showdebuglist);
27045
900e11f9
JK
27046 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27047Set cross-checking of \"physname\" code against demangler."), _("\
27048Show cross-checking of \"physname\" code against demangler."), _("\
27049When enabled, GDB's internal \"physname\" code is checked against\n\
27050the demangler."),
27051 NULL, show_check_physname,
27052 &setdebuglist, &showdebuglist);
27053
e615022a
DE
27054 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27055 no_class, &use_deprecated_index_sections, _("\
27056Set whether to use deprecated gdb_index sections."), _("\
27057Show whether to use deprecated gdb_index sections."), _("\
27058When enabled, deprecated .gdb_index sections are used anyway.\n\
27059Normally they are ignored either because of a missing feature or\n\
27060performance issue.\n\
27061Warning: This option must be enabled before gdb reads the file."),
27062 NULL,
27063 NULL,
27064 &setlist, &showlist);
27065
96d19272 27066 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 27067 _("\
fc1a9d6e 27068Save a gdb-index file.\n\
437afbb8
JK
27069Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27070\n\
27071No options create one file with .gdb-index extension for pre-DWARF-5\n\
27072compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27073extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
96d19272
JK
27074 &save_cmdlist);
27075 set_cmd_completer (c, filename_completer);
f1e6e072
TT
27076
27077 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27078 &dwarf2_locexpr_funcs);
27079 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27080 &dwarf2_loclist_funcs);
27081
27082 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27083 &dwarf2_block_frame_base_locexpr_funcs);
27084 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27085 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
27086
27087#if GDB_SELF_TEST
27088 selftests::register_test ("dw2_expand_symtabs_matching",
27089 selftests::dw2_expand_symtabs_matching::run_test);
27090#endif
6502dd73 27091}
This page took 5.426006 seconds and 4 git commands to generate.