Fix ChangeLog dates of previous commit
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
e2882c85 3 Copyright (C) 1994-2018 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270 55#include "typeprint.h"
ccefe4c4 56#include "psympriv.h"
53ce3c39 57#include <sys/stat.h>
96d19272 58#include "completer.h"
34eaf542 59#include "vec.h"
98bfdba5 60#include "c-lang.h"
a766d390 61#include "go-lang.h"
98bfdba5 62#include "valprint.h"
3019eac3 63#include "gdbcore.h" /* for gnutarget */
156942c7 64#include "gdb/gdb-index.h"
60d5a603 65#include <ctype.h>
cbb099e8 66#include "gdb_bfd.h"
4357ac6c 67#include "f-lang.h"
05cba821 68#include "source.h"
614c279d 69#include "filestuff.h"
dc294be5 70#include "build-id.h"
22cee43f 71#include "namespace.h"
bef155c3 72#include "common/gdb_unlinker.h"
14bc53a8 73#include "common/function-view.h"
ecfb656c
PA
74#include "common/gdb_optional.h"
75#include "common/underlying.h"
d5722aa2 76#include "common/byte-vector.h"
927aa2e7 77#include "common/hash_enum.h"
bbf2f4df 78#include "filename-seen-cache.h"
b32b108a 79#include "producer.h"
c906108c 80#include <fcntl.h>
c906108c 81#include <sys/types.h>
325fac50 82#include <algorithm>
bc8f2430
JK
83#include <unordered_set>
84#include <unordered_map>
c62446b1 85#include "selftest.h"
437afbb8
JK
86#include <cmath>
87#include <set>
88#include <forward_list>
89
90typedef struct symbol *symbolp;
91DEF_VEC_P (symbolp);
d8151005 92
73be47f5
DE
93/* When == 1, print basic high level tracing messages.
94 When > 1, be more verbose.
b4f54984
DE
95 This is in contrast to the low level DIE reading of dwarf_die_debug. */
96static unsigned int dwarf_read_debug = 0;
45cfd468 97
d97bc12b 98/* When non-zero, dump DIEs after they are read in. */
b4f54984 99static unsigned int dwarf_die_debug = 0;
d97bc12b 100
27e0867f
DE
101/* When non-zero, dump line number entries as they are read in. */
102static unsigned int dwarf_line_debug = 0;
103
900e11f9
JK
104/* When non-zero, cross-check physname against demangler. */
105static int check_physname = 0;
106
481860b3 107/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 108static int use_deprecated_index_sections = 0;
481860b3 109
6502dd73
DJ
110static const struct objfile_data *dwarf2_objfile_data_key;
111
f1e6e072
TT
112/* The "aclass" indices for various kinds of computed DWARF symbols. */
113
114static int dwarf2_locexpr_index;
115static int dwarf2_loclist_index;
116static int dwarf2_locexpr_block_index;
117static int dwarf2_loclist_block_index;
118
73869dc2
DE
119/* A descriptor for dwarf sections.
120
121 S.ASECTION, SIZE are typically initialized when the objfile is first
122 scanned. BUFFER, READIN are filled in later when the section is read.
123 If the section contained compressed data then SIZE is updated to record
124 the uncompressed size of the section.
125
126 DWP file format V2 introduces a wrinkle that is easiest to handle by
127 creating the concept of virtual sections contained within a real section.
128 In DWP V2 the sections of the input DWO files are concatenated together
129 into one section, but section offsets are kept relative to the original
130 input section.
131 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
132 the real section this "virtual" section is contained in, and BUFFER,SIZE
133 describe the virtual section. */
134
dce234bc
PP
135struct dwarf2_section_info
136{
73869dc2
DE
137 union
138 {
e5aa3347 139 /* If this is a real section, the bfd section. */
049412e3 140 asection *section;
73869dc2 141 /* If this is a virtual section, pointer to the containing ("real")
e5aa3347 142 section. */
73869dc2
DE
143 struct dwarf2_section_info *containing_section;
144 } s;
19ac8c2e 145 /* Pointer to section data, only valid if readin. */
d521ce57 146 const gdb_byte *buffer;
73869dc2 147 /* The size of the section, real or virtual. */
dce234bc 148 bfd_size_type size;
73869dc2
DE
149 /* If this is a virtual section, the offset in the real section.
150 Only valid if is_virtual. */
151 bfd_size_type virtual_offset;
be391dca 152 /* True if we have tried to read this section. */
73869dc2
DE
153 char readin;
154 /* True if this is a virtual section, False otherwise.
049412e3 155 This specifies which of s.section and s.containing_section to use. */
73869dc2 156 char is_virtual;
dce234bc
PP
157};
158
8b70b953
TT
159typedef struct dwarf2_section_info dwarf2_section_info_def;
160DEF_VEC_O (dwarf2_section_info_def);
161
9291a0cd
TT
162/* All offsets in the index are of this type. It must be
163 architecture-independent. */
164typedef uint32_t offset_type;
165
166DEF_VEC_I (offset_type);
167
156942c7
DE
168/* Ensure only legit values are used. */
169#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
170 do { \
171 gdb_assert ((unsigned int) (value) <= 1); \
172 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
173 } while (0)
174
175/* Ensure only legit values are used. */
176#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
177 do { \
178 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
179 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
180 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
181 } while (0)
182
183/* Ensure we don't use more than the alloted nuber of bits for the CU. */
184#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
185 do { \
186 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
187 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
188 } while (0)
189
3f563c84
PA
190#if WORDS_BIGENDIAN
191
192/* Convert VALUE between big- and little-endian. */
193
194static offset_type
195byte_swap (offset_type value)
196{
197 offset_type result;
198
199 result = (value & 0xff) << 24;
200 result |= (value & 0xff00) << 8;
201 result |= (value & 0xff0000) >> 8;
202 result |= (value & 0xff000000) >> 24;
203 return result;
204}
205
206#define MAYBE_SWAP(V) byte_swap (V)
207
208#else
209#define MAYBE_SWAP(V) static_cast<offset_type> (V)
210#endif /* WORDS_BIGENDIAN */
211
212/* An index into a (C++) symbol name component in a symbol name as
213 recorded in the mapped_index's symbol table. For each C++ symbol
214 in the symbol table, we record one entry for the start of each
215 component in the symbol in a table of name components, and then
216 sort the table, in order to be able to binary search symbol names,
217 ignoring leading namespaces, both completion and regular look up.
218 For example, for symbol "A::B::C", we'll have an entry that points
219 to "A::B::C", another that points to "B::C", and another for "C".
220 Note that function symbols in GDB index have no parameter
221 information, just the function/method names. You can convert a
222 name_component to a "const char *" using the
223 'mapped_index::symbol_name_at(offset_type)' method. */
224
225struct name_component
226{
227 /* Offset in the symbol name where the component starts. Stored as
228 a (32-bit) offset instead of a pointer to save memory and improve
229 locality on 64-bit architectures. */
230 offset_type name_offset;
231
232 /* The symbol's index in the symbol and constant pool tables of a
233 mapped_index. */
234 offset_type idx;
235};
236
44ed8f3e
PA
237/* Base class containing bits shared by both .gdb_index and
238 .debug_name indexes. */
239
240struct mapped_index_base
241{
242 /* The name_component table (a sorted vector). See name_component's
243 description above. */
244 std::vector<name_component> name_components;
245
246 /* How NAME_COMPONENTS is sorted. */
247 enum case_sensitivity name_components_casing;
248
249 /* Return the number of names in the symbol table. */
250 virtual size_t symbol_name_count () const = 0;
251
252 /* Get the name of the symbol at IDX in the symbol table. */
253 virtual const char *symbol_name_at (offset_type idx) const = 0;
254
255 /* Return whether the name at IDX in the symbol table should be
256 ignored. */
257 virtual bool symbol_name_slot_invalid (offset_type idx) const
258 {
259 return false;
260 }
261
262 /* Build the symbol name component sorted vector, if we haven't
263 yet. */
264 void build_name_components ();
265
266 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
267 possible matches for LN_NO_PARAMS in the name component
268 vector. */
269 std::pair<std::vector<name_component>::const_iterator,
270 std::vector<name_component>::const_iterator>
271 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
272
273 /* Prevent deleting/destroying via a base class pointer. */
274protected:
275 ~mapped_index_base() = default;
276};
277
9291a0cd
TT
278/* A description of the mapped index. The file format is described in
279 a comment by the code that writes the index. */
fc898b42 280struct mapped_index final : public mapped_index_base
9291a0cd 281{
f00a2de2
PA
282 /* A slot/bucket in the symbol table hash. */
283 struct symbol_table_slot
284 {
285 const offset_type name;
286 const offset_type vec;
287 };
288
559a7a62
JK
289 /* Index data format version. */
290 int version;
291
9291a0cd
TT
292 /* The total length of the buffer. */
293 off_t total_size;
b11b1f88 294
f00a2de2
PA
295 /* The address table data. */
296 gdb::array_view<const gdb_byte> address_table;
b11b1f88 297
3876f04e 298 /* The symbol table, implemented as a hash table. */
f00a2de2 299 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 300
9291a0cd
TT
301 /* A pointer to the constant pool. */
302 const char *constant_pool;
3f563c84 303
44ed8f3e
PA
304 bool symbol_name_slot_invalid (offset_type idx) const override
305 {
306 const auto &bucket = this->symbol_table[idx];
307 return bucket.name == 0 && bucket.vec;
308 }
5c58de74 309
3f563c84
PA
310 /* Convenience method to get at the name of the symbol at IDX in the
311 symbol table. */
44ed8f3e 312 const char *symbol_name_at (offset_type idx) const override
f00a2de2 313 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 314
44ed8f3e
PA
315 size_t symbol_name_count () const override
316 { return this->symbol_table.size (); }
9291a0cd
TT
317};
318
927aa2e7
JK
319/* A description of the mapped .debug_names.
320 Uninitialized map has CU_COUNT 0. */
fc898b42 321struct mapped_debug_names final : public mapped_index_base
927aa2e7 322{
ed2dc618
SM
323 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
324 : dwarf2_per_objfile (dwarf2_per_objfile_)
325 {}
326
327 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
328 bfd_endian dwarf5_byte_order;
329 bool dwarf5_is_dwarf64;
330 bool augmentation_is_gdb;
331 uint8_t offset_size;
332 uint32_t cu_count = 0;
333 uint32_t tu_count, bucket_count, name_count;
334 const gdb_byte *cu_table_reordered, *tu_table_reordered;
335 const uint32_t *bucket_table_reordered, *hash_table_reordered;
336 const gdb_byte *name_table_string_offs_reordered;
337 const gdb_byte *name_table_entry_offs_reordered;
338 const gdb_byte *entry_pool;
339
340 struct index_val
341 {
342 ULONGEST dwarf_tag;
343 struct attr
344 {
345 /* Attribute name DW_IDX_*. */
346 ULONGEST dw_idx;
347
348 /* Attribute form DW_FORM_*. */
349 ULONGEST form;
350
351 /* Value if FORM is DW_FORM_implicit_const. */
352 LONGEST implicit_const;
353 };
354 std::vector<attr> attr_vec;
355 };
356
357 std::unordered_map<ULONGEST, index_val> abbrev_map;
358
359 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
360
361 /* Implementation of the mapped_index_base virtual interface, for
362 the name_components cache. */
363
364 const char *symbol_name_at (offset_type idx) const override
365 { return namei_to_name (idx); }
366
367 size_t symbol_name_count () const override
368 { return this->name_count; }
927aa2e7
JK
369};
370
95554aad
TT
371typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
372DEF_VEC_P (dwarf2_per_cu_ptr);
373
52059ffd
TT
374struct tu_stats
375{
376 int nr_uniq_abbrev_tables;
377 int nr_symtabs;
378 int nr_symtab_sharers;
379 int nr_stmt_less_type_units;
380 int nr_all_type_units_reallocs;
381};
382
9cdd5dbd
DE
383/* Collection of data recorded per objfile.
384 This hangs off of dwarf2_objfile_data_key. */
385
6502dd73
DJ
386struct dwarf2_per_objfile
387{
330cdd98
PA
388 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
389 dwarf2 section names, or is NULL if the standard ELF names are
390 used. */
391 dwarf2_per_objfile (struct objfile *objfile,
392 const dwarf2_debug_sections *names);
ae038cb0 393
330cdd98
PA
394 ~dwarf2_per_objfile ();
395
d6541620 396 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
330cdd98
PA
397
398 /* Free all cached compilation units. */
399 void free_cached_comp_units ();
400private:
401 /* This function is mapped across the sections and remembers the
402 offset and size of each of the debugging sections we are
403 interested in. */
404 void locate_sections (bfd *abfd, asection *sectp,
405 const dwarf2_debug_sections &names);
406
407public:
408 dwarf2_section_info info {};
409 dwarf2_section_info abbrev {};
410 dwarf2_section_info line {};
411 dwarf2_section_info loc {};
412 dwarf2_section_info loclists {};
413 dwarf2_section_info macinfo {};
414 dwarf2_section_info macro {};
415 dwarf2_section_info str {};
416 dwarf2_section_info line_str {};
417 dwarf2_section_info ranges {};
418 dwarf2_section_info rnglists {};
419 dwarf2_section_info addr {};
420 dwarf2_section_info frame {};
421 dwarf2_section_info eh_frame {};
422 dwarf2_section_info gdb_index {};
927aa2e7
JK
423 dwarf2_section_info debug_names {};
424 dwarf2_section_info debug_aranges {};
330cdd98
PA
425
426 VEC (dwarf2_section_info_def) *types = NULL;
8b70b953 427
be391dca 428 /* Back link. */
330cdd98 429 struct objfile *objfile = NULL;
be391dca 430
d467dd73 431 /* Table of all the compilation units. This is used to locate
10b3939b 432 the target compilation unit of a particular reference. */
330cdd98 433 struct dwarf2_per_cu_data **all_comp_units = NULL;
ae038cb0
DJ
434
435 /* The number of compilation units in ALL_COMP_UNITS. */
330cdd98 436 int n_comp_units = 0;
ae038cb0 437
1fd400ff 438 /* The number of .debug_types-related CUs. */
330cdd98 439 int n_type_units = 0;
1fd400ff 440
6aa5f3a6
DE
441 /* The number of elements allocated in all_type_units.
442 If there are skeleton-less TUs, we add them to all_type_units lazily. */
330cdd98 443 int n_allocated_type_units = 0;
6aa5f3a6 444
a2ce51a0
DE
445 /* The .debug_types-related CUs (TUs).
446 This is stored in malloc space because we may realloc it. */
330cdd98 447 struct signatured_type **all_type_units = NULL;
1fd400ff 448
f4dc4d17
DE
449 /* Table of struct type_unit_group objects.
450 The hash key is the DW_AT_stmt_list value. */
330cdd98 451 htab_t type_unit_groups {};
72dca2f5 452
348e048f
DE
453 /* A table mapping .debug_types signatures to its signatured_type entry.
454 This is NULL if the .debug_types section hasn't been read in yet. */
330cdd98 455 htab_t signatured_types {};
348e048f 456
f4dc4d17
DE
457 /* Type unit statistics, to see how well the scaling improvements
458 are doing. */
330cdd98 459 struct tu_stats tu_stats {};
f4dc4d17
DE
460
461 /* A chain of compilation units that are currently read in, so that
462 they can be freed later. */
330cdd98 463 dwarf2_per_cu_data *read_in_chain = NULL;
f4dc4d17 464
3019eac3
DE
465 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
466 This is NULL if the table hasn't been allocated yet. */
330cdd98 467 htab_t dwo_files {};
3019eac3 468
330cdd98
PA
469 /* True if we've checked for whether there is a DWP file. */
470 bool dwp_checked = false;
80626a55
DE
471
472 /* The DWP file if there is one, or NULL. */
330cdd98 473 struct dwp_file *dwp_file = NULL;
80626a55 474
36586728
TT
475 /* The shared '.dwz' file, if one exists. This is used when the
476 original data was compressed using 'dwz -m'. */
330cdd98 477 struct dwz_file *dwz_file = NULL;
36586728 478
330cdd98 479 /* A flag indicating whether this objfile has a section loaded at a
72dca2f5 480 VMA of 0. */
330cdd98 481 bool has_section_at_zero = false;
9291a0cd 482
ae2de4f8
DE
483 /* True if we are using the mapped index,
484 or we are faking it for OBJF_READNOW's sake. */
330cdd98 485 bool using_index = false;
9291a0cd 486
ae2de4f8 487 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
330cdd98 488 mapped_index *index_table = NULL;
98bfdba5 489
927aa2e7
JK
490 /* The mapped index, or NULL if .debug_names is missing or not being used. */
491 std::unique_ptr<mapped_debug_names> debug_names_table;
492
7b9f3c50 493 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
494 TUs typically share line table entries with a CU, so we maintain a
495 separate table of all line table entries to support the sharing.
496 Note that while there can be way more TUs than CUs, we've already
497 sorted all the TUs into "type unit groups", grouped by their
498 DW_AT_stmt_list value. Therefore the only sharing done here is with a
499 CU and its associated TU group if there is one. */
330cdd98 500 htab_t quick_file_names_table {};
7b9f3c50 501
98bfdba5
PA
502 /* Set during partial symbol reading, to prevent queueing of full
503 symbols. */
330cdd98 504 bool reading_partial_symbols = false;
673bfd45 505
dee91e82 506 /* Table mapping type DIEs to their struct type *.
673bfd45 507 This is NULL if not allocated yet.
02142a6c 508 The mapping is done via (CU/TU + DIE offset) -> type. */
330cdd98 509 htab_t die_type_hash {};
95554aad
TT
510
511 /* The CUs we recently read. */
330cdd98 512 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
527f3840
JK
513
514 /* Table containing line_header indexed by offset and offset_in_dwz. */
330cdd98 515 htab_t line_header_hash {};
bbf2f4df
PA
516
517 /* Table containing all filenames. This is an optional because the
518 table is lazily constructed on first access. */
519 gdb::optional<filename_seen_cache> filenames_cache;
6502dd73
DJ
520};
521
ed2dc618
SM
522/* Get the dwarf2_per_objfile associated to OBJFILE. */
523
524struct dwarf2_per_objfile *
525get_dwarf2_per_objfile (struct objfile *objfile)
526{
527 return ((struct dwarf2_per_objfile *)
528 objfile_data (objfile, dwarf2_objfile_data_key));
529}
530
531/* Set the dwarf2_per_objfile associated to OBJFILE. */
532
533void
534set_dwarf2_per_objfile (struct objfile *objfile,
535 struct dwarf2_per_objfile *dwarf2_per_objfile)
536{
537 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
538 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
539}
c906108c 540
251d32d9 541/* Default names of the debugging sections. */
c906108c 542
233a11ab
CS
543/* Note that if the debugging section has been compressed, it might
544 have a name like .zdebug_info. */
545
9cdd5dbd
DE
546static const struct dwarf2_debug_sections dwarf2_elf_names =
547{
251d32d9
TG
548 { ".debug_info", ".zdebug_info" },
549 { ".debug_abbrev", ".zdebug_abbrev" },
550 { ".debug_line", ".zdebug_line" },
551 { ".debug_loc", ".zdebug_loc" },
43988095 552 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 553 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 554 { ".debug_macro", ".zdebug_macro" },
251d32d9 555 { ".debug_str", ".zdebug_str" },
43988095 556 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 557 { ".debug_ranges", ".zdebug_ranges" },
43988095 558 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 559 { ".debug_types", ".zdebug_types" },
3019eac3 560 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
561 { ".debug_frame", ".zdebug_frame" },
562 { ".eh_frame", NULL },
24d3216f 563 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
564 { ".debug_names", ".zdebug_names" },
565 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 566 23
251d32d9 567};
c906108c 568
80626a55 569/* List of DWO/DWP sections. */
3019eac3 570
80626a55 571static const struct dwop_section_names
3019eac3
DE
572{
573 struct dwarf2_section_names abbrev_dwo;
574 struct dwarf2_section_names info_dwo;
575 struct dwarf2_section_names line_dwo;
576 struct dwarf2_section_names loc_dwo;
43988095 577 struct dwarf2_section_names loclists_dwo;
09262596
DE
578 struct dwarf2_section_names macinfo_dwo;
579 struct dwarf2_section_names macro_dwo;
3019eac3
DE
580 struct dwarf2_section_names str_dwo;
581 struct dwarf2_section_names str_offsets_dwo;
582 struct dwarf2_section_names types_dwo;
80626a55
DE
583 struct dwarf2_section_names cu_index;
584 struct dwarf2_section_names tu_index;
3019eac3 585}
80626a55 586dwop_section_names =
3019eac3
DE
587{
588 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
589 { ".debug_info.dwo", ".zdebug_info.dwo" },
590 { ".debug_line.dwo", ".zdebug_line.dwo" },
591 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 592 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
593 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
594 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
595 { ".debug_str.dwo", ".zdebug_str.dwo" },
596 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
597 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
598 { ".debug_cu_index", ".zdebug_cu_index" },
599 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
600};
601
c906108c
SS
602/* local data types */
603
107d2387
AC
604/* The data in a compilation unit header, after target2host
605 translation, looks like this. */
c906108c 606struct comp_unit_head
a738430d 607{
c764a876 608 unsigned int length;
a738430d 609 short version;
a738430d
MK
610 unsigned char addr_size;
611 unsigned char signed_addr_p;
9c541725 612 sect_offset abbrev_sect_off;
57349743 613
a738430d
MK
614 /* Size of file offsets; either 4 or 8. */
615 unsigned int offset_size;
57349743 616
a738430d
MK
617 /* Size of the length field; either 4 or 12. */
618 unsigned int initial_length_size;
57349743 619
43988095
JK
620 enum dwarf_unit_type unit_type;
621
a738430d
MK
622 /* Offset to the first byte of this compilation unit header in the
623 .debug_info section, for resolving relative reference dies. */
9c541725 624 sect_offset sect_off;
57349743 625
d00adf39
DE
626 /* Offset to first die in this cu from the start of the cu.
627 This will be the first byte following the compilation unit header. */
9c541725 628 cu_offset first_die_cu_offset;
43988095
JK
629
630 /* 64-bit signature of this type unit - it is valid only for
631 UNIT_TYPE DW_UT_type. */
632 ULONGEST signature;
633
634 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 635 cu_offset type_cu_offset_in_tu;
a738430d 636};
c906108c 637
3da10d80
KS
638/* Type used for delaying computation of method physnames.
639 See comments for compute_delayed_physnames. */
640struct delayed_method_info
641{
642 /* The type to which the method is attached, i.e., its parent class. */
643 struct type *type;
644
645 /* The index of the method in the type's function fieldlists. */
646 int fnfield_index;
647
648 /* The index of the method in the fieldlist. */
649 int index;
650
651 /* The name of the DIE. */
652 const char *name;
653
654 /* The DIE associated with this method. */
655 struct die_info *die;
656};
657
658typedef struct delayed_method_info delayed_method_info;
659DEF_VEC_O (delayed_method_info);
660
e7c27a73
DJ
661/* Internal state when decoding a particular compilation unit. */
662struct dwarf2_cu
663{
d00adf39 664 /* The header of the compilation unit. */
e7c27a73 665 struct comp_unit_head header;
e142c38c 666
d00adf39
DE
667 /* Base address of this compilation unit. */
668 CORE_ADDR base_address;
669
670 /* Non-zero if base_address has been set. */
671 int base_known;
672
e142c38c
DJ
673 /* The language we are debugging. */
674 enum language language;
675 const struct language_defn *language_defn;
676
b0f35d58
DL
677 const char *producer;
678
e142c38c
DJ
679 /* The generic symbol table building routines have separate lists for
680 file scope symbols and all all other scopes (local scopes). So
681 we need to select the right one to pass to add_symbol_to_list().
682 We do it by keeping a pointer to the correct list in list_in_scope.
683
684 FIXME: The original dwarf code just treated the file scope as the
685 first local scope, and all other local scopes as nested local
686 scopes, and worked fine. Check to see if we really need to
687 distinguish these in buildsym.c. */
688 struct pending **list_in_scope;
689
433df2d4
DE
690 /* The abbrev table for this CU.
691 Normally this points to the abbrev table in the objfile.
692 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
693 struct abbrev_table *abbrev_table;
72bf9492 694
b64f50a1
JK
695 /* Hash table holding all the loaded partial DIEs
696 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
697 htab_t partial_dies;
698
699 /* Storage for things with the same lifetime as this read-in compilation
700 unit, including partial DIEs. */
701 struct obstack comp_unit_obstack;
702
ae038cb0
DJ
703 /* When multiple dwarf2_cu structures are living in memory, this field
704 chains them all together, so that they can be released efficiently.
705 We will probably also want a generation counter so that most-recently-used
706 compilation units are cached... */
707 struct dwarf2_per_cu_data *read_in_chain;
708
69d751e3 709 /* Backlink to our per_cu entry. */
ae038cb0
DJ
710 struct dwarf2_per_cu_data *per_cu;
711
712 /* How many compilation units ago was this CU last referenced? */
713 int last_used;
714
b64f50a1
JK
715 /* A hash table of DIE cu_offset for following references with
716 die_info->offset.sect_off as hash. */
51545339 717 htab_t die_hash;
10b3939b
DJ
718
719 /* Full DIEs if read in. */
720 struct die_info *dies;
721
722 /* A set of pointers to dwarf2_per_cu_data objects for compilation
723 units referenced by this one. Only set during full symbol processing;
724 partial symbol tables do not have dependencies. */
725 htab_t dependencies;
726
cb1df416
DJ
727 /* Header data from the line table, during full symbol processing. */
728 struct line_header *line_header;
4c8aa72d
PA
729 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
730 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
731 this is the DW_TAG_compile_unit die for this CU. We'll hold on
732 to the line header as long as this DIE is being processed. See
733 process_die_scope. */
734 die_info *line_header_die_owner;
cb1df416 735
3da10d80
KS
736 /* A list of methods which need to have physnames computed
737 after all type information has been read. */
738 VEC (delayed_method_info) *method_list;
739
96408a79
SA
740 /* To be copied to symtab->call_site_htab. */
741 htab_t call_site_htab;
742
034e5797
DE
743 /* Non-NULL if this CU came from a DWO file.
744 There is an invariant here that is important to remember:
745 Except for attributes copied from the top level DIE in the "main"
746 (or "stub") file in preparation for reading the DWO file
747 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
748 Either there isn't a DWO file (in which case this is NULL and the point
749 is moot), or there is and either we're not going to read it (in which
750 case this is NULL) or there is and we are reading it (in which case this
751 is non-NULL). */
3019eac3
DE
752 struct dwo_unit *dwo_unit;
753
754 /* The DW_AT_addr_base attribute if present, zero otherwise
755 (zero is a valid value though).
1dbab08b 756 Note this value comes from the Fission stub CU/TU's DIE. */
3019eac3
DE
757 ULONGEST addr_base;
758
2e3cf129
DE
759 /* The DW_AT_ranges_base attribute if present, zero otherwise
760 (zero is a valid value though).
1dbab08b 761 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 762 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
763 be used without needing to know whether DWO files are in use or not.
764 N.B. This does not apply to DW_AT_ranges appearing in
765 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
766 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
767 DW_AT_ranges_base *would* have to be applied, and we'd have to care
768 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
769 ULONGEST ranges_base;
770
ae038cb0
DJ
771 /* Mark used when releasing cached dies. */
772 unsigned int mark : 1;
773
8be455d7
JK
774 /* This CU references .debug_loc. See the symtab->locations_valid field.
775 This test is imperfect as there may exist optimized debug code not using
776 any location list and still facing inlining issues if handled as
777 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 778 unsigned int has_loclist : 1;
ba919b58 779
1b80a9fa
JK
780 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
781 if all the producer_is_* fields are valid. This information is cached
782 because profiling CU expansion showed excessive time spent in
783 producer_is_gxx_lt_4_6. */
ba919b58
TT
784 unsigned int checked_producer : 1;
785 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 786 unsigned int producer_is_gcc_lt_4_3 : 1;
5230b05a 787 unsigned int producer_is_icc_lt_14 : 1;
4d4ec4e5
TT
788
789 /* When set, the file that we're processing is known to have
790 debugging info for C++ namespaces. GCC 3.3.x did not produce
791 this information, but later versions do. */
792
793 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
794};
795
10b3939b
DJ
796/* Persistent data held for a compilation unit, even when not
797 processing it. We put a pointer to this structure in the
28dee7f5 798 read_symtab_private field of the psymtab. */
10b3939b 799
ae038cb0
DJ
800struct dwarf2_per_cu_data
801{
36586728 802 /* The start offset and length of this compilation unit.
45452591 803 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
804 initial_length_size.
805 If the DIE refers to a DWO file, this is always of the original die,
806 not the DWO file. */
9c541725 807 sect_offset sect_off;
36586728 808 unsigned int length;
ae038cb0 809
43988095
JK
810 /* DWARF standard version this data has been read from (such as 4 or 5). */
811 short dwarf_version;
812
ae038cb0
DJ
813 /* Flag indicating this compilation unit will be read in before
814 any of the current compilation units are processed. */
c764a876 815 unsigned int queued : 1;
ae038cb0 816
0d99eb77
DE
817 /* This flag will be set when reading partial DIEs if we need to load
818 absolutely all DIEs for this compilation unit, instead of just the ones
819 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
820 hash table and don't find it. */
821 unsigned int load_all_dies : 1;
822
0186c6a7
DE
823 /* Non-zero if this CU is from .debug_types.
824 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
825 this is non-zero. */
3019eac3
DE
826 unsigned int is_debug_types : 1;
827
36586728
TT
828 /* Non-zero if this CU is from the .dwz file. */
829 unsigned int is_dwz : 1;
830
a2ce51a0
DE
831 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
832 This flag is only valid if is_debug_types is true.
833 We can't read a CU directly from a DWO file: There are required
834 attributes in the stub. */
835 unsigned int reading_dwo_directly : 1;
836
7ee85ab1
DE
837 /* Non-zero if the TU has been read.
838 This is used to assist the "Stay in DWO Optimization" for Fission:
839 When reading a DWO, it's faster to read TUs from the DWO instead of
840 fetching them from random other DWOs (due to comdat folding).
841 If the TU has already been read, the optimization is unnecessary
842 (and unwise - we don't want to change where gdb thinks the TU lives
843 "midflight").
844 This flag is only valid if is_debug_types is true. */
845 unsigned int tu_read : 1;
846
3019eac3
DE
847 /* The section this CU/TU lives in.
848 If the DIE refers to a DWO file, this is always the original die,
849 not the DWO file. */
8a0459fd 850 struct dwarf2_section_info *section;
348e048f 851
17ea53c3 852 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
853 of the CU cache it gets reset to NULL again. This is left as NULL for
854 dummy CUs (a CU header, but nothing else). */
ae038cb0 855 struct dwarf2_cu *cu;
1c379e20 856
e3b94546
SM
857 /* The corresponding dwarf2_per_objfile. */
858 struct dwarf2_per_objfile *dwarf2_per_objfile;
9291a0cd 859
fffbe6a8
YQ
860 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
861 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
862 union
863 {
864 /* The partial symbol table associated with this compilation unit,
95554aad 865 or NULL for unread partial units. */
9291a0cd
TT
866 struct partial_symtab *psymtab;
867
868 /* Data needed by the "quick" functions. */
869 struct dwarf2_per_cu_quick_data *quick;
870 } v;
95554aad 871
796a7ff8
DE
872 /* The CUs we import using DW_TAG_imported_unit. This is filled in
873 while reading psymtabs, used to compute the psymtab dependencies,
874 and then cleared. Then it is filled in again while reading full
875 symbols, and only deleted when the objfile is destroyed.
876
877 This is also used to work around a difference between the way gold
878 generates .gdb_index version <=7 and the way gdb does. Arguably this
879 is a gold bug. For symbols coming from TUs, gold records in the index
880 the CU that includes the TU instead of the TU itself. This breaks
881 dw2_lookup_symbol: It assumes that if the index says symbol X lives
882 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
883 will find X. Alas TUs live in their own symtab, so after expanding CU Y
884 we need to look in TU Z to find X. Fortunately, this is akin to
885 DW_TAG_imported_unit, so we just use the same mechanism: For
886 .gdb_index version <=7 this also records the TUs that the CU referred
887 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
888 indices so we only pay a price for gold generated indices.
889 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 890 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
891};
892
348e048f
DE
893/* Entry in the signatured_types hash table. */
894
895struct signatured_type
896{
42e7ad6c 897 /* The "per_cu" object of this type.
ac9ec31b 898 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
899 N.B.: This is the first member so that it's easy to convert pointers
900 between them. */
901 struct dwarf2_per_cu_data per_cu;
902
3019eac3 903 /* The type's signature. */
348e048f
DE
904 ULONGEST signature;
905
3019eac3 906 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
907 If this TU is a DWO stub and the definition lives in a DWO file
908 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
909 cu_offset type_offset_in_tu;
910
911 /* Offset in the section of the type's DIE.
912 If the definition lives in a DWO file, this is the offset in the
913 .debug_types.dwo section.
914 The value is zero until the actual value is known.
915 Zero is otherwise not a valid section offset. */
916 sect_offset type_offset_in_section;
0186c6a7
DE
917
918 /* Type units are grouped by their DW_AT_stmt_list entry so that they
919 can share them. This points to the containing symtab. */
920 struct type_unit_group *type_unit_group;
ac9ec31b
DE
921
922 /* The type.
923 The first time we encounter this type we fully read it in and install it
924 in the symbol tables. Subsequent times we only need the type. */
925 struct type *type;
a2ce51a0
DE
926
927 /* Containing DWO unit.
928 This field is valid iff per_cu.reading_dwo_directly. */
929 struct dwo_unit *dwo_unit;
348e048f
DE
930};
931
0186c6a7
DE
932typedef struct signatured_type *sig_type_ptr;
933DEF_VEC_P (sig_type_ptr);
934
094b34ac
DE
935/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
936 This includes type_unit_group and quick_file_names. */
937
938struct stmt_list_hash
939{
940 /* The DWO unit this table is from or NULL if there is none. */
941 struct dwo_unit *dwo_unit;
942
943 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 944 sect_offset line_sect_off;
094b34ac
DE
945};
946
f4dc4d17
DE
947/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
948 an object of this type. */
949
950struct type_unit_group
951{
0186c6a7 952 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
953 To simplify things we create an artificial CU that "includes" all the
954 type units using this stmt_list so that the rest of the code still has
955 a "per_cu" handle on the symtab.
956 This PER_CU is recognized by having no section. */
8a0459fd 957#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
958 struct dwarf2_per_cu_data per_cu;
959
0186c6a7
DE
960 /* The TUs that share this DW_AT_stmt_list entry.
961 This is added to while parsing type units to build partial symtabs,
962 and is deleted afterwards and not used again. */
963 VEC (sig_type_ptr) *tus;
f4dc4d17 964
43f3e411 965 /* The compunit symtab.
094b34ac 966 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
967 so we create an essentially anonymous symtab as the compunit symtab. */
968 struct compunit_symtab *compunit_symtab;
f4dc4d17 969
094b34ac
DE
970 /* The data used to construct the hash key. */
971 struct stmt_list_hash hash;
f4dc4d17
DE
972
973 /* The number of symtabs from the line header.
974 The value here must match line_header.num_file_names. */
975 unsigned int num_symtabs;
976
977 /* The symbol tables for this TU (obtained from the files listed in
978 DW_AT_stmt_list).
979 WARNING: The order of entries here must match the order of entries
980 in the line header. After the first TU using this type_unit_group, the
981 line header for the subsequent TUs is recreated from this. This is done
982 because we need to use the same symtabs for each TU using the same
983 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
984 there's no guarantee the line header doesn't have duplicate entries. */
985 struct symtab **symtabs;
986};
987
73869dc2 988/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
989
990struct dwo_sections
991{
992 struct dwarf2_section_info abbrev;
3019eac3
DE
993 struct dwarf2_section_info line;
994 struct dwarf2_section_info loc;
43988095 995 struct dwarf2_section_info loclists;
09262596
DE
996 struct dwarf2_section_info macinfo;
997 struct dwarf2_section_info macro;
3019eac3
DE
998 struct dwarf2_section_info str;
999 struct dwarf2_section_info str_offsets;
80626a55
DE
1000 /* In the case of a virtual DWO file, these two are unused. */
1001 struct dwarf2_section_info info;
3019eac3
DE
1002 VEC (dwarf2_section_info_def) *types;
1003};
1004
c88ee1f0 1005/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
1006
1007struct dwo_unit
1008{
1009 /* Backlink to the containing struct dwo_file. */
1010 struct dwo_file *dwo_file;
1011
1012 /* The "id" that distinguishes this CU/TU.
1013 .debug_info calls this "dwo_id", .debug_types calls this "signature".
1014 Since signatures came first, we stick with it for consistency. */
1015 ULONGEST signature;
1016
1017 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 1018 struct dwarf2_section_info *section;
3019eac3 1019
9c541725
PA
1020 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
1021 sect_offset sect_off;
3019eac3
DE
1022 unsigned int length;
1023
1024 /* For types, offset in the type's DIE of the type defined by this TU. */
1025 cu_offset type_offset_in_tu;
1026};
1027
73869dc2
DE
1028/* include/dwarf2.h defines the DWP section codes.
1029 It defines a max value but it doesn't define a min value, which we
1030 use for error checking, so provide one. */
1031
1032enum dwp_v2_section_ids
1033{
1034 DW_SECT_MIN = 1
1035};
1036
80626a55 1037/* Data for one DWO file.
57d63ce2
DE
1038
1039 This includes virtual DWO files (a virtual DWO file is a DWO file as it
1040 appears in a DWP file). DWP files don't really have DWO files per se -
1041 comdat folding of types "loses" the DWO file they came from, and from
1042 a high level view DWP files appear to contain a mass of random types.
1043 However, to maintain consistency with the non-DWP case we pretend DWP
1044 files contain virtual DWO files, and we assign each TU with one virtual
1045 DWO file (generally based on the line and abbrev section offsets -
1046 a heuristic that seems to work in practice). */
3019eac3
DE
1047
1048struct dwo_file
1049{
0ac5b59e 1050 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
1051 For virtual DWO files the name is constructed from the section offsets
1052 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
1053 from related CU+TUs. */
0ac5b59e
DE
1054 const char *dwo_name;
1055
1056 /* The DW_AT_comp_dir attribute. */
1057 const char *comp_dir;
3019eac3 1058
80626a55
DE
1059 /* The bfd, when the file is open. Otherwise this is NULL.
1060 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1061 bfd *dbfd;
3019eac3 1062
73869dc2
DE
1063 /* The sections that make up this DWO file.
1064 Remember that for virtual DWO files in DWP V2, these are virtual
1065 sections (for lack of a better name). */
3019eac3
DE
1066 struct dwo_sections sections;
1067
33c5cd75
DB
1068 /* The CUs in the file.
1069 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1070 an extension to handle LLVM's Link Time Optimization output (where
1071 multiple source files may be compiled into a single object/dwo pair). */
1072 htab_t cus;
3019eac3
DE
1073
1074 /* Table of TUs in the file.
1075 Each element is a struct dwo_unit. */
1076 htab_t tus;
1077};
1078
80626a55
DE
1079/* These sections are what may appear in a DWP file. */
1080
1081struct dwp_sections
1082{
73869dc2 1083 /* These are used by both DWP version 1 and 2. */
80626a55
DE
1084 struct dwarf2_section_info str;
1085 struct dwarf2_section_info cu_index;
1086 struct dwarf2_section_info tu_index;
73869dc2
DE
1087
1088 /* These are only used by DWP version 2 files.
1089 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1090 sections are referenced by section number, and are not recorded here.
1091 In DWP version 2 there is at most one copy of all these sections, each
1092 section being (effectively) comprised of the concatenation of all of the
1093 individual sections that exist in the version 1 format.
1094 To keep the code simple we treat each of these concatenated pieces as a
1095 section itself (a virtual section?). */
1096 struct dwarf2_section_info abbrev;
1097 struct dwarf2_section_info info;
1098 struct dwarf2_section_info line;
1099 struct dwarf2_section_info loc;
1100 struct dwarf2_section_info macinfo;
1101 struct dwarf2_section_info macro;
1102 struct dwarf2_section_info str_offsets;
1103 struct dwarf2_section_info types;
80626a55
DE
1104};
1105
73869dc2
DE
1106/* These sections are what may appear in a virtual DWO file in DWP version 1.
1107 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 1108
73869dc2 1109struct virtual_v1_dwo_sections
80626a55
DE
1110{
1111 struct dwarf2_section_info abbrev;
1112 struct dwarf2_section_info line;
1113 struct dwarf2_section_info loc;
1114 struct dwarf2_section_info macinfo;
1115 struct dwarf2_section_info macro;
1116 struct dwarf2_section_info str_offsets;
1117 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 1118 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
1119 struct dwarf2_section_info info_or_types;
1120};
1121
73869dc2
DE
1122/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1123 In version 2, the sections of the DWO files are concatenated together
1124 and stored in one section of that name. Thus each ELF section contains
1125 several "virtual" sections. */
1126
1127struct virtual_v2_dwo_sections
1128{
1129 bfd_size_type abbrev_offset;
1130 bfd_size_type abbrev_size;
1131
1132 bfd_size_type line_offset;
1133 bfd_size_type line_size;
1134
1135 bfd_size_type loc_offset;
1136 bfd_size_type loc_size;
1137
1138 bfd_size_type macinfo_offset;
1139 bfd_size_type macinfo_size;
1140
1141 bfd_size_type macro_offset;
1142 bfd_size_type macro_size;
1143
1144 bfd_size_type str_offsets_offset;
1145 bfd_size_type str_offsets_size;
1146
1147 /* Each DWP hash table entry records one CU or one TU.
1148 That is recorded here, and copied to dwo_unit.section. */
1149 bfd_size_type info_or_types_offset;
1150 bfd_size_type info_or_types_size;
1151};
1152
80626a55
DE
1153/* Contents of DWP hash tables. */
1154
1155struct dwp_hash_table
1156{
73869dc2 1157 uint32_t version, nr_columns;
80626a55 1158 uint32_t nr_units, nr_slots;
73869dc2
DE
1159 const gdb_byte *hash_table, *unit_table;
1160 union
1161 {
1162 struct
1163 {
1164 const gdb_byte *indices;
1165 } v1;
1166 struct
1167 {
1168 /* This is indexed by column number and gives the id of the section
1169 in that column. */
1170#define MAX_NR_V2_DWO_SECTIONS \
1171 (1 /* .debug_info or .debug_types */ \
1172 + 1 /* .debug_abbrev */ \
1173 + 1 /* .debug_line */ \
1174 + 1 /* .debug_loc */ \
1175 + 1 /* .debug_str_offsets */ \
1176 + 1 /* .debug_macro or .debug_macinfo */)
1177 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1178 const gdb_byte *offsets;
1179 const gdb_byte *sizes;
1180 } v2;
1181 } section_pool;
80626a55
DE
1182};
1183
1184/* Data for one DWP file. */
1185
1186struct dwp_file
1187{
1188 /* Name of the file. */
1189 const char *name;
1190
73869dc2
DE
1191 /* File format version. */
1192 int version;
1193
93417882 1194 /* The bfd. */
80626a55
DE
1195 bfd *dbfd;
1196
1197 /* Section info for this file. */
1198 struct dwp_sections sections;
1199
57d63ce2 1200 /* Table of CUs in the file. */
80626a55
DE
1201 const struct dwp_hash_table *cus;
1202
1203 /* Table of TUs in the file. */
1204 const struct dwp_hash_table *tus;
1205
19ac8c2e
DE
1206 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1207 htab_t loaded_cus;
1208 htab_t loaded_tus;
80626a55 1209
73869dc2
DE
1210 /* Table to map ELF section numbers to their sections.
1211 This is only needed for the DWP V1 file format. */
80626a55
DE
1212 unsigned int num_sections;
1213 asection **elf_sections;
1214};
1215
36586728
TT
1216/* This represents a '.dwz' file. */
1217
1218struct dwz_file
1219{
1220 /* A dwz file can only contain a few sections. */
1221 struct dwarf2_section_info abbrev;
1222 struct dwarf2_section_info info;
1223 struct dwarf2_section_info str;
1224 struct dwarf2_section_info line;
1225 struct dwarf2_section_info macro;
2ec9a5e0 1226 struct dwarf2_section_info gdb_index;
927aa2e7 1227 struct dwarf2_section_info debug_names;
36586728
TT
1228
1229 /* The dwz's BFD. */
1230 bfd *dwz_bfd;
1231};
1232
0963b4bd
MS
1233/* Struct used to pass misc. parameters to read_die_and_children, et
1234 al. which are used for both .debug_info and .debug_types dies.
1235 All parameters here are unchanging for the life of the call. This
dee91e82 1236 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1237
1238struct die_reader_specs
1239{
a32a8923 1240 /* The bfd of die_section. */
93311388
DE
1241 bfd* abfd;
1242
1243 /* The CU of the DIE we are parsing. */
1244 struct dwarf2_cu *cu;
1245
80626a55 1246 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1247 struct dwo_file *dwo_file;
1248
dee91e82 1249 /* The section the die comes from.
3019eac3 1250 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1251 struct dwarf2_section_info *die_section;
1252
1253 /* die_section->buffer. */
d521ce57 1254 const gdb_byte *buffer;
f664829e
DE
1255
1256 /* The end of the buffer. */
1257 const gdb_byte *buffer_end;
a2ce51a0
DE
1258
1259 /* The value of the DW_AT_comp_dir attribute. */
1260 const char *comp_dir;
93311388
DE
1261};
1262
fd820528 1263/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1264typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1265 const gdb_byte *info_ptr,
dee91e82
DE
1266 struct die_info *comp_unit_die,
1267 int has_children,
1268 void *data);
1269
ecfb656c
PA
1270/* A 1-based directory index. This is a strong typedef to prevent
1271 accidentally using a directory index as a 0-based index into an
1272 array/vector. */
1273enum class dir_index : unsigned int {};
1274
1275/* Likewise, a 1-based file name index. */
1276enum class file_name_index : unsigned int {};
1277
52059ffd
TT
1278struct file_entry
1279{
fff8551c
PA
1280 file_entry () = default;
1281
ecfb656c 1282 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
1283 unsigned int mod_time_, unsigned int length_)
1284 : name (name_),
ecfb656c 1285 d_index (d_index_),
fff8551c
PA
1286 mod_time (mod_time_),
1287 length (length_)
1288 {}
1289
ecfb656c
PA
1290 /* Return the include directory at D_INDEX stored in LH. Returns
1291 NULL if D_INDEX is out of bounds. */
8c43009f
PA
1292 const char *include_dir (const line_header *lh) const;
1293
fff8551c
PA
1294 /* The file name. Note this is an observing pointer. The memory is
1295 owned by debug_line_buffer. */
1296 const char *name {};
1297
8c43009f 1298 /* The directory index (1-based). */
ecfb656c 1299 dir_index d_index {};
fff8551c
PA
1300
1301 unsigned int mod_time {};
1302
1303 unsigned int length {};
1304
1305 /* True if referenced by the Line Number Program. */
1306 bool included_p {};
1307
83769d0b 1308 /* The associated symbol table, if any. */
fff8551c 1309 struct symtab *symtab {};
52059ffd
TT
1310};
1311
debd256d
JB
1312/* The line number information for a compilation unit (found in the
1313 .debug_line section) begins with a "statement program header",
1314 which contains the following information. */
1315struct line_header
1316{
fff8551c
PA
1317 line_header ()
1318 : offset_in_dwz {}
1319 {}
1320
1321 /* Add an entry to the include directory table. */
1322 void add_include_dir (const char *include_dir);
1323
1324 /* Add an entry to the file name table. */
ecfb656c 1325 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1326 unsigned int mod_time, unsigned int length);
1327
ecfb656c 1328 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 1329 is out of bounds. */
ecfb656c 1330 const char *include_dir_at (dir_index index) const
8c43009f 1331 {
ecfb656c
PA
1332 /* Convert directory index number (1-based) to vector index
1333 (0-based). */
1334 size_t vec_index = to_underlying (index) - 1;
1335
1336 if (vec_index >= include_dirs.size ())
8c43009f 1337 return NULL;
ecfb656c 1338 return include_dirs[vec_index];
8c43009f
PA
1339 }
1340
ecfb656c 1341 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 1342 is out of bounds. */
ecfb656c 1343 file_entry *file_name_at (file_name_index index)
8c43009f 1344 {
ecfb656c
PA
1345 /* Convert file name index number (1-based) to vector index
1346 (0-based). */
1347 size_t vec_index = to_underlying (index) - 1;
1348
1349 if (vec_index >= file_names.size ())
fff8551c 1350 return NULL;
ecfb656c 1351 return &file_names[vec_index];
fff8551c
PA
1352 }
1353
1354 /* Const version of the above. */
1355 const file_entry *file_name_at (unsigned int index) const
1356 {
1357 if (index >= file_names.size ())
8c43009f
PA
1358 return NULL;
1359 return &file_names[index];
1360 }
1361
527f3840 1362 /* Offset of line number information in .debug_line section. */
9c541725 1363 sect_offset sect_off {};
527f3840
JK
1364
1365 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1366 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1367
1368 unsigned int total_length {};
1369 unsigned short version {};
1370 unsigned int header_length {};
1371 unsigned char minimum_instruction_length {};
1372 unsigned char maximum_ops_per_instruction {};
1373 unsigned char default_is_stmt {};
1374 int line_base {};
1375 unsigned char line_range {};
1376 unsigned char opcode_base {};
debd256d
JB
1377
1378 /* standard_opcode_lengths[i] is the number of operands for the
1379 standard opcode whose value is i. This means that
1380 standard_opcode_lengths[0] is unused, and the last meaningful
1381 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1382 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1383
fff8551c
PA
1384 /* The include_directories table. Note these are observing
1385 pointers. The memory is owned by debug_line_buffer. */
1386 std::vector<const char *> include_dirs;
debd256d 1387
fff8551c
PA
1388 /* The file_names table. */
1389 std::vector<file_entry> file_names;
debd256d
JB
1390
1391 /* The start and end of the statement program following this
6502dd73 1392 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1393 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1394};
c906108c 1395
fff8551c
PA
1396typedef std::unique_ptr<line_header> line_header_up;
1397
8c43009f
PA
1398const char *
1399file_entry::include_dir (const line_header *lh) const
1400{
ecfb656c 1401 return lh->include_dir_at (d_index);
8c43009f
PA
1402}
1403
c906108c 1404/* When we construct a partial symbol table entry we only
0963b4bd 1405 need this much information. */
c906108c
SS
1406struct partial_die_info
1407 {
72bf9492 1408 /* Offset of this DIE. */
9c541725 1409 sect_offset sect_off;
72bf9492
DJ
1410
1411 /* DWARF-2 tag for this DIE. */
1412 ENUM_BITFIELD(dwarf_tag) tag : 16;
1413
72bf9492
DJ
1414 /* Assorted flags describing the data found in this DIE. */
1415 unsigned int has_children : 1;
1416 unsigned int is_external : 1;
1417 unsigned int is_declaration : 1;
1418 unsigned int has_type : 1;
1419 unsigned int has_specification : 1;
1420 unsigned int has_pc_info : 1;
481860b3 1421 unsigned int may_be_inlined : 1;
72bf9492 1422
0c1b455e
TT
1423 /* This DIE has been marked DW_AT_main_subprogram. */
1424 unsigned int main_subprogram : 1;
1425
72bf9492
DJ
1426 /* Flag set if the SCOPE field of this structure has been
1427 computed. */
1428 unsigned int scope_set : 1;
1429
fa4028e9
JB
1430 /* Flag set if the DIE has a byte_size attribute. */
1431 unsigned int has_byte_size : 1;
1432
ff908ebf
AW
1433 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1434 unsigned int has_const_value : 1;
1435
98bfdba5
PA
1436 /* Flag set if any of the DIE's children are template arguments. */
1437 unsigned int has_template_arguments : 1;
1438
abc72ce4
DE
1439 /* Flag set if fixup_partial_die has been called on this die. */
1440 unsigned int fixup_called : 1;
1441
36586728
TT
1442 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1443 unsigned int is_dwz : 1;
1444
1445 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1446 unsigned int spec_is_dwz : 1;
1447
72bf9492 1448 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1449 sometimes a default name for unnamed DIEs. */
15d034d0 1450 const char *name;
72bf9492 1451
abc72ce4
DE
1452 /* The linkage name, if present. */
1453 const char *linkage_name;
1454
72bf9492
DJ
1455 /* The scope to prepend to our children. This is generally
1456 allocated on the comp_unit_obstack, so will disappear
1457 when this compilation unit leaves the cache. */
15d034d0 1458 const char *scope;
72bf9492 1459
95554aad
TT
1460 /* Some data associated with the partial DIE. The tag determines
1461 which field is live. */
1462 union
1463 {
1464 /* The location description associated with this DIE, if any. */
1465 struct dwarf_block *locdesc;
1466 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1467 sect_offset sect_off;
95554aad 1468 } d;
72bf9492
DJ
1469
1470 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1471 CORE_ADDR lowpc;
1472 CORE_ADDR highpc;
72bf9492 1473
93311388 1474 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1475 DW_AT_sibling, if any. */
abc72ce4
DE
1476 /* NOTE: This member isn't strictly necessary, read_partial_die could
1477 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1478 const gdb_byte *sibling;
72bf9492
DJ
1479
1480 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1481 DW_AT_specification (or DW_AT_abstract_origin or
1482 DW_AT_extension). */
b64f50a1 1483 sect_offset spec_offset;
72bf9492
DJ
1484
1485 /* Pointers to this DIE's parent, first child, and next sibling,
1486 if any. */
1487 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1488 };
1489
0963b4bd 1490/* This data structure holds the information of an abbrev. */
c906108c
SS
1491struct abbrev_info
1492 {
1493 unsigned int number; /* number identifying abbrev */
1494 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1495 unsigned short has_children; /* boolean */
1496 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1497 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1498 struct abbrev_info *next; /* next in chain */
1499 };
1500
1501struct attr_abbrev
1502 {
9d25dd43
DE
1503 ENUM_BITFIELD(dwarf_attribute) name : 16;
1504 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1505
1506 /* It is valid only if FORM is DW_FORM_implicit_const. */
1507 LONGEST implicit_const;
c906108c
SS
1508 };
1509
433df2d4
DE
1510/* Size of abbrev_table.abbrev_hash_table. */
1511#define ABBREV_HASH_SIZE 121
1512
1513/* Top level data structure to contain an abbreviation table. */
1514
1515struct abbrev_table
1516{
f4dc4d17
DE
1517 /* Where the abbrev table came from.
1518 This is used as a sanity check when the table is used. */
9c541725 1519 sect_offset sect_off;
433df2d4
DE
1520
1521 /* Storage for the abbrev table. */
1522 struct obstack abbrev_obstack;
1523
1524 /* Hash table of abbrevs.
1525 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1526 It could be statically allocated, but the previous code didn't so we
1527 don't either. */
1528 struct abbrev_info **abbrevs;
1529};
1530
0963b4bd 1531/* Attributes have a name and a value. */
b60c80d6
DJ
1532struct attribute
1533 {
9d25dd43 1534 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1535 ENUM_BITFIELD(dwarf_form) form : 15;
1536
1537 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1538 field should be in u.str (existing only for DW_STRING) but it is kept
1539 here for better struct attribute alignment. */
1540 unsigned int string_is_canonical : 1;
1541
b60c80d6
DJ
1542 union
1543 {
15d034d0 1544 const char *str;
b60c80d6 1545 struct dwarf_block *blk;
43bbcdc2
PH
1546 ULONGEST unsnd;
1547 LONGEST snd;
b60c80d6 1548 CORE_ADDR addr;
ac9ec31b 1549 ULONGEST signature;
b60c80d6
DJ
1550 }
1551 u;
1552 };
1553
0963b4bd 1554/* This data structure holds a complete die structure. */
c906108c
SS
1555struct die_info
1556 {
76815b17
DE
1557 /* DWARF-2 tag for this DIE. */
1558 ENUM_BITFIELD(dwarf_tag) tag : 16;
1559
1560 /* Number of attributes */
98bfdba5
PA
1561 unsigned char num_attrs;
1562
1563 /* True if we're presently building the full type name for the
1564 type derived from this DIE. */
1565 unsigned char building_fullname : 1;
76815b17 1566
adde2bff
DE
1567 /* True if this die is in process. PR 16581. */
1568 unsigned char in_process : 1;
1569
76815b17
DE
1570 /* Abbrev number */
1571 unsigned int abbrev;
1572
93311388 1573 /* Offset in .debug_info or .debug_types section. */
9c541725 1574 sect_offset sect_off;
78ba4af6
JB
1575
1576 /* The dies in a compilation unit form an n-ary tree. PARENT
1577 points to this die's parent; CHILD points to the first child of
1578 this node; and all the children of a given node are chained
4950bc1c 1579 together via their SIBLING fields. */
639d11d3
DC
1580 struct die_info *child; /* Its first child, if any. */
1581 struct die_info *sibling; /* Its next sibling, if any. */
1582 struct die_info *parent; /* Its parent, if any. */
c906108c 1583
b60c80d6
DJ
1584 /* An array of attributes, with NUM_ATTRS elements. There may be
1585 zero, but it's not common and zero-sized arrays are not
1586 sufficiently portable C. */
1587 struct attribute attrs[1];
c906108c
SS
1588 };
1589
0963b4bd 1590/* Get at parts of an attribute structure. */
c906108c
SS
1591
1592#define DW_STRING(attr) ((attr)->u.str)
8285870a 1593#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1594#define DW_UNSND(attr) ((attr)->u.unsnd)
1595#define DW_BLOCK(attr) ((attr)->u.blk)
1596#define DW_SND(attr) ((attr)->u.snd)
1597#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1598#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1599
0963b4bd 1600/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1601struct dwarf_block
1602 {
56eb65bd 1603 size_t size;
1d6edc3c
JK
1604
1605 /* Valid only if SIZE is not zero. */
d521ce57 1606 const gdb_byte *data;
c906108c
SS
1607 };
1608
c906108c
SS
1609#ifndef ATTR_ALLOC_CHUNK
1610#define ATTR_ALLOC_CHUNK 4
1611#endif
1612
c906108c
SS
1613/* Allocate fields for structs, unions and enums in this size. */
1614#ifndef DW_FIELD_ALLOC_CHUNK
1615#define DW_FIELD_ALLOC_CHUNK 4
1616#endif
1617
c906108c
SS
1618/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1619 but this would require a corresponding change in unpack_field_as_long
1620 and friends. */
1621static int bits_per_byte = 8;
1622
52059ffd
TT
1623struct nextfield
1624{
1625 struct nextfield *next;
1626 int accessibility;
1627 int virtuality;
1628 struct field field;
1629};
1630
1631struct nextfnfield
1632{
1633 struct nextfnfield *next;
1634 struct fn_field fnfield;
1635};
1636
1637struct fnfieldlist
1638{
1639 const char *name;
1640 int length;
1641 struct nextfnfield *head;
1642};
1643
883fd55a 1644struct decl_field_list
52059ffd 1645{
883fd55a
KS
1646 struct decl_field field;
1647 struct decl_field_list *next;
52059ffd
TT
1648};
1649
c906108c
SS
1650/* The routines that read and process dies for a C struct or C++ class
1651 pass lists of data member fields and lists of member function fields
1652 in an instance of a field_info structure, as defined below. */
1653struct field_info
c5aa993b 1654 {
0963b4bd 1655 /* List of data member and baseclasses fields. */
52059ffd 1656 struct nextfield *fields, *baseclasses;
c906108c 1657
7d0ccb61 1658 /* Number of fields (including baseclasses). */
c5aa993b 1659 int nfields;
c906108c 1660
c5aa993b
JM
1661 /* Number of baseclasses. */
1662 int nbaseclasses;
c906108c 1663
c5aa993b
JM
1664 /* Set if the accesibility of one of the fields is not public. */
1665 int non_public_fields;
c906108c 1666
c5aa993b
JM
1667 /* Member function fieldlist array, contains name of possibly overloaded
1668 member function, number of overloaded member functions and a pointer
1669 to the head of the member function field chain. */
52059ffd 1670 struct fnfieldlist *fnfieldlists;
c906108c 1671
c5aa993b
JM
1672 /* Number of entries in the fnfieldlists array. */
1673 int nfnfields;
98751a41
JK
1674
1675 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1676 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
883fd55a 1677 struct decl_field_list *typedef_field_list;
98751a41 1678 unsigned typedef_field_list_count;
883fd55a
KS
1679
1680 /* Nested types defined by this class and the number of elements in this
1681 list. */
1682 struct decl_field_list *nested_types_list;
1683 unsigned nested_types_list_count;
c5aa993b 1684 };
c906108c 1685
10b3939b
DJ
1686/* One item on the queue of compilation units to read in full symbols
1687 for. */
1688struct dwarf2_queue_item
1689{
1690 struct dwarf2_per_cu_data *per_cu;
95554aad 1691 enum language pretend_language;
10b3939b
DJ
1692 struct dwarf2_queue_item *next;
1693};
1694
1695/* The current queue. */
1696static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1697
ae038cb0
DJ
1698/* Loaded secondary compilation units are kept in memory until they
1699 have not been referenced for the processing of this many
1700 compilation units. Set this to zero to disable caching. Cache
1701 sizes of up to at least twenty will improve startup time for
1702 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1703static int dwarf_max_cache_age = 5;
920d2a44 1704static void
b4f54984
DE
1705show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1706 struct cmd_list_element *c, const char *value)
920d2a44 1707{
3e43a32a 1708 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1709 "DWARF compilation units is %s.\n"),
920d2a44
AC
1710 value);
1711}
4390d890 1712\f
c906108c
SS
1713/* local function prototypes */
1714
a32a8923
DE
1715static const char *get_section_name (const struct dwarf2_section_info *);
1716
1717static const char *get_section_file_name (const struct dwarf2_section_info *);
1718
918dd910
JK
1719static void dwarf2_find_base_address (struct die_info *die,
1720 struct dwarf2_cu *cu);
1721
0018ea6f
DE
1722static struct partial_symtab *create_partial_symtab
1723 (struct dwarf2_per_cu_data *per_cu, const char *name);
1724
f1902523
JK
1725static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1726 const gdb_byte *info_ptr,
1727 struct die_info *type_unit_die,
1728 int has_children, void *data);
1729
ed2dc618
SM
1730static void dwarf2_build_psymtabs_hard
1731 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1732
72bf9492
DJ
1733static void scan_partial_symbols (struct partial_die_info *,
1734 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1735 int, struct dwarf2_cu *);
c906108c 1736
72bf9492
DJ
1737static void add_partial_symbol (struct partial_die_info *,
1738 struct dwarf2_cu *);
63d06c5c 1739
72bf9492
DJ
1740static void add_partial_namespace (struct partial_die_info *pdi,
1741 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1742 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1743
5d7cb8df 1744static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1745 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1746 struct dwarf2_cu *cu);
1747
72bf9492
DJ
1748static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1749 struct dwarf2_cu *cu);
91c24f0a 1750
bc30ff58
JB
1751static void add_partial_subprogram (struct partial_die_info *pdi,
1752 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1753 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1754
257e7a09
YQ
1755static void dwarf2_read_symtab (struct partial_symtab *,
1756 struct objfile *);
c906108c 1757
a14ed312 1758static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1759
433df2d4
DE
1760static struct abbrev_info *abbrev_table_lookup_abbrev
1761 (const struct abbrev_table *, unsigned int);
1762
1763static struct abbrev_table *abbrev_table_read_table
ed2dc618
SM
1764 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1765 sect_offset);
433df2d4
DE
1766
1767static void abbrev_table_free (struct abbrev_table *);
1768
f4dc4d17
DE
1769static void abbrev_table_free_cleanup (void *);
1770
dee91e82
DE
1771static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1772 struct dwarf2_section_info *);
c906108c 1773
f3dd6933 1774static void dwarf2_free_abbrev_table (void *);
c906108c 1775
d521ce57 1776static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1777
dee91e82 1778static struct partial_die_info *load_partial_dies
d521ce57 1779 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1780
d521ce57
TT
1781static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1782 struct partial_die_info *,
1783 struct abbrev_info *,
1784 unsigned int,
1785 const gdb_byte *);
c906108c 1786
36586728 1787static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1788 struct dwarf2_cu *);
72bf9492
DJ
1789
1790static void fixup_partial_die (struct partial_die_info *,
1791 struct dwarf2_cu *);
1792
d521ce57
TT
1793static const gdb_byte *read_attribute (const struct die_reader_specs *,
1794 struct attribute *, struct attr_abbrev *,
1795 const gdb_byte *);
a8329558 1796
a1855c1d 1797static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1798
a1855c1d 1799static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1800
a1855c1d 1801static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1802
a1855c1d 1803static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1804
a1855c1d 1805static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1806
d521ce57 1807static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1808 unsigned int *);
c906108c 1809
d521ce57 1810static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1811
1812static LONGEST read_checked_initial_length_and_offset
d521ce57 1813 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1814 unsigned int *, unsigned int *);
613e1657 1815
d521ce57
TT
1816static LONGEST read_offset (bfd *, const gdb_byte *,
1817 const struct comp_unit_head *,
c764a876
DE
1818 unsigned int *);
1819
d521ce57 1820static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1821
ed2dc618
SM
1822static sect_offset read_abbrev_offset
1823 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1824 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1825
d521ce57 1826static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1827
d521ce57 1828static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1829
ed2dc618
SM
1830static const char *read_indirect_string
1831 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1832 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1833
ed2dc618
SM
1834static const char *read_indirect_line_string
1835 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1836 const struct comp_unit_head *, unsigned int *);
36586728 1837
ed2dc618
SM
1838static const char *read_indirect_string_at_offset
1839 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1840 LONGEST str_offset);
927aa2e7 1841
ed2dc618
SM
1842static const char *read_indirect_string_from_dwz
1843 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1844
d521ce57 1845static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1846
d521ce57
TT
1847static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1848 const gdb_byte *,
3019eac3
DE
1849 unsigned int *);
1850
d521ce57 1851static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1852 ULONGEST str_index);
3019eac3 1853
e142c38c 1854static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1855
e142c38c
DJ
1856static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1857 struct dwarf2_cu *);
c906108c 1858
348e048f 1859static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1860 unsigned int);
348e048f 1861
7d45c7c3
KB
1862static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1863 struct dwarf2_cu *cu);
1864
05cf31d1
JB
1865static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1866 struct dwarf2_cu *cu);
1867
e142c38c 1868static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1869
e142c38c 1870static struct die_info *die_specification (struct die_info *die,
f2f0e013 1871 struct dwarf2_cu **);
63d06c5c 1872
9c541725 1873static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1874 struct dwarf2_cu *cu);
debd256d 1875
f3f5162e 1876static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1877 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1878 CORE_ADDR, int decode_mapping);
c906108c 1879
4d663531 1880static void dwarf2_start_subfile (const char *, const char *);
c906108c 1881
43f3e411
DE
1882static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1883 const char *, const char *,
1884 CORE_ADDR);
f4dc4d17 1885
a14ed312 1886static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1887 struct dwarf2_cu *);
c906108c 1888
34eaf542
TT
1889static struct symbol *new_symbol_full (struct die_info *, struct type *,
1890 struct dwarf2_cu *, struct symbol *);
1891
ff39bb5e 1892static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1893 struct dwarf2_cu *);
c906108c 1894
ff39bb5e 1895static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1896 struct type *type,
1897 const char *name,
1898 struct obstack *obstack,
12df843f 1899 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1900 const gdb_byte **bytes,
98bfdba5 1901 struct dwarf2_locexpr_baton **baton);
2df3850c 1902
e7c27a73 1903static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1904
b4ba55a1
JB
1905static int need_gnat_info (struct dwarf2_cu *);
1906
3e43a32a
MS
1907static struct type *die_descriptive_type (struct die_info *,
1908 struct dwarf2_cu *);
b4ba55a1
JB
1909
1910static void set_descriptive_type (struct type *, struct die_info *,
1911 struct dwarf2_cu *);
1912
e7c27a73
DJ
1913static struct type *die_containing_type (struct die_info *,
1914 struct dwarf2_cu *);
c906108c 1915
ff39bb5e 1916static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1917 struct dwarf2_cu *);
c906108c 1918
f792889a 1919static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1920
673bfd45
DE
1921static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1922
0d5cff50 1923static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1924
6e70227d 1925static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1926 const char *suffix, int physname,
1927 struct dwarf2_cu *cu);
63d06c5c 1928
e7c27a73 1929static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1930
348e048f
DE
1931static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1932
e7c27a73 1933static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1934
e7c27a73 1935static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1936
96408a79
SA
1937static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1938
71a3c369
TT
1939static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1940
ff013f42
JK
1941static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1942 struct dwarf2_cu *, struct partial_symtab *);
1943
3a2b436a 1944/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1945 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1946enum pc_bounds_kind
1947{
e385593e 1948 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1949 PC_BOUNDS_NOT_PRESENT,
1950
e385593e
JK
1951 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1952 were present but they do not form a valid range of PC addresses. */
1953 PC_BOUNDS_INVALID,
1954
3a2b436a
JK
1955 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1956 PC_BOUNDS_RANGES,
1957
1958 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1959 PC_BOUNDS_HIGH_LOW,
1960};
1961
1962static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1963 CORE_ADDR *, CORE_ADDR *,
1964 struct dwarf2_cu *,
1965 struct partial_symtab *);
c906108c 1966
fae299cd
DC
1967static void get_scope_pc_bounds (struct die_info *,
1968 CORE_ADDR *, CORE_ADDR *,
1969 struct dwarf2_cu *);
1970
801e3a5b
JB
1971static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1972 CORE_ADDR, struct dwarf2_cu *);
1973
a14ed312 1974static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1975 struct dwarf2_cu *);
c906108c 1976
a14ed312 1977static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1978 struct type *, struct dwarf2_cu *);
c906108c 1979
a14ed312 1980static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1981 struct die_info *, struct type *,
e7c27a73 1982 struct dwarf2_cu *);
c906108c 1983
a14ed312 1984static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1985 struct type *,
1986 struct dwarf2_cu *);
c906108c 1987
134d01f1 1988static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1989
e7c27a73 1990static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1991
e7c27a73 1992static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1993
5d7cb8df
JK
1994static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1995
22cee43f
PMR
1996static struct using_direct **using_directives (enum language);
1997
27aa8d6a
SW
1998static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1999
74921315
KS
2000static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
2001
f55ee35c
JK
2002static struct type *read_module_type (struct die_info *die,
2003 struct dwarf2_cu *cu);
2004
38d518c9 2005static const char *namespace_name (struct die_info *die,
e142c38c 2006 int *is_anonymous, struct dwarf2_cu *);
38d518c9 2007
134d01f1 2008static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 2009
e7c27a73 2010static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 2011
6e70227d 2012static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
2013 struct dwarf2_cu *);
2014
bf6af496 2015static struct die_info *read_die_and_siblings_1
d521ce57 2016 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 2017 struct die_info *);
639d11d3 2018
dee91e82 2019static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
2020 const gdb_byte *info_ptr,
2021 const gdb_byte **new_info_ptr,
639d11d3
DC
2022 struct die_info *parent);
2023
d521ce57
TT
2024static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
2025 struct die_info **, const gdb_byte *,
2026 int *, int);
3019eac3 2027
d521ce57
TT
2028static const gdb_byte *read_full_die (const struct die_reader_specs *,
2029 struct die_info **, const gdb_byte *,
2030 int *);
93311388 2031
e7c27a73 2032static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 2033
15d034d0
TT
2034static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
2035 struct obstack *);
71c25dea 2036
15d034d0 2037static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 2038
15d034d0 2039static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
2040 struct die_info *die,
2041 struct dwarf2_cu *cu);
2042
ca69b9e6
DE
2043static const char *dwarf2_physname (const char *name, struct die_info *die,
2044 struct dwarf2_cu *cu);
2045
e142c38c 2046static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 2047 struct dwarf2_cu **);
9219021c 2048
f39c6ffd 2049static const char *dwarf_tag_name (unsigned int);
c906108c 2050
f39c6ffd 2051static const char *dwarf_attr_name (unsigned int);
c906108c 2052
f39c6ffd 2053static const char *dwarf_form_name (unsigned int);
c906108c 2054
a121b7c1 2055static const char *dwarf_bool_name (unsigned int);
c906108c 2056
f39c6ffd 2057static const char *dwarf_type_encoding_name (unsigned int);
c906108c 2058
f9aca02d 2059static struct die_info *sibling_die (struct die_info *);
c906108c 2060
d97bc12b
DE
2061static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2062
2063static void dump_die_for_error (struct die_info *);
2064
2065static void dump_die_1 (struct ui_file *, int level, int max_level,
2066 struct die_info *);
c906108c 2067
d97bc12b 2068/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 2069
51545339 2070static void store_in_ref_table (struct die_info *,
10b3939b 2071 struct dwarf2_cu *);
c906108c 2072
ff39bb5e 2073static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 2074
ff39bb5e 2075static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 2076
348e048f 2077static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 2078 const struct attribute *,
348e048f
DE
2079 struct dwarf2_cu **);
2080
10b3939b 2081static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 2082 const struct attribute *,
f2f0e013 2083 struct dwarf2_cu **);
c906108c 2084
348e048f 2085static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 2086 const struct attribute *,
348e048f
DE
2087 struct dwarf2_cu **);
2088
ac9ec31b
DE
2089static struct type *get_signatured_type (struct die_info *, ULONGEST,
2090 struct dwarf2_cu *);
2091
2092static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 2093 const struct attribute *,
ac9ec31b
DE
2094 struct dwarf2_cu *);
2095
e5fe5e75 2096static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 2097
52dc124a 2098static void read_signatured_type (struct signatured_type *);
348e048f 2099
63e43d3a
PMR
2100static int attr_to_dynamic_prop (const struct attribute *attr,
2101 struct die_info *die, struct dwarf2_cu *cu,
2102 struct dynamic_prop *prop);
2103
c906108c
SS
2104/* memory allocation interface */
2105
7b5a2f43 2106static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 2107
b60c80d6 2108static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 2109
43f3e411 2110static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 2111
6e5a29e1 2112static int attr_form_is_block (const struct attribute *);
8e19ed76 2113
6e5a29e1 2114static int attr_form_is_section_offset (const struct attribute *);
3690dd37 2115
6e5a29e1 2116static int attr_form_is_constant (const struct attribute *);
3690dd37 2117
6e5a29e1 2118static int attr_form_is_ref (const struct attribute *);
7771576e 2119
8cf6f0b1
TT
2120static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2121 struct dwarf2_loclist_baton *baton,
ff39bb5e 2122 const struct attribute *attr);
8cf6f0b1 2123
ff39bb5e 2124static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 2125 struct symbol *sym,
f1e6e072
TT
2126 struct dwarf2_cu *cu,
2127 int is_block);
4c2df51b 2128
d521ce57
TT
2129static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2130 const gdb_byte *info_ptr,
2131 struct abbrev_info *abbrev);
4bb7a0a7 2132
72bf9492
DJ
2133static void free_stack_comp_unit (void *);
2134
72bf9492
DJ
2135static hashval_t partial_die_hash (const void *item);
2136
2137static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2138
ae038cb0 2139static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
2140 (sect_offset sect_off, unsigned int offset_in_dwz,
2141 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 2142
9816fde3 2143static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 2144 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
2145
2146static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
2147 struct die_info *comp_unit_die,
2148 enum language pretend_language);
93311388 2149
68dc6402 2150static void free_heap_comp_unit (void *);
ae038cb0
DJ
2151
2152static void free_cached_comp_units (void *);
2153
ed2dc618 2154static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 2155
dee91e82 2156static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 2157
f792889a
DJ
2158static struct type *set_die_type (struct die_info *, struct type *,
2159 struct dwarf2_cu *);
1c379e20 2160
ed2dc618 2161static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 2162
ed2dc618 2163static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 2164
95554aad
TT
2165static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2166 enum language);
10b3939b 2167
95554aad
TT
2168static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2169 enum language);
10b3939b 2170
f4dc4d17
DE
2171static void process_full_type_unit (struct dwarf2_per_cu_data *,
2172 enum language);
2173
10b3939b
DJ
2174static void dwarf2_add_dependence (struct dwarf2_cu *,
2175 struct dwarf2_per_cu_data *);
2176
ae038cb0
DJ
2177static void dwarf2_mark (struct dwarf2_cu *);
2178
2179static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2180
b64f50a1 2181static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 2182 struct dwarf2_per_cu_data *);
673bfd45 2183
f792889a 2184static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 2185
9291a0cd
TT
2186static void dwarf2_release_queue (void *dummy);
2187
95554aad
TT
2188static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2189 enum language pretend_language);
2190
ed2dc618 2191static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 2192
d721ba37
PA
2193/* The return type of find_file_and_directory. Note, the enclosed
2194 string pointers are only valid while this object is valid. */
2195
2196struct file_and_directory
2197{
2198 /* The filename. This is never NULL. */
2199 const char *name;
2200
2201 /* The compilation directory. NULL if not known. If we needed to
2202 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2203 points directly to the DW_AT_comp_dir string attribute owned by
2204 the obstack that owns the DIE. */
2205 const char *comp_dir;
2206
2207 /* If we needed to build a new string for comp_dir, this is what
2208 owns the storage. */
2209 std::string comp_dir_storage;
2210};
2211
2212static file_and_directory find_file_and_directory (struct die_info *die,
2213 struct dwarf2_cu *cu);
9291a0cd
TT
2214
2215static char *file_full_name (int file, struct line_header *lh,
2216 const char *comp_dir);
2217
43988095
JK
2218/* Expected enum dwarf_unit_type for read_comp_unit_head. */
2219enum class rcuh_kind { COMPILE, TYPE };
2220
d521ce57 2221static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
2222 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2223 struct comp_unit_head *header,
36586728 2224 struct dwarf2_section_info *section,
d521ce57 2225 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 2226 rcuh_kind section_kind);
36586728 2227
fd820528 2228static void init_cutu_and_read_dies
f4dc4d17
DE
2229 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2230 int use_existing_cu, int keep,
3019eac3
DE
2231 die_reader_func_ftype *die_reader_func, void *data);
2232
dee91e82
DE
2233static void init_cutu_and_read_dies_simple
2234 (struct dwarf2_per_cu_data *this_cu,
2235 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 2236
673bfd45 2237static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2238
3019eac3
DE
2239static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2240
57d63ce2 2241static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
2242 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2243 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 2244 ULONGEST signature, int is_debug_types);
a2ce51a0 2245
ed2dc618
SM
2246static struct dwp_file *get_dwp_file
2247 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 2248
3019eac3 2249static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2250 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2251
2252static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2253 (struct signatured_type *, const char *, const char *);
3019eac3 2254
89e63ee4
DE
2255static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2256
3019eac3
DE
2257static void free_dwo_file_cleanup (void *);
2258
ed2dc618
SM
2259struct free_dwo_file_cleanup_data
2260{
2261 struct dwo_file *dwo_file;
2262 struct dwarf2_per_objfile *dwarf2_per_objfile;
2263};
2264
2265static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 2266
1b80a9fa 2267static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2268
2269static void free_line_header_voidp (void *arg);
4390d890
DE
2270\f
2271/* Various complaints about symbol reading that don't abort the process. */
2272
2273static void
2274dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2275{
2276 complaint (&symfile_complaints,
2277 _("statement list doesn't fit in .debug_line section"));
2278}
2279
2280static void
2281dwarf2_debug_line_missing_file_complaint (void)
2282{
2283 complaint (&symfile_complaints,
2284 _(".debug_line section has line data without a file"));
2285}
2286
2287static void
2288dwarf2_debug_line_missing_end_sequence_complaint (void)
2289{
2290 complaint (&symfile_complaints,
2291 _(".debug_line section has line "
2292 "program sequence without an end"));
2293}
2294
2295static void
2296dwarf2_complex_location_expr_complaint (void)
2297{
2298 complaint (&symfile_complaints, _("location expression too complex"));
2299}
2300
2301static void
2302dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2303 int arg3)
2304{
2305 complaint (&symfile_complaints,
2306 _("const value length mismatch for '%s', got %d, expected %d"),
2307 arg1, arg2, arg3);
2308}
2309
2310static void
2311dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2312{
2313 complaint (&symfile_complaints,
2314 _("debug info runs off end of %s section"
2315 " [in module %s]"),
a32a8923
DE
2316 get_section_name (section),
2317 get_section_file_name (section));
4390d890 2318}
1b80a9fa 2319
4390d890
DE
2320static void
2321dwarf2_macro_malformed_definition_complaint (const char *arg1)
2322{
2323 complaint (&symfile_complaints,
2324 _("macro debug info contains a "
2325 "malformed macro definition:\n`%s'"),
2326 arg1);
2327}
2328
2329static void
2330dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2331{
2332 complaint (&symfile_complaints,
2333 _("invalid attribute class or form for '%s' in '%s'"),
2334 arg1, arg2);
2335}
527f3840
JK
2336
2337/* Hash function for line_header_hash. */
2338
2339static hashval_t
2340line_header_hash (const struct line_header *ofs)
2341{
9c541725 2342 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2343}
2344
2345/* Hash function for htab_create_alloc_ex for line_header_hash. */
2346
2347static hashval_t
2348line_header_hash_voidp (const void *item)
2349{
9a3c8263 2350 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2351
2352 return line_header_hash (ofs);
2353}
2354
2355/* Equality function for line_header_hash. */
2356
2357static int
2358line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2359{
9a3c8263
SM
2360 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2361 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2362
9c541725 2363 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2364 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2365}
2366
4390d890 2367\f
9291a0cd 2368
31aa7e4e
JB
2369/* Read the given attribute value as an address, taking the attribute's
2370 form into account. */
2371
2372static CORE_ADDR
2373attr_value_as_address (struct attribute *attr)
2374{
2375 CORE_ADDR addr;
2376
2377 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2378 {
2379 /* Aside from a few clearly defined exceptions, attributes that
2380 contain an address must always be in DW_FORM_addr form.
2381 Unfortunately, some compilers happen to be violating this
2382 requirement by encoding addresses using other forms, such
2383 as DW_FORM_data4 for example. For those broken compilers,
2384 we try to do our best, without any guarantee of success,
2385 to interpret the address correctly. It would also be nice
2386 to generate a complaint, but that would require us to maintain
2387 a list of legitimate cases where a non-address form is allowed,
2388 as well as update callers to pass in at least the CU's DWARF
2389 version. This is more overhead than what we're willing to
2390 expand for a pretty rare case. */
2391 addr = DW_UNSND (attr);
2392 }
2393 else
2394 addr = DW_ADDR (attr);
2395
2396 return addr;
2397}
2398
9291a0cd 2399/* The suffix for an index file. */
437afbb8
JK
2400#define INDEX4_SUFFIX ".gdb-index"
2401#define INDEX5_SUFFIX ".debug_names"
2402#define DEBUG_STR_SUFFIX ".debug_str"
9291a0cd 2403
330cdd98
PA
2404/* See declaration. */
2405
2406dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2407 const dwarf2_debug_sections *names)
2408 : objfile (objfile_)
2409{
2410 if (names == NULL)
2411 names = &dwarf2_elf_names;
2412
2413 bfd *obfd = objfile->obfd;
2414
2415 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2416 locate_sections (obfd, sec, *names);
2417}
2418
2419dwarf2_per_objfile::~dwarf2_per_objfile ()
2420{
2421 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2422 free_cached_comp_units ();
2423
2424 if (quick_file_names_table)
2425 htab_delete (quick_file_names_table);
2426
2427 if (line_header_hash)
2428 htab_delete (line_header_hash);
2429
2430 /* Everything else should be on the objfile obstack. */
2431}
2432
2433/* See declaration. */
2434
2435void
2436dwarf2_per_objfile::free_cached_comp_units ()
2437{
2438 dwarf2_per_cu_data *per_cu = read_in_chain;
2439 dwarf2_per_cu_data **last_chain = &read_in_chain;
2440 while (per_cu != NULL)
2441 {
2442 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2443
2444 free_heap_comp_unit (per_cu->cu);
2445 *last_chain = next_cu;
2446 per_cu = next_cu;
2447 }
2448}
2449
c906108c 2450/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2451 information and return true if we have enough to do something.
2452 NAMES points to the dwarf2 section names, or is NULL if the standard
2453 ELF names are used. */
c906108c
SS
2454
2455int
251d32d9
TG
2456dwarf2_has_info (struct objfile *objfile,
2457 const struct dwarf2_debug_sections *names)
c906108c 2458{
97cbe998
SDJ
2459 if (objfile->flags & OBJF_READNEVER)
2460 return 0;
2461
ed2dc618
SM
2462 struct dwarf2_per_objfile *dwarf2_per_objfile
2463 = get_dwarf2_per_objfile (objfile);
2464
2465 if (dwarf2_per_objfile == NULL)
be391dca
TT
2466 {
2467 /* Initialize per-objfile state. */
2468 struct dwarf2_per_objfile *data
8d749320 2469 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
9a619af0 2470
330cdd98 2471 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
ed2dc618 2472 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
be391dca 2473 }
73869dc2 2474 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2475 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2476 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2477 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2478}
2479
2480/* Return the containing section of virtual section SECTION. */
2481
2482static struct dwarf2_section_info *
2483get_containing_section (const struct dwarf2_section_info *section)
2484{
2485 gdb_assert (section->is_virtual);
2486 return section->s.containing_section;
c906108c
SS
2487}
2488
a32a8923
DE
2489/* Return the bfd owner of SECTION. */
2490
2491static struct bfd *
2492get_section_bfd_owner (const struct dwarf2_section_info *section)
2493{
73869dc2
DE
2494 if (section->is_virtual)
2495 {
2496 section = get_containing_section (section);
2497 gdb_assert (!section->is_virtual);
2498 }
049412e3 2499 return section->s.section->owner;
a32a8923
DE
2500}
2501
2502/* Return the bfd section of SECTION.
2503 Returns NULL if the section is not present. */
2504
2505static asection *
2506get_section_bfd_section (const struct dwarf2_section_info *section)
2507{
73869dc2
DE
2508 if (section->is_virtual)
2509 {
2510 section = get_containing_section (section);
2511 gdb_assert (!section->is_virtual);
2512 }
049412e3 2513 return section->s.section;
a32a8923
DE
2514}
2515
2516/* Return the name of SECTION. */
2517
2518static const char *
2519get_section_name (const struct dwarf2_section_info *section)
2520{
2521 asection *sectp = get_section_bfd_section (section);
2522
2523 gdb_assert (sectp != NULL);
2524 return bfd_section_name (get_section_bfd_owner (section), sectp);
2525}
2526
2527/* Return the name of the file SECTION is in. */
2528
2529static const char *
2530get_section_file_name (const struct dwarf2_section_info *section)
2531{
2532 bfd *abfd = get_section_bfd_owner (section);
2533
2534 return bfd_get_filename (abfd);
2535}
2536
2537/* Return the id of SECTION.
2538 Returns 0 if SECTION doesn't exist. */
2539
2540static int
2541get_section_id (const struct dwarf2_section_info *section)
2542{
2543 asection *sectp = get_section_bfd_section (section);
2544
2545 if (sectp == NULL)
2546 return 0;
2547 return sectp->id;
2548}
2549
2550/* Return the flags of SECTION.
73869dc2 2551 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2552
2553static int
2554get_section_flags (const struct dwarf2_section_info *section)
2555{
2556 asection *sectp = get_section_bfd_section (section);
2557
2558 gdb_assert (sectp != NULL);
2559 return bfd_get_section_flags (sectp->owner, sectp);
2560}
2561
251d32d9
TG
2562/* When loading sections, we look either for uncompressed section or for
2563 compressed section names. */
233a11ab
CS
2564
2565static int
251d32d9
TG
2566section_is_p (const char *section_name,
2567 const struct dwarf2_section_names *names)
233a11ab 2568{
251d32d9
TG
2569 if (names->normal != NULL
2570 && strcmp (section_name, names->normal) == 0)
2571 return 1;
2572 if (names->compressed != NULL
2573 && strcmp (section_name, names->compressed) == 0)
2574 return 1;
2575 return 0;
233a11ab
CS
2576}
2577
330cdd98 2578/* See declaration. */
c906108c 2579
330cdd98
PA
2580void
2581dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2582 const dwarf2_debug_sections &names)
c906108c 2583{
dc7650b8 2584 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2585
dc7650b8
JK
2586 if ((aflag & SEC_HAS_CONTENTS) == 0)
2587 {
2588 }
330cdd98 2589 else if (section_is_p (sectp->name, &names.info))
c906108c 2590 {
330cdd98
PA
2591 this->info.s.section = sectp;
2592 this->info.size = bfd_get_section_size (sectp);
c906108c 2593 }
330cdd98 2594 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2595 {
330cdd98
PA
2596 this->abbrev.s.section = sectp;
2597 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2598 }
330cdd98 2599 else if (section_is_p (sectp->name, &names.line))
c906108c 2600 {
330cdd98
PA
2601 this->line.s.section = sectp;
2602 this->line.size = bfd_get_section_size (sectp);
c906108c 2603 }
330cdd98 2604 else if (section_is_p (sectp->name, &names.loc))
c906108c 2605 {
330cdd98
PA
2606 this->loc.s.section = sectp;
2607 this->loc.size = bfd_get_section_size (sectp);
c906108c 2608 }
330cdd98 2609 else if (section_is_p (sectp->name, &names.loclists))
43988095 2610 {
330cdd98
PA
2611 this->loclists.s.section = sectp;
2612 this->loclists.size = bfd_get_section_size (sectp);
43988095 2613 }
330cdd98 2614 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2615 {
330cdd98
PA
2616 this->macinfo.s.section = sectp;
2617 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2618 }
330cdd98 2619 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2620 {
330cdd98
PA
2621 this->macro.s.section = sectp;
2622 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2623 }
330cdd98 2624 else if (section_is_p (sectp->name, &names.str))
c906108c 2625 {
330cdd98
PA
2626 this->str.s.section = sectp;
2627 this->str.size = bfd_get_section_size (sectp);
c906108c 2628 }
330cdd98 2629 else if (section_is_p (sectp->name, &names.line_str))
43988095 2630 {
330cdd98
PA
2631 this->line_str.s.section = sectp;
2632 this->line_str.size = bfd_get_section_size (sectp);
43988095 2633 }
330cdd98 2634 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2635 {
330cdd98
PA
2636 this->addr.s.section = sectp;
2637 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2638 }
330cdd98 2639 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2640 {
330cdd98
PA
2641 this->frame.s.section = sectp;
2642 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2643 }
330cdd98 2644 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2645 {
330cdd98
PA
2646 this->eh_frame.s.section = sectp;
2647 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2648 }
330cdd98 2649 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2650 {
330cdd98
PA
2651 this->ranges.s.section = sectp;
2652 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2653 }
330cdd98 2654 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2655 {
330cdd98
PA
2656 this->rnglists.s.section = sectp;
2657 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2658 }
330cdd98 2659 else if (section_is_p (sectp->name, &names.types))
348e048f 2660 {
8b70b953
TT
2661 struct dwarf2_section_info type_section;
2662
2663 memset (&type_section, 0, sizeof (type_section));
049412e3 2664 type_section.s.section = sectp;
8b70b953
TT
2665 type_section.size = bfd_get_section_size (sectp);
2666
330cdd98 2667 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2668 &type_section);
348e048f 2669 }
330cdd98 2670 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2671 {
330cdd98
PA
2672 this->gdb_index.s.section = sectp;
2673 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2674 }
927aa2e7
JK
2675 else if (section_is_p (sectp->name, &names.debug_names))
2676 {
2677 this->debug_names.s.section = sectp;
2678 this->debug_names.size = bfd_get_section_size (sectp);
2679 }
2680 else if (section_is_p (sectp->name, &names.debug_aranges))
2681 {
2682 this->debug_aranges.s.section = sectp;
2683 this->debug_aranges.size = bfd_get_section_size (sectp);
2684 }
dce234bc 2685
b4e1fd61 2686 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2687 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2688 this->has_section_at_zero = true;
c906108c
SS
2689}
2690
fceca515
DE
2691/* A helper function that decides whether a section is empty,
2692 or not present. */
9e0ac564
TT
2693
2694static int
19ac8c2e 2695dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2696{
73869dc2
DE
2697 if (section->is_virtual)
2698 return section->size == 0;
049412e3 2699 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2700}
2701
3019eac3
DE
2702/* Read the contents of the section INFO.
2703 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2704 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2705 of the DWO file.
dce234bc 2706 If the section is compressed, uncompress it before returning. */
c906108c 2707
dce234bc
PP
2708static void
2709dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2710{
a32a8923 2711 asection *sectp;
3019eac3 2712 bfd *abfd;
dce234bc 2713 gdb_byte *buf, *retbuf;
c906108c 2714
be391dca
TT
2715 if (info->readin)
2716 return;
dce234bc 2717 info->buffer = NULL;
be391dca 2718 info->readin = 1;
188dd5d6 2719
9e0ac564 2720 if (dwarf2_section_empty_p (info))
dce234bc 2721 return;
c906108c 2722
a32a8923 2723 sectp = get_section_bfd_section (info);
3019eac3 2724
73869dc2
DE
2725 /* If this is a virtual section we need to read in the real one first. */
2726 if (info->is_virtual)
2727 {
2728 struct dwarf2_section_info *containing_section =
2729 get_containing_section (info);
2730
2731 gdb_assert (sectp != NULL);
2732 if ((sectp->flags & SEC_RELOC) != 0)
2733 {
2734 error (_("Dwarf Error: DWP format V2 with relocations is not"
2735 " supported in section %s [in module %s]"),
2736 get_section_name (info), get_section_file_name (info));
2737 }
2738 dwarf2_read_section (objfile, containing_section);
2739 /* Other code should have already caught virtual sections that don't
2740 fit. */
2741 gdb_assert (info->virtual_offset + info->size
2742 <= containing_section->size);
2743 /* If the real section is empty or there was a problem reading the
2744 section we shouldn't get here. */
2745 gdb_assert (containing_section->buffer != NULL);
2746 info->buffer = containing_section->buffer + info->virtual_offset;
2747 return;
2748 }
2749
4bf44c1c
TT
2750 /* If the section has relocations, we must read it ourselves.
2751 Otherwise we attach it to the BFD. */
2752 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2753 {
d521ce57 2754 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2755 return;
dce234bc 2756 }
dce234bc 2757
224c3ddb 2758 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2759 info->buffer = buf;
dce234bc
PP
2760
2761 /* When debugging .o files, we may need to apply relocations; see
2762 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2763 We never compress sections in .o files, so we only need to
2764 try this when the section is not compressed. */
ac8035ab 2765 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2766 if (retbuf != NULL)
2767 {
2768 info->buffer = retbuf;
2769 return;
2770 }
2771
a32a8923
DE
2772 abfd = get_section_bfd_owner (info);
2773 gdb_assert (abfd != NULL);
2774
dce234bc
PP
2775 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2776 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2777 {
2778 error (_("Dwarf Error: Can't read DWARF data"
2779 " in section %s [in module %s]"),
2780 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2781 }
dce234bc
PP
2782}
2783
9e0ac564
TT
2784/* A helper function that returns the size of a section in a safe way.
2785 If you are positive that the section has been read before using the
2786 size, then it is safe to refer to the dwarf2_section_info object's
2787 "size" field directly. In other cases, you must call this
2788 function, because for compressed sections the size field is not set
2789 correctly until the section has been read. */
2790
2791static bfd_size_type
2792dwarf2_section_size (struct objfile *objfile,
2793 struct dwarf2_section_info *info)
2794{
2795 if (!info->readin)
2796 dwarf2_read_section (objfile, info);
2797 return info->size;
2798}
2799
dce234bc 2800/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2801 SECTION_NAME. */
af34e669 2802
dce234bc 2803void
3017a003
TG
2804dwarf2_get_section_info (struct objfile *objfile,
2805 enum dwarf2_section_enum sect,
d521ce57 2806 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2807 bfd_size_type *sizep)
2808{
2809 struct dwarf2_per_objfile *data
9a3c8263
SM
2810 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2811 dwarf2_objfile_data_key);
dce234bc 2812 struct dwarf2_section_info *info;
a3b2a86b
TT
2813
2814 /* We may see an objfile without any DWARF, in which case we just
2815 return nothing. */
2816 if (data == NULL)
2817 {
2818 *sectp = NULL;
2819 *bufp = NULL;
2820 *sizep = 0;
2821 return;
2822 }
3017a003
TG
2823 switch (sect)
2824 {
2825 case DWARF2_DEBUG_FRAME:
2826 info = &data->frame;
2827 break;
2828 case DWARF2_EH_FRAME:
2829 info = &data->eh_frame;
2830 break;
2831 default:
2832 gdb_assert_not_reached ("unexpected section");
2833 }
dce234bc 2834
9e0ac564 2835 dwarf2_read_section (objfile, info);
dce234bc 2836
a32a8923 2837 *sectp = get_section_bfd_section (info);
dce234bc
PP
2838 *bufp = info->buffer;
2839 *sizep = info->size;
2840}
2841
36586728
TT
2842/* A helper function to find the sections for a .dwz file. */
2843
2844static void
2845locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2846{
9a3c8263 2847 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2848
2849 /* Note that we only support the standard ELF names, because .dwz
2850 is ELF-only (at the time of writing). */
2851 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2852 {
049412e3 2853 dwz_file->abbrev.s.section = sectp;
36586728
TT
2854 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2855 }
2856 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2857 {
049412e3 2858 dwz_file->info.s.section = sectp;
36586728
TT
2859 dwz_file->info.size = bfd_get_section_size (sectp);
2860 }
2861 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2862 {
049412e3 2863 dwz_file->str.s.section = sectp;
36586728
TT
2864 dwz_file->str.size = bfd_get_section_size (sectp);
2865 }
2866 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2867 {
049412e3 2868 dwz_file->line.s.section = sectp;
36586728
TT
2869 dwz_file->line.size = bfd_get_section_size (sectp);
2870 }
2871 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2872 {
049412e3 2873 dwz_file->macro.s.section = sectp;
36586728
TT
2874 dwz_file->macro.size = bfd_get_section_size (sectp);
2875 }
2ec9a5e0
TT
2876 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2877 {
049412e3 2878 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2879 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2880 }
927aa2e7
JK
2881 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2882 {
2883 dwz_file->debug_names.s.section = sectp;
2884 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2885 }
36586728
TT
2886}
2887
4db1a1dc
TT
2888/* Open the separate '.dwz' debug file, if needed. Return NULL if
2889 there is no .gnu_debugaltlink section in the file. Error if there
2890 is such a section but the file cannot be found. */
36586728
TT
2891
2892static struct dwz_file *
ed2dc618 2893dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2894{
36586728
TT
2895 const char *filename;
2896 struct dwz_file *result;
acd13123 2897 bfd_size_type buildid_len_arg;
dc294be5
TT
2898 size_t buildid_len;
2899 bfd_byte *buildid;
36586728
TT
2900
2901 if (dwarf2_per_objfile->dwz_file != NULL)
2902 return dwarf2_per_objfile->dwz_file;
2903
4db1a1dc 2904 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2905 gdb::unique_xmalloc_ptr<char> data
2906 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2907 &buildid_len_arg, &buildid));
4db1a1dc
TT
2908 if (data == NULL)
2909 {
2910 if (bfd_get_error () == bfd_error_no_error)
2911 return NULL;
2912 error (_("could not read '.gnu_debugaltlink' section: %s"),
2913 bfd_errmsg (bfd_get_error ()));
2914 }
791afaa2
TT
2915
2916 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2917
acd13123
TT
2918 buildid_len = (size_t) buildid_len_arg;
2919
791afaa2 2920 filename = data.get ();
d721ba37
PA
2921
2922 std::string abs_storage;
36586728
TT
2923 if (!IS_ABSOLUTE_PATH (filename))
2924 {
14278e1f
TT
2925 gdb::unique_xmalloc_ptr<char> abs
2926 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2927
14278e1f 2928 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2929 filename = abs_storage.c_str ();
36586728
TT
2930 }
2931
dc294be5
TT
2932 /* First try the file name given in the section. If that doesn't
2933 work, try to use the build-id instead. */
192b62ce 2934 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2935 if (dwz_bfd != NULL)
36586728 2936 {
192b62ce
TT
2937 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2938 dwz_bfd.release ();
36586728
TT
2939 }
2940
dc294be5
TT
2941 if (dwz_bfd == NULL)
2942 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2943
2944 if (dwz_bfd == NULL)
2945 error (_("could not find '.gnu_debugaltlink' file for %s"),
2946 objfile_name (dwarf2_per_objfile->objfile));
2947
36586728
TT
2948 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2949 struct dwz_file);
192b62ce 2950 result->dwz_bfd = dwz_bfd.release ();
36586728 2951
192b62ce 2952 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728 2953
192b62ce 2954 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2955 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2956 return result;
2957}
9291a0cd 2958\f
7b9f3c50
DE
2959/* DWARF quick_symbols_functions support. */
2960
2961/* TUs can share .debug_line entries, and there can be a lot more TUs than
2962 unique line tables, so we maintain a separate table of all .debug_line
2963 derived entries to support the sharing.
2964 All the quick functions need is the list of file names. We discard the
2965 line_header when we're done and don't need to record it here. */
2966struct quick_file_names
2967{
094b34ac
DE
2968 /* The data used to construct the hash key. */
2969 struct stmt_list_hash hash;
7b9f3c50
DE
2970
2971 /* The number of entries in file_names, real_names. */
2972 unsigned int num_file_names;
2973
2974 /* The file names from the line table, after being run through
2975 file_full_name. */
2976 const char **file_names;
2977
2978 /* The file names from the line table after being run through
2979 gdb_realpath. These are computed lazily. */
2980 const char **real_names;
2981};
2982
2983/* When using the index (and thus not using psymtabs), each CU has an
2984 object of this type. This is used to hold information needed by
2985 the various "quick" methods. */
2986struct dwarf2_per_cu_quick_data
2987{
2988 /* The file table. This can be NULL if there was no file table
2989 or it's currently not read in.
2990 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2991 struct quick_file_names *file_names;
2992
2993 /* The corresponding symbol table. This is NULL if symbols for this
2994 CU have not yet been read. */
43f3e411 2995 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2996
2997 /* A temporary mark bit used when iterating over all CUs in
2998 expand_symtabs_matching. */
2999 unsigned int mark : 1;
3000
3001 /* True if we've tried to read the file table and found there isn't one.
3002 There will be no point in trying to read it again next time. */
3003 unsigned int no_file_data : 1;
3004};
3005
094b34ac
DE
3006/* Utility hash function for a stmt_list_hash. */
3007
3008static hashval_t
3009hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
3010{
3011 hashval_t v = 0;
3012
3013 if (stmt_list_hash->dwo_unit != NULL)
3014 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 3015 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
3016 return v;
3017}
3018
3019/* Utility equality function for a stmt_list_hash. */
3020
3021static int
3022eq_stmt_list_entry (const struct stmt_list_hash *lhs,
3023 const struct stmt_list_hash *rhs)
3024{
3025 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
3026 return 0;
3027 if (lhs->dwo_unit != NULL
3028 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
3029 return 0;
3030
9c541725 3031 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
3032}
3033
7b9f3c50
DE
3034/* Hash function for a quick_file_names. */
3035
3036static hashval_t
3037hash_file_name_entry (const void *e)
3038{
9a3c8263
SM
3039 const struct quick_file_names *file_data
3040 = (const struct quick_file_names *) e;
7b9f3c50 3041
094b34ac 3042 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
3043}
3044
3045/* Equality function for a quick_file_names. */
3046
3047static int
3048eq_file_name_entry (const void *a, const void *b)
3049{
9a3c8263
SM
3050 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3051 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 3052
094b34ac 3053 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
3054}
3055
3056/* Delete function for a quick_file_names. */
3057
3058static void
3059delete_file_name_entry (void *e)
3060{
9a3c8263 3061 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
3062 int i;
3063
3064 for (i = 0; i < file_data->num_file_names; ++i)
3065 {
3066 xfree ((void*) file_data->file_names[i]);
3067 if (file_data->real_names)
3068 xfree ((void*) file_data->real_names[i]);
3069 }
3070
3071 /* The space for the struct itself lives on objfile_obstack,
3072 so we don't free it here. */
3073}
3074
3075/* Create a quick_file_names hash table. */
3076
3077static htab_t
3078create_quick_file_names_table (unsigned int nr_initial_entries)
3079{
3080 return htab_create_alloc (nr_initial_entries,
3081 hash_file_name_entry, eq_file_name_entry,
3082 delete_file_name_entry, xcalloc, xfree);
3083}
9291a0cd 3084
918dd910
JK
3085/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3086 have to be created afterwards. You should call age_cached_comp_units after
3087 processing PER_CU->CU. dw2_setup must have been already called. */
3088
3089static void
3090load_cu (struct dwarf2_per_cu_data *per_cu)
3091{
3019eac3 3092 if (per_cu->is_debug_types)
e5fe5e75 3093 load_full_type_unit (per_cu);
918dd910 3094 else
95554aad 3095 load_full_comp_unit (per_cu, language_minimal);
918dd910 3096
cc12ce38
DE
3097 if (per_cu->cu == NULL)
3098 return; /* Dummy CU. */
2dc860c0
DE
3099
3100 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
3101}
3102
a0f42c21 3103/* Read in the symbols for PER_CU. */
2fdf6df6 3104
9291a0cd 3105static void
a0f42c21 3106dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
3107{
3108 struct cleanup *back_to;
ed2dc618 3109 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 3110
f4dc4d17
DE
3111 /* Skip type_unit_groups, reading the type units they contain
3112 is handled elsewhere. */
3113 if (IS_TYPE_UNIT_GROUP (per_cu))
3114 return;
3115
9291a0cd
TT
3116 back_to = make_cleanup (dwarf2_release_queue, NULL);
3117
95554aad 3118 if (dwarf2_per_objfile->using_index
43f3e411 3119 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
3120 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3121 {
3122 queue_comp_unit (per_cu, language_minimal);
3123 load_cu (per_cu);
89e63ee4
DE
3124
3125 /* If we just loaded a CU from a DWO, and we're working with an index
3126 that may badly handle TUs, load all the TUs in that DWO as well.
3127 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3128 if (!per_cu->is_debug_types
cc12ce38 3129 && per_cu->cu != NULL
89e63ee4
DE
3130 && per_cu->cu->dwo_unit != NULL
3131 && dwarf2_per_objfile->index_table != NULL
3132 && dwarf2_per_objfile->index_table->version <= 7
3133 /* DWP files aren't supported yet. */
ed2dc618 3134 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 3135 queue_and_load_all_dwo_tus (per_cu);
95554aad 3136 }
9291a0cd 3137
ed2dc618 3138 process_queue (dwarf2_per_objfile);
9291a0cd
TT
3139
3140 /* Age the cache, releasing compilation units that have not
3141 been used recently. */
ed2dc618 3142 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
3143
3144 do_cleanups (back_to);
3145}
3146
3147/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3148 the objfile from which this CU came. Returns the resulting symbol
3149 table. */
2fdf6df6 3150
43f3e411 3151static struct compunit_symtab *
a0f42c21 3152dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 3153{
ed2dc618
SM
3154 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3155
95554aad 3156 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 3157 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 3158 {
ed2dc618
SM
3159 struct cleanup *back_to = make_cleanup (free_cached_comp_units,
3160 dwarf2_per_objfile);
c83dd867 3161 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 3162 dw2_do_instantiate_symtab (per_cu);
ed2dc618 3163 process_cu_includes (dwarf2_per_objfile);
9291a0cd
TT
3164 do_cleanups (back_to);
3165 }
f194fefb 3166
43f3e411 3167 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
3168}
3169
8832e7e3 3170/* Return the CU/TU given its index.
f4dc4d17
DE
3171
3172 This is intended for loops like:
3173
3174 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3175 + dwarf2_per_objfile->n_type_units); ++i)
3176 {
8832e7e3 3177 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
3178
3179 ...;
3180 }
3181*/
2fdf6df6 3182
1fd400ff 3183static struct dwarf2_per_cu_data *
ed2dc618
SM
3184dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
3185 int index)
1fd400ff
TT
3186{
3187 if (index >= dwarf2_per_objfile->n_comp_units)
3188 {
f4dc4d17 3189 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
3190 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3191 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
3192 }
3193
3194 return dwarf2_per_objfile->all_comp_units[index];
3195}
3196
8832e7e3
DE
3197/* Return the CU given its index.
3198 This differs from dw2_get_cutu in that it's for when you know INDEX
3199 refers to a CU. */
f4dc4d17
DE
3200
3201static struct dwarf2_per_cu_data *
ed2dc618 3202dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
f4dc4d17 3203{
8832e7e3 3204 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 3205
1fd400ff
TT
3206 return dwarf2_per_objfile->all_comp_units[index];
3207}
3208
4b514bc8
JK
3209/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3210 objfile_obstack, and constructed with the specified field
3211 values. */
3212
3213static dwarf2_per_cu_data *
ed2dc618 3214create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
3215 struct dwarf2_section_info *section,
3216 int is_dwz,
3217 sect_offset sect_off, ULONGEST length)
3218{
ed2dc618 3219 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
3220 dwarf2_per_cu_data *the_cu
3221 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3222 struct dwarf2_per_cu_data);
3223 the_cu->sect_off = sect_off;
3224 the_cu->length = length;
e3b94546 3225 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
3226 the_cu->section = section;
3227 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3228 struct dwarf2_per_cu_quick_data);
3229 the_cu->is_dwz = is_dwz;
3230 return the_cu;
3231}
3232
2ec9a5e0
TT
3233/* A helper for create_cus_from_index that handles a given list of
3234 CUs. */
2fdf6df6 3235
74a0d9f6 3236static void
2ec9a5e0
TT
3237create_cus_from_index_list (struct objfile *objfile,
3238 const gdb_byte *cu_list, offset_type n_elements,
3239 struct dwarf2_section_info *section,
3240 int is_dwz,
3241 int base_offset)
9291a0cd
TT
3242{
3243 offset_type i;
ed2dc618
SM
3244 struct dwarf2_per_objfile *dwarf2_per_objfile
3245 = get_dwarf2_per_objfile (objfile);
9291a0cd 3246
2ec9a5e0 3247 for (i = 0; i < n_elements; i += 2)
9291a0cd 3248 {
74a0d9f6 3249 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3250
3251 sect_offset sect_off
3252 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3253 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3254 cu_list += 2 * 8;
3255
4b514bc8 3256 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
ed2dc618
SM
3257 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3258 sect_off, length);
9291a0cd 3259 }
9291a0cd
TT
3260}
3261
2ec9a5e0 3262/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3263 the CU objects for this objfile. */
2ec9a5e0 3264
74a0d9f6 3265static void
2ec9a5e0
TT
3266create_cus_from_index (struct objfile *objfile,
3267 const gdb_byte *cu_list, offset_type cu_list_elements,
3268 const gdb_byte *dwz_list, offset_type dwz_elements)
3269{
3270 struct dwz_file *dwz;
ed2dc618
SM
3271 struct dwarf2_per_objfile *dwarf2_per_objfile
3272 = get_dwarf2_per_objfile (objfile);
2ec9a5e0
TT
3273
3274 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
3275 dwarf2_per_objfile->all_comp_units =
3276 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3277 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 3278
74a0d9f6
JK
3279 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3280 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
3281
3282 if (dwz_elements == 0)
74a0d9f6 3283 return;
2ec9a5e0 3284
ed2dc618 3285 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
74a0d9f6
JK
3286 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3287 cu_list_elements / 2);
2ec9a5e0
TT
3288}
3289
1fd400ff 3290/* Create the signatured type hash table from the index. */
673bfd45 3291
74a0d9f6 3292static void
673bfd45 3293create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3294 struct dwarf2_section_info *section,
673bfd45
DE
3295 const gdb_byte *bytes,
3296 offset_type elements)
1fd400ff
TT
3297{
3298 offset_type i;
673bfd45 3299 htab_t sig_types_hash;
ed2dc618
SM
3300 struct dwarf2_per_objfile *dwarf2_per_objfile
3301 = get_dwarf2_per_objfile (objfile);
1fd400ff 3302
6aa5f3a6
DE
3303 dwarf2_per_objfile->n_type_units
3304 = dwarf2_per_objfile->n_allocated_type_units
3305 = elements / 3;
8d749320
SM
3306 dwarf2_per_objfile->all_type_units =
3307 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3308
673bfd45 3309 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3310
3311 for (i = 0; i < elements; i += 3)
3312 {
52dc124a 3313 struct signatured_type *sig_type;
9c541725 3314 ULONGEST signature;
1fd400ff 3315 void **slot;
9c541725 3316 cu_offset type_offset_in_tu;
1fd400ff 3317
74a0d9f6 3318 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3319 sect_offset sect_off
3320 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3321 type_offset_in_tu
3322 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3323 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3324 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3325 bytes += 3 * 8;
3326
52dc124a 3327 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3328 struct signatured_type);
52dc124a 3329 sig_type->signature = signature;
9c541725 3330 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3331 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3332 sig_type->per_cu.section = section;
9c541725 3333 sig_type->per_cu.sect_off = sect_off;
e3b94546 3334 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3335 sig_type->per_cu.v.quick
1fd400ff
TT
3336 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3337 struct dwarf2_per_cu_quick_data);
3338
52dc124a
DE
3339 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3340 *slot = sig_type;
1fd400ff 3341
b4dd5633 3342 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3343 }
3344
673bfd45 3345 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3346}
3347
927aa2e7
JK
3348/* Create the signatured type hash table from .debug_names. */
3349
3350static void
3351create_signatured_type_table_from_debug_names
ed2dc618 3352 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3353 const mapped_debug_names &map,
3354 struct dwarf2_section_info *section,
3355 struct dwarf2_section_info *abbrev_section)
3356{
ed2dc618
SM
3357 struct objfile *objfile = dwarf2_per_objfile->objfile;
3358
927aa2e7
JK
3359 dwarf2_read_section (objfile, section);
3360 dwarf2_read_section (objfile, abbrev_section);
3361
3362 dwarf2_per_objfile->n_type_units
3363 = dwarf2_per_objfile->n_allocated_type_units
3364 = map.tu_count;
3365 dwarf2_per_objfile->all_type_units
3366 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3367
3368 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3369
3370 for (uint32_t i = 0; i < map.tu_count; ++i)
3371 {
3372 struct signatured_type *sig_type;
3373 ULONGEST signature;
3374 void **slot;
3375 cu_offset type_offset_in_tu;
3376
3377 sect_offset sect_off
3378 = (sect_offset) (extract_unsigned_integer
3379 (map.tu_table_reordered + i * map.offset_size,
3380 map.offset_size,
3381 map.dwarf5_byte_order));
3382
3383 comp_unit_head cu_header;
ed2dc618
SM
3384 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3385 abbrev_section,
927aa2e7
JK
3386 section->buffer + to_underlying (sect_off),
3387 rcuh_kind::TYPE);
3388
3389 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3390 struct signatured_type);
3391 sig_type->signature = cu_header.signature;
3392 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3393 sig_type->per_cu.is_debug_types = 1;
3394 sig_type->per_cu.section = section;
3395 sig_type->per_cu.sect_off = sect_off;
e3b94546 3396 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3397 sig_type->per_cu.v.quick
3398 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3399 struct dwarf2_per_cu_quick_data);
3400
3401 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3402 *slot = sig_type;
3403
3404 dwarf2_per_objfile->all_type_units[i] = sig_type;
3405 }
3406
3407 dwarf2_per_objfile->signatured_types = sig_types_hash;
3408}
3409
9291a0cd
TT
3410/* Read the address map data from the mapped index, and use it to
3411 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3412
9291a0cd 3413static void
ed2dc618
SM
3414create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3415 struct mapped_index *index)
9291a0cd 3416{
ed2dc618 3417 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3418 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3419 const gdb_byte *iter, *end;
9291a0cd 3420 struct addrmap *mutable_map;
9291a0cd
TT
3421 CORE_ADDR baseaddr;
3422
8268c778
PA
3423 auto_obstack temp_obstack;
3424
9291a0cd
TT
3425 mutable_map = addrmap_create_mutable (&temp_obstack);
3426
f00a2de2
PA
3427 iter = index->address_table.data ();
3428 end = iter + index->address_table.size ();
9291a0cd
TT
3429
3430 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3431
3432 while (iter < end)
3433 {
3434 ULONGEST hi, lo, cu_index;
3435 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3436 iter += 8;
3437 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3438 iter += 8;
3439 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3440 iter += 4;
f652bce2 3441
24a55014 3442 if (lo > hi)
f652bce2 3443 {
24a55014
DE
3444 complaint (&symfile_complaints,
3445 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3446 hex_string (lo), hex_string (hi));
24a55014 3447 continue;
f652bce2 3448 }
24a55014
DE
3449
3450 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3451 {
3452 complaint (&symfile_complaints,
3453 _(".gdb_index address table has invalid CU number %u"),
3454 (unsigned) cu_index);
24a55014 3455 continue;
f652bce2 3456 }
24a55014 3457
3e29f34a
MR
3458 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3459 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
ed2dc618
SM
3460 addrmap_set_empty (mutable_map, lo, hi - 1,
3461 dw2_get_cutu (dwarf2_per_objfile, cu_index));
9291a0cd
TT
3462 }
3463
3464 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3465 &objfile->objfile_obstack);
9291a0cd
TT
3466}
3467
927aa2e7
JK
3468/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3469 populate the objfile's psymtabs_addrmap. */
3470
3471static void
ed2dc618 3472create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3473 struct dwarf2_section_info *section)
3474{
ed2dc618 3475 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3476 bfd *abfd = objfile->obfd;
3477 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3478 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3479 SECT_OFF_TEXT (objfile));
3480
3481 auto_obstack temp_obstack;
3482 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3483
3484 std::unordered_map<sect_offset,
3485 dwarf2_per_cu_data *,
3486 gdb::hash_enum<sect_offset>>
3487 debug_info_offset_to_per_cu;
3488 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3489 {
ed2dc618 3490 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
927aa2e7
JK
3491 const auto insertpair
3492 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3493 if (!insertpair.second)
3494 {
3495 warning (_("Section .debug_aranges in %s has duplicate "
3496 "debug_info_offset %u, ignoring .debug_aranges."),
3497 objfile_name (objfile), to_underlying (per_cu->sect_off));
3498 return;
3499 }
3500 }
3501
3502 dwarf2_read_section (objfile, section);
3503
3504 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3505
3506 const gdb_byte *addr = section->buffer;
3507
3508 while (addr < section->buffer + section->size)
3509 {
3510 const gdb_byte *const entry_addr = addr;
3511 unsigned int bytes_read;
3512
3513 const LONGEST entry_length = read_initial_length (abfd, addr,
3514 &bytes_read);
3515 addr += bytes_read;
3516
3517 const gdb_byte *const entry_end = addr + entry_length;
3518 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3519 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3520 if (addr + entry_length > section->buffer + section->size)
3521 {
3522 warning (_("Section .debug_aranges in %s entry at offset %zu "
3523 "length %s exceeds section length %s, "
3524 "ignoring .debug_aranges."),
3525 objfile_name (objfile), entry_addr - section->buffer,
3526 plongest (bytes_read + entry_length),
3527 pulongest (section->size));
3528 return;
3529 }
3530
3531 /* The version number. */
3532 const uint16_t version = read_2_bytes (abfd, addr);
3533 addr += 2;
3534 if (version != 2)
3535 {
3536 warning (_("Section .debug_aranges in %s entry at offset %zu "
3537 "has unsupported version %d, ignoring .debug_aranges."),
3538 objfile_name (objfile), entry_addr - section->buffer,
3539 version);
3540 return;
3541 }
3542
3543 const uint64_t debug_info_offset
3544 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3545 addr += offset_size;
3546 const auto per_cu_it
3547 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3548 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3549 {
3550 warning (_("Section .debug_aranges in %s entry at offset %zu "
3551 "debug_info_offset %s does not exists, "
3552 "ignoring .debug_aranges."),
3553 objfile_name (objfile), entry_addr - section->buffer,
3554 pulongest (debug_info_offset));
3555 return;
3556 }
3557 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3558
3559 const uint8_t address_size = *addr++;
3560 if (address_size < 1 || address_size > 8)
3561 {
3562 warning (_("Section .debug_aranges in %s entry at offset %zu "
3563 "address_size %u is invalid, ignoring .debug_aranges."),
3564 objfile_name (objfile), entry_addr - section->buffer,
3565 address_size);
3566 return;
3567 }
3568
3569 const uint8_t segment_selector_size = *addr++;
3570 if (segment_selector_size != 0)
3571 {
3572 warning (_("Section .debug_aranges in %s entry at offset %zu "
3573 "segment_selector_size %u is not supported, "
3574 "ignoring .debug_aranges."),
3575 objfile_name (objfile), entry_addr - section->buffer,
3576 segment_selector_size);
3577 return;
3578 }
3579
3580 /* Must pad to an alignment boundary that is twice the address
3581 size. It is undocumented by the DWARF standard but GCC does
3582 use it. */
3583 for (size_t padding = ((-(addr - section->buffer))
3584 & (2 * address_size - 1));
3585 padding > 0; padding--)
3586 if (*addr++ != 0)
3587 {
3588 warning (_("Section .debug_aranges in %s entry at offset %zu "
3589 "padding is not zero, ignoring .debug_aranges."),
3590 objfile_name (objfile), entry_addr - section->buffer);
3591 return;
3592 }
3593
3594 for (;;)
3595 {
3596 if (addr + 2 * address_size > entry_end)
3597 {
3598 warning (_("Section .debug_aranges in %s entry at offset %zu "
3599 "address list is not properly terminated, "
3600 "ignoring .debug_aranges."),
3601 objfile_name (objfile), entry_addr - section->buffer);
3602 return;
3603 }
3604 ULONGEST start = extract_unsigned_integer (addr, address_size,
3605 dwarf5_byte_order);
3606 addr += address_size;
3607 ULONGEST length = extract_unsigned_integer (addr, address_size,
3608 dwarf5_byte_order);
3609 addr += address_size;
3610 if (start == 0 && length == 0)
3611 break;
3612 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3613 {
3614 /* Symbol was eliminated due to a COMDAT group. */
3615 continue;
3616 }
3617 ULONGEST end = start + length;
3618 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3619 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3620 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3621 }
3622 }
3623
3624 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3625 &objfile->objfile_obstack);
3626}
3627
59d7bcaf
JK
3628/* The hash function for strings in the mapped index. This is the same as
3629 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3630 implementation. This is necessary because the hash function is tied to the
3631 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3632 SYMBOL_HASH_NEXT.
3633
3634 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3635
9291a0cd 3636static hashval_t
559a7a62 3637mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3638{
3639 const unsigned char *str = (const unsigned char *) p;
3640 hashval_t r = 0;
3641 unsigned char c;
3642
3643 while ((c = *str++) != 0)
559a7a62
JK
3644 {
3645 if (index_version >= 5)
3646 c = tolower (c);
3647 r = r * 67 + c - 113;
3648 }
9291a0cd
TT
3649
3650 return r;
3651}
3652
3653/* Find a slot in the mapped index INDEX for the object named NAME.
3654 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3655 constant pool and return true. If NAME cannot be found, return
3656 false. */
2fdf6df6 3657
109483d9 3658static bool
9291a0cd
TT
3659find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3660 offset_type **vec_out)
3661{
0cf03b49 3662 offset_type hash;
9291a0cd 3663 offset_type slot, step;
559a7a62 3664 int (*cmp) (const char *, const char *);
9291a0cd 3665
791afaa2 3666 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3667 if (current_language->la_language == language_cplus
45280282
IB
3668 || current_language->la_language == language_fortran
3669 || current_language->la_language == language_d)
0cf03b49
JK
3670 {
3671 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3672 not contain any. */
a8719064 3673
72998fb3 3674 if (strchr (name, '(') != NULL)
0cf03b49 3675 {
109483d9 3676 without_params = cp_remove_params (name);
0cf03b49 3677
72998fb3 3678 if (without_params != NULL)
791afaa2 3679 name = without_params.get ();
0cf03b49
JK
3680 }
3681 }
3682
559a7a62 3683 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3684 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3685 simulate our NAME being searched is also lowercased. */
3686 hash = mapped_index_string_hash ((index->version == 4
3687 && case_sensitivity == case_sensitive_off
3688 ? 5 : index->version),
3689 name);
3690
f00a2de2
PA
3691 slot = hash & (index->symbol_table.size () - 1);
3692 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3693 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3694
3695 for (;;)
3696 {
9291a0cd 3697 const char *str;
f00a2de2
PA
3698
3699 const auto &bucket = index->symbol_table[slot];
3700 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3701 return false;
9291a0cd 3702
f00a2de2 3703 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3704 if (!cmp (name, str))
9291a0cd
TT
3705 {
3706 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3707 + MAYBE_SWAP (bucket.vec));
109483d9 3708 return true;
9291a0cd
TT
3709 }
3710
f00a2de2 3711 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3712 }
3713}
3714
2ec9a5e0
TT
3715/* A helper function that reads the .gdb_index from SECTION and fills
3716 in MAP. FILENAME is the name of the file containing the section;
3717 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3718 ok to use deprecated sections.
3719
3720 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3721 out parameters that are filled in with information about the CU and
3722 TU lists in the section.
3723
3724 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3725
9291a0cd 3726static int
2ec9a5e0
TT
3727read_index_from_section (struct objfile *objfile,
3728 const char *filename,
3729 int deprecated_ok,
3730 struct dwarf2_section_info *section,
3731 struct mapped_index *map,
3732 const gdb_byte **cu_list,
3733 offset_type *cu_list_elements,
3734 const gdb_byte **types_list,
3735 offset_type *types_list_elements)
9291a0cd 3736{
948f8e3d 3737 const gdb_byte *addr;
2ec9a5e0 3738 offset_type version;
b3b272e1 3739 offset_type *metadata;
1fd400ff 3740 int i;
9291a0cd 3741
2ec9a5e0 3742 if (dwarf2_section_empty_p (section))
9291a0cd 3743 return 0;
82430852
JK
3744
3745 /* Older elfutils strip versions could keep the section in the main
3746 executable while splitting it for the separate debug info file. */
a32a8923 3747 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3748 return 0;
3749
2ec9a5e0 3750 dwarf2_read_section (objfile, section);
9291a0cd 3751
2ec9a5e0 3752 addr = section->buffer;
9291a0cd 3753 /* Version check. */
1fd400ff 3754 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3755 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3756 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3757 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3758 indices. */
831adc1f 3759 if (version < 4)
481860b3
GB
3760 {
3761 static int warning_printed = 0;
3762 if (!warning_printed)
3763 {
3764 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3765 filename);
481860b3
GB
3766 warning_printed = 1;
3767 }
3768 return 0;
3769 }
3770 /* Index version 4 uses a different hash function than index version
3771 5 and later.
3772
3773 Versions earlier than 6 did not emit psymbols for inlined
3774 functions. Using these files will cause GDB not to be able to
3775 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3776 indices unless the user has done
3777 "set use-deprecated-index-sections on". */
2ec9a5e0 3778 if (version < 6 && !deprecated_ok)
481860b3
GB
3779 {
3780 static int warning_printed = 0;
3781 if (!warning_printed)
3782 {
e615022a
DE
3783 warning (_("\
3784Skipping deprecated .gdb_index section in %s.\n\
3785Do \"set use-deprecated-index-sections on\" before the file is read\n\
3786to use the section anyway."),
2ec9a5e0 3787 filename);
481860b3
GB
3788 warning_printed = 1;
3789 }
3790 return 0;
3791 }
796a7ff8 3792 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3793 of the TU (for symbols coming from TUs),
3794 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3795 Plus gold-generated indices can have duplicate entries for global symbols,
3796 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3797 These are just performance bugs, and we can't distinguish gdb-generated
3798 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3799
481860b3 3800 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3801 longer backward compatible. */
796a7ff8 3802 if (version > 8)
594e8718 3803 return 0;
9291a0cd 3804
559a7a62 3805 map->version = version;
2ec9a5e0 3806 map->total_size = section->size;
9291a0cd
TT
3807
3808 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3809
3810 i = 0;
2ec9a5e0
TT
3811 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3812 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3813 / 8);
1fd400ff
TT
3814 ++i;
3815
2ec9a5e0
TT
3816 *types_list = addr + MAYBE_SWAP (metadata[i]);
3817 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3818 - MAYBE_SWAP (metadata[i]))
3819 / 8);
987d643c 3820 ++i;
1fd400ff 3821
f00a2de2
PA
3822 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3823 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3824 map->address_table
3825 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3826 ++i;
3827
f00a2de2
PA
3828 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3829 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3830 map->symbol_table
3831 = gdb::array_view<mapped_index::symbol_table_slot>
3832 ((mapped_index::symbol_table_slot *) symbol_table,
3833 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3834
f00a2de2 3835 ++i;
f9d83a0b 3836 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3837
2ec9a5e0
TT
3838 return 1;
3839}
3840
927aa2e7 3841/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3842 elements of all the CUs and return 1. Otherwise, return 0. */
3843
3844static int
3845dwarf2_read_index (struct objfile *objfile)
3846{
3847 struct mapped_index local_map, *map;
3848 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3849 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3850 struct dwz_file *dwz;
ed2dc618
SM
3851 struct dwarf2_per_objfile *dwarf2_per_objfile
3852 = get_dwarf2_per_objfile (objfile);
2ec9a5e0 3853
4262abfb 3854 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3855 use_deprecated_index_sections,
3856 &dwarf2_per_objfile->gdb_index, &local_map,
3857 &cu_list, &cu_list_elements,
3858 &types_list, &types_list_elements))
3859 return 0;
3860
0fefef59 3861 /* Don't use the index if it's empty. */
f00a2de2 3862 if (local_map.symbol_table.empty ())
0fefef59
DE
3863 return 0;
3864
2ec9a5e0
TT
3865 /* If there is a .dwz file, read it so we can get its CU list as
3866 well. */
ed2dc618 3867 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3868 if (dwz != NULL)
2ec9a5e0 3869 {
2ec9a5e0
TT
3870 struct mapped_index dwz_map;
3871 const gdb_byte *dwz_types_ignore;
3872 offset_type dwz_types_elements_ignore;
3873
3874 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3875 1,
3876 &dwz->gdb_index, &dwz_map,
3877 &dwz_list, &dwz_list_elements,
3878 &dwz_types_ignore,
3879 &dwz_types_elements_ignore))
3880 {
3881 warning (_("could not read '.gdb_index' section from %s; skipping"),
3882 bfd_get_filename (dwz->dwz_bfd));
3883 return 0;
3884 }
3885 }
3886
74a0d9f6
JK
3887 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3888 dwz_list_elements);
1fd400ff 3889
8b70b953
TT
3890 if (types_list_elements)
3891 {
3892 struct dwarf2_section_info *section;
3893
3894 /* We can only handle a single .debug_types when we have an
3895 index. */
3896 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3897 return 0;
3898
3899 section = VEC_index (dwarf2_section_info_def,
3900 dwarf2_per_objfile->types, 0);
3901
74a0d9f6
JK
3902 create_signatured_type_table_from_index (objfile, section, types_list,
3903 types_list_elements);
8b70b953 3904 }
9291a0cd 3905
ed2dc618 3906 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
2ec9a5e0 3907
8d749320 3908 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3f563c84 3909 map = new (map) mapped_index ();
2ec9a5e0 3910 *map = local_map;
9291a0cd
TT
3911
3912 dwarf2_per_objfile->index_table = map;
3913 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3914 dwarf2_per_objfile->quick_file_names_table =
3915 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3916
3917 return 1;
3918}
3919
dee91e82 3920/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3921
dee91e82
DE
3922static void
3923dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3924 const gdb_byte *info_ptr,
dee91e82
DE
3925 struct die_info *comp_unit_die,
3926 int has_children,
3927 void *data)
9291a0cd 3928{
dee91e82 3929 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3930 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3931 struct dwarf2_per_objfile *dwarf2_per_objfile
3932 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3933 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3934 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3935 struct attribute *attr;
dee91e82 3936 int i;
7b9f3c50
DE
3937 void **slot;
3938 struct quick_file_names *qfn;
9291a0cd 3939
0186c6a7
DE
3940 gdb_assert (! this_cu->is_debug_types);
3941
07261596
TT
3942 /* Our callers never want to match partial units -- instead they
3943 will match the enclosing full CU. */
3944 if (comp_unit_die->tag == DW_TAG_partial_unit)
3945 {
3946 this_cu->v.quick->no_file_data = 1;
3947 return;
3948 }
3949
0186c6a7 3950 lh_cu = this_cu;
7b9f3c50 3951 slot = NULL;
dee91e82 3952
fff8551c 3953 line_header_up lh;
9c541725 3954 sect_offset line_offset {};
fff8551c 3955
dee91e82 3956 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3957 if (attr)
3958 {
7b9f3c50
DE
3959 struct quick_file_names find_entry;
3960
9c541725 3961 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3962
3963 /* We may have already read in this line header (TU line header sharing).
3964 If we have we're done. */
094b34ac 3965 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3966 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3967 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3968 &find_entry, INSERT);
3969 if (*slot != NULL)
3970 {
9a3c8263 3971 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3972 return;
7b9f3c50
DE
3973 }
3974
3019eac3 3975 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3976 }
3977 if (lh == NULL)
3978 {
094b34ac 3979 lh_cu->v.quick->no_file_data = 1;
dee91e82 3980 return;
9291a0cd
TT
3981 }
3982
8d749320 3983 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3984 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3985 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3986 gdb_assert (slot != NULL);
3987 *slot = qfn;
9291a0cd 3988
d721ba37 3989 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3990
fff8551c 3991 qfn->num_file_names = lh->file_names.size ();
8d749320 3992 qfn->file_names =
fff8551c
PA
3993 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3994 for (i = 0; i < lh->file_names.size (); ++i)
3995 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3996 qfn->real_names = NULL;
9291a0cd 3997
094b34ac 3998 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3999}
4000
4001/* A helper for the "quick" functions which attempts to read the line
4002 table for THIS_CU. */
4003
4004static struct quick_file_names *
e4a48d9d 4005dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 4006{
0186c6a7
DE
4007 /* This should never be called for TUs. */
4008 gdb_assert (! this_cu->is_debug_types);
4009 /* Nor type unit groups. */
4010 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 4011
dee91e82
DE
4012 if (this_cu->v.quick->file_names != NULL)
4013 return this_cu->v.quick->file_names;
4014 /* If we know there is no line data, no point in looking again. */
4015 if (this_cu->v.quick->no_file_data)
4016 return NULL;
4017
0186c6a7 4018 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
4019
4020 if (this_cu->v.quick->no_file_data)
4021 return NULL;
4022 return this_cu->v.quick->file_names;
9291a0cd
TT
4023}
4024
4025/* A helper for the "quick" functions which computes and caches the
7b9f3c50 4026 real path for a given file name from the line table. */
2fdf6df6 4027
9291a0cd 4028static const char *
7b9f3c50
DE
4029dw2_get_real_path (struct objfile *objfile,
4030 struct quick_file_names *qfn, int index)
9291a0cd 4031{
7b9f3c50
DE
4032 if (qfn->real_names == NULL)
4033 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 4034 qfn->num_file_names, const char *);
9291a0cd 4035
7b9f3c50 4036 if (qfn->real_names[index] == NULL)
14278e1f 4037 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 4038
7b9f3c50 4039 return qfn->real_names[index];
9291a0cd
TT
4040}
4041
4042static struct symtab *
4043dw2_find_last_source_symtab (struct objfile *objfile)
4044{
ed2dc618
SM
4045 struct dwarf2_per_objfile *dwarf2_per_objfile
4046 = get_dwarf2_per_objfile (objfile);
4047 int index = dwarf2_per_objfile->n_comp_units - 1;
4048 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
4049 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
ae2de4f8 4050
43f3e411
DE
4051 if (cust == NULL)
4052 return NULL;
ed2dc618 4053
43f3e411 4054 return compunit_primary_filetab (cust);
9291a0cd
TT
4055}
4056
7b9f3c50
DE
4057/* Traversal function for dw2_forget_cached_source_info. */
4058
4059static int
4060dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 4061{
7b9f3c50 4062 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 4063
7b9f3c50 4064 if (file_data->real_names)
9291a0cd 4065 {
7b9f3c50 4066 int i;
9291a0cd 4067
7b9f3c50 4068 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 4069 {
7b9f3c50
DE
4070 xfree ((void*) file_data->real_names[i]);
4071 file_data->real_names[i] = NULL;
9291a0cd
TT
4072 }
4073 }
7b9f3c50
DE
4074
4075 return 1;
4076}
4077
4078static void
4079dw2_forget_cached_source_info (struct objfile *objfile)
4080{
ed2dc618
SM
4081 struct dwarf2_per_objfile *dwarf2_per_objfile
4082 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
4083
4084 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4085 dw2_free_cached_file_names, NULL);
9291a0cd
TT
4086}
4087
f8eba3c6
TT
4088/* Helper function for dw2_map_symtabs_matching_filename that expands
4089 the symtabs and calls the iterator. */
4090
4091static int
4092dw2_map_expand_apply (struct objfile *objfile,
4093 struct dwarf2_per_cu_data *per_cu,
f5b95b50 4094 const char *name, const char *real_path,
14bc53a8 4095 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 4096{
43f3e411 4097 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
4098
4099 /* Don't visit already-expanded CUs. */
43f3e411 4100 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
4101 return 0;
4102
4103 /* This may expand more than one symtab, and we want to iterate over
4104 all of them. */
a0f42c21 4105 dw2_instantiate_symtab (per_cu);
f8eba3c6 4106
14bc53a8
PA
4107 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4108 last_made, callback);
f8eba3c6
TT
4109}
4110
4111/* Implementation of the map_symtabs_matching_filename method. */
4112
14bc53a8
PA
4113static bool
4114dw2_map_symtabs_matching_filename
4115 (struct objfile *objfile, const char *name, const char *real_path,
4116 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
4117{
4118 int i;
c011a4f4 4119 const char *name_basename = lbasename (name);
ed2dc618
SM
4120 struct dwarf2_per_objfile *dwarf2_per_objfile
4121 = get_dwarf2_per_objfile (objfile);
ae2de4f8 4122
848e3e78
DE
4123 /* The rule is CUs specify all the files, including those used by
4124 any TU, so there's no need to scan TUs here. */
f4dc4d17 4125
ed2dc618 4126 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4127 {
4128 int j;
ed2dc618 4129 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
7b9f3c50 4130 struct quick_file_names *file_data;
9291a0cd 4131
3d7bb9d9 4132 /* We only need to look at symtabs not already expanded. */
43f3e411 4133 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4134 continue;
4135
e4a48d9d 4136 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4137 if (file_data == NULL)
9291a0cd
TT
4138 continue;
4139
7b9f3c50 4140 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4141 {
7b9f3c50 4142 const char *this_name = file_data->file_names[j];
da235a7c 4143 const char *this_real_name;
9291a0cd 4144
af529f8f 4145 if (compare_filenames_for_search (this_name, name))
9291a0cd 4146 {
f5b95b50 4147 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4148 callback))
4149 return true;
288e77a7 4150 continue;
4aac40c8 4151 }
9291a0cd 4152
c011a4f4
DE
4153 /* Before we invoke realpath, which can get expensive when many
4154 files are involved, do a quick comparison of the basenames. */
4155 if (! basenames_may_differ
4156 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4157 continue;
4158
da235a7c
JK
4159 this_real_name = dw2_get_real_path (objfile, file_data, j);
4160 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 4161 {
da235a7c 4162 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4163 callback))
4164 return true;
288e77a7 4165 continue;
da235a7c 4166 }
9291a0cd 4167
da235a7c
JK
4168 if (real_path != NULL)
4169 {
af529f8f
JK
4170 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4171 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 4172 if (this_real_name != NULL
af529f8f 4173 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 4174 {
f5b95b50 4175 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
4176 callback))
4177 return true;
288e77a7 4178 continue;
9291a0cd
TT
4179 }
4180 }
4181 }
4182 }
4183
14bc53a8 4184 return false;
9291a0cd
TT
4185}
4186
da51c347
DE
4187/* Struct used to manage iterating over all CUs looking for a symbol. */
4188
4189struct dw2_symtab_iterator
9291a0cd 4190{
ed2dc618
SM
4191 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
4192 struct dwarf2_per_objfile *dwarf2_per_objfile;
da51c347
DE
4193 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4194 int want_specific_block;
4195 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4196 Unused if !WANT_SPECIFIC_BLOCK. */
4197 int block_index;
4198 /* The kind of symbol we're looking for. */
4199 domain_enum domain;
4200 /* The list of CUs from the index entry of the symbol,
4201 or NULL if not found. */
4202 offset_type *vec;
4203 /* The next element in VEC to look at. */
4204 int next;
4205 /* The number of elements in VEC, or zero if there is no match. */
4206 int length;
8943b874
DE
4207 /* Have we seen a global version of the symbol?
4208 If so we can ignore all further global instances.
4209 This is to work around gold/15646, inefficient gold-generated
4210 indices. */
4211 int global_seen;
da51c347 4212};
9291a0cd 4213
da51c347
DE
4214/* Initialize the index symtab iterator ITER.
4215 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4216 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 4217
9291a0cd 4218static void
da51c347 4219dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 4220 struct dwarf2_per_objfile *dwarf2_per_objfile,
da51c347
DE
4221 int want_specific_block,
4222 int block_index,
4223 domain_enum domain,
4224 const char *name)
4225{
ed2dc618 4226 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
4227 iter->want_specific_block = want_specific_block;
4228 iter->block_index = block_index;
4229 iter->domain = domain;
4230 iter->next = 0;
8943b874 4231 iter->global_seen = 0;
da51c347 4232
ed2dc618
SM
4233 mapped_index *index = dwarf2_per_objfile->index_table;
4234
4235 /* index is NULL if OBJF_READNOW. */
4236 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
4237 iter->length = MAYBE_SWAP (*iter->vec);
4238 else
4239 {
4240 iter->vec = NULL;
4241 iter->length = 0;
4242 }
4243}
4244
4245/* Return the next matching CU or NULL if there are no more. */
4246
4247static struct dwarf2_per_cu_data *
4248dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4249{
ed2dc618
SM
4250 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4251
da51c347
DE
4252 for ( ; iter->next < iter->length; ++iter->next)
4253 {
4254 offset_type cu_index_and_attrs =
4255 MAYBE_SWAP (iter->vec[iter->next + 1]);
4256 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 4257 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
4258 int want_static = iter->block_index != GLOBAL_BLOCK;
4259 /* This value is only valid for index versions >= 7. */
4260 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4261 gdb_index_symbol_kind symbol_kind =
4262 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4263 /* Only check the symbol attributes if they're present.
4264 Indices prior to version 7 don't record them,
4265 and indices >= 7 may elide them for certain symbols
4266 (gold does this). */
4267 int attrs_valid =
ed2dc618 4268 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
4269 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4270
3190f0c6
DE
4271 /* Don't crash on bad data. */
4272 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4273 + dwarf2_per_objfile->n_type_units))
4274 {
4275 complaint (&symfile_complaints,
4276 _(".gdb_index entry has bad CU index"
4262abfb
JK
4277 " [in module %s]"),
4278 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
4279 continue;
4280 }
4281
ed2dc618 4282 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
3190f0c6 4283
da51c347 4284 /* Skip if already read in. */
43f3e411 4285 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
4286 continue;
4287
8943b874
DE
4288 /* Check static vs global. */
4289 if (attrs_valid)
4290 {
4291 if (iter->want_specific_block
4292 && want_static != is_static)
4293 continue;
4294 /* Work around gold/15646. */
4295 if (!is_static && iter->global_seen)
4296 continue;
4297 if (!is_static)
4298 iter->global_seen = 1;
4299 }
da51c347
DE
4300
4301 /* Only check the symbol's kind if it has one. */
4302 if (attrs_valid)
4303 {
4304 switch (iter->domain)
4305 {
4306 case VAR_DOMAIN:
4307 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4308 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4309 /* Some types are also in VAR_DOMAIN. */
4310 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4311 continue;
4312 break;
4313 case STRUCT_DOMAIN:
4314 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4315 continue;
4316 break;
4317 case LABEL_DOMAIN:
4318 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4319 continue;
4320 break;
4321 default:
4322 break;
4323 }
4324 }
4325
4326 ++iter->next;
4327 return per_cu;
4328 }
4329
4330 return NULL;
4331}
4332
43f3e411 4333static struct compunit_symtab *
da51c347
DE
4334dw2_lookup_symbol (struct objfile *objfile, int block_index,
4335 const char *name, domain_enum domain)
9291a0cd 4336{
43f3e411 4337 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4338 struct dwarf2_per_objfile *dwarf2_per_objfile
4339 = get_dwarf2_per_objfile (objfile);
9291a0cd 4340
b5ec771e
PA
4341 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4342
ed2dc618
SM
4343 struct dw2_symtab_iterator iter;
4344 struct dwarf2_per_cu_data *per_cu;
da51c347 4345
ed2dc618 4346 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
9291a0cd 4347
ed2dc618
SM
4348 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4349 {
4350 struct symbol *sym, *with_opaque = NULL;
4351 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4352 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4353 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4354
ed2dc618
SM
4355 sym = block_find_symbol (block, name, domain,
4356 block_find_non_opaque_type_preferred,
4357 &with_opaque);
b2e2f908 4358
ed2dc618
SM
4359 /* Some caution must be observed with overloaded functions
4360 and methods, since the index will not contain any overload
4361 information (but NAME might contain it). */
da51c347 4362
ed2dc618
SM
4363 if (sym != NULL
4364 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4365 return stab;
4366 if (with_opaque != NULL
4367 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4368 stab_best = stab;
da51c347 4369
ed2dc618 4370 /* Keep looking through other CUs. */
9291a0cd 4371 }
9291a0cd 4372
da51c347 4373 return stab_best;
9291a0cd
TT
4374}
4375
4376static void
4377dw2_print_stats (struct objfile *objfile)
4378{
ed2dc618
SM
4379 struct dwarf2_per_objfile *dwarf2_per_objfile
4380 = get_dwarf2_per_objfile (objfile);
4381 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4382 int count = 0;
9291a0cd 4383
ed2dc618 4384 for (int i = 0; i < total; ++i)
9291a0cd 4385 {
ed2dc618 4386 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 4387
43f3e411 4388 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4389 ++count;
4390 }
e4a48d9d 4391 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4392 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4393}
4394
779bd270
DE
4395/* This dumps minimal information about the index.
4396 It is called via "mt print objfiles".
4397 One use is to verify .gdb_index has been loaded by the
4398 gdb.dwarf2/gdb-index.exp testcase. */
4399
9291a0cd
TT
4400static void
4401dw2_dump (struct objfile *objfile)
4402{
ed2dc618
SM
4403 struct dwarf2_per_objfile *dwarf2_per_objfile
4404 = get_dwarf2_per_objfile (objfile);
4405
779bd270
DE
4406 gdb_assert (dwarf2_per_objfile->using_index);
4407 printf_filtered (".gdb_index:");
4408 if (dwarf2_per_objfile->index_table != NULL)
4409 {
4410 printf_filtered (" version %d\n",
4411 dwarf2_per_objfile->index_table->version);
4412 }
4413 else
4414 printf_filtered (" faked for \"readnow\"\n");
4415 printf_filtered ("\n");
9291a0cd
TT
4416}
4417
4418static void
3189cb12
DE
4419dw2_relocate (struct objfile *objfile,
4420 const struct section_offsets *new_offsets,
4421 const struct section_offsets *delta)
9291a0cd
TT
4422{
4423 /* There's nothing to relocate here. */
4424}
4425
4426static void
4427dw2_expand_symtabs_for_function (struct objfile *objfile,
4428 const char *func_name)
4429{
ed2dc618
SM
4430 struct dwarf2_per_objfile *dwarf2_per_objfile
4431 = get_dwarf2_per_objfile (objfile);
da51c347 4432
ed2dc618
SM
4433 struct dw2_symtab_iterator iter;
4434 struct dwarf2_per_cu_data *per_cu;
da51c347 4435
ed2dc618
SM
4436 /* Note: It doesn't matter what we pass for block_index here. */
4437 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4438 func_name);
da51c347 4439
ed2dc618
SM
4440 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4441 dw2_instantiate_symtab (per_cu);
da51c347 4442
9291a0cd
TT
4443}
4444
4445static void
4446dw2_expand_all_symtabs (struct objfile *objfile)
4447{
ed2dc618
SM
4448 struct dwarf2_per_objfile *dwarf2_per_objfile
4449 = get_dwarf2_per_objfile (objfile);
4450 int total_units = (dwarf2_per_objfile->n_comp_units
4451 + dwarf2_per_objfile->n_type_units);
9291a0cd 4452
ed2dc618 4453 for (int i = 0; i < total_units; ++i)
9291a0cd 4454 {
ed2dc618
SM
4455 struct dwarf2_per_cu_data *per_cu
4456 = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 4457
a0f42c21 4458 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4459 }
4460}
4461
4462static void
652a8996
JK
4463dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4464 const char *fullname)
9291a0cd 4465{
ed2dc618
SM
4466 struct dwarf2_per_objfile *dwarf2_per_objfile
4467 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4468
4469 /* We don't need to consider type units here.
4470 This is only called for examining code, e.g. expand_line_sal.
4471 There can be an order of magnitude (or more) more type units
4472 than comp units, and we avoid them if we can. */
4473
ed2dc618 4474 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4475 {
4476 int j;
ed2dc618 4477 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
7b9f3c50 4478 struct quick_file_names *file_data;
9291a0cd 4479
3d7bb9d9 4480 /* We only need to look at symtabs not already expanded. */
43f3e411 4481 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4482 continue;
4483
e4a48d9d 4484 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4485 if (file_data == NULL)
9291a0cd
TT
4486 continue;
4487
7b9f3c50 4488 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4489 {
652a8996
JK
4490 const char *this_fullname = file_data->file_names[j];
4491
4492 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4493 {
a0f42c21 4494 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4495 break;
4496 }
4497 }
4498 }
4499}
4500
9291a0cd 4501static void
ade7ed9e 4502dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4503 const char * name, domain_enum domain,
ade7ed9e 4504 int global,
40658b94
PH
4505 int (*callback) (struct block *,
4506 struct symbol *, void *),
b5ec771e 4507 void *data, symbol_name_match_type match,
2edb89d3 4508 symbol_compare_ftype *ordered_compare)
9291a0cd 4509{
40658b94 4510 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4511 current language is Ada for a non-Ada objfile using GNU index. As Ada
4512 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4513}
4514
b5ec771e
PA
4515/* Symbol name matcher for .gdb_index names.
4516
4517 Symbol names in .gdb_index have a few particularities:
4518
4519 - There's no indication of which is the language of each symbol.
4520
4521 Since each language has its own symbol name matching algorithm,
4522 and we don't know which language is the right one, we must match
3f563c84
PA
4523 each symbol against all languages. This would be a potential
4524 performance problem if it were not mitigated by the
4525 mapped_index::name_components lookup table, which significantly
4526 reduces the number of times we need to call into this matcher,
4527 making it a non-issue.
b5ec771e
PA
4528
4529 - Symbol names in the index have no overload (parameter)
4530 information. I.e., in C++, "foo(int)" and "foo(long)" both
4531 appear as "foo" in the index, for example.
4532
4533 This means that the lookup names passed to the symbol name
4534 matcher functions must have no parameter information either
4535 because (e.g.) symbol search name "foo" does not match
4536 lookup-name "foo(int)" [while swapping search name for lookup
4537 name would match].
4538*/
4539class gdb_index_symbol_name_matcher
4540{
4541public:
4542 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4543 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4544
4545 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4546 Returns true if any matcher matches. */
4547 bool matches (const char *symbol_name);
4548
4549private:
4550 /* A reference to the lookup name we're matching against. */
4551 const lookup_name_info &m_lookup_name;
4552
4553 /* A vector holding all the different symbol name matchers, for all
4554 languages. */
4555 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4556};
4557
4558gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4559 (const lookup_name_info &lookup_name)
4560 : m_lookup_name (lookup_name)
4561{
4562 /* Prepare the vector of comparison functions upfront, to avoid
4563 doing the same work for each symbol. Care is taken to avoid
4564 matching with the same matcher more than once if/when multiple
4565 languages use the same matcher function. */
4566 auto &matchers = m_symbol_name_matcher_funcs;
4567 matchers.reserve (nr_languages);
4568
4569 matchers.push_back (default_symbol_name_matcher);
4570
4571 for (int i = 0; i < nr_languages; i++)
4572 {
4573 const language_defn *lang = language_def ((enum language) i);
c63d3e8d 4574 symbol_name_matcher_ftype *name_matcher
618daa93 4575 = get_symbol_name_matcher (lang, m_lookup_name);
c63d3e8d
PA
4576
4577 /* Don't insert the same comparison routine more than once.
4578 Note that we do this linear walk instead of a seemingly
4579 cheaper sorted insert, or use a std::set or something like
4580 that, because relative order of function addresses is not
4581 stable. This is not a problem in practice because the number
4582 of supported languages is low, and the cost here is tiny
4583 compared to the number of searches we'll do afterwards using
4584 this object. */
4585 if (name_matcher != default_symbol_name_matcher
4586 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4587 == matchers.end ()))
4588 matchers.push_back (name_matcher);
b5ec771e
PA
4589 }
4590}
4591
4592bool
4593gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4594{
4595 for (auto matches_name : m_symbol_name_matcher_funcs)
4596 if (matches_name (symbol_name, m_lookup_name, NULL))
4597 return true;
4598
4599 return false;
4600}
4601
e1ef7d7a
PA
4602/* Starting from a search name, return the string that finds the upper
4603 bound of all strings that start with SEARCH_NAME in a sorted name
4604 list. Returns the empty string to indicate that the upper bound is
4605 the end of the list. */
4606
4607static std::string
4608make_sort_after_prefix_name (const char *search_name)
4609{
4610 /* When looking to complete "func", we find the upper bound of all
4611 symbols that start with "func" by looking for where we'd insert
4612 the closest string that would follow "func" in lexicographical
4613 order. Usually, that's "func"-with-last-character-incremented,
4614 i.e. "fund". Mind non-ASCII characters, though. Usually those
4615 will be UTF-8 multi-byte sequences, but we can't be certain.
4616 Especially mind the 0xff character, which is a valid character in
4617 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4618 rule out compilers allowing it in identifiers. Note that
4619 conveniently, strcmp/strcasecmp are specified to compare
4620 characters interpreted as unsigned char. So what we do is treat
4621 the whole string as a base 256 number composed of a sequence of
4622 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4623 to 0, and carries 1 to the following more-significant position.
4624 If the very first character in SEARCH_NAME ends up incremented
4625 and carries/overflows, then the upper bound is the end of the
4626 list. The string after the empty string is also the empty
4627 string.
4628
4629 Some examples of this operation:
4630
4631 SEARCH_NAME => "+1" RESULT
4632
4633 "abc" => "abd"
4634 "ab\xff" => "ac"
4635 "\xff" "a" "\xff" => "\xff" "b"
4636 "\xff" => ""
4637 "\xff\xff" => ""
4638 "" => ""
4639
4640 Then, with these symbols for example:
4641
4642 func
4643 func1
4644 fund
4645
4646 completing "func" looks for symbols between "func" and
4647 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4648 which finds "func" and "func1", but not "fund".
4649
4650 And with:
4651
4652 funcÿ (Latin1 'ÿ' [0xff])
4653 funcÿ1
4654 fund
4655
4656 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4657 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4658
4659 And with:
4660
4661 ÿÿ (Latin1 'ÿ' [0xff])
4662 ÿÿ1
4663
4664 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4665 the end of the list.
4666 */
4667 std::string after = search_name;
4668 while (!after.empty () && (unsigned char) after.back () == 0xff)
4669 after.pop_back ();
4670 if (!after.empty ())
4671 after.back () = (unsigned char) after.back () + 1;
4672 return after;
4673}
4674
5c58de74 4675/* See declaration. */
61d96d7e 4676
5c58de74
PA
4677std::pair<std::vector<name_component>::const_iterator,
4678 std::vector<name_component>::const_iterator>
44ed8f3e 4679mapped_index_base::find_name_components_bounds
5c58de74 4680 (const lookup_name_info &lookup_name_without_params) const
3f563c84 4681{
5c58de74
PA
4682 auto *name_cmp
4683 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84
PA
4684
4685 const char *cplus
c62446b1 4686 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4687
3f563c84
PA
4688 /* Comparison function object for lower_bound that matches against a
4689 given symbol name. */
4690 auto lookup_compare_lower = [&] (const name_component &elem,
4691 const char *name)
4692 {
5c58de74 4693 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4694 const char *elem_name = elem_qualified + elem.name_offset;
4695 return name_cmp (elem_name, name) < 0;
4696 };
4697
4698 /* Comparison function object for upper_bound that matches against a
4699 given symbol name. */
4700 auto lookup_compare_upper = [&] (const char *name,
4701 const name_component &elem)
4702 {
5c58de74 4703 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4704 const char *elem_name = elem_qualified + elem.name_offset;
4705 return name_cmp (name, elem_name) < 0;
4706 };
4707
5c58de74
PA
4708 auto begin = this->name_components.begin ();
4709 auto end = this->name_components.end ();
3f563c84
PA
4710
4711 /* Find the lower bound. */
4712 auto lower = [&] ()
4713 {
5c58de74 4714 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4715 return begin;
4716 else
4717 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4718 } ();
4719
4720 /* Find the upper bound. */
4721 auto upper = [&] ()
4722 {
5c58de74 4723 if (lookup_name_without_params.completion_mode ())
3f563c84 4724 {
e1ef7d7a
PA
4725 /* In completion mode, we want UPPER to point past all
4726 symbols names that have the same prefix. I.e., with
4727 these symbols, and completing "func":
4728
4729 function << lower bound
4730 function1
4731 other_function << upper bound
4732
4733 We find the upper bound by looking for the insertion
4734 point of "func"-with-last-character-incremented,
4735 i.e. "fund". */
4736 std::string after = make_sort_after_prefix_name (cplus);
4737 if (after.empty ())
3f563c84 4738 return end;
e6b2f5ef
PA
4739 return std::lower_bound (lower, end, after.c_str (),
4740 lookup_compare_lower);
3f563c84
PA
4741 }
4742 else
4743 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4744 } ();
4745
5c58de74
PA
4746 return {lower, upper};
4747}
4748
4749/* See declaration. */
4750
4751void
44ed8f3e 4752mapped_index_base::build_name_components ()
5c58de74
PA
4753{
4754 if (!this->name_components.empty ())
4755 return;
4756
4757 this->name_components_casing = case_sensitivity;
4758 auto *name_cmp
4759 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4760
4761 /* The code below only knows how to break apart components of C++
4762 symbol names (and other languages that use '::' as
4763 namespace/module separator). If we add support for wild matching
4764 to some language that uses some other operator (E.g., Ada, Go and
4765 D use '.'), then we'll need to try splitting the symbol name
4766 according to that language too. Note that Ada does support wild
4767 matching, but doesn't currently support .gdb_index. */
44ed8f3e
PA
4768 auto count = this->symbol_name_count ();
4769 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4770 {
44ed8f3e 4771 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4772 continue;
4773
4774 const char *name = this->symbol_name_at (idx);
4775
4776 /* Add each name component to the name component table. */
4777 unsigned int previous_len = 0;
4778 for (unsigned int current_len = cp_find_first_component (name);
4779 name[current_len] != '\0';
4780 current_len += cp_find_first_component (name + current_len))
4781 {
4782 gdb_assert (name[current_len] == ':');
4783 this->name_components.push_back ({previous_len, idx});
4784 /* Skip the '::'. */
4785 current_len += 2;
4786 previous_len = current_len;
4787 }
4788 this->name_components.push_back ({previous_len, idx});
4789 }
4790
4791 /* Sort name_components elements by name. */
4792 auto name_comp_compare = [&] (const name_component &left,
4793 const name_component &right)
4794 {
4795 const char *left_qualified = this->symbol_name_at (left.idx);
4796 const char *right_qualified = this->symbol_name_at (right.idx);
4797
4798 const char *left_name = left_qualified + left.name_offset;
4799 const char *right_name = right_qualified + right.name_offset;
4800
4801 return name_cmp (left_name, right_name) < 0;
4802 };
4803
4804 std::sort (this->name_components.begin (),
4805 this->name_components.end (),
4806 name_comp_compare);
4807}
4808
4809/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4810 mapped_index_base instead of the containing objfile. This is split
4811 to a separate function in order to be able to unit test the
4812 name_components matching using a mock mapped_index_base. For each
5c58de74 4813 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4814 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4815
4816static void
4817dw2_expand_symtabs_matching_symbol
44ed8f3e 4818 (mapped_index_base &index,
5c58de74
PA
4819 const lookup_name_info &lookup_name_in,
4820 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4821 enum search_domain kind,
4822 gdb::function_view<void (offset_type)> match_callback)
4823{
4824 lookup_name_info lookup_name_without_params
4825 = lookup_name_in.make_ignore_params ();
4826 gdb_index_symbol_name_matcher lookup_name_matcher
4827 (lookup_name_without_params);
4828
4829 /* Build the symbol name component sorted vector, if we haven't
4830 yet. */
4831 index.build_name_components ();
4832
4833 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4834
3f563c84
PA
4835 /* Now for each symbol name in range, check to see if we have a name
4836 match, and if so, call the MATCH_CALLBACK callback. */
4837
4838 /* The same symbol may appear more than once in the range though.
4839 E.g., if we're looking for symbols that complete "w", and we have
4840 a symbol named "w1::w2", we'll find the two name components for
4841 that same symbol in the range. To be sure we only call the
4842 callback once per symbol, we first collect the symbol name
4843 indexes that matched in a temporary vector and ignore
4844 duplicates. */
4845 std::vector<offset_type> matches;
5c58de74 4846 matches.reserve (std::distance (bounds.first, bounds.second));
3f563c84 4847
5c58de74 4848 for (; bounds.first != bounds.second; ++bounds.first)
3f563c84 4849 {
5c58de74 4850 const char *qualified = index.symbol_name_at (bounds.first->idx);
3f563c84
PA
4851
4852 if (!lookup_name_matcher.matches (qualified)
4853 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4854 continue;
4855
5c58de74 4856 matches.push_back (bounds.first->idx);
3f563c84
PA
4857 }
4858
4859 std::sort (matches.begin (), matches.end ());
4860
4861 /* Finally call the callback, once per match. */
4862 ULONGEST prev = -1;
4863 for (offset_type idx : matches)
4864 {
4865 if (prev != idx)
4866 {
4867 match_callback (idx);
4868 prev = idx;
4869 }
4870 }
4871
4872 /* Above we use a type wider than idx's for 'prev', since 0 and
4873 (offset_type)-1 are both possible values. */
4874 static_assert (sizeof (prev) > sizeof (offset_type), "");
4875}
4876
c62446b1
PA
4877#if GDB_SELF_TEST
4878
4879namespace selftests { namespace dw2_expand_symtabs_matching {
4880
a3c5fafd
PA
4881/* A mock .gdb_index/.debug_names-like name index table, enough to
4882 exercise dw2_expand_symtabs_matching_symbol, which works with the
4883 mapped_index_base interface. Builds an index from the symbol list
4884 passed as parameter to the constructor. */
4885class mock_mapped_index : public mapped_index_base
c62446b1
PA
4886{
4887public:
a3c5fafd
PA
4888 mock_mapped_index (gdb::array_view<const char *> symbols)
4889 : m_symbol_table (symbols)
c62446b1
PA
4890 {}
4891
a3c5fafd 4892 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4893
a3c5fafd
PA
4894 /* Return the number of names in the symbol table. */
4895 virtual size_t symbol_name_count () const
c62446b1 4896 {
a3c5fafd 4897 return m_symbol_table.size ();
c62446b1
PA
4898 }
4899
a3c5fafd
PA
4900 /* Get the name of the symbol at IDX in the symbol table. */
4901 virtual const char *symbol_name_at (offset_type idx) const
4902 {
4903 return m_symbol_table[idx];
4904 }
c62446b1 4905
a3c5fafd
PA
4906private:
4907 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4908};
4909
4910/* Convenience function that converts a NULL pointer to a "<null>"
4911 string, to pass to print routines. */
4912
4913static const char *
4914string_or_null (const char *str)
4915{
4916 return str != NULL ? str : "<null>";
4917}
4918
4919/* Check if a lookup_name_info built from
4920 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4921 index. EXPECTED_LIST is the list of expected matches, in expected
4922 matching order. If no match expected, then an empty list is
4923 specified. Returns true on success. On failure prints a warning
4924 indicating the file:line that failed, and returns false. */
4925
4926static bool
4927check_match (const char *file, int line,
4928 mock_mapped_index &mock_index,
4929 const char *name, symbol_name_match_type match_type,
4930 bool completion_mode,
4931 std::initializer_list<const char *> expected_list)
4932{
4933 lookup_name_info lookup_name (name, match_type, completion_mode);
4934
4935 bool matched = true;
4936
4937 auto mismatch = [&] (const char *expected_str,
4938 const char *got)
4939 {
4940 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4941 "expected=\"%s\", got=\"%s\"\n"),
4942 file, line,
4943 (match_type == symbol_name_match_type::FULL
4944 ? "FULL" : "WILD"),
4945 name, string_or_null (expected_str), string_or_null (got));
4946 matched = false;
4947 };
4948
4949 auto expected_it = expected_list.begin ();
4950 auto expected_end = expected_list.end ();
4951
a3c5fafd 4952 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4953 NULL, ALL_DOMAIN,
4954 [&] (offset_type idx)
4955 {
a3c5fafd 4956 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4957 const char *expected_str
4958 = expected_it == expected_end ? NULL : *expected_it++;
4959
4960 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4961 mismatch (expected_str, matched_name);
4962 });
4963
4964 const char *expected_str
4965 = expected_it == expected_end ? NULL : *expected_it++;
4966 if (expected_str != NULL)
4967 mismatch (expected_str, NULL);
4968
4969 return matched;
4970}
4971
4972/* The symbols added to the mock mapped_index for testing (in
4973 canonical form). */
4974static const char *test_symbols[] = {
4975 "function",
4976 "std::bar",
4977 "std::zfunction",
4978 "std::zfunction2",
4979 "w1::w2",
4980 "ns::foo<char*>",
4981 "ns::foo<int>",
4982 "ns::foo<long>",
a20714ff
PA
4983 "ns2::tmpl<int>::foo2",
4984 "(anonymous namespace)::A::B::C",
c62446b1 4985
e1ef7d7a
PA
4986 /* These are used to check that the increment-last-char in the
4987 matching algorithm for completion doesn't match "t1_fund" when
4988 completing "t1_func". */
4989 "t1_func",
4990 "t1_func1",
4991 "t1_fund",
4992 "t1_fund1",
4993
4994 /* A UTF-8 name with multi-byte sequences to make sure that
4995 cp-name-parser understands this as a single identifier ("função"
4996 is "function" in PT). */
4997 u8"u8função",
4998
4999 /* \377 (0xff) is Latin1 'ÿ'. */
5000 "yfunc\377",
5001
5002 /* \377 (0xff) is Latin1 'ÿ'. */
5003 "\377",
5004 "\377\377123",
5005
c62446b1
PA
5006 /* A name with all sorts of complications. Starts with "z" to make
5007 it easier for the completion tests below. */
5008#define Z_SYM_NAME \
5009 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
5010 "::tuple<(anonymous namespace)::ui*, " \
5011 "std::default_delete<(anonymous namespace)::ui>, void>"
5012
5013 Z_SYM_NAME
5014};
5015
a3c5fafd
PA
5016/* Returns true if the mapped_index_base::find_name_component_bounds
5017 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
5018 in completion mode. */
5c58de74
PA
5019
5020static bool
a3c5fafd 5021check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
5022 const char *search_name,
5023 gdb::array_view<const char *> expected_syms)
5024{
5025 lookup_name_info lookup_name (search_name,
5026 symbol_name_match_type::FULL, true);
5027
5028 auto bounds = index.find_name_components_bounds (lookup_name);
5029
5030 size_t distance = std::distance (bounds.first, bounds.second);
5031 if (distance != expected_syms.size ())
5032 return false;
5033
5034 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
5035 {
5036 auto nc_elem = bounds.first + exp_elem;
5037 const char *qualified = index.symbol_name_at (nc_elem->idx);
5038 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
5039 return false;
5040 }
5041
5042 return true;
5043}
5044
5045/* Test the lower-level mapped_index::find_name_component_bounds
5046 method. */
5047
c62446b1 5048static void
5c58de74
PA
5049test_mapped_index_find_name_component_bounds ()
5050{
5051 mock_mapped_index mock_index (test_symbols);
5052
a3c5fafd 5053 mock_index.build_name_components ();
5c58de74
PA
5054
5055 /* Test the lower-level mapped_index::find_name_component_bounds
5056 method in completion mode. */
5057 {
5058 static const char *expected_syms[] = {
5059 "t1_func",
5060 "t1_func1",
5c58de74
PA
5061 };
5062
a3c5fafd 5063 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
5064 "t1_func", expected_syms));
5065 }
5066
5067 /* Check that the increment-last-char in the name matching algorithm
5068 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5069 {
5070 static const char *expected_syms1[] = {
5071 "\377",
5072 "\377\377123",
5073 };
a3c5fafd 5074 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
5075 "\377", expected_syms1));
5076
5077 static const char *expected_syms2[] = {
5078 "\377\377123",
5079 };
a3c5fafd 5080 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
5081 "\377\377", expected_syms2));
5082 }
5083}
5084
5085/* Test dw2_expand_symtabs_matching_symbol. */
5086
5087static void
5088test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
5089{
5090 mock_mapped_index mock_index (test_symbols);
5091
5092 /* We let all tests run until the end even if some fails, for debug
5093 convenience. */
5094 bool any_mismatch = false;
5095
5096 /* Create the expected symbols list (an initializer_list). Needed
5097 because lists have commas, and we need to pass them to CHECK,
5098 which is a macro. */
5099#define EXPECT(...) { __VA_ARGS__ }
5100
5101 /* Wrapper for check_match that passes down the current
5102 __FILE__/__LINE__. */
5103#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5104 any_mismatch |= !check_match (__FILE__, __LINE__, \
5105 mock_index, \
5106 NAME, MATCH_TYPE, COMPLETION_MODE, \
5107 EXPECTED_LIST)
5108
5109 /* Identity checks. */
5110 for (const char *sym : test_symbols)
5111 {
5112 /* Should be able to match all existing symbols. */
5113 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5114 EXPECT (sym));
5115
5116 /* Should be able to match all existing symbols with
5117 parameters. */
5118 std::string with_params = std::string (sym) + "(int)";
5119 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5120 EXPECT (sym));
5121
5122 /* Should be able to match all existing symbols with
5123 parameters and qualifiers. */
5124 with_params = std::string (sym) + " ( int ) const";
5125 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5126 EXPECT (sym));
5127
5128 /* This should really find sym, but cp-name-parser.y doesn't
5129 know about lvalue/rvalue qualifiers yet. */
5130 with_params = std::string (sym) + " ( int ) &&";
5131 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5132 {});
5133 }
5134
e1ef7d7a
PA
5135 /* Check that the name matching algorithm for completion doesn't get
5136 confused with Latin1 'ÿ' / 0xff. */
5137 {
5138 static const char str[] = "\377";
5139 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5140 EXPECT ("\377", "\377\377123"));
5141 }
5142
5143 /* Check that the increment-last-char in the matching algorithm for
5144 completion doesn't match "t1_fund" when completing "t1_func". */
5145 {
5146 static const char str[] = "t1_func";
5147 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5148 EXPECT ("t1_func", "t1_func1"));
5149 }
5150
c62446b1
PA
5151 /* Check that completion mode works at each prefix of the expected
5152 symbol name. */
5153 {
5154 static const char str[] = "function(int)";
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 ("function"));
5163 }
5164 }
5165
5166 /* While "w" is a prefix of both components, the match function
5167 should still only be called once. */
5168 {
5169 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5170 EXPECT ("w1::w2"));
a20714ff
PA
5171 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5172 EXPECT ("w1::w2"));
c62446b1
PA
5173 }
5174
5175 /* Same, with a "complicated" symbol. */
5176 {
5177 static const char str[] = Z_SYM_NAME;
5178 size_t len = strlen (str);
5179 std::string lookup;
5180
5181 for (size_t i = 1; i < len; i++)
5182 {
5183 lookup.assign (str, i);
5184 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5185 EXPECT (Z_SYM_NAME));
5186 }
5187 }
5188
5189 /* In FULL mode, an incomplete symbol doesn't match. */
5190 {
5191 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5192 {});
5193 }
5194
5195 /* A complete symbol with parameters matches any overload, since the
5196 index has no overload info. */
5197 {
5198 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5199 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
5200 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5201 EXPECT ("std::zfunction", "std::zfunction2"));
5202 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5203 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
5204 }
5205
5206 /* Check that whitespace is ignored appropriately. A symbol with a
5207 template argument list. */
5208 {
5209 static const char expected[] = "ns::foo<int>";
5210 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5211 EXPECT (expected));
a20714ff
PA
5212 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5213 EXPECT (expected));
c62446b1
PA
5214 }
5215
5216 /* Check that whitespace is ignored appropriately. A symbol with a
5217 template argument list that includes a pointer. */
5218 {
5219 static const char expected[] = "ns::foo<char*>";
5220 /* Try both completion and non-completion modes. */
5221 static const bool completion_mode[2] = {false, true};
5222 for (size_t i = 0; i < 2; i++)
5223 {
5224 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5225 completion_mode[i], EXPECT (expected));
a20714ff
PA
5226 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5227 completion_mode[i], EXPECT (expected));
c62446b1
PA
5228
5229 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5230 completion_mode[i], EXPECT (expected));
a20714ff
PA
5231 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5232 completion_mode[i], EXPECT (expected));
c62446b1
PA
5233 }
5234 }
5235
5236 {
5237 /* Check method qualifiers are ignored. */
5238 static const char expected[] = "ns::foo<char*>";
5239 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5240 symbol_name_match_type::FULL, true, EXPECT (expected));
5241 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5242 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
5243 CHECK_MATCH ("foo < char * > ( int ) const",
5244 symbol_name_match_type::WILD, true, EXPECT (expected));
5245 CHECK_MATCH ("foo < char * > ( int ) &&",
5246 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
5247 }
5248
5249 /* Test lookup names that don't match anything. */
5250 {
a20714ff
PA
5251 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5252 {});
5253
c62446b1
PA
5254 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5255 {});
5256 }
5257
a20714ff
PA
5258 /* Some wild matching tests, exercising "(anonymous namespace)",
5259 which should not be confused with a parameter list. */
5260 {
5261 static const char *syms[] = {
5262 "A::B::C",
5263 "B::C",
5264 "C",
5265 "A :: B :: C ( int )",
5266 "B :: C ( int )",
5267 "C ( int )",
5268 };
5269
5270 for (const char *s : syms)
5271 {
5272 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5273 EXPECT ("(anonymous namespace)::A::B::C"));
5274 }
5275 }
5276
5277 {
5278 static const char expected[] = "ns2::tmpl<int>::foo2";
5279 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5280 EXPECT (expected));
5281 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5282 EXPECT (expected));
5283 }
5284
c62446b1
PA
5285 SELF_CHECK (!any_mismatch);
5286
5287#undef EXPECT
5288#undef CHECK_MATCH
5289}
5290
5c58de74
PA
5291static void
5292run_test ()
5293{
5294 test_mapped_index_find_name_component_bounds ();
5295 test_dw2_expand_symtabs_matching_symbol ();
5296}
5297
c62446b1
PA
5298}} // namespace selftests::dw2_expand_symtabs_matching
5299
5300#endif /* GDB_SELF_TEST */
5301
4b514bc8
JK
5302/* If FILE_MATCHER is NULL or if PER_CU has
5303 dwarf2_per_cu_quick_data::MARK set (see
5304 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5305 EXPANSION_NOTIFY on it. */
5306
5307static void
5308dw2_expand_symtabs_matching_one
5309 (struct dwarf2_per_cu_data *per_cu,
5310 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5311 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5312{
5313 if (file_matcher == NULL || per_cu->v.quick->mark)
5314 {
5315 bool symtab_was_null
5316 = (per_cu->v.quick->compunit_symtab == NULL);
5317
5318 dw2_instantiate_symtab (per_cu);
5319
5320 if (expansion_notify != NULL
5321 && symtab_was_null
5322 && per_cu->v.quick->compunit_symtab != NULL)
5323 expansion_notify (per_cu->v.quick->compunit_symtab);
5324 }
5325}
5326
3f563c84
PA
5327/* Helper for dw2_expand_matching symtabs. Called on each symbol
5328 matched, to expand corresponding CUs that were marked. IDX is the
5329 index of the symbol name that matched. */
5330
5331static void
5332dw2_expand_marked_cus
ed2dc618 5333 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5334 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5335 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5336 search_domain kind)
5337{
3f563c84
PA
5338 offset_type *vec, vec_len, vec_idx;
5339 bool global_seen = false;
ed2dc618 5340 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5341
61920122 5342 vec = (offset_type *) (index.constant_pool
f00a2de2 5343 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5344 vec_len = MAYBE_SWAP (vec[0]);
5345 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5346 {
5347 struct dwarf2_per_cu_data *per_cu;
5348 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5349 /* This value is only valid for index versions >= 7. */
5350 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5351 gdb_index_symbol_kind symbol_kind =
5352 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5353 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5354 /* Only check the symbol attributes if they're present.
5355 Indices prior to version 7 don't record them,
5356 and indices >= 7 may elide them for certain symbols
5357 (gold does this). */
5358 int attrs_valid =
5359 (index.version >= 7
5360 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5361
5362 /* Work around gold/15646. */
5363 if (attrs_valid)
9291a0cd 5364 {
61920122
PA
5365 if (!is_static && global_seen)
5366 continue;
5367 if (!is_static)
5368 global_seen = true;
5369 }
3190f0c6 5370
61920122
PA
5371 /* Only check the symbol's kind if it has one. */
5372 if (attrs_valid)
5373 {
5374 switch (kind)
8943b874 5375 {
61920122
PA
5376 case VARIABLES_DOMAIN:
5377 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5378 continue;
5379 break;
5380 case FUNCTIONS_DOMAIN:
5381 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5382 continue;
61920122
PA
5383 break;
5384 case TYPES_DOMAIN:
5385 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5386 continue;
5387 break;
5388 default:
5389 break;
8943b874 5390 }
61920122 5391 }
8943b874 5392
61920122
PA
5393 /* Don't crash on bad data. */
5394 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5395 + dwarf2_per_objfile->n_type_units))
5396 {
5397 complaint (&symfile_complaints,
5398 _(".gdb_index entry has bad CU index"
ed2dc618
SM
5399 " [in module %s]"),
5400 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5401 continue;
5402 }
5403
ed2dc618 5404 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4b514bc8
JK
5405 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5406 expansion_notify);
61920122
PA
5407 }
5408}
5409
4b514bc8
JK
5410/* If FILE_MATCHER is non-NULL, set all the
5411 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5412 that match FILE_MATCHER. */
5413
61920122 5414static void
4b514bc8 5415dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5416 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5417 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5418{
4b514bc8 5419 if (file_matcher == NULL)
61920122
PA
5420 return;
5421
4b514bc8
JK
5422 objfile *const objfile = dwarf2_per_objfile->objfile;
5423
5424 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5425 htab_eq_pointer,
5426 NULL, xcalloc, xfree));
5427 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5428 htab_eq_pointer,
5429 NULL, xcalloc, xfree));
61920122 5430
4b514bc8
JK
5431 /* The rule is CUs specify all the files, including those used by
5432 any TU, so there's no need to scan TUs here. */
61920122 5433
927aa2e7
JK
5434 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5435 {
5436 int j;
ed2dc618 5437 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
927aa2e7
JK
5438 struct quick_file_names *file_data;
5439 void **slot;
5440
5441 QUIT;
5442
5443 per_cu->v.quick->mark = 0;
5444
5445 /* We only need to look at symtabs not already expanded. */
5446 if (per_cu->v.quick->compunit_symtab)
5447 continue;
5448
5449 file_data = dw2_get_file_names (per_cu);
5450 if (file_data == NULL)
5451 continue;
5452
5453 if (htab_find (visited_not_found.get (), file_data) != NULL)
5454 continue;
5455 else if (htab_find (visited_found.get (), file_data) != NULL)
5456 {
5457 per_cu->v.quick->mark = 1;
5458 continue;
5459 }
5460
5461 for (j = 0; j < file_data->num_file_names; ++j)
5462 {
5463 const char *this_real_name;
5464
5465 if (file_matcher (file_data->file_names[j], false))
5466 {
5467 per_cu->v.quick->mark = 1;
5468 break;
5469 }
5470
5471 /* Before we invoke realpath, which can get expensive when many
5472 files are involved, do a quick comparison of the basenames. */
5473 if (!basenames_may_differ
5474 && !file_matcher (lbasename (file_data->file_names[j]),
5475 true))
5476 continue;
5477
5478 this_real_name = dw2_get_real_path (objfile, file_data, j);
5479 if (file_matcher (this_real_name, false))
5480 {
5481 per_cu->v.quick->mark = 1;
5482 break;
5483 }
5484 }
5485
5486 slot = htab_find_slot (per_cu->v.quick->mark
5487 ? visited_found.get ()
5488 : visited_not_found.get (),
5489 file_data, INSERT);
5490 *slot = file_data;
5491 }
5492}
5493
5494static void
5495dw2_expand_symtabs_matching
5496 (struct objfile *objfile,
5497 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5498 const lookup_name_info &lookup_name,
5499 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5500 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5501 enum search_domain kind)
5502{
ed2dc618
SM
5503 struct dwarf2_per_objfile *dwarf2_per_objfile
5504 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5505
5506 /* index_table is NULL if OBJF_READNOW. */
5507 if (!dwarf2_per_objfile->index_table)
5508 return;
5509
ed2dc618 5510 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5511
5512 mapped_index &index = *dwarf2_per_objfile->index_table;
5513
5514 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5515 symbol_matcher,
5516 kind, [&] (offset_type idx)
5517 {
ed2dc618 5518 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7
JK
5519 expansion_notify, kind);
5520 });
5521}
5522
5523/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5524 symtab. */
5525
5526static struct compunit_symtab *
5527recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5528 CORE_ADDR pc)
5529{
5530 int i;
5531
5532 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5533 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5534 return cust;
5535
5536 if (cust->includes == NULL)
5537 return NULL;
5538
5539 for (i = 0; cust->includes[i]; ++i)
5540 {
5541 struct compunit_symtab *s = cust->includes[i];
5542
5543 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5544 if (s != NULL)
5545 return s;
5546 }
5547
5548 return NULL;
5549}
5550
5551static struct compunit_symtab *
5552dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5553 struct bound_minimal_symbol msymbol,
5554 CORE_ADDR pc,
5555 struct obj_section *section,
5556 int warn_if_readin)
5557{
5558 struct dwarf2_per_cu_data *data;
5559 struct compunit_symtab *result;
5560
927aa2e7
JK
5561 if (!objfile->psymtabs_addrmap)
5562 return NULL;
5563
5564 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5565 pc);
5566 if (!data)
5567 return NULL;
5568
5569 if (warn_if_readin && data->v.quick->compunit_symtab)
5570 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5571 paddress (get_objfile_arch (objfile), pc));
5572
5573 result
5574 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5575 pc);
5576 gdb_assert (result != NULL);
5577 return result;
5578}
5579
5580static void
5581dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5582 void *data, int need_fullname)
5583{
ed2dc618
SM
5584 struct dwarf2_per_objfile *dwarf2_per_objfile
5585 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5586
5587 if (!dwarf2_per_objfile->filenames_cache)
5588 {
5589 dwarf2_per_objfile->filenames_cache.emplace ();
5590
5591 htab_up visited (htab_create_alloc (10,
5592 htab_hash_pointer, htab_eq_pointer,
5593 NULL, xcalloc, xfree));
5594
5595 /* The rule is CUs specify all the files, including those used
5596 by any TU, so there's no need to scan TUs here. We can
5597 ignore file names coming from already-expanded CUs. */
5598
5599 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5600 {
ed2dc618 5601 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
927aa2e7
JK
5602
5603 if (per_cu->v.quick->compunit_symtab)
5604 {
5605 void **slot = htab_find_slot (visited.get (),
5606 per_cu->v.quick->file_names,
5607 INSERT);
5608
5609 *slot = per_cu->v.quick->file_names;
5610 }
5611 }
5612
5613 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5614 {
ed2dc618 5615 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
927aa2e7
JK
5616 struct quick_file_names *file_data;
5617 void **slot;
5618
5619 /* We only need to look at symtabs not already expanded. */
5620 if (per_cu->v.quick->compunit_symtab)
5621 continue;
5622
5623 file_data = dw2_get_file_names (per_cu);
5624 if (file_data == NULL)
5625 continue;
5626
5627 slot = htab_find_slot (visited.get (), file_data, INSERT);
5628 if (*slot)
5629 {
5630 /* Already visited. */
5631 continue;
5632 }
5633 *slot = file_data;
5634
5635 for (int j = 0; j < file_data->num_file_names; ++j)
5636 {
5637 const char *filename = file_data->file_names[j];
5638 dwarf2_per_objfile->filenames_cache->seen (filename);
5639 }
5640 }
5641 }
5642
5643 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5644 {
5645 gdb::unique_xmalloc_ptr<char> this_real_name;
5646
5647 if (need_fullname)
5648 this_real_name = gdb_realpath (filename);
5649 (*fun) (filename, this_real_name.get (), data);
5650 });
5651}
5652
5653static int
5654dw2_has_symbols (struct objfile *objfile)
5655{
5656 return 1;
5657}
5658
5659const struct quick_symbol_functions dwarf2_gdb_index_functions =
5660{
5661 dw2_has_symbols,
5662 dw2_find_last_source_symtab,
5663 dw2_forget_cached_source_info,
5664 dw2_map_symtabs_matching_filename,
5665 dw2_lookup_symbol,
5666 dw2_print_stats,
5667 dw2_dump,
5668 dw2_relocate,
5669 dw2_expand_symtabs_for_function,
5670 dw2_expand_all_symtabs,
5671 dw2_expand_symtabs_with_fullname,
5672 dw2_map_matching_symbols,
5673 dw2_expand_symtabs_matching,
5674 dw2_find_pc_sect_compunit_symtab,
5675 NULL,
5676 dw2_map_symbol_filenames
5677};
5678
5679/* DWARF-5 debug_names reader. */
5680
5681/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5682static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5683
5684/* A helper function that reads the .debug_names section in SECTION
5685 and fills in MAP. FILENAME is the name of the file containing the
5686 section; it is used for error reporting.
5687
5688 Returns true if all went well, false otherwise. */
5689
5690static bool
5691read_debug_names_from_section (struct objfile *objfile,
5692 const char *filename,
5693 struct dwarf2_section_info *section,
5694 mapped_debug_names &map)
5695{
5696 if (dwarf2_section_empty_p (section))
5697 return false;
5698
5699 /* Older elfutils strip versions could keep the section in the main
5700 executable while splitting it for the separate debug info file. */
5701 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5702 return false;
5703
5704 dwarf2_read_section (objfile, section);
5705
5706 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5707
5708 const gdb_byte *addr = section->buffer;
5709
5710 bfd *const abfd = get_section_bfd_owner (section);
5711
5712 unsigned int bytes_read;
5713 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5714 addr += bytes_read;
5715
5716 map.dwarf5_is_dwarf64 = bytes_read != 4;
5717 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5718 if (bytes_read + length != section->size)
5719 {
5720 /* There may be multiple per-CU indices. */
5721 warning (_("Section .debug_names in %s length %s does not match "
5722 "section length %s, ignoring .debug_names."),
5723 filename, plongest (bytes_read + length),
5724 pulongest (section->size));
5725 return false;
5726 }
5727
5728 /* The version number. */
5729 uint16_t version = read_2_bytes (abfd, addr);
5730 addr += 2;
5731 if (version != 5)
5732 {
5733 warning (_("Section .debug_names in %s has unsupported version %d, "
5734 "ignoring .debug_names."),
5735 filename, version);
5736 return false;
5737 }
5738
5739 /* Padding. */
5740 uint16_t padding = read_2_bytes (abfd, addr);
5741 addr += 2;
5742 if (padding != 0)
5743 {
5744 warning (_("Section .debug_names in %s has unsupported padding %d, "
5745 "ignoring .debug_names."),
5746 filename, padding);
5747 return false;
5748 }
5749
5750 /* comp_unit_count - The number of CUs in the CU list. */
5751 map.cu_count = read_4_bytes (abfd, addr);
5752 addr += 4;
5753
5754 /* local_type_unit_count - The number of TUs in the local TU
5755 list. */
5756 map.tu_count = read_4_bytes (abfd, addr);
5757 addr += 4;
5758
5759 /* foreign_type_unit_count - The number of TUs in the foreign TU
5760 list. */
5761 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5762 addr += 4;
5763 if (foreign_tu_count != 0)
5764 {
5765 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5766 "ignoring .debug_names."),
5767 filename, static_cast<unsigned long> (foreign_tu_count));
5768 return false;
5769 }
5770
5771 /* bucket_count - The number of hash buckets in the hash lookup
5772 table. */
5773 map.bucket_count = read_4_bytes (abfd, addr);
5774 addr += 4;
5775
5776 /* name_count - The number of unique names in the index. */
5777 map.name_count = read_4_bytes (abfd, addr);
5778 addr += 4;
5779
5780 /* abbrev_table_size - The size in bytes of the abbreviations
5781 table. */
5782 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5783 addr += 4;
5784
5785 /* augmentation_string_size - The size in bytes of the augmentation
5786 string. This value is rounded up to a multiple of 4. */
5787 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5788 addr += 4;
5789 map.augmentation_is_gdb = ((augmentation_string_size
5790 == sizeof (dwarf5_augmentation))
5791 && memcmp (addr, dwarf5_augmentation,
5792 sizeof (dwarf5_augmentation)) == 0);
5793 augmentation_string_size += (-augmentation_string_size) & 3;
5794 addr += augmentation_string_size;
5795
5796 /* List of CUs */
5797 map.cu_table_reordered = addr;
5798 addr += map.cu_count * map.offset_size;
5799
5800 /* List of Local TUs */
5801 map.tu_table_reordered = addr;
5802 addr += map.tu_count * map.offset_size;
5803
5804 /* Hash Lookup Table */
5805 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5806 addr += map.bucket_count * 4;
5807 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5808 addr += map.name_count * 4;
5809
5810 /* Name Table */
5811 map.name_table_string_offs_reordered = addr;
5812 addr += map.name_count * map.offset_size;
5813 map.name_table_entry_offs_reordered = addr;
5814 addr += map.name_count * map.offset_size;
5815
5816 const gdb_byte *abbrev_table_start = addr;
5817 for (;;)
5818 {
5819 unsigned int bytes_read;
5820 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5821 addr += bytes_read;
5822 if (index_num == 0)
5823 break;
5824
5825 const auto insertpair
5826 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5827 if (!insertpair.second)
5828 {
5829 warning (_("Section .debug_names in %s has duplicate index %s, "
5830 "ignoring .debug_names."),
5831 filename, pulongest (index_num));
5832 return false;
5833 }
5834 mapped_debug_names::index_val &indexval = insertpair.first->second;
5835 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5836 addr += bytes_read;
5837
5838 for (;;)
5839 {
5840 mapped_debug_names::index_val::attr attr;
5841 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5842 addr += bytes_read;
5843 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5844 addr += bytes_read;
5845 if (attr.form == DW_FORM_implicit_const)
5846 {
5847 attr.implicit_const = read_signed_leb128 (abfd, addr,
5848 &bytes_read);
5849 addr += bytes_read;
5850 }
5851 if (attr.dw_idx == 0 && attr.form == 0)
5852 break;
5853 indexval.attr_vec.push_back (std::move (attr));
5854 }
5855 }
5856 if (addr != abbrev_table_start + abbrev_table_size)
5857 {
5858 warning (_("Section .debug_names in %s has abbreviation_table "
5859 "of size %zu vs. written as %u, ignoring .debug_names."),
5860 filename, addr - abbrev_table_start, abbrev_table_size);
5861 return false;
5862 }
5863 map.entry_pool = addr;
5864
5865 return true;
5866}
5867
5868/* A helper for create_cus_from_debug_names that handles the MAP's CU
5869 list. */
5870
5871static void
ed2dc618 5872create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5873 const mapped_debug_names &map,
5874 dwarf2_section_info &section,
5875 bool is_dwz, int base_offset)
5876{
5877 sect_offset sect_off_prev;
5878 for (uint32_t i = 0; i <= map.cu_count; ++i)
5879 {
5880 sect_offset sect_off_next;
5881 if (i < map.cu_count)
5882 {
5883 sect_off_next
5884 = (sect_offset) (extract_unsigned_integer
5885 (map.cu_table_reordered + i * map.offset_size,
5886 map.offset_size,
5887 map.dwarf5_byte_order));
5888 }
5889 else
5890 sect_off_next = (sect_offset) section.size;
5891 if (i >= 1)
5892 {
5893 const ULONGEST length = sect_off_next - sect_off_prev;
5894 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
ed2dc618 5895 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7
JK
5896 sect_off_prev, length);
5897 }
5898 sect_off_prev = sect_off_next;
5899 }
5900}
5901
5902/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5903 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5904
5905static void
ed2dc618 5906create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5907 const mapped_debug_names &map,
5908 const mapped_debug_names &dwz_map)
5909{
ed2dc618 5910 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5911
5912 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
5913 dwarf2_per_objfile->all_comp_units
5914 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
5915 dwarf2_per_objfile->n_comp_units);
5916
ed2dc618
SM
5917 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5918 dwarf2_per_objfile->info,
927aa2e7
JK
5919 false /* is_dwz */,
5920 0 /* base_offset */);
5921
5922 if (dwz_map.cu_count == 0)
5923 return;
5924
ed2dc618
SM
5925 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5926 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
927aa2e7
JK
5927 true /* is_dwz */,
5928 map.cu_count /* base_offset */);
5929}
5930
5931/* Read .debug_names. If everything went ok, initialize the "quick"
5932 elements of all the CUs and return true. Otherwise, return false. */
5933
5934static bool
ed2dc618 5935dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5936{
ed2dc618
SM
5937 mapped_debug_names local_map (dwarf2_per_objfile);
5938 mapped_debug_names dwz_map (dwarf2_per_objfile);
5939 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5940
5941 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5942 &dwarf2_per_objfile->debug_names,
5943 local_map))
5944 return false;
5945
5946 /* Don't use the index if it's empty. */
5947 if (local_map.name_count == 0)
5948 return false;
5949
5950 /* If there is a .dwz file, read it so we can get its CU list as
5951 well. */
ed2dc618 5952 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5953 if (dwz != NULL)
5954 {
5955 if (!read_debug_names_from_section (objfile,
5956 bfd_get_filename (dwz->dwz_bfd),
5957 &dwz->debug_names, dwz_map))
5958 {
5959 warning (_("could not read '.debug_names' section from %s; skipping"),
5960 bfd_get_filename (dwz->dwz_bfd));
5961 return false;
5962 }
5963 }
5964
ed2dc618 5965 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
927aa2e7
JK
5966
5967 if (local_map.tu_count != 0)
5968 {
5969 /* We can only handle a single .debug_types when we have an
5970 index. */
5971 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5972 return false;
5973
5974 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5975 dwarf2_per_objfile->types, 0);
5976
5977 create_signatured_type_table_from_debug_names
ed2dc618 5978 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5979 }
5980
ed2dc618
SM
5981 create_addrmap_from_aranges (dwarf2_per_objfile,
5982 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5983
ed2dc618
SM
5984 dwarf2_per_objfile->debug_names_table.reset
5985 (new mapped_debug_names (dwarf2_per_objfile));
927aa2e7
JK
5986 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5987 dwarf2_per_objfile->using_index = 1;
5988 dwarf2_per_objfile->quick_file_names_table =
5989 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5990
5991 return true;
5992}
5993
5994/* Symbol name hashing function as specified by DWARF-5. */
5995
5996static uint32_t
5997dwarf5_djb_hash (const char *str_)
5998{
5999 const unsigned char *str = (const unsigned char *) str_;
6000
6001 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
6002 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
6003
6004 uint32_t hash = 5381;
6005 while (int c = *str++)
6006 hash = hash * 33 + tolower (c);
6007 return hash;
6008}
6009
6010/* Type used to manage iterating over all CUs looking for a symbol for
6011 .debug_names. */
6012
6013class dw2_debug_names_iterator
6014{
6015public:
6016 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
6017 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
6018 dw2_debug_names_iterator (const mapped_debug_names &map,
6019 bool want_specific_block,
6020 block_enum block_index, domain_enum domain,
6021 const char *name)
6022 : m_map (map), m_want_specific_block (want_specific_block),
6023 m_block_index (block_index), m_domain (domain),
6024 m_addr (find_vec_in_debug_names (map, name))
6025 {}
6026
6027 dw2_debug_names_iterator (const mapped_debug_names &map,
6028 search_domain search, uint32_t namei)
6029 : m_map (map),
6030 m_search (search),
6031 m_addr (find_vec_in_debug_names (map, namei))
6032 {}
6033
6034 /* Return the next matching CU or NULL if there are no more. */
6035 dwarf2_per_cu_data *next ();
6036
6037private:
6038 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6039 const char *name);
6040 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6041 uint32_t namei);
6042
6043 /* The internalized form of .debug_names. */
6044 const mapped_debug_names &m_map;
6045
6046 /* If true, only look for symbols that match BLOCK_INDEX. */
6047 const bool m_want_specific_block = false;
6048
6049 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6050 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6051 value. */
6052 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6053
6054 /* The kind of symbol we're looking for. */
6055 const domain_enum m_domain = UNDEF_DOMAIN;
6056 const search_domain m_search = ALL_DOMAIN;
6057
6058 /* The list of CUs from the index entry of the symbol, or NULL if
6059 not found. */
6060 const gdb_byte *m_addr;
6061};
6062
6063const char *
6064mapped_debug_names::namei_to_name (uint32_t namei) const
6065{
6066 const ULONGEST namei_string_offs
6067 = extract_unsigned_integer ((name_table_string_offs_reordered
6068 + namei * offset_size),
6069 offset_size,
6070 dwarf5_byte_order);
6071 return read_indirect_string_at_offset
ed2dc618 6072 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
6073}
6074
6075/* Find a slot in .debug_names for the object named NAME. If NAME is
6076 found, return pointer to its pool data. If NAME cannot be found,
6077 return NULL. */
6078
6079const gdb_byte *
6080dw2_debug_names_iterator::find_vec_in_debug_names
6081 (const mapped_debug_names &map, const char *name)
6082{
6083 int (*cmp) (const char *, const char *);
6084
6085 if (current_language->la_language == language_cplus
6086 || current_language->la_language == language_fortran
6087 || current_language->la_language == language_d)
6088 {
6089 /* NAME is already canonical. Drop any qualifiers as
6090 .debug_names does not contain any. */
6091
6092 if (strchr (name, '(') != NULL)
6093 {
6094 gdb::unique_xmalloc_ptr<char> without_params
6095 = cp_remove_params (name);
6096
6097 if (without_params != NULL)
6098 {
6099 name = without_params.get();
6100 }
6101 }
6102 }
6103
6104 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6105
6106 const uint32_t full_hash = dwarf5_djb_hash (name);
6107 uint32_t namei
6108 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6109 (map.bucket_table_reordered
6110 + (full_hash % map.bucket_count)), 4,
6111 map.dwarf5_byte_order);
6112 if (namei == 0)
6113 return NULL;
6114 --namei;
6115 if (namei >= map.name_count)
6116 {
6117 complaint (&symfile_complaints,
6118 _("Wrong .debug_names with name index %u but name_count=%u "
6119 "[in module %s]"),
6120 namei, map.name_count,
ed2dc618 6121 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
6122 return NULL;
6123 }
6124
6125 for (;;)
6126 {
6127 const uint32_t namei_full_hash
6128 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6129 (map.hash_table_reordered + namei), 4,
6130 map.dwarf5_byte_order);
6131 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6132 return NULL;
6133
6134 if (full_hash == namei_full_hash)
6135 {
6136 const char *const namei_string = map.namei_to_name (namei);
6137
6138#if 0 /* An expensive sanity check. */
6139 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6140 {
6141 complaint (&symfile_complaints,
6142 _("Wrong .debug_names hash for string at index %u "
6143 "[in module %s]"),
6144 namei, objfile_name (dwarf2_per_objfile->objfile));
6145 return NULL;
6146 }
6147#endif
6148
6149 if (cmp (namei_string, name) == 0)
6150 {
6151 const ULONGEST namei_entry_offs
6152 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6153 + namei * map.offset_size),
6154 map.offset_size, map.dwarf5_byte_order);
6155 return map.entry_pool + namei_entry_offs;
6156 }
6157 }
6158
6159 ++namei;
6160 if (namei >= map.name_count)
6161 return NULL;
6162 }
6163}
6164
6165const gdb_byte *
6166dw2_debug_names_iterator::find_vec_in_debug_names
6167 (const mapped_debug_names &map, uint32_t namei)
6168{
6169 if (namei >= map.name_count)
6170 {
6171 complaint (&symfile_complaints,
6172 _("Wrong .debug_names with name index %u but name_count=%u "
6173 "[in module %s]"),
6174 namei, map.name_count,
ed2dc618 6175 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
6176 return NULL;
6177 }
6178
6179 const ULONGEST namei_entry_offs
6180 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6181 + namei * map.offset_size),
6182 map.offset_size, map.dwarf5_byte_order);
6183 return map.entry_pool + namei_entry_offs;
6184}
6185
6186/* See dw2_debug_names_iterator. */
6187
6188dwarf2_per_cu_data *
6189dw2_debug_names_iterator::next ()
6190{
6191 if (m_addr == NULL)
6192 return NULL;
6193
ed2dc618
SM
6194 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
6195 struct objfile *objfile = dwarf2_per_objfile->objfile;
6196 bfd *const abfd = objfile->obfd;
927aa2e7
JK
6197
6198 again:
6199
6200 unsigned int bytes_read;
6201 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6202 m_addr += bytes_read;
6203 if (abbrev == 0)
6204 return NULL;
6205
6206 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6207 if (indexval_it == m_map.abbrev_map.cend ())
6208 {
6209 complaint (&symfile_complaints,
6210 _("Wrong .debug_names undefined abbrev code %s "
6211 "[in module %s]"),
ed2dc618 6212 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
6213 return NULL;
6214 }
6215 const mapped_debug_names::index_val &indexval = indexval_it->second;
6216 bool have_is_static = false;
6217 bool is_static;
6218 dwarf2_per_cu_data *per_cu = NULL;
6219 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6220 {
6221 ULONGEST ull;
6222 switch (attr.form)
6223 {
6224 case DW_FORM_implicit_const:
6225 ull = attr.implicit_const;
6226 break;
6227 case DW_FORM_flag_present:
6228 ull = 1;
6229 break;
6230 case DW_FORM_udata:
6231 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6232 m_addr += bytes_read;
6233 break;
6234 default:
6235 complaint (&symfile_complaints,
6236 _("Unsupported .debug_names form %s [in module %s]"),
6237 dwarf_form_name (attr.form),
ed2dc618 6238 objfile_name (objfile));
927aa2e7
JK
6239 return NULL;
6240 }
6241 switch (attr.dw_idx)
6242 {
6243 case DW_IDX_compile_unit:
6244 /* Don't crash on bad data. */
8af5c486 6245 if (ull >= dwarf2_per_objfile->n_comp_units)
927aa2e7
JK
6246 {
6247 complaint (&symfile_complaints,
6248 _(".debug_names entry has bad CU index %s"
6249 " [in module %s]"),
6250 pulongest (ull),
6251 objfile_name (dwarf2_per_objfile->objfile));
6252 continue;
6253 }
ed2dc618 6254 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
927aa2e7 6255 break;
8af5c486
JK
6256 case DW_IDX_type_unit:
6257 /* Don't crash on bad data. */
6258 if (ull >= dwarf2_per_objfile->n_type_units)
6259 {
6260 complaint (&symfile_complaints,
6261 _(".debug_names entry has bad TU index %s"
6262 " [in module %s]"),
6263 pulongest (ull),
6264 objfile_name (dwarf2_per_objfile->objfile));
6265 continue;
6266 }
ed2dc618
SM
6267 per_cu = dw2_get_cutu (dwarf2_per_objfile,
6268 dwarf2_per_objfile->n_comp_units + ull);
8af5c486 6269 break;
927aa2e7
JK
6270 case DW_IDX_GNU_internal:
6271 if (!m_map.augmentation_is_gdb)
6272 break;
6273 have_is_static = true;
6274 is_static = true;
6275 break;
6276 case DW_IDX_GNU_external:
6277 if (!m_map.augmentation_is_gdb)
6278 break;
6279 have_is_static = true;
6280 is_static = false;
6281 break;
6282 }
6283 }
6284
6285 /* Skip if already read in. */
6286 if (per_cu->v.quick->compunit_symtab)
6287 goto again;
6288
6289 /* Check static vs global. */
6290 if (have_is_static)
6291 {
6292 const bool want_static = m_block_index != GLOBAL_BLOCK;
6293 if (m_want_specific_block && want_static != is_static)
6294 goto again;
6295 }
6296
6297 /* Match dw2_symtab_iter_next, symbol_kind
6298 and debug_names::psymbol_tag. */
6299 switch (m_domain)
6300 {
6301 case VAR_DOMAIN:
6302 switch (indexval.dwarf_tag)
6303 {
6304 case DW_TAG_variable:
6305 case DW_TAG_subprogram:
6306 /* Some types are also in VAR_DOMAIN. */
6307 case DW_TAG_typedef:
6308 case DW_TAG_structure_type:
6309 break;
6310 default:
6311 goto again;
6312 }
6313 break;
6314 case STRUCT_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 case LABEL_DOMAIN:
6325 switch (indexval.dwarf_tag)
6326 {
6327 case 0:
6328 case DW_TAG_variable:
6329 break;
6330 default:
6331 goto again;
6332 }
6333 break;
6334 default:
6335 break;
6336 }
6337
6338 /* Match dw2_expand_symtabs_matching, symbol_kind and
6339 debug_names::psymbol_tag. */
6340 switch (m_search)
4b514bc8 6341 {
927aa2e7
JK
6342 case VARIABLES_DOMAIN:
6343 switch (indexval.dwarf_tag)
4b514bc8 6344 {
927aa2e7
JK
6345 case DW_TAG_variable:
6346 break;
6347 default:
6348 goto again;
4b514bc8 6349 }
927aa2e7
JK
6350 break;
6351 case FUNCTIONS_DOMAIN:
6352 switch (indexval.dwarf_tag)
4b514bc8 6353 {
927aa2e7
JK
6354 case DW_TAG_subprogram:
6355 break;
6356 default:
6357 goto again;
4b514bc8 6358 }
927aa2e7
JK
6359 break;
6360 case TYPES_DOMAIN:
6361 switch (indexval.dwarf_tag)
6362 {
6363 case DW_TAG_typedef:
6364 case DW_TAG_structure_type:
6365 break;
6366 default:
6367 goto again;
6368 }
6369 break;
6370 default:
6371 break;
4b514bc8 6372 }
927aa2e7
JK
6373
6374 return per_cu;
4b514bc8 6375}
61920122 6376
927aa2e7
JK
6377static struct compunit_symtab *
6378dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6379 const char *name, domain_enum domain)
4b514bc8 6380{
927aa2e7 6381 const block_enum block_index = static_cast<block_enum> (block_index_int);
ed2dc618
SM
6382 struct dwarf2_per_objfile *dwarf2_per_objfile
6383 = get_dwarf2_per_objfile (objfile);
61920122 6384
927aa2e7
JK
6385 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6386 if (!mapp)
61920122 6387 {
927aa2e7
JK
6388 /* index is NULL if OBJF_READNOW. */
6389 return NULL;
6390 }
6391 const auto &map = *mapp;
9291a0cd 6392
927aa2e7
JK
6393 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6394 block_index, domain, name);
9703b513 6395
927aa2e7
JK
6396 struct compunit_symtab *stab_best = NULL;
6397 struct dwarf2_per_cu_data *per_cu;
6398 while ((per_cu = iter.next ()) != NULL)
6399 {
6400 struct symbol *sym, *with_opaque = NULL;
6401 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6402 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6403 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6404
927aa2e7
JK
6405 sym = block_find_symbol (block, name, domain,
6406 block_find_non_opaque_type_preferred,
6407 &with_opaque);
9703b513 6408
927aa2e7
JK
6409 /* Some caution must be observed with overloaded functions and
6410 methods, since the index will not contain any overload
6411 information (but NAME might contain it). */
a3ec0bb1 6412
927aa2e7
JK
6413 if (sym != NULL
6414 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6415 return stab;
6416 if (with_opaque != NULL
6417 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6418 stab_best = stab;
9703b513 6419
927aa2e7 6420 /* Keep looking through other CUs. */
9703b513
TT
6421 }
6422
927aa2e7 6423 return stab_best;
9703b513
TT
6424}
6425
927aa2e7
JK
6426/* This dumps minimal information about .debug_names. It is called
6427 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6428 uses this to verify that .debug_names has been loaded. */
9291a0cd 6429
927aa2e7
JK
6430static void
6431dw2_debug_names_dump (struct objfile *objfile)
6432{
ed2dc618
SM
6433 struct dwarf2_per_objfile *dwarf2_per_objfile
6434 = get_dwarf2_per_objfile (objfile);
6435
927aa2e7
JK
6436 gdb_assert (dwarf2_per_objfile->using_index);
6437 printf_filtered (".debug_names:");
6438 if (dwarf2_per_objfile->debug_names_table)
6439 printf_filtered (" exists\n");
6440 else
6441 printf_filtered (" faked for \"readnow\"\n");
6442 printf_filtered ("\n");
9291a0cd
TT
6443}
6444
9291a0cd 6445static void
927aa2e7
JK
6446dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6447 const char *func_name)
9291a0cd 6448{
ed2dc618
SM
6449 struct dwarf2_per_objfile *dwarf2_per_objfile
6450 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6451
927aa2e7
JK
6452 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6453 if (dwarf2_per_objfile->debug_names_table)
24c79950 6454 {
927aa2e7 6455 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6456
927aa2e7
JK
6457 /* Note: It doesn't matter what we pass for block_index here. */
6458 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6459 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
24c79950 6460
927aa2e7
JK
6461 struct dwarf2_per_cu_data *per_cu;
6462 while ((per_cu = iter.next ()) != NULL)
6463 dw2_instantiate_symtab (per_cu);
6464 }
6465}
24c79950 6466
927aa2e7
JK
6467static void
6468dw2_debug_names_expand_symtabs_matching
6469 (struct objfile *objfile,
6470 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6471 const lookup_name_info &lookup_name,
6472 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6473 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6474 enum search_domain kind)
6475{
ed2dc618
SM
6476 struct dwarf2_per_objfile *dwarf2_per_objfile
6477 = get_dwarf2_per_objfile (objfile);
9291a0cd 6478
927aa2e7
JK
6479 /* debug_names_table is NULL if OBJF_READNOW. */
6480 if (!dwarf2_per_objfile->debug_names_table)
6481 return;
9291a0cd 6482
ed2dc618 6483 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6484
44ed8f3e 6485 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6486
44ed8f3e
PA
6487 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6488 symbol_matcher,
6489 kind, [&] (offset_type namei)
927aa2e7 6490 {
927aa2e7
JK
6491 /* The name was matched, now expand corresponding CUs that were
6492 marked. */
6493 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6494
927aa2e7
JK
6495 struct dwarf2_per_cu_data *per_cu;
6496 while ((per_cu = iter.next ()) != NULL)
6497 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6498 expansion_notify);
44ed8f3e 6499 });
9291a0cd
TT
6500}
6501
927aa2e7 6502const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6503{
6504 dw2_has_symbols,
6505 dw2_find_last_source_symtab,
6506 dw2_forget_cached_source_info,
f8eba3c6 6507 dw2_map_symtabs_matching_filename,
927aa2e7 6508 dw2_debug_names_lookup_symbol,
9291a0cd 6509 dw2_print_stats,
927aa2e7 6510 dw2_debug_names_dump,
9291a0cd 6511 dw2_relocate,
927aa2e7 6512 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6513 dw2_expand_all_symtabs,
652a8996 6514 dw2_expand_symtabs_with_fullname,
40658b94 6515 dw2_map_matching_symbols,
927aa2e7 6516 dw2_debug_names_expand_symtabs_matching,
43f3e411 6517 dw2_find_pc_sect_compunit_symtab,
71a3c369 6518 NULL,
9291a0cd
TT
6519 dw2_map_symbol_filenames
6520};
6521
3c0aa29a 6522/* See symfile.h. */
9291a0cd 6523
3c0aa29a
PA
6524bool
6525dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6526{
ed2dc618
SM
6527 struct dwarf2_per_objfile *dwarf2_per_objfile
6528 = get_dwarf2_per_objfile (objfile);
6529
9291a0cd
TT
6530 /* If we're about to read full symbols, don't bother with the
6531 indices. In this case we also don't care if some other debug
6532 format is making psymtabs, because they are all about to be
6533 expanded anyway. */
6534 if ((objfile->flags & OBJF_READNOW))
6535 {
6536 int i;
6537
6538 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6539 create_all_comp_units (dwarf2_per_objfile);
6540 create_all_type_units (dwarf2_per_objfile);
7b9f3c50
DE
6541 dwarf2_per_objfile->quick_file_names_table =
6542 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 6543
1fd400ff 6544 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 6545 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 6546 {
ed2dc618 6547 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
9291a0cd 6548
e254ef6a
DE
6549 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6550 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6551 }
6552
6553 /* Return 1 so that gdb sees the "quick" functions. However,
6554 these functions will be no-ops because we will have expanded
6555 all symtabs. */
3c0aa29a
PA
6556 *index_kind = dw_index_kind::GDB_INDEX;
6557 return true;
9291a0cd
TT
6558 }
6559
ed2dc618 6560 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6561 {
6562 *index_kind = dw_index_kind::DEBUG_NAMES;
6563 return true;
6564 }
927aa2e7 6565
9291a0cd 6566 if (dwarf2_read_index (objfile))
3c0aa29a
PA
6567 {
6568 *index_kind = dw_index_kind::GDB_INDEX;
6569 return true;
6570 }
9291a0cd 6571
3c0aa29a 6572 return false;
9291a0cd
TT
6573}
6574
6575\f
6576
dce234bc
PP
6577/* Build a partial symbol table. */
6578
6579void
f29dff0a 6580dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6581{
ed2dc618
SM
6582 struct dwarf2_per_objfile *dwarf2_per_objfile
6583 = get_dwarf2_per_objfile (objfile);
c9bf0622 6584
af5bf4ad
SM
6585 if (objfile->global_psymbols.capacity () == 0
6586 && objfile->static_psymbols.capacity () == 0)
6587 init_psymbol_list (objfile, 1024);
c906108c 6588
492d29ea 6589 TRY
c9bf0622
TT
6590 {
6591 /* This isn't really ideal: all the data we allocate on the
6592 objfile's obstack is still uselessly kept around. However,
6593 freeing it seems unsafe. */
906768f9 6594 psymtab_discarder psymtabs (objfile);
ed2dc618 6595 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6596 psymtabs.keep ();
c9bf0622 6597 }
492d29ea
PA
6598 CATCH (except, RETURN_MASK_ERROR)
6599 {
6600 exception_print (gdb_stderr, except);
6601 }
6602 END_CATCH
c906108c 6603}
c906108c 6604
1ce1cefd
DE
6605/* Return the total length of the CU described by HEADER. */
6606
6607static unsigned int
6608get_cu_length (const struct comp_unit_head *header)
6609{
6610 return header->initial_length_size + header->length;
6611}
6612
9c541725 6613/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6614
9c541725
PA
6615static inline bool
6616offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6617{
9c541725
PA
6618 sect_offset bottom = cu_header->sect_off;
6619 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6620
9c541725 6621 return sect_off >= bottom && sect_off < top;
45452591
DE
6622}
6623
3b80fe9b
DE
6624/* Find the base address of the compilation unit for range lists and
6625 location lists. It will normally be specified by DW_AT_low_pc.
6626 In DWARF-3 draft 4, the base address could be overridden by
6627 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6628 compilation units with discontinuous ranges. */
6629
6630static void
6631dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6632{
6633 struct attribute *attr;
6634
6635 cu->base_known = 0;
6636 cu->base_address = 0;
6637
6638 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6639 if (attr)
6640 {
31aa7e4e 6641 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6642 cu->base_known = 1;
6643 }
6644 else
6645 {
6646 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6647 if (attr)
6648 {
31aa7e4e 6649 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6650 cu->base_known = 1;
6651 }
6652 }
6653}
6654
93311388 6655/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6656 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6657 NOTE: This leaves members offset, first_die_offset to be filled in
6658 by the caller. */
107d2387 6659
d521ce57 6660static const gdb_byte *
107d2387 6661read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6662 const gdb_byte *info_ptr,
6663 struct dwarf2_section_info *section,
6664 rcuh_kind section_kind)
107d2387
AC
6665{
6666 int signed_addr;
891d2f0b 6667 unsigned int bytes_read;
43988095
JK
6668 const char *filename = get_section_file_name (section);
6669 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6670
6671 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6672 cu_header->initial_length_size = bytes_read;
6673 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6674 info_ptr += bytes_read;
107d2387
AC
6675 cu_header->version = read_2_bytes (abfd, info_ptr);
6676 info_ptr += 2;
43988095
JK
6677 if (cu_header->version < 5)
6678 switch (section_kind)
6679 {
6680 case rcuh_kind::COMPILE:
6681 cu_header->unit_type = DW_UT_compile;
6682 break;
6683 case rcuh_kind::TYPE:
6684 cu_header->unit_type = DW_UT_type;
6685 break;
6686 default:
6687 internal_error (__FILE__, __LINE__,
6688 _("read_comp_unit_head: invalid section_kind"));
6689 }
6690 else
6691 {
6692 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6693 (read_1_byte (abfd, info_ptr));
6694 info_ptr += 1;
6695 switch (cu_header->unit_type)
6696 {
6697 case DW_UT_compile:
6698 if (section_kind != rcuh_kind::COMPILE)
6699 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6700 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6701 filename);
6702 break;
6703 case DW_UT_type:
6704 section_kind = rcuh_kind::TYPE;
6705 break;
6706 default:
6707 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6708 "(is %d, should be %d or %d) [in module %s]"),
6709 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6710 }
6711
6712 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6713 info_ptr += 1;
6714 }
9c541725
PA
6715 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6716 cu_header,
6717 &bytes_read);
613e1657 6718 info_ptr += bytes_read;
43988095
JK
6719 if (cu_header->version < 5)
6720 {
6721 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6722 info_ptr += 1;
6723 }
107d2387
AC
6724 signed_addr = bfd_get_sign_extend_vma (abfd);
6725 if (signed_addr < 0)
8e65ff28 6726 internal_error (__FILE__, __LINE__,
e2e0b3e5 6727 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6728 cu_header->signed_addr_p = signed_addr;
c764a876 6729
43988095
JK
6730 if (section_kind == rcuh_kind::TYPE)
6731 {
6732 LONGEST type_offset;
6733
6734 cu_header->signature = read_8_bytes (abfd, info_ptr);
6735 info_ptr += 8;
6736
6737 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6738 info_ptr += bytes_read;
9c541725
PA
6739 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6740 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6741 error (_("Dwarf Error: Too big type_offset in compilation unit "
6742 "header (is %s) [in module %s]"), plongest (type_offset),
6743 filename);
6744 }
6745
107d2387
AC
6746 return info_ptr;
6747}
6748
36586728
TT
6749/* Helper function that returns the proper abbrev section for
6750 THIS_CU. */
6751
6752static struct dwarf2_section_info *
6753get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6754{
6755 struct dwarf2_section_info *abbrev;
ed2dc618 6756 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6757
6758 if (this_cu->is_dwz)
ed2dc618 6759 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6760 else
6761 abbrev = &dwarf2_per_objfile->abbrev;
6762
6763 return abbrev;
6764}
6765
9ff913ba
DE
6766/* Subroutine of read_and_check_comp_unit_head and
6767 read_and_check_type_unit_head to simplify them.
6768 Perform various error checking on the header. */
6769
6770static void
ed2dc618
SM
6771error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6772 struct comp_unit_head *header,
4bdcc0c1
DE
6773 struct dwarf2_section_info *section,
6774 struct dwarf2_section_info *abbrev_section)
9ff913ba 6775{
a32a8923 6776 const char *filename = get_section_file_name (section);
9ff913ba 6777
43988095 6778 if (header->version < 2 || header->version > 5)
9ff913ba 6779 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 6780 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
6781 filename);
6782
9c541725 6783 if (to_underlying (header->abbrev_sect_off)
36586728 6784 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9c541725
PA
6785 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
6786 "(offset 0x%x + 6) [in module %s]"),
6787 to_underlying (header->abbrev_sect_off),
6788 to_underlying (header->sect_off),
9ff913ba
DE
6789 filename);
6790
9c541725 6791 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6792 avoid potential 32-bit overflow. */
9c541725 6793 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6794 > section->size)
9c541725
PA
6795 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6796 "(offset 0x%x + 0) [in module %s]"),
6797 header->length, to_underlying (header->sect_off),
9ff913ba
DE
6798 filename);
6799}
6800
6801/* Read in a CU/TU header and perform some basic error checking.
6802 The contents of the header are stored in HEADER.
6803 The result is a pointer to the start of the first DIE. */
adabb602 6804
d521ce57 6805static const gdb_byte *
ed2dc618
SM
6806read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6807 struct comp_unit_head *header,
9ff913ba 6808 struct dwarf2_section_info *section,
4bdcc0c1 6809 struct dwarf2_section_info *abbrev_section,
d521ce57 6810 const gdb_byte *info_ptr,
43988095 6811 rcuh_kind section_kind)
72bf9492 6812{
d521ce57 6813 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6814
9c541725 6815 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6816
43988095 6817 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6818
9c541725 6819 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6820
ed2dc618
SM
6821 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6822 abbrev_section);
9ff913ba
DE
6823
6824 return info_ptr;
348e048f
DE
6825}
6826
f4dc4d17
DE
6827/* Fetch the abbreviation table offset from a comp or type unit header. */
6828
6829static sect_offset
ed2dc618
SM
6830read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6831 struct dwarf2_section_info *section,
9c541725 6832 sect_offset sect_off)
f4dc4d17 6833{
a32a8923 6834 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6835 const gdb_byte *info_ptr;
ac298888 6836 unsigned int initial_length_size, offset_size;
43988095 6837 uint16_t version;
f4dc4d17
DE
6838
6839 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6840 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6841 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6842 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6843 info_ptr += initial_length_size;
6844
6845 version = read_2_bytes (abfd, info_ptr);
6846 info_ptr += 2;
6847 if (version >= 5)
6848 {
6849 /* Skip unit type and address size. */
6850 info_ptr += 2;
6851 }
6852
9c541725 6853 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6854}
6855
aaa75496
JB
6856/* Allocate a new partial symtab for file named NAME and mark this new
6857 partial symtab as being an include of PST. */
6858
6859static void
d521ce57 6860dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6861 struct objfile *objfile)
6862{
6863 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6864
fbd9ab74
JK
6865 if (!IS_ABSOLUTE_PATH (subpst->filename))
6866 {
6867 /* It shares objfile->objfile_obstack. */
6868 subpst->dirname = pst->dirname;
6869 }
6870
aaa75496
JB
6871 subpst->textlow = 0;
6872 subpst->texthigh = 0;
6873
8d749320
SM
6874 subpst->dependencies
6875 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
6876 subpst->dependencies[0] = pst;
6877 subpst->number_of_dependencies = 1;
6878
6879 subpst->globals_offset = 0;
6880 subpst->n_global_syms = 0;
6881 subpst->statics_offset = 0;
6882 subpst->n_static_syms = 0;
43f3e411 6883 subpst->compunit_symtab = NULL;
aaa75496
JB
6884 subpst->read_symtab = pst->read_symtab;
6885 subpst->readin = 0;
6886
6887 /* No private part is necessary for include psymtabs. This property
6888 can be used to differentiate between such include psymtabs and
10b3939b 6889 the regular ones. */
58a9656e 6890 subpst->read_symtab_private = NULL;
aaa75496
JB
6891}
6892
6893/* Read the Line Number Program data and extract the list of files
6894 included by the source file represented by PST. Build an include
d85a05f0 6895 partial symtab for each of these included files. */
aaa75496
JB
6896
6897static void
6898dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6899 struct die_info *die,
6900 struct partial_symtab *pst)
aaa75496 6901{
fff8551c 6902 line_header_up lh;
d85a05f0 6903 struct attribute *attr;
aaa75496 6904
d85a05f0
DJ
6905 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6906 if (attr)
9c541725 6907 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6908 if (lh == NULL)
6909 return; /* No linetable, so no includes. */
6910
c6da4cef 6911 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 6912 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
6913}
6914
348e048f 6915static hashval_t
52dc124a 6916hash_signatured_type (const void *item)
348e048f 6917{
9a3c8263
SM
6918 const struct signatured_type *sig_type
6919 = (const struct signatured_type *) item;
9a619af0 6920
348e048f 6921 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6922 return sig_type->signature;
348e048f
DE
6923}
6924
6925static int
52dc124a 6926eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6927{
9a3c8263
SM
6928 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6929 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6930
348e048f
DE
6931 return lhs->signature == rhs->signature;
6932}
6933
1fd400ff
TT
6934/* Allocate a hash table for signatured types. */
6935
6936static htab_t
673bfd45 6937allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6938{
6939 return htab_create_alloc_ex (41,
52dc124a
DE
6940 hash_signatured_type,
6941 eq_signatured_type,
1fd400ff
TT
6942 NULL,
6943 &objfile->objfile_obstack,
6944 hashtab_obstack_allocate,
6945 dummy_obstack_deallocate);
6946}
6947
d467dd73 6948/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6949
6950static int
d467dd73 6951add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6952{
9a3c8263
SM
6953 struct signatured_type *sigt = (struct signatured_type *) *slot;
6954 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 6955
b4dd5633 6956 **datap = sigt;
1fd400ff
TT
6957 ++*datap;
6958
6959 return 1;
6960}
6961
78d4d2c5 6962/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6963 and fill them into TYPES_HTAB. It will process only type units,
6964 therefore DW_UT_type. */
c88ee1f0 6965
78d4d2c5 6966static void
ed2dc618
SM
6967create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6968 struct dwo_file *dwo_file,
43988095
JK
6969 dwarf2_section_info *section, htab_t &types_htab,
6970 rcuh_kind section_kind)
348e048f 6971{
3019eac3 6972 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6973 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6974 bfd *abfd;
6975 const gdb_byte *info_ptr, *end_ptr;
348e048f 6976
4bdcc0c1
DE
6977 abbrev_section = (dwo_file != NULL
6978 ? &dwo_file->sections.abbrev
6979 : &dwarf2_per_objfile->abbrev);
6980
b4f54984 6981 if (dwarf_read_debug)
43988095
JK
6982 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6983 get_section_name (section),
a32a8923 6984 get_section_file_name (abbrev_section));
09406207 6985
78d4d2c5
JK
6986 dwarf2_read_section (objfile, section);
6987 info_ptr = section->buffer;
348e048f 6988
78d4d2c5
JK
6989 if (info_ptr == NULL)
6990 return;
348e048f 6991
78d4d2c5
JK
6992 /* We can't set abfd until now because the section may be empty or
6993 not present, in which case the bfd is unknown. */
6994 abfd = get_section_bfd_owner (section);
348e048f 6995
78d4d2c5
JK
6996 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6997 because we don't need to read any dies: the signature is in the
6998 header. */
3019eac3 6999
78d4d2c5
JK
7000 end_ptr = info_ptr + section->size;
7001 while (info_ptr < end_ptr)
7002 {
78d4d2c5
JK
7003 struct signatured_type *sig_type;
7004 struct dwo_unit *dwo_tu;
7005 void **slot;
7006 const gdb_byte *ptr = info_ptr;
7007 struct comp_unit_head header;
7008 unsigned int length;
8b70b953 7009
9c541725 7010 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 7011
a49dd8dd
JK
7012 /* Initialize it due to a false compiler warning. */
7013 header.signature = -1;
9c541725 7014 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 7015
78d4d2c5
JK
7016 /* We need to read the type's signature in order to build the hash
7017 table, but we don't need anything else just yet. */
348e048f 7018
ed2dc618 7019 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 7020 abbrev_section, ptr, section_kind);
348e048f 7021
78d4d2c5 7022 length = get_cu_length (&header);
6caca83c 7023
78d4d2c5
JK
7024 /* Skip dummy type units. */
7025 if (ptr >= info_ptr + length
43988095
JK
7026 || peek_abbrev_code (abfd, ptr) == 0
7027 || header.unit_type != DW_UT_type)
78d4d2c5
JK
7028 {
7029 info_ptr += length;
7030 continue;
7031 }
dee91e82 7032
78d4d2c5
JK
7033 if (types_htab == NULL)
7034 {
7035 if (dwo_file)
7036 types_htab = allocate_dwo_unit_table (objfile);
7037 else
7038 types_htab = allocate_signatured_type_table (objfile);
7039 }
8b70b953 7040
78d4d2c5
JK
7041 if (dwo_file)
7042 {
7043 sig_type = NULL;
7044 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7045 struct dwo_unit);
7046 dwo_tu->dwo_file = dwo_file;
43988095 7047 dwo_tu->signature = header.signature;
9c541725 7048 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 7049 dwo_tu->section = section;
9c541725 7050 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
7051 dwo_tu->length = length;
7052 }
7053 else
7054 {
7055 /* N.B.: type_offset is not usable if this type uses a DWO file.
7056 The real type_offset is in the DWO file. */
7057 dwo_tu = NULL;
7058 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7059 struct signatured_type);
43988095 7060 sig_type->signature = header.signature;
9c541725 7061 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 7062 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
7063 sig_type->per_cu.is_debug_types = 1;
7064 sig_type->per_cu.section = section;
9c541725 7065 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
7066 sig_type->per_cu.length = length;
7067 }
7068
7069 slot = htab_find_slot (types_htab,
7070 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7071 INSERT);
7072 gdb_assert (slot != NULL);
7073 if (*slot != NULL)
7074 {
9c541725 7075 sect_offset dup_sect_off;
0349ea22 7076
3019eac3
DE
7077 if (dwo_file)
7078 {
78d4d2c5
JK
7079 const struct dwo_unit *dup_tu
7080 = (const struct dwo_unit *) *slot;
7081
9c541725 7082 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
7083 }
7084 else
7085 {
78d4d2c5
JK
7086 const struct signatured_type *dup_tu
7087 = (const struct signatured_type *) *slot;
7088
9c541725 7089 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 7090 }
8b70b953 7091
78d4d2c5
JK
7092 complaint (&symfile_complaints,
7093 _("debug type entry at offset 0x%x is duplicate to"
7094 " the entry at offset 0x%x, signature %s"),
9c541725 7095 to_underlying (sect_off), to_underlying (dup_sect_off),
43988095 7096 hex_string (header.signature));
78d4d2c5
JK
7097 }
7098 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 7099
78d4d2c5
JK
7100 if (dwarf_read_debug > 1)
7101 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
9c541725 7102 to_underlying (sect_off),
43988095 7103 hex_string (header.signature));
3019eac3 7104
78d4d2c5
JK
7105 info_ptr += length;
7106 }
7107}
3019eac3 7108
78d4d2c5
JK
7109/* Create the hash table of all entries in the .debug_types
7110 (or .debug_types.dwo) section(s).
7111 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7112 otherwise it is NULL.
b3c8eb43 7113
78d4d2c5 7114 The result is a pointer to the hash table or NULL if there are no types.
348e048f 7115
78d4d2c5 7116 Note: This function processes DWO files only, not DWP files. */
348e048f 7117
78d4d2c5 7118static void
ed2dc618
SM
7119create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7120 struct dwo_file *dwo_file,
78d4d2c5
JK
7121 VEC (dwarf2_section_info_def) *types,
7122 htab_t &types_htab)
7123{
7124 int ix;
7125 struct dwarf2_section_info *section;
7126
7127 if (VEC_empty (dwarf2_section_info_def, types))
7128 return;
348e048f 7129
78d4d2c5
JK
7130 for (ix = 0;
7131 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7132 ++ix)
ed2dc618
SM
7133 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
7134 types_htab, rcuh_kind::TYPE);
3019eac3
DE
7135}
7136
7137/* Create the hash table of all entries in the .debug_types section,
7138 and initialize all_type_units.
7139 The result is zero if there is an error (e.g. missing .debug_types section),
7140 otherwise non-zero. */
7141
7142static int
ed2dc618 7143create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 7144{
78d4d2c5 7145 htab_t types_htab = NULL;
b4dd5633 7146 struct signatured_type **iter;
3019eac3 7147
ed2dc618
SM
7148 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
7149 &dwarf2_per_objfile->info, types_htab,
43988095 7150 rcuh_kind::COMPILE);
ed2dc618
SM
7151 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
7152 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
7153 if (types_htab == NULL)
7154 {
7155 dwarf2_per_objfile->signatured_types = NULL;
7156 return 0;
7157 }
7158
348e048f
DE
7159 dwarf2_per_objfile->signatured_types = types_htab;
7160
6aa5f3a6
DE
7161 dwarf2_per_objfile->n_type_units
7162 = dwarf2_per_objfile->n_allocated_type_units
7163 = htab_elements (types_htab);
8d749320
SM
7164 dwarf2_per_objfile->all_type_units =
7165 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
7166 iter = &dwarf2_per_objfile->all_type_units[0];
7167 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7168 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7169 == dwarf2_per_objfile->n_type_units);
1fd400ff 7170
348e048f
DE
7171 return 1;
7172}
7173
6aa5f3a6
DE
7174/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7175 If SLOT is non-NULL, it is the entry to use in the hash table.
7176 Otherwise we find one. */
7177
7178static struct signatured_type *
ed2dc618
SM
7179add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
7180 void **slot)
6aa5f3a6
DE
7181{
7182 struct objfile *objfile = dwarf2_per_objfile->objfile;
7183 int n_type_units = dwarf2_per_objfile->n_type_units;
7184 struct signatured_type *sig_type;
7185
7186 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7187 ++n_type_units;
7188 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7189 {
7190 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7191 dwarf2_per_objfile->n_allocated_type_units = 1;
7192 dwarf2_per_objfile->n_allocated_type_units *= 2;
7193 dwarf2_per_objfile->all_type_units
224c3ddb
SM
7194 = XRESIZEVEC (struct signatured_type *,
7195 dwarf2_per_objfile->all_type_units,
7196 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
7197 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7198 }
7199 dwarf2_per_objfile->n_type_units = n_type_units;
7200
7201 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7202 struct signatured_type);
7203 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7204 sig_type->signature = sig;
7205 sig_type->per_cu.is_debug_types = 1;
7206 if (dwarf2_per_objfile->using_index)
7207 {
7208 sig_type->per_cu.v.quick =
7209 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7210 struct dwarf2_per_cu_quick_data);
7211 }
7212
7213 if (slot == NULL)
7214 {
7215 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7216 sig_type, INSERT);
7217 }
7218 gdb_assert (*slot == NULL);
7219 *slot = sig_type;
7220 /* The rest of sig_type must be filled in by the caller. */
7221 return sig_type;
7222}
7223
a2ce51a0
DE
7224/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7225 Fill in SIG_ENTRY with DWO_ENTRY. */
7226
7227static void
ed2dc618 7228fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
7229 struct signatured_type *sig_entry,
7230 struct dwo_unit *dwo_entry)
7231{
7ee85ab1 7232 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
7233 gdb_assert (! sig_entry->per_cu.queued);
7234 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
7235 if (dwarf2_per_objfile->using_index)
7236 {
7237 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 7238 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
7239 }
7240 else
7241 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 7242 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 7243 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 7244 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
7245 gdb_assert (sig_entry->dwo_unit == NULL);
7246
7247 sig_entry->per_cu.section = dwo_entry->section;
9c541725 7248 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
7249 sig_entry->per_cu.length = dwo_entry->length;
7250 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 7251 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
7252 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7253 sig_entry->dwo_unit = dwo_entry;
7254}
7255
7256/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
7257 If we haven't read the TU yet, create the signatured_type data structure
7258 for a TU to be read in directly from a DWO file, bypassing the stub.
7259 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7260 using .gdb_index, then when reading a CU we want to stay in the DWO file
7261 containing that CU. Otherwise we could end up reading several other DWO
7262 files (due to comdat folding) to process the transitive closure of all the
7263 mentioned TUs, and that can be slow. The current DWO file will have every
7264 type signature that it needs.
a2ce51a0
DE
7265 We only do this for .gdb_index because in the psymtab case we already have
7266 to read all the DWOs to build the type unit groups. */
7267
7268static struct signatured_type *
7269lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7270{
518817b3
SM
7271 struct dwarf2_per_objfile *dwarf2_per_objfile
7272 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
7273 struct objfile *objfile = dwarf2_per_objfile->objfile;
7274 struct dwo_file *dwo_file;
7275 struct dwo_unit find_dwo_entry, *dwo_entry;
7276 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7277 void **slot;
a2ce51a0
DE
7278
7279 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7280
6aa5f3a6
DE
7281 /* If TU skeletons have been removed then we may not have read in any
7282 TUs yet. */
7283 if (dwarf2_per_objfile->signatured_types == NULL)
7284 {
7285 dwarf2_per_objfile->signatured_types
7286 = allocate_signatured_type_table (objfile);
7287 }
a2ce51a0
DE
7288
7289 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7290 Use the global signatured_types array to do our own comdat-folding
7291 of types. If this is the first time we're reading this TU, and
7292 the TU has an entry in .gdb_index, replace the recorded data from
7293 .gdb_index with this TU. */
a2ce51a0 7294
a2ce51a0 7295 find_sig_entry.signature = sig;
6aa5f3a6
DE
7296 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7297 &find_sig_entry, INSERT);
9a3c8263 7298 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7299
7300 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7301 read. Don't reassign the global entry to point to this DWO if that's
7302 the case. Also note that if the TU is already being read, it may not
7303 have come from a DWO, the program may be a mix of Fission-compiled
7304 code and non-Fission-compiled code. */
7305
7306 /* Have we already tried to read this TU?
7307 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7308 needn't exist in the global table yet). */
7309 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7310 return sig_entry;
7311
6aa5f3a6
DE
7312 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7313 dwo_unit of the TU itself. */
7314 dwo_file = cu->dwo_unit->dwo_file;
7315
a2ce51a0
DE
7316 /* Ok, this is the first time we're reading this TU. */
7317 if (dwo_file->tus == NULL)
7318 return NULL;
7319 find_dwo_entry.signature = sig;
9a3c8263 7320 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7321 if (dwo_entry == NULL)
7322 return NULL;
7323
6aa5f3a6
DE
7324 /* If the global table doesn't have an entry for this TU, add one. */
7325 if (sig_entry == NULL)
ed2dc618 7326 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7327
ed2dc618 7328 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7329 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7330 return sig_entry;
7331}
7332
a2ce51a0
DE
7333/* Subroutine of lookup_signatured_type.
7334 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7335 then try the DWP file. If the TU stub (skeleton) has been removed then
7336 it won't be in .gdb_index. */
a2ce51a0
DE
7337
7338static struct signatured_type *
7339lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7340{
518817b3
SM
7341 struct dwarf2_per_objfile *dwarf2_per_objfile
7342 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7343 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7344 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7345 struct dwo_unit *dwo_entry;
7346 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7347 void **slot;
a2ce51a0
DE
7348
7349 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7350 gdb_assert (dwp_file != NULL);
7351
6aa5f3a6
DE
7352 /* If TU skeletons have been removed then we may not have read in any
7353 TUs yet. */
7354 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7355 {
6aa5f3a6
DE
7356 dwarf2_per_objfile->signatured_types
7357 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7358 }
7359
6aa5f3a6
DE
7360 find_sig_entry.signature = sig;
7361 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7362 &find_sig_entry, INSERT);
9a3c8263 7363 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7364
7365 /* Have we already tried to read this TU?
7366 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7367 needn't exist in the global table yet). */
7368 if (sig_entry != NULL)
7369 return sig_entry;
7370
a2ce51a0
DE
7371 if (dwp_file->tus == NULL)
7372 return NULL;
ed2dc618 7373 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7374 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7375 if (dwo_entry == NULL)
7376 return NULL;
7377
ed2dc618
SM
7378 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7379 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7380
a2ce51a0
DE
7381 return sig_entry;
7382}
7383
380bca97 7384/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7385 Returns NULL if signature SIG is not present in the table.
7386 It is up to the caller to complain about this. */
348e048f
DE
7387
7388static struct signatured_type *
a2ce51a0 7389lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7390{
518817b3
SM
7391 struct dwarf2_per_objfile *dwarf2_per_objfile
7392 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7393
a2ce51a0
DE
7394 if (cu->dwo_unit
7395 && dwarf2_per_objfile->using_index)
7396 {
7397 /* We're in a DWO/DWP file, and we're using .gdb_index.
7398 These cases require special processing. */
ed2dc618 7399 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7400 return lookup_dwo_signatured_type (cu, sig);
7401 else
7402 return lookup_dwp_signatured_type (cu, sig);
7403 }
7404 else
7405 {
7406 struct signatured_type find_entry, *entry;
348e048f 7407
a2ce51a0
DE
7408 if (dwarf2_per_objfile->signatured_types == NULL)
7409 return NULL;
7410 find_entry.signature = sig;
9a3c8263
SM
7411 entry = ((struct signatured_type *)
7412 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7413 return entry;
7414 }
348e048f 7415}
42e7ad6c
DE
7416\f
7417/* Low level DIE reading support. */
348e048f 7418
d85a05f0
DJ
7419/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7420
7421static void
7422init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7423 struct dwarf2_cu *cu,
3019eac3
DE
7424 struct dwarf2_section_info *section,
7425 struct dwo_file *dwo_file)
d85a05f0 7426{
fceca515 7427 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7428 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7429 reader->cu = cu;
3019eac3 7430 reader->dwo_file = dwo_file;
dee91e82
DE
7431 reader->die_section = section;
7432 reader->buffer = section->buffer;
f664829e 7433 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7434 reader->comp_dir = NULL;
d85a05f0
DJ
7435}
7436
b0c7bfa9
DE
7437/* Subroutine of init_cutu_and_read_dies to simplify it.
7438 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7439 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7440 already.
7441
7442 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7443 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7444 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7445 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7446 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7447 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7448 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7449 are filled in with the info of the DIE from the DWO file.
7450 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
7451 provided an abbrev table to use.
7452 The result is non-zero if a valid (non-dummy) DIE was found. */
7453
7454static int
7455read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7456 struct dwo_unit *dwo_unit,
7457 int abbrev_table_provided,
7458 struct die_info *stub_comp_unit_die,
a2ce51a0 7459 const char *stub_comp_dir,
b0c7bfa9 7460 struct die_reader_specs *result_reader,
d521ce57 7461 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
7462 struct die_info **result_comp_unit_die,
7463 int *result_has_children)
7464{
ed2dc618 7465 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7466 struct objfile *objfile = dwarf2_per_objfile->objfile;
7467 struct dwarf2_cu *cu = this_cu->cu;
7468 struct dwarf2_section_info *section;
7469 bfd *abfd;
d521ce57 7470 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7471 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7472 int i,num_extra_attrs;
7473 struct dwarf2_section_info *dwo_abbrev_section;
7474 struct attribute *attr;
7475 struct die_info *comp_unit_die;
7476
b0aeadb3
DE
7477 /* At most one of these may be provided. */
7478 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7479
b0c7bfa9
DE
7480 /* These attributes aren't processed until later:
7481 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7482 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7483 referenced later. However, these attributes are found in the stub
7484 which we won't have later. In order to not impose this complication
7485 on the rest of the code, we read them here and copy them to the
7486 DWO CU/TU die. */
b0c7bfa9
DE
7487
7488 stmt_list = NULL;
7489 low_pc = NULL;
7490 high_pc = NULL;
7491 ranges = NULL;
7492 comp_dir = NULL;
7493
7494 if (stub_comp_unit_die != NULL)
7495 {
7496 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7497 DWO file. */
7498 if (! this_cu->is_debug_types)
7499 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7500 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7501 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7502 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7503 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7504
7505 /* There should be a DW_AT_addr_base attribute here (if needed).
7506 We need the value before we can process DW_FORM_GNU_addr_index. */
7507 cu->addr_base = 0;
7508 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7509 if (attr)
7510 cu->addr_base = DW_UNSND (attr);
7511
7512 /* There should be a DW_AT_ranges_base attribute here (if needed).
7513 We need the value before we can process DW_AT_ranges. */
7514 cu->ranges_base = 0;
7515 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7516 if (attr)
7517 cu->ranges_base = DW_UNSND (attr);
7518 }
a2ce51a0
DE
7519 else if (stub_comp_dir != NULL)
7520 {
7521 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7522 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7523 comp_dir->name = DW_AT_comp_dir;
7524 comp_dir->form = DW_FORM_string;
7525 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7526 DW_STRING (comp_dir) = stub_comp_dir;
7527 }
b0c7bfa9
DE
7528
7529 /* Set up for reading the DWO CU/TU. */
7530 cu->dwo_unit = dwo_unit;
7531 section = dwo_unit->section;
7532 dwarf2_read_section (objfile, section);
a32a8923 7533 abfd = get_section_bfd_owner (section);
9c541725
PA
7534 begin_info_ptr = info_ptr = (section->buffer
7535 + to_underlying (dwo_unit->sect_off));
b0c7bfa9
DE
7536 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7537 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
7538
7539 if (this_cu->is_debug_types)
7540 {
b0c7bfa9
DE
7541 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7542
ed2dc618
SM
7543 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7544 &cu->header, section,
b0c7bfa9 7545 dwo_abbrev_section,
43988095 7546 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7547 /* This is not an assert because it can be caused by bad debug info. */
43988095 7548 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7549 {
7550 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7551 " TU at offset 0x%x [in module %s]"),
7552 hex_string (sig_type->signature),
43988095 7553 hex_string (cu->header.signature),
9c541725 7554 to_underlying (dwo_unit->sect_off),
a2ce51a0
DE
7555 bfd_get_filename (abfd));
7556 }
9c541725 7557 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7558 /* For DWOs coming from DWP files, we don't know the CU length
7559 nor the type's offset in the TU until now. */
7560 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7561 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7562
7563 /* Establish the type offset that can be used to lookup the type.
7564 For DWO files, we don't know it until now. */
9c541725
PA
7565 sig_type->type_offset_in_section
7566 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7567 }
7568 else
7569 {
ed2dc618
SM
7570 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7571 &cu->header, section,
b0c7bfa9 7572 dwo_abbrev_section,
43988095 7573 info_ptr, rcuh_kind::COMPILE);
9c541725 7574 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7575 /* For DWOs coming from DWP files, we don't know the CU length
7576 until now. */
7577 dwo_unit->length = get_cu_length (&cu->header);
7578 }
7579
02142a6c
DE
7580 /* Replace the CU's original abbrev table with the DWO's.
7581 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
7582 if (abbrev_table_provided)
7583 {
7584 /* Don't free the provided abbrev table, the caller of
7585 init_cutu_and_read_dies owns it. */
7586 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 7587 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
7588 make_cleanup (dwarf2_free_abbrev_table, cu);
7589 }
7590 else
7591 {
7592 dwarf2_free_abbrev_table (cu);
7593 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 7594 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
7595 }
7596
7597 /* Read in the die, but leave space to copy over the attributes
7598 from the stub. This has the benefit of simplifying the rest of
7599 the code - all the work to maintain the illusion of a single
7600 DW_TAG_{compile,type}_unit DIE is done here. */
7601 num_extra_attrs = ((stmt_list != NULL)
7602 + (low_pc != NULL)
7603 + (high_pc != NULL)
7604 + (ranges != NULL)
7605 + (comp_dir != NULL));
7606 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7607 result_has_children, num_extra_attrs);
7608
7609 /* Copy over the attributes from the stub to the DIE we just read in. */
7610 comp_unit_die = *result_comp_unit_die;
7611 i = comp_unit_die->num_attrs;
7612 if (stmt_list != NULL)
7613 comp_unit_die->attrs[i++] = *stmt_list;
7614 if (low_pc != NULL)
7615 comp_unit_die->attrs[i++] = *low_pc;
7616 if (high_pc != NULL)
7617 comp_unit_die->attrs[i++] = *high_pc;
7618 if (ranges != NULL)
7619 comp_unit_die->attrs[i++] = *ranges;
7620 if (comp_dir != NULL)
7621 comp_unit_die->attrs[i++] = *comp_dir;
7622 comp_unit_die->num_attrs += num_extra_attrs;
7623
b4f54984 7624 if (dwarf_die_debug)
bf6af496
DE
7625 {
7626 fprintf_unfiltered (gdb_stdlog,
7627 "Read die from %s@0x%x of %s:\n",
a32a8923 7628 get_section_name (section),
bf6af496
DE
7629 (unsigned) (begin_info_ptr - section->buffer),
7630 bfd_get_filename (abfd));
b4f54984 7631 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7632 }
7633
a2ce51a0
DE
7634 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7635 TUs by skipping the stub and going directly to the entry in the DWO file.
7636 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7637 to get it via circuitous means. Blech. */
7638 if (comp_dir != NULL)
7639 result_reader->comp_dir = DW_STRING (comp_dir);
7640
b0c7bfa9
DE
7641 /* Skip dummy compilation units. */
7642 if (info_ptr >= begin_info_ptr + dwo_unit->length
7643 || peek_abbrev_code (abfd, info_ptr) == 0)
7644 return 0;
7645
7646 *result_info_ptr = info_ptr;
7647 return 1;
7648}
7649
7650/* Subroutine of init_cutu_and_read_dies to simplify it.
7651 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7652 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7653
7654static struct dwo_unit *
7655lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7656 struct die_info *comp_unit_die)
7657{
7658 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7659 ULONGEST signature;
7660 struct dwo_unit *dwo_unit;
7661 const char *comp_dir, *dwo_name;
7662
a2ce51a0
DE
7663 gdb_assert (cu != NULL);
7664
b0c7bfa9 7665 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
7666 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7667 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7668
7669 if (this_cu->is_debug_types)
7670 {
7671 struct signatured_type *sig_type;
7672
7673 /* Since this_cu is the first member of struct signatured_type,
7674 we can go from a pointer to one to a pointer to the other. */
7675 sig_type = (struct signatured_type *) this_cu;
7676 signature = sig_type->signature;
7677 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7678 }
7679 else
7680 {
7681 struct attribute *attr;
7682
7683 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7684 if (! attr)
7685 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7686 " [in module %s]"),
e3b94546 7687 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9
DE
7688 signature = DW_UNSND (attr);
7689 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7690 signature);
7691 }
7692
b0c7bfa9
DE
7693 return dwo_unit;
7694}
7695
a2ce51a0 7696/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6
DE
7697 See it for a description of the parameters.
7698 Read a TU directly from a DWO file, bypassing the stub.
7699
7700 Note: This function could be a little bit simpler if we shared cleanups
7701 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
7702 to do, so we keep this function self-contained. Or we could move this
7703 into our caller, but it's complex enough already. */
a2ce51a0
DE
7704
7705static void
6aa5f3a6
DE
7706init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7707 int use_existing_cu, int keep,
a2ce51a0
DE
7708 die_reader_func_ftype *die_reader_func,
7709 void *data)
7710{
7711 struct dwarf2_cu *cu;
7712 struct signatured_type *sig_type;
6aa5f3a6 7713 struct cleanup *cleanups, *free_cu_cleanup = NULL;
a2ce51a0
DE
7714 struct die_reader_specs reader;
7715 const gdb_byte *info_ptr;
7716 struct die_info *comp_unit_die;
7717 int has_children;
ed2dc618 7718 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7719
7720 /* Verify we can do the following downcast, and that we have the
7721 data we need. */
7722 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7723 sig_type = (struct signatured_type *) this_cu;
7724 gdb_assert (sig_type->dwo_unit != NULL);
7725
7726 cleanups = make_cleanup (null_cleanup, NULL);
7727
6aa5f3a6
DE
7728 if (use_existing_cu && this_cu->cu != NULL)
7729 {
7730 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7731 cu = this_cu->cu;
7732 /* There's no need to do the rereading_dwo_cu handling that
7733 init_cutu_and_read_dies does since we don't read the stub. */
7734 }
7735 else
7736 {
7737 /* If !use_existing_cu, this_cu->cu must be NULL. */
7738 gdb_assert (this_cu->cu == NULL);
8d749320 7739 cu = XNEW (struct dwarf2_cu);
6aa5f3a6
DE
7740 init_one_comp_unit (cu, this_cu);
7741 /* If an error occurs while loading, release our storage. */
7742 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
7743 }
7744
7745 /* A future optimization, if needed, would be to use an existing
7746 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7747 could share abbrev tables. */
a2ce51a0
DE
7748
7749 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7750 0 /* abbrev_table_provided */,
7751 NULL /* stub_comp_unit_die */,
7752 sig_type->dwo_unit->dwo_file->comp_dir,
7753 &reader, &info_ptr,
7754 &comp_unit_die, &has_children) == 0)
7755 {
7756 /* Dummy die. */
7757 do_cleanups (cleanups);
7758 return;
7759 }
7760
7761 /* All the "real" work is done here. */
7762 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7763
6aa5f3a6 7764 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7765 but the alternative is making the latter more complex.
7766 This function is only for the special case of using DWO files directly:
7767 no point in overly complicating the general case just to handle this. */
6aa5f3a6 7768 if (free_cu_cleanup != NULL)
a2ce51a0 7769 {
6aa5f3a6
DE
7770 if (keep)
7771 {
7772 /* We've successfully allocated this compilation unit. Let our
7773 caller clean it up when finished with it. */
7774 discard_cleanups (free_cu_cleanup);
a2ce51a0 7775
6aa5f3a6
DE
7776 /* We can only discard free_cu_cleanup and all subsequent cleanups.
7777 So we have to manually free the abbrev table. */
7778 dwarf2_free_abbrev_table (cu);
a2ce51a0 7779
6aa5f3a6
DE
7780 /* Link this CU into read_in_chain. */
7781 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7782 dwarf2_per_objfile->read_in_chain = this_cu;
7783 }
7784 else
7785 do_cleanups (free_cu_cleanup);
a2ce51a0 7786 }
a2ce51a0
DE
7787
7788 do_cleanups (cleanups);
7789}
7790
fd820528 7791/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7792 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7793
f4dc4d17
DE
7794 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7795 Otherwise the table specified in the comp unit header is read in and used.
7796 This is an optimization for when we already have the abbrev table.
7797
dee91e82
DE
7798 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7799 Otherwise, a new CU is allocated with xmalloc.
7800
7801 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7802 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7803
7804 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7805 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7806
70221824 7807static void
fd820528 7808init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7809 struct abbrev_table *abbrev_table,
fd820528
DE
7810 int use_existing_cu, int keep,
7811 die_reader_func_ftype *die_reader_func,
7812 void *data)
c906108c 7813{
ed2dc618 7814 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7815 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7816 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7817 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7818 struct dwarf2_cu *cu;
d521ce57 7819 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7820 struct die_reader_specs reader;
d85a05f0 7821 struct die_info *comp_unit_die;
dee91e82 7822 int has_children;
d85a05f0 7823 struct attribute *attr;
365156ad 7824 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 7825 struct signatured_type *sig_type = NULL;
4bdcc0c1 7826 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7827 /* Non-zero if CU currently points to a DWO file and we need to
7828 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7829 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7830 int rereading_dwo_cu = 0;
c906108c 7831
b4f54984 7832 if (dwarf_die_debug)
09406207
DE
7833 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7834 this_cu->is_debug_types ? "type" : "comp",
9c541725 7835 to_underlying (this_cu->sect_off));
09406207 7836
dee91e82
DE
7837 if (use_existing_cu)
7838 gdb_assert (keep);
23745b47 7839
a2ce51a0
DE
7840 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7841 file (instead of going through the stub), short-circuit all of this. */
7842 if (this_cu->reading_dwo_directly)
7843 {
7844 /* Narrow down the scope of possibilities to have to understand. */
7845 gdb_assert (this_cu->is_debug_types);
7846 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7847 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7848 die_reader_func, data);
a2ce51a0
DE
7849 return;
7850 }
7851
dee91e82
DE
7852 cleanups = make_cleanup (null_cleanup, NULL);
7853
7854 /* This is cheap if the section is already read in. */
7855 dwarf2_read_section (objfile, section);
7856
9c541725 7857 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7858
7859 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
7860
7861 if (use_existing_cu && this_cu->cu != NULL)
7862 {
7863 cu = this_cu->cu;
42e7ad6c
DE
7864 /* If this CU is from a DWO file we need to start over, we need to
7865 refetch the attributes from the skeleton CU.
7866 This could be optimized by retrieving those attributes from when we
7867 were here the first time: the previous comp_unit_die was stored in
7868 comp_unit_obstack. But there's no data yet that we need this
7869 optimization. */
7870 if (cu->dwo_unit != NULL)
7871 rereading_dwo_cu = 1;
dee91e82
DE
7872 }
7873 else
7874 {
7875 /* If !use_existing_cu, this_cu->cu must be NULL. */
7876 gdb_assert (this_cu->cu == NULL);
8d749320 7877 cu = XNEW (struct dwarf2_cu);
dee91e82 7878 init_one_comp_unit (cu, this_cu);
dee91e82 7879 /* If an error occurs while loading, release our storage. */
365156ad 7880 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 7881 }
dee91e82 7882
b0c7bfa9 7883 /* Get the header. */
9c541725 7884 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7885 {
7886 /* We already have the header, there's no need to read it in again. */
9c541725 7887 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7888 }
7889 else
7890 {
3019eac3 7891 if (this_cu->is_debug_types)
dee91e82 7892 {
ed2dc618
SM
7893 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7894 &cu->header, section,
4bdcc0c1 7895 abbrev_section, info_ptr,
43988095 7896 rcuh_kind::TYPE);
dee91e82 7897
42e7ad6c
DE
7898 /* Since per_cu is the first member of struct signatured_type,
7899 we can go from a pointer to one to a pointer to the other. */
7900 sig_type = (struct signatured_type *) this_cu;
43988095 7901 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7902 gdb_assert (sig_type->type_offset_in_tu
7903 == cu->header.type_cu_offset_in_tu);
7904 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7905
42e7ad6c
DE
7906 /* LENGTH has not been set yet for type units if we're
7907 using .gdb_index. */
1ce1cefd 7908 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7909
7910 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7911 sig_type->type_offset_in_section =
7912 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7913
7914 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7915 }
7916 else
7917 {
ed2dc618
SM
7918 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7919 &cu->header, section,
4bdcc0c1 7920 abbrev_section,
43988095
JK
7921 info_ptr,
7922 rcuh_kind::COMPILE);
dee91e82 7923
9c541725 7924 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7925 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7926 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7927 }
7928 }
10b3939b 7929
6caca83c 7930 /* Skip dummy compilation units. */
dee91e82 7931 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
7932 || peek_abbrev_code (abfd, info_ptr) == 0)
7933 {
dee91e82 7934 do_cleanups (cleanups);
21b2bd31 7935 return;
6caca83c
CC
7936 }
7937
433df2d4
DE
7938 /* If we don't have them yet, read the abbrevs for this compilation unit.
7939 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
7940 done. Note that it's important that if the CU had an abbrev table
7941 on entry we don't free it when we're done: Somewhere up the call stack
7942 it may be in use. */
f4dc4d17
DE
7943 if (abbrev_table != NULL)
7944 {
7945 gdb_assert (cu->abbrev_table == NULL);
9c541725 7946 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
f4dc4d17
DE
7947 cu->abbrev_table = abbrev_table;
7948 }
7949 else if (cu->abbrev_table == NULL)
dee91e82 7950 {
4bdcc0c1 7951 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
7952 make_cleanup (dwarf2_free_abbrev_table, cu);
7953 }
42e7ad6c
DE
7954 else if (rereading_dwo_cu)
7955 {
7956 dwarf2_free_abbrev_table (cu);
7957 dwarf2_read_abbrevs (cu, abbrev_section);
7958 }
af703f96 7959
dee91e82 7960 /* Read the top level CU/TU die. */
3019eac3 7961 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 7962 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7963
b0c7bfa9
DE
7964 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7965 from the DWO file.
7966 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7967 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
7968 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7969 if (attr)
7970 {
3019eac3 7971 struct dwo_unit *dwo_unit;
b0c7bfa9 7972 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7973
7974 if (has_children)
6a506a2d
DE
7975 {
7976 complaint (&symfile_complaints,
7977 _("compilation unit with DW_AT_GNU_dwo_name"
7978 " has children (offset 0x%x) [in module %s]"),
9c541725 7979 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6a506a2d 7980 }
b0c7bfa9 7981 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7982 if (dwo_unit != NULL)
3019eac3 7983 {
6a506a2d
DE
7984 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7985 abbrev_table != NULL,
a2ce51a0 7986 comp_unit_die, NULL,
6a506a2d
DE
7987 &reader, &info_ptr,
7988 &dwo_comp_unit_die, &has_children) == 0)
7989 {
7990 /* Dummy die. */
7991 do_cleanups (cleanups);
7992 return;
7993 }
7994 comp_unit_die = dwo_comp_unit_die;
7995 }
7996 else
7997 {
7998 /* Yikes, we couldn't find the rest of the DIE, we only have
7999 the stub. A complaint has already been logged. There's
8000 not much more we can do except pass on the stub DIE to
8001 die_reader_func. We don't want to throw an error on bad
8002 debug info. */
3019eac3
DE
8003 }
8004 }
8005
b0c7bfa9 8006 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
8007 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8008
b0c7bfa9 8009 /* Done, clean up. */
365156ad 8010 if (free_cu_cleanup != NULL)
348e048f 8011 {
365156ad
TT
8012 if (keep)
8013 {
8014 /* We've successfully allocated this compilation unit. Let our
8015 caller clean it up when finished with it. */
8016 discard_cleanups (free_cu_cleanup);
dee91e82 8017
365156ad
TT
8018 /* We can only discard free_cu_cleanup and all subsequent cleanups.
8019 So we have to manually free the abbrev table. */
8020 dwarf2_free_abbrev_table (cu);
dee91e82 8021
365156ad
TT
8022 /* Link this CU into read_in_chain. */
8023 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8024 dwarf2_per_objfile->read_in_chain = this_cu;
8025 }
8026 else
8027 do_cleanups (free_cu_cleanup);
348e048f 8028 }
365156ad
TT
8029
8030 do_cleanups (cleanups);
dee91e82
DE
8031}
8032
33e80786
DE
8033/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
8034 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
8035 to have already done the lookup to find the DWO file).
dee91e82
DE
8036
8037 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 8038 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
8039
8040 We fill in THIS_CU->length.
8041
8042 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
8043 linker) then DIE_READER_FUNC will not get called.
8044
8045 THIS_CU->cu is always freed when done.
3019eac3
DE
8046 This is done in order to not leave THIS_CU->cu in a state where we have
8047 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
8048
8049static void
8050init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 8051 struct dwo_file *dwo_file,
dee91e82
DE
8052 die_reader_func_ftype *die_reader_func,
8053 void *data)
8054{
ed2dc618 8055 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 8056 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 8057 struct dwarf2_section_info *section = this_cu->section;
a32a8923 8058 bfd *abfd = get_section_bfd_owner (section);
33e80786 8059 struct dwarf2_section_info *abbrev_section;
dee91e82 8060 struct dwarf2_cu cu;
d521ce57 8061 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
8062 struct die_reader_specs reader;
8063 struct cleanup *cleanups;
8064 struct die_info *comp_unit_die;
8065 int has_children;
8066
b4f54984 8067 if (dwarf_die_debug)
09406207
DE
8068 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
8069 this_cu->is_debug_types ? "type" : "comp",
9c541725 8070 to_underlying (this_cu->sect_off));
09406207 8071
dee91e82
DE
8072 gdb_assert (this_cu->cu == NULL);
8073
33e80786
DE
8074 abbrev_section = (dwo_file != NULL
8075 ? &dwo_file->sections.abbrev
8076 : get_abbrev_section_for_cu (this_cu));
8077
dee91e82
DE
8078 /* This is cheap if the section is already read in. */
8079 dwarf2_read_section (objfile, section);
8080
8081 init_one_comp_unit (&cu, this_cu);
8082
8083 cleanups = make_cleanup (free_stack_comp_unit, &cu);
8084
9c541725 8085 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
8086 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8087 &cu.header, section,
4bdcc0c1 8088 abbrev_section, info_ptr,
43988095
JK
8089 (this_cu->is_debug_types
8090 ? rcuh_kind::TYPE
8091 : rcuh_kind::COMPILE));
dee91e82 8092
1ce1cefd 8093 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
8094
8095 /* Skip dummy compilation units. */
8096 if (info_ptr >= begin_info_ptr + this_cu->length
8097 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 8098 {
dee91e82 8099 do_cleanups (cleanups);
21b2bd31 8100 return;
93311388 8101 }
72bf9492 8102
dee91e82
DE
8103 dwarf2_read_abbrevs (&cu, abbrev_section);
8104 make_cleanup (dwarf2_free_abbrev_table, &cu);
8105
3019eac3 8106 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
8107 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8108
8109 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8110
8111 do_cleanups (cleanups);
8112}
8113
3019eac3
DE
8114/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8115 does not lookup the specified DWO file.
8116 This cannot be used to read DWO files.
dee91e82
DE
8117
8118 THIS_CU->cu is always freed when done.
3019eac3
DE
8119 This is done in order to not leave THIS_CU->cu in a state where we have
8120 to care whether it refers to the "main" CU or the DWO CU.
8121 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
8122
8123static void
8124init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8125 die_reader_func_ftype *die_reader_func,
8126 void *data)
8127{
33e80786 8128 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 8129}
0018ea6f
DE
8130\f
8131/* Type Unit Groups.
dee91e82 8132
0018ea6f
DE
8133 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8134 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8135 so that all types coming from the same compilation (.o file) are grouped
8136 together. A future step could be to put the types in the same symtab as
8137 the CU the types ultimately came from. */
ff013f42 8138
f4dc4d17
DE
8139static hashval_t
8140hash_type_unit_group (const void *item)
8141{
9a3c8263
SM
8142 const struct type_unit_group *tu_group
8143 = (const struct type_unit_group *) item;
f4dc4d17 8144
094b34ac 8145 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 8146}
348e048f
DE
8147
8148static int
f4dc4d17 8149eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 8150{
9a3c8263
SM
8151 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8152 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 8153
094b34ac 8154 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 8155}
348e048f 8156
f4dc4d17
DE
8157/* Allocate a hash table for type unit groups. */
8158
8159static htab_t
ed2dc618 8160allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
8161{
8162 return htab_create_alloc_ex (3,
8163 hash_type_unit_group,
8164 eq_type_unit_group,
8165 NULL,
ed2dc618 8166 &objfile->objfile_obstack,
f4dc4d17
DE
8167 hashtab_obstack_allocate,
8168 dummy_obstack_deallocate);
8169}
dee91e82 8170
f4dc4d17
DE
8171/* Type units that don't have DW_AT_stmt_list are grouped into their own
8172 partial symtabs. We combine several TUs per psymtab to not let the size
8173 of any one psymtab grow too big. */
8174#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8175#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 8176
094b34ac 8177/* Helper routine for get_type_unit_group.
f4dc4d17
DE
8178 Create the type_unit_group object used to hold one or more TUs. */
8179
8180static struct type_unit_group *
094b34ac 8181create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 8182{
518817b3
SM
8183 struct dwarf2_per_objfile *dwarf2_per_objfile
8184 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 8185 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 8186 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 8187 struct type_unit_group *tu_group;
f4dc4d17
DE
8188
8189 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8190 struct type_unit_group);
094b34ac 8191 per_cu = &tu_group->per_cu;
518817b3 8192 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 8193
094b34ac
DE
8194 if (dwarf2_per_objfile->using_index)
8195 {
8196 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8197 struct dwarf2_per_cu_quick_data);
094b34ac
DE
8198 }
8199 else
8200 {
9c541725 8201 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
8202 struct partial_symtab *pst;
8203 char *name;
8204
8205 /* Give the symtab a useful name for debug purposes. */
8206 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8207 name = xstrprintf ("<type_units_%d>",
8208 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8209 else
8210 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8211
8212 pst = create_partial_symtab (per_cu, name);
8213 pst->anonymous = 1;
f4dc4d17 8214
094b34ac
DE
8215 xfree (name);
8216 }
f4dc4d17 8217
094b34ac 8218 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 8219 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
8220
8221 return tu_group;
8222}
8223
094b34ac
DE
8224/* Look up the type_unit_group for type unit CU, and create it if necessary.
8225 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
8226
8227static struct type_unit_group *
ff39bb5e 8228get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 8229{
518817b3
SM
8230 struct dwarf2_per_objfile *dwarf2_per_objfile
8231 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8232 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8233 struct type_unit_group *tu_group;
8234 void **slot;
8235 unsigned int line_offset;
8236 struct type_unit_group type_unit_group_for_lookup;
8237
8238 if (dwarf2_per_objfile->type_unit_groups == NULL)
8239 {
8240 dwarf2_per_objfile->type_unit_groups =
ed2dc618 8241 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
8242 }
8243
8244 /* Do we need to create a new group, or can we use an existing one? */
8245
8246 if (stmt_list)
8247 {
8248 line_offset = DW_UNSND (stmt_list);
8249 ++tu_stats->nr_symtab_sharers;
8250 }
8251 else
8252 {
8253 /* Ugh, no stmt_list. Rare, but we have to handle it.
8254 We can do various things here like create one group per TU or
8255 spread them over multiple groups to split up the expansion work.
8256 To avoid worst case scenarios (too many groups or too large groups)
8257 we, umm, group them in bunches. */
8258 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8259 | (tu_stats->nr_stmt_less_type_units
8260 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8261 ++tu_stats->nr_stmt_less_type_units;
8262 }
8263
094b34ac 8264 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 8265 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
8266 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8267 &type_unit_group_for_lookup, INSERT);
8268 if (*slot != NULL)
8269 {
9a3c8263 8270 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
8271 gdb_assert (tu_group != NULL);
8272 }
8273 else
8274 {
9c541725 8275 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 8276 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
8277 *slot = tu_group;
8278 ++tu_stats->nr_symtabs;
8279 }
8280
8281 return tu_group;
8282}
0018ea6f
DE
8283\f
8284/* Partial symbol tables. */
8285
8286/* Create a psymtab named NAME and assign it to PER_CU.
8287
8288 The caller must fill in the following details:
8289 dirname, textlow, texthigh. */
8290
8291static struct partial_symtab *
8292create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8293{
e3b94546 8294 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
8295 struct partial_symtab *pst;
8296
18a94d75 8297 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
8298 objfile->global_psymbols,
8299 objfile->static_psymbols);
0018ea6f
DE
8300
8301 pst->psymtabs_addrmap_supported = 1;
8302
8303 /* This is the glue that links PST into GDB's symbol API. */
8304 pst->read_symtab_private = per_cu;
8305 pst->read_symtab = dwarf2_read_symtab;
8306 per_cu->v.psymtab = pst;
8307
8308 return pst;
8309}
8310
b93601f3
TT
8311/* The DATA object passed to process_psymtab_comp_unit_reader has this
8312 type. */
8313
8314struct process_psymtab_comp_unit_data
8315{
8316 /* True if we are reading a DW_TAG_partial_unit. */
8317
8318 int want_partial_unit;
8319
8320 /* The "pretend" language that is used if the CU doesn't declare a
8321 language. */
8322
8323 enum language pretend_language;
8324};
8325
0018ea6f
DE
8326/* die_reader_func for process_psymtab_comp_unit. */
8327
8328static void
8329process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8330 const gdb_byte *info_ptr,
0018ea6f
DE
8331 struct die_info *comp_unit_die,
8332 int has_children,
8333 void *data)
8334{
8335 struct dwarf2_cu *cu = reader->cu;
518817b3 8336 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 8337 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8338 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8339 CORE_ADDR baseaddr;
8340 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8341 struct partial_symtab *pst;
3a2b436a 8342 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8343 const char *filename;
9a3c8263
SM
8344 struct process_psymtab_comp_unit_data *info
8345 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 8346
b93601f3 8347 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
8348 return;
8349
8350 gdb_assert (! per_cu->is_debug_types);
8351
b93601f3 8352 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
8353
8354 cu->list_in_scope = &file_symbols;
8355
8356 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8357 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8358 if (filename == NULL)
0018ea6f 8359 filename = "";
0018ea6f
DE
8360
8361 pst = create_partial_symtab (per_cu, filename);
8362
8363 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8364 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
8365
8366 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8367
8368 dwarf2_find_base_address (comp_unit_die, cu);
8369
8370 /* Possibly set the default values of LOWPC and HIGHPC from
8371 `DW_AT_ranges'. */
3a2b436a
JK
8372 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8373 &best_highpc, cu, pst);
8374 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
8375 /* Store the contiguous range if it is not empty; it can be empty for
8376 CUs with no code. */
8377 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
8378 gdbarch_adjust_dwarf2_addr (gdbarch,
8379 best_lowpc + baseaddr),
8380 gdbarch_adjust_dwarf2_addr (gdbarch,
8381 best_highpc + baseaddr) - 1,
8382 pst);
0018ea6f
DE
8383
8384 /* Check if comp unit has_children.
8385 If so, read the rest of the partial symbols from this comp unit.
8386 If not, there's no more debug_info for this comp unit. */
8387 if (has_children)
8388 {
8389 struct partial_die_info *first_die;
8390 CORE_ADDR lowpc, highpc;
8391
8392 lowpc = ((CORE_ADDR) -1);
8393 highpc = ((CORE_ADDR) 0);
8394
8395 first_die = load_partial_dies (reader, info_ptr, 1);
8396
8397 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8398 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8399
8400 /* If we didn't find a lowpc, set it to highpc to avoid
8401 complaints from `maint check'. */
8402 if (lowpc == ((CORE_ADDR) -1))
8403 lowpc = highpc;
8404
8405 /* If the compilation unit didn't have an explicit address range,
8406 then use the information extracted from its child dies. */
e385593e 8407 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8408 {
8409 best_lowpc = lowpc;
8410 best_highpc = highpc;
8411 }
8412 }
3e29f34a
MR
8413 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8414 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 8415
8763cede 8416 end_psymtab_common (objfile, pst);
0018ea6f
DE
8417
8418 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8419 {
8420 int i;
8421 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8422 struct dwarf2_per_cu_data *iter;
8423
8424 /* Fill in 'dependencies' here; we fill in 'users' in a
8425 post-pass. */
8426 pst->number_of_dependencies = len;
8d749320
SM
8427 pst->dependencies =
8428 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
8429 for (i = 0;
8430 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8431 i, iter);
8432 ++i)
8433 pst->dependencies[i] = iter->v.psymtab;
8434
8435 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8436 }
8437
8438 /* Get the list of files included in the current compilation unit,
8439 and build a psymtab for each of them. */
8440 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8441
b4f54984 8442 if (dwarf_read_debug)
0018ea6f
DE
8443 {
8444 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8445
8446 fprintf_unfiltered (gdb_stdlog,
8447 "Psymtab for %s unit @0x%x: %s - %s"
8448 ", %d global, %d static syms\n",
8449 per_cu->is_debug_types ? "type" : "comp",
9c541725 8450 to_underlying (per_cu->sect_off),
0018ea6f
DE
8451 paddress (gdbarch, pst->textlow),
8452 paddress (gdbarch, pst->texthigh),
8453 pst->n_global_syms, pst->n_static_syms);
8454 }
8455}
8456
8457/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8458 Process compilation unit THIS_CU for a psymtab. */
8459
8460static void
8461process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8462 int want_partial_unit,
8463 enum language pretend_language)
0018ea6f
DE
8464{
8465 /* If this compilation unit was already read in, free the
8466 cached copy in order to read it in again. This is
8467 necessary because we skipped some symbols when we first
8468 read in the compilation unit (see load_partial_dies).
8469 This problem could be avoided, but the benefit is unclear. */
8470 if (this_cu->cu != NULL)
8471 free_one_cached_comp_unit (this_cu);
8472
f1902523
JK
8473 if (this_cu->is_debug_types)
8474 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8475 NULL);
8476 else
8477 {
8478 process_psymtab_comp_unit_data info;
8479 info.want_partial_unit = want_partial_unit;
8480 info.pretend_language = pretend_language;
8481 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8482 process_psymtab_comp_unit_reader, &info);
8483 }
0018ea6f
DE
8484
8485 /* Age out any secondary CUs. */
ed2dc618 8486 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8487}
f4dc4d17
DE
8488
8489/* Reader function for build_type_psymtabs. */
8490
8491static void
8492build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8493 const gdb_byte *info_ptr,
f4dc4d17
DE
8494 struct die_info *type_unit_die,
8495 int has_children,
8496 void *data)
8497{
ed2dc618 8498 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8499 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8500 struct objfile *objfile = dwarf2_per_objfile->objfile;
8501 struct dwarf2_cu *cu = reader->cu;
8502 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8503 struct signatured_type *sig_type;
f4dc4d17
DE
8504 struct type_unit_group *tu_group;
8505 struct attribute *attr;
8506 struct partial_die_info *first_die;
8507 CORE_ADDR lowpc, highpc;
8508 struct partial_symtab *pst;
8509
8510 gdb_assert (data == NULL);
0186c6a7
DE
8511 gdb_assert (per_cu->is_debug_types);
8512 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8513
8514 if (! has_children)
8515 return;
8516
8517 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8518 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8519
0186c6a7 8520 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
8521
8522 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8523 cu->list_in_scope = &file_symbols;
8524 pst = create_partial_symtab (per_cu, "");
8525 pst->anonymous = 1;
8526
8527 first_die = load_partial_dies (reader, info_ptr, 1);
8528
8529 lowpc = (CORE_ADDR) -1;
8530 highpc = (CORE_ADDR) 0;
8531 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8532
8763cede 8533 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8534}
8535
73051182
DE
8536/* Struct used to sort TUs by their abbreviation table offset. */
8537
8538struct tu_abbrev_offset
8539{
8540 struct signatured_type *sig_type;
8541 sect_offset abbrev_offset;
8542};
8543
8544/* Helper routine for build_type_psymtabs_1, passed to qsort. */
8545
8546static int
8547sort_tu_by_abbrev_offset (const void *ap, const void *bp)
8548{
9a3c8263
SM
8549 const struct tu_abbrev_offset * const *a
8550 = (const struct tu_abbrev_offset * const*) ap;
8551 const struct tu_abbrev_offset * const *b
8552 = (const struct tu_abbrev_offset * const*) bp;
9c541725
PA
8553 sect_offset aoff = (*a)->abbrev_offset;
8554 sect_offset boff = (*b)->abbrev_offset;
73051182
DE
8555
8556 return (aoff > boff) - (aoff < boff);
8557}
8558
8559/* Efficiently read all the type units.
8560 This does the bulk of the work for build_type_psymtabs.
8561
8562 The efficiency is because we sort TUs by the abbrev table they use and
8563 only read each abbrev table once. In one program there are 200K TUs
8564 sharing 8K abbrev tables.
8565
8566 The main purpose of this function is to support building the
8567 dwarf2_per_objfile->type_unit_groups table.
8568 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8569 can collapse the search space by grouping them by stmt_list.
8570 The savings can be significant, in the same program from above the 200K TUs
8571 share 8K stmt_list tables.
8572
8573 FUNC is expected to call get_type_unit_group, which will create the
8574 struct type_unit_group if necessary and add it to
8575 dwarf2_per_objfile->type_unit_groups. */
8576
8577static void
ed2dc618 8578build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8579{
73051182
DE
8580 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8581 struct cleanup *cleanups;
8582 struct abbrev_table *abbrev_table;
8583 sect_offset abbrev_offset;
8584 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
8585 int i;
8586
8587 /* It's up to the caller to not call us multiple times. */
8588 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8589
8590 if (dwarf2_per_objfile->n_type_units == 0)
8591 return;
8592
8593 /* TUs typically share abbrev tables, and there can be way more TUs than
8594 abbrev tables. Sort by abbrev table to reduce the number of times we
8595 read each abbrev table in.
8596 Alternatives are to punt or to maintain a cache of abbrev tables.
8597 This is simpler and efficient enough for now.
8598
8599 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8600 symtab to use). Typically TUs with the same abbrev offset have the same
8601 stmt_list value too so in practice this should work well.
8602
8603 The basic algorithm here is:
8604
8605 sort TUs by abbrev table
8606 for each TU with same abbrev table:
8607 read abbrev table if first user
8608 read TU top level DIE
8609 [IWBN if DWO skeletons had DW_AT_stmt_list]
8610 call FUNC */
8611
b4f54984 8612 if (dwarf_read_debug)
73051182
DE
8613 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8614
8615 /* Sort in a separate table to maintain the order of all_type_units
8616 for .gdb_index: TU indices directly index all_type_units. */
8617 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
8618 dwarf2_per_objfile->n_type_units);
8619 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8620 {
8621 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8622
8623 sorted_by_abbrev[i].sig_type = sig_type;
8624 sorted_by_abbrev[i].abbrev_offset =
ed2dc618
SM
8625 read_abbrev_offset (dwarf2_per_objfile,
8626 sig_type->per_cu.section,
9c541725 8627 sig_type->per_cu.sect_off);
73051182
DE
8628 }
8629 cleanups = make_cleanup (xfree, sorted_by_abbrev);
8630 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
8631 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
8632
9c541725 8633 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
8634 abbrev_table = NULL;
8635 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
8636
8637 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8638 {
8639 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8640
8641 /* Switch to the next abbrev table if necessary. */
8642 if (abbrev_table == NULL
9c541725 8643 || tu->abbrev_offset != abbrev_offset)
73051182
DE
8644 {
8645 if (abbrev_table != NULL)
8646 {
8647 abbrev_table_free (abbrev_table);
8648 /* Reset to NULL in case abbrev_table_read_table throws
8649 an error: abbrev_table_free_cleanup will get called. */
8650 abbrev_table = NULL;
8651 }
8652 abbrev_offset = tu->abbrev_offset;
8653 abbrev_table =
ed2dc618
SM
8654 abbrev_table_read_table (dwarf2_per_objfile,
8655 &dwarf2_per_objfile->abbrev,
73051182
DE
8656 abbrev_offset);
8657 ++tu_stats->nr_uniq_abbrev_tables;
8658 }
8659
8660 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
8661 build_type_psymtabs_reader, NULL);
8662 }
8663
73051182 8664 do_cleanups (cleanups);
6aa5f3a6 8665}
73051182 8666
6aa5f3a6
DE
8667/* Print collected type unit statistics. */
8668
8669static void
ed2dc618 8670print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8671{
8672 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8673
8674 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8675 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8676 dwarf2_per_objfile->n_type_units);
8677 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8678 tu_stats->nr_uniq_abbrev_tables);
8679 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8680 tu_stats->nr_symtabs);
8681 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8682 tu_stats->nr_symtab_sharers);
8683 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8684 tu_stats->nr_stmt_less_type_units);
8685 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8686 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8687}
8688
f4dc4d17
DE
8689/* Traversal function for build_type_psymtabs. */
8690
8691static int
8692build_type_psymtab_dependencies (void **slot, void *info)
8693{
ed2dc618
SM
8694 struct dwarf2_per_objfile *dwarf2_per_objfile
8695 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8696 struct objfile *objfile = dwarf2_per_objfile->objfile;
8697 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8698 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8699 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
8700 int len = VEC_length (sig_type_ptr, tu_group->tus);
8701 struct signatured_type *iter;
f4dc4d17
DE
8702 int i;
8703
8704 gdb_assert (len > 0);
0186c6a7 8705 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8706
8707 pst->number_of_dependencies = len;
8d749320
SM
8708 pst->dependencies =
8709 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 8710 for (i = 0;
0186c6a7 8711 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
8712 ++i)
8713 {
0186c6a7
DE
8714 gdb_assert (iter->per_cu.is_debug_types);
8715 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8716 iter->type_unit_group = tu_group;
f4dc4d17
DE
8717 }
8718
0186c6a7 8719 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
8720
8721 return 1;
8722}
8723
8724/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8725 Build partial symbol tables for the .debug_types comp-units. */
8726
8727static void
ed2dc618 8728build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8729{
ed2dc618 8730 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8731 return;
8732
ed2dc618 8733 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8734}
f4dc4d17 8735
6aa5f3a6
DE
8736/* Traversal function for process_skeletonless_type_unit.
8737 Read a TU in a DWO file and build partial symbols for it. */
8738
8739static int
8740process_skeletonless_type_unit (void **slot, void *info)
8741{
8742 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8743 struct dwarf2_per_objfile *dwarf2_per_objfile
8744 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8745 struct signatured_type find_entry, *entry;
8746
8747 /* If this TU doesn't exist in the global table, add it and read it in. */
8748
8749 if (dwarf2_per_objfile->signatured_types == NULL)
8750 {
8751 dwarf2_per_objfile->signatured_types
ed2dc618 8752 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8753 }
8754
8755 find_entry.signature = dwo_unit->signature;
8756 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8757 INSERT);
8758 /* If we've already seen this type there's nothing to do. What's happening
8759 is we're doing our own version of comdat-folding here. */
8760 if (*slot != NULL)
8761 return 1;
8762
8763 /* This does the job that create_all_type_units would have done for
8764 this TU. */
ed2dc618
SM
8765 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8766 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8767 *slot = entry;
8768
8769 /* This does the job that build_type_psymtabs_1 would have done. */
8770 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8771 build_type_psymtabs_reader, NULL);
8772
8773 return 1;
8774}
8775
8776/* Traversal function for process_skeletonless_type_units. */
8777
8778static int
8779process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8780{
8781 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8782
8783 if (dwo_file->tus != NULL)
8784 {
8785 htab_traverse_noresize (dwo_file->tus,
8786 process_skeletonless_type_unit, info);
8787 }
8788
8789 return 1;
8790}
8791
8792/* Scan all TUs of DWO files, verifying we've processed them.
8793 This is needed in case a TU was emitted without its skeleton.
8794 Note: This can't be done until we know what all the DWO files are. */
8795
8796static void
ed2dc618 8797process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8798{
8799 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8800 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8801 && dwarf2_per_objfile->dwo_files != NULL)
8802 {
8803 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8804 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8805 dwarf2_per_objfile);
6aa5f3a6 8806 }
348e048f
DE
8807}
8808
ed2dc618 8809/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8810
8811static void
ed2dc618 8812set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad
TT
8813{
8814 int i;
8815
8816 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8817 {
ed2dc618 8818 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
95554aad
TT
8819 struct partial_symtab *pst = per_cu->v.psymtab;
8820 int j;
8821
36586728
TT
8822 if (pst == NULL)
8823 continue;
8824
95554aad
TT
8825 for (j = 0; j < pst->number_of_dependencies; ++j)
8826 {
8827 /* Set the 'user' field only if it is not already set. */
8828 if (pst->dependencies[j]->user == NULL)
8829 pst->dependencies[j]->user = pst;
8830 }
8831 }
8832}
8833
93311388
DE
8834/* Build the partial symbol table by doing a quick pass through the
8835 .debug_info and .debug_abbrev sections. */
72bf9492 8836
93311388 8837static void
ed2dc618 8838dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8839{
791afaa2 8840 struct cleanup *back_to;
21b2bd31 8841 int i;
ed2dc618 8842 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8843
b4f54984 8844 if (dwarf_read_debug)
45cfd468
DE
8845 {
8846 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8847 objfile_name (objfile));
45cfd468
DE
8848 }
8849
98bfdba5
PA
8850 dwarf2_per_objfile->reading_partial_symbols = 1;
8851
be391dca 8852 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8853
93311388
DE
8854 /* Any cached compilation units will be linked by the per-objfile
8855 read_in_chain. Make sure to free them when we're done. */
ed2dc618 8856 back_to = make_cleanup (free_cached_comp_units, dwarf2_per_objfile);
72bf9492 8857
ed2dc618 8858 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8859
ed2dc618 8860 create_all_comp_units (dwarf2_per_objfile);
c906108c 8861
60606b2c
TT
8862 /* Create a temporary address map on a temporary obstack. We later
8863 copy this to the final obstack. */
8268c778 8864 auto_obstack temp_obstack;
791afaa2
TT
8865
8866 scoped_restore save_psymtabs_addrmap
8867 = make_scoped_restore (&objfile->psymtabs_addrmap,
8868 addrmap_create_mutable (&temp_obstack));
72bf9492 8869
21b2bd31 8870 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 8871 {
ed2dc618 8872 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
aaa75496 8873
b93601f3 8874 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 8875 }
ff013f42 8876
6aa5f3a6 8877 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8878 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8879
8880 /* Now that all TUs have been processed we can fill in the dependencies. */
8881 if (dwarf2_per_objfile->type_unit_groups != NULL)
8882 {
8883 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8884 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8885 }
8886
b4f54984 8887 if (dwarf_read_debug)
ed2dc618 8888 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8889
ed2dc618 8890 set_partial_user (dwarf2_per_objfile);
95554aad 8891
ff013f42
JK
8892 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8893 &objfile->objfile_obstack);
791afaa2
TT
8894 /* At this point we want to keep the address map. */
8895 save_psymtabs_addrmap.release ();
ff013f42 8896
ae038cb0 8897 do_cleanups (back_to);
45cfd468 8898
b4f54984 8899 if (dwarf_read_debug)
45cfd468 8900 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8901 objfile_name (objfile));
ae038cb0
DJ
8902}
8903
3019eac3 8904/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8905
8906static void
dee91e82 8907load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8908 const gdb_byte *info_ptr,
dee91e82
DE
8909 struct die_info *comp_unit_die,
8910 int has_children,
8911 void *data)
ae038cb0 8912{
dee91e82 8913 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8914
95554aad 8915 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8916
ae038cb0
DJ
8917 /* Check if comp unit has_children.
8918 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8919 If not, there's no more debug_info for this comp unit. */
d85a05f0 8920 if (has_children)
dee91e82
DE
8921 load_partial_dies (reader, info_ptr, 0);
8922}
98bfdba5 8923
dee91e82
DE
8924/* Load the partial DIEs for a secondary CU into memory.
8925 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8926
dee91e82
DE
8927static void
8928load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8929{
f4dc4d17
DE
8930 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8931 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8932}
8933
ae038cb0 8934static void
ed2dc618 8935read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8936 struct dwarf2_section_info *section,
f1902523 8937 struct dwarf2_section_info *abbrev_section,
36586728
TT
8938 unsigned int is_dwz,
8939 int *n_allocated,
8940 int *n_comp_units,
8941 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 8942{
d521ce57 8943 const gdb_byte *info_ptr;
ed2dc618 8944 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8945
b4f54984 8946 if (dwarf_read_debug)
bf6af496 8947 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8948 get_section_name (section),
8949 get_section_file_name (section));
bf6af496 8950
36586728 8951 dwarf2_read_section (objfile, section);
ae038cb0 8952
36586728 8953 info_ptr = section->buffer;
6e70227d 8954
36586728 8955 while (info_ptr < section->buffer + section->size)
ae038cb0 8956 {
ae038cb0 8957 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8958
9c541725 8959 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8960
f1902523 8961 comp_unit_head cu_header;
ed2dc618
SM
8962 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8963 abbrev_section, info_ptr,
8964 rcuh_kind::COMPILE);
ae038cb0
DJ
8965
8966 /* Save the compilation unit for later lookup. */
f1902523
JK
8967 if (cu_header.unit_type != DW_UT_type)
8968 {
8969 this_cu = XOBNEW (&objfile->objfile_obstack,
8970 struct dwarf2_per_cu_data);
8971 memset (this_cu, 0, sizeof (*this_cu));
8972 }
8973 else
8974 {
8975 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8976 struct signatured_type);
8977 memset (sig_type, 0, sizeof (*sig_type));
8978 sig_type->signature = cu_header.signature;
8979 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8980 this_cu = &sig_type->per_cu;
8981 }
8982 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8983 this_cu->sect_off = sect_off;
f1902523 8984 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8985 this_cu->is_dwz = is_dwz;
e3b94546 8986 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8987 this_cu->section = section;
ae038cb0 8988
36586728 8989 if (*n_comp_units == *n_allocated)
ae038cb0 8990 {
36586728 8991 *n_allocated *= 2;
224c3ddb
SM
8992 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
8993 *all_comp_units, *n_allocated);
ae038cb0 8994 }
36586728
TT
8995 (*all_comp_units)[*n_comp_units] = this_cu;
8996 ++*n_comp_units;
ae038cb0
DJ
8997
8998 info_ptr = info_ptr + this_cu->length;
8999 }
36586728
TT
9000}
9001
9002/* Create a list of all compilation units in OBJFILE.
9003 This is only done for -readnow and building partial symtabs. */
9004
9005static void
ed2dc618 9006create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728
TT
9007{
9008 int n_allocated;
9009 int n_comp_units;
9010 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 9011 struct dwz_file *dwz;
ed2dc618 9012 struct objfile *objfile = dwarf2_per_objfile->objfile;
36586728
TT
9013
9014 n_comp_units = 0;
9015 n_allocated = 10;
8d749320 9016 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728 9017
ed2dc618 9018 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
f1902523 9019 &dwarf2_per_objfile->abbrev, 0,
36586728
TT
9020 &n_allocated, &n_comp_units, &all_comp_units);
9021
ed2dc618 9022 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 9023 if (dwz != NULL)
ed2dc618
SM
9024 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
9025 1, &n_allocated, &n_comp_units,
4db1a1dc 9026 &all_comp_units);
ae038cb0 9027
8d749320
SM
9028 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
9029 struct dwarf2_per_cu_data *,
9030 n_comp_units);
ae038cb0
DJ
9031 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
9032 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
9033 xfree (all_comp_units);
9034 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
9035}
9036
5734ee8b 9037/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 9038 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 9039 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
9040 DW_AT_ranges). See the comments of add_partial_subprogram on how
9041 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 9042
72bf9492
DJ
9043static void
9044scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
9045 CORE_ADDR *highpc, int set_addrmap,
9046 struct dwarf2_cu *cu)
c906108c 9047{
72bf9492 9048 struct partial_die_info *pdi;
c906108c 9049
91c24f0a
DC
9050 /* Now, march along the PDI's, descending into ones which have
9051 interesting children but skipping the children of the other ones,
9052 until we reach the end of the compilation unit. */
c906108c 9053
72bf9492 9054 pdi = first_die;
91c24f0a 9055
72bf9492
DJ
9056 while (pdi != NULL)
9057 {
9058 fixup_partial_die (pdi, cu);
c906108c 9059
f55ee35c 9060 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
9061 children, so we need to look at them. Ditto for anonymous
9062 enums. */
933c6fe4 9063
72bf9492 9064 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 9065 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
9066 || pdi->tag == DW_TAG_imported_unit
9067 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 9068 {
72bf9492 9069 switch (pdi->tag)
c906108c
SS
9070 {
9071 case DW_TAG_subprogram:
b1dc1806 9072 case DW_TAG_inlined_subroutine:
cdc07690 9073 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 9074 break;
72929c62 9075 case DW_TAG_constant:
c906108c
SS
9076 case DW_TAG_variable:
9077 case DW_TAG_typedef:
91c24f0a 9078 case DW_TAG_union_type:
72bf9492 9079 if (!pdi->is_declaration)
63d06c5c 9080 {
72bf9492 9081 add_partial_symbol (pdi, cu);
63d06c5c
DC
9082 }
9083 break;
c906108c 9084 case DW_TAG_class_type:
680b30c7 9085 case DW_TAG_interface_type:
c906108c 9086 case DW_TAG_structure_type:
72bf9492 9087 if (!pdi->is_declaration)
c906108c 9088 {
72bf9492 9089 add_partial_symbol (pdi, cu);
c906108c 9090 }
e98c9e7c
TT
9091 if (cu->language == language_rust && pdi->has_children)
9092 scan_partial_symbols (pdi->die_child, lowpc, highpc,
9093 set_addrmap, cu);
c906108c 9094 break;
91c24f0a 9095 case DW_TAG_enumeration_type:
72bf9492
DJ
9096 if (!pdi->is_declaration)
9097 add_partial_enumeration (pdi, cu);
c906108c
SS
9098 break;
9099 case DW_TAG_base_type:
a02abb62 9100 case DW_TAG_subrange_type:
c906108c 9101 /* File scope base type definitions are added to the partial
c5aa993b 9102 symbol table. */
72bf9492 9103 add_partial_symbol (pdi, cu);
c906108c 9104 break;
d9fa45fe 9105 case DW_TAG_namespace:
cdc07690 9106 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 9107 break;
5d7cb8df 9108 case DW_TAG_module:
cdc07690 9109 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 9110 break;
95554aad
TT
9111 case DW_TAG_imported_unit:
9112 {
9113 struct dwarf2_per_cu_data *per_cu;
9114
f4dc4d17
DE
9115 /* For now we don't handle imported units in type units. */
9116 if (cu->per_cu->is_debug_types)
9117 {
9118 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9119 " supported in type units [in module %s]"),
518817b3 9120 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9121 }
9122
e3b94546
SM
9123 per_cu = dwarf2_find_containing_comp_unit
9124 (pdi->d.sect_off, pdi->is_dwz,
518817b3 9125 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
9126
9127 /* Go read the partial unit, if needed. */
9128 if (per_cu->v.psymtab == NULL)
b93601f3 9129 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 9130
f4dc4d17 9131 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 9132 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
9133 }
9134 break;
74921315
KS
9135 case DW_TAG_imported_declaration:
9136 add_partial_symbol (pdi, cu);
9137 break;
c906108c
SS
9138 default:
9139 break;
9140 }
9141 }
9142
72bf9492
DJ
9143 /* If the die has a sibling, skip to the sibling. */
9144
9145 pdi = pdi->die_sibling;
9146 }
9147}
9148
9149/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 9150
72bf9492 9151 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 9152 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
9153 Enumerators are an exception; they use the scope of their parent
9154 enumeration type, i.e. the name of the enumeration type is not
9155 prepended to the enumerator.
91c24f0a 9156
72bf9492
DJ
9157 There are two complexities. One is DW_AT_specification; in this
9158 case "parent" means the parent of the target of the specification,
9159 instead of the direct parent of the DIE. The other is compilers
9160 which do not emit DW_TAG_namespace; in this case we try to guess
9161 the fully qualified name of structure types from their members'
9162 linkage names. This must be done using the DIE's children rather
9163 than the children of any DW_AT_specification target. We only need
9164 to do this for structures at the top level, i.e. if the target of
9165 any DW_AT_specification (if any; otherwise the DIE itself) does not
9166 have a parent. */
9167
9168/* Compute the scope prefix associated with PDI's parent, in
9169 compilation unit CU. The result will be allocated on CU's
9170 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9171 field. NULL is returned if no prefix is necessary. */
15d034d0 9172static const char *
72bf9492
DJ
9173partial_die_parent_scope (struct partial_die_info *pdi,
9174 struct dwarf2_cu *cu)
9175{
15d034d0 9176 const char *grandparent_scope;
72bf9492 9177 struct partial_die_info *parent, *real_pdi;
91c24f0a 9178
72bf9492
DJ
9179 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9180 then this means the parent of the specification DIE. */
9181
9182 real_pdi = pdi;
72bf9492 9183 while (real_pdi->has_specification)
36586728
TT
9184 real_pdi = find_partial_die (real_pdi->spec_offset,
9185 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
9186
9187 parent = real_pdi->die_parent;
9188 if (parent == NULL)
9189 return NULL;
9190
9191 if (parent->scope_set)
9192 return parent->scope;
9193
9194 fixup_partial_die (parent, cu);
9195
10b3939b 9196 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 9197
acebe513
UW
9198 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9199 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9200 Work around this problem here. */
9201 if (cu->language == language_cplus
6e70227d 9202 && parent->tag == DW_TAG_namespace
acebe513
UW
9203 && strcmp (parent->name, "::") == 0
9204 && grandparent_scope == NULL)
9205 {
9206 parent->scope = NULL;
9207 parent->scope_set = 1;
9208 return NULL;
9209 }
9210
9c6c53f7
SA
9211 if (pdi->tag == DW_TAG_enumerator)
9212 /* Enumerators should not get the name of the enumeration as a prefix. */
9213 parent->scope = grandparent_scope;
9214 else if (parent->tag == DW_TAG_namespace
f55ee35c 9215 || parent->tag == DW_TAG_module
72bf9492
DJ
9216 || parent->tag == DW_TAG_structure_type
9217 || parent->tag == DW_TAG_class_type
680b30c7 9218 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
9219 || parent->tag == DW_TAG_union_type
9220 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
9221 {
9222 if (grandparent_scope == NULL)
9223 parent->scope = parent->name;
9224 else
3e43a32a
MS
9225 parent->scope = typename_concat (&cu->comp_unit_obstack,
9226 grandparent_scope,
f55ee35c 9227 parent->name, 0, cu);
72bf9492 9228 }
72bf9492
DJ
9229 else
9230 {
9231 /* FIXME drow/2004-04-01: What should we be doing with
9232 function-local names? For partial symbols, we should probably be
9233 ignoring them. */
9234 complaint (&symfile_complaints,
e2e0b3e5 9235 _("unhandled containing DIE tag %d for DIE at %d"),
9c541725 9236 parent->tag, to_underlying (pdi->sect_off));
72bf9492 9237 parent->scope = grandparent_scope;
c906108c
SS
9238 }
9239
72bf9492
DJ
9240 parent->scope_set = 1;
9241 return parent->scope;
9242}
9243
9244/* Return the fully scoped name associated with PDI, from compilation unit
9245 CU. The result will be allocated with malloc. */
4568ecf9 9246
72bf9492
DJ
9247static char *
9248partial_die_full_name (struct partial_die_info *pdi,
9249 struct dwarf2_cu *cu)
9250{
15d034d0 9251 const char *parent_scope;
72bf9492 9252
98bfdba5
PA
9253 /* If this is a template instantiation, we can not work out the
9254 template arguments from partial DIEs. So, unfortunately, we have
9255 to go through the full DIEs. At least any work we do building
9256 types here will be reused if full symbols are loaded later. */
9257 if (pdi->has_template_arguments)
9258 {
9259 fixup_partial_die (pdi, cu);
9260
9261 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9262 {
9263 struct die_info *die;
9264 struct attribute attr;
9265 struct dwarf2_cu *ref_cu = cu;
9266
b64f50a1 9267 /* DW_FORM_ref_addr is using section offset. */
b4069958 9268 attr.name = (enum dwarf_attribute) 0;
98bfdba5 9269 attr.form = DW_FORM_ref_addr;
9c541725 9270 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
9271 die = follow_die_ref (NULL, &attr, &ref_cu);
9272
9273 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9274 }
9275 }
9276
72bf9492
DJ
9277 parent_scope = partial_die_parent_scope (pdi, cu);
9278 if (parent_scope == NULL)
9279 return NULL;
9280 else
f55ee35c 9281 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
9282}
9283
9284static void
72bf9492 9285add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 9286{
518817b3
SM
9287 struct dwarf2_per_objfile *dwarf2_per_objfile
9288 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 9289 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9290 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 9291 CORE_ADDR addr = 0;
15d034d0 9292 const char *actual_name = NULL;
e142c38c 9293 CORE_ADDR baseaddr;
15d034d0 9294 char *built_actual_name;
e142c38c
DJ
9295
9296 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9297
15d034d0
TT
9298 built_actual_name = partial_die_full_name (pdi, cu);
9299 if (built_actual_name != NULL)
9300 actual_name = built_actual_name;
63d06c5c 9301
72bf9492
DJ
9302 if (actual_name == NULL)
9303 actual_name = pdi->name;
9304
c906108c
SS
9305 switch (pdi->tag)
9306 {
b1dc1806 9307 case DW_TAG_inlined_subroutine:
c906108c 9308 case DW_TAG_subprogram:
3e29f34a 9309 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 9310 if (pdi->is_external || cu->language == language_ada)
c906108c 9311 {
2cfa0c8d
JB
9312 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9313 of the global scope. But in Ada, we want to be able to access
9314 nested procedures globally. So all Ada subprograms are stored
9315 in the global scope. */
f47fb265 9316 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9317 built_actual_name != NULL,
f47fb265
MS
9318 VAR_DOMAIN, LOC_BLOCK,
9319 &objfile->global_psymbols,
1762568f 9320 addr, cu->language, objfile);
c906108c
SS
9321 }
9322 else
9323 {
f47fb265 9324 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9325 built_actual_name != NULL,
f47fb265
MS
9326 VAR_DOMAIN, LOC_BLOCK,
9327 &objfile->static_psymbols,
1762568f 9328 addr, cu->language, objfile);
c906108c 9329 }
0c1b455e
TT
9330
9331 if (pdi->main_subprogram && actual_name != NULL)
9332 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 9333 break;
72929c62
JB
9334 case DW_TAG_constant:
9335 {
af5bf4ad 9336 std::vector<partial_symbol *> *list;
72929c62
JB
9337
9338 if (pdi->is_external)
9339 list = &objfile->global_psymbols;
9340 else
9341 list = &objfile->static_psymbols;
f47fb265 9342 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9343 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 9344 list, 0, cu->language, objfile);
72929c62
JB
9345 }
9346 break;
c906108c 9347 case DW_TAG_variable:
95554aad
TT
9348 if (pdi->d.locdesc)
9349 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 9350
95554aad 9351 if (pdi->d.locdesc
caac4577
JG
9352 && addr == 0
9353 && !dwarf2_per_objfile->has_section_at_zero)
9354 {
9355 /* A global or static variable may also have been stripped
9356 out by the linker if unused, in which case its address
9357 will be nullified; do not add such variables into partial
9358 symbol table then. */
9359 }
9360 else if (pdi->is_external)
c906108c
SS
9361 {
9362 /* Global Variable.
9363 Don't enter into the minimal symbol tables as there is
9364 a minimal symbol table entry from the ELF symbols already.
9365 Enter into partial symbol table if it has a location
9366 descriptor or a type.
9367 If the location descriptor is missing, new_symbol will create
9368 a LOC_UNRESOLVED symbol, the address of the variable will then
9369 be determined from the minimal symbol table whenever the variable
9370 is referenced.
9371 The address for the partial symbol table entry is not
9372 used by GDB, but it comes in handy for debugging partial symbol
9373 table building. */
9374
95554aad 9375 if (pdi->d.locdesc || pdi->has_type)
f47fb265 9376 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9377 built_actual_name != NULL,
f47fb265
MS
9378 VAR_DOMAIN, LOC_STATIC,
9379 &objfile->global_psymbols,
1762568f 9380 addr + baseaddr,
f47fb265 9381 cu->language, objfile);
c906108c
SS
9382 }
9383 else
9384 {
ff908ebf
AW
9385 int has_loc = pdi->d.locdesc != NULL;
9386
9387 /* Static Variable. Skip symbols whose value we cannot know (those
9388 without location descriptors or constant values). */
9389 if (!has_loc && !pdi->has_const_value)
decbce07 9390 {
15d034d0 9391 xfree (built_actual_name);
decbce07
MS
9392 return;
9393 }
ff908ebf 9394
f47fb265 9395 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9396 built_actual_name != NULL,
f47fb265
MS
9397 VAR_DOMAIN, LOC_STATIC,
9398 &objfile->static_psymbols,
ff908ebf 9399 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 9400 cu->language, objfile);
c906108c
SS
9401 }
9402 break;
9403 case DW_TAG_typedef:
9404 case DW_TAG_base_type:
a02abb62 9405 case DW_TAG_subrange_type:
38d518c9 9406 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9407 built_actual_name != NULL,
176620f1 9408 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 9409 &objfile->static_psymbols,
1762568f 9410 0, cu->language, objfile);
c906108c 9411 break;
74921315 9412 case DW_TAG_imported_declaration:
72bf9492
DJ
9413 case DW_TAG_namespace:
9414 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9415 built_actual_name != NULL,
72bf9492
DJ
9416 VAR_DOMAIN, LOC_TYPEDEF,
9417 &objfile->global_psymbols,
1762568f 9418 0, cu->language, objfile);
72bf9492 9419 break;
530e8392
KB
9420 case DW_TAG_module:
9421 add_psymbol_to_list (actual_name, strlen (actual_name),
9422 built_actual_name != NULL,
9423 MODULE_DOMAIN, LOC_TYPEDEF,
9424 &objfile->global_psymbols,
1762568f 9425 0, cu->language, objfile);
530e8392 9426 break;
c906108c 9427 case DW_TAG_class_type:
680b30c7 9428 case DW_TAG_interface_type:
c906108c
SS
9429 case DW_TAG_structure_type:
9430 case DW_TAG_union_type:
9431 case DW_TAG_enumeration_type:
fa4028e9
JB
9432 /* Skip external references. The DWARF standard says in the section
9433 about "Structure, Union, and Class Type Entries": "An incomplete
9434 structure, union or class type is represented by a structure,
9435 union or class entry that does not have a byte size attribute
9436 and that has a DW_AT_declaration attribute." */
9437 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 9438 {
15d034d0 9439 xfree (built_actual_name);
decbce07
MS
9440 return;
9441 }
fa4028e9 9442
63d06c5c
DC
9443 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9444 static vs. global. */
38d518c9 9445 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9446 built_actual_name != NULL,
176620f1 9447 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 9448 cu->language == language_cplus
63d06c5c
DC
9449 ? &objfile->global_psymbols
9450 : &objfile->static_psymbols,
1762568f 9451 0, cu->language, objfile);
c906108c 9452
c906108c
SS
9453 break;
9454 case DW_TAG_enumerator:
38d518c9 9455 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9456 built_actual_name != NULL,
176620f1 9457 VAR_DOMAIN, LOC_CONST,
9c37b5ae 9458 cu->language == language_cplus
f6fe98ef
DJ
9459 ? &objfile->global_psymbols
9460 : &objfile->static_psymbols,
1762568f 9461 0, cu->language, objfile);
c906108c
SS
9462 break;
9463 default:
9464 break;
9465 }
5c4e30ca 9466
15d034d0 9467 xfree (built_actual_name);
c906108c
SS
9468}
9469
5c4e30ca
DC
9470/* Read a partial die corresponding to a namespace; also, add a symbol
9471 corresponding to that namespace to the symbol table. NAMESPACE is
9472 the name of the enclosing namespace. */
91c24f0a 9473
72bf9492
DJ
9474static void
9475add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9476 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9477 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9478{
72bf9492 9479 /* Add a symbol for the namespace. */
e7c27a73 9480
72bf9492 9481 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9482
9483 /* Now scan partial symbols in that namespace. */
9484
91c24f0a 9485 if (pdi->has_children)
cdc07690 9486 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9487}
9488
5d7cb8df
JK
9489/* Read a partial die corresponding to a Fortran module. */
9490
9491static void
9492add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9493 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9494{
530e8392
KB
9495 /* Add a symbol for the namespace. */
9496
9497 add_partial_symbol (pdi, cu);
9498
f55ee35c 9499 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9500
9501 if (pdi->has_children)
cdc07690 9502 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9503}
9504
b1dc1806
XR
9505/* Read a partial die corresponding to a subprogram or an inlined
9506 subprogram and create a partial symbol for that subprogram.
9507 When the CU language allows it, this routine also defines a partial
9508 symbol for each nested subprogram that this subprogram contains.
9509 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9510 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9511
cdc07690
YQ
9512 PDI may also be a lexical block, in which case we simply search
9513 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9514 Again, this is only performed when the CU language allows this
9515 type of definitions. */
9516
9517static void
9518add_partial_subprogram (struct partial_die_info *pdi,
9519 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9520 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9521{
b1dc1806 9522 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9523 {
9524 if (pdi->has_pc_info)
9525 {
9526 if (pdi->lowpc < *lowpc)
9527 *lowpc = pdi->lowpc;
9528 if (pdi->highpc > *highpc)
9529 *highpc = pdi->highpc;
cdc07690 9530 if (set_addrmap)
5734ee8b 9531 {
518817b3 9532 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9533 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9534 CORE_ADDR baseaddr;
9535 CORE_ADDR highpc;
9536 CORE_ADDR lowpc;
5734ee8b
DJ
9537
9538 baseaddr = ANOFFSET (objfile->section_offsets,
9539 SECT_OFF_TEXT (objfile));
3e29f34a
MR
9540 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9541 pdi->lowpc + baseaddr);
9542 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9543 pdi->highpc + baseaddr);
9544 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 9545 cu->per_cu->v.psymtab);
5734ee8b 9546 }
481860b3
GB
9547 }
9548
9549 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9550 {
bc30ff58 9551 if (!pdi->is_declaration)
e8d05480
JB
9552 /* Ignore subprogram DIEs that do not have a name, they are
9553 illegal. Do not emit a complaint at this point, we will
9554 do so when we convert this psymtab into a symtab. */
9555 if (pdi->name)
9556 add_partial_symbol (pdi, cu);
bc30ff58
JB
9557 }
9558 }
6e70227d 9559
bc30ff58
JB
9560 if (! pdi->has_children)
9561 return;
9562
9563 if (cu->language == language_ada)
9564 {
9565 pdi = pdi->die_child;
9566 while (pdi != NULL)
9567 {
9568 fixup_partial_die (pdi, cu);
9569 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9570 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9571 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9572 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9573 pdi = pdi->die_sibling;
9574 }
9575 }
9576}
9577
91c24f0a
DC
9578/* Read a partial die corresponding to an enumeration type. */
9579
72bf9492
DJ
9580static void
9581add_partial_enumeration (struct partial_die_info *enum_pdi,
9582 struct dwarf2_cu *cu)
91c24f0a 9583{
72bf9492 9584 struct partial_die_info *pdi;
91c24f0a
DC
9585
9586 if (enum_pdi->name != NULL)
72bf9492
DJ
9587 add_partial_symbol (enum_pdi, cu);
9588
9589 pdi = enum_pdi->die_child;
9590 while (pdi)
91c24f0a 9591 {
72bf9492 9592 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 9593 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 9594 else
72bf9492
DJ
9595 add_partial_symbol (pdi, cu);
9596 pdi = pdi->die_sibling;
91c24f0a 9597 }
91c24f0a
DC
9598}
9599
6caca83c
CC
9600/* Return the initial uleb128 in the die at INFO_PTR. */
9601
9602static unsigned int
d521ce57 9603peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9604{
9605 unsigned int bytes_read;
9606
9607 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9608}
9609
4bb7a0a7
DJ
9610/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
9611 Return the corresponding abbrev, or NULL if the number is zero (indicating
9612 an empty DIE). In either case *BYTES_READ will be set to the length of
9613 the initial number. */
9614
9615static struct abbrev_info *
d521ce57 9616peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 9617 struct dwarf2_cu *cu)
4bb7a0a7 9618{
518817b3 9619 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
4bb7a0a7
DJ
9620 unsigned int abbrev_number;
9621 struct abbrev_info *abbrev;
9622
9623 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9624
9625 if (abbrev_number == 0)
9626 return NULL;
9627
433df2d4 9628 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
9629 if (!abbrev)
9630 {
422b9917
DE
9631 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9632 " at offset 0x%x [in module %s]"),
9633 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9c541725 9634 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9635 }
9636
9637 return abbrev;
9638}
9639
93311388
DE
9640/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9641 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9642 DIE. Any children of the skipped DIEs will also be skipped. */
9643
d521ce57
TT
9644static const gdb_byte *
9645skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9646{
dee91e82 9647 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
9648 struct abbrev_info *abbrev;
9649 unsigned int bytes_read;
9650
9651 while (1)
9652 {
9653 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9654 if (abbrev == NULL)
9655 return info_ptr + bytes_read;
9656 else
dee91e82 9657 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9658 }
9659}
9660
93311388
DE
9661/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9662 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9663 abbrev corresponding to that skipped uleb128 should be passed in
9664 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9665 children. */
9666
d521ce57
TT
9667static const gdb_byte *
9668skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9669 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9670{
9671 unsigned int bytes_read;
9672 struct attribute attr;
dee91e82
DE
9673 bfd *abfd = reader->abfd;
9674 struct dwarf2_cu *cu = reader->cu;
d521ce57 9675 const gdb_byte *buffer = reader->buffer;
f664829e 9676 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9677 unsigned int form, i;
9678
9679 for (i = 0; i < abbrev->num_attrs; i++)
9680 {
9681 /* The only abbrev we care about is DW_AT_sibling. */
9682 if (abbrev->attrs[i].name == DW_AT_sibling)
9683 {
dee91e82 9684 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9685 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9686 complaint (&symfile_complaints,
9687 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9688 else
b9502d3f 9689 {
9c541725
PA
9690 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9691 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9692
9693 if (sibling_ptr < info_ptr)
9694 complaint (&symfile_complaints,
9695 _("DW_AT_sibling points backwards"));
22869d73
KS
9696 else if (sibling_ptr > reader->buffer_end)
9697 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9698 else
9699 return sibling_ptr;
9700 }
4bb7a0a7
DJ
9701 }
9702
9703 /* If it isn't DW_AT_sibling, skip this attribute. */
9704 form = abbrev->attrs[i].form;
9705 skip_attribute:
9706 switch (form)
9707 {
4bb7a0a7 9708 case DW_FORM_ref_addr:
ae411497
TT
9709 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9710 and later it is offset sized. */
9711 if (cu->header.version == 2)
9712 info_ptr += cu->header.addr_size;
9713 else
9714 info_ptr += cu->header.offset_size;
9715 break;
36586728
TT
9716 case DW_FORM_GNU_ref_alt:
9717 info_ptr += cu->header.offset_size;
9718 break;
ae411497 9719 case DW_FORM_addr:
4bb7a0a7
DJ
9720 info_ptr += cu->header.addr_size;
9721 break;
9722 case DW_FORM_data1:
9723 case DW_FORM_ref1:
9724 case DW_FORM_flag:
9725 info_ptr += 1;
9726 break;
2dc7f7b3 9727 case DW_FORM_flag_present:
43988095 9728 case DW_FORM_implicit_const:
2dc7f7b3 9729 break;
4bb7a0a7
DJ
9730 case DW_FORM_data2:
9731 case DW_FORM_ref2:
9732 info_ptr += 2;
9733 break;
9734 case DW_FORM_data4:
9735 case DW_FORM_ref4:
9736 info_ptr += 4;
9737 break;
9738 case DW_FORM_data8:
9739 case DW_FORM_ref8:
55f1336d 9740 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9741 info_ptr += 8;
9742 break;
0224619f
JK
9743 case DW_FORM_data16:
9744 info_ptr += 16;
9745 break;
4bb7a0a7 9746 case DW_FORM_string:
9b1c24c8 9747 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9748 info_ptr += bytes_read;
9749 break;
2dc7f7b3 9750 case DW_FORM_sec_offset:
4bb7a0a7 9751 case DW_FORM_strp:
36586728 9752 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9753 info_ptr += cu->header.offset_size;
9754 break;
2dc7f7b3 9755 case DW_FORM_exprloc:
4bb7a0a7
DJ
9756 case DW_FORM_block:
9757 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9758 info_ptr += bytes_read;
9759 break;
9760 case DW_FORM_block1:
9761 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9762 break;
9763 case DW_FORM_block2:
9764 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9765 break;
9766 case DW_FORM_block4:
9767 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9768 break;
9769 case DW_FORM_sdata:
9770 case DW_FORM_udata:
9771 case DW_FORM_ref_udata:
3019eac3
DE
9772 case DW_FORM_GNU_addr_index:
9773 case DW_FORM_GNU_str_index:
d521ce57 9774 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9775 break;
9776 case DW_FORM_indirect:
9777 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9778 info_ptr += bytes_read;
9779 /* We need to continue parsing from here, so just go back to
9780 the top. */
9781 goto skip_attribute;
9782
9783 default:
3e43a32a
MS
9784 error (_("Dwarf Error: Cannot handle %s "
9785 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9786 dwarf_form_name (form),
9787 bfd_get_filename (abfd));
9788 }
9789 }
9790
9791 if (abbrev->has_children)
dee91e82 9792 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9793 else
9794 return info_ptr;
9795}
9796
93311388 9797/* Locate ORIG_PDI's sibling.
dee91e82 9798 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9799
d521ce57 9800static const gdb_byte *
dee91e82
DE
9801locate_pdi_sibling (const struct die_reader_specs *reader,
9802 struct partial_die_info *orig_pdi,
d521ce57 9803 const gdb_byte *info_ptr)
91c24f0a
DC
9804{
9805 /* Do we know the sibling already? */
72bf9492 9806
91c24f0a
DC
9807 if (orig_pdi->sibling)
9808 return orig_pdi->sibling;
9809
9810 /* Are there any children to deal with? */
9811
9812 if (!orig_pdi->has_children)
9813 return info_ptr;
9814
4bb7a0a7 9815 /* Skip the children the long way. */
91c24f0a 9816
dee91e82 9817 return skip_children (reader, info_ptr);
91c24f0a
DC
9818}
9819
257e7a09 9820/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9821 not NULL. */
c906108c
SS
9822
9823static void
257e7a09
YQ
9824dwarf2_read_symtab (struct partial_symtab *self,
9825 struct objfile *objfile)
c906108c 9826{
ed2dc618
SM
9827 struct dwarf2_per_objfile *dwarf2_per_objfile
9828 = get_dwarf2_per_objfile (objfile);
9829
257e7a09 9830 if (self->readin)
c906108c 9831 {
442e4d9c 9832 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9833 self->filename);
442e4d9c
YQ
9834 }
9835 else
9836 {
9837 if (info_verbose)
c906108c 9838 {
442e4d9c 9839 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9840 self->filename);
442e4d9c 9841 gdb_flush (gdb_stdout);
c906108c 9842 }
c906108c 9843
442e4d9c
YQ
9844 /* If this psymtab is constructed from a debug-only objfile, the
9845 has_section_at_zero flag will not necessarily be correct. We
9846 can get the correct value for this flag by looking at the data
9847 associated with the (presumably stripped) associated objfile. */
9848 if (objfile->separate_debug_objfile_backlink)
9849 {
9850 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9851 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9852
442e4d9c
YQ
9853 dwarf2_per_objfile->has_section_at_zero
9854 = dpo_backlink->has_section_at_zero;
9855 }
b2ab525c 9856
442e4d9c 9857 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9858
257e7a09 9859 psymtab_to_symtab_1 (self);
c906108c 9860
442e4d9c
YQ
9861 /* Finish up the debug error message. */
9862 if (info_verbose)
9863 printf_filtered (_("done.\n"));
c906108c 9864 }
95554aad 9865
ed2dc618 9866 process_cu_includes (dwarf2_per_objfile);
c906108c 9867}
9cdd5dbd
DE
9868\f
9869/* Reading in full CUs. */
c906108c 9870
10b3939b
DJ
9871/* Add PER_CU to the queue. */
9872
9873static void
95554aad
TT
9874queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9875 enum language pretend_language)
10b3939b
DJ
9876{
9877 struct dwarf2_queue_item *item;
9878
9879 per_cu->queued = 1;
8d749320 9880 item = XNEW (struct dwarf2_queue_item);
10b3939b 9881 item->per_cu = per_cu;
95554aad 9882 item->pretend_language = pretend_language;
10b3939b
DJ
9883 item->next = NULL;
9884
9885 if (dwarf2_queue == NULL)
9886 dwarf2_queue = item;
9887 else
9888 dwarf2_queue_tail->next = item;
9889
9890 dwarf2_queue_tail = item;
9891}
9892
89e63ee4
DE
9893/* If PER_CU is not yet queued, add it to the queue.
9894 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9895 dependency.
0907af0c 9896 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9897 meaning either PER_CU is already queued or it is already loaded.
9898
9899 N.B. There is an invariant here that if a CU is queued then it is loaded.
9900 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9901
9902static int
89e63ee4 9903maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9904 struct dwarf2_per_cu_data *per_cu,
9905 enum language pretend_language)
9906{
9907 /* We may arrive here during partial symbol reading, if we need full
9908 DIEs to process an unusual case (e.g. template arguments). Do
9909 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9910 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9911 {
9912 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9913 return 1;
9914 return 0;
9915 }
9916
9917 /* Mark the dependence relation so that we don't flush PER_CU
9918 too early. */
89e63ee4
DE
9919 if (dependent_cu != NULL)
9920 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9921
9922 /* If it's already on the queue, we have nothing to do. */
9923 if (per_cu->queued)
9924 return 0;
9925
9926 /* If the compilation unit is already loaded, just mark it as
9927 used. */
9928 if (per_cu->cu != NULL)
9929 {
9930 per_cu->cu->last_used = 0;
9931 return 0;
9932 }
9933
9934 /* Add it to the queue. */
9935 queue_comp_unit (per_cu, pretend_language);
9936
9937 return 1;
9938}
9939
10b3939b
DJ
9940/* Process the queue. */
9941
9942static void
ed2dc618 9943process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9944{
9945 struct dwarf2_queue_item *item, *next_item;
9946
b4f54984 9947 if (dwarf_read_debug)
45cfd468
DE
9948 {
9949 fprintf_unfiltered (gdb_stdlog,
9950 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9951 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9952 }
9953
03dd20cc
DJ
9954 /* The queue starts out with one item, but following a DIE reference
9955 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9956 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9957 {
cc12ce38
DE
9958 if ((dwarf2_per_objfile->using_index
9959 ? !item->per_cu->v.quick->compunit_symtab
9960 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9961 /* Skip dummy CUs. */
9962 && item->per_cu->cu != NULL)
f4dc4d17
DE
9963 {
9964 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9965 unsigned int debug_print_threshold;
247f5c4f 9966 char buf[100];
f4dc4d17 9967
247f5c4f 9968 if (per_cu->is_debug_types)
f4dc4d17 9969 {
247f5c4f
DE
9970 struct signatured_type *sig_type =
9971 (struct signatured_type *) per_cu;
9972
9973 sprintf (buf, "TU %s at offset 0x%x",
73be47f5 9974 hex_string (sig_type->signature),
9c541725 9975 to_underlying (per_cu->sect_off));
73be47f5
DE
9976 /* There can be 100s of TUs.
9977 Only print them in verbose mode. */
9978 debug_print_threshold = 2;
f4dc4d17 9979 }
247f5c4f 9980 else
73be47f5 9981 {
9c541725
PA
9982 sprintf (buf, "CU at offset 0x%x",
9983 to_underlying (per_cu->sect_off));
73be47f5
DE
9984 debug_print_threshold = 1;
9985 }
247f5c4f 9986
b4f54984 9987 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9988 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9989
9990 if (per_cu->is_debug_types)
9991 process_full_type_unit (per_cu, item->pretend_language);
9992 else
9993 process_full_comp_unit (per_cu, item->pretend_language);
9994
b4f54984 9995 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9996 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9997 }
10b3939b
DJ
9998
9999 item->per_cu->queued = 0;
10000 next_item = item->next;
10001 xfree (item);
10002 }
10003
10004 dwarf2_queue_tail = NULL;
45cfd468 10005
b4f54984 10006 if (dwarf_read_debug)
45cfd468
DE
10007 {
10008 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 10009 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 10010 }
10b3939b
DJ
10011}
10012
10013/* Free all allocated queue entries. This function only releases anything if
10014 an error was thrown; if the queue was processed then it would have been
10015 freed as we went along. */
10016
10017static void
10018dwarf2_release_queue (void *dummy)
10019{
10020 struct dwarf2_queue_item *item, *last;
10021
10022 item = dwarf2_queue;
10023 while (item)
10024 {
10025 /* Anything still marked queued is likely to be in an
10026 inconsistent state, so discard it. */
10027 if (item->per_cu->queued)
10028 {
10029 if (item->per_cu->cu != NULL)
dee91e82 10030 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
10031 item->per_cu->queued = 0;
10032 }
10033
10034 last = item;
10035 item = item->next;
10036 xfree (last);
10037 }
10038
10039 dwarf2_queue = dwarf2_queue_tail = NULL;
10040}
10041
10042/* Read in full symbols for PST, and anything it depends on. */
10043
c906108c 10044static void
fba45db2 10045psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 10046{
10b3939b 10047 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
10048 int i;
10049
95554aad
TT
10050 if (pst->readin)
10051 return;
10052
aaa75496 10053 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
10054 if (!pst->dependencies[i]->readin
10055 && pst->dependencies[i]->user == NULL)
aaa75496
JB
10056 {
10057 /* Inform about additional files that need to be read in. */
10058 if (info_verbose)
10059 {
a3f17187 10060 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
10061 fputs_filtered (" ", gdb_stdout);
10062 wrap_here ("");
10063 fputs_filtered ("and ", gdb_stdout);
10064 wrap_here ("");
10065 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 10066 wrap_here (""); /* Flush output. */
aaa75496
JB
10067 gdb_flush (gdb_stdout);
10068 }
10069 psymtab_to_symtab_1 (pst->dependencies[i]);
10070 }
10071
9a3c8263 10072 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
10073
10074 if (per_cu == NULL)
aaa75496
JB
10075 {
10076 /* It's an include file, no symbols to read for it.
10077 Everything is in the parent symtab. */
10078 pst->readin = 1;
10079 return;
10080 }
c906108c 10081
a0f42c21 10082 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
10083}
10084
dee91e82
DE
10085/* Trivial hash function for die_info: the hash value of a DIE
10086 is its offset in .debug_info for this objfile. */
10b3939b 10087
dee91e82
DE
10088static hashval_t
10089die_hash (const void *item)
10b3939b 10090{
9a3c8263 10091 const struct die_info *die = (const struct die_info *) item;
6502dd73 10092
9c541725 10093 return to_underlying (die->sect_off);
dee91e82 10094}
63d06c5c 10095
dee91e82
DE
10096/* Trivial comparison function for die_info structures: two DIEs
10097 are equal if they have the same offset. */
98bfdba5 10098
dee91e82
DE
10099static int
10100die_eq (const void *item_lhs, const void *item_rhs)
10101{
9a3c8263
SM
10102 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
10103 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 10104
9c541725 10105 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 10106}
c906108c 10107
dee91e82
DE
10108/* die_reader_func for load_full_comp_unit.
10109 This is identical to read_signatured_type_reader,
10110 but is kept separate for now. */
c906108c 10111
dee91e82
DE
10112static void
10113load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 10114 const gdb_byte *info_ptr,
dee91e82
DE
10115 struct die_info *comp_unit_die,
10116 int has_children,
10117 void *data)
10118{
10119 struct dwarf2_cu *cu = reader->cu;
9a3c8263 10120 enum language *language_ptr = (enum language *) data;
6caca83c 10121
dee91e82
DE
10122 gdb_assert (cu->die_hash == NULL);
10123 cu->die_hash =
10124 htab_create_alloc_ex (cu->header.length / 12,
10125 die_hash,
10126 die_eq,
10127 NULL,
10128 &cu->comp_unit_obstack,
10129 hashtab_obstack_allocate,
10130 dummy_obstack_deallocate);
e142c38c 10131
dee91e82
DE
10132 if (has_children)
10133 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10134 &info_ptr, comp_unit_die);
10135 cu->dies = comp_unit_die;
10136 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
10137
10138 /* We try not to read any attributes in this function, because not
9cdd5dbd 10139 all CUs needed for references have been loaded yet, and symbol
10b3939b 10140 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
10141 or we won't be able to build types correctly.
10142 Similarly, if we do not read the producer, we can not apply
10143 producer-specific interpretation. */
95554aad 10144 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 10145}
10b3939b 10146
dee91e82 10147/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 10148
dee91e82 10149static void
95554aad
TT
10150load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10151 enum language pretend_language)
dee91e82 10152{
3019eac3 10153 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 10154
f4dc4d17
DE
10155 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10156 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
10157}
10158
3da10d80
KS
10159/* Add a DIE to the delayed physname list. */
10160
10161static void
10162add_to_method_list (struct type *type, int fnfield_index, int index,
10163 const char *name, struct die_info *die,
10164 struct dwarf2_cu *cu)
10165{
10166 struct delayed_method_info mi;
10167 mi.type = type;
10168 mi.fnfield_index = fnfield_index;
10169 mi.index = index;
10170 mi.name = name;
10171 mi.die = die;
10172 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
10173}
10174
10175/* A cleanup for freeing the delayed method list. */
10176
10177static void
10178free_delayed_list (void *ptr)
10179{
10180 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
10181 if (cu->method_list != NULL)
10182 {
10183 VEC_free (delayed_method_info, cu->method_list);
10184 cu->method_list = NULL;
10185 }
10186}
10187
3693fdb3
PA
10188/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10189 "const" / "volatile". If so, decrements LEN by the length of the
10190 modifier and return true. Otherwise return false. */
10191
10192template<size_t N>
10193static bool
10194check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10195{
10196 size_t mod_len = sizeof (mod) - 1;
10197 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10198 {
10199 len -= mod_len;
10200 return true;
10201 }
10202 return false;
10203}
10204
3da10d80
KS
10205/* Compute the physnames of any methods on the CU's method list.
10206
10207 The computation of method physnames is delayed in order to avoid the
10208 (bad) condition that one of the method's formal parameters is of an as yet
10209 incomplete type. */
10210
10211static void
10212compute_delayed_physnames (struct dwarf2_cu *cu)
10213{
10214 int i;
10215 struct delayed_method_info *mi;
3693fdb3
PA
10216
10217 /* Only C++ delays computing physnames. */
10218 if (VEC_empty (delayed_method_info, cu->method_list))
10219 return;
10220 gdb_assert (cu->language == language_cplus);
10221
3da10d80
KS
10222 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
10223 {
1d06ead6 10224 const char *physname;
3da10d80
KS
10225 struct fn_fieldlist *fn_flp
10226 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 10227 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
10228 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
10229 = physname ? physname : "";
3693fdb3
PA
10230
10231 /* Since there's no tag to indicate whether a method is a
10232 const/volatile overload, extract that information out of the
10233 demangled name. */
10234 if (physname != NULL)
10235 {
10236 size_t len = strlen (physname);
10237
10238 while (1)
10239 {
10240 if (physname[len] == ')') /* shortcut */
10241 break;
10242 else if (check_modifier (physname, len, " const"))
10243 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
10244 else if (check_modifier (physname, len, " volatile"))
10245 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
10246 else
10247 break;
10248 }
10249 }
3da10d80
KS
10250 }
10251}
10252
a766d390
DE
10253/* Go objects should be embedded in a DW_TAG_module DIE,
10254 and it's not clear if/how imported objects will appear.
10255 To keep Go support simple until that's worked out,
10256 go back through what we've read and create something usable.
10257 We could do this while processing each DIE, and feels kinda cleaner,
10258 but that way is more invasive.
10259 This is to, for example, allow the user to type "p var" or "b main"
10260 without having to specify the package name, and allow lookups
10261 of module.object to work in contexts that use the expression
10262 parser. */
10263
10264static void
10265fixup_go_packaging (struct dwarf2_cu *cu)
10266{
10267 char *package_name = NULL;
10268 struct pending *list;
10269 int i;
10270
10271 for (list = global_symbols; list != NULL; list = list->next)
10272 {
10273 for (i = 0; i < list->nsyms; ++i)
10274 {
10275 struct symbol *sym = list->symbol[i];
10276
10277 if (SYMBOL_LANGUAGE (sym) == language_go
10278 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10279 {
10280 char *this_package_name = go_symbol_package_name (sym);
10281
10282 if (this_package_name == NULL)
10283 continue;
10284 if (package_name == NULL)
10285 package_name = this_package_name;
10286 else
10287 {
518817b3
SM
10288 struct objfile *objfile
10289 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390
DE
10290 if (strcmp (package_name, this_package_name) != 0)
10291 complaint (&symfile_complaints,
10292 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
10293 (symbol_symtab (sym) != NULL
10294 ? symtab_to_filename_for_display
10295 (symbol_symtab (sym))
e3b94546 10296 : objfile_name (objfile)),
a766d390
DE
10297 this_package_name, package_name);
10298 xfree (this_package_name);
10299 }
10300 }
10301 }
10302 }
10303
10304 if (package_name != NULL)
10305 {
518817b3 10306 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 10307 const char *saved_package_name
224c3ddb
SM
10308 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10309 package_name,
10310 strlen (package_name));
19f392bc
UW
10311 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10312 saved_package_name);
a766d390
DE
10313 struct symbol *sym;
10314
10315 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10316
e623cf5d 10317 sym = allocate_symbol (objfile);
f85f34ed 10318 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
10319 SYMBOL_SET_NAMES (sym, saved_package_name,
10320 strlen (saved_package_name), 0, objfile);
a766d390
DE
10321 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10322 e.g., "main" finds the "main" module and not C's main(). */
10323 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 10324 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
10325 SYMBOL_TYPE (sym) = type;
10326
10327 add_symbol_to_list (sym, &global_symbols);
10328
10329 xfree (package_name);
10330 }
10331}
10332
95554aad
TT
10333/* Return the symtab for PER_CU. This works properly regardless of
10334 whether we're using the index or psymtabs. */
10335
43f3e411
DE
10336static struct compunit_symtab *
10337get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10338{
ed2dc618 10339 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10340 ? per_cu->v.quick->compunit_symtab
10341 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10342}
10343
10344/* A helper function for computing the list of all symbol tables
10345 included by PER_CU. */
10346
10347static void
43f3e411 10348recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 10349 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10350 struct dwarf2_per_cu_data *per_cu,
43f3e411 10351 struct compunit_symtab *immediate_parent)
95554aad
TT
10352{
10353 void **slot;
10354 int ix;
43f3e411 10355 struct compunit_symtab *cust;
95554aad
TT
10356 struct dwarf2_per_cu_data *iter;
10357
10358 slot = htab_find_slot (all_children, per_cu, INSERT);
10359 if (*slot != NULL)
10360 {
10361 /* This inclusion and its children have been processed. */
10362 return;
10363 }
10364
10365 *slot = per_cu;
10366 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10367 cust = get_compunit_symtab (per_cu);
10368 if (cust != NULL)
ec94af83
DE
10369 {
10370 /* If this is a type unit only add its symbol table if we haven't
10371 seen it yet (type unit per_cu's can share symtabs). */
10372 if (per_cu->is_debug_types)
10373 {
43f3e411 10374 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10375 if (*slot == NULL)
10376 {
43f3e411
DE
10377 *slot = cust;
10378 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10379 if (cust->user == NULL)
10380 cust->user = immediate_parent;
ec94af83
DE
10381 }
10382 }
10383 else
f9125b6c 10384 {
43f3e411
DE
10385 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10386 if (cust->user == NULL)
10387 cust->user = immediate_parent;
f9125b6c 10388 }
ec94af83 10389 }
95554aad
TT
10390
10391 for (ix = 0;
796a7ff8 10392 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 10393 ++ix)
ec94af83
DE
10394 {
10395 recursively_compute_inclusions (result, all_children,
43f3e411 10396 all_type_symtabs, iter, cust);
ec94af83 10397 }
95554aad
TT
10398}
10399
43f3e411 10400/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10401 PER_CU. */
10402
10403static void
43f3e411 10404compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10405{
f4dc4d17
DE
10406 gdb_assert (! per_cu->is_debug_types);
10407
796a7ff8 10408 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
10409 {
10410 int ix, len;
ec94af83 10411 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
10412 struct compunit_symtab *compunit_symtab_iter;
10413 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 10414 htab_t all_children, all_type_symtabs;
43f3e411 10415 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10416
10417 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10418 if (cust == NULL)
95554aad
TT
10419 return;
10420
10421 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10422 NULL, xcalloc, xfree);
ec94af83
DE
10423 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10424 NULL, xcalloc, xfree);
95554aad
TT
10425
10426 for (ix = 0;
796a7ff8 10427 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 10428 ix, per_cu_iter);
95554aad 10429 ++ix)
ec94af83
DE
10430 {
10431 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 10432 all_type_symtabs, per_cu_iter,
43f3e411 10433 cust);
ec94af83 10434 }
95554aad 10435
ec94af83 10436 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
10437 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10438 cust->includes
ed2dc618 10439 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10440 struct compunit_symtab *, len + 1);
95554aad 10441 for (ix = 0;
43f3e411
DE
10442 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10443 compunit_symtab_iter);
95554aad 10444 ++ix)
43f3e411
DE
10445 cust->includes[ix] = compunit_symtab_iter;
10446 cust->includes[len] = NULL;
95554aad 10447
43f3e411 10448 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 10449 htab_delete (all_children);
ec94af83 10450 htab_delete (all_type_symtabs);
95554aad
TT
10451 }
10452}
10453
10454/* Compute the 'includes' field for the symtabs of all the CUs we just
10455 read. */
10456
10457static void
ed2dc618 10458process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad
TT
10459{
10460 int ix;
10461 struct dwarf2_per_cu_data *iter;
10462
10463 for (ix = 0;
10464 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10465 ix, iter);
10466 ++ix)
f4dc4d17
DE
10467 {
10468 if (! iter->is_debug_types)
43f3e411 10469 compute_compunit_symtab_includes (iter);
f4dc4d17 10470 }
95554aad
TT
10471
10472 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10473}
10474
9cdd5dbd 10475/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10476 already been loaded into memory. */
10477
10478static void
95554aad
TT
10479process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10480 enum language pretend_language)
10b3939b 10481{
10b3939b 10482 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10483 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10484 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10485 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10486 CORE_ADDR lowpc, highpc;
43f3e411 10487 struct compunit_symtab *cust;
33c7c59d 10488 struct cleanup *delayed_list_cleanup;
10b3939b 10489 CORE_ADDR baseaddr;
4359dff1 10490 struct block *static_block;
3e29f34a 10491 CORE_ADDR addr;
10b3939b
DJ
10492
10493 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10494
10b3939b 10495 buildsym_init ();
33c7c59d 10496 scoped_free_pendings free_pending;
3da10d80 10497 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
10498
10499 cu->list_in_scope = &file_symbols;
c906108c 10500
95554aad
TT
10501 cu->language = pretend_language;
10502 cu->language_defn = language_def (cu->language);
10503
c906108c 10504 /* Do line number decoding in read_file_scope () */
10b3939b 10505 process_die (cu->dies, cu);
c906108c 10506
a766d390
DE
10507 /* For now fudge the Go package. */
10508 if (cu->language == language_go)
10509 fixup_go_packaging (cu);
10510
3da10d80
KS
10511 /* Now that we have processed all the DIEs in the CU, all the types
10512 should be complete, and it should now be safe to compute all of the
10513 physnames. */
10514 compute_delayed_physnames (cu);
10515 do_cleanups (delayed_list_cleanup);
10516
fae299cd
DC
10517 /* Some compilers don't define a DW_AT_high_pc attribute for the
10518 compilation unit. If the DW_AT_high_pc is missing, synthesize
10519 it, by scanning the DIE's below the compilation unit. */
10b3939b 10520 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10521
3e29f34a
MR
10522 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10523 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10524
10525 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10526 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10527 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10528 addrmap to help ensure it has an accurate map of pc values belonging to
10529 this comp unit. */
10530 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10531
43f3e411
DE
10532 cust = end_symtab_from_static_block (static_block,
10533 SECT_OFF_TEXT (objfile), 0);
c906108c 10534
43f3e411 10535 if (cust != NULL)
c906108c 10536 {
df15bd07 10537 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10538
8be455d7
JK
10539 /* Set symtab language to language from DW_AT_language. If the
10540 compilation is from a C file generated by language preprocessors, do
10541 not set the language if it was already deduced by start_subfile. */
43f3e411 10542 if (!(cu->language == language_c
40e3ad0e 10543 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10544 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10545
10546 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10547 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10548 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10549 there were bugs in prologue debug info, fixed later in GCC-4.5
10550 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10551
10552 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10553 needed, it would be wrong due to missing DW_AT_producer there.
10554
10555 Still one can confuse GDB by using non-standard GCC compilation
10556 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10557 */
ab260dad 10558 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10559 cust->locations_valid = 1;
e0d00bc7
JK
10560
10561 if (gcc_4_minor >= 5)
43f3e411 10562 cust->epilogue_unwind_valid = 1;
96408a79 10563
43f3e411 10564 cust->call_site_htab = cu->call_site_htab;
c906108c 10565 }
9291a0cd
TT
10566
10567 if (dwarf2_per_objfile->using_index)
43f3e411 10568 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10569 else
10570 {
10571 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10572 pst->compunit_symtab = cust;
9291a0cd
TT
10573 pst->readin = 1;
10574 }
c906108c 10575
95554aad
TT
10576 /* Push it for inclusion processing later. */
10577 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
f4dc4d17 10578}
45cfd468 10579
f4dc4d17
DE
10580/* Generate full symbol information for type unit PER_CU, whose DIEs have
10581 already been loaded into memory. */
10582
10583static void
10584process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10585 enum language pretend_language)
10586{
10587 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10588 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10589 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10590 struct compunit_symtab *cust;
33c7c59d 10591 struct cleanup *delayed_list_cleanup;
0186c6a7
DE
10592 struct signatured_type *sig_type;
10593
10594 gdb_assert (per_cu->is_debug_types);
10595 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
10596
10597 buildsym_init ();
33c7c59d 10598 scoped_free_pendings free_pending;
f4dc4d17
DE
10599 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10600
10601 cu->list_in_scope = &file_symbols;
10602
10603 cu->language = pretend_language;
10604 cu->language_defn = language_def (cu->language);
10605
10606 /* The symbol tables are set up in read_type_unit_scope. */
10607 process_die (cu->dies, cu);
10608
10609 /* For now fudge the Go package. */
10610 if (cu->language == language_go)
10611 fixup_go_packaging (cu);
10612
10613 /* Now that we have processed all the DIEs in the CU, all the types
10614 should be complete, and it should now be safe to compute all of the
10615 physnames. */
10616 compute_delayed_physnames (cu);
10617 do_cleanups (delayed_list_cleanup);
10618
10619 /* TUs share symbol tables.
10620 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10621 of it with end_expandable_symtab. Otherwise, complete the addition of
10622 this TU's symbols to the existing symtab. */
43f3e411 10623 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10624 {
43f3e411
DE
10625 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10626 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10627
43f3e411 10628 if (cust != NULL)
f4dc4d17
DE
10629 {
10630 /* Set symtab language to language from DW_AT_language. If the
10631 compilation is from a C file generated by language preprocessors,
10632 do not set the language if it was already deduced by
10633 start_subfile. */
43f3e411
DE
10634 if (!(cu->language == language_c
10635 && COMPUNIT_FILETABS (cust)->language != language_c))
10636 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10637 }
10638 }
10639 else
10640 {
0ab9ce85 10641 augment_type_symtab ();
43f3e411 10642 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10643 }
10644
10645 if (dwarf2_per_objfile->using_index)
43f3e411 10646 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10647 else
10648 {
10649 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10650 pst->compunit_symtab = cust;
f4dc4d17 10651 pst->readin = 1;
45cfd468 10652 }
c906108c
SS
10653}
10654
95554aad
TT
10655/* Process an imported unit DIE. */
10656
10657static void
10658process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10659{
10660 struct attribute *attr;
10661
f4dc4d17
DE
10662 /* For now we don't handle imported units in type units. */
10663 if (cu->per_cu->is_debug_types)
10664 {
10665 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10666 " supported in type units [in module %s]"),
518817b3 10667 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10668 }
10669
95554aad
TT
10670 attr = dwarf2_attr (die, DW_AT_import, cu);
10671 if (attr != NULL)
10672 {
9c541725
PA
10673 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10674 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10675 dwarf2_per_cu_data *per_cu
e3b94546 10676 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10677 cu->per_cu->dwarf2_per_objfile);
95554aad 10678
69d751e3 10679 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
10680 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10681 load_full_comp_unit (per_cu, cu->language);
10682
796a7ff8 10683 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
10684 per_cu);
10685 }
10686}
10687
4c8aa72d
PA
10688/* RAII object that represents a process_die scope: i.e.,
10689 starts/finishes processing a DIE. */
10690class process_die_scope
adde2bff 10691{
4c8aa72d
PA
10692public:
10693 process_die_scope (die_info *die, dwarf2_cu *cu)
10694 : m_die (die), m_cu (cu)
10695 {
10696 /* We should only be processing DIEs not already in process. */
10697 gdb_assert (!m_die->in_process);
10698 m_die->in_process = true;
10699 }
8c3cb9fa 10700
4c8aa72d
PA
10701 ~process_die_scope ()
10702 {
10703 m_die->in_process = false;
10704
10705 /* If we're done processing the DIE for the CU that owns the line
10706 header, we don't need the line header anymore. */
10707 if (m_cu->line_header_die_owner == m_die)
10708 {
10709 delete m_cu->line_header;
10710 m_cu->line_header = NULL;
10711 m_cu->line_header_die_owner = NULL;
10712 }
10713 }
10714
10715private:
10716 die_info *m_die;
10717 dwarf2_cu *m_cu;
10718};
adde2bff 10719
c906108c
SS
10720/* Process a die and its children. */
10721
10722static void
e7c27a73 10723process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10724{
4c8aa72d 10725 process_die_scope scope (die, cu);
adde2bff 10726
c906108c
SS
10727 switch (die->tag)
10728 {
10729 case DW_TAG_padding:
10730 break;
10731 case DW_TAG_compile_unit:
95554aad 10732 case DW_TAG_partial_unit:
e7c27a73 10733 read_file_scope (die, cu);
c906108c 10734 break;
348e048f
DE
10735 case DW_TAG_type_unit:
10736 read_type_unit_scope (die, cu);
10737 break;
c906108c 10738 case DW_TAG_subprogram:
c906108c 10739 case DW_TAG_inlined_subroutine:
edb3359d 10740 read_func_scope (die, cu);
c906108c
SS
10741 break;
10742 case DW_TAG_lexical_block:
14898363
L
10743 case DW_TAG_try_block:
10744 case DW_TAG_catch_block:
e7c27a73 10745 read_lexical_block_scope (die, cu);
c906108c 10746 break;
216f72a1 10747 case DW_TAG_call_site:
96408a79
SA
10748 case DW_TAG_GNU_call_site:
10749 read_call_site_scope (die, cu);
10750 break;
c906108c 10751 case DW_TAG_class_type:
680b30c7 10752 case DW_TAG_interface_type:
c906108c
SS
10753 case DW_TAG_structure_type:
10754 case DW_TAG_union_type:
134d01f1 10755 process_structure_scope (die, cu);
c906108c
SS
10756 break;
10757 case DW_TAG_enumeration_type:
134d01f1 10758 process_enumeration_scope (die, cu);
c906108c 10759 break;
134d01f1 10760
f792889a
DJ
10761 /* These dies have a type, but processing them does not create
10762 a symbol or recurse to process the children. Therefore we can
10763 read them on-demand through read_type_die. */
c906108c 10764 case DW_TAG_subroutine_type:
72019c9c 10765 case DW_TAG_set_type:
c906108c 10766 case DW_TAG_array_type:
c906108c 10767 case DW_TAG_pointer_type:
c906108c 10768 case DW_TAG_ptr_to_member_type:
c906108c 10769 case DW_TAG_reference_type:
4297a3f0 10770 case DW_TAG_rvalue_reference_type:
c906108c 10771 case DW_TAG_string_type:
c906108c 10772 break;
134d01f1 10773
c906108c 10774 case DW_TAG_base_type:
a02abb62 10775 case DW_TAG_subrange_type:
cb249c71 10776 case DW_TAG_typedef:
134d01f1
DJ
10777 /* Add a typedef symbol for the type definition, if it has a
10778 DW_AT_name. */
f792889a 10779 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10780 break;
c906108c 10781 case DW_TAG_common_block:
e7c27a73 10782 read_common_block (die, cu);
c906108c
SS
10783 break;
10784 case DW_TAG_common_inclusion:
10785 break;
d9fa45fe 10786 case DW_TAG_namespace:
4d4ec4e5 10787 cu->processing_has_namespace_info = 1;
e7c27a73 10788 read_namespace (die, cu);
d9fa45fe 10789 break;
5d7cb8df 10790 case DW_TAG_module:
4d4ec4e5 10791 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
10792 read_module (die, cu);
10793 break;
d9fa45fe 10794 case DW_TAG_imported_declaration:
74921315
KS
10795 cu->processing_has_namespace_info = 1;
10796 if (read_namespace_alias (die, cu))
10797 break;
10798 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 10799 case DW_TAG_imported_module:
4d4ec4e5 10800 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
10801 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10802 || cu->language != language_fortran))
10803 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10804 dwarf_tag_name (die->tag));
10805 read_import_statement (die, cu);
d9fa45fe 10806 break;
95554aad
TT
10807
10808 case DW_TAG_imported_unit:
10809 process_imported_unit_die (die, cu);
10810 break;
10811
71a3c369
TT
10812 case DW_TAG_variable:
10813 read_variable (die, cu);
10814 break;
10815
c906108c 10816 default:
e7c27a73 10817 new_symbol (die, NULL, cu);
c906108c
SS
10818 break;
10819 }
10820}
ca69b9e6
DE
10821\f
10822/* DWARF name computation. */
c906108c 10823
94af9270
KS
10824/* A helper function for dwarf2_compute_name which determines whether DIE
10825 needs to have the name of the scope prepended to the name listed in the
10826 die. */
10827
10828static int
10829die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10830{
1c809c68
TT
10831 struct attribute *attr;
10832
94af9270
KS
10833 switch (die->tag)
10834 {
10835 case DW_TAG_namespace:
10836 case DW_TAG_typedef:
10837 case DW_TAG_class_type:
10838 case DW_TAG_interface_type:
10839 case DW_TAG_structure_type:
10840 case DW_TAG_union_type:
10841 case DW_TAG_enumeration_type:
10842 case DW_TAG_enumerator:
10843 case DW_TAG_subprogram:
08a76f8a 10844 case DW_TAG_inlined_subroutine:
94af9270 10845 case DW_TAG_member:
74921315 10846 case DW_TAG_imported_declaration:
94af9270
KS
10847 return 1;
10848
10849 case DW_TAG_variable:
c2b0a229 10850 case DW_TAG_constant:
94af9270
KS
10851 /* We only need to prefix "globally" visible variables. These include
10852 any variable marked with DW_AT_external or any variable that
10853 lives in a namespace. [Variables in anonymous namespaces
10854 require prefixing, but they are not DW_AT_external.] */
10855
10856 if (dwarf2_attr (die, DW_AT_specification, cu))
10857 {
10858 struct dwarf2_cu *spec_cu = cu;
9a619af0 10859
94af9270
KS
10860 return die_needs_namespace (die_specification (die, &spec_cu),
10861 spec_cu);
10862 }
10863
1c809c68 10864 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10865 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10866 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10867 return 0;
10868 /* A variable in a lexical block of some kind does not need a
10869 namespace, even though in C++ such variables may be external
10870 and have a mangled name. */
10871 if (die->parent->tag == DW_TAG_lexical_block
10872 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10873 || die->parent->tag == DW_TAG_catch_block
10874 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10875 return 0;
10876 return 1;
94af9270
KS
10877
10878 default:
10879 return 0;
10880 }
10881}
10882
73b9be8b
KS
10883/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10884 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10885 defined for the given DIE. */
10886
10887static struct attribute *
10888dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10889{
10890 struct attribute *attr;
10891
10892 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10893 if (attr == NULL)
10894 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10895
10896 return attr;
10897}
10898
10899/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10900 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10901 defined for the given DIE. */
10902
10903static const char *
10904dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10905{
10906 const char *linkage_name;
10907
10908 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10909 if (linkage_name == NULL)
10910 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10911
10912 return linkage_name;
10913}
10914
94af9270 10915/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10916 compute the physname for the object, which include a method's:
9c37b5ae 10917 - formal parameters (C++),
a766d390 10918 - receiver type (Go),
a766d390
DE
10919
10920 The term "physname" is a bit confusing.
10921 For C++, for example, it is the demangled name.
10922 For Go, for example, it's the mangled name.
94af9270 10923
af6b7be1
JB
10924 For Ada, return the DIE's linkage name rather than the fully qualified
10925 name. PHYSNAME is ignored..
10926
94af9270
KS
10927 The result is allocated on the objfile_obstack and canonicalized. */
10928
10929static const char *
15d034d0
TT
10930dwarf2_compute_name (const char *name,
10931 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10932 int physname)
10933{
518817b3 10934 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10935
94af9270
KS
10936 if (name == NULL)
10937 name = dwarf2_name (die, cu);
10938
2ee7123e
DE
10939 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10940 but otherwise compute it by typename_concat inside GDB.
10941 FIXME: Actually this is not really true, or at least not always true.
10942 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10943 Fortran names because there is no mangling standard. So new_symbol_full
10944 will set the demangled name to the result of dwarf2_full_name, and it is
10945 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10946 if (cu->language == language_ada
10947 || (cu->language == language_fortran && physname))
10948 {
10949 /* For Ada unit, we prefer the linkage name over the name, as
10950 the former contains the exported name, which the user expects
10951 to be able to reference. Ideally, we want the user to be able
10952 to reference this entity using either natural or linkage name,
10953 but we haven't started looking at this enhancement yet. */
73b9be8b 10954 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10955
2ee7123e
DE
10956 if (linkage_name != NULL)
10957 return linkage_name;
f55ee35c
JK
10958 }
10959
94af9270
KS
10960 /* These are the only languages we know how to qualify names in. */
10961 if (name != NULL
9c37b5ae 10962 && (cu->language == language_cplus
c44af4eb
TT
10963 || cu->language == language_fortran || cu->language == language_d
10964 || cu->language == language_rust))
94af9270
KS
10965 {
10966 if (die_needs_namespace (die, cu))
10967 {
0d5cff50 10968 const char *prefix;
34a68019 10969 const char *canonical_name = NULL;
94af9270 10970
d7e74731
PA
10971 string_file buf;
10972
94af9270 10973 prefix = determine_prefix (die, cu);
94af9270
KS
10974 if (*prefix != '\0')
10975 {
f55ee35c
JK
10976 char *prefixed_name = typename_concat (NULL, prefix, name,
10977 physname, cu);
9a619af0 10978
d7e74731 10979 buf.puts (prefixed_name);
94af9270
KS
10980 xfree (prefixed_name);
10981 }
10982 else
d7e74731 10983 buf.puts (name);
94af9270 10984
98bfdba5
PA
10985 /* Template parameters may be specified in the DIE's DW_AT_name, or
10986 as children with DW_TAG_template_type_param or
10987 DW_TAG_value_type_param. If the latter, add them to the name
10988 here. If the name already has template parameters, then
10989 skip this step; some versions of GCC emit both, and
10990 it is more efficient to use the pre-computed name.
10991
10992 Something to keep in mind about this process: it is very
10993 unlikely, or in some cases downright impossible, to produce
10994 something that will match the mangled name of a function.
10995 If the definition of the function has the same debug info,
10996 we should be able to match up with it anyway. But fallbacks
10997 using the minimal symbol, for instance to find a method
10998 implemented in a stripped copy of libstdc++, will not work.
10999 If we do not have debug info for the definition, we will have to
11000 match them up some other way.
11001
11002 When we do name matching there is a related problem with function
11003 templates; two instantiated function templates are allowed to
11004 differ only by their return types, which we do not add here. */
11005
11006 if (cu->language == language_cplus && strchr (name, '<') == NULL)
11007 {
11008 struct attribute *attr;
11009 struct die_info *child;
11010 int first = 1;
11011
11012 die->building_fullname = 1;
11013
11014 for (child = die->child; child != NULL; child = child->sibling)
11015 {
11016 struct type *type;
12df843f 11017 LONGEST value;
d521ce57 11018 const gdb_byte *bytes;
98bfdba5
PA
11019 struct dwarf2_locexpr_baton *baton;
11020 struct value *v;
11021
11022 if (child->tag != DW_TAG_template_type_param
11023 && child->tag != DW_TAG_template_value_param)
11024 continue;
11025
11026 if (first)
11027 {
d7e74731 11028 buf.puts ("<");
98bfdba5
PA
11029 first = 0;
11030 }
11031 else
d7e74731 11032 buf.puts (", ");
98bfdba5
PA
11033
11034 attr = dwarf2_attr (child, DW_AT_type, cu);
11035 if (attr == NULL)
11036 {
11037 complaint (&symfile_complaints,
11038 _("template parameter missing DW_AT_type"));
d7e74731 11039 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
11040 continue;
11041 }
11042 type = die_type (child, cu);
11043
11044 if (child->tag == DW_TAG_template_type_param)
11045 {
d7e74731 11046 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
11047 continue;
11048 }
11049
11050 attr = dwarf2_attr (child, DW_AT_const_value, cu);
11051 if (attr == NULL)
11052 {
11053 complaint (&symfile_complaints,
3e43a32a
MS
11054 _("template parameter missing "
11055 "DW_AT_const_value"));
d7e74731 11056 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
11057 continue;
11058 }
11059
11060 dwarf2_const_value_attr (attr, type, name,
11061 &cu->comp_unit_obstack, cu,
11062 &value, &bytes, &baton);
11063
11064 if (TYPE_NOSIGN (type))
11065 /* GDB prints characters as NUMBER 'CHAR'. If that's
11066 changed, this can use value_print instead. */
d7e74731 11067 c_printchar (value, type, &buf);
98bfdba5
PA
11068 else
11069 {
11070 struct value_print_options opts;
11071
11072 if (baton != NULL)
11073 v = dwarf2_evaluate_loc_desc (type, NULL,
11074 baton->data,
11075 baton->size,
11076 baton->per_cu);
11077 else if (bytes != NULL)
11078 {
11079 v = allocate_value (type);
11080 memcpy (value_contents_writeable (v), bytes,
11081 TYPE_LENGTH (type));
11082 }
11083 else
11084 v = value_from_longest (type, value);
11085
3e43a32a
MS
11086 /* Specify decimal so that we do not depend on
11087 the radix. */
98bfdba5
PA
11088 get_formatted_print_options (&opts, 'd');
11089 opts.raw = 1;
d7e74731 11090 value_print (v, &buf, &opts);
98bfdba5
PA
11091 release_value (v);
11092 value_free (v);
11093 }
11094 }
11095
11096 die->building_fullname = 0;
11097
11098 if (!first)
11099 {
11100 /* Close the argument list, with a space if necessary
11101 (nested templates). */
d7e74731
PA
11102 if (!buf.empty () && buf.string ().back () == '>')
11103 buf.puts (" >");
98bfdba5 11104 else
d7e74731 11105 buf.puts (">");
98bfdba5
PA
11106 }
11107 }
11108
9c37b5ae 11109 /* For C++ methods, append formal parameter type
94af9270 11110 information, if PHYSNAME. */
6e70227d 11111
94af9270 11112 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 11113 && cu->language == language_cplus)
94af9270
KS
11114 {
11115 struct type *type = read_type_die (die, cu);
11116
d7e74731 11117 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 11118 &type_print_raw_options);
94af9270 11119
9c37b5ae 11120 if (cu->language == language_cplus)
94af9270 11121 {
60430eff
DJ
11122 /* Assume that an artificial first parameter is
11123 "this", but do not crash if it is not. RealView
11124 marks unnamed (and thus unused) parameters as
11125 artificial; there is no way to differentiate
11126 the two cases. */
94af9270
KS
11127 if (TYPE_NFIELDS (type) > 0
11128 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11129 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11130 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11131 0))))
d7e74731 11132 buf.puts (" const");
94af9270
KS
11133 }
11134 }
11135
d7e74731 11136 const std::string &intermediate_name = buf.string ();
94af9270
KS
11137
11138 if (cu->language == language_cplus)
34a68019 11139 canonical_name
322a8516 11140 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11141 &objfile->per_bfd->storage_obstack);
11142
11143 /* If we only computed INTERMEDIATE_NAME, or if
11144 INTERMEDIATE_NAME is already canonical, then we need to
11145 copy it to the appropriate obstack. */
322a8516 11146 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
11147 name = ((const char *)
11148 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
11149 intermediate_name.c_str (),
11150 intermediate_name.length ()));
34a68019
TT
11151 else
11152 name = canonical_name;
94af9270
KS
11153 }
11154 }
11155
11156 return name;
11157}
11158
0114d602
DJ
11159/* Return the fully qualified name of DIE, based on its DW_AT_name.
11160 If scope qualifiers are appropriate they will be added. The result
34a68019 11161 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11162 not have a name. NAME may either be from a previous call to
11163 dwarf2_name or NULL.
11164
9c37b5ae 11165 The output string will be canonicalized (if C++). */
0114d602
DJ
11166
11167static const char *
15d034d0 11168dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11169{
94af9270
KS
11170 return dwarf2_compute_name (name, die, cu, 0);
11171}
0114d602 11172
94af9270
KS
11173/* Construct a physname for the given DIE in CU. NAME may either be
11174 from a previous call to dwarf2_name or NULL. The result will be
11175 allocated on the objfile_objstack or NULL if the DIE does not have a
11176 name.
0114d602 11177
9c37b5ae 11178 The output string will be canonicalized (if C++). */
0114d602 11179
94af9270 11180static const char *
15d034d0 11181dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11182{
518817b3 11183 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11184 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11185 int need_copy = 1;
11186
11187 /* In this case dwarf2_compute_name is just a shortcut not building anything
11188 on its own. */
11189 if (!die_needs_namespace (die, cu))
11190 return dwarf2_compute_name (name, die, cu, 1);
11191
73b9be8b 11192 mangled = dw2_linkage_name (die, cu);
900e11f9 11193
e98c9e7c
TT
11194 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11195 See https://github.com/rust-lang/rust/issues/32925. */
11196 if (cu->language == language_rust && mangled != NULL
11197 && strchr (mangled, '{') != NULL)
11198 mangled = NULL;
11199
900e11f9
JK
11200 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11201 has computed. */
791afaa2 11202 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11203 if (mangled != NULL)
900e11f9 11204 {
900e11f9
JK
11205 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
11206 type. It is easier for GDB users to search for such functions as
11207 `name(params)' than `long name(params)'. In such case the minimal
11208 symbol names do not match the full symbol names but for template
11209 functions there is never a need to look up their definition from their
11210 declaration so the only disadvantage remains the minimal symbol
11211 variant `long name(params)' does not have the proper inferior type.
11212 */
11213
a766d390
DE
11214 if (cu->language == language_go)
11215 {
11216 /* This is a lie, but we already lie to the caller new_symbol_full.
11217 new_symbol_full assumes we return the mangled name.
11218 This just undoes that lie until things are cleaned up. */
a766d390
DE
11219 }
11220 else
11221 {
791afaa2
TT
11222 demangled.reset (gdb_demangle (mangled,
11223 (DMGL_PARAMS | DMGL_ANSI
11224 | DMGL_RET_DROP)));
a766d390 11225 }
900e11f9 11226 if (demangled)
791afaa2 11227 canon = demangled.get ();
900e11f9
JK
11228 else
11229 {
11230 canon = mangled;
11231 need_copy = 0;
11232 }
11233 }
11234
11235 if (canon == NULL || check_physname)
11236 {
11237 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11238
11239 if (canon != NULL && strcmp (physname, canon) != 0)
11240 {
11241 /* It may not mean a bug in GDB. The compiler could also
11242 compute DW_AT_linkage_name incorrectly. But in such case
11243 GDB would need to be bug-to-bug compatible. */
11244
11245 complaint (&symfile_complaints,
11246 _("Computed physname <%s> does not match demangled <%s> "
11247 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9c541725 11248 physname, canon, mangled, to_underlying (die->sect_off),
4262abfb 11249 objfile_name (objfile));
900e11f9
JK
11250
11251 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11252 is available here - over computed PHYSNAME. It is safer
11253 against both buggy GDB and buggy compilers. */
11254
11255 retval = canon;
11256 }
11257 else
11258 {
11259 retval = physname;
11260 need_copy = 0;
11261 }
11262 }
11263 else
11264 retval = canon;
11265
11266 if (need_copy)
224c3ddb
SM
11267 retval = ((const char *)
11268 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11269 retval, strlen (retval)));
900e11f9 11270
900e11f9 11271 return retval;
0114d602
DJ
11272}
11273
74921315
KS
11274/* Inspect DIE in CU for a namespace alias. If one exists, record
11275 a new symbol for it.
11276
11277 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11278
11279static int
11280read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11281{
11282 struct attribute *attr;
11283
11284 /* If the die does not have a name, this is not a namespace
11285 alias. */
11286 attr = dwarf2_attr (die, DW_AT_name, cu);
11287 if (attr != NULL)
11288 {
11289 int num;
11290 struct die_info *d = die;
11291 struct dwarf2_cu *imported_cu = cu;
11292
11293 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11294 keep inspecting DIEs until we hit the underlying import. */
11295#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11296 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11297 {
11298 attr = dwarf2_attr (d, DW_AT_import, cu);
11299 if (attr == NULL)
11300 break;
11301
11302 d = follow_die_ref (d, attr, &imported_cu);
11303 if (d->tag != DW_TAG_imported_declaration)
11304 break;
11305 }
11306
11307 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11308 {
11309 complaint (&symfile_complaints,
11310 _("DIE at 0x%x has too many recursively imported "
9c541725 11311 "declarations"), to_underlying (d->sect_off));
74921315
KS
11312 return 0;
11313 }
11314
11315 if (attr != NULL)
11316 {
11317 struct type *type;
9c541725 11318 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11319
9c541725 11320 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11321 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11322 {
11323 /* This declaration is a global namespace alias. Add
11324 a symbol for it whose type is the aliased namespace. */
11325 new_symbol (die, type, cu);
11326 return 1;
11327 }
11328 }
11329 }
11330
11331 return 0;
11332}
11333
22cee43f
PMR
11334/* Return the using directives repository (global or local?) to use in the
11335 current context for LANGUAGE.
11336
11337 For Ada, imported declarations can materialize renamings, which *may* be
11338 global. However it is impossible (for now?) in DWARF to distinguish
11339 "external" imported declarations and "static" ones. As all imported
11340 declarations seem to be static in all other languages, make them all CU-wide
11341 global only in Ada. */
11342
11343static struct using_direct **
11344using_directives (enum language language)
11345{
11346 if (language == language_ada && context_stack_depth == 0)
11347 return &global_using_directives;
11348 else
11349 return &local_using_directives;
11350}
11351
27aa8d6a
SW
11352/* Read the import statement specified by the given die and record it. */
11353
11354static void
11355read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11356{
518817b3 11357 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11358 struct attribute *import_attr;
32019081 11359 struct die_info *imported_die, *child_die;
de4affc9 11360 struct dwarf2_cu *imported_cu;
27aa8d6a 11361 const char *imported_name;
794684b6 11362 const char *imported_name_prefix;
13387711
SW
11363 const char *canonical_name;
11364 const char *import_alias;
11365 const char *imported_declaration = NULL;
794684b6 11366 const char *import_prefix;
eb1e02fd 11367 std::vector<const char *> excludes;
13387711 11368
27aa8d6a
SW
11369 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11370 if (import_attr == NULL)
11371 {
11372 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11373 dwarf_tag_name (die->tag));
11374 return;
11375 }
11376
de4affc9
CC
11377 imported_cu = cu;
11378 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11379 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11380 if (imported_name == NULL)
11381 {
11382 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11383
11384 The import in the following code:
11385 namespace A
11386 {
11387 typedef int B;
11388 }
11389
11390 int main ()
11391 {
11392 using A::B;
11393 B b;
11394 return b;
11395 }
11396
11397 ...
11398 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11399 <52> DW_AT_decl_file : 1
11400 <53> DW_AT_decl_line : 6
11401 <54> DW_AT_import : <0x75>
11402 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11403 <59> DW_AT_name : B
11404 <5b> DW_AT_decl_file : 1
11405 <5c> DW_AT_decl_line : 2
11406 <5d> DW_AT_type : <0x6e>
11407 ...
11408 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11409 <76> DW_AT_byte_size : 4
11410 <77> DW_AT_encoding : 5 (signed)
11411
11412 imports the wrong die ( 0x75 instead of 0x58 ).
11413 This case will be ignored until the gcc bug is fixed. */
11414 return;
11415 }
11416
82856980
SW
11417 /* Figure out the local name after import. */
11418 import_alias = dwarf2_name (die, cu);
27aa8d6a 11419
794684b6
SW
11420 /* Figure out where the statement is being imported to. */
11421 import_prefix = determine_prefix (die, cu);
11422
11423 /* Figure out what the scope of the imported die is and prepend it
11424 to the name of the imported die. */
de4affc9 11425 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11426
f55ee35c
JK
11427 if (imported_die->tag != DW_TAG_namespace
11428 && imported_die->tag != DW_TAG_module)
794684b6 11429 {
13387711
SW
11430 imported_declaration = imported_name;
11431 canonical_name = imported_name_prefix;
794684b6 11432 }
13387711 11433 else if (strlen (imported_name_prefix) > 0)
12aaed36 11434 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11435 imported_name_prefix,
11436 (cu->language == language_d ? "." : "::"),
11437 imported_name, (char *) NULL);
13387711
SW
11438 else
11439 canonical_name = imported_name;
794684b6 11440
32019081
JK
11441 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11442 for (child_die = die->child; child_die && child_die->tag;
11443 child_die = sibling_die (child_die))
11444 {
11445 /* DWARF-4: A Fortran use statement with a “rename list” may be
11446 represented by an imported module entry with an import attribute
11447 referring to the module and owned entries corresponding to those
11448 entities that are renamed as part of being imported. */
11449
11450 if (child_die->tag != DW_TAG_imported_declaration)
11451 {
11452 complaint (&symfile_complaints,
11453 _("child DW_TAG_imported_declaration expected "
11454 "- DIE at 0x%x [in module %s]"),
9c541725 11455 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
11456 continue;
11457 }
11458
11459 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11460 if (import_attr == NULL)
11461 {
11462 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11463 dwarf_tag_name (child_die->tag));
11464 continue;
11465 }
11466
11467 imported_cu = cu;
11468 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11469 &imported_cu);
11470 imported_name = dwarf2_name (imported_die, imported_cu);
11471 if (imported_name == NULL)
11472 {
11473 complaint (&symfile_complaints,
11474 _("child DW_TAG_imported_declaration has unknown "
11475 "imported name - DIE at 0x%x [in module %s]"),
9c541725 11476 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
11477 continue;
11478 }
11479
eb1e02fd 11480 excludes.push_back (imported_name);
32019081
JK
11481
11482 process_die (child_die, cu);
11483 }
11484
22cee43f
PMR
11485 add_using_directive (using_directives (cu->language),
11486 import_prefix,
11487 canonical_name,
11488 import_alias,
11489 imported_declaration,
11490 excludes,
11491 0,
11492 &objfile->objfile_obstack);
27aa8d6a
SW
11493}
11494
5230b05a
WT
11495/* ICC<14 does not output the required DW_AT_declaration on incomplete
11496 types, but gives them a size of zero. Starting with version 14,
11497 ICC is compatible with GCC. */
11498
11499static int
11500producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11501{
11502 if (!cu->checked_producer)
11503 check_producer (cu);
11504
11505 return cu->producer_is_icc_lt_14;
11506}
11507
1b80a9fa
JK
11508/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11509 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11510 this, it was first present in GCC release 4.3.0. */
11511
11512static int
11513producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11514{
11515 if (!cu->checked_producer)
11516 check_producer (cu);
11517
11518 return cu->producer_is_gcc_lt_4_3;
11519}
11520
d721ba37
PA
11521static file_and_directory
11522find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11523{
d721ba37
PA
11524 file_and_directory res;
11525
9291a0cd
TT
11526 /* Find the filename. Do not use dwarf2_name here, since the filename
11527 is not a source language identifier. */
d721ba37
PA
11528 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11529 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11530
d721ba37
PA
11531 if (res.comp_dir == NULL
11532 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11533 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11534 {
d721ba37
PA
11535 res.comp_dir_storage = ldirname (res.name);
11536 if (!res.comp_dir_storage.empty ())
11537 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11538 }
d721ba37 11539 if (res.comp_dir != NULL)
9291a0cd
TT
11540 {
11541 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11542 directory, get rid of it. */
d721ba37 11543 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11544
d721ba37
PA
11545 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11546 res.comp_dir = cp + 1;
9291a0cd
TT
11547 }
11548
d721ba37
PA
11549 if (res.name == NULL)
11550 res.name = "<unknown>";
11551
11552 return res;
9291a0cd
TT
11553}
11554
f4dc4d17
DE
11555/* Handle DW_AT_stmt_list for a compilation unit.
11556 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11557 COMP_DIR is the compilation directory. LOWPC is passed to
11558 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11559
11560static void
11561handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11562 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11563{
518817b3
SM
11564 struct dwarf2_per_objfile *dwarf2_per_objfile
11565 = cu->per_cu->dwarf2_per_objfile;
527f3840 11566 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11567 struct attribute *attr;
527f3840
JK
11568 struct line_header line_header_local;
11569 hashval_t line_header_local_hash;
527f3840
JK
11570 void **slot;
11571 int decode_mapping;
2ab95328 11572
f4dc4d17
DE
11573 gdb_assert (! cu->per_cu->is_debug_types);
11574
2ab95328 11575 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11576 if (attr == NULL)
11577 return;
11578
9c541725 11579 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11580
11581 /* The line header hash table is only created if needed (it exists to
11582 prevent redundant reading of the line table for partial_units).
11583 If we're given a partial_unit, we'll need it. If we're given a
11584 compile_unit, then use the line header hash table if it's already
11585 created, but don't create one just yet. */
11586
11587 if (dwarf2_per_objfile->line_header_hash == NULL
11588 && die->tag == DW_TAG_partial_unit)
2ab95328 11589 {
527f3840
JK
11590 dwarf2_per_objfile->line_header_hash
11591 = htab_create_alloc_ex (127, line_header_hash_voidp,
11592 line_header_eq_voidp,
11593 free_line_header_voidp,
11594 &objfile->objfile_obstack,
11595 hashtab_obstack_allocate,
11596 dummy_obstack_deallocate);
11597 }
2ab95328 11598
9c541725 11599 line_header_local.sect_off = line_offset;
527f3840
JK
11600 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11601 line_header_local_hash = line_header_hash (&line_header_local);
11602 if (dwarf2_per_objfile->line_header_hash != NULL)
11603 {
11604 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11605 &line_header_local,
11606 line_header_local_hash, NO_INSERT);
11607
11608 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11609 is not present in *SLOT (since if there is something in *SLOT then
11610 it will be for a partial_unit). */
11611 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11612 {
527f3840 11613 gdb_assert (*slot != NULL);
9a3c8263 11614 cu->line_header = (struct line_header *) *slot;
527f3840 11615 return;
dee91e82 11616 }
2ab95328 11617 }
527f3840
JK
11618
11619 /* dwarf_decode_line_header does not yet provide sufficient information.
11620 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11621 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11622 if (lh == NULL)
527f3840 11623 return;
4c8aa72d
PA
11624
11625 cu->line_header = lh.release ();
11626 cu->line_header_die_owner = die;
527f3840
JK
11627
11628 if (dwarf2_per_objfile->line_header_hash == NULL)
11629 slot = NULL;
11630 else
11631 {
11632 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11633 &line_header_local,
11634 line_header_local_hash, INSERT);
11635 gdb_assert (slot != NULL);
11636 }
11637 if (slot != NULL && *slot == NULL)
11638 {
11639 /* This newly decoded line number information unit will be owned
11640 by line_header_hash hash table. */
11641 *slot = cu->line_header;
4c8aa72d 11642 cu->line_header_die_owner = NULL;
527f3840
JK
11643 }
11644 else
11645 {
11646 /* We cannot free any current entry in (*slot) as that struct line_header
11647 may be already used by multiple CUs. Create only temporary decoded
11648 line_header for this CU - it may happen at most once for each line
11649 number information unit. And if we're not using line_header_hash
11650 then this is what we want as well. */
11651 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11652 }
11653 decode_mapping = (die->tag != DW_TAG_partial_unit);
11654 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11655 decode_mapping);
fff8551c 11656
2ab95328
TT
11657}
11658
95554aad 11659/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11660
c906108c 11661static void
e7c27a73 11662read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11663{
518817b3
SM
11664 struct dwarf2_per_objfile *dwarf2_per_objfile
11665 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11666 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11667 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11668 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11669 CORE_ADDR highpc = ((CORE_ADDR) 0);
11670 struct attribute *attr;
c906108c 11671 struct die_info *child_die;
e142c38c 11672 CORE_ADDR baseaddr;
6e70227d 11673
e142c38c 11674 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11675
fae299cd 11676 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11677
11678 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11679 from finish_block. */
2acceee2 11680 if (lowpc == ((CORE_ADDR) -1))
c906108c 11681 lowpc = highpc;
3e29f34a 11682 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11683
d721ba37 11684 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11685
95554aad 11686 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 11687
f4b8a18d
KW
11688 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11689 standardised yet. As a workaround for the language detection we fall
11690 back to the DW_AT_producer string. */
11691 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11692 cu->language = language_opencl;
11693
3019eac3
DE
11694 /* Similar hack for Go. */
11695 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11696 set_cu_language (DW_LANG_Go, cu);
11697
d721ba37 11698 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11699
11700 /* Decode line number information if present. We do this before
11701 processing child DIEs, so that the line header table is available
11702 for DW_AT_decl_file. */
d721ba37 11703 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11704
11705 /* Process all dies in compilation unit. */
11706 if (die->child != NULL)
11707 {
11708 child_die = die->child;
11709 while (child_die && child_die->tag)
11710 {
11711 process_die (child_die, cu);
11712 child_die = sibling_die (child_die);
11713 }
11714 }
11715
11716 /* Decode macro information, if present. Dwarf 2 macro information
11717 refers to information in the line number info statement program
11718 header, so we can only read it if we've read the header
11719 successfully. */
0af92d60
JK
11720 attr = dwarf2_attr (die, DW_AT_macros, cu);
11721 if (attr == NULL)
11722 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11723 if (attr && cu->line_header)
11724 {
11725 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11726 complaint (&symfile_complaints,
0af92d60 11727 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11728
43f3e411 11729 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11730 }
11731 else
11732 {
11733 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11734 if (attr && cu->line_header)
11735 {
11736 unsigned int macro_offset = DW_UNSND (attr);
11737
43f3e411 11738 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11739 }
11740 }
3019eac3
DE
11741}
11742
f4dc4d17
DE
11743/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11744 Create the set of symtabs used by this TU, or if this TU is sharing
11745 symtabs with another TU and the symtabs have already been created
11746 then restore those symtabs in the line header.
11747 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
11748
11749static void
f4dc4d17 11750setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 11751{
f4dc4d17
DE
11752 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11753 struct type_unit_group *tu_group;
11754 int first_time;
3019eac3 11755 struct attribute *attr;
9c541725 11756 unsigned int i;
0186c6a7 11757 struct signatured_type *sig_type;
3019eac3 11758
f4dc4d17 11759 gdb_assert (per_cu->is_debug_types);
0186c6a7 11760 sig_type = (struct signatured_type *) per_cu;
3019eac3 11761
f4dc4d17 11762 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 11763
f4dc4d17 11764 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11765 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
11766 if (sig_type->type_unit_group == NULL)
11767 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11768 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11769
11770 /* If we've already processed this stmt_list there's no real need to
11771 do it again, we could fake it and just recreate the part we need
11772 (file name,index -> symtab mapping). If data shows this optimization
11773 is useful we can do it then. */
43f3e411 11774 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11775
11776 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11777 debug info. */
fff8551c 11778 line_header_up lh;
f4dc4d17 11779 if (attr != NULL)
3019eac3 11780 {
9c541725 11781 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
11782 lh = dwarf_decode_line_header (line_offset, cu);
11783 }
11784 if (lh == NULL)
11785 {
11786 if (first_time)
11787 dwarf2_start_symtab (cu, "", NULL, 0);
11788 else
11789 {
11790 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 11791 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11792 }
f4dc4d17 11793 return;
3019eac3
DE
11794 }
11795
4c8aa72d
PA
11796 cu->line_header = lh.release ();
11797 cu->line_header_die_owner = die;
3019eac3 11798
f4dc4d17
DE
11799 if (first_time)
11800 {
43f3e411 11801 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 11802
1fd60fc0
DE
11803 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11804 still initializing it, and our caller (a few levels up)
11805 process_full_type_unit still needs to know if this is the first
11806 time. */
11807
4c8aa72d
PA
11808 tu_group->num_symtabs = cu->line_header->file_names.size ();
11809 tu_group->symtabs = XNEWVEC (struct symtab *,
11810 cu->line_header->file_names.size ());
3019eac3 11811
4c8aa72d 11812 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11813 {
4c8aa72d 11814 file_entry &fe = cu->line_header->file_names[i];
3019eac3 11815
4c8aa72d 11816 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 11817
f4dc4d17
DE
11818 if (current_subfile->symtab == NULL)
11819 {
4c8aa72d
PA
11820 /* NOTE: start_subfile will recognize when it's been
11821 passed a file it has already seen. So we can't
11822 assume there's a simple mapping from
11823 cu->line_header->file_names to subfiles, plus
11824 cu->line_header->file_names may contain dups. */
43f3e411
DE
11825 current_subfile->symtab
11826 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
11827 }
11828
8c43009f
PA
11829 fe.symtab = current_subfile->symtab;
11830 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11831 }
11832 }
11833 else
3019eac3 11834 {
0ab9ce85 11835 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11836
4c8aa72d 11837 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11838 {
4c8aa72d 11839 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 11840
4c8aa72d 11841 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11842 }
3019eac3
DE
11843 }
11844
f4dc4d17
DE
11845 /* The main symtab is allocated last. Type units don't have DW_AT_name
11846 so they don't have a "real" (so to speak) symtab anyway.
11847 There is later code that will assign the main symtab to all symbols
11848 that don't have one. We need to handle the case of a symbol with a
11849 missing symtab (DW_AT_decl_file) anyway. */
11850}
3019eac3 11851
f4dc4d17
DE
11852/* Process DW_TAG_type_unit.
11853 For TUs we want to skip the first top level sibling if it's not the
11854 actual type being defined by this TU. In this case the first top
11855 level sibling is there to provide context only. */
3019eac3 11856
f4dc4d17
DE
11857static void
11858read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11859{
11860 struct die_info *child_die;
3019eac3 11861
f4dc4d17
DE
11862 prepare_one_comp_unit (cu, die, language_minimal);
11863
11864 /* Initialize (or reinitialize) the machinery for building symtabs.
11865 We do this before processing child DIEs, so that the line header table
11866 is available for DW_AT_decl_file. */
11867 setup_type_unit_groups (die, cu);
11868
11869 if (die->child != NULL)
11870 {
11871 child_die = die->child;
11872 while (child_die && child_die->tag)
11873 {
11874 process_die (child_die, cu);
11875 child_die = sibling_die (child_die);
11876 }
11877 }
3019eac3
DE
11878}
11879\f
80626a55
DE
11880/* DWO/DWP files.
11881
11882 http://gcc.gnu.org/wiki/DebugFission
11883 http://gcc.gnu.org/wiki/DebugFissionDWP
11884
11885 To simplify handling of both DWO files ("object" files with the DWARF info)
11886 and DWP files (a file with the DWOs packaged up into one file), we treat
11887 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11888
11889static hashval_t
11890hash_dwo_file (const void *item)
11891{
9a3c8263 11892 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11893 hashval_t hash;
3019eac3 11894
a2ce51a0
DE
11895 hash = htab_hash_string (dwo_file->dwo_name);
11896 if (dwo_file->comp_dir != NULL)
11897 hash += htab_hash_string (dwo_file->comp_dir);
11898 return hash;
3019eac3
DE
11899}
11900
11901static int
11902eq_dwo_file (const void *item_lhs, const void *item_rhs)
11903{
9a3c8263
SM
11904 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11905 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11906
a2ce51a0
DE
11907 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11908 return 0;
11909 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11910 return lhs->comp_dir == rhs->comp_dir;
11911 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11912}
11913
11914/* Allocate a hash table for DWO files. */
11915
11916static htab_t
ed2dc618 11917allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11918{
3019eac3
DE
11919 return htab_create_alloc_ex (41,
11920 hash_dwo_file,
11921 eq_dwo_file,
11922 NULL,
11923 &objfile->objfile_obstack,
11924 hashtab_obstack_allocate,
11925 dummy_obstack_deallocate);
11926}
11927
80626a55
DE
11928/* Lookup DWO file DWO_NAME. */
11929
11930static void **
ed2dc618
SM
11931lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11932 const char *dwo_name,
11933 const char *comp_dir)
80626a55
DE
11934{
11935 struct dwo_file find_entry;
11936 void **slot;
11937
11938 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11939 dwarf2_per_objfile->dwo_files
11940 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55
DE
11941
11942 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
11943 find_entry.dwo_name = dwo_name;
11944 find_entry.comp_dir = comp_dir;
80626a55
DE
11945 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11946
11947 return slot;
11948}
11949
3019eac3
DE
11950static hashval_t
11951hash_dwo_unit (const void *item)
11952{
9a3c8263 11953 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11954
11955 /* This drops the top 32 bits of the id, but is ok for a hash. */
11956 return dwo_unit->signature;
11957}
11958
11959static int
11960eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11961{
9a3c8263
SM
11962 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11963 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11964
11965 /* The signature is assumed to be unique within the DWO file.
11966 So while object file CU dwo_id's always have the value zero,
11967 that's OK, assuming each object file DWO file has only one CU,
11968 and that's the rule for now. */
11969 return lhs->signature == rhs->signature;
11970}
11971
11972/* Allocate a hash table for DWO CUs,TUs.
11973 There is one of these tables for each of CUs,TUs for each DWO file. */
11974
11975static htab_t
11976allocate_dwo_unit_table (struct objfile *objfile)
11977{
11978 /* Start out with a pretty small number.
11979 Generally DWO files contain only one CU and maybe some TUs. */
11980 return htab_create_alloc_ex (3,
11981 hash_dwo_unit,
11982 eq_dwo_unit,
11983 NULL,
11984 &objfile->objfile_obstack,
11985 hashtab_obstack_allocate,
11986 dummy_obstack_deallocate);
11987}
11988
80626a55 11989/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11990
19c3d4c9 11991struct create_dwo_cu_data
3019eac3
DE
11992{
11993 struct dwo_file *dwo_file;
19c3d4c9 11994 struct dwo_unit dwo_unit;
3019eac3
DE
11995};
11996
19c3d4c9 11997/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11998
11999static void
19c3d4c9
DE
12000create_dwo_cu_reader (const struct die_reader_specs *reader,
12001 const gdb_byte *info_ptr,
12002 struct die_info *comp_unit_die,
12003 int has_children,
12004 void *datap)
3019eac3
DE
12005{
12006 struct dwarf2_cu *cu = reader->cu;
9c541725 12007 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 12008 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 12009 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 12010 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 12011 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 12012 struct attribute *attr;
3019eac3
DE
12013
12014 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
12015 if (attr == NULL)
12016 {
19c3d4c9
DE
12017 complaint (&symfile_complaints,
12018 _("Dwarf Error: debug entry at offset 0x%x is missing"
12019 " its dwo_id [in module %s]"),
9c541725 12020 to_underlying (sect_off), dwo_file->dwo_name);
3019eac3
DE
12021 return;
12022 }
12023
3019eac3
DE
12024 dwo_unit->dwo_file = dwo_file;
12025 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 12026 dwo_unit->section = section;
9c541725 12027 dwo_unit->sect_off = sect_off;
3019eac3
DE
12028 dwo_unit->length = cu->per_cu->length;
12029
b4f54984 12030 if (dwarf_read_debug)
4031ecc5 12031 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9c541725
PA
12032 to_underlying (sect_off),
12033 hex_string (dwo_unit->signature));
3019eac3
DE
12034}
12035
33c5cd75 12036/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 12037 Note: This function processes DWO files only, not DWP files. */
3019eac3 12038
33c5cd75 12039static void
ed2dc618
SM
12040create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12041 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 12042 htab_t &cus_htab)
3019eac3
DE
12043{
12044 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 12045 const gdb_byte *info_ptr, *end_ptr;
3019eac3 12046
33c5cd75
DB
12047 dwarf2_read_section (objfile, &section);
12048 info_ptr = section.buffer;
3019eac3
DE
12049
12050 if (info_ptr == NULL)
33c5cd75 12051 return;
3019eac3 12052
b4f54984 12053 if (dwarf_read_debug)
19c3d4c9
DE
12054 {
12055 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
12056 get_section_name (&section),
12057 get_section_file_name (&section));
19c3d4c9 12058 }
3019eac3 12059
33c5cd75 12060 end_ptr = info_ptr + section.size;
3019eac3
DE
12061 while (info_ptr < end_ptr)
12062 {
12063 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
12064 struct create_dwo_cu_data create_dwo_cu_data;
12065 struct dwo_unit *dwo_unit;
12066 void **slot;
12067 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 12068
19c3d4c9
DE
12069 memset (&create_dwo_cu_data.dwo_unit, 0,
12070 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 12071 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 12072 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 12073 per_cu.is_debug_types = 0;
33c5cd75
DB
12074 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12075 per_cu.section = &section;
c5ed0576 12076 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
12077
12078 init_cutu_and_read_dies_no_follow (
12079 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12080 info_ptr += per_cu.length;
12081
12082 // If the unit could not be parsed, skip it.
12083 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12084 continue;
3019eac3 12085
33c5cd75
DB
12086 if (cus_htab == NULL)
12087 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 12088
33c5cd75
DB
12089 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12090 *dwo_unit = create_dwo_cu_data.dwo_unit;
12091 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12092 gdb_assert (slot != NULL);
12093 if (*slot != NULL)
19c3d4c9 12094 {
33c5cd75
DB
12095 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12096 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 12097
33c5cd75
DB
12098 complaint (&symfile_complaints,
12099 _("debug cu entry at offset 0x%x is duplicate to"
12100 " the entry at offset 0x%x, signature %s"),
12101 to_underlying (sect_off), to_underlying (dup_sect_off),
12102 hex_string (dwo_unit->signature));
19c3d4c9 12103 }
33c5cd75 12104 *slot = (void *)dwo_unit;
3019eac3 12105 }
3019eac3
DE
12106}
12107
80626a55
DE
12108/* DWP file .debug_{cu,tu}_index section format:
12109 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12110
d2415c6c
DE
12111 DWP Version 1:
12112
80626a55
DE
12113 Both index sections have the same format, and serve to map a 64-bit
12114 signature to a set of section numbers. Each section begins with a header,
12115 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12116 indexes, and a pool of 32-bit section numbers. The index sections will be
12117 aligned at 8-byte boundaries in the file.
12118
d2415c6c
DE
12119 The index section header consists of:
12120
12121 V, 32 bit version number
12122 -, 32 bits unused
12123 N, 32 bit number of compilation units or type units in the index
12124 M, 32 bit number of slots in the hash table
80626a55 12125
d2415c6c 12126 Numbers are recorded using the byte order of the application binary.
80626a55 12127
d2415c6c
DE
12128 The hash table begins at offset 16 in the section, and consists of an array
12129 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12130 order of the application binary). Unused slots in the hash table are 0.
12131 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12132
d2415c6c
DE
12133 The parallel table begins immediately after the hash table
12134 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12135 array of 32-bit indexes (using the byte order of the application binary),
12136 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12137 table contains a 32-bit index into the pool of section numbers. For unused
12138 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12139
73869dc2
DE
12140 The pool of section numbers begins immediately following the hash table
12141 (at offset 16 + 12 * M from the beginning of the section). The pool of
12142 section numbers consists of an array of 32-bit words (using the byte order
12143 of the application binary). Each item in the array is indexed starting
12144 from 0. The hash table entry provides the index of the first section
12145 number in the set. Additional section numbers in the set follow, and the
12146 set is terminated by a 0 entry (section number 0 is not used in ELF).
12147
12148 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12149 section must be the first entry in the set, and the .debug_abbrev.dwo must
12150 be the second entry. Other members of the set may follow in any order.
12151
12152 ---
12153
12154 DWP Version 2:
12155
12156 DWP Version 2 combines all the .debug_info, etc. sections into one,
12157 and the entries in the index tables are now offsets into these sections.
12158 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12159 section.
12160
12161 Index Section Contents:
12162 Header
12163 Hash Table of Signatures dwp_hash_table.hash_table
12164 Parallel Table of Indices dwp_hash_table.unit_table
12165 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12166 Table of Section Sizes dwp_hash_table.v2.sizes
12167
12168 The index section header consists of:
12169
12170 V, 32 bit version number
12171 L, 32 bit number of columns in the table of section offsets
12172 N, 32 bit number of compilation units or type units in the index
12173 M, 32 bit number of slots in the hash table
12174
12175 Numbers are recorded using the byte order of the application binary.
12176
12177 The hash table has the same format as version 1.
12178 The parallel table of indices has the same format as version 1,
12179 except that the entries are origin-1 indices into the table of sections
12180 offsets and the table of section sizes.
12181
12182 The table of offsets begins immediately following the parallel table
12183 (at offset 16 + 12 * M from the beginning of the section). The table is
12184 a two-dimensional array of 32-bit words (using the byte order of the
12185 application binary), with L columns and N+1 rows, in row-major order.
12186 Each row in the array is indexed starting from 0. The first row provides
12187 a key to the remaining rows: each column in this row provides an identifier
12188 for a debug section, and the offsets in the same column of subsequent rows
12189 refer to that section. The section identifiers are:
12190
12191 DW_SECT_INFO 1 .debug_info.dwo
12192 DW_SECT_TYPES 2 .debug_types.dwo
12193 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12194 DW_SECT_LINE 4 .debug_line.dwo
12195 DW_SECT_LOC 5 .debug_loc.dwo
12196 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12197 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12198 DW_SECT_MACRO 8 .debug_macro.dwo
12199
12200 The offsets provided by the CU and TU index sections are the base offsets
12201 for the contributions made by each CU or TU to the corresponding section
12202 in the package file. Each CU and TU header contains an abbrev_offset
12203 field, used to find the abbreviations table for that CU or TU within the
12204 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12205 be interpreted as relative to the base offset given in the index section.
12206 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12207 should be interpreted as relative to the base offset for .debug_line.dwo,
12208 and offsets into other debug sections obtained from DWARF attributes should
12209 also be interpreted as relative to the corresponding base offset.
12210
12211 The table of sizes begins immediately following the table of offsets.
12212 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12213 with L columns and N rows, in row-major order. Each row in the array is
12214 indexed starting from 1 (row 0 is shared by the two tables).
12215
12216 ---
12217
12218 Hash table lookup is handled the same in version 1 and 2:
12219
12220 We assume that N and M will not exceed 2^32 - 1.
12221 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12222
d2415c6c
DE
12223 Given a 64-bit compilation unit signature or a type signature S, an entry
12224 in the hash table is located as follows:
80626a55 12225
d2415c6c
DE
12226 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12227 the low-order k bits all set to 1.
80626a55 12228
d2415c6c 12229 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12230
d2415c6c
DE
12231 3) If the hash table entry at index H matches the signature, use that
12232 entry. If the hash table entry at index H is unused (all zeroes),
12233 terminate the search: the signature is not present in the table.
80626a55 12234
d2415c6c 12235 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12236
d2415c6c 12237 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12238 to stop at an unused slot or find the match. */
80626a55
DE
12239
12240/* Create a hash table to map DWO IDs to their CU/TU entry in
12241 .debug_{info,types}.dwo in DWP_FILE.
12242 Returns NULL if there isn't one.
12243 Note: This function processes DWP files only, not DWO files. */
12244
12245static struct dwp_hash_table *
ed2dc618
SM
12246create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12247 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12248{
12249 struct objfile *objfile = dwarf2_per_objfile->objfile;
12250 bfd *dbfd = dwp_file->dbfd;
948f8e3d 12251 const gdb_byte *index_ptr, *index_end;
80626a55 12252 struct dwarf2_section_info *index;
73869dc2 12253 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12254 struct dwp_hash_table *htab;
12255
12256 if (is_debug_types)
12257 index = &dwp_file->sections.tu_index;
12258 else
12259 index = &dwp_file->sections.cu_index;
12260
12261 if (dwarf2_section_empty_p (index))
12262 return NULL;
12263 dwarf2_read_section (objfile, index);
12264
12265 index_ptr = index->buffer;
12266 index_end = index_ptr + index->size;
12267
12268 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12269 index_ptr += 4;
12270 if (version == 2)
12271 nr_columns = read_4_bytes (dbfd, index_ptr);
12272 else
12273 nr_columns = 0;
12274 index_ptr += 4;
80626a55
DE
12275 nr_units = read_4_bytes (dbfd, index_ptr);
12276 index_ptr += 4;
12277 nr_slots = read_4_bytes (dbfd, index_ptr);
12278 index_ptr += 4;
12279
73869dc2 12280 if (version != 1 && version != 2)
80626a55 12281 {
21aa081e 12282 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12283 " [in module %s]"),
21aa081e 12284 pulongest (version), dwp_file->name);
80626a55
DE
12285 }
12286 if (nr_slots != (nr_slots & -nr_slots))
12287 {
21aa081e 12288 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12289 " is not power of 2 [in module %s]"),
21aa081e 12290 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12291 }
12292
12293 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12294 htab->version = version;
12295 htab->nr_columns = nr_columns;
80626a55
DE
12296 htab->nr_units = nr_units;
12297 htab->nr_slots = nr_slots;
12298 htab->hash_table = index_ptr;
12299 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12300
12301 /* Exit early if the table is empty. */
12302 if (nr_slots == 0 || nr_units == 0
12303 || (version == 2 && nr_columns == 0))
12304 {
12305 /* All must be zero. */
12306 if (nr_slots != 0 || nr_units != 0
12307 || (version == 2 && nr_columns != 0))
12308 {
12309 complaint (&symfile_complaints,
12310 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12311 " all zero [in modules %s]"),
12312 dwp_file->name);
12313 }
12314 return htab;
12315 }
12316
12317 if (version == 1)
12318 {
12319 htab->section_pool.v1.indices =
12320 htab->unit_table + sizeof (uint32_t) * nr_slots;
12321 /* It's harder to decide whether the section is too small in v1.
12322 V1 is deprecated anyway so we punt. */
12323 }
12324 else
12325 {
12326 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12327 int *ids = htab->section_pool.v2.section_ids;
12328 /* Reverse map for error checking. */
12329 int ids_seen[DW_SECT_MAX + 1];
12330 int i;
12331
12332 if (nr_columns < 2)
12333 {
12334 error (_("Dwarf Error: bad DWP hash table, too few columns"
12335 " in section table [in module %s]"),
12336 dwp_file->name);
12337 }
12338 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12339 {
12340 error (_("Dwarf Error: bad DWP hash table, too many columns"
12341 " in section table [in module %s]"),
12342 dwp_file->name);
12343 }
12344 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12345 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12346 for (i = 0; i < nr_columns; ++i)
12347 {
12348 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12349
12350 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12351 {
12352 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12353 " in section table [in module %s]"),
12354 id, dwp_file->name);
12355 }
12356 if (ids_seen[id] != -1)
12357 {
12358 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12359 " id %d in section table [in module %s]"),
12360 id, dwp_file->name);
12361 }
12362 ids_seen[id] = i;
12363 ids[i] = id;
12364 }
12365 /* Must have exactly one info or types section. */
12366 if (((ids_seen[DW_SECT_INFO] != -1)
12367 + (ids_seen[DW_SECT_TYPES] != -1))
12368 != 1)
12369 {
12370 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12371 " DWO info/types section [in module %s]"),
12372 dwp_file->name);
12373 }
12374 /* Must have an abbrev section. */
12375 if (ids_seen[DW_SECT_ABBREV] == -1)
12376 {
12377 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12378 " section [in module %s]"),
12379 dwp_file->name);
12380 }
12381 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12382 htab->section_pool.v2.sizes =
12383 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12384 * nr_units * nr_columns);
12385 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12386 * nr_units * nr_columns))
12387 > index_end)
12388 {
12389 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12390 " [in module %s]"),
12391 dwp_file->name);
12392 }
12393 }
80626a55
DE
12394
12395 return htab;
12396}
12397
12398/* Update SECTIONS with the data from SECTP.
12399
12400 This function is like the other "locate" section routines that are
12401 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12402 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12403
12404 The result is non-zero for success, or zero if an error was found. */
12405
12406static int
73869dc2
DE
12407locate_v1_virtual_dwo_sections (asection *sectp,
12408 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12409{
12410 const struct dwop_section_names *names = &dwop_section_names;
12411
12412 if (section_is_p (sectp->name, &names->abbrev_dwo))
12413 {
12414 /* There can be only one. */
049412e3 12415 if (sections->abbrev.s.section != NULL)
80626a55 12416 return 0;
049412e3 12417 sections->abbrev.s.section = sectp;
80626a55
DE
12418 sections->abbrev.size = bfd_get_section_size (sectp);
12419 }
12420 else if (section_is_p (sectp->name, &names->info_dwo)
12421 || section_is_p (sectp->name, &names->types_dwo))
12422 {
12423 /* There can be only one. */
049412e3 12424 if (sections->info_or_types.s.section != NULL)
80626a55 12425 return 0;
049412e3 12426 sections->info_or_types.s.section = sectp;
80626a55
DE
12427 sections->info_or_types.size = bfd_get_section_size (sectp);
12428 }
12429 else if (section_is_p (sectp->name, &names->line_dwo))
12430 {
12431 /* There can be only one. */
049412e3 12432 if (sections->line.s.section != NULL)
80626a55 12433 return 0;
049412e3 12434 sections->line.s.section = sectp;
80626a55
DE
12435 sections->line.size = bfd_get_section_size (sectp);
12436 }
12437 else if (section_is_p (sectp->name, &names->loc_dwo))
12438 {
12439 /* There can be only one. */
049412e3 12440 if (sections->loc.s.section != NULL)
80626a55 12441 return 0;
049412e3 12442 sections->loc.s.section = sectp;
80626a55
DE
12443 sections->loc.size = bfd_get_section_size (sectp);
12444 }
12445 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12446 {
12447 /* There can be only one. */
049412e3 12448 if (sections->macinfo.s.section != NULL)
80626a55 12449 return 0;
049412e3 12450 sections->macinfo.s.section = sectp;
80626a55
DE
12451 sections->macinfo.size = bfd_get_section_size (sectp);
12452 }
12453 else if (section_is_p (sectp->name, &names->macro_dwo))
12454 {
12455 /* There can be only one. */
049412e3 12456 if (sections->macro.s.section != NULL)
80626a55 12457 return 0;
049412e3 12458 sections->macro.s.section = sectp;
80626a55
DE
12459 sections->macro.size = bfd_get_section_size (sectp);
12460 }
12461 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12462 {
12463 /* There can be only one. */
049412e3 12464 if (sections->str_offsets.s.section != NULL)
80626a55 12465 return 0;
049412e3 12466 sections->str_offsets.s.section = sectp;
80626a55
DE
12467 sections->str_offsets.size = bfd_get_section_size (sectp);
12468 }
12469 else
12470 {
12471 /* No other kind of section is valid. */
12472 return 0;
12473 }
12474
12475 return 1;
12476}
12477
73869dc2
DE
12478/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12479 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12480 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12481 This is for DWP version 1 files. */
80626a55
DE
12482
12483static struct dwo_unit *
ed2dc618
SM
12484create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12485 struct dwp_file *dwp_file,
73869dc2
DE
12486 uint32_t unit_index,
12487 const char *comp_dir,
12488 ULONGEST signature, int is_debug_types)
80626a55
DE
12489{
12490 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12491 const struct dwp_hash_table *dwp_htab =
12492 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
12493 bfd *dbfd = dwp_file->dbfd;
12494 const char *kind = is_debug_types ? "TU" : "CU";
12495 struct dwo_file *dwo_file;
12496 struct dwo_unit *dwo_unit;
73869dc2 12497 struct virtual_v1_dwo_sections sections;
80626a55 12498 void **dwo_file_slot;
80626a55
DE
12499 int i;
12500
73869dc2
DE
12501 gdb_assert (dwp_file->version == 1);
12502
b4f54984 12503 if (dwarf_read_debug)
80626a55 12504 {
73869dc2 12505 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12506 kind,
73869dc2 12507 pulongest (unit_index), hex_string (signature),
80626a55
DE
12508 dwp_file->name);
12509 }
12510
19ac8c2e 12511 /* Fetch the sections of this DWO unit.
80626a55
DE
12512 Put a limit on the number of sections we look for so that bad data
12513 doesn't cause us to loop forever. */
12514
73869dc2 12515#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12516 (1 /* .debug_info or .debug_types */ \
12517 + 1 /* .debug_abbrev */ \
12518 + 1 /* .debug_line */ \
12519 + 1 /* .debug_loc */ \
12520 + 1 /* .debug_str_offsets */ \
19ac8c2e 12521 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12522 + 1 /* trailing zero */)
12523
12524 memset (&sections, 0, sizeof (sections));
80626a55 12525
73869dc2 12526 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12527 {
12528 asection *sectp;
12529 uint32_t section_nr =
12530 read_4_bytes (dbfd,
73869dc2
DE
12531 dwp_htab->section_pool.v1.indices
12532 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12533
12534 if (section_nr == 0)
12535 break;
12536 if (section_nr >= dwp_file->num_sections)
12537 {
12538 error (_("Dwarf Error: bad DWP hash table, section number too large"
12539 " [in module %s]"),
12540 dwp_file->name);
12541 }
12542
12543 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12544 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12545 {
12546 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12547 " [in module %s]"),
12548 dwp_file->name);
12549 }
12550 }
12551
12552 if (i < 2
a32a8923
DE
12553 || dwarf2_section_empty_p (&sections.info_or_types)
12554 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12555 {
12556 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12557 " [in module %s]"),
12558 dwp_file->name);
12559 }
73869dc2 12560 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12561 {
12562 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12563 " [in module %s]"),
12564 dwp_file->name);
12565 }
12566
12567 /* It's easier for the rest of the code if we fake a struct dwo_file and
12568 have dwo_unit "live" in that. At least for now.
12569
12570 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12571 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12572 file, we can combine them back into a virtual DWO file to save space
12573 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12574 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12575
791afaa2
TT
12576 std::string virtual_dwo_name =
12577 string_printf ("virtual-dwo/%d-%d-%d-%d",
12578 get_section_id (&sections.abbrev),
12579 get_section_id (&sections.line),
12580 get_section_id (&sections.loc),
12581 get_section_id (&sections.str_offsets));
80626a55 12582 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12583 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12584 virtual_dwo_name.c_str (),
12585 comp_dir);
80626a55
DE
12586 /* Create one if necessary. */
12587 if (*dwo_file_slot == NULL)
12588 {
b4f54984 12589 if (dwarf_read_debug)
80626a55
DE
12590 {
12591 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12592 virtual_dwo_name.c_str ());
80626a55
DE
12593 }
12594 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12595 dwo_file->dwo_name
12596 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12597 virtual_dwo_name.c_str (),
12598 virtual_dwo_name.size ());
0ac5b59e 12599 dwo_file->comp_dir = comp_dir;
80626a55
DE
12600 dwo_file->sections.abbrev = sections.abbrev;
12601 dwo_file->sections.line = sections.line;
12602 dwo_file->sections.loc = sections.loc;
12603 dwo_file->sections.macinfo = sections.macinfo;
12604 dwo_file->sections.macro = sections.macro;
12605 dwo_file->sections.str_offsets = sections.str_offsets;
12606 /* The "str" section is global to the entire DWP file. */
12607 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12608 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12609 there's no need to record it in dwo_file.
12610 Also, we can't simply record type sections in dwo_file because
12611 we record a pointer into the vector in dwo_unit. As we collect more
12612 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12613 for it, invalidating all copies of pointers into the previous
12614 contents. */
80626a55
DE
12615 *dwo_file_slot = dwo_file;
12616 }
12617 else
12618 {
b4f54984 12619 if (dwarf_read_debug)
80626a55
DE
12620 {
12621 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12622 virtual_dwo_name.c_str ());
80626a55 12623 }
9a3c8263 12624 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12625 }
80626a55
DE
12626
12627 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12628 dwo_unit->dwo_file = dwo_file;
12629 dwo_unit->signature = signature;
8d749320
SM
12630 dwo_unit->section =
12631 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12632 *dwo_unit->section = sections.info_or_types;
57d63ce2 12633 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12634
12635 return dwo_unit;
12636}
12637
73869dc2
DE
12638/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12639 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12640 piece within that section used by a TU/CU, return a virtual section
12641 of just that piece. */
12642
12643static struct dwarf2_section_info
ed2dc618
SM
12644create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12645 struct dwarf2_section_info *section,
73869dc2
DE
12646 bfd_size_type offset, bfd_size_type size)
12647{
12648 struct dwarf2_section_info result;
12649 asection *sectp;
12650
12651 gdb_assert (section != NULL);
12652 gdb_assert (!section->is_virtual);
12653
12654 memset (&result, 0, sizeof (result));
12655 result.s.containing_section = section;
12656 result.is_virtual = 1;
12657
12658 if (size == 0)
12659 return result;
12660
12661 sectp = get_section_bfd_section (section);
12662
12663 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12664 bounds of the real section. This is a pretty-rare event, so just
12665 flag an error (easier) instead of a warning and trying to cope. */
12666 if (sectp == NULL
12667 || offset + size > bfd_get_section_size (sectp))
12668 {
73869dc2
DE
12669 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12670 " in section %s [in module %s]"),
12671 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12672 objfile_name (dwarf2_per_objfile->objfile));
12673 }
12674
12675 result.virtual_offset = offset;
12676 result.size = size;
12677 return result;
12678}
12679
12680/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12681 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12682 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12683 This is for DWP version 2 files. */
12684
12685static struct dwo_unit *
ed2dc618
SM
12686create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12687 struct dwp_file *dwp_file,
73869dc2
DE
12688 uint32_t unit_index,
12689 const char *comp_dir,
12690 ULONGEST signature, int is_debug_types)
12691{
12692 struct objfile *objfile = dwarf2_per_objfile->objfile;
12693 const struct dwp_hash_table *dwp_htab =
12694 is_debug_types ? dwp_file->tus : dwp_file->cus;
12695 bfd *dbfd = dwp_file->dbfd;
12696 const char *kind = is_debug_types ? "TU" : "CU";
12697 struct dwo_file *dwo_file;
12698 struct dwo_unit *dwo_unit;
12699 struct virtual_v2_dwo_sections sections;
12700 void **dwo_file_slot;
73869dc2
DE
12701 int i;
12702
12703 gdb_assert (dwp_file->version == 2);
12704
b4f54984 12705 if (dwarf_read_debug)
73869dc2
DE
12706 {
12707 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12708 kind,
12709 pulongest (unit_index), hex_string (signature),
12710 dwp_file->name);
12711 }
12712
12713 /* Fetch the section offsets of this DWO unit. */
12714
12715 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12716
12717 for (i = 0; i < dwp_htab->nr_columns; ++i)
12718 {
12719 uint32_t offset = read_4_bytes (dbfd,
12720 dwp_htab->section_pool.v2.offsets
12721 + (((unit_index - 1) * dwp_htab->nr_columns
12722 + i)
12723 * sizeof (uint32_t)));
12724 uint32_t size = read_4_bytes (dbfd,
12725 dwp_htab->section_pool.v2.sizes
12726 + (((unit_index - 1) * dwp_htab->nr_columns
12727 + i)
12728 * sizeof (uint32_t)));
12729
12730 switch (dwp_htab->section_pool.v2.section_ids[i])
12731 {
12732 case DW_SECT_INFO:
12733 case DW_SECT_TYPES:
12734 sections.info_or_types_offset = offset;
12735 sections.info_or_types_size = size;
12736 break;
12737 case DW_SECT_ABBREV:
12738 sections.abbrev_offset = offset;
12739 sections.abbrev_size = size;
12740 break;
12741 case DW_SECT_LINE:
12742 sections.line_offset = offset;
12743 sections.line_size = size;
12744 break;
12745 case DW_SECT_LOC:
12746 sections.loc_offset = offset;
12747 sections.loc_size = size;
12748 break;
12749 case DW_SECT_STR_OFFSETS:
12750 sections.str_offsets_offset = offset;
12751 sections.str_offsets_size = size;
12752 break;
12753 case DW_SECT_MACINFO:
12754 sections.macinfo_offset = offset;
12755 sections.macinfo_size = size;
12756 break;
12757 case DW_SECT_MACRO:
12758 sections.macro_offset = offset;
12759 sections.macro_size = size;
12760 break;
12761 }
12762 }
12763
12764 /* It's easier for the rest of the code if we fake a struct dwo_file and
12765 have dwo_unit "live" in that. At least for now.
12766
12767 The DWP file can be made up of a random collection of CUs and TUs.
12768 However, for each CU + set of TUs that came from the same original DWO
12769 file, we can combine them back into a virtual DWO file to save space
12770 (fewer struct dwo_file objects to allocate). Remember that for really
12771 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12772
791afaa2
TT
12773 std::string virtual_dwo_name =
12774 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12775 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12776 (long) (sections.line_size ? sections.line_offset : 0),
12777 (long) (sections.loc_size ? sections.loc_offset : 0),
12778 (long) (sections.str_offsets_size
12779 ? sections.str_offsets_offset : 0));
73869dc2 12780 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12781 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12782 virtual_dwo_name.c_str (),
12783 comp_dir);
73869dc2
DE
12784 /* Create one if necessary. */
12785 if (*dwo_file_slot == NULL)
12786 {
b4f54984 12787 if (dwarf_read_debug)
73869dc2
DE
12788 {
12789 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12790 virtual_dwo_name.c_str ());
73869dc2
DE
12791 }
12792 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12793 dwo_file->dwo_name
12794 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12795 virtual_dwo_name.c_str (),
12796 virtual_dwo_name.size ());
73869dc2
DE
12797 dwo_file->comp_dir = comp_dir;
12798 dwo_file->sections.abbrev =
ed2dc618 12799 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12800 sections.abbrev_offset, sections.abbrev_size);
12801 dwo_file->sections.line =
ed2dc618 12802 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12803 sections.line_offset, sections.line_size);
12804 dwo_file->sections.loc =
ed2dc618 12805 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12806 sections.loc_offset, sections.loc_size);
12807 dwo_file->sections.macinfo =
ed2dc618 12808 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12809 sections.macinfo_offset, sections.macinfo_size);
12810 dwo_file->sections.macro =
ed2dc618 12811 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12812 sections.macro_offset, sections.macro_size);
12813 dwo_file->sections.str_offsets =
ed2dc618
SM
12814 create_dwp_v2_section (dwarf2_per_objfile,
12815 &dwp_file->sections.str_offsets,
73869dc2
DE
12816 sections.str_offsets_offset,
12817 sections.str_offsets_size);
12818 /* The "str" section is global to the entire DWP file. */
12819 dwo_file->sections.str = dwp_file->sections.str;
12820 /* The info or types section is assigned below to dwo_unit,
12821 there's no need to record it in dwo_file.
12822 Also, we can't simply record type sections in dwo_file because
12823 we record a pointer into the vector in dwo_unit. As we collect more
12824 types we'll grow the vector and eventually have to reallocate space
12825 for it, invalidating all copies of pointers into the previous
12826 contents. */
12827 *dwo_file_slot = dwo_file;
12828 }
12829 else
12830 {
b4f54984 12831 if (dwarf_read_debug)
73869dc2
DE
12832 {
12833 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12834 virtual_dwo_name.c_str ());
73869dc2 12835 }
9a3c8263 12836 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12837 }
73869dc2
DE
12838
12839 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12840 dwo_unit->dwo_file = dwo_file;
12841 dwo_unit->signature = signature;
8d749320
SM
12842 dwo_unit->section =
12843 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12844 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12845 is_debug_types
73869dc2
DE
12846 ? &dwp_file->sections.types
12847 : &dwp_file->sections.info,
12848 sections.info_or_types_offset,
12849 sections.info_or_types_size);
12850 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12851
12852 return dwo_unit;
12853}
12854
57d63ce2
DE
12855/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12856 Returns NULL if the signature isn't found. */
80626a55
DE
12857
12858static struct dwo_unit *
ed2dc618
SM
12859lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12860 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12861 ULONGEST signature, int is_debug_types)
80626a55 12862{
57d63ce2
DE
12863 const struct dwp_hash_table *dwp_htab =
12864 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 12865 bfd *dbfd = dwp_file->dbfd;
57d63ce2 12866 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12867 uint32_t hash = signature & mask;
12868 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12869 unsigned int i;
12870 void **slot;
870f88f7 12871 struct dwo_unit find_dwo_cu;
80626a55
DE
12872
12873 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12874 find_dwo_cu.signature = signature;
19ac8c2e
DE
12875 slot = htab_find_slot (is_debug_types
12876 ? dwp_file->loaded_tus
12877 : dwp_file->loaded_cus,
12878 &find_dwo_cu, INSERT);
80626a55
DE
12879
12880 if (*slot != NULL)
9a3c8263 12881 return (struct dwo_unit *) *slot;
80626a55
DE
12882
12883 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12884 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12885 {
12886 ULONGEST signature_in_table;
12887
12888 signature_in_table =
57d63ce2 12889 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12890 if (signature_in_table == signature)
12891 {
57d63ce2
DE
12892 uint32_t unit_index =
12893 read_4_bytes (dbfd,
12894 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12895
73869dc2
DE
12896 if (dwp_file->version == 1)
12897 {
ed2dc618
SM
12898 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12899 dwp_file, unit_index,
73869dc2
DE
12900 comp_dir, signature,
12901 is_debug_types);
12902 }
12903 else
12904 {
ed2dc618
SM
12905 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12906 dwp_file, unit_index,
73869dc2
DE
12907 comp_dir, signature,
12908 is_debug_types);
12909 }
9a3c8263 12910 return (struct dwo_unit *) *slot;
80626a55
DE
12911 }
12912 if (signature_in_table == 0)
12913 return NULL;
12914 hash = (hash + hash2) & mask;
12915 }
12916
12917 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12918 " [in module %s]"),
12919 dwp_file->name);
12920}
12921
ab5088bf 12922/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12923 Open the file specified by FILE_NAME and hand it off to BFD for
12924 preliminary analysis. Return a newly initialized bfd *, which
12925 includes a canonicalized copy of FILE_NAME.
80626a55 12926 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12927 SEARCH_CWD is true if the current directory is to be searched.
12928 It will be searched before debug-file-directory.
13aaf454
DE
12929 If successful, the file is added to the bfd include table of the
12930 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12931 If unable to find/open the file, return NULL.
3019eac3
DE
12932 NOTE: This function is derived from symfile_bfd_open. */
12933
192b62ce 12934static gdb_bfd_ref_ptr
ed2dc618
SM
12935try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12936 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12937{
80626a55 12938 int desc, flags;
3019eac3 12939 char *absolute_name;
9c02c129
DE
12940 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12941 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12942 to debug_file_directory. */
12943 char *search_path;
12944 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12945
6ac97d4c
DE
12946 if (search_cwd)
12947 {
12948 if (*debug_file_directory != '\0')
12949 search_path = concat (".", dirname_separator_string,
b36cec19 12950 debug_file_directory, (char *) NULL);
6ac97d4c
DE
12951 else
12952 search_path = xstrdup (".");
12953 }
9c02c129 12954 else
6ac97d4c 12955 search_path = xstrdup (debug_file_directory);
3019eac3 12956
492c0ab7 12957 flags = OPF_RETURN_REALPATH;
80626a55
DE
12958 if (is_dwp)
12959 flags |= OPF_SEARCH_IN_PATH;
9c02c129 12960 desc = openp (search_path, flags, file_name,
3019eac3 12961 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 12962 xfree (search_path);
3019eac3
DE
12963 if (desc < 0)
12964 return NULL;
12965
192b62ce 12966 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 12967 xfree (absolute_name);
9c02c129
DE
12968 if (sym_bfd == NULL)
12969 return NULL;
192b62ce 12970 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12971
192b62ce
TT
12972 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12973 return NULL;
3019eac3 12974
13aaf454
DE
12975 /* Success. Record the bfd as having been included by the objfile's bfd.
12976 This is important because things like demangled_names_hash lives in the
12977 objfile's per_bfd space and may have references to things like symbol
12978 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12979 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12980
3019eac3
DE
12981 return sym_bfd;
12982}
12983
ab5088bf 12984/* Try to open DWO file FILE_NAME.
3019eac3
DE
12985 COMP_DIR is the DW_AT_comp_dir attribute.
12986 The result is the bfd handle of the file.
12987 If there is a problem finding or opening the file, return NULL.
12988 Upon success, the canonicalized path of the file is stored in the bfd,
12989 same as symfile_bfd_open. */
12990
192b62ce 12991static gdb_bfd_ref_ptr
ed2dc618
SM
12992open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12993 const char *file_name, const char *comp_dir)
3019eac3 12994{
80626a55 12995 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12996 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12997 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12998
12999 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
13000
13001 if (comp_dir != NULL)
13002 {
b36cec19
PA
13003 char *path_to_try = concat (comp_dir, SLASH_STRING,
13004 file_name, (char *) NULL);
3019eac3
DE
13005
13006 /* NOTE: If comp_dir is a relative path, this will also try the
13007 search path, which seems useful. */
ed2dc618
SM
13008 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
13009 path_to_try,
13010 0 /*is_dwp*/,
192b62ce 13011 1 /*search_cwd*/));
3019eac3
DE
13012 xfree (path_to_try);
13013 if (abfd != NULL)
13014 return abfd;
13015 }
13016
13017 /* That didn't work, try debug-file-directory, which, despite its name,
13018 is a list of paths. */
13019
13020 if (*debug_file_directory == '\0')
13021 return NULL;
13022
ed2dc618
SM
13023 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13024 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
13025}
13026
80626a55
DE
13027/* This function is mapped across the sections and remembers the offset and
13028 size of each of the DWO debugging sections we are interested in. */
13029
13030static void
13031dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
13032{
9a3c8263 13033 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
13034 const struct dwop_section_names *names = &dwop_section_names;
13035
13036 if (section_is_p (sectp->name, &names->abbrev_dwo))
13037 {
049412e3 13038 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
13039 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
13040 }
13041 else if (section_is_p (sectp->name, &names->info_dwo))
13042 {
049412e3 13043 dwo_sections->info.s.section = sectp;
80626a55
DE
13044 dwo_sections->info.size = bfd_get_section_size (sectp);
13045 }
13046 else if (section_is_p (sectp->name, &names->line_dwo))
13047 {
049412e3 13048 dwo_sections->line.s.section = sectp;
80626a55
DE
13049 dwo_sections->line.size = bfd_get_section_size (sectp);
13050 }
13051 else if (section_is_p (sectp->name, &names->loc_dwo))
13052 {
049412e3 13053 dwo_sections->loc.s.section = sectp;
80626a55
DE
13054 dwo_sections->loc.size = bfd_get_section_size (sectp);
13055 }
13056 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13057 {
049412e3 13058 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
13059 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
13060 }
13061 else if (section_is_p (sectp->name, &names->macro_dwo))
13062 {
049412e3 13063 dwo_sections->macro.s.section = sectp;
80626a55
DE
13064 dwo_sections->macro.size = bfd_get_section_size (sectp);
13065 }
13066 else if (section_is_p (sectp->name, &names->str_dwo))
13067 {
049412e3 13068 dwo_sections->str.s.section = sectp;
80626a55
DE
13069 dwo_sections->str.size = bfd_get_section_size (sectp);
13070 }
13071 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13072 {
049412e3 13073 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
13074 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
13075 }
13076 else if (section_is_p (sectp->name, &names->types_dwo))
13077 {
13078 struct dwarf2_section_info type_section;
13079
13080 memset (&type_section, 0, sizeof (type_section));
049412e3 13081 type_section.s.section = sectp;
80626a55
DE
13082 type_section.size = bfd_get_section_size (sectp);
13083 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
13084 &type_section);
13085 }
13086}
13087
ab5088bf 13088/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 13089 by PER_CU. This is for the non-DWP case.
80626a55 13090 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
13091
13092static struct dwo_file *
0ac5b59e
DE
13093open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13094 const char *dwo_name, const char *comp_dir)
3019eac3 13095{
ed2dc618 13096 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 13097 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13098 struct dwo_file *dwo_file;
3019eac3
DE
13099 struct cleanup *cleanups;
13100
ed2dc618 13101 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
80626a55
DE
13102 if (dbfd == NULL)
13103 {
b4f54984 13104 if (dwarf_read_debug)
80626a55
DE
13105 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13106 return NULL;
13107 }
13108 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
13109 dwo_file->dwo_name = dwo_name;
13110 dwo_file->comp_dir = comp_dir;
192b62ce 13111 dwo_file->dbfd = dbfd.release ();
3019eac3 13112
ed2dc618
SM
13113 free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
13114 cleanup_data->dwo_file = dwo_file;
13115 cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
13116
13117 cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
3019eac3 13118
192b62ce
TT
13119 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13120 &dwo_file->sections);
3019eac3 13121
ed2dc618
SM
13122 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13123 dwo_file->cus);
3019eac3 13124
ed2dc618
SM
13125 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
13126 dwo_file->sections.types, dwo_file->tus);
3019eac3
DE
13127
13128 discard_cleanups (cleanups);
13129
b4f54984 13130 if (dwarf_read_debug)
80626a55
DE
13131 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13132
3019eac3
DE
13133 return dwo_file;
13134}
13135
80626a55 13136/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13137 size of each of the DWP debugging sections common to version 1 and 2 that
13138 we are interested in. */
3019eac3 13139
80626a55 13140static void
73869dc2
DE
13141dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13142 void *dwp_file_ptr)
3019eac3 13143{
9a3c8263 13144 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13145 const struct dwop_section_names *names = &dwop_section_names;
13146 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13147
80626a55 13148 /* Record the ELF section number for later lookup: this is what the
73869dc2 13149 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13150 gdb_assert (elf_section_nr < dwp_file->num_sections);
13151 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13152
80626a55
DE
13153 /* Look for specific sections that we need. */
13154 if (section_is_p (sectp->name, &names->str_dwo))
13155 {
049412e3 13156 dwp_file->sections.str.s.section = sectp;
80626a55
DE
13157 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13158 }
13159 else if (section_is_p (sectp->name, &names->cu_index))
13160 {
049412e3 13161 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
13162 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13163 }
13164 else if (section_is_p (sectp->name, &names->tu_index))
13165 {
049412e3 13166 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
13167 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13168 }
13169}
3019eac3 13170
73869dc2
DE
13171/* This function is mapped across the sections and remembers the offset and
13172 size of each of the DWP version 2 debugging sections that we are interested
13173 in. This is split into a separate function because we don't know if we
13174 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13175
13176static void
13177dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13178{
9a3c8263 13179 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13180 const struct dwop_section_names *names = &dwop_section_names;
13181 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13182
13183 /* Record the ELF section number for later lookup: this is what the
13184 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13185 gdb_assert (elf_section_nr < dwp_file->num_sections);
13186 dwp_file->elf_sections[elf_section_nr] = sectp;
13187
13188 /* Look for specific sections that we need. */
13189 if (section_is_p (sectp->name, &names->abbrev_dwo))
13190 {
049412e3 13191 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
13192 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13193 }
13194 else if (section_is_p (sectp->name, &names->info_dwo))
13195 {
049412e3 13196 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
13197 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13198 }
13199 else if (section_is_p (sectp->name, &names->line_dwo))
13200 {
049412e3 13201 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
13202 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13203 }
13204 else if (section_is_p (sectp->name, &names->loc_dwo))
13205 {
049412e3 13206 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
13207 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13208 }
13209 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13210 {
049412e3 13211 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
13212 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13213 }
13214 else if (section_is_p (sectp->name, &names->macro_dwo))
13215 {
049412e3 13216 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
13217 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13218 }
13219 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13220 {
049412e3 13221 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
13222 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13223 }
13224 else if (section_is_p (sectp->name, &names->types_dwo))
13225 {
049412e3 13226 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
13227 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13228 }
13229}
13230
80626a55 13231/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13232
80626a55
DE
13233static hashval_t
13234hash_dwp_loaded_cutus (const void *item)
13235{
9a3c8263 13236 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13237
80626a55
DE
13238 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13239 return dwo_unit->signature;
3019eac3
DE
13240}
13241
80626a55 13242/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13243
80626a55
DE
13244static int
13245eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13246{
9a3c8263
SM
13247 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13248 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13249
80626a55
DE
13250 return dua->signature == dub->signature;
13251}
3019eac3 13252
80626a55 13253/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13254
80626a55
DE
13255static htab_t
13256allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13257{
13258 return htab_create_alloc_ex (3,
13259 hash_dwp_loaded_cutus,
13260 eq_dwp_loaded_cutus,
13261 NULL,
13262 &objfile->objfile_obstack,
13263 hashtab_obstack_allocate,
13264 dummy_obstack_deallocate);
13265}
3019eac3 13266
ab5088bf
DE
13267/* Try to open DWP file FILE_NAME.
13268 The result is the bfd handle of the file.
13269 If there is a problem finding or opening the file, return NULL.
13270 Upon success, the canonicalized path of the file is stored in the bfd,
13271 same as symfile_bfd_open. */
13272
192b62ce 13273static gdb_bfd_ref_ptr
ed2dc618
SM
13274open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13275 const char *file_name)
ab5088bf 13276{
ed2dc618
SM
13277 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13278 1 /*is_dwp*/,
192b62ce 13279 1 /*search_cwd*/));
6ac97d4c
DE
13280 if (abfd != NULL)
13281 return abfd;
13282
13283 /* Work around upstream bug 15652.
13284 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13285 [Whether that's a "bug" is debatable, but it is getting in our way.]
13286 We have no real idea where the dwp file is, because gdb's realpath-ing
13287 of the executable's path may have discarded the needed info.
13288 [IWBN if the dwp file name was recorded in the executable, akin to
13289 .gnu_debuglink, but that doesn't exist yet.]
13290 Strip the directory from FILE_NAME and search again. */
13291 if (*debug_file_directory != '\0')
13292 {
13293 /* Don't implicitly search the current directory here.
13294 If the user wants to search "." to handle this case,
13295 it must be added to debug-file-directory. */
ed2dc618
SM
13296 return try_open_dwop_file (dwarf2_per_objfile,
13297 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13298 0 /*search_cwd*/);
13299 }
13300
13301 return NULL;
ab5088bf
DE
13302}
13303
80626a55
DE
13304/* Initialize the use of the DWP file for the current objfile.
13305 By convention the name of the DWP file is ${objfile}.dwp.
13306 The result is NULL if it can't be found. */
a766d390 13307
80626a55 13308static struct dwp_file *
ed2dc618 13309open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13310{
13311 struct objfile *objfile = dwarf2_per_objfile->objfile;
13312 struct dwp_file *dwp_file;
80626a55 13313
82bf32bc
JK
13314 /* Try to find first .dwp for the binary file before any symbolic links
13315 resolving. */
6c447423
DE
13316
13317 /* If the objfile is a debug file, find the name of the real binary
13318 file and get the name of dwp file from there. */
d721ba37 13319 std::string dwp_name;
6c447423
DE
13320 if (objfile->separate_debug_objfile_backlink != NULL)
13321 {
13322 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13323 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13324
d721ba37 13325 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13326 }
13327 else
d721ba37
PA
13328 dwp_name = objfile->original_name;
13329
13330 dwp_name += ".dwp";
80626a55 13331
ed2dc618 13332 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13333 if (dbfd == NULL
13334 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13335 {
13336 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13337 dwp_name = objfile_name (objfile);
13338 dwp_name += ".dwp";
ed2dc618 13339 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13340 }
13341
80626a55
DE
13342 if (dbfd == NULL)
13343 {
b4f54984 13344 if (dwarf_read_debug)
d721ba37 13345 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 13346 return NULL;
3019eac3 13347 }
80626a55 13348 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
13349 dwp_file->name = bfd_get_filename (dbfd.get ());
13350 dwp_file->dbfd = dbfd.release ();
c906108c 13351
80626a55 13352 /* +1: section 0 is unused */
192b62ce 13353 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
13354 dwp_file->elf_sections =
13355 OBSTACK_CALLOC (&objfile->objfile_obstack,
13356 dwp_file->num_sections, asection *);
13357
192b62ce
TT
13358 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13359 dwp_file);
80626a55 13360
ed2dc618 13361 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
80626a55 13362
ed2dc618 13363 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
80626a55 13364
73869dc2 13365 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13366 if (dwp_file->cus && dwp_file->tus
13367 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13368 {
13369 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13370 pretty bizarre. We use pulongest here because that's the established
4d65956b 13371 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13372 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13373 " TU version %s [in DWP file %s]"),
13374 pulongest (dwp_file->cus->version),
d721ba37 13375 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13376 }
08302ed2
DE
13377
13378 if (dwp_file->cus)
13379 dwp_file->version = dwp_file->cus->version;
13380 else if (dwp_file->tus)
13381 dwp_file->version = dwp_file->tus->version;
13382 else
13383 dwp_file->version = 2;
73869dc2
DE
13384
13385 if (dwp_file->version == 2)
192b62ce
TT
13386 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13387 dwp_file);
73869dc2 13388
19ac8c2e
DE
13389 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13390 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13391
b4f54984 13392 if (dwarf_read_debug)
80626a55
DE
13393 {
13394 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13395 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13396 " %s CUs, %s TUs\n",
13397 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13398 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13399 }
13400
13401 return dwp_file;
3019eac3 13402}
c906108c 13403
ab5088bf
DE
13404/* Wrapper around open_and_init_dwp_file, only open it once. */
13405
13406static struct dwp_file *
ed2dc618 13407get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13408{
13409 if (! dwarf2_per_objfile->dwp_checked)
13410 {
ed2dc618
SM
13411 dwarf2_per_objfile->dwp_file
13412 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13413 dwarf2_per_objfile->dwp_checked = 1;
13414 }
13415 return dwarf2_per_objfile->dwp_file;
13416}
13417
80626a55
DE
13418/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13419 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13420 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13421 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13422 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13423
13424 This is called, for example, when wanting to read a variable with a
13425 complex location. Therefore we don't want to do file i/o for every call.
13426 Therefore we don't want to look for a DWO file on every call.
13427 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13428 then we check if we've already seen DWO_NAME, and only THEN do we check
13429 for a DWO file.
13430
1c658ad5 13431 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13432 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13433
3019eac3 13434static struct dwo_unit *
80626a55
DE
13435lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13436 const char *dwo_name, const char *comp_dir,
13437 ULONGEST signature, int is_debug_types)
3019eac3 13438{
ed2dc618 13439 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13440 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13441 const char *kind = is_debug_types ? "TU" : "CU";
13442 void **dwo_file_slot;
3019eac3 13443 struct dwo_file *dwo_file;
80626a55 13444 struct dwp_file *dwp_file;
cb1df416 13445
6a506a2d
DE
13446 /* First see if there's a DWP file.
13447 If we have a DWP file but didn't find the DWO inside it, don't
13448 look for the original DWO file. It makes gdb behave differently
13449 depending on whether one is debugging in the build tree. */
cf2c3c16 13450
ed2dc618 13451 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13452 if (dwp_file != NULL)
cf2c3c16 13453 {
80626a55
DE
13454 const struct dwp_hash_table *dwp_htab =
13455 is_debug_types ? dwp_file->tus : dwp_file->cus;
13456
13457 if (dwp_htab != NULL)
13458 {
13459 struct dwo_unit *dwo_cutu =
ed2dc618 13460 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13461 signature, is_debug_types);
80626a55
DE
13462
13463 if (dwo_cutu != NULL)
13464 {
b4f54984 13465 if (dwarf_read_debug)
80626a55
DE
13466 {
13467 fprintf_unfiltered (gdb_stdlog,
13468 "Virtual DWO %s %s found: @%s\n",
13469 kind, hex_string (signature),
13470 host_address_to_string (dwo_cutu));
13471 }
13472 return dwo_cutu;
13473 }
13474 }
13475 }
6a506a2d 13476 else
80626a55 13477 {
6a506a2d 13478 /* No DWP file, look for the DWO file. */
80626a55 13479
ed2dc618
SM
13480 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13481 dwo_name, comp_dir);
6a506a2d 13482 if (*dwo_file_slot == NULL)
80626a55 13483 {
6a506a2d
DE
13484 /* Read in the file and build a table of the CUs/TUs it contains. */
13485 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13486 }
6a506a2d 13487 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13488 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13489
6a506a2d 13490 if (dwo_file != NULL)
19c3d4c9 13491 {
6a506a2d
DE
13492 struct dwo_unit *dwo_cutu = NULL;
13493
13494 if (is_debug_types && dwo_file->tus)
13495 {
13496 struct dwo_unit find_dwo_cutu;
13497
13498 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13499 find_dwo_cutu.signature = signature;
9a3c8263
SM
13500 dwo_cutu
13501 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13502 }
33c5cd75 13503 else if (!is_debug_types && dwo_file->cus)
80626a55 13504 {
33c5cd75
DB
13505 struct dwo_unit find_dwo_cutu;
13506
13507 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13508 find_dwo_cutu.signature = signature;
13509 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13510 &find_dwo_cutu);
6a506a2d
DE
13511 }
13512
13513 if (dwo_cutu != NULL)
13514 {
b4f54984 13515 if (dwarf_read_debug)
6a506a2d
DE
13516 {
13517 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13518 kind, dwo_name, hex_string (signature),
13519 host_address_to_string (dwo_cutu));
13520 }
13521 return dwo_cutu;
80626a55
DE
13522 }
13523 }
2e276125 13524 }
9cdd5dbd 13525
80626a55
DE
13526 /* We didn't find it. This could mean a dwo_id mismatch, or
13527 someone deleted the DWO/DWP file, or the search path isn't set up
13528 correctly to find the file. */
13529
b4f54984 13530 if (dwarf_read_debug)
80626a55
DE
13531 {
13532 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13533 kind, dwo_name, hex_string (signature));
13534 }
3019eac3 13535
6656a72d
DE
13536 /* This is a warning and not a complaint because it can be caused by
13537 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13538 {
13539 /* Print the name of the DWP file if we looked there, helps the user
13540 better diagnose the problem. */
791afaa2 13541 std::string dwp_text;
43942612
DE
13542
13543 if (dwp_file != NULL)
791afaa2
TT
13544 dwp_text = string_printf (" [in DWP file %s]",
13545 lbasename (dwp_file->name));
43942612
DE
13546
13547 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
13548 " [in module %s]"),
13549 kind, dwo_name, hex_string (signature),
791afaa2 13550 dwp_text.c_str (),
43942612 13551 this_unit->is_debug_types ? "TU" : "CU",
9c541725 13552 to_underlying (this_unit->sect_off), objfile_name (objfile));
43942612 13553 }
3019eac3 13554 return NULL;
5fb290d7
DJ
13555}
13556
80626a55
DE
13557/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13558 See lookup_dwo_cutu_unit for details. */
13559
13560static struct dwo_unit *
13561lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13562 const char *dwo_name, const char *comp_dir,
13563 ULONGEST signature)
13564{
13565 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13566}
13567
13568/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13569 See lookup_dwo_cutu_unit for details. */
13570
13571static struct dwo_unit *
13572lookup_dwo_type_unit (struct signatured_type *this_tu,
13573 const char *dwo_name, const char *comp_dir)
13574{
13575 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13576}
13577
89e63ee4
DE
13578/* Traversal function for queue_and_load_all_dwo_tus. */
13579
13580static int
13581queue_and_load_dwo_tu (void **slot, void *info)
13582{
13583 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13584 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13585 ULONGEST signature = dwo_unit->signature;
13586 struct signatured_type *sig_type =
13587 lookup_dwo_signatured_type (per_cu->cu, signature);
13588
13589 if (sig_type != NULL)
13590 {
13591 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13592
13593 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13594 a real dependency of PER_CU on SIG_TYPE. That is detected later
13595 while processing PER_CU. */
13596 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13597 load_full_type_unit (sig_cu);
13598 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13599 }
13600
13601 return 1;
13602}
13603
13604/* Queue all TUs contained in the DWO of PER_CU to be read in.
13605 The DWO may have the only definition of the type, though it may not be
13606 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13607 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13608
13609static void
13610queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13611{
13612 struct dwo_unit *dwo_unit;
13613 struct dwo_file *dwo_file;
13614
13615 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13616 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13617 gdb_assert (per_cu->cu != NULL);
13618
13619 dwo_unit = per_cu->cu->dwo_unit;
13620 gdb_assert (dwo_unit != NULL);
13621
13622 dwo_file = dwo_unit->dwo_file;
13623 if (dwo_file->tus != NULL)
13624 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13625}
13626
3019eac3
DE
13627/* Free all resources associated with DWO_FILE.
13628 Close the DWO file and munmap the sections.
13629 All memory should be on the objfile obstack. */
348e048f
DE
13630
13631static void
3019eac3 13632free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 13633{
348e048f 13634
5c6fa7ab 13635 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 13636 gdb_bfd_unref (dwo_file->dbfd);
348e048f 13637
3019eac3
DE
13638 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13639}
348e048f 13640
3019eac3 13641/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 13642
3019eac3
DE
13643static void
13644free_dwo_file_cleanup (void *arg)
13645{
ed2dc618
SM
13646 struct free_dwo_file_cleanup_data *data
13647 = (struct free_dwo_file_cleanup_data *) arg;
13648 struct objfile *objfile = data->dwarf2_per_objfile->objfile;
348e048f 13649
ed2dc618
SM
13650 free_dwo_file (data->dwo_file, objfile);
13651
13652 xfree (data);
3019eac3 13653}
348e048f 13654
3019eac3 13655/* Traversal function for free_dwo_files. */
2ab95328 13656
3019eac3
DE
13657static int
13658free_dwo_file_from_slot (void **slot, void *info)
13659{
13660 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13661 struct objfile *objfile = (struct objfile *) info;
348e048f 13662
3019eac3 13663 free_dwo_file (dwo_file, objfile);
348e048f 13664
3019eac3
DE
13665 return 1;
13666}
348e048f 13667
3019eac3 13668/* Free all resources associated with DWO_FILES. */
348e048f 13669
3019eac3
DE
13670static void
13671free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13672{
13673 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 13674}
3019eac3
DE
13675\f
13676/* Read in various DIEs. */
348e048f 13677
d389af10 13678/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13679 Inherit only the children of the DW_AT_abstract_origin DIE not being
13680 already referenced by DW_AT_abstract_origin from the children of the
13681 current DIE. */
d389af10
JK
13682
13683static void
13684inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13685{
13686 struct die_info *child_die;
791afaa2 13687 sect_offset *offsetp;
d389af10
JK
13688 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13689 struct die_info *origin_die;
13690 /* Iterator of the ORIGIN_DIE children. */
13691 struct die_info *origin_child_die;
d389af10 13692 struct attribute *attr;
cd02d79d
PA
13693 struct dwarf2_cu *origin_cu;
13694 struct pending **origin_previous_list_in_scope;
d389af10
JK
13695
13696 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13697 if (!attr)
13698 return;
13699
cd02d79d
PA
13700 /* Note that following die references may follow to a die in a
13701 different cu. */
13702
13703 origin_cu = cu;
13704 origin_die = follow_die_ref (die, attr, &origin_cu);
13705
13706 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13707 symbols in. */
13708 origin_previous_list_in_scope = origin_cu->list_in_scope;
13709 origin_cu->list_in_scope = cu->list_in_scope;
13710
edb3359d
DJ
13711 if (die->tag != origin_die->tag
13712 && !(die->tag == DW_TAG_inlined_subroutine
13713 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
13714 complaint (&symfile_complaints,
13715 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9c541725
PA
13716 to_underlying (die->sect_off),
13717 to_underlying (origin_die->sect_off));
d389af10 13718
791afaa2 13719 std::vector<sect_offset> offsets;
d389af10 13720
3ea89b92
PMR
13721 for (child_die = die->child;
13722 child_die && child_die->tag;
13723 child_die = sibling_die (child_die))
13724 {
13725 struct die_info *child_origin_die;
13726 struct dwarf2_cu *child_origin_cu;
13727
13728 /* We are trying to process concrete instance entries:
216f72a1 13729 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13730 it's not relevant to our analysis here. i.e. detecting DIEs that are
13731 present in the abstract instance but not referenced in the concrete
13732 one. */
216f72a1
JK
13733 if (child_die->tag == DW_TAG_call_site
13734 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13735 continue;
13736
c38f313d
DJ
13737 /* For each CHILD_DIE, find the corresponding child of
13738 ORIGIN_DIE. If there is more than one layer of
13739 DW_AT_abstract_origin, follow them all; there shouldn't be,
13740 but GCC versions at least through 4.4 generate this (GCC PR
13741 40573). */
3ea89b92
PMR
13742 child_origin_die = child_die;
13743 child_origin_cu = cu;
c38f313d
DJ
13744 while (1)
13745 {
cd02d79d
PA
13746 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13747 child_origin_cu);
c38f313d
DJ
13748 if (attr == NULL)
13749 break;
cd02d79d
PA
13750 child_origin_die = follow_die_ref (child_origin_die, attr,
13751 &child_origin_cu);
c38f313d
DJ
13752 }
13753
d389af10
JK
13754 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13755 counterpart may exist. */
c38f313d 13756 if (child_origin_die != child_die)
d389af10 13757 {
edb3359d
DJ
13758 if (child_die->tag != child_origin_die->tag
13759 && !(child_die->tag == DW_TAG_inlined_subroutine
13760 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
13761 complaint (&symfile_complaints,
13762 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
13763 "different tags"),
13764 to_underlying (child_die->sect_off),
13765 to_underlying (child_origin_die->sect_off));
c38f313d
DJ
13766 if (child_origin_die->parent != origin_die)
13767 complaint (&symfile_complaints,
13768 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
13769 "different parents"),
13770 to_underlying (child_die->sect_off),
13771 to_underlying (child_origin_die->sect_off));
c38f313d 13772 else
791afaa2 13773 offsets.push_back (child_origin_die->sect_off);
d389af10 13774 }
d389af10 13775 }
791afaa2
TT
13776 std::sort (offsets.begin (), offsets.end ());
13777 sect_offset *offsets_end = offsets.data () + offsets.size ();
13778 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13779 if (offsetp[-1] == *offsetp)
3e43a32a
MS
13780 complaint (&symfile_complaints,
13781 _("Multiple children of DIE 0x%x refer "
13782 "to DIE 0x%x as their abstract origin"),
9c541725 13783 to_underlying (die->sect_off), to_underlying (*offsetp));
d389af10 13784
791afaa2 13785 offsetp = offsets.data ();
d389af10
JK
13786 origin_child_die = origin_die->child;
13787 while (origin_child_die && origin_child_die->tag)
13788 {
13789 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13790 while (offsetp < offsets_end
9c541725 13791 && *offsetp < origin_child_die->sect_off)
d389af10 13792 offsetp++;
b64f50a1 13793 if (offsetp >= offsets_end
9c541725 13794 || *offsetp > origin_child_die->sect_off)
d389af10 13795 {
adde2bff
DE
13796 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13797 Check whether we're already processing ORIGIN_CHILD_DIE.
13798 This can happen with mutually referenced abstract_origins.
13799 PR 16581. */
13800 if (!origin_child_die->in_process)
13801 process_die (origin_child_die, origin_cu);
d389af10
JK
13802 }
13803 origin_child_die = sibling_die (origin_child_die);
13804 }
cd02d79d 13805 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
13806}
13807
c906108c 13808static void
e7c27a73 13809read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13810{
518817b3 13811 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13812 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13813 struct context_stack *newobj;
c906108c
SS
13814 CORE_ADDR lowpc;
13815 CORE_ADDR highpc;
13816 struct die_info *child_die;
edb3359d 13817 struct attribute *attr, *call_line, *call_file;
15d034d0 13818 const char *name;
e142c38c 13819 CORE_ADDR baseaddr;
801e3a5b 13820 struct block *block;
edb3359d 13821 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13822 std::vector<struct symbol *> template_args;
34eaf542 13823 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13824
13825 if (inlined_func)
13826 {
13827 /* If we do not have call site information, we can't show the
13828 caller of this inlined function. That's too confusing, so
13829 only use the scope for local variables. */
13830 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13831 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13832 if (call_line == NULL || call_file == NULL)
13833 {
13834 read_lexical_block_scope (die, cu);
13835 return;
13836 }
13837 }
c906108c 13838
e142c38c
DJ
13839 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13840
94af9270 13841 name = dwarf2_name (die, cu);
c906108c 13842
e8d05480
JB
13843 /* Ignore functions with missing or empty names. These are actually
13844 illegal according to the DWARF standard. */
13845 if (name == NULL)
13846 {
13847 complaint (&symfile_complaints,
b64f50a1 13848 _("missing name for subprogram DIE at %d"),
9c541725 13849 to_underlying (die->sect_off));
e8d05480
JB
13850 return;
13851 }
13852
13853 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13854 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13855 <= PC_BOUNDS_INVALID)
e8d05480 13856 {
ae4d0c03
PM
13857 attr = dwarf2_attr (die, DW_AT_external, cu);
13858 if (!attr || !DW_UNSND (attr))
13859 complaint (&symfile_complaints,
3e43a32a
MS
13860 _("cannot get low and high bounds "
13861 "for subprogram DIE at %d"),
9c541725 13862 to_underlying (die->sect_off));
e8d05480
JB
13863 return;
13864 }
c906108c 13865
3e29f34a
MR
13866 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13867 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13868
34eaf542
TT
13869 /* If we have any template arguments, then we must allocate a
13870 different sort of symbol. */
13871 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13872 {
13873 if (child_die->tag == DW_TAG_template_type_param
13874 || child_die->tag == DW_TAG_template_value_param)
13875 {
e623cf5d 13876 templ_func = allocate_template_symbol (objfile);
cf724bc9 13877 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13878 break;
13879 }
13880 }
13881
fe978cb0
PA
13882 newobj = push_context (0, lowpc);
13883 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 13884 (struct symbol *) templ_func);
4c2df51b 13885
4cecd739
DJ
13886 /* If there is a location expression for DW_AT_frame_base, record
13887 it. */
e142c38c 13888 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 13889 if (attr)
fe978cb0 13890 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13891
63e43d3a
PMR
13892 /* If there is a location for the static link, record it. */
13893 newobj->static_link = NULL;
13894 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13895 if (attr)
13896 {
224c3ddb
SM
13897 newobj->static_link
13898 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
13899 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13900 }
13901
e142c38c 13902 cu->list_in_scope = &local_symbols;
c906108c 13903
639d11d3 13904 if (die->child != NULL)
c906108c 13905 {
639d11d3 13906 child_die = die->child;
c906108c
SS
13907 while (child_die && child_die->tag)
13908 {
34eaf542
TT
13909 if (child_die->tag == DW_TAG_template_type_param
13910 || child_die->tag == DW_TAG_template_value_param)
13911 {
13912 struct symbol *arg = new_symbol (child_die, NULL, cu);
13913
f1078f66 13914 if (arg != NULL)
2f4732b0 13915 template_args.push_back (arg);
34eaf542
TT
13916 }
13917 else
13918 process_die (child_die, cu);
c906108c
SS
13919 child_die = sibling_die (child_die);
13920 }
13921 }
13922
d389af10
JK
13923 inherit_abstract_dies (die, cu);
13924
4a811a97
UW
13925 /* If we have a DW_AT_specification, we might need to import using
13926 directives from the context of the specification DIE. See the
13927 comment in determine_prefix. */
13928 if (cu->language == language_cplus
13929 && dwarf2_attr (die, DW_AT_specification, cu))
13930 {
13931 struct dwarf2_cu *spec_cu = cu;
13932 struct die_info *spec_die = die_specification (die, &spec_cu);
13933
13934 while (spec_die)
13935 {
13936 child_die = spec_die->child;
13937 while (child_die && child_die->tag)
13938 {
13939 if (child_die->tag == DW_TAG_imported_module)
13940 process_die (child_die, spec_cu);
13941 child_die = sibling_die (child_die);
13942 }
13943
13944 /* In some cases, GCC generates specification DIEs that
13945 themselves contain DW_AT_specification attributes. */
13946 spec_die = die_specification (spec_die, &spec_cu);
13947 }
13948 }
13949
fe978cb0 13950 newobj = pop_context ();
c906108c 13951 /* Make a block for the local symbols within. */
fe978cb0 13952 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 13953 newobj->static_link, lowpc, highpc);
801e3a5b 13954
df8a16a1 13955 /* For C++, set the block's scope. */
45280282
IB
13956 if ((cu->language == language_cplus
13957 || cu->language == language_fortran
c44af4eb
TT
13958 || cu->language == language_d
13959 || cu->language == language_rust)
4d4ec4e5 13960 && cu->processing_has_namespace_info)
195a3f6c
TT
13961 block_set_scope (block, determine_prefix (die, cu),
13962 &objfile->objfile_obstack);
df8a16a1 13963
801e3a5b
JB
13964 /* If we have address ranges, record them. */
13965 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13966
fe978cb0 13967 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 13968
34eaf542 13969 /* Attach template arguments to function. */
2f4732b0 13970 if (!template_args.empty ())
34eaf542
TT
13971 {
13972 gdb_assert (templ_func != NULL);
13973
2f4732b0 13974 templ_func->n_template_arguments = template_args.size ();
34eaf542 13975 templ_func->template_arguments
8d749320
SM
13976 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13977 templ_func->n_template_arguments);
34eaf542 13978 memcpy (templ_func->template_arguments,
2f4732b0 13979 template_args.data (),
34eaf542 13980 (templ_func->n_template_arguments * sizeof (struct symbol *)));
34eaf542
TT
13981 }
13982
208d8187
JB
13983 /* In C++, we can have functions nested inside functions (e.g., when
13984 a function declares a class that has methods). This means that
13985 when we finish processing a function scope, we may need to go
13986 back to building a containing block's symbol lists. */
fe978cb0 13987 local_symbols = newobj->locals;
22cee43f 13988 local_using_directives = newobj->local_using_directives;
208d8187 13989
921e78cf
JB
13990 /* If we've finished processing a top-level function, subsequent
13991 symbols go in the file symbol list. */
13992 if (outermost_context_p ())
e142c38c 13993 cu->list_in_scope = &file_symbols;
c906108c
SS
13994}
13995
13996/* Process all the DIES contained within a lexical block scope. Start
13997 a new scope, process the dies, and then close the scope. */
13998
13999static void
e7c27a73 14000read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14001{
518817b3 14002 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14003 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 14004 struct context_stack *newobj;
c906108c
SS
14005 CORE_ADDR lowpc, highpc;
14006 struct die_info *child_die;
e142c38c
DJ
14007 CORE_ADDR baseaddr;
14008
14009 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
14010
14011 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
14012 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
14013 as multiple lexical blocks? Handling children in a sane way would
6e70227d 14014 be nasty. Might be easier to properly extend generic blocks to
af34e669 14015 describe ranges. */
e385593e
JK
14016 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
14017 {
14018 case PC_BOUNDS_NOT_PRESENT:
14019 /* DW_TAG_lexical_block has no attributes, process its children as if
14020 there was no wrapping by that DW_TAG_lexical_block.
14021 GCC does no longer produces such DWARF since GCC r224161. */
14022 for (child_die = die->child;
14023 child_die != NULL && child_die->tag;
14024 child_die = sibling_die (child_die))
14025 process_die (child_die, cu);
14026 return;
14027 case PC_BOUNDS_INVALID:
14028 return;
14029 }
3e29f34a
MR
14030 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14031 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
14032
14033 push_context (0, lowpc);
639d11d3 14034 if (die->child != NULL)
c906108c 14035 {
639d11d3 14036 child_die = die->child;
c906108c
SS
14037 while (child_die && child_die->tag)
14038 {
e7c27a73 14039 process_die (child_die, cu);
c906108c
SS
14040 child_die = sibling_die (child_die);
14041 }
14042 }
3ea89b92 14043 inherit_abstract_dies (die, cu);
fe978cb0 14044 newobj = pop_context ();
c906108c 14045
22cee43f 14046 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 14047 {
801e3a5b 14048 struct block *block
63e43d3a 14049 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 14050 newobj->start_addr, highpc);
801e3a5b
JB
14051
14052 /* Note that recording ranges after traversing children, as we
14053 do here, means that recording a parent's ranges entails
14054 walking across all its children's ranges as they appear in
14055 the address map, which is quadratic behavior.
14056
14057 It would be nicer to record the parent's ranges before
14058 traversing its children, simply overriding whatever you find
14059 there. But since we don't even decide whether to create a
14060 block until after we've traversed its children, that's hard
14061 to do. */
14062 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 14063 }
fe978cb0 14064 local_symbols = newobj->locals;
22cee43f 14065 local_using_directives = newobj->local_using_directives;
c906108c
SS
14066}
14067
216f72a1 14068/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
14069
14070static void
14071read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
14072{
518817b3 14073 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
14074 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14075 CORE_ADDR pc, baseaddr;
14076 struct attribute *attr;
14077 struct call_site *call_site, call_site_local;
14078 void **slot;
14079 int nparams;
14080 struct die_info *child_die;
14081
14082 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14083
216f72a1
JK
14084 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
14085 if (attr == NULL)
14086 {
14087 /* This was a pre-DWARF-5 GNU extension alias
14088 for DW_AT_call_return_pc. */
14089 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14090 }
96408a79
SA
14091 if (!attr)
14092 {
14093 complaint (&symfile_complaints,
216f72a1 14094 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 14095 "DIE 0x%x [in module %s]"),
9c541725 14096 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
14097 return;
14098 }
31aa7e4e 14099 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 14100 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
14101
14102 if (cu->call_site_htab == NULL)
14103 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14104 NULL, &objfile->objfile_obstack,
14105 hashtab_obstack_allocate, NULL);
14106 call_site_local.pc = pc;
14107 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14108 if (*slot != NULL)
14109 {
14110 complaint (&symfile_complaints,
216f72a1 14111 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 14112 "DIE 0x%x [in module %s]"),
9c541725 14113 paddress (gdbarch, pc), to_underlying (die->sect_off),
4262abfb 14114 objfile_name (objfile));
96408a79
SA
14115 return;
14116 }
14117
14118 /* Count parameters at the caller. */
14119
14120 nparams = 0;
14121 for (child_die = die->child; child_die && child_die->tag;
14122 child_die = sibling_die (child_die))
14123 {
216f72a1
JK
14124 if (child_die->tag != DW_TAG_call_site_parameter
14125 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14126 {
14127 complaint (&symfile_complaints,
216f72a1
JK
14128 _("Tag %d is not DW_TAG_call_site_parameter in "
14129 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 14130 child_die->tag, to_underlying (child_die->sect_off),
4262abfb 14131 objfile_name (objfile));
96408a79
SA
14132 continue;
14133 }
14134
14135 nparams++;
14136 }
14137
224c3ddb
SM
14138 call_site
14139 = ((struct call_site *)
14140 obstack_alloc (&objfile->objfile_obstack,
14141 sizeof (*call_site)
14142 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
14143 *slot = call_site;
14144 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14145 call_site->pc = pc;
14146
216f72a1
JK
14147 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14148 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
14149 {
14150 struct die_info *func_die;
14151
14152 /* Skip also over DW_TAG_inlined_subroutine. */
14153 for (func_die = die->parent;
14154 func_die && func_die->tag != DW_TAG_subprogram
14155 && func_die->tag != DW_TAG_subroutine_type;
14156 func_die = func_die->parent);
14157
216f72a1
JK
14158 /* DW_AT_call_all_calls is a superset
14159 of DW_AT_call_all_tail_calls. */
96408a79 14160 if (func_die
216f72a1 14161 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14162 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14163 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14164 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14165 {
14166 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14167 not complete. But keep CALL_SITE for look ups via call_site_htab,
14168 both the initial caller containing the real return address PC and
14169 the final callee containing the current PC of a chain of tail
14170 calls do not need to have the tail call list complete. But any
14171 function candidate for a virtual tail call frame searched via
14172 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14173 determined unambiguously. */
14174 }
14175 else
14176 {
14177 struct type *func_type = NULL;
14178
14179 if (func_die)
14180 func_type = get_die_type (func_die, cu);
14181 if (func_type != NULL)
14182 {
14183 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14184
14185 /* Enlist this call site to the function. */
14186 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14187 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14188 }
14189 else
14190 complaint (&symfile_complaints,
216f72a1 14191 _("Cannot find function owning DW_TAG_call_site "
96408a79 14192 "DIE 0x%x [in module %s]"),
9c541725 14193 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
14194 }
14195 }
14196
216f72a1
JK
14197 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14198 if (attr == NULL)
14199 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14200 if (attr == NULL)
14201 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14202 if (attr == NULL)
216f72a1
JK
14203 {
14204 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14205 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14206 }
96408a79
SA
14207 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14208 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14209 /* Keep NULL DWARF_BLOCK. */;
14210 else if (attr_form_is_block (attr))
14211 {
14212 struct dwarf2_locexpr_baton *dlbaton;
14213
8d749320 14214 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14215 dlbaton->data = DW_BLOCK (attr)->data;
14216 dlbaton->size = DW_BLOCK (attr)->size;
14217 dlbaton->per_cu = cu->per_cu;
14218
14219 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14220 }
7771576e 14221 else if (attr_form_is_ref (attr))
96408a79 14222 {
96408a79
SA
14223 struct dwarf2_cu *target_cu = cu;
14224 struct die_info *target_die;
14225
ac9ec31b 14226 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14227 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14228 if (die_is_declaration (target_die, target_cu))
14229 {
7d45c7c3 14230 const char *target_physname;
9112db09
JK
14231
14232 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14233 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14234 if (target_physname == NULL)
9112db09 14235 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
14236 if (target_physname == NULL)
14237 complaint (&symfile_complaints,
216f72a1 14238 _("DW_AT_call_target target DIE has invalid "
96408a79 14239 "physname, for referencing DIE 0x%x [in module %s]"),
9c541725 14240 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 14241 else
7d455152 14242 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14243 }
14244 else
14245 {
14246 CORE_ADDR lowpc;
14247
14248 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14249 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14250 <= PC_BOUNDS_INVALID)
96408a79 14251 complaint (&symfile_complaints,
216f72a1 14252 _("DW_AT_call_target target DIE has invalid "
96408a79 14253 "low pc, for referencing DIE 0x%x [in module %s]"),
9c541725 14254 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 14255 else
3e29f34a
MR
14256 {
14257 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14258 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14259 }
96408a79
SA
14260 }
14261 }
14262 else
14263 complaint (&symfile_complaints,
216f72a1 14264 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 14265 "block nor reference, for DIE 0x%x [in module %s]"),
9c541725 14266 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
14267
14268 call_site->per_cu = cu->per_cu;
14269
14270 for (child_die = die->child;
14271 child_die && child_die->tag;
14272 child_die = sibling_die (child_die))
14273 {
96408a79 14274 struct call_site_parameter *parameter;
1788b2d3 14275 struct attribute *loc, *origin;
96408a79 14276
216f72a1
JK
14277 if (child_die->tag != DW_TAG_call_site_parameter
14278 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14279 {
14280 /* Already printed the complaint above. */
14281 continue;
14282 }
14283
14284 gdb_assert (call_site->parameter_count < nparams);
14285 parameter = &call_site->parameter[call_site->parameter_count];
14286
1788b2d3
JK
14287 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14288 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14289 register is contained in DW_AT_call_value. */
96408a79 14290
24c5c679 14291 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14292 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14293 if (origin == NULL)
14294 {
14295 /* This was a pre-DWARF-5 GNU extension alias
14296 for DW_AT_call_parameter. */
14297 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14298 }
7771576e 14299 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14300 {
1788b2d3 14301 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14302
14303 sect_offset sect_off
14304 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14305 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14306 {
14307 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14308 binding can be done only inside one CU. Such referenced DIE
14309 therefore cannot be even moved to DW_TAG_partial_unit. */
14310 complaint (&symfile_complaints,
216f72a1
JK
14311 _("DW_AT_call_parameter offset is not in CU for "
14312 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
14313 to_underlying (child_die->sect_off),
14314 objfile_name (objfile));
d76b7dbc
JK
14315 continue;
14316 }
9c541725
PA
14317 parameter->u.param_cu_off
14318 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14319 }
14320 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
14321 {
14322 complaint (&symfile_complaints,
14323 _("No DW_FORM_block* DW_AT_location for "
216f72a1 14324 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 14325 to_underlying (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14326 continue;
14327 }
24c5c679 14328 else
96408a79 14329 {
24c5c679
JK
14330 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14331 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14332 if (parameter->u.dwarf_reg != -1)
14333 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14334 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14335 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14336 &parameter->u.fb_offset))
14337 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14338 else
14339 {
14340 complaint (&symfile_complaints,
14341 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14342 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 14343 "DW_TAG_call_site child DIE 0x%x "
24c5c679 14344 "[in module %s]"),
9c541725
PA
14345 to_underlying (child_die->sect_off),
14346 objfile_name (objfile));
24c5c679
JK
14347 continue;
14348 }
96408a79
SA
14349 }
14350
216f72a1
JK
14351 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14352 if (attr == NULL)
14353 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14354 if (!attr_form_is_block (attr))
14355 {
14356 complaint (&symfile_complaints,
216f72a1
JK
14357 _("No DW_FORM_block* DW_AT_call_value for "
14358 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
14359 to_underlying (child_die->sect_off),
14360 objfile_name (objfile));
96408a79
SA
14361 continue;
14362 }
14363 parameter->value = DW_BLOCK (attr)->data;
14364 parameter->value_size = DW_BLOCK (attr)->size;
14365
14366 /* Parameters are not pre-cleared by memset above. */
14367 parameter->data_value = NULL;
14368 parameter->data_value_size = 0;
14369 call_site->parameter_count++;
14370
216f72a1
JK
14371 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14372 if (attr == NULL)
14373 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14374 if (attr)
14375 {
14376 if (!attr_form_is_block (attr))
14377 complaint (&symfile_complaints,
216f72a1
JK
14378 _("No DW_FORM_block* DW_AT_call_data_value for "
14379 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
14380 to_underlying (child_die->sect_off),
14381 objfile_name (objfile));
96408a79
SA
14382 else
14383 {
14384 parameter->data_value = DW_BLOCK (attr)->data;
14385 parameter->data_value_size = DW_BLOCK (attr)->size;
14386 }
14387 }
14388 }
14389}
14390
71a3c369
TT
14391/* Helper function for read_variable. If DIE represents a virtual
14392 table, then return the type of the concrete object that is
14393 associated with the virtual table. Otherwise, return NULL. */
14394
14395static struct type *
14396rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14397{
14398 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14399 if (attr == NULL)
14400 return NULL;
14401
14402 /* Find the type DIE. */
14403 struct die_info *type_die = NULL;
14404 struct dwarf2_cu *type_cu = cu;
14405
14406 if (attr_form_is_ref (attr))
14407 type_die = follow_die_ref (die, attr, &type_cu);
14408 if (type_die == NULL)
14409 return NULL;
14410
14411 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14412 return NULL;
14413 return die_containing_type (type_die, type_cu);
14414}
14415
14416/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14417
14418static void
14419read_variable (struct die_info *die, struct dwarf2_cu *cu)
14420{
14421 struct rust_vtable_symbol *storage = NULL;
14422
14423 if (cu->language == language_rust)
14424 {
14425 struct type *containing_type = rust_containing_type (die, cu);
14426
14427 if (containing_type != NULL)
14428 {
518817b3 14429 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369
TT
14430
14431 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14432 struct rust_vtable_symbol);
14433 initialize_objfile_symbol (storage);
14434 storage->concrete_type = containing_type;
cf724bc9 14435 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14436 }
14437 }
14438
14439 new_symbol_full (die, NULL, cu, storage);
14440}
14441
43988095
JK
14442/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14443 reading .debug_rnglists.
14444 Callback's type should be:
14445 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14446 Return true if the attributes are present and valid, otherwise,
14447 return false. */
14448
14449template <typename Callback>
14450static bool
14451dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14452 Callback &&callback)
14453{
ed2dc618 14454 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14455 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14456 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14457 bfd *obfd = objfile->obfd;
43988095
JK
14458 /* Base address selection entry. */
14459 CORE_ADDR base;
14460 int found_base;
43988095 14461 const gdb_byte *buffer;
43988095
JK
14462 CORE_ADDR baseaddr;
14463 bool overflow = false;
14464
14465 found_base = cu->base_known;
14466 base = cu->base_address;
14467
14468 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14469 if (offset >= dwarf2_per_objfile->rnglists.size)
14470 {
14471 complaint (&symfile_complaints,
14472 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14473 offset);
14474 return false;
14475 }
14476 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14477
14478 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14479
14480 while (1)
14481 {
7814882a
JK
14482 /* Initialize it due to a false compiler warning. */
14483 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14484 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14485 + dwarf2_per_objfile->rnglists.size);
14486 unsigned int bytes_read;
14487
14488 if (buffer == buf_end)
14489 {
14490 overflow = true;
14491 break;
14492 }
14493 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14494 switch (rlet)
14495 {
14496 case DW_RLE_end_of_list:
14497 break;
14498 case DW_RLE_base_address:
14499 if (buffer + cu->header.addr_size > buf_end)
14500 {
14501 overflow = true;
14502 break;
14503 }
14504 base = read_address (obfd, buffer, cu, &bytes_read);
14505 found_base = 1;
14506 buffer += bytes_read;
14507 break;
14508 case DW_RLE_start_length:
14509 if (buffer + cu->header.addr_size > buf_end)
14510 {
14511 overflow = true;
14512 break;
14513 }
14514 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14515 buffer += bytes_read;
14516 range_end = (range_beginning
14517 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14518 buffer += bytes_read;
14519 if (buffer > buf_end)
14520 {
14521 overflow = true;
14522 break;
14523 }
14524 break;
14525 case DW_RLE_offset_pair:
14526 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14527 buffer += bytes_read;
14528 if (buffer > buf_end)
14529 {
14530 overflow = true;
14531 break;
14532 }
14533 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14534 buffer += bytes_read;
14535 if (buffer > buf_end)
14536 {
14537 overflow = true;
14538 break;
14539 }
14540 break;
14541 case DW_RLE_start_end:
14542 if (buffer + 2 * cu->header.addr_size > buf_end)
14543 {
14544 overflow = true;
14545 break;
14546 }
14547 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14548 buffer += bytes_read;
14549 range_end = read_address (obfd, buffer, cu, &bytes_read);
14550 buffer += bytes_read;
14551 break;
14552 default:
14553 complaint (&symfile_complaints,
14554 _("Invalid .debug_rnglists data (no base address)"));
14555 return false;
14556 }
14557 if (rlet == DW_RLE_end_of_list || overflow)
14558 break;
14559 if (rlet == DW_RLE_base_address)
14560 continue;
14561
14562 if (!found_base)
14563 {
14564 /* We have no valid base address for the ranges
14565 data. */
14566 complaint (&symfile_complaints,
14567 _("Invalid .debug_rnglists data (no base address)"));
14568 return false;
14569 }
14570
14571 if (range_beginning > range_end)
14572 {
14573 /* Inverted range entries are invalid. */
14574 complaint (&symfile_complaints,
14575 _("Invalid .debug_rnglists data (inverted range)"));
14576 return false;
14577 }
14578
14579 /* Empty range entries have no effect. */
14580 if (range_beginning == range_end)
14581 continue;
14582
14583 range_beginning += base;
14584 range_end += base;
14585
14586 /* A not-uncommon case of bad debug info.
14587 Don't pollute the addrmap with bad data. */
14588 if (range_beginning + baseaddr == 0
14589 && !dwarf2_per_objfile->has_section_at_zero)
14590 {
14591 complaint (&symfile_complaints,
14592 _(".debug_rnglists entry has start address of zero"
14593 " [in module %s]"), objfile_name (objfile));
14594 continue;
14595 }
14596
14597 callback (range_beginning, range_end);
14598 }
14599
14600 if (overflow)
14601 {
14602 complaint (&symfile_complaints,
14603 _("Offset %d is not terminated "
14604 "for DW_AT_ranges attribute"),
14605 offset);
14606 return false;
14607 }
14608
14609 return true;
14610}
14611
14612/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14613 Callback's type should be:
14614 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14615 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14616
43988095 14617template <typename Callback>
43039443 14618static int
5f46c5a5 14619dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14620 Callback &&callback)
43039443 14621{
ed2dc618 14622 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14623 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14624 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14625 struct comp_unit_head *cu_header = &cu->header;
14626 bfd *obfd = objfile->obfd;
14627 unsigned int addr_size = cu_header->addr_size;
14628 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14629 /* Base address selection entry. */
14630 CORE_ADDR base;
14631 int found_base;
14632 unsigned int dummy;
d521ce57 14633 const gdb_byte *buffer;
ff013f42 14634 CORE_ADDR baseaddr;
43039443 14635
43988095
JK
14636 if (cu_header->version >= 5)
14637 return dwarf2_rnglists_process (offset, cu, callback);
14638
d00adf39
DE
14639 found_base = cu->base_known;
14640 base = cu->base_address;
43039443 14641
be391dca 14642 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14643 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
14644 {
14645 complaint (&symfile_complaints,
14646 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14647 offset);
14648 return 0;
14649 }
dce234bc 14650 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14651
e7030f15 14652 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14653
43039443
JK
14654 while (1)
14655 {
14656 CORE_ADDR range_beginning, range_end;
14657
14658 range_beginning = read_address (obfd, buffer, cu, &dummy);
14659 buffer += addr_size;
14660 range_end = read_address (obfd, buffer, cu, &dummy);
14661 buffer += addr_size;
14662 offset += 2 * addr_size;
14663
14664 /* An end of list marker is a pair of zero addresses. */
14665 if (range_beginning == 0 && range_end == 0)
14666 /* Found the end of list entry. */
14667 break;
14668
14669 /* Each base address selection entry is a pair of 2 values.
14670 The first is the largest possible address, the second is
14671 the base address. Check for a base address here. */
14672 if ((range_beginning & mask) == mask)
14673 {
28d2bfb9
AB
14674 /* If we found the largest possible address, then we already
14675 have the base address in range_end. */
14676 base = range_end;
43039443
JK
14677 found_base = 1;
14678 continue;
14679 }
14680
14681 if (!found_base)
14682 {
14683 /* We have no valid base address for the ranges
14684 data. */
14685 complaint (&symfile_complaints,
14686 _("Invalid .debug_ranges data (no base address)"));
14687 return 0;
14688 }
14689
9277c30c
UW
14690 if (range_beginning > range_end)
14691 {
14692 /* Inverted range entries are invalid. */
14693 complaint (&symfile_complaints,
14694 _("Invalid .debug_ranges data (inverted range)"));
14695 return 0;
14696 }
14697
14698 /* Empty range entries have no effect. */
14699 if (range_beginning == range_end)
14700 continue;
14701
43039443
JK
14702 range_beginning += base;
14703 range_end += base;
14704
01093045
DE
14705 /* A not-uncommon case of bad debug info.
14706 Don't pollute the addrmap with bad data. */
14707 if (range_beginning + baseaddr == 0
14708 && !dwarf2_per_objfile->has_section_at_zero)
14709 {
14710 complaint (&symfile_complaints,
14711 _(".debug_ranges entry has start address of zero"
4262abfb 14712 " [in module %s]"), objfile_name (objfile));
01093045
DE
14713 continue;
14714 }
14715
5f46c5a5
JK
14716 callback (range_beginning, range_end);
14717 }
14718
14719 return 1;
14720}
14721
14722/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14723 Return 1 if the attributes are present and valid, otherwise, return 0.
14724 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14725
14726static int
14727dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14728 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14729 struct partial_symtab *ranges_pst)
14730{
518817b3 14731 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14732 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14733 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14734 SECT_OFF_TEXT (objfile));
14735 int low_set = 0;
14736 CORE_ADDR low = 0;
14737 CORE_ADDR high = 0;
14738 int retval;
14739
14740 retval = dwarf2_ranges_process (offset, cu,
14741 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14742 {
9277c30c 14743 if (ranges_pst != NULL)
3e29f34a
MR
14744 {
14745 CORE_ADDR lowpc;
14746 CORE_ADDR highpc;
14747
14748 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14749 range_beginning + baseaddr);
14750 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14751 range_end + baseaddr);
14752 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14753 ranges_pst);
14754 }
ff013f42 14755
43039443
JK
14756 /* FIXME: This is recording everything as a low-high
14757 segment of consecutive addresses. We should have a
14758 data structure for discontiguous block ranges
14759 instead. */
14760 if (! low_set)
14761 {
14762 low = range_beginning;
14763 high = range_end;
14764 low_set = 1;
14765 }
14766 else
14767 {
14768 if (range_beginning < low)
14769 low = range_beginning;
14770 if (range_end > high)
14771 high = range_end;
14772 }
5f46c5a5
JK
14773 });
14774 if (!retval)
14775 return 0;
43039443
JK
14776
14777 if (! low_set)
14778 /* If the first entry is an end-of-list marker, the range
14779 describes an empty scope, i.e. no instructions. */
14780 return 0;
14781
14782 if (low_return)
14783 *low_return = low;
14784 if (high_return)
14785 *high_return = high;
14786 return 1;
14787}
14788
3a2b436a
JK
14789/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14790 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14791 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14792
3a2b436a 14793static enum pc_bounds_kind
af34e669 14794dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14795 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14796 struct partial_symtab *pst)
c906108c 14797{
518817b3
SM
14798 struct dwarf2_per_objfile *dwarf2_per_objfile
14799 = cu->per_cu->dwarf2_per_objfile;
c906108c 14800 struct attribute *attr;
91da1414 14801 struct attribute *attr_high;
af34e669
DJ
14802 CORE_ADDR low = 0;
14803 CORE_ADDR high = 0;
e385593e 14804 enum pc_bounds_kind ret;
c906108c 14805
91da1414
MW
14806 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14807 if (attr_high)
af34e669 14808 {
e142c38c 14809 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 14810 if (attr)
91da1414 14811 {
31aa7e4e
JB
14812 low = attr_value_as_address (attr);
14813 high = attr_value_as_address (attr_high);
14814 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14815 high += low;
91da1414 14816 }
af34e669
DJ
14817 else
14818 /* Found high w/o low attribute. */
e385593e 14819 return PC_BOUNDS_INVALID;
af34e669
DJ
14820
14821 /* Found consecutive range of addresses. */
3a2b436a 14822 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14823 }
c906108c 14824 else
af34e669 14825 {
e142c38c 14826 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14827 if (attr != NULL)
14828 {
ab435259
DE
14829 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14830 We take advantage of the fact that DW_AT_ranges does not appear
14831 in DW_TAG_compile_unit of DWO files. */
14832 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14833 unsigned int ranges_offset = (DW_UNSND (attr)
14834 + (need_ranges_base
14835 ? cu->ranges_base
14836 : 0));
2e3cf129 14837
af34e669 14838 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14839 .debug_ranges section. */
2e3cf129 14840 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14841 return PC_BOUNDS_INVALID;
43039443 14842 /* Found discontinuous range of addresses. */
3a2b436a 14843 ret = PC_BOUNDS_RANGES;
af34e669 14844 }
e385593e
JK
14845 else
14846 return PC_BOUNDS_NOT_PRESENT;
af34e669 14847 }
c906108c 14848
9373cf26
JK
14849 /* read_partial_die has also the strict LOW < HIGH requirement. */
14850 if (high <= low)
e385593e 14851 return PC_BOUNDS_INVALID;
c906108c
SS
14852
14853 /* When using the GNU linker, .gnu.linkonce. sections are used to
14854 eliminate duplicate copies of functions and vtables and such.
14855 The linker will arbitrarily choose one and discard the others.
14856 The AT_*_pc values for such functions refer to local labels in
14857 these sections. If the section from that file was discarded, the
14858 labels are not in the output, so the relocs get a value of 0.
14859 If this is a discarded function, mark the pc bounds as invalid,
14860 so that GDB will ignore it. */
72dca2f5 14861 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14862 return PC_BOUNDS_INVALID;
c906108c
SS
14863
14864 *lowpc = low;
96408a79
SA
14865 if (highpc)
14866 *highpc = high;
af34e669 14867 return ret;
c906108c
SS
14868}
14869
b084d499
JB
14870/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14871 its low and high PC addresses. Do nothing if these addresses could not
14872 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14873 and HIGHPC to the high address if greater than HIGHPC. */
14874
14875static void
14876dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14877 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14878 struct dwarf2_cu *cu)
14879{
14880 CORE_ADDR low, high;
14881 struct die_info *child = die->child;
14882
e385593e 14883 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14884 {
325fac50
PA
14885 *lowpc = std::min (*lowpc, low);
14886 *highpc = std::max (*highpc, high);
b084d499
JB
14887 }
14888
14889 /* If the language does not allow nested subprograms (either inside
14890 subprograms or lexical blocks), we're done. */
14891 if (cu->language != language_ada)
14892 return;
6e70227d 14893
b084d499
JB
14894 /* Check all the children of the given DIE. If it contains nested
14895 subprograms, then check their pc bounds. Likewise, we need to
14896 check lexical blocks as well, as they may also contain subprogram
14897 definitions. */
14898 while (child && child->tag)
14899 {
14900 if (child->tag == DW_TAG_subprogram
14901 || child->tag == DW_TAG_lexical_block)
14902 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14903 child = sibling_die (child);
14904 }
14905}
14906
fae299cd
DC
14907/* Get the low and high pc's represented by the scope DIE, and store
14908 them in *LOWPC and *HIGHPC. If the correct values can't be
14909 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14910
14911static void
14912get_scope_pc_bounds (struct die_info *die,
14913 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14914 struct dwarf2_cu *cu)
14915{
14916 CORE_ADDR best_low = (CORE_ADDR) -1;
14917 CORE_ADDR best_high = (CORE_ADDR) 0;
14918 CORE_ADDR current_low, current_high;
14919
3a2b436a 14920 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14921 >= PC_BOUNDS_RANGES)
fae299cd
DC
14922 {
14923 best_low = current_low;
14924 best_high = current_high;
14925 }
14926 else
14927 {
14928 struct die_info *child = die->child;
14929
14930 while (child && child->tag)
14931 {
14932 switch (child->tag) {
14933 case DW_TAG_subprogram:
b084d499 14934 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14935 break;
14936 case DW_TAG_namespace:
f55ee35c 14937 case DW_TAG_module:
fae299cd
DC
14938 /* FIXME: carlton/2004-01-16: Should we do this for
14939 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14940 that current GCC's always emit the DIEs corresponding
14941 to definitions of methods of classes as children of a
14942 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14943 the DIEs giving the declarations, which could be
14944 anywhere). But I don't see any reason why the
14945 standards says that they have to be there. */
14946 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14947
14948 if (current_low != ((CORE_ADDR) -1))
14949 {
325fac50
PA
14950 best_low = std::min (best_low, current_low);
14951 best_high = std::max (best_high, current_high);
fae299cd
DC
14952 }
14953 break;
14954 default:
0963b4bd 14955 /* Ignore. */
fae299cd
DC
14956 break;
14957 }
14958
14959 child = sibling_die (child);
14960 }
14961 }
14962
14963 *lowpc = best_low;
14964 *highpc = best_high;
14965}
14966
801e3a5b
JB
14967/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14968 in DIE. */
380bca97 14969
801e3a5b
JB
14970static void
14971dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14972 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14973{
518817b3 14974 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14975 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14976 struct attribute *attr;
91da1414 14977 struct attribute *attr_high;
801e3a5b 14978
91da1414
MW
14979 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14980 if (attr_high)
801e3a5b 14981 {
801e3a5b
JB
14982 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14983 if (attr)
14984 {
31aa7e4e
JB
14985 CORE_ADDR low = attr_value_as_address (attr);
14986 CORE_ADDR high = attr_value_as_address (attr_high);
14987
14988 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14989 high += low;
9a619af0 14990
3e29f34a
MR
14991 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14992 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14993 record_block_range (block, low, high - 1);
801e3a5b
JB
14994 }
14995 }
14996
14997 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14998 if (attr)
14999 {
ab435259
DE
15000 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
15001 We take advantage of the fact that DW_AT_ranges does not appear
15002 in DW_TAG_compile_unit of DWO files. */
15003 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
15004
15005 /* The value of the DW_AT_ranges attribute is the offset of the
15006 address range list in the .debug_ranges section. */
ab435259
DE
15007 unsigned long offset = (DW_UNSND (attr)
15008 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 15009 const gdb_byte *buffer;
801e3a5b
JB
15010
15011 /* For some target architectures, but not others, the
15012 read_address function sign-extends the addresses it returns.
15013 To recognize base address selection entries, we need a
15014 mask. */
15015 unsigned int addr_size = cu->header.addr_size;
15016 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
15017
15018 /* The base address, to which the next pair is relative. Note
15019 that this 'base' is a DWARF concept: most entries in a range
15020 list are relative, to reduce the number of relocs against the
15021 debugging information. This is separate from this function's
15022 'baseaddr' argument, which GDB uses to relocate debugging
15023 information from a shared library based on the address at
15024 which the library was loaded. */
d00adf39
DE
15025 CORE_ADDR base = cu->base_address;
15026 int base_known = cu->base_known;
801e3a5b 15027
5f46c5a5
JK
15028 dwarf2_ranges_process (offset, cu,
15029 [&] (CORE_ADDR start, CORE_ADDR end)
15030 {
58fdfd2c
JK
15031 start += baseaddr;
15032 end += baseaddr;
5f46c5a5
JK
15033 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
15034 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
15035 record_block_range (block, start, end - 1);
15036 });
801e3a5b
JB
15037 }
15038}
15039
685b1105
JK
15040/* Check whether the producer field indicates either of GCC < 4.6, or the
15041 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 15042
685b1105
JK
15043static void
15044check_producer (struct dwarf2_cu *cu)
60d5a603 15045{
38360086 15046 int major, minor;
60d5a603
JK
15047
15048 if (cu->producer == NULL)
15049 {
15050 /* For unknown compilers expect their behavior is DWARF version
15051 compliant.
15052
15053 GCC started to support .debug_types sections by -gdwarf-4 since
15054 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
15055 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
15056 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
15057 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 15058 }
b1ffba5a 15059 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 15060 {
38360086
MW
15061 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
15062 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 15063 }
5230b05a
WT
15064 else if (producer_is_icc (cu->producer, &major, &minor))
15065 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
15066 else
15067 {
15068 /* For other non-GCC compilers, expect their behavior is DWARF version
15069 compliant. */
60d5a603
JK
15070 }
15071
ba919b58 15072 cu->checked_producer = 1;
685b1105 15073}
ba919b58 15074
685b1105
JK
15075/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
15076 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
15077 during 4.6.0 experimental. */
15078
15079static int
15080producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
15081{
15082 if (!cu->checked_producer)
15083 check_producer (cu);
15084
15085 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
15086}
15087
15088/* Return the default accessibility type if it is not overriden by
15089 DW_AT_accessibility. */
15090
15091static enum dwarf_access_attribute
15092dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15093{
15094 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15095 {
15096 /* The default DWARF 2 accessibility for members is public, the default
15097 accessibility for inheritance is private. */
15098
15099 if (die->tag != DW_TAG_inheritance)
15100 return DW_ACCESS_public;
15101 else
15102 return DW_ACCESS_private;
15103 }
15104 else
15105 {
15106 /* DWARF 3+ defines the default accessibility a different way. The same
15107 rules apply now for DW_TAG_inheritance as for the members and it only
15108 depends on the container kind. */
15109
15110 if (die->parent->tag == DW_TAG_class_type)
15111 return DW_ACCESS_private;
15112 else
15113 return DW_ACCESS_public;
15114 }
15115}
15116
74ac6d43
TT
15117/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15118 offset. If the attribute was not found return 0, otherwise return
15119 1. If it was found but could not properly be handled, set *OFFSET
15120 to 0. */
15121
15122static int
15123handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15124 LONGEST *offset)
15125{
15126 struct attribute *attr;
15127
15128 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15129 if (attr != NULL)
15130 {
15131 *offset = 0;
15132
15133 /* Note that we do not check for a section offset first here.
15134 This is because DW_AT_data_member_location is new in DWARF 4,
15135 so if we see it, we can assume that a constant form is really
15136 a constant and not a section offset. */
15137 if (attr_form_is_constant (attr))
15138 *offset = dwarf2_get_attr_constant_value (attr, 0);
15139 else if (attr_form_is_section_offset (attr))
15140 dwarf2_complex_location_expr_complaint ();
15141 else if (attr_form_is_block (attr))
15142 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15143 else
15144 dwarf2_complex_location_expr_complaint ();
15145
15146 return 1;
15147 }
15148
15149 return 0;
15150}
15151
c906108c
SS
15152/* Add an aggregate field to the field list. */
15153
15154static void
107d2387 15155dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15156 struct dwarf2_cu *cu)
6e70227d 15157{
518817b3 15158 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15159 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15160 struct nextfield *new_field;
15161 struct attribute *attr;
15162 struct field *fp;
15d034d0 15163 const char *fieldname = "";
c906108c
SS
15164
15165 /* Allocate a new field list entry and link it in. */
8d749320 15166 new_field = XNEW (struct nextfield);
b8c9b27d 15167 make_cleanup (xfree, new_field);
c906108c 15168 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
15169
15170 if (die->tag == DW_TAG_inheritance)
15171 {
15172 new_field->next = fip->baseclasses;
15173 fip->baseclasses = new_field;
15174 }
15175 else
15176 {
15177 new_field->next = fip->fields;
15178 fip->fields = new_field;
15179 }
c906108c
SS
15180 fip->nfields++;
15181
e142c38c 15182 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
15183 if (attr)
15184 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15185 else
15186 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15187 if (new_field->accessibility != DW_ACCESS_public)
15188 fip->non_public_fields = 1;
60d5a603 15189
e142c38c 15190 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
15191 if (attr)
15192 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15193 else
15194 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15195
15196 fp = &new_field->field;
a9a9bd0f 15197
e142c38c 15198 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15199 {
74ac6d43
TT
15200 LONGEST offset;
15201
a9a9bd0f 15202 /* Data member other than a C++ static data member. */
6e70227d 15203
c906108c 15204 /* Get type of field. */
e7c27a73 15205 fp->type = die_type (die, cu);
c906108c 15206
d6a843b5 15207 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15208
c906108c 15209 /* Get bit size of field (zero if none). */
e142c38c 15210 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
15211 if (attr)
15212 {
15213 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15214 }
15215 else
15216 {
15217 FIELD_BITSIZE (*fp) = 0;
15218 }
15219
15220 /* Get bit offset of field. */
74ac6d43
TT
15221 if (handle_data_member_location (die, cu, &offset))
15222 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15223 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
15224 if (attr)
15225 {
5e2b427d 15226 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
15227 {
15228 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15229 additional bit offset from the MSB of the containing
15230 anonymous object to the MSB of the field. We don't
15231 have to do anything special since we don't need to
15232 know the size of the anonymous object. */
f41f5e61 15233 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15234 }
15235 else
15236 {
15237 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15238 MSB of the anonymous object, subtract off the number of
15239 bits from the MSB of the field to the MSB of the
15240 object, and then subtract off the number of bits of
15241 the field itself. The result is the bit offset of
15242 the LSB of the field. */
c906108c
SS
15243 int anonymous_size;
15244 int bit_offset = DW_UNSND (attr);
15245
e142c38c 15246 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15247 if (attr)
15248 {
15249 /* The size of the anonymous object containing
15250 the bit field is explicit, so use the
15251 indicated size (in bytes). */
15252 anonymous_size = DW_UNSND (attr);
15253 }
15254 else
15255 {
15256 /* The size of the anonymous object containing
15257 the bit field must be inferred from the type
15258 attribute of the data member containing the
15259 bit field. */
15260 anonymous_size = TYPE_LENGTH (fp->type);
15261 }
f41f5e61
PA
15262 SET_FIELD_BITPOS (*fp,
15263 (FIELD_BITPOS (*fp)
15264 + anonymous_size * bits_per_byte
15265 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15266 }
15267 }
da5b30da
AA
15268 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15269 if (attr != NULL)
15270 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15271 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15272
15273 /* Get name of field. */
39cbfefa
DJ
15274 fieldname = dwarf2_name (die, cu);
15275 if (fieldname == NULL)
15276 fieldname = "";
d8151005
DJ
15277
15278 /* The name is already allocated along with this objfile, so we don't
15279 need to duplicate it for the type. */
15280 fp->name = fieldname;
c906108c
SS
15281
15282 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15283 pointer or virtual base class pointer) to private. */
e142c38c 15284 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15285 {
d48cc9dd 15286 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15287 new_field->accessibility = DW_ACCESS_private;
15288 fip->non_public_fields = 1;
15289 }
15290 }
a9a9bd0f 15291 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15292 {
a9a9bd0f
DC
15293 /* C++ static member. */
15294
15295 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15296 is a declaration, but all versions of G++ as of this writing
15297 (so through at least 3.2.1) incorrectly generate
15298 DW_TAG_variable tags. */
6e70227d 15299
ff355380 15300 const char *physname;
c906108c 15301
a9a9bd0f 15302 /* Get name of field. */
39cbfefa
DJ
15303 fieldname = dwarf2_name (die, cu);
15304 if (fieldname == NULL)
c906108c
SS
15305 return;
15306
254e6b9e 15307 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15308 if (attr
15309 /* Only create a symbol if this is an external value.
15310 new_symbol checks this and puts the value in the global symbol
15311 table, which we want. If it is not external, new_symbol
15312 will try to put the value in cu->list_in_scope which is wrong. */
15313 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15314 {
15315 /* A static const member, not much different than an enum as far as
15316 we're concerned, except that we can support more types. */
15317 new_symbol (die, NULL, cu);
15318 }
15319
2df3850c 15320 /* Get physical name. */
ff355380 15321 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15322
d8151005
DJ
15323 /* The name is already allocated along with this objfile, so we don't
15324 need to duplicate it for the type. */
15325 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15326 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15327 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15328 }
15329 else if (die->tag == DW_TAG_inheritance)
15330 {
74ac6d43 15331 LONGEST offset;
d4b96c9a 15332
74ac6d43
TT
15333 /* C++ base class field. */
15334 if (handle_data_member_location (die, cu, &offset))
15335 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15336 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15337 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
15338 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15339 fip->nbaseclasses++;
15340 }
15341}
15342
883fd55a
KS
15343/* Can the type given by DIE define another type? */
15344
15345static bool
15346type_can_define_types (const struct die_info *die)
15347{
15348 switch (die->tag)
15349 {
15350 case DW_TAG_typedef:
15351 case DW_TAG_class_type:
15352 case DW_TAG_structure_type:
15353 case DW_TAG_union_type:
15354 case DW_TAG_enumeration_type:
15355 return true;
15356
15357 default:
15358 return false;
15359 }
15360}
15361
15362/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15363
15364static void
883fd55a
KS
15365dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15366 struct dwarf2_cu *cu)
6e70227d 15367{
883fd55a
KS
15368 struct decl_field_list *new_field;
15369 struct decl_field *fp;
98751a41
JK
15370
15371 /* Allocate a new field list entry and link it in. */
883fd55a 15372 new_field = XCNEW (struct decl_field_list);
98751a41
JK
15373 make_cleanup (xfree, new_field);
15374
883fd55a 15375 gdb_assert (type_can_define_types (die));
98751a41
JK
15376
15377 fp = &new_field->field;
15378
883fd55a 15379 /* Get name of field. NULL is okay here, meaning an anonymous type. */
98751a41 15380 fp->name = dwarf2_name (die, cu);
98751a41
JK
15381 fp->type = read_type_die (die, cu);
15382
c191a687
KS
15383 /* Save accessibility. */
15384 enum dwarf_access_attribute accessibility;
15385 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15386 if (attr != NULL)
15387 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15388 else
15389 accessibility = dwarf2_default_access_attribute (die, cu);
15390 switch (accessibility)
15391 {
15392 case DW_ACCESS_public:
15393 /* The assumed value if neither private nor protected. */
15394 break;
15395 case DW_ACCESS_private:
15396 fp->is_private = 1;
15397 break;
15398 case DW_ACCESS_protected:
15399 fp->is_protected = 1;
15400 break;
15401 default:
37534686
KS
15402 complaint (&symfile_complaints,
15403 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15404 }
15405
883fd55a
KS
15406 if (die->tag == DW_TAG_typedef)
15407 {
15408 new_field->next = fip->typedef_field_list;
15409 fip->typedef_field_list = new_field;
15410 fip->typedef_field_list_count++;
15411 }
15412 else
15413 {
15414 new_field->next = fip->nested_types_list;
15415 fip->nested_types_list = new_field;
15416 fip->nested_types_list_count++;
15417 }
98751a41
JK
15418}
15419
c906108c
SS
15420/* Create the vector of fields, and attach it to the type. */
15421
15422static void
fba45db2 15423dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15424 struct dwarf2_cu *cu)
c906108c
SS
15425{
15426 int nfields = fip->nfields;
15427
15428 /* Record the field count, allocate space for the array of fields,
15429 and create blank accessibility bitfields if necessary. */
15430 TYPE_NFIELDS (type) = nfields;
15431 TYPE_FIELDS (type) = (struct field *)
15432 TYPE_ALLOC (type, sizeof (struct field) * nfields);
15433 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
15434
b4ba55a1 15435 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15436 {
15437 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15438
15439 TYPE_FIELD_PRIVATE_BITS (type) =
15440 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15441 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15442
15443 TYPE_FIELD_PROTECTED_BITS (type) =
15444 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15445 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15446
774b6a14
TT
15447 TYPE_FIELD_IGNORE_BITS (type) =
15448 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15449 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15450 }
15451
15452 /* If the type has baseclasses, allocate and clear a bit vector for
15453 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 15454 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
15455 {
15456 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 15457 unsigned char *pointer;
c906108c
SS
15458
15459 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15460 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15461 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
15462 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
15463 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
15464 }
15465
3e43a32a
MS
15466 /* Copy the saved-up fields into the field vector. Start from the head of
15467 the list, adding to the tail of the field array, so that they end up in
15468 the same order in the array in which they were added to the list. */
c906108c
SS
15469 while (nfields-- > 0)
15470 {
7d0ccb61
DJ
15471 struct nextfield *fieldp;
15472
15473 if (fip->fields)
15474 {
15475 fieldp = fip->fields;
15476 fip->fields = fieldp->next;
15477 }
15478 else
15479 {
15480 fieldp = fip->baseclasses;
15481 fip->baseclasses = fieldp->next;
15482 }
15483
15484 TYPE_FIELD (type, nfields) = fieldp->field;
15485 switch (fieldp->accessibility)
c906108c 15486 {
c5aa993b 15487 case DW_ACCESS_private:
b4ba55a1
JB
15488 if (cu->language != language_ada)
15489 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 15490 break;
c906108c 15491
c5aa993b 15492 case DW_ACCESS_protected:
b4ba55a1
JB
15493 if (cu->language != language_ada)
15494 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 15495 break;
c906108c 15496
c5aa993b
JM
15497 case DW_ACCESS_public:
15498 break;
c906108c 15499
c5aa993b
JM
15500 default:
15501 /* Unknown accessibility. Complain and treat it as public. */
15502 {
e2e0b3e5 15503 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 15504 fieldp->accessibility);
c5aa993b
JM
15505 }
15506 break;
c906108c
SS
15507 }
15508 if (nfields < fip->nbaseclasses)
15509 {
7d0ccb61 15510 switch (fieldp->virtuality)
c906108c 15511 {
c5aa993b
JM
15512 case DW_VIRTUALITY_virtual:
15513 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15514 if (cu->language == language_ada)
a73c6dcd 15515 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
15516 SET_TYPE_FIELD_VIRTUAL (type, nfields);
15517 break;
c906108c
SS
15518 }
15519 }
c906108c
SS
15520 }
15521}
15522
7d27a96d
TT
15523/* Return true if this member function is a constructor, false
15524 otherwise. */
15525
15526static int
15527dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15528{
15529 const char *fieldname;
fe978cb0 15530 const char *type_name;
7d27a96d
TT
15531 int len;
15532
15533 if (die->parent == NULL)
15534 return 0;
15535
15536 if (die->parent->tag != DW_TAG_structure_type
15537 && die->parent->tag != DW_TAG_union_type
15538 && die->parent->tag != DW_TAG_class_type)
15539 return 0;
15540
15541 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15542 type_name = dwarf2_name (die->parent, cu);
15543 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15544 return 0;
15545
15546 len = strlen (fieldname);
fe978cb0
PA
15547 return (strncmp (fieldname, type_name, len) == 0
15548 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15549}
15550
c906108c
SS
15551/* Add a member function to the proper fieldlist. */
15552
15553static void
107d2387 15554dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15555 struct type *type, struct dwarf2_cu *cu)
c906108c 15556{
518817b3 15557 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15558 struct attribute *attr;
15559 struct fnfieldlist *flp;
15560 int i;
15561 struct fn_field *fnp;
15d034d0 15562 const char *fieldname;
c906108c 15563 struct nextfnfield *new_fnfield;
f792889a 15564 struct type *this_type;
60d5a603 15565 enum dwarf_access_attribute accessibility;
c906108c 15566
b4ba55a1 15567 if (cu->language == language_ada)
a73c6dcd 15568 error (_("unexpected member function in Ada type"));
b4ba55a1 15569
2df3850c 15570 /* Get name of member function. */
39cbfefa
DJ
15571 fieldname = dwarf2_name (die, cu);
15572 if (fieldname == NULL)
2df3850c 15573 return;
c906108c 15574
c906108c
SS
15575 /* Look up member function name in fieldlist. */
15576 for (i = 0; i < fip->nfnfields; i++)
15577 {
27bfe10e 15578 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
15579 break;
15580 }
15581
15582 /* Create new list element if necessary. */
15583 if (i < fip->nfnfields)
15584 flp = &fip->fnfieldlists[i];
15585 else
15586 {
15587 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
15588 {
15589 fip->fnfieldlists = (struct fnfieldlist *)
15590 xrealloc (fip->fnfieldlists,
15591 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 15592 * sizeof (struct fnfieldlist));
c906108c 15593 if (fip->nfnfields == 0)
c13c43fd 15594 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
15595 }
15596 flp = &fip->fnfieldlists[fip->nfnfields];
15597 flp->name = fieldname;
15598 flp->length = 0;
15599 flp->head = NULL;
3da10d80 15600 i = fip->nfnfields++;
c906108c
SS
15601 }
15602
15603 /* Create a new member function field and chain it to the field list
0963b4bd 15604 entry. */
8d749320 15605 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 15606 make_cleanup (xfree, new_fnfield);
c906108c
SS
15607 memset (new_fnfield, 0, sizeof (struct nextfnfield));
15608 new_fnfield->next = flp->head;
15609 flp->head = new_fnfield;
15610 flp->length++;
15611
15612 /* Fill in the member function field info. */
15613 fnp = &new_fnfield->fnfield;
3da10d80
KS
15614
15615 /* Delay processing of the physname until later. */
9c37b5ae 15616 if (cu->language == language_cplus)
3da10d80
KS
15617 {
15618 add_to_method_list (type, i, flp->length - 1, fieldname,
15619 die, cu);
15620 }
15621 else
15622 {
1d06ead6 15623 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15624 fnp->physname = physname ? physname : "";
15625 }
15626
c906108c 15627 fnp->type = alloc_type (objfile);
f792889a
DJ
15628 this_type = read_type_die (die, cu);
15629 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15630 {
f792889a 15631 int nparams = TYPE_NFIELDS (this_type);
c906108c 15632
f792889a 15633 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15634 of the method itself (TYPE_CODE_METHOD). */
15635 smash_to_method_type (fnp->type, type,
f792889a
DJ
15636 TYPE_TARGET_TYPE (this_type),
15637 TYPE_FIELDS (this_type),
15638 TYPE_NFIELDS (this_type),
15639 TYPE_VARARGS (this_type));
c906108c
SS
15640
15641 /* Handle static member functions.
c5aa993b 15642 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15643 member functions. G++ helps GDB by marking the first
15644 parameter for non-static member functions (which is the this
15645 pointer) as artificial. We obtain this information from
15646 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15647 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15648 fnp->voffset = VOFFSET_STATIC;
15649 }
15650 else
e2e0b3e5 15651 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 15652 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15653
15654 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15655 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15656 fnp->fcontext = die_containing_type (die, cu);
c906108c 15657
3e43a32a
MS
15658 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15659 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15660
15661 /* Get accessibility. */
e142c38c 15662 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15663 if (attr)
aead7601 15664 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15665 else
15666 accessibility = dwarf2_default_access_attribute (die, cu);
15667 switch (accessibility)
c906108c 15668 {
60d5a603
JK
15669 case DW_ACCESS_private:
15670 fnp->is_private = 1;
15671 break;
15672 case DW_ACCESS_protected:
15673 fnp->is_protected = 1;
15674 break;
c906108c
SS
15675 }
15676
b02dede2 15677 /* Check for artificial methods. */
e142c38c 15678 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15679 if (attr && DW_UNSND (attr) != 0)
15680 fnp->is_artificial = 1;
15681
7d27a96d
TT
15682 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15683
0d564a31 15684 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15685 function. For older versions of GCC, this is an offset in the
15686 appropriate virtual table, as specified by DW_AT_containing_type.
15687 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15688 to the object address. */
15689
e142c38c 15690 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15691 if (attr)
8e19ed76 15692 {
aec5aa8b 15693 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15694 {
aec5aa8b
TT
15695 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15696 {
15697 /* Old-style GCC. */
15698 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15699 }
15700 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15701 || (DW_BLOCK (attr)->size > 1
15702 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15703 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15704 {
aec5aa8b
TT
15705 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15706 if ((fnp->voffset % cu->header.addr_size) != 0)
15707 dwarf2_complex_location_expr_complaint ();
15708 else
15709 fnp->voffset /= cu->header.addr_size;
15710 fnp->voffset += 2;
15711 }
15712 else
15713 dwarf2_complex_location_expr_complaint ();
15714
15715 if (!fnp->fcontext)
7e993ebf
KS
15716 {
15717 /* If there is no `this' field and no DW_AT_containing_type,
15718 we cannot actually find a base class context for the
15719 vtable! */
15720 if (TYPE_NFIELDS (this_type) == 0
15721 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15722 {
15723 complaint (&symfile_complaints,
15724 _("cannot determine context for virtual member "
15725 "function \"%s\" (offset %d)"),
9c541725 15726 fieldname, to_underlying (die->sect_off));
7e993ebf
KS
15727 }
15728 else
15729 {
15730 fnp->fcontext
15731 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15732 }
15733 }
aec5aa8b 15734 }
3690dd37 15735 else if (attr_form_is_section_offset (attr))
8e19ed76 15736 {
4d3c2250 15737 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15738 }
15739 else
15740 {
4d3c2250
KB
15741 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15742 fieldname);
8e19ed76 15743 }
0d564a31 15744 }
d48cc9dd
DJ
15745 else
15746 {
15747 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15748 if (attr && DW_UNSND (attr))
15749 {
15750 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15751 complaint (&symfile_complaints,
3e43a32a
MS
15752 _("Member function \"%s\" (offset %d) is virtual "
15753 "but the vtable offset is not specified"),
9c541725 15754 fieldname, to_underlying (die->sect_off));
9655fd1a 15755 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15756 TYPE_CPLUS_DYNAMIC (type) = 1;
15757 }
15758 }
c906108c
SS
15759}
15760
15761/* Create the vector of member function fields, and attach it to the type. */
15762
15763static void
fba45db2 15764dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15765 struct dwarf2_cu *cu)
c906108c
SS
15766{
15767 struct fnfieldlist *flp;
c906108c
SS
15768 int i;
15769
b4ba55a1 15770 if (cu->language == language_ada)
a73c6dcd 15771 error (_("unexpected member functions in Ada type"));
b4ba55a1 15772
c906108c
SS
15773 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15774 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15775 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
15776
15777 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
15778 {
15779 struct nextfnfield *nfp = flp->head;
15780 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15781 int k;
15782
15783 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
15784 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
15785 fn_flp->fn_fields = (struct fn_field *)
15786 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
15787 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 15788 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
15789 }
15790
15791 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
15792}
15793
1168df01
JB
15794/* Returns non-zero if NAME is the name of a vtable member in CU's
15795 language, zero otherwise. */
15796static int
15797is_vtable_name (const char *name, struct dwarf2_cu *cu)
15798{
15799 static const char vptr[] = "_vptr";
15800
9c37b5ae
TT
15801 /* Look for the C++ form of the vtable. */
15802 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15803 return 1;
15804
15805 return 0;
15806}
15807
c0dd20ea 15808/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15809 functions, with the ABI-specified layout. If TYPE describes
15810 such a structure, smash it into a member function type.
61049d3b
DJ
15811
15812 GCC shouldn't do this; it should just output pointer to member DIEs.
15813 This is GCC PR debug/28767. */
c0dd20ea 15814
0b92b5bb
TT
15815static void
15816quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15817{
09e2d7c7 15818 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15819
15820 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15821 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15822 return;
c0dd20ea
DJ
15823
15824 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15825 if (TYPE_FIELD_NAME (type, 0) == NULL
15826 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15827 || TYPE_FIELD_NAME (type, 1) == NULL
15828 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15829 return;
c0dd20ea
DJ
15830
15831 /* Find the type of the method. */
0b92b5bb 15832 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15833 if (pfn_type == NULL
15834 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15835 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15836 return;
c0dd20ea
DJ
15837
15838 /* Look for the "this" argument. */
15839 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15840 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15841 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15842 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15843 return;
c0dd20ea 15844
09e2d7c7 15845 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15846 new_type = alloc_type (objfile);
09e2d7c7 15847 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15848 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15849 TYPE_VARARGS (pfn_type));
0b92b5bb 15850 smash_to_methodptr_type (type, new_type);
c0dd20ea 15851}
1168df01 15852
685b1105 15853
c906108c 15854/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15855 (definition) to create a type for the structure or union. Fill in
15856 the type's name and general properties; the members will not be
83655187
DE
15857 processed until process_structure_scope. A symbol table entry for
15858 the type will also not be done until process_structure_scope (assuming
15859 the type has a name).
c906108c 15860
c767944b
DJ
15861 NOTE: we need to call these functions regardless of whether or not the
15862 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15863 structure or union. This gets the type entered into our set of
83655187 15864 user defined types. */
c906108c 15865
f792889a 15866static struct type *
134d01f1 15867read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15868{
518817b3 15869 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15870 struct type *type;
15871 struct attribute *attr;
15d034d0 15872 const char *name;
c906108c 15873
348e048f
DE
15874 /* If the definition of this type lives in .debug_types, read that type.
15875 Don't follow DW_AT_specification though, that will take us back up
15876 the chain and we want to go down. */
45e58e77 15877 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
15878 if (attr)
15879 {
ac9ec31b 15880 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15881
ac9ec31b 15882 /* The type's CU may not be the same as CU.
02142a6c 15883 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15884 return set_die_type (die, type, cu);
15885 }
15886
c0dd20ea 15887 type = alloc_type (objfile);
c906108c 15888 INIT_CPLUS_SPECIFIC (type);
93311388 15889
39cbfefa
DJ
15890 name = dwarf2_name (die, cu);
15891 if (name != NULL)
c906108c 15892 {
987504bb 15893 if (cu->language == language_cplus
c44af4eb
TT
15894 || cu->language == language_d
15895 || cu->language == language_rust)
63d06c5c 15896 {
15d034d0 15897 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15898
15899 /* dwarf2_full_name might have already finished building the DIE's
15900 type. If so, there is no need to continue. */
15901 if (get_die_type (die, cu) != NULL)
15902 return get_die_type (die, cu);
15903
15904 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
15905 if (die->tag == DW_TAG_structure_type
15906 || die->tag == DW_TAG_class_type)
15907 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
15908 }
15909 else
15910 {
d8151005
DJ
15911 /* The name is already allocated along with this objfile, so
15912 we don't need to duplicate it for the type. */
7d455152 15913 TYPE_TAG_NAME (type) = name;
94af9270
KS
15914 if (die->tag == DW_TAG_class_type)
15915 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 15916 }
c906108c
SS
15917 }
15918
15919 if (die->tag == DW_TAG_structure_type)
15920 {
15921 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15922 }
15923 else if (die->tag == DW_TAG_union_type)
15924 {
15925 TYPE_CODE (type) = TYPE_CODE_UNION;
15926 }
15927 else
15928 {
4753d33b 15929 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15930 }
15931
0cc2414c
TT
15932 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15933 TYPE_DECLARED_CLASS (type) = 1;
15934
e142c38c 15935 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15936 if (attr)
15937 {
155bfbd3
JB
15938 if (attr_form_is_constant (attr))
15939 TYPE_LENGTH (type) = DW_UNSND (attr);
15940 else
15941 {
15942 /* For the moment, dynamic type sizes are not supported
15943 by GDB's struct type. The actual size is determined
15944 on-demand when resolving the type of a given object,
15945 so set the type's length to zero for now. Otherwise,
15946 we record an expression as the length, and that expression
15947 could lead to a very large value, which could eventually
15948 lead to us trying to allocate that much memory when creating
15949 a value of that type. */
15950 TYPE_LENGTH (type) = 0;
15951 }
c906108c
SS
15952 }
15953 else
15954 {
15955 TYPE_LENGTH (type) = 0;
15956 }
15957
5230b05a 15958 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15959 {
5230b05a
WT
15960 /* ICC<14 does not output the required DW_AT_declaration on
15961 incomplete types, but gives them a size of zero. */
422b1cb0 15962 TYPE_STUB (type) = 1;
685b1105
JK
15963 }
15964 else
15965 TYPE_STUB_SUPPORTED (type) = 1;
15966
dc718098 15967 if (die_is_declaration (die, cu))
876cecd0 15968 TYPE_STUB (type) = 1;
a6c727b2
DJ
15969 else if (attr == NULL && die->child == NULL
15970 && producer_is_realview (cu->producer))
15971 /* RealView does not output the required DW_AT_declaration
15972 on incomplete types. */
15973 TYPE_STUB (type) = 1;
dc718098 15974
c906108c
SS
15975 /* We need to add the type field to the die immediately so we don't
15976 infinitely recurse when dealing with pointers to the structure
0963b4bd 15977 type within the structure itself. */
1c379e20 15978 set_die_type (die, type, cu);
c906108c 15979
7e314c57
JK
15980 /* set_die_type should be already done. */
15981 set_descriptive_type (type, die, cu);
15982
c767944b
DJ
15983 return type;
15984}
15985
15986/* Finish creating a structure or union type, including filling in
15987 its members and creating a symbol for it. */
15988
15989static void
15990process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15991{
518817b3 15992 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15993 struct die_info *child_die;
c767944b
DJ
15994 struct type *type;
15995
15996 type = get_die_type (die, cu);
15997 if (type == NULL)
15998 type = read_structure_type (die, cu);
15999
e142c38c 16000 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16001 {
16002 struct field_info fi;
2f4732b0 16003 std::vector<struct symbol *> template_args;
c767944b 16004 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
16005
16006 memset (&fi, 0, sizeof (struct field_info));
16007
639d11d3 16008 child_die = die->child;
c906108c
SS
16009
16010 while (child_die && child_die->tag)
16011 {
a9a9bd0f
DC
16012 if (child_die->tag == DW_TAG_member
16013 || child_die->tag == DW_TAG_variable)
c906108c 16014 {
a9a9bd0f
DC
16015 /* NOTE: carlton/2002-11-05: A C++ static data member
16016 should be a DW_TAG_member that is a declaration, but
16017 all versions of G++ as of this writing (so through at
16018 least 3.2.1) incorrectly generate DW_TAG_variable
16019 tags for them instead. */
e7c27a73 16020 dwarf2_add_field (&fi, child_die, cu);
c906108c 16021 }
8713b1b1 16022 else if (child_die->tag == DW_TAG_subprogram)
c906108c 16023 {
e98c9e7c
TT
16024 /* Rust doesn't have member functions in the C++ sense.
16025 However, it does emit ordinary functions as children
16026 of a struct DIE. */
16027 if (cu->language == language_rust)
16028 read_func_scope (child_die, cu);
16029 else
16030 {
16031 /* C++ member function. */
16032 dwarf2_add_member_fn (&fi, child_die, type, cu);
16033 }
c906108c
SS
16034 }
16035 else if (child_die->tag == DW_TAG_inheritance)
16036 {
16037 /* C++ base class field. */
e7c27a73 16038 dwarf2_add_field (&fi, child_die, cu);
c906108c 16039 }
883fd55a
KS
16040 else if (type_can_define_types (child_die))
16041 dwarf2_add_type_defn (&fi, child_die, cu);
34eaf542
TT
16042 else if (child_die->tag == DW_TAG_template_type_param
16043 || child_die->tag == DW_TAG_template_value_param)
16044 {
16045 struct symbol *arg = new_symbol (child_die, NULL, cu);
16046
f1078f66 16047 if (arg != NULL)
2f4732b0 16048 template_args.push_back (arg);
34eaf542
TT
16049 }
16050
c906108c
SS
16051 child_die = sibling_die (child_die);
16052 }
16053
34eaf542 16054 /* Attach template arguments to type. */
2f4732b0 16055 if (!template_args.empty ())
34eaf542
TT
16056 {
16057 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16058 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16059 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16060 = XOBNEWVEC (&objfile->objfile_obstack,
16061 struct symbol *,
16062 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16063 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16064 template_args.data (),
34eaf542
TT
16065 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16066 * sizeof (struct symbol *)));
34eaf542
TT
16067 }
16068
c906108c
SS
16069 /* Attach fields and member functions to the type. */
16070 if (fi.nfields)
e7c27a73 16071 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
16072 if (fi.nfnfields)
16073 {
e7c27a73 16074 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16075
c5aa993b 16076 /* Get the type which refers to the base class (possibly this
c906108c 16077 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16078 class from the DW_AT_containing_type attribute. This use of
16079 DW_AT_containing_type is a GNU extension. */
c906108c 16080
e142c38c 16081 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16082 {
e7c27a73 16083 struct type *t = die_containing_type (die, cu);
c906108c 16084
ae6ae975 16085 set_type_vptr_basetype (type, t);
c906108c
SS
16086 if (type == t)
16087 {
c906108c
SS
16088 int i;
16089
16090 /* Our own class provides vtbl ptr. */
16091 for (i = TYPE_NFIELDS (t) - 1;
16092 i >= TYPE_N_BASECLASSES (t);
16093 --i)
16094 {
0d5cff50 16095 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16096
1168df01 16097 if (is_vtable_name (fieldname, cu))
c906108c 16098 {
ae6ae975 16099 set_type_vptr_fieldno (type, i);
c906108c
SS
16100 break;
16101 }
16102 }
16103
16104 /* Complain if virtual function table field not found. */
16105 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 16106 complaint (&symfile_complaints,
3e43a32a
MS
16107 _("virtual function table pointer "
16108 "not found when defining class '%s'"),
4d3c2250
KB
16109 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16110 "");
c906108c
SS
16111 }
16112 else
16113 {
ae6ae975 16114 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16115 }
16116 }
f6235d4c 16117 else if (cu->producer
61012eef 16118 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16119 {
16120 /* The IBM XLC compiler does not provide direct indication
16121 of the containing type, but the vtable pointer is
16122 always named __vfp. */
16123
16124 int i;
16125
16126 for (i = TYPE_NFIELDS (type) - 1;
16127 i >= TYPE_N_BASECLASSES (type);
16128 --i)
16129 {
16130 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16131 {
ae6ae975
DE
16132 set_type_vptr_fieldno (type, i);
16133 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16134 break;
16135 }
16136 }
16137 }
c906108c 16138 }
98751a41
JK
16139
16140 /* Copy fi.typedef_field_list linked list elements content into the
16141 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16142 if (fi.typedef_field_list)
16143 {
16144 int i = fi.typedef_field_list_count;
16145
a0d7a4ff 16146 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16147 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16148 = ((struct decl_field *)
224c3ddb 16149 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
16150 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
16151
16152 /* Reverse the list order to keep the debug info elements order. */
16153 while (--i >= 0)
16154 {
883fd55a 16155 struct decl_field *dest, *src;
6e70227d 16156
98751a41
JK
16157 dest = &TYPE_TYPEDEF_FIELD (type, i);
16158 src = &fi.typedef_field_list->field;
16159 fi.typedef_field_list = fi.typedef_field_list->next;
16160 *dest = *src;
16161 }
16162 }
c767944b 16163
883fd55a
KS
16164 /* Copy fi.nested_types_list linked list elements content into the
16165 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16166 if (fi.nested_types_list != NULL && cu->language != language_ada)
16167 {
16168 int i = fi.nested_types_list_count;
16169
16170 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16171 TYPE_NESTED_TYPES_ARRAY (type)
16172 = ((struct decl_field *)
16173 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
16174 TYPE_NESTED_TYPES_COUNT (type) = i;
16175
16176 /* Reverse the list order to keep the debug info elements order. */
16177 while (--i >= 0)
16178 {
16179 struct decl_field *dest, *src;
16180
16181 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
16182 src = &fi.nested_types_list->field;
16183 fi.nested_types_list = fi.nested_types_list->next;
16184 *dest = *src;
16185 }
16186 }
16187
c767944b 16188 do_cleanups (back_to);
c906108c 16189 }
63d06c5c 16190
bb5ed363 16191 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 16192
90aeadfc
DC
16193 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16194 snapshots) has been known to create a die giving a declaration
16195 for a class that has, as a child, a die giving a definition for a
16196 nested class. So we have to process our children even if the
16197 current die is a declaration. Normally, of course, a declaration
16198 won't have any children at all. */
134d01f1 16199
ca040673
DE
16200 child_die = die->child;
16201
90aeadfc
DC
16202 while (child_die != NULL && child_die->tag)
16203 {
16204 if (child_die->tag == DW_TAG_member
16205 || child_die->tag == DW_TAG_variable
34eaf542
TT
16206 || child_die->tag == DW_TAG_inheritance
16207 || child_die->tag == DW_TAG_template_value_param
16208 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16209 {
90aeadfc 16210 /* Do nothing. */
134d01f1 16211 }
90aeadfc
DC
16212 else
16213 process_die (child_die, cu);
134d01f1 16214
90aeadfc 16215 child_die = sibling_die (child_die);
134d01f1
DJ
16216 }
16217
fa4028e9
JB
16218 /* Do not consider external references. According to the DWARF standard,
16219 these DIEs are identified by the fact that they have no byte_size
16220 attribute, and a declaration attribute. */
16221 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16222 || !die_is_declaration (die, cu))
c767944b 16223 new_symbol (die, type, cu);
134d01f1
DJ
16224}
16225
55426c9d
JB
16226/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16227 update TYPE using some information only available in DIE's children. */
16228
16229static void
16230update_enumeration_type_from_children (struct die_info *die,
16231 struct type *type,
16232 struct dwarf2_cu *cu)
16233{
60f7655a 16234 struct die_info *child_die;
55426c9d
JB
16235 int unsigned_enum = 1;
16236 int flag_enum = 1;
16237 ULONGEST mask = 0;
55426c9d 16238
8268c778 16239 auto_obstack obstack;
55426c9d 16240
60f7655a
DE
16241 for (child_die = die->child;
16242 child_die != NULL && child_die->tag;
16243 child_die = sibling_die (child_die))
55426c9d
JB
16244 {
16245 struct attribute *attr;
16246 LONGEST value;
16247 const gdb_byte *bytes;
16248 struct dwarf2_locexpr_baton *baton;
16249 const char *name;
60f7655a 16250
55426c9d
JB
16251 if (child_die->tag != DW_TAG_enumerator)
16252 continue;
16253
16254 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16255 if (attr == NULL)
16256 continue;
16257
16258 name = dwarf2_name (child_die, cu);
16259 if (name == NULL)
16260 name = "<anonymous enumerator>";
16261
16262 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16263 &value, &bytes, &baton);
16264 if (value < 0)
16265 {
16266 unsigned_enum = 0;
16267 flag_enum = 0;
16268 }
16269 else if ((mask & value) != 0)
16270 flag_enum = 0;
16271 else
16272 mask |= value;
16273
16274 /* If we already know that the enum type is neither unsigned, nor
16275 a flag type, no need to look at the rest of the enumerates. */
16276 if (!unsigned_enum && !flag_enum)
16277 break;
55426c9d
JB
16278 }
16279
16280 if (unsigned_enum)
16281 TYPE_UNSIGNED (type) = 1;
16282 if (flag_enum)
16283 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16284}
16285
134d01f1
DJ
16286/* Given a DW_AT_enumeration_type die, set its type. We do not
16287 complete the type's fields yet, or create any symbols. */
c906108c 16288
f792889a 16289static struct type *
134d01f1 16290read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16291{
518817b3 16292 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16293 struct type *type;
c906108c 16294 struct attribute *attr;
0114d602 16295 const char *name;
134d01f1 16296
348e048f
DE
16297 /* If the definition of this type lives in .debug_types, read that type.
16298 Don't follow DW_AT_specification though, that will take us back up
16299 the chain and we want to go down. */
45e58e77 16300 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16301 if (attr)
16302 {
ac9ec31b 16303 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16304
ac9ec31b 16305 /* The type's CU may not be the same as CU.
02142a6c 16306 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16307 return set_die_type (die, type, cu);
16308 }
16309
c906108c
SS
16310 type = alloc_type (objfile);
16311
16312 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16313 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16314 if (name != NULL)
7d455152 16315 TYPE_TAG_NAME (type) = name;
c906108c 16316
0626fc76
TT
16317 attr = dwarf2_attr (die, DW_AT_type, cu);
16318 if (attr != NULL)
16319 {
16320 struct type *underlying_type = die_type (die, cu);
16321
16322 TYPE_TARGET_TYPE (type) = underlying_type;
16323 }
16324
e142c38c 16325 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16326 if (attr)
16327 {
16328 TYPE_LENGTH (type) = DW_UNSND (attr);
16329 }
16330 else
16331 {
16332 TYPE_LENGTH (type) = 0;
16333 }
16334
137033e9
JB
16335 /* The enumeration DIE can be incomplete. In Ada, any type can be
16336 declared as private in the package spec, and then defined only
16337 inside the package body. Such types are known as Taft Amendment
16338 Types. When another package uses such a type, an incomplete DIE
16339 may be generated by the compiler. */
02eb380e 16340 if (die_is_declaration (die, cu))
876cecd0 16341 TYPE_STUB (type) = 1;
02eb380e 16342
0626fc76
TT
16343 /* Finish the creation of this type by using the enum's children.
16344 We must call this even when the underlying type has been provided
16345 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16346 update_enumeration_type_from_children (die, type, cu);
16347
0626fc76
TT
16348 /* If this type has an underlying type that is not a stub, then we
16349 may use its attributes. We always use the "unsigned" attribute
16350 in this situation, because ordinarily we guess whether the type
16351 is unsigned -- but the guess can be wrong and the underlying type
16352 can tell us the reality. However, we defer to a local size
16353 attribute if one exists, because this lets the compiler override
16354 the underlying type if needed. */
16355 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16356 {
16357 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16358 if (TYPE_LENGTH (type) == 0)
16359 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16360 }
16361
3d567982
TT
16362 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16363
f792889a 16364 return set_die_type (die, type, cu);
134d01f1
DJ
16365}
16366
16367/* Given a pointer to a die which begins an enumeration, process all
16368 the dies that define the members of the enumeration, and create the
16369 symbol for the enumeration type.
16370
16371 NOTE: We reverse the order of the element list. */
16372
16373static void
16374process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16375{
f792889a 16376 struct type *this_type;
134d01f1 16377
f792889a
DJ
16378 this_type = get_die_type (die, cu);
16379 if (this_type == NULL)
16380 this_type = read_enumeration_type (die, cu);
9dc481d3 16381
639d11d3 16382 if (die->child != NULL)
c906108c 16383 {
9dc481d3
DE
16384 struct die_info *child_die;
16385 struct symbol *sym;
16386 struct field *fields = NULL;
16387 int num_fields = 0;
15d034d0 16388 const char *name;
9dc481d3 16389
639d11d3 16390 child_die = die->child;
c906108c
SS
16391 while (child_die && child_die->tag)
16392 {
16393 if (child_die->tag != DW_TAG_enumerator)
16394 {
e7c27a73 16395 process_die (child_die, cu);
c906108c
SS
16396 }
16397 else
16398 {
39cbfefa
DJ
16399 name = dwarf2_name (child_die, cu);
16400 if (name)
c906108c 16401 {
f792889a 16402 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16403
16404 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16405 {
16406 fields = (struct field *)
16407 xrealloc (fields,
16408 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16409 * sizeof (struct field));
c906108c
SS
16410 }
16411
3567439c 16412 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16413 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16414 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16415 FIELD_BITSIZE (fields[num_fields]) = 0;
16416
16417 num_fields++;
16418 }
16419 }
16420
16421 child_die = sibling_die (child_die);
16422 }
16423
16424 if (num_fields)
16425 {
f792889a
DJ
16426 TYPE_NFIELDS (this_type) = num_fields;
16427 TYPE_FIELDS (this_type) = (struct field *)
16428 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16429 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16430 sizeof (struct field) * num_fields);
b8c9b27d 16431 xfree (fields);
c906108c 16432 }
c906108c 16433 }
134d01f1 16434
6c83ed52
TT
16435 /* If we are reading an enum from a .debug_types unit, and the enum
16436 is a declaration, and the enum is not the signatured type in the
16437 unit, then we do not want to add a symbol for it. Adding a
16438 symbol would in some cases obscure the true definition of the
16439 enum, giving users an incomplete type when the definition is
16440 actually available. Note that we do not want to do this for all
16441 enums which are just declarations, because C++0x allows forward
16442 enum declarations. */
3019eac3 16443 if (cu->per_cu->is_debug_types
6c83ed52
TT
16444 && die_is_declaration (die, cu))
16445 {
52dc124a 16446 struct signatured_type *sig_type;
6c83ed52 16447
c0f78cd4 16448 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16449 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16450 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16451 return;
16452 }
16453
f792889a 16454 new_symbol (die, this_type, cu);
c906108c
SS
16455}
16456
16457/* Extract all information from a DW_TAG_array_type DIE and put it in
16458 the DIE's type field. For now, this only handles one dimensional
16459 arrays. */
16460
f792889a 16461static struct type *
e7c27a73 16462read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16463{
518817b3 16464 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16465 struct die_info *child_die;
7e314c57 16466 struct type *type;
c906108c 16467 struct type *element_type, *range_type, *index_type;
c906108c 16468 struct attribute *attr;
15d034d0 16469 const char *name;
a405673c 16470 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16471 unsigned int bit_stride = 0;
c906108c 16472
e7c27a73 16473 element_type = die_type (die, cu);
c906108c 16474
7e314c57
JK
16475 /* The die_type call above may have already set the type for this DIE. */
16476 type = get_die_type (die, cu);
16477 if (type)
16478 return type;
16479
dc53a7ad
JB
16480 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16481 if (attr != NULL)
a405673c
JB
16482 {
16483 int stride_ok;
16484
16485 byte_stride_prop
16486 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16487 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16488 if (!stride_ok)
16489 {
16490 complaint (&symfile_complaints,
16491 _("unable to read array DW_AT_byte_stride "
16492 " - DIE at 0x%x [in module %s]"),
16493 to_underlying (die->sect_off),
518817b3 16494 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16495 /* Ignore this attribute. We will likely not be able to print
16496 arrays of this type correctly, but there is little we can do
16497 to help if we cannot read the attribute's value. */
16498 byte_stride_prop = NULL;
16499 }
16500 }
dc53a7ad
JB
16501
16502 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16503 if (attr != NULL)
16504 bit_stride = DW_UNSND (attr);
16505
c906108c
SS
16506 /* Irix 6.2 native cc creates array types without children for
16507 arrays with unspecified length. */
639d11d3 16508 if (die->child == NULL)
c906108c 16509 {
46bf5051 16510 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16511 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16512 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16513 byte_stride_prop, bit_stride);
f792889a 16514 return set_die_type (die, type, cu);
c906108c
SS
16515 }
16516
791afaa2 16517 std::vector<struct type *> range_types;
639d11d3 16518 child_die = die->child;
c906108c
SS
16519 while (child_die && child_die->tag)
16520 {
16521 if (child_die->tag == DW_TAG_subrange_type)
16522 {
f792889a 16523 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16524
f792889a 16525 if (child_type != NULL)
a02abb62 16526 {
0963b4bd
MS
16527 /* The range type was succesfully read. Save it for the
16528 array type creation. */
791afaa2 16529 range_types.push_back (child_type);
a02abb62 16530 }
c906108c
SS
16531 }
16532 child_die = sibling_die (child_die);
16533 }
16534
16535 /* Dwarf2 dimensions are output from left to right, create the
16536 necessary array types in backwards order. */
7ca2d3a3 16537
c906108c 16538 type = element_type;
7ca2d3a3
DL
16539
16540 if (read_array_order (die, cu) == DW_ORD_col_major)
16541 {
16542 int i = 0;
9a619af0 16543
791afaa2 16544 while (i < range_types.size ())
dc53a7ad 16545 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16546 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16547 }
16548 else
16549 {
791afaa2 16550 size_t ndim = range_types.size ();
7ca2d3a3 16551 while (ndim-- > 0)
dc53a7ad 16552 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16553 byte_stride_prop, bit_stride);
7ca2d3a3 16554 }
c906108c 16555
f5f8a009
EZ
16556 /* Understand Dwarf2 support for vector types (like they occur on
16557 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16558 array type. This is not part of the Dwarf2/3 standard yet, but a
16559 custom vendor extension. The main difference between a regular
16560 array and the vector variant is that vectors are passed by value
16561 to functions. */
e142c38c 16562 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16563 if (attr)
ea37ba09 16564 make_vector_type (type);
f5f8a009 16565
dbc98a8b
KW
16566 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16567 implementation may choose to implement triple vectors using this
16568 attribute. */
16569 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16570 if (attr)
16571 {
16572 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16573 TYPE_LENGTH (type) = DW_UNSND (attr);
16574 else
3e43a32a
MS
16575 complaint (&symfile_complaints,
16576 _("DW_AT_byte_size for array type smaller "
16577 "than the total size of elements"));
dbc98a8b
KW
16578 }
16579
39cbfefa
DJ
16580 name = dwarf2_name (die, cu);
16581 if (name)
16582 TYPE_NAME (type) = name;
6e70227d 16583
0963b4bd 16584 /* Install the type in the die. */
7e314c57
JK
16585 set_die_type (die, type, cu);
16586
16587 /* set_die_type should be already done. */
b4ba55a1
JB
16588 set_descriptive_type (type, die, cu);
16589
7e314c57 16590 return type;
c906108c
SS
16591}
16592
7ca2d3a3 16593static enum dwarf_array_dim_ordering
6e70227d 16594read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16595{
16596 struct attribute *attr;
16597
16598 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16599
aead7601
SM
16600 if (attr)
16601 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16602
0963b4bd
MS
16603 /* GNU F77 is a special case, as at 08/2004 array type info is the
16604 opposite order to the dwarf2 specification, but data is still
16605 laid out as per normal fortran.
7ca2d3a3 16606
0963b4bd
MS
16607 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16608 version checking. */
7ca2d3a3 16609
905e0470
PM
16610 if (cu->language == language_fortran
16611 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16612 {
16613 return DW_ORD_row_major;
16614 }
16615
6e70227d 16616 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16617 {
16618 case array_column_major:
16619 return DW_ORD_col_major;
16620 case array_row_major:
16621 default:
16622 return DW_ORD_row_major;
16623 };
16624}
16625
72019c9c 16626/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16627 the DIE's type field. */
72019c9c 16628
f792889a 16629static struct type *
72019c9c
GM
16630read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16631{
7e314c57
JK
16632 struct type *domain_type, *set_type;
16633 struct attribute *attr;
f792889a 16634
7e314c57
JK
16635 domain_type = die_type (die, cu);
16636
16637 /* The die_type call above may have already set the type for this DIE. */
16638 set_type = get_die_type (die, cu);
16639 if (set_type)
16640 return set_type;
16641
16642 set_type = create_set_type (NULL, domain_type);
16643
16644 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16645 if (attr)
16646 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16647
f792889a 16648 return set_die_type (die, set_type, cu);
72019c9c 16649}
7ca2d3a3 16650
0971de02
TT
16651/* A helper for read_common_block that creates a locexpr baton.
16652 SYM is the symbol which we are marking as computed.
16653 COMMON_DIE is the DIE for the common block.
16654 COMMON_LOC is the location expression attribute for the common
16655 block itself.
16656 MEMBER_LOC is the location expression attribute for the particular
16657 member of the common block that we are processing.
16658 CU is the CU from which the above come. */
16659
16660static void
16661mark_common_block_symbol_computed (struct symbol *sym,
16662 struct die_info *common_die,
16663 struct attribute *common_loc,
16664 struct attribute *member_loc,
16665 struct dwarf2_cu *cu)
16666{
518817b3
SM
16667 struct dwarf2_per_objfile *dwarf2_per_objfile
16668 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16669 struct objfile *objfile = dwarf2_per_objfile->objfile;
16670 struct dwarf2_locexpr_baton *baton;
16671 gdb_byte *ptr;
16672 unsigned int cu_off;
16673 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16674 LONGEST offset = 0;
16675
16676 gdb_assert (common_loc && member_loc);
16677 gdb_assert (attr_form_is_block (common_loc));
16678 gdb_assert (attr_form_is_block (member_loc)
16679 || attr_form_is_constant (member_loc));
16680
8d749320 16681 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16682 baton->per_cu = cu->per_cu;
16683 gdb_assert (baton->per_cu);
16684
16685 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16686
16687 if (attr_form_is_constant (member_loc))
16688 {
16689 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16690 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16691 }
16692 else
16693 baton->size += DW_BLOCK (member_loc)->size;
16694
224c3ddb 16695 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16696 baton->data = ptr;
16697
16698 *ptr++ = DW_OP_call4;
9c541725 16699 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16700 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16701 ptr += 4;
16702
16703 if (attr_form_is_constant (member_loc))
16704 {
16705 *ptr++ = DW_OP_addr;
16706 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16707 ptr += cu->header.addr_size;
16708 }
16709 else
16710 {
16711 /* We have to copy the data here, because DW_OP_call4 will only
16712 use a DW_AT_location attribute. */
16713 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16714 ptr += DW_BLOCK (member_loc)->size;
16715 }
16716
16717 *ptr++ = DW_OP_plus;
16718 gdb_assert (ptr - baton->data == baton->size);
16719
0971de02 16720 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16721 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16722}
16723
4357ac6c
TT
16724/* Create appropriate locally-scoped variables for all the
16725 DW_TAG_common_block entries. Also create a struct common_block
16726 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16727 is used to sepate the common blocks name namespace from regular
16728 variable names. */
c906108c
SS
16729
16730static void
e7c27a73 16731read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16732{
0971de02
TT
16733 struct attribute *attr;
16734
16735 attr = dwarf2_attr (die, DW_AT_location, cu);
16736 if (attr)
16737 {
16738 /* Support the .debug_loc offsets. */
16739 if (attr_form_is_block (attr))
16740 {
16741 /* Ok. */
16742 }
16743 else if (attr_form_is_section_offset (attr))
16744 {
16745 dwarf2_complex_location_expr_complaint ();
16746 attr = NULL;
16747 }
16748 else
16749 {
16750 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16751 "common block member");
16752 attr = NULL;
16753 }
16754 }
16755
639d11d3 16756 if (die->child != NULL)
c906108c 16757 {
518817b3 16758 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16759 struct die_info *child_die;
16760 size_t n_entries = 0, size;
16761 struct common_block *common_block;
16762 struct symbol *sym;
74ac6d43 16763
4357ac6c
TT
16764 for (child_die = die->child;
16765 child_die && child_die->tag;
16766 child_die = sibling_die (child_die))
16767 ++n_entries;
16768
16769 size = (sizeof (struct common_block)
16770 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16771 common_block
16772 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16773 size);
4357ac6c
TT
16774 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16775 common_block->n_entries = 0;
16776
16777 for (child_die = die->child;
16778 child_die && child_die->tag;
16779 child_die = sibling_die (child_die))
16780 {
16781 /* Create the symbol in the DW_TAG_common_block block in the current
16782 symbol scope. */
e7c27a73 16783 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16784 if (sym != NULL)
16785 {
16786 struct attribute *member_loc;
16787
16788 common_block->contents[common_block->n_entries++] = sym;
16789
16790 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16791 cu);
16792 if (member_loc)
16793 {
16794 /* GDB has handled this for a long time, but it is
16795 not specified by DWARF. It seems to have been
16796 emitted by gfortran at least as recently as:
16797 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16798 complaint (&symfile_complaints,
16799 _("Variable in common block has "
16800 "DW_AT_data_member_location "
16801 "- DIE at 0x%x [in module %s]"),
9c541725 16802 to_underlying (child_die->sect_off),
518817b3 16803 objfile_name (objfile));
0971de02
TT
16804
16805 if (attr_form_is_section_offset (member_loc))
16806 dwarf2_complex_location_expr_complaint ();
16807 else if (attr_form_is_constant (member_loc)
16808 || attr_form_is_block (member_loc))
16809 {
16810 if (attr)
16811 mark_common_block_symbol_computed (sym, die, attr,
16812 member_loc, cu);
16813 }
16814 else
16815 dwarf2_complex_location_expr_complaint ();
16816 }
16817 }
c906108c 16818 }
4357ac6c
TT
16819
16820 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16821 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16822 }
16823}
16824
0114d602 16825/* Create a type for a C++ namespace. */
d9fa45fe 16826
0114d602
DJ
16827static struct type *
16828read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16829{
518817b3 16830 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16831 const char *previous_prefix, *name;
9219021c 16832 int is_anonymous;
0114d602
DJ
16833 struct type *type;
16834
16835 /* For extensions, reuse the type of the original namespace. */
16836 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16837 {
16838 struct die_info *ext_die;
16839 struct dwarf2_cu *ext_cu = cu;
9a619af0 16840
0114d602
DJ
16841 ext_die = dwarf2_extension (die, &ext_cu);
16842 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16843
16844 /* EXT_CU may not be the same as CU.
02142a6c 16845 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16846 return set_die_type (die, type, cu);
16847 }
9219021c 16848
e142c38c 16849 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16850
16851 /* Now build the name of the current namespace. */
16852
0114d602
DJ
16853 previous_prefix = determine_prefix (die, cu);
16854 if (previous_prefix[0] != '\0')
16855 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16856 previous_prefix, name, 0, cu);
0114d602
DJ
16857
16858 /* Create the type. */
19f392bc 16859 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
16860 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16861
60531b24 16862 return set_die_type (die, type, cu);
0114d602
DJ
16863}
16864
22cee43f 16865/* Read a namespace scope. */
0114d602
DJ
16866
16867static void
16868read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16869{
518817b3 16870 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16871 int is_anonymous;
9219021c 16872
5c4e30ca
DC
16873 /* Add a symbol associated to this if we haven't seen the namespace
16874 before. Also, add a using directive if it's an anonymous
16875 namespace. */
9219021c 16876
f2f0e013 16877 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16878 {
16879 struct type *type;
16880
0114d602 16881 type = read_type_die (die, cu);
e7c27a73 16882 new_symbol (die, type, cu);
5c4e30ca 16883
e8e80198 16884 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16885 if (is_anonymous)
0114d602
DJ
16886 {
16887 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16888
eb1e02fd 16889 std::vector<const char *> excludes;
22cee43f
PMR
16890 add_using_directive (using_directives (cu->language),
16891 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16892 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16893 }
5c4e30ca 16894 }
9219021c 16895
639d11d3 16896 if (die->child != NULL)
d9fa45fe 16897 {
639d11d3 16898 struct die_info *child_die = die->child;
6e70227d 16899
d9fa45fe
DC
16900 while (child_die && child_die->tag)
16901 {
e7c27a73 16902 process_die (child_die, cu);
d9fa45fe
DC
16903 child_die = sibling_die (child_die);
16904 }
16905 }
38d518c9
EZ
16906}
16907
f55ee35c
JK
16908/* Read a Fortran module as type. This DIE can be only a declaration used for
16909 imported module. Still we need that type as local Fortran "use ... only"
16910 declaration imports depend on the created type in determine_prefix. */
16911
16912static struct type *
16913read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16914{
518817b3 16915 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16916 const char *module_name;
f55ee35c
JK
16917 struct type *type;
16918
16919 module_name = dwarf2_name (die, cu);
16920 if (!module_name)
3e43a32a
MS
16921 complaint (&symfile_complaints,
16922 _("DW_TAG_module has no name, offset 0x%x"),
9c541725 16923 to_underlying (die->sect_off));
19f392bc 16924 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
16925
16926 /* determine_prefix uses TYPE_TAG_NAME. */
16927 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16928
16929 return set_die_type (die, type, cu);
16930}
16931
5d7cb8df
JK
16932/* Read a Fortran module. */
16933
16934static void
16935read_module (struct die_info *die, struct dwarf2_cu *cu)
16936{
16937 struct die_info *child_die = die->child;
530e8392
KB
16938 struct type *type;
16939
16940 type = read_type_die (die, cu);
16941 new_symbol (die, type, cu);
5d7cb8df 16942
5d7cb8df
JK
16943 while (child_die && child_die->tag)
16944 {
16945 process_die (child_die, cu);
16946 child_die = sibling_die (child_die);
16947 }
16948}
16949
38d518c9
EZ
16950/* Return the name of the namespace represented by DIE. Set
16951 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16952 namespace. */
16953
16954static const char *
e142c38c 16955namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16956{
16957 struct die_info *current_die;
16958 const char *name = NULL;
16959
16960 /* Loop through the extensions until we find a name. */
16961
16962 for (current_die = die;
16963 current_die != NULL;
f2f0e013 16964 current_die = dwarf2_extension (die, &cu))
38d518c9 16965 {
96553a0c
DE
16966 /* We don't use dwarf2_name here so that we can detect the absence
16967 of a name -> anonymous namespace. */
7d45c7c3 16968 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16969
38d518c9
EZ
16970 if (name != NULL)
16971 break;
16972 }
16973
16974 /* Is it an anonymous namespace? */
16975
16976 *is_anonymous = (name == NULL);
16977 if (*is_anonymous)
2b1dbab0 16978 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16979
16980 return name;
d9fa45fe
DC
16981}
16982
c906108c
SS
16983/* Extract all information from a DW_TAG_pointer_type DIE and add to
16984 the user defined type vector. */
16985
f792889a 16986static struct type *
e7c27a73 16987read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16988{
518817b3
SM
16989 struct gdbarch *gdbarch
16990 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16991 struct comp_unit_head *cu_header = &cu->header;
c906108c 16992 struct type *type;
8b2dbe47
KB
16993 struct attribute *attr_byte_size;
16994 struct attribute *attr_address_class;
16995 int byte_size, addr_class;
7e314c57
JK
16996 struct type *target_type;
16997
16998 target_type = die_type (die, cu);
c906108c 16999
7e314c57
JK
17000 /* The die_type call above may have already set the type for this DIE. */
17001 type = get_die_type (die, cu);
17002 if (type)
17003 return type;
17004
17005 type = lookup_pointer_type (target_type);
8b2dbe47 17006
e142c38c 17007 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17008 if (attr_byte_size)
17009 byte_size = DW_UNSND (attr_byte_size);
c906108c 17010 else
8b2dbe47
KB
17011 byte_size = cu_header->addr_size;
17012
e142c38c 17013 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17014 if (attr_address_class)
17015 addr_class = DW_UNSND (attr_address_class);
17016 else
17017 addr_class = DW_ADDR_none;
17018
17019 /* If the pointer size or address class is different than the
17020 default, create a type variant marked as such and set the
17021 length accordingly. */
17022 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 17023 {
5e2b427d 17024 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
17025 {
17026 int type_flags;
17027
849957d9 17028 type_flags = gdbarch_address_class_type_flags
5e2b427d 17029 (gdbarch, byte_size, addr_class);
876cecd0
TT
17030 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17031 == 0);
8b2dbe47
KB
17032 type = make_type_with_address_space (type, type_flags);
17033 }
17034 else if (TYPE_LENGTH (type) != byte_size)
17035 {
3e43a32a
MS
17036 complaint (&symfile_complaints,
17037 _("invalid pointer size %d"), byte_size);
8b2dbe47 17038 }
6e70227d 17039 else
9a619af0
MS
17040 {
17041 /* Should we also complain about unhandled address classes? */
17042 }
c906108c 17043 }
8b2dbe47
KB
17044
17045 TYPE_LENGTH (type) = byte_size;
f792889a 17046 return set_die_type (die, type, cu);
c906108c
SS
17047}
17048
17049/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17050 the user defined type vector. */
17051
f792889a 17052static struct type *
e7c27a73 17053read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17054{
17055 struct type *type;
17056 struct type *to_type;
17057 struct type *domain;
17058
e7c27a73
DJ
17059 to_type = die_type (die, cu);
17060 domain = die_containing_type (die, cu);
0d5de010 17061
7e314c57
JK
17062 /* The calls above may have already set the type for this DIE. */
17063 type = get_die_type (die, cu);
17064 if (type)
17065 return type;
17066
0d5de010
DJ
17067 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17068 type = lookup_methodptr_type (to_type);
7078baeb
TT
17069 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17070 {
518817b3
SM
17071 struct type *new_type
17072 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17073
17074 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17075 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17076 TYPE_VARARGS (to_type));
17077 type = lookup_methodptr_type (new_type);
17078 }
0d5de010
DJ
17079 else
17080 type = lookup_memberptr_type (to_type, domain);
c906108c 17081
f792889a 17082 return set_die_type (die, type, cu);
c906108c
SS
17083}
17084
4297a3f0 17085/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17086 the user defined type vector. */
17087
f792889a 17088static struct type *
4297a3f0
AV
17089read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17090 enum type_code refcode)
c906108c 17091{
e7c27a73 17092 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17093 struct type *type, *target_type;
c906108c
SS
17094 struct attribute *attr;
17095
4297a3f0
AV
17096 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17097
7e314c57
JK
17098 target_type = die_type (die, cu);
17099
17100 /* The die_type call above may have already set the type for this DIE. */
17101 type = get_die_type (die, cu);
17102 if (type)
17103 return type;
17104
4297a3f0 17105 type = lookup_reference_type (target_type, refcode);
e142c38c 17106 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17107 if (attr)
17108 {
17109 TYPE_LENGTH (type) = DW_UNSND (attr);
17110 }
17111 else
17112 {
107d2387 17113 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17114 }
f792889a 17115 return set_die_type (die, type, cu);
c906108c
SS
17116}
17117
cf363f18
MW
17118/* Add the given cv-qualifiers to the element type of the array. GCC
17119 outputs DWARF type qualifiers that apply to an array, not the
17120 element type. But GDB relies on the array element type to carry
17121 the cv-qualifiers. This mimics section 6.7.3 of the C99
17122 specification. */
17123
17124static struct type *
17125add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17126 struct type *base_type, int cnst, int voltl)
17127{
17128 struct type *el_type, *inner_array;
17129
17130 base_type = copy_type (base_type);
17131 inner_array = base_type;
17132
17133 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17134 {
17135 TYPE_TARGET_TYPE (inner_array) =
17136 copy_type (TYPE_TARGET_TYPE (inner_array));
17137 inner_array = TYPE_TARGET_TYPE (inner_array);
17138 }
17139
17140 el_type = TYPE_TARGET_TYPE (inner_array);
17141 cnst |= TYPE_CONST (el_type);
17142 voltl |= TYPE_VOLATILE (el_type);
17143 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17144
17145 return set_die_type (die, base_type, cu);
17146}
17147
f792889a 17148static struct type *
e7c27a73 17149read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17150{
f792889a 17151 struct type *base_type, *cv_type;
c906108c 17152
e7c27a73 17153 base_type = die_type (die, cu);
7e314c57
JK
17154
17155 /* The die_type call above may have already set the type for this DIE. */
17156 cv_type = get_die_type (die, cu);
17157 if (cv_type)
17158 return cv_type;
17159
2f608a3a
KW
17160 /* In case the const qualifier is applied to an array type, the element type
17161 is so qualified, not the array type (section 6.7.3 of C99). */
17162 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17163 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17164
f792889a
DJ
17165 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17166 return set_die_type (die, cv_type, cu);
c906108c
SS
17167}
17168
f792889a 17169static struct type *
e7c27a73 17170read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17171{
f792889a 17172 struct type *base_type, *cv_type;
c906108c 17173
e7c27a73 17174 base_type = die_type (die, cu);
7e314c57
JK
17175
17176 /* The die_type call above may have already set the type for this DIE. */
17177 cv_type = get_die_type (die, cu);
17178 if (cv_type)
17179 return cv_type;
17180
cf363f18
MW
17181 /* In case the volatile qualifier is applied to an array type, the
17182 element type is so qualified, not the array type (section 6.7.3
17183 of C99). */
17184 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17185 return add_array_cv_type (die, cu, base_type, 0, 1);
17186
f792889a
DJ
17187 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17188 return set_die_type (die, cv_type, cu);
c906108c
SS
17189}
17190
06d66ee9
TT
17191/* Handle DW_TAG_restrict_type. */
17192
17193static struct type *
17194read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17195{
17196 struct type *base_type, *cv_type;
17197
17198 base_type = die_type (die, cu);
17199
17200 /* The die_type call above may have already set the type for this DIE. */
17201 cv_type = get_die_type (die, cu);
17202 if (cv_type)
17203 return cv_type;
17204
17205 cv_type = make_restrict_type (base_type);
17206 return set_die_type (die, cv_type, cu);
17207}
17208
a2c2acaf
MW
17209/* Handle DW_TAG_atomic_type. */
17210
17211static struct type *
17212read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17213{
17214 struct type *base_type, *cv_type;
17215
17216 base_type = die_type (die, cu);
17217
17218 /* The die_type call above may have already set the type for this DIE. */
17219 cv_type = get_die_type (die, cu);
17220 if (cv_type)
17221 return cv_type;
17222
17223 cv_type = make_atomic_type (base_type);
17224 return set_die_type (die, cv_type, cu);
17225}
17226
c906108c
SS
17227/* Extract all information from a DW_TAG_string_type DIE and add to
17228 the user defined type vector. It isn't really a user defined type,
17229 but it behaves like one, with other DIE's using an AT_user_def_type
17230 attribute to reference it. */
17231
f792889a 17232static struct type *
e7c27a73 17233read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17234{
518817b3 17235 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17236 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17237 struct type *type, *range_type, *index_type, *char_type;
17238 struct attribute *attr;
17239 unsigned int length;
17240
e142c38c 17241 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17242 if (attr)
17243 {
17244 length = DW_UNSND (attr);
17245 }
17246 else
17247 {
0963b4bd 17248 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17249 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17250 if (attr)
17251 {
17252 length = DW_UNSND (attr);
17253 }
17254 else
17255 {
17256 length = 1;
17257 }
c906108c 17258 }
6ccb9162 17259
46bf5051 17260 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17261 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17262 char_type = language_string_char_type (cu->language_defn, gdbarch);
17263 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17264
f792889a 17265 return set_die_type (die, type, cu);
c906108c
SS
17266}
17267
4d804846
JB
17268/* Assuming that DIE corresponds to a function, returns nonzero
17269 if the function is prototyped. */
17270
17271static int
17272prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17273{
17274 struct attribute *attr;
17275
17276 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17277 if (attr && (DW_UNSND (attr) != 0))
17278 return 1;
17279
17280 /* The DWARF standard implies that the DW_AT_prototyped attribute
17281 is only meaninful for C, but the concept also extends to other
17282 languages that allow unprototyped functions (Eg: Objective C).
17283 For all other languages, assume that functions are always
17284 prototyped. */
17285 if (cu->language != language_c
17286 && cu->language != language_objc
17287 && cu->language != language_opencl)
17288 return 1;
17289
17290 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17291 prototyped and unprototyped functions; default to prototyped,
17292 since that is more common in modern code (and RealView warns
17293 about unprototyped functions). */
17294 if (producer_is_realview (cu->producer))
17295 return 1;
17296
17297 return 0;
17298}
17299
c906108c
SS
17300/* Handle DIES due to C code like:
17301
17302 struct foo
c5aa993b
JM
17303 {
17304 int (*funcp)(int a, long l);
17305 int b;
17306 };
c906108c 17307
0963b4bd 17308 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17309
f792889a 17310static struct type *
e7c27a73 17311read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17312{
518817b3 17313 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17314 struct type *type; /* Type that this function returns. */
17315 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17316 struct attribute *attr;
17317
e7c27a73 17318 type = die_type (die, cu);
7e314c57
JK
17319
17320 /* The die_type call above may have already set the type for this DIE. */
17321 ftype = get_die_type (die, cu);
17322 if (ftype)
17323 return ftype;
17324
0c8b41f1 17325 ftype = lookup_function_type (type);
c906108c 17326
4d804846 17327 if (prototyped_function_p (die, cu))
a6c727b2 17328 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17329
c055b101
CV
17330 /* Store the calling convention in the type if it's available in
17331 the subroutine die. Otherwise set the calling convention to
17332 the default value DW_CC_normal. */
17333 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17334 if (attr)
17335 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17336 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17337 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17338 else
17339 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17340
743649fd
MW
17341 /* Record whether the function returns normally to its caller or not
17342 if the DWARF producer set that information. */
17343 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17344 if (attr && (DW_UNSND (attr) != 0))
17345 TYPE_NO_RETURN (ftype) = 1;
17346
76c10ea2
GM
17347 /* We need to add the subroutine type to the die immediately so
17348 we don't infinitely recurse when dealing with parameters
0963b4bd 17349 declared as the same subroutine type. */
76c10ea2 17350 set_die_type (die, ftype, cu);
6e70227d 17351
639d11d3 17352 if (die->child != NULL)
c906108c 17353 {
bb5ed363 17354 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17355 struct die_info *child_die;
8072405b 17356 int nparams, iparams;
c906108c
SS
17357
17358 /* Count the number of parameters.
17359 FIXME: GDB currently ignores vararg functions, but knows about
17360 vararg member functions. */
8072405b 17361 nparams = 0;
639d11d3 17362 child_die = die->child;
c906108c
SS
17363 while (child_die && child_die->tag)
17364 {
17365 if (child_die->tag == DW_TAG_formal_parameter)
17366 nparams++;
17367 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17368 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17369 child_die = sibling_die (child_die);
17370 }
17371
17372 /* Allocate storage for parameters and fill them in. */
17373 TYPE_NFIELDS (ftype) = nparams;
17374 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17375 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17376
8072405b
JK
17377 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17378 even if we error out during the parameters reading below. */
17379 for (iparams = 0; iparams < nparams; iparams++)
17380 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17381
17382 iparams = 0;
639d11d3 17383 child_die = die->child;
c906108c
SS
17384 while (child_die && child_die->tag)
17385 {
17386 if (child_die->tag == DW_TAG_formal_parameter)
17387 {
3ce3b1ba
PA
17388 struct type *arg_type;
17389
17390 /* DWARF version 2 has no clean way to discern C++
17391 static and non-static member functions. G++ helps
17392 GDB by marking the first parameter for non-static
17393 member functions (which is the this pointer) as
17394 artificial. We pass this information to
17395 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17396
17397 DWARF version 3 added DW_AT_object_pointer, which GCC
17398 4.5 does not yet generate. */
e142c38c 17399 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17400 if (attr)
17401 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17402 else
9c37b5ae 17403 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17404 arg_type = die_type (child_die, cu);
17405
17406 /* RealView does not mark THIS as const, which the testsuite
17407 expects. GCC marks THIS as const in method definitions,
17408 but not in the class specifications (GCC PR 43053). */
17409 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17410 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17411 {
17412 int is_this = 0;
17413 struct dwarf2_cu *arg_cu = cu;
17414 const char *name = dwarf2_name (child_die, cu);
17415
17416 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17417 if (attr)
17418 {
17419 /* If the compiler emits this, use it. */
17420 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17421 is_this = 1;
17422 }
17423 else if (name && strcmp (name, "this") == 0)
17424 /* Function definitions will have the argument names. */
17425 is_this = 1;
17426 else if (name == NULL && iparams == 0)
17427 /* Declarations may not have the names, so like
17428 elsewhere in GDB, assume an artificial first
17429 argument is "this". */
17430 is_this = 1;
17431
17432 if (is_this)
17433 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17434 arg_type, 0);
17435 }
17436
17437 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17438 iparams++;
17439 }
17440 child_die = sibling_die (child_die);
17441 }
17442 }
17443
76c10ea2 17444 return ftype;
c906108c
SS
17445}
17446
f792889a 17447static struct type *
e7c27a73 17448read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17449{
518817b3 17450 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17451 const char *name = NULL;
3c8e0968 17452 struct type *this_type, *target_type;
c906108c 17453
94af9270 17454 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17455 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17456 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17457 set_die_type (die, this_type, cu);
3c8e0968
DE
17458 target_type = die_type (die, cu);
17459 if (target_type != this_type)
17460 TYPE_TARGET_TYPE (this_type) = target_type;
17461 else
17462 {
17463 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17464 spec and cause infinite loops in GDB. */
17465 complaint (&symfile_complaints,
17466 _("Self-referential DW_TAG_typedef "
17467 "- DIE at 0x%x [in module %s]"),
9c541725 17468 to_underlying (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17469 TYPE_TARGET_TYPE (this_type) = NULL;
17470 }
f792889a 17471 return this_type;
c906108c
SS
17472}
17473
9b790ce7
UW
17474/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17475 (which may be different from NAME) to the architecture back-end to allow
17476 it to guess the correct format if necessary. */
17477
17478static struct type *
17479dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17480 const char *name_hint)
17481{
17482 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17483 const struct floatformat **format;
17484 struct type *type;
17485
17486 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17487 if (format)
17488 type = init_float_type (objfile, bits, name, format);
17489 else
77b7c781 17490 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17491
17492 return type;
17493}
17494
c906108c
SS
17495/* Find a representation of a given base type and install
17496 it in the TYPE field of the die. */
17497
f792889a 17498static struct type *
e7c27a73 17499read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17500{
518817b3 17501 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17502 struct type *type;
17503 struct attribute *attr;
19f392bc 17504 int encoding = 0, bits = 0;
15d034d0 17505 const char *name;
c906108c 17506
e142c38c 17507 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17508 if (attr)
17509 {
17510 encoding = DW_UNSND (attr);
17511 }
e142c38c 17512 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17513 if (attr)
17514 {
19f392bc 17515 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17516 }
39cbfefa 17517 name = dwarf2_name (die, cu);
6ccb9162 17518 if (!name)
c906108c 17519 {
6ccb9162
UW
17520 complaint (&symfile_complaints,
17521 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17522 }
6ccb9162
UW
17523
17524 switch (encoding)
c906108c 17525 {
6ccb9162
UW
17526 case DW_ATE_address:
17527 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17528 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17529 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17530 break;
17531 case DW_ATE_boolean:
19f392bc 17532 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17533 break;
17534 case DW_ATE_complex_float:
9b790ce7 17535 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 17536 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17537 break;
17538 case DW_ATE_decimal_float:
19f392bc 17539 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17540 break;
17541 case DW_ATE_float:
9b790ce7 17542 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17543 break;
17544 case DW_ATE_signed:
19f392bc 17545 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17546 break;
17547 case DW_ATE_unsigned:
3b2b8fea
TT
17548 if (cu->language == language_fortran
17549 && name
61012eef 17550 && startswith (name, "character("))
19f392bc
UW
17551 type = init_character_type (objfile, bits, 1, name);
17552 else
17553 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
17554 break;
17555 case DW_ATE_signed_char:
6e70227d 17556 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17557 || cu->language == language_pascal
17558 || cu->language == language_fortran)
19f392bc
UW
17559 type = init_character_type (objfile, bits, 0, name);
17560 else
17561 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17562 break;
17563 case DW_ATE_unsigned_char:
868a0084 17564 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17565 || cu->language == language_pascal
c44af4eb
TT
17566 || cu->language == language_fortran
17567 || cu->language == language_rust)
19f392bc
UW
17568 type = init_character_type (objfile, bits, 1, name);
17569 else
17570 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 17571 break;
75079b2b 17572 case DW_ATE_UTF:
53e710ac
PA
17573 {
17574 gdbarch *arch = get_objfile_arch (objfile);
17575
17576 if (bits == 16)
17577 type = builtin_type (arch)->builtin_char16;
17578 else if (bits == 32)
17579 type = builtin_type (arch)->builtin_char32;
17580 else
17581 {
17582 complaint (&symfile_complaints,
17583 _("unsupported DW_ATE_UTF bit size: '%d'"),
17584 bits);
17585 type = init_integer_type (objfile, bits, 1, name);
17586 }
17587 return set_die_type (die, type, cu);
17588 }
75079b2b
TT
17589 break;
17590
6ccb9162
UW
17591 default:
17592 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17593 dwarf_type_encoding_name (encoding));
77b7c781 17594 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17595 break;
c906108c 17596 }
6ccb9162 17597
0114d602 17598 if (name && strcmp (name, "char") == 0)
876cecd0 17599 TYPE_NOSIGN (type) = 1;
0114d602 17600
f792889a 17601 return set_die_type (die, type, cu);
c906108c
SS
17602}
17603
80180f79
SA
17604/* Parse dwarf attribute if it's a block, reference or constant and put the
17605 resulting value of the attribute into struct bound_prop.
17606 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17607
17608static int
17609attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17610 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17611{
17612 struct dwarf2_property_baton *baton;
518817b3
SM
17613 struct obstack *obstack
17614 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79
SA
17615
17616 if (attr == NULL || prop == NULL)
17617 return 0;
17618
17619 if (attr_form_is_block (attr))
17620 {
8d749320 17621 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
17622 baton->referenced_type = NULL;
17623 baton->locexpr.per_cu = cu->per_cu;
17624 baton->locexpr.size = DW_BLOCK (attr)->size;
17625 baton->locexpr.data = DW_BLOCK (attr)->data;
17626 prop->data.baton = baton;
17627 prop->kind = PROP_LOCEXPR;
17628 gdb_assert (prop->data.baton != NULL);
17629 }
17630 else if (attr_form_is_ref (attr))
17631 {
17632 struct dwarf2_cu *target_cu = cu;
17633 struct die_info *target_die;
17634 struct attribute *target_attr;
17635
17636 target_die = follow_die_ref (die, attr, &target_cu);
17637 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17638 if (target_attr == NULL)
17639 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17640 target_cu);
80180f79
SA
17641 if (target_attr == NULL)
17642 return 0;
17643
df25ebbd 17644 switch (target_attr->name)
80180f79 17645 {
df25ebbd
JB
17646 case DW_AT_location:
17647 if (attr_form_is_section_offset (target_attr))
17648 {
8d749320 17649 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17650 baton->referenced_type = die_type (target_die, target_cu);
17651 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17652 prop->data.baton = baton;
17653 prop->kind = PROP_LOCLIST;
17654 gdb_assert (prop->data.baton != NULL);
17655 }
17656 else if (attr_form_is_block (target_attr))
17657 {
8d749320 17658 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17659 baton->referenced_type = die_type (target_die, target_cu);
17660 baton->locexpr.per_cu = cu->per_cu;
17661 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17662 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17663 prop->data.baton = baton;
17664 prop->kind = PROP_LOCEXPR;
17665 gdb_assert (prop->data.baton != NULL);
17666 }
17667 else
17668 {
17669 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17670 "dynamic property");
17671 return 0;
17672 }
17673 break;
17674 case DW_AT_data_member_location:
17675 {
17676 LONGEST offset;
17677
17678 if (!handle_data_member_location (target_die, target_cu,
17679 &offset))
17680 return 0;
17681
8d749320 17682 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
17683 baton->referenced_type = read_type_die (target_die->parent,
17684 target_cu);
df25ebbd
JB
17685 baton->offset_info.offset = offset;
17686 baton->offset_info.type = die_type (target_die, target_cu);
17687 prop->data.baton = baton;
17688 prop->kind = PROP_ADDR_OFFSET;
17689 break;
17690 }
80180f79
SA
17691 }
17692 }
17693 else if (attr_form_is_constant (attr))
17694 {
17695 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17696 prop->kind = PROP_CONST;
17697 }
17698 else
17699 {
17700 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17701 dwarf2_name (die, cu));
17702 return 0;
17703 }
17704
17705 return 1;
17706}
17707
a02abb62
JB
17708/* Read the given DW_AT_subrange DIE. */
17709
f792889a 17710static struct type *
a02abb62
JB
17711read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17712{
4c9ad8c2 17713 struct type *base_type, *orig_base_type;
a02abb62
JB
17714 struct type *range_type;
17715 struct attribute *attr;
729efb13 17716 struct dynamic_prop low, high;
4fae6e18 17717 int low_default_is_valid;
c451ebe5 17718 int high_bound_is_count = 0;
15d034d0 17719 const char *name;
43bbcdc2 17720 LONGEST negative_mask;
e77813c8 17721
4c9ad8c2
TT
17722 orig_base_type = die_type (die, cu);
17723 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17724 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17725 creating the range type, but we use the result of check_typedef
17726 when examining properties of the type. */
17727 base_type = check_typedef (orig_base_type);
a02abb62 17728
7e314c57
JK
17729 /* The die_type call above may have already set the type for this DIE. */
17730 range_type = get_die_type (die, cu);
17731 if (range_type)
17732 return range_type;
17733
729efb13
SA
17734 low.kind = PROP_CONST;
17735 high.kind = PROP_CONST;
17736 high.data.const_val = 0;
17737
4fae6e18
JK
17738 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17739 omitting DW_AT_lower_bound. */
17740 switch (cu->language)
6e70227d 17741 {
4fae6e18
JK
17742 case language_c:
17743 case language_cplus:
729efb13 17744 low.data.const_val = 0;
4fae6e18
JK
17745 low_default_is_valid = 1;
17746 break;
17747 case language_fortran:
729efb13 17748 low.data.const_val = 1;
4fae6e18
JK
17749 low_default_is_valid = 1;
17750 break;
17751 case language_d:
4fae6e18 17752 case language_objc:
c44af4eb 17753 case language_rust:
729efb13 17754 low.data.const_val = 0;
4fae6e18
JK
17755 low_default_is_valid = (cu->header.version >= 4);
17756 break;
17757 case language_ada:
17758 case language_m2:
17759 case language_pascal:
729efb13 17760 low.data.const_val = 1;
4fae6e18
JK
17761 low_default_is_valid = (cu->header.version >= 4);
17762 break;
17763 default:
729efb13 17764 low.data.const_val = 0;
4fae6e18
JK
17765 low_default_is_valid = 0;
17766 break;
a02abb62
JB
17767 }
17768
e142c38c 17769 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 17770 if (attr)
11c1ba78 17771 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
17772 else if (!low_default_is_valid)
17773 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17774 "- DIE at 0x%x [in module %s]"),
e3b94546 17775 to_underlying (die->sect_off),
518817b3 17776 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17777
e142c38c 17778 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 17779 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
17780 {
17781 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 17782 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 17783 {
c451ebe5
SA
17784 /* If bounds are constant do the final calculation here. */
17785 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17786 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17787 else
17788 high_bound_is_count = 1;
c2ff108b 17789 }
e77813c8
PM
17790 }
17791
17792 /* Dwarf-2 specifications explicitly allows to create subrange types
17793 without specifying a base type.
17794 In that case, the base type must be set to the type of
17795 the lower bound, upper bound or count, in that order, if any of these
17796 three attributes references an object that has a type.
17797 If no base type is found, the Dwarf-2 specifications say that
17798 a signed integer type of size equal to the size of an address should
17799 be used.
17800 For the following C code: `extern char gdb_int [];'
17801 GCC produces an empty range DIE.
17802 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 17803 high bound or count are not yet handled by this code. */
e77813c8
PM
17804 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17805 {
518817b3 17806 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e77813c8
PM
17807 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17808 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17809 struct type *int_type = objfile_type (objfile)->builtin_int;
17810
17811 /* Test "int", "long int", and "long long int" objfile types,
17812 and select the first one having a size above or equal to the
17813 architecture address size. */
17814 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17815 base_type = int_type;
17816 else
17817 {
17818 int_type = objfile_type (objfile)->builtin_long;
17819 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17820 base_type = int_type;
17821 else
17822 {
17823 int_type = objfile_type (objfile)->builtin_long_long;
17824 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17825 base_type = int_type;
17826 }
17827 }
17828 }
a02abb62 17829
dbb9c2b1
JB
17830 /* Normally, the DWARF producers are expected to use a signed
17831 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17832 But this is unfortunately not always the case, as witnessed
17833 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17834 is used instead. To work around that ambiguity, we treat
17835 the bounds as signed, and thus sign-extend their values, when
17836 the base type is signed. */
6e70227d 17837 negative_mask =
66c6502d 17838 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17839 if (low.kind == PROP_CONST
17840 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17841 low.data.const_val |= negative_mask;
17842 if (high.kind == PROP_CONST
17843 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17844 high.data.const_val |= negative_mask;
43bbcdc2 17845
729efb13 17846 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 17847
c451ebe5
SA
17848 if (high_bound_is_count)
17849 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17850
c2ff108b
JK
17851 /* Ada expects an empty array on no boundary attributes. */
17852 if (attr == NULL && cu->language != language_ada)
729efb13 17853 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17854
39cbfefa
DJ
17855 name = dwarf2_name (die, cu);
17856 if (name)
17857 TYPE_NAME (range_type) = name;
6e70227d 17858
e142c38c 17859 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
17860 if (attr)
17861 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17862
7e314c57
JK
17863 set_die_type (die, range_type, cu);
17864
17865 /* set_die_type should be already done. */
b4ba55a1
JB
17866 set_descriptive_type (range_type, die, cu);
17867
7e314c57 17868 return range_type;
a02abb62 17869}
6e70227d 17870
f792889a 17871static struct type *
81a17f79
JB
17872read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17873{
17874 struct type *type;
81a17f79 17875
518817b3
SM
17876 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17877 NULL);
0114d602 17878 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17879
74a2f8ff
JB
17880 /* In Ada, an unspecified type is typically used when the description
17881 of the type is defered to a different unit. When encountering
17882 such a type, we treat it as a stub, and try to resolve it later on,
17883 when needed. */
17884 if (cu->language == language_ada)
17885 TYPE_STUB (type) = 1;
17886
f792889a 17887 return set_die_type (die, type, cu);
81a17f79 17888}
a02abb62 17889
639d11d3
DC
17890/* Read a single die and all its descendents. Set the die's sibling
17891 field to NULL; set other fields in the die correctly, and set all
17892 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17893 location of the info_ptr after reading all of those dies. PARENT
17894 is the parent of the die in question. */
17895
17896static struct die_info *
dee91e82 17897read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17898 const gdb_byte *info_ptr,
17899 const gdb_byte **new_info_ptr,
dee91e82 17900 struct die_info *parent)
639d11d3
DC
17901{
17902 struct die_info *die;
d521ce57 17903 const gdb_byte *cur_ptr;
639d11d3
DC
17904 int has_children;
17905
bf6af496 17906 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
17907 if (die == NULL)
17908 {
17909 *new_info_ptr = cur_ptr;
17910 return NULL;
17911 }
93311388 17912 store_in_ref_table (die, reader->cu);
639d11d3
DC
17913
17914 if (has_children)
bf6af496 17915 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17916 else
17917 {
17918 die->child = NULL;
17919 *new_info_ptr = cur_ptr;
17920 }
17921
17922 die->sibling = NULL;
17923 die->parent = parent;
17924 return die;
17925}
17926
17927/* Read a die, all of its descendents, and all of its siblings; set
17928 all of the fields of all of the dies correctly. Arguments are as
17929 in read_die_and_children. */
17930
17931static struct die_info *
bf6af496 17932read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17933 const gdb_byte *info_ptr,
17934 const gdb_byte **new_info_ptr,
bf6af496 17935 struct die_info *parent)
639d11d3
DC
17936{
17937 struct die_info *first_die, *last_sibling;
d521ce57 17938 const gdb_byte *cur_ptr;
639d11d3 17939
c906108c 17940 cur_ptr = info_ptr;
639d11d3
DC
17941 first_die = last_sibling = NULL;
17942
17943 while (1)
c906108c 17944 {
639d11d3 17945 struct die_info *die
dee91e82 17946 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17947
1d325ec1 17948 if (die == NULL)
c906108c 17949 {
639d11d3
DC
17950 *new_info_ptr = cur_ptr;
17951 return first_die;
c906108c 17952 }
1d325ec1
DJ
17953
17954 if (!first_die)
17955 first_die = die;
c906108c 17956 else
1d325ec1
DJ
17957 last_sibling->sibling = die;
17958
17959 last_sibling = die;
c906108c 17960 }
c906108c
SS
17961}
17962
bf6af496
DE
17963/* Read a die, all of its descendents, and all of its siblings; set
17964 all of the fields of all of the dies correctly. Arguments are as
17965 in read_die_and_children.
17966 This the main entry point for reading a DIE and all its children. */
17967
17968static struct die_info *
17969read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17970 const gdb_byte *info_ptr,
17971 const gdb_byte **new_info_ptr,
bf6af496
DE
17972 struct die_info *parent)
17973{
17974 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17975 new_info_ptr, parent);
17976
b4f54984 17977 if (dwarf_die_debug)
bf6af496
DE
17978 {
17979 fprintf_unfiltered (gdb_stdlog,
17980 "Read die from %s@0x%x of %s:\n",
a32a8923 17981 get_section_name (reader->die_section),
bf6af496
DE
17982 (unsigned) (info_ptr - reader->die_section->buffer),
17983 bfd_get_filename (reader->abfd));
b4f54984 17984 dump_die (die, dwarf_die_debug);
bf6af496
DE
17985 }
17986
17987 return die;
17988}
17989
3019eac3
DE
17990/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17991 attributes.
17992 The caller is responsible for filling in the extra attributes
17993 and updating (*DIEP)->num_attrs.
17994 Set DIEP to point to a newly allocated die with its information,
17995 except for its child, sibling, and parent fields.
17996 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 17997
d521ce57 17998static const gdb_byte *
3019eac3 17999read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18000 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18001 int *has_children, int num_extra_attrs)
93311388 18002{
b64f50a1 18003 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18004 struct abbrev_info *abbrev;
18005 struct die_info *die;
18006 struct dwarf2_cu *cu = reader->cu;
18007 bfd *abfd = reader->abfd;
18008
9c541725 18009 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18010 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18011 info_ptr += bytes_read;
18012 if (!abbrev_number)
18013 {
18014 *diep = NULL;
18015 *has_children = 0;
18016 return info_ptr;
18017 }
18018
433df2d4 18019 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 18020 if (!abbrev)
348e048f
DE
18021 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18022 abbrev_number,
18023 bfd_get_filename (abfd));
18024
3019eac3 18025 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18026 die->sect_off = sect_off;
93311388
DE
18027 die->tag = abbrev->tag;
18028 die->abbrev = abbrev_number;
18029
3019eac3
DE
18030 /* Make the result usable.
18031 The caller needs to update num_attrs after adding the extra
18032 attributes. */
93311388
DE
18033 die->num_attrs = abbrev->num_attrs;
18034
18035 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
18036 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18037 info_ptr);
93311388
DE
18038
18039 *diep = die;
18040 *has_children = abbrev->has_children;
18041 return info_ptr;
18042}
18043
3019eac3
DE
18044/* Read a die and all its attributes.
18045 Set DIEP to point to a newly allocated die with its information,
18046 except for its child, sibling, and parent fields.
18047 Set HAS_CHILDREN to tell whether the die has children or not. */
18048
d521ce57 18049static const gdb_byte *
3019eac3 18050read_full_die (const struct die_reader_specs *reader,
d521ce57 18051 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18052 int *has_children)
18053{
d521ce57 18054 const gdb_byte *result;
bf6af496
DE
18055
18056 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18057
b4f54984 18058 if (dwarf_die_debug)
bf6af496
DE
18059 {
18060 fprintf_unfiltered (gdb_stdlog,
18061 "Read die from %s@0x%x of %s:\n",
a32a8923 18062 get_section_name (reader->die_section),
bf6af496
DE
18063 (unsigned) (info_ptr - reader->die_section->buffer),
18064 bfd_get_filename (reader->abfd));
b4f54984 18065 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18066 }
18067
18068 return result;
3019eac3 18069}
433df2d4
DE
18070\f
18071/* Abbreviation tables.
3019eac3 18072
433df2d4 18073 In DWARF version 2, the description of the debugging information is
c906108c
SS
18074 stored in a separate .debug_abbrev section. Before we read any
18075 dies from a section we read in all abbreviations and install them
433df2d4
DE
18076 in a hash table. */
18077
18078/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18079
18080static struct abbrev_info *
18081abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
18082{
18083 struct abbrev_info *abbrev;
18084
8d749320 18085 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 18086 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18087
433df2d4
DE
18088 return abbrev;
18089}
18090
18091/* Add an abbreviation to the table. */
c906108c
SS
18092
18093static void
433df2d4
DE
18094abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
18095 unsigned int abbrev_number,
18096 struct abbrev_info *abbrev)
18097{
18098 unsigned int hash_number;
18099
18100 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18101 abbrev->next = abbrev_table->abbrevs[hash_number];
18102 abbrev_table->abbrevs[hash_number] = abbrev;
18103}
dee91e82 18104
433df2d4
DE
18105/* Look up an abbrev in the table.
18106 Returns NULL if the abbrev is not found. */
18107
18108static struct abbrev_info *
18109abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
18110 unsigned int abbrev_number)
c906108c 18111{
433df2d4
DE
18112 unsigned int hash_number;
18113 struct abbrev_info *abbrev;
18114
18115 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18116 abbrev = abbrev_table->abbrevs[hash_number];
18117
18118 while (abbrev)
18119 {
18120 if (abbrev->number == abbrev_number)
18121 return abbrev;
18122 abbrev = abbrev->next;
18123 }
18124 return NULL;
18125}
18126
18127/* Read in an abbrev table. */
18128
18129static struct abbrev_table *
ed2dc618
SM
18130abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18131 struct dwarf2_section_info *section,
9c541725 18132 sect_offset sect_off)
433df2d4
DE
18133{
18134 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18135 bfd *abfd = get_section_bfd_owner (section);
433df2d4 18136 struct abbrev_table *abbrev_table;
d521ce57 18137 const gdb_byte *abbrev_ptr;
c906108c
SS
18138 struct abbrev_info *cur_abbrev;
18139 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18140 unsigned int abbrev_form;
f3dd6933
DJ
18141 struct attr_abbrev *cur_attrs;
18142 unsigned int allocated_attrs;
c906108c 18143
70ba0933 18144 abbrev_table = XNEW (struct abbrev_table);
9c541725 18145 abbrev_table->sect_off = sect_off;
433df2d4 18146 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
18147 abbrev_table->abbrevs =
18148 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
18149 ABBREV_HASH_SIZE);
433df2d4
DE
18150 memset (abbrev_table->abbrevs, 0,
18151 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 18152
433df2d4 18153 dwarf2_read_section (objfile, section);
9c541725 18154 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18155 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18156 abbrev_ptr += bytes_read;
18157
f3dd6933 18158 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 18159 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 18160
0963b4bd 18161 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18162 while (abbrev_number)
18163 {
433df2d4 18164 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
18165
18166 /* read in abbrev header */
18167 cur_abbrev->number = abbrev_number;
aead7601
SM
18168 cur_abbrev->tag
18169 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18170 abbrev_ptr += bytes_read;
18171 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18172 abbrev_ptr += 1;
18173
18174 /* now read in declarations */
22d2f3ab 18175 for (;;)
c906108c 18176 {
43988095
JK
18177 LONGEST implicit_const;
18178
22d2f3ab
JK
18179 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18180 abbrev_ptr += bytes_read;
18181 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18182 abbrev_ptr += bytes_read;
43988095
JK
18183 if (abbrev_form == DW_FORM_implicit_const)
18184 {
18185 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18186 &bytes_read);
18187 abbrev_ptr += bytes_read;
18188 }
18189 else
18190 {
18191 /* Initialize it due to a false compiler warning. */
18192 implicit_const = -1;
18193 }
22d2f3ab
JK
18194
18195 if (abbrev_name == 0)
18196 break;
18197
f3dd6933 18198 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18199 {
f3dd6933
DJ
18200 allocated_attrs += ATTR_ALLOC_CHUNK;
18201 cur_attrs
224c3ddb 18202 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18203 }
ae038cb0 18204
aead7601
SM
18205 cur_attrs[cur_abbrev->num_attrs].name
18206 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18207 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18208 = (enum dwarf_form) abbrev_form;
43988095 18209 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18210 ++cur_abbrev->num_attrs;
c906108c
SS
18211 }
18212
8d749320
SM
18213 cur_abbrev->attrs =
18214 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18215 cur_abbrev->num_attrs);
f3dd6933
DJ
18216 memcpy (cur_abbrev->attrs, cur_attrs,
18217 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18218
433df2d4 18219 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
18220
18221 /* Get next abbreviation.
18222 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18223 always properly terminated with an abbrev number of 0.
18224 Exit loop if we encounter an abbreviation which we have
18225 already read (which means we are about to read the abbreviations
18226 for the next compile unit) or if the end of the abbreviation
18227 table is reached. */
433df2d4 18228 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18229 break;
18230 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18231 abbrev_ptr += bytes_read;
433df2d4 18232 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
18233 break;
18234 }
f3dd6933
DJ
18235
18236 xfree (cur_attrs);
433df2d4 18237 return abbrev_table;
c906108c
SS
18238}
18239
433df2d4 18240/* Free the resources held by ABBREV_TABLE. */
c906108c 18241
c906108c 18242static void
433df2d4 18243abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 18244{
433df2d4
DE
18245 obstack_free (&abbrev_table->abbrev_obstack, NULL);
18246 xfree (abbrev_table);
c906108c
SS
18247}
18248
f4dc4d17
DE
18249/* Same as abbrev_table_free but as a cleanup.
18250 We pass in a pointer to the pointer to the table so that we can
18251 set the pointer to NULL when we're done. It also simplifies
73051182 18252 build_type_psymtabs_1. */
f4dc4d17
DE
18253
18254static void
18255abbrev_table_free_cleanup (void *table_ptr)
18256{
9a3c8263 18257 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
18258
18259 if (*abbrev_table_ptr != NULL)
18260 abbrev_table_free (*abbrev_table_ptr);
18261 *abbrev_table_ptr = NULL;
18262}
18263
433df2d4
DE
18264/* Read the abbrev table for CU from ABBREV_SECTION. */
18265
18266static void
18267dwarf2_read_abbrevs (struct dwarf2_cu *cu,
18268 struct dwarf2_section_info *abbrev_section)
c906108c 18269{
433df2d4 18270 cu->abbrev_table =
518817b3 18271 abbrev_table_read_table (cu->per_cu->dwarf2_per_objfile, abbrev_section,
ed2dc618 18272 cu->header.abbrev_sect_off);
433df2d4 18273}
c906108c 18274
433df2d4 18275/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 18276
433df2d4
DE
18277static void
18278dwarf2_free_abbrev_table (void *ptr_to_cu)
18279{
9a3c8263 18280 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 18281
a2ce51a0
DE
18282 if (cu->abbrev_table != NULL)
18283 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
18284 /* Set this to NULL so that we SEGV if we try to read it later,
18285 and also because free_comp_unit verifies this is NULL. */
18286 cu->abbrev_table = NULL;
18287}
18288\f
72bf9492
DJ
18289/* Returns nonzero if TAG represents a type that we might generate a partial
18290 symbol for. */
18291
18292static int
18293is_type_tag_for_partial (int tag)
18294{
18295 switch (tag)
18296 {
18297#if 0
18298 /* Some types that would be reasonable to generate partial symbols for,
18299 that we don't at present. */
18300 case DW_TAG_array_type:
18301 case DW_TAG_file_type:
18302 case DW_TAG_ptr_to_member_type:
18303 case DW_TAG_set_type:
18304 case DW_TAG_string_type:
18305 case DW_TAG_subroutine_type:
18306#endif
18307 case DW_TAG_base_type:
18308 case DW_TAG_class_type:
680b30c7 18309 case DW_TAG_interface_type:
72bf9492
DJ
18310 case DW_TAG_enumeration_type:
18311 case DW_TAG_structure_type:
18312 case DW_TAG_subrange_type:
18313 case DW_TAG_typedef:
18314 case DW_TAG_union_type:
18315 return 1;
18316 default:
18317 return 0;
18318 }
18319}
18320
18321/* Load all DIEs that are interesting for partial symbols into memory. */
18322
18323static struct partial_die_info *
dee91e82 18324load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18325 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18326{
dee91e82 18327 struct dwarf2_cu *cu = reader->cu;
518817b3 18328 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492
DJ
18329 struct partial_die_info *part_die;
18330 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18331 struct abbrev_info *abbrev;
18332 unsigned int bytes_read;
5afb4e99 18333 unsigned int load_all = 0;
72bf9492
DJ
18334 int nesting_level = 1;
18335
18336 parent_die = NULL;
18337 last_die = NULL;
18338
7adf1e79
DE
18339 gdb_assert (cu->per_cu != NULL);
18340 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18341 load_all = 1;
18342
72bf9492
DJ
18343 cu->partial_dies
18344 = htab_create_alloc_ex (cu->header.length / 12,
18345 partial_die_hash,
18346 partial_die_eq,
18347 NULL,
18348 &cu->comp_unit_obstack,
18349 hashtab_obstack_allocate,
18350 dummy_obstack_deallocate);
18351
8d749320 18352 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
18353
18354 while (1)
18355 {
18356 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
18357
18358 /* A NULL abbrev means the end of a series of children. */
18359 if (abbrev == NULL)
18360 {
18361 if (--nesting_level == 0)
18362 {
18363 /* PART_DIE was probably the last thing allocated on the
18364 comp_unit_obstack, so we could call obstack_free
18365 here. We don't do that because the waste is small,
18366 and will be cleaned up when we're done with this
18367 compilation unit. This way, we're also more robust
18368 against other users of the comp_unit_obstack. */
18369 return first_die;
18370 }
18371 info_ptr += bytes_read;
18372 last_die = parent_die;
18373 parent_die = parent_die->die_parent;
18374 continue;
18375 }
18376
98bfdba5
PA
18377 /* Check for template arguments. We never save these; if
18378 they're seen, we just mark the parent, and go on our way. */
18379 if (parent_die != NULL
18380 && cu->language == language_cplus
18381 && (abbrev->tag == DW_TAG_template_type_param
18382 || abbrev->tag == DW_TAG_template_value_param))
18383 {
18384 parent_die->has_template_arguments = 1;
18385
18386 if (!load_all)
18387 {
18388 /* We don't need a partial DIE for the template argument. */
dee91e82 18389 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18390 continue;
18391 }
18392 }
18393
0d99eb77 18394 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18395 Skip their other children. */
18396 if (!load_all
18397 && cu->language == language_cplus
18398 && parent_die != NULL
18399 && parent_die->tag == DW_TAG_subprogram)
18400 {
dee91e82 18401 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18402 continue;
18403 }
18404
5afb4e99
DJ
18405 /* Check whether this DIE is interesting enough to save. Normally
18406 we would not be interested in members here, but there may be
18407 later variables referencing them via DW_AT_specification (for
18408 static members). */
18409 if (!load_all
18410 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18411 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18412 && abbrev->tag != DW_TAG_enumerator
18413 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18414 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18415 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18416 && abbrev->tag != DW_TAG_variable
5afb4e99 18417 && abbrev->tag != DW_TAG_namespace
f55ee35c 18418 && abbrev->tag != DW_TAG_module
95554aad 18419 && abbrev->tag != DW_TAG_member
74921315
KS
18420 && abbrev->tag != DW_TAG_imported_unit
18421 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18422 {
18423 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18424 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18425 continue;
18426 }
18427
dee91e82
DE
18428 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
18429 info_ptr);
72bf9492
DJ
18430
18431 /* This two-pass algorithm for processing partial symbols has a
18432 high cost in cache pressure. Thus, handle some simple cases
18433 here which cover the majority of C partial symbols. DIEs
18434 which neither have specification tags in them, nor could have
18435 specification tags elsewhere pointing at them, can simply be
18436 processed and discarded.
18437
18438 This segment is also optional; scan_partial_symbols and
18439 add_partial_symbol will handle these DIEs if we chain
18440 them in normally. When compilers which do not emit large
18441 quantities of duplicate debug information are more common,
18442 this code can probably be removed. */
18443
18444 /* Any complete simple types at the top level (pretty much all
18445 of them, for a language without namespaces), can be processed
18446 directly. */
18447 if (parent_die == NULL
18448 && part_die->has_specification == 0
18449 && part_die->is_declaration == 0
d8228535 18450 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
18451 || part_die->tag == DW_TAG_base_type
18452 || part_die->tag == DW_TAG_subrange_type))
18453 {
18454 if (building_psymtab && part_die->name != NULL)
04a679b8 18455 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 18456 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 18457 &objfile->static_psymbols,
1762568f 18458 0, cu->language, objfile);
dee91e82 18459 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
18460 continue;
18461 }
18462
d8228535
JK
18463 /* The exception for DW_TAG_typedef with has_children above is
18464 a workaround of GCC PR debug/47510. In the case of this complaint
18465 type_name_no_tag_or_error will error on such types later.
18466
18467 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18468 it could not find the child DIEs referenced later, this is checked
18469 above. In correct DWARF DW_TAG_typedef should have no children. */
18470
18471 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
18472 complaint (&symfile_complaints,
18473 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18474 "- DIE at 0x%x [in module %s]"),
9c541725 18475 to_underlying (part_die->sect_off), objfile_name (objfile));
d8228535 18476
72bf9492
DJ
18477 /* If we're at the second level, and we're an enumerator, and
18478 our parent has no specification (meaning possibly lives in a
18479 namespace elsewhere), then we can add the partial symbol now
18480 instead of queueing it. */
18481 if (part_die->tag == DW_TAG_enumerator
18482 && parent_die != NULL
18483 && parent_die->die_parent == NULL
18484 && parent_die->tag == DW_TAG_enumeration_type
18485 && parent_die->has_specification == 0)
18486 {
18487 if (part_die->name == NULL)
3e43a32a
MS
18488 complaint (&symfile_complaints,
18489 _("malformed enumerator DIE ignored"));
72bf9492 18490 else if (building_psymtab)
04a679b8 18491 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 18492 VAR_DOMAIN, LOC_CONST,
9c37b5ae 18493 cu->language == language_cplus
bb5ed363
DE
18494 ? &objfile->global_psymbols
18495 : &objfile->static_psymbols,
1762568f 18496 0, cu->language, objfile);
72bf9492 18497
dee91e82 18498 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
18499 continue;
18500 }
18501
18502 /* We'll save this DIE so link it in. */
18503 part_die->die_parent = parent_die;
18504 part_die->die_sibling = NULL;
18505 part_die->die_child = NULL;
18506
18507 if (last_die && last_die == parent_die)
18508 last_die->die_child = part_die;
18509 else if (last_die)
18510 last_die->die_sibling = part_die;
18511
18512 last_die = part_die;
18513
18514 if (first_die == NULL)
18515 first_die = part_die;
18516
18517 /* Maybe add the DIE to the hash table. Not all DIEs that we
18518 find interesting need to be in the hash table, because we
18519 also have the parent/sibling/child chains; only those that we
18520 might refer to by offset later during partial symbol reading.
18521
18522 For now this means things that might have be the target of a
18523 DW_AT_specification, DW_AT_abstract_origin, or
18524 DW_AT_extension. DW_AT_extension will refer only to
18525 namespaces; DW_AT_abstract_origin refers to functions (and
18526 many things under the function DIE, but we do not recurse
18527 into function DIEs during partial symbol reading) and
18528 possibly variables as well; DW_AT_specification refers to
18529 declarations. Declarations ought to have the DW_AT_declaration
18530 flag. It happens that GCC forgets to put it in sometimes, but
18531 only for functions, not for types.
18532
18533 Adding more things than necessary to the hash table is harmless
18534 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18535 wasted time in find_partial_die, when we reread the compilation
18536 unit with load_all_dies set. */
72bf9492 18537
5afb4e99 18538 if (load_all
72929c62 18539 || abbrev->tag == DW_TAG_constant
5afb4e99 18540 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18541 || abbrev->tag == DW_TAG_variable
18542 || abbrev->tag == DW_TAG_namespace
18543 || part_die->is_declaration)
18544 {
18545 void **slot;
18546
18547 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18548 to_underlying (part_die->sect_off),
18549 INSERT);
72bf9492
DJ
18550 *slot = part_die;
18551 }
18552
8d749320 18553 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
18554
18555 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18556 we have no reason to follow the children of structures; for other
98bfdba5
PA
18557 languages we have to, so that we can get at method physnames
18558 to infer fully qualified class names, for DW_AT_specification,
18559 and for C++ template arguments. For C++, we also look one level
18560 inside functions to find template arguments (if the name of the
18561 function does not already contain the template arguments).
bc30ff58
JB
18562
18563 For Ada, we need to scan the children of subprograms and lexical
18564 blocks as well because Ada allows the definition of nested
18565 entities that could be interesting for the debugger, such as
18566 nested subprograms for instance. */
72bf9492 18567 if (last_die->has_children
5afb4e99
DJ
18568 && (load_all
18569 || last_die->tag == DW_TAG_namespace
f55ee35c 18570 || last_die->tag == DW_TAG_module
72bf9492 18571 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18572 || (cu->language == language_cplus
18573 && last_die->tag == DW_TAG_subprogram
18574 && (last_die->name == NULL
18575 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18576 || (cu->language != language_c
18577 && (last_die->tag == DW_TAG_class_type
680b30c7 18578 || last_die->tag == DW_TAG_interface_type
72bf9492 18579 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
18580 || last_die->tag == DW_TAG_union_type))
18581 || (cu->language == language_ada
18582 && (last_die->tag == DW_TAG_subprogram
18583 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18584 {
18585 nesting_level++;
18586 parent_die = last_die;
18587 continue;
18588 }
18589
18590 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18591 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18592
18593 /* Back to the top, do it again. */
18594 }
18595}
18596
c906108c
SS
18597/* Read a minimal amount of information into the minimal die structure. */
18598
d521ce57 18599static const gdb_byte *
dee91e82
DE
18600read_partial_die (const struct die_reader_specs *reader,
18601 struct partial_die_info *part_die,
18602 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 18603 const gdb_byte *info_ptr)
c906108c 18604{
dee91e82 18605 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18606 struct dwarf2_per_objfile *dwarf2_per_objfile
18607 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18608 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 18609 const gdb_byte *buffer = reader->buffer;
fa238c03 18610 unsigned int i;
c906108c 18611 struct attribute attr;
c5aa993b 18612 int has_low_pc_attr = 0;
c906108c 18613 int has_high_pc_attr = 0;
91da1414 18614 int high_pc_relative = 0;
c906108c 18615
72bf9492 18616 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 18617
9c541725 18618 part_die->sect_off = (sect_offset) (info_ptr - buffer);
72bf9492
DJ
18619
18620 info_ptr += abbrev_len;
18621
18622 if (abbrev == NULL)
18623 return info_ptr;
18624
c906108c
SS
18625 part_die->tag = abbrev->tag;
18626 part_die->has_children = abbrev->has_children;
c906108c
SS
18627
18628 for (i = 0; i < abbrev->num_attrs; ++i)
18629 {
dee91e82 18630 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
18631
18632 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18633 partial symbol table. */
c906108c
SS
18634 switch (attr.name)
18635 {
18636 case DW_AT_name:
71c25dea
TT
18637 switch (part_die->tag)
18638 {
18639 case DW_TAG_compile_unit:
95554aad 18640 case DW_TAG_partial_unit:
348e048f 18641 case DW_TAG_type_unit:
71c25dea
TT
18642 /* Compilation units have a DW_AT_name that is a filename, not
18643 a source language identifier. */
18644 case DW_TAG_enumeration_type:
18645 case DW_TAG_enumerator:
18646 /* These tags always have simple identifiers already; no need
18647 to canonicalize them. */
18648 part_die->name = DW_STRING (&attr);
18649 break;
18650 default:
18651 part_die->name
18652 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 18653 &objfile->per_bfd->storage_obstack);
71c25dea
TT
18654 break;
18655 }
c906108c 18656 break;
31ef98ae 18657 case DW_AT_linkage_name:
c906108c 18658 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18659 /* Note that both forms of linkage name might appear. We
18660 assume they will be the same, and we only store the last
18661 one we see. */
94af9270
KS
18662 if (cu->language == language_ada)
18663 part_die->name = DW_STRING (&attr);
abc72ce4 18664 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
18665 break;
18666 case DW_AT_low_pc:
18667 has_low_pc_attr = 1;
31aa7e4e 18668 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
18669 break;
18670 case DW_AT_high_pc:
18671 has_high_pc_attr = 1;
31aa7e4e
JB
18672 part_die->highpc = attr_value_as_address (&attr);
18673 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18674 high_pc_relative = 1;
c906108c
SS
18675 break;
18676 case DW_AT_location:
0963b4bd 18677 /* Support the .debug_loc offsets. */
8e19ed76
PS
18678 if (attr_form_is_block (&attr))
18679 {
95554aad 18680 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 18681 }
3690dd37 18682 else if (attr_form_is_section_offset (&attr))
8e19ed76 18683 {
4d3c2250 18684 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18685 }
18686 else
18687 {
4d3c2250
KB
18688 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18689 "partial symbol information");
8e19ed76 18690 }
c906108c 18691 break;
c906108c
SS
18692 case DW_AT_external:
18693 part_die->is_external = DW_UNSND (&attr);
18694 break;
18695 case DW_AT_declaration:
18696 part_die->is_declaration = DW_UNSND (&attr);
18697 break;
18698 case DW_AT_type:
18699 part_die->has_type = 1;
18700 break;
18701 case DW_AT_abstract_origin:
18702 case DW_AT_specification:
72bf9492
DJ
18703 case DW_AT_extension:
18704 part_die->has_specification = 1;
c764a876 18705 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
18706 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18707 || cu->per_cu->is_dwz);
c906108c
SS
18708 break;
18709 case DW_AT_sibling:
18710 /* Ignore absolute siblings, they might point outside of
18711 the current compile unit. */
18712 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
18713 complaint (&symfile_complaints,
18714 _("ignoring absolute DW_AT_sibling"));
c906108c 18715 else
b9502d3f 18716 {
9c541725
PA
18717 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18718 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18719
18720 if (sibling_ptr < info_ptr)
18721 complaint (&symfile_complaints,
18722 _("DW_AT_sibling points backwards"));
22869d73
KS
18723 else if (sibling_ptr > reader->buffer_end)
18724 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
18725 else
18726 part_die->sibling = sibling_ptr;
18727 }
c906108c 18728 break;
fa4028e9
JB
18729 case DW_AT_byte_size:
18730 part_die->has_byte_size = 1;
18731 break;
ff908ebf
AW
18732 case DW_AT_const_value:
18733 part_die->has_const_value = 1;
18734 break;
68511cec
CES
18735 case DW_AT_calling_convention:
18736 /* DWARF doesn't provide a way to identify a program's source-level
18737 entry point. DW_AT_calling_convention attributes are only meant
18738 to describe functions' calling conventions.
18739
18740 However, because it's a necessary piece of information in
0c1b455e
TT
18741 Fortran, and before DWARF 4 DW_CC_program was the only
18742 piece of debugging information whose definition refers to
18743 a 'main program' at all, several compilers marked Fortran
18744 main programs with DW_CC_program --- even when those
18745 functions use the standard calling conventions.
18746
18747 Although DWARF now specifies a way to provide this
18748 information, we support this practice for backward
18749 compatibility. */
68511cec 18750 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
18751 && cu->language == language_fortran)
18752 part_die->main_subprogram = 1;
68511cec 18753 break;
481860b3
GB
18754 case DW_AT_inline:
18755 if (DW_UNSND (&attr) == DW_INL_inlined
18756 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18757 part_die->may_be_inlined = 1;
18758 break;
95554aad
TT
18759
18760 case DW_AT_import:
18761 if (part_die->tag == DW_TAG_imported_unit)
36586728 18762 {
9c541725 18763 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
36586728
TT
18764 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18765 || cu->per_cu->is_dwz);
18766 }
95554aad
TT
18767 break;
18768
0c1b455e
TT
18769 case DW_AT_main_subprogram:
18770 part_die->main_subprogram = DW_UNSND (&attr);
18771 break;
18772
c906108c
SS
18773 default:
18774 break;
18775 }
18776 }
18777
91da1414
MW
18778 if (high_pc_relative)
18779 part_die->highpc += part_die->lowpc;
18780
9373cf26
JK
18781 if (has_low_pc_attr && has_high_pc_attr)
18782 {
18783 /* When using the GNU linker, .gnu.linkonce. sections are used to
18784 eliminate duplicate copies of functions and vtables and such.
18785 The linker will arbitrarily choose one and discard the others.
18786 The AT_*_pc values for such functions refer to local labels in
18787 these sections. If the section from that file was discarded, the
18788 labels are not in the output, so the relocs get a value of 0.
18789 If this is a discarded function, mark the pc bounds as invalid,
18790 so that GDB will ignore it. */
18791 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18792 {
bb5ed363 18793 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
18794
18795 complaint (&symfile_complaints,
18796 _("DW_AT_low_pc %s is zero "
18797 "for DIE at 0x%x [in module %s]"),
18798 paddress (gdbarch, part_die->lowpc),
9c541725 18799 to_underlying (part_die->sect_off), objfile_name (objfile));
9373cf26
JK
18800 }
18801 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18802 else if (part_die->lowpc >= part_die->highpc)
18803 {
bb5ed363 18804 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
18805
18806 complaint (&symfile_complaints,
18807 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18808 "for DIE at 0x%x [in module %s]"),
18809 paddress (gdbarch, part_die->lowpc),
18810 paddress (gdbarch, part_die->highpc),
9c541725
PA
18811 to_underlying (part_die->sect_off),
18812 objfile_name (objfile));
9373cf26
JK
18813 }
18814 else
18815 part_die->has_pc_info = 1;
18816 }
85cbf3d3 18817
c906108c
SS
18818 return info_ptr;
18819}
18820
72bf9492
DJ
18821/* Find a cached partial DIE at OFFSET in CU. */
18822
18823static struct partial_die_info *
9c541725 18824find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
72bf9492
DJ
18825{
18826 struct partial_die_info *lookup_die = NULL;
18827 struct partial_die_info part_die;
18828
9c541725 18829 part_die.sect_off = sect_off;
9a3c8263
SM
18830 lookup_die = ((struct partial_die_info *)
18831 htab_find_with_hash (cu->partial_dies, &part_die,
9c541725 18832 to_underlying (sect_off)));
72bf9492 18833
72bf9492
DJ
18834 return lookup_die;
18835}
18836
348e048f
DE
18837/* Find a partial DIE at OFFSET, which may or may not be in CU,
18838 except in the case of .debug_types DIEs which do not reference
18839 outside their CU (they do however referencing other types via
55f1336d 18840 DW_FORM_ref_sig8). */
72bf9492
DJ
18841
18842static struct partial_die_info *
9c541725 18843find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18844{
518817b3
SM
18845 struct dwarf2_per_objfile *dwarf2_per_objfile
18846 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18847 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18848 struct dwarf2_per_cu_data *per_cu = NULL;
18849 struct partial_die_info *pd = NULL;
72bf9492 18850
36586728 18851 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18852 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18853 {
9c541725 18854 pd = find_partial_die_in_comp_unit (sect_off, cu);
5afb4e99
DJ
18855 if (pd != NULL)
18856 return pd;
0d99eb77
DE
18857 /* We missed recording what we needed.
18858 Load all dies and try again. */
18859 per_cu = cu->per_cu;
5afb4e99 18860 }
0d99eb77
DE
18861 else
18862 {
18863 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18864 if (cu->per_cu->is_debug_types)
0d99eb77 18865 {
9c541725
PA
18866 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
18867 " external reference to offset 0x%x [in module %s].\n"),
18868 to_underlying (cu->header.sect_off), to_underlying (sect_off),
0d99eb77
DE
18869 bfd_get_filename (objfile->obfd));
18870 }
9c541725 18871 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18872 dwarf2_per_objfile);
72bf9492 18873
0d99eb77
DE
18874 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18875 load_partial_comp_unit (per_cu);
ae038cb0 18876
0d99eb77 18877 per_cu->cu->last_used = 0;
9c541725 18878 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
0d99eb77 18879 }
5afb4e99 18880
dee91e82
DE
18881 /* If we didn't find it, and not all dies have been loaded,
18882 load them all and try again. */
18883
5afb4e99
DJ
18884 if (pd == NULL && per_cu->load_all_dies == 0)
18885 {
5afb4e99 18886 per_cu->load_all_dies = 1;
fd820528
DE
18887
18888 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18889 THIS_CU->cu may already be in use. So we can't just free it and
18890 replace its DIEs with the ones we read in. Instead, we leave those
18891 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18892 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18893 set. */
dee91e82 18894 load_partial_comp_unit (per_cu);
5afb4e99 18895
9c541725 18896 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
5afb4e99
DJ
18897 }
18898
18899 if (pd == NULL)
18900 internal_error (__FILE__, __LINE__,
3e43a32a
MS
18901 _("could not find partial DIE 0x%x "
18902 "in cache [from module %s]\n"),
9c541725 18903 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 18904 return pd;
72bf9492
DJ
18905}
18906
abc72ce4
DE
18907/* See if we can figure out if the class lives in a namespace. We do
18908 this by looking for a member function; its demangled name will
18909 contain namespace info, if there is any. */
18910
18911static void
18912guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18913 struct dwarf2_cu *cu)
18914{
18915 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18916 what template types look like, because the demangler
18917 frequently doesn't give the same name as the debug info. We
18918 could fix this by only using the demangled name to get the
18919 prefix (but see comment in read_structure_type). */
18920
18921 struct partial_die_info *real_pdi;
18922 struct partial_die_info *child_pdi;
18923
18924 /* If this DIE (this DIE's specification, if any) has a parent, then
18925 we should not do this. We'll prepend the parent's fully qualified
18926 name when we create the partial symbol. */
18927
18928 real_pdi = struct_pdi;
18929 while (real_pdi->has_specification)
36586728
TT
18930 real_pdi = find_partial_die (real_pdi->spec_offset,
18931 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
18932
18933 if (real_pdi->die_parent != NULL)
18934 return;
18935
18936 for (child_pdi = struct_pdi->die_child;
18937 child_pdi != NULL;
18938 child_pdi = child_pdi->die_sibling)
18939 {
18940 if (child_pdi->tag == DW_TAG_subprogram
18941 && child_pdi->linkage_name != NULL)
18942 {
18943 char *actual_class_name
18944 = language_class_name_from_physname (cu->language_defn,
18945 child_pdi->linkage_name);
18946 if (actual_class_name != NULL)
18947 {
518817b3 18948 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 18949 struct_pdi->name
224c3ddb 18950 = ((const char *)
e3b94546 18951 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
18952 actual_class_name,
18953 strlen (actual_class_name)));
abc72ce4
DE
18954 xfree (actual_class_name);
18955 }
18956 break;
18957 }
18958 }
18959}
18960
72bf9492
DJ
18961/* Adjust PART_DIE before generating a symbol for it. This function
18962 may set the is_external flag or change the DIE's name. */
18963
18964static void
18965fixup_partial_die (struct partial_die_info *part_die,
18966 struct dwarf2_cu *cu)
18967{
abc72ce4
DE
18968 /* Once we've fixed up a die, there's no point in doing so again.
18969 This also avoids a memory leak if we were to call
18970 guess_partial_die_structure_name multiple times. */
18971 if (part_die->fixup_called)
18972 return;
18973
72bf9492
DJ
18974 /* If we found a reference attribute and the DIE has no name, try
18975 to find a name in the referred to DIE. */
18976
18977 if (part_die->name == NULL && part_die->has_specification)
18978 {
18979 struct partial_die_info *spec_die;
72bf9492 18980
36586728
TT
18981 spec_die = find_partial_die (part_die->spec_offset,
18982 part_die->spec_is_dwz, cu);
72bf9492 18983
10b3939b 18984 fixup_partial_die (spec_die, cu);
72bf9492
DJ
18985
18986 if (spec_die->name)
18987 {
18988 part_die->name = spec_die->name;
18989
18990 /* Copy DW_AT_external attribute if it is set. */
18991 if (spec_die->is_external)
18992 part_die->is_external = spec_die->is_external;
18993 }
18994 }
18995
18996 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
18997
18998 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 18999 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19000
abc72ce4
DE
19001 /* If there is no parent die to provide a namespace, and there are
19002 children, see if we can determine the namespace from their linkage
122d1940 19003 name. */
abc72ce4 19004 if (cu->language == language_cplus
518817b3
SM
19005 && !VEC_empty (dwarf2_section_info_def,
19006 cu->per_cu->dwarf2_per_objfile->types)
abc72ce4
DE
19007 && part_die->die_parent == NULL
19008 && part_die->has_children
19009 && (part_die->tag == DW_TAG_class_type
19010 || part_die->tag == DW_TAG_structure_type
19011 || part_die->tag == DW_TAG_union_type))
19012 guess_partial_die_structure_name (part_die, cu);
19013
53832f31
TT
19014 /* GCC might emit a nameless struct or union that has a linkage
19015 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19016 if (part_die->name == NULL
96408a79
SA
19017 && (part_die->tag == DW_TAG_class_type
19018 || part_die->tag == DW_TAG_interface_type
19019 || part_die->tag == DW_TAG_structure_type
19020 || part_die->tag == DW_TAG_union_type)
53832f31
TT
19021 && part_die->linkage_name != NULL)
19022 {
19023 char *demangled;
19024
8de20a37 19025 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
19026 if (demangled)
19027 {
96408a79
SA
19028 const char *base;
19029
19030 /* Strip any leading namespaces/classes, keep only the base name.
19031 DW_AT_name for named DIEs does not contain the prefixes. */
19032 base = strrchr (demangled, ':');
19033 if (base && base > demangled && base[-1] == ':')
19034 base++;
19035 else
19036 base = demangled;
19037
518817b3 19038 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 19039 part_die->name
224c3ddb 19040 = ((const char *)
e3b94546 19041 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 19042 base, strlen (base)));
53832f31
TT
19043 xfree (demangled);
19044 }
19045 }
19046
abc72ce4 19047 part_die->fixup_called = 1;
72bf9492
DJ
19048}
19049
a8329558 19050/* Read an attribute value described by an attribute form. */
c906108c 19051
d521ce57 19052static const gdb_byte *
dee91e82
DE
19053read_attribute_value (const struct die_reader_specs *reader,
19054 struct attribute *attr, unsigned form,
43988095 19055 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19056{
dee91e82 19057 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19058 struct dwarf2_per_objfile *dwarf2_per_objfile
19059 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19060 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19061 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19062 bfd *abfd = reader->abfd;
e7c27a73 19063 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19064 unsigned int bytes_read;
19065 struct dwarf_block *blk;
19066
aead7601 19067 attr->form = (enum dwarf_form) form;
a8329558 19068 switch (form)
c906108c 19069 {
c906108c 19070 case DW_FORM_ref_addr:
ae411497 19071 if (cu->header.version == 2)
4568ecf9 19072 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19073 else
4568ecf9
DE
19074 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19075 &cu->header, &bytes_read);
ae411497
TT
19076 info_ptr += bytes_read;
19077 break;
36586728
TT
19078 case DW_FORM_GNU_ref_alt:
19079 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19080 info_ptr += bytes_read;
19081 break;
ae411497 19082 case DW_FORM_addr:
e7c27a73 19083 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19084 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19085 info_ptr += bytes_read;
c906108c
SS
19086 break;
19087 case DW_FORM_block2:
7b5a2f43 19088 blk = dwarf_alloc_block (cu);
c906108c
SS
19089 blk->size = read_2_bytes (abfd, info_ptr);
19090 info_ptr += 2;
19091 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19092 info_ptr += blk->size;
19093 DW_BLOCK (attr) = blk;
19094 break;
19095 case DW_FORM_block4:
7b5a2f43 19096 blk = dwarf_alloc_block (cu);
c906108c
SS
19097 blk->size = read_4_bytes (abfd, info_ptr);
19098 info_ptr += 4;
19099 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19100 info_ptr += blk->size;
19101 DW_BLOCK (attr) = blk;
19102 break;
19103 case DW_FORM_data2:
19104 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19105 info_ptr += 2;
19106 break;
19107 case DW_FORM_data4:
19108 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19109 info_ptr += 4;
19110 break;
19111 case DW_FORM_data8:
19112 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19113 info_ptr += 8;
19114 break;
0224619f
JK
19115 case DW_FORM_data16:
19116 blk = dwarf_alloc_block (cu);
19117 blk->size = 16;
19118 blk->data = read_n_bytes (abfd, info_ptr, 16);
19119 info_ptr += 16;
19120 DW_BLOCK (attr) = blk;
19121 break;
2dc7f7b3
TT
19122 case DW_FORM_sec_offset:
19123 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19124 info_ptr += bytes_read;
19125 break;
c906108c 19126 case DW_FORM_string:
9b1c24c8 19127 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19128 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19129 info_ptr += bytes_read;
19130 break;
4bdf3d34 19131 case DW_FORM_strp:
36586728
TT
19132 if (!cu->per_cu->is_dwz)
19133 {
ed2dc618
SM
19134 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19135 abfd, info_ptr, cu_header,
36586728
TT
19136 &bytes_read);
19137 DW_STRING_IS_CANONICAL (attr) = 0;
19138 info_ptr += bytes_read;
19139 break;
19140 }
19141 /* FALLTHROUGH */
43988095
JK
19142 case DW_FORM_line_strp:
19143 if (!cu->per_cu->is_dwz)
19144 {
ed2dc618
SM
19145 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19146 abfd, info_ptr,
43988095
JK
19147 cu_header, &bytes_read);
19148 DW_STRING_IS_CANONICAL (attr) = 0;
19149 info_ptr += bytes_read;
19150 break;
19151 }
19152 /* FALLTHROUGH */
36586728
TT
19153 case DW_FORM_GNU_strp_alt:
19154 {
ed2dc618 19155 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19156 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19157 &bytes_read);
19158
ed2dc618
SM
19159 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19160 dwz, str_offset);
36586728
TT
19161 DW_STRING_IS_CANONICAL (attr) = 0;
19162 info_ptr += bytes_read;
19163 }
4bdf3d34 19164 break;
2dc7f7b3 19165 case DW_FORM_exprloc:
c906108c 19166 case DW_FORM_block:
7b5a2f43 19167 blk = dwarf_alloc_block (cu);
c906108c
SS
19168 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19169 info_ptr += bytes_read;
19170 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19171 info_ptr += blk->size;
19172 DW_BLOCK (attr) = blk;
19173 break;
19174 case DW_FORM_block1:
7b5a2f43 19175 blk = dwarf_alloc_block (cu);
c906108c
SS
19176 blk->size = read_1_byte (abfd, info_ptr);
19177 info_ptr += 1;
19178 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19179 info_ptr += blk->size;
19180 DW_BLOCK (attr) = blk;
19181 break;
19182 case DW_FORM_data1:
19183 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19184 info_ptr += 1;
19185 break;
19186 case DW_FORM_flag:
19187 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19188 info_ptr += 1;
19189 break;
2dc7f7b3
TT
19190 case DW_FORM_flag_present:
19191 DW_UNSND (attr) = 1;
19192 break;
c906108c
SS
19193 case DW_FORM_sdata:
19194 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19195 info_ptr += bytes_read;
19196 break;
19197 case DW_FORM_udata:
19198 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19199 info_ptr += bytes_read;
19200 break;
19201 case DW_FORM_ref1:
9c541725 19202 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19203 + read_1_byte (abfd, info_ptr));
c906108c
SS
19204 info_ptr += 1;
19205 break;
19206 case DW_FORM_ref2:
9c541725 19207 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19208 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19209 info_ptr += 2;
19210 break;
19211 case DW_FORM_ref4:
9c541725 19212 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19213 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19214 info_ptr += 4;
19215 break;
613e1657 19216 case DW_FORM_ref8:
9c541725 19217 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19218 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19219 info_ptr += 8;
19220 break;
55f1336d 19221 case DW_FORM_ref_sig8:
ac9ec31b 19222 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19223 info_ptr += 8;
19224 break;
c906108c 19225 case DW_FORM_ref_udata:
9c541725 19226 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19227 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19228 info_ptr += bytes_read;
19229 break;
c906108c 19230 case DW_FORM_indirect:
a8329558
KW
19231 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19232 info_ptr += bytes_read;
43988095
JK
19233 if (form == DW_FORM_implicit_const)
19234 {
19235 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19236 info_ptr += bytes_read;
19237 }
19238 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19239 info_ptr);
19240 break;
19241 case DW_FORM_implicit_const:
19242 DW_SND (attr) = implicit_const;
a8329558 19243 break;
3019eac3
DE
19244 case DW_FORM_GNU_addr_index:
19245 if (reader->dwo_file == NULL)
19246 {
19247 /* For now flag a hard error.
19248 Later we can turn this into a complaint. */
19249 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19250 dwarf_form_name (form),
19251 bfd_get_filename (abfd));
19252 }
19253 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19254 info_ptr += bytes_read;
19255 break;
19256 case DW_FORM_GNU_str_index:
19257 if (reader->dwo_file == NULL)
19258 {
19259 /* For now flag a hard error.
19260 Later we can turn this into a complaint if warranted. */
19261 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19262 dwarf_form_name (form),
19263 bfd_get_filename (abfd));
19264 }
19265 {
19266 ULONGEST str_index =
19267 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19268
342587c4 19269 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
19270 DW_STRING_IS_CANONICAL (attr) = 0;
19271 info_ptr += bytes_read;
19272 }
19273 break;
c906108c 19274 default:
8a3fe4f8 19275 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19276 dwarf_form_name (form),
19277 bfd_get_filename (abfd));
c906108c 19278 }
28e94949 19279
36586728 19280 /* Super hack. */
7771576e 19281 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19282 attr->form = DW_FORM_GNU_ref_alt;
19283
28e94949
JB
19284 /* We have seen instances where the compiler tried to emit a byte
19285 size attribute of -1 which ended up being encoded as an unsigned
19286 0xffffffff. Although 0xffffffff is technically a valid size value,
19287 an object of this size seems pretty unlikely so we can relatively
19288 safely treat these cases as if the size attribute was invalid and
19289 treat them as zero by default. */
19290 if (attr->name == DW_AT_byte_size
19291 && form == DW_FORM_data4
19292 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19293 {
19294 complaint
19295 (&symfile_complaints,
43bbcdc2
PH
19296 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19297 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19298 DW_UNSND (attr) = 0;
19299 }
28e94949 19300
c906108c
SS
19301 return info_ptr;
19302}
19303
a8329558
KW
19304/* Read an attribute described by an abbreviated attribute. */
19305
d521ce57 19306static const gdb_byte *
dee91e82
DE
19307read_attribute (const struct die_reader_specs *reader,
19308 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19309 const gdb_byte *info_ptr)
a8329558
KW
19310{
19311 attr->name = abbrev->name;
43988095
JK
19312 return read_attribute_value (reader, attr, abbrev->form,
19313 abbrev->implicit_const, info_ptr);
a8329558
KW
19314}
19315
0963b4bd 19316/* Read dwarf information from a buffer. */
c906108c
SS
19317
19318static unsigned int
a1855c1d 19319read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19320{
fe1b8b76 19321 return bfd_get_8 (abfd, buf);
c906108c
SS
19322}
19323
19324static int
a1855c1d 19325read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19326{
fe1b8b76 19327 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19328}
19329
19330static unsigned int
a1855c1d 19331read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19332{
fe1b8b76 19333 return bfd_get_16 (abfd, buf);
c906108c
SS
19334}
19335
21ae7a4d 19336static int
a1855c1d 19337read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19338{
19339 return bfd_get_signed_16 (abfd, buf);
19340}
19341
c906108c 19342static unsigned int
a1855c1d 19343read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19344{
fe1b8b76 19345 return bfd_get_32 (abfd, buf);
c906108c
SS
19346}
19347
21ae7a4d 19348static int
a1855c1d 19349read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19350{
19351 return bfd_get_signed_32 (abfd, buf);
19352}
19353
93311388 19354static ULONGEST
a1855c1d 19355read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19356{
fe1b8b76 19357 return bfd_get_64 (abfd, buf);
c906108c
SS
19358}
19359
19360static CORE_ADDR
d521ce57 19361read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19362 unsigned int *bytes_read)
c906108c 19363{
e7c27a73 19364 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19365 CORE_ADDR retval = 0;
19366
107d2387 19367 if (cu_header->signed_addr_p)
c906108c 19368 {
107d2387
AC
19369 switch (cu_header->addr_size)
19370 {
19371 case 2:
fe1b8b76 19372 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19373 break;
19374 case 4:
fe1b8b76 19375 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19376 break;
19377 case 8:
fe1b8b76 19378 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19379 break;
19380 default:
8e65ff28 19381 internal_error (__FILE__, __LINE__,
e2e0b3e5 19382 _("read_address: bad switch, signed [in module %s]"),
659b0389 19383 bfd_get_filename (abfd));
107d2387
AC
19384 }
19385 }
19386 else
19387 {
19388 switch (cu_header->addr_size)
19389 {
19390 case 2:
fe1b8b76 19391 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19392 break;
19393 case 4:
fe1b8b76 19394 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19395 break;
19396 case 8:
fe1b8b76 19397 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19398 break;
19399 default:
8e65ff28 19400 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19401 _("read_address: bad switch, "
19402 "unsigned [in module %s]"),
659b0389 19403 bfd_get_filename (abfd));
107d2387 19404 }
c906108c 19405 }
64367e0a 19406
107d2387
AC
19407 *bytes_read = cu_header->addr_size;
19408 return retval;
c906108c
SS
19409}
19410
f7ef9339 19411/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19412 specification allows the initial length to take up either 4 bytes
19413 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19414 bytes describe the length and all offsets will be 8 bytes in length
19415 instead of 4.
19416
f7ef9339
KB
19417 An older, non-standard 64-bit format is also handled by this
19418 function. The older format in question stores the initial length
19419 as an 8-byte quantity without an escape value. Lengths greater
19420 than 2^32 aren't very common which means that the initial 4 bytes
19421 is almost always zero. Since a length value of zero doesn't make
19422 sense for the 32-bit format, this initial zero can be considered to
19423 be an escape value which indicates the presence of the older 64-bit
19424 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19425 greater than 4GB. If it becomes necessary to handle lengths
19426 somewhat larger than 4GB, we could allow other small values (such
19427 as the non-sensical values of 1, 2, and 3) to also be used as
19428 escape values indicating the presence of the old format.
f7ef9339 19429
917c78fc
MK
19430 The value returned via bytes_read should be used to increment the
19431 relevant pointer after calling read_initial_length().
c764a876 19432
613e1657
KB
19433 [ Note: read_initial_length() and read_offset() are based on the
19434 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19435 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19436 from:
19437
f7ef9339 19438 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19439
613e1657
KB
19440 This document is only a draft and is subject to change. (So beware.)
19441
f7ef9339 19442 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19443 determined empirically by examining 64-bit ELF files produced by
19444 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19445
19446 - Kevin, July 16, 2002
613e1657
KB
19447 ] */
19448
19449static LONGEST
d521ce57 19450read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19451{
fe1b8b76 19452 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19453
dd373385 19454 if (length == 0xffffffff)
613e1657 19455 {
fe1b8b76 19456 length = bfd_get_64 (abfd, buf + 4);
613e1657 19457 *bytes_read = 12;
613e1657 19458 }
dd373385 19459 else if (length == 0)
f7ef9339 19460 {
dd373385 19461 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19462 length = bfd_get_64 (abfd, buf);
f7ef9339 19463 *bytes_read = 8;
f7ef9339 19464 }
613e1657
KB
19465 else
19466 {
19467 *bytes_read = 4;
613e1657
KB
19468 }
19469
c764a876
DE
19470 return length;
19471}
dd373385 19472
c764a876
DE
19473/* Cover function for read_initial_length.
19474 Returns the length of the object at BUF, and stores the size of the
19475 initial length in *BYTES_READ and stores the size that offsets will be in
19476 *OFFSET_SIZE.
19477 If the initial length size is not equivalent to that specified in
19478 CU_HEADER then issue a complaint.
19479 This is useful when reading non-comp-unit headers. */
dd373385 19480
c764a876 19481static LONGEST
d521ce57 19482read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19483 const struct comp_unit_head *cu_header,
19484 unsigned int *bytes_read,
19485 unsigned int *offset_size)
19486{
19487 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19488
19489 gdb_assert (cu_header->initial_length_size == 4
19490 || cu_header->initial_length_size == 8
19491 || cu_header->initial_length_size == 12);
19492
19493 if (cu_header->initial_length_size != *bytes_read)
19494 complaint (&symfile_complaints,
19495 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19496
c764a876 19497 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19498 return length;
613e1657
KB
19499}
19500
19501/* Read an offset from the data stream. The size of the offset is
917c78fc 19502 given by cu_header->offset_size. */
613e1657
KB
19503
19504static LONGEST
d521ce57
TT
19505read_offset (bfd *abfd, const gdb_byte *buf,
19506 const struct comp_unit_head *cu_header,
891d2f0b 19507 unsigned int *bytes_read)
c764a876
DE
19508{
19509 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19510
c764a876
DE
19511 *bytes_read = cu_header->offset_size;
19512 return offset;
19513}
19514
19515/* Read an offset from the data stream. */
19516
19517static LONGEST
d521ce57 19518read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19519{
19520 LONGEST retval = 0;
19521
c764a876 19522 switch (offset_size)
613e1657
KB
19523 {
19524 case 4:
fe1b8b76 19525 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19526 break;
19527 case 8:
fe1b8b76 19528 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19529 break;
19530 default:
8e65ff28 19531 internal_error (__FILE__, __LINE__,
c764a876 19532 _("read_offset_1: bad switch [in module %s]"),
659b0389 19533 bfd_get_filename (abfd));
613e1657
KB
19534 }
19535
917c78fc 19536 return retval;
613e1657
KB
19537}
19538
d521ce57
TT
19539static const gdb_byte *
19540read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19541{
19542 /* If the size of a host char is 8 bits, we can return a pointer
19543 to the buffer, otherwise we have to copy the data to a buffer
19544 allocated on the temporary obstack. */
4bdf3d34 19545 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19546 return buf;
c906108c
SS
19547}
19548
d521ce57
TT
19549static const char *
19550read_direct_string (bfd *abfd, const gdb_byte *buf,
19551 unsigned int *bytes_read_ptr)
c906108c
SS
19552{
19553 /* If the size of a host char is 8 bits, we can return a pointer
19554 to the string, otherwise we have to copy the string to a buffer
19555 allocated on the temporary obstack. */
4bdf3d34 19556 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19557 if (*buf == '\0')
19558 {
19559 *bytes_read_ptr = 1;
19560 return NULL;
19561 }
d521ce57
TT
19562 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19563 return (const char *) buf;
4bdf3d34
JJ
19564}
19565
43988095
JK
19566/* Return pointer to string at section SECT offset STR_OFFSET with error
19567 reporting strings FORM_NAME and SECT_NAME. */
19568
d521ce57 19569static const char *
ed2dc618
SM
19570read_indirect_string_at_offset_from (struct objfile *objfile,
19571 bfd *abfd, LONGEST str_offset,
43988095
JK
19572 struct dwarf2_section_info *sect,
19573 const char *form_name,
19574 const char *sect_name)
19575{
ed2dc618 19576 dwarf2_read_section (objfile, sect);
43988095
JK
19577 if (sect->buffer == NULL)
19578 error (_("%s used without %s section [in module %s]"),
19579 form_name, sect_name, bfd_get_filename (abfd));
19580 if (str_offset >= sect->size)
19581 error (_("%s pointing outside of %s section [in module %s]"),
19582 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19583 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19584 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19585 return NULL;
43988095
JK
19586 return (const char *) (sect->buffer + str_offset);
19587}
19588
19589/* Return pointer to string at .debug_str offset STR_OFFSET. */
19590
19591static const char *
ed2dc618
SM
19592read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19593 bfd *abfd, LONGEST str_offset)
43988095 19594{
ed2dc618
SM
19595 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19596 abfd, str_offset,
43988095
JK
19597 &dwarf2_per_objfile->str,
19598 "DW_FORM_strp", ".debug_str");
19599}
19600
19601/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19602
19603static const char *
ed2dc618
SM
19604read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19605 bfd *abfd, LONGEST str_offset)
43988095 19606{
ed2dc618
SM
19607 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19608 abfd, str_offset,
43988095
JK
19609 &dwarf2_per_objfile->line_str,
19610 "DW_FORM_line_strp",
19611 ".debug_line_str");
c906108c
SS
19612}
19613
36586728
TT
19614/* Read a string at offset STR_OFFSET in the .debug_str section from
19615 the .dwz file DWZ. Throw an error if the offset is too large. If
19616 the string consists of a single NUL byte, return NULL; otherwise
19617 return a pointer to the string. */
19618
d521ce57 19619static const char *
ed2dc618
SM
19620read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19621 LONGEST str_offset)
36586728 19622{
ed2dc618 19623 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19624
19625 if (dwz->str.buffer == NULL)
19626 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19627 "section [in module %s]"),
19628 bfd_get_filename (dwz->dwz_bfd));
19629 if (str_offset >= dwz->str.size)
19630 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19631 ".debug_str section [in module %s]"),
19632 bfd_get_filename (dwz->dwz_bfd));
19633 gdb_assert (HOST_CHAR_BIT == 8);
19634 if (dwz->str.buffer[str_offset] == '\0')
19635 return NULL;
d521ce57 19636 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19637}
19638
43988095
JK
19639/* Return pointer to string at .debug_str offset as read from BUF.
19640 BUF is assumed to be in a compilation unit described by CU_HEADER.
19641 Return *BYTES_READ_PTR count of bytes read from BUF. */
19642
d521ce57 19643static const char *
ed2dc618
SM
19644read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19645 const gdb_byte *buf,
cf2c3c16
TT
19646 const struct comp_unit_head *cu_header,
19647 unsigned int *bytes_read_ptr)
19648{
19649 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19650
ed2dc618 19651 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19652}
19653
43988095
JK
19654/* Return pointer to string at .debug_line_str offset as read from BUF.
19655 BUF is assumed to be in a compilation unit described by CU_HEADER.
19656 Return *BYTES_READ_PTR count of bytes read from BUF. */
19657
19658static const char *
ed2dc618
SM
19659read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19660 bfd *abfd, const gdb_byte *buf,
43988095
JK
19661 const struct comp_unit_head *cu_header,
19662 unsigned int *bytes_read_ptr)
19663{
19664 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19665
ed2dc618
SM
19666 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19667 str_offset);
43988095
JK
19668}
19669
19670ULONGEST
d521ce57 19671read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19672 unsigned int *bytes_read_ptr)
c906108c 19673{
12df843f 19674 ULONGEST result;
ce5d95e1 19675 unsigned int num_read;
870f88f7 19676 int shift;
c906108c
SS
19677 unsigned char byte;
19678
19679 result = 0;
19680 shift = 0;
19681 num_read = 0;
c906108c
SS
19682 while (1)
19683 {
fe1b8b76 19684 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19685 buf++;
19686 num_read++;
12df843f 19687 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19688 if ((byte & 128) == 0)
19689 {
19690 break;
19691 }
19692 shift += 7;
19693 }
19694 *bytes_read_ptr = num_read;
19695 return result;
19696}
19697
12df843f 19698static LONGEST
d521ce57
TT
19699read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19700 unsigned int *bytes_read_ptr)
c906108c 19701{
12df843f 19702 LONGEST result;
870f88f7 19703 int shift, num_read;
c906108c
SS
19704 unsigned char byte;
19705
19706 result = 0;
19707 shift = 0;
c906108c 19708 num_read = 0;
c906108c
SS
19709 while (1)
19710 {
fe1b8b76 19711 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19712 buf++;
19713 num_read++;
12df843f 19714 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
19715 shift += 7;
19716 if ((byte & 128) == 0)
19717 {
19718 break;
19719 }
19720 }
77e0b926 19721 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 19722 result |= -(((LONGEST) 1) << shift);
c906108c
SS
19723 *bytes_read_ptr = num_read;
19724 return result;
19725}
19726
3019eac3
DE
19727/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19728 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19729 ADDR_SIZE is the size of addresses from the CU header. */
19730
19731static CORE_ADDR
ed2dc618
SM
19732read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19733 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
19734{
19735 struct objfile *objfile = dwarf2_per_objfile->objfile;
19736 bfd *abfd = objfile->obfd;
19737 const gdb_byte *info_ptr;
19738
19739 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19740 if (dwarf2_per_objfile->addr.buffer == NULL)
19741 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19742 objfile_name (objfile));
3019eac3
DE
19743 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19744 error (_("DW_FORM_addr_index pointing outside of "
19745 ".debug_addr section [in module %s]"),
4262abfb 19746 objfile_name (objfile));
3019eac3
DE
19747 info_ptr = (dwarf2_per_objfile->addr.buffer
19748 + addr_base + addr_index * addr_size);
19749 if (addr_size == 4)
19750 return bfd_get_32 (abfd, info_ptr);
19751 else
19752 return bfd_get_64 (abfd, info_ptr);
19753}
19754
19755/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19756
19757static CORE_ADDR
19758read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19759{
518817b3
SM
19760 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19761 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19762}
19763
19764/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19765
19766static CORE_ADDR
d521ce57 19767read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19768 unsigned int *bytes_read)
19769{
518817b3 19770 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19771 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19772
19773 return read_addr_index (cu, addr_index);
19774}
19775
19776/* Data structure to pass results from dwarf2_read_addr_index_reader
19777 back to dwarf2_read_addr_index. */
19778
19779struct dwarf2_read_addr_index_data
19780{
19781 ULONGEST addr_base;
19782 int addr_size;
19783};
19784
19785/* die_reader_func for dwarf2_read_addr_index. */
19786
19787static void
19788dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19789 const gdb_byte *info_ptr,
3019eac3
DE
19790 struct die_info *comp_unit_die,
19791 int has_children,
19792 void *data)
19793{
19794 struct dwarf2_cu *cu = reader->cu;
19795 struct dwarf2_read_addr_index_data *aidata =
19796 (struct dwarf2_read_addr_index_data *) data;
19797
19798 aidata->addr_base = cu->addr_base;
19799 aidata->addr_size = cu->header.addr_size;
19800}
19801
19802/* Given an index in .debug_addr, fetch the value.
19803 NOTE: This can be called during dwarf expression evaluation,
19804 long after the debug information has been read, and thus per_cu->cu
19805 may no longer exist. */
19806
19807CORE_ADDR
19808dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19809 unsigned int addr_index)
19810{
ed2dc618
SM
19811 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19812 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
19813 struct dwarf2_cu *cu = per_cu->cu;
19814 ULONGEST addr_base;
19815 int addr_size;
19816
3019eac3
DE
19817 /* We need addr_base and addr_size.
19818 If we don't have PER_CU->cu, we have to get it.
19819 Nasty, but the alternative is storing the needed info in PER_CU,
19820 which at this point doesn't seem justified: it's not clear how frequently
19821 it would get used and it would increase the size of every PER_CU.
19822 Entry points like dwarf2_per_cu_addr_size do a similar thing
19823 so we're not in uncharted territory here.
19824 Alas we need to be a bit more complicated as addr_base is contained
19825 in the DIE.
19826
19827 We don't need to read the entire CU(/TU).
19828 We just need the header and top level die.
a1b64ce1 19829
3019eac3 19830 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19831 For now we skip this optimization. */
3019eac3
DE
19832
19833 if (cu != NULL)
19834 {
19835 addr_base = cu->addr_base;
19836 addr_size = cu->header.addr_size;
19837 }
19838 else
19839 {
19840 struct dwarf2_read_addr_index_data aidata;
19841
a1b64ce1
DE
19842 /* Note: We can't use init_cutu_and_read_dies_simple here,
19843 we need addr_base. */
19844 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19845 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
19846 addr_base = aidata.addr_base;
19847 addr_size = aidata.addr_size;
19848 }
19849
ed2dc618
SM
19850 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19851 addr_size);
3019eac3
DE
19852}
19853
57d63ce2
DE
19854/* Given a DW_FORM_GNU_str_index, fetch the string.
19855 This is only used by the Fission support. */
3019eac3 19856
d521ce57 19857static const char *
342587c4 19858read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 19859{
ed2dc618 19860 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19861 struct dwarf2_per_objfile *dwarf2_per_objfile
19862 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19863 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19864 const char *objf_name = objfile_name (objfile);
3019eac3 19865 bfd *abfd = objfile->obfd;
73869dc2
DE
19866 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19867 struct dwarf2_section_info *str_offsets_section =
19868 &reader->dwo_file->sections.str_offsets;
d521ce57 19869 const gdb_byte *info_ptr;
3019eac3 19870 ULONGEST str_offset;
57d63ce2 19871 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 19872
73869dc2
DE
19873 dwarf2_read_section (objfile, str_section);
19874 dwarf2_read_section (objfile, str_offsets_section);
19875 if (str_section->buffer == NULL)
57d63ce2 19876 error (_("%s used without .debug_str.dwo section"
9c541725
PA
19877 " in CU at offset 0x%x [in module %s]"),
19878 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19879 if (str_offsets_section->buffer == NULL)
57d63ce2 19880 error (_("%s used without .debug_str_offsets.dwo section"
9c541725
PA
19881 " in CU at offset 0x%x [in module %s]"),
19882 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19883 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 19884 error (_("%s pointing outside of .debug_str_offsets.dwo"
9c541725
PA
19885 " section in CU at offset 0x%x [in module %s]"),
19886 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19887 info_ptr = (str_offsets_section->buffer
3019eac3
DE
19888 + str_index * cu->header.offset_size);
19889 if (cu->header.offset_size == 4)
19890 str_offset = bfd_get_32 (abfd, info_ptr);
19891 else
19892 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19893 if (str_offset >= str_section->size)
57d63ce2 19894 error (_("Offset from %s pointing outside of"
9c541725
PA
19895 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
19896 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 19897 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19898}
19899
3019eac3
DE
19900/* Return the length of an LEB128 number in BUF. */
19901
19902static int
19903leb128_size (const gdb_byte *buf)
19904{
19905 const gdb_byte *begin = buf;
19906 gdb_byte byte;
19907
19908 while (1)
19909 {
19910 byte = *buf++;
19911 if ((byte & 128) == 0)
19912 return buf - begin;
19913 }
19914}
19915
c906108c 19916static void
e142c38c 19917set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19918{
19919 switch (lang)
19920 {
19921 case DW_LANG_C89:
76bee0cc 19922 case DW_LANG_C99:
0cfd832f 19923 case DW_LANG_C11:
c906108c 19924 case DW_LANG_C:
d1be3247 19925 case DW_LANG_UPC:
e142c38c 19926 cu->language = language_c;
c906108c 19927 break;
9c37b5ae 19928 case DW_LANG_Java:
c906108c 19929 case DW_LANG_C_plus_plus:
0cfd832f
MW
19930 case DW_LANG_C_plus_plus_11:
19931 case DW_LANG_C_plus_plus_14:
e142c38c 19932 cu->language = language_cplus;
c906108c 19933 break;
6aecb9c2
JB
19934 case DW_LANG_D:
19935 cu->language = language_d;
19936 break;
c906108c
SS
19937 case DW_LANG_Fortran77:
19938 case DW_LANG_Fortran90:
b21b22e0 19939 case DW_LANG_Fortran95:
f7de9aab
MW
19940 case DW_LANG_Fortran03:
19941 case DW_LANG_Fortran08:
e142c38c 19942 cu->language = language_fortran;
c906108c 19943 break;
a766d390
DE
19944 case DW_LANG_Go:
19945 cu->language = language_go;
19946 break;
c906108c 19947 case DW_LANG_Mips_Assembler:
e142c38c 19948 cu->language = language_asm;
c906108c
SS
19949 break;
19950 case DW_LANG_Ada83:
8aaf0b47 19951 case DW_LANG_Ada95:
bc5f45f8
JB
19952 cu->language = language_ada;
19953 break;
72019c9c
GM
19954 case DW_LANG_Modula2:
19955 cu->language = language_m2;
19956 break;
fe8e67fd
PM
19957 case DW_LANG_Pascal83:
19958 cu->language = language_pascal;
19959 break;
22566fbd
DJ
19960 case DW_LANG_ObjC:
19961 cu->language = language_objc;
19962 break;
c44af4eb
TT
19963 case DW_LANG_Rust:
19964 case DW_LANG_Rust_old:
19965 cu->language = language_rust;
19966 break;
c906108c
SS
19967 case DW_LANG_Cobol74:
19968 case DW_LANG_Cobol85:
c906108c 19969 default:
e142c38c 19970 cu->language = language_minimal;
c906108c
SS
19971 break;
19972 }
e142c38c 19973 cu->language_defn = language_def (cu->language);
c906108c
SS
19974}
19975
19976/* Return the named attribute or NULL if not there. */
19977
19978static struct attribute *
e142c38c 19979dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19980{
a48e046c 19981 for (;;)
c906108c 19982 {
a48e046c
TT
19983 unsigned int i;
19984 struct attribute *spec = NULL;
19985
19986 for (i = 0; i < die->num_attrs; ++i)
19987 {
19988 if (die->attrs[i].name == name)
19989 return &die->attrs[i];
19990 if (die->attrs[i].name == DW_AT_specification
19991 || die->attrs[i].name == DW_AT_abstract_origin)
19992 spec = &die->attrs[i];
19993 }
19994
19995 if (!spec)
19996 break;
c906108c 19997
f2f0e013 19998 die = follow_die_ref (die, spec, &cu);
f2f0e013 19999 }
c5aa993b 20000
c906108c
SS
20001 return NULL;
20002}
20003
348e048f
DE
20004/* Return the named attribute or NULL if not there,
20005 but do not follow DW_AT_specification, etc.
20006 This is for use in contexts where we're reading .debug_types dies.
20007 Following DW_AT_specification, DW_AT_abstract_origin will take us
20008 back up the chain, and we want to go down. */
20009
20010static struct attribute *
45e58e77 20011dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20012{
20013 unsigned int i;
20014
20015 for (i = 0; i < die->num_attrs; ++i)
20016 if (die->attrs[i].name == name)
20017 return &die->attrs[i];
20018
20019 return NULL;
20020}
20021
7d45c7c3
KB
20022/* Return the string associated with a string-typed attribute, or NULL if it
20023 is either not found or is of an incorrect type. */
20024
20025static const char *
20026dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20027{
20028 struct attribute *attr;
20029 const char *str = NULL;
20030
20031 attr = dwarf2_attr (die, name, cu);
20032
20033 if (attr != NULL)
20034 {
43988095 20035 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
20036 || attr->form == DW_FORM_string
20037 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20038 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20039 str = DW_STRING (attr);
20040 else
20041 complaint (&symfile_complaints,
20042 _("string type expected for attribute %s for "
20043 "DIE at 0x%x in module %s"),
9c541725 20044 dwarf_attr_name (name), to_underlying (die->sect_off),
518817b3 20045 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20046 }
20047
20048 return str;
20049}
20050
05cf31d1
JB
20051/* Return non-zero iff the attribute NAME is defined for the given DIE,
20052 and holds a non-zero value. This function should only be used for
2dc7f7b3 20053 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20054
20055static int
20056dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20057{
20058 struct attribute *attr = dwarf2_attr (die, name, cu);
20059
20060 return (attr && DW_UNSND (attr));
20061}
20062
3ca72b44 20063static int
e142c38c 20064die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20065{
05cf31d1
JB
20066 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20067 which value is non-zero. However, we have to be careful with
20068 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20069 (via dwarf2_flag_true_p) follows this attribute. So we may
20070 end up accidently finding a declaration attribute that belongs
20071 to a different DIE referenced by the specification attribute,
20072 even though the given DIE does not have a declaration attribute. */
20073 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20074 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20075}
20076
63d06c5c 20077/* Return the die giving the specification for DIE, if there is
f2f0e013 20078 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20079 containing the return value on output. If there is no
20080 specification, but there is an abstract origin, that is
20081 returned. */
63d06c5c
DC
20082
20083static struct die_info *
f2f0e013 20084die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20085{
f2f0e013
DJ
20086 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20087 *spec_cu);
63d06c5c 20088
edb3359d
DJ
20089 if (spec_attr == NULL)
20090 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20091
63d06c5c
DC
20092 if (spec_attr == NULL)
20093 return NULL;
20094 else
f2f0e013 20095 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20096}
c906108c 20097
527f3840
JK
20098/* Stub for free_line_header to match void * callback types. */
20099
20100static void
20101free_line_header_voidp (void *arg)
20102{
9a3c8263 20103 struct line_header *lh = (struct line_header *) arg;
527f3840 20104
fff8551c 20105 delete lh;
527f3840
JK
20106}
20107
fff8551c
PA
20108void
20109line_header::add_include_dir (const char *include_dir)
c906108c 20110{
27e0867f 20111 if (dwarf_line_debug >= 2)
fff8551c
PA
20112 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20113 include_dirs.size () + 1, include_dir);
27e0867f 20114
fff8551c 20115 include_dirs.push_back (include_dir);
debd256d 20116}
6e70227d 20117
fff8551c
PA
20118void
20119line_header::add_file_name (const char *name,
ecfb656c 20120 dir_index d_index,
fff8551c
PA
20121 unsigned int mod_time,
20122 unsigned int length)
debd256d 20123{
27e0867f
DE
20124 if (dwarf_line_debug >= 2)
20125 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 20126 (unsigned) file_names.size () + 1, name);
27e0867f 20127
ecfb656c 20128 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20129}
6e70227d 20130
83769d0b 20131/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20132
20133static struct dwarf2_section_info *
20134get_debug_line_section (struct dwarf2_cu *cu)
20135{
20136 struct dwarf2_section_info *section;
518817b3
SM
20137 struct dwarf2_per_objfile *dwarf2_per_objfile
20138 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20139
20140 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20141 DWO file. */
20142 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20143 section = &cu->dwo_unit->dwo_file->sections.line;
20144 else if (cu->per_cu->is_dwz)
20145 {
ed2dc618 20146 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20147
20148 section = &dwz->line;
20149 }
20150 else
20151 section = &dwarf2_per_objfile->line;
20152
20153 return section;
20154}
20155
43988095
JK
20156/* Read directory or file name entry format, starting with byte of
20157 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20158 entries count and the entries themselves in the described entry
20159 format. */
20160
20161static void
ed2dc618
SM
20162read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20163 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20164 struct line_header *lh,
20165 const struct comp_unit_head *cu_header,
20166 void (*callback) (struct line_header *lh,
20167 const char *name,
ecfb656c 20168 dir_index d_index,
43988095
JK
20169 unsigned int mod_time,
20170 unsigned int length))
20171{
20172 gdb_byte format_count, formati;
20173 ULONGEST data_count, datai;
20174 const gdb_byte *buf = *bufp;
20175 const gdb_byte *format_header_data;
43988095
JK
20176 unsigned int bytes_read;
20177
20178 format_count = read_1_byte (abfd, buf);
20179 buf += 1;
20180 format_header_data = buf;
20181 for (formati = 0; formati < format_count; formati++)
20182 {
20183 read_unsigned_leb128 (abfd, buf, &bytes_read);
20184 buf += bytes_read;
20185 read_unsigned_leb128 (abfd, buf, &bytes_read);
20186 buf += bytes_read;
20187 }
20188
20189 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20190 buf += bytes_read;
20191 for (datai = 0; datai < data_count; datai++)
20192 {
20193 const gdb_byte *format = format_header_data;
20194 struct file_entry fe;
20195
43988095
JK
20196 for (formati = 0; formati < format_count; formati++)
20197 {
ecfb656c 20198 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20199 format += bytes_read;
43988095 20200
ecfb656c 20201 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20202 format += bytes_read;
ecfb656c
PA
20203
20204 gdb::optional<const char *> string;
20205 gdb::optional<unsigned int> uint;
20206
43988095
JK
20207 switch (form)
20208 {
20209 case DW_FORM_string:
ecfb656c 20210 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20211 buf += bytes_read;
20212 break;
20213
20214 case DW_FORM_line_strp:
ed2dc618
SM
20215 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20216 abfd, buf,
ecfb656c
PA
20217 cu_header,
20218 &bytes_read));
43988095
JK
20219 buf += bytes_read;
20220 break;
20221
20222 case DW_FORM_data1:
ecfb656c 20223 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20224 buf += 1;
20225 break;
20226
20227 case DW_FORM_data2:
ecfb656c 20228 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20229 buf += 2;
20230 break;
20231
20232 case DW_FORM_data4:
ecfb656c 20233 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20234 buf += 4;
20235 break;
20236
20237 case DW_FORM_data8:
ecfb656c 20238 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20239 buf += 8;
20240 break;
20241
20242 case DW_FORM_udata:
ecfb656c 20243 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20244 buf += bytes_read;
20245 break;
20246
20247 case DW_FORM_block:
20248 /* It is valid only for DW_LNCT_timestamp which is ignored by
20249 current GDB. */
20250 break;
20251 }
ecfb656c
PA
20252
20253 switch (content_type)
20254 {
20255 case DW_LNCT_path:
20256 if (string.has_value ())
20257 fe.name = *string;
20258 break;
20259 case DW_LNCT_directory_index:
20260 if (uint.has_value ())
20261 fe.d_index = (dir_index) *uint;
20262 break;
20263 case DW_LNCT_timestamp:
20264 if (uint.has_value ())
20265 fe.mod_time = *uint;
20266 break;
20267 case DW_LNCT_size:
20268 if (uint.has_value ())
20269 fe.length = *uint;
20270 break;
20271 case DW_LNCT_MD5:
20272 break;
20273 default:
20274 complaint (&symfile_complaints,
20275 _("Unknown format content type %s"),
20276 pulongest (content_type));
20277 }
43988095
JK
20278 }
20279
ecfb656c 20280 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20281 }
20282
20283 *bufp = buf;
20284}
20285
debd256d 20286/* Read the statement program header starting at OFFSET in
3019eac3 20287 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20288 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20289 Returns NULL if there is a problem reading the header, e.g., if it
20290 has a version we don't understand.
debd256d
JB
20291
20292 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20293 the returned object point into the dwarf line section buffer,
20294 and must not be freed. */
ae2de4f8 20295
fff8551c 20296static line_header_up
9c541725 20297dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20298{
d521ce57 20299 const gdb_byte *line_ptr;
c764a876 20300 unsigned int bytes_read, offset_size;
debd256d 20301 int i;
d521ce57 20302 const char *cur_dir, *cur_file;
3019eac3
DE
20303 struct dwarf2_section_info *section;
20304 bfd *abfd;
518817b3
SM
20305 struct dwarf2_per_objfile *dwarf2_per_objfile
20306 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20307
36586728 20308 section = get_debug_line_section (cu);
3019eac3
DE
20309 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20310 if (section->buffer == NULL)
debd256d 20311 {
3019eac3
DE
20312 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20313 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20314 else
20315 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
20316 return 0;
20317 }
20318
fceca515
DE
20319 /* We can't do this until we know the section is non-empty.
20320 Only then do we know we have such a section. */
a32a8923 20321 abfd = get_section_bfd_owner (section);
fceca515 20322
a738430d
MK
20323 /* Make sure that at least there's room for the total_length field.
20324 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20325 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20326 {
4d3c2250 20327 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20328 return 0;
20329 }
20330
fff8551c 20331 line_header_up lh (new line_header ());
debd256d 20332
9c541725 20333 lh->sect_off = sect_off;
527f3840
JK
20334 lh->offset_in_dwz = cu->per_cu->is_dwz;
20335
9c541725 20336 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20337
a738430d 20338 /* Read in the header. */
6e70227d 20339 lh->total_length =
c764a876
DE
20340 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20341 &bytes_read, &offset_size);
debd256d 20342 line_ptr += bytes_read;
3019eac3 20343 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20344 {
4d3c2250 20345 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20346 return 0;
20347 }
20348 lh->statement_program_end = line_ptr + lh->total_length;
20349 lh->version = read_2_bytes (abfd, line_ptr);
20350 line_ptr += 2;
43988095 20351 if (lh->version > 5)
cd366ee8
DE
20352 {
20353 /* This is a version we don't understand. The format could have
20354 changed in ways we don't handle properly so just punt. */
20355 complaint (&symfile_complaints,
20356 _("unsupported version in .debug_line section"));
20357 return NULL;
20358 }
43988095
JK
20359 if (lh->version >= 5)
20360 {
20361 gdb_byte segment_selector_size;
20362
20363 /* Skip address size. */
20364 read_1_byte (abfd, line_ptr);
20365 line_ptr += 1;
20366
20367 segment_selector_size = read_1_byte (abfd, line_ptr);
20368 line_ptr += 1;
20369 if (segment_selector_size != 0)
20370 {
20371 complaint (&symfile_complaints,
20372 _("unsupported segment selector size %u "
20373 "in .debug_line section"),
20374 segment_selector_size);
20375 return NULL;
20376 }
20377 }
c764a876
DE
20378 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20379 line_ptr += offset_size;
debd256d
JB
20380 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20381 line_ptr += 1;
2dc7f7b3
TT
20382 if (lh->version >= 4)
20383 {
20384 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20385 line_ptr += 1;
20386 }
20387 else
20388 lh->maximum_ops_per_instruction = 1;
20389
20390 if (lh->maximum_ops_per_instruction == 0)
20391 {
20392 lh->maximum_ops_per_instruction = 1;
20393 complaint (&symfile_complaints,
3e43a32a
MS
20394 _("invalid maximum_ops_per_instruction "
20395 "in `.debug_line' section"));
2dc7f7b3
TT
20396 }
20397
debd256d
JB
20398 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20399 line_ptr += 1;
20400 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20401 line_ptr += 1;
20402 lh->line_range = read_1_byte (abfd, line_ptr);
20403 line_ptr += 1;
20404 lh->opcode_base = read_1_byte (abfd, line_ptr);
20405 line_ptr += 1;
fff8551c 20406 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20407
20408 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20409 for (i = 1; i < lh->opcode_base; ++i)
20410 {
20411 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20412 line_ptr += 1;
20413 }
20414
43988095 20415 if (lh->version >= 5)
debd256d 20416 {
43988095 20417 /* Read directory table. */
ed2dc618
SM
20418 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20419 &cu->header,
fff8551c 20420 [] (struct line_header *lh, const char *name,
ecfb656c 20421 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20422 unsigned int length)
20423 {
20424 lh->add_include_dir (name);
20425 });
debd256d 20426
43988095 20427 /* Read file name table. */
ed2dc618
SM
20428 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20429 &cu->header,
fff8551c 20430 [] (struct line_header *lh, const char *name,
ecfb656c 20431 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20432 unsigned int length)
20433 {
ecfb656c 20434 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 20435 });
43988095
JK
20436 }
20437 else
debd256d 20438 {
43988095
JK
20439 /* Read directory table. */
20440 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20441 {
20442 line_ptr += bytes_read;
fff8551c 20443 lh->add_include_dir (cur_dir);
43988095 20444 }
debd256d
JB
20445 line_ptr += bytes_read;
20446
43988095
JK
20447 /* Read file name table. */
20448 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20449 {
ecfb656c
PA
20450 unsigned int mod_time, length;
20451 dir_index d_index;
43988095
JK
20452
20453 line_ptr += bytes_read;
ecfb656c 20454 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20455 line_ptr += bytes_read;
20456 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20457 line_ptr += bytes_read;
20458 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20459 line_ptr += bytes_read;
20460
ecfb656c 20461 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20462 }
20463 line_ptr += bytes_read;
debd256d 20464 }
6e70227d 20465 lh->statement_program_start = line_ptr;
debd256d 20466
3019eac3 20467 if (line_ptr > (section->buffer + section->size))
4d3c2250 20468 complaint (&symfile_complaints,
3e43a32a
MS
20469 _("line number info header doesn't "
20470 "fit in `.debug_line' section"));
debd256d 20471
debd256d
JB
20472 return lh;
20473}
c906108c 20474
c6da4cef
DE
20475/* Subroutine of dwarf_decode_lines to simplify it.
20476 Return the file name of the psymtab for included file FILE_INDEX
20477 in line header LH of PST.
20478 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20479 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
20480 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
20481
20482 The function creates dangling cleanup registration. */
c6da4cef 20483
d521ce57 20484static const char *
c6da4cef
DE
20485psymtab_include_file_name (const struct line_header *lh, int file_index,
20486 const struct partial_symtab *pst,
20487 const char *comp_dir)
20488{
8c43009f 20489 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20490 const char *include_name = fe.name;
20491 const char *include_name_to_compare = include_name;
72b9f47f
TT
20492 const char *pst_filename;
20493 char *copied_name = NULL;
c6da4cef
DE
20494 int file_is_pst;
20495
8c43009f 20496 const char *dir_name = fe.include_dir (lh);
c6da4cef
DE
20497
20498 if (!IS_ABSOLUTE_PATH (include_name)
20499 && (dir_name != NULL || comp_dir != NULL))
20500 {
20501 /* Avoid creating a duplicate psymtab for PST.
20502 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20503 Before we do the comparison, however, we need to account
20504 for DIR_NAME and COMP_DIR.
20505 First prepend dir_name (if non-NULL). If we still don't
20506 have an absolute path prepend comp_dir (if non-NULL).
20507 However, the directory we record in the include-file's
20508 psymtab does not contain COMP_DIR (to match the
20509 corresponding symtab(s)).
20510
20511 Example:
20512
20513 bash$ cd /tmp
20514 bash$ gcc -g ./hello.c
20515 include_name = "hello.c"
20516 dir_name = "."
20517 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20518 DW_AT_name = "./hello.c"
20519
20520 */
c6da4cef
DE
20521
20522 if (dir_name != NULL)
20523 {
d521ce57
TT
20524 char *tem = concat (dir_name, SLASH_STRING,
20525 include_name, (char *)NULL);
20526
20527 make_cleanup (xfree, tem);
20528 include_name = tem;
c6da4cef 20529 include_name_to_compare = include_name;
c6da4cef
DE
20530 }
20531 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20532 {
d521ce57
TT
20533 char *tem = concat (comp_dir, SLASH_STRING,
20534 include_name, (char *)NULL);
20535
20536 make_cleanup (xfree, tem);
20537 include_name_to_compare = tem;
c6da4cef
DE
20538 }
20539 }
20540
20541 pst_filename = pst->filename;
20542 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20543 {
72b9f47f
TT
20544 copied_name = concat (pst->dirname, SLASH_STRING,
20545 pst_filename, (char *)NULL);
20546 pst_filename = copied_name;
c6da4cef
DE
20547 }
20548
1e3fad37 20549 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20550
72b9f47f
TT
20551 if (copied_name != NULL)
20552 xfree (copied_name);
c6da4cef
DE
20553
20554 if (file_is_pst)
20555 return NULL;
20556 return include_name;
20557}
20558
d9b3de22
DE
20559/* State machine to track the state of the line number program. */
20560
6f77053d 20561class lnp_state_machine
d9b3de22 20562{
6f77053d
PA
20563public:
20564 /* Initialize a machine state for the start of a line number
20565 program. */
20566 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20567
8c43009f
PA
20568 file_entry *current_file ()
20569 {
20570 /* lh->file_names is 0-based, but the file name numbers in the
20571 statement program are 1-based. */
6f77053d
PA
20572 return m_line_header->file_name_at (m_file);
20573 }
20574
20575 /* Record the line in the state machine. END_SEQUENCE is true if
20576 we're processing the end of a sequence. */
20577 void record_line (bool end_sequence);
20578
20579 /* Check address and if invalid nop-out the rest of the lines in this
20580 sequence. */
20581 void check_line_address (struct dwarf2_cu *cu,
20582 const gdb_byte *line_ptr,
20583 CORE_ADDR lowpc, CORE_ADDR address);
20584
20585 void handle_set_discriminator (unsigned int discriminator)
20586 {
20587 m_discriminator = discriminator;
20588 m_line_has_non_zero_discriminator |= discriminator != 0;
20589 }
20590
20591 /* Handle DW_LNE_set_address. */
20592 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20593 {
20594 m_op_index = 0;
20595 address += baseaddr;
20596 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20597 }
20598
20599 /* Handle DW_LNS_advance_pc. */
20600 void handle_advance_pc (CORE_ADDR adjust);
20601
20602 /* Handle a special opcode. */
20603 void handle_special_opcode (unsigned char op_code);
20604
20605 /* Handle DW_LNS_advance_line. */
20606 void handle_advance_line (int line_delta)
20607 {
20608 advance_line (line_delta);
20609 }
20610
20611 /* Handle DW_LNS_set_file. */
20612 void handle_set_file (file_name_index file);
20613
20614 /* Handle DW_LNS_negate_stmt. */
20615 void handle_negate_stmt ()
20616 {
20617 m_is_stmt = !m_is_stmt;
20618 }
20619
20620 /* Handle DW_LNS_const_add_pc. */
20621 void handle_const_add_pc ();
20622
20623 /* Handle DW_LNS_fixed_advance_pc. */
20624 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20625 {
20626 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20627 m_op_index = 0;
20628 }
20629
20630 /* Handle DW_LNS_copy. */
20631 void handle_copy ()
20632 {
20633 record_line (false);
20634 m_discriminator = 0;
20635 }
20636
20637 /* Handle DW_LNE_end_sequence. */
20638 void handle_end_sequence ()
20639 {
20640 m_record_line_callback = ::record_line;
20641 }
20642
20643private:
20644 /* Advance the line by LINE_DELTA. */
20645 void advance_line (int line_delta)
20646 {
20647 m_line += line_delta;
20648
20649 if (line_delta != 0)
20650 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20651 }
20652
6f77053d
PA
20653 gdbarch *m_gdbarch;
20654
20655 /* True if we're recording lines.
20656 Otherwise we're building partial symtabs and are just interested in
20657 finding include files mentioned by the line number program. */
20658 bool m_record_lines_p;
20659
8c43009f 20660 /* The line number header. */
6f77053d 20661 line_header *m_line_header;
8c43009f 20662
6f77053d
PA
20663 /* These are part of the standard DWARF line number state machine,
20664 and initialized according to the DWARF spec. */
d9b3de22 20665
6f77053d 20666 unsigned char m_op_index = 0;
8c43009f 20667 /* The line table index (1-based) of the current file. */
6f77053d
PA
20668 file_name_index m_file = (file_name_index) 1;
20669 unsigned int m_line = 1;
20670
20671 /* These are initialized in the constructor. */
20672
20673 CORE_ADDR m_address;
20674 bool m_is_stmt;
20675 unsigned int m_discriminator;
d9b3de22
DE
20676
20677 /* Additional bits of state we need to track. */
20678
20679 /* The last file that we called dwarf2_start_subfile for.
20680 This is only used for TLLs. */
6f77053d 20681 unsigned int m_last_file = 0;
d9b3de22 20682 /* The last file a line number was recorded for. */
6f77053d 20683 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
20684
20685 /* The function to call to record a line. */
6f77053d 20686 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
20687
20688 /* The last line number that was recorded, used to coalesce
20689 consecutive entries for the same line. This can happen, for
20690 example, when discriminators are present. PR 17276. */
6f77053d
PA
20691 unsigned int m_last_line = 0;
20692 bool m_line_has_non_zero_discriminator = false;
8c43009f 20693};
d9b3de22 20694
6f77053d
PA
20695void
20696lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20697{
20698 CORE_ADDR addr_adj = (((m_op_index + adjust)
20699 / m_line_header->maximum_ops_per_instruction)
20700 * m_line_header->minimum_instruction_length);
20701 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20702 m_op_index = ((m_op_index + adjust)
20703 % m_line_header->maximum_ops_per_instruction);
20704}
d9b3de22 20705
6f77053d
PA
20706void
20707lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20708{
6f77053d
PA
20709 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20710 CORE_ADDR addr_adj = (((m_op_index
20711 + (adj_opcode / m_line_header->line_range))
20712 / m_line_header->maximum_ops_per_instruction)
20713 * m_line_header->minimum_instruction_length);
20714 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20715 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20716 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20717
6f77053d
PA
20718 int line_delta = (m_line_header->line_base
20719 + (adj_opcode % m_line_header->line_range));
20720 advance_line (line_delta);
20721 record_line (false);
20722 m_discriminator = 0;
20723}
d9b3de22 20724
6f77053d
PA
20725void
20726lnp_state_machine::handle_set_file (file_name_index file)
20727{
20728 m_file = file;
20729
20730 const file_entry *fe = current_file ();
20731 if (fe == NULL)
20732 dwarf2_debug_line_missing_file_complaint ();
20733 else if (m_record_lines_p)
20734 {
20735 const char *dir = fe->include_dir (m_line_header);
20736
20737 m_last_subfile = current_subfile;
20738 m_line_has_non_zero_discriminator = m_discriminator != 0;
20739 dwarf2_start_subfile (fe->name, dir);
20740 }
20741}
20742
20743void
20744lnp_state_machine::handle_const_add_pc ()
20745{
20746 CORE_ADDR adjust
20747 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20748
20749 CORE_ADDR addr_adj
20750 = (((m_op_index + adjust)
20751 / m_line_header->maximum_ops_per_instruction)
20752 * m_line_header->minimum_instruction_length);
20753
20754 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20755 m_op_index = ((m_op_index + adjust)
20756 % m_line_header->maximum_ops_per_instruction);
20757}
d9b3de22 20758
c91513d8
PP
20759/* Ignore this record_line request. */
20760
20761static void
20762noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20763{
20764 return;
20765}
20766
a05a36a5
DE
20767/* Return non-zero if we should add LINE to the line number table.
20768 LINE is the line to add, LAST_LINE is the last line that was added,
20769 LAST_SUBFILE is the subfile for LAST_LINE.
20770 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20771 had a non-zero discriminator.
20772
20773 We have to be careful in the presence of discriminators.
20774 E.g., for this line:
20775
20776 for (i = 0; i < 100000; i++);
20777
20778 clang can emit four line number entries for that one line,
20779 each with a different discriminator.
20780 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20781
20782 However, we want gdb to coalesce all four entries into one.
20783 Otherwise the user could stepi into the middle of the line and
20784 gdb would get confused about whether the pc really was in the
20785 middle of the line.
20786
20787 Things are further complicated by the fact that two consecutive
20788 line number entries for the same line is a heuristic used by gcc
20789 to denote the end of the prologue. So we can't just discard duplicate
20790 entries, we have to be selective about it. The heuristic we use is
20791 that we only collapse consecutive entries for the same line if at least
20792 one of those entries has a non-zero discriminator. PR 17276.
20793
20794 Note: Addresses in the line number state machine can never go backwards
20795 within one sequence, thus this coalescing is ok. */
20796
20797static int
20798dwarf_record_line_p (unsigned int line, unsigned int last_line,
20799 int line_has_non_zero_discriminator,
20800 struct subfile *last_subfile)
20801{
20802 if (current_subfile != last_subfile)
20803 return 1;
20804 if (line != last_line)
20805 return 1;
20806 /* Same line for the same file that we've seen already.
20807 As a last check, for pr 17276, only record the line if the line
20808 has never had a non-zero discriminator. */
20809 if (!line_has_non_zero_discriminator)
20810 return 1;
20811 return 0;
20812}
20813
252a6764
DE
20814/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20815 in the line table of subfile SUBFILE. */
20816
20817static void
d9b3de22
DE
20818dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20819 unsigned int line, CORE_ADDR address,
20820 record_line_ftype p_record_line)
252a6764
DE
20821{
20822 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20823
27e0867f
DE
20824 if (dwarf_line_debug)
20825 {
20826 fprintf_unfiltered (gdb_stdlog,
20827 "Recording line %u, file %s, address %s\n",
20828 line, lbasename (subfile->name),
20829 paddress (gdbarch, address));
20830 }
20831
d5962de5 20832 (*p_record_line) (subfile, line, addr);
252a6764
DE
20833}
20834
20835/* Subroutine of dwarf_decode_lines_1 to simplify it.
20836 Mark the end of a set of line number records.
d9b3de22 20837 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20838 If SUBFILE is NULL the request is ignored. */
20839
20840static void
20841dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20842 CORE_ADDR address, record_line_ftype p_record_line)
20843{
27e0867f
DE
20844 if (subfile == NULL)
20845 return;
20846
20847 if (dwarf_line_debug)
20848 {
20849 fprintf_unfiltered (gdb_stdlog,
20850 "Finishing current line, file %s, address %s\n",
20851 lbasename (subfile->name),
20852 paddress (gdbarch, address));
20853 }
20854
d9b3de22
DE
20855 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20856}
20857
6f77053d
PA
20858void
20859lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20860{
d9b3de22
DE
20861 if (dwarf_line_debug)
20862 {
20863 fprintf_unfiltered (gdb_stdlog,
20864 "Processing actual line %u: file %u,"
20865 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
20866 m_line, to_underlying (m_file),
20867 paddress (m_gdbarch, m_address),
20868 m_is_stmt, m_discriminator);
d9b3de22
DE
20869 }
20870
6f77053d 20871 file_entry *fe = current_file ();
8c43009f
PA
20872
20873 if (fe == NULL)
d9b3de22
DE
20874 dwarf2_debug_line_missing_file_complaint ();
20875 /* For now we ignore lines not starting on an instruction boundary.
20876 But not when processing end_sequence for compatibility with the
20877 previous version of the code. */
6f77053d 20878 else if (m_op_index == 0 || end_sequence)
d9b3de22 20879 {
8c43009f 20880 fe->included_p = 1;
6f77053d 20881 if (m_record_lines_p && m_is_stmt)
d9b3de22 20882 {
6f77053d 20883 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 20884 {
6f77053d
PA
20885 dwarf_finish_line (m_gdbarch, m_last_subfile,
20886 m_address, m_record_line_callback);
d9b3de22
DE
20887 }
20888
20889 if (!end_sequence)
20890 {
6f77053d
PA
20891 if (dwarf_record_line_p (m_line, m_last_line,
20892 m_line_has_non_zero_discriminator,
20893 m_last_subfile))
d9b3de22 20894 {
6f77053d
PA
20895 dwarf_record_line_1 (m_gdbarch, current_subfile,
20896 m_line, m_address,
20897 m_record_line_callback);
d9b3de22 20898 }
6f77053d
PA
20899 m_last_subfile = current_subfile;
20900 m_last_line = m_line;
d9b3de22
DE
20901 }
20902 }
20903 }
20904}
20905
6f77053d
PA
20906lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20907 bool record_lines_p)
d9b3de22 20908{
6f77053d
PA
20909 m_gdbarch = arch;
20910 m_record_lines_p = record_lines_p;
20911 m_line_header = lh;
d9b3de22 20912
6f77053d 20913 m_record_line_callback = ::record_line;
d9b3de22 20914
d9b3de22
DE
20915 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20916 was a line entry for it so that the backend has a chance to adjust it
20917 and also record it in case it needs it. This is currently used by MIPS
20918 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20919 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20920 m_is_stmt = lh->default_is_stmt;
20921 m_discriminator = 0;
252a6764
DE
20922}
20923
6f77053d
PA
20924void
20925lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20926 const gdb_byte *line_ptr,
20927 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
20928{
20929 /* If address < lowpc then it's not a usable value, it's outside the
20930 pc range of the CU. However, we restrict the test to only address
20931 values of zero to preserve GDB's previous behaviour which is to
20932 handle the specific case of a function being GC'd by the linker. */
20933
20934 if (address == 0 && address < lowpc)
20935 {
20936 /* This line table is for a function which has been
20937 GCd by the linker. Ignore it. PR gdb/12528 */
20938
518817b3 20939 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20940 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20941
20942 complaint (&symfile_complaints,
20943 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20944 line_offset, objfile_name (objfile));
6f77053d
PA
20945 m_record_line_callback = noop_record_line;
20946 /* Note: record_line_callback is left as noop_record_line until
20947 we see DW_LNE_end_sequence. */
924c2928
DE
20948 }
20949}
20950
f3f5162e 20951/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20952 Process the line number information in LH.
20953 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20954 program in order to set included_p for every referenced header. */
debd256d 20955
c906108c 20956static void
43f3e411
DE
20957dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20958 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20959{
d521ce57
TT
20960 const gdb_byte *line_ptr, *extended_end;
20961 const gdb_byte *line_end;
a8c50c1f 20962 unsigned int bytes_read, extended_len;
699ca60a 20963 unsigned char op_code, extended_op;
e142c38c 20964 CORE_ADDR baseaddr;
518817b3 20965 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20966 bfd *abfd = objfile->obfd;
fbf65064 20967 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20968 /* True if we're recording line info (as opposed to building partial
20969 symtabs and just interested in finding include files mentioned by
20970 the line number program). */
20971 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
20972
20973 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 20974
debd256d
JB
20975 line_ptr = lh->statement_program_start;
20976 line_end = lh->statement_program_end;
c906108c
SS
20977
20978 /* Read the statement sequences until there's nothing left. */
20979 while (line_ptr < line_end)
20980 {
6f77053d
PA
20981 /* The DWARF line number program state machine. Reset the state
20982 machine at the start of each sequence. */
20983 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20984 bool end_sequence = false;
d9b3de22 20985
8c43009f 20986 if (record_lines_p)
c906108c 20987 {
8c43009f
PA
20988 /* Start a subfile for the current file of the state
20989 machine. */
20990 const file_entry *fe = state_machine.current_file ();
20991
20992 if (fe != NULL)
20993 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
20994 }
20995
a738430d 20996 /* Decode the table. */
d9b3de22 20997 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20998 {
20999 op_code = read_1_byte (abfd, line_ptr);
21000 line_ptr += 1;
9aa1fe7e 21001
debd256d 21002 if (op_code >= lh->opcode_base)
6e70227d 21003 {
8e07a239 21004 /* Special opcode. */
6f77053d 21005 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21006 }
21007 else switch (op_code)
c906108c
SS
21008 {
21009 case DW_LNS_extended_op:
3e43a32a
MS
21010 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21011 &bytes_read);
473b7be6 21012 line_ptr += bytes_read;
a8c50c1f 21013 extended_end = line_ptr + extended_len;
c906108c
SS
21014 extended_op = read_1_byte (abfd, line_ptr);
21015 line_ptr += 1;
21016 switch (extended_op)
21017 {
21018 case DW_LNE_end_sequence:
6f77053d
PA
21019 state_machine.handle_end_sequence ();
21020 end_sequence = true;
c906108c
SS
21021 break;
21022 case DW_LNE_set_address:
d9b3de22
DE
21023 {
21024 CORE_ADDR address
21025 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21026 line_ptr += bytes_read;
6f77053d
PA
21027
21028 state_machine.check_line_address (cu, line_ptr,
21029 lowpc, address);
21030 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21031 }
c906108c
SS
21032 break;
21033 case DW_LNE_define_file:
debd256d 21034 {
d521ce57 21035 const char *cur_file;
ecfb656c
PA
21036 unsigned int mod_time, length;
21037 dir_index dindex;
6e70227d 21038
3e43a32a
MS
21039 cur_file = read_direct_string (abfd, line_ptr,
21040 &bytes_read);
debd256d 21041 line_ptr += bytes_read;
ecfb656c 21042 dindex = (dir_index)
debd256d
JB
21043 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21044 line_ptr += bytes_read;
21045 mod_time =
21046 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21047 line_ptr += bytes_read;
21048 length =
21049 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21050 line_ptr += bytes_read;
ecfb656c 21051 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21052 }
c906108c 21053 break;
d0c6ba3d 21054 case DW_LNE_set_discriminator:
6f77053d
PA
21055 {
21056 /* The discriminator is not interesting to the
21057 debugger; just ignore it. We still need to
21058 check its value though:
21059 if there are consecutive entries for the same
21060 (non-prologue) line we want to coalesce them.
21061 PR 17276. */
21062 unsigned int discr
21063 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21064 line_ptr += bytes_read;
21065
21066 state_machine.handle_set_discriminator (discr);
21067 }
d0c6ba3d 21068 break;
c906108c 21069 default:
4d3c2250 21070 complaint (&symfile_complaints,
e2e0b3e5 21071 _("mangled .debug_line section"));
debd256d 21072 return;
c906108c 21073 }
a8c50c1f
DJ
21074 /* Make sure that we parsed the extended op correctly. If e.g.
21075 we expected a different address size than the producer used,
21076 we may have read the wrong number of bytes. */
21077 if (line_ptr != extended_end)
21078 {
21079 complaint (&symfile_complaints,
21080 _("mangled .debug_line section"));
21081 return;
21082 }
c906108c
SS
21083 break;
21084 case DW_LNS_copy:
6f77053d 21085 state_machine.handle_copy ();
c906108c
SS
21086 break;
21087 case DW_LNS_advance_pc:
2dc7f7b3
TT
21088 {
21089 CORE_ADDR adjust
21090 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21091 line_ptr += bytes_read;
6f77053d
PA
21092
21093 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21094 }
c906108c
SS
21095 break;
21096 case DW_LNS_advance_line:
a05a36a5
DE
21097 {
21098 int line_delta
21099 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21100 line_ptr += bytes_read;
6f77053d
PA
21101
21102 state_machine.handle_advance_line (line_delta);
a05a36a5 21103 }
c906108c
SS
21104 break;
21105 case DW_LNS_set_file:
d9b3de22 21106 {
6f77053d 21107 file_name_index file
ecfb656c
PA
21108 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21109 &bytes_read);
d9b3de22 21110 line_ptr += bytes_read;
8c43009f 21111
6f77053d 21112 state_machine.handle_set_file (file);
d9b3de22 21113 }
c906108c
SS
21114 break;
21115 case DW_LNS_set_column:
0ad93d4f 21116 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21117 line_ptr += bytes_read;
21118 break;
21119 case DW_LNS_negate_stmt:
6f77053d 21120 state_machine.handle_negate_stmt ();
c906108c
SS
21121 break;
21122 case DW_LNS_set_basic_block:
c906108c 21123 break;
c2c6d25f
JM
21124 /* Add to the address register of the state machine the
21125 address increment value corresponding to special opcode
a738430d
MK
21126 255. I.e., this value is scaled by the minimum
21127 instruction length since special opcode 255 would have
b021a221 21128 scaled the increment. */
c906108c 21129 case DW_LNS_const_add_pc:
6f77053d 21130 state_machine.handle_const_add_pc ();
c906108c
SS
21131 break;
21132 case DW_LNS_fixed_advance_pc:
3e29f34a 21133 {
6f77053d 21134 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21135 line_ptr += 2;
6f77053d
PA
21136
21137 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21138 }
c906108c 21139 break;
9aa1fe7e 21140 default:
a738430d
MK
21141 {
21142 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21143 int i;
a738430d 21144
debd256d 21145 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21146 {
21147 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21148 line_ptr += bytes_read;
21149 }
21150 }
c906108c
SS
21151 }
21152 }
d9b3de22
DE
21153
21154 if (!end_sequence)
21155 dwarf2_debug_line_missing_end_sequence_complaint ();
21156
21157 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21158 in which case we still finish recording the last line). */
6f77053d 21159 state_machine.record_line (true);
c906108c 21160 }
f3f5162e
DE
21161}
21162
21163/* Decode the Line Number Program (LNP) for the given line_header
21164 structure and CU. The actual information extracted and the type
21165 of structures created from the LNP depends on the value of PST.
21166
21167 1. If PST is NULL, then this procedure uses the data from the program
21168 to create all necessary symbol tables, and their linetables.
21169
21170 2. If PST is not NULL, this procedure reads the program to determine
21171 the list of files included by the unit represented by PST, and
21172 builds all the associated partial symbol tables.
21173
21174 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21175 It is used for relative paths in the line table.
21176 NOTE: When processing partial symtabs (pst != NULL),
21177 comp_dir == pst->dirname.
21178
21179 NOTE: It is important that psymtabs have the same file name (via strcmp)
21180 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21181 symtab we don't use it in the name of the psymtabs we create.
21182 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21183 A good testcase for this is mb-inline.exp.
21184
527f3840
JK
21185 LOWPC is the lowest address in CU (or 0 if not known).
21186
21187 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21188 for its PC<->lines mapping information. Otherwise only the filename
21189 table is read in. */
f3f5162e
DE
21190
21191static void
21192dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21193 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21194 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21195{
518817b3 21196 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21197 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21198
527f3840
JK
21199 if (decode_mapping)
21200 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21201
21202 if (decode_for_pst_p)
21203 {
21204 int file_index;
21205
21206 /* Now that we're done scanning the Line Header Program, we can
21207 create the psymtab of each included file. */
fff8551c 21208 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
21209 if (lh->file_names[file_index].included_p == 1)
21210 {
d521ce57 21211 const char *include_name =
c6da4cef
DE
21212 psymtab_include_file_name (lh, file_index, pst, comp_dir);
21213 if (include_name != NULL)
aaa75496
JB
21214 dwarf2_create_include_psymtab (include_name, pst, objfile);
21215 }
21216 }
cb1df416
DJ
21217 else
21218 {
21219 /* Make sure a symtab is created for every file, even files
21220 which contain only variables (i.e. no code with associated
21221 line numbers). */
43f3e411 21222 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 21223 int i;
cb1df416 21224
fff8551c 21225 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 21226 {
8c43009f 21227 file_entry &fe = lh->file_names[i];
9a619af0 21228
8c43009f 21229 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 21230
cb1df416 21231 if (current_subfile->symtab == NULL)
43f3e411
DE
21232 {
21233 current_subfile->symtab
21234 = allocate_symtab (cust, current_subfile->name);
21235 }
8c43009f 21236 fe.symtab = current_subfile->symtab;
cb1df416
DJ
21237 }
21238 }
c906108c
SS
21239}
21240
21241/* Start a subfile for DWARF. FILENAME is the name of the file and
21242 DIRNAME the name of the source directory which contains FILENAME
4d663531 21243 or NULL if not known.
c906108c
SS
21244 This routine tries to keep line numbers from identical absolute and
21245 relative file names in a common subfile.
21246
21247 Using the `list' example from the GDB testsuite, which resides in
21248 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21249 of /srcdir/list0.c yields the following debugging information for list0.c:
21250
c5aa993b 21251 DW_AT_name: /srcdir/list0.c
4d663531 21252 DW_AT_comp_dir: /compdir
357e46e7 21253 files.files[0].name: list0.h
c5aa993b 21254 files.files[0].dir: /srcdir
357e46e7 21255 files.files[1].name: list0.c
c5aa993b 21256 files.files[1].dir: /srcdir
c906108c
SS
21257
21258 The line number information for list0.c has to end up in a single
4f1520fb
FR
21259 subfile, so that `break /srcdir/list0.c:1' works as expected.
21260 start_subfile will ensure that this happens provided that we pass the
21261 concatenation of files.files[1].dir and files.files[1].name as the
21262 subfile's name. */
c906108c
SS
21263
21264static void
4d663531 21265dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 21266{
d521ce57 21267 char *copy = NULL;
4f1520fb 21268
4d663531 21269 /* In order not to lose the line information directory,
4f1520fb
FR
21270 we concatenate it to the filename when it makes sense.
21271 Note that the Dwarf3 standard says (speaking of filenames in line
21272 information): ``The directory index is ignored for file names
21273 that represent full path names''. Thus ignoring dirname in the
21274 `else' branch below isn't an issue. */
c906108c 21275
d5166ae1 21276 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21277 {
21278 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21279 filename = copy;
21280 }
c906108c 21281
4d663531 21282 start_subfile (filename);
4f1520fb 21283
d521ce57
TT
21284 if (copy != NULL)
21285 xfree (copy);
c906108c
SS
21286}
21287
f4dc4d17
DE
21288/* Start a symtab for DWARF.
21289 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21290
43f3e411 21291static struct compunit_symtab *
f4dc4d17 21292dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 21293 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 21294{
43f3e411 21295 struct compunit_symtab *cust
518817b3
SM
21296 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21297 low_pc, cu->language);
43f3e411 21298
f4dc4d17
DE
21299 record_debugformat ("DWARF 2");
21300 record_producer (cu->producer);
21301
21302 /* We assume that we're processing GCC output. */
21303 processing_gcc_compilation = 2;
21304
4d4ec4e5 21305 cu->processing_has_namespace_info = 0;
43f3e411
DE
21306
21307 return cust;
f4dc4d17
DE
21308}
21309
4c2df51b
DJ
21310static void
21311var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21312 struct dwarf2_cu *cu)
4c2df51b 21313{
518817b3 21314 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21315 struct comp_unit_head *cu_header = &cu->header;
21316
4c2df51b
DJ
21317 /* NOTE drow/2003-01-30: There used to be a comment and some special
21318 code here to turn a symbol with DW_AT_external and a
21319 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21320 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21321 with some versions of binutils) where shared libraries could have
21322 relocations against symbols in their debug information - the
21323 minimal symbol would have the right address, but the debug info
21324 would not. It's no longer necessary, because we will explicitly
21325 apply relocations when we read in the debug information now. */
21326
21327 /* A DW_AT_location attribute with no contents indicates that a
21328 variable has been optimized away. */
21329 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21330 {
f1e6e072 21331 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21332 return;
21333 }
21334
21335 /* Handle one degenerate form of location expression specially, to
21336 preserve GDB's previous behavior when section offsets are
3019eac3
DE
21337 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21338 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21339
21340 if (attr_form_is_block (attr)
3019eac3
DE
21341 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21342 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21343 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21344 && (DW_BLOCK (attr)->size
21345 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21346 {
891d2f0b 21347 unsigned int dummy;
4c2df51b 21348
3019eac3
DE
21349 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21350 SYMBOL_VALUE_ADDRESS (sym) =
21351 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21352 else
21353 SYMBOL_VALUE_ADDRESS (sym) =
21354 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 21355 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
21356 fixup_symbol_section (sym, objfile);
21357 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21358 SYMBOL_SECTION (sym));
4c2df51b
DJ
21359 return;
21360 }
21361
21362 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21363 expression evaluator, and use LOC_COMPUTED only when necessary
21364 (i.e. when the value of a register or memory location is
21365 referenced, or a thread-local block, etc.). Then again, it might
21366 not be worthwhile. I'm assuming that it isn't unless performance
21367 or memory numbers show me otherwise. */
21368
f1e6e072 21369 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21370
f1e6e072 21371 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 21372 cu->has_loclist = 1;
4c2df51b
DJ
21373}
21374
c906108c
SS
21375/* Given a pointer to a DWARF information entry, figure out if we need
21376 to make a symbol table entry for it, and if so, create a new entry
21377 and return a pointer to it.
21378 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21379 used the passed type.
21380 If SPACE is not NULL, use it to hold the new symbol. If it is
21381 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21382
21383static struct symbol *
34eaf542
TT
21384new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21385 struct symbol *space)
c906108c 21386{
518817b3
SM
21387 struct dwarf2_per_objfile *dwarf2_per_objfile
21388 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21389 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21390 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21391 struct symbol *sym = NULL;
15d034d0 21392 const char *name;
c906108c
SS
21393 struct attribute *attr = NULL;
21394 struct attribute *attr2 = NULL;
e142c38c 21395 CORE_ADDR baseaddr;
e37fd15a
SW
21396 struct pending **list_to_add = NULL;
21397
edb3359d 21398 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21399
21400 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21401
94af9270 21402 name = dwarf2_name (die, cu);
c906108c
SS
21403 if (name)
21404 {
94af9270 21405 const char *linkagename;
34eaf542 21406 int suppress_add = 0;
94af9270 21407
34eaf542
TT
21408 if (space)
21409 sym = space;
21410 else
e623cf5d 21411 sym = allocate_symbol (objfile);
c906108c 21412 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21413
21414 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21415 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21416 linkagename = dwarf2_physname (name, die, cu);
21417 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21418
f55ee35c
JK
21419 /* Fortran does not have mangling standard and the mangling does differ
21420 between gfortran, iFort etc. */
21421 if (cu->language == language_fortran
b250c185 21422 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21423 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21424 dwarf2_full_name (name, die, cu),
29df156d 21425 NULL);
f55ee35c 21426
c906108c 21427 /* Default assumptions.
c5aa993b 21428 Use the passed type or decode it from the die. */
176620f1 21429 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21430 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21431 if (type != NULL)
21432 SYMBOL_TYPE (sym) = type;
21433 else
e7c27a73 21434 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21435 attr = dwarf2_attr (die,
21436 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21437 cu);
c906108c
SS
21438 if (attr)
21439 {
21440 SYMBOL_LINE (sym) = DW_UNSND (attr);
21441 }
cb1df416 21442
edb3359d
DJ
21443 attr = dwarf2_attr (die,
21444 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21445 cu);
cb1df416
DJ
21446 if (attr)
21447 {
ecfb656c 21448 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21449 struct file_entry *fe;
9a619af0 21450
ecfb656c
PA
21451 if (cu->line_header != NULL)
21452 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21453 else
21454 fe = NULL;
21455
21456 if (fe == NULL)
cb1df416
DJ
21457 complaint (&symfile_complaints,
21458 _("file index out of range"));
8c43009f
PA
21459 else
21460 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21461 }
21462
c906108c
SS
21463 switch (die->tag)
21464 {
21465 case DW_TAG_label:
e142c38c 21466 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21467 if (attr)
3e29f34a
MR
21468 {
21469 CORE_ADDR addr;
21470
21471 addr = attr_value_as_address (attr);
21472 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21473 SYMBOL_VALUE_ADDRESS (sym) = addr;
21474 }
0f5238ed
TT
21475 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21476 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21477 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 21478 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21479 break;
21480 case DW_TAG_subprogram:
21481 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21482 finish_block. */
f1e6e072 21483 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21484 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
21485 if ((attr2 && (DW_UNSND (attr2) != 0))
21486 || cu->language == language_ada)
c906108c 21487 {
2cfa0c8d
JB
21488 /* Subprograms marked external are stored as a global symbol.
21489 Ada subprograms, whether marked external or not, are always
21490 stored as a global symbol, because we want to be able to
21491 access them globally. For instance, we want to be able
21492 to break on a nested subprogram without having to
21493 specify the context. */
e37fd15a 21494 list_to_add = &global_symbols;
c906108c
SS
21495 }
21496 else
21497 {
e37fd15a 21498 list_to_add = cu->list_in_scope;
c906108c
SS
21499 }
21500 break;
edb3359d
DJ
21501 case DW_TAG_inlined_subroutine:
21502 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21503 finish_block. */
f1e6e072 21504 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21505 SYMBOL_INLINED (sym) = 1;
481860b3 21506 list_to_add = cu->list_in_scope;
edb3359d 21507 break;
34eaf542
TT
21508 case DW_TAG_template_value_param:
21509 suppress_add = 1;
21510 /* Fall through. */
72929c62 21511 case DW_TAG_constant:
c906108c 21512 case DW_TAG_variable:
254e6b9e 21513 case DW_TAG_member:
0963b4bd
MS
21514 /* Compilation with minimal debug info may result in
21515 variables with missing type entries. Change the
21516 misleading `void' type to something sensible. */
c906108c 21517 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21518 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21519
e142c38c 21520 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21521 /* In the case of DW_TAG_member, we should only be called for
21522 static const members. */
21523 if (die->tag == DW_TAG_member)
21524 {
3863f96c
DE
21525 /* dwarf2_add_field uses die_is_declaration,
21526 so we do the same. */
254e6b9e
DE
21527 gdb_assert (die_is_declaration (die, cu));
21528 gdb_assert (attr);
21529 }
c906108c
SS
21530 if (attr)
21531 {
e7c27a73 21532 dwarf2_const_value (attr, sym, cu);
e142c38c 21533 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21534 if (!suppress_add)
34eaf542
TT
21535 {
21536 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 21537 list_to_add = &global_symbols;
34eaf542 21538 else
e37fd15a 21539 list_to_add = cu->list_in_scope;
34eaf542 21540 }
c906108c
SS
21541 break;
21542 }
e142c38c 21543 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21544 if (attr)
21545 {
e7c27a73 21546 var_decode_location (attr, sym, cu);
e142c38c 21547 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21548
21549 /* Fortran explicitly imports any global symbols to the local
21550 scope by DW_TAG_common_block. */
21551 if (cu->language == language_fortran && die->parent
21552 && die->parent->tag == DW_TAG_common_block)
21553 attr2 = NULL;
21554
caac4577
JG
21555 if (SYMBOL_CLASS (sym) == LOC_STATIC
21556 && SYMBOL_VALUE_ADDRESS (sym) == 0
21557 && !dwarf2_per_objfile->has_section_at_zero)
21558 {
21559 /* When a static variable is eliminated by the linker,
21560 the corresponding debug information is not stripped
21561 out, but the variable address is set to null;
21562 do not add such variables into symbol table. */
21563 }
21564 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21565 {
f55ee35c
JK
21566 /* Workaround gfortran PR debug/40040 - it uses
21567 DW_AT_location for variables in -fPIC libraries which may
21568 get overriden by other libraries/executable and get
21569 a different address. Resolve it by the minimal symbol
21570 which may come from inferior's executable using copy
21571 relocation. Make this workaround only for gfortran as for
21572 other compilers GDB cannot guess the minimal symbol
21573 Fortran mangling kind. */
21574 if (cu->language == language_fortran && die->parent
21575 && die->parent->tag == DW_TAG_module
21576 && cu->producer
28586665 21577 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 21578 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 21579
1c809c68
TT
21580 /* A variable with DW_AT_external is never static,
21581 but it may be block-scoped. */
21582 list_to_add = (cu->list_in_scope == &file_symbols
21583 ? &global_symbols : cu->list_in_scope);
1c809c68 21584 }
c906108c 21585 else
e37fd15a 21586 list_to_add = cu->list_in_scope;
c906108c
SS
21587 }
21588 else
21589 {
21590 /* We do not know the address of this symbol.
c5aa993b
JM
21591 If it is an external symbol and we have type information
21592 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21593 The address of the variable will then be determined from
21594 the minimal symbol table whenever the variable is
21595 referenced. */
e142c38c 21596 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21597
21598 /* Fortran explicitly imports any global symbols to the local
21599 scope by DW_TAG_common_block. */
21600 if (cu->language == language_fortran && die->parent
21601 && die->parent->tag == DW_TAG_common_block)
21602 {
21603 /* SYMBOL_CLASS doesn't matter here because
21604 read_common_block is going to reset it. */
21605 if (!suppress_add)
21606 list_to_add = cu->list_in_scope;
21607 }
21608 else if (attr2 && (DW_UNSND (attr2) != 0)
21609 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21610 {
0fe7935b
DJ
21611 /* A variable with DW_AT_external is never static, but it
21612 may be block-scoped. */
21613 list_to_add = (cu->list_in_scope == &file_symbols
21614 ? &global_symbols : cu->list_in_scope);
21615
f1e6e072 21616 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21617 }
442ddf59
JK
21618 else if (!die_is_declaration (die, cu))
21619 {
21620 /* Use the default LOC_OPTIMIZED_OUT class. */
21621 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21622 if (!suppress_add)
21623 list_to_add = cu->list_in_scope;
442ddf59 21624 }
c906108c
SS
21625 }
21626 break;
21627 case DW_TAG_formal_parameter:
edb3359d
DJ
21628 /* If we are inside a function, mark this as an argument. If
21629 not, we might be looking at an argument to an inlined function
21630 when we do not have enough information to show inlined frames;
21631 pretend it's a local variable in that case so that the user can
21632 still see it. */
21633 if (context_stack_depth > 0
21634 && context_stack[context_stack_depth - 1].name != NULL)
21635 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 21636 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21637 if (attr)
21638 {
e7c27a73 21639 var_decode_location (attr, sym, cu);
c906108c 21640 }
e142c38c 21641 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21642 if (attr)
21643 {
e7c27a73 21644 dwarf2_const_value (attr, sym, cu);
c906108c 21645 }
f346a30d 21646
e37fd15a 21647 list_to_add = cu->list_in_scope;
c906108c
SS
21648 break;
21649 case DW_TAG_unspecified_parameters:
21650 /* From varargs functions; gdb doesn't seem to have any
21651 interest in this information, so just ignore it for now.
21652 (FIXME?) */
21653 break;
34eaf542
TT
21654 case DW_TAG_template_type_param:
21655 suppress_add = 1;
21656 /* Fall through. */
c906108c 21657 case DW_TAG_class_type:
680b30c7 21658 case DW_TAG_interface_type:
c906108c
SS
21659 case DW_TAG_structure_type:
21660 case DW_TAG_union_type:
72019c9c 21661 case DW_TAG_set_type:
c906108c 21662 case DW_TAG_enumeration_type:
f1e6e072 21663 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21664 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21665
63d06c5c 21666 {
9c37b5ae 21667 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21668 really ever be static objects: otherwise, if you try
21669 to, say, break of a class's method and you're in a file
21670 which doesn't mention that class, it won't work unless
21671 the check for all static symbols in lookup_symbol_aux
21672 saves you. See the OtherFileClass tests in
21673 gdb.c++/namespace.exp. */
21674
e37fd15a 21675 if (!suppress_add)
34eaf542 21676 {
34eaf542 21677 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21678 && cu->language == language_cplus
34eaf542 21679 ? &global_symbols : cu->list_in_scope);
63d06c5c 21680
64382290 21681 /* The semantics of C++ state that "struct foo {
9c37b5ae 21682 ... }" also defines a typedef for "foo". */
64382290 21683 if (cu->language == language_cplus
45280282 21684 || cu->language == language_ada
c44af4eb
TT
21685 || cu->language == language_d
21686 || cu->language == language_rust)
64382290
TT
21687 {
21688 /* The symbol's name is already allocated along
21689 with this objfile, so we don't need to
21690 duplicate it for the type. */
21691 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21692 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21693 }
63d06c5c
DC
21694 }
21695 }
c906108c
SS
21696 break;
21697 case DW_TAG_typedef:
f1e6e072 21698 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21699 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21700 list_to_add = cu->list_in_scope;
63d06c5c 21701 break;
c906108c 21702 case DW_TAG_base_type:
a02abb62 21703 case DW_TAG_subrange_type:
f1e6e072 21704 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21705 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21706 list_to_add = cu->list_in_scope;
c906108c
SS
21707 break;
21708 case DW_TAG_enumerator:
e142c38c 21709 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21710 if (attr)
21711 {
e7c27a73 21712 dwarf2_const_value (attr, sym, cu);
c906108c 21713 }
63d06c5c
DC
21714 {
21715 /* NOTE: carlton/2003-11-10: See comment above in the
21716 DW_TAG_class_type, etc. block. */
21717
e142c38c 21718 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21719 && cu->language == language_cplus
e142c38c 21720 ? &global_symbols : cu->list_in_scope);
63d06c5c 21721 }
c906108c 21722 break;
74921315 21723 case DW_TAG_imported_declaration:
5c4e30ca 21724 case DW_TAG_namespace:
f1e6e072 21725 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 21726 list_to_add = &global_symbols;
5c4e30ca 21727 break;
530e8392
KB
21728 case DW_TAG_module:
21729 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21730 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21731 list_to_add = &global_symbols;
21732 break;
4357ac6c 21733 case DW_TAG_common_block:
f1e6e072 21734 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
21735 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21736 add_symbol_to_list (sym, cu->list_in_scope);
21737 break;
c906108c
SS
21738 default:
21739 /* Not a tag we recognize. Hopefully we aren't processing
21740 trash data, but since we must specifically ignore things
21741 we don't recognize, there is nothing else we should do at
0963b4bd 21742 this point. */
e2e0b3e5 21743 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 21744 dwarf_tag_name (die->tag));
c906108c
SS
21745 break;
21746 }
df8a16a1 21747
e37fd15a
SW
21748 if (suppress_add)
21749 {
21750 sym->hash_next = objfile->template_symbols;
21751 objfile->template_symbols = sym;
21752 list_to_add = NULL;
21753 }
21754
21755 if (list_to_add != NULL)
21756 add_symbol_to_list (sym, list_to_add);
21757
df8a16a1
DJ
21758 /* For the benefit of old versions of GCC, check for anonymous
21759 namespaces based on the demangled name. */
4d4ec4e5 21760 if (!cu->processing_has_namespace_info
94af9270 21761 && cu->language == language_cplus)
a10964d1 21762 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
21763 }
21764 return (sym);
21765}
21766
34eaf542
TT
21767/* A wrapper for new_symbol_full that always allocates a new symbol. */
21768
21769static struct symbol *
21770new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
21771{
21772 return new_symbol_full (die, type, cu, NULL);
21773}
21774
98bfdba5
PA
21775/* Given an attr with a DW_FORM_dataN value in host byte order,
21776 zero-extend it as appropriate for the symbol's type. The DWARF
21777 standard (v4) is not entirely clear about the meaning of using
21778 DW_FORM_dataN for a constant with a signed type, where the type is
21779 wider than the data. The conclusion of a discussion on the DWARF
21780 list was that this is unspecified. We choose to always zero-extend
21781 because that is the interpretation long in use by GCC. */
c906108c 21782
98bfdba5 21783static gdb_byte *
ff39bb5e 21784dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21785 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21786{
518817b3 21787 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21788 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21789 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21790 LONGEST l = DW_UNSND (attr);
21791
21792 if (bits < sizeof (*value) * 8)
21793 {
21794 l &= ((LONGEST) 1 << bits) - 1;
21795 *value = l;
21796 }
21797 else if (bits == sizeof (*value) * 8)
21798 *value = l;
21799 else
21800 {
224c3ddb 21801 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21802 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21803 return bytes;
21804 }
21805
21806 return NULL;
21807}
21808
21809/* Read a constant value from an attribute. Either set *VALUE, or if
21810 the value does not fit in *VALUE, set *BYTES - either already
21811 allocated on the objfile obstack, or newly allocated on OBSTACK,
21812 or, set *BATON, if we translated the constant to a location
21813 expression. */
21814
21815static void
ff39bb5e 21816dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21817 const char *name, struct obstack *obstack,
21818 struct dwarf2_cu *cu,
d521ce57 21819 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21820 struct dwarf2_locexpr_baton **baton)
21821{
518817b3 21822 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21823 struct comp_unit_head *cu_header = &cu->header;
c906108c 21824 struct dwarf_block *blk;
98bfdba5
PA
21825 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21826 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21827
21828 *value = 0;
21829 *bytes = NULL;
21830 *baton = NULL;
c906108c
SS
21831
21832 switch (attr->form)
21833 {
21834 case DW_FORM_addr:
3019eac3 21835 case DW_FORM_GNU_addr_index:
ac56253d 21836 {
ac56253d
TT
21837 gdb_byte *data;
21838
98bfdba5
PA
21839 if (TYPE_LENGTH (type) != cu_header->addr_size)
21840 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21841 cu_header->addr_size,
98bfdba5 21842 TYPE_LENGTH (type));
ac56253d
TT
21843 /* Symbols of this form are reasonably rare, so we just
21844 piggyback on the existing location code rather than writing
21845 a new implementation of symbol_computed_ops. */
8d749320 21846 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21847 (*baton)->per_cu = cu->per_cu;
21848 gdb_assert ((*baton)->per_cu);
ac56253d 21849
98bfdba5 21850 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21851 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21852 (*baton)->data = data;
ac56253d
TT
21853
21854 data[0] = DW_OP_addr;
21855 store_unsigned_integer (&data[1], cu_header->addr_size,
21856 byte_order, DW_ADDR (attr));
21857 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21858 }
c906108c 21859 break;
4ac36638 21860 case DW_FORM_string:
93b5768b 21861 case DW_FORM_strp:
3019eac3 21862 case DW_FORM_GNU_str_index:
36586728 21863 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21864 /* DW_STRING is already allocated on the objfile obstack, point
21865 directly to it. */
d521ce57 21866 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21867 break;
c906108c
SS
21868 case DW_FORM_block1:
21869 case DW_FORM_block2:
21870 case DW_FORM_block4:
21871 case DW_FORM_block:
2dc7f7b3 21872 case DW_FORM_exprloc:
0224619f 21873 case DW_FORM_data16:
c906108c 21874 blk = DW_BLOCK (attr);
98bfdba5
PA
21875 if (TYPE_LENGTH (type) != blk->size)
21876 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21877 TYPE_LENGTH (type));
21878 *bytes = blk->data;
c906108c 21879 break;
2df3850c
JM
21880
21881 /* The DW_AT_const_value attributes are supposed to carry the
21882 symbol's value "represented as it would be on the target
21883 architecture." By the time we get here, it's already been
21884 converted to host endianness, so we just need to sign- or
21885 zero-extend it as appropriate. */
21886 case DW_FORM_data1:
3aef2284 21887 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21888 break;
c906108c 21889 case DW_FORM_data2:
3aef2284 21890 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21891 break;
c906108c 21892 case DW_FORM_data4:
3aef2284 21893 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21894 break;
c906108c 21895 case DW_FORM_data8:
3aef2284 21896 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21897 break;
21898
c906108c 21899 case DW_FORM_sdata:
663c44ac 21900 case DW_FORM_implicit_const:
98bfdba5 21901 *value = DW_SND (attr);
2df3850c
JM
21902 break;
21903
c906108c 21904 case DW_FORM_udata:
98bfdba5 21905 *value = DW_UNSND (attr);
c906108c 21906 break;
2df3850c 21907
c906108c 21908 default:
4d3c2250 21909 complaint (&symfile_complaints,
e2e0b3e5 21910 _("unsupported const value attribute form: '%s'"),
4d3c2250 21911 dwarf_form_name (attr->form));
98bfdba5 21912 *value = 0;
c906108c
SS
21913 break;
21914 }
21915}
21916
2df3850c 21917
98bfdba5
PA
21918/* Copy constant value from an attribute to a symbol. */
21919
2df3850c 21920static void
ff39bb5e 21921dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21922 struct dwarf2_cu *cu)
2df3850c 21923{
518817b3 21924 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21925 LONGEST value;
d521ce57 21926 const gdb_byte *bytes;
98bfdba5 21927 struct dwarf2_locexpr_baton *baton;
2df3850c 21928
98bfdba5
PA
21929 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21930 SYMBOL_PRINT_NAME (sym),
21931 &objfile->objfile_obstack, cu,
21932 &value, &bytes, &baton);
2df3850c 21933
98bfdba5
PA
21934 if (baton != NULL)
21935 {
98bfdba5 21936 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21937 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21938 }
21939 else if (bytes != NULL)
21940 {
21941 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21942 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21943 }
21944 else
21945 {
21946 SYMBOL_VALUE (sym) = value;
f1e6e072 21947 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21948 }
2df3850c
JM
21949}
21950
c906108c
SS
21951/* Return the type of the die in question using its DW_AT_type attribute. */
21952
21953static struct type *
e7c27a73 21954die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21955{
c906108c 21956 struct attribute *type_attr;
c906108c 21957
e142c38c 21958 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21959 if (!type_attr)
21960 {
518817b3 21961 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21962 /* A missing DW_AT_type represents a void type. */
518817b3 21963 return objfile_type (objfile)->builtin_void;
c906108c 21964 }
348e048f 21965
673bfd45 21966 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21967}
21968
b4ba55a1
JB
21969/* True iff CU's producer generates GNAT Ada auxiliary information
21970 that allows to find parallel types through that information instead
21971 of having to do expensive parallel lookups by type name. */
21972
21973static int
21974need_gnat_info (struct dwarf2_cu *cu)
21975{
21976 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
21977 of GNAT produces this auxiliary information, without any indication
21978 that it is produced. Part of enhancing the FSF version of GNAT
21979 to produce that information will be to put in place an indicator
21980 that we can use in order to determine whether the descriptive type
21981 info is available or not. One suggestion that has been made is
21982 to use a new attribute, attached to the CU die. For now, assume
21983 that the descriptive type info is not available. */
21984 return 0;
21985}
21986
b4ba55a1
JB
21987/* Return the auxiliary type of the die in question using its
21988 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21989 attribute is not present. */
21990
21991static struct type *
21992die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21993{
b4ba55a1 21994 struct attribute *type_attr;
b4ba55a1
JB
21995
21996 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21997 if (!type_attr)
21998 return NULL;
21999
673bfd45 22000 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22001}
22002
22003/* If DIE has a descriptive_type attribute, then set the TYPE's
22004 descriptive type accordingly. */
22005
22006static void
22007set_descriptive_type (struct type *type, struct die_info *die,
22008 struct dwarf2_cu *cu)
22009{
22010 struct type *descriptive_type = die_descriptive_type (die, cu);
22011
22012 if (descriptive_type)
22013 {
22014 ALLOCATE_GNAT_AUX_TYPE (type);
22015 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22016 }
22017}
22018
c906108c
SS
22019/* Return the containing type of the die in question using its
22020 DW_AT_containing_type attribute. */
22021
22022static struct type *
e7c27a73 22023die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22024{
c906108c 22025 struct attribute *type_attr;
518817b3 22026 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22027
e142c38c 22028 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22029 if (!type_attr)
22030 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22031 "[in module %s]"), objfile_name (objfile));
33ac96f0 22032
673bfd45 22033 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22034}
22035
ac9ec31b
DE
22036/* Return an error marker type to use for the ill formed type in DIE/CU. */
22037
22038static struct type *
22039build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22040{
518817b3
SM
22041 struct dwarf2_per_objfile *dwarf2_per_objfile
22042 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
22043 struct objfile *objfile = dwarf2_per_objfile->objfile;
22044 char *message, *saved;
22045
22046 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 22047 objfile_name (objfile),
9c541725
PA
22048 to_underlying (cu->header.sect_off),
22049 to_underlying (die->sect_off));
224c3ddb
SM
22050 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
22051 message, strlen (message));
ac9ec31b
DE
22052 xfree (message);
22053
19f392bc 22054 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22055}
22056
673bfd45 22057/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22058 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22059 DW_AT_containing_type.
673bfd45
DE
22060 If there is no type substitute an error marker. */
22061
c906108c 22062static struct type *
ff39bb5e 22063lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22064 struct dwarf2_cu *cu)
c906108c 22065{
518817b3
SM
22066 struct dwarf2_per_objfile *dwarf2_per_objfile
22067 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22068 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22069 struct type *this_type;
22070
ac9ec31b
DE
22071 gdb_assert (attr->name == DW_AT_type
22072 || attr->name == DW_AT_GNAT_descriptive_type
22073 || attr->name == DW_AT_containing_type);
22074
673bfd45
DE
22075 /* First see if we have it cached. */
22076
36586728
TT
22077 if (attr->form == DW_FORM_GNU_ref_alt)
22078 {
22079 struct dwarf2_per_cu_data *per_cu;
9c541725 22080 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22081
ed2dc618
SM
22082 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22083 dwarf2_per_objfile);
9c541725 22084 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22085 }
7771576e 22086 else if (attr_form_is_ref (attr))
673bfd45 22087 {
9c541725 22088 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22089
9c541725 22090 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22091 }
55f1336d 22092 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22093 {
ac9ec31b 22094 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22095
ac9ec31b 22096 return get_signatured_type (die, signature, cu);
673bfd45
DE
22097 }
22098 else
22099 {
ac9ec31b
DE
22100 complaint (&symfile_complaints,
22101 _("Dwarf Error: Bad type attribute %s in DIE"
22102 " at 0x%x [in module %s]"),
9c541725 22103 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
4262abfb 22104 objfile_name (objfile));
ac9ec31b 22105 return build_error_marker_type (cu, die);
673bfd45
DE
22106 }
22107
22108 /* If not cached we need to read it in. */
22109
22110 if (this_type == NULL)
22111 {
ac9ec31b 22112 struct die_info *type_die = NULL;
673bfd45
DE
22113 struct dwarf2_cu *type_cu = cu;
22114
7771576e 22115 if (attr_form_is_ref (attr))
ac9ec31b
DE
22116 type_die = follow_die_ref (die, attr, &type_cu);
22117 if (type_die == NULL)
22118 return build_error_marker_type (cu, die);
22119 /* If we find the type now, it's probably because the type came
3019eac3
DE
22120 from an inter-CU reference and the type's CU got expanded before
22121 ours. */
ac9ec31b 22122 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22123 }
22124
22125 /* If we still don't have a type use an error marker. */
22126
22127 if (this_type == NULL)
ac9ec31b 22128 return build_error_marker_type (cu, die);
673bfd45 22129
f792889a 22130 return this_type;
c906108c
SS
22131}
22132
673bfd45
DE
22133/* Return the type in DIE, CU.
22134 Returns NULL for invalid types.
22135
02142a6c 22136 This first does a lookup in die_type_hash,
673bfd45
DE
22137 and only reads the die in if necessary.
22138
22139 NOTE: This can be called when reading in partial or full symbols. */
22140
f792889a 22141static struct type *
e7c27a73 22142read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22143{
f792889a
DJ
22144 struct type *this_type;
22145
22146 this_type = get_die_type (die, cu);
22147 if (this_type)
22148 return this_type;
22149
673bfd45
DE
22150 return read_type_die_1 (die, cu);
22151}
22152
22153/* Read the type in DIE, CU.
22154 Returns NULL for invalid types. */
22155
22156static struct type *
22157read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22158{
22159 struct type *this_type = NULL;
22160
c906108c
SS
22161 switch (die->tag)
22162 {
22163 case DW_TAG_class_type:
680b30c7 22164 case DW_TAG_interface_type:
c906108c
SS
22165 case DW_TAG_structure_type:
22166 case DW_TAG_union_type:
f792889a 22167 this_type = read_structure_type (die, cu);
c906108c
SS
22168 break;
22169 case DW_TAG_enumeration_type:
f792889a 22170 this_type = read_enumeration_type (die, cu);
c906108c
SS
22171 break;
22172 case DW_TAG_subprogram:
22173 case DW_TAG_subroutine_type:
edb3359d 22174 case DW_TAG_inlined_subroutine:
f792889a 22175 this_type = read_subroutine_type (die, cu);
c906108c
SS
22176 break;
22177 case DW_TAG_array_type:
f792889a 22178 this_type = read_array_type (die, cu);
c906108c 22179 break;
72019c9c 22180 case DW_TAG_set_type:
f792889a 22181 this_type = read_set_type (die, cu);
72019c9c 22182 break;
c906108c 22183 case DW_TAG_pointer_type:
f792889a 22184 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22185 break;
22186 case DW_TAG_ptr_to_member_type:
f792889a 22187 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22188 break;
22189 case DW_TAG_reference_type:
4297a3f0
AV
22190 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22191 break;
22192 case DW_TAG_rvalue_reference_type:
22193 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22194 break;
22195 case DW_TAG_const_type:
f792889a 22196 this_type = read_tag_const_type (die, cu);
c906108c
SS
22197 break;
22198 case DW_TAG_volatile_type:
f792889a 22199 this_type = read_tag_volatile_type (die, cu);
c906108c 22200 break;
06d66ee9
TT
22201 case DW_TAG_restrict_type:
22202 this_type = read_tag_restrict_type (die, cu);
22203 break;
c906108c 22204 case DW_TAG_string_type:
f792889a 22205 this_type = read_tag_string_type (die, cu);
c906108c
SS
22206 break;
22207 case DW_TAG_typedef:
f792889a 22208 this_type = read_typedef (die, cu);
c906108c 22209 break;
a02abb62 22210 case DW_TAG_subrange_type:
f792889a 22211 this_type = read_subrange_type (die, cu);
a02abb62 22212 break;
c906108c 22213 case DW_TAG_base_type:
f792889a 22214 this_type = read_base_type (die, cu);
c906108c 22215 break;
81a17f79 22216 case DW_TAG_unspecified_type:
f792889a 22217 this_type = read_unspecified_type (die, cu);
81a17f79 22218 break;
0114d602
DJ
22219 case DW_TAG_namespace:
22220 this_type = read_namespace_type (die, cu);
22221 break;
f55ee35c
JK
22222 case DW_TAG_module:
22223 this_type = read_module_type (die, cu);
22224 break;
a2c2acaf
MW
22225 case DW_TAG_atomic_type:
22226 this_type = read_tag_atomic_type (die, cu);
22227 break;
c906108c 22228 default:
3e43a32a
MS
22229 complaint (&symfile_complaints,
22230 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 22231 dwarf_tag_name (die->tag));
c906108c
SS
22232 break;
22233 }
63d06c5c 22234
f792889a 22235 return this_type;
63d06c5c
DC
22236}
22237
abc72ce4
DE
22238/* See if we can figure out if the class lives in a namespace. We do
22239 this by looking for a member function; its demangled name will
22240 contain namespace info, if there is any.
22241 Return the computed name or NULL.
22242 Space for the result is allocated on the objfile's obstack.
22243 This is the full-die version of guess_partial_die_structure_name.
22244 In this case we know DIE has no useful parent. */
22245
22246static char *
22247guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22248{
22249 struct die_info *spec_die;
22250 struct dwarf2_cu *spec_cu;
22251 struct die_info *child;
518817b3 22252 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22253
22254 spec_cu = cu;
22255 spec_die = die_specification (die, &spec_cu);
22256 if (spec_die != NULL)
22257 {
22258 die = spec_die;
22259 cu = spec_cu;
22260 }
22261
22262 for (child = die->child;
22263 child != NULL;
22264 child = child->sibling)
22265 {
22266 if (child->tag == DW_TAG_subprogram)
22267 {
73b9be8b 22268 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22269
7d45c7c3 22270 if (linkage_name != NULL)
abc72ce4
DE
22271 {
22272 char *actual_name
22273 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22274 linkage_name);
abc72ce4
DE
22275 char *name = NULL;
22276
22277 if (actual_name != NULL)
22278 {
15d034d0 22279 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22280
22281 if (die_name != NULL
22282 && strcmp (die_name, actual_name) != 0)
22283 {
22284 /* Strip off the class name from the full name.
22285 We want the prefix. */
22286 int die_name_len = strlen (die_name);
22287 int actual_name_len = strlen (actual_name);
22288
22289 /* Test for '::' as a sanity check. */
22290 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22291 && actual_name[actual_name_len
22292 - die_name_len - 1] == ':')
224c3ddb 22293 name = (char *) obstack_copy0 (
e3b94546 22294 &objfile->per_bfd->storage_obstack,
224c3ddb 22295 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22296 }
22297 }
22298 xfree (actual_name);
22299 return name;
22300 }
22301 }
22302 }
22303
22304 return NULL;
22305}
22306
96408a79
SA
22307/* GCC might emit a nameless typedef that has a linkage name. Determine the
22308 prefix part in such case. See
22309 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22310
a121b7c1 22311static const char *
96408a79
SA
22312anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22313{
22314 struct attribute *attr;
e6a959d6 22315 const char *base;
96408a79
SA
22316
22317 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22318 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22319 return NULL;
22320
7d45c7c3 22321 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22322 return NULL;
22323
73b9be8b 22324 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22325 if (attr == NULL || DW_STRING (attr) == NULL)
22326 return NULL;
22327
22328 /* dwarf2_name had to be already called. */
22329 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22330
22331 /* Strip the base name, keep any leading namespaces/classes. */
22332 base = strrchr (DW_STRING (attr), ':');
22333 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22334 return "";
22335
518817b3 22336 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e3b94546 22337 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
22338 DW_STRING (attr),
22339 &base[-1] - DW_STRING (attr));
96408a79
SA
22340}
22341
fdde2d81 22342/* Return the name of the namespace/class that DIE is defined within,
0114d602 22343 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22344
0114d602
DJ
22345 For example, if we're within the method foo() in the following
22346 code:
22347
22348 namespace N {
22349 class C {
22350 void foo () {
22351 }
22352 };
22353 }
22354
22355 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22356
0d5cff50 22357static const char *
e142c38c 22358determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22359{
518817b3
SM
22360 struct dwarf2_per_objfile *dwarf2_per_objfile
22361 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22362 struct die_info *parent, *spec_die;
22363 struct dwarf2_cu *spec_cu;
22364 struct type *parent_type;
a121b7c1 22365 const char *retval;
63d06c5c 22366
9c37b5ae 22367 if (cu->language != language_cplus
c44af4eb
TT
22368 && cu->language != language_fortran && cu->language != language_d
22369 && cu->language != language_rust)
0114d602
DJ
22370 return "";
22371
96408a79
SA
22372 retval = anonymous_struct_prefix (die, cu);
22373 if (retval)
22374 return retval;
22375
0114d602
DJ
22376 /* We have to be careful in the presence of DW_AT_specification.
22377 For example, with GCC 3.4, given the code
22378
22379 namespace N {
22380 void foo() {
22381 // Definition of N::foo.
22382 }
22383 }
22384
22385 then we'll have a tree of DIEs like this:
22386
22387 1: DW_TAG_compile_unit
22388 2: DW_TAG_namespace // N
22389 3: DW_TAG_subprogram // declaration of N::foo
22390 4: DW_TAG_subprogram // definition of N::foo
22391 DW_AT_specification // refers to die #3
22392
22393 Thus, when processing die #4, we have to pretend that we're in
22394 the context of its DW_AT_specification, namely the contex of die
22395 #3. */
22396 spec_cu = cu;
22397 spec_die = die_specification (die, &spec_cu);
22398 if (spec_die == NULL)
22399 parent = die->parent;
22400 else
63d06c5c 22401 {
0114d602
DJ
22402 parent = spec_die->parent;
22403 cu = spec_cu;
63d06c5c 22404 }
0114d602
DJ
22405
22406 if (parent == NULL)
22407 return "";
98bfdba5
PA
22408 else if (parent->building_fullname)
22409 {
22410 const char *name;
22411 const char *parent_name;
22412
22413 /* It has been seen on RealView 2.2 built binaries,
22414 DW_TAG_template_type_param types actually _defined_ as
22415 children of the parent class:
22416
22417 enum E {};
22418 template class <class Enum> Class{};
22419 Class<enum E> class_e;
22420
22421 1: DW_TAG_class_type (Class)
22422 2: DW_TAG_enumeration_type (E)
22423 3: DW_TAG_enumerator (enum1:0)
22424 3: DW_TAG_enumerator (enum2:1)
22425 ...
22426 2: DW_TAG_template_type_param
22427 DW_AT_type DW_FORM_ref_udata (E)
22428
22429 Besides being broken debug info, it can put GDB into an
22430 infinite loop. Consider:
22431
22432 When we're building the full name for Class<E>, we'll start
22433 at Class, and go look over its template type parameters,
22434 finding E. We'll then try to build the full name of E, and
22435 reach here. We're now trying to build the full name of E,
22436 and look over the parent DIE for containing scope. In the
22437 broken case, if we followed the parent DIE of E, we'd again
22438 find Class, and once again go look at its template type
22439 arguments, etc., etc. Simply don't consider such parent die
22440 as source-level parent of this die (it can't be, the language
22441 doesn't allow it), and break the loop here. */
22442 name = dwarf2_name (die, cu);
22443 parent_name = dwarf2_name (parent, cu);
22444 complaint (&symfile_complaints,
22445 _("template param type '%s' defined within parent '%s'"),
22446 name ? name : "<unknown>",
22447 parent_name ? parent_name : "<unknown>");
22448 return "";
22449 }
63d06c5c 22450 else
0114d602
DJ
22451 switch (parent->tag)
22452 {
63d06c5c 22453 case DW_TAG_namespace:
0114d602 22454 parent_type = read_type_die (parent, cu);
acebe513
UW
22455 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22456 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22457 Work around this problem here. */
22458 if (cu->language == language_cplus
22459 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22460 return "";
0114d602
DJ
22461 /* We give a name to even anonymous namespaces. */
22462 return TYPE_TAG_NAME (parent_type);
63d06c5c 22463 case DW_TAG_class_type:
680b30c7 22464 case DW_TAG_interface_type:
63d06c5c 22465 case DW_TAG_structure_type:
0114d602 22466 case DW_TAG_union_type:
f55ee35c 22467 case DW_TAG_module:
0114d602
DJ
22468 parent_type = read_type_die (parent, cu);
22469 if (TYPE_TAG_NAME (parent_type) != NULL)
22470 return TYPE_TAG_NAME (parent_type);
22471 else
22472 /* An anonymous structure is only allowed non-static data
22473 members; no typedefs, no member functions, et cetera.
22474 So it does not need a prefix. */
22475 return "";
abc72ce4 22476 case DW_TAG_compile_unit:
95554aad 22477 case DW_TAG_partial_unit:
abc72ce4
DE
22478 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22479 if (cu->language == language_cplus
8b70b953 22480 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
22481 && die->child != NULL
22482 && (die->tag == DW_TAG_class_type
22483 || die->tag == DW_TAG_structure_type
22484 || die->tag == DW_TAG_union_type))
22485 {
22486 char *name = guess_full_die_structure_name (die, cu);
22487 if (name != NULL)
22488 return name;
22489 }
22490 return "";
3d567982
TT
22491 case DW_TAG_enumeration_type:
22492 parent_type = read_type_die (parent, cu);
22493 if (TYPE_DECLARED_CLASS (parent_type))
22494 {
22495 if (TYPE_TAG_NAME (parent_type) != NULL)
22496 return TYPE_TAG_NAME (parent_type);
22497 return "";
22498 }
22499 /* Fall through. */
63d06c5c 22500 default:
8176b9b8 22501 return determine_prefix (parent, cu);
63d06c5c 22502 }
63d06c5c
DC
22503}
22504
3e43a32a
MS
22505/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22506 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22507 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22508 an obconcat, otherwise allocate storage for the result. The CU argument is
22509 used to determine the language and hence, the appropriate separator. */
987504bb 22510
f55ee35c 22511#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22512
22513static char *
f55ee35c
JK
22514typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22515 int physname, struct dwarf2_cu *cu)
63d06c5c 22516{
f55ee35c 22517 const char *lead = "";
5c315b68 22518 const char *sep;
63d06c5c 22519
3e43a32a
MS
22520 if (suffix == NULL || suffix[0] == '\0'
22521 || prefix == NULL || prefix[0] == '\0')
987504bb 22522 sep = "";
45280282
IB
22523 else if (cu->language == language_d)
22524 {
22525 /* For D, the 'main' function could be defined in any module, but it
22526 should never be prefixed. */
22527 if (strcmp (suffix, "D main") == 0)
22528 {
22529 prefix = "";
22530 sep = "";
22531 }
22532 else
22533 sep = ".";
22534 }
f55ee35c
JK
22535 else if (cu->language == language_fortran && physname)
22536 {
22537 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22538 DW_AT_MIPS_linkage_name is preferred and used instead. */
22539
22540 lead = "__";
22541 sep = "_MOD_";
22542 }
987504bb
JJ
22543 else
22544 sep = "::";
63d06c5c 22545
6dd47d34
DE
22546 if (prefix == NULL)
22547 prefix = "";
22548 if (suffix == NULL)
22549 suffix = "";
22550
987504bb
JJ
22551 if (obs == NULL)
22552 {
3e43a32a 22553 char *retval
224c3ddb
SM
22554 = ((char *)
22555 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22556
f55ee35c
JK
22557 strcpy (retval, lead);
22558 strcat (retval, prefix);
6dd47d34
DE
22559 strcat (retval, sep);
22560 strcat (retval, suffix);
63d06c5c
DC
22561 return retval;
22562 }
987504bb
JJ
22563 else
22564 {
22565 /* We have an obstack. */
f55ee35c 22566 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22567 }
63d06c5c
DC
22568}
22569
c906108c
SS
22570/* Return sibling of die, NULL if no sibling. */
22571
f9aca02d 22572static struct die_info *
fba45db2 22573sibling_die (struct die_info *die)
c906108c 22574{
639d11d3 22575 return die->sibling;
c906108c
SS
22576}
22577
71c25dea
TT
22578/* Get name of a die, return NULL if not found. */
22579
15d034d0
TT
22580static const char *
22581dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22582 struct obstack *obstack)
22583{
22584 if (name && cu->language == language_cplus)
22585 {
2f408ecb 22586 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22587
2f408ecb 22588 if (!canon_name.empty ())
71c25dea 22589 {
2f408ecb
PA
22590 if (canon_name != name)
22591 name = (const char *) obstack_copy0 (obstack,
22592 canon_name.c_str (),
22593 canon_name.length ());
71c25dea
TT
22594 }
22595 }
22596
22597 return name;
c906108c
SS
22598}
22599
96553a0c
DE
22600/* Get name of a die, return NULL if not found.
22601 Anonymous namespaces are converted to their magic string. */
9219021c 22602
15d034d0 22603static const char *
e142c38c 22604dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22605{
22606 struct attribute *attr;
518817b3 22607 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22608
e142c38c 22609 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22610 if ((!attr || !DW_STRING (attr))
96553a0c 22611 && die->tag != DW_TAG_namespace
53832f31
TT
22612 && die->tag != DW_TAG_class_type
22613 && die->tag != DW_TAG_interface_type
22614 && die->tag != DW_TAG_structure_type
22615 && die->tag != DW_TAG_union_type)
71c25dea
TT
22616 return NULL;
22617
22618 switch (die->tag)
22619 {
22620 case DW_TAG_compile_unit:
95554aad 22621 case DW_TAG_partial_unit:
71c25dea
TT
22622 /* Compilation units have a DW_AT_name that is a filename, not
22623 a source language identifier. */
22624 case DW_TAG_enumeration_type:
22625 case DW_TAG_enumerator:
22626 /* These tags always have simple identifiers already; no need
22627 to canonicalize them. */
22628 return DW_STRING (attr);
907af001 22629
96553a0c
DE
22630 case DW_TAG_namespace:
22631 if (attr != NULL && DW_STRING (attr) != NULL)
22632 return DW_STRING (attr);
22633 return CP_ANONYMOUS_NAMESPACE_STR;
22634
907af001
UW
22635 case DW_TAG_class_type:
22636 case DW_TAG_interface_type:
22637 case DW_TAG_structure_type:
22638 case DW_TAG_union_type:
22639 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22640 structures or unions. These were of the form "._%d" in GCC 4.1,
22641 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22642 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22643 if (attr && DW_STRING (attr)
61012eef
GB
22644 && (startswith (DW_STRING (attr), "._")
22645 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22646 return NULL;
53832f31
TT
22647
22648 /* GCC might emit a nameless typedef that has a linkage name. See
22649 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22650 if (!attr || DW_STRING (attr) == NULL)
22651 {
df5c6c50 22652 char *demangled = NULL;
53832f31 22653
73b9be8b 22654 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22655 if (attr == NULL || DW_STRING (attr) == NULL)
22656 return NULL;
22657
df5c6c50
JK
22658 /* Avoid demangling DW_STRING (attr) the second time on a second
22659 call for the same DIE. */
22660 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22661 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22662
22663 if (demangled)
22664 {
e6a959d6 22665 const char *base;
96408a79 22666
53832f31 22667 /* FIXME: we already did this for the partial symbol... */
34a68019 22668 DW_STRING (attr)
224c3ddb 22669 = ((const char *)
e3b94546 22670 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 22671 demangled, strlen (demangled)));
53832f31
TT
22672 DW_STRING_IS_CANONICAL (attr) = 1;
22673 xfree (demangled);
96408a79
SA
22674
22675 /* Strip any leading namespaces/classes, keep only the base name.
22676 DW_AT_name for named DIEs does not contain the prefixes. */
22677 base = strrchr (DW_STRING (attr), ':');
22678 if (base && base > DW_STRING (attr) && base[-1] == ':')
22679 return &base[1];
22680 else
22681 return DW_STRING (attr);
53832f31
TT
22682 }
22683 }
907af001
UW
22684 break;
22685
71c25dea 22686 default:
907af001
UW
22687 break;
22688 }
22689
22690 if (!DW_STRING_IS_CANONICAL (attr))
22691 {
22692 DW_STRING (attr)
22693 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22694 &objfile->per_bfd->storage_obstack);
907af001 22695 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22696 }
907af001 22697 return DW_STRING (attr);
9219021c
DC
22698}
22699
22700/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22701 is none. *EXT_CU is the CU containing DIE on input, and the CU
22702 containing the return value on output. */
9219021c
DC
22703
22704static struct die_info *
f2f0e013 22705dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22706{
22707 struct attribute *attr;
9219021c 22708
f2f0e013 22709 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22710 if (attr == NULL)
22711 return NULL;
22712
f2f0e013 22713 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22714}
22715
c906108c
SS
22716/* Convert a DIE tag into its string name. */
22717
f39c6ffd 22718static const char *
aa1ee363 22719dwarf_tag_name (unsigned tag)
c906108c 22720{
f39c6ffd
TT
22721 const char *name = get_DW_TAG_name (tag);
22722
22723 if (name == NULL)
22724 return "DW_TAG_<unknown>";
22725
22726 return name;
c906108c
SS
22727}
22728
22729/* Convert a DWARF attribute code into its string name. */
22730
f39c6ffd 22731static const char *
aa1ee363 22732dwarf_attr_name (unsigned attr)
c906108c 22733{
f39c6ffd
TT
22734 const char *name;
22735
c764a876 22736#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22737 if (attr == DW_AT_MIPS_fde)
22738 return "DW_AT_MIPS_fde";
22739#else
22740 if (attr == DW_AT_HP_block_index)
22741 return "DW_AT_HP_block_index";
c764a876 22742#endif
f39c6ffd
TT
22743
22744 name = get_DW_AT_name (attr);
22745
22746 if (name == NULL)
22747 return "DW_AT_<unknown>";
22748
22749 return name;
c906108c
SS
22750}
22751
22752/* Convert a DWARF value form code into its string name. */
22753
f39c6ffd 22754static const char *
aa1ee363 22755dwarf_form_name (unsigned form)
c906108c 22756{
f39c6ffd
TT
22757 const char *name = get_DW_FORM_name (form);
22758
22759 if (name == NULL)
22760 return "DW_FORM_<unknown>";
22761
22762 return name;
c906108c
SS
22763}
22764
a121b7c1 22765static const char *
fba45db2 22766dwarf_bool_name (unsigned mybool)
c906108c
SS
22767{
22768 if (mybool)
22769 return "TRUE";
22770 else
22771 return "FALSE";
22772}
22773
22774/* Convert a DWARF type code into its string name. */
22775
f39c6ffd 22776static const char *
aa1ee363 22777dwarf_type_encoding_name (unsigned enc)
c906108c 22778{
f39c6ffd 22779 const char *name = get_DW_ATE_name (enc);
c906108c 22780
f39c6ffd
TT
22781 if (name == NULL)
22782 return "DW_ATE_<unknown>";
c906108c 22783
f39c6ffd 22784 return name;
c906108c 22785}
c906108c 22786
f9aca02d 22787static void
d97bc12b 22788dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22789{
22790 unsigned int i;
22791
d97bc12b
DE
22792 print_spaces (indent, f);
22793 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
9c541725
PA
22794 dwarf_tag_name (die->tag), die->abbrev,
22795 to_underlying (die->sect_off));
d97bc12b
DE
22796
22797 if (die->parent != NULL)
22798 {
22799 print_spaces (indent, f);
22800 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9c541725 22801 to_underlying (die->parent->sect_off));
d97bc12b
DE
22802 }
22803
22804 print_spaces (indent, f);
22805 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22806 dwarf_bool_name (die->child != NULL));
c906108c 22807
d97bc12b
DE
22808 print_spaces (indent, f);
22809 fprintf_unfiltered (f, " attributes:\n");
22810
c906108c
SS
22811 for (i = 0; i < die->num_attrs; ++i)
22812 {
d97bc12b
DE
22813 print_spaces (indent, f);
22814 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22815 dwarf_attr_name (die->attrs[i].name),
22816 dwarf_form_name (die->attrs[i].form));
d97bc12b 22817
c906108c
SS
22818 switch (die->attrs[i].form)
22819 {
c906108c 22820 case DW_FORM_addr:
3019eac3 22821 case DW_FORM_GNU_addr_index:
d97bc12b 22822 fprintf_unfiltered (f, "address: ");
5af949e3 22823 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22824 break;
22825 case DW_FORM_block2:
22826 case DW_FORM_block4:
22827 case DW_FORM_block:
22828 case DW_FORM_block1:
56eb65bd
SP
22829 fprintf_unfiltered (f, "block: size %s",
22830 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22831 break;
2dc7f7b3 22832 case DW_FORM_exprloc:
56eb65bd
SP
22833 fprintf_unfiltered (f, "expression: size %s",
22834 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22835 break;
0224619f
JK
22836 case DW_FORM_data16:
22837 fprintf_unfiltered (f, "constant of 16 bytes");
22838 break;
4568ecf9
DE
22839 case DW_FORM_ref_addr:
22840 fprintf_unfiltered (f, "ref address: ");
22841 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22842 break;
36586728
TT
22843 case DW_FORM_GNU_ref_alt:
22844 fprintf_unfiltered (f, "alt ref address: ");
22845 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22846 break;
10b3939b
DJ
22847 case DW_FORM_ref1:
22848 case DW_FORM_ref2:
22849 case DW_FORM_ref4:
4568ecf9
DE
22850 case DW_FORM_ref8:
22851 case DW_FORM_ref_udata:
d97bc12b 22852 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22853 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22854 break;
c906108c
SS
22855 case DW_FORM_data1:
22856 case DW_FORM_data2:
22857 case DW_FORM_data4:
ce5d95e1 22858 case DW_FORM_data8:
c906108c
SS
22859 case DW_FORM_udata:
22860 case DW_FORM_sdata:
43bbcdc2
PH
22861 fprintf_unfiltered (f, "constant: %s",
22862 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22863 break;
2dc7f7b3
TT
22864 case DW_FORM_sec_offset:
22865 fprintf_unfiltered (f, "section offset: %s",
22866 pulongest (DW_UNSND (&die->attrs[i])));
22867 break;
55f1336d 22868 case DW_FORM_ref_sig8:
ac9ec31b
DE
22869 fprintf_unfiltered (f, "signature: %s",
22870 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22871 break;
c906108c 22872 case DW_FORM_string:
4bdf3d34 22873 case DW_FORM_strp:
43988095 22874 case DW_FORM_line_strp:
3019eac3 22875 case DW_FORM_GNU_str_index:
36586728 22876 case DW_FORM_GNU_strp_alt:
8285870a 22877 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22878 DW_STRING (&die->attrs[i])
8285870a
JK
22879 ? DW_STRING (&die->attrs[i]) : "",
22880 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22881 break;
22882 case DW_FORM_flag:
22883 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22884 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22885 else
d97bc12b 22886 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22887 break;
2dc7f7b3
TT
22888 case DW_FORM_flag_present:
22889 fprintf_unfiltered (f, "flag: TRUE");
22890 break;
a8329558 22891 case DW_FORM_indirect:
0963b4bd
MS
22892 /* The reader will have reduced the indirect form to
22893 the "base form" so this form should not occur. */
3e43a32a
MS
22894 fprintf_unfiltered (f,
22895 "unexpected attribute form: DW_FORM_indirect");
a8329558 22896 break;
663c44ac
JK
22897 case DW_FORM_implicit_const:
22898 fprintf_unfiltered (f, "constant: %s",
22899 plongest (DW_SND (&die->attrs[i])));
22900 break;
c906108c 22901 default:
d97bc12b 22902 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22903 die->attrs[i].form);
d97bc12b 22904 break;
c906108c 22905 }
d97bc12b 22906 fprintf_unfiltered (f, "\n");
c906108c
SS
22907 }
22908}
22909
f9aca02d 22910static void
d97bc12b 22911dump_die_for_error (struct die_info *die)
c906108c 22912{
d97bc12b
DE
22913 dump_die_shallow (gdb_stderr, 0, die);
22914}
22915
22916static void
22917dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22918{
22919 int indent = level * 4;
22920
22921 gdb_assert (die != NULL);
22922
22923 if (level >= max_level)
22924 return;
22925
22926 dump_die_shallow (f, indent, die);
22927
22928 if (die->child != NULL)
c906108c 22929 {
d97bc12b
DE
22930 print_spaces (indent, f);
22931 fprintf_unfiltered (f, " Children:");
22932 if (level + 1 < max_level)
22933 {
22934 fprintf_unfiltered (f, "\n");
22935 dump_die_1 (f, level + 1, max_level, die->child);
22936 }
22937 else
22938 {
3e43a32a
MS
22939 fprintf_unfiltered (f,
22940 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22941 }
22942 }
22943
22944 if (die->sibling != NULL && level > 0)
22945 {
22946 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22947 }
22948}
22949
d97bc12b
DE
22950/* This is called from the pdie macro in gdbinit.in.
22951 It's not static so gcc will keep a copy callable from gdb. */
22952
22953void
22954dump_die (struct die_info *die, int max_level)
22955{
22956 dump_die_1 (gdb_stdlog, 0, max_level, die);
22957}
22958
f9aca02d 22959static void
51545339 22960store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22961{
51545339 22962 void **slot;
c906108c 22963
9c541725
PA
22964 slot = htab_find_slot_with_hash (cu->die_hash, die,
22965 to_underlying (die->sect_off),
b64f50a1 22966 INSERT);
51545339
DJ
22967
22968 *slot = die;
c906108c
SS
22969}
22970
b64f50a1
JK
22971/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22972 required kind. */
22973
22974static sect_offset
ff39bb5e 22975dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22976{
7771576e 22977 if (attr_form_is_ref (attr))
9c541725 22978 return (sect_offset) DW_UNSND (attr);
93311388
DE
22979
22980 complaint (&symfile_complaints,
22981 _("unsupported die ref attribute form: '%s'"),
22982 dwarf_form_name (attr->form));
9c541725 22983 return {};
c906108c
SS
22984}
22985
43bbcdc2
PH
22986/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22987 * the value held by the attribute is not constant. */
a02abb62 22988
43bbcdc2 22989static LONGEST
ff39bb5e 22990dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22991{
663c44ac 22992 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22993 return DW_SND (attr);
22994 else if (attr->form == DW_FORM_udata
22995 || attr->form == DW_FORM_data1
22996 || attr->form == DW_FORM_data2
22997 || attr->form == DW_FORM_data4
22998 || attr->form == DW_FORM_data8)
22999 return DW_UNSND (attr);
23000 else
23001 {
0224619f 23002 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
23003 complaint (&symfile_complaints,
23004 _("Attribute value is not a constant (%s)"),
a02abb62
JB
23005 dwarf_form_name (attr->form));
23006 return default_value;
23007 }
23008}
23009
348e048f
DE
23010/* Follow reference or signature attribute ATTR of SRC_DIE.
23011 On entry *REF_CU is the CU of SRC_DIE.
23012 On exit *REF_CU is the CU of the result. */
23013
23014static struct die_info *
ff39bb5e 23015follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23016 struct dwarf2_cu **ref_cu)
23017{
23018 struct die_info *die;
23019
7771576e 23020 if (attr_form_is_ref (attr))
348e048f 23021 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23022 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23023 die = follow_die_sig (src_die, attr, ref_cu);
23024 else
23025 {
23026 dump_die_for_error (src_die);
23027 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23028 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23029 }
23030
23031 return die;
03dd20cc
DJ
23032}
23033
5c631832 23034/* Follow reference OFFSET.
673bfd45
DE
23035 On entry *REF_CU is the CU of the source die referencing OFFSET.
23036 On exit *REF_CU is the CU of the result.
23037 Returns NULL if OFFSET is invalid. */
f504f079 23038
f9aca02d 23039static struct die_info *
9c541725 23040follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23041 struct dwarf2_cu **ref_cu)
c906108c 23042{
10b3939b 23043 struct die_info temp_die;
f2f0e013 23044 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23045 struct dwarf2_per_objfile *dwarf2_per_objfile
23046 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23047 struct objfile *objfile = dwarf2_per_objfile->objfile;
10b3939b 23048
348e048f
DE
23049 gdb_assert (cu->per_cu != NULL);
23050
98bfdba5
PA
23051 target_cu = cu;
23052
3019eac3 23053 if (cu->per_cu->is_debug_types)
348e048f
DE
23054 {
23055 /* .debug_types CUs cannot reference anything outside their CU.
23056 If they need to, they have to reference a signatured type via
55f1336d 23057 DW_FORM_ref_sig8. */
9c541725 23058 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23059 return NULL;
348e048f 23060 }
36586728 23061 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23062 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23063 {
23064 struct dwarf2_per_cu_data *per_cu;
9a619af0 23065
9c541725 23066 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23067 dwarf2_per_objfile);
03dd20cc
DJ
23068
23069 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
23070 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23071 load_full_comp_unit (per_cu, cu->language);
03dd20cc 23072
10b3939b
DJ
23073 target_cu = per_cu->cu;
23074 }
98bfdba5
PA
23075 else if (cu->dies == NULL)
23076 {
23077 /* We're loading full DIEs during partial symbol reading. */
23078 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 23079 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 23080 }
c906108c 23081
f2f0e013 23082 *ref_cu = target_cu;
9c541725 23083 temp_die.sect_off = sect_off;
9a3c8263 23084 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23085 &temp_die,
23086 to_underlying (sect_off));
5c631832 23087}
10b3939b 23088
5c631832
JK
23089/* Follow reference attribute ATTR of SRC_DIE.
23090 On entry *REF_CU is the CU of SRC_DIE.
23091 On exit *REF_CU is the CU of the result. */
23092
23093static struct die_info *
ff39bb5e 23094follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23095 struct dwarf2_cu **ref_cu)
23096{
9c541725 23097 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23098 struct dwarf2_cu *cu = *ref_cu;
23099 struct die_info *die;
23100
9c541725 23101 die = follow_die_offset (sect_off,
36586728
TT
23102 (attr->form == DW_FORM_GNU_ref_alt
23103 || cu->per_cu->is_dwz),
23104 ref_cu);
5c631832
JK
23105 if (!die)
23106 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
23107 "at 0x%x [in module %s]"),
9c541725 23108 to_underlying (sect_off), to_underlying (src_die->sect_off),
518817b3 23109 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23110
5c631832
JK
23111 return die;
23112}
23113
9c541725 23114/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23115 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23116 dwarf2_locexpr_baton->data has lifetime of
23117 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23118
23119struct dwarf2_locexpr_baton
9c541725 23120dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23121 struct dwarf2_per_cu_data *per_cu,
23122 CORE_ADDR (*get_frame_pc) (void *baton),
23123 void *baton)
5c631832 23124{
918dd910 23125 struct dwarf2_cu *cu;
5c631832
JK
23126 struct die_info *die;
23127 struct attribute *attr;
23128 struct dwarf2_locexpr_baton retval;
e3b94546 23129 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ed2dc618
SM
23130 struct dwarf2_per_objfile *dwarf2_per_objfile
23131 = get_dwarf2_per_objfile (objfile);
8cf6f0b1 23132
918dd910
JK
23133 if (per_cu->cu == NULL)
23134 load_cu (per_cu);
23135 cu = per_cu->cu;
cc12ce38
DE
23136 if (cu == NULL)
23137 {
23138 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23139 Instead just throw an error, not much else we can do. */
23140 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
e3b94546 23141 to_underlying (sect_off), objfile_name (objfile));
cc12ce38 23142 }
918dd910 23143
9c541725 23144 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832
JK
23145 if (!die)
23146 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
e3b94546 23147 to_underlying (sect_off), objfile_name (objfile));
5c631832
JK
23148
23149 attr = dwarf2_attr (die, DW_AT_location, cu);
23150 if (!attr)
23151 {
e103e986
JK
23152 /* DWARF: "If there is no such attribute, then there is no effect.".
23153 DATA is ignored if SIZE is 0. */
5c631832 23154
e103e986 23155 retval.data = NULL;
5c631832
JK
23156 retval.size = 0;
23157 }
8cf6f0b1
TT
23158 else if (attr_form_is_section_offset (attr))
23159 {
23160 struct dwarf2_loclist_baton loclist_baton;
23161 CORE_ADDR pc = (*get_frame_pc) (baton);
23162 size_t size;
23163
23164 fill_in_loclist_baton (cu, &loclist_baton, attr);
23165
23166 retval.data = dwarf2_find_location_expression (&loclist_baton,
23167 &size, pc);
23168 retval.size = size;
23169 }
5c631832
JK
23170 else
23171 {
23172 if (!attr_form_is_block (attr))
23173 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
23174 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
e3b94546 23175 to_underlying (sect_off), objfile_name (objfile));
5c631832
JK
23176
23177 retval.data = DW_BLOCK (attr)->data;
23178 retval.size = DW_BLOCK (attr)->size;
23179 }
23180 retval.per_cu = cu->per_cu;
918dd910 23181
ed2dc618 23182 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23183
5c631832 23184 return retval;
348e048f
DE
23185}
23186
8b9737bf
TT
23187/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23188 offset. */
23189
23190struct dwarf2_locexpr_baton
23191dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23192 struct dwarf2_per_cu_data *per_cu,
23193 CORE_ADDR (*get_frame_pc) (void *baton),
23194 void *baton)
23195{
9c541725 23196 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23197
9c541725 23198 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23199}
23200
b6807d98
TT
23201/* Write a constant of a given type as target-ordered bytes into
23202 OBSTACK. */
23203
23204static const gdb_byte *
23205write_constant_as_bytes (struct obstack *obstack,
23206 enum bfd_endian byte_order,
23207 struct type *type,
23208 ULONGEST value,
23209 LONGEST *len)
23210{
23211 gdb_byte *result;
23212
23213 *len = TYPE_LENGTH (type);
224c3ddb 23214 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23215 store_unsigned_integer (result, *len, byte_order, value);
23216
23217 return result;
23218}
23219
23220/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23221 pointer to the constant bytes and set LEN to the length of the
23222 data. If memory is needed, allocate it on OBSTACK. If the DIE
23223 does not have a DW_AT_const_value, return NULL. */
23224
23225const gdb_byte *
9c541725 23226dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23227 struct dwarf2_per_cu_data *per_cu,
23228 struct obstack *obstack,
23229 LONGEST *len)
23230{
23231 struct dwarf2_cu *cu;
23232 struct die_info *die;
23233 struct attribute *attr;
23234 const gdb_byte *result = NULL;
23235 struct type *type;
23236 LONGEST value;
23237 enum bfd_endian byte_order;
e3b94546 23238 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23239
b6807d98
TT
23240 if (per_cu->cu == NULL)
23241 load_cu (per_cu);
23242 cu = per_cu->cu;
cc12ce38
DE
23243 if (cu == NULL)
23244 {
23245 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23246 Instead just throw an error, not much else we can do. */
23247 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
e3b94546 23248 to_underlying (sect_off), objfile_name (objfile));
cc12ce38 23249 }
b6807d98 23250
9c541725 23251 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98
TT
23252 if (!die)
23253 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
e3b94546 23254 to_underlying (sect_off), objfile_name (objfile));
b6807d98
TT
23255
23256
23257 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23258 if (attr == NULL)
23259 return NULL;
23260
e3b94546 23261 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23262 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23263
23264 switch (attr->form)
23265 {
23266 case DW_FORM_addr:
23267 case DW_FORM_GNU_addr_index:
23268 {
23269 gdb_byte *tem;
23270
23271 *len = cu->header.addr_size;
224c3ddb 23272 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23273 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23274 result = tem;
23275 }
23276 break;
23277 case DW_FORM_string:
23278 case DW_FORM_strp:
23279 case DW_FORM_GNU_str_index:
23280 case DW_FORM_GNU_strp_alt:
23281 /* DW_STRING is already allocated on the objfile obstack, point
23282 directly to it. */
23283 result = (const gdb_byte *) DW_STRING (attr);
23284 *len = strlen (DW_STRING (attr));
23285 break;
23286 case DW_FORM_block1:
23287 case DW_FORM_block2:
23288 case DW_FORM_block4:
23289 case DW_FORM_block:
23290 case DW_FORM_exprloc:
0224619f 23291 case DW_FORM_data16:
b6807d98
TT
23292 result = DW_BLOCK (attr)->data;
23293 *len = DW_BLOCK (attr)->size;
23294 break;
23295
23296 /* The DW_AT_const_value attributes are supposed to carry the
23297 symbol's value "represented as it would be on the target
23298 architecture." By the time we get here, it's already been
23299 converted to host endianness, so we just need to sign- or
23300 zero-extend it as appropriate. */
23301 case DW_FORM_data1:
23302 type = die_type (die, cu);
23303 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23304 if (result == NULL)
23305 result = write_constant_as_bytes (obstack, byte_order,
23306 type, value, len);
23307 break;
23308 case DW_FORM_data2:
23309 type = die_type (die, cu);
23310 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23311 if (result == NULL)
23312 result = write_constant_as_bytes (obstack, byte_order,
23313 type, value, len);
23314 break;
23315 case DW_FORM_data4:
23316 type = die_type (die, cu);
23317 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23318 if (result == NULL)
23319 result = write_constant_as_bytes (obstack, byte_order,
23320 type, value, len);
23321 break;
23322 case DW_FORM_data8:
23323 type = die_type (die, cu);
23324 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23325 if (result == NULL)
23326 result = write_constant_as_bytes (obstack, byte_order,
23327 type, value, len);
23328 break;
23329
23330 case DW_FORM_sdata:
663c44ac 23331 case DW_FORM_implicit_const:
b6807d98
TT
23332 type = die_type (die, cu);
23333 result = write_constant_as_bytes (obstack, byte_order,
23334 type, DW_SND (attr), len);
23335 break;
23336
23337 case DW_FORM_udata:
23338 type = die_type (die, cu);
23339 result = write_constant_as_bytes (obstack, byte_order,
23340 type, DW_UNSND (attr), len);
23341 break;
23342
23343 default:
23344 complaint (&symfile_complaints,
23345 _("unsupported const value attribute form: '%s'"),
23346 dwarf_form_name (attr->form));
23347 break;
23348 }
23349
23350 return result;
23351}
23352
7942e96e
AA
23353/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23354 valid type for this die is found. */
23355
23356struct type *
9c541725 23357dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23358 struct dwarf2_per_cu_data *per_cu)
23359{
23360 struct dwarf2_cu *cu;
23361 struct die_info *die;
23362
7942e96e
AA
23363 if (per_cu->cu == NULL)
23364 load_cu (per_cu);
23365 cu = per_cu->cu;
23366 if (!cu)
23367 return NULL;
23368
9c541725 23369 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23370 if (!die)
23371 return NULL;
23372
23373 return die_type (die, cu);
23374}
23375
8a9b8146
TT
23376/* Return the type of the DIE at DIE_OFFSET in the CU named by
23377 PER_CU. */
23378
23379struct type *
b64f50a1 23380dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23381 struct dwarf2_per_cu_data *per_cu)
23382{
9c541725 23383 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23384 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23385}
23386
ac9ec31b 23387/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23388 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23389 On exit *REF_CU is the CU of the result.
23390 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23391
23392static struct die_info *
ac9ec31b
DE
23393follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23394 struct dwarf2_cu **ref_cu)
348e048f 23395{
348e048f 23396 struct die_info temp_die;
348e048f
DE
23397 struct dwarf2_cu *sig_cu;
23398 struct die_info *die;
23399
ac9ec31b
DE
23400 /* While it might be nice to assert sig_type->type == NULL here,
23401 we can get here for DW_AT_imported_declaration where we need
23402 the DIE not the type. */
348e048f
DE
23403
23404 /* If necessary, add it to the queue and load its DIEs. */
23405
95554aad 23406 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23407 read_signatured_type (sig_type);
348e048f 23408
348e048f 23409 sig_cu = sig_type->per_cu.cu;
69d751e3 23410 gdb_assert (sig_cu != NULL);
9c541725
PA
23411 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23412 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23413 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23414 to_underlying (temp_die.sect_off));
348e048f
DE
23415 if (die)
23416 {
ed2dc618 23417 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23418 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23419
796a7ff8
DE
23420 /* For .gdb_index version 7 keep track of included TUs.
23421 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23422 if (dwarf2_per_objfile->index_table != NULL
23423 && dwarf2_per_objfile->index_table->version <= 7)
23424 {
23425 VEC_safe_push (dwarf2_per_cu_ptr,
23426 (*ref_cu)->per_cu->imported_symtabs,
23427 sig_cu->per_cu);
23428 }
23429
348e048f
DE
23430 *ref_cu = sig_cu;
23431 return die;
23432 }
23433
ac9ec31b
DE
23434 return NULL;
23435}
23436
23437/* Follow signatured type referenced by ATTR in SRC_DIE.
23438 On entry *REF_CU is the CU of SRC_DIE.
23439 On exit *REF_CU is the CU of the result.
23440 The result is the DIE of the type.
23441 If the referenced type cannot be found an error is thrown. */
23442
23443static struct die_info *
ff39bb5e 23444follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23445 struct dwarf2_cu **ref_cu)
23446{
23447 ULONGEST signature = DW_SIGNATURE (attr);
23448 struct signatured_type *sig_type;
23449 struct die_info *die;
23450
23451 gdb_assert (attr->form == DW_FORM_ref_sig8);
23452
a2ce51a0 23453 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23454 /* sig_type will be NULL if the signatured type is missing from
23455 the debug info. */
23456 if (sig_type == NULL)
23457 {
23458 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23459 " from DIE at 0x%x [in module %s]"),
9c541725 23460 hex_string (signature), to_underlying (src_die->sect_off),
518817b3 23461 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23462 }
23463
23464 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23465 if (die == NULL)
23466 {
23467 dump_die_for_error (src_die);
23468 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23469 " from DIE at 0x%x [in module %s]"),
9c541725 23470 hex_string (signature), to_underlying (src_die->sect_off),
518817b3 23471 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23472 }
23473
23474 return die;
23475}
23476
23477/* Get the type specified by SIGNATURE referenced in DIE/CU,
23478 reading in and processing the type unit if necessary. */
23479
23480static struct type *
23481get_signatured_type (struct die_info *die, ULONGEST signature,
23482 struct dwarf2_cu *cu)
23483{
518817b3
SM
23484 struct dwarf2_per_objfile *dwarf2_per_objfile
23485 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23486 struct signatured_type *sig_type;
23487 struct dwarf2_cu *type_cu;
23488 struct die_info *type_die;
23489 struct type *type;
23490
a2ce51a0 23491 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23492 /* sig_type will be NULL if the signatured type is missing from
23493 the debug info. */
23494 if (sig_type == NULL)
23495 {
23496 complaint (&symfile_complaints,
23497 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23498 " from DIE at 0x%x [in module %s]"),
9c541725 23499 hex_string (signature), to_underlying (die->sect_off),
4262abfb 23500 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23501 return build_error_marker_type (cu, die);
23502 }
23503
23504 /* If we already know the type we're done. */
23505 if (sig_type->type != NULL)
23506 return sig_type->type;
23507
23508 type_cu = cu;
23509 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23510 if (type_die != NULL)
23511 {
23512 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23513 is created. This is important, for example, because for c++ classes
23514 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23515 type = read_type_die (type_die, type_cu);
23516 if (type == NULL)
23517 {
23518 complaint (&symfile_complaints,
23519 _("Dwarf Error: Cannot build signatured type %s"
23520 " referenced from DIE at 0x%x [in module %s]"),
9c541725 23521 hex_string (signature), to_underlying (die->sect_off),
4262abfb 23522 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23523 type = build_error_marker_type (cu, die);
23524 }
23525 }
23526 else
23527 {
23528 complaint (&symfile_complaints,
23529 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23530 " from DIE at 0x%x [in module %s]"),
9c541725 23531 hex_string (signature), to_underlying (die->sect_off),
4262abfb 23532 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23533 type = build_error_marker_type (cu, die);
23534 }
23535 sig_type->type = type;
23536
23537 return type;
23538}
23539
23540/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23541 reading in and processing the type unit if necessary. */
23542
23543static struct type *
ff39bb5e 23544get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23545 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23546{
23547 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23548 if (attr_form_is_ref (attr))
ac9ec31b
DE
23549 {
23550 struct dwarf2_cu *type_cu = cu;
23551 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23552
23553 return read_type_die (type_die, type_cu);
23554 }
23555 else if (attr->form == DW_FORM_ref_sig8)
23556 {
23557 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23558 }
23559 else
23560 {
518817b3
SM
23561 struct dwarf2_per_objfile *dwarf2_per_objfile
23562 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23563
ac9ec31b
DE
23564 complaint (&symfile_complaints,
23565 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23566 " at 0x%x [in module %s]"),
9c541725 23567 dwarf_form_name (attr->form), to_underlying (die->sect_off),
4262abfb 23568 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23569 return build_error_marker_type (cu, die);
23570 }
348e048f
DE
23571}
23572
e5fe5e75 23573/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23574
23575static void
e5fe5e75 23576load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23577{
52dc124a 23578 struct signatured_type *sig_type;
348e048f 23579
f4dc4d17
DE
23580 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23581 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23582
6721b2ec
DE
23583 /* We have the per_cu, but we need the signatured_type.
23584 Fortunately this is an easy translation. */
23585 gdb_assert (per_cu->is_debug_types);
23586 sig_type = (struct signatured_type *) per_cu;
348e048f 23587
6721b2ec 23588 gdb_assert (per_cu->cu == NULL);
348e048f 23589
52dc124a 23590 read_signatured_type (sig_type);
348e048f 23591
6721b2ec 23592 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23593}
23594
dee91e82
DE
23595/* die_reader_func for read_signatured_type.
23596 This is identical to load_full_comp_unit_reader,
23597 but is kept separate for now. */
348e048f
DE
23598
23599static void
dee91e82 23600read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23601 const gdb_byte *info_ptr,
dee91e82
DE
23602 struct die_info *comp_unit_die,
23603 int has_children,
23604 void *data)
348e048f 23605{
dee91e82 23606 struct dwarf2_cu *cu = reader->cu;
348e048f 23607
dee91e82
DE
23608 gdb_assert (cu->die_hash == NULL);
23609 cu->die_hash =
23610 htab_create_alloc_ex (cu->header.length / 12,
23611 die_hash,
23612 die_eq,
23613 NULL,
23614 &cu->comp_unit_obstack,
23615 hashtab_obstack_allocate,
23616 dummy_obstack_deallocate);
348e048f 23617
dee91e82
DE
23618 if (has_children)
23619 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23620 &info_ptr, comp_unit_die);
23621 cu->dies = comp_unit_die;
23622 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23623
23624 /* We try not to read any attributes in this function, because not
9cdd5dbd 23625 all CUs needed for references have been loaded yet, and symbol
348e048f 23626 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23627 or we won't be able to build types correctly.
23628 Similarly, if we do not read the producer, we can not apply
23629 producer-specific interpretation. */
95554aad 23630 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23631}
348e048f 23632
3019eac3
DE
23633/* Read in a signatured type and build its CU and DIEs.
23634 If the type is a stub for the real type in a DWO file,
23635 read in the real type from the DWO file as well. */
dee91e82
DE
23636
23637static void
23638read_signatured_type (struct signatured_type *sig_type)
23639{
23640 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23641
3019eac3 23642 gdb_assert (per_cu->is_debug_types);
dee91e82 23643 gdb_assert (per_cu->cu == NULL);
348e048f 23644
f4dc4d17
DE
23645 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23646 read_signatured_type_reader, NULL);
7ee85ab1 23647 sig_type->per_cu.tu_read = 1;
c906108c
SS
23648}
23649
c906108c
SS
23650/* Decode simple location descriptions.
23651 Given a pointer to a dwarf block that defines a location, compute
23652 the location and return the value.
23653
4cecd739
DJ
23654 NOTE drow/2003-11-18: This function is called in two situations
23655 now: for the address of static or global variables (partial symbols
23656 only) and for offsets into structures which are expected to be
23657 (more or less) constant. The partial symbol case should go away,
23658 and only the constant case should remain. That will let this
23659 function complain more accurately. A few special modes are allowed
23660 without complaint for global variables (for instance, global
23661 register values and thread-local values).
c906108c
SS
23662
23663 A location description containing no operations indicates that the
4cecd739 23664 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23665 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23666 callers will only want a very basic result and this can become a
21ae7a4d
JK
23667 complaint.
23668
23669 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23670
23671static CORE_ADDR
e7c27a73 23672decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23673{
518817b3 23674 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23675 size_t i;
23676 size_t size = blk->size;
d521ce57 23677 const gdb_byte *data = blk->data;
21ae7a4d
JK
23678 CORE_ADDR stack[64];
23679 int stacki;
23680 unsigned int bytes_read, unsnd;
23681 gdb_byte op;
c906108c 23682
21ae7a4d
JK
23683 i = 0;
23684 stacki = 0;
23685 stack[stacki] = 0;
23686 stack[++stacki] = 0;
23687
23688 while (i < size)
23689 {
23690 op = data[i++];
23691 switch (op)
23692 {
23693 case DW_OP_lit0:
23694 case DW_OP_lit1:
23695 case DW_OP_lit2:
23696 case DW_OP_lit3:
23697 case DW_OP_lit4:
23698 case DW_OP_lit5:
23699 case DW_OP_lit6:
23700 case DW_OP_lit7:
23701 case DW_OP_lit8:
23702 case DW_OP_lit9:
23703 case DW_OP_lit10:
23704 case DW_OP_lit11:
23705 case DW_OP_lit12:
23706 case DW_OP_lit13:
23707 case DW_OP_lit14:
23708 case DW_OP_lit15:
23709 case DW_OP_lit16:
23710 case DW_OP_lit17:
23711 case DW_OP_lit18:
23712 case DW_OP_lit19:
23713 case DW_OP_lit20:
23714 case DW_OP_lit21:
23715 case DW_OP_lit22:
23716 case DW_OP_lit23:
23717 case DW_OP_lit24:
23718 case DW_OP_lit25:
23719 case DW_OP_lit26:
23720 case DW_OP_lit27:
23721 case DW_OP_lit28:
23722 case DW_OP_lit29:
23723 case DW_OP_lit30:
23724 case DW_OP_lit31:
23725 stack[++stacki] = op - DW_OP_lit0;
23726 break;
f1bea926 23727
21ae7a4d
JK
23728 case DW_OP_reg0:
23729 case DW_OP_reg1:
23730 case DW_OP_reg2:
23731 case DW_OP_reg3:
23732 case DW_OP_reg4:
23733 case DW_OP_reg5:
23734 case DW_OP_reg6:
23735 case DW_OP_reg7:
23736 case DW_OP_reg8:
23737 case DW_OP_reg9:
23738 case DW_OP_reg10:
23739 case DW_OP_reg11:
23740 case DW_OP_reg12:
23741 case DW_OP_reg13:
23742 case DW_OP_reg14:
23743 case DW_OP_reg15:
23744 case DW_OP_reg16:
23745 case DW_OP_reg17:
23746 case DW_OP_reg18:
23747 case DW_OP_reg19:
23748 case DW_OP_reg20:
23749 case DW_OP_reg21:
23750 case DW_OP_reg22:
23751 case DW_OP_reg23:
23752 case DW_OP_reg24:
23753 case DW_OP_reg25:
23754 case DW_OP_reg26:
23755 case DW_OP_reg27:
23756 case DW_OP_reg28:
23757 case DW_OP_reg29:
23758 case DW_OP_reg30:
23759 case DW_OP_reg31:
23760 stack[++stacki] = op - DW_OP_reg0;
23761 if (i < size)
23762 dwarf2_complex_location_expr_complaint ();
23763 break;
c906108c 23764
21ae7a4d
JK
23765 case DW_OP_regx:
23766 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23767 i += bytes_read;
23768 stack[++stacki] = unsnd;
23769 if (i < size)
23770 dwarf2_complex_location_expr_complaint ();
23771 break;
c906108c 23772
21ae7a4d
JK
23773 case DW_OP_addr:
23774 stack[++stacki] = read_address (objfile->obfd, &data[i],
23775 cu, &bytes_read);
23776 i += bytes_read;
23777 break;
d53d4ac5 23778
21ae7a4d
JK
23779 case DW_OP_const1u:
23780 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23781 i += 1;
23782 break;
23783
23784 case DW_OP_const1s:
23785 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23786 i += 1;
23787 break;
23788
23789 case DW_OP_const2u:
23790 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23791 i += 2;
23792 break;
23793
23794 case DW_OP_const2s:
23795 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23796 i += 2;
23797 break;
d53d4ac5 23798
21ae7a4d
JK
23799 case DW_OP_const4u:
23800 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23801 i += 4;
23802 break;
23803
23804 case DW_OP_const4s:
23805 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23806 i += 4;
23807 break;
23808
585861ea
JK
23809 case DW_OP_const8u:
23810 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23811 i += 8;
23812 break;
23813
21ae7a4d
JK
23814 case DW_OP_constu:
23815 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23816 &bytes_read);
23817 i += bytes_read;
23818 break;
23819
23820 case DW_OP_consts:
23821 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23822 i += bytes_read;
23823 break;
23824
23825 case DW_OP_dup:
23826 stack[stacki + 1] = stack[stacki];
23827 stacki++;
23828 break;
23829
23830 case DW_OP_plus:
23831 stack[stacki - 1] += stack[stacki];
23832 stacki--;
23833 break;
23834
23835 case DW_OP_plus_uconst:
23836 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23837 &bytes_read);
23838 i += bytes_read;
23839 break;
23840
23841 case DW_OP_minus:
23842 stack[stacki - 1] -= stack[stacki];
23843 stacki--;
23844 break;
23845
23846 case DW_OP_deref:
23847 /* If we're not the last op, then we definitely can't encode
23848 this using GDB's address_class enum. This is valid for partial
23849 global symbols, although the variable's address will be bogus
23850 in the psymtab. */
23851 if (i < size)
23852 dwarf2_complex_location_expr_complaint ();
23853 break;
23854
23855 case DW_OP_GNU_push_tls_address:
4aa4e28b 23856 case DW_OP_form_tls_address:
21ae7a4d
JK
23857 /* The top of the stack has the offset from the beginning
23858 of the thread control block at which the variable is located. */
23859 /* Nothing should follow this operator, so the top of stack would
23860 be returned. */
23861 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23862 address will be bogus in the psymtab. Make it always at least
23863 non-zero to not look as a variable garbage collected by linker
23864 which have DW_OP_addr 0. */
21ae7a4d
JK
23865 if (i < size)
23866 dwarf2_complex_location_expr_complaint ();
585861ea 23867 stack[stacki]++;
21ae7a4d
JK
23868 break;
23869
23870 case DW_OP_GNU_uninit:
23871 break;
23872
3019eac3 23873 case DW_OP_GNU_addr_index:
49f6c839 23874 case DW_OP_GNU_const_index:
3019eac3
DE
23875 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23876 &bytes_read);
23877 i += bytes_read;
23878 break;
23879
21ae7a4d
JK
23880 default:
23881 {
f39c6ffd 23882 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23883
23884 if (name)
23885 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23886 name);
23887 else
23888 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23889 op);
23890 }
23891
23892 return (stack[stacki]);
d53d4ac5 23893 }
3c6e0cb3 23894
21ae7a4d
JK
23895 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23896 outside of the allocated space. Also enforce minimum>0. */
23897 if (stacki >= ARRAY_SIZE (stack) - 1)
23898 {
23899 complaint (&symfile_complaints,
23900 _("location description stack overflow"));
23901 return 0;
23902 }
23903
23904 if (stacki <= 0)
23905 {
23906 complaint (&symfile_complaints,
23907 _("location description stack underflow"));
23908 return 0;
23909 }
23910 }
23911 return (stack[stacki]);
c906108c
SS
23912}
23913
23914/* memory allocation interface */
23915
c906108c 23916static struct dwarf_block *
7b5a2f43 23917dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23918{
8d749320 23919 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23920}
23921
c906108c 23922static struct die_info *
b60c80d6 23923dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23924{
23925 struct die_info *die;
b60c80d6
DJ
23926 size_t size = sizeof (struct die_info);
23927
23928 if (num_attrs > 1)
23929 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23930
b60c80d6 23931 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23932 memset (die, 0, sizeof (struct die_info));
23933 return (die);
23934}
2e276125
JB
23935
23936\f
23937/* Macro support. */
23938
233d95b5
JK
23939/* Return file name relative to the compilation directory of file number I in
23940 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 23941 responsible for freeing it. */
233d95b5 23942
2e276125 23943static char *
233d95b5 23944file_file_name (int file, struct line_header *lh)
2e276125 23945{
6a83a1e6
EZ
23946 /* Is the file number a valid index into the line header's file name
23947 table? Remember that file numbers start with one, not zero. */
fff8551c 23948 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 23949 {
8c43009f 23950 const file_entry &fe = lh->file_names[file - 1];
6e70227d 23951
8c43009f
PA
23952 if (!IS_ABSOLUTE_PATH (fe.name))
23953 {
23954 const char *dir = fe.include_dir (lh);
23955 if (dir != NULL)
23956 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23957 }
23958 return xstrdup (fe.name);
6a83a1e6 23959 }
2e276125
JB
23960 else
23961 {
6a83a1e6
EZ
23962 /* The compiler produced a bogus file number. We can at least
23963 record the macro definitions made in the file, even if we
23964 won't be able to find the file by name. */
23965 char fake_name[80];
9a619af0 23966
8c042590
PM
23967 xsnprintf (fake_name, sizeof (fake_name),
23968 "<bad macro file number %d>", file);
2e276125 23969
6e70227d 23970 complaint (&symfile_complaints,
6a83a1e6
EZ
23971 _("bad file number in macro information (%d)"),
23972 file);
2e276125 23973
6a83a1e6 23974 return xstrdup (fake_name);
2e276125
JB
23975 }
23976}
23977
233d95b5
JK
23978/* Return the full name of file number I in *LH's file name table.
23979 Use COMP_DIR as the name of the current directory of the
23980 compilation. The result is allocated using xmalloc; the caller is
23981 responsible for freeing it. */
23982static char *
23983file_full_name (int file, struct line_header *lh, const char *comp_dir)
23984{
23985 /* Is the file number a valid index into the line header's file name
23986 table? Remember that file numbers start with one, not zero. */
fff8551c 23987 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
23988 {
23989 char *relative = file_file_name (file, lh);
23990
23991 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23992 return relative;
b36cec19
PA
23993 return reconcat (relative, comp_dir, SLASH_STRING,
23994 relative, (char *) NULL);
233d95b5
JK
23995 }
23996 else
23997 return file_file_name (file, lh);
23998}
23999
2e276125
JB
24000
24001static struct macro_source_file *
24002macro_start_file (int file, int line,
24003 struct macro_source_file *current_file,
43f3e411 24004 struct line_header *lh)
2e276125 24005{
233d95b5
JK
24006 /* File name relative to the compilation directory of this source file. */
24007 char *file_name = file_file_name (file, lh);
2e276125 24008
2e276125 24009 if (! current_file)
abc9d0dc 24010 {
fc474241
DE
24011 /* Note: We don't create a macro table for this compilation unit
24012 at all until we actually get a filename. */
43f3e411 24013 struct macro_table *macro_table = get_macro_table ();
fc474241 24014
abc9d0dc
TT
24015 /* If we have no current file, then this must be the start_file
24016 directive for the compilation unit's main source file. */
fc474241
DE
24017 current_file = macro_set_main (macro_table, file_name);
24018 macro_define_special (macro_table);
abc9d0dc 24019 }
2e276125 24020 else
233d95b5 24021 current_file = macro_include (current_file, line, file_name);
2e276125 24022
233d95b5 24023 xfree (file_name);
6e70227d 24024
2e276125
JB
24025 return current_file;
24026}
24027
2e276125
JB
24028static const char *
24029consume_improper_spaces (const char *p, const char *body)
24030{
24031 if (*p == ' ')
24032 {
4d3c2250 24033 complaint (&symfile_complaints,
3e43a32a
MS
24034 _("macro definition contains spaces "
24035 "in formal argument list:\n`%s'"),
4d3c2250 24036 body);
2e276125
JB
24037
24038 while (*p == ' ')
24039 p++;
24040 }
24041
24042 return p;
24043}
24044
24045
24046static void
24047parse_macro_definition (struct macro_source_file *file, int line,
24048 const char *body)
24049{
24050 const char *p;
24051
24052 /* The body string takes one of two forms. For object-like macro
24053 definitions, it should be:
24054
24055 <macro name> " " <definition>
24056
24057 For function-like macro definitions, it should be:
24058
24059 <macro name> "() " <definition>
24060 or
24061 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24062
24063 Spaces may appear only where explicitly indicated, and in the
24064 <definition>.
24065
24066 The Dwarf 2 spec says that an object-like macro's name is always
24067 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24068 the space when the macro's definition is the empty string.
2e276125
JB
24069
24070 The Dwarf 2 spec says that there should be no spaces between the
24071 formal arguments in a function-like macro's formal argument list,
24072 but versions of GCC around March 2002 include spaces after the
24073 commas. */
24074
24075
24076 /* Find the extent of the macro name. The macro name is terminated
24077 by either a space or null character (for an object-like macro) or
24078 an opening paren (for a function-like macro). */
24079 for (p = body; *p; p++)
24080 if (*p == ' ' || *p == '(')
24081 break;
24082
24083 if (*p == ' ' || *p == '\0')
24084 {
24085 /* It's an object-like macro. */
24086 int name_len = p - body;
3f8a7804 24087 char *name = savestring (body, name_len);
2e276125
JB
24088 const char *replacement;
24089
24090 if (*p == ' ')
24091 replacement = body + name_len + 1;
24092 else
24093 {
4d3c2250 24094 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24095 replacement = body + name_len;
24096 }
6e70227d 24097
2e276125
JB
24098 macro_define_object (file, line, name, replacement);
24099
24100 xfree (name);
24101 }
24102 else if (*p == '(')
24103 {
24104 /* It's a function-like macro. */
3f8a7804 24105 char *name = savestring (body, p - body);
2e276125
JB
24106 int argc = 0;
24107 int argv_size = 1;
8d749320 24108 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24109
24110 p++;
24111
24112 p = consume_improper_spaces (p, body);
24113
24114 /* Parse the formal argument list. */
24115 while (*p && *p != ')')
24116 {
24117 /* Find the extent of the current argument name. */
24118 const char *arg_start = p;
24119
24120 while (*p && *p != ',' && *p != ')' && *p != ' ')
24121 p++;
24122
24123 if (! *p || p == arg_start)
4d3c2250 24124 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24125 else
24126 {
24127 /* Make sure argv has room for the new argument. */
24128 if (argc >= argv_size)
24129 {
24130 argv_size *= 2;
224c3ddb 24131 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24132 }
24133
3f8a7804 24134 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24135 }
24136
24137 p = consume_improper_spaces (p, body);
24138
24139 /* Consume the comma, if present. */
24140 if (*p == ',')
24141 {
24142 p++;
24143
24144 p = consume_improper_spaces (p, body);
24145 }
24146 }
24147
24148 if (*p == ')')
24149 {
24150 p++;
24151
24152 if (*p == ' ')
24153 /* Perfectly formed definition, no complaints. */
24154 macro_define_function (file, line, name,
6e70227d 24155 argc, (const char **) argv,
2e276125
JB
24156 p + 1);
24157 else if (*p == '\0')
24158 {
24159 /* Complain, but do define it. */
4d3c2250 24160 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24161 macro_define_function (file, line, name,
6e70227d 24162 argc, (const char **) argv,
2e276125
JB
24163 p);
24164 }
24165 else
24166 /* Just complain. */
4d3c2250 24167 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24168 }
24169 else
24170 /* Just complain. */
4d3c2250 24171 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24172
24173 xfree (name);
24174 {
24175 int i;
24176
24177 for (i = 0; i < argc; i++)
24178 xfree (argv[i]);
24179 }
24180 xfree (argv);
24181 }
24182 else
4d3c2250 24183 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24184}
24185
cf2c3c16
TT
24186/* Skip some bytes from BYTES according to the form given in FORM.
24187 Returns the new pointer. */
2e276125 24188
d521ce57
TT
24189static const gdb_byte *
24190skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24191 enum dwarf_form form,
24192 unsigned int offset_size,
24193 struct dwarf2_section_info *section)
2e276125 24194{
cf2c3c16 24195 unsigned int bytes_read;
2e276125 24196
cf2c3c16 24197 switch (form)
2e276125 24198 {
cf2c3c16
TT
24199 case DW_FORM_data1:
24200 case DW_FORM_flag:
24201 ++bytes;
24202 break;
24203
24204 case DW_FORM_data2:
24205 bytes += 2;
24206 break;
24207
24208 case DW_FORM_data4:
24209 bytes += 4;
24210 break;
24211
24212 case DW_FORM_data8:
24213 bytes += 8;
24214 break;
24215
0224619f
JK
24216 case DW_FORM_data16:
24217 bytes += 16;
24218 break;
24219
cf2c3c16
TT
24220 case DW_FORM_string:
24221 read_direct_string (abfd, bytes, &bytes_read);
24222 bytes += bytes_read;
24223 break;
24224
24225 case DW_FORM_sec_offset:
24226 case DW_FORM_strp:
36586728 24227 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24228 bytes += offset_size;
24229 break;
24230
24231 case DW_FORM_block:
24232 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24233 bytes += bytes_read;
24234 break;
24235
24236 case DW_FORM_block1:
24237 bytes += 1 + read_1_byte (abfd, bytes);
24238 break;
24239 case DW_FORM_block2:
24240 bytes += 2 + read_2_bytes (abfd, bytes);
24241 break;
24242 case DW_FORM_block4:
24243 bytes += 4 + read_4_bytes (abfd, bytes);
24244 break;
24245
24246 case DW_FORM_sdata:
24247 case DW_FORM_udata:
3019eac3
DE
24248 case DW_FORM_GNU_addr_index:
24249 case DW_FORM_GNU_str_index:
d521ce57 24250 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24251 if (bytes == NULL)
24252 {
24253 dwarf2_section_buffer_overflow_complaint (section);
24254 return NULL;
24255 }
cf2c3c16
TT
24256 break;
24257
663c44ac
JK
24258 case DW_FORM_implicit_const:
24259 break;
24260
cf2c3c16
TT
24261 default:
24262 {
cf2c3c16
TT
24263 complaint (&symfile_complaints,
24264 _("invalid form 0x%x in `%s'"),
a32a8923 24265 form, get_section_name (section));
cf2c3c16
TT
24266 return NULL;
24267 }
2e276125
JB
24268 }
24269
cf2c3c16
TT
24270 return bytes;
24271}
757a13d0 24272
cf2c3c16
TT
24273/* A helper for dwarf_decode_macros that handles skipping an unknown
24274 opcode. Returns an updated pointer to the macro data buffer; or,
24275 on error, issues a complaint and returns NULL. */
757a13d0 24276
d521ce57 24277static const gdb_byte *
cf2c3c16 24278skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24279 const gdb_byte **opcode_definitions,
24280 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24281 bfd *abfd,
24282 unsigned int offset_size,
24283 struct dwarf2_section_info *section)
24284{
24285 unsigned int bytes_read, i;
24286 unsigned long arg;
d521ce57 24287 const gdb_byte *defn;
2e276125 24288
cf2c3c16 24289 if (opcode_definitions[opcode] == NULL)
2e276125 24290 {
cf2c3c16
TT
24291 complaint (&symfile_complaints,
24292 _("unrecognized DW_MACFINO opcode 0x%x"),
24293 opcode);
24294 return NULL;
24295 }
2e276125 24296
cf2c3c16
TT
24297 defn = opcode_definitions[opcode];
24298 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24299 defn += bytes_read;
2e276125 24300
cf2c3c16
TT
24301 for (i = 0; i < arg; ++i)
24302 {
aead7601
SM
24303 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24304 (enum dwarf_form) defn[i], offset_size,
f664829e 24305 section);
cf2c3c16
TT
24306 if (mac_ptr == NULL)
24307 {
24308 /* skip_form_bytes already issued the complaint. */
24309 return NULL;
24310 }
24311 }
757a13d0 24312
cf2c3c16
TT
24313 return mac_ptr;
24314}
757a13d0 24315
cf2c3c16
TT
24316/* A helper function which parses the header of a macro section.
24317 If the macro section is the extended (for now called "GNU") type,
24318 then this updates *OFFSET_SIZE. Returns a pointer to just after
24319 the header, or issues a complaint and returns NULL on error. */
757a13d0 24320
d521ce57
TT
24321static const gdb_byte *
24322dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24323 bfd *abfd,
d521ce57 24324 const gdb_byte *mac_ptr,
cf2c3c16
TT
24325 unsigned int *offset_size,
24326 int section_is_gnu)
24327{
24328 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24329
cf2c3c16
TT
24330 if (section_is_gnu)
24331 {
24332 unsigned int version, flags;
757a13d0 24333
cf2c3c16 24334 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24335 if (version != 4 && version != 5)
cf2c3c16
TT
24336 {
24337 complaint (&symfile_complaints,
24338 _("unrecognized version `%d' in .debug_macro section"),
24339 version);
24340 return NULL;
24341 }
24342 mac_ptr += 2;
757a13d0 24343
cf2c3c16
TT
24344 flags = read_1_byte (abfd, mac_ptr);
24345 ++mac_ptr;
24346 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24347
cf2c3c16
TT
24348 if ((flags & 2) != 0)
24349 /* We don't need the line table offset. */
24350 mac_ptr += *offset_size;
757a13d0 24351
cf2c3c16
TT
24352 /* Vendor opcode descriptions. */
24353 if ((flags & 4) != 0)
24354 {
24355 unsigned int i, count;
757a13d0 24356
cf2c3c16
TT
24357 count = read_1_byte (abfd, mac_ptr);
24358 ++mac_ptr;
24359 for (i = 0; i < count; ++i)
24360 {
24361 unsigned int opcode, bytes_read;
24362 unsigned long arg;
24363
24364 opcode = read_1_byte (abfd, mac_ptr);
24365 ++mac_ptr;
24366 opcode_definitions[opcode] = mac_ptr;
24367 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24368 mac_ptr += bytes_read;
24369 mac_ptr += arg;
24370 }
757a13d0 24371 }
cf2c3c16 24372 }
757a13d0 24373
cf2c3c16
TT
24374 return mac_ptr;
24375}
757a13d0 24376
cf2c3c16 24377/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24378 including DW_MACRO_import. */
cf2c3c16
TT
24379
24380static void
ed2dc618
SM
24381dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24382 bfd *abfd,
d521ce57 24383 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24384 struct macro_source_file *current_file,
43f3e411 24385 struct line_header *lh,
cf2c3c16 24386 struct dwarf2_section_info *section,
36586728 24387 int section_is_gnu, int section_is_dwz,
cf2c3c16 24388 unsigned int offset_size,
8fc3fc34 24389 htab_t include_hash)
cf2c3c16 24390{
4d663531 24391 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24392 enum dwarf_macro_record_type macinfo_type;
24393 int at_commandline;
d521ce57 24394 const gdb_byte *opcode_definitions[256];
757a13d0 24395
cf2c3c16
TT
24396 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24397 &offset_size, section_is_gnu);
24398 if (mac_ptr == NULL)
24399 {
24400 /* We already issued a complaint. */
24401 return;
24402 }
757a13d0
JK
24403
24404 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24405 GDB is still reading the definitions from command line. First
24406 DW_MACINFO_start_file will need to be ignored as it was already executed
24407 to create CURRENT_FILE for the main source holding also the command line
24408 definitions. On first met DW_MACINFO_start_file this flag is reset to
24409 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24410
24411 at_commandline = 1;
24412
24413 do
24414 {
24415 /* Do we at least have room for a macinfo type byte? */
24416 if (mac_ptr >= mac_end)
24417 {
f664829e 24418 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24419 break;
24420 }
24421
aead7601 24422 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24423 mac_ptr++;
24424
cf2c3c16
TT
24425 /* Note that we rely on the fact that the corresponding GNU and
24426 DWARF constants are the same. */
132448f8
SM
24427 DIAGNOSTIC_PUSH
24428 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24429 switch (macinfo_type)
24430 {
24431 /* A zero macinfo type indicates the end of the macro
24432 information. */
24433 case 0:
24434 break;
2e276125 24435
0af92d60
JK
24436 case DW_MACRO_define:
24437 case DW_MACRO_undef:
24438 case DW_MACRO_define_strp:
24439 case DW_MACRO_undef_strp:
24440 case DW_MACRO_define_sup:
24441 case DW_MACRO_undef_sup:
2e276125 24442 {
891d2f0b 24443 unsigned int bytes_read;
2e276125 24444 int line;
d521ce57 24445 const char *body;
cf2c3c16 24446 int is_define;
2e276125 24447
cf2c3c16
TT
24448 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24449 mac_ptr += bytes_read;
24450
0af92d60
JK
24451 if (macinfo_type == DW_MACRO_define
24452 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24453 {
24454 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24455 mac_ptr += bytes_read;
24456 }
24457 else
24458 {
24459 LONGEST str_offset;
24460
24461 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24462 mac_ptr += offset_size;
2e276125 24463
0af92d60
JK
24464 if (macinfo_type == DW_MACRO_define_sup
24465 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24466 || section_is_dwz)
36586728 24467 {
ed2dc618
SM
24468 struct dwz_file *dwz
24469 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24470
ed2dc618
SM
24471 body = read_indirect_string_from_dwz (objfile,
24472 dwz, str_offset);
36586728
TT
24473 }
24474 else
ed2dc618
SM
24475 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24476 abfd, str_offset);
cf2c3c16
TT
24477 }
24478
0af92d60
JK
24479 is_define = (macinfo_type == DW_MACRO_define
24480 || macinfo_type == DW_MACRO_define_strp
24481 || macinfo_type == DW_MACRO_define_sup);
2e276125 24482 if (! current_file)
757a13d0
JK
24483 {
24484 /* DWARF violation as no main source is present. */
24485 complaint (&symfile_complaints,
24486 _("debug info with no main source gives macro %s "
24487 "on line %d: %s"),
cf2c3c16
TT
24488 is_define ? _("definition") : _("undefinition"),
24489 line, body);
757a13d0
JK
24490 break;
24491 }
3e43a32a
MS
24492 if ((line == 0 && !at_commandline)
24493 || (line != 0 && at_commandline))
4d3c2250 24494 complaint (&symfile_complaints,
757a13d0
JK
24495 _("debug info gives %s macro %s with %s line %d: %s"),
24496 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24497 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24498 line == 0 ? _("zero") : _("non-zero"), line, body);
24499
cf2c3c16 24500 if (is_define)
757a13d0 24501 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24502 else
24503 {
0af92d60
JK
24504 gdb_assert (macinfo_type == DW_MACRO_undef
24505 || macinfo_type == DW_MACRO_undef_strp
24506 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24507 macro_undef (current_file, line, body);
24508 }
2e276125
JB
24509 }
24510 break;
24511
0af92d60 24512 case DW_MACRO_start_file:
2e276125 24513 {
891d2f0b 24514 unsigned int bytes_read;
2e276125
JB
24515 int line, file;
24516
24517 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24518 mac_ptr += bytes_read;
24519 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24520 mac_ptr += bytes_read;
24521
3e43a32a
MS
24522 if ((line == 0 && !at_commandline)
24523 || (line != 0 && at_commandline))
757a13d0
JK
24524 complaint (&symfile_complaints,
24525 _("debug info gives source %d included "
24526 "from %s at %s line %d"),
24527 file, at_commandline ? _("command-line") : _("file"),
24528 line == 0 ? _("zero") : _("non-zero"), line);
24529
24530 if (at_commandline)
24531 {
0af92d60 24532 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24533 pass one. */
757a13d0
JK
24534 at_commandline = 0;
24535 }
24536 else
43f3e411 24537 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
24538 }
24539 break;
24540
0af92d60 24541 case DW_MACRO_end_file:
2e276125 24542 if (! current_file)
4d3c2250 24543 complaint (&symfile_complaints,
3e43a32a
MS
24544 _("macro debug info has an unmatched "
24545 "`close_file' directive"));
2e276125
JB
24546 else
24547 {
24548 current_file = current_file->included_by;
24549 if (! current_file)
24550 {
cf2c3c16 24551 enum dwarf_macro_record_type next_type;
2e276125
JB
24552
24553 /* GCC circa March 2002 doesn't produce the zero
24554 type byte marking the end of the compilation
24555 unit. Complain if it's not there, but exit no
24556 matter what. */
24557
24558 /* Do we at least have room for a macinfo type byte? */
24559 if (mac_ptr >= mac_end)
24560 {
f664829e 24561 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24562 return;
24563 }
24564
24565 /* We don't increment mac_ptr here, so this is just
24566 a look-ahead. */
aead7601
SM
24567 next_type
24568 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24569 mac_ptr);
2e276125 24570 if (next_type != 0)
4d3c2250 24571 complaint (&symfile_complaints,
3e43a32a
MS
24572 _("no terminating 0-type entry for "
24573 "macros in `.debug_macinfo' section"));
2e276125
JB
24574
24575 return;
24576 }
24577 }
24578 break;
24579
0af92d60
JK
24580 case DW_MACRO_import:
24581 case DW_MACRO_import_sup:
cf2c3c16
TT
24582 {
24583 LONGEST offset;
8fc3fc34 24584 void **slot;
a036ba48
TT
24585 bfd *include_bfd = abfd;
24586 struct dwarf2_section_info *include_section = section;
d521ce57 24587 const gdb_byte *include_mac_end = mac_end;
a036ba48 24588 int is_dwz = section_is_dwz;
d521ce57 24589 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24590
24591 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24592 mac_ptr += offset_size;
24593
0af92d60 24594 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24595 {
ed2dc618 24596 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24597
4d663531 24598 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24599
a036ba48 24600 include_section = &dwz->macro;
a32a8923 24601 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24602 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24603 is_dwz = 1;
24604 }
24605
24606 new_mac_ptr = include_section->buffer + offset;
24607 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24608
8fc3fc34
TT
24609 if (*slot != NULL)
24610 {
24611 /* This has actually happened; see
24612 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24613 complaint (&symfile_complaints,
0af92d60 24614 _("recursive DW_MACRO_import in "
8fc3fc34
TT
24615 ".debug_macro section"));
24616 }
24617 else
24618 {
d521ce57 24619 *slot = (void *) new_mac_ptr;
36586728 24620
ed2dc618
SM
24621 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24622 include_bfd, new_mac_ptr,
43f3e411 24623 include_mac_end, current_file, lh,
36586728 24624 section, section_is_gnu, is_dwz,
4d663531 24625 offset_size, include_hash);
8fc3fc34 24626
d521ce57 24627 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24628 }
cf2c3c16
TT
24629 }
24630 break;
24631
2e276125 24632 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24633 if (!section_is_gnu)
24634 {
24635 unsigned int bytes_read;
2e276125 24636
ac298888
TT
24637 /* This reads the constant, but since we don't recognize
24638 any vendor extensions, we ignore it. */
24639 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24640 mac_ptr += bytes_read;
24641 read_direct_string (abfd, mac_ptr, &bytes_read);
24642 mac_ptr += bytes_read;
2e276125 24643
cf2c3c16
TT
24644 /* We don't recognize any vendor extensions. */
24645 break;
24646 }
24647 /* FALLTHROUGH */
24648
24649 default:
24650 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24651 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24652 section);
24653 if (mac_ptr == NULL)
24654 return;
24655 break;
2e276125 24656 }
132448f8 24657 DIAGNOSTIC_POP
757a13d0 24658 } while (macinfo_type != 0);
2e276125 24659}
8e19ed76 24660
cf2c3c16 24661static void
09262596 24662dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24663 int section_is_gnu)
cf2c3c16 24664{
518817b3
SM
24665 struct dwarf2_per_objfile *dwarf2_per_objfile
24666 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24667 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24668 struct line_header *lh = cu->line_header;
24669 bfd *abfd;
d521ce57 24670 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24671 struct macro_source_file *current_file = 0;
24672 enum dwarf_macro_record_type macinfo_type;
24673 unsigned int offset_size = cu->header.offset_size;
d521ce57 24674 const gdb_byte *opcode_definitions[256];
8fc3fc34 24675 void **slot;
09262596
DE
24676 struct dwarf2_section_info *section;
24677 const char *section_name;
24678
24679 if (cu->dwo_unit != NULL)
24680 {
24681 if (section_is_gnu)
24682 {
24683 section = &cu->dwo_unit->dwo_file->sections.macro;
24684 section_name = ".debug_macro.dwo";
24685 }
24686 else
24687 {
24688 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24689 section_name = ".debug_macinfo.dwo";
24690 }
24691 }
24692 else
24693 {
24694 if (section_is_gnu)
24695 {
24696 section = &dwarf2_per_objfile->macro;
24697 section_name = ".debug_macro";
24698 }
24699 else
24700 {
24701 section = &dwarf2_per_objfile->macinfo;
24702 section_name = ".debug_macinfo";
24703 }
24704 }
cf2c3c16 24705
bb5ed363 24706 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24707 if (section->buffer == NULL)
24708 {
fceca515 24709 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
24710 return;
24711 }
a32a8923 24712 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24713
24714 /* First pass: Find the name of the base filename.
24715 This filename is needed in order to process all macros whose definition
24716 (or undefinition) comes from the command line. These macros are defined
24717 before the first DW_MACINFO_start_file entry, and yet still need to be
24718 associated to the base file.
24719
24720 To determine the base file name, we scan the macro definitions until we
24721 reach the first DW_MACINFO_start_file entry. We then initialize
24722 CURRENT_FILE accordingly so that any macro definition found before the
24723 first DW_MACINFO_start_file can still be associated to the base file. */
24724
24725 mac_ptr = section->buffer + offset;
24726 mac_end = section->buffer + section->size;
24727
24728 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24729 &offset_size, section_is_gnu);
24730 if (mac_ptr == NULL)
24731 {
24732 /* We already issued a complaint. */
24733 return;
24734 }
24735
24736 do
24737 {
24738 /* Do we at least have room for a macinfo type byte? */
24739 if (mac_ptr >= mac_end)
24740 {
24741 /* Complaint is printed during the second pass as GDB will probably
24742 stop the first pass earlier upon finding
24743 DW_MACINFO_start_file. */
24744 break;
24745 }
24746
aead7601 24747 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24748 mac_ptr++;
24749
24750 /* Note that we rely on the fact that the corresponding GNU and
24751 DWARF constants are the same. */
132448f8
SM
24752 DIAGNOSTIC_PUSH
24753 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
24754 switch (macinfo_type)
24755 {
24756 /* A zero macinfo type indicates the end of the macro
24757 information. */
24758 case 0:
24759 break;
24760
0af92d60
JK
24761 case DW_MACRO_define:
24762 case DW_MACRO_undef:
cf2c3c16
TT
24763 /* Only skip the data by MAC_PTR. */
24764 {
24765 unsigned int bytes_read;
24766
24767 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24768 mac_ptr += bytes_read;
24769 read_direct_string (abfd, mac_ptr, &bytes_read);
24770 mac_ptr += bytes_read;
24771 }
24772 break;
24773
0af92d60 24774 case DW_MACRO_start_file:
cf2c3c16
TT
24775 {
24776 unsigned int bytes_read;
24777 int line, file;
24778
24779 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24780 mac_ptr += bytes_read;
24781 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24782 mac_ptr += bytes_read;
24783
43f3e411 24784 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
24785 }
24786 break;
24787
0af92d60 24788 case DW_MACRO_end_file:
cf2c3c16
TT
24789 /* No data to skip by MAC_PTR. */
24790 break;
24791
0af92d60
JK
24792 case DW_MACRO_define_strp:
24793 case DW_MACRO_undef_strp:
24794 case DW_MACRO_define_sup:
24795 case DW_MACRO_undef_sup:
cf2c3c16
TT
24796 {
24797 unsigned int bytes_read;
24798
24799 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24800 mac_ptr += bytes_read;
24801 mac_ptr += offset_size;
24802 }
24803 break;
24804
0af92d60
JK
24805 case DW_MACRO_import:
24806 case DW_MACRO_import_sup:
cf2c3c16 24807 /* Note that, according to the spec, a transparent include
0af92d60 24808 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
24809 skip this opcode. */
24810 mac_ptr += offset_size;
24811 break;
24812
24813 case DW_MACINFO_vendor_ext:
24814 /* Only skip the data by MAC_PTR. */
24815 if (!section_is_gnu)
24816 {
24817 unsigned int bytes_read;
24818
24819 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24820 mac_ptr += bytes_read;
24821 read_direct_string (abfd, mac_ptr, &bytes_read);
24822 mac_ptr += bytes_read;
24823 }
24824 /* FALLTHROUGH */
24825
24826 default:
24827 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24828 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24829 section);
24830 if (mac_ptr == NULL)
24831 return;
24832 break;
24833 }
132448f8 24834 DIAGNOSTIC_POP
cf2c3c16
TT
24835 } while (macinfo_type != 0 && current_file == NULL);
24836
24837 /* Second pass: Process all entries.
24838
24839 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24840 command-line macro definitions/undefinitions. This flag is unset when we
24841 reach the first DW_MACINFO_start_file entry. */
24842
fc4007c9
TT
24843 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24844 htab_eq_pointer,
24845 NULL, xcalloc, xfree));
8fc3fc34 24846 mac_ptr = section->buffer + offset;
fc4007c9 24847 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 24848 *slot = (void *) mac_ptr;
ed2dc618
SM
24849 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24850 abfd, mac_ptr, mac_end,
43f3e411 24851 current_file, lh, section,
fc4007c9
TT
24852 section_is_gnu, 0, offset_size,
24853 include_hash.get ());
cf2c3c16
TT
24854}
24855
8e19ed76 24856/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 24857 if so return true else false. */
380bca97 24858
8e19ed76 24859static int
6e5a29e1 24860attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
24861{
24862 return (attr == NULL ? 0 :
24863 attr->form == DW_FORM_block1
24864 || attr->form == DW_FORM_block2
24865 || attr->form == DW_FORM_block4
2dc7f7b3
TT
24866 || attr->form == DW_FORM_block
24867 || attr->form == DW_FORM_exprloc);
8e19ed76 24868}
4c2df51b 24869
c6a0999f
JB
24870/* Return non-zero if ATTR's value is a section offset --- classes
24871 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24872 You may use DW_UNSND (attr) to retrieve such offsets.
24873
24874 Section 7.5.4, "Attribute Encodings", explains that no attribute
24875 may have a value that belongs to more than one of these classes; it
24876 would be ambiguous if we did, because we use the same forms for all
24877 of them. */
380bca97 24878
3690dd37 24879static int
6e5a29e1 24880attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
24881{
24882 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
24883 || attr->form == DW_FORM_data8
24884 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
24885}
24886
3690dd37
JB
24887/* Return non-zero if ATTR's value falls in the 'constant' class, or
24888 zero otherwise. When this function returns true, you can apply
24889 dwarf2_get_attr_constant_value to it.
24890
24891 However, note that for some attributes you must check
24892 attr_form_is_section_offset before using this test. DW_FORM_data4
24893 and DW_FORM_data8 are members of both the constant class, and of
24894 the classes that contain offsets into other debug sections
24895 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24896 that, if an attribute's can be either a constant or one of the
24897 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
24898 taken as section offsets, not constants.
24899
24900 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24901 cannot handle that. */
380bca97 24902
3690dd37 24903static int
6e5a29e1 24904attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
24905{
24906 switch (attr->form)
24907 {
24908 case DW_FORM_sdata:
24909 case DW_FORM_udata:
24910 case DW_FORM_data1:
24911 case DW_FORM_data2:
24912 case DW_FORM_data4:
24913 case DW_FORM_data8:
663c44ac 24914 case DW_FORM_implicit_const:
3690dd37
JB
24915 return 1;
24916 default:
24917 return 0;
24918 }
24919}
24920
7771576e
SA
24921
24922/* DW_ADDR is always stored already as sect_offset; despite for the forms
24923 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24924
24925static int
6e5a29e1 24926attr_form_is_ref (const struct attribute *attr)
7771576e
SA
24927{
24928 switch (attr->form)
24929 {
24930 case DW_FORM_ref_addr:
24931 case DW_FORM_ref1:
24932 case DW_FORM_ref2:
24933 case DW_FORM_ref4:
24934 case DW_FORM_ref8:
24935 case DW_FORM_ref_udata:
24936 case DW_FORM_GNU_ref_alt:
24937 return 1;
24938 default:
24939 return 0;
24940 }
24941}
24942
3019eac3
DE
24943/* Return the .debug_loc section to use for CU.
24944 For DWO files use .debug_loc.dwo. */
24945
24946static struct dwarf2_section_info *
24947cu_debug_loc_section (struct dwarf2_cu *cu)
24948{
518817b3
SM
24949 struct dwarf2_per_objfile *dwarf2_per_objfile
24950 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24951
3019eac3 24952 if (cu->dwo_unit)
43988095
JK
24953 {
24954 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24955
24956 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24957 }
24958 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24959 : &dwarf2_per_objfile->loc);
3019eac3
DE
24960}
24961
8cf6f0b1
TT
24962/* A helper function that fills in a dwarf2_loclist_baton. */
24963
24964static void
24965fill_in_loclist_baton (struct dwarf2_cu *cu,
24966 struct dwarf2_loclist_baton *baton,
ff39bb5e 24967 const struct attribute *attr)
8cf6f0b1 24968{
518817b3
SM
24969 struct dwarf2_per_objfile *dwarf2_per_objfile
24970 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
24971 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24972
24973 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
24974
24975 baton->per_cu = cu->per_cu;
24976 gdb_assert (baton->per_cu);
24977 /* We don't know how long the location list is, but make sure we
24978 don't run off the edge of the section. */
3019eac3
DE
24979 baton->size = section->size - DW_UNSND (attr);
24980 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 24981 baton->base_address = cu->base_address;
f664829e 24982 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24983}
24984
4c2df51b 24985static void
ff39bb5e 24986dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24987 struct dwarf2_cu *cu, int is_block)
4c2df51b 24988{
518817b3
SM
24989 struct dwarf2_per_objfile *dwarf2_per_objfile
24990 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24991 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 24992 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24993
3690dd37 24994 if (attr_form_is_section_offset (attr)
3019eac3 24995 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24996 the section. If so, fall through to the complaint in the
24997 other branch. */
3019eac3 24998 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 24999 {
0d53c4c4 25000 struct dwarf2_loclist_baton *baton;
4c2df51b 25001
8d749320 25002 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25003
8cf6f0b1 25004 fill_in_loclist_baton (cu, baton, attr);
be391dca 25005
d00adf39 25006 if (cu->base_known == 0)
0d53c4c4 25007 complaint (&symfile_complaints,
3e43a32a
MS
25008 _("Location list used without "
25009 "specifying the CU base address."));
4c2df51b 25010
f1e6e072
TT
25011 SYMBOL_ACLASS_INDEX (sym) = (is_block
25012 ? dwarf2_loclist_block_index
25013 : dwarf2_loclist_index);
0d53c4c4
DJ
25014 SYMBOL_LOCATION_BATON (sym) = baton;
25015 }
25016 else
25017 {
25018 struct dwarf2_locexpr_baton *baton;
25019
8d749320 25020 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25021 baton->per_cu = cu->per_cu;
25022 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25023
25024 if (attr_form_is_block (attr))
25025 {
25026 /* Note that we're just copying the block's data pointer
25027 here, not the actual data. We're still pointing into the
6502dd73
DJ
25028 info_buffer for SYM's objfile; right now we never release
25029 that buffer, but when we do clean up properly this may
25030 need to change. */
0d53c4c4
DJ
25031 baton->size = DW_BLOCK (attr)->size;
25032 baton->data = DW_BLOCK (attr)->data;
25033 }
25034 else
25035 {
25036 dwarf2_invalid_attrib_class_complaint ("location description",
25037 SYMBOL_NATURAL_NAME (sym));
25038 baton->size = 0;
0d53c4c4 25039 }
6e70227d 25040
f1e6e072
TT
25041 SYMBOL_ACLASS_INDEX (sym) = (is_block
25042 ? dwarf2_locexpr_block_index
25043 : dwarf2_locexpr_index);
0d53c4c4
DJ
25044 SYMBOL_LOCATION_BATON (sym) = baton;
25045 }
4c2df51b 25046}
6502dd73 25047
9aa1f1e3
TT
25048/* Return the OBJFILE associated with the compilation unit CU. If CU
25049 came from a separate debuginfo file, then the master objfile is
25050 returned. */
ae0d2f24
UW
25051
25052struct objfile *
25053dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25054{
e3b94546 25055 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25056
25057 /* Return the master objfile, so that we can report and look up the
25058 correct file containing this variable. */
25059 if (objfile->separate_debug_objfile_backlink)
25060 objfile = objfile->separate_debug_objfile_backlink;
25061
25062 return objfile;
25063}
25064
96408a79
SA
25065/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25066 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25067 CU_HEADERP first. */
25068
25069static const struct comp_unit_head *
25070per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25071 struct dwarf2_per_cu_data *per_cu)
25072{
d521ce57 25073 const gdb_byte *info_ptr;
96408a79
SA
25074
25075 if (per_cu->cu)
25076 return &per_cu->cu->header;
25077
9c541725 25078 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25079
25080 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25081 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25082 rcuh_kind::COMPILE);
96408a79
SA
25083
25084 return cu_headerp;
25085}
25086
ae0d2f24
UW
25087/* Return the address size given in the compilation unit header for CU. */
25088
98714339 25089int
ae0d2f24
UW
25090dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25091{
96408a79
SA
25092 struct comp_unit_head cu_header_local;
25093 const struct comp_unit_head *cu_headerp;
c471e790 25094
96408a79
SA
25095 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25096
25097 return cu_headerp->addr_size;
ae0d2f24
UW
25098}
25099
9eae7c52
TT
25100/* Return the offset size given in the compilation unit header for CU. */
25101
25102int
25103dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25104{
96408a79
SA
25105 struct comp_unit_head cu_header_local;
25106 const struct comp_unit_head *cu_headerp;
9c6c53f7 25107
96408a79
SA
25108 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25109
25110 return cu_headerp->offset_size;
25111}
25112
25113/* See its dwarf2loc.h declaration. */
25114
25115int
25116dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25117{
25118 struct comp_unit_head cu_header_local;
25119 const struct comp_unit_head *cu_headerp;
25120
25121 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25122
25123 if (cu_headerp->version == 2)
25124 return cu_headerp->addr_size;
25125 else
25126 return cu_headerp->offset_size;
181cebd4
JK
25127}
25128
9aa1f1e3
TT
25129/* Return the text offset of the CU. The returned offset comes from
25130 this CU's objfile. If this objfile came from a separate debuginfo
25131 file, then the offset may be different from the corresponding
25132 offset in the parent objfile. */
25133
25134CORE_ADDR
25135dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25136{
e3b94546 25137 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
25138
25139 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25140}
25141
43988095
JK
25142/* Return DWARF version number of PER_CU. */
25143
25144short
25145dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25146{
25147 return per_cu->dwarf_version;
25148}
25149
348e048f
DE
25150/* Locate the .debug_info compilation unit from CU's objfile which contains
25151 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25152
25153static struct dwarf2_per_cu_data *
9c541725 25154dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25155 unsigned int offset_in_dwz,
ed2dc618 25156 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25157{
25158 struct dwarf2_per_cu_data *this_cu;
25159 int low, high;
36586728 25160 const sect_offset *cu_off;
ae038cb0 25161
ae038cb0
DJ
25162 low = 0;
25163 high = dwarf2_per_objfile->n_comp_units - 1;
25164 while (high > low)
25165 {
36586728 25166 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25167 int mid = low + (high - low) / 2;
9a619af0 25168
36586728 25169 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 25170 cu_off = &mid_cu->sect_off;
36586728 25171 if (mid_cu->is_dwz > offset_in_dwz
9c541725 25172 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
25173 high = mid;
25174 else
25175 low = mid + 1;
25176 }
25177 gdb_assert (low == high);
36586728 25178 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
25179 cu_off = &this_cu->sect_off;
25180 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 25181 {
36586728 25182 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25183 error (_("Dwarf Error: could not find partial DIE containing "
9c541725 25184 "offset 0x%x [in module %s]"),
ed2dc618
SM
25185 to_underlying (sect_off),
25186 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25187
9c541725
PA
25188 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25189 <= sect_off);
ae038cb0
DJ
25190 return dwarf2_per_objfile->all_comp_units[low-1];
25191 }
25192 else
25193 {
25194 this_cu = dwarf2_per_objfile->all_comp_units[low];
25195 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725
PA
25196 && sect_off >= this_cu->sect_off + this_cu->length)
25197 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
25198 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25199 return this_cu;
25200 }
25201}
25202
23745b47 25203/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25204
9816fde3 25205static void
23745b47 25206init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 25207{
9816fde3 25208 memset (cu, 0, sizeof (*cu));
23745b47
DE
25209 per_cu->cu = cu;
25210 cu->per_cu = per_cu;
93311388 25211 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
25212}
25213
25214/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25215
25216static void
95554aad
TT
25217prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25218 enum language pretend_language)
9816fde3
JK
25219{
25220 struct attribute *attr;
25221
25222 /* Set the language we're debugging. */
25223 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25224 if (attr)
25225 set_cu_language (DW_UNSND (attr), cu);
25226 else
9cded63f 25227 {
95554aad 25228 cu->language = pretend_language;
9cded63f
TT
25229 cu->language_defn = language_def (cu->language);
25230 }
dee91e82 25231
7d45c7c3 25232 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25233}
25234
ae038cb0
DJ
25235/* Release one cached compilation unit, CU. We unlink it from the tree
25236 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
25237 the caller is responsible for that.
25238 NOTE: DATA is a void * because this function is also used as a
25239 cleanup routine. */
ae038cb0
DJ
25240
25241static void
68dc6402 25242free_heap_comp_unit (void *data)
ae038cb0 25243{
9a3c8263 25244 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 25245
23745b47
DE
25246 gdb_assert (cu->per_cu != NULL);
25247 cu->per_cu->cu = NULL;
ae038cb0
DJ
25248 cu->per_cu = NULL;
25249
25250 obstack_free (&cu->comp_unit_obstack, NULL);
25251
25252 xfree (cu);
25253}
25254
72bf9492 25255/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 25256 when we're finished with it. We can't free the pointer itself, but be
dee91e82 25257 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
25258
25259static void
25260free_stack_comp_unit (void *data)
25261{
9a3c8263 25262 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 25263
23745b47
DE
25264 gdb_assert (cu->per_cu != NULL);
25265 cu->per_cu->cu = NULL;
25266 cu->per_cu = NULL;
25267
72bf9492
DJ
25268 obstack_free (&cu->comp_unit_obstack, NULL);
25269 cu->partial_dies = NULL;
ae038cb0
DJ
25270}
25271
25272/* Free all cached compilation units. */
25273
25274static void
25275free_cached_comp_units (void *data)
25276{
ed2dc618
SM
25277 struct dwarf2_per_objfile *dwarf2_per_objfile
25278 = (struct dwarf2_per_objfile *) data;
25279
330cdd98 25280 dwarf2_per_objfile->free_cached_comp_units ();
ae038cb0
DJ
25281}
25282
25283/* Increase the age counter on each cached compilation unit, and free
25284 any that are too old. */
25285
25286static void
ed2dc618 25287age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25288{
25289 struct dwarf2_per_cu_data *per_cu, **last_chain;
25290
25291 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25292 per_cu = dwarf2_per_objfile->read_in_chain;
25293 while (per_cu != NULL)
25294 {
25295 per_cu->cu->last_used ++;
b4f54984 25296 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25297 dwarf2_mark (per_cu->cu);
25298 per_cu = per_cu->cu->read_in_chain;
25299 }
25300
25301 per_cu = dwarf2_per_objfile->read_in_chain;
25302 last_chain = &dwarf2_per_objfile->read_in_chain;
25303 while (per_cu != NULL)
25304 {
25305 struct dwarf2_per_cu_data *next_cu;
25306
25307 next_cu = per_cu->cu->read_in_chain;
25308
25309 if (!per_cu->cu->mark)
25310 {
68dc6402 25311 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
25312 *last_chain = next_cu;
25313 }
25314 else
25315 last_chain = &per_cu->cu->read_in_chain;
25316
25317 per_cu = next_cu;
25318 }
25319}
25320
25321/* Remove a single compilation unit from the cache. */
25322
25323static void
dee91e82 25324free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25325{
25326 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25327 struct dwarf2_per_objfile *dwarf2_per_objfile
25328 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25329
25330 per_cu = dwarf2_per_objfile->read_in_chain;
25331 last_chain = &dwarf2_per_objfile->read_in_chain;
25332 while (per_cu != NULL)
25333 {
25334 struct dwarf2_per_cu_data *next_cu;
25335
25336 next_cu = per_cu->cu->read_in_chain;
25337
dee91e82 25338 if (per_cu == target_per_cu)
ae038cb0 25339 {
68dc6402 25340 free_heap_comp_unit (per_cu->cu);
dee91e82 25341 per_cu->cu = NULL;
ae038cb0
DJ
25342 *last_chain = next_cu;
25343 break;
25344 }
25345 else
25346 last_chain = &per_cu->cu->read_in_chain;
25347
25348 per_cu = next_cu;
25349 }
25350}
25351
fe3e1990
DJ
25352/* Release all extra memory associated with OBJFILE. */
25353
25354void
25355dwarf2_free_objfile (struct objfile *objfile)
25356{
ed2dc618
SM
25357 struct dwarf2_per_objfile *dwarf2_per_objfile
25358 = get_dwarf2_per_objfile (objfile);
fe3e1990
DJ
25359
25360 if (dwarf2_per_objfile == NULL)
25361 return;
25362
330cdd98 25363 dwarf2_per_objfile->~dwarf2_per_objfile ();
fe3e1990
DJ
25364}
25365
dee91e82
DE
25366/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25367 We store these in a hash table separate from the DIEs, and preserve them
25368 when the DIEs are flushed out of cache.
25369
25370 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25371 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25372 or the type may come from a DWO file. Furthermore, while it's more logical
25373 to use per_cu->section+offset, with Fission the section with the data is in
25374 the DWO file but we don't know that section at the point we need it.
25375 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25376 because we can enter the lookup routine, get_die_type_at_offset, from
25377 outside this file, and thus won't necessarily have PER_CU->cu.
25378 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25379
dee91e82 25380struct dwarf2_per_cu_offset_and_type
1c379e20 25381{
dee91e82 25382 const struct dwarf2_per_cu_data *per_cu;
9c541725 25383 sect_offset sect_off;
1c379e20
DJ
25384 struct type *type;
25385};
25386
dee91e82 25387/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25388
25389static hashval_t
dee91e82 25390per_cu_offset_and_type_hash (const void *item)
1c379e20 25391{
9a3c8263
SM
25392 const struct dwarf2_per_cu_offset_and_type *ofs
25393 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25394
9c541725 25395 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25396}
25397
dee91e82 25398/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25399
25400static int
dee91e82 25401per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25402{
9a3c8263
SM
25403 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25404 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25405 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25406 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25407
dee91e82 25408 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25409 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25410}
25411
25412/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25413 table if necessary. For convenience, return TYPE.
25414
25415 The DIEs reading must have careful ordering to:
25416 * Not cause infite loops trying to read in DIEs as a prerequisite for
25417 reading current DIE.
25418 * Not trying to dereference contents of still incompletely read in types
25419 while reading in other DIEs.
25420 * Enable referencing still incompletely read in types just by a pointer to
25421 the type without accessing its fields.
25422
25423 Therefore caller should follow these rules:
25424 * Try to fetch any prerequisite types we may need to build this DIE type
25425 before building the type and calling set_die_type.
e71ec853 25426 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25427 possible before fetching more types to complete the current type.
25428 * Make the type as complete as possible before fetching more types. */
1c379e20 25429
f792889a 25430static struct type *
1c379e20
DJ
25431set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25432{
518817b3
SM
25433 struct dwarf2_per_objfile *dwarf2_per_objfile
25434 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25435 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25436 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25437 struct attribute *attr;
25438 struct dynamic_prop prop;
1c379e20 25439
b4ba55a1
JB
25440 /* For Ada types, make sure that the gnat-specific data is always
25441 initialized (if not already set). There are a few types where
25442 we should not be doing so, because the type-specific area is
25443 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25444 where the type-specific area is used to store the floatformat).
25445 But this is not a problem, because the gnat-specific information
25446 is actually not needed for these types. */
25447 if (need_gnat_info (cu)
25448 && TYPE_CODE (type) != TYPE_CODE_FUNC
25449 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25450 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25451 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25452 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25453 && !HAVE_GNAT_AUX_INFO (type))
25454 INIT_GNAT_SPECIFIC (type);
25455
3f2f83dd
KB
25456 /* Read DW_AT_allocated and set in type. */
25457 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25458 if (attr_form_is_block (attr))
25459 {
25460 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25461 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
25462 }
25463 else if (attr != NULL)
25464 {
25465 complaint (&symfile_complaints,
9c541725
PA
25466 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
25467 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25468 to_underlying (die->sect_off));
3f2f83dd
KB
25469 }
25470
25471 /* Read DW_AT_associated and set in type. */
25472 attr = dwarf2_attr (die, DW_AT_associated, cu);
25473 if (attr_form_is_block (attr))
25474 {
25475 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25476 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
25477 }
25478 else if (attr != NULL)
25479 {
25480 complaint (&symfile_complaints,
9c541725
PA
25481 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
25482 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25483 to_underlying (die->sect_off));
3f2f83dd
KB
25484 }
25485
3cdcd0ce
JB
25486 /* Read DW_AT_data_location and set in type. */
25487 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25488 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 25489 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 25490
dee91e82 25491 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25492 {
dee91e82
DE
25493 dwarf2_per_objfile->die_type_hash =
25494 htab_create_alloc_ex (127,
25495 per_cu_offset_and_type_hash,
25496 per_cu_offset_and_type_eq,
25497 NULL,
25498 &objfile->objfile_obstack,
25499 hashtab_obstack_allocate,
25500 dummy_obstack_deallocate);
f792889a 25501 }
1c379e20 25502
dee91e82 25503 ofs.per_cu = cu->per_cu;
9c541725 25504 ofs.sect_off = die->sect_off;
1c379e20 25505 ofs.type = type;
dee91e82
DE
25506 slot = (struct dwarf2_per_cu_offset_and_type **)
25507 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
25508 if (*slot)
25509 complaint (&symfile_complaints,
25510 _("A problem internal to GDB: DIE 0x%x has type already set"),
9c541725 25511 to_underlying (die->sect_off));
8d749320
SM
25512 *slot = XOBNEW (&objfile->objfile_obstack,
25513 struct dwarf2_per_cu_offset_and_type);
1c379e20 25514 **slot = ofs;
f792889a 25515 return type;
1c379e20
DJ
25516}
25517
9c541725 25518/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25519 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25520
25521static struct type *
9c541725 25522get_die_type_at_offset (sect_offset sect_off,
673bfd45 25523 struct dwarf2_per_cu_data *per_cu)
1c379e20 25524{
dee91e82 25525 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25526 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25527
dee91e82 25528 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25529 return NULL;
1c379e20 25530
dee91e82 25531 ofs.per_cu = per_cu;
9c541725 25532 ofs.sect_off = sect_off;
9a3c8263
SM
25533 slot = ((struct dwarf2_per_cu_offset_and_type *)
25534 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25535 if (slot)
25536 return slot->type;
25537 else
25538 return NULL;
25539}
25540
02142a6c 25541/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25542 or return NULL if DIE does not have a saved type. */
25543
25544static struct type *
25545get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25546{
9c541725 25547 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25548}
25549
10b3939b
DJ
25550/* Add a dependence relationship from CU to REF_PER_CU. */
25551
25552static void
25553dwarf2_add_dependence (struct dwarf2_cu *cu,
25554 struct dwarf2_per_cu_data *ref_per_cu)
25555{
25556 void **slot;
25557
25558 if (cu->dependencies == NULL)
25559 cu->dependencies
25560 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25561 NULL, &cu->comp_unit_obstack,
25562 hashtab_obstack_allocate,
25563 dummy_obstack_deallocate);
25564
25565 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25566 if (*slot == NULL)
25567 *slot = ref_per_cu;
25568}
1c379e20 25569
f504f079
DE
25570/* Subroutine of dwarf2_mark to pass to htab_traverse.
25571 Set the mark field in every compilation unit in the
ae038cb0
DJ
25572 cache that we must keep because we are keeping CU. */
25573
10b3939b
DJ
25574static int
25575dwarf2_mark_helper (void **slot, void *data)
25576{
25577 struct dwarf2_per_cu_data *per_cu;
25578
25579 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25580
25581 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25582 reading of the chain. As such dependencies remain valid it is not much
25583 useful to track and undo them during QUIT cleanups. */
25584 if (per_cu->cu == NULL)
25585 return 1;
25586
10b3939b
DJ
25587 if (per_cu->cu->mark)
25588 return 1;
25589 per_cu->cu->mark = 1;
25590
25591 if (per_cu->cu->dependencies != NULL)
25592 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25593
25594 return 1;
25595}
25596
f504f079
DE
25597/* Set the mark field in CU and in every other compilation unit in the
25598 cache that we must keep because we are keeping CU. */
25599
ae038cb0
DJ
25600static void
25601dwarf2_mark (struct dwarf2_cu *cu)
25602{
25603 if (cu->mark)
25604 return;
25605 cu->mark = 1;
10b3939b
DJ
25606 if (cu->dependencies != NULL)
25607 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25608}
25609
25610static void
25611dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25612{
25613 while (per_cu)
25614 {
25615 per_cu->cu->mark = 0;
25616 per_cu = per_cu->cu->read_in_chain;
25617 }
72bf9492
DJ
25618}
25619
72bf9492
DJ
25620/* Trivial hash function for partial_die_info: the hash value of a DIE
25621 is its offset in .debug_info for this objfile. */
25622
25623static hashval_t
25624partial_die_hash (const void *item)
25625{
9a3c8263
SM
25626 const struct partial_die_info *part_die
25627 = (const struct partial_die_info *) item;
9a619af0 25628
9c541725 25629 return to_underlying (part_die->sect_off);
72bf9492
DJ
25630}
25631
25632/* Trivial comparison function for partial_die_info structures: two DIEs
25633 are equal if they have the same offset. */
25634
25635static int
25636partial_die_eq (const void *item_lhs, const void *item_rhs)
25637{
9a3c8263
SM
25638 const struct partial_die_info *part_die_lhs
25639 = (const struct partial_die_info *) item_lhs;
25640 const struct partial_die_info *part_die_rhs
25641 = (const struct partial_die_info *) item_rhs;
9a619af0 25642
9c541725 25643 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25644}
25645
b4f54984
DE
25646static struct cmd_list_element *set_dwarf_cmdlist;
25647static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25648
25649static void
981a3fb3 25650set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25651{
b4f54984 25652 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25653 gdb_stdout);
ae038cb0
DJ
25654}
25655
25656static void
981a3fb3 25657show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25658{
b4f54984 25659 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25660}
25661
4bf44c1c 25662/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
25663
25664static void
c1bd65d0 25665dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 25666{
9a3c8263 25667 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 25668 int ix;
8b70b953 25669
59b0c7c1
JB
25670 for (ix = 0; ix < data->n_comp_units; ++ix)
25671 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 25672
59b0c7c1 25673 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 25674 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
25675 data->all_type_units[ix]->per_cu.imported_symtabs);
25676 xfree (data->all_type_units);
95554aad 25677
8b70b953 25678 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
25679
25680 if (data->dwo_files)
25681 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
25682 if (data->dwp_file)
25683 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
25684
25685 if (data->dwz_file && data->dwz_file->dwz_bfd)
25686 gdb_bfd_unref (data->dwz_file->dwz_bfd);
3f563c84
PA
25687
25688 if (data->index_table != NULL)
25689 data->index_table->~mapped_index ();
9291a0cd
TT
25690}
25691
25692\f
ae2de4f8 25693/* The "save gdb-index" command. */
9291a0cd 25694
437afbb8
JK
25695/* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25696 error checking. */
25697
25698static void
25699file_write (FILE *file, const void *data, size_t size)
25700{
25701 if (fwrite (data, 1, size, file) != size)
25702 error (_("couldn't data write to file"));
25703}
25704
25705/* Write the contents of VEC to FILE, with error checking. */
25706
25707template<typename Elem, typename Alloc>
25708static void
25709file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25710{
25711 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25712}
25713
bc8f2430
JK
25714/* In-memory buffer to prepare data to be written later to a file. */
25715class data_buf
9291a0cd 25716{
bc8f2430 25717public:
bc8f2430
JK
25718 /* Copy DATA to the end of the buffer. */
25719 template<typename T>
25720 void append_data (const T &data)
25721 {
25722 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25723 reinterpret_cast<const gdb_byte *> (&data + 1),
c2f134ac 25724 grow (sizeof (data)));
bc8f2430 25725 }
b89be57b 25726
c2f134ac
PA
25727 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25728 terminating zero is appended too. */
bc8f2430
JK
25729 void append_cstr0 (const char *cstr)
25730 {
25731 const size_t size = strlen (cstr) + 1;
c2f134ac
PA
25732 std::copy (cstr, cstr + size, grow (size));
25733 }
25734
437afbb8
JK
25735 /* Store INPUT as ULEB128 to the end of buffer. */
25736 void append_unsigned_leb128 (ULONGEST input)
25737 {
25738 for (;;)
25739 {
25740 gdb_byte output = input & 0x7f;
25741 input >>= 7;
25742 if (input)
25743 output |= 0x80;
25744 append_data (output);
25745 if (input == 0)
25746 break;
25747 }
25748 }
25749
c2f134ac
PA
25750 /* Accept a host-format integer in VAL and append it to the buffer
25751 as a target-format integer which is LEN bytes long. */
25752 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25753 {
25754 ::store_unsigned_integer (grow (len), len, byte_order, val);
bc8f2430 25755 }
9291a0cd 25756
bc8f2430
JK
25757 /* Return the size of the buffer. */
25758 size_t size () const
25759 {
25760 return m_vec.size ();
25761 }
25762
437afbb8
JK
25763 /* Return true iff the buffer is empty. */
25764 bool empty () const
25765 {
25766 return m_vec.empty ();
25767 }
25768
bc8f2430
JK
25769 /* Write the buffer to FILE. */
25770 void file_write (FILE *file) const
25771 {
437afbb8 25772 ::file_write (file, m_vec);
bc8f2430
JK
25773 }
25774
25775private:
c2f134ac
PA
25776 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25777 the start of the new block. */
25778 gdb_byte *grow (size_t size)
25779 {
25780 m_vec.resize (m_vec.size () + size);
25781 return &*m_vec.end () - size;
25782 }
25783
d5722aa2 25784 gdb::byte_vector m_vec;
bc8f2430 25785};
9291a0cd
TT
25786
25787/* An entry in the symbol table. */
25788struct symtab_index_entry
25789{
25790 /* The name of the symbol. */
25791 const char *name;
25792 /* The offset of the name in the constant pool. */
25793 offset_type index_offset;
25794 /* A sorted vector of the indices of all the CUs that hold an object
25795 of this name. */
bc8f2430 25796 std::vector<offset_type> cu_indices;
9291a0cd
TT
25797};
25798
25799/* The symbol table. This is a power-of-2-sized hash table. */
25800struct mapped_symtab
25801{
bc8f2430
JK
25802 mapped_symtab ()
25803 {
25804 data.resize (1024);
25805 }
b89be57b 25806
bc8f2430 25807 offset_type n_elements = 0;
4b76cda9 25808 std::vector<symtab_index_entry> data;
bc8f2430 25809};
9291a0cd 25810
bc8f2430 25811/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
559a7a62
JK
25812 the slot.
25813
25814 Function is used only during write_hash_table so no index format backward
25815 compatibility is needed. */
b89be57b 25816
4b76cda9 25817static symtab_index_entry &
9291a0cd
TT
25818find_slot (struct mapped_symtab *symtab, const char *name)
25819{
559a7a62 25820 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd 25821
bc8f2430
JK
25822 index = hash & (symtab->data.size () - 1);
25823 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
9291a0cd
TT
25824
25825 for (;;)
25826 {
4b76cda9
PA
25827 if (symtab->data[index].name == NULL
25828 || strcmp (name, symtab->data[index].name) == 0)
bc8f2430
JK
25829 return symtab->data[index];
25830 index = (index + step) & (symtab->data.size () - 1);
9291a0cd
TT
25831 }
25832}
25833
25834/* Expand SYMTAB's hash table. */
b89be57b 25835
9291a0cd
TT
25836static void
25837hash_expand (struct mapped_symtab *symtab)
25838{
bc8f2430 25839 auto old_entries = std::move (symtab->data);
9291a0cd 25840
bc8f2430
JK
25841 symtab->data.clear ();
25842 symtab->data.resize (old_entries.size () * 2);
9291a0cd 25843
bc8f2430 25844 for (auto &it : old_entries)
4b76cda9 25845 if (it.name != NULL)
bc8f2430 25846 {
4b76cda9 25847 auto &ref = find_slot (symtab, it.name);
bc8f2430
JK
25848 ref = std::move (it);
25849 }
9291a0cd
TT
25850}
25851
156942c7
DE
25852/* Add an entry to SYMTAB. NAME is the name of the symbol.
25853 CU_INDEX is the index of the CU in which the symbol appears.
25854 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 25855
9291a0cd
TT
25856static void
25857add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 25858 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
25859 offset_type cu_index)
25860{
156942c7 25861 offset_type cu_index_and_attrs;
9291a0cd
TT
25862
25863 ++symtab->n_elements;
bc8f2430 25864 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
9291a0cd
TT
25865 hash_expand (symtab);
25866
4b76cda9
PA
25867 symtab_index_entry &slot = find_slot (symtab, name);
25868 if (slot.name == NULL)
9291a0cd 25869 {
4b76cda9 25870 slot.name = name;
156942c7 25871 /* index_offset is set later. */
9291a0cd 25872 }
156942c7
DE
25873
25874 cu_index_and_attrs = 0;
25875 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
25876 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
25877 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
25878
25879 /* We don't want to record an index value twice as we want to avoid the
25880 duplication.
25881 We process all global symbols and then all static symbols
25882 (which would allow us to avoid the duplication by only having to check
25883 the last entry pushed), but a symbol could have multiple kinds in one CU.
25884 To keep things simple we don't worry about the duplication here and
25885 sort and uniqufy the list after we've processed all symbols. */
4b76cda9 25886 slot.cu_indices.push_back (cu_index_and_attrs);
156942c7
DE
25887}
25888
25889/* Sort and remove duplicates of all symbols' cu_indices lists. */
25890
25891static void
25892uniquify_cu_indices (struct mapped_symtab *symtab)
25893{
4b76cda9 25894 for (auto &entry : symtab->data)
156942c7 25895 {
4b76cda9 25896 if (entry.name != NULL && !entry.cu_indices.empty ())
156942c7 25897 {
4b76cda9 25898 auto &cu_indices = entry.cu_indices;
6fd931f2
PA
25899 std::sort (cu_indices.begin (), cu_indices.end ());
25900 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
25901 cu_indices.erase (from, cu_indices.end ());
156942c7
DE
25902 }
25903 }
9291a0cd
TT
25904}
25905
bc8f2430
JK
25906/* A form of 'const char *' suitable for container keys. Only the
25907 pointer is stored. The strings themselves are compared, not the
25908 pointers. */
25909class c_str_view
9291a0cd 25910{
bc8f2430
JK
25911public:
25912 c_str_view (const char *cstr)
25913 : m_cstr (cstr)
25914 {}
9291a0cd 25915
bc8f2430
JK
25916 bool operator== (const c_str_view &other) const
25917 {
25918 return strcmp (m_cstr, other.m_cstr) == 0;
25919 }
9291a0cd 25920
437afbb8
JK
25921 /* Return the underlying C string. Note, the returned string is
25922 only a reference with lifetime of this object. */
25923 const char *c_str () const
25924 {
25925 return m_cstr;
25926 }
25927
bc8f2430
JK
25928private:
25929 friend class c_str_view_hasher;
25930 const char *const m_cstr;
25931};
9291a0cd 25932
bc8f2430
JK
25933/* A std::unordered_map::hasher for c_str_view that uses the right
25934 hash function for strings in a mapped index. */
25935class c_str_view_hasher
25936{
25937public:
25938 size_t operator () (const c_str_view &x) const
25939 {
25940 return mapped_index_string_hash (INT_MAX, x.m_cstr);
25941 }
25942};
b89be57b 25943
bc8f2430
JK
25944/* A std::unordered_map::hasher for std::vector<>. */
25945template<typename T>
25946class vector_hasher
9291a0cd 25947{
bc8f2430
JK
25948public:
25949 size_t operator () (const std::vector<T> &key) const
25950 {
25951 return iterative_hash (key.data (),
25952 sizeof (key.front ()) * key.size (), 0);
25953 }
25954};
9291a0cd 25955
bc8f2430
JK
25956/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
25957 constant pool entries going into the data buffer CPOOL. */
3876f04e 25958
bc8f2430
JK
25959static void
25960write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
25961{
25962 {
25963 /* Elements are sorted vectors of the indices of all the CUs that
25964 hold an object of this name. */
25965 std::unordered_map<std::vector<offset_type>, offset_type,
25966 vector_hasher<offset_type>>
25967 symbol_hash_table;
25968
25969 /* We add all the index vectors to the constant pool first, to
25970 ensure alignment is ok. */
4b76cda9 25971 for (symtab_index_entry &entry : symtab->data)
bc8f2430 25972 {
4b76cda9 25973 if (entry.name == NULL)
bc8f2430 25974 continue;
4b76cda9 25975 gdb_assert (entry.index_offset == 0);
70a1152b
PA
25976
25977 /* Finding before inserting is faster than always trying to
25978 insert, because inserting always allocates a node, does the
25979 lookup, and then destroys the new node if another node
25980 already had the same key. C++17 try_emplace will avoid
25981 this. */
25982 const auto found
4b76cda9 25983 = symbol_hash_table.find (entry.cu_indices);
70a1152b
PA
25984 if (found != symbol_hash_table.end ())
25985 {
4b76cda9 25986 entry.index_offset = found->second;
70a1152b
PA
25987 continue;
25988 }
25989
4b76cda9
PA
25990 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
25991 entry.index_offset = cpool.size ();
25992 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
25993 for (const auto index : entry.cu_indices)
25994 cpool.append_data (MAYBE_SWAP (index));
bc8f2430
JK
25995 }
25996 }
9291a0cd
TT
25997
25998 /* Now write out the hash table. */
bc8f2430 25999 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
4b76cda9 26000 for (const auto &entry : symtab->data)
9291a0cd
TT
26001 {
26002 offset_type str_off, vec_off;
26003
4b76cda9 26004 if (entry.name != NULL)
9291a0cd 26005 {
4b76cda9 26006 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
bc8f2430 26007 if (insertpair.second)
4b76cda9 26008 cpool.append_cstr0 (entry.name);
bc8f2430 26009 str_off = insertpair.first->second;
4b76cda9 26010 vec_off = entry.index_offset;
9291a0cd
TT
26011 }
26012 else
26013 {
26014 /* While 0 is a valid constant pool index, it is not valid
26015 to have 0 for both offsets. */
26016 str_off = 0;
26017 vec_off = 0;
26018 }
26019
bc8f2430
JK
26020 output.append_data (MAYBE_SWAP (str_off));
26021 output.append_data (MAYBE_SWAP (vec_off));
9291a0cd 26022 }
9291a0cd
TT
26023}
26024
bc8f2430 26025typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
0a5429f6
DE
26026
26027/* Helper struct for building the address table. */
26028struct addrmap_index_data
26029{
bc8f2430
JK
26030 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
26031 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
26032 {}
26033
0a5429f6 26034 struct objfile *objfile;
bc8f2430
JK
26035 data_buf &addr_vec;
26036 psym_index_map &cu_index_htab;
0a5429f6
DE
26037
26038 /* Non-zero if the previous_* fields are valid.
26039 We can't write an entry until we see the next entry (since it is only then
26040 that we know the end of the entry). */
26041 int previous_valid;
26042 /* Index of the CU in the table of all CUs in the index file. */
26043 unsigned int previous_cu_index;
0963b4bd 26044 /* Start address of the CU. */
0a5429f6
DE
26045 CORE_ADDR previous_cu_start;
26046};
26047
bc8f2430 26048/* Write an address entry to ADDR_VEC. */
b89be57b 26049
9291a0cd 26050static void
bc8f2430 26051add_address_entry (struct objfile *objfile, data_buf &addr_vec,
0a5429f6 26052 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 26053{
9291a0cd
TT
26054 CORE_ADDR baseaddr;
26055
26056 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
26057
c2f134ac
PA
26058 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
26059 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
bc8f2430 26060 addr_vec.append_data (MAYBE_SWAP (cu_index));
0a5429f6
DE
26061}
26062
26063/* Worker function for traversing an addrmap to build the address table. */
26064
26065static int
26066add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
26067{
9a3c8263
SM
26068 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
26069 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
26070
26071 if (data->previous_valid)
bc8f2430 26072 add_address_entry (data->objfile, data->addr_vec,
0a5429f6
DE
26073 data->previous_cu_start, start_addr,
26074 data->previous_cu_index);
26075
26076 data->previous_cu_start = start_addr;
26077 if (pst != NULL)
26078 {
bc8f2430
JK
26079 const auto it = data->cu_index_htab.find (pst);
26080 gdb_assert (it != data->cu_index_htab.cend ());
26081 data->previous_cu_index = it->second;
0a5429f6
DE
26082 data->previous_valid = 1;
26083 }
26084 else
bc8f2430 26085 data->previous_valid = 0;
0a5429f6
DE
26086
26087 return 0;
26088}
26089
bc8f2430 26090/* Write OBJFILE's address map to ADDR_VEC.
0a5429f6
DE
26091 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
26092 in the index file. */
26093
26094static void
bc8f2430
JK
26095write_address_map (struct objfile *objfile, data_buf &addr_vec,
26096 psym_index_map &cu_index_htab)
0a5429f6 26097{
bc8f2430 26098 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
0a5429f6
DE
26099
26100 /* When writing the address table, we have to cope with the fact that
26101 the addrmap iterator only provides the start of a region; we have to
26102 wait until the next invocation to get the start of the next region. */
26103
26104 addrmap_index_data.objfile = objfile;
0a5429f6
DE
26105 addrmap_index_data.previous_valid = 0;
26106
26107 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
26108 &addrmap_index_data);
26109
26110 /* It's highly unlikely the last entry (end address = 0xff...ff)
26111 is valid, but we should still handle it.
26112 The end address is recorded as the start of the next region, but that
26113 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
26114 anyway. */
26115 if (addrmap_index_data.previous_valid)
bc8f2430 26116 add_address_entry (objfile, addr_vec,
0a5429f6
DE
26117 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
26118 addrmap_index_data.previous_cu_index);
9291a0cd
TT
26119}
26120
156942c7
DE
26121/* Return the symbol kind of PSYM. */
26122
26123static gdb_index_symbol_kind
26124symbol_kind (struct partial_symbol *psym)
26125{
26126 domain_enum domain = PSYMBOL_DOMAIN (psym);
26127 enum address_class aclass = PSYMBOL_CLASS (psym);
26128
26129 switch (domain)
26130 {
26131 case VAR_DOMAIN:
26132 switch (aclass)
26133 {
26134 case LOC_BLOCK:
26135 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
26136 case LOC_TYPEDEF:
26137 return GDB_INDEX_SYMBOL_KIND_TYPE;
26138 case LOC_COMPUTED:
26139 case LOC_CONST_BYTES:
26140 case LOC_OPTIMIZED_OUT:
26141 case LOC_STATIC:
26142 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26143 case LOC_CONST:
26144 /* Note: It's currently impossible to recognize psyms as enum values
26145 short of reading the type info. For now punt. */
26146 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26147 default:
26148 /* There are other LOC_FOO values that one might want to classify
26149 as variables, but dwarf2read.c doesn't currently use them. */
26150 return GDB_INDEX_SYMBOL_KIND_OTHER;
26151 }
26152 case STRUCT_DOMAIN:
26153 return GDB_INDEX_SYMBOL_KIND_TYPE;
26154 default:
26155 return GDB_INDEX_SYMBOL_KIND_OTHER;
26156 }
26157}
26158
9291a0cd 26159/* Add a list of partial symbols to SYMTAB. */
b89be57b 26160
9291a0cd
TT
26161static void
26162write_psymbols (struct mapped_symtab *symtab,
bc8f2430 26163 std::unordered_set<partial_symbol *> &psyms_seen,
9291a0cd
TT
26164 struct partial_symbol **psymp,
26165 int count,
987d643c
TT
26166 offset_type cu_index,
26167 int is_static)
9291a0cd
TT
26168{
26169 for (; count-- > 0; ++psymp)
26170 {
156942c7 26171 struct partial_symbol *psym = *psymp;
987d643c 26172
156942c7 26173 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 26174 error (_("Ada is not currently supported by the index"));
987d643c 26175
987d643c 26176 /* Only add a given psymbol once. */
bc8f2430 26177 if (psyms_seen.insert (psym).second)
987d643c 26178 {
156942c7
DE
26179 gdb_index_symbol_kind kind = symbol_kind (psym);
26180
156942c7
DE
26181 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
26182 is_static, kind, cu_index);
987d643c 26183 }
9291a0cd
TT
26184 }
26185}
26186
1fd400ff
TT
26187/* A helper struct used when iterating over debug_types. */
26188struct signatured_type_index_data
26189{
bc8f2430
JK
26190 signatured_type_index_data (data_buf &types_list_,
26191 std::unordered_set<partial_symbol *> &psyms_seen_)
26192 : types_list (types_list_), psyms_seen (psyms_seen_)
26193 {}
26194
1fd400ff
TT
26195 struct objfile *objfile;
26196 struct mapped_symtab *symtab;
bc8f2430
JK
26197 data_buf &types_list;
26198 std::unordered_set<partial_symbol *> &psyms_seen;
1fd400ff
TT
26199 int cu_index;
26200};
26201
26202/* A helper function that writes a single signatured_type to an
26203 obstack. */
b89be57b 26204
1fd400ff
TT
26205static int
26206write_one_signatured_type (void **slot, void *d)
26207{
9a3c8263
SM
26208 struct signatured_type_index_data *info
26209 = (struct signatured_type_index_data *) d;
1fd400ff 26210 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 26211 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
26212
26213 write_psymbols (info->symtab,
987d643c 26214 info->psyms_seen,
af5bf4ad 26215 &info->objfile->global_psymbols[psymtab->globals_offset],
987d643c
TT
26216 psymtab->n_global_syms, info->cu_index,
26217 0);
1fd400ff 26218 write_psymbols (info->symtab,
987d643c 26219 info->psyms_seen,
af5bf4ad 26220 &info->objfile->static_psymbols[psymtab->statics_offset],
987d643c
TT
26221 psymtab->n_static_syms, info->cu_index,
26222 1);
1fd400ff 26223
c2f134ac
PA
26224 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26225 to_underlying (entry->per_cu.sect_off));
26226 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26227 to_underlying (entry->type_offset_in_tu));
26228 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
1fd400ff
TT
26229
26230 ++info->cu_index;
26231
26232 return 1;
26233}
26234
e8f8bcb3
PA
26235/* Recurse into all "included" dependencies and count their symbols as
26236 if they appeared in this psymtab. */
26237
26238static void
26239recursively_count_psymbols (struct partial_symtab *psymtab,
26240 size_t &psyms_seen)
26241{
26242 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26243 if (psymtab->dependencies[i]->user != NULL)
26244 recursively_count_psymbols (psymtab->dependencies[i],
26245 psyms_seen);
26246
26247 psyms_seen += psymtab->n_global_syms;
26248 psyms_seen += psymtab->n_static_syms;
26249}
26250
95554aad
TT
26251/* Recurse into all "included" dependencies and write their symbols as
26252 if they appeared in this psymtab. */
26253
26254static void
26255recursively_write_psymbols (struct objfile *objfile,
26256 struct partial_symtab *psymtab,
26257 struct mapped_symtab *symtab,
bc8f2430 26258 std::unordered_set<partial_symbol *> &psyms_seen,
95554aad
TT
26259 offset_type cu_index)
26260{
26261 int i;
26262
26263 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26264 if (psymtab->dependencies[i]->user != NULL)
26265 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26266 symtab, psyms_seen, cu_index);
26267
26268 write_psymbols (symtab,
26269 psyms_seen,
af5bf4ad 26270 &objfile->global_psymbols[psymtab->globals_offset],
95554aad
TT
26271 psymtab->n_global_syms, cu_index,
26272 0);
26273 write_psymbols (symtab,
26274 psyms_seen,
af5bf4ad 26275 &objfile->static_psymbols[psymtab->statics_offset],
95554aad
TT
26276 psymtab->n_static_syms, cu_index,
26277 1);
26278}
26279
437afbb8
JK
26280/* DWARF-5 .debug_names builder. */
26281class debug_names
26282{
26283public:
ed2dc618
SM
26284 debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64,
26285 bfd_endian dwarf5_byte_order)
437afbb8
JK
26286 : m_dwarf5_byte_order (dwarf5_byte_order),
26287 m_dwarf32 (dwarf5_byte_order),
26288 m_dwarf64 (dwarf5_byte_order),
26289 m_dwarf (is_dwarf64
26290 ? static_cast<dwarf &> (m_dwarf64)
26291 : static_cast<dwarf &> (m_dwarf32)),
26292 m_name_table_string_offs (m_dwarf.name_table_string_offs),
ed2dc618
SM
26293 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
26294 m_debugstrlookup (dwarf2_per_objfile)
437afbb8 26295 {}
9291a0cd 26296
8af5c486
JK
26297 int dwarf5_offset_size () const
26298 {
26299 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26300 return dwarf5_is_dwarf64 ? 8 : 4;
26301 }
26302
26303 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26304 enum class unit_kind { cu, tu };
26305
437afbb8 26306 /* Insert one symbol. */
8af5c486
JK
26307 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26308 unit_kind kind)
437afbb8
JK
26309 {
26310 const int dwarf_tag = psymbol_tag (psym);
26311 if (dwarf_tag == 0)
26312 return;
26313 const char *const name = SYMBOL_SEARCH_NAME (psym);
26314 const auto insertpair
26315 = m_name_to_value_set.emplace (c_str_view (name),
26316 std::set<symbol_value> ());
26317 std::set<symbol_value> &value_set = insertpair.first->second;
8af5c486 26318 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
437afbb8 26319 }
9291a0cd 26320
437afbb8
JK
26321 /* Build all the tables. All symbols must be already inserted.
26322 This function does not call file_write, caller has to do it
26323 afterwards. */
26324 void build ()
26325 {
26326 /* Verify the build method has not be called twice. */
26327 gdb_assert (m_abbrev_table.empty ());
26328 const size_t name_count = m_name_to_value_set.size ();
26329 m_bucket_table.resize
26330 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26331 m_hash_table.reserve (name_count);
26332 m_name_table_string_offs.reserve (name_count);
26333 m_name_table_entry_offs.reserve (name_count);
26334
26335 /* Map each hash of symbol to its name and value. */
26336 struct hash_it_pair
26337 {
26338 uint32_t hash;
26339 decltype (m_name_to_value_set)::const_iterator it;
26340 };
26341 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26342 bucket_hash.resize (m_bucket_table.size ());
26343 for (decltype (m_name_to_value_set)::const_iterator it
26344 = m_name_to_value_set.cbegin ();
26345 it != m_name_to_value_set.cend ();
26346 ++it)
26347 {
26348 const char *const name = it->first.c_str ();
26349 const uint32_t hash = dwarf5_djb_hash (name);
26350 hash_it_pair hashitpair;
26351 hashitpair.hash = hash;
26352 hashitpair.it = it;
26353 auto &slot = bucket_hash[hash % bucket_hash.size()];
26354 slot.push_front (std::move (hashitpair));
26355 }
26356 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26357 {
26358 const std::forward_list<hash_it_pair> &hashitlist
26359 = bucket_hash[bucket_ix];
26360 if (hashitlist.empty ())
26361 continue;
26362 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26363 /* The hashes array is indexed starting at 1. */
26364 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26365 sizeof (bucket_slot), m_dwarf5_byte_order,
26366 m_hash_table.size () + 1);
26367 for (const hash_it_pair &hashitpair : hashitlist)
26368 {
26369 m_hash_table.push_back (0);
26370 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26371 (&m_hash_table.back ()),
26372 sizeof (m_hash_table.back ()),
26373 m_dwarf5_byte_order, hashitpair.hash);
26374 const c_str_view &name = hashitpair.it->first;
26375 const std::set<symbol_value> &value_set = hashitpair.it->second;
26376 m_name_table_string_offs.push_back_reorder
26377 (m_debugstrlookup.lookup (name.c_str ()));
26378 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26379 gdb_assert (!value_set.empty ());
26380 for (const symbol_value &value : value_set)
26381 {
26382 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
8af5c486
JK
26383 value.is_static,
26384 value.kind)];
437afbb8
JK
26385 if (idx == 0)
26386 {
26387 idx = m_idx_next++;
26388 m_abbrev_table.append_unsigned_leb128 (idx);
26389 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
8af5c486
JK
26390 m_abbrev_table.append_unsigned_leb128
26391 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26392 : DW_IDX_type_unit);
437afbb8
JK
26393 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26394 m_abbrev_table.append_unsigned_leb128 (value.is_static
26395 ? DW_IDX_GNU_internal
26396 : DW_IDX_GNU_external);
26397 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26398
26399 /* Terminate attributes list. */
26400 m_abbrev_table.append_unsigned_leb128 (0);
26401 m_abbrev_table.append_unsigned_leb128 (0);
26402 }
9291a0cd 26403
437afbb8
JK
26404 m_entry_pool.append_unsigned_leb128 (idx);
26405 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26406 }
9291a0cd 26407
437afbb8
JK
26408 /* Terminate the list of CUs. */
26409 m_entry_pool.append_unsigned_leb128 (0);
26410 }
26411 }
26412 gdb_assert (m_hash_table.size () == name_count);
987d643c 26413
437afbb8
JK
26414 /* Terminate tags list. */
26415 m_abbrev_table.append_unsigned_leb128 (0);
26416 }
0a5429f6 26417
437afbb8
JK
26418 /* Return .debug_names bucket count. This must be called only after
26419 calling the build method. */
26420 uint32_t bucket_count () const
26421 {
26422 /* Verify the build method has been already called. */
26423 gdb_assert (!m_abbrev_table.empty ());
26424 const uint32_t retval = m_bucket_table.size ();
26425
26426 /* Check for overflow. */
26427 gdb_assert (retval == m_bucket_table.size ());
26428 return retval;
26429 }
26430
26431 /* Return .debug_names names count. This must be called only after
26432 calling the build method. */
26433 uint32_t name_count () const
26434 {
26435 /* Verify the build method has been already called. */
26436 gdb_assert (!m_abbrev_table.empty ());
26437 const uint32_t retval = m_hash_table.size ();
26438
26439 /* Check for overflow. */
26440 gdb_assert (retval == m_hash_table.size ());
26441 return retval;
26442 }
26443
26444 /* Return number of bytes of .debug_names abbreviation table. This
26445 must be called only after calling the build method. */
26446 uint32_t abbrev_table_bytes () const
26447 {
26448 gdb_assert (!m_abbrev_table.empty ());
26449 return m_abbrev_table.size ();
26450 }
26451
26452 /* Recurse into all "included" dependencies and store their symbols
26453 as if they appeared in this psymtab. */
26454 void recursively_write_psymbols
26455 (struct objfile *objfile,
26456 struct partial_symtab *psymtab,
26457 std::unordered_set<partial_symbol *> &psyms_seen,
26458 int cu_index)
26459 {
26460 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26461 if (psymtab->dependencies[i]->user != NULL)
26462 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26463 psyms_seen, cu_index);
26464
26465 write_psymbols (psyms_seen,
26466 &objfile->global_psymbols[psymtab->globals_offset],
8af5c486 26467 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
437afbb8
JK
26468 write_psymbols (psyms_seen,
26469 &objfile->static_psymbols[psymtab->statics_offset],
8af5c486 26470 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
437afbb8
JK
26471 }
26472
26473 /* Return number of bytes the .debug_names section will have. This
26474 must be called only after calling the build method. */
26475 size_t bytes () const
26476 {
26477 /* Verify the build method has been already called. */
26478 gdb_assert (!m_abbrev_table.empty ());
26479 size_t expected_bytes = 0;
26480 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26481 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26482 expected_bytes += m_name_table_string_offs.bytes ();
26483 expected_bytes += m_name_table_entry_offs.bytes ();
26484 expected_bytes += m_abbrev_table.size ();
26485 expected_bytes += m_entry_pool.size ();
26486 return expected_bytes;
26487 }
26488
26489 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26490 FILE_STR. This must be called only after calling the build
26491 method. */
26492 void file_write (FILE *file_names, FILE *file_str) const
26493 {
26494 /* Verify the build method has been already called. */
26495 gdb_assert (!m_abbrev_table.empty ());
26496 ::file_write (file_names, m_bucket_table);
26497 ::file_write (file_names, m_hash_table);
26498 m_name_table_string_offs.file_write (file_names);
26499 m_name_table_entry_offs.file_write (file_names);
26500 m_abbrev_table.file_write (file_names);
26501 m_entry_pool.file_write (file_names);
26502 m_debugstrlookup.file_write (file_str);
26503 }
26504
8af5c486
JK
26505 /* A helper user data for write_one_signatured_type. */
26506 class write_one_signatured_type_data
26507 {
26508 public:
26509 write_one_signatured_type_data (debug_names &nametable_,
26510 signatured_type_index_data &&info_)
26511 : nametable (nametable_), info (std::move (info_))
26512 {}
26513 debug_names &nametable;
26514 struct signatured_type_index_data info;
26515 };
26516
26517 /* A helper function to pass write_one_signatured_type to
26518 htab_traverse_noresize. */
26519 static int
26520 write_one_signatured_type (void **slot, void *d)
26521 {
26522 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26523 struct signatured_type_index_data *info = &data->info;
26524 struct signatured_type *entry = (struct signatured_type *) *slot;
26525
26526 data->nametable.write_one_signatured_type (entry, info);
26527
26528 return 1;
26529 }
26530
437afbb8
JK
26531private:
26532
26533 /* Storage for symbol names mapping them to their .debug_str section
26534 offsets. */
26535 class debug_str_lookup
26536 {
26537 public:
26538
26539 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26540 All .debug_str section strings are automatically stored. */
ed2dc618
SM
26541 debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile)
26542 : m_abfd (dwarf2_per_objfile->objfile->obfd),
26543 m_dwarf2_per_objfile (dwarf2_per_objfile)
437afbb8
JK
26544 {
26545 dwarf2_read_section (dwarf2_per_objfile->objfile,
26546 &dwarf2_per_objfile->str);
26547 if (dwarf2_per_objfile->str.buffer == NULL)
26548 return;
26549 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26550 data < (dwarf2_per_objfile->str.buffer
26551 + dwarf2_per_objfile->str.size);)
26552 {
26553 const char *const s = reinterpret_cast<const char *> (data);
26554 const auto insertpair
26555 = m_str_table.emplace (c_str_view (s),
26556 data - dwarf2_per_objfile->str.buffer);
26557 if (!insertpair.second)
26558 complaint (&symfile_complaints,
26559 _("Duplicate string \"%s\" in "
26560 ".debug_str section [in module %s]"),
26561 s, bfd_get_filename (m_abfd));
26562 data += strlen (s) + 1;
26563 }
26564 }
26565
26566 /* Return offset of symbol name S in the .debug_str section. Add
26567 such symbol to the section's end if it does not exist there
26568 yet. */
26569 size_t lookup (const char *s)
26570 {
26571 const auto it = m_str_table.find (c_str_view (s));
26572 if (it != m_str_table.end ())
26573 return it->second;
ed2dc618 26574 const size_t offset = (m_dwarf2_per_objfile->str.size
437afbb8
JK
26575 + m_str_add_buf.size ());
26576 m_str_table.emplace (c_str_view (s), offset);
26577 m_str_add_buf.append_cstr0 (s);
26578 return offset;
26579 }
26580
26581 /* Append the end of the .debug_str section to FILE. */
26582 void file_write (FILE *file) const
26583 {
26584 m_str_add_buf.file_write (file);
26585 }
26586
26587 private:
26588 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26589 bfd *const m_abfd;
ed2dc618 26590 struct dwarf2_per_objfile *m_dwarf2_per_objfile;
437afbb8
JK
26591
26592 /* Data to add at the end of .debug_str for new needed symbol names. */
26593 data_buf m_str_add_buf;
26594 };
26595
26596 /* Container to map used DWARF tags to their .debug_names abbreviation
26597 tags. */
26598 class index_key
26599 {
26600 public:
8af5c486
JK
26601 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26602 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
437afbb8
JK
26603 {
26604 }
26605
26606 bool
26607 operator== (const index_key &other) const
26608 {
8af5c486
JK
26609 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26610 && kind == other.kind);
437afbb8
JK
26611 }
26612
26613 const int dwarf_tag;
26614 const bool is_static;
8af5c486 26615 const unit_kind kind;
437afbb8
JK
26616 };
26617
26618 /* Provide std::unordered_map::hasher for index_key. */
26619 class index_key_hasher
26620 {
26621 public:
26622 size_t
26623 operator () (const index_key &key) const
26624 {
26625 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26626 }
26627 };
26628
26629 /* Parameters of one symbol entry. */
26630 class symbol_value
26631 {
26632 public:
26633 const int dwarf_tag, cu_index;
26634 const bool is_static;
8af5c486 26635 const unit_kind kind;
437afbb8 26636
8af5c486
JK
26637 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26638 unit_kind kind_)
26639 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26640 kind (kind_)
437afbb8
JK
26641 {}
26642
26643 bool
26644 operator< (const symbol_value &other) const
26645 {
26646#define X(n) \
26647 do \
26648 { \
26649 if (n < other.n) \
26650 return true; \
26651 if (n > other.n) \
26652 return false; \
26653 } \
26654 while (0)
26655 X (dwarf_tag);
26656 X (is_static);
8af5c486 26657 X (kind);
437afbb8
JK
26658 X (cu_index);
26659#undef X
26660 return false;
26661 }
26662 };
26663
26664 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26665 output. */
26666 class offset_vec
26667 {
26668 protected:
26669 const bfd_endian dwarf5_byte_order;
26670 public:
26671 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26672 : dwarf5_byte_order (dwarf5_byte_order_)
26673 {}
26674
26675 /* Call std::vector::reserve for NELEM elements. */
26676 virtual void reserve (size_t nelem) = 0;
26677
26678 /* Call std::vector::push_back with store_unsigned_integer byte
26679 reordering for ELEM. */
26680 virtual void push_back_reorder (size_t elem) = 0;
26681
26682 /* Return expected output size in bytes. */
26683 virtual size_t bytes () const = 0;
26684
26685 /* Write name table to FILE. */
26686 virtual void file_write (FILE *file) const = 0;
26687 };
26688
26689 /* Template to unify DWARF-32 and DWARF-64 output. */
26690 template<typename OffsetSize>
26691 class offset_vec_tmpl : public offset_vec
26692 {
26693 public:
26694 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26695 : offset_vec (dwarf5_byte_order_)
26696 {}
26697
26698 /* Implement offset_vec::reserve. */
26699 void reserve (size_t nelem) override
26700 {
26701 m_vec.reserve (nelem);
26702 }
26703
26704 /* Implement offset_vec::push_back_reorder. */
26705 void push_back_reorder (size_t elem) override
26706 {
26707 m_vec.push_back (elem);
26708 /* Check for overflow. */
26709 gdb_assert (m_vec.back () == elem);
26710 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26711 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26712 }
26713
26714 /* Implement offset_vec::bytes. */
26715 size_t bytes () const override
26716 {
26717 return m_vec.size () * sizeof (m_vec[0]);
26718 }
26719
26720 /* Implement offset_vec::file_write. */
26721 void file_write (FILE *file) const override
26722 {
26723 ::file_write (file, m_vec);
26724 }
26725
26726 private:
26727 std::vector<OffsetSize> m_vec;
26728 };
26729
26730 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26731 respecting name table width. */
26732 class dwarf
26733 {
26734 public:
26735 offset_vec &name_table_string_offs, &name_table_entry_offs;
26736
26737 dwarf (offset_vec &name_table_string_offs_,
26738 offset_vec &name_table_entry_offs_)
26739 : name_table_string_offs (name_table_string_offs_),
26740 name_table_entry_offs (name_table_entry_offs_)
26741 {
26742 }
26743 };
e8f8bcb3 26744
437afbb8
JK
26745 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26746 respecting name table width. */
26747 template<typename OffsetSize>
26748 class dwarf_tmpl : public dwarf
26749 {
26750 public:
26751 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26752 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26753 m_name_table_string_offs (dwarf5_byte_order_),
26754 m_name_table_entry_offs (dwarf5_byte_order_)
26755 {}
26756
26757 private:
26758 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26759 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26760 };
26761
26762 /* Try to reconstruct original DWARF tag for given partial_symbol.
26763 This function is not DWARF-5 compliant but it is sufficient for
26764 GDB as a DWARF-5 index consumer. */
26765 static int psymbol_tag (const struct partial_symbol *psym)
26766 {
26767 domain_enum domain = PSYMBOL_DOMAIN (psym);
26768 enum address_class aclass = PSYMBOL_CLASS (psym);
26769
26770 switch (domain)
26771 {
26772 case VAR_DOMAIN:
26773 switch (aclass)
26774 {
26775 case LOC_BLOCK:
26776 return DW_TAG_subprogram;
26777 case LOC_TYPEDEF:
26778 return DW_TAG_typedef;
26779 case LOC_COMPUTED:
26780 case LOC_CONST_BYTES:
26781 case LOC_OPTIMIZED_OUT:
26782 case LOC_STATIC:
26783 return DW_TAG_variable;
26784 case LOC_CONST:
26785 /* Note: It's currently impossible to recognize psyms as enum values
26786 short of reading the type info. For now punt. */
26787 return DW_TAG_variable;
26788 default:
26789 /* There are other LOC_FOO values that one might want to classify
26790 as variables, but dwarf2read.c doesn't currently use them. */
26791 return DW_TAG_variable;
26792 }
26793 case STRUCT_DOMAIN:
26794 return DW_TAG_structure_type;
26795 default:
26796 return 0;
26797 }
26798 }
26799
26800 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26801 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26802 struct partial_symbol **psymp, int count, int cu_index,
8af5c486 26803 bool is_static, unit_kind kind)
437afbb8
JK
26804 {
26805 for (; count-- > 0; ++psymp)
26806 {
26807 struct partial_symbol *psym = *psymp;
26808
26809 if (SYMBOL_LANGUAGE (psym) == language_ada)
26810 error (_("Ada is not currently supported by the index"));
26811
26812 /* Only add a given psymbol once. */
26813 if (psyms_seen.insert (psym).second)
8af5c486 26814 insert (psym, cu_index, is_static, kind);
437afbb8
JK
26815 }
26816 }
26817
8af5c486
JK
26818 /* A helper function that writes a single signatured_type
26819 to a debug_names. */
26820 void
26821 write_one_signatured_type (struct signatured_type *entry,
26822 struct signatured_type_index_data *info)
26823 {
26824 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26825
26826 write_psymbols (info->psyms_seen,
26827 &info->objfile->global_psymbols[psymtab->globals_offset],
26828 psymtab->n_global_syms, info->cu_index, false,
26829 unit_kind::tu);
26830 write_psymbols (info->psyms_seen,
26831 &info->objfile->static_psymbols[psymtab->statics_offset],
26832 psymtab->n_static_syms, info->cu_index, true,
26833 unit_kind::tu);
26834
26835 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
26836 to_underlying (entry->per_cu.sect_off));
26837
26838 ++info->cu_index;
26839 }
26840
437afbb8
JK
26841 /* Store value of each symbol. */
26842 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
26843 m_name_to_value_set;
26844
26845 /* Tables of DWARF-5 .debug_names. They are in object file byte
26846 order. */
26847 std::vector<uint32_t> m_bucket_table;
26848 std::vector<uint32_t> m_hash_table;
26849
26850 const bfd_endian m_dwarf5_byte_order;
26851 dwarf_tmpl<uint32_t> m_dwarf32;
26852 dwarf_tmpl<uint64_t> m_dwarf64;
26853 dwarf &m_dwarf;
26854 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
26855 debug_str_lookup m_debugstrlookup;
26856
26857 /* Map each used .debug_names abbreviation tag parameter to its
26858 index value. */
26859 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
26860
26861 /* Next unused .debug_names abbreviation tag for
26862 m_indexkey_to_idx. */
26863 int m_idx_next = 1;
26864
26865 /* .debug_names abbreviation table. */
26866 data_buf m_abbrev_table;
26867
26868 /* .debug_names entry pool. */
26869 data_buf m_entry_pool;
26870};
26871
26872/* Return iff any of the needed offsets does not fit into 32-bit
26873 .debug_names section. */
26874
26875static bool
ed2dc618 26876check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile)
437afbb8
JK
26877{
26878 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26879 {
26880 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
26881
26882 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26883 return true;
26884 }
26885 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26886 {
26887 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26888 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26889
26890 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26891 return true;
26892 }
26893 return false;
26894}
26895
26896/* The psyms_seen set is potentially going to be largish (~40k
26897 elements when indexing a -g3 build of GDB itself). Estimate the
26898 number of elements in order to avoid too many rehashes, which
26899 require rebuilding buckets and thus many trips to
26900 malloc/free. */
26901
26902static size_t
ed2dc618 26903psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile)
437afbb8 26904{
e8f8bcb3
PA
26905 size_t psyms_count = 0;
26906 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26907 {
26908 struct dwarf2_per_cu_data *per_cu
26909 = dwarf2_per_objfile->all_comp_units[i];
26910 struct partial_symtab *psymtab = per_cu->v.psymtab;
26911
26912 if (psymtab != NULL && psymtab->user == NULL)
26913 recursively_count_psymbols (psymtab, psyms_count);
26914 }
26915 /* Generating an index for gdb itself shows a ratio of
26916 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
437afbb8
JK
26917 return psyms_count / 4;
26918}
26919
26920/* Write new .gdb_index section for OBJFILE into OUT_FILE.
26921 Return how many bytes were expected to be written into OUT_FILE. */
26922
26923static size_t
ed2dc618 26924write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file)
437afbb8 26925{
ed2dc618 26926 struct objfile *objfile = dwarf2_per_objfile->objfile;
437afbb8
JK
26927 mapped_symtab symtab;
26928 data_buf cu_list;
26929
26930 /* While we're scanning CU's create a table that maps a psymtab pointer
26931 (which is what addrmap records) to its index (which is what is recorded
26932 in the index file). This will later be needed to write the address
26933 table. */
26934 psym_index_map cu_index_htab;
26935 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
26936
26937 /* The CU list is already sorted, so we don't need to do additional
26938 work here. Also, the debug_types entries do not appear in
26939 all_comp_units, but only in their own hash table. */
26940
ed2dc618
SM
26941 std::unordered_set<partial_symbol *> psyms_seen
26942 (psyms_seen_size (dwarf2_per_objfile));
bc8f2430 26943 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 26944 {
3e43a32a
MS
26945 struct dwarf2_per_cu_data *per_cu
26946 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 26947 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 26948
92fac807
JK
26949 /* CU of a shared file from 'dwz -m' may be unused by this main file.
26950 It may be referenced from a local scope but in such case it does not
26951 need to be present in .gdb_index. */
26952 if (psymtab == NULL)
26953 continue;
26954
95554aad 26955 if (psymtab->user == NULL)
bc8f2430
JK
26956 recursively_write_psymbols (objfile, psymtab, &symtab,
26957 psyms_seen, i);
9291a0cd 26958
bc8f2430
JK
26959 const auto insertpair = cu_index_htab.emplace (psymtab, i);
26960 gdb_assert (insertpair.second);
9291a0cd 26961
c2f134ac
PA
26962 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
26963 to_underlying (per_cu->sect_off));
26964 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
26965 }
26966
0a5429f6 26967 /* Dump the address map. */
bc8f2430
JK
26968 data_buf addr_vec;
26969 write_address_map (objfile, addr_vec, cu_index_htab);
0a5429f6 26970
1fd400ff 26971 /* Write out the .debug_type entries, if any. */
bc8f2430 26972 data_buf types_cu_list;
1fd400ff
TT
26973 if (dwarf2_per_objfile->signatured_types)
26974 {
bc8f2430
JK
26975 signatured_type_index_data sig_data (types_cu_list,
26976 psyms_seen);
1fd400ff
TT
26977
26978 sig_data.objfile = objfile;
bc8f2430 26979 sig_data.symtab = &symtab;
1fd400ff
TT
26980 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
26981 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26982 write_one_signatured_type, &sig_data);
26983 }
26984
156942c7
DE
26985 /* Now that we've processed all symbols we can shrink their cu_indices
26986 lists. */
bc8f2430 26987 uniquify_cu_indices (&symtab);
156942c7 26988
bc8f2430
JK
26989 data_buf symtab_vec, constant_pool;
26990 write_hash_table (&symtab, symtab_vec, constant_pool);
9291a0cd 26991
bc8f2430
JK
26992 data_buf contents;
26993 const offset_type size_of_contents = 6 * sizeof (offset_type);
26994 offset_type total_len = size_of_contents;
9291a0cd
TT
26995
26996 /* The version number. */
bc8f2430 26997 contents.append_data (MAYBE_SWAP (8));
9291a0cd
TT
26998
26999 /* The offset of the CU list from the start of the file. */
bc8f2430
JK
27000 contents.append_data (MAYBE_SWAP (total_len));
27001 total_len += cu_list.size ();
9291a0cd 27002
1fd400ff 27003 /* The offset of the types CU list from the start of the file. */
bc8f2430
JK
27004 contents.append_data (MAYBE_SWAP (total_len));
27005 total_len += types_cu_list.size ();
1fd400ff 27006
9291a0cd 27007 /* The offset of the address table from the start of the file. */
bc8f2430
JK
27008 contents.append_data (MAYBE_SWAP (total_len));
27009 total_len += addr_vec.size ();
9291a0cd
TT
27010
27011 /* The offset of the symbol table from the start of the file. */
bc8f2430
JK
27012 contents.append_data (MAYBE_SWAP (total_len));
27013 total_len += symtab_vec.size ();
9291a0cd
TT
27014
27015 /* The offset of the constant pool from the start of the file. */
bc8f2430
JK
27016 contents.append_data (MAYBE_SWAP (total_len));
27017 total_len += constant_pool.size ();
9291a0cd 27018
bc8f2430 27019 gdb_assert (contents.size () == size_of_contents);
9291a0cd 27020
bc8f2430
JK
27021 contents.file_write (out_file);
27022 cu_list.file_write (out_file);
27023 types_cu_list.file_write (out_file);
27024 addr_vec.file_write (out_file);
27025 symtab_vec.file_write (out_file);
27026 constant_pool.file_write (out_file);
9291a0cd 27027
437afbb8
JK
27028 return total_len;
27029}
27030
27031/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
27032static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
27033
27034/* Write a new .debug_names section for OBJFILE into OUT_FILE, write
27035 needed addition to .debug_str section to OUT_FILE_STR. Return how
27036 many bytes were expected to be written into OUT_FILE. */
27037
27038static size_t
ed2dc618
SM
27039write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
27040 FILE *out_file, FILE *out_file_str)
437afbb8 27041{
ed2dc618
SM
27042 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile);
27043 struct objfile *objfile = dwarf2_per_objfile->objfile;
437afbb8
JK
27044 const enum bfd_endian dwarf5_byte_order
27045 = gdbarch_byte_order (get_objfile_arch (objfile));
27046
27047 /* The CU list is already sorted, so we don't need to do additional
27048 work here. Also, the debug_types entries do not appear in
27049 all_comp_units, but only in their own hash table. */
27050 data_buf cu_list;
ed2dc618
SM
27051 debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64,
27052 dwarf5_byte_order);
27053 std::unordered_set<partial_symbol *>
27054 psyms_seen (psyms_seen_size (dwarf2_per_objfile));
437afbb8
JK
27055 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27056 {
27057 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
27058 partial_symtab *psymtab = per_cu->v.psymtab;
27059
27060 /* CU of a shared file from 'dwz -m' may be unused by this main
27061 file. It may be referenced from a local scope but in such
27062 case it does not need to be present in .debug_names. */
27063 if (psymtab == NULL)
27064 continue;
27065
27066 if (psymtab->user == NULL)
27067 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
27068
8af5c486 27069 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
437afbb8
JK
27070 to_underlying (per_cu->sect_off));
27071 }
437afbb8 27072
8af5c486 27073 /* Write out the .debug_type entries, if any. */
437afbb8 27074 data_buf types_cu_list;
8af5c486 27075 if (dwarf2_per_objfile->signatured_types)
437afbb8 27076 {
8af5c486
JK
27077 debug_names::write_one_signatured_type_data sig_data (nametable,
27078 signatured_type_index_data (types_cu_list, psyms_seen));
437afbb8 27079
8af5c486
JK
27080 sig_data.info.objfile = objfile;
27081 /* It is used only for gdb_index. */
27082 sig_data.info.symtab = nullptr;
27083 sig_data.info.cu_index = 0;
27084 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
27085 debug_names::write_one_signatured_type,
27086 &sig_data);
437afbb8
JK
27087 }
27088
8af5c486
JK
27089 nametable.build ();
27090
27091 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
27092
437afbb8
JK
27093 const offset_type bytes_of_header
27094 = ((dwarf5_is_dwarf64 ? 12 : 4)
27095 + 2 + 2 + 7 * 4
27096 + sizeof (dwarf5_gdb_augmentation));
27097 size_t expected_bytes = 0;
27098 expected_bytes += bytes_of_header;
27099 expected_bytes += cu_list.size ();
27100 expected_bytes += types_cu_list.size ();
27101 expected_bytes += nametable.bytes ();
27102 data_buf header;
27103
27104 if (!dwarf5_is_dwarf64)
27105 {
27106 const uint64_t size64 = expected_bytes - 4;
27107 gdb_assert (size64 < 0xfffffff0);
27108 header.append_uint (4, dwarf5_byte_order, size64);
27109 }
27110 else
27111 {
27112 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
27113 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
27114 }
27115
27116 /* The version number. */
27117 header.append_uint (2, dwarf5_byte_order, 5);
27118
27119 /* Padding. */
27120 header.append_uint (2, dwarf5_byte_order, 0);
27121
27122 /* comp_unit_count - The number of CUs in the CU list. */
27123 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
27124
27125 /* local_type_unit_count - The number of TUs in the local TU
27126 list. */
27127 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
27128
27129 /* foreign_type_unit_count - The number of TUs in the foreign TU
27130 list. */
27131 header.append_uint (4, dwarf5_byte_order, 0);
27132
27133 /* bucket_count - The number of hash buckets in the hash lookup
27134 table. */
27135 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
27136
27137 /* name_count - The number of unique names in the index. */
27138 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
27139
27140 /* abbrev_table_size - The size in bytes of the abbreviations
27141 table. */
27142 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
27143
27144 /* augmentation_string_size - The size in bytes of the augmentation
27145 string. This value is rounded up to a multiple of 4. */
27146 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
27147 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
27148 header.append_data (dwarf5_gdb_augmentation);
27149
27150 gdb_assert (header.size () == bytes_of_header);
27151
27152 header.file_write (out_file);
27153 cu_list.file_write (out_file);
27154 types_cu_list.file_write (out_file);
27155 nametable.file_write (out_file, out_file_str);
27156
27157 return expected_bytes;
27158}
27159
27160/* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
27161 position is at the end of the file. */
27162
27163static void
27164assert_file_size (FILE *file, const char *filename, size_t expected_size)
27165{
27166 const auto file_size = ftell (file);
27167 if (file_size == -1)
27168 error (_("Can't get `%s' size"), filename);
27169 gdb_assert (file_size == expected_size);
27170}
27171
437afbb8
JK
27172/* Create an index file for OBJFILE in the directory DIR. */
27173
27174static void
ed2dc618
SM
27175write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
27176 const char *dir,
437afbb8
JK
27177 dw_index_kind index_kind)
27178{
ed2dc618
SM
27179 struct objfile *objfile = dwarf2_per_objfile->objfile;
27180
437afbb8
JK
27181 if (dwarf2_per_objfile->using_index)
27182 error (_("Cannot use an index to create the index"));
27183
27184 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
27185 error (_("Cannot make an index when the file has multiple .debug_types sections"));
27186
27187 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
27188 return;
27189
27190 struct stat st;
27191 if (stat (objfile_name (objfile), &st) < 0)
27192 perror_with_name (objfile_name (objfile));
27193
27194 std::string filename (std::string (dir) + SLASH_STRING
27195 + lbasename (objfile_name (objfile))
27196 + (index_kind == dw_index_kind::DEBUG_NAMES
27197 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
27198
27199 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
27200 if (!out_file)
27201 error (_("Can't open `%s' for writing"), filename.c_str ());
27202
27203 /* Order matters here; we want FILE to be closed before FILENAME is
27204 unlinked, because on MS-Windows one cannot delete a file that is
27205 still open. (Don't call anything here that might throw until
27206 file_closer is created.) */
27207 gdb::unlinker unlink_file (filename.c_str ());
27208 gdb_file_up close_out_file (out_file);
27209
27210 if (index_kind == dw_index_kind::DEBUG_NAMES)
27211 {
27212 std::string filename_str (std::string (dir) + SLASH_STRING
27213 + lbasename (objfile_name (objfile))
27214 + DEBUG_STR_SUFFIX);
27215 FILE *out_file_str
27216 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
27217 if (!out_file_str)
27218 error (_("Can't open `%s' for writing"), filename_str.c_str ());
27219 gdb::unlinker unlink_file_str (filename_str.c_str ());
27220 gdb_file_up close_out_file_str (out_file_str);
27221
27222 const size_t total_len
ed2dc618 27223 = write_debug_names (dwarf2_per_objfile, out_file, out_file_str);
437afbb8
JK
27224 assert_file_size (out_file, filename.c_str (), total_len);
27225
27226 /* We want to keep the file .debug_str file too. */
27227 unlink_file_str.keep ();
27228 }
27229 else
27230 {
27231 const size_t total_len
ed2dc618 27232 = write_gdbindex (dwarf2_per_objfile, out_file);
437afbb8
JK
27233 assert_file_size (out_file, filename.c_str (), total_len);
27234 }
27235
bef155c3
TT
27236 /* We want to keep the file. */
27237 unlink_file.keep ();
9291a0cd
TT
27238}
27239
90476074
TT
27240/* Implementation of the `save gdb-index' command.
27241
437afbb8
JK
27242 Note that the .gdb_index file format used by this command is
27243 documented in the GDB manual. Any changes here must be documented
27244 there. */
11570e71 27245
9291a0cd 27246static void
8384c356 27247save_gdb_index_command (const char *arg, int from_tty)
9291a0cd
TT
27248{
27249 struct objfile *objfile;
437afbb8
JK
27250 const char dwarf5space[] = "-dwarf-5 ";
27251 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
27252
27253 if (!arg)
27254 arg = "";
27255
27256 arg = skip_spaces (arg);
27257 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
27258 {
27259 index_kind = dw_index_kind::DEBUG_NAMES;
27260 arg += strlen (dwarf5space);
27261 arg = skip_spaces (arg);
27262 }
9291a0cd 27263
437afbb8
JK
27264 if (!*arg)
27265 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
9291a0cd
TT
27266
27267 ALL_OBJFILES (objfile)
27268 {
27269 struct stat st;
27270
27271 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 27272 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
27273 continue;
27274
ed2dc618
SM
27275 struct dwarf2_per_objfile *dwarf2_per_objfile
27276 = get_dwarf2_per_objfile (objfile);
9291a0cd 27277
ed2dc618
SM
27278 if (dwarf2_per_objfile != NULL)
27279 {
492d29ea 27280 TRY
9291a0cd 27281 {
ed2dc618 27282 write_psymtabs_to_index (dwarf2_per_objfile, arg, index_kind);
9291a0cd 27283 }
492d29ea
PA
27284 CATCH (except, RETURN_MASK_ERROR)
27285 {
27286 exception_fprintf (gdb_stderr, except,
27287 _("Error while writing index for `%s': "),
27288 objfile_name (objfile));
27289 }
27290 END_CATCH
9291a0cd 27291 }
ed2dc618 27292
9291a0cd 27293 }
dce234bc
PP
27294}
27295
9291a0cd
TT
27296\f
27297
b4f54984 27298int dwarf_always_disassemble;
9eae7c52
TT
27299
27300static void
b4f54984
DE
27301show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27302 struct cmd_list_element *c, const char *value)
9eae7c52 27303{
3e43a32a
MS
27304 fprintf_filtered (file,
27305 _("Whether to always disassemble "
27306 "DWARF expressions is %s.\n"),
9eae7c52
TT
27307 value);
27308}
27309
900e11f9
JK
27310static void
27311show_check_physname (struct ui_file *file, int from_tty,
27312 struct cmd_list_element *c, const char *value)
27313{
27314 fprintf_filtered (file,
27315 _("Whether to check \"physname\" is %s.\n"),
27316 value);
27317}
27318
6502dd73
DJ
27319void
27320_initialize_dwarf2_read (void)
27321{
96d19272
JK
27322 struct cmd_list_element *c;
27323
dce234bc 27324 dwarf2_objfile_data_key
c1bd65d0 27325 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 27326
b4f54984
DE
27327 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27328Set DWARF specific variables.\n\
27329Configure DWARF variables such as the cache size"),
27330 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
27331 0/*allow-unknown*/, &maintenance_set_cmdlist);
27332
b4f54984
DE
27333 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27334Show DWARF specific variables\n\
27335Show DWARF variables such as the cache size"),
27336 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
27337 0/*allow-unknown*/, &maintenance_show_cmdlist);
27338
27339 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
27340 &dwarf_max_cache_age, _("\
27341Set the upper bound on the age of cached DWARF compilation units."), _("\
27342Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
27343A higher limit means that cached compilation units will be stored\n\
27344in memory longer, and more total memory will be used. Zero disables\n\
27345caching, which can slow down startup."),
2c5b56ce 27346 NULL,
b4f54984
DE
27347 show_dwarf_max_cache_age,
27348 &set_dwarf_cmdlist,
27349 &show_dwarf_cmdlist);
d97bc12b 27350
9eae7c52 27351 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 27352 &dwarf_always_disassemble, _("\
9eae7c52
TT
27353Set whether `info address' always disassembles DWARF expressions."), _("\
27354Show whether `info address' always disassembles DWARF expressions."), _("\
27355When enabled, DWARF expressions are always printed in an assembly-like\n\
27356syntax. When disabled, expressions will be printed in a more\n\
27357conversational style, when possible."),
27358 NULL,
b4f54984
DE
27359 show_dwarf_always_disassemble,
27360 &set_dwarf_cmdlist,
27361 &show_dwarf_cmdlist);
27362
27363 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27364Set debugging of the DWARF reader."), _("\
27365Show debugging of the DWARF reader."), _("\
27366When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
27367reading and symtab expansion. A value of 1 (one) provides basic\n\
27368information. A value greater than 1 provides more verbose information."),
45cfd468
DE
27369 NULL,
27370 NULL,
27371 &setdebuglist, &showdebuglist);
27372
b4f54984
DE
27373 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27374Set debugging of the DWARF DIE reader."), _("\
27375Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
27376When enabled (non-zero), DIEs are dumped after they are read in.\n\
27377The value is the maximum depth to print."),
ccce17b0
YQ
27378 NULL,
27379 NULL,
27380 &setdebuglist, &showdebuglist);
9291a0cd 27381
27e0867f
DE
27382 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27383Set debugging of the dwarf line reader."), _("\
27384Show debugging of the dwarf line reader."), _("\
27385When enabled (non-zero), line number entries are dumped as they are read in.\n\
27386A value of 1 (one) provides basic information.\n\
27387A value greater than 1 provides more verbose information."),
27388 NULL,
27389 NULL,
27390 &setdebuglist, &showdebuglist);
27391
900e11f9
JK
27392 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27393Set cross-checking of \"physname\" code against demangler."), _("\
27394Show cross-checking of \"physname\" code against demangler."), _("\
27395When enabled, GDB's internal \"physname\" code is checked against\n\
27396the demangler."),
27397 NULL, show_check_physname,
27398 &setdebuglist, &showdebuglist);
27399
e615022a
DE
27400 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27401 no_class, &use_deprecated_index_sections, _("\
27402Set whether to use deprecated gdb_index sections."), _("\
27403Show whether to use deprecated gdb_index sections."), _("\
27404When enabled, deprecated .gdb_index sections are used anyway.\n\
27405Normally they are ignored either because of a missing feature or\n\
27406performance issue.\n\
27407Warning: This option must be enabled before gdb reads the file."),
27408 NULL,
27409 NULL,
27410 &setlist, &showlist);
27411
96d19272 27412 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 27413 _("\
fc1a9d6e 27414Save a gdb-index file.\n\
437afbb8
JK
27415Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27416\n\
27417No options create one file with .gdb-index extension for pre-DWARF-5\n\
27418compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27419extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
96d19272
JK
27420 &save_cmdlist);
27421 set_cmd_completer (c, filename_completer);
f1e6e072
TT
27422
27423 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27424 &dwarf2_locexpr_funcs);
27425 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27426 &dwarf2_loclist_funcs);
27427
27428 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27429 &dwarf2_block_frame_base_locexpr_funcs);
27430 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27431 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
27432
27433#if GDB_SELF_TEST
27434 selftests::register_test ("dw2_expand_symtabs_matching",
27435 selftests::dw2_expand_symtabs_matching::run_test);
27436#endif
6502dd73 27437}
This page took 4.729802 seconds and 4 git commands to generate.