Add support for the readnever concept
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
61baf725 3 Copyright (C) 1994-2017 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270 55#include "typeprint.h"
ccefe4c4 56#include "psympriv.h"
53ce3c39 57#include <sys/stat.h>
96d19272 58#include "completer.h"
34eaf542 59#include "vec.h"
98bfdba5 60#include "c-lang.h"
a766d390 61#include "go-lang.h"
98bfdba5 62#include "valprint.h"
3019eac3 63#include "gdbcore.h" /* for gnutarget */
156942c7 64#include "gdb/gdb-index.h"
60d5a603 65#include <ctype.h>
cbb099e8 66#include "gdb_bfd.h"
4357ac6c 67#include "f-lang.h"
05cba821 68#include "source.h"
614c279d 69#include "filestuff.h"
dc294be5 70#include "build-id.h"
22cee43f 71#include "namespace.h"
bef155c3 72#include "common/gdb_unlinker.h"
14bc53a8 73#include "common/function-view.h"
ecfb656c
PA
74#include "common/gdb_optional.h"
75#include "common/underlying.h"
d5722aa2 76#include "common/byte-vector.h"
bbf2f4df 77#include "filename-seen-cache.h"
b32b108a 78#include "producer.h"
c906108c 79#include <fcntl.h>
c906108c 80#include <sys/types.h>
325fac50 81#include <algorithm>
bc8f2430
JK
82#include <unordered_set>
83#include <unordered_map>
c62446b1 84#include "selftest.h"
d8151005 85
73be47f5
DE
86/* When == 1, print basic high level tracing messages.
87 When > 1, be more verbose.
b4f54984
DE
88 This is in contrast to the low level DIE reading of dwarf_die_debug. */
89static unsigned int dwarf_read_debug = 0;
45cfd468 90
d97bc12b 91/* When non-zero, dump DIEs after they are read in. */
b4f54984 92static unsigned int dwarf_die_debug = 0;
d97bc12b 93
27e0867f
DE
94/* When non-zero, dump line number entries as they are read in. */
95static unsigned int dwarf_line_debug = 0;
96
900e11f9
JK
97/* When non-zero, cross-check physname against demangler. */
98static int check_physname = 0;
99
481860b3 100/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 101static int use_deprecated_index_sections = 0;
481860b3 102
6502dd73
DJ
103static const struct objfile_data *dwarf2_objfile_data_key;
104
f1e6e072
TT
105/* The "aclass" indices for various kinds of computed DWARF symbols. */
106
107static int dwarf2_locexpr_index;
108static int dwarf2_loclist_index;
109static int dwarf2_locexpr_block_index;
110static int dwarf2_loclist_block_index;
111
73869dc2
DE
112/* A descriptor for dwarf sections.
113
114 S.ASECTION, SIZE are typically initialized when the objfile is first
115 scanned. BUFFER, READIN are filled in later when the section is read.
116 If the section contained compressed data then SIZE is updated to record
117 the uncompressed size of the section.
118
119 DWP file format V2 introduces a wrinkle that is easiest to handle by
120 creating the concept of virtual sections contained within a real section.
121 In DWP V2 the sections of the input DWO files are concatenated together
122 into one section, but section offsets are kept relative to the original
123 input section.
124 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
125 the real section this "virtual" section is contained in, and BUFFER,SIZE
126 describe the virtual section. */
127
dce234bc
PP
128struct dwarf2_section_info
129{
73869dc2
DE
130 union
131 {
e5aa3347 132 /* If this is a real section, the bfd section. */
049412e3 133 asection *section;
73869dc2 134 /* If this is a virtual section, pointer to the containing ("real")
e5aa3347 135 section. */
73869dc2
DE
136 struct dwarf2_section_info *containing_section;
137 } s;
19ac8c2e 138 /* Pointer to section data, only valid if readin. */
d521ce57 139 const gdb_byte *buffer;
73869dc2 140 /* The size of the section, real or virtual. */
dce234bc 141 bfd_size_type size;
73869dc2
DE
142 /* If this is a virtual section, the offset in the real section.
143 Only valid if is_virtual. */
144 bfd_size_type virtual_offset;
be391dca 145 /* True if we have tried to read this section. */
73869dc2
DE
146 char readin;
147 /* True if this is a virtual section, False otherwise.
049412e3 148 This specifies which of s.section and s.containing_section to use. */
73869dc2 149 char is_virtual;
dce234bc
PP
150};
151
8b70b953
TT
152typedef struct dwarf2_section_info dwarf2_section_info_def;
153DEF_VEC_O (dwarf2_section_info_def);
154
9291a0cd
TT
155/* All offsets in the index are of this type. It must be
156 architecture-independent. */
157typedef uint32_t offset_type;
158
159DEF_VEC_I (offset_type);
160
156942c7
DE
161/* Ensure only legit values are used. */
162#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
163 do { \
164 gdb_assert ((unsigned int) (value) <= 1); \
165 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
166 } while (0)
167
168/* Ensure only legit values are used. */
169#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
170 do { \
171 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
172 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
173 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
174 } while (0)
175
176/* Ensure we don't use more than the alloted nuber of bits for the CU. */
177#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
178 do { \
179 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
180 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
181 } while (0)
182
3f563c84
PA
183#if WORDS_BIGENDIAN
184
185/* Convert VALUE between big- and little-endian. */
186
187static offset_type
188byte_swap (offset_type value)
189{
190 offset_type result;
191
192 result = (value & 0xff) << 24;
193 result |= (value & 0xff00) << 8;
194 result |= (value & 0xff0000) >> 8;
195 result |= (value & 0xff000000) >> 24;
196 return result;
197}
198
199#define MAYBE_SWAP(V) byte_swap (V)
200
201#else
202#define MAYBE_SWAP(V) static_cast<offset_type> (V)
203#endif /* WORDS_BIGENDIAN */
204
205/* An index into a (C++) symbol name component in a symbol name as
206 recorded in the mapped_index's symbol table. For each C++ symbol
207 in the symbol table, we record one entry for the start of each
208 component in the symbol in a table of name components, and then
209 sort the table, in order to be able to binary search symbol names,
210 ignoring leading namespaces, both completion and regular look up.
211 For example, for symbol "A::B::C", we'll have an entry that points
212 to "A::B::C", another that points to "B::C", and another for "C".
213 Note that function symbols in GDB index have no parameter
214 information, just the function/method names. You can convert a
215 name_component to a "const char *" using the
216 'mapped_index::symbol_name_at(offset_type)' method. */
217
218struct name_component
219{
220 /* Offset in the symbol name where the component starts. Stored as
221 a (32-bit) offset instead of a pointer to save memory and improve
222 locality on 64-bit architectures. */
223 offset_type name_offset;
224
225 /* The symbol's index in the symbol and constant pool tables of a
226 mapped_index. */
227 offset_type idx;
228};
229
9291a0cd
TT
230/* A description of the mapped index. The file format is described in
231 a comment by the code that writes the index. */
232struct mapped_index
233{
559a7a62
JK
234 /* Index data format version. */
235 int version;
236
9291a0cd
TT
237 /* The total length of the buffer. */
238 off_t total_size;
b11b1f88 239
9291a0cd
TT
240 /* A pointer to the address table data. */
241 const gdb_byte *address_table;
b11b1f88 242
9291a0cd
TT
243 /* Size of the address table data in bytes. */
244 offset_type address_table_size;
b11b1f88 245
3876f04e
DE
246 /* The symbol table, implemented as a hash table. */
247 const offset_type *symbol_table;
b11b1f88 248
9291a0cd 249 /* Size in slots, each slot is 2 offset_types. */
3876f04e 250 offset_type symbol_table_slots;
b11b1f88 251
9291a0cd
TT
252 /* A pointer to the constant pool. */
253 const char *constant_pool;
3f563c84
PA
254
255 /* The name_component table (a sorted vector). See name_component's
256 description above. */
257 std::vector<name_component> name_components;
258
5c58de74
PA
259 /* How NAME_COMPONENTS is sorted. */
260 enum case_sensitivity name_components_casing;
261
3f563c84
PA
262 /* Convenience method to get at the name of the symbol at IDX in the
263 symbol table. */
264 const char *symbol_name_at (offset_type idx) const
265 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx]); }
5c58de74
PA
266
267 /* Build the symbol name component sorted vector, if we haven't
268 yet. */
269 void build_name_components ();
270
271 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
272 possible matches for LN_NO_PARAMS in the name component
273 vector. */
274 std::pair<std::vector<name_component>::const_iterator,
275 std::vector<name_component>::const_iterator>
276 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
9291a0cd
TT
277};
278
95554aad
TT
279typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
280DEF_VEC_P (dwarf2_per_cu_ptr);
281
52059ffd
TT
282struct tu_stats
283{
284 int nr_uniq_abbrev_tables;
285 int nr_symtabs;
286 int nr_symtab_sharers;
287 int nr_stmt_less_type_units;
288 int nr_all_type_units_reallocs;
289};
290
9cdd5dbd
DE
291/* Collection of data recorded per objfile.
292 This hangs off of dwarf2_objfile_data_key. */
293
6502dd73
DJ
294struct dwarf2_per_objfile
295{
330cdd98
PA
296 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
297 dwarf2 section names, or is NULL if the standard ELF names are
298 used. */
299 dwarf2_per_objfile (struct objfile *objfile,
300 const dwarf2_debug_sections *names);
ae038cb0 301
330cdd98
PA
302 ~dwarf2_per_objfile ();
303
d6541620 304 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
330cdd98
PA
305
306 /* Free all cached compilation units. */
307 void free_cached_comp_units ();
308private:
309 /* This function is mapped across the sections and remembers the
310 offset and size of each of the debugging sections we are
311 interested in. */
312 void locate_sections (bfd *abfd, asection *sectp,
313 const dwarf2_debug_sections &names);
314
315public:
316 dwarf2_section_info info {};
317 dwarf2_section_info abbrev {};
318 dwarf2_section_info line {};
319 dwarf2_section_info loc {};
320 dwarf2_section_info loclists {};
321 dwarf2_section_info macinfo {};
322 dwarf2_section_info macro {};
323 dwarf2_section_info str {};
324 dwarf2_section_info line_str {};
325 dwarf2_section_info ranges {};
326 dwarf2_section_info rnglists {};
327 dwarf2_section_info addr {};
328 dwarf2_section_info frame {};
329 dwarf2_section_info eh_frame {};
330 dwarf2_section_info gdb_index {};
331
332 VEC (dwarf2_section_info_def) *types = NULL;
8b70b953 333
be391dca 334 /* Back link. */
330cdd98 335 struct objfile *objfile = NULL;
be391dca 336
d467dd73 337 /* Table of all the compilation units. This is used to locate
10b3939b 338 the target compilation unit of a particular reference. */
330cdd98 339 struct dwarf2_per_cu_data **all_comp_units = NULL;
ae038cb0
DJ
340
341 /* The number of compilation units in ALL_COMP_UNITS. */
330cdd98 342 int n_comp_units = 0;
ae038cb0 343
1fd400ff 344 /* The number of .debug_types-related CUs. */
330cdd98 345 int n_type_units = 0;
1fd400ff 346
6aa5f3a6
DE
347 /* The number of elements allocated in all_type_units.
348 If there are skeleton-less TUs, we add them to all_type_units lazily. */
330cdd98 349 int n_allocated_type_units = 0;
6aa5f3a6 350
a2ce51a0
DE
351 /* The .debug_types-related CUs (TUs).
352 This is stored in malloc space because we may realloc it. */
330cdd98 353 struct signatured_type **all_type_units = NULL;
1fd400ff 354
f4dc4d17
DE
355 /* Table of struct type_unit_group objects.
356 The hash key is the DW_AT_stmt_list value. */
330cdd98 357 htab_t type_unit_groups {};
72dca2f5 358
348e048f
DE
359 /* A table mapping .debug_types signatures to its signatured_type entry.
360 This is NULL if the .debug_types section hasn't been read in yet. */
330cdd98 361 htab_t signatured_types {};
348e048f 362
f4dc4d17
DE
363 /* Type unit statistics, to see how well the scaling improvements
364 are doing. */
330cdd98 365 struct tu_stats tu_stats {};
f4dc4d17
DE
366
367 /* A chain of compilation units that are currently read in, so that
368 they can be freed later. */
330cdd98 369 dwarf2_per_cu_data *read_in_chain = NULL;
f4dc4d17 370
3019eac3
DE
371 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
372 This is NULL if the table hasn't been allocated yet. */
330cdd98 373 htab_t dwo_files {};
3019eac3 374
330cdd98
PA
375 /* True if we've checked for whether there is a DWP file. */
376 bool dwp_checked = false;
80626a55
DE
377
378 /* The DWP file if there is one, or NULL. */
330cdd98 379 struct dwp_file *dwp_file = NULL;
80626a55 380
36586728
TT
381 /* The shared '.dwz' file, if one exists. This is used when the
382 original data was compressed using 'dwz -m'. */
330cdd98 383 struct dwz_file *dwz_file = NULL;
36586728 384
330cdd98 385 /* A flag indicating whether this objfile has a section loaded at a
72dca2f5 386 VMA of 0. */
330cdd98 387 bool has_section_at_zero = false;
9291a0cd 388
ae2de4f8
DE
389 /* True if we are using the mapped index,
390 or we are faking it for OBJF_READNOW's sake. */
330cdd98 391 bool using_index = false;
9291a0cd 392
ae2de4f8 393 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
330cdd98 394 mapped_index *index_table = NULL;
98bfdba5 395
7b9f3c50 396 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
397 TUs typically share line table entries with a CU, so we maintain a
398 separate table of all line table entries to support the sharing.
399 Note that while there can be way more TUs than CUs, we've already
400 sorted all the TUs into "type unit groups", grouped by their
401 DW_AT_stmt_list value. Therefore the only sharing done here is with a
402 CU and its associated TU group if there is one. */
330cdd98 403 htab_t quick_file_names_table {};
7b9f3c50 404
98bfdba5
PA
405 /* Set during partial symbol reading, to prevent queueing of full
406 symbols. */
330cdd98 407 bool reading_partial_symbols = false;
673bfd45 408
dee91e82 409 /* Table mapping type DIEs to their struct type *.
673bfd45 410 This is NULL if not allocated yet.
02142a6c 411 The mapping is done via (CU/TU + DIE offset) -> type. */
330cdd98 412 htab_t die_type_hash {};
95554aad
TT
413
414 /* The CUs we recently read. */
330cdd98 415 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
527f3840
JK
416
417 /* Table containing line_header indexed by offset and offset_in_dwz. */
330cdd98 418 htab_t line_header_hash {};
bbf2f4df
PA
419
420 /* Table containing all filenames. This is an optional because the
421 table is lazily constructed on first access. */
422 gdb::optional<filename_seen_cache> filenames_cache;
6502dd73
DJ
423};
424
425static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 426
251d32d9 427/* Default names of the debugging sections. */
c906108c 428
233a11ab
CS
429/* Note that if the debugging section has been compressed, it might
430 have a name like .zdebug_info. */
431
9cdd5dbd
DE
432static const struct dwarf2_debug_sections dwarf2_elf_names =
433{
251d32d9
TG
434 { ".debug_info", ".zdebug_info" },
435 { ".debug_abbrev", ".zdebug_abbrev" },
436 { ".debug_line", ".zdebug_line" },
437 { ".debug_loc", ".zdebug_loc" },
43988095 438 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 439 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 440 { ".debug_macro", ".zdebug_macro" },
251d32d9 441 { ".debug_str", ".zdebug_str" },
43988095 442 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 443 { ".debug_ranges", ".zdebug_ranges" },
43988095 444 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 445 { ".debug_types", ".zdebug_types" },
3019eac3 446 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
447 { ".debug_frame", ".zdebug_frame" },
448 { ".eh_frame", NULL },
24d3216f
TT
449 { ".gdb_index", ".zgdb_index" },
450 23
251d32d9 451};
c906108c 452
80626a55 453/* List of DWO/DWP sections. */
3019eac3 454
80626a55 455static const struct dwop_section_names
3019eac3
DE
456{
457 struct dwarf2_section_names abbrev_dwo;
458 struct dwarf2_section_names info_dwo;
459 struct dwarf2_section_names line_dwo;
460 struct dwarf2_section_names loc_dwo;
43988095 461 struct dwarf2_section_names loclists_dwo;
09262596
DE
462 struct dwarf2_section_names macinfo_dwo;
463 struct dwarf2_section_names macro_dwo;
3019eac3
DE
464 struct dwarf2_section_names str_dwo;
465 struct dwarf2_section_names str_offsets_dwo;
466 struct dwarf2_section_names types_dwo;
80626a55
DE
467 struct dwarf2_section_names cu_index;
468 struct dwarf2_section_names tu_index;
3019eac3 469}
80626a55 470dwop_section_names =
3019eac3
DE
471{
472 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
473 { ".debug_info.dwo", ".zdebug_info.dwo" },
474 { ".debug_line.dwo", ".zdebug_line.dwo" },
475 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 476 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
477 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
478 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
479 { ".debug_str.dwo", ".zdebug_str.dwo" },
480 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
481 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
482 { ".debug_cu_index", ".zdebug_cu_index" },
483 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
484};
485
c906108c
SS
486/* local data types */
487
107d2387
AC
488/* The data in a compilation unit header, after target2host
489 translation, looks like this. */
c906108c 490struct comp_unit_head
a738430d 491{
c764a876 492 unsigned int length;
a738430d 493 short version;
a738430d
MK
494 unsigned char addr_size;
495 unsigned char signed_addr_p;
9c541725 496 sect_offset abbrev_sect_off;
57349743 497
a738430d
MK
498 /* Size of file offsets; either 4 or 8. */
499 unsigned int offset_size;
57349743 500
a738430d
MK
501 /* Size of the length field; either 4 or 12. */
502 unsigned int initial_length_size;
57349743 503
43988095
JK
504 enum dwarf_unit_type unit_type;
505
a738430d
MK
506 /* Offset to the first byte of this compilation unit header in the
507 .debug_info section, for resolving relative reference dies. */
9c541725 508 sect_offset sect_off;
57349743 509
d00adf39
DE
510 /* Offset to first die in this cu from the start of the cu.
511 This will be the first byte following the compilation unit header. */
9c541725 512 cu_offset first_die_cu_offset;
43988095
JK
513
514 /* 64-bit signature of this type unit - it is valid only for
515 UNIT_TYPE DW_UT_type. */
516 ULONGEST signature;
517
518 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 519 cu_offset type_cu_offset_in_tu;
a738430d 520};
c906108c 521
3da10d80
KS
522/* Type used for delaying computation of method physnames.
523 See comments for compute_delayed_physnames. */
524struct delayed_method_info
525{
526 /* The type to which the method is attached, i.e., its parent class. */
527 struct type *type;
528
529 /* The index of the method in the type's function fieldlists. */
530 int fnfield_index;
531
532 /* The index of the method in the fieldlist. */
533 int index;
534
535 /* The name of the DIE. */
536 const char *name;
537
538 /* The DIE associated with this method. */
539 struct die_info *die;
540};
541
542typedef struct delayed_method_info delayed_method_info;
543DEF_VEC_O (delayed_method_info);
544
e7c27a73
DJ
545/* Internal state when decoding a particular compilation unit. */
546struct dwarf2_cu
547{
548 /* The objfile containing this compilation unit. */
549 struct objfile *objfile;
550
d00adf39 551 /* The header of the compilation unit. */
e7c27a73 552 struct comp_unit_head header;
e142c38c 553
d00adf39
DE
554 /* Base address of this compilation unit. */
555 CORE_ADDR base_address;
556
557 /* Non-zero if base_address has been set. */
558 int base_known;
559
e142c38c
DJ
560 /* The language we are debugging. */
561 enum language language;
562 const struct language_defn *language_defn;
563
b0f35d58
DL
564 const char *producer;
565
e142c38c
DJ
566 /* The generic symbol table building routines have separate lists for
567 file scope symbols and all all other scopes (local scopes). So
568 we need to select the right one to pass to add_symbol_to_list().
569 We do it by keeping a pointer to the correct list in list_in_scope.
570
571 FIXME: The original dwarf code just treated the file scope as the
572 first local scope, and all other local scopes as nested local
573 scopes, and worked fine. Check to see if we really need to
574 distinguish these in buildsym.c. */
575 struct pending **list_in_scope;
576
433df2d4
DE
577 /* The abbrev table for this CU.
578 Normally this points to the abbrev table in the objfile.
579 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
580 struct abbrev_table *abbrev_table;
72bf9492 581
b64f50a1
JK
582 /* Hash table holding all the loaded partial DIEs
583 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
584 htab_t partial_dies;
585
586 /* Storage for things with the same lifetime as this read-in compilation
587 unit, including partial DIEs. */
588 struct obstack comp_unit_obstack;
589
ae038cb0
DJ
590 /* When multiple dwarf2_cu structures are living in memory, this field
591 chains them all together, so that they can be released efficiently.
592 We will probably also want a generation counter so that most-recently-used
593 compilation units are cached... */
594 struct dwarf2_per_cu_data *read_in_chain;
595
69d751e3 596 /* Backlink to our per_cu entry. */
ae038cb0
DJ
597 struct dwarf2_per_cu_data *per_cu;
598
599 /* How many compilation units ago was this CU last referenced? */
600 int last_used;
601
b64f50a1
JK
602 /* A hash table of DIE cu_offset for following references with
603 die_info->offset.sect_off as hash. */
51545339 604 htab_t die_hash;
10b3939b
DJ
605
606 /* Full DIEs if read in. */
607 struct die_info *dies;
608
609 /* A set of pointers to dwarf2_per_cu_data objects for compilation
610 units referenced by this one. Only set during full symbol processing;
611 partial symbol tables do not have dependencies. */
612 htab_t dependencies;
613
cb1df416
DJ
614 /* Header data from the line table, during full symbol processing. */
615 struct line_header *line_header;
4c8aa72d
PA
616 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
617 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
618 this is the DW_TAG_compile_unit die for this CU. We'll hold on
619 to the line header as long as this DIE is being processed. See
620 process_die_scope. */
621 die_info *line_header_die_owner;
cb1df416 622
3da10d80
KS
623 /* A list of methods which need to have physnames computed
624 after all type information has been read. */
625 VEC (delayed_method_info) *method_list;
626
96408a79
SA
627 /* To be copied to symtab->call_site_htab. */
628 htab_t call_site_htab;
629
034e5797
DE
630 /* Non-NULL if this CU came from a DWO file.
631 There is an invariant here that is important to remember:
632 Except for attributes copied from the top level DIE in the "main"
633 (or "stub") file in preparation for reading the DWO file
634 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
635 Either there isn't a DWO file (in which case this is NULL and the point
636 is moot), or there is and either we're not going to read it (in which
637 case this is NULL) or there is and we are reading it (in which case this
638 is non-NULL). */
3019eac3
DE
639 struct dwo_unit *dwo_unit;
640
641 /* The DW_AT_addr_base attribute if present, zero otherwise
642 (zero is a valid value though).
1dbab08b 643 Note this value comes from the Fission stub CU/TU's DIE. */
3019eac3
DE
644 ULONGEST addr_base;
645
2e3cf129
DE
646 /* The DW_AT_ranges_base attribute if present, zero otherwise
647 (zero is a valid value though).
1dbab08b 648 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 649 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
650 be used without needing to know whether DWO files are in use or not.
651 N.B. This does not apply to DW_AT_ranges appearing in
652 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
653 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
654 DW_AT_ranges_base *would* have to be applied, and we'd have to care
655 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
656 ULONGEST ranges_base;
657
ae038cb0
DJ
658 /* Mark used when releasing cached dies. */
659 unsigned int mark : 1;
660
8be455d7
JK
661 /* This CU references .debug_loc. See the symtab->locations_valid field.
662 This test is imperfect as there may exist optimized debug code not using
663 any location list and still facing inlining issues if handled as
664 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 665 unsigned int has_loclist : 1;
ba919b58 666
1b80a9fa
JK
667 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
668 if all the producer_is_* fields are valid. This information is cached
669 because profiling CU expansion showed excessive time spent in
670 producer_is_gxx_lt_4_6. */
ba919b58
TT
671 unsigned int checked_producer : 1;
672 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 673 unsigned int producer_is_gcc_lt_4_3 : 1;
5230b05a 674 unsigned int producer_is_icc_lt_14 : 1;
4d4ec4e5
TT
675
676 /* When set, the file that we're processing is known to have
677 debugging info for C++ namespaces. GCC 3.3.x did not produce
678 this information, but later versions do. */
679
680 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
681};
682
10b3939b
DJ
683/* Persistent data held for a compilation unit, even when not
684 processing it. We put a pointer to this structure in the
28dee7f5 685 read_symtab_private field of the psymtab. */
10b3939b 686
ae038cb0
DJ
687struct dwarf2_per_cu_data
688{
36586728 689 /* The start offset and length of this compilation unit.
45452591 690 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
691 initial_length_size.
692 If the DIE refers to a DWO file, this is always of the original die,
693 not the DWO file. */
9c541725 694 sect_offset sect_off;
36586728 695 unsigned int length;
ae038cb0 696
43988095
JK
697 /* DWARF standard version this data has been read from (such as 4 or 5). */
698 short dwarf_version;
699
ae038cb0
DJ
700 /* Flag indicating this compilation unit will be read in before
701 any of the current compilation units are processed. */
c764a876 702 unsigned int queued : 1;
ae038cb0 703
0d99eb77
DE
704 /* This flag will be set when reading partial DIEs if we need to load
705 absolutely all DIEs for this compilation unit, instead of just the ones
706 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
707 hash table and don't find it. */
708 unsigned int load_all_dies : 1;
709
0186c6a7
DE
710 /* Non-zero if this CU is from .debug_types.
711 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
712 this is non-zero. */
3019eac3
DE
713 unsigned int is_debug_types : 1;
714
36586728
TT
715 /* Non-zero if this CU is from the .dwz file. */
716 unsigned int is_dwz : 1;
717
a2ce51a0
DE
718 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
719 This flag is only valid if is_debug_types is true.
720 We can't read a CU directly from a DWO file: There are required
721 attributes in the stub. */
722 unsigned int reading_dwo_directly : 1;
723
7ee85ab1
DE
724 /* Non-zero if the TU has been read.
725 This is used to assist the "Stay in DWO Optimization" for Fission:
726 When reading a DWO, it's faster to read TUs from the DWO instead of
727 fetching them from random other DWOs (due to comdat folding).
728 If the TU has already been read, the optimization is unnecessary
729 (and unwise - we don't want to change where gdb thinks the TU lives
730 "midflight").
731 This flag is only valid if is_debug_types is true. */
732 unsigned int tu_read : 1;
733
3019eac3
DE
734 /* The section this CU/TU lives in.
735 If the DIE refers to a DWO file, this is always the original die,
736 not the DWO file. */
8a0459fd 737 struct dwarf2_section_info *section;
348e048f 738
17ea53c3 739 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
740 of the CU cache it gets reset to NULL again. This is left as NULL for
741 dummy CUs (a CU header, but nothing else). */
ae038cb0 742 struct dwarf2_cu *cu;
1c379e20 743
9cdd5dbd
DE
744 /* The corresponding objfile.
745 Normally we can get the objfile from dwarf2_per_objfile.
746 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
747 struct objfile *objfile;
748
fffbe6a8
YQ
749 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
750 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
751 union
752 {
753 /* The partial symbol table associated with this compilation unit,
95554aad 754 or NULL for unread partial units. */
9291a0cd
TT
755 struct partial_symtab *psymtab;
756
757 /* Data needed by the "quick" functions. */
758 struct dwarf2_per_cu_quick_data *quick;
759 } v;
95554aad 760
796a7ff8
DE
761 /* The CUs we import using DW_TAG_imported_unit. This is filled in
762 while reading psymtabs, used to compute the psymtab dependencies,
763 and then cleared. Then it is filled in again while reading full
764 symbols, and only deleted when the objfile is destroyed.
765
766 This is also used to work around a difference between the way gold
767 generates .gdb_index version <=7 and the way gdb does. Arguably this
768 is a gold bug. For symbols coming from TUs, gold records in the index
769 the CU that includes the TU instead of the TU itself. This breaks
770 dw2_lookup_symbol: It assumes that if the index says symbol X lives
771 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
772 will find X. Alas TUs live in their own symtab, so after expanding CU Y
773 we need to look in TU Z to find X. Fortunately, this is akin to
774 DW_TAG_imported_unit, so we just use the same mechanism: For
775 .gdb_index version <=7 this also records the TUs that the CU referred
776 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
777 indices so we only pay a price for gold generated indices.
778 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 779 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
780};
781
348e048f
DE
782/* Entry in the signatured_types hash table. */
783
784struct signatured_type
785{
42e7ad6c 786 /* The "per_cu" object of this type.
ac9ec31b 787 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
788 N.B.: This is the first member so that it's easy to convert pointers
789 between them. */
790 struct dwarf2_per_cu_data per_cu;
791
3019eac3 792 /* The type's signature. */
348e048f
DE
793 ULONGEST signature;
794
3019eac3 795 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
796 If this TU is a DWO stub and the definition lives in a DWO file
797 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
798 cu_offset type_offset_in_tu;
799
800 /* Offset in the section of the type's DIE.
801 If the definition lives in a DWO file, this is the offset in the
802 .debug_types.dwo section.
803 The value is zero until the actual value is known.
804 Zero is otherwise not a valid section offset. */
805 sect_offset type_offset_in_section;
0186c6a7
DE
806
807 /* Type units are grouped by their DW_AT_stmt_list entry so that they
808 can share them. This points to the containing symtab. */
809 struct type_unit_group *type_unit_group;
ac9ec31b
DE
810
811 /* The type.
812 The first time we encounter this type we fully read it in and install it
813 in the symbol tables. Subsequent times we only need the type. */
814 struct type *type;
a2ce51a0
DE
815
816 /* Containing DWO unit.
817 This field is valid iff per_cu.reading_dwo_directly. */
818 struct dwo_unit *dwo_unit;
348e048f
DE
819};
820
0186c6a7
DE
821typedef struct signatured_type *sig_type_ptr;
822DEF_VEC_P (sig_type_ptr);
823
094b34ac
DE
824/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
825 This includes type_unit_group and quick_file_names. */
826
827struct stmt_list_hash
828{
829 /* The DWO unit this table is from or NULL if there is none. */
830 struct dwo_unit *dwo_unit;
831
832 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 833 sect_offset line_sect_off;
094b34ac
DE
834};
835
f4dc4d17
DE
836/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
837 an object of this type. */
838
839struct type_unit_group
840{
0186c6a7 841 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
842 To simplify things we create an artificial CU that "includes" all the
843 type units using this stmt_list so that the rest of the code still has
844 a "per_cu" handle on the symtab.
845 This PER_CU is recognized by having no section. */
8a0459fd 846#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
847 struct dwarf2_per_cu_data per_cu;
848
0186c6a7
DE
849 /* The TUs that share this DW_AT_stmt_list entry.
850 This is added to while parsing type units to build partial symtabs,
851 and is deleted afterwards and not used again. */
852 VEC (sig_type_ptr) *tus;
f4dc4d17 853
43f3e411 854 /* The compunit symtab.
094b34ac 855 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
856 so we create an essentially anonymous symtab as the compunit symtab. */
857 struct compunit_symtab *compunit_symtab;
f4dc4d17 858
094b34ac
DE
859 /* The data used to construct the hash key. */
860 struct stmt_list_hash hash;
f4dc4d17
DE
861
862 /* The number of symtabs from the line header.
863 The value here must match line_header.num_file_names. */
864 unsigned int num_symtabs;
865
866 /* The symbol tables for this TU (obtained from the files listed in
867 DW_AT_stmt_list).
868 WARNING: The order of entries here must match the order of entries
869 in the line header. After the first TU using this type_unit_group, the
870 line header for the subsequent TUs is recreated from this. This is done
871 because we need to use the same symtabs for each TU using the same
872 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
873 there's no guarantee the line header doesn't have duplicate entries. */
874 struct symtab **symtabs;
875};
876
73869dc2 877/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
878
879struct dwo_sections
880{
881 struct dwarf2_section_info abbrev;
3019eac3
DE
882 struct dwarf2_section_info line;
883 struct dwarf2_section_info loc;
43988095 884 struct dwarf2_section_info loclists;
09262596
DE
885 struct dwarf2_section_info macinfo;
886 struct dwarf2_section_info macro;
3019eac3
DE
887 struct dwarf2_section_info str;
888 struct dwarf2_section_info str_offsets;
80626a55
DE
889 /* In the case of a virtual DWO file, these two are unused. */
890 struct dwarf2_section_info info;
3019eac3
DE
891 VEC (dwarf2_section_info_def) *types;
892};
893
c88ee1f0 894/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
895
896struct dwo_unit
897{
898 /* Backlink to the containing struct dwo_file. */
899 struct dwo_file *dwo_file;
900
901 /* The "id" that distinguishes this CU/TU.
902 .debug_info calls this "dwo_id", .debug_types calls this "signature".
903 Since signatures came first, we stick with it for consistency. */
904 ULONGEST signature;
905
906 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 907 struct dwarf2_section_info *section;
3019eac3 908
9c541725
PA
909 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
910 sect_offset sect_off;
3019eac3
DE
911 unsigned int length;
912
913 /* For types, offset in the type's DIE of the type defined by this TU. */
914 cu_offset type_offset_in_tu;
915};
916
73869dc2
DE
917/* include/dwarf2.h defines the DWP section codes.
918 It defines a max value but it doesn't define a min value, which we
919 use for error checking, so provide one. */
920
921enum dwp_v2_section_ids
922{
923 DW_SECT_MIN = 1
924};
925
80626a55 926/* Data for one DWO file.
57d63ce2
DE
927
928 This includes virtual DWO files (a virtual DWO file is a DWO file as it
929 appears in a DWP file). DWP files don't really have DWO files per se -
930 comdat folding of types "loses" the DWO file they came from, and from
931 a high level view DWP files appear to contain a mass of random types.
932 However, to maintain consistency with the non-DWP case we pretend DWP
933 files contain virtual DWO files, and we assign each TU with one virtual
934 DWO file (generally based on the line and abbrev section offsets -
935 a heuristic that seems to work in practice). */
3019eac3
DE
936
937struct dwo_file
938{
0ac5b59e 939 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
940 For virtual DWO files the name is constructed from the section offsets
941 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
942 from related CU+TUs. */
0ac5b59e
DE
943 const char *dwo_name;
944
945 /* The DW_AT_comp_dir attribute. */
946 const char *comp_dir;
3019eac3 947
80626a55
DE
948 /* The bfd, when the file is open. Otherwise this is NULL.
949 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
950 bfd *dbfd;
3019eac3 951
73869dc2
DE
952 /* The sections that make up this DWO file.
953 Remember that for virtual DWO files in DWP V2, these are virtual
954 sections (for lack of a better name). */
3019eac3
DE
955 struct dwo_sections sections;
956
33c5cd75
DB
957 /* The CUs in the file.
958 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
959 an extension to handle LLVM's Link Time Optimization output (where
960 multiple source files may be compiled into a single object/dwo pair). */
961 htab_t cus;
3019eac3
DE
962
963 /* Table of TUs in the file.
964 Each element is a struct dwo_unit. */
965 htab_t tus;
966};
967
80626a55
DE
968/* These sections are what may appear in a DWP file. */
969
970struct dwp_sections
971{
73869dc2 972 /* These are used by both DWP version 1 and 2. */
80626a55
DE
973 struct dwarf2_section_info str;
974 struct dwarf2_section_info cu_index;
975 struct dwarf2_section_info tu_index;
73869dc2
DE
976
977 /* These are only used by DWP version 2 files.
978 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
979 sections are referenced by section number, and are not recorded here.
980 In DWP version 2 there is at most one copy of all these sections, each
981 section being (effectively) comprised of the concatenation of all of the
982 individual sections that exist in the version 1 format.
983 To keep the code simple we treat each of these concatenated pieces as a
984 section itself (a virtual section?). */
985 struct dwarf2_section_info abbrev;
986 struct dwarf2_section_info info;
987 struct dwarf2_section_info line;
988 struct dwarf2_section_info loc;
989 struct dwarf2_section_info macinfo;
990 struct dwarf2_section_info macro;
991 struct dwarf2_section_info str_offsets;
992 struct dwarf2_section_info types;
80626a55
DE
993};
994
73869dc2
DE
995/* These sections are what may appear in a virtual DWO file in DWP version 1.
996 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 997
73869dc2 998struct virtual_v1_dwo_sections
80626a55
DE
999{
1000 struct dwarf2_section_info abbrev;
1001 struct dwarf2_section_info line;
1002 struct dwarf2_section_info loc;
1003 struct dwarf2_section_info macinfo;
1004 struct dwarf2_section_info macro;
1005 struct dwarf2_section_info str_offsets;
1006 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 1007 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
1008 struct dwarf2_section_info info_or_types;
1009};
1010
73869dc2
DE
1011/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1012 In version 2, the sections of the DWO files are concatenated together
1013 and stored in one section of that name. Thus each ELF section contains
1014 several "virtual" sections. */
1015
1016struct virtual_v2_dwo_sections
1017{
1018 bfd_size_type abbrev_offset;
1019 bfd_size_type abbrev_size;
1020
1021 bfd_size_type line_offset;
1022 bfd_size_type line_size;
1023
1024 bfd_size_type loc_offset;
1025 bfd_size_type loc_size;
1026
1027 bfd_size_type macinfo_offset;
1028 bfd_size_type macinfo_size;
1029
1030 bfd_size_type macro_offset;
1031 bfd_size_type macro_size;
1032
1033 bfd_size_type str_offsets_offset;
1034 bfd_size_type str_offsets_size;
1035
1036 /* Each DWP hash table entry records one CU or one TU.
1037 That is recorded here, and copied to dwo_unit.section. */
1038 bfd_size_type info_or_types_offset;
1039 bfd_size_type info_or_types_size;
1040};
1041
80626a55
DE
1042/* Contents of DWP hash tables. */
1043
1044struct dwp_hash_table
1045{
73869dc2 1046 uint32_t version, nr_columns;
80626a55 1047 uint32_t nr_units, nr_slots;
73869dc2
DE
1048 const gdb_byte *hash_table, *unit_table;
1049 union
1050 {
1051 struct
1052 {
1053 const gdb_byte *indices;
1054 } v1;
1055 struct
1056 {
1057 /* This is indexed by column number and gives the id of the section
1058 in that column. */
1059#define MAX_NR_V2_DWO_SECTIONS \
1060 (1 /* .debug_info or .debug_types */ \
1061 + 1 /* .debug_abbrev */ \
1062 + 1 /* .debug_line */ \
1063 + 1 /* .debug_loc */ \
1064 + 1 /* .debug_str_offsets */ \
1065 + 1 /* .debug_macro or .debug_macinfo */)
1066 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1067 const gdb_byte *offsets;
1068 const gdb_byte *sizes;
1069 } v2;
1070 } section_pool;
80626a55
DE
1071};
1072
1073/* Data for one DWP file. */
1074
1075struct dwp_file
1076{
1077 /* Name of the file. */
1078 const char *name;
1079
73869dc2
DE
1080 /* File format version. */
1081 int version;
1082
93417882 1083 /* The bfd. */
80626a55
DE
1084 bfd *dbfd;
1085
1086 /* Section info for this file. */
1087 struct dwp_sections sections;
1088
57d63ce2 1089 /* Table of CUs in the file. */
80626a55
DE
1090 const struct dwp_hash_table *cus;
1091
1092 /* Table of TUs in the file. */
1093 const struct dwp_hash_table *tus;
1094
19ac8c2e
DE
1095 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1096 htab_t loaded_cus;
1097 htab_t loaded_tus;
80626a55 1098
73869dc2
DE
1099 /* Table to map ELF section numbers to their sections.
1100 This is only needed for the DWP V1 file format. */
80626a55
DE
1101 unsigned int num_sections;
1102 asection **elf_sections;
1103};
1104
36586728
TT
1105/* This represents a '.dwz' file. */
1106
1107struct dwz_file
1108{
1109 /* A dwz file can only contain a few sections. */
1110 struct dwarf2_section_info abbrev;
1111 struct dwarf2_section_info info;
1112 struct dwarf2_section_info str;
1113 struct dwarf2_section_info line;
1114 struct dwarf2_section_info macro;
2ec9a5e0 1115 struct dwarf2_section_info gdb_index;
36586728
TT
1116
1117 /* The dwz's BFD. */
1118 bfd *dwz_bfd;
1119};
1120
0963b4bd
MS
1121/* Struct used to pass misc. parameters to read_die_and_children, et
1122 al. which are used for both .debug_info and .debug_types dies.
1123 All parameters here are unchanging for the life of the call. This
dee91e82 1124 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1125
1126struct die_reader_specs
1127{
a32a8923 1128 /* The bfd of die_section. */
93311388
DE
1129 bfd* abfd;
1130
1131 /* The CU of the DIE we are parsing. */
1132 struct dwarf2_cu *cu;
1133
80626a55 1134 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1135 struct dwo_file *dwo_file;
1136
dee91e82 1137 /* The section the die comes from.
3019eac3 1138 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1139 struct dwarf2_section_info *die_section;
1140
1141 /* die_section->buffer. */
d521ce57 1142 const gdb_byte *buffer;
f664829e
DE
1143
1144 /* The end of the buffer. */
1145 const gdb_byte *buffer_end;
a2ce51a0
DE
1146
1147 /* The value of the DW_AT_comp_dir attribute. */
1148 const char *comp_dir;
93311388
DE
1149};
1150
fd820528 1151/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1152typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1153 const gdb_byte *info_ptr,
dee91e82
DE
1154 struct die_info *comp_unit_die,
1155 int has_children,
1156 void *data);
1157
ecfb656c
PA
1158/* A 1-based directory index. This is a strong typedef to prevent
1159 accidentally using a directory index as a 0-based index into an
1160 array/vector. */
1161enum class dir_index : unsigned int {};
1162
1163/* Likewise, a 1-based file name index. */
1164enum class file_name_index : unsigned int {};
1165
52059ffd
TT
1166struct file_entry
1167{
fff8551c
PA
1168 file_entry () = default;
1169
ecfb656c 1170 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
1171 unsigned int mod_time_, unsigned int length_)
1172 : name (name_),
ecfb656c 1173 d_index (d_index_),
fff8551c
PA
1174 mod_time (mod_time_),
1175 length (length_)
1176 {}
1177
ecfb656c
PA
1178 /* Return the include directory at D_INDEX stored in LH. Returns
1179 NULL if D_INDEX is out of bounds. */
8c43009f
PA
1180 const char *include_dir (const line_header *lh) const;
1181
fff8551c
PA
1182 /* The file name. Note this is an observing pointer. The memory is
1183 owned by debug_line_buffer. */
1184 const char *name {};
1185
8c43009f 1186 /* The directory index (1-based). */
ecfb656c 1187 dir_index d_index {};
fff8551c
PA
1188
1189 unsigned int mod_time {};
1190
1191 unsigned int length {};
1192
1193 /* True if referenced by the Line Number Program. */
1194 bool included_p {};
1195
83769d0b 1196 /* The associated symbol table, if any. */
fff8551c 1197 struct symtab *symtab {};
52059ffd
TT
1198};
1199
debd256d
JB
1200/* The line number information for a compilation unit (found in the
1201 .debug_line section) begins with a "statement program header",
1202 which contains the following information. */
1203struct line_header
1204{
fff8551c
PA
1205 line_header ()
1206 : offset_in_dwz {}
1207 {}
1208
1209 /* Add an entry to the include directory table. */
1210 void add_include_dir (const char *include_dir);
1211
1212 /* Add an entry to the file name table. */
ecfb656c 1213 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1214 unsigned int mod_time, unsigned int length);
1215
ecfb656c 1216 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 1217 is out of bounds. */
ecfb656c 1218 const char *include_dir_at (dir_index index) const
8c43009f 1219 {
ecfb656c
PA
1220 /* Convert directory index number (1-based) to vector index
1221 (0-based). */
1222 size_t vec_index = to_underlying (index) - 1;
1223
1224 if (vec_index >= include_dirs.size ())
8c43009f 1225 return NULL;
ecfb656c 1226 return include_dirs[vec_index];
8c43009f
PA
1227 }
1228
ecfb656c 1229 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 1230 is out of bounds. */
ecfb656c 1231 file_entry *file_name_at (file_name_index index)
8c43009f 1232 {
ecfb656c
PA
1233 /* Convert file name index number (1-based) to vector index
1234 (0-based). */
1235 size_t vec_index = to_underlying (index) - 1;
1236
1237 if (vec_index >= file_names.size ())
fff8551c 1238 return NULL;
ecfb656c 1239 return &file_names[vec_index];
fff8551c
PA
1240 }
1241
1242 /* Const version of the above. */
1243 const file_entry *file_name_at (unsigned int index) const
1244 {
1245 if (index >= file_names.size ())
8c43009f
PA
1246 return NULL;
1247 return &file_names[index];
1248 }
1249
527f3840 1250 /* Offset of line number information in .debug_line section. */
9c541725 1251 sect_offset sect_off {};
527f3840
JK
1252
1253 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1254 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1255
1256 unsigned int total_length {};
1257 unsigned short version {};
1258 unsigned int header_length {};
1259 unsigned char minimum_instruction_length {};
1260 unsigned char maximum_ops_per_instruction {};
1261 unsigned char default_is_stmt {};
1262 int line_base {};
1263 unsigned char line_range {};
1264 unsigned char opcode_base {};
debd256d
JB
1265
1266 /* standard_opcode_lengths[i] is the number of operands for the
1267 standard opcode whose value is i. This means that
1268 standard_opcode_lengths[0] is unused, and the last meaningful
1269 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1270 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1271
fff8551c
PA
1272 /* The include_directories table. Note these are observing
1273 pointers. The memory is owned by debug_line_buffer. */
1274 std::vector<const char *> include_dirs;
debd256d 1275
fff8551c
PA
1276 /* The file_names table. */
1277 std::vector<file_entry> file_names;
debd256d
JB
1278
1279 /* The start and end of the statement program following this
6502dd73 1280 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1281 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1282};
c906108c 1283
fff8551c
PA
1284typedef std::unique_ptr<line_header> line_header_up;
1285
8c43009f
PA
1286const char *
1287file_entry::include_dir (const line_header *lh) const
1288{
ecfb656c 1289 return lh->include_dir_at (d_index);
8c43009f
PA
1290}
1291
c906108c 1292/* When we construct a partial symbol table entry we only
0963b4bd 1293 need this much information. */
c906108c
SS
1294struct partial_die_info
1295 {
72bf9492 1296 /* Offset of this DIE. */
9c541725 1297 sect_offset sect_off;
72bf9492
DJ
1298
1299 /* DWARF-2 tag for this DIE. */
1300 ENUM_BITFIELD(dwarf_tag) tag : 16;
1301
72bf9492
DJ
1302 /* Assorted flags describing the data found in this DIE. */
1303 unsigned int has_children : 1;
1304 unsigned int is_external : 1;
1305 unsigned int is_declaration : 1;
1306 unsigned int has_type : 1;
1307 unsigned int has_specification : 1;
1308 unsigned int has_pc_info : 1;
481860b3 1309 unsigned int may_be_inlined : 1;
72bf9492 1310
0c1b455e
TT
1311 /* This DIE has been marked DW_AT_main_subprogram. */
1312 unsigned int main_subprogram : 1;
1313
72bf9492
DJ
1314 /* Flag set if the SCOPE field of this structure has been
1315 computed. */
1316 unsigned int scope_set : 1;
1317
fa4028e9
JB
1318 /* Flag set if the DIE has a byte_size attribute. */
1319 unsigned int has_byte_size : 1;
1320
ff908ebf
AW
1321 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1322 unsigned int has_const_value : 1;
1323
98bfdba5
PA
1324 /* Flag set if any of the DIE's children are template arguments. */
1325 unsigned int has_template_arguments : 1;
1326
abc72ce4
DE
1327 /* Flag set if fixup_partial_die has been called on this die. */
1328 unsigned int fixup_called : 1;
1329
36586728
TT
1330 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1331 unsigned int is_dwz : 1;
1332
1333 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1334 unsigned int spec_is_dwz : 1;
1335
72bf9492 1336 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1337 sometimes a default name for unnamed DIEs. */
15d034d0 1338 const char *name;
72bf9492 1339
abc72ce4
DE
1340 /* The linkage name, if present. */
1341 const char *linkage_name;
1342
72bf9492
DJ
1343 /* The scope to prepend to our children. This is generally
1344 allocated on the comp_unit_obstack, so will disappear
1345 when this compilation unit leaves the cache. */
15d034d0 1346 const char *scope;
72bf9492 1347
95554aad
TT
1348 /* Some data associated with the partial DIE. The tag determines
1349 which field is live. */
1350 union
1351 {
1352 /* The location description associated with this DIE, if any. */
1353 struct dwarf_block *locdesc;
1354 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1355 sect_offset sect_off;
95554aad 1356 } d;
72bf9492
DJ
1357
1358 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1359 CORE_ADDR lowpc;
1360 CORE_ADDR highpc;
72bf9492 1361
93311388 1362 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1363 DW_AT_sibling, if any. */
abc72ce4
DE
1364 /* NOTE: This member isn't strictly necessary, read_partial_die could
1365 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1366 const gdb_byte *sibling;
72bf9492
DJ
1367
1368 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1369 DW_AT_specification (or DW_AT_abstract_origin or
1370 DW_AT_extension). */
b64f50a1 1371 sect_offset spec_offset;
72bf9492
DJ
1372
1373 /* Pointers to this DIE's parent, first child, and next sibling,
1374 if any. */
1375 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1376 };
1377
0963b4bd 1378/* This data structure holds the information of an abbrev. */
c906108c
SS
1379struct abbrev_info
1380 {
1381 unsigned int number; /* number identifying abbrev */
1382 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1383 unsigned short has_children; /* boolean */
1384 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1385 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1386 struct abbrev_info *next; /* next in chain */
1387 };
1388
1389struct attr_abbrev
1390 {
9d25dd43
DE
1391 ENUM_BITFIELD(dwarf_attribute) name : 16;
1392 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1393
1394 /* It is valid only if FORM is DW_FORM_implicit_const. */
1395 LONGEST implicit_const;
c906108c
SS
1396 };
1397
433df2d4
DE
1398/* Size of abbrev_table.abbrev_hash_table. */
1399#define ABBREV_HASH_SIZE 121
1400
1401/* Top level data structure to contain an abbreviation table. */
1402
1403struct abbrev_table
1404{
f4dc4d17
DE
1405 /* Where the abbrev table came from.
1406 This is used as a sanity check when the table is used. */
9c541725 1407 sect_offset sect_off;
433df2d4
DE
1408
1409 /* Storage for the abbrev table. */
1410 struct obstack abbrev_obstack;
1411
1412 /* Hash table of abbrevs.
1413 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1414 It could be statically allocated, but the previous code didn't so we
1415 don't either. */
1416 struct abbrev_info **abbrevs;
1417};
1418
0963b4bd 1419/* Attributes have a name and a value. */
b60c80d6
DJ
1420struct attribute
1421 {
9d25dd43 1422 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1423 ENUM_BITFIELD(dwarf_form) form : 15;
1424
1425 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1426 field should be in u.str (existing only for DW_STRING) but it is kept
1427 here for better struct attribute alignment. */
1428 unsigned int string_is_canonical : 1;
1429
b60c80d6
DJ
1430 union
1431 {
15d034d0 1432 const char *str;
b60c80d6 1433 struct dwarf_block *blk;
43bbcdc2
PH
1434 ULONGEST unsnd;
1435 LONGEST snd;
b60c80d6 1436 CORE_ADDR addr;
ac9ec31b 1437 ULONGEST signature;
b60c80d6
DJ
1438 }
1439 u;
1440 };
1441
0963b4bd 1442/* This data structure holds a complete die structure. */
c906108c
SS
1443struct die_info
1444 {
76815b17
DE
1445 /* DWARF-2 tag for this DIE. */
1446 ENUM_BITFIELD(dwarf_tag) tag : 16;
1447
1448 /* Number of attributes */
98bfdba5
PA
1449 unsigned char num_attrs;
1450
1451 /* True if we're presently building the full type name for the
1452 type derived from this DIE. */
1453 unsigned char building_fullname : 1;
76815b17 1454
adde2bff
DE
1455 /* True if this die is in process. PR 16581. */
1456 unsigned char in_process : 1;
1457
76815b17
DE
1458 /* Abbrev number */
1459 unsigned int abbrev;
1460
93311388 1461 /* Offset in .debug_info or .debug_types section. */
9c541725 1462 sect_offset sect_off;
78ba4af6
JB
1463
1464 /* The dies in a compilation unit form an n-ary tree. PARENT
1465 points to this die's parent; CHILD points to the first child of
1466 this node; and all the children of a given node are chained
4950bc1c 1467 together via their SIBLING fields. */
639d11d3
DC
1468 struct die_info *child; /* Its first child, if any. */
1469 struct die_info *sibling; /* Its next sibling, if any. */
1470 struct die_info *parent; /* Its parent, if any. */
c906108c 1471
b60c80d6
DJ
1472 /* An array of attributes, with NUM_ATTRS elements. There may be
1473 zero, but it's not common and zero-sized arrays are not
1474 sufficiently portable C. */
1475 struct attribute attrs[1];
c906108c
SS
1476 };
1477
0963b4bd 1478/* Get at parts of an attribute structure. */
c906108c
SS
1479
1480#define DW_STRING(attr) ((attr)->u.str)
8285870a 1481#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1482#define DW_UNSND(attr) ((attr)->u.unsnd)
1483#define DW_BLOCK(attr) ((attr)->u.blk)
1484#define DW_SND(attr) ((attr)->u.snd)
1485#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1486#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1487
0963b4bd 1488/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1489struct dwarf_block
1490 {
56eb65bd 1491 size_t size;
1d6edc3c
JK
1492
1493 /* Valid only if SIZE is not zero. */
d521ce57 1494 const gdb_byte *data;
c906108c
SS
1495 };
1496
c906108c
SS
1497#ifndef ATTR_ALLOC_CHUNK
1498#define ATTR_ALLOC_CHUNK 4
1499#endif
1500
c906108c
SS
1501/* Allocate fields for structs, unions and enums in this size. */
1502#ifndef DW_FIELD_ALLOC_CHUNK
1503#define DW_FIELD_ALLOC_CHUNK 4
1504#endif
1505
c906108c
SS
1506/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1507 but this would require a corresponding change in unpack_field_as_long
1508 and friends. */
1509static int bits_per_byte = 8;
1510
52059ffd
TT
1511struct nextfield
1512{
1513 struct nextfield *next;
1514 int accessibility;
1515 int virtuality;
1516 struct field field;
1517};
1518
1519struct nextfnfield
1520{
1521 struct nextfnfield *next;
1522 struct fn_field fnfield;
1523};
1524
1525struct fnfieldlist
1526{
1527 const char *name;
1528 int length;
1529 struct nextfnfield *head;
1530};
1531
1532struct typedef_field_list
1533{
1534 struct typedef_field field;
1535 struct typedef_field_list *next;
1536};
1537
c906108c
SS
1538/* The routines that read and process dies for a C struct or C++ class
1539 pass lists of data member fields and lists of member function fields
1540 in an instance of a field_info structure, as defined below. */
1541struct field_info
c5aa993b 1542 {
0963b4bd 1543 /* List of data member and baseclasses fields. */
52059ffd 1544 struct nextfield *fields, *baseclasses;
c906108c 1545
7d0ccb61 1546 /* Number of fields (including baseclasses). */
c5aa993b 1547 int nfields;
c906108c 1548
c5aa993b
JM
1549 /* Number of baseclasses. */
1550 int nbaseclasses;
c906108c 1551
c5aa993b
JM
1552 /* Set if the accesibility of one of the fields is not public. */
1553 int non_public_fields;
c906108c 1554
c5aa993b
JM
1555 /* Member function fieldlist array, contains name of possibly overloaded
1556 member function, number of overloaded member functions and a pointer
1557 to the head of the member function field chain. */
52059ffd 1558 struct fnfieldlist *fnfieldlists;
c906108c 1559
c5aa993b
JM
1560 /* Number of entries in the fnfieldlists array. */
1561 int nfnfields;
98751a41
JK
1562
1563 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1564 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
52059ffd 1565 struct typedef_field_list *typedef_field_list;
98751a41 1566 unsigned typedef_field_list_count;
c5aa993b 1567 };
c906108c 1568
10b3939b
DJ
1569/* One item on the queue of compilation units to read in full symbols
1570 for. */
1571struct dwarf2_queue_item
1572{
1573 struct dwarf2_per_cu_data *per_cu;
95554aad 1574 enum language pretend_language;
10b3939b
DJ
1575 struct dwarf2_queue_item *next;
1576};
1577
1578/* The current queue. */
1579static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1580
ae038cb0
DJ
1581/* Loaded secondary compilation units are kept in memory until they
1582 have not been referenced for the processing of this many
1583 compilation units. Set this to zero to disable caching. Cache
1584 sizes of up to at least twenty will improve startup time for
1585 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1586static int dwarf_max_cache_age = 5;
920d2a44 1587static void
b4f54984
DE
1588show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1589 struct cmd_list_element *c, const char *value)
920d2a44 1590{
3e43a32a 1591 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1592 "DWARF compilation units is %s.\n"),
920d2a44
AC
1593 value);
1594}
4390d890 1595\f
c906108c
SS
1596/* local function prototypes */
1597
a32a8923
DE
1598static const char *get_section_name (const struct dwarf2_section_info *);
1599
1600static const char *get_section_file_name (const struct dwarf2_section_info *);
1601
918dd910
JK
1602static void dwarf2_find_base_address (struct die_info *die,
1603 struct dwarf2_cu *cu);
1604
0018ea6f
DE
1605static struct partial_symtab *create_partial_symtab
1606 (struct dwarf2_per_cu_data *per_cu, const char *name);
1607
f1902523
JK
1608static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1609 const gdb_byte *info_ptr,
1610 struct die_info *type_unit_die,
1611 int has_children, void *data);
1612
c67a9c90 1613static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1614
72bf9492
DJ
1615static void scan_partial_symbols (struct partial_die_info *,
1616 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1617 int, struct dwarf2_cu *);
c906108c 1618
72bf9492
DJ
1619static void add_partial_symbol (struct partial_die_info *,
1620 struct dwarf2_cu *);
63d06c5c 1621
72bf9492
DJ
1622static void add_partial_namespace (struct partial_die_info *pdi,
1623 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1624 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1625
5d7cb8df 1626static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1627 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1628 struct dwarf2_cu *cu);
1629
72bf9492
DJ
1630static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1631 struct dwarf2_cu *cu);
91c24f0a 1632
bc30ff58
JB
1633static void add_partial_subprogram (struct partial_die_info *pdi,
1634 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1635 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1636
257e7a09
YQ
1637static void dwarf2_read_symtab (struct partial_symtab *,
1638 struct objfile *);
c906108c 1639
a14ed312 1640static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1641
433df2d4
DE
1642static struct abbrev_info *abbrev_table_lookup_abbrev
1643 (const struct abbrev_table *, unsigned int);
1644
1645static struct abbrev_table *abbrev_table_read_table
1646 (struct dwarf2_section_info *, sect_offset);
1647
1648static void abbrev_table_free (struct abbrev_table *);
1649
f4dc4d17
DE
1650static void abbrev_table_free_cleanup (void *);
1651
dee91e82
DE
1652static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1653 struct dwarf2_section_info *);
c906108c 1654
f3dd6933 1655static void dwarf2_free_abbrev_table (void *);
c906108c 1656
d521ce57 1657static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1658
dee91e82 1659static struct partial_die_info *load_partial_dies
d521ce57 1660 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1661
d521ce57
TT
1662static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1663 struct partial_die_info *,
1664 struct abbrev_info *,
1665 unsigned int,
1666 const gdb_byte *);
c906108c 1667
36586728 1668static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1669 struct dwarf2_cu *);
72bf9492
DJ
1670
1671static void fixup_partial_die (struct partial_die_info *,
1672 struct dwarf2_cu *);
1673
d521ce57
TT
1674static const gdb_byte *read_attribute (const struct die_reader_specs *,
1675 struct attribute *, struct attr_abbrev *,
1676 const gdb_byte *);
a8329558 1677
a1855c1d 1678static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1679
a1855c1d 1680static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1681
a1855c1d 1682static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1683
a1855c1d 1684static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1685
a1855c1d 1686static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1687
d521ce57 1688static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1689 unsigned int *);
c906108c 1690
d521ce57 1691static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1692
1693static LONGEST read_checked_initial_length_and_offset
d521ce57 1694 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1695 unsigned int *, unsigned int *);
613e1657 1696
d521ce57
TT
1697static LONGEST read_offset (bfd *, const gdb_byte *,
1698 const struct comp_unit_head *,
c764a876
DE
1699 unsigned int *);
1700
d521ce57 1701static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1702
f4dc4d17
DE
1703static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1704 sect_offset);
1705
d521ce57 1706static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1707
d521ce57 1708static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1709
d521ce57
TT
1710static const char *read_indirect_string (bfd *, const gdb_byte *,
1711 const struct comp_unit_head *,
1712 unsigned int *);
4bdf3d34 1713
43988095
JK
1714static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1715 const struct comp_unit_head *,
1716 unsigned int *);
36586728 1717
43988095 1718static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
c906108c 1719
d521ce57 1720static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1721
d521ce57
TT
1722static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1723 const gdb_byte *,
3019eac3
DE
1724 unsigned int *);
1725
d521ce57 1726static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1727 ULONGEST str_index);
3019eac3 1728
e142c38c 1729static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1730
e142c38c
DJ
1731static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1732 struct dwarf2_cu *);
c906108c 1733
348e048f 1734static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1735 unsigned int);
348e048f 1736
7d45c7c3
KB
1737static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1738 struct dwarf2_cu *cu);
1739
05cf31d1
JB
1740static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1741 struct dwarf2_cu *cu);
1742
e142c38c 1743static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1744
e142c38c 1745static struct die_info *die_specification (struct die_info *die,
f2f0e013 1746 struct dwarf2_cu **);
63d06c5c 1747
9c541725 1748static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1749 struct dwarf2_cu *cu);
debd256d 1750
f3f5162e 1751static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1752 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1753 CORE_ADDR, int decode_mapping);
c906108c 1754
4d663531 1755static void dwarf2_start_subfile (const char *, const char *);
c906108c 1756
43f3e411
DE
1757static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1758 const char *, const char *,
1759 CORE_ADDR);
f4dc4d17 1760
a14ed312 1761static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1762 struct dwarf2_cu *);
c906108c 1763
34eaf542
TT
1764static struct symbol *new_symbol_full (struct die_info *, struct type *,
1765 struct dwarf2_cu *, struct symbol *);
1766
ff39bb5e 1767static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1768 struct dwarf2_cu *);
c906108c 1769
ff39bb5e 1770static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1771 struct type *type,
1772 const char *name,
1773 struct obstack *obstack,
12df843f 1774 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1775 const gdb_byte **bytes,
98bfdba5 1776 struct dwarf2_locexpr_baton **baton);
2df3850c 1777
e7c27a73 1778static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1779
b4ba55a1
JB
1780static int need_gnat_info (struct dwarf2_cu *);
1781
3e43a32a
MS
1782static struct type *die_descriptive_type (struct die_info *,
1783 struct dwarf2_cu *);
b4ba55a1
JB
1784
1785static void set_descriptive_type (struct type *, struct die_info *,
1786 struct dwarf2_cu *);
1787
e7c27a73
DJ
1788static struct type *die_containing_type (struct die_info *,
1789 struct dwarf2_cu *);
c906108c 1790
ff39bb5e 1791static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1792 struct dwarf2_cu *);
c906108c 1793
f792889a 1794static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1795
673bfd45
DE
1796static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1797
0d5cff50 1798static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1799
6e70227d 1800static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1801 const char *suffix, int physname,
1802 struct dwarf2_cu *cu);
63d06c5c 1803
e7c27a73 1804static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1805
348e048f
DE
1806static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1807
e7c27a73 1808static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1809
e7c27a73 1810static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1811
96408a79
SA
1812static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1813
71a3c369
TT
1814static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1815
ff013f42
JK
1816static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1817 struct dwarf2_cu *, struct partial_symtab *);
1818
3a2b436a 1819/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1820 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1821enum pc_bounds_kind
1822{
e385593e 1823 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1824 PC_BOUNDS_NOT_PRESENT,
1825
e385593e
JK
1826 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1827 were present but they do not form a valid range of PC addresses. */
1828 PC_BOUNDS_INVALID,
1829
3a2b436a
JK
1830 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1831 PC_BOUNDS_RANGES,
1832
1833 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1834 PC_BOUNDS_HIGH_LOW,
1835};
1836
1837static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1838 CORE_ADDR *, CORE_ADDR *,
1839 struct dwarf2_cu *,
1840 struct partial_symtab *);
c906108c 1841
fae299cd
DC
1842static void get_scope_pc_bounds (struct die_info *,
1843 CORE_ADDR *, CORE_ADDR *,
1844 struct dwarf2_cu *);
1845
801e3a5b
JB
1846static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1847 CORE_ADDR, struct dwarf2_cu *);
1848
a14ed312 1849static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1850 struct dwarf2_cu *);
c906108c 1851
a14ed312 1852static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1853 struct type *, struct dwarf2_cu *);
c906108c 1854
a14ed312 1855static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1856 struct die_info *, struct type *,
e7c27a73 1857 struct dwarf2_cu *);
c906108c 1858
a14ed312 1859static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1860 struct type *,
1861 struct dwarf2_cu *);
c906108c 1862
134d01f1 1863static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1864
e7c27a73 1865static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1866
e7c27a73 1867static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1868
5d7cb8df
JK
1869static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1870
22cee43f
PMR
1871static struct using_direct **using_directives (enum language);
1872
27aa8d6a
SW
1873static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1874
74921315
KS
1875static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1876
f55ee35c
JK
1877static struct type *read_module_type (struct die_info *die,
1878 struct dwarf2_cu *cu);
1879
38d518c9 1880static const char *namespace_name (struct die_info *die,
e142c38c 1881 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1882
134d01f1 1883static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1884
e7c27a73 1885static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1886
6e70227d 1887static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1888 struct dwarf2_cu *);
1889
bf6af496 1890static struct die_info *read_die_and_siblings_1
d521ce57 1891 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1892 struct die_info *);
639d11d3 1893
dee91e82 1894static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1895 const gdb_byte *info_ptr,
1896 const gdb_byte **new_info_ptr,
639d11d3
DC
1897 struct die_info *parent);
1898
d521ce57
TT
1899static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1900 struct die_info **, const gdb_byte *,
1901 int *, int);
3019eac3 1902
d521ce57
TT
1903static const gdb_byte *read_full_die (const struct die_reader_specs *,
1904 struct die_info **, const gdb_byte *,
1905 int *);
93311388 1906
e7c27a73 1907static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1908
15d034d0
TT
1909static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1910 struct obstack *);
71c25dea 1911
15d034d0 1912static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1913
15d034d0 1914static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1915 struct die_info *die,
1916 struct dwarf2_cu *cu);
1917
ca69b9e6
DE
1918static const char *dwarf2_physname (const char *name, struct die_info *die,
1919 struct dwarf2_cu *cu);
1920
e142c38c 1921static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1922 struct dwarf2_cu **);
9219021c 1923
f39c6ffd 1924static const char *dwarf_tag_name (unsigned int);
c906108c 1925
f39c6ffd 1926static const char *dwarf_attr_name (unsigned int);
c906108c 1927
f39c6ffd 1928static const char *dwarf_form_name (unsigned int);
c906108c 1929
a121b7c1 1930static const char *dwarf_bool_name (unsigned int);
c906108c 1931
f39c6ffd 1932static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1933
f9aca02d 1934static struct die_info *sibling_die (struct die_info *);
c906108c 1935
d97bc12b
DE
1936static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1937
1938static void dump_die_for_error (struct die_info *);
1939
1940static void dump_die_1 (struct ui_file *, int level, int max_level,
1941 struct die_info *);
c906108c 1942
d97bc12b 1943/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1944
51545339 1945static void store_in_ref_table (struct die_info *,
10b3939b 1946 struct dwarf2_cu *);
c906108c 1947
ff39bb5e 1948static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1949
ff39bb5e 1950static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1951
348e048f 1952static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1953 const struct attribute *,
348e048f
DE
1954 struct dwarf2_cu **);
1955
10b3939b 1956static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1957 const struct attribute *,
f2f0e013 1958 struct dwarf2_cu **);
c906108c 1959
348e048f 1960static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1961 const struct attribute *,
348e048f
DE
1962 struct dwarf2_cu **);
1963
ac9ec31b
DE
1964static struct type *get_signatured_type (struct die_info *, ULONGEST,
1965 struct dwarf2_cu *);
1966
1967static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1968 const struct attribute *,
ac9ec31b
DE
1969 struct dwarf2_cu *);
1970
e5fe5e75 1971static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1972
52dc124a 1973static void read_signatured_type (struct signatured_type *);
348e048f 1974
63e43d3a
PMR
1975static int attr_to_dynamic_prop (const struct attribute *attr,
1976 struct die_info *die, struct dwarf2_cu *cu,
1977 struct dynamic_prop *prop);
1978
c906108c
SS
1979/* memory allocation interface */
1980
7b5a2f43 1981static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1982
b60c80d6 1983static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1984
43f3e411 1985static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1986
6e5a29e1 1987static int attr_form_is_block (const struct attribute *);
8e19ed76 1988
6e5a29e1 1989static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1990
6e5a29e1 1991static int attr_form_is_constant (const struct attribute *);
3690dd37 1992
6e5a29e1 1993static int attr_form_is_ref (const struct attribute *);
7771576e 1994
8cf6f0b1
TT
1995static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1996 struct dwarf2_loclist_baton *baton,
ff39bb5e 1997 const struct attribute *attr);
8cf6f0b1 1998
ff39bb5e 1999static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 2000 struct symbol *sym,
f1e6e072
TT
2001 struct dwarf2_cu *cu,
2002 int is_block);
4c2df51b 2003
d521ce57
TT
2004static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2005 const gdb_byte *info_ptr,
2006 struct abbrev_info *abbrev);
4bb7a0a7 2007
72bf9492
DJ
2008static void free_stack_comp_unit (void *);
2009
72bf9492
DJ
2010static hashval_t partial_die_hash (const void *item);
2011
2012static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2013
ae038cb0 2014static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
9c541725 2015 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 2016
9816fde3 2017static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 2018 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
2019
2020static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
2021 struct die_info *comp_unit_die,
2022 enum language pretend_language);
93311388 2023
68dc6402 2024static void free_heap_comp_unit (void *);
ae038cb0
DJ
2025
2026static void free_cached_comp_units (void *);
2027
2028static void age_cached_comp_units (void);
2029
dee91e82 2030static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 2031
f792889a
DJ
2032static struct type *set_die_type (struct die_info *, struct type *,
2033 struct dwarf2_cu *);
1c379e20 2034
ae038cb0
DJ
2035static void create_all_comp_units (struct objfile *);
2036
0e50663e 2037static int create_all_type_units (struct objfile *);
1fd400ff 2038
95554aad
TT
2039static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2040 enum language);
10b3939b 2041
95554aad
TT
2042static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2043 enum language);
10b3939b 2044
f4dc4d17
DE
2045static void process_full_type_unit (struct dwarf2_per_cu_data *,
2046 enum language);
2047
10b3939b
DJ
2048static void dwarf2_add_dependence (struct dwarf2_cu *,
2049 struct dwarf2_per_cu_data *);
2050
ae038cb0
DJ
2051static void dwarf2_mark (struct dwarf2_cu *);
2052
2053static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2054
b64f50a1 2055static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 2056 struct dwarf2_per_cu_data *);
673bfd45 2057
f792889a 2058static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 2059
9291a0cd
TT
2060static void dwarf2_release_queue (void *dummy);
2061
95554aad
TT
2062static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2063 enum language pretend_language);
2064
a0f42c21 2065static void process_queue (void);
9291a0cd 2066
d721ba37
PA
2067/* The return type of find_file_and_directory. Note, the enclosed
2068 string pointers are only valid while this object is valid. */
2069
2070struct file_and_directory
2071{
2072 /* The filename. This is never NULL. */
2073 const char *name;
2074
2075 /* The compilation directory. NULL if not known. If we needed to
2076 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2077 points directly to the DW_AT_comp_dir string attribute owned by
2078 the obstack that owns the DIE. */
2079 const char *comp_dir;
2080
2081 /* If we needed to build a new string for comp_dir, this is what
2082 owns the storage. */
2083 std::string comp_dir_storage;
2084};
2085
2086static file_and_directory find_file_and_directory (struct die_info *die,
2087 struct dwarf2_cu *cu);
9291a0cd
TT
2088
2089static char *file_full_name (int file, struct line_header *lh,
2090 const char *comp_dir);
2091
43988095
JK
2092/* Expected enum dwarf_unit_type for read_comp_unit_head. */
2093enum class rcuh_kind { COMPILE, TYPE };
2094
d521ce57 2095static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
2096 (struct comp_unit_head *header,
2097 struct dwarf2_section_info *section,
d521ce57 2098 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 2099 rcuh_kind section_kind);
36586728 2100
fd820528 2101static void init_cutu_and_read_dies
f4dc4d17
DE
2102 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2103 int use_existing_cu, int keep,
3019eac3
DE
2104 die_reader_func_ftype *die_reader_func, void *data);
2105
dee91e82
DE
2106static void init_cutu_and_read_dies_simple
2107 (struct dwarf2_per_cu_data *this_cu,
2108 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 2109
673bfd45 2110static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2111
3019eac3
DE
2112static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2113
57d63ce2
DE
2114static struct dwo_unit *lookup_dwo_unit_in_dwp
2115 (struct dwp_file *dwp_file, const char *comp_dir,
2116 ULONGEST signature, int is_debug_types);
a2ce51a0
DE
2117
2118static struct dwp_file *get_dwp_file (void);
2119
3019eac3 2120static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2121 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2122
2123static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2124 (struct signatured_type *, const char *, const char *);
3019eac3 2125
89e63ee4
DE
2126static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2127
3019eac3
DE
2128static void free_dwo_file_cleanup (void *);
2129
95554aad
TT
2130static void process_cu_includes (void);
2131
1b80a9fa 2132static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2133
2134static void free_line_header_voidp (void *arg);
4390d890
DE
2135\f
2136/* Various complaints about symbol reading that don't abort the process. */
2137
2138static void
2139dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2140{
2141 complaint (&symfile_complaints,
2142 _("statement list doesn't fit in .debug_line section"));
2143}
2144
2145static void
2146dwarf2_debug_line_missing_file_complaint (void)
2147{
2148 complaint (&symfile_complaints,
2149 _(".debug_line section has line data without a file"));
2150}
2151
2152static void
2153dwarf2_debug_line_missing_end_sequence_complaint (void)
2154{
2155 complaint (&symfile_complaints,
2156 _(".debug_line section has line "
2157 "program sequence without an end"));
2158}
2159
2160static void
2161dwarf2_complex_location_expr_complaint (void)
2162{
2163 complaint (&symfile_complaints, _("location expression too complex"));
2164}
2165
2166static void
2167dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2168 int arg3)
2169{
2170 complaint (&symfile_complaints,
2171 _("const value length mismatch for '%s', got %d, expected %d"),
2172 arg1, arg2, arg3);
2173}
2174
2175static void
2176dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2177{
2178 complaint (&symfile_complaints,
2179 _("debug info runs off end of %s section"
2180 " [in module %s]"),
a32a8923
DE
2181 get_section_name (section),
2182 get_section_file_name (section));
4390d890 2183}
1b80a9fa 2184
4390d890
DE
2185static void
2186dwarf2_macro_malformed_definition_complaint (const char *arg1)
2187{
2188 complaint (&symfile_complaints,
2189 _("macro debug info contains a "
2190 "malformed macro definition:\n`%s'"),
2191 arg1);
2192}
2193
2194static void
2195dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2196{
2197 complaint (&symfile_complaints,
2198 _("invalid attribute class or form for '%s' in '%s'"),
2199 arg1, arg2);
2200}
527f3840
JK
2201
2202/* Hash function for line_header_hash. */
2203
2204static hashval_t
2205line_header_hash (const struct line_header *ofs)
2206{
9c541725 2207 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2208}
2209
2210/* Hash function for htab_create_alloc_ex for line_header_hash. */
2211
2212static hashval_t
2213line_header_hash_voidp (const void *item)
2214{
9a3c8263 2215 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2216
2217 return line_header_hash (ofs);
2218}
2219
2220/* Equality function for line_header_hash. */
2221
2222static int
2223line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2224{
9a3c8263
SM
2225 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2226 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2227
9c541725 2228 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2229 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2230}
2231
4390d890 2232\f
9291a0cd 2233
31aa7e4e
JB
2234/* Read the given attribute value as an address, taking the attribute's
2235 form into account. */
2236
2237static CORE_ADDR
2238attr_value_as_address (struct attribute *attr)
2239{
2240 CORE_ADDR addr;
2241
2242 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2243 {
2244 /* Aside from a few clearly defined exceptions, attributes that
2245 contain an address must always be in DW_FORM_addr form.
2246 Unfortunately, some compilers happen to be violating this
2247 requirement by encoding addresses using other forms, such
2248 as DW_FORM_data4 for example. For those broken compilers,
2249 we try to do our best, without any guarantee of success,
2250 to interpret the address correctly. It would also be nice
2251 to generate a complaint, but that would require us to maintain
2252 a list of legitimate cases where a non-address form is allowed,
2253 as well as update callers to pass in at least the CU's DWARF
2254 version. This is more overhead than what we're willing to
2255 expand for a pretty rare case. */
2256 addr = DW_UNSND (attr);
2257 }
2258 else
2259 addr = DW_ADDR (attr);
2260
2261 return addr;
2262}
2263
9291a0cd
TT
2264/* The suffix for an index file. */
2265#define INDEX_SUFFIX ".gdb-index"
2266
330cdd98
PA
2267/* See declaration. */
2268
2269dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2270 const dwarf2_debug_sections *names)
2271 : objfile (objfile_)
2272{
2273 if (names == NULL)
2274 names = &dwarf2_elf_names;
2275
2276 bfd *obfd = objfile->obfd;
2277
2278 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2279 locate_sections (obfd, sec, *names);
2280}
2281
2282dwarf2_per_objfile::~dwarf2_per_objfile ()
2283{
2284 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2285 free_cached_comp_units ();
2286
2287 if (quick_file_names_table)
2288 htab_delete (quick_file_names_table);
2289
2290 if (line_header_hash)
2291 htab_delete (line_header_hash);
2292
2293 /* Everything else should be on the objfile obstack. */
2294}
2295
2296/* See declaration. */
2297
2298void
2299dwarf2_per_objfile::free_cached_comp_units ()
2300{
2301 dwarf2_per_cu_data *per_cu = read_in_chain;
2302 dwarf2_per_cu_data **last_chain = &read_in_chain;
2303 while (per_cu != NULL)
2304 {
2305 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2306
2307 free_heap_comp_unit (per_cu->cu);
2308 *last_chain = next_cu;
2309 per_cu = next_cu;
2310 }
2311}
2312
c906108c 2313/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2314 information and return true if we have enough to do something.
2315 NAMES points to the dwarf2 section names, or is NULL if the standard
2316 ELF names are used. */
c906108c
SS
2317
2318int
251d32d9
TG
2319dwarf2_has_info (struct objfile *objfile,
2320 const struct dwarf2_debug_sections *names)
c906108c 2321{
e2e32174
SDJ
2322 if (objfile->flags & OBJF_READNEVER)
2323 return 0;
2324
9a3c8263
SM
2325 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2326 objfile_data (objfile, dwarf2_objfile_data_key));
be391dca
TT
2327 if (!dwarf2_per_objfile)
2328 {
2329 /* Initialize per-objfile state. */
2330 struct dwarf2_per_objfile *data
8d749320 2331 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
9a619af0 2332
330cdd98
PA
2333 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2334 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
be391dca 2335 }
73869dc2 2336 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2337 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2338 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2339 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2340}
2341
2342/* Return the containing section of virtual section SECTION. */
2343
2344static struct dwarf2_section_info *
2345get_containing_section (const struct dwarf2_section_info *section)
2346{
2347 gdb_assert (section->is_virtual);
2348 return section->s.containing_section;
c906108c
SS
2349}
2350
a32a8923
DE
2351/* Return the bfd owner of SECTION. */
2352
2353static struct bfd *
2354get_section_bfd_owner (const struct dwarf2_section_info *section)
2355{
73869dc2
DE
2356 if (section->is_virtual)
2357 {
2358 section = get_containing_section (section);
2359 gdb_assert (!section->is_virtual);
2360 }
049412e3 2361 return section->s.section->owner;
a32a8923
DE
2362}
2363
2364/* Return the bfd section of SECTION.
2365 Returns NULL if the section is not present. */
2366
2367static asection *
2368get_section_bfd_section (const struct dwarf2_section_info *section)
2369{
73869dc2
DE
2370 if (section->is_virtual)
2371 {
2372 section = get_containing_section (section);
2373 gdb_assert (!section->is_virtual);
2374 }
049412e3 2375 return section->s.section;
a32a8923
DE
2376}
2377
2378/* Return the name of SECTION. */
2379
2380static const char *
2381get_section_name (const struct dwarf2_section_info *section)
2382{
2383 asection *sectp = get_section_bfd_section (section);
2384
2385 gdb_assert (sectp != NULL);
2386 return bfd_section_name (get_section_bfd_owner (section), sectp);
2387}
2388
2389/* Return the name of the file SECTION is in. */
2390
2391static const char *
2392get_section_file_name (const struct dwarf2_section_info *section)
2393{
2394 bfd *abfd = get_section_bfd_owner (section);
2395
2396 return bfd_get_filename (abfd);
2397}
2398
2399/* Return the id of SECTION.
2400 Returns 0 if SECTION doesn't exist. */
2401
2402static int
2403get_section_id (const struct dwarf2_section_info *section)
2404{
2405 asection *sectp = get_section_bfd_section (section);
2406
2407 if (sectp == NULL)
2408 return 0;
2409 return sectp->id;
2410}
2411
2412/* Return the flags of SECTION.
73869dc2 2413 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2414
2415static int
2416get_section_flags (const struct dwarf2_section_info *section)
2417{
2418 asection *sectp = get_section_bfd_section (section);
2419
2420 gdb_assert (sectp != NULL);
2421 return bfd_get_section_flags (sectp->owner, sectp);
2422}
2423
251d32d9
TG
2424/* When loading sections, we look either for uncompressed section or for
2425 compressed section names. */
233a11ab
CS
2426
2427static int
251d32d9
TG
2428section_is_p (const char *section_name,
2429 const struct dwarf2_section_names *names)
233a11ab 2430{
251d32d9
TG
2431 if (names->normal != NULL
2432 && strcmp (section_name, names->normal) == 0)
2433 return 1;
2434 if (names->compressed != NULL
2435 && strcmp (section_name, names->compressed) == 0)
2436 return 1;
2437 return 0;
233a11ab
CS
2438}
2439
330cdd98 2440/* See declaration. */
c906108c 2441
330cdd98
PA
2442void
2443dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2444 const dwarf2_debug_sections &names)
c906108c 2445{
dc7650b8 2446 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2447
dc7650b8
JK
2448 if ((aflag & SEC_HAS_CONTENTS) == 0)
2449 {
2450 }
330cdd98 2451 else if (section_is_p (sectp->name, &names.info))
c906108c 2452 {
330cdd98
PA
2453 this->info.s.section = sectp;
2454 this->info.size = bfd_get_section_size (sectp);
c906108c 2455 }
330cdd98 2456 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2457 {
330cdd98
PA
2458 this->abbrev.s.section = sectp;
2459 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2460 }
330cdd98 2461 else if (section_is_p (sectp->name, &names.line))
c906108c 2462 {
330cdd98
PA
2463 this->line.s.section = sectp;
2464 this->line.size = bfd_get_section_size (sectp);
c906108c 2465 }
330cdd98 2466 else if (section_is_p (sectp->name, &names.loc))
c906108c 2467 {
330cdd98
PA
2468 this->loc.s.section = sectp;
2469 this->loc.size = bfd_get_section_size (sectp);
c906108c 2470 }
330cdd98 2471 else if (section_is_p (sectp->name, &names.loclists))
43988095 2472 {
330cdd98
PA
2473 this->loclists.s.section = sectp;
2474 this->loclists.size = bfd_get_section_size (sectp);
43988095 2475 }
330cdd98 2476 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2477 {
330cdd98
PA
2478 this->macinfo.s.section = sectp;
2479 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2480 }
330cdd98 2481 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2482 {
330cdd98
PA
2483 this->macro.s.section = sectp;
2484 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2485 }
330cdd98 2486 else if (section_is_p (sectp->name, &names.str))
c906108c 2487 {
330cdd98
PA
2488 this->str.s.section = sectp;
2489 this->str.size = bfd_get_section_size (sectp);
c906108c 2490 }
330cdd98 2491 else if (section_is_p (sectp->name, &names.line_str))
43988095 2492 {
330cdd98
PA
2493 this->line_str.s.section = sectp;
2494 this->line_str.size = bfd_get_section_size (sectp);
43988095 2495 }
330cdd98 2496 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2497 {
330cdd98
PA
2498 this->addr.s.section = sectp;
2499 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2500 }
330cdd98 2501 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2502 {
330cdd98
PA
2503 this->frame.s.section = sectp;
2504 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2505 }
330cdd98 2506 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2507 {
330cdd98
PA
2508 this->eh_frame.s.section = sectp;
2509 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2510 }
330cdd98 2511 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2512 {
330cdd98
PA
2513 this->ranges.s.section = sectp;
2514 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2515 }
330cdd98 2516 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2517 {
330cdd98
PA
2518 this->rnglists.s.section = sectp;
2519 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2520 }
330cdd98 2521 else if (section_is_p (sectp->name, &names.types))
348e048f 2522 {
8b70b953
TT
2523 struct dwarf2_section_info type_section;
2524
2525 memset (&type_section, 0, sizeof (type_section));
049412e3 2526 type_section.s.section = sectp;
8b70b953
TT
2527 type_section.size = bfd_get_section_size (sectp);
2528
330cdd98 2529 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2530 &type_section);
348e048f 2531 }
330cdd98 2532 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2533 {
330cdd98
PA
2534 this->gdb_index.s.section = sectp;
2535 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2536 }
dce234bc 2537
b4e1fd61 2538 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2539 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2540 this->has_section_at_zero = true;
c906108c
SS
2541}
2542
fceca515
DE
2543/* A helper function that decides whether a section is empty,
2544 or not present. */
9e0ac564
TT
2545
2546static int
19ac8c2e 2547dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2548{
73869dc2
DE
2549 if (section->is_virtual)
2550 return section->size == 0;
049412e3 2551 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2552}
2553
3019eac3
DE
2554/* Read the contents of the section INFO.
2555 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2556 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2557 of the DWO file.
dce234bc 2558 If the section is compressed, uncompress it before returning. */
c906108c 2559
dce234bc
PP
2560static void
2561dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2562{
a32a8923 2563 asection *sectp;
3019eac3 2564 bfd *abfd;
dce234bc 2565 gdb_byte *buf, *retbuf;
c906108c 2566
be391dca
TT
2567 if (info->readin)
2568 return;
dce234bc 2569 info->buffer = NULL;
be391dca 2570 info->readin = 1;
188dd5d6 2571
9e0ac564 2572 if (dwarf2_section_empty_p (info))
dce234bc 2573 return;
c906108c 2574
a32a8923 2575 sectp = get_section_bfd_section (info);
3019eac3 2576
73869dc2
DE
2577 /* If this is a virtual section we need to read in the real one first. */
2578 if (info->is_virtual)
2579 {
2580 struct dwarf2_section_info *containing_section =
2581 get_containing_section (info);
2582
2583 gdb_assert (sectp != NULL);
2584 if ((sectp->flags & SEC_RELOC) != 0)
2585 {
2586 error (_("Dwarf Error: DWP format V2 with relocations is not"
2587 " supported in section %s [in module %s]"),
2588 get_section_name (info), get_section_file_name (info));
2589 }
2590 dwarf2_read_section (objfile, containing_section);
2591 /* Other code should have already caught virtual sections that don't
2592 fit. */
2593 gdb_assert (info->virtual_offset + info->size
2594 <= containing_section->size);
2595 /* If the real section is empty or there was a problem reading the
2596 section we shouldn't get here. */
2597 gdb_assert (containing_section->buffer != NULL);
2598 info->buffer = containing_section->buffer + info->virtual_offset;
2599 return;
2600 }
2601
4bf44c1c
TT
2602 /* If the section has relocations, we must read it ourselves.
2603 Otherwise we attach it to the BFD. */
2604 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2605 {
d521ce57 2606 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2607 return;
dce234bc 2608 }
dce234bc 2609
224c3ddb 2610 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2611 info->buffer = buf;
dce234bc
PP
2612
2613 /* When debugging .o files, we may need to apply relocations; see
2614 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2615 We never compress sections in .o files, so we only need to
2616 try this when the section is not compressed. */
ac8035ab 2617 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2618 if (retbuf != NULL)
2619 {
2620 info->buffer = retbuf;
2621 return;
2622 }
2623
a32a8923
DE
2624 abfd = get_section_bfd_owner (info);
2625 gdb_assert (abfd != NULL);
2626
dce234bc
PP
2627 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2628 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2629 {
2630 error (_("Dwarf Error: Can't read DWARF data"
2631 " in section %s [in module %s]"),
2632 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2633 }
dce234bc
PP
2634}
2635
9e0ac564
TT
2636/* A helper function that returns the size of a section in a safe way.
2637 If you are positive that the section has been read before using the
2638 size, then it is safe to refer to the dwarf2_section_info object's
2639 "size" field directly. In other cases, you must call this
2640 function, because for compressed sections the size field is not set
2641 correctly until the section has been read. */
2642
2643static bfd_size_type
2644dwarf2_section_size (struct objfile *objfile,
2645 struct dwarf2_section_info *info)
2646{
2647 if (!info->readin)
2648 dwarf2_read_section (objfile, info);
2649 return info->size;
2650}
2651
dce234bc 2652/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2653 SECTION_NAME. */
af34e669 2654
dce234bc 2655void
3017a003
TG
2656dwarf2_get_section_info (struct objfile *objfile,
2657 enum dwarf2_section_enum sect,
d521ce57 2658 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2659 bfd_size_type *sizep)
2660{
2661 struct dwarf2_per_objfile *data
9a3c8263
SM
2662 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2663 dwarf2_objfile_data_key);
dce234bc 2664 struct dwarf2_section_info *info;
a3b2a86b
TT
2665
2666 /* We may see an objfile without any DWARF, in which case we just
2667 return nothing. */
2668 if (data == NULL)
2669 {
2670 *sectp = NULL;
2671 *bufp = NULL;
2672 *sizep = 0;
2673 return;
2674 }
3017a003
TG
2675 switch (sect)
2676 {
2677 case DWARF2_DEBUG_FRAME:
2678 info = &data->frame;
2679 break;
2680 case DWARF2_EH_FRAME:
2681 info = &data->eh_frame;
2682 break;
2683 default:
2684 gdb_assert_not_reached ("unexpected section");
2685 }
dce234bc 2686
9e0ac564 2687 dwarf2_read_section (objfile, info);
dce234bc 2688
a32a8923 2689 *sectp = get_section_bfd_section (info);
dce234bc
PP
2690 *bufp = info->buffer;
2691 *sizep = info->size;
2692}
2693
36586728
TT
2694/* A helper function to find the sections for a .dwz file. */
2695
2696static void
2697locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2698{
9a3c8263 2699 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2700
2701 /* Note that we only support the standard ELF names, because .dwz
2702 is ELF-only (at the time of writing). */
2703 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2704 {
049412e3 2705 dwz_file->abbrev.s.section = sectp;
36586728
TT
2706 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2707 }
2708 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2709 {
049412e3 2710 dwz_file->info.s.section = sectp;
36586728
TT
2711 dwz_file->info.size = bfd_get_section_size (sectp);
2712 }
2713 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2714 {
049412e3 2715 dwz_file->str.s.section = sectp;
36586728
TT
2716 dwz_file->str.size = bfd_get_section_size (sectp);
2717 }
2718 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2719 {
049412e3 2720 dwz_file->line.s.section = sectp;
36586728
TT
2721 dwz_file->line.size = bfd_get_section_size (sectp);
2722 }
2723 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2724 {
049412e3 2725 dwz_file->macro.s.section = sectp;
36586728
TT
2726 dwz_file->macro.size = bfd_get_section_size (sectp);
2727 }
2ec9a5e0
TT
2728 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2729 {
049412e3 2730 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2731 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2732 }
36586728
TT
2733}
2734
4db1a1dc
TT
2735/* Open the separate '.dwz' debug file, if needed. Return NULL if
2736 there is no .gnu_debugaltlink section in the file. Error if there
2737 is such a section but the file cannot be found. */
36586728
TT
2738
2739static struct dwz_file *
2740dwarf2_get_dwz_file (void)
2741{
36586728
TT
2742 const char *filename;
2743 struct dwz_file *result;
acd13123 2744 bfd_size_type buildid_len_arg;
dc294be5
TT
2745 size_t buildid_len;
2746 bfd_byte *buildid;
36586728
TT
2747
2748 if (dwarf2_per_objfile->dwz_file != NULL)
2749 return dwarf2_per_objfile->dwz_file;
2750
4db1a1dc 2751 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2752 gdb::unique_xmalloc_ptr<char> data
2753 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2754 &buildid_len_arg, &buildid));
4db1a1dc
TT
2755 if (data == NULL)
2756 {
2757 if (bfd_get_error () == bfd_error_no_error)
2758 return NULL;
2759 error (_("could not read '.gnu_debugaltlink' section: %s"),
2760 bfd_errmsg (bfd_get_error ()));
2761 }
791afaa2
TT
2762
2763 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2764
acd13123
TT
2765 buildid_len = (size_t) buildid_len_arg;
2766
791afaa2 2767 filename = data.get ();
d721ba37
PA
2768
2769 std::string abs_storage;
36586728
TT
2770 if (!IS_ABSOLUTE_PATH (filename))
2771 {
14278e1f
TT
2772 gdb::unique_xmalloc_ptr<char> abs
2773 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2774
14278e1f 2775 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2776 filename = abs_storage.c_str ();
36586728
TT
2777 }
2778
dc294be5
TT
2779 /* First try the file name given in the section. If that doesn't
2780 work, try to use the build-id instead. */
192b62ce 2781 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2782 if (dwz_bfd != NULL)
36586728 2783 {
192b62ce
TT
2784 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2785 dwz_bfd.release ();
36586728
TT
2786 }
2787
dc294be5
TT
2788 if (dwz_bfd == NULL)
2789 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2790
2791 if (dwz_bfd == NULL)
2792 error (_("could not find '.gnu_debugaltlink' file for %s"),
2793 objfile_name (dwarf2_per_objfile->objfile));
2794
36586728
TT
2795 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2796 struct dwz_file);
192b62ce 2797 result->dwz_bfd = dwz_bfd.release ();
36586728 2798
192b62ce 2799 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728 2800
192b62ce 2801 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2802 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2803 return result;
2804}
9291a0cd 2805\f
7b9f3c50
DE
2806/* DWARF quick_symbols_functions support. */
2807
2808/* TUs can share .debug_line entries, and there can be a lot more TUs than
2809 unique line tables, so we maintain a separate table of all .debug_line
2810 derived entries to support the sharing.
2811 All the quick functions need is the list of file names. We discard the
2812 line_header when we're done and don't need to record it here. */
2813struct quick_file_names
2814{
094b34ac
DE
2815 /* The data used to construct the hash key. */
2816 struct stmt_list_hash hash;
7b9f3c50
DE
2817
2818 /* The number of entries in file_names, real_names. */
2819 unsigned int num_file_names;
2820
2821 /* The file names from the line table, after being run through
2822 file_full_name. */
2823 const char **file_names;
2824
2825 /* The file names from the line table after being run through
2826 gdb_realpath. These are computed lazily. */
2827 const char **real_names;
2828};
2829
2830/* When using the index (and thus not using psymtabs), each CU has an
2831 object of this type. This is used to hold information needed by
2832 the various "quick" methods. */
2833struct dwarf2_per_cu_quick_data
2834{
2835 /* The file table. This can be NULL if there was no file table
2836 or it's currently not read in.
2837 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2838 struct quick_file_names *file_names;
2839
2840 /* The corresponding symbol table. This is NULL if symbols for this
2841 CU have not yet been read. */
43f3e411 2842 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2843
2844 /* A temporary mark bit used when iterating over all CUs in
2845 expand_symtabs_matching. */
2846 unsigned int mark : 1;
2847
2848 /* True if we've tried to read the file table and found there isn't one.
2849 There will be no point in trying to read it again next time. */
2850 unsigned int no_file_data : 1;
2851};
2852
094b34ac
DE
2853/* Utility hash function for a stmt_list_hash. */
2854
2855static hashval_t
2856hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2857{
2858 hashval_t v = 0;
2859
2860 if (stmt_list_hash->dwo_unit != NULL)
2861 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2862 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2863 return v;
2864}
2865
2866/* Utility equality function for a stmt_list_hash. */
2867
2868static int
2869eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2870 const struct stmt_list_hash *rhs)
2871{
2872 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2873 return 0;
2874 if (lhs->dwo_unit != NULL
2875 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2876 return 0;
2877
9c541725 2878 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2879}
2880
7b9f3c50
DE
2881/* Hash function for a quick_file_names. */
2882
2883static hashval_t
2884hash_file_name_entry (const void *e)
2885{
9a3c8263
SM
2886 const struct quick_file_names *file_data
2887 = (const struct quick_file_names *) e;
7b9f3c50 2888
094b34ac 2889 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2890}
2891
2892/* Equality function for a quick_file_names. */
2893
2894static int
2895eq_file_name_entry (const void *a, const void *b)
2896{
9a3c8263
SM
2897 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2898 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2899
094b34ac 2900 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2901}
2902
2903/* Delete function for a quick_file_names. */
2904
2905static void
2906delete_file_name_entry (void *e)
2907{
9a3c8263 2908 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2909 int i;
2910
2911 for (i = 0; i < file_data->num_file_names; ++i)
2912 {
2913 xfree ((void*) file_data->file_names[i]);
2914 if (file_data->real_names)
2915 xfree ((void*) file_data->real_names[i]);
2916 }
2917
2918 /* The space for the struct itself lives on objfile_obstack,
2919 so we don't free it here. */
2920}
2921
2922/* Create a quick_file_names hash table. */
2923
2924static htab_t
2925create_quick_file_names_table (unsigned int nr_initial_entries)
2926{
2927 return htab_create_alloc (nr_initial_entries,
2928 hash_file_name_entry, eq_file_name_entry,
2929 delete_file_name_entry, xcalloc, xfree);
2930}
9291a0cd 2931
918dd910
JK
2932/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2933 have to be created afterwards. You should call age_cached_comp_units after
2934 processing PER_CU->CU. dw2_setup must have been already called. */
2935
2936static void
2937load_cu (struct dwarf2_per_cu_data *per_cu)
2938{
3019eac3 2939 if (per_cu->is_debug_types)
e5fe5e75 2940 load_full_type_unit (per_cu);
918dd910 2941 else
95554aad 2942 load_full_comp_unit (per_cu, language_minimal);
918dd910 2943
cc12ce38
DE
2944 if (per_cu->cu == NULL)
2945 return; /* Dummy CU. */
2dc860c0
DE
2946
2947 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2948}
2949
a0f42c21 2950/* Read in the symbols for PER_CU. */
2fdf6df6 2951
9291a0cd 2952static void
a0f42c21 2953dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2954{
2955 struct cleanup *back_to;
2956
f4dc4d17
DE
2957 /* Skip type_unit_groups, reading the type units they contain
2958 is handled elsewhere. */
2959 if (IS_TYPE_UNIT_GROUP (per_cu))
2960 return;
2961
9291a0cd
TT
2962 back_to = make_cleanup (dwarf2_release_queue, NULL);
2963
95554aad 2964 if (dwarf2_per_objfile->using_index
43f3e411 2965 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2966 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2967 {
2968 queue_comp_unit (per_cu, language_minimal);
2969 load_cu (per_cu);
89e63ee4
DE
2970
2971 /* If we just loaded a CU from a DWO, and we're working with an index
2972 that may badly handle TUs, load all the TUs in that DWO as well.
2973 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2974 if (!per_cu->is_debug_types
cc12ce38 2975 && per_cu->cu != NULL
89e63ee4
DE
2976 && per_cu->cu->dwo_unit != NULL
2977 && dwarf2_per_objfile->index_table != NULL
2978 && dwarf2_per_objfile->index_table->version <= 7
2979 /* DWP files aren't supported yet. */
2980 && get_dwp_file () == NULL)
2981 queue_and_load_all_dwo_tus (per_cu);
95554aad 2982 }
9291a0cd 2983
a0f42c21 2984 process_queue ();
9291a0cd
TT
2985
2986 /* Age the cache, releasing compilation units that have not
2987 been used recently. */
2988 age_cached_comp_units ();
2989
2990 do_cleanups (back_to);
2991}
2992
2993/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2994 the objfile from which this CU came. Returns the resulting symbol
2995 table. */
2fdf6df6 2996
43f3e411 2997static struct compunit_symtab *
a0f42c21 2998dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2999{
95554aad 3000 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 3001 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3002 {
3003 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
c83dd867 3004 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 3005 dw2_do_instantiate_symtab (per_cu);
95554aad 3006 process_cu_includes ();
9291a0cd
TT
3007 do_cleanups (back_to);
3008 }
f194fefb 3009
43f3e411 3010 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
3011}
3012
8832e7e3 3013/* Return the CU/TU given its index.
f4dc4d17
DE
3014
3015 This is intended for loops like:
3016
3017 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3018 + dwarf2_per_objfile->n_type_units); ++i)
3019 {
8832e7e3 3020 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
3021
3022 ...;
3023 }
3024*/
2fdf6df6 3025
1fd400ff 3026static struct dwarf2_per_cu_data *
8832e7e3 3027dw2_get_cutu (int index)
1fd400ff
TT
3028{
3029 if (index >= dwarf2_per_objfile->n_comp_units)
3030 {
f4dc4d17 3031 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
3032 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3033 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
3034 }
3035
3036 return dwarf2_per_objfile->all_comp_units[index];
3037}
3038
8832e7e3
DE
3039/* Return the CU given its index.
3040 This differs from dw2_get_cutu in that it's for when you know INDEX
3041 refers to a CU. */
f4dc4d17
DE
3042
3043static struct dwarf2_per_cu_data *
8832e7e3 3044dw2_get_cu (int index)
f4dc4d17 3045{
8832e7e3 3046 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 3047
1fd400ff
TT
3048 return dwarf2_per_objfile->all_comp_units[index];
3049}
3050
2ec9a5e0
TT
3051/* A helper for create_cus_from_index that handles a given list of
3052 CUs. */
2fdf6df6 3053
74a0d9f6 3054static void
2ec9a5e0
TT
3055create_cus_from_index_list (struct objfile *objfile,
3056 const gdb_byte *cu_list, offset_type n_elements,
3057 struct dwarf2_section_info *section,
3058 int is_dwz,
3059 int base_offset)
9291a0cd
TT
3060{
3061 offset_type i;
9291a0cd 3062
2ec9a5e0 3063 for (i = 0; i < n_elements; i += 2)
9291a0cd 3064 {
74a0d9f6 3065 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3066
3067 sect_offset sect_off
3068 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3069 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3070 cu_list += 2 * 8;
3071
9c541725
PA
3072 dwarf2_per_cu_data *the_cu
3073 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3074 struct dwarf2_per_cu_data);
3075 the_cu->sect_off = sect_off;
9291a0cd
TT
3076 the_cu->length = length;
3077 the_cu->objfile = objfile;
8a0459fd 3078 the_cu->section = section;
9291a0cd
TT
3079 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3080 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
3081 the_cu->is_dwz = is_dwz;
3082 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 3083 }
9291a0cd
TT
3084}
3085
2ec9a5e0 3086/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3087 the CU objects for this objfile. */
2ec9a5e0 3088
74a0d9f6 3089static void
2ec9a5e0
TT
3090create_cus_from_index (struct objfile *objfile,
3091 const gdb_byte *cu_list, offset_type cu_list_elements,
3092 const gdb_byte *dwz_list, offset_type dwz_elements)
3093{
3094 struct dwz_file *dwz;
3095
3096 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
3097 dwarf2_per_objfile->all_comp_units =
3098 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3099 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 3100
74a0d9f6
JK
3101 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3102 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
3103
3104 if (dwz_elements == 0)
74a0d9f6 3105 return;
2ec9a5e0
TT
3106
3107 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
3108 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3109 cu_list_elements / 2);
2ec9a5e0
TT
3110}
3111
1fd400ff 3112/* Create the signatured type hash table from the index. */
673bfd45 3113
74a0d9f6 3114static void
673bfd45 3115create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3116 struct dwarf2_section_info *section,
673bfd45
DE
3117 const gdb_byte *bytes,
3118 offset_type elements)
1fd400ff
TT
3119{
3120 offset_type i;
673bfd45 3121 htab_t sig_types_hash;
1fd400ff 3122
6aa5f3a6
DE
3123 dwarf2_per_objfile->n_type_units
3124 = dwarf2_per_objfile->n_allocated_type_units
3125 = elements / 3;
8d749320
SM
3126 dwarf2_per_objfile->all_type_units =
3127 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3128
673bfd45 3129 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3130
3131 for (i = 0; i < elements; i += 3)
3132 {
52dc124a 3133 struct signatured_type *sig_type;
9c541725 3134 ULONGEST signature;
1fd400ff 3135 void **slot;
9c541725 3136 cu_offset type_offset_in_tu;
1fd400ff 3137
74a0d9f6 3138 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3139 sect_offset sect_off
3140 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3141 type_offset_in_tu
3142 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3143 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3144 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3145 bytes += 3 * 8;
3146
52dc124a 3147 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3148 struct signatured_type);
52dc124a 3149 sig_type->signature = signature;
9c541725 3150 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3151 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3152 sig_type->per_cu.section = section;
9c541725 3153 sig_type->per_cu.sect_off = sect_off;
52dc124a
DE
3154 sig_type->per_cu.objfile = objfile;
3155 sig_type->per_cu.v.quick
1fd400ff
TT
3156 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3157 struct dwarf2_per_cu_quick_data);
3158
52dc124a
DE
3159 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3160 *slot = sig_type;
1fd400ff 3161
b4dd5633 3162 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3163 }
3164
673bfd45 3165 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3166}
3167
9291a0cd
TT
3168/* Read the address map data from the mapped index, and use it to
3169 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3170
9291a0cd
TT
3171static void
3172create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3173{
3e29f34a 3174 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3175 const gdb_byte *iter, *end;
9291a0cd 3176 struct addrmap *mutable_map;
9291a0cd
TT
3177 CORE_ADDR baseaddr;
3178
8268c778
PA
3179 auto_obstack temp_obstack;
3180
9291a0cd
TT
3181 mutable_map = addrmap_create_mutable (&temp_obstack);
3182
3183 iter = index->address_table;
3184 end = iter + index->address_table_size;
3185
3186 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3187
3188 while (iter < end)
3189 {
3190 ULONGEST hi, lo, cu_index;
3191 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3192 iter += 8;
3193 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3194 iter += 8;
3195 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3196 iter += 4;
f652bce2 3197
24a55014 3198 if (lo > hi)
f652bce2 3199 {
24a55014
DE
3200 complaint (&symfile_complaints,
3201 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3202 hex_string (lo), hex_string (hi));
24a55014 3203 continue;
f652bce2 3204 }
24a55014
DE
3205
3206 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3207 {
3208 complaint (&symfile_complaints,
3209 _(".gdb_index address table has invalid CU number %u"),
3210 (unsigned) cu_index);
24a55014 3211 continue;
f652bce2 3212 }
24a55014 3213
3e29f34a
MR
3214 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3215 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3216 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
9291a0cd
TT
3217 }
3218
3219 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3220 &objfile->objfile_obstack);
9291a0cd
TT
3221}
3222
59d7bcaf
JK
3223/* The hash function for strings in the mapped index. This is the same as
3224 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3225 implementation. This is necessary because the hash function is tied to the
3226 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3227 SYMBOL_HASH_NEXT.
3228
3229 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3230
9291a0cd 3231static hashval_t
559a7a62 3232mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3233{
3234 const unsigned char *str = (const unsigned char *) p;
3235 hashval_t r = 0;
3236 unsigned char c;
3237
3238 while ((c = *str++) != 0)
559a7a62
JK
3239 {
3240 if (index_version >= 5)
3241 c = tolower (c);
3242 r = r * 67 + c - 113;
3243 }
9291a0cd
TT
3244
3245 return r;
3246}
3247
3248/* Find a slot in the mapped index INDEX for the object named NAME.
3249 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3250 constant pool and return true. If NAME cannot be found, return
3251 false. */
2fdf6df6 3252
109483d9 3253static bool
9291a0cd
TT
3254find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3255 offset_type **vec_out)
3256{
0cf03b49 3257 offset_type hash;
9291a0cd 3258 offset_type slot, step;
559a7a62 3259 int (*cmp) (const char *, const char *);
9291a0cd 3260
791afaa2 3261 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3262 if (current_language->la_language == language_cplus
45280282
IB
3263 || current_language->la_language == language_fortran
3264 || current_language->la_language == language_d)
0cf03b49
JK
3265 {
3266 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3267 not contain any. */
a8719064 3268
72998fb3 3269 if (strchr (name, '(') != NULL)
0cf03b49 3270 {
109483d9 3271 without_params = cp_remove_params (name);
0cf03b49 3272
72998fb3 3273 if (without_params != NULL)
791afaa2 3274 name = without_params.get ();
0cf03b49
JK
3275 }
3276 }
3277
559a7a62 3278 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3279 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3280 simulate our NAME being searched is also lowercased. */
3281 hash = mapped_index_string_hash ((index->version == 4
3282 && case_sensitivity == case_sensitive_off
3283 ? 5 : index->version),
3284 name);
3285
3876f04e
DE
3286 slot = hash & (index->symbol_table_slots - 1);
3287 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 3288 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3289
3290 for (;;)
3291 {
3292 /* Convert a slot number to an offset into the table. */
3293 offset_type i = 2 * slot;
3294 const char *str;
3876f04e 3295 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
109483d9 3296 return false;
9291a0cd 3297
3876f04e 3298 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 3299 if (!cmp (name, str))
9291a0cd
TT
3300 {
3301 *vec_out = (offset_type *) (index->constant_pool
3876f04e 3302 + MAYBE_SWAP (index->symbol_table[i + 1]));
109483d9 3303 return true;
9291a0cd
TT
3304 }
3305
3876f04e 3306 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
3307 }
3308}
3309
2ec9a5e0
TT
3310/* A helper function that reads the .gdb_index from SECTION and fills
3311 in MAP. FILENAME is the name of the file containing the section;
3312 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3313 ok to use deprecated sections.
3314
3315 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3316 out parameters that are filled in with information about the CU and
3317 TU lists in the section.
3318
3319 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3320
9291a0cd 3321static int
2ec9a5e0
TT
3322read_index_from_section (struct objfile *objfile,
3323 const char *filename,
3324 int deprecated_ok,
3325 struct dwarf2_section_info *section,
3326 struct mapped_index *map,
3327 const gdb_byte **cu_list,
3328 offset_type *cu_list_elements,
3329 const gdb_byte **types_list,
3330 offset_type *types_list_elements)
9291a0cd 3331{
948f8e3d 3332 const gdb_byte *addr;
2ec9a5e0 3333 offset_type version;
b3b272e1 3334 offset_type *metadata;
1fd400ff 3335 int i;
9291a0cd 3336
2ec9a5e0 3337 if (dwarf2_section_empty_p (section))
9291a0cd 3338 return 0;
82430852
JK
3339
3340 /* Older elfutils strip versions could keep the section in the main
3341 executable while splitting it for the separate debug info file. */
a32a8923 3342 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3343 return 0;
3344
2ec9a5e0 3345 dwarf2_read_section (objfile, section);
9291a0cd 3346
2ec9a5e0 3347 addr = section->buffer;
9291a0cd 3348 /* Version check. */
1fd400ff 3349 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3350 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3351 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3352 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3353 indices. */
831adc1f 3354 if (version < 4)
481860b3
GB
3355 {
3356 static int warning_printed = 0;
3357 if (!warning_printed)
3358 {
3359 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3360 filename);
481860b3
GB
3361 warning_printed = 1;
3362 }
3363 return 0;
3364 }
3365 /* Index version 4 uses a different hash function than index version
3366 5 and later.
3367
3368 Versions earlier than 6 did not emit psymbols for inlined
3369 functions. Using these files will cause GDB not to be able to
3370 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3371 indices unless the user has done
3372 "set use-deprecated-index-sections on". */
2ec9a5e0 3373 if (version < 6 && !deprecated_ok)
481860b3
GB
3374 {
3375 static int warning_printed = 0;
3376 if (!warning_printed)
3377 {
e615022a
DE
3378 warning (_("\
3379Skipping deprecated .gdb_index section in %s.\n\
3380Do \"set use-deprecated-index-sections on\" before the file is read\n\
3381to use the section anyway."),
2ec9a5e0 3382 filename);
481860b3
GB
3383 warning_printed = 1;
3384 }
3385 return 0;
3386 }
796a7ff8 3387 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3388 of the TU (for symbols coming from TUs),
3389 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3390 Plus gold-generated indices can have duplicate entries for global symbols,
3391 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3392 These are just performance bugs, and we can't distinguish gdb-generated
3393 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3394
481860b3 3395 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3396 longer backward compatible. */
796a7ff8 3397 if (version > 8)
594e8718 3398 return 0;
9291a0cd 3399
559a7a62 3400 map->version = version;
2ec9a5e0 3401 map->total_size = section->size;
9291a0cd
TT
3402
3403 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3404
3405 i = 0;
2ec9a5e0
TT
3406 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3407 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3408 / 8);
1fd400ff
TT
3409 ++i;
3410
2ec9a5e0
TT
3411 *types_list = addr + MAYBE_SWAP (metadata[i]);
3412 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3413 - MAYBE_SWAP (metadata[i]))
3414 / 8);
987d643c 3415 ++i;
1fd400ff
TT
3416
3417 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3418 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3419 - MAYBE_SWAP (metadata[i]));
3420 ++i;
3421
3876f04e
DE
3422 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3423 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3424 - MAYBE_SWAP (metadata[i]))
3425 / (2 * sizeof (offset_type)));
1fd400ff 3426 ++i;
9291a0cd 3427
f9d83a0b 3428 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3429
2ec9a5e0
TT
3430 return 1;
3431}
3432
3433
3434/* Read the index file. If everything went ok, initialize the "quick"
3435 elements of all the CUs and return 1. Otherwise, return 0. */
3436
3437static int
3438dwarf2_read_index (struct objfile *objfile)
3439{
3440 struct mapped_index local_map, *map;
3441 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3442 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3443 struct dwz_file *dwz;
2ec9a5e0 3444
4262abfb 3445 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3446 use_deprecated_index_sections,
3447 &dwarf2_per_objfile->gdb_index, &local_map,
3448 &cu_list, &cu_list_elements,
3449 &types_list, &types_list_elements))
3450 return 0;
3451
0fefef59 3452 /* Don't use the index if it's empty. */
2ec9a5e0 3453 if (local_map.symbol_table_slots == 0)
0fefef59
DE
3454 return 0;
3455
2ec9a5e0
TT
3456 /* If there is a .dwz file, read it so we can get its CU list as
3457 well. */
4db1a1dc
TT
3458 dwz = dwarf2_get_dwz_file ();
3459 if (dwz != NULL)
2ec9a5e0 3460 {
2ec9a5e0
TT
3461 struct mapped_index dwz_map;
3462 const gdb_byte *dwz_types_ignore;
3463 offset_type dwz_types_elements_ignore;
3464
3465 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3466 1,
3467 &dwz->gdb_index, &dwz_map,
3468 &dwz_list, &dwz_list_elements,
3469 &dwz_types_ignore,
3470 &dwz_types_elements_ignore))
3471 {
3472 warning (_("could not read '.gdb_index' section from %s; skipping"),
3473 bfd_get_filename (dwz->dwz_bfd));
3474 return 0;
3475 }
3476 }
3477
74a0d9f6
JK
3478 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3479 dwz_list_elements);
1fd400ff 3480
8b70b953
TT
3481 if (types_list_elements)
3482 {
3483 struct dwarf2_section_info *section;
3484
3485 /* We can only handle a single .debug_types when we have an
3486 index. */
3487 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3488 return 0;
3489
3490 section = VEC_index (dwarf2_section_info_def,
3491 dwarf2_per_objfile->types, 0);
3492
74a0d9f6
JK
3493 create_signatured_type_table_from_index (objfile, section, types_list,
3494 types_list_elements);
8b70b953 3495 }
9291a0cd 3496
2ec9a5e0
TT
3497 create_addrmap_from_index (objfile, &local_map);
3498
8d749320 3499 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3f563c84 3500 map = new (map) mapped_index ();
2ec9a5e0 3501 *map = local_map;
9291a0cd
TT
3502
3503 dwarf2_per_objfile->index_table = map;
3504 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3505 dwarf2_per_objfile->quick_file_names_table =
3506 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3507
3508 return 1;
3509}
3510
3511/* A helper for the "quick" functions which sets the global
3512 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 3513
9291a0cd
TT
3514static void
3515dw2_setup (struct objfile *objfile)
3516{
9a3c8263
SM
3517 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3518 objfile_data (objfile, dwarf2_objfile_data_key));
9291a0cd
TT
3519 gdb_assert (dwarf2_per_objfile);
3520}
3521
dee91e82 3522/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3523
dee91e82
DE
3524static void
3525dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3526 const gdb_byte *info_ptr,
dee91e82
DE
3527 struct die_info *comp_unit_die,
3528 int has_children,
3529 void *data)
9291a0cd 3530{
dee91e82
DE
3531 struct dwarf2_cu *cu = reader->cu;
3532 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3533 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3534 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3535 struct attribute *attr;
dee91e82 3536 int i;
7b9f3c50
DE
3537 void **slot;
3538 struct quick_file_names *qfn;
9291a0cd 3539
0186c6a7
DE
3540 gdb_assert (! this_cu->is_debug_types);
3541
07261596
TT
3542 /* Our callers never want to match partial units -- instead they
3543 will match the enclosing full CU. */
3544 if (comp_unit_die->tag == DW_TAG_partial_unit)
3545 {
3546 this_cu->v.quick->no_file_data = 1;
3547 return;
3548 }
3549
0186c6a7 3550 lh_cu = this_cu;
7b9f3c50 3551 slot = NULL;
dee91e82 3552
fff8551c 3553 line_header_up lh;
9c541725 3554 sect_offset line_offset {};
fff8551c 3555
dee91e82 3556 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3557 if (attr)
3558 {
7b9f3c50
DE
3559 struct quick_file_names find_entry;
3560
9c541725 3561 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3562
3563 /* We may have already read in this line header (TU line header sharing).
3564 If we have we're done. */
094b34ac 3565 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3566 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3567 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3568 &find_entry, INSERT);
3569 if (*slot != NULL)
3570 {
9a3c8263 3571 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3572 return;
7b9f3c50
DE
3573 }
3574
3019eac3 3575 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3576 }
3577 if (lh == NULL)
3578 {
094b34ac 3579 lh_cu->v.quick->no_file_data = 1;
dee91e82 3580 return;
9291a0cd
TT
3581 }
3582
8d749320 3583 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3584 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3585 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3586 gdb_assert (slot != NULL);
3587 *slot = qfn;
9291a0cd 3588
d721ba37 3589 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3590
fff8551c 3591 qfn->num_file_names = lh->file_names.size ();
8d749320 3592 qfn->file_names =
fff8551c
PA
3593 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3594 for (i = 0; i < lh->file_names.size (); ++i)
3595 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3596 qfn->real_names = NULL;
9291a0cd 3597
094b34ac 3598 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3599}
3600
3601/* A helper for the "quick" functions which attempts to read the line
3602 table for THIS_CU. */
3603
3604static struct quick_file_names *
e4a48d9d 3605dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3606{
0186c6a7
DE
3607 /* This should never be called for TUs. */
3608 gdb_assert (! this_cu->is_debug_types);
3609 /* Nor type unit groups. */
3610 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3611
dee91e82
DE
3612 if (this_cu->v.quick->file_names != NULL)
3613 return this_cu->v.quick->file_names;
3614 /* If we know there is no line data, no point in looking again. */
3615 if (this_cu->v.quick->no_file_data)
3616 return NULL;
3617
0186c6a7 3618 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3619
3620 if (this_cu->v.quick->no_file_data)
3621 return NULL;
3622 return this_cu->v.quick->file_names;
9291a0cd
TT
3623}
3624
3625/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3626 real path for a given file name from the line table. */
2fdf6df6 3627
9291a0cd 3628static const char *
7b9f3c50
DE
3629dw2_get_real_path (struct objfile *objfile,
3630 struct quick_file_names *qfn, int index)
9291a0cd 3631{
7b9f3c50
DE
3632 if (qfn->real_names == NULL)
3633 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3634 qfn->num_file_names, const char *);
9291a0cd 3635
7b9f3c50 3636 if (qfn->real_names[index] == NULL)
14278e1f 3637 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3638
7b9f3c50 3639 return qfn->real_names[index];
9291a0cd
TT
3640}
3641
3642static struct symtab *
3643dw2_find_last_source_symtab (struct objfile *objfile)
3644{
43f3e411 3645 struct compunit_symtab *cust;
9291a0cd 3646 int index;
ae2de4f8 3647
9291a0cd
TT
3648 dw2_setup (objfile);
3649 index = dwarf2_per_objfile->n_comp_units - 1;
43f3e411
DE
3650 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3651 if (cust == NULL)
3652 return NULL;
3653 return compunit_primary_filetab (cust);
9291a0cd
TT
3654}
3655
7b9f3c50
DE
3656/* Traversal function for dw2_forget_cached_source_info. */
3657
3658static int
3659dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3660{
7b9f3c50 3661 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3662
7b9f3c50 3663 if (file_data->real_names)
9291a0cd 3664 {
7b9f3c50 3665 int i;
9291a0cd 3666
7b9f3c50 3667 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3668 {
7b9f3c50
DE
3669 xfree ((void*) file_data->real_names[i]);
3670 file_data->real_names[i] = NULL;
9291a0cd
TT
3671 }
3672 }
7b9f3c50
DE
3673
3674 return 1;
3675}
3676
3677static void
3678dw2_forget_cached_source_info (struct objfile *objfile)
3679{
3680 dw2_setup (objfile);
3681
3682 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3683 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3684}
3685
f8eba3c6
TT
3686/* Helper function for dw2_map_symtabs_matching_filename that expands
3687 the symtabs and calls the iterator. */
3688
3689static int
3690dw2_map_expand_apply (struct objfile *objfile,
3691 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3692 const char *name, const char *real_path,
14bc53a8 3693 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3694{
43f3e411 3695 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3696
3697 /* Don't visit already-expanded CUs. */
43f3e411 3698 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3699 return 0;
3700
3701 /* This may expand more than one symtab, and we want to iterate over
3702 all of them. */
a0f42c21 3703 dw2_instantiate_symtab (per_cu);
f8eba3c6 3704
14bc53a8
PA
3705 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3706 last_made, callback);
f8eba3c6
TT
3707}
3708
3709/* Implementation of the map_symtabs_matching_filename method. */
3710
14bc53a8
PA
3711static bool
3712dw2_map_symtabs_matching_filename
3713 (struct objfile *objfile, const char *name, const char *real_path,
3714 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
3715{
3716 int i;
c011a4f4 3717 const char *name_basename = lbasename (name);
9291a0cd
TT
3718
3719 dw2_setup (objfile);
ae2de4f8 3720
848e3e78
DE
3721 /* The rule is CUs specify all the files, including those used by
3722 any TU, so there's no need to scan TUs here. */
f4dc4d17 3723
848e3e78 3724 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3725 {
3726 int j;
8832e7e3 3727 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3728 struct quick_file_names *file_data;
9291a0cd 3729
3d7bb9d9 3730 /* We only need to look at symtabs not already expanded. */
43f3e411 3731 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3732 continue;
3733
e4a48d9d 3734 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3735 if (file_data == NULL)
9291a0cd
TT
3736 continue;
3737
7b9f3c50 3738 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3739 {
7b9f3c50 3740 const char *this_name = file_data->file_names[j];
da235a7c 3741 const char *this_real_name;
9291a0cd 3742
af529f8f 3743 if (compare_filenames_for_search (this_name, name))
9291a0cd 3744 {
f5b95b50 3745 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3746 callback))
3747 return true;
288e77a7 3748 continue;
4aac40c8 3749 }
9291a0cd 3750
c011a4f4
DE
3751 /* Before we invoke realpath, which can get expensive when many
3752 files are involved, do a quick comparison of the basenames. */
3753 if (! basenames_may_differ
3754 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3755 continue;
3756
da235a7c
JK
3757 this_real_name = dw2_get_real_path (objfile, file_data, j);
3758 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3759 {
da235a7c 3760 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3761 callback))
3762 return true;
288e77a7 3763 continue;
da235a7c 3764 }
9291a0cd 3765
da235a7c
JK
3766 if (real_path != NULL)
3767 {
af529f8f
JK
3768 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3769 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3770 if (this_real_name != NULL
af529f8f 3771 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3772 {
f5b95b50 3773 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3774 callback))
3775 return true;
288e77a7 3776 continue;
9291a0cd
TT
3777 }
3778 }
3779 }
3780 }
3781
14bc53a8 3782 return false;
9291a0cd
TT
3783}
3784
da51c347
DE
3785/* Struct used to manage iterating over all CUs looking for a symbol. */
3786
3787struct dw2_symtab_iterator
9291a0cd 3788{
da51c347
DE
3789 /* The internalized form of .gdb_index. */
3790 struct mapped_index *index;
3791 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3792 int want_specific_block;
3793 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3794 Unused if !WANT_SPECIFIC_BLOCK. */
3795 int block_index;
3796 /* The kind of symbol we're looking for. */
3797 domain_enum domain;
3798 /* The list of CUs from the index entry of the symbol,
3799 or NULL if not found. */
3800 offset_type *vec;
3801 /* The next element in VEC to look at. */
3802 int next;
3803 /* The number of elements in VEC, or zero if there is no match. */
3804 int length;
8943b874
DE
3805 /* Have we seen a global version of the symbol?
3806 If so we can ignore all further global instances.
3807 This is to work around gold/15646, inefficient gold-generated
3808 indices. */
3809 int global_seen;
da51c347 3810};
9291a0cd 3811
da51c347
DE
3812/* Initialize the index symtab iterator ITER.
3813 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3814 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3815
9291a0cd 3816static void
da51c347
DE
3817dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3818 struct mapped_index *index,
3819 int want_specific_block,
3820 int block_index,
3821 domain_enum domain,
3822 const char *name)
3823{
3824 iter->index = index;
3825 iter->want_specific_block = want_specific_block;
3826 iter->block_index = block_index;
3827 iter->domain = domain;
3828 iter->next = 0;
8943b874 3829 iter->global_seen = 0;
da51c347
DE
3830
3831 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3832 iter->length = MAYBE_SWAP (*iter->vec);
3833 else
3834 {
3835 iter->vec = NULL;
3836 iter->length = 0;
3837 }
3838}
3839
3840/* Return the next matching CU or NULL if there are no more. */
3841
3842static struct dwarf2_per_cu_data *
3843dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3844{
3845 for ( ; iter->next < iter->length; ++iter->next)
3846 {
3847 offset_type cu_index_and_attrs =
3848 MAYBE_SWAP (iter->vec[iter->next + 1]);
3849 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3850 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3851 int want_static = iter->block_index != GLOBAL_BLOCK;
3852 /* This value is only valid for index versions >= 7. */
3853 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3854 gdb_index_symbol_kind symbol_kind =
3855 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3856 /* Only check the symbol attributes if they're present.
3857 Indices prior to version 7 don't record them,
3858 and indices >= 7 may elide them for certain symbols
3859 (gold does this). */
3860 int attrs_valid =
3861 (iter->index->version >= 7
3862 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3863
3190f0c6
DE
3864 /* Don't crash on bad data. */
3865 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3866 + dwarf2_per_objfile->n_type_units))
3867 {
3868 complaint (&symfile_complaints,
3869 _(".gdb_index entry has bad CU index"
4262abfb
JK
3870 " [in module %s]"),
3871 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3872 continue;
3873 }
3874
8832e7e3 3875 per_cu = dw2_get_cutu (cu_index);
3190f0c6 3876
da51c347 3877 /* Skip if already read in. */
43f3e411 3878 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3879 continue;
3880
8943b874
DE
3881 /* Check static vs global. */
3882 if (attrs_valid)
3883 {
3884 if (iter->want_specific_block
3885 && want_static != is_static)
3886 continue;
3887 /* Work around gold/15646. */
3888 if (!is_static && iter->global_seen)
3889 continue;
3890 if (!is_static)
3891 iter->global_seen = 1;
3892 }
da51c347
DE
3893
3894 /* Only check the symbol's kind if it has one. */
3895 if (attrs_valid)
3896 {
3897 switch (iter->domain)
3898 {
3899 case VAR_DOMAIN:
3900 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3901 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3902 /* Some types are also in VAR_DOMAIN. */
3903 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3904 continue;
3905 break;
3906 case STRUCT_DOMAIN:
3907 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3908 continue;
3909 break;
3910 case LABEL_DOMAIN:
3911 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3912 continue;
3913 break;
3914 default:
3915 break;
3916 }
3917 }
3918
3919 ++iter->next;
3920 return per_cu;
3921 }
3922
3923 return NULL;
3924}
3925
43f3e411 3926static struct compunit_symtab *
da51c347
DE
3927dw2_lookup_symbol (struct objfile *objfile, int block_index,
3928 const char *name, domain_enum domain)
9291a0cd 3929{
43f3e411 3930 struct compunit_symtab *stab_best = NULL;
156942c7
DE
3931 struct mapped_index *index;
3932
9291a0cd
TT
3933 dw2_setup (objfile);
3934
b5ec771e
PA
3935 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3936
156942c7
DE
3937 index = dwarf2_per_objfile->index_table;
3938
da51c347 3939 /* index is NULL if OBJF_READNOW. */
156942c7 3940 if (index)
9291a0cd 3941 {
da51c347
DE
3942 struct dw2_symtab_iterator iter;
3943 struct dwarf2_per_cu_data *per_cu;
3944
3945 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3946
da51c347 3947 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3948 {
b2e2f908 3949 struct symbol *sym, *with_opaque = NULL;
43f3e411
DE
3950 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3951 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
f194fefb 3952 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3953
b2e2f908
DE
3954 sym = block_find_symbol (block, name, domain,
3955 block_find_non_opaque_type_preferred,
3956 &with_opaque);
3957
da51c347
DE
3958 /* Some caution must be observed with overloaded functions
3959 and methods, since the index will not contain any overload
3960 information (but NAME might contain it). */
da51c347 3961
b2e2f908 3962 if (sym != NULL
b5ec771e 3963 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
b2e2f908
DE
3964 return stab;
3965 if (with_opaque != NULL
b5ec771e 3966 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
b2e2f908 3967 stab_best = stab;
da51c347
DE
3968
3969 /* Keep looking through other CUs. */
9291a0cd
TT
3970 }
3971 }
9291a0cd 3972
da51c347 3973 return stab_best;
9291a0cd
TT
3974}
3975
3976static void
3977dw2_print_stats (struct objfile *objfile)
3978{
e4a48d9d 3979 int i, total, count;
9291a0cd
TT
3980
3981 dw2_setup (objfile);
e4a48d9d 3982 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3983 count = 0;
e4a48d9d 3984 for (i = 0; i < total; ++i)
9291a0cd 3985 {
8832e7e3 3986 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3987
43f3e411 3988 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3989 ++count;
3990 }
e4a48d9d 3991 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3992 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3993}
3994
779bd270
DE
3995/* This dumps minimal information about the index.
3996 It is called via "mt print objfiles".
3997 One use is to verify .gdb_index has been loaded by the
3998 gdb.dwarf2/gdb-index.exp testcase. */
3999
9291a0cd
TT
4000static void
4001dw2_dump (struct objfile *objfile)
4002{
779bd270
DE
4003 dw2_setup (objfile);
4004 gdb_assert (dwarf2_per_objfile->using_index);
4005 printf_filtered (".gdb_index:");
4006 if (dwarf2_per_objfile->index_table != NULL)
4007 {
4008 printf_filtered (" version %d\n",
4009 dwarf2_per_objfile->index_table->version);
4010 }
4011 else
4012 printf_filtered (" faked for \"readnow\"\n");
4013 printf_filtered ("\n");
9291a0cd
TT
4014}
4015
4016static void
3189cb12
DE
4017dw2_relocate (struct objfile *objfile,
4018 const struct section_offsets *new_offsets,
4019 const struct section_offsets *delta)
9291a0cd
TT
4020{
4021 /* There's nothing to relocate here. */
4022}
4023
4024static void
4025dw2_expand_symtabs_for_function (struct objfile *objfile,
4026 const char *func_name)
4027{
da51c347
DE
4028 struct mapped_index *index;
4029
4030 dw2_setup (objfile);
4031
4032 index = dwarf2_per_objfile->index_table;
4033
4034 /* index is NULL if OBJF_READNOW. */
4035 if (index)
4036 {
4037 struct dw2_symtab_iterator iter;
4038 struct dwarf2_per_cu_data *per_cu;
4039
4040 /* Note: It doesn't matter what we pass for block_index here. */
4041 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4042 func_name);
4043
4044 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4045 dw2_instantiate_symtab (per_cu);
4046 }
9291a0cd
TT
4047}
4048
4049static void
4050dw2_expand_all_symtabs (struct objfile *objfile)
4051{
4052 int i;
4053
4054 dw2_setup (objfile);
1fd400ff
TT
4055
4056 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4057 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4058 {
8832e7e3 4059 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4060
a0f42c21 4061 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4062 }
4063}
4064
4065static void
652a8996
JK
4066dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4067 const char *fullname)
9291a0cd
TT
4068{
4069 int i;
4070
4071 dw2_setup (objfile);
d4637a04
DE
4072
4073 /* We don't need to consider type units here.
4074 This is only called for examining code, e.g. expand_line_sal.
4075 There can be an order of magnitude (or more) more type units
4076 than comp units, and we avoid them if we can. */
4077
4078 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4079 {
4080 int j;
8832e7e3 4081 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7b9f3c50 4082 struct quick_file_names *file_data;
9291a0cd 4083
3d7bb9d9 4084 /* We only need to look at symtabs not already expanded. */
43f3e411 4085 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4086 continue;
4087
e4a48d9d 4088 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4089 if (file_data == NULL)
9291a0cd
TT
4090 continue;
4091
7b9f3c50 4092 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4093 {
652a8996
JK
4094 const char *this_fullname = file_data->file_names[j];
4095
4096 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4097 {
a0f42c21 4098 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4099 break;
4100 }
4101 }
4102 }
4103}
4104
9291a0cd 4105static void
ade7ed9e 4106dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4107 const char * name, domain_enum domain,
ade7ed9e 4108 int global,
40658b94
PH
4109 int (*callback) (struct block *,
4110 struct symbol *, void *),
b5ec771e 4111 void *data, symbol_name_match_type match,
2edb89d3 4112 symbol_compare_ftype *ordered_compare)
9291a0cd 4113{
40658b94 4114 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4115 current language is Ada for a non-Ada objfile using GNU index. As Ada
4116 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4117}
4118
b5ec771e
PA
4119/* Symbol name matcher for .gdb_index names.
4120
4121 Symbol names in .gdb_index have a few particularities:
4122
4123 - There's no indication of which is the language of each symbol.
4124
4125 Since each language has its own symbol name matching algorithm,
4126 and we don't know which language is the right one, we must match
3f563c84
PA
4127 each symbol against all languages. This would be a potential
4128 performance problem if it were not mitigated by the
4129 mapped_index::name_components lookup table, which significantly
4130 reduces the number of times we need to call into this matcher,
4131 making it a non-issue.
b5ec771e
PA
4132
4133 - Symbol names in the index have no overload (parameter)
4134 information. I.e., in C++, "foo(int)" and "foo(long)" both
4135 appear as "foo" in the index, for example.
4136
4137 This means that the lookup names passed to the symbol name
4138 matcher functions must have no parameter information either
4139 because (e.g.) symbol search name "foo" does not match
4140 lookup-name "foo(int)" [while swapping search name for lookup
4141 name would match].
4142*/
4143class gdb_index_symbol_name_matcher
4144{
4145public:
4146 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4147 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4148
4149 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4150 Returns true if any matcher matches. */
4151 bool matches (const char *symbol_name);
4152
4153private:
4154 /* A reference to the lookup name we're matching against. */
4155 const lookup_name_info &m_lookup_name;
4156
4157 /* A vector holding all the different symbol name matchers, for all
4158 languages. */
4159 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4160};
4161
4162gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4163 (const lookup_name_info &lookup_name)
4164 : m_lookup_name (lookup_name)
4165{
4166 /* Prepare the vector of comparison functions upfront, to avoid
4167 doing the same work for each symbol. Care is taken to avoid
4168 matching with the same matcher more than once if/when multiple
4169 languages use the same matcher function. */
4170 auto &matchers = m_symbol_name_matcher_funcs;
4171 matchers.reserve (nr_languages);
4172
4173 matchers.push_back (default_symbol_name_matcher);
4174
4175 for (int i = 0; i < nr_languages; i++)
4176 {
4177 const language_defn *lang = language_def ((enum language) i);
4178 if (lang->la_get_symbol_name_matcher != NULL)
4179 {
4180 symbol_name_matcher_ftype *name_matcher
4181 = lang->la_get_symbol_name_matcher (m_lookup_name);
4182
4183 /* Don't insert the same comparison routine more than once.
4184 Note that we do this linear walk instead of a cheaper
4185 sorted insert, or use a std::set or something like that,
4186 because relative order of function addresses is not
4187 stable. This is not a problem in practice because the
4188 number of supported languages is low, and the cost here
4189 is tiny compared to the number of searches we'll do
4190 afterwards using this object. */
4191 if (std::find (matchers.begin (), matchers.end (), name_matcher)
4192 == matchers.end ())
4193 matchers.push_back (name_matcher);
4194 }
4195 }
4196}
4197
4198bool
4199gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4200{
4201 for (auto matches_name : m_symbol_name_matcher_funcs)
4202 if (matches_name (symbol_name, m_lookup_name, NULL))
4203 return true;
4204
4205 return false;
4206}
4207
e1ef7d7a
PA
4208/* Starting from a search name, return the string that finds the upper
4209 bound of all strings that start with SEARCH_NAME in a sorted name
4210 list. Returns the empty string to indicate that the upper bound is
4211 the end of the list. */
4212
4213static std::string
4214make_sort_after_prefix_name (const char *search_name)
4215{
4216 /* When looking to complete "func", we find the upper bound of all
4217 symbols that start with "func" by looking for where we'd insert
4218 the closest string that would follow "func" in lexicographical
4219 order. Usually, that's "func"-with-last-character-incremented,
4220 i.e. "fund". Mind non-ASCII characters, though. Usually those
4221 will be UTF-8 multi-byte sequences, but we can't be certain.
4222 Especially mind the 0xff character, which is a valid character in
4223 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4224 rule out compilers allowing it in identifiers. Note that
4225 conveniently, strcmp/strcasecmp are specified to compare
4226 characters interpreted as unsigned char. So what we do is treat
4227 the whole string as a base 256 number composed of a sequence of
4228 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4229 to 0, and carries 1 to the following more-significant position.
4230 If the very first character in SEARCH_NAME ends up incremented
4231 and carries/overflows, then the upper bound is the end of the
4232 list. The string after the empty string is also the empty
4233 string.
4234
4235 Some examples of this operation:
4236
4237 SEARCH_NAME => "+1" RESULT
4238
4239 "abc" => "abd"
4240 "ab\xff" => "ac"
4241 "\xff" "a" "\xff" => "\xff" "b"
4242 "\xff" => ""
4243 "\xff\xff" => ""
4244 "" => ""
4245
4246 Then, with these symbols for example:
4247
4248 func
4249 func1
4250 fund
4251
4252 completing "func" looks for symbols between "func" and
4253 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4254 which finds "func" and "func1", but not "fund".
4255
4256 And with:
4257
4258 funcÿ (Latin1 'ÿ' [0xff])
4259 funcÿ1
4260 fund
4261
4262 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4263 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4264
4265 And with:
4266
4267 ÿÿ (Latin1 'ÿ' [0xff])
4268 ÿÿ1
4269
4270 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4271 the end of the list.
4272 */
4273 std::string after = search_name;
4274 while (!after.empty () && (unsigned char) after.back () == 0xff)
4275 after.pop_back ();
4276 if (!after.empty ())
4277 after.back () = (unsigned char) after.back () + 1;
4278 return after;
4279}
4280
5c58de74 4281/* See declaration. */
61d96d7e 4282
5c58de74
PA
4283std::pair<std::vector<name_component>::const_iterator,
4284 std::vector<name_component>::const_iterator>
4285mapped_index::find_name_components_bounds
4286 (const lookup_name_info &lookup_name_without_params) const
3f563c84 4287{
5c58de74
PA
4288 auto *name_cmp
4289 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84
PA
4290
4291 const char *cplus
c62446b1 4292 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4293
3f563c84
PA
4294 /* Comparison function object for lower_bound that matches against a
4295 given symbol name. */
4296 auto lookup_compare_lower = [&] (const name_component &elem,
4297 const char *name)
4298 {
5c58de74 4299 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4300 const char *elem_name = elem_qualified + elem.name_offset;
4301 return name_cmp (elem_name, name) < 0;
4302 };
4303
4304 /* Comparison function object for upper_bound that matches against a
4305 given symbol name. */
4306 auto lookup_compare_upper = [&] (const char *name,
4307 const name_component &elem)
4308 {
5c58de74 4309 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4310 const char *elem_name = elem_qualified + elem.name_offset;
4311 return name_cmp (name, elem_name) < 0;
4312 };
4313
5c58de74
PA
4314 auto begin = this->name_components.begin ();
4315 auto end = this->name_components.end ();
3f563c84
PA
4316
4317 /* Find the lower bound. */
4318 auto lower = [&] ()
4319 {
5c58de74 4320 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4321 return begin;
4322 else
4323 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4324 } ();
4325
4326 /* Find the upper bound. */
4327 auto upper = [&] ()
4328 {
5c58de74 4329 if (lookup_name_without_params.completion_mode ())
3f563c84 4330 {
e1ef7d7a
PA
4331 /* In completion mode, we want UPPER to point past all
4332 symbols names that have the same prefix. I.e., with
4333 these symbols, and completing "func":
4334
4335 function << lower bound
4336 function1
4337 other_function << upper bound
4338
4339 We find the upper bound by looking for the insertion
4340 point of "func"-with-last-character-incremented,
4341 i.e. "fund". */
4342 std::string after = make_sort_after_prefix_name (cplus);
4343 if (after.empty ())
3f563c84 4344 return end;
e6b2f5ef
PA
4345 return std::lower_bound (lower, end, after.c_str (),
4346 lookup_compare_lower);
3f563c84
PA
4347 }
4348 else
4349 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4350 } ();
4351
5c58de74
PA
4352 return {lower, upper};
4353}
4354
4355/* See declaration. */
4356
4357void
4358mapped_index::build_name_components ()
4359{
4360 if (!this->name_components.empty ())
4361 return;
4362
4363 this->name_components_casing = case_sensitivity;
4364 auto *name_cmp
4365 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4366
4367 /* The code below only knows how to break apart components of C++
4368 symbol names (and other languages that use '::' as
4369 namespace/module separator). If we add support for wild matching
4370 to some language that uses some other operator (E.g., Ada, Go and
4371 D use '.'), then we'll need to try splitting the symbol name
4372 according to that language too. Note that Ada does support wild
4373 matching, but doesn't currently support .gdb_index. */
4374 for (size_t iter = 0; iter < this->symbol_table_slots; ++iter)
4375 {
4376 offset_type idx = 2 * iter;
4377
4378 if (this->symbol_table[idx] == 0
4379 && this->symbol_table[idx + 1] == 0)
4380 continue;
4381
4382 const char *name = this->symbol_name_at (idx);
4383
4384 /* Add each name component to the name component table. */
4385 unsigned int previous_len = 0;
4386 for (unsigned int current_len = cp_find_first_component (name);
4387 name[current_len] != '\0';
4388 current_len += cp_find_first_component (name + current_len))
4389 {
4390 gdb_assert (name[current_len] == ':');
4391 this->name_components.push_back ({previous_len, idx});
4392 /* Skip the '::'. */
4393 current_len += 2;
4394 previous_len = current_len;
4395 }
4396 this->name_components.push_back ({previous_len, idx});
4397 }
4398
4399 /* Sort name_components elements by name. */
4400 auto name_comp_compare = [&] (const name_component &left,
4401 const name_component &right)
4402 {
4403 const char *left_qualified = this->symbol_name_at (left.idx);
4404 const char *right_qualified = this->symbol_name_at (right.idx);
4405
4406 const char *left_name = left_qualified + left.name_offset;
4407 const char *right_name = right_qualified + right.name_offset;
4408
4409 return name_cmp (left_name, right_name) < 0;
4410 };
4411
4412 std::sort (this->name_components.begin (),
4413 this->name_components.end (),
4414 name_comp_compare);
4415}
4416
4417/* Helper for dw2_expand_symtabs_matching that works with a
4418 mapped_index instead of the containing objfile. This is split to a
4419 separate function in order to be able to unit test the
4420 name_components matching using a mock mapped_index. For each
4421 symbol name that matches, calls MATCH_CALLBACK, passing it the
4422 symbol's index in the mapped_index symbol table. */
4423
4424static void
4425dw2_expand_symtabs_matching_symbol
4426 (mapped_index &index,
4427 const lookup_name_info &lookup_name_in,
4428 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4429 enum search_domain kind,
4430 gdb::function_view<void (offset_type)> match_callback)
4431{
4432 lookup_name_info lookup_name_without_params
4433 = lookup_name_in.make_ignore_params ();
4434 gdb_index_symbol_name_matcher lookup_name_matcher
4435 (lookup_name_without_params);
4436
4437 /* Build the symbol name component sorted vector, if we haven't
4438 yet. */
4439 index.build_name_components ();
4440
4441 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4442
3f563c84
PA
4443 /* Now for each symbol name in range, check to see if we have a name
4444 match, and if so, call the MATCH_CALLBACK callback. */
4445
4446 /* The same symbol may appear more than once in the range though.
4447 E.g., if we're looking for symbols that complete "w", and we have
4448 a symbol named "w1::w2", we'll find the two name components for
4449 that same symbol in the range. To be sure we only call the
4450 callback once per symbol, we first collect the symbol name
4451 indexes that matched in a temporary vector and ignore
4452 duplicates. */
4453 std::vector<offset_type> matches;
5c58de74 4454 matches.reserve (std::distance (bounds.first, bounds.second));
3f563c84 4455
5c58de74 4456 for (; bounds.first != bounds.second; ++bounds.first)
3f563c84 4457 {
5c58de74 4458 const char *qualified = index.symbol_name_at (bounds.first->idx);
3f563c84
PA
4459
4460 if (!lookup_name_matcher.matches (qualified)
4461 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4462 continue;
4463
5c58de74 4464 matches.push_back (bounds.first->idx);
3f563c84
PA
4465 }
4466
4467 std::sort (matches.begin (), matches.end ());
4468
4469 /* Finally call the callback, once per match. */
4470 ULONGEST prev = -1;
4471 for (offset_type idx : matches)
4472 {
4473 if (prev != idx)
4474 {
4475 match_callback (idx);
4476 prev = idx;
4477 }
4478 }
4479
4480 /* Above we use a type wider than idx's for 'prev', since 0 and
4481 (offset_type)-1 are both possible values. */
4482 static_assert (sizeof (prev) > sizeof (offset_type), "");
4483}
4484
c62446b1
PA
4485#if GDB_SELF_TEST
4486
4487namespace selftests { namespace dw2_expand_symtabs_matching {
4488
4489/* A wrapper around mapped_index that builds a mock mapped_index, from
4490 the symbol list passed as parameter to the constructor. */
4491class mock_mapped_index
4492{
4493public:
4494 template<size_t N>
4495 mock_mapped_index (const char *(&symbols)[N])
4496 : mock_mapped_index (symbols, N)
4497 {}
4498
4499 /* Access the built index. */
4500 mapped_index &index ()
4501 { return m_index; }
4502
4503 /* Disable copy. */
4504 mock_mapped_index(const mock_mapped_index &) = delete;
4505 void operator= (const mock_mapped_index &) = delete;
4506
4507private:
4508 mock_mapped_index (const char **symbols, size_t symbols_size)
4509 {
4510 /* No string can live at offset zero. Add a dummy entry. */
4511 obstack_grow_str0 (&m_constant_pool, "");
4512
4513 for (size_t i = 0; i < symbols_size; i++)
4514 {
4515 const char *sym = symbols[i];
4516 size_t offset = obstack_object_size (&m_constant_pool);
4517 obstack_grow_str0 (&m_constant_pool, sym);
4518 m_symbol_table.push_back (offset);
4519 m_symbol_table.push_back (0);
4520 };
4521
4522 m_index.constant_pool = (const char *) obstack_base (&m_constant_pool);
4523 m_index.symbol_table = m_symbol_table.data ();
4524 m_index.symbol_table_slots = m_symbol_table.size () / 2;
4525 }
4526
4527public:
4528 /* The built mapped_index. */
4529 mapped_index m_index{};
4530
4531 /* The storage that the built mapped_index uses for symbol and
4532 constant pool tables. */
4533 std::vector<offset_type> m_symbol_table;
4534 auto_obstack m_constant_pool;
4535};
4536
4537/* Convenience function that converts a NULL pointer to a "<null>"
4538 string, to pass to print routines. */
4539
4540static const char *
4541string_or_null (const char *str)
4542{
4543 return str != NULL ? str : "<null>";
4544}
4545
4546/* Check if a lookup_name_info built from
4547 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4548 index. EXPECTED_LIST is the list of expected matches, in expected
4549 matching order. If no match expected, then an empty list is
4550 specified. Returns true on success. On failure prints a warning
4551 indicating the file:line that failed, and returns false. */
4552
4553static bool
4554check_match (const char *file, int line,
4555 mock_mapped_index &mock_index,
4556 const char *name, symbol_name_match_type match_type,
4557 bool completion_mode,
4558 std::initializer_list<const char *> expected_list)
4559{
4560 lookup_name_info lookup_name (name, match_type, completion_mode);
4561
4562 bool matched = true;
4563
4564 auto mismatch = [&] (const char *expected_str,
4565 const char *got)
4566 {
4567 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4568 "expected=\"%s\", got=\"%s\"\n"),
4569 file, line,
4570 (match_type == symbol_name_match_type::FULL
4571 ? "FULL" : "WILD"),
4572 name, string_or_null (expected_str), string_or_null (got));
4573 matched = false;
4574 };
4575
4576 auto expected_it = expected_list.begin ();
4577 auto expected_end = expected_list.end ();
4578
4579 dw2_expand_symtabs_matching_symbol (mock_index.index (), lookup_name,
4580 NULL, ALL_DOMAIN,
4581 [&] (offset_type idx)
4582 {
4583 const char *matched_name = mock_index.index ().symbol_name_at (idx);
4584 const char *expected_str
4585 = expected_it == expected_end ? NULL : *expected_it++;
4586
4587 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4588 mismatch (expected_str, matched_name);
4589 });
4590
4591 const char *expected_str
4592 = expected_it == expected_end ? NULL : *expected_it++;
4593 if (expected_str != NULL)
4594 mismatch (expected_str, NULL);
4595
4596 return matched;
4597}
4598
4599/* The symbols added to the mock mapped_index for testing (in
4600 canonical form). */
4601static const char *test_symbols[] = {
4602 "function",
4603 "std::bar",
4604 "std::zfunction",
4605 "std::zfunction2",
4606 "w1::w2",
4607 "ns::foo<char*>",
4608 "ns::foo<int>",
4609 "ns::foo<long>",
a20714ff
PA
4610 "ns2::tmpl<int>::foo2",
4611 "(anonymous namespace)::A::B::C",
c62446b1 4612
e1ef7d7a
PA
4613 /* These are used to check that the increment-last-char in the
4614 matching algorithm for completion doesn't match "t1_fund" when
4615 completing "t1_func". */
4616 "t1_func",
4617 "t1_func1",
4618 "t1_fund",
4619 "t1_fund1",
4620
4621 /* A UTF-8 name with multi-byte sequences to make sure that
4622 cp-name-parser understands this as a single identifier ("função"
4623 is "function" in PT). */
4624 u8"u8função",
4625
4626 /* \377 (0xff) is Latin1 'ÿ'. */
4627 "yfunc\377",
4628
4629 /* \377 (0xff) is Latin1 'ÿ'. */
4630 "\377",
4631 "\377\377123",
4632
c62446b1
PA
4633 /* A name with all sorts of complications. Starts with "z" to make
4634 it easier for the completion tests below. */
4635#define Z_SYM_NAME \
4636 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4637 "::tuple<(anonymous namespace)::ui*, " \
4638 "std::default_delete<(anonymous namespace)::ui>, void>"
4639
4640 Z_SYM_NAME
4641};
4642
5c58de74
PA
4643/* Returns true if the mapped_index::find_name_component_bounds method
4644 finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME, in
4645 completion mode. */
4646
4647static bool
4648check_find_bounds_finds (mapped_index &index,
4649 const char *search_name,
4650 gdb::array_view<const char *> expected_syms)
4651{
4652 lookup_name_info lookup_name (search_name,
4653 symbol_name_match_type::FULL, true);
4654
4655 auto bounds = index.find_name_components_bounds (lookup_name);
4656
4657 size_t distance = std::distance (bounds.first, bounds.second);
4658 if (distance != expected_syms.size ())
4659 return false;
4660
4661 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4662 {
4663 auto nc_elem = bounds.first + exp_elem;
4664 const char *qualified = index.symbol_name_at (nc_elem->idx);
4665 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4666 return false;
4667 }
4668
4669 return true;
4670}
4671
4672/* Test the lower-level mapped_index::find_name_component_bounds
4673 method. */
4674
c62446b1 4675static void
5c58de74
PA
4676test_mapped_index_find_name_component_bounds ()
4677{
4678 mock_mapped_index mock_index (test_symbols);
4679
4680 mock_index.index ().build_name_components ();
4681
4682 /* Test the lower-level mapped_index::find_name_component_bounds
4683 method in completion mode. */
4684 {
4685 static const char *expected_syms[] = {
4686 "t1_func",
4687 "t1_func1",
5c58de74
PA
4688 };
4689
4690 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
4691 "t1_func", expected_syms));
4692 }
4693
4694 /* Check that the increment-last-char in the name matching algorithm
4695 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4696 {
4697 static const char *expected_syms1[] = {
4698 "\377",
4699 "\377\377123",
4700 };
4701 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
4702 "\377", expected_syms1));
4703
4704 static const char *expected_syms2[] = {
4705 "\377\377123",
4706 };
4707 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
4708 "\377\377", expected_syms2));
4709 }
4710}
4711
4712/* Test dw2_expand_symtabs_matching_symbol. */
4713
4714static void
4715test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4716{
4717 mock_mapped_index mock_index (test_symbols);
4718
4719 /* We let all tests run until the end even if some fails, for debug
4720 convenience. */
4721 bool any_mismatch = false;
4722
4723 /* Create the expected symbols list (an initializer_list). Needed
4724 because lists have commas, and we need to pass them to CHECK,
4725 which is a macro. */
4726#define EXPECT(...) { __VA_ARGS__ }
4727
4728 /* Wrapper for check_match that passes down the current
4729 __FILE__/__LINE__. */
4730#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4731 any_mismatch |= !check_match (__FILE__, __LINE__, \
4732 mock_index, \
4733 NAME, MATCH_TYPE, COMPLETION_MODE, \
4734 EXPECTED_LIST)
4735
4736 /* Identity checks. */
4737 for (const char *sym : test_symbols)
4738 {
4739 /* Should be able to match all existing symbols. */
4740 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4741 EXPECT (sym));
4742
4743 /* Should be able to match all existing symbols with
4744 parameters. */
4745 std::string with_params = std::string (sym) + "(int)";
4746 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4747 EXPECT (sym));
4748
4749 /* Should be able to match all existing symbols with
4750 parameters and qualifiers. */
4751 with_params = std::string (sym) + " ( int ) const";
4752 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4753 EXPECT (sym));
4754
4755 /* This should really find sym, but cp-name-parser.y doesn't
4756 know about lvalue/rvalue qualifiers yet. */
4757 with_params = std::string (sym) + " ( int ) &&";
4758 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4759 {});
4760 }
4761
e1ef7d7a
PA
4762 /* Check that the name matching algorithm for completion doesn't get
4763 confused with Latin1 'ÿ' / 0xff. */
4764 {
4765 static const char str[] = "\377";
4766 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4767 EXPECT ("\377", "\377\377123"));
4768 }
4769
4770 /* Check that the increment-last-char in the matching algorithm for
4771 completion doesn't match "t1_fund" when completing "t1_func". */
4772 {
4773 static const char str[] = "t1_func";
4774 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4775 EXPECT ("t1_func", "t1_func1"));
4776 }
4777
c62446b1
PA
4778 /* Check that completion mode works at each prefix of the expected
4779 symbol name. */
4780 {
4781 static const char str[] = "function(int)";
4782 size_t len = strlen (str);
4783 std::string lookup;
4784
4785 for (size_t i = 1; i < len; i++)
4786 {
4787 lookup.assign (str, i);
4788 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4789 EXPECT ("function"));
4790 }
4791 }
4792
4793 /* While "w" is a prefix of both components, the match function
4794 should still only be called once. */
4795 {
4796 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4797 EXPECT ("w1::w2"));
a20714ff
PA
4798 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4799 EXPECT ("w1::w2"));
c62446b1
PA
4800 }
4801
4802 /* Same, with a "complicated" symbol. */
4803 {
4804 static const char str[] = Z_SYM_NAME;
4805 size_t len = strlen (str);
4806 std::string lookup;
4807
4808 for (size_t i = 1; i < len; i++)
4809 {
4810 lookup.assign (str, i);
4811 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4812 EXPECT (Z_SYM_NAME));
4813 }
4814 }
4815
4816 /* In FULL mode, an incomplete symbol doesn't match. */
4817 {
4818 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4819 {});
4820 }
4821
4822 /* A complete symbol with parameters matches any overload, since the
4823 index has no overload info. */
4824 {
4825 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4826 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4827 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4828 EXPECT ("std::zfunction", "std::zfunction2"));
4829 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4830 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4831 }
4832
4833 /* Check that whitespace is ignored appropriately. A symbol with a
4834 template argument list. */
4835 {
4836 static const char expected[] = "ns::foo<int>";
4837 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4838 EXPECT (expected));
a20714ff
PA
4839 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4840 EXPECT (expected));
c62446b1
PA
4841 }
4842
4843 /* Check that whitespace is ignored appropriately. A symbol with a
4844 template argument list that includes a pointer. */
4845 {
4846 static const char expected[] = "ns::foo<char*>";
4847 /* Try both completion and non-completion modes. */
4848 static const bool completion_mode[2] = {false, true};
4849 for (size_t i = 0; i < 2; i++)
4850 {
4851 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4852 completion_mode[i], EXPECT (expected));
a20714ff
PA
4853 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4854 completion_mode[i], EXPECT (expected));
c62446b1
PA
4855
4856 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4857 completion_mode[i], EXPECT (expected));
a20714ff
PA
4858 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4859 completion_mode[i], EXPECT (expected));
c62446b1
PA
4860 }
4861 }
4862
4863 {
4864 /* Check method qualifiers are ignored. */
4865 static const char expected[] = "ns::foo<char*>";
4866 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4867 symbol_name_match_type::FULL, true, EXPECT (expected));
4868 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4869 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4870 CHECK_MATCH ("foo < char * > ( int ) const",
4871 symbol_name_match_type::WILD, true, EXPECT (expected));
4872 CHECK_MATCH ("foo < char * > ( int ) &&",
4873 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4874 }
4875
4876 /* Test lookup names that don't match anything. */
4877 {
a20714ff
PA
4878 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4879 {});
4880
c62446b1
PA
4881 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4882 {});
4883 }
4884
a20714ff
PA
4885 /* Some wild matching tests, exercising "(anonymous namespace)",
4886 which should not be confused with a parameter list. */
4887 {
4888 static const char *syms[] = {
4889 "A::B::C",
4890 "B::C",
4891 "C",
4892 "A :: B :: C ( int )",
4893 "B :: C ( int )",
4894 "C ( int )",
4895 };
4896
4897 for (const char *s : syms)
4898 {
4899 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4900 EXPECT ("(anonymous namespace)::A::B::C"));
4901 }
4902 }
4903
4904 {
4905 static const char expected[] = "ns2::tmpl<int>::foo2";
4906 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4907 EXPECT (expected));
4908 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4909 EXPECT (expected));
4910 }
4911
c62446b1
PA
4912 SELF_CHECK (!any_mismatch);
4913
4914#undef EXPECT
4915#undef CHECK_MATCH
4916}
4917
5c58de74
PA
4918static void
4919run_test ()
4920{
4921 test_mapped_index_find_name_component_bounds ();
4922 test_dw2_expand_symtabs_matching_symbol ();
4923}
4924
c62446b1
PA
4925}} // namespace selftests::dw2_expand_symtabs_matching
4926
4927#endif /* GDB_SELF_TEST */
4928
3f563c84
PA
4929/* Helper for dw2_expand_matching symtabs. Called on each symbol
4930 matched, to expand corresponding CUs that were marked. IDX is the
4931 index of the symbol name that matched. */
4932
4933static void
4934dw2_expand_marked_cus
4935 (mapped_index &index, offset_type idx,
4936 struct objfile *objfile,
4937 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4938 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4939 search_domain kind)
4940{
4941 const char *name;
4942 offset_type *vec, vec_len, vec_idx;
4943 bool global_seen = false;
4944
61920122
PA
4945 vec = (offset_type *) (index.constant_pool
4946 + MAYBE_SWAP (index.symbol_table[idx + 1]));
4947 vec_len = MAYBE_SWAP (vec[0]);
4948 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4949 {
4950 struct dwarf2_per_cu_data *per_cu;
4951 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4952 /* This value is only valid for index versions >= 7. */
4953 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4954 gdb_index_symbol_kind symbol_kind =
4955 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4956 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4957 /* Only check the symbol attributes if they're present.
4958 Indices prior to version 7 don't record them,
4959 and indices >= 7 may elide them for certain symbols
4960 (gold does this). */
4961 int attrs_valid =
4962 (index.version >= 7
4963 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4964
4965 /* Work around gold/15646. */
4966 if (attrs_valid)
9291a0cd 4967 {
61920122
PA
4968 if (!is_static && global_seen)
4969 continue;
4970 if (!is_static)
4971 global_seen = true;
4972 }
3190f0c6 4973
61920122
PA
4974 /* Only check the symbol's kind if it has one. */
4975 if (attrs_valid)
4976 {
4977 switch (kind)
8943b874 4978 {
61920122
PA
4979 case VARIABLES_DOMAIN:
4980 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4981 continue;
4982 break;
4983 case FUNCTIONS_DOMAIN:
4984 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4985 continue;
61920122
PA
4986 break;
4987 case TYPES_DOMAIN:
4988 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4989 continue;
4990 break;
4991 default:
4992 break;
8943b874 4993 }
61920122 4994 }
8943b874 4995
61920122
PA
4996 /* Don't crash on bad data. */
4997 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4998 + dwarf2_per_objfile->n_type_units))
4999 {
5000 complaint (&symfile_complaints,
5001 _(".gdb_index entry has bad CU index"
5002 " [in module %s]"), objfile_name (objfile));
5003 continue;
5004 }
5005
5006 per_cu = dw2_get_cutu (cu_index);
5007 if (file_matcher == NULL || per_cu->v.quick->mark)
5008 {
5009 int symtab_was_null =
5010 (per_cu->v.quick->compunit_symtab == NULL);
5011
5012 dw2_instantiate_symtab (per_cu);
5013
5014 if (expansion_notify != NULL
5015 && symtab_was_null
5016 && per_cu->v.quick->compunit_symtab != NULL)
5017 expansion_notify (per_cu->v.quick->compunit_symtab);
5018 }
5019 }
5020}
5021
5022static void
5023dw2_expand_symtabs_matching
5024 (struct objfile *objfile,
5025 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5026 const lookup_name_info &lookup_name,
5027 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5028 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5029 enum search_domain kind)
5030{
5031 int i;
5032 offset_type iter;
5033
5034 dw2_setup (objfile);
5035
5036 /* index_table is NULL if OBJF_READNOW. */
5037 if (!dwarf2_per_objfile->index_table)
5038 return;
5039
5040 if (file_matcher != NULL)
5041 {
5042 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5043 htab_eq_pointer,
5044 NULL, xcalloc, xfree));
5045 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5046 htab_eq_pointer,
5047 NULL, xcalloc, xfree));
5048
5049 /* The rule is CUs specify all the files, including those used by
5050 any TU, so there's no need to scan TUs here. */
5051
5052 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5053 {
5054 int j;
5055 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5056 struct quick_file_names *file_data;
5057 void **slot;
5058
5059 QUIT;
5060
5061 per_cu->v.quick->mark = 0;
156942c7 5062
61920122
PA
5063 /* We only need to look at symtabs not already expanded. */
5064 if (per_cu->v.quick->compunit_symtab)
5065 continue;
5066
5067 file_data = dw2_get_file_names (per_cu);
5068 if (file_data == NULL)
5069 continue;
5070
5071 if (htab_find (visited_not_found.get (), file_data) != NULL)
5072 continue;
5073 else if (htab_find (visited_found.get (), file_data) != NULL)
3190f0c6 5074 {
61920122 5075 per_cu->v.quick->mark = 1;
3190f0c6
DE
5076 continue;
5077 }
5078
61920122 5079 for (j = 0; j < file_data->num_file_names; ++j)
276d885b 5080 {
61920122 5081 const char *this_real_name;
276d885b 5082
61920122
PA
5083 if (file_matcher (file_data->file_names[j], false))
5084 {
5085 per_cu->v.quick->mark = 1;
5086 break;
5087 }
5088
5089 /* Before we invoke realpath, which can get expensive when many
5090 files are involved, do a quick comparison of the basenames. */
5091 if (!basenames_may_differ
5092 && !file_matcher (lbasename (file_data->file_names[j]),
5093 true))
5094 continue;
276d885b 5095
61920122
PA
5096 this_real_name = dw2_get_real_path (objfile, file_data, j);
5097 if (file_matcher (this_real_name, false))
276d885b 5098 {
61920122
PA
5099 per_cu->v.quick->mark = 1;
5100 break;
276d885b
GB
5101 }
5102 }
61920122
PA
5103
5104 slot = htab_find_slot (per_cu->v.quick->mark
5105 ? visited_found.get ()
5106 : visited_not_found.get (),
5107 file_data, INSERT);
5108 *slot = file_data;
9291a0cd 5109 }
61920122
PA
5110 }
5111
5112 mapped_index &index = *dwarf2_per_objfile->index_table;
5113
5114 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5115 symbol_matcher,
5116 kind, [&] (offset_type idx)
5117 {
5118 dw2_expand_marked_cus (index, idx, objfile, file_matcher,
5119 expansion_notify, kind);
5120 });
9291a0cd
TT
5121}
5122
43f3e411 5123/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
9703b513
TT
5124 symtab. */
5125
43f3e411
DE
5126static struct compunit_symtab *
5127recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5128 CORE_ADDR pc)
9703b513
TT
5129{
5130 int i;
5131
43f3e411
DE
5132 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5133 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5134 return cust;
9703b513 5135
43f3e411 5136 if (cust->includes == NULL)
a3ec0bb1
DE
5137 return NULL;
5138
43f3e411 5139 for (i = 0; cust->includes[i]; ++i)
9703b513 5140 {
43f3e411 5141 struct compunit_symtab *s = cust->includes[i];
9703b513 5142
43f3e411 5143 s = recursively_find_pc_sect_compunit_symtab (s, pc);
9703b513
TT
5144 if (s != NULL)
5145 return s;
5146 }
5147
5148 return NULL;
5149}
5150
43f3e411
DE
5151static struct compunit_symtab *
5152dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5153 struct bound_minimal_symbol msymbol,
5154 CORE_ADDR pc,
5155 struct obj_section *section,
5156 int warn_if_readin)
9291a0cd
TT
5157{
5158 struct dwarf2_per_cu_data *data;
43f3e411 5159 struct compunit_symtab *result;
9291a0cd
TT
5160
5161 dw2_setup (objfile);
5162
5163 if (!objfile->psymtabs_addrmap)
5164 return NULL;
5165
9a3c8263
SM
5166 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5167 pc);
9291a0cd
TT
5168 if (!data)
5169 return NULL;
5170
43f3e411 5171 if (warn_if_readin && data->v.quick->compunit_symtab)
abebb8b0 5172 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
5173 paddress (get_objfile_arch (objfile), pc));
5174
43f3e411
DE
5175 result
5176 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5177 pc);
9703b513
TT
5178 gdb_assert (result != NULL);
5179 return result;
9291a0cd
TT
5180}
5181
9291a0cd 5182static void
44b13c5a 5183dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 5184 void *data, int need_fullname)
9291a0cd 5185{
9291a0cd 5186 dw2_setup (objfile);
ae2de4f8 5187
bbf2f4df 5188 if (!dwarf2_per_objfile->filenames_cache)
24c79950 5189 {
bbf2f4df 5190 dwarf2_per_objfile->filenames_cache.emplace ();
24c79950 5191
bbf2f4df
PA
5192 htab_up visited (htab_create_alloc (10,
5193 htab_hash_pointer, htab_eq_pointer,
5194 NULL, xcalloc, xfree));
24c79950 5195
bbf2f4df
PA
5196 /* The rule is CUs specify all the files, including those used
5197 by any TU, so there's no need to scan TUs here. We can
5198 ignore file names coming from already-expanded CUs. */
24c79950 5199
bbf2f4df
PA
5200 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5201 {
5202 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 5203
bbf2f4df
PA
5204 if (per_cu->v.quick->compunit_symtab)
5205 {
5206 void **slot = htab_find_slot (visited.get (),
5207 per_cu->v.quick->file_names,
5208 INSERT);
9291a0cd 5209
bbf2f4df
PA
5210 *slot = per_cu->v.quick->file_names;
5211 }
24c79950 5212 }
24c79950 5213
bbf2f4df 5214 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 5215 {
bbf2f4df
PA
5216 int j;
5217 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5218 struct quick_file_names *file_data;
5219 void **slot;
5220
5221 /* We only need to look at symtabs not already expanded. */
5222 if (per_cu->v.quick->compunit_symtab)
5223 continue;
74e2f255 5224
bbf2f4df
PA
5225 file_data = dw2_get_file_names (per_cu);
5226 if (file_data == NULL)
5227 continue;
5228
5229 slot = htab_find_slot (visited.get (), file_data, INSERT);
5230 if (*slot)
5231 {
5232 /* Already visited. */
5233 continue;
5234 }
5235 *slot = file_data;
5236
5237 for (int j = 0; j < file_data->num_file_names; ++j)
5238 {
5239 const char *filename = file_data->file_names[j];
5240 dwarf2_per_objfile->filenames_cache->seen (filename);
5241 }
9291a0cd
TT
5242 }
5243 }
bbf2f4df
PA
5244
5245 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5246 {
14278e1f 5247 gdb::unique_xmalloc_ptr<char> this_real_name;
bbf2f4df
PA
5248
5249 if (need_fullname)
5250 this_real_name = gdb_realpath (filename);
14278e1f 5251 (*fun) (filename, this_real_name.get (), data);
bbf2f4df 5252 });
9291a0cd
TT
5253}
5254
5255static int
5256dw2_has_symbols (struct objfile *objfile)
5257{
5258 return 1;
5259}
5260
5261const struct quick_symbol_functions dwarf2_gdb_index_functions =
5262{
5263 dw2_has_symbols,
5264 dw2_find_last_source_symtab,
5265 dw2_forget_cached_source_info,
f8eba3c6 5266 dw2_map_symtabs_matching_filename,
9291a0cd 5267 dw2_lookup_symbol,
9291a0cd
TT
5268 dw2_print_stats,
5269 dw2_dump,
5270 dw2_relocate,
5271 dw2_expand_symtabs_for_function,
5272 dw2_expand_all_symtabs,
652a8996 5273 dw2_expand_symtabs_with_fullname,
40658b94 5274 dw2_map_matching_symbols,
9291a0cd 5275 dw2_expand_symtabs_matching,
43f3e411 5276 dw2_find_pc_sect_compunit_symtab,
71a3c369 5277 NULL,
9291a0cd
TT
5278 dw2_map_symbol_filenames
5279};
5280
5281/* Initialize for reading DWARF for this objfile. Return 0 if this
5282 file will use psymtabs, or 1 if using the GNU index. */
5283
5284int
5285dwarf2_initialize_objfile (struct objfile *objfile)
5286{
5287 /* If we're about to read full symbols, don't bother with the
5288 indices. In this case we also don't care if some other debug
5289 format is making psymtabs, because they are all about to be
5290 expanded anyway. */
5291 if ((objfile->flags & OBJF_READNOW))
5292 {
5293 int i;
5294
5295 dwarf2_per_objfile->using_index = 1;
5296 create_all_comp_units (objfile);
0e50663e 5297 create_all_type_units (objfile);
7b9f3c50
DE
5298 dwarf2_per_objfile->quick_file_names_table =
5299 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 5300
1fd400ff 5301 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 5302 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 5303 {
8832e7e3 5304 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 5305
e254ef6a
DE
5306 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5307 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5308 }
5309
5310 /* Return 1 so that gdb sees the "quick" functions. However,
5311 these functions will be no-ops because we will have expanded
5312 all symtabs. */
5313 return 1;
5314 }
5315
5316 if (dwarf2_read_index (objfile))
5317 return 1;
5318
9291a0cd
TT
5319 return 0;
5320}
5321
5322\f
5323
dce234bc
PP
5324/* Build a partial symbol table. */
5325
5326void
f29dff0a 5327dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5328{
c9bf0622 5329
af5bf4ad
SM
5330 if (objfile->global_psymbols.capacity () == 0
5331 && objfile->static_psymbols.capacity () == 0)
5332 init_psymbol_list (objfile, 1024);
c906108c 5333
492d29ea 5334 TRY
c9bf0622
TT
5335 {
5336 /* This isn't really ideal: all the data we allocate on the
5337 objfile's obstack is still uselessly kept around. However,
5338 freeing it seems unsafe. */
906768f9 5339 psymtab_discarder psymtabs (objfile);
c9bf0622 5340 dwarf2_build_psymtabs_hard (objfile);
906768f9 5341 psymtabs.keep ();
c9bf0622 5342 }
492d29ea
PA
5343 CATCH (except, RETURN_MASK_ERROR)
5344 {
5345 exception_print (gdb_stderr, except);
5346 }
5347 END_CATCH
c906108c 5348}
c906108c 5349
1ce1cefd
DE
5350/* Return the total length of the CU described by HEADER. */
5351
5352static unsigned int
5353get_cu_length (const struct comp_unit_head *header)
5354{
5355 return header->initial_length_size + header->length;
5356}
5357
9c541725 5358/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 5359
9c541725
PA
5360static inline bool
5361offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 5362{
9c541725
PA
5363 sect_offset bottom = cu_header->sect_off;
5364 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 5365
9c541725 5366 return sect_off >= bottom && sect_off < top;
45452591
DE
5367}
5368
3b80fe9b
DE
5369/* Find the base address of the compilation unit for range lists and
5370 location lists. It will normally be specified by DW_AT_low_pc.
5371 In DWARF-3 draft 4, the base address could be overridden by
5372 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5373 compilation units with discontinuous ranges. */
5374
5375static void
5376dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5377{
5378 struct attribute *attr;
5379
5380 cu->base_known = 0;
5381 cu->base_address = 0;
5382
5383 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5384 if (attr)
5385 {
31aa7e4e 5386 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
5387 cu->base_known = 1;
5388 }
5389 else
5390 {
5391 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5392 if (attr)
5393 {
31aa7e4e 5394 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
5395 cu->base_known = 1;
5396 }
5397 }
5398}
5399
93311388 5400/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 5401 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
5402 NOTE: This leaves members offset, first_die_offset to be filled in
5403 by the caller. */
107d2387 5404
d521ce57 5405static const gdb_byte *
107d2387 5406read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
5407 const gdb_byte *info_ptr,
5408 struct dwarf2_section_info *section,
5409 rcuh_kind section_kind)
107d2387
AC
5410{
5411 int signed_addr;
891d2f0b 5412 unsigned int bytes_read;
43988095
JK
5413 const char *filename = get_section_file_name (section);
5414 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
5415
5416 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
5417 cu_header->initial_length_size = bytes_read;
5418 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 5419 info_ptr += bytes_read;
107d2387
AC
5420 cu_header->version = read_2_bytes (abfd, info_ptr);
5421 info_ptr += 2;
43988095
JK
5422 if (cu_header->version < 5)
5423 switch (section_kind)
5424 {
5425 case rcuh_kind::COMPILE:
5426 cu_header->unit_type = DW_UT_compile;
5427 break;
5428 case rcuh_kind::TYPE:
5429 cu_header->unit_type = DW_UT_type;
5430 break;
5431 default:
5432 internal_error (__FILE__, __LINE__,
5433 _("read_comp_unit_head: invalid section_kind"));
5434 }
5435 else
5436 {
5437 cu_header->unit_type = static_cast<enum dwarf_unit_type>
5438 (read_1_byte (abfd, info_ptr));
5439 info_ptr += 1;
5440 switch (cu_header->unit_type)
5441 {
5442 case DW_UT_compile:
5443 if (section_kind != rcuh_kind::COMPILE)
5444 error (_("Dwarf Error: wrong unit_type in compilation unit header "
5445 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
5446 filename);
5447 break;
5448 case DW_UT_type:
5449 section_kind = rcuh_kind::TYPE;
5450 break;
5451 default:
5452 error (_("Dwarf Error: wrong unit_type in compilation unit header "
5453 "(is %d, should be %d or %d) [in module %s]"),
5454 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
5455 }
5456
5457 cu_header->addr_size = read_1_byte (abfd, info_ptr);
5458 info_ptr += 1;
5459 }
9c541725
PA
5460 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
5461 cu_header,
5462 &bytes_read);
613e1657 5463 info_ptr += bytes_read;
43988095
JK
5464 if (cu_header->version < 5)
5465 {
5466 cu_header->addr_size = read_1_byte (abfd, info_ptr);
5467 info_ptr += 1;
5468 }
107d2387
AC
5469 signed_addr = bfd_get_sign_extend_vma (abfd);
5470 if (signed_addr < 0)
8e65ff28 5471 internal_error (__FILE__, __LINE__,
e2e0b3e5 5472 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 5473 cu_header->signed_addr_p = signed_addr;
c764a876 5474
43988095
JK
5475 if (section_kind == rcuh_kind::TYPE)
5476 {
5477 LONGEST type_offset;
5478
5479 cu_header->signature = read_8_bytes (abfd, info_ptr);
5480 info_ptr += 8;
5481
5482 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
5483 info_ptr += bytes_read;
9c541725
PA
5484 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
5485 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
5486 error (_("Dwarf Error: Too big type_offset in compilation unit "
5487 "header (is %s) [in module %s]"), plongest (type_offset),
5488 filename);
5489 }
5490
107d2387
AC
5491 return info_ptr;
5492}
5493
36586728
TT
5494/* Helper function that returns the proper abbrev section for
5495 THIS_CU. */
5496
5497static struct dwarf2_section_info *
5498get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5499{
5500 struct dwarf2_section_info *abbrev;
5501
5502 if (this_cu->is_dwz)
5503 abbrev = &dwarf2_get_dwz_file ()->abbrev;
5504 else
5505 abbrev = &dwarf2_per_objfile->abbrev;
5506
5507 return abbrev;
5508}
5509
9ff913ba
DE
5510/* Subroutine of read_and_check_comp_unit_head and
5511 read_and_check_type_unit_head to simplify them.
5512 Perform various error checking on the header. */
5513
5514static void
5515error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
5516 struct dwarf2_section_info *section,
5517 struct dwarf2_section_info *abbrev_section)
9ff913ba 5518{
a32a8923 5519 const char *filename = get_section_file_name (section);
9ff913ba 5520
43988095 5521 if (header->version < 2 || header->version > 5)
9ff913ba 5522 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 5523 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
5524 filename);
5525
9c541725 5526 if (to_underlying (header->abbrev_sect_off)
36586728 5527 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9c541725
PA
5528 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
5529 "(offset 0x%x + 6) [in module %s]"),
5530 to_underlying (header->abbrev_sect_off),
5531 to_underlying (header->sect_off),
9ff913ba
DE
5532 filename);
5533
9c541725 5534 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 5535 avoid potential 32-bit overflow. */
9c541725 5536 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 5537 > section->size)
9c541725
PA
5538 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
5539 "(offset 0x%x + 0) [in module %s]"),
5540 header->length, to_underlying (header->sect_off),
9ff913ba
DE
5541 filename);
5542}
5543
5544/* Read in a CU/TU header and perform some basic error checking.
5545 The contents of the header are stored in HEADER.
5546 The result is a pointer to the start of the first DIE. */
adabb602 5547
d521ce57 5548static const gdb_byte *
9ff913ba
DE
5549read_and_check_comp_unit_head (struct comp_unit_head *header,
5550 struct dwarf2_section_info *section,
4bdcc0c1 5551 struct dwarf2_section_info *abbrev_section,
d521ce57 5552 const gdb_byte *info_ptr,
43988095 5553 rcuh_kind section_kind)
72bf9492 5554{
d521ce57 5555 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 5556 bfd *abfd = get_section_bfd_owner (section);
72bf9492 5557
9c541725 5558 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 5559
43988095 5560 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 5561
9c541725 5562 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 5563
4bdcc0c1 5564 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
5565
5566 return info_ptr;
348e048f
DE
5567}
5568
f4dc4d17
DE
5569/* Fetch the abbreviation table offset from a comp or type unit header. */
5570
5571static sect_offset
5572read_abbrev_offset (struct dwarf2_section_info *section,
9c541725 5573 sect_offset sect_off)
f4dc4d17 5574{
a32a8923 5575 bfd *abfd = get_section_bfd_owner (section);
d521ce57 5576 const gdb_byte *info_ptr;
ac298888 5577 unsigned int initial_length_size, offset_size;
43988095 5578 uint16_t version;
f4dc4d17
DE
5579
5580 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 5581 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5582 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5583 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5584 info_ptr += initial_length_size;
5585
5586 version = read_2_bytes (abfd, info_ptr);
5587 info_ptr += 2;
5588 if (version >= 5)
5589 {
5590 /* Skip unit type and address size. */
5591 info_ptr += 2;
5592 }
5593
9c541725 5594 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
5595}
5596
aaa75496
JB
5597/* Allocate a new partial symtab for file named NAME and mark this new
5598 partial symtab as being an include of PST. */
5599
5600static void
d521ce57 5601dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
5602 struct objfile *objfile)
5603{
5604 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
5605
fbd9ab74
JK
5606 if (!IS_ABSOLUTE_PATH (subpst->filename))
5607 {
5608 /* It shares objfile->objfile_obstack. */
5609 subpst->dirname = pst->dirname;
5610 }
5611
aaa75496
JB
5612 subpst->textlow = 0;
5613 subpst->texthigh = 0;
5614
8d749320
SM
5615 subpst->dependencies
5616 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
5617 subpst->dependencies[0] = pst;
5618 subpst->number_of_dependencies = 1;
5619
5620 subpst->globals_offset = 0;
5621 subpst->n_global_syms = 0;
5622 subpst->statics_offset = 0;
5623 subpst->n_static_syms = 0;
43f3e411 5624 subpst->compunit_symtab = NULL;
aaa75496
JB
5625 subpst->read_symtab = pst->read_symtab;
5626 subpst->readin = 0;
5627
5628 /* No private part is necessary for include psymtabs. This property
5629 can be used to differentiate between such include psymtabs and
10b3939b 5630 the regular ones. */
58a9656e 5631 subpst->read_symtab_private = NULL;
aaa75496
JB
5632}
5633
5634/* Read the Line Number Program data and extract the list of files
5635 included by the source file represented by PST. Build an include
d85a05f0 5636 partial symtab for each of these included files. */
aaa75496
JB
5637
5638static void
5639dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
5640 struct die_info *die,
5641 struct partial_symtab *pst)
aaa75496 5642{
fff8551c 5643 line_header_up lh;
d85a05f0 5644 struct attribute *attr;
aaa75496 5645
d85a05f0
DJ
5646 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5647 if (attr)
9c541725 5648 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
5649 if (lh == NULL)
5650 return; /* No linetable, so no includes. */
5651
c6da4cef 5652 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 5653 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
5654}
5655
348e048f 5656static hashval_t
52dc124a 5657hash_signatured_type (const void *item)
348e048f 5658{
9a3c8263
SM
5659 const struct signatured_type *sig_type
5660 = (const struct signatured_type *) item;
9a619af0 5661
348e048f 5662 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 5663 return sig_type->signature;
348e048f
DE
5664}
5665
5666static int
52dc124a 5667eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 5668{
9a3c8263
SM
5669 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5670 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 5671
348e048f
DE
5672 return lhs->signature == rhs->signature;
5673}
5674
1fd400ff
TT
5675/* Allocate a hash table for signatured types. */
5676
5677static htab_t
673bfd45 5678allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
5679{
5680 return htab_create_alloc_ex (41,
52dc124a
DE
5681 hash_signatured_type,
5682 eq_signatured_type,
1fd400ff
TT
5683 NULL,
5684 &objfile->objfile_obstack,
5685 hashtab_obstack_allocate,
5686 dummy_obstack_deallocate);
5687}
5688
d467dd73 5689/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
5690
5691static int
d467dd73 5692add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 5693{
9a3c8263
SM
5694 struct signatured_type *sigt = (struct signatured_type *) *slot;
5695 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 5696
b4dd5633 5697 **datap = sigt;
1fd400ff
TT
5698 ++*datap;
5699
5700 return 1;
5701}
5702
78d4d2c5 5703/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
5704 and fill them into TYPES_HTAB. It will process only type units,
5705 therefore DW_UT_type. */
c88ee1f0 5706
78d4d2c5
JK
5707static void
5708create_debug_type_hash_table (struct dwo_file *dwo_file,
43988095
JK
5709 dwarf2_section_info *section, htab_t &types_htab,
5710 rcuh_kind section_kind)
348e048f 5711{
3019eac3 5712 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 5713 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
5714 bfd *abfd;
5715 const gdb_byte *info_ptr, *end_ptr;
348e048f 5716
4bdcc0c1
DE
5717 abbrev_section = (dwo_file != NULL
5718 ? &dwo_file->sections.abbrev
5719 : &dwarf2_per_objfile->abbrev);
5720
b4f54984 5721 if (dwarf_read_debug)
43988095
JK
5722 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
5723 get_section_name (section),
a32a8923 5724 get_section_file_name (abbrev_section));
09406207 5725
78d4d2c5
JK
5726 dwarf2_read_section (objfile, section);
5727 info_ptr = section->buffer;
348e048f 5728
78d4d2c5
JK
5729 if (info_ptr == NULL)
5730 return;
348e048f 5731
78d4d2c5
JK
5732 /* We can't set abfd until now because the section may be empty or
5733 not present, in which case the bfd is unknown. */
5734 abfd = get_section_bfd_owner (section);
348e048f 5735
78d4d2c5
JK
5736 /* We don't use init_cutu_and_read_dies_simple, or some such, here
5737 because we don't need to read any dies: the signature is in the
5738 header. */
3019eac3 5739
78d4d2c5
JK
5740 end_ptr = info_ptr + section->size;
5741 while (info_ptr < end_ptr)
5742 {
78d4d2c5
JK
5743 struct signatured_type *sig_type;
5744 struct dwo_unit *dwo_tu;
5745 void **slot;
5746 const gdb_byte *ptr = info_ptr;
5747 struct comp_unit_head header;
5748 unsigned int length;
8b70b953 5749
9c541725 5750 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 5751
a49dd8dd
JK
5752 /* Initialize it due to a false compiler warning. */
5753 header.signature = -1;
9c541725 5754 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 5755
78d4d2c5
JK
5756 /* We need to read the type's signature in order to build the hash
5757 table, but we don't need anything else just yet. */
348e048f 5758
43988095
JK
5759 ptr = read_and_check_comp_unit_head (&header, section,
5760 abbrev_section, ptr, section_kind);
348e048f 5761
78d4d2c5 5762 length = get_cu_length (&header);
6caca83c 5763
78d4d2c5
JK
5764 /* Skip dummy type units. */
5765 if (ptr >= info_ptr + length
43988095
JK
5766 || peek_abbrev_code (abfd, ptr) == 0
5767 || header.unit_type != DW_UT_type)
78d4d2c5
JK
5768 {
5769 info_ptr += length;
5770 continue;
5771 }
dee91e82 5772
78d4d2c5
JK
5773 if (types_htab == NULL)
5774 {
5775 if (dwo_file)
5776 types_htab = allocate_dwo_unit_table (objfile);
5777 else
5778 types_htab = allocate_signatured_type_table (objfile);
5779 }
8b70b953 5780
78d4d2c5
JK
5781 if (dwo_file)
5782 {
5783 sig_type = NULL;
5784 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5785 struct dwo_unit);
5786 dwo_tu->dwo_file = dwo_file;
43988095 5787 dwo_tu->signature = header.signature;
9c541725 5788 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 5789 dwo_tu->section = section;
9c541725 5790 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
5791 dwo_tu->length = length;
5792 }
5793 else
5794 {
5795 /* N.B.: type_offset is not usable if this type uses a DWO file.
5796 The real type_offset is in the DWO file. */
5797 dwo_tu = NULL;
5798 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5799 struct signatured_type);
43988095 5800 sig_type->signature = header.signature;
9c541725 5801 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
5802 sig_type->per_cu.objfile = objfile;
5803 sig_type->per_cu.is_debug_types = 1;
5804 sig_type->per_cu.section = section;
9c541725 5805 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
5806 sig_type->per_cu.length = length;
5807 }
5808
5809 slot = htab_find_slot (types_htab,
5810 dwo_file ? (void*) dwo_tu : (void *) sig_type,
5811 INSERT);
5812 gdb_assert (slot != NULL);
5813 if (*slot != NULL)
5814 {
9c541725 5815 sect_offset dup_sect_off;
0349ea22 5816
3019eac3
DE
5817 if (dwo_file)
5818 {
78d4d2c5
JK
5819 const struct dwo_unit *dup_tu
5820 = (const struct dwo_unit *) *slot;
5821
9c541725 5822 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
5823 }
5824 else
5825 {
78d4d2c5
JK
5826 const struct signatured_type *dup_tu
5827 = (const struct signatured_type *) *slot;
5828
9c541725 5829 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 5830 }
8b70b953 5831
78d4d2c5
JK
5832 complaint (&symfile_complaints,
5833 _("debug type entry at offset 0x%x is duplicate to"
5834 " the entry at offset 0x%x, signature %s"),
9c541725 5835 to_underlying (sect_off), to_underlying (dup_sect_off),
43988095 5836 hex_string (header.signature));
78d4d2c5
JK
5837 }
5838 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 5839
78d4d2c5
JK
5840 if (dwarf_read_debug > 1)
5841 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
9c541725 5842 to_underlying (sect_off),
43988095 5843 hex_string (header.signature));
3019eac3 5844
78d4d2c5
JK
5845 info_ptr += length;
5846 }
5847}
3019eac3 5848
78d4d2c5
JK
5849/* Create the hash table of all entries in the .debug_types
5850 (or .debug_types.dwo) section(s).
5851 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
5852 otherwise it is NULL.
b3c8eb43 5853
78d4d2c5 5854 The result is a pointer to the hash table or NULL if there are no types.
348e048f 5855
78d4d2c5 5856 Note: This function processes DWO files only, not DWP files. */
348e048f 5857
78d4d2c5
JK
5858static void
5859create_debug_types_hash_table (struct dwo_file *dwo_file,
5860 VEC (dwarf2_section_info_def) *types,
5861 htab_t &types_htab)
5862{
5863 int ix;
5864 struct dwarf2_section_info *section;
5865
5866 if (VEC_empty (dwarf2_section_info_def, types))
5867 return;
348e048f 5868
78d4d2c5
JK
5869 for (ix = 0;
5870 VEC_iterate (dwarf2_section_info_def, types, ix, section);
5871 ++ix)
43988095
JK
5872 create_debug_type_hash_table (dwo_file, section, types_htab,
5873 rcuh_kind::TYPE);
3019eac3
DE
5874}
5875
5876/* Create the hash table of all entries in the .debug_types section,
5877 and initialize all_type_units.
5878 The result is zero if there is an error (e.g. missing .debug_types section),
5879 otherwise non-zero. */
5880
5881static int
5882create_all_type_units (struct objfile *objfile)
5883{
78d4d2c5 5884 htab_t types_htab = NULL;
b4dd5633 5885 struct signatured_type **iter;
3019eac3 5886
43988095
JK
5887 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
5888 rcuh_kind::COMPILE);
78d4d2c5 5889 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
3019eac3
DE
5890 if (types_htab == NULL)
5891 {
5892 dwarf2_per_objfile->signatured_types = NULL;
5893 return 0;
5894 }
5895
348e048f
DE
5896 dwarf2_per_objfile->signatured_types = types_htab;
5897
6aa5f3a6
DE
5898 dwarf2_per_objfile->n_type_units
5899 = dwarf2_per_objfile->n_allocated_type_units
5900 = htab_elements (types_htab);
8d749320
SM
5901 dwarf2_per_objfile->all_type_units =
5902 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
5903 iter = &dwarf2_per_objfile->all_type_units[0];
5904 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
5905 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
5906 == dwarf2_per_objfile->n_type_units);
1fd400ff 5907
348e048f
DE
5908 return 1;
5909}
5910
6aa5f3a6
DE
5911/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
5912 If SLOT is non-NULL, it is the entry to use in the hash table.
5913 Otherwise we find one. */
5914
5915static struct signatured_type *
5916add_type_unit (ULONGEST sig, void **slot)
5917{
5918 struct objfile *objfile = dwarf2_per_objfile->objfile;
5919 int n_type_units = dwarf2_per_objfile->n_type_units;
5920 struct signatured_type *sig_type;
5921
5922 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
5923 ++n_type_units;
5924 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
5925 {
5926 if (dwarf2_per_objfile->n_allocated_type_units == 0)
5927 dwarf2_per_objfile->n_allocated_type_units = 1;
5928 dwarf2_per_objfile->n_allocated_type_units *= 2;
5929 dwarf2_per_objfile->all_type_units
224c3ddb
SM
5930 = XRESIZEVEC (struct signatured_type *,
5931 dwarf2_per_objfile->all_type_units,
5932 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
5933 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5934 }
5935 dwarf2_per_objfile->n_type_units = n_type_units;
5936
5937 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5938 struct signatured_type);
5939 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5940 sig_type->signature = sig;
5941 sig_type->per_cu.is_debug_types = 1;
5942 if (dwarf2_per_objfile->using_index)
5943 {
5944 sig_type->per_cu.v.quick =
5945 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5946 struct dwarf2_per_cu_quick_data);
5947 }
5948
5949 if (slot == NULL)
5950 {
5951 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5952 sig_type, INSERT);
5953 }
5954 gdb_assert (*slot == NULL);
5955 *slot = sig_type;
5956 /* The rest of sig_type must be filled in by the caller. */
5957 return sig_type;
5958}
5959
a2ce51a0
DE
5960/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5961 Fill in SIG_ENTRY with DWO_ENTRY. */
5962
5963static void
5964fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5965 struct signatured_type *sig_entry,
5966 struct dwo_unit *dwo_entry)
5967{
7ee85ab1 5968 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
5969 gdb_assert (! sig_entry->per_cu.queued);
5970 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
5971 if (dwarf2_per_objfile->using_index)
5972 {
5973 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 5974 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
5975 }
5976 else
5977 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 5978 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 5979 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 5980 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
5981 gdb_assert (sig_entry->dwo_unit == NULL);
5982
5983 sig_entry->per_cu.section = dwo_entry->section;
9c541725 5984 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
5985 sig_entry->per_cu.length = dwo_entry->length;
5986 sig_entry->per_cu.reading_dwo_directly = 1;
5987 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
5988 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5989 sig_entry->dwo_unit = dwo_entry;
5990}
5991
5992/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
5993 If we haven't read the TU yet, create the signatured_type data structure
5994 for a TU to be read in directly from a DWO file, bypassing the stub.
5995 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5996 using .gdb_index, then when reading a CU we want to stay in the DWO file
5997 containing that CU. Otherwise we could end up reading several other DWO
5998 files (due to comdat folding) to process the transitive closure of all the
5999 mentioned TUs, and that can be slow. The current DWO file will have every
6000 type signature that it needs.
a2ce51a0
DE
6001 We only do this for .gdb_index because in the psymtab case we already have
6002 to read all the DWOs to build the type unit groups. */
6003
6004static struct signatured_type *
6005lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6006{
6007 struct objfile *objfile = dwarf2_per_objfile->objfile;
6008 struct dwo_file *dwo_file;
6009 struct dwo_unit find_dwo_entry, *dwo_entry;
6010 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6011 void **slot;
a2ce51a0
DE
6012
6013 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6014
6aa5f3a6
DE
6015 /* If TU skeletons have been removed then we may not have read in any
6016 TUs yet. */
6017 if (dwarf2_per_objfile->signatured_types == NULL)
6018 {
6019 dwarf2_per_objfile->signatured_types
6020 = allocate_signatured_type_table (objfile);
6021 }
a2ce51a0
DE
6022
6023 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6024 Use the global signatured_types array to do our own comdat-folding
6025 of types. If this is the first time we're reading this TU, and
6026 the TU has an entry in .gdb_index, replace the recorded data from
6027 .gdb_index with this TU. */
a2ce51a0 6028
a2ce51a0 6029 find_sig_entry.signature = sig;
6aa5f3a6
DE
6030 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6031 &find_sig_entry, INSERT);
9a3c8263 6032 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6033
6034 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6035 read. Don't reassign the global entry to point to this DWO if that's
6036 the case. Also note that if the TU is already being read, it may not
6037 have come from a DWO, the program may be a mix of Fission-compiled
6038 code and non-Fission-compiled code. */
6039
6040 /* Have we already tried to read this TU?
6041 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6042 needn't exist in the global table yet). */
6043 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6044 return sig_entry;
6045
6aa5f3a6
DE
6046 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6047 dwo_unit of the TU itself. */
6048 dwo_file = cu->dwo_unit->dwo_file;
6049
a2ce51a0
DE
6050 /* Ok, this is the first time we're reading this TU. */
6051 if (dwo_file->tus == NULL)
6052 return NULL;
6053 find_dwo_entry.signature = sig;
9a3c8263 6054 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
6055 if (dwo_entry == NULL)
6056 return NULL;
6057
6aa5f3a6
DE
6058 /* If the global table doesn't have an entry for this TU, add one. */
6059 if (sig_entry == NULL)
6060 sig_entry = add_type_unit (sig, slot);
6061
a2ce51a0 6062 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 6063 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6064 return sig_entry;
6065}
6066
a2ce51a0
DE
6067/* Subroutine of lookup_signatured_type.
6068 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6069 then try the DWP file. If the TU stub (skeleton) has been removed then
6070 it won't be in .gdb_index. */
a2ce51a0
DE
6071
6072static struct signatured_type *
6073lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6074{
6075 struct objfile *objfile = dwarf2_per_objfile->objfile;
6076 struct dwp_file *dwp_file = get_dwp_file ();
6077 struct dwo_unit *dwo_entry;
6078 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6079 void **slot;
a2ce51a0
DE
6080
6081 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6082 gdb_assert (dwp_file != NULL);
6083
6aa5f3a6
DE
6084 /* If TU skeletons have been removed then we may not have read in any
6085 TUs yet. */
6086 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 6087 {
6aa5f3a6
DE
6088 dwarf2_per_objfile->signatured_types
6089 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
6090 }
6091
6aa5f3a6
DE
6092 find_sig_entry.signature = sig;
6093 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6094 &find_sig_entry, INSERT);
9a3c8263 6095 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6096
6097 /* Have we already tried to read this TU?
6098 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6099 needn't exist in the global table yet). */
6100 if (sig_entry != NULL)
6101 return sig_entry;
6102
a2ce51a0
DE
6103 if (dwp_file->tus == NULL)
6104 return NULL;
57d63ce2
DE
6105 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
6106 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6107 if (dwo_entry == NULL)
6108 return NULL;
6109
6aa5f3a6 6110 sig_entry = add_type_unit (sig, slot);
a2ce51a0
DE
6111 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
6112
a2ce51a0
DE
6113 return sig_entry;
6114}
6115
380bca97 6116/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6117 Returns NULL if signature SIG is not present in the table.
6118 It is up to the caller to complain about this. */
348e048f
DE
6119
6120static struct signatured_type *
a2ce51a0 6121lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6122{
a2ce51a0
DE
6123 if (cu->dwo_unit
6124 && dwarf2_per_objfile->using_index)
6125 {
6126 /* We're in a DWO/DWP file, and we're using .gdb_index.
6127 These cases require special processing. */
6128 if (get_dwp_file () == NULL)
6129 return lookup_dwo_signatured_type (cu, sig);
6130 else
6131 return lookup_dwp_signatured_type (cu, sig);
6132 }
6133 else
6134 {
6135 struct signatured_type find_entry, *entry;
348e048f 6136
a2ce51a0
DE
6137 if (dwarf2_per_objfile->signatured_types == NULL)
6138 return NULL;
6139 find_entry.signature = sig;
9a3c8263
SM
6140 entry = ((struct signatured_type *)
6141 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
6142 return entry;
6143 }
348e048f 6144}
42e7ad6c
DE
6145\f
6146/* Low level DIE reading support. */
348e048f 6147
d85a05f0
DJ
6148/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6149
6150static void
6151init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6152 struct dwarf2_cu *cu,
3019eac3
DE
6153 struct dwarf2_section_info *section,
6154 struct dwo_file *dwo_file)
d85a05f0 6155{
fceca515 6156 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 6157 reader->abfd = get_section_bfd_owner (section);
d85a05f0 6158 reader->cu = cu;
3019eac3 6159 reader->dwo_file = dwo_file;
dee91e82
DE
6160 reader->die_section = section;
6161 reader->buffer = section->buffer;
f664829e 6162 reader->buffer_end = section->buffer + section->size;
a2ce51a0 6163 reader->comp_dir = NULL;
d85a05f0
DJ
6164}
6165
b0c7bfa9
DE
6166/* Subroutine of init_cutu_and_read_dies to simplify it.
6167 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6168 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
6169 already.
6170
6171 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6172 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6173 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6174 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6175 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6176 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
6177 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
6178 are filled in with the info of the DIE from the DWO file.
6179 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
6180 provided an abbrev table to use.
6181 The result is non-zero if a valid (non-dummy) DIE was found. */
6182
6183static int
6184read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6185 struct dwo_unit *dwo_unit,
6186 int abbrev_table_provided,
6187 struct die_info *stub_comp_unit_die,
a2ce51a0 6188 const char *stub_comp_dir,
b0c7bfa9 6189 struct die_reader_specs *result_reader,
d521ce57 6190 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
6191 struct die_info **result_comp_unit_die,
6192 int *result_has_children)
6193{
6194 struct objfile *objfile = dwarf2_per_objfile->objfile;
6195 struct dwarf2_cu *cu = this_cu->cu;
6196 struct dwarf2_section_info *section;
6197 bfd *abfd;
d521ce57 6198 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6199 ULONGEST signature; /* Or dwo_id. */
6200 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6201 int i,num_extra_attrs;
6202 struct dwarf2_section_info *dwo_abbrev_section;
6203 struct attribute *attr;
6204 struct die_info *comp_unit_die;
6205
b0aeadb3
DE
6206 /* At most one of these may be provided. */
6207 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6208
b0c7bfa9
DE
6209 /* These attributes aren't processed until later:
6210 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6211 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6212 referenced later. However, these attributes are found in the stub
6213 which we won't have later. In order to not impose this complication
6214 on the rest of the code, we read them here and copy them to the
6215 DWO CU/TU die. */
b0c7bfa9
DE
6216
6217 stmt_list = NULL;
6218 low_pc = NULL;
6219 high_pc = NULL;
6220 ranges = NULL;
6221 comp_dir = NULL;
6222
6223 if (stub_comp_unit_die != NULL)
6224 {
6225 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6226 DWO file. */
6227 if (! this_cu->is_debug_types)
6228 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6229 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6230 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6231 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6232 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6233
6234 /* There should be a DW_AT_addr_base attribute here (if needed).
6235 We need the value before we can process DW_FORM_GNU_addr_index. */
6236 cu->addr_base = 0;
6237 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
6238 if (attr)
6239 cu->addr_base = DW_UNSND (attr);
6240
6241 /* There should be a DW_AT_ranges_base attribute here (if needed).
6242 We need the value before we can process DW_AT_ranges. */
6243 cu->ranges_base = 0;
6244 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
6245 if (attr)
6246 cu->ranges_base = DW_UNSND (attr);
6247 }
a2ce51a0
DE
6248 else if (stub_comp_dir != NULL)
6249 {
6250 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6251 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6252 comp_dir->name = DW_AT_comp_dir;
6253 comp_dir->form = DW_FORM_string;
6254 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6255 DW_STRING (comp_dir) = stub_comp_dir;
6256 }
b0c7bfa9
DE
6257
6258 /* Set up for reading the DWO CU/TU. */
6259 cu->dwo_unit = dwo_unit;
6260 section = dwo_unit->section;
6261 dwarf2_read_section (objfile, section);
a32a8923 6262 abfd = get_section_bfd_owner (section);
9c541725
PA
6263 begin_info_ptr = info_ptr = (section->buffer
6264 + to_underlying (dwo_unit->sect_off));
b0c7bfa9
DE
6265 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6266 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
6267
6268 if (this_cu->is_debug_types)
6269 {
b0c7bfa9
DE
6270 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6271
43988095 6272 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
b0c7bfa9 6273 dwo_abbrev_section,
43988095 6274 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6275 /* This is not an assert because it can be caused by bad debug info. */
43988095 6276 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6277 {
6278 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6279 " TU at offset 0x%x [in module %s]"),
6280 hex_string (sig_type->signature),
43988095 6281 hex_string (cu->header.signature),
9c541725 6282 to_underlying (dwo_unit->sect_off),
a2ce51a0
DE
6283 bfd_get_filename (abfd));
6284 }
9c541725 6285 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6286 /* For DWOs coming from DWP files, we don't know the CU length
6287 nor the type's offset in the TU until now. */
6288 dwo_unit->length = get_cu_length (&cu->header);
9c541725 6289 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6290
6291 /* Establish the type offset that can be used to lookup the type.
6292 For DWO files, we don't know it until now. */
9c541725
PA
6293 sig_type->type_offset_in_section
6294 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6295 }
6296 else
6297 {
6298 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6299 dwo_abbrev_section,
43988095 6300 info_ptr, rcuh_kind::COMPILE);
9c541725 6301 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6302 /* For DWOs coming from DWP files, we don't know the CU length
6303 until now. */
6304 dwo_unit->length = get_cu_length (&cu->header);
6305 }
6306
02142a6c
DE
6307 /* Replace the CU's original abbrev table with the DWO's.
6308 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
6309 if (abbrev_table_provided)
6310 {
6311 /* Don't free the provided abbrev table, the caller of
6312 init_cutu_and_read_dies owns it. */
6313 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 6314 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
6315 make_cleanup (dwarf2_free_abbrev_table, cu);
6316 }
6317 else
6318 {
6319 dwarf2_free_abbrev_table (cu);
6320 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 6321 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
6322 }
6323
6324 /* Read in the die, but leave space to copy over the attributes
6325 from the stub. This has the benefit of simplifying the rest of
6326 the code - all the work to maintain the illusion of a single
6327 DW_TAG_{compile,type}_unit DIE is done here. */
6328 num_extra_attrs = ((stmt_list != NULL)
6329 + (low_pc != NULL)
6330 + (high_pc != NULL)
6331 + (ranges != NULL)
6332 + (comp_dir != NULL));
6333 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6334 result_has_children, num_extra_attrs);
6335
6336 /* Copy over the attributes from the stub to the DIE we just read in. */
6337 comp_unit_die = *result_comp_unit_die;
6338 i = comp_unit_die->num_attrs;
6339 if (stmt_list != NULL)
6340 comp_unit_die->attrs[i++] = *stmt_list;
6341 if (low_pc != NULL)
6342 comp_unit_die->attrs[i++] = *low_pc;
6343 if (high_pc != NULL)
6344 comp_unit_die->attrs[i++] = *high_pc;
6345 if (ranges != NULL)
6346 comp_unit_die->attrs[i++] = *ranges;
6347 if (comp_dir != NULL)
6348 comp_unit_die->attrs[i++] = *comp_dir;
6349 comp_unit_die->num_attrs += num_extra_attrs;
6350
b4f54984 6351 if (dwarf_die_debug)
bf6af496
DE
6352 {
6353 fprintf_unfiltered (gdb_stdlog,
6354 "Read die from %s@0x%x of %s:\n",
a32a8923 6355 get_section_name (section),
bf6af496
DE
6356 (unsigned) (begin_info_ptr - section->buffer),
6357 bfd_get_filename (abfd));
b4f54984 6358 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6359 }
6360
a2ce51a0
DE
6361 /* Save the comp_dir attribute. If there is no DWP file then we'll read
6362 TUs by skipping the stub and going directly to the entry in the DWO file.
6363 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
6364 to get it via circuitous means. Blech. */
6365 if (comp_dir != NULL)
6366 result_reader->comp_dir = DW_STRING (comp_dir);
6367
b0c7bfa9
DE
6368 /* Skip dummy compilation units. */
6369 if (info_ptr >= begin_info_ptr + dwo_unit->length
6370 || peek_abbrev_code (abfd, info_ptr) == 0)
6371 return 0;
6372
6373 *result_info_ptr = info_ptr;
6374 return 1;
6375}
6376
6377/* Subroutine of init_cutu_and_read_dies to simplify it.
6378 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6379 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6380
6381static struct dwo_unit *
6382lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6383 struct die_info *comp_unit_die)
6384{
6385 struct dwarf2_cu *cu = this_cu->cu;
6386 struct attribute *attr;
6387 ULONGEST signature;
6388 struct dwo_unit *dwo_unit;
6389 const char *comp_dir, *dwo_name;
6390
a2ce51a0
DE
6391 gdb_assert (cu != NULL);
6392
b0c7bfa9 6393 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
6394 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
6395 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6396
6397 if (this_cu->is_debug_types)
6398 {
6399 struct signatured_type *sig_type;
6400
6401 /* Since this_cu is the first member of struct signatured_type,
6402 we can go from a pointer to one to a pointer to the other. */
6403 sig_type = (struct signatured_type *) this_cu;
6404 signature = sig_type->signature;
6405 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6406 }
6407 else
6408 {
6409 struct attribute *attr;
6410
6411 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6412 if (! attr)
6413 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6414 " [in module %s]"),
4262abfb 6415 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
6416 signature = DW_UNSND (attr);
6417 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6418 signature);
6419 }
6420
b0c7bfa9
DE
6421 return dwo_unit;
6422}
6423
a2ce51a0 6424/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6
DE
6425 See it for a description of the parameters.
6426 Read a TU directly from a DWO file, bypassing the stub.
6427
6428 Note: This function could be a little bit simpler if we shared cleanups
6429 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
6430 to do, so we keep this function self-contained. Or we could move this
6431 into our caller, but it's complex enough already. */
a2ce51a0
DE
6432
6433static void
6aa5f3a6
DE
6434init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6435 int use_existing_cu, int keep,
a2ce51a0
DE
6436 die_reader_func_ftype *die_reader_func,
6437 void *data)
6438{
6439 struct dwarf2_cu *cu;
6440 struct signatured_type *sig_type;
6aa5f3a6 6441 struct cleanup *cleanups, *free_cu_cleanup = NULL;
a2ce51a0
DE
6442 struct die_reader_specs reader;
6443 const gdb_byte *info_ptr;
6444 struct die_info *comp_unit_die;
6445 int has_children;
6446
6447 /* Verify we can do the following downcast, and that we have the
6448 data we need. */
6449 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6450 sig_type = (struct signatured_type *) this_cu;
6451 gdb_assert (sig_type->dwo_unit != NULL);
6452
6453 cleanups = make_cleanup (null_cleanup, NULL);
6454
6aa5f3a6
DE
6455 if (use_existing_cu && this_cu->cu != NULL)
6456 {
6457 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6458 cu = this_cu->cu;
6459 /* There's no need to do the rereading_dwo_cu handling that
6460 init_cutu_and_read_dies does since we don't read the stub. */
6461 }
6462 else
6463 {
6464 /* If !use_existing_cu, this_cu->cu must be NULL. */
6465 gdb_assert (this_cu->cu == NULL);
8d749320 6466 cu = XNEW (struct dwarf2_cu);
6aa5f3a6
DE
6467 init_one_comp_unit (cu, this_cu);
6468 /* If an error occurs while loading, release our storage. */
6469 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
6470 }
6471
6472 /* A future optimization, if needed, would be to use an existing
6473 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6474 could share abbrev tables. */
a2ce51a0
DE
6475
6476 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6477 0 /* abbrev_table_provided */,
6478 NULL /* stub_comp_unit_die */,
6479 sig_type->dwo_unit->dwo_file->comp_dir,
6480 &reader, &info_ptr,
6481 &comp_unit_die, &has_children) == 0)
6482 {
6483 /* Dummy die. */
6484 do_cleanups (cleanups);
6485 return;
6486 }
6487
6488 /* All the "real" work is done here. */
6489 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6490
6aa5f3a6 6491 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
6492 but the alternative is making the latter more complex.
6493 This function is only for the special case of using DWO files directly:
6494 no point in overly complicating the general case just to handle this. */
6aa5f3a6 6495 if (free_cu_cleanup != NULL)
a2ce51a0 6496 {
6aa5f3a6
DE
6497 if (keep)
6498 {
6499 /* We've successfully allocated this compilation unit. Let our
6500 caller clean it up when finished with it. */
6501 discard_cleanups (free_cu_cleanup);
a2ce51a0 6502
6aa5f3a6
DE
6503 /* We can only discard free_cu_cleanup and all subsequent cleanups.
6504 So we have to manually free the abbrev table. */
6505 dwarf2_free_abbrev_table (cu);
a2ce51a0 6506
6aa5f3a6
DE
6507 /* Link this CU into read_in_chain. */
6508 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6509 dwarf2_per_objfile->read_in_chain = this_cu;
6510 }
6511 else
6512 do_cleanups (free_cu_cleanup);
a2ce51a0 6513 }
a2ce51a0
DE
6514
6515 do_cleanups (cleanups);
6516}
6517
fd820528 6518/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6519 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6520
f4dc4d17
DE
6521 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6522 Otherwise the table specified in the comp unit header is read in and used.
6523 This is an optimization for when we already have the abbrev table.
6524
dee91e82
DE
6525 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6526 Otherwise, a new CU is allocated with xmalloc.
6527
6528 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
6529 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
6530
6531 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 6532 linker) then DIE_READER_FUNC will not get called. */
aaa75496 6533
70221824 6534static void
fd820528 6535init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 6536 struct abbrev_table *abbrev_table,
fd820528
DE
6537 int use_existing_cu, int keep,
6538 die_reader_func_ftype *die_reader_func,
6539 void *data)
c906108c 6540{
dee91e82 6541 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6542 struct dwarf2_section_info *section = this_cu->section;
a32a8923 6543 bfd *abfd = get_section_bfd_owner (section);
dee91e82 6544 struct dwarf2_cu *cu;
d521ce57 6545 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 6546 struct die_reader_specs reader;
d85a05f0 6547 struct die_info *comp_unit_die;
dee91e82 6548 int has_children;
d85a05f0 6549 struct attribute *attr;
365156ad 6550 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 6551 struct signatured_type *sig_type = NULL;
4bdcc0c1 6552 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6553 /* Non-zero if CU currently points to a DWO file and we need to
6554 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6555 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6556 int rereading_dwo_cu = 0;
c906108c 6557
b4f54984 6558 if (dwarf_die_debug)
09406207
DE
6559 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
6560 this_cu->is_debug_types ? "type" : "comp",
9c541725 6561 to_underlying (this_cu->sect_off));
09406207 6562
dee91e82
DE
6563 if (use_existing_cu)
6564 gdb_assert (keep);
23745b47 6565
a2ce51a0
DE
6566 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6567 file (instead of going through the stub), short-circuit all of this. */
6568 if (this_cu->reading_dwo_directly)
6569 {
6570 /* Narrow down the scope of possibilities to have to understand. */
6571 gdb_assert (this_cu->is_debug_types);
6572 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
6573 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
6574 die_reader_func, data);
a2ce51a0
DE
6575 return;
6576 }
6577
dee91e82
DE
6578 cleanups = make_cleanup (null_cleanup, NULL);
6579
6580 /* This is cheap if the section is already read in. */
6581 dwarf2_read_section (objfile, section);
6582
9c541725 6583 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6584
6585 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6586
6587 if (use_existing_cu && this_cu->cu != NULL)
6588 {
6589 cu = this_cu->cu;
42e7ad6c
DE
6590 /* If this CU is from a DWO file we need to start over, we need to
6591 refetch the attributes from the skeleton CU.
6592 This could be optimized by retrieving those attributes from when we
6593 were here the first time: the previous comp_unit_die was stored in
6594 comp_unit_obstack. But there's no data yet that we need this
6595 optimization. */
6596 if (cu->dwo_unit != NULL)
6597 rereading_dwo_cu = 1;
dee91e82
DE
6598 }
6599 else
6600 {
6601 /* If !use_existing_cu, this_cu->cu must be NULL. */
6602 gdb_assert (this_cu->cu == NULL);
8d749320 6603 cu = XNEW (struct dwarf2_cu);
dee91e82 6604 init_one_comp_unit (cu, this_cu);
dee91e82 6605 /* If an error occurs while loading, release our storage. */
365156ad 6606 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 6607 }
dee91e82 6608
b0c7bfa9 6609 /* Get the header. */
9c541725 6610 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6611 {
6612 /* We already have the header, there's no need to read it in again. */
9c541725 6613 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6614 }
6615 else
6616 {
3019eac3 6617 if (this_cu->is_debug_types)
dee91e82 6618 {
43988095 6619 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4bdcc0c1 6620 abbrev_section, info_ptr,
43988095 6621 rcuh_kind::TYPE);
dee91e82 6622
42e7ad6c
DE
6623 /* Since per_cu is the first member of struct signatured_type,
6624 we can go from a pointer to one to a pointer to the other. */
6625 sig_type = (struct signatured_type *) this_cu;
43988095 6626 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6627 gdb_assert (sig_type->type_offset_in_tu
6628 == cu->header.type_cu_offset_in_tu);
6629 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6630
42e7ad6c
DE
6631 /* LENGTH has not been set yet for type units if we're
6632 using .gdb_index. */
1ce1cefd 6633 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
6634
6635 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6636 sig_type->type_offset_in_section =
6637 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6638
6639 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6640 }
6641 else
6642 {
4bdcc0c1
DE
6643 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6644 abbrev_section,
43988095
JK
6645 info_ptr,
6646 rcuh_kind::COMPILE);
dee91e82 6647
9c541725 6648 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 6649 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 6650 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6651 }
6652 }
10b3939b 6653
6caca83c 6654 /* Skip dummy compilation units. */
dee91e82 6655 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
6656 || peek_abbrev_code (abfd, info_ptr) == 0)
6657 {
dee91e82 6658 do_cleanups (cleanups);
21b2bd31 6659 return;
6caca83c
CC
6660 }
6661
433df2d4
DE
6662 /* If we don't have them yet, read the abbrevs for this compilation unit.
6663 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
6664 done. Note that it's important that if the CU had an abbrev table
6665 on entry we don't free it when we're done: Somewhere up the call stack
6666 it may be in use. */
f4dc4d17
DE
6667 if (abbrev_table != NULL)
6668 {
6669 gdb_assert (cu->abbrev_table == NULL);
9c541725 6670 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
f4dc4d17
DE
6671 cu->abbrev_table = abbrev_table;
6672 }
6673 else if (cu->abbrev_table == NULL)
dee91e82 6674 {
4bdcc0c1 6675 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
6676 make_cleanup (dwarf2_free_abbrev_table, cu);
6677 }
42e7ad6c
DE
6678 else if (rereading_dwo_cu)
6679 {
6680 dwarf2_free_abbrev_table (cu);
6681 dwarf2_read_abbrevs (cu, abbrev_section);
6682 }
af703f96 6683
dee91e82 6684 /* Read the top level CU/TU die. */
3019eac3 6685 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 6686 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 6687
b0c7bfa9
DE
6688 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6689 from the DWO file.
6690 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6691 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
6692 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
6693 if (attr)
6694 {
3019eac3 6695 struct dwo_unit *dwo_unit;
b0c7bfa9 6696 struct die_info *dwo_comp_unit_die;
3019eac3
DE
6697
6698 if (has_children)
6a506a2d
DE
6699 {
6700 complaint (&symfile_complaints,
6701 _("compilation unit with DW_AT_GNU_dwo_name"
6702 " has children (offset 0x%x) [in module %s]"),
9c541725 6703 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6a506a2d 6704 }
b0c7bfa9 6705 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 6706 if (dwo_unit != NULL)
3019eac3 6707 {
6a506a2d
DE
6708 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6709 abbrev_table != NULL,
a2ce51a0 6710 comp_unit_die, NULL,
6a506a2d
DE
6711 &reader, &info_ptr,
6712 &dwo_comp_unit_die, &has_children) == 0)
6713 {
6714 /* Dummy die. */
6715 do_cleanups (cleanups);
6716 return;
6717 }
6718 comp_unit_die = dwo_comp_unit_die;
6719 }
6720 else
6721 {
6722 /* Yikes, we couldn't find the rest of the DIE, we only have
6723 the stub. A complaint has already been logged. There's
6724 not much more we can do except pass on the stub DIE to
6725 die_reader_func. We don't want to throw an error on bad
6726 debug info. */
3019eac3
DE
6727 }
6728 }
6729
b0c7bfa9 6730 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
6731 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6732
b0c7bfa9 6733 /* Done, clean up. */
365156ad 6734 if (free_cu_cleanup != NULL)
348e048f 6735 {
365156ad
TT
6736 if (keep)
6737 {
6738 /* We've successfully allocated this compilation unit. Let our
6739 caller clean it up when finished with it. */
6740 discard_cleanups (free_cu_cleanup);
dee91e82 6741
365156ad
TT
6742 /* We can only discard free_cu_cleanup and all subsequent cleanups.
6743 So we have to manually free the abbrev table. */
6744 dwarf2_free_abbrev_table (cu);
dee91e82 6745
365156ad
TT
6746 /* Link this CU into read_in_chain. */
6747 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6748 dwarf2_per_objfile->read_in_chain = this_cu;
6749 }
6750 else
6751 do_cleanups (free_cu_cleanup);
348e048f 6752 }
365156ad
TT
6753
6754 do_cleanups (cleanups);
dee91e82
DE
6755}
6756
33e80786
DE
6757/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
6758 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
6759 to have already done the lookup to find the DWO file).
dee91e82
DE
6760
6761 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 6762 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
6763
6764 We fill in THIS_CU->length.
6765
6766 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
6767 linker) then DIE_READER_FUNC will not get called.
6768
6769 THIS_CU->cu is always freed when done.
3019eac3
DE
6770 This is done in order to not leave THIS_CU->cu in a state where we have
6771 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
6772
6773static void
6774init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 6775 struct dwo_file *dwo_file,
dee91e82
DE
6776 die_reader_func_ftype *die_reader_func,
6777 void *data)
6778{
6779 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6780 struct dwarf2_section_info *section = this_cu->section;
a32a8923 6781 bfd *abfd = get_section_bfd_owner (section);
33e80786 6782 struct dwarf2_section_info *abbrev_section;
dee91e82 6783 struct dwarf2_cu cu;
d521ce57 6784 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
6785 struct die_reader_specs reader;
6786 struct cleanup *cleanups;
6787 struct die_info *comp_unit_die;
6788 int has_children;
6789
b4f54984 6790 if (dwarf_die_debug)
09406207
DE
6791 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
6792 this_cu->is_debug_types ? "type" : "comp",
9c541725 6793 to_underlying (this_cu->sect_off));
09406207 6794
dee91e82
DE
6795 gdb_assert (this_cu->cu == NULL);
6796
33e80786
DE
6797 abbrev_section = (dwo_file != NULL
6798 ? &dwo_file->sections.abbrev
6799 : get_abbrev_section_for_cu (this_cu));
6800
dee91e82
DE
6801 /* This is cheap if the section is already read in. */
6802 dwarf2_read_section (objfile, section);
6803
6804 init_one_comp_unit (&cu, this_cu);
6805
6806 cleanups = make_cleanup (free_stack_comp_unit, &cu);
6807
9c541725 6808 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4bdcc0c1
DE
6809 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
6810 abbrev_section, info_ptr,
43988095
JK
6811 (this_cu->is_debug_types
6812 ? rcuh_kind::TYPE
6813 : rcuh_kind::COMPILE));
dee91e82 6814
1ce1cefd 6815 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
6816
6817 /* Skip dummy compilation units. */
6818 if (info_ptr >= begin_info_ptr + this_cu->length
6819 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 6820 {
dee91e82 6821 do_cleanups (cleanups);
21b2bd31 6822 return;
93311388 6823 }
72bf9492 6824
dee91e82
DE
6825 dwarf2_read_abbrevs (&cu, abbrev_section);
6826 make_cleanup (dwarf2_free_abbrev_table, &cu);
6827
3019eac3 6828 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
6829 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
6830
6831 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6832
6833 do_cleanups (cleanups);
6834}
6835
3019eac3
DE
6836/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
6837 does not lookup the specified DWO file.
6838 This cannot be used to read DWO files.
dee91e82
DE
6839
6840 THIS_CU->cu is always freed when done.
3019eac3
DE
6841 This is done in order to not leave THIS_CU->cu in a state where we have
6842 to care whether it refers to the "main" CU or the DWO CU.
6843 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
6844
6845static void
6846init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
6847 die_reader_func_ftype *die_reader_func,
6848 void *data)
6849{
33e80786 6850 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 6851}
0018ea6f
DE
6852\f
6853/* Type Unit Groups.
dee91e82 6854
0018ea6f
DE
6855 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6856 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6857 so that all types coming from the same compilation (.o file) are grouped
6858 together. A future step could be to put the types in the same symtab as
6859 the CU the types ultimately came from. */
ff013f42 6860
f4dc4d17
DE
6861static hashval_t
6862hash_type_unit_group (const void *item)
6863{
9a3c8263
SM
6864 const struct type_unit_group *tu_group
6865 = (const struct type_unit_group *) item;
f4dc4d17 6866
094b34ac 6867 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 6868}
348e048f
DE
6869
6870static int
f4dc4d17 6871eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 6872{
9a3c8263
SM
6873 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6874 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 6875
094b34ac 6876 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 6877}
348e048f 6878
f4dc4d17
DE
6879/* Allocate a hash table for type unit groups. */
6880
6881static htab_t
6882allocate_type_unit_groups_table (void)
6883{
6884 return htab_create_alloc_ex (3,
6885 hash_type_unit_group,
6886 eq_type_unit_group,
6887 NULL,
6888 &dwarf2_per_objfile->objfile->objfile_obstack,
6889 hashtab_obstack_allocate,
6890 dummy_obstack_deallocate);
6891}
dee91e82 6892
f4dc4d17
DE
6893/* Type units that don't have DW_AT_stmt_list are grouped into their own
6894 partial symtabs. We combine several TUs per psymtab to not let the size
6895 of any one psymtab grow too big. */
6896#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6897#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 6898
094b34ac 6899/* Helper routine for get_type_unit_group.
f4dc4d17
DE
6900 Create the type_unit_group object used to hold one or more TUs. */
6901
6902static struct type_unit_group *
094b34ac 6903create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
6904{
6905 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 6906 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 6907 struct type_unit_group *tu_group;
f4dc4d17
DE
6908
6909 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6910 struct type_unit_group);
094b34ac 6911 per_cu = &tu_group->per_cu;
f4dc4d17 6912 per_cu->objfile = objfile;
f4dc4d17 6913
094b34ac
DE
6914 if (dwarf2_per_objfile->using_index)
6915 {
6916 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6917 struct dwarf2_per_cu_quick_data);
094b34ac
DE
6918 }
6919 else
6920 {
9c541725 6921 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
6922 struct partial_symtab *pst;
6923 char *name;
6924
6925 /* Give the symtab a useful name for debug purposes. */
6926 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6927 name = xstrprintf ("<type_units_%d>",
6928 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6929 else
6930 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
6931
6932 pst = create_partial_symtab (per_cu, name);
6933 pst->anonymous = 1;
f4dc4d17 6934
094b34ac
DE
6935 xfree (name);
6936 }
f4dc4d17 6937
094b34ac 6938 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 6939 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
6940
6941 return tu_group;
6942}
6943
094b34ac
DE
6944/* Look up the type_unit_group for type unit CU, and create it if necessary.
6945 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
6946
6947static struct type_unit_group *
ff39bb5e 6948get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
6949{
6950 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6951 struct type_unit_group *tu_group;
6952 void **slot;
6953 unsigned int line_offset;
6954 struct type_unit_group type_unit_group_for_lookup;
6955
6956 if (dwarf2_per_objfile->type_unit_groups == NULL)
6957 {
6958 dwarf2_per_objfile->type_unit_groups =
6959 allocate_type_unit_groups_table ();
6960 }
6961
6962 /* Do we need to create a new group, or can we use an existing one? */
6963
6964 if (stmt_list)
6965 {
6966 line_offset = DW_UNSND (stmt_list);
6967 ++tu_stats->nr_symtab_sharers;
6968 }
6969 else
6970 {
6971 /* Ugh, no stmt_list. Rare, but we have to handle it.
6972 We can do various things here like create one group per TU or
6973 spread them over multiple groups to split up the expansion work.
6974 To avoid worst case scenarios (too many groups or too large groups)
6975 we, umm, group them in bunches. */
6976 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6977 | (tu_stats->nr_stmt_less_type_units
6978 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6979 ++tu_stats->nr_stmt_less_type_units;
6980 }
6981
094b34ac 6982 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 6983 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
6984 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6985 &type_unit_group_for_lookup, INSERT);
6986 if (*slot != NULL)
6987 {
9a3c8263 6988 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
6989 gdb_assert (tu_group != NULL);
6990 }
6991 else
6992 {
9c541725 6993 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 6994 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
6995 *slot = tu_group;
6996 ++tu_stats->nr_symtabs;
6997 }
6998
6999 return tu_group;
7000}
0018ea6f
DE
7001\f
7002/* Partial symbol tables. */
7003
7004/* Create a psymtab named NAME and assign it to PER_CU.
7005
7006 The caller must fill in the following details:
7007 dirname, textlow, texthigh. */
7008
7009static struct partial_symtab *
7010create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7011{
7012 struct objfile *objfile = per_cu->objfile;
7013 struct partial_symtab *pst;
7014
18a94d75 7015 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
7016 objfile->global_psymbols,
7017 objfile->static_psymbols);
0018ea6f
DE
7018
7019 pst->psymtabs_addrmap_supported = 1;
7020
7021 /* This is the glue that links PST into GDB's symbol API. */
7022 pst->read_symtab_private = per_cu;
7023 pst->read_symtab = dwarf2_read_symtab;
7024 per_cu->v.psymtab = pst;
7025
7026 return pst;
7027}
7028
b93601f3
TT
7029/* The DATA object passed to process_psymtab_comp_unit_reader has this
7030 type. */
7031
7032struct process_psymtab_comp_unit_data
7033{
7034 /* True if we are reading a DW_TAG_partial_unit. */
7035
7036 int want_partial_unit;
7037
7038 /* The "pretend" language that is used if the CU doesn't declare a
7039 language. */
7040
7041 enum language pretend_language;
7042};
7043
0018ea6f
DE
7044/* die_reader_func for process_psymtab_comp_unit. */
7045
7046static void
7047process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7048 const gdb_byte *info_ptr,
0018ea6f
DE
7049 struct die_info *comp_unit_die,
7050 int has_children,
7051 void *data)
7052{
7053 struct dwarf2_cu *cu = reader->cu;
7054 struct objfile *objfile = cu->objfile;
3e29f34a 7055 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7056 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7057 CORE_ADDR baseaddr;
7058 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7059 struct partial_symtab *pst;
3a2b436a 7060 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7061 const char *filename;
9a3c8263
SM
7062 struct process_psymtab_comp_unit_data *info
7063 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 7064
b93601f3 7065 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
7066 return;
7067
7068 gdb_assert (! per_cu->is_debug_types);
7069
b93601f3 7070 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
7071
7072 cu->list_in_scope = &file_symbols;
7073
7074 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
7075 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7076 if (filename == NULL)
0018ea6f 7077 filename = "";
0018ea6f
DE
7078
7079 pst = create_partial_symtab (per_cu, filename);
7080
7081 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7082 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
7083
7084 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7085
7086 dwarf2_find_base_address (comp_unit_die, cu);
7087
7088 /* Possibly set the default values of LOWPC and HIGHPC from
7089 `DW_AT_ranges'. */
3a2b436a
JK
7090 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7091 &best_highpc, cu, pst);
7092 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
7093 /* Store the contiguous range if it is not empty; it can be empty for
7094 CUs with no code. */
7095 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
7096 gdbarch_adjust_dwarf2_addr (gdbarch,
7097 best_lowpc + baseaddr),
7098 gdbarch_adjust_dwarf2_addr (gdbarch,
7099 best_highpc + baseaddr) - 1,
7100 pst);
0018ea6f
DE
7101
7102 /* Check if comp unit has_children.
7103 If so, read the rest of the partial symbols from this comp unit.
7104 If not, there's no more debug_info for this comp unit. */
7105 if (has_children)
7106 {
7107 struct partial_die_info *first_die;
7108 CORE_ADDR lowpc, highpc;
7109
7110 lowpc = ((CORE_ADDR) -1);
7111 highpc = ((CORE_ADDR) 0);
7112
7113 first_die = load_partial_dies (reader, info_ptr, 1);
7114
7115 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7116 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7117
7118 /* If we didn't find a lowpc, set it to highpc to avoid
7119 complaints from `maint check'. */
7120 if (lowpc == ((CORE_ADDR) -1))
7121 lowpc = highpc;
7122
7123 /* If the compilation unit didn't have an explicit address range,
7124 then use the information extracted from its child dies. */
e385593e 7125 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7126 {
7127 best_lowpc = lowpc;
7128 best_highpc = highpc;
7129 }
7130 }
3e29f34a
MR
7131 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
7132 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 7133
8763cede 7134 end_psymtab_common (objfile, pst);
0018ea6f
DE
7135
7136 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
7137 {
7138 int i;
7139 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7140 struct dwarf2_per_cu_data *iter;
7141
7142 /* Fill in 'dependencies' here; we fill in 'users' in a
7143 post-pass. */
7144 pst->number_of_dependencies = len;
8d749320
SM
7145 pst->dependencies =
7146 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
7147 for (i = 0;
7148 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7149 i, iter);
7150 ++i)
7151 pst->dependencies[i] = iter->v.psymtab;
7152
7153 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7154 }
7155
7156 /* Get the list of files included in the current compilation unit,
7157 and build a psymtab for each of them. */
7158 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7159
b4f54984 7160 if (dwarf_read_debug)
0018ea6f
DE
7161 {
7162 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7163
7164 fprintf_unfiltered (gdb_stdlog,
7165 "Psymtab for %s unit @0x%x: %s - %s"
7166 ", %d global, %d static syms\n",
7167 per_cu->is_debug_types ? "type" : "comp",
9c541725 7168 to_underlying (per_cu->sect_off),
0018ea6f
DE
7169 paddress (gdbarch, pst->textlow),
7170 paddress (gdbarch, pst->texthigh),
7171 pst->n_global_syms, pst->n_static_syms);
7172 }
7173}
7174
7175/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7176 Process compilation unit THIS_CU for a psymtab. */
7177
7178static void
7179process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
7180 int want_partial_unit,
7181 enum language pretend_language)
0018ea6f
DE
7182{
7183 /* If this compilation unit was already read in, free the
7184 cached copy in order to read it in again. This is
7185 necessary because we skipped some symbols when we first
7186 read in the compilation unit (see load_partial_dies).
7187 This problem could be avoided, but the benefit is unclear. */
7188 if (this_cu->cu != NULL)
7189 free_one_cached_comp_unit (this_cu);
7190
f1902523
JK
7191 if (this_cu->is_debug_types)
7192 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
7193 NULL);
7194 else
7195 {
7196 process_psymtab_comp_unit_data info;
7197 info.want_partial_unit = want_partial_unit;
7198 info.pretend_language = pretend_language;
7199 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
7200 process_psymtab_comp_unit_reader, &info);
7201 }
0018ea6f
DE
7202
7203 /* Age out any secondary CUs. */
7204 age_cached_comp_units ();
7205}
f4dc4d17
DE
7206
7207/* Reader function for build_type_psymtabs. */
7208
7209static void
7210build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7211 const gdb_byte *info_ptr,
f4dc4d17
DE
7212 struct die_info *type_unit_die,
7213 int has_children,
7214 void *data)
7215{
7216 struct objfile *objfile = dwarf2_per_objfile->objfile;
7217 struct dwarf2_cu *cu = reader->cu;
7218 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7219 struct signatured_type *sig_type;
f4dc4d17
DE
7220 struct type_unit_group *tu_group;
7221 struct attribute *attr;
7222 struct partial_die_info *first_die;
7223 CORE_ADDR lowpc, highpc;
7224 struct partial_symtab *pst;
7225
7226 gdb_assert (data == NULL);
0186c6a7
DE
7227 gdb_assert (per_cu->is_debug_types);
7228 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
7229
7230 if (! has_children)
7231 return;
7232
7233 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 7234 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7235
0186c6a7 7236 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
7237
7238 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7239 cu->list_in_scope = &file_symbols;
7240 pst = create_partial_symtab (per_cu, "");
7241 pst->anonymous = 1;
7242
7243 first_die = load_partial_dies (reader, info_ptr, 1);
7244
7245 lowpc = (CORE_ADDR) -1;
7246 highpc = (CORE_ADDR) 0;
7247 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7248
8763cede 7249 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7250}
7251
73051182
DE
7252/* Struct used to sort TUs by their abbreviation table offset. */
7253
7254struct tu_abbrev_offset
7255{
7256 struct signatured_type *sig_type;
7257 sect_offset abbrev_offset;
7258};
7259
7260/* Helper routine for build_type_psymtabs_1, passed to qsort. */
7261
7262static int
7263sort_tu_by_abbrev_offset (const void *ap, const void *bp)
7264{
9a3c8263
SM
7265 const struct tu_abbrev_offset * const *a
7266 = (const struct tu_abbrev_offset * const*) ap;
7267 const struct tu_abbrev_offset * const *b
7268 = (const struct tu_abbrev_offset * const*) bp;
9c541725
PA
7269 sect_offset aoff = (*a)->abbrev_offset;
7270 sect_offset boff = (*b)->abbrev_offset;
73051182
DE
7271
7272 return (aoff > boff) - (aoff < boff);
7273}
7274
7275/* Efficiently read all the type units.
7276 This does the bulk of the work for build_type_psymtabs.
7277
7278 The efficiency is because we sort TUs by the abbrev table they use and
7279 only read each abbrev table once. In one program there are 200K TUs
7280 sharing 8K abbrev tables.
7281
7282 The main purpose of this function is to support building the
7283 dwarf2_per_objfile->type_unit_groups table.
7284 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7285 can collapse the search space by grouping them by stmt_list.
7286 The savings can be significant, in the same program from above the 200K TUs
7287 share 8K stmt_list tables.
7288
7289 FUNC is expected to call get_type_unit_group, which will create the
7290 struct type_unit_group if necessary and add it to
7291 dwarf2_per_objfile->type_unit_groups. */
7292
7293static void
7294build_type_psymtabs_1 (void)
7295{
73051182
DE
7296 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7297 struct cleanup *cleanups;
7298 struct abbrev_table *abbrev_table;
7299 sect_offset abbrev_offset;
7300 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
7301 int i;
7302
7303 /* It's up to the caller to not call us multiple times. */
7304 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7305
7306 if (dwarf2_per_objfile->n_type_units == 0)
7307 return;
7308
7309 /* TUs typically share abbrev tables, and there can be way more TUs than
7310 abbrev tables. Sort by abbrev table to reduce the number of times we
7311 read each abbrev table in.
7312 Alternatives are to punt or to maintain a cache of abbrev tables.
7313 This is simpler and efficient enough for now.
7314
7315 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7316 symtab to use). Typically TUs with the same abbrev offset have the same
7317 stmt_list value too so in practice this should work well.
7318
7319 The basic algorithm here is:
7320
7321 sort TUs by abbrev table
7322 for each TU with same abbrev table:
7323 read abbrev table if first user
7324 read TU top level DIE
7325 [IWBN if DWO skeletons had DW_AT_stmt_list]
7326 call FUNC */
7327
b4f54984 7328 if (dwarf_read_debug)
73051182
DE
7329 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7330
7331 /* Sort in a separate table to maintain the order of all_type_units
7332 for .gdb_index: TU indices directly index all_type_units. */
7333 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
7334 dwarf2_per_objfile->n_type_units);
7335 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
7336 {
7337 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
7338
7339 sorted_by_abbrev[i].sig_type = sig_type;
7340 sorted_by_abbrev[i].abbrev_offset =
7341 read_abbrev_offset (sig_type->per_cu.section,
9c541725 7342 sig_type->per_cu.sect_off);
73051182
DE
7343 }
7344 cleanups = make_cleanup (xfree, sorted_by_abbrev);
7345 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
7346 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
7347
9c541725 7348 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
7349 abbrev_table = NULL;
7350 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
7351
7352 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
7353 {
7354 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
7355
7356 /* Switch to the next abbrev table if necessary. */
7357 if (abbrev_table == NULL
9c541725 7358 || tu->abbrev_offset != abbrev_offset)
73051182
DE
7359 {
7360 if (abbrev_table != NULL)
7361 {
7362 abbrev_table_free (abbrev_table);
7363 /* Reset to NULL in case abbrev_table_read_table throws
7364 an error: abbrev_table_free_cleanup will get called. */
7365 abbrev_table = NULL;
7366 }
7367 abbrev_offset = tu->abbrev_offset;
7368 abbrev_table =
7369 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
7370 abbrev_offset);
7371 ++tu_stats->nr_uniq_abbrev_tables;
7372 }
7373
7374 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
7375 build_type_psymtabs_reader, NULL);
7376 }
7377
73051182 7378 do_cleanups (cleanups);
6aa5f3a6 7379}
73051182 7380
6aa5f3a6
DE
7381/* Print collected type unit statistics. */
7382
7383static void
7384print_tu_stats (void)
7385{
7386 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7387
7388 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7389 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
7390 dwarf2_per_objfile->n_type_units);
7391 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7392 tu_stats->nr_uniq_abbrev_tables);
7393 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7394 tu_stats->nr_symtabs);
7395 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7396 tu_stats->nr_symtab_sharers);
7397 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7398 tu_stats->nr_stmt_less_type_units);
7399 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7400 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7401}
7402
f4dc4d17
DE
7403/* Traversal function for build_type_psymtabs. */
7404
7405static int
7406build_type_psymtab_dependencies (void **slot, void *info)
7407{
7408 struct objfile *objfile = dwarf2_per_objfile->objfile;
7409 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7410 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 7411 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
7412 int len = VEC_length (sig_type_ptr, tu_group->tus);
7413 struct signatured_type *iter;
f4dc4d17
DE
7414 int i;
7415
7416 gdb_assert (len > 0);
0186c6a7 7417 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
7418
7419 pst->number_of_dependencies = len;
8d749320
SM
7420 pst->dependencies =
7421 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 7422 for (i = 0;
0186c6a7 7423 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
7424 ++i)
7425 {
0186c6a7
DE
7426 gdb_assert (iter->per_cu.is_debug_types);
7427 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7428 iter->type_unit_group = tu_group;
f4dc4d17
DE
7429 }
7430
0186c6a7 7431 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
7432
7433 return 1;
7434}
7435
7436/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7437 Build partial symbol tables for the .debug_types comp-units. */
7438
7439static void
7440build_type_psymtabs (struct objfile *objfile)
7441{
0e50663e 7442 if (! create_all_type_units (objfile))
348e048f
DE
7443 return;
7444
73051182 7445 build_type_psymtabs_1 ();
6aa5f3a6 7446}
f4dc4d17 7447
6aa5f3a6
DE
7448/* Traversal function for process_skeletonless_type_unit.
7449 Read a TU in a DWO file and build partial symbols for it. */
7450
7451static int
7452process_skeletonless_type_unit (void **slot, void *info)
7453{
7454 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9a3c8263 7455 struct objfile *objfile = (struct objfile *) info;
6aa5f3a6
DE
7456 struct signatured_type find_entry, *entry;
7457
7458 /* If this TU doesn't exist in the global table, add it and read it in. */
7459
7460 if (dwarf2_per_objfile->signatured_types == NULL)
7461 {
7462 dwarf2_per_objfile->signatured_types
7463 = allocate_signatured_type_table (objfile);
7464 }
7465
7466 find_entry.signature = dwo_unit->signature;
7467 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
7468 INSERT);
7469 /* If we've already seen this type there's nothing to do. What's happening
7470 is we're doing our own version of comdat-folding here. */
7471 if (*slot != NULL)
7472 return 1;
7473
7474 /* This does the job that create_all_type_units would have done for
7475 this TU. */
7476 entry = add_type_unit (dwo_unit->signature, slot);
7477 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
7478 *slot = entry;
7479
7480 /* This does the job that build_type_psymtabs_1 would have done. */
7481 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
7482 build_type_psymtabs_reader, NULL);
7483
7484 return 1;
7485}
7486
7487/* Traversal function for process_skeletonless_type_units. */
7488
7489static int
7490process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7491{
7492 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7493
7494 if (dwo_file->tus != NULL)
7495 {
7496 htab_traverse_noresize (dwo_file->tus,
7497 process_skeletonless_type_unit, info);
7498 }
7499
7500 return 1;
7501}
7502
7503/* Scan all TUs of DWO files, verifying we've processed them.
7504 This is needed in case a TU was emitted without its skeleton.
7505 Note: This can't be done until we know what all the DWO files are. */
7506
7507static void
7508process_skeletonless_type_units (struct objfile *objfile)
7509{
7510 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7511 if (get_dwp_file () == NULL
7512 && dwarf2_per_objfile->dwo_files != NULL)
7513 {
7514 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
7515 process_dwo_file_for_skeletonless_type_units,
7516 objfile);
7517 }
348e048f
DE
7518}
7519
95554aad
TT
7520/* Compute the 'user' field for each psymtab in OBJFILE. */
7521
7522static void
7523set_partial_user (struct objfile *objfile)
7524{
7525 int i;
7526
7527 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
7528 {
8832e7e3 7529 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
95554aad
TT
7530 struct partial_symtab *pst = per_cu->v.psymtab;
7531 int j;
7532
36586728
TT
7533 if (pst == NULL)
7534 continue;
7535
95554aad
TT
7536 for (j = 0; j < pst->number_of_dependencies; ++j)
7537 {
7538 /* Set the 'user' field only if it is not already set. */
7539 if (pst->dependencies[j]->user == NULL)
7540 pst->dependencies[j]->user = pst;
7541 }
7542 }
7543}
7544
93311388
DE
7545/* Build the partial symbol table by doing a quick pass through the
7546 .debug_info and .debug_abbrev sections. */
72bf9492 7547
93311388 7548static void
c67a9c90 7549dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 7550{
791afaa2 7551 struct cleanup *back_to;
21b2bd31 7552 int i;
93311388 7553
b4f54984 7554 if (dwarf_read_debug)
45cfd468
DE
7555 {
7556 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7557 objfile_name (objfile));
45cfd468
DE
7558 }
7559
98bfdba5
PA
7560 dwarf2_per_objfile->reading_partial_symbols = 1;
7561
be391dca 7562 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 7563
93311388
DE
7564 /* Any cached compilation units will be linked by the per-objfile
7565 read_in_chain. Make sure to free them when we're done. */
7566 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 7567
348e048f
DE
7568 build_type_psymtabs (objfile);
7569
93311388 7570 create_all_comp_units (objfile);
c906108c 7571
60606b2c
TT
7572 /* Create a temporary address map on a temporary obstack. We later
7573 copy this to the final obstack. */
8268c778 7574 auto_obstack temp_obstack;
791afaa2
TT
7575
7576 scoped_restore save_psymtabs_addrmap
7577 = make_scoped_restore (&objfile->psymtabs_addrmap,
7578 addrmap_create_mutable (&temp_obstack));
72bf9492 7579
21b2bd31 7580 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 7581 {
8832e7e3 7582 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
aaa75496 7583
b93601f3 7584 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 7585 }
ff013f42 7586
6aa5f3a6
DE
7587 /* This has to wait until we read the CUs, we need the list of DWOs. */
7588 process_skeletonless_type_units (objfile);
7589
7590 /* Now that all TUs have been processed we can fill in the dependencies. */
7591 if (dwarf2_per_objfile->type_unit_groups != NULL)
7592 {
7593 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
7594 build_type_psymtab_dependencies, NULL);
7595 }
7596
b4f54984 7597 if (dwarf_read_debug)
6aa5f3a6
DE
7598 print_tu_stats ();
7599
95554aad
TT
7600 set_partial_user (objfile);
7601
ff013f42
JK
7602 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
7603 &objfile->objfile_obstack);
791afaa2
TT
7604 /* At this point we want to keep the address map. */
7605 save_psymtabs_addrmap.release ();
ff013f42 7606
ae038cb0 7607 do_cleanups (back_to);
45cfd468 7608
b4f54984 7609 if (dwarf_read_debug)
45cfd468 7610 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7611 objfile_name (objfile));
ae038cb0
DJ
7612}
7613
3019eac3 7614/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
7615
7616static void
dee91e82 7617load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7618 const gdb_byte *info_ptr,
dee91e82
DE
7619 struct die_info *comp_unit_die,
7620 int has_children,
7621 void *data)
ae038cb0 7622{
dee91e82 7623 struct dwarf2_cu *cu = reader->cu;
ae038cb0 7624
95554aad 7625 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 7626
ae038cb0
DJ
7627 /* Check if comp unit has_children.
7628 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 7629 If not, there's no more debug_info for this comp unit. */
d85a05f0 7630 if (has_children)
dee91e82
DE
7631 load_partial_dies (reader, info_ptr, 0);
7632}
98bfdba5 7633
dee91e82
DE
7634/* Load the partial DIEs for a secondary CU into memory.
7635 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7636
dee91e82
DE
7637static void
7638load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7639{
f4dc4d17
DE
7640 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7641 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
7642}
7643
ae038cb0 7644static void
36586728
TT
7645read_comp_units_from_section (struct objfile *objfile,
7646 struct dwarf2_section_info *section,
f1902523 7647 struct dwarf2_section_info *abbrev_section,
36586728
TT
7648 unsigned int is_dwz,
7649 int *n_allocated,
7650 int *n_comp_units,
7651 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 7652{
d521ce57 7653 const gdb_byte *info_ptr;
a32a8923 7654 bfd *abfd = get_section_bfd_owner (section);
be391dca 7655
b4f54984 7656 if (dwarf_read_debug)
bf6af496 7657 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
7658 get_section_name (section),
7659 get_section_file_name (section));
bf6af496 7660
36586728 7661 dwarf2_read_section (objfile, section);
ae038cb0 7662
36586728 7663 info_ptr = section->buffer;
6e70227d 7664
36586728 7665 while (info_ptr < section->buffer + section->size)
ae038cb0 7666 {
ae038cb0 7667 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7668
9c541725 7669 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7670
f1902523
JK
7671 comp_unit_head cu_header;
7672 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
7673 info_ptr, rcuh_kind::COMPILE);
ae038cb0
DJ
7674
7675 /* Save the compilation unit for later lookup. */
f1902523
JK
7676 if (cu_header.unit_type != DW_UT_type)
7677 {
7678 this_cu = XOBNEW (&objfile->objfile_obstack,
7679 struct dwarf2_per_cu_data);
7680 memset (this_cu, 0, sizeof (*this_cu));
7681 }
7682 else
7683 {
7684 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7685 struct signatured_type);
7686 memset (sig_type, 0, sizeof (*sig_type));
7687 sig_type->signature = cu_header.signature;
7688 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7689 this_cu = &sig_type->per_cu;
7690 }
7691 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7692 this_cu->sect_off = sect_off;
f1902523 7693 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7694 this_cu->is_dwz = is_dwz;
9291a0cd 7695 this_cu->objfile = objfile;
8a0459fd 7696 this_cu->section = section;
ae038cb0 7697
36586728 7698 if (*n_comp_units == *n_allocated)
ae038cb0 7699 {
36586728 7700 *n_allocated *= 2;
224c3ddb
SM
7701 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
7702 *all_comp_units, *n_allocated);
ae038cb0 7703 }
36586728
TT
7704 (*all_comp_units)[*n_comp_units] = this_cu;
7705 ++*n_comp_units;
ae038cb0
DJ
7706
7707 info_ptr = info_ptr + this_cu->length;
7708 }
36586728
TT
7709}
7710
7711/* Create a list of all compilation units in OBJFILE.
7712 This is only done for -readnow and building partial symtabs. */
7713
7714static void
7715create_all_comp_units (struct objfile *objfile)
7716{
7717 int n_allocated;
7718 int n_comp_units;
7719 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 7720 struct dwz_file *dwz;
36586728
TT
7721
7722 n_comp_units = 0;
7723 n_allocated = 10;
8d749320 7724 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728 7725
f1902523
JK
7726 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
7727 &dwarf2_per_objfile->abbrev, 0,
36586728
TT
7728 &n_allocated, &n_comp_units, &all_comp_units);
7729
4db1a1dc
TT
7730 dwz = dwarf2_get_dwz_file ();
7731 if (dwz != NULL)
f1902523 7732 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
4db1a1dc
TT
7733 &n_allocated, &n_comp_units,
7734 &all_comp_units);
ae038cb0 7735
8d749320
SM
7736 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
7737 struct dwarf2_per_cu_data *,
7738 n_comp_units);
ae038cb0
DJ
7739 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
7740 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
7741 xfree (all_comp_units);
7742 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
7743}
7744
5734ee8b 7745/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7746 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7747 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7748 DW_AT_ranges). See the comments of add_partial_subprogram on how
7749 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7750
72bf9492
DJ
7751static void
7752scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7753 CORE_ADDR *highpc, int set_addrmap,
7754 struct dwarf2_cu *cu)
c906108c 7755{
72bf9492 7756 struct partial_die_info *pdi;
c906108c 7757
91c24f0a
DC
7758 /* Now, march along the PDI's, descending into ones which have
7759 interesting children but skipping the children of the other ones,
7760 until we reach the end of the compilation unit. */
c906108c 7761
72bf9492 7762 pdi = first_die;
91c24f0a 7763
72bf9492
DJ
7764 while (pdi != NULL)
7765 {
7766 fixup_partial_die (pdi, cu);
c906108c 7767
f55ee35c 7768 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7769 children, so we need to look at them. Ditto for anonymous
7770 enums. */
933c6fe4 7771
72bf9492 7772 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
7773 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7774 || pdi->tag == DW_TAG_imported_unit)
c906108c 7775 {
72bf9492 7776 switch (pdi->tag)
c906108c
SS
7777 {
7778 case DW_TAG_subprogram:
cdc07690 7779 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 7780 break;
72929c62 7781 case DW_TAG_constant:
c906108c
SS
7782 case DW_TAG_variable:
7783 case DW_TAG_typedef:
91c24f0a 7784 case DW_TAG_union_type:
72bf9492 7785 if (!pdi->is_declaration)
63d06c5c 7786 {
72bf9492 7787 add_partial_symbol (pdi, cu);
63d06c5c
DC
7788 }
7789 break;
c906108c 7790 case DW_TAG_class_type:
680b30c7 7791 case DW_TAG_interface_type:
c906108c 7792 case DW_TAG_structure_type:
72bf9492 7793 if (!pdi->is_declaration)
c906108c 7794 {
72bf9492 7795 add_partial_symbol (pdi, cu);
c906108c 7796 }
e98c9e7c
TT
7797 if (cu->language == language_rust && pdi->has_children)
7798 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7799 set_addrmap, cu);
c906108c 7800 break;
91c24f0a 7801 case DW_TAG_enumeration_type:
72bf9492
DJ
7802 if (!pdi->is_declaration)
7803 add_partial_enumeration (pdi, cu);
c906108c
SS
7804 break;
7805 case DW_TAG_base_type:
a02abb62 7806 case DW_TAG_subrange_type:
c906108c 7807 /* File scope base type definitions are added to the partial
c5aa993b 7808 symbol table. */
72bf9492 7809 add_partial_symbol (pdi, cu);
c906108c 7810 break;
d9fa45fe 7811 case DW_TAG_namespace:
cdc07690 7812 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7813 break;
5d7cb8df 7814 case DW_TAG_module:
cdc07690 7815 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7816 break;
95554aad
TT
7817 case DW_TAG_imported_unit:
7818 {
7819 struct dwarf2_per_cu_data *per_cu;
7820
f4dc4d17
DE
7821 /* For now we don't handle imported units in type units. */
7822 if (cu->per_cu->is_debug_types)
7823 {
7824 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7825 " supported in type units [in module %s]"),
4262abfb 7826 objfile_name (cu->objfile));
f4dc4d17
DE
7827 }
7828
9c541725 7829 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
36586728 7830 pdi->is_dwz,
95554aad
TT
7831 cu->objfile);
7832
7833 /* Go read the partial unit, if needed. */
7834 if (per_cu->v.psymtab == NULL)
b93601f3 7835 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 7836
f4dc4d17 7837 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 7838 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
7839 }
7840 break;
74921315
KS
7841 case DW_TAG_imported_declaration:
7842 add_partial_symbol (pdi, cu);
7843 break;
c906108c
SS
7844 default:
7845 break;
7846 }
7847 }
7848
72bf9492
DJ
7849 /* If the die has a sibling, skip to the sibling. */
7850
7851 pdi = pdi->die_sibling;
7852 }
7853}
7854
7855/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7856
72bf9492 7857 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7858 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7859 Enumerators are an exception; they use the scope of their parent
7860 enumeration type, i.e. the name of the enumeration type is not
7861 prepended to the enumerator.
91c24f0a 7862
72bf9492
DJ
7863 There are two complexities. One is DW_AT_specification; in this
7864 case "parent" means the parent of the target of the specification,
7865 instead of the direct parent of the DIE. The other is compilers
7866 which do not emit DW_TAG_namespace; in this case we try to guess
7867 the fully qualified name of structure types from their members'
7868 linkage names. This must be done using the DIE's children rather
7869 than the children of any DW_AT_specification target. We only need
7870 to do this for structures at the top level, i.e. if the target of
7871 any DW_AT_specification (if any; otherwise the DIE itself) does not
7872 have a parent. */
7873
7874/* Compute the scope prefix associated with PDI's parent, in
7875 compilation unit CU. The result will be allocated on CU's
7876 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7877 field. NULL is returned if no prefix is necessary. */
15d034d0 7878static const char *
72bf9492
DJ
7879partial_die_parent_scope (struct partial_die_info *pdi,
7880 struct dwarf2_cu *cu)
7881{
15d034d0 7882 const char *grandparent_scope;
72bf9492 7883 struct partial_die_info *parent, *real_pdi;
91c24f0a 7884
72bf9492
DJ
7885 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7886 then this means the parent of the specification DIE. */
7887
7888 real_pdi = pdi;
72bf9492 7889 while (real_pdi->has_specification)
36586728
TT
7890 real_pdi = find_partial_die (real_pdi->spec_offset,
7891 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
7892
7893 parent = real_pdi->die_parent;
7894 if (parent == NULL)
7895 return NULL;
7896
7897 if (parent->scope_set)
7898 return parent->scope;
7899
7900 fixup_partial_die (parent, cu);
7901
10b3939b 7902 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 7903
acebe513
UW
7904 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7905 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7906 Work around this problem here. */
7907 if (cu->language == language_cplus
6e70227d 7908 && parent->tag == DW_TAG_namespace
acebe513
UW
7909 && strcmp (parent->name, "::") == 0
7910 && grandparent_scope == NULL)
7911 {
7912 parent->scope = NULL;
7913 parent->scope_set = 1;
7914 return NULL;
7915 }
7916
9c6c53f7
SA
7917 if (pdi->tag == DW_TAG_enumerator)
7918 /* Enumerators should not get the name of the enumeration as a prefix. */
7919 parent->scope = grandparent_scope;
7920 else if (parent->tag == DW_TAG_namespace
f55ee35c 7921 || parent->tag == DW_TAG_module
72bf9492
DJ
7922 || parent->tag == DW_TAG_structure_type
7923 || parent->tag == DW_TAG_class_type
680b30c7 7924 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
7925 || parent->tag == DW_TAG_union_type
7926 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
7927 {
7928 if (grandparent_scope == NULL)
7929 parent->scope = parent->name;
7930 else
3e43a32a
MS
7931 parent->scope = typename_concat (&cu->comp_unit_obstack,
7932 grandparent_scope,
f55ee35c 7933 parent->name, 0, cu);
72bf9492 7934 }
72bf9492
DJ
7935 else
7936 {
7937 /* FIXME drow/2004-04-01: What should we be doing with
7938 function-local names? For partial symbols, we should probably be
7939 ignoring them. */
7940 complaint (&symfile_complaints,
e2e0b3e5 7941 _("unhandled containing DIE tag %d for DIE at %d"),
9c541725 7942 parent->tag, to_underlying (pdi->sect_off));
72bf9492 7943 parent->scope = grandparent_scope;
c906108c
SS
7944 }
7945
72bf9492
DJ
7946 parent->scope_set = 1;
7947 return parent->scope;
7948}
7949
7950/* Return the fully scoped name associated with PDI, from compilation unit
7951 CU. The result will be allocated with malloc. */
4568ecf9 7952
72bf9492
DJ
7953static char *
7954partial_die_full_name (struct partial_die_info *pdi,
7955 struct dwarf2_cu *cu)
7956{
15d034d0 7957 const char *parent_scope;
72bf9492 7958
98bfdba5
PA
7959 /* If this is a template instantiation, we can not work out the
7960 template arguments from partial DIEs. So, unfortunately, we have
7961 to go through the full DIEs. At least any work we do building
7962 types here will be reused if full symbols are loaded later. */
7963 if (pdi->has_template_arguments)
7964 {
7965 fixup_partial_die (pdi, cu);
7966
7967 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7968 {
7969 struct die_info *die;
7970 struct attribute attr;
7971 struct dwarf2_cu *ref_cu = cu;
7972
b64f50a1 7973 /* DW_FORM_ref_addr is using section offset. */
b4069958 7974 attr.name = (enum dwarf_attribute) 0;
98bfdba5 7975 attr.form = DW_FORM_ref_addr;
9c541725 7976 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
7977 die = follow_die_ref (NULL, &attr, &ref_cu);
7978
7979 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7980 }
7981 }
7982
72bf9492
DJ
7983 parent_scope = partial_die_parent_scope (pdi, cu);
7984 if (parent_scope == NULL)
7985 return NULL;
7986 else
f55ee35c 7987 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
7988}
7989
7990static void
72bf9492 7991add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 7992{
e7c27a73 7993 struct objfile *objfile = cu->objfile;
3e29f34a 7994 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 7995 CORE_ADDR addr = 0;
15d034d0 7996 const char *actual_name = NULL;
e142c38c 7997 CORE_ADDR baseaddr;
15d034d0 7998 char *built_actual_name;
e142c38c
DJ
7999
8000 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8001
15d034d0
TT
8002 built_actual_name = partial_die_full_name (pdi, cu);
8003 if (built_actual_name != NULL)
8004 actual_name = built_actual_name;
63d06c5c 8005
72bf9492
DJ
8006 if (actual_name == NULL)
8007 actual_name = pdi->name;
8008
c906108c
SS
8009 switch (pdi->tag)
8010 {
8011 case DW_TAG_subprogram:
3e29f34a 8012 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 8013 if (pdi->is_external || cu->language == language_ada)
c906108c 8014 {
2cfa0c8d
JB
8015 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8016 of the global scope. But in Ada, we want to be able to access
8017 nested procedures globally. So all Ada subprograms are stored
8018 in the global scope. */
f47fb265 8019 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8020 built_actual_name != NULL,
f47fb265
MS
8021 VAR_DOMAIN, LOC_BLOCK,
8022 &objfile->global_psymbols,
1762568f 8023 addr, cu->language, objfile);
c906108c
SS
8024 }
8025 else
8026 {
f47fb265 8027 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8028 built_actual_name != NULL,
f47fb265
MS
8029 VAR_DOMAIN, LOC_BLOCK,
8030 &objfile->static_psymbols,
1762568f 8031 addr, cu->language, objfile);
c906108c 8032 }
0c1b455e
TT
8033
8034 if (pdi->main_subprogram && actual_name != NULL)
8035 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8036 break;
72929c62
JB
8037 case DW_TAG_constant:
8038 {
af5bf4ad 8039 std::vector<partial_symbol *> *list;
72929c62
JB
8040
8041 if (pdi->is_external)
8042 list = &objfile->global_psymbols;
8043 else
8044 list = &objfile->static_psymbols;
f47fb265 8045 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8046 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 8047 list, 0, cu->language, objfile);
72929c62
JB
8048 }
8049 break;
c906108c 8050 case DW_TAG_variable:
95554aad
TT
8051 if (pdi->d.locdesc)
8052 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8053
95554aad 8054 if (pdi->d.locdesc
caac4577
JG
8055 && addr == 0
8056 && !dwarf2_per_objfile->has_section_at_zero)
8057 {
8058 /* A global or static variable may also have been stripped
8059 out by the linker if unused, in which case its address
8060 will be nullified; do not add such variables into partial
8061 symbol table then. */
8062 }
8063 else if (pdi->is_external)
c906108c
SS
8064 {
8065 /* Global Variable.
8066 Don't enter into the minimal symbol tables as there is
8067 a minimal symbol table entry from the ELF symbols already.
8068 Enter into partial symbol table if it has a location
8069 descriptor or a type.
8070 If the location descriptor is missing, new_symbol will create
8071 a LOC_UNRESOLVED symbol, the address of the variable will then
8072 be determined from the minimal symbol table whenever the variable
8073 is referenced.
8074 The address for the partial symbol table entry is not
8075 used by GDB, but it comes in handy for debugging partial symbol
8076 table building. */
8077
95554aad 8078 if (pdi->d.locdesc || pdi->has_type)
f47fb265 8079 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8080 built_actual_name != NULL,
f47fb265
MS
8081 VAR_DOMAIN, LOC_STATIC,
8082 &objfile->global_psymbols,
1762568f 8083 addr + baseaddr,
f47fb265 8084 cu->language, objfile);
c906108c
SS
8085 }
8086 else
8087 {
ff908ebf
AW
8088 int has_loc = pdi->d.locdesc != NULL;
8089
8090 /* Static Variable. Skip symbols whose value we cannot know (those
8091 without location descriptors or constant values). */
8092 if (!has_loc && !pdi->has_const_value)
decbce07 8093 {
15d034d0 8094 xfree (built_actual_name);
decbce07
MS
8095 return;
8096 }
ff908ebf 8097
f47fb265 8098 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8099 built_actual_name != NULL,
f47fb265
MS
8100 VAR_DOMAIN, LOC_STATIC,
8101 &objfile->static_psymbols,
ff908ebf 8102 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 8103 cu->language, objfile);
c906108c
SS
8104 }
8105 break;
8106 case DW_TAG_typedef:
8107 case DW_TAG_base_type:
a02abb62 8108 case DW_TAG_subrange_type:
38d518c9 8109 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8110 built_actual_name != NULL,
176620f1 8111 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 8112 &objfile->static_psymbols,
1762568f 8113 0, cu->language, objfile);
c906108c 8114 break;
74921315 8115 case DW_TAG_imported_declaration:
72bf9492
DJ
8116 case DW_TAG_namespace:
8117 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8118 built_actual_name != NULL,
72bf9492
DJ
8119 VAR_DOMAIN, LOC_TYPEDEF,
8120 &objfile->global_psymbols,
1762568f 8121 0, cu->language, objfile);
72bf9492 8122 break;
530e8392
KB
8123 case DW_TAG_module:
8124 add_psymbol_to_list (actual_name, strlen (actual_name),
8125 built_actual_name != NULL,
8126 MODULE_DOMAIN, LOC_TYPEDEF,
8127 &objfile->global_psymbols,
1762568f 8128 0, cu->language, objfile);
530e8392 8129 break;
c906108c 8130 case DW_TAG_class_type:
680b30c7 8131 case DW_TAG_interface_type:
c906108c
SS
8132 case DW_TAG_structure_type:
8133 case DW_TAG_union_type:
8134 case DW_TAG_enumeration_type:
fa4028e9
JB
8135 /* Skip external references. The DWARF standard says in the section
8136 about "Structure, Union, and Class Type Entries": "An incomplete
8137 structure, union or class type is represented by a structure,
8138 union or class entry that does not have a byte size attribute
8139 and that has a DW_AT_declaration attribute." */
8140 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 8141 {
15d034d0 8142 xfree (built_actual_name);
decbce07
MS
8143 return;
8144 }
fa4028e9 8145
63d06c5c
DC
8146 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8147 static vs. global. */
38d518c9 8148 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8149 built_actual_name != NULL,
176620f1 8150 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 8151 cu->language == language_cplus
63d06c5c
DC
8152 ? &objfile->global_psymbols
8153 : &objfile->static_psymbols,
1762568f 8154 0, cu->language, objfile);
c906108c 8155
c906108c
SS
8156 break;
8157 case DW_TAG_enumerator:
38d518c9 8158 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8159 built_actual_name != NULL,
176620f1 8160 VAR_DOMAIN, LOC_CONST,
9c37b5ae 8161 cu->language == language_cplus
f6fe98ef
DJ
8162 ? &objfile->global_psymbols
8163 : &objfile->static_psymbols,
1762568f 8164 0, cu->language, objfile);
c906108c
SS
8165 break;
8166 default:
8167 break;
8168 }
5c4e30ca 8169
15d034d0 8170 xfree (built_actual_name);
c906108c
SS
8171}
8172
5c4e30ca
DC
8173/* Read a partial die corresponding to a namespace; also, add a symbol
8174 corresponding to that namespace to the symbol table. NAMESPACE is
8175 the name of the enclosing namespace. */
91c24f0a 8176
72bf9492
DJ
8177static void
8178add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8179 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8180 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8181{
72bf9492 8182 /* Add a symbol for the namespace. */
e7c27a73 8183
72bf9492 8184 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8185
8186 /* Now scan partial symbols in that namespace. */
8187
91c24f0a 8188 if (pdi->has_children)
cdc07690 8189 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8190}
8191
5d7cb8df
JK
8192/* Read a partial die corresponding to a Fortran module. */
8193
8194static void
8195add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8196 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8197{
530e8392
KB
8198 /* Add a symbol for the namespace. */
8199
8200 add_partial_symbol (pdi, cu);
8201
f55ee35c 8202 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8203
8204 if (pdi->has_children)
cdc07690 8205 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8206}
8207
bc30ff58
JB
8208/* Read a partial die corresponding to a subprogram and create a partial
8209 symbol for that subprogram. When the CU language allows it, this
8210 routine also defines a partial symbol for each nested subprogram
cdc07690 8211 that this subprogram contains. If SET_ADDRMAP is true, record the
428fc5fc
YQ
8212 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
8213 and highest PC values found in PDI.
6e70227d 8214
cdc07690
YQ
8215 PDI may also be a lexical block, in which case we simply search
8216 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8217 Again, this is only performed when the CU language allows this
8218 type of definitions. */
8219
8220static void
8221add_partial_subprogram (struct partial_die_info *pdi,
8222 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8223 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58
JB
8224{
8225 if (pdi->tag == DW_TAG_subprogram)
8226 {
8227 if (pdi->has_pc_info)
8228 {
8229 if (pdi->lowpc < *lowpc)
8230 *lowpc = pdi->lowpc;
8231 if (pdi->highpc > *highpc)
8232 *highpc = pdi->highpc;
cdc07690 8233 if (set_addrmap)
5734ee8b 8234 {
5734ee8b 8235 struct objfile *objfile = cu->objfile;
3e29f34a
MR
8236 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8237 CORE_ADDR baseaddr;
8238 CORE_ADDR highpc;
8239 CORE_ADDR lowpc;
5734ee8b
DJ
8240
8241 baseaddr = ANOFFSET (objfile->section_offsets,
8242 SECT_OFF_TEXT (objfile));
3e29f34a
MR
8243 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
8244 pdi->lowpc + baseaddr);
8245 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
8246 pdi->highpc + baseaddr);
8247 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 8248 cu->per_cu->v.psymtab);
5734ee8b 8249 }
481860b3
GB
8250 }
8251
8252 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8253 {
bc30ff58 8254 if (!pdi->is_declaration)
e8d05480
JB
8255 /* Ignore subprogram DIEs that do not have a name, they are
8256 illegal. Do not emit a complaint at this point, we will
8257 do so when we convert this psymtab into a symtab. */
8258 if (pdi->name)
8259 add_partial_symbol (pdi, cu);
bc30ff58
JB
8260 }
8261 }
6e70227d 8262
bc30ff58
JB
8263 if (! pdi->has_children)
8264 return;
8265
8266 if (cu->language == language_ada)
8267 {
8268 pdi = pdi->die_child;
8269 while (pdi != NULL)
8270 {
8271 fixup_partial_die (pdi, cu);
8272 if (pdi->tag == DW_TAG_subprogram
8273 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8274 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8275 pdi = pdi->die_sibling;
8276 }
8277 }
8278}
8279
91c24f0a
DC
8280/* Read a partial die corresponding to an enumeration type. */
8281
72bf9492
DJ
8282static void
8283add_partial_enumeration (struct partial_die_info *enum_pdi,
8284 struct dwarf2_cu *cu)
91c24f0a 8285{
72bf9492 8286 struct partial_die_info *pdi;
91c24f0a
DC
8287
8288 if (enum_pdi->name != NULL)
72bf9492
DJ
8289 add_partial_symbol (enum_pdi, cu);
8290
8291 pdi = enum_pdi->die_child;
8292 while (pdi)
91c24f0a 8293 {
72bf9492 8294 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 8295 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 8296 else
72bf9492
DJ
8297 add_partial_symbol (pdi, cu);
8298 pdi = pdi->die_sibling;
91c24f0a 8299 }
91c24f0a
DC
8300}
8301
6caca83c
CC
8302/* Return the initial uleb128 in the die at INFO_PTR. */
8303
8304static unsigned int
d521ce57 8305peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8306{
8307 unsigned int bytes_read;
8308
8309 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8310}
8311
4bb7a0a7
DJ
8312/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
8313 Return the corresponding abbrev, or NULL if the number is zero (indicating
8314 an empty DIE). In either case *BYTES_READ will be set to the length of
8315 the initial number. */
8316
8317static struct abbrev_info *
d521ce57 8318peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 8319 struct dwarf2_cu *cu)
4bb7a0a7
DJ
8320{
8321 bfd *abfd = cu->objfile->obfd;
8322 unsigned int abbrev_number;
8323 struct abbrev_info *abbrev;
8324
8325 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8326
8327 if (abbrev_number == 0)
8328 return NULL;
8329
433df2d4 8330 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
8331 if (!abbrev)
8332 {
422b9917
DE
8333 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8334 " at offset 0x%x [in module %s]"),
8335 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9c541725 8336 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8337 }
8338
8339 return abbrev;
8340}
8341
93311388
DE
8342/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8343 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8344 DIE. Any children of the skipped DIEs will also be skipped. */
8345
d521ce57
TT
8346static const gdb_byte *
8347skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8348{
dee91e82 8349 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
8350 struct abbrev_info *abbrev;
8351 unsigned int bytes_read;
8352
8353 while (1)
8354 {
8355 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8356 if (abbrev == NULL)
8357 return info_ptr + bytes_read;
8358 else
dee91e82 8359 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8360 }
8361}
8362
93311388
DE
8363/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8364 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8365 abbrev corresponding to that skipped uleb128 should be passed in
8366 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8367 children. */
8368
d521ce57
TT
8369static const gdb_byte *
8370skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8371 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8372{
8373 unsigned int bytes_read;
8374 struct attribute attr;
dee91e82
DE
8375 bfd *abfd = reader->abfd;
8376 struct dwarf2_cu *cu = reader->cu;
d521ce57 8377 const gdb_byte *buffer = reader->buffer;
f664829e 8378 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8379 unsigned int form, i;
8380
8381 for (i = 0; i < abbrev->num_attrs; i++)
8382 {
8383 /* The only abbrev we care about is DW_AT_sibling. */
8384 if (abbrev->attrs[i].name == DW_AT_sibling)
8385 {
dee91e82 8386 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8387 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
8388 complaint (&symfile_complaints,
8389 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8390 else
b9502d3f 8391 {
9c541725
PA
8392 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8393 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8394
8395 if (sibling_ptr < info_ptr)
8396 complaint (&symfile_complaints,
8397 _("DW_AT_sibling points backwards"));
22869d73
KS
8398 else if (sibling_ptr > reader->buffer_end)
8399 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
8400 else
8401 return sibling_ptr;
8402 }
4bb7a0a7
DJ
8403 }
8404
8405 /* If it isn't DW_AT_sibling, skip this attribute. */
8406 form = abbrev->attrs[i].form;
8407 skip_attribute:
8408 switch (form)
8409 {
4bb7a0a7 8410 case DW_FORM_ref_addr:
ae411497
TT
8411 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8412 and later it is offset sized. */
8413 if (cu->header.version == 2)
8414 info_ptr += cu->header.addr_size;
8415 else
8416 info_ptr += cu->header.offset_size;
8417 break;
36586728
TT
8418 case DW_FORM_GNU_ref_alt:
8419 info_ptr += cu->header.offset_size;
8420 break;
ae411497 8421 case DW_FORM_addr:
4bb7a0a7
DJ
8422 info_ptr += cu->header.addr_size;
8423 break;
8424 case DW_FORM_data1:
8425 case DW_FORM_ref1:
8426 case DW_FORM_flag:
8427 info_ptr += 1;
8428 break;
2dc7f7b3 8429 case DW_FORM_flag_present:
43988095 8430 case DW_FORM_implicit_const:
2dc7f7b3 8431 break;
4bb7a0a7
DJ
8432 case DW_FORM_data2:
8433 case DW_FORM_ref2:
8434 info_ptr += 2;
8435 break;
8436 case DW_FORM_data4:
8437 case DW_FORM_ref4:
8438 info_ptr += 4;
8439 break;
8440 case DW_FORM_data8:
8441 case DW_FORM_ref8:
55f1336d 8442 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8443 info_ptr += 8;
8444 break;
0224619f
JK
8445 case DW_FORM_data16:
8446 info_ptr += 16;
8447 break;
4bb7a0a7 8448 case DW_FORM_string:
9b1c24c8 8449 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8450 info_ptr += bytes_read;
8451 break;
2dc7f7b3 8452 case DW_FORM_sec_offset:
4bb7a0a7 8453 case DW_FORM_strp:
36586728 8454 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8455 info_ptr += cu->header.offset_size;
8456 break;
2dc7f7b3 8457 case DW_FORM_exprloc:
4bb7a0a7
DJ
8458 case DW_FORM_block:
8459 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8460 info_ptr += bytes_read;
8461 break;
8462 case DW_FORM_block1:
8463 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8464 break;
8465 case DW_FORM_block2:
8466 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8467 break;
8468 case DW_FORM_block4:
8469 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8470 break;
8471 case DW_FORM_sdata:
8472 case DW_FORM_udata:
8473 case DW_FORM_ref_udata:
3019eac3
DE
8474 case DW_FORM_GNU_addr_index:
8475 case DW_FORM_GNU_str_index:
d521ce57 8476 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8477 break;
8478 case DW_FORM_indirect:
8479 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8480 info_ptr += bytes_read;
8481 /* We need to continue parsing from here, so just go back to
8482 the top. */
8483 goto skip_attribute;
8484
8485 default:
3e43a32a
MS
8486 error (_("Dwarf Error: Cannot handle %s "
8487 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8488 dwarf_form_name (form),
8489 bfd_get_filename (abfd));
8490 }
8491 }
8492
8493 if (abbrev->has_children)
dee91e82 8494 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8495 else
8496 return info_ptr;
8497}
8498
93311388 8499/* Locate ORIG_PDI's sibling.
dee91e82 8500 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8501
d521ce57 8502static const gdb_byte *
dee91e82
DE
8503locate_pdi_sibling (const struct die_reader_specs *reader,
8504 struct partial_die_info *orig_pdi,
d521ce57 8505 const gdb_byte *info_ptr)
91c24f0a
DC
8506{
8507 /* Do we know the sibling already? */
72bf9492 8508
91c24f0a
DC
8509 if (orig_pdi->sibling)
8510 return orig_pdi->sibling;
8511
8512 /* Are there any children to deal with? */
8513
8514 if (!orig_pdi->has_children)
8515 return info_ptr;
8516
4bb7a0a7 8517 /* Skip the children the long way. */
91c24f0a 8518
dee91e82 8519 return skip_children (reader, info_ptr);
91c24f0a
DC
8520}
8521
257e7a09 8522/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8523 not NULL. */
c906108c
SS
8524
8525static void
257e7a09
YQ
8526dwarf2_read_symtab (struct partial_symtab *self,
8527 struct objfile *objfile)
c906108c 8528{
257e7a09 8529 if (self->readin)
c906108c 8530 {
442e4d9c 8531 warning (_("bug: psymtab for %s is already read in."),
257e7a09 8532 self->filename);
442e4d9c
YQ
8533 }
8534 else
8535 {
8536 if (info_verbose)
c906108c 8537 {
442e4d9c 8538 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 8539 self->filename);
442e4d9c 8540 gdb_flush (gdb_stdout);
c906108c 8541 }
c906108c 8542
442e4d9c 8543 /* Restore our global data. */
9a3c8263
SM
8544 dwarf2_per_objfile
8545 = (struct dwarf2_per_objfile *) objfile_data (objfile,
8546 dwarf2_objfile_data_key);
10b3939b 8547
442e4d9c
YQ
8548 /* If this psymtab is constructed from a debug-only objfile, the
8549 has_section_at_zero flag will not necessarily be correct. We
8550 can get the correct value for this flag by looking at the data
8551 associated with the (presumably stripped) associated objfile. */
8552 if (objfile->separate_debug_objfile_backlink)
8553 {
8554 struct dwarf2_per_objfile *dpo_backlink
9a3c8263
SM
8555 = ((struct dwarf2_per_objfile *)
8556 objfile_data (objfile->separate_debug_objfile_backlink,
8557 dwarf2_objfile_data_key));
9a619af0 8558
442e4d9c
YQ
8559 dwarf2_per_objfile->has_section_at_zero
8560 = dpo_backlink->has_section_at_zero;
8561 }
b2ab525c 8562
442e4d9c 8563 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 8564
257e7a09 8565 psymtab_to_symtab_1 (self);
c906108c 8566
442e4d9c
YQ
8567 /* Finish up the debug error message. */
8568 if (info_verbose)
8569 printf_filtered (_("done.\n"));
c906108c 8570 }
95554aad
TT
8571
8572 process_cu_includes ();
c906108c 8573}
9cdd5dbd
DE
8574\f
8575/* Reading in full CUs. */
c906108c 8576
10b3939b
DJ
8577/* Add PER_CU to the queue. */
8578
8579static void
95554aad
TT
8580queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8581 enum language pretend_language)
10b3939b
DJ
8582{
8583 struct dwarf2_queue_item *item;
8584
8585 per_cu->queued = 1;
8d749320 8586 item = XNEW (struct dwarf2_queue_item);
10b3939b 8587 item->per_cu = per_cu;
95554aad 8588 item->pretend_language = pretend_language;
10b3939b
DJ
8589 item->next = NULL;
8590
8591 if (dwarf2_queue == NULL)
8592 dwarf2_queue = item;
8593 else
8594 dwarf2_queue_tail->next = item;
8595
8596 dwarf2_queue_tail = item;
8597}
8598
89e63ee4
DE
8599/* If PER_CU is not yet queued, add it to the queue.
8600 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8601 dependency.
0907af0c 8602 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8603 meaning either PER_CU is already queued or it is already loaded.
8604
8605 N.B. There is an invariant here that if a CU is queued then it is loaded.
8606 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8607
8608static int
89e63ee4 8609maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8610 struct dwarf2_per_cu_data *per_cu,
8611 enum language pretend_language)
8612{
8613 /* We may arrive here during partial symbol reading, if we need full
8614 DIEs to process an unusual case (e.g. template arguments). Do
8615 not queue PER_CU, just tell our caller to load its DIEs. */
8616 if (dwarf2_per_objfile->reading_partial_symbols)
8617 {
8618 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8619 return 1;
8620 return 0;
8621 }
8622
8623 /* Mark the dependence relation so that we don't flush PER_CU
8624 too early. */
89e63ee4
DE
8625 if (dependent_cu != NULL)
8626 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8627
8628 /* If it's already on the queue, we have nothing to do. */
8629 if (per_cu->queued)
8630 return 0;
8631
8632 /* If the compilation unit is already loaded, just mark it as
8633 used. */
8634 if (per_cu->cu != NULL)
8635 {
8636 per_cu->cu->last_used = 0;
8637 return 0;
8638 }
8639
8640 /* Add it to the queue. */
8641 queue_comp_unit (per_cu, pretend_language);
8642
8643 return 1;
8644}
8645
10b3939b
DJ
8646/* Process the queue. */
8647
8648static void
a0f42c21 8649process_queue (void)
10b3939b
DJ
8650{
8651 struct dwarf2_queue_item *item, *next_item;
8652
b4f54984 8653 if (dwarf_read_debug)
45cfd468
DE
8654 {
8655 fprintf_unfiltered (gdb_stdlog,
8656 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8657 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8658 }
8659
03dd20cc
DJ
8660 /* The queue starts out with one item, but following a DIE reference
8661 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
8662 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
8663 {
cc12ce38
DE
8664 if ((dwarf2_per_objfile->using_index
8665 ? !item->per_cu->v.quick->compunit_symtab
8666 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
8667 /* Skip dummy CUs. */
8668 && item->per_cu->cu != NULL)
f4dc4d17
DE
8669 {
8670 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 8671 unsigned int debug_print_threshold;
247f5c4f 8672 char buf[100];
f4dc4d17 8673
247f5c4f 8674 if (per_cu->is_debug_types)
f4dc4d17 8675 {
247f5c4f
DE
8676 struct signatured_type *sig_type =
8677 (struct signatured_type *) per_cu;
8678
8679 sprintf (buf, "TU %s at offset 0x%x",
73be47f5 8680 hex_string (sig_type->signature),
9c541725 8681 to_underlying (per_cu->sect_off));
73be47f5
DE
8682 /* There can be 100s of TUs.
8683 Only print them in verbose mode. */
8684 debug_print_threshold = 2;
f4dc4d17 8685 }
247f5c4f 8686 else
73be47f5 8687 {
9c541725
PA
8688 sprintf (buf, "CU at offset 0x%x",
8689 to_underlying (per_cu->sect_off));
73be47f5
DE
8690 debug_print_threshold = 1;
8691 }
247f5c4f 8692
b4f54984 8693 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8694 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
8695
8696 if (per_cu->is_debug_types)
8697 process_full_type_unit (per_cu, item->pretend_language);
8698 else
8699 process_full_comp_unit (per_cu, item->pretend_language);
8700
b4f54984 8701 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8702 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 8703 }
10b3939b
DJ
8704
8705 item->per_cu->queued = 0;
8706 next_item = item->next;
8707 xfree (item);
8708 }
8709
8710 dwarf2_queue_tail = NULL;
45cfd468 8711
b4f54984 8712 if (dwarf_read_debug)
45cfd468
DE
8713 {
8714 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 8715 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 8716 }
10b3939b
DJ
8717}
8718
8719/* Free all allocated queue entries. This function only releases anything if
8720 an error was thrown; if the queue was processed then it would have been
8721 freed as we went along. */
8722
8723static void
8724dwarf2_release_queue (void *dummy)
8725{
8726 struct dwarf2_queue_item *item, *last;
8727
8728 item = dwarf2_queue;
8729 while (item)
8730 {
8731 /* Anything still marked queued is likely to be in an
8732 inconsistent state, so discard it. */
8733 if (item->per_cu->queued)
8734 {
8735 if (item->per_cu->cu != NULL)
dee91e82 8736 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
8737 item->per_cu->queued = 0;
8738 }
8739
8740 last = item;
8741 item = item->next;
8742 xfree (last);
8743 }
8744
8745 dwarf2_queue = dwarf2_queue_tail = NULL;
8746}
8747
8748/* Read in full symbols for PST, and anything it depends on. */
8749
c906108c 8750static void
fba45db2 8751psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 8752{
10b3939b 8753 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
8754 int i;
8755
95554aad
TT
8756 if (pst->readin)
8757 return;
8758
aaa75496 8759 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
8760 if (!pst->dependencies[i]->readin
8761 && pst->dependencies[i]->user == NULL)
aaa75496
JB
8762 {
8763 /* Inform about additional files that need to be read in. */
8764 if (info_verbose)
8765 {
a3f17187 8766 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
8767 fputs_filtered (" ", gdb_stdout);
8768 wrap_here ("");
8769 fputs_filtered ("and ", gdb_stdout);
8770 wrap_here ("");
8771 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 8772 wrap_here (""); /* Flush output. */
aaa75496
JB
8773 gdb_flush (gdb_stdout);
8774 }
8775 psymtab_to_symtab_1 (pst->dependencies[i]);
8776 }
8777
9a3c8263 8778 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
8779
8780 if (per_cu == NULL)
aaa75496
JB
8781 {
8782 /* It's an include file, no symbols to read for it.
8783 Everything is in the parent symtab. */
8784 pst->readin = 1;
8785 return;
8786 }
c906108c 8787
a0f42c21 8788 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
8789}
8790
dee91e82
DE
8791/* Trivial hash function for die_info: the hash value of a DIE
8792 is its offset in .debug_info for this objfile. */
10b3939b 8793
dee91e82
DE
8794static hashval_t
8795die_hash (const void *item)
10b3939b 8796{
9a3c8263 8797 const struct die_info *die = (const struct die_info *) item;
6502dd73 8798
9c541725 8799 return to_underlying (die->sect_off);
dee91e82 8800}
63d06c5c 8801
dee91e82
DE
8802/* Trivial comparison function for die_info structures: two DIEs
8803 are equal if they have the same offset. */
98bfdba5 8804
dee91e82
DE
8805static int
8806die_eq (const void *item_lhs, const void *item_rhs)
8807{
9a3c8263
SM
8808 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8809 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8810
9c541725 8811 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8812}
c906108c 8813
dee91e82
DE
8814/* die_reader_func for load_full_comp_unit.
8815 This is identical to read_signatured_type_reader,
8816 but is kept separate for now. */
c906108c 8817
dee91e82
DE
8818static void
8819load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8820 const gdb_byte *info_ptr,
dee91e82
DE
8821 struct die_info *comp_unit_die,
8822 int has_children,
8823 void *data)
8824{
8825 struct dwarf2_cu *cu = reader->cu;
9a3c8263 8826 enum language *language_ptr = (enum language *) data;
6caca83c 8827
dee91e82
DE
8828 gdb_assert (cu->die_hash == NULL);
8829 cu->die_hash =
8830 htab_create_alloc_ex (cu->header.length / 12,
8831 die_hash,
8832 die_eq,
8833 NULL,
8834 &cu->comp_unit_obstack,
8835 hashtab_obstack_allocate,
8836 dummy_obstack_deallocate);
e142c38c 8837
dee91e82
DE
8838 if (has_children)
8839 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
8840 &info_ptr, comp_unit_die);
8841 cu->dies = comp_unit_die;
8842 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8843
8844 /* We try not to read any attributes in this function, because not
9cdd5dbd 8845 all CUs needed for references have been loaded yet, and symbol
10b3939b 8846 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8847 or we won't be able to build types correctly.
8848 Similarly, if we do not read the producer, we can not apply
8849 producer-specific interpretation. */
95554aad 8850 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 8851}
10b3939b 8852
dee91e82 8853/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 8854
dee91e82 8855static void
95554aad
TT
8856load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8857 enum language pretend_language)
dee91e82 8858{
3019eac3 8859 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 8860
f4dc4d17
DE
8861 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8862 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
8863}
8864
3da10d80
KS
8865/* Add a DIE to the delayed physname list. */
8866
8867static void
8868add_to_method_list (struct type *type, int fnfield_index, int index,
8869 const char *name, struct die_info *die,
8870 struct dwarf2_cu *cu)
8871{
8872 struct delayed_method_info mi;
8873 mi.type = type;
8874 mi.fnfield_index = fnfield_index;
8875 mi.index = index;
8876 mi.name = name;
8877 mi.die = die;
8878 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
8879}
8880
8881/* A cleanup for freeing the delayed method list. */
8882
8883static void
8884free_delayed_list (void *ptr)
8885{
8886 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
8887 if (cu->method_list != NULL)
8888 {
8889 VEC_free (delayed_method_info, cu->method_list);
8890 cu->method_list = NULL;
8891 }
8892}
8893
3693fdb3
PA
8894/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8895 "const" / "volatile". If so, decrements LEN by the length of the
8896 modifier and return true. Otherwise return false. */
8897
8898template<size_t N>
8899static bool
8900check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8901{
8902 size_t mod_len = sizeof (mod) - 1;
8903 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8904 {
8905 len -= mod_len;
8906 return true;
8907 }
8908 return false;
8909}
8910
3da10d80
KS
8911/* Compute the physnames of any methods on the CU's method list.
8912
8913 The computation of method physnames is delayed in order to avoid the
8914 (bad) condition that one of the method's formal parameters is of an as yet
8915 incomplete type. */
8916
8917static void
8918compute_delayed_physnames (struct dwarf2_cu *cu)
8919{
8920 int i;
8921 struct delayed_method_info *mi;
3693fdb3
PA
8922
8923 /* Only C++ delays computing physnames. */
8924 if (VEC_empty (delayed_method_info, cu->method_list))
8925 return;
8926 gdb_assert (cu->language == language_cplus);
8927
3da10d80
KS
8928 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
8929 {
1d06ead6 8930 const char *physname;
3da10d80
KS
8931 struct fn_fieldlist *fn_flp
8932 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 8933 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
8934 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
8935 = physname ? physname : "";
3693fdb3
PA
8936
8937 /* Since there's no tag to indicate whether a method is a
8938 const/volatile overload, extract that information out of the
8939 demangled name. */
8940 if (physname != NULL)
8941 {
8942 size_t len = strlen (physname);
8943
8944 while (1)
8945 {
8946 if (physname[len] == ')') /* shortcut */
8947 break;
8948 else if (check_modifier (physname, len, " const"))
8949 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
8950 else if (check_modifier (physname, len, " volatile"))
8951 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
8952 else
8953 break;
8954 }
8955 }
3da10d80
KS
8956 }
8957}
8958
a766d390
DE
8959/* Go objects should be embedded in a DW_TAG_module DIE,
8960 and it's not clear if/how imported objects will appear.
8961 To keep Go support simple until that's worked out,
8962 go back through what we've read and create something usable.
8963 We could do this while processing each DIE, and feels kinda cleaner,
8964 but that way is more invasive.
8965 This is to, for example, allow the user to type "p var" or "b main"
8966 without having to specify the package name, and allow lookups
8967 of module.object to work in contexts that use the expression
8968 parser. */
8969
8970static void
8971fixup_go_packaging (struct dwarf2_cu *cu)
8972{
8973 char *package_name = NULL;
8974 struct pending *list;
8975 int i;
8976
8977 for (list = global_symbols; list != NULL; list = list->next)
8978 {
8979 for (i = 0; i < list->nsyms; ++i)
8980 {
8981 struct symbol *sym = list->symbol[i];
8982
8983 if (SYMBOL_LANGUAGE (sym) == language_go
8984 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8985 {
8986 char *this_package_name = go_symbol_package_name (sym);
8987
8988 if (this_package_name == NULL)
8989 continue;
8990 if (package_name == NULL)
8991 package_name = this_package_name;
8992 else
8993 {
8994 if (strcmp (package_name, this_package_name) != 0)
8995 complaint (&symfile_complaints,
8996 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
8997 (symbol_symtab (sym) != NULL
8998 ? symtab_to_filename_for_display
8999 (symbol_symtab (sym))
4262abfb 9000 : objfile_name (cu->objfile)),
a766d390
DE
9001 this_package_name, package_name);
9002 xfree (this_package_name);
9003 }
9004 }
9005 }
9006 }
9007
9008 if (package_name != NULL)
9009 {
9010 struct objfile *objfile = cu->objfile;
34a68019 9011 const char *saved_package_name
224c3ddb
SM
9012 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9013 package_name,
9014 strlen (package_name));
19f392bc
UW
9015 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9016 saved_package_name);
a766d390
DE
9017 struct symbol *sym;
9018
9019 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9020
e623cf5d 9021 sym = allocate_symbol (objfile);
f85f34ed 9022 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
9023 SYMBOL_SET_NAMES (sym, saved_package_name,
9024 strlen (saved_package_name), 0, objfile);
a766d390
DE
9025 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9026 e.g., "main" finds the "main" module and not C's main(). */
9027 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9028 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9029 SYMBOL_TYPE (sym) = type;
9030
9031 add_symbol_to_list (sym, &global_symbols);
9032
9033 xfree (package_name);
9034 }
9035}
9036
95554aad
TT
9037/* Return the symtab for PER_CU. This works properly regardless of
9038 whether we're using the index or psymtabs. */
9039
43f3e411
DE
9040static struct compunit_symtab *
9041get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad
TT
9042{
9043 return (dwarf2_per_objfile->using_index
43f3e411
DE
9044 ? per_cu->v.quick->compunit_symtab
9045 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
9046}
9047
9048/* A helper function for computing the list of all symbol tables
9049 included by PER_CU. */
9050
9051static void
43f3e411 9052recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 9053 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9054 struct dwarf2_per_cu_data *per_cu,
43f3e411 9055 struct compunit_symtab *immediate_parent)
95554aad
TT
9056{
9057 void **slot;
9058 int ix;
43f3e411 9059 struct compunit_symtab *cust;
95554aad
TT
9060 struct dwarf2_per_cu_data *iter;
9061
9062 slot = htab_find_slot (all_children, per_cu, INSERT);
9063 if (*slot != NULL)
9064 {
9065 /* This inclusion and its children have been processed. */
9066 return;
9067 }
9068
9069 *slot = per_cu;
9070 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9071 cust = get_compunit_symtab (per_cu);
9072 if (cust != NULL)
ec94af83
DE
9073 {
9074 /* If this is a type unit only add its symbol table if we haven't
9075 seen it yet (type unit per_cu's can share symtabs). */
9076 if (per_cu->is_debug_types)
9077 {
43f3e411 9078 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9079 if (*slot == NULL)
9080 {
43f3e411
DE
9081 *slot = cust;
9082 VEC_safe_push (compunit_symtab_ptr, *result, cust);
9083 if (cust->user == NULL)
9084 cust->user = immediate_parent;
ec94af83
DE
9085 }
9086 }
9087 else
f9125b6c 9088 {
43f3e411
DE
9089 VEC_safe_push (compunit_symtab_ptr, *result, cust);
9090 if (cust->user == NULL)
9091 cust->user = immediate_parent;
f9125b6c 9092 }
ec94af83 9093 }
95554aad
TT
9094
9095 for (ix = 0;
796a7ff8 9096 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 9097 ++ix)
ec94af83
DE
9098 {
9099 recursively_compute_inclusions (result, all_children,
43f3e411 9100 all_type_symtabs, iter, cust);
ec94af83 9101 }
95554aad
TT
9102}
9103
43f3e411 9104/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9105 PER_CU. */
9106
9107static void
43f3e411 9108compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9109{
f4dc4d17
DE
9110 gdb_assert (! per_cu->is_debug_types);
9111
796a7ff8 9112 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
9113 {
9114 int ix, len;
ec94af83 9115 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
9116 struct compunit_symtab *compunit_symtab_iter;
9117 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 9118 htab_t all_children, all_type_symtabs;
43f3e411 9119 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9120
9121 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9122 if (cust == NULL)
95554aad
TT
9123 return;
9124
9125 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9126 NULL, xcalloc, xfree);
ec94af83
DE
9127 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9128 NULL, xcalloc, xfree);
95554aad
TT
9129
9130 for (ix = 0;
796a7ff8 9131 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 9132 ix, per_cu_iter);
95554aad 9133 ++ix)
ec94af83
DE
9134 {
9135 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 9136 all_type_symtabs, per_cu_iter,
43f3e411 9137 cust);
ec94af83 9138 }
95554aad 9139
ec94af83 9140 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
9141 len = VEC_length (compunit_symtab_ptr, result_symtabs);
9142 cust->includes
8d749320
SM
9143 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
9144 struct compunit_symtab *, len + 1);
95554aad 9145 for (ix = 0;
43f3e411
DE
9146 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
9147 compunit_symtab_iter);
95554aad 9148 ++ix)
43f3e411
DE
9149 cust->includes[ix] = compunit_symtab_iter;
9150 cust->includes[len] = NULL;
95554aad 9151
43f3e411 9152 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 9153 htab_delete (all_children);
ec94af83 9154 htab_delete (all_type_symtabs);
95554aad
TT
9155 }
9156}
9157
9158/* Compute the 'includes' field for the symtabs of all the CUs we just
9159 read. */
9160
9161static void
9162process_cu_includes (void)
9163{
9164 int ix;
9165 struct dwarf2_per_cu_data *iter;
9166
9167 for (ix = 0;
9168 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
9169 ix, iter);
9170 ++ix)
f4dc4d17
DE
9171 {
9172 if (! iter->is_debug_types)
43f3e411 9173 compute_compunit_symtab_includes (iter);
f4dc4d17 9174 }
95554aad
TT
9175
9176 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
9177}
9178
9cdd5dbd 9179/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9180 already been loaded into memory. */
9181
9182static void
95554aad
TT
9183process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9184 enum language pretend_language)
10b3939b 9185{
10b3939b 9186 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 9187 struct objfile *objfile = per_cu->objfile;
3e29f34a 9188 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 9189 CORE_ADDR lowpc, highpc;
43f3e411 9190 struct compunit_symtab *cust;
33c7c59d 9191 struct cleanup *delayed_list_cleanup;
10b3939b 9192 CORE_ADDR baseaddr;
4359dff1 9193 struct block *static_block;
3e29f34a 9194 CORE_ADDR addr;
10b3939b
DJ
9195
9196 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9197
10b3939b 9198 buildsym_init ();
33c7c59d 9199 scoped_free_pendings free_pending;
3da10d80 9200 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
9201
9202 cu->list_in_scope = &file_symbols;
c906108c 9203
95554aad
TT
9204 cu->language = pretend_language;
9205 cu->language_defn = language_def (cu->language);
9206
c906108c 9207 /* Do line number decoding in read_file_scope () */
10b3939b 9208 process_die (cu->dies, cu);
c906108c 9209
a766d390
DE
9210 /* For now fudge the Go package. */
9211 if (cu->language == language_go)
9212 fixup_go_packaging (cu);
9213
3da10d80
KS
9214 /* Now that we have processed all the DIEs in the CU, all the types
9215 should be complete, and it should now be safe to compute all of the
9216 physnames. */
9217 compute_delayed_physnames (cu);
9218 do_cleanups (delayed_list_cleanup);
9219
fae299cd
DC
9220 /* Some compilers don't define a DW_AT_high_pc attribute for the
9221 compilation unit. If the DW_AT_high_pc is missing, synthesize
9222 it, by scanning the DIE's below the compilation unit. */
10b3939b 9223 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9224
3e29f34a
MR
9225 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9226 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9227
9228 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9229 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9230 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9231 addrmap to help ensure it has an accurate map of pc values belonging to
9232 this comp unit. */
9233 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9234
43f3e411
DE
9235 cust = end_symtab_from_static_block (static_block,
9236 SECT_OFF_TEXT (objfile), 0);
c906108c 9237
43f3e411 9238 if (cust != NULL)
c906108c 9239 {
df15bd07 9240 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9241
8be455d7
JK
9242 /* Set symtab language to language from DW_AT_language. If the
9243 compilation is from a C file generated by language preprocessors, do
9244 not set the language if it was already deduced by start_subfile. */
43f3e411 9245 if (!(cu->language == language_c
40e3ad0e 9246 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9247 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9248
9249 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9250 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9251 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9252 there were bugs in prologue debug info, fixed later in GCC-4.5
9253 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9254
9255 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9256 needed, it would be wrong due to missing DW_AT_producer there.
9257
9258 Still one can confuse GDB by using non-standard GCC compilation
9259 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9260 */
ab260dad 9261 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9262 cust->locations_valid = 1;
e0d00bc7
JK
9263
9264 if (gcc_4_minor >= 5)
43f3e411 9265 cust->epilogue_unwind_valid = 1;
96408a79 9266
43f3e411 9267 cust->call_site_htab = cu->call_site_htab;
c906108c 9268 }
9291a0cd
TT
9269
9270 if (dwarf2_per_objfile->using_index)
43f3e411 9271 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
9272 else
9273 {
9274 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 9275 pst->compunit_symtab = cust;
9291a0cd
TT
9276 pst->readin = 1;
9277 }
c906108c 9278
95554aad
TT
9279 /* Push it for inclusion processing later. */
9280 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
f4dc4d17 9281}
45cfd468 9282
f4dc4d17
DE
9283/* Generate full symbol information for type unit PER_CU, whose DIEs have
9284 already been loaded into memory. */
9285
9286static void
9287process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9288 enum language pretend_language)
9289{
9290 struct dwarf2_cu *cu = per_cu->cu;
9291 struct objfile *objfile = per_cu->objfile;
43f3e411 9292 struct compunit_symtab *cust;
33c7c59d 9293 struct cleanup *delayed_list_cleanup;
0186c6a7
DE
9294 struct signatured_type *sig_type;
9295
9296 gdb_assert (per_cu->is_debug_types);
9297 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
9298
9299 buildsym_init ();
33c7c59d 9300 scoped_free_pendings free_pending;
f4dc4d17
DE
9301 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
9302
9303 cu->list_in_scope = &file_symbols;
9304
9305 cu->language = pretend_language;
9306 cu->language_defn = language_def (cu->language);
9307
9308 /* The symbol tables are set up in read_type_unit_scope. */
9309 process_die (cu->dies, cu);
9310
9311 /* For now fudge the Go package. */
9312 if (cu->language == language_go)
9313 fixup_go_packaging (cu);
9314
9315 /* Now that we have processed all the DIEs in the CU, all the types
9316 should be complete, and it should now be safe to compute all of the
9317 physnames. */
9318 compute_delayed_physnames (cu);
9319 do_cleanups (delayed_list_cleanup);
9320
9321 /* TUs share symbol tables.
9322 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9323 of it with end_expandable_symtab. Otherwise, complete the addition of
9324 this TU's symbols to the existing symtab. */
43f3e411 9325 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9326 {
43f3e411
DE
9327 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9328 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9329
43f3e411 9330 if (cust != NULL)
f4dc4d17
DE
9331 {
9332 /* Set symtab language to language from DW_AT_language. If the
9333 compilation is from a C file generated by language preprocessors,
9334 do not set the language if it was already deduced by
9335 start_subfile. */
43f3e411
DE
9336 if (!(cu->language == language_c
9337 && COMPUNIT_FILETABS (cust)->language != language_c))
9338 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9339 }
9340 }
9341 else
9342 {
0ab9ce85 9343 augment_type_symtab ();
43f3e411 9344 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9345 }
9346
9347 if (dwarf2_per_objfile->using_index)
43f3e411 9348 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
9349 else
9350 {
9351 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 9352 pst->compunit_symtab = cust;
f4dc4d17 9353 pst->readin = 1;
45cfd468 9354 }
c906108c
SS
9355}
9356
95554aad
TT
9357/* Process an imported unit DIE. */
9358
9359static void
9360process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9361{
9362 struct attribute *attr;
9363
f4dc4d17
DE
9364 /* For now we don't handle imported units in type units. */
9365 if (cu->per_cu->is_debug_types)
9366 {
9367 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9368 " supported in type units [in module %s]"),
4262abfb 9369 objfile_name (cu->objfile));
f4dc4d17
DE
9370 }
9371
95554aad
TT
9372 attr = dwarf2_attr (die, DW_AT_import, cu);
9373 if (attr != NULL)
9374 {
9c541725
PA
9375 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9376 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9377 dwarf2_per_cu_data *per_cu
9378 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
95554aad 9379
69d751e3 9380 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
9381 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9382 load_full_comp_unit (per_cu, cu->language);
9383
796a7ff8 9384 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
9385 per_cu);
9386 }
9387}
9388
4c8aa72d
PA
9389/* RAII object that represents a process_die scope: i.e.,
9390 starts/finishes processing a DIE. */
9391class process_die_scope
adde2bff 9392{
4c8aa72d
PA
9393public:
9394 process_die_scope (die_info *die, dwarf2_cu *cu)
9395 : m_die (die), m_cu (cu)
9396 {
9397 /* We should only be processing DIEs not already in process. */
9398 gdb_assert (!m_die->in_process);
9399 m_die->in_process = true;
9400 }
8c3cb9fa 9401
4c8aa72d
PA
9402 ~process_die_scope ()
9403 {
9404 m_die->in_process = false;
9405
9406 /* If we're done processing the DIE for the CU that owns the line
9407 header, we don't need the line header anymore. */
9408 if (m_cu->line_header_die_owner == m_die)
9409 {
9410 delete m_cu->line_header;
9411 m_cu->line_header = NULL;
9412 m_cu->line_header_die_owner = NULL;
9413 }
9414 }
9415
9416private:
9417 die_info *m_die;
9418 dwarf2_cu *m_cu;
9419};
adde2bff 9420
c906108c
SS
9421/* Process a die and its children. */
9422
9423static void
e7c27a73 9424process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9425{
4c8aa72d 9426 process_die_scope scope (die, cu);
adde2bff 9427
c906108c
SS
9428 switch (die->tag)
9429 {
9430 case DW_TAG_padding:
9431 break;
9432 case DW_TAG_compile_unit:
95554aad 9433 case DW_TAG_partial_unit:
e7c27a73 9434 read_file_scope (die, cu);
c906108c 9435 break;
348e048f
DE
9436 case DW_TAG_type_unit:
9437 read_type_unit_scope (die, cu);
9438 break;
c906108c 9439 case DW_TAG_subprogram:
c906108c 9440 case DW_TAG_inlined_subroutine:
edb3359d 9441 read_func_scope (die, cu);
c906108c
SS
9442 break;
9443 case DW_TAG_lexical_block:
14898363
L
9444 case DW_TAG_try_block:
9445 case DW_TAG_catch_block:
e7c27a73 9446 read_lexical_block_scope (die, cu);
c906108c 9447 break;
216f72a1 9448 case DW_TAG_call_site:
96408a79
SA
9449 case DW_TAG_GNU_call_site:
9450 read_call_site_scope (die, cu);
9451 break;
c906108c 9452 case DW_TAG_class_type:
680b30c7 9453 case DW_TAG_interface_type:
c906108c
SS
9454 case DW_TAG_structure_type:
9455 case DW_TAG_union_type:
134d01f1 9456 process_structure_scope (die, cu);
c906108c
SS
9457 break;
9458 case DW_TAG_enumeration_type:
134d01f1 9459 process_enumeration_scope (die, cu);
c906108c 9460 break;
134d01f1 9461
f792889a
DJ
9462 /* These dies have a type, but processing them does not create
9463 a symbol or recurse to process the children. Therefore we can
9464 read them on-demand through read_type_die. */
c906108c 9465 case DW_TAG_subroutine_type:
72019c9c 9466 case DW_TAG_set_type:
c906108c 9467 case DW_TAG_array_type:
c906108c 9468 case DW_TAG_pointer_type:
c906108c 9469 case DW_TAG_ptr_to_member_type:
c906108c 9470 case DW_TAG_reference_type:
4297a3f0 9471 case DW_TAG_rvalue_reference_type:
c906108c 9472 case DW_TAG_string_type:
c906108c 9473 break;
134d01f1 9474
c906108c 9475 case DW_TAG_base_type:
a02abb62 9476 case DW_TAG_subrange_type:
cb249c71 9477 case DW_TAG_typedef:
134d01f1
DJ
9478 /* Add a typedef symbol for the type definition, if it has a
9479 DW_AT_name. */
f792889a 9480 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9481 break;
c906108c 9482 case DW_TAG_common_block:
e7c27a73 9483 read_common_block (die, cu);
c906108c
SS
9484 break;
9485 case DW_TAG_common_inclusion:
9486 break;
d9fa45fe 9487 case DW_TAG_namespace:
4d4ec4e5 9488 cu->processing_has_namespace_info = 1;
e7c27a73 9489 read_namespace (die, cu);
d9fa45fe 9490 break;
5d7cb8df 9491 case DW_TAG_module:
4d4ec4e5 9492 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
9493 read_module (die, cu);
9494 break;
d9fa45fe 9495 case DW_TAG_imported_declaration:
74921315
KS
9496 cu->processing_has_namespace_info = 1;
9497 if (read_namespace_alias (die, cu))
9498 break;
9499 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 9500 case DW_TAG_imported_module:
4d4ec4e5 9501 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
9502 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9503 || cu->language != language_fortran))
9504 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
9505 dwarf_tag_name (die->tag));
9506 read_import_statement (die, cu);
d9fa45fe 9507 break;
95554aad
TT
9508
9509 case DW_TAG_imported_unit:
9510 process_imported_unit_die (die, cu);
9511 break;
9512
71a3c369
TT
9513 case DW_TAG_variable:
9514 read_variable (die, cu);
9515 break;
9516
c906108c 9517 default:
e7c27a73 9518 new_symbol (die, NULL, cu);
c906108c
SS
9519 break;
9520 }
9521}
ca69b9e6
DE
9522\f
9523/* DWARF name computation. */
c906108c 9524
94af9270
KS
9525/* A helper function for dwarf2_compute_name which determines whether DIE
9526 needs to have the name of the scope prepended to the name listed in the
9527 die. */
9528
9529static int
9530die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9531{
1c809c68
TT
9532 struct attribute *attr;
9533
94af9270
KS
9534 switch (die->tag)
9535 {
9536 case DW_TAG_namespace:
9537 case DW_TAG_typedef:
9538 case DW_TAG_class_type:
9539 case DW_TAG_interface_type:
9540 case DW_TAG_structure_type:
9541 case DW_TAG_union_type:
9542 case DW_TAG_enumeration_type:
9543 case DW_TAG_enumerator:
9544 case DW_TAG_subprogram:
08a76f8a 9545 case DW_TAG_inlined_subroutine:
94af9270 9546 case DW_TAG_member:
74921315 9547 case DW_TAG_imported_declaration:
94af9270
KS
9548 return 1;
9549
9550 case DW_TAG_variable:
c2b0a229 9551 case DW_TAG_constant:
94af9270
KS
9552 /* We only need to prefix "globally" visible variables. These include
9553 any variable marked with DW_AT_external or any variable that
9554 lives in a namespace. [Variables in anonymous namespaces
9555 require prefixing, but they are not DW_AT_external.] */
9556
9557 if (dwarf2_attr (die, DW_AT_specification, cu))
9558 {
9559 struct dwarf2_cu *spec_cu = cu;
9a619af0 9560
94af9270
KS
9561 return die_needs_namespace (die_specification (die, &spec_cu),
9562 spec_cu);
9563 }
9564
1c809c68 9565 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9566 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9567 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9568 return 0;
9569 /* A variable in a lexical block of some kind does not need a
9570 namespace, even though in C++ such variables may be external
9571 and have a mangled name. */
9572 if (die->parent->tag == DW_TAG_lexical_block
9573 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9574 || die->parent->tag == DW_TAG_catch_block
9575 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9576 return 0;
9577 return 1;
94af9270
KS
9578
9579 default:
9580 return 0;
9581 }
9582}
9583
73b9be8b
KS
9584/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9585 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9586 defined for the given DIE. */
9587
9588static struct attribute *
9589dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9590{
9591 struct attribute *attr;
9592
9593 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9594 if (attr == NULL)
9595 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9596
9597 return attr;
9598}
9599
9600/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9601 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9602 defined for the given DIE. */
9603
9604static const char *
9605dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9606{
9607 const char *linkage_name;
9608
9609 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9610 if (linkage_name == NULL)
9611 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9612
9613 return linkage_name;
9614}
9615
94af9270 9616/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 9617 compute the physname for the object, which include a method's:
9c37b5ae 9618 - formal parameters (C++),
a766d390 9619 - receiver type (Go),
a766d390
DE
9620
9621 The term "physname" is a bit confusing.
9622 For C++, for example, it is the demangled name.
9623 For Go, for example, it's the mangled name.
94af9270 9624
af6b7be1
JB
9625 For Ada, return the DIE's linkage name rather than the fully qualified
9626 name. PHYSNAME is ignored..
9627
94af9270
KS
9628 The result is allocated on the objfile_obstack and canonicalized. */
9629
9630static const char *
15d034d0
TT
9631dwarf2_compute_name (const char *name,
9632 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
9633 int physname)
9634{
bb5ed363
DE
9635 struct objfile *objfile = cu->objfile;
9636
94af9270
KS
9637 if (name == NULL)
9638 name = dwarf2_name (die, cu);
9639
2ee7123e
DE
9640 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9641 but otherwise compute it by typename_concat inside GDB.
9642 FIXME: Actually this is not really true, or at least not always true.
9643 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
9644 Fortran names because there is no mangling standard. So new_symbol_full
9645 will set the demangled name to the result of dwarf2_full_name, and it is
9646 the demangled name that GDB uses if it exists. */
f55ee35c
JK
9647 if (cu->language == language_ada
9648 || (cu->language == language_fortran && physname))
9649 {
9650 /* For Ada unit, we prefer the linkage name over the name, as
9651 the former contains the exported name, which the user expects
9652 to be able to reference. Ideally, we want the user to be able
9653 to reference this entity using either natural or linkage name,
9654 but we haven't started looking at this enhancement yet. */
73b9be8b 9655 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 9656
2ee7123e
DE
9657 if (linkage_name != NULL)
9658 return linkage_name;
f55ee35c
JK
9659 }
9660
94af9270
KS
9661 /* These are the only languages we know how to qualify names in. */
9662 if (name != NULL
9c37b5ae 9663 && (cu->language == language_cplus
c44af4eb
TT
9664 || cu->language == language_fortran || cu->language == language_d
9665 || cu->language == language_rust))
94af9270
KS
9666 {
9667 if (die_needs_namespace (die, cu))
9668 {
9669 long length;
0d5cff50 9670 const char *prefix;
34a68019 9671 const char *canonical_name = NULL;
94af9270 9672
d7e74731
PA
9673 string_file buf;
9674
94af9270 9675 prefix = determine_prefix (die, cu);
94af9270
KS
9676 if (*prefix != '\0')
9677 {
f55ee35c
JK
9678 char *prefixed_name = typename_concat (NULL, prefix, name,
9679 physname, cu);
9a619af0 9680
d7e74731 9681 buf.puts (prefixed_name);
94af9270
KS
9682 xfree (prefixed_name);
9683 }
9684 else
d7e74731 9685 buf.puts (name);
94af9270 9686
98bfdba5
PA
9687 /* Template parameters may be specified in the DIE's DW_AT_name, or
9688 as children with DW_TAG_template_type_param or
9689 DW_TAG_value_type_param. If the latter, add them to the name
9690 here. If the name already has template parameters, then
9691 skip this step; some versions of GCC emit both, and
9692 it is more efficient to use the pre-computed name.
9693
9694 Something to keep in mind about this process: it is very
9695 unlikely, or in some cases downright impossible, to produce
9696 something that will match the mangled name of a function.
9697 If the definition of the function has the same debug info,
9698 we should be able to match up with it anyway. But fallbacks
9699 using the minimal symbol, for instance to find a method
9700 implemented in a stripped copy of libstdc++, will not work.
9701 If we do not have debug info for the definition, we will have to
9702 match them up some other way.
9703
9704 When we do name matching there is a related problem with function
9705 templates; two instantiated function templates are allowed to
9706 differ only by their return types, which we do not add here. */
9707
9708 if (cu->language == language_cplus && strchr (name, '<') == NULL)
9709 {
9710 struct attribute *attr;
9711 struct die_info *child;
9712 int first = 1;
9713
9714 die->building_fullname = 1;
9715
9716 for (child = die->child; child != NULL; child = child->sibling)
9717 {
9718 struct type *type;
12df843f 9719 LONGEST value;
d521ce57 9720 const gdb_byte *bytes;
98bfdba5
PA
9721 struct dwarf2_locexpr_baton *baton;
9722 struct value *v;
9723
9724 if (child->tag != DW_TAG_template_type_param
9725 && child->tag != DW_TAG_template_value_param)
9726 continue;
9727
9728 if (first)
9729 {
d7e74731 9730 buf.puts ("<");
98bfdba5
PA
9731 first = 0;
9732 }
9733 else
d7e74731 9734 buf.puts (", ");
98bfdba5
PA
9735
9736 attr = dwarf2_attr (child, DW_AT_type, cu);
9737 if (attr == NULL)
9738 {
9739 complaint (&symfile_complaints,
9740 _("template parameter missing DW_AT_type"));
d7e74731 9741 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
9742 continue;
9743 }
9744 type = die_type (child, cu);
9745
9746 if (child->tag == DW_TAG_template_type_param)
9747 {
d7e74731 9748 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
9749 continue;
9750 }
9751
9752 attr = dwarf2_attr (child, DW_AT_const_value, cu);
9753 if (attr == NULL)
9754 {
9755 complaint (&symfile_complaints,
3e43a32a
MS
9756 _("template parameter missing "
9757 "DW_AT_const_value"));
d7e74731 9758 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
9759 continue;
9760 }
9761
9762 dwarf2_const_value_attr (attr, type, name,
9763 &cu->comp_unit_obstack, cu,
9764 &value, &bytes, &baton);
9765
9766 if (TYPE_NOSIGN (type))
9767 /* GDB prints characters as NUMBER 'CHAR'. If that's
9768 changed, this can use value_print instead. */
d7e74731 9769 c_printchar (value, type, &buf);
98bfdba5
PA
9770 else
9771 {
9772 struct value_print_options opts;
9773
9774 if (baton != NULL)
9775 v = dwarf2_evaluate_loc_desc (type, NULL,
9776 baton->data,
9777 baton->size,
9778 baton->per_cu);
9779 else if (bytes != NULL)
9780 {
9781 v = allocate_value (type);
9782 memcpy (value_contents_writeable (v), bytes,
9783 TYPE_LENGTH (type));
9784 }
9785 else
9786 v = value_from_longest (type, value);
9787
3e43a32a
MS
9788 /* Specify decimal so that we do not depend on
9789 the radix. */
98bfdba5
PA
9790 get_formatted_print_options (&opts, 'd');
9791 opts.raw = 1;
d7e74731 9792 value_print (v, &buf, &opts);
98bfdba5
PA
9793 release_value (v);
9794 value_free (v);
9795 }
9796 }
9797
9798 die->building_fullname = 0;
9799
9800 if (!first)
9801 {
9802 /* Close the argument list, with a space if necessary
9803 (nested templates). */
d7e74731
PA
9804 if (!buf.empty () && buf.string ().back () == '>')
9805 buf.puts (" >");
98bfdba5 9806 else
d7e74731 9807 buf.puts (">");
98bfdba5
PA
9808 }
9809 }
9810
9c37b5ae 9811 /* For C++ methods, append formal parameter type
94af9270 9812 information, if PHYSNAME. */
6e70227d 9813
94af9270 9814 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 9815 && cu->language == language_cplus)
94af9270
KS
9816 {
9817 struct type *type = read_type_die (die, cu);
9818
d7e74731 9819 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 9820 &type_print_raw_options);
94af9270 9821
9c37b5ae 9822 if (cu->language == language_cplus)
94af9270 9823 {
60430eff
DJ
9824 /* Assume that an artificial first parameter is
9825 "this", but do not crash if it is not. RealView
9826 marks unnamed (and thus unused) parameters as
9827 artificial; there is no way to differentiate
9828 the two cases. */
94af9270
KS
9829 if (TYPE_NFIELDS (type) > 0
9830 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 9831 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
9832 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
9833 0))))
d7e74731 9834 buf.puts (" const");
94af9270
KS
9835 }
9836 }
9837
d7e74731 9838 const std::string &intermediate_name = buf.string ();
94af9270
KS
9839
9840 if (cu->language == language_cplus)
34a68019 9841 canonical_name
322a8516 9842 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
9843 &objfile->per_bfd->storage_obstack);
9844
9845 /* If we only computed INTERMEDIATE_NAME, or if
9846 INTERMEDIATE_NAME is already canonical, then we need to
9847 copy it to the appropriate obstack. */
322a8516 9848 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
9849 name = ((const char *)
9850 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
9851 intermediate_name.c_str (),
9852 intermediate_name.length ()));
34a68019
TT
9853 else
9854 name = canonical_name;
94af9270
KS
9855 }
9856 }
9857
9858 return name;
9859}
9860
0114d602
DJ
9861/* Return the fully qualified name of DIE, based on its DW_AT_name.
9862 If scope qualifiers are appropriate they will be added. The result
34a68019 9863 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
9864 not have a name. NAME may either be from a previous call to
9865 dwarf2_name or NULL.
9866
9c37b5ae 9867 The output string will be canonicalized (if C++). */
0114d602
DJ
9868
9869static const char *
15d034d0 9870dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 9871{
94af9270
KS
9872 return dwarf2_compute_name (name, die, cu, 0);
9873}
0114d602 9874
94af9270
KS
9875/* Construct a physname for the given DIE in CU. NAME may either be
9876 from a previous call to dwarf2_name or NULL. The result will be
9877 allocated on the objfile_objstack or NULL if the DIE does not have a
9878 name.
0114d602 9879
9c37b5ae 9880 The output string will be canonicalized (if C++). */
0114d602 9881
94af9270 9882static const char *
15d034d0 9883dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 9884{
bb5ed363 9885 struct objfile *objfile = cu->objfile;
900e11f9 9886 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
9887 int need_copy = 1;
9888
9889 /* In this case dwarf2_compute_name is just a shortcut not building anything
9890 on its own. */
9891 if (!die_needs_namespace (die, cu))
9892 return dwarf2_compute_name (name, die, cu, 1);
9893
73b9be8b 9894 mangled = dw2_linkage_name (die, cu);
900e11f9 9895
e98c9e7c
TT
9896 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9897 See https://github.com/rust-lang/rust/issues/32925. */
9898 if (cu->language == language_rust && mangled != NULL
9899 && strchr (mangled, '{') != NULL)
9900 mangled = NULL;
9901
900e11f9
JK
9902 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9903 has computed. */
791afaa2 9904 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 9905 if (mangled != NULL)
900e11f9 9906 {
900e11f9
JK
9907 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
9908 type. It is easier for GDB users to search for such functions as
9909 `name(params)' than `long name(params)'. In such case the minimal
9910 symbol names do not match the full symbol names but for template
9911 functions there is never a need to look up their definition from their
9912 declaration so the only disadvantage remains the minimal symbol
9913 variant `long name(params)' does not have the proper inferior type.
9914 */
9915
a766d390
DE
9916 if (cu->language == language_go)
9917 {
9918 /* This is a lie, but we already lie to the caller new_symbol_full.
9919 new_symbol_full assumes we return the mangled name.
9920 This just undoes that lie until things are cleaned up. */
a766d390
DE
9921 }
9922 else
9923 {
791afaa2
TT
9924 demangled.reset (gdb_demangle (mangled,
9925 (DMGL_PARAMS | DMGL_ANSI
9926 | DMGL_RET_DROP)));
a766d390 9927 }
900e11f9 9928 if (demangled)
791afaa2 9929 canon = demangled.get ();
900e11f9
JK
9930 else
9931 {
9932 canon = mangled;
9933 need_copy = 0;
9934 }
9935 }
9936
9937 if (canon == NULL || check_physname)
9938 {
9939 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9940
9941 if (canon != NULL && strcmp (physname, canon) != 0)
9942 {
9943 /* It may not mean a bug in GDB. The compiler could also
9944 compute DW_AT_linkage_name incorrectly. But in such case
9945 GDB would need to be bug-to-bug compatible. */
9946
9947 complaint (&symfile_complaints,
9948 _("Computed physname <%s> does not match demangled <%s> "
9949 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9c541725 9950 physname, canon, mangled, to_underlying (die->sect_off),
4262abfb 9951 objfile_name (objfile));
900e11f9
JK
9952
9953 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9954 is available here - over computed PHYSNAME. It is safer
9955 against both buggy GDB and buggy compilers. */
9956
9957 retval = canon;
9958 }
9959 else
9960 {
9961 retval = physname;
9962 need_copy = 0;
9963 }
9964 }
9965 else
9966 retval = canon;
9967
9968 if (need_copy)
224c3ddb
SM
9969 retval = ((const char *)
9970 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9971 retval, strlen (retval)));
900e11f9 9972
900e11f9 9973 return retval;
0114d602
DJ
9974}
9975
74921315
KS
9976/* Inspect DIE in CU for a namespace alias. If one exists, record
9977 a new symbol for it.
9978
9979 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9980
9981static int
9982read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
9983{
9984 struct attribute *attr;
9985
9986 /* If the die does not have a name, this is not a namespace
9987 alias. */
9988 attr = dwarf2_attr (die, DW_AT_name, cu);
9989 if (attr != NULL)
9990 {
9991 int num;
9992 struct die_info *d = die;
9993 struct dwarf2_cu *imported_cu = cu;
9994
9995 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9996 keep inspecting DIEs until we hit the underlying import. */
9997#define MAX_NESTED_IMPORTED_DECLARATIONS 100
9998 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9999 {
10000 attr = dwarf2_attr (d, DW_AT_import, cu);
10001 if (attr == NULL)
10002 break;
10003
10004 d = follow_die_ref (d, attr, &imported_cu);
10005 if (d->tag != DW_TAG_imported_declaration)
10006 break;
10007 }
10008
10009 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10010 {
10011 complaint (&symfile_complaints,
10012 _("DIE at 0x%x has too many recursively imported "
9c541725 10013 "declarations"), to_underlying (d->sect_off));
74921315
KS
10014 return 0;
10015 }
10016
10017 if (attr != NULL)
10018 {
10019 struct type *type;
9c541725 10020 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 10021
9c541725 10022 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
10023 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10024 {
10025 /* This declaration is a global namespace alias. Add
10026 a symbol for it whose type is the aliased namespace. */
10027 new_symbol (die, type, cu);
10028 return 1;
10029 }
10030 }
10031 }
10032
10033 return 0;
10034}
10035
22cee43f
PMR
10036/* Return the using directives repository (global or local?) to use in the
10037 current context for LANGUAGE.
10038
10039 For Ada, imported declarations can materialize renamings, which *may* be
10040 global. However it is impossible (for now?) in DWARF to distinguish
10041 "external" imported declarations and "static" ones. As all imported
10042 declarations seem to be static in all other languages, make them all CU-wide
10043 global only in Ada. */
10044
10045static struct using_direct **
10046using_directives (enum language language)
10047{
10048 if (language == language_ada && context_stack_depth == 0)
10049 return &global_using_directives;
10050 else
10051 return &local_using_directives;
10052}
10053
27aa8d6a
SW
10054/* Read the import statement specified by the given die and record it. */
10055
10056static void
10057read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10058{
bb5ed363 10059 struct objfile *objfile = cu->objfile;
27aa8d6a 10060 struct attribute *import_attr;
32019081 10061 struct die_info *imported_die, *child_die;
de4affc9 10062 struct dwarf2_cu *imported_cu;
27aa8d6a 10063 const char *imported_name;
794684b6 10064 const char *imported_name_prefix;
13387711
SW
10065 const char *canonical_name;
10066 const char *import_alias;
10067 const char *imported_declaration = NULL;
794684b6 10068 const char *import_prefix;
eb1e02fd 10069 std::vector<const char *> excludes;
13387711 10070
27aa8d6a
SW
10071 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10072 if (import_attr == NULL)
10073 {
10074 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
10075 dwarf_tag_name (die->tag));
10076 return;
10077 }
10078
de4affc9
CC
10079 imported_cu = cu;
10080 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10081 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10082 if (imported_name == NULL)
10083 {
10084 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10085
10086 The import in the following code:
10087 namespace A
10088 {
10089 typedef int B;
10090 }
10091
10092 int main ()
10093 {
10094 using A::B;
10095 B b;
10096 return b;
10097 }
10098
10099 ...
10100 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10101 <52> DW_AT_decl_file : 1
10102 <53> DW_AT_decl_line : 6
10103 <54> DW_AT_import : <0x75>
10104 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10105 <59> DW_AT_name : B
10106 <5b> DW_AT_decl_file : 1
10107 <5c> DW_AT_decl_line : 2
10108 <5d> DW_AT_type : <0x6e>
10109 ...
10110 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10111 <76> DW_AT_byte_size : 4
10112 <77> DW_AT_encoding : 5 (signed)
10113
10114 imports the wrong die ( 0x75 instead of 0x58 ).
10115 This case will be ignored until the gcc bug is fixed. */
10116 return;
10117 }
10118
82856980
SW
10119 /* Figure out the local name after import. */
10120 import_alias = dwarf2_name (die, cu);
27aa8d6a 10121
794684b6
SW
10122 /* Figure out where the statement is being imported to. */
10123 import_prefix = determine_prefix (die, cu);
10124
10125 /* Figure out what the scope of the imported die is and prepend it
10126 to the name of the imported die. */
de4affc9 10127 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10128
f55ee35c
JK
10129 if (imported_die->tag != DW_TAG_namespace
10130 && imported_die->tag != DW_TAG_module)
794684b6 10131 {
13387711
SW
10132 imported_declaration = imported_name;
10133 canonical_name = imported_name_prefix;
794684b6 10134 }
13387711 10135 else if (strlen (imported_name_prefix) > 0)
12aaed36 10136 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10137 imported_name_prefix,
10138 (cu->language == language_d ? "." : "::"),
10139 imported_name, (char *) NULL);
13387711
SW
10140 else
10141 canonical_name = imported_name;
794684b6 10142
32019081
JK
10143 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10144 for (child_die = die->child; child_die && child_die->tag;
10145 child_die = sibling_die (child_die))
10146 {
10147 /* DWARF-4: A Fortran use statement with a “rename list” may be
10148 represented by an imported module entry with an import attribute
10149 referring to the module and owned entries corresponding to those
10150 entities that are renamed as part of being imported. */
10151
10152 if (child_die->tag != DW_TAG_imported_declaration)
10153 {
10154 complaint (&symfile_complaints,
10155 _("child DW_TAG_imported_declaration expected "
10156 "- DIE at 0x%x [in module %s]"),
9c541725 10157 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
10158 continue;
10159 }
10160
10161 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10162 if (import_attr == NULL)
10163 {
10164 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
10165 dwarf_tag_name (child_die->tag));
10166 continue;
10167 }
10168
10169 imported_cu = cu;
10170 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10171 &imported_cu);
10172 imported_name = dwarf2_name (imported_die, imported_cu);
10173 if (imported_name == NULL)
10174 {
10175 complaint (&symfile_complaints,
10176 _("child DW_TAG_imported_declaration has unknown "
10177 "imported name - DIE at 0x%x [in module %s]"),
9c541725 10178 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
10179 continue;
10180 }
10181
eb1e02fd 10182 excludes.push_back (imported_name);
32019081
JK
10183
10184 process_die (child_die, cu);
10185 }
10186
22cee43f
PMR
10187 add_using_directive (using_directives (cu->language),
10188 import_prefix,
10189 canonical_name,
10190 import_alias,
10191 imported_declaration,
10192 excludes,
10193 0,
10194 &objfile->objfile_obstack);
27aa8d6a
SW
10195}
10196
5230b05a
WT
10197/* ICC<14 does not output the required DW_AT_declaration on incomplete
10198 types, but gives them a size of zero. Starting with version 14,
10199 ICC is compatible with GCC. */
10200
10201static int
10202producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10203{
10204 if (!cu->checked_producer)
10205 check_producer (cu);
10206
10207 return cu->producer_is_icc_lt_14;
10208}
10209
1b80a9fa
JK
10210/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10211 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10212 this, it was first present in GCC release 4.3.0. */
10213
10214static int
10215producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10216{
10217 if (!cu->checked_producer)
10218 check_producer (cu);
10219
10220 return cu->producer_is_gcc_lt_4_3;
10221}
10222
d721ba37
PA
10223static file_and_directory
10224find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10225{
d721ba37
PA
10226 file_and_directory res;
10227
9291a0cd
TT
10228 /* Find the filename. Do not use dwarf2_name here, since the filename
10229 is not a source language identifier. */
d721ba37
PA
10230 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10231 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10232
d721ba37
PA
10233 if (res.comp_dir == NULL
10234 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10235 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10236 {
d721ba37
PA
10237 res.comp_dir_storage = ldirname (res.name);
10238 if (!res.comp_dir_storage.empty ())
10239 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10240 }
d721ba37 10241 if (res.comp_dir != NULL)
9291a0cd
TT
10242 {
10243 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10244 directory, get rid of it. */
d721ba37 10245 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10246
d721ba37
PA
10247 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10248 res.comp_dir = cp + 1;
9291a0cd
TT
10249 }
10250
d721ba37
PA
10251 if (res.name == NULL)
10252 res.name = "<unknown>";
10253
10254 return res;
9291a0cd
TT
10255}
10256
f4dc4d17
DE
10257/* Handle DW_AT_stmt_list for a compilation unit.
10258 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10259 COMP_DIR is the compilation directory. LOWPC is passed to
10260 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10261
10262static void
10263handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10264 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10265{
527f3840 10266 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 10267 struct attribute *attr;
527f3840
JK
10268 struct line_header line_header_local;
10269 hashval_t line_header_local_hash;
10270 unsigned u;
10271 void **slot;
10272 int decode_mapping;
2ab95328 10273
f4dc4d17
DE
10274 gdb_assert (! cu->per_cu->is_debug_types);
10275
2ab95328 10276 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10277 if (attr == NULL)
10278 return;
10279
9c541725 10280 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10281
10282 /* The line header hash table is only created if needed (it exists to
10283 prevent redundant reading of the line table for partial_units).
10284 If we're given a partial_unit, we'll need it. If we're given a
10285 compile_unit, then use the line header hash table if it's already
10286 created, but don't create one just yet. */
10287
10288 if (dwarf2_per_objfile->line_header_hash == NULL
10289 && die->tag == DW_TAG_partial_unit)
2ab95328 10290 {
527f3840
JK
10291 dwarf2_per_objfile->line_header_hash
10292 = htab_create_alloc_ex (127, line_header_hash_voidp,
10293 line_header_eq_voidp,
10294 free_line_header_voidp,
10295 &objfile->objfile_obstack,
10296 hashtab_obstack_allocate,
10297 dummy_obstack_deallocate);
10298 }
2ab95328 10299
9c541725 10300 line_header_local.sect_off = line_offset;
527f3840
JK
10301 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10302 line_header_local_hash = line_header_hash (&line_header_local);
10303 if (dwarf2_per_objfile->line_header_hash != NULL)
10304 {
10305 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
10306 &line_header_local,
10307 line_header_local_hash, NO_INSERT);
10308
10309 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10310 is not present in *SLOT (since if there is something in *SLOT then
10311 it will be for a partial_unit). */
10312 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10313 {
527f3840 10314 gdb_assert (*slot != NULL);
9a3c8263 10315 cu->line_header = (struct line_header *) *slot;
527f3840 10316 return;
dee91e82 10317 }
2ab95328 10318 }
527f3840
JK
10319
10320 /* dwarf_decode_line_header does not yet provide sufficient information.
10321 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10322 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10323 if (lh == NULL)
527f3840 10324 return;
4c8aa72d
PA
10325
10326 cu->line_header = lh.release ();
10327 cu->line_header_die_owner = die;
527f3840
JK
10328
10329 if (dwarf2_per_objfile->line_header_hash == NULL)
10330 slot = NULL;
10331 else
10332 {
10333 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
10334 &line_header_local,
10335 line_header_local_hash, INSERT);
10336 gdb_assert (slot != NULL);
10337 }
10338 if (slot != NULL && *slot == NULL)
10339 {
10340 /* This newly decoded line number information unit will be owned
10341 by line_header_hash hash table. */
10342 *slot = cu->line_header;
4c8aa72d 10343 cu->line_header_die_owner = NULL;
527f3840
JK
10344 }
10345 else
10346 {
10347 /* We cannot free any current entry in (*slot) as that struct line_header
10348 may be already used by multiple CUs. Create only temporary decoded
10349 line_header for this CU - it may happen at most once for each line
10350 number information unit. And if we're not using line_header_hash
10351 then this is what we want as well. */
10352 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10353 }
10354 decode_mapping = (die->tag != DW_TAG_partial_unit);
10355 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10356 decode_mapping);
fff8551c 10357
2ab95328
TT
10358}
10359
95554aad 10360/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10361
c906108c 10362static void
e7c27a73 10363read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10364{
dee91e82 10365 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10366 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 10367 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10368 CORE_ADDR highpc = ((CORE_ADDR) 0);
10369 struct attribute *attr;
c906108c 10370 struct die_info *child_die;
e142c38c 10371 CORE_ADDR baseaddr;
6e70227d 10372
e142c38c 10373 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10374
fae299cd 10375 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10376
10377 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10378 from finish_block. */
2acceee2 10379 if (lowpc == ((CORE_ADDR) -1))
c906108c 10380 lowpc = highpc;
3e29f34a 10381 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10382
d721ba37 10383 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10384
95554aad 10385 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 10386
f4b8a18d
KW
10387 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10388 standardised yet. As a workaround for the language detection we fall
10389 back to the DW_AT_producer string. */
10390 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10391 cu->language = language_opencl;
10392
3019eac3
DE
10393 /* Similar hack for Go. */
10394 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10395 set_cu_language (DW_LANG_Go, cu);
10396
d721ba37 10397 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10398
10399 /* Decode line number information if present. We do this before
10400 processing child DIEs, so that the line header table is available
10401 for DW_AT_decl_file. */
d721ba37 10402 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10403
10404 /* Process all dies in compilation unit. */
10405 if (die->child != NULL)
10406 {
10407 child_die = die->child;
10408 while (child_die && child_die->tag)
10409 {
10410 process_die (child_die, cu);
10411 child_die = sibling_die (child_die);
10412 }
10413 }
10414
10415 /* Decode macro information, if present. Dwarf 2 macro information
10416 refers to information in the line number info statement program
10417 header, so we can only read it if we've read the header
10418 successfully. */
0af92d60
JK
10419 attr = dwarf2_attr (die, DW_AT_macros, cu);
10420 if (attr == NULL)
10421 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10422 if (attr && cu->line_header)
10423 {
10424 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10425 complaint (&symfile_complaints,
0af92d60 10426 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10427
43f3e411 10428 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10429 }
10430 else
10431 {
10432 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10433 if (attr && cu->line_header)
10434 {
10435 unsigned int macro_offset = DW_UNSND (attr);
10436
43f3e411 10437 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10438 }
10439 }
3019eac3
DE
10440}
10441
f4dc4d17
DE
10442/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
10443 Create the set of symtabs used by this TU, or if this TU is sharing
10444 symtabs with another TU and the symtabs have already been created
10445 then restore those symtabs in the line header.
10446 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
10447
10448static void
f4dc4d17 10449setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 10450{
f4dc4d17
DE
10451 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
10452 struct type_unit_group *tu_group;
10453 int first_time;
3019eac3 10454 struct attribute *attr;
9c541725 10455 unsigned int i;
0186c6a7 10456 struct signatured_type *sig_type;
3019eac3 10457
f4dc4d17 10458 gdb_assert (per_cu->is_debug_types);
0186c6a7 10459 sig_type = (struct signatured_type *) per_cu;
3019eac3 10460
f4dc4d17 10461 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 10462
f4dc4d17 10463 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10464 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
10465 if (sig_type->type_unit_group == NULL)
10466 sig_type->type_unit_group = get_type_unit_group (cu, attr);
10467 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10468
10469 /* If we've already processed this stmt_list there's no real need to
10470 do it again, we could fake it and just recreate the part we need
10471 (file name,index -> symtab mapping). If data shows this optimization
10472 is useful we can do it then. */
43f3e411 10473 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
10474
10475 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10476 debug info. */
fff8551c 10477 line_header_up lh;
f4dc4d17 10478 if (attr != NULL)
3019eac3 10479 {
9c541725 10480 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
10481 lh = dwarf_decode_line_header (line_offset, cu);
10482 }
10483 if (lh == NULL)
10484 {
10485 if (first_time)
10486 dwarf2_start_symtab (cu, "", NULL, 0);
10487 else
10488 {
10489 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 10490 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 10491 }
f4dc4d17 10492 return;
3019eac3
DE
10493 }
10494
4c8aa72d
PA
10495 cu->line_header = lh.release ();
10496 cu->line_header_die_owner = die;
3019eac3 10497
f4dc4d17
DE
10498 if (first_time)
10499 {
43f3e411 10500 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 10501
1fd60fc0
DE
10502 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10503 still initializing it, and our caller (a few levels up)
10504 process_full_type_unit still needs to know if this is the first
10505 time. */
10506
4c8aa72d
PA
10507 tu_group->num_symtabs = cu->line_header->file_names.size ();
10508 tu_group->symtabs = XNEWVEC (struct symtab *,
10509 cu->line_header->file_names.size ());
3019eac3 10510
4c8aa72d 10511 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 10512 {
4c8aa72d 10513 file_entry &fe = cu->line_header->file_names[i];
3019eac3 10514
4c8aa72d 10515 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 10516
f4dc4d17
DE
10517 if (current_subfile->symtab == NULL)
10518 {
4c8aa72d
PA
10519 /* NOTE: start_subfile will recognize when it's been
10520 passed a file it has already seen. So we can't
10521 assume there's a simple mapping from
10522 cu->line_header->file_names to subfiles, plus
10523 cu->line_header->file_names may contain dups. */
43f3e411
DE
10524 current_subfile->symtab
10525 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
10526 }
10527
8c43009f
PA
10528 fe.symtab = current_subfile->symtab;
10529 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
10530 }
10531 }
10532 else
3019eac3 10533 {
0ab9ce85 10534 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 10535
4c8aa72d 10536 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 10537 {
4c8aa72d 10538 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 10539
4c8aa72d 10540 fe.symtab = tu_group->symtabs[i];
f4dc4d17 10541 }
3019eac3
DE
10542 }
10543
f4dc4d17
DE
10544 /* The main symtab is allocated last. Type units don't have DW_AT_name
10545 so they don't have a "real" (so to speak) symtab anyway.
10546 There is later code that will assign the main symtab to all symbols
10547 that don't have one. We need to handle the case of a symbol with a
10548 missing symtab (DW_AT_decl_file) anyway. */
10549}
3019eac3 10550
f4dc4d17
DE
10551/* Process DW_TAG_type_unit.
10552 For TUs we want to skip the first top level sibling if it's not the
10553 actual type being defined by this TU. In this case the first top
10554 level sibling is there to provide context only. */
3019eac3 10555
f4dc4d17
DE
10556static void
10557read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10558{
10559 struct die_info *child_die;
3019eac3 10560
f4dc4d17
DE
10561 prepare_one_comp_unit (cu, die, language_minimal);
10562
10563 /* Initialize (or reinitialize) the machinery for building symtabs.
10564 We do this before processing child DIEs, so that the line header table
10565 is available for DW_AT_decl_file. */
10566 setup_type_unit_groups (die, cu);
10567
10568 if (die->child != NULL)
10569 {
10570 child_die = die->child;
10571 while (child_die && child_die->tag)
10572 {
10573 process_die (child_die, cu);
10574 child_die = sibling_die (child_die);
10575 }
10576 }
3019eac3
DE
10577}
10578\f
80626a55
DE
10579/* DWO/DWP files.
10580
10581 http://gcc.gnu.org/wiki/DebugFission
10582 http://gcc.gnu.org/wiki/DebugFissionDWP
10583
10584 To simplify handling of both DWO files ("object" files with the DWARF info)
10585 and DWP files (a file with the DWOs packaged up into one file), we treat
10586 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10587
10588static hashval_t
10589hash_dwo_file (const void *item)
10590{
9a3c8263 10591 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10592 hashval_t hash;
3019eac3 10593
a2ce51a0
DE
10594 hash = htab_hash_string (dwo_file->dwo_name);
10595 if (dwo_file->comp_dir != NULL)
10596 hash += htab_hash_string (dwo_file->comp_dir);
10597 return hash;
3019eac3
DE
10598}
10599
10600static int
10601eq_dwo_file (const void *item_lhs, const void *item_rhs)
10602{
9a3c8263
SM
10603 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10604 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 10605
a2ce51a0
DE
10606 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10607 return 0;
10608 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10609 return lhs->comp_dir == rhs->comp_dir;
10610 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
10611}
10612
10613/* Allocate a hash table for DWO files. */
10614
10615static htab_t
10616allocate_dwo_file_hash_table (void)
10617{
10618 struct objfile *objfile = dwarf2_per_objfile->objfile;
10619
10620 return htab_create_alloc_ex (41,
10621 hash_dwo_file,
10622 eq_dwo_file,
10623 NULL,
10624 &objfile->objfile_obstack,
10625 hashtab_obstack_allocate,
10626 dummy_obstack_deallocate);
10627}
10628
80626a55
DE
10629/* Lookup DWO file DWO_NAME. */
10630
10631static void **
0ac5b59e 10632lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
10633{
10634 struct dwo_file find_entry;
10635 void **slot;
10636
10637 if (dwarf2_per_objfile->dwo_files == NULL)
10638 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
10639
10640 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
10641 find_entry.dwo_name = dwo_name;
10642 find_entry.comp_dir = comp_dir;
80626a55
DE
10643 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
10644
10645 return slot;
10646}
10647
3019eac3
DE
10648static hashval_t
10649hash_dwo_unit (const void *item)
10650{
9a3c8263 10651 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
10652
10653 /* This drops the top 32 bits of the id, but is ok for a hash. */
10654 return dwo_unit->signature;
10655}
10656
10657static int
10658eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10659{
9a3c8263
SM
10660 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10661 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
10662
10663 /* The signature is assumed to be unique within the DWO file.
10664 So while object file CU dwo_id's always have the value zero,
10665 that's OK, assuming each object file DWO file has only one CU,
10666 and that's the rule for now. */
10667 return lhs->signature == rhs->signature;
10668}
10669
10670/* Allocate a hash table for DWO CUs,TUs.
10671 There is one of these tables for each of CUs,TUs for each DWO file. */
10672
10673static htab_t
10674allocate_dwo_unit_table (struct objfile *objfile)
10675{
10676 /* Start out with a pretty small number.
10677 Generally DWO files contain only one CU and maybe some TUs. */
10678 return htab_create_alloc_ex (3,
10679 hash_dwo_unit,
10680 eq_dwo_unit,
10681 NULL,
10682 &objfile->objfile_obstack,
10683 hashtab_obstack_allocate,
10684 dummy_obstack_deallocate);
10685}
10686
80626a55 10687/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 10688
19c3d4c9 10689struct create_dwo_cu_data
3019eac3
DE
10690{
10691 struct dwo_file *dwo_file;
19c3d4c9 10692 struct dwo_unit dwo_unit;
3019eac3
DE
10693};
10694
19c3d4c9 10695/* die_reader_func for create_dwo_cu. */
3019eac3
DE
10696
10697static void
19c3d4c9
DE
10698create_dwo_cu_reader (const struct die_reader_specs *reader,
10699 const gdb_byte *info_ptr,
10700 struct die_info *comp_unit_die,
10701 int has_children,
10702 void *datap)
3019eac3
DE
10703{
10704 struct dwarf2_cu *cu = reader->cu;
9c541725 10705 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 10706 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 10707 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 10708 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 10709 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 10710 struct attribute *attr;
3019eac3
DE
10711
10712 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
10713 if (attr == NULL)
10714 {
19c3d4c9
DE
10715 complaint (&symfile_complaints,
10716 _("Dwarf Error: debug entry at offset 0x%x is missing"
10717 " its dwo_id [in module %s]"),
9c541725 10718 to_underlying (sect_off), dwo_file->dwo_name);
3019eac3
DE
10719 return;
10720 }
10721
3019eac3
DE
10722 dwo_unit->dwo_file = dwo_file;
10723 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 10724 dwo_unit->section = section;
9c541725 10725 dwo_unit->sect_off = sect_off;
3019eac3
DE
10726 dwo_unit->length = cu->per_cu->length;
10727
b4f54984 10728 if (dwarf_read_debug)
4031ecc5 10729 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9c541725
PA
10730 to_underlying (sect_off),
10731 hex_string (dwo_unit->signature));
3019eac3
DE
10732}
10733
33c5cd75 10734/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 10735 Note: This function processes DWO files only, not DWP files. */
3019eac3 10736
33c5cd75
DB
10737static void
10738create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
10739 htab_t &cus_htab)
3019eac3
DE
10740{
10741 struct objfile *objfile = dwarf2_per_objfile->objfile;
33c5cd75 10742 const struct dwarf2_section_info *abbrev_section = &dwo_file.sections.abbrev;
d521ce57 10743 const gdb_byte *info_ptr, *end_ptr;
3019eac3 10744
33c5cd75
DB
10745 dwarf2_read_section (objfile, &section);
10746 info_ptr = section.buffer;
3019eac3
DE
10747
10748 if (info_ptr == NULL)
33c5cd75 10749 return;
3019eac3 10750
b4f54984 10751 if (dwarf_read_debug)
19c3d4c9
DE
10752 {
10753 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
10754 get_section_name (&section),
10755 get_section_file_name (&section));
19c3d4c9 10756 }
3019eac3 10757
33c5cd75 10758 end_ptr = info_ptr + section.size;
3019eac3
DE
10759 while (info_ptr < end_ptr)
10760 {
10761 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
10762 struct create_dwo_cu_data create_dwo_cu_data;
10763 struct dwo_unit *dwo_unit;
10764 void **slot;
10765 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 10766
19c3d4c9
DE
10767 memset (&create_dwo_cu_data.dwo_unit, 0,
10768 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
10769 memset (&per_cu, 0, sizeof (per_cu));
10770 per_cu.objfile = objfile;
10771 per_cu.is_debug_types = 0;
33c5cd75
DB
10772 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10773 per_cu.section = &section;
c5ed0576 10774 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
10775
10776 init_cutu_and_read_dies_no_follow (
10777 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
10778 info_ptr += per_cu.length;
10779
10780 // If the unit could not be parsed, skip it.
10781 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
10782 continue;
3019eac3 10783
33c5cd75
DB
10784 if (cus_htab == NULL)
10785 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 10786
33c5cd75
DB
10787 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10788 *dwo_unit = create_dwo_cu_data.dwo_unit;
10789 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
10790 gdb_assert (slot != NULL);
10791 if (*slot != NULL)
19c3d4c9 10792 {
33c5cd75
DB
10793 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10794 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 10795
33c5cd75
DB
10796 complaint (&symfile_complaints,
10797 _("debug cu entry at offset 0x%x is duplicate to"
10798 " the entry at offset 0x%x, signature %s"),
10799 to_underlying (sect_off), to_underlying (dup_sect_off),
10800 hex_string (dwo_unit->signature));
19c3d4c9 10801 }
33c5cd75 10802 *slot = (void *)dwo_unit;
3019eac3 10803 }
3019eac3
DE
10804}
10805
80626a55
DE
10806/* DWP file .debug_{cu,tu}_index section format:
10807 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
10808
d2415c6c
DE
10809 DWP Version 1:
10810
80626a55
DE
10811 Both index sections have the same format, and serve to map a 64-bit
10812 signature to a set of section numbers. Each section begins with a header,
10813 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10814 indexes, and a pool of 32-bit section numbers. The index sections will be
10815 aligned at 8-byte boundaries in the file.
10816
d2415c6c
DE
10817 The index section header consists of:
10818
10819 V, 32 bit version number
10820 -, 32 bits unused
10821 N, 32 bit number of compilation units or type units in the index
10822 M, 32 bit number of slots in the hash table
80626a55 10823
d2415c6c 10824 Numbers are recorded using the byte order of the application binary.
80626a55 10825
d2415c6c
DE
10826 The hash table begins at offset 16 in the section, and consists of an array
10827 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10828 order of the application binary). Unused slots in the hash table are 0.
10829 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 10830
d2415c6c
DE
10831 The parallel table begins immediately after the hash table
10832 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10833 array of 32-bit indexes (using the byte order of the application binary),
10834 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10835 table contains a 32-bit index into the pool of section numbers. For unused
10836 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 10837
73869dc2
DE
10838 The pool of section numbers begins immediately following the hash table
10839 (at offset 16 + 12 * M from the beginning of the section). The pool of
10840 section numbers consists of an array of 32-bit words (using the byte order
10841 of the application binary). Each item in the array is indexed starting
10842 from 0. The hash table entry provides the index of the first section
10843 number in the set. Additional section numbers in the set follow, and the
10844 set is terminated by a 0 entry (section number 0 is not used in ELF).
10845
10846 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10847 section must be the first entry in the set, and the .debug_abbrev.dwo must
10848 be the second entry. Other members of the set may follow in any order.
10849
10850 ---
10851
10852 DWP Version 2:
10853
10854 DWP Version 2 combines all the .debug_info, etc. sections into one,
10855 and the entries in the index tables are now offsets into these sections.
10856 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10857 section.
10858
10859 Index Section Contents:
10860 Header
10861 Hash Table of Signatures dwp_hash_table.hash_table
10862 Parallel Table of Indices dwp_hash_table.unit_table
10863 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
10864 Table of Section Sizes dwp_hash_table.v2.sizes
10865
10866 The index section header consists of:
10867
10868 V, 32 bit version number
10869 L, 32 bit number of columns in the table of section offsets
10870 N, 32 bit number of compilation units or type units in the index
10871 M, 32 bit number of slots in the hash table
10872
10873 Numbers are recorded using the byte order of the application binary.
10874
10875 The hash table has the same format as version 1.
10876 The parallel table of indices has the same format as version 1,
10877 except that the entries are origin-1 indices into the table of sections
10878 offsets and the table of section sizes.
10879
10880 The table of offsets begins immediately following the parallel table
10881 (at offset 16 + 12 * M from the beginning of the section). The table is
10882 a two-dimensional array of 32-bit words (using the byte order of the
10883 application binary), with L columns and N+1 rows, in row-major order.
10884 Each row in the array is indexed starting from 0. The first row provides
10885 a key to the remaining rows: each column in this row provides an identifier
10886 for a debug section, and the offsets in the same column of subsequent rows
10887 refer to that section. The section identifiers are:
10888
10889 DW_SECT_INFO 1 .debug_info.dwo
10890 DW_SECT_TYPES 2 .debug_types.dwo
10891 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10892 DW_SECT_LINE 4 .debug_line.dwo
10893 DW_SECT_LOC 5 .debug_loc.dwo
10894 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10895 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10896 DW_SECT_MACRO 8 .debug_macro.dwo
10897
10898 The offsets provided by the CU and TU index sections are the base offsets
10899 for the contributions made by each CU or TU to the corresponding section
10900 in the package file. Each CU and TU header contains an abbrev_offset
10901 field, used to find the abbreviations table for that CU or TU within the
10902 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10903 be interpreted as relative to the base offset given in the index section.
10904 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10905 should be interpreted as relative to the base offset for .debug_line.dwo,
10906 and offsets into other debug sections obtained from DWARF attributes should
10907 also be interpreted as relative to the corresponding base offset.
10908
10909 The table of sizes begins immediately following the table of offsets.
10910 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10911 with L columns and N rows, in row-major order. Each row in the array is
10912 indexed starting from 1 (row 0 is shared by the two tables).
10913
10914 ---
10915
10916 Hash table lookup is handled the same in version 1 and 2:
10917
10918 We assume that N and M will not exceed 2^32 - 1.
10919 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10920
d2415c6c
DE
10921 Given a 64-bit compilation unit signature or a type signature S, an entry
10922 in the hash table is located as follows:
80626a55 10923
d2415c6c
DE
10924 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10925 the low-order k bits all set to 1.
80626a55 10926
d2415c6c 10927 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 10928
d2415c6c
DE
10929 3) If the hash table entry at index H matches the signature, use that
10930 entry. If the hash table entry at index H is unused (all zeroes),
10931 terminate the search: the signature is not present in the table.
80626a55 10932
d2415c6c 10933 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 10934
d2415c6c 10935 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 10936 to stop at an unused slot or find the match. */
80626a55
DE
10937
10938/* Create a hash table to map DWO IDs to their CU/TU entry in
10939 .debug_{info,types}.dwo in DWP_FILE.
10940 Returns NULL if there isn't one.
10941 Note: This function processes DWP files only, not DWO files. */
10942
10943static struct dwp_hash_table *
10944create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
10945{
10946 struct objfile *objfile = dwarf2_per_objfile->objfile;
10947 bfd *dbfd = dwp_file->dbfd;
948f8e3d 10948 const gdb_byte *index_ptr, *index_end;
80626a55 10949 struct dwarf2_section_info *index;
73869dc2 10950 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
10951 struct dwp_hash_table *htab;
10952
10953 if (is_debug_types)
10954 index = &dwp_file->sections.tu_index;
10955 else
10956 index = &dwp_file->sections.cu_index;
10957
10958 if (dwarf2_section_empty_p (index))
10959 return NULL;
10960 dwarf2_read_section (objfile, index);
10961
10962 index_ptr = index->buffer;
10963 index_end = index_ptr + index->size;
10964
10965 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
10966 index_ptr += 4;
10967 if (version == 2)
10968 nr_columns = read_4_bytes (dbfd, index_ptr);
10969 else
10970 nr_columns = 0;
10971 index_ptr += 4;
80626a55
DE
10972 nr_units = read_4_bytes (dbfd, index_ptr);
10973 index_ptr += 4;
10974 nr_slots = read_4_bytes (dbfd, index_ptr);
10975 index_ptr += 4;
10976
73869dc2 10977 if (version != 1 && version != 2)
80626a55 10978 {
21aa081e 10979 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 10980 " [in module %s]"),
21aa081e 10981 pulongest (version), dwp_file->name);
80626a55
DE
10982 }
10983 if (nr_slots != (nr_slots & -nr_slots))
10984 {
21aa081e 10985 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 10986 " is not power of 2 [in module %s]"),
21aa081e 10987 pulongest (nr_slots), dwp_file->name);
80626a55
DE
10988 }
10989
10990 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
10991 htab->version = version;
10992 htab->nr_columns = nr_columns;
80626a55
DE
10993 htab->nr_units = nr_units;
10994 htab->nr_slots = nr_slots;
10995 htab->hash_table = index_ptr;
10996 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
10997
10998 /* Exit early if the table is empty. */
10999 if (nr_slots == 0 || nr_units == 0
11000 || (version == 2 && nr_columns == 0))
11001 {
11002 /* All must be zero. */
11003 if (nr_slots != 0 || nr_units != 0
11004 || (version == 2 && nr_columns != 0))
11005 {
11006 complaint (&symfile_complaints,
11007 _("Empty DWP but nr_slots,nr_units,nr_columns not"
11008 " all zero [in modules %s]"),
11009 dwp_file->name);
11010 }
11011 return htab;
11012 }
11013
11014 if (version == 1)
11015 {
11016 htab->section_pool.v1.indices =
11017 htab->unit_table + sizeof (uint32_t) * nr_slots;
11018 /* It's harder to decide whether the section is too small in v1.
11019 V1 is deprecated anyway so we punt. */
11020 }
11021 else
11022 {
11023 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11024 int *ids = htab->section_pool.v2.section_ids;
11025 /* Reverse map for error checking. */
11026 int ids_seen[DW_SECT_MAX + 1];
11027 int i;
11028
11029 if (nr_columns < 2)
11030 {
11031 error (_("Dwarf Error: bad DWP hash table, too few columns"
11032 " in section table [in module %s]"),
11033 dwp_file->name);
11034 }
11035 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11036 {
11037 error (_("Dwarf Error: bad DWP hash table, too many columns"
11038 " in section table [in module %s]"),
11039 dwp_file->name);
11040 }
11041 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
11042 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
11043 for (i = 0; i < nr_columns; ++i)
11044 {
11045 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11046
11047 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11048 {
11049 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11050 " in section table [in module %s]"),
11051 id, dwp_file->name);
11052 }
11053 if (ids_seen[id] != -1)
11054 {
11055 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11056 " id %d in section table [in module %s]"),
11057 id, dwp_file->name);
11058 }
11059 ids_seen[id] = i;
11060 ids[i] = id;
11061 }
11062 /* Must have exactly one info or types section. */
11063 if (((ids_seen[DW_SECT_INFO] != -1)
11064 + (ids_seen[DW_SECT_TYPES] != -1))
11065 != 1)
11066 {
11067 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11068 " DWO info/types section [in module %s]"),
11069 dwp_file->name);
11070 }
11071 /* Must have an abbrev section. */
11072 if (ids_seen[DW_SECT_ABBREV] == -1)
11073 {
11074 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11075 " section [in module %s]"),
11076 dwp_file->name);
11077 }
11078 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11079 htab->section_pool.v2.sizes =
11080 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11081 * nr_units * nr_columns);
11082 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11083 * nr_units * nr_columns))
11084 > index_end)
11085 {
11086 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11087 " [in module %s]"),
11088 dwp_file->name);
11089 }
11090 }
80626a55
DE
11091
11092 return htab;
11093}
11094
11095/* Update SECTIONS with the data from SECTP.
11096
11097 This function is like the other "locate" section routines that are
11098 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11099 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11100
11101 The result is non-zero for success, or zero if an error was found. */
11102
11103static int
73869dc2
DE
11104locate_v1_virtual_dwo_sections (asection *sectp,
11105 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11106{
11107 const struct dwop_section_names *names = &dwop_section_names;
11108
11109 if (section_is_p (sectp->name, &names->abbrev_dwo))
11110 {
11111 /* There can be only one. */
049412e3 11112 if (sections->abbrev.s.section != NULL)
80626a55 11113 return 0;
049412e3 11114 sections->abbrev.s.section = sectp;
80626a55
DE
11115 sections->abbrev.size = bfd_get_section_size (sectp);
11116 }
11117 else if (section_is_p (sectp->name, &names->info_dwo)
11118 || section_is_p (sectp->name, &names->types_dwo))
11119 {
11120 /* There can be only one. */
049412e3 11121 if (sections->info_or_types.s.section != NULL)
80626a55 11122 return 0;
049412e3 11123 sections->info_or_types.s.section = sectp;
80626a55
DE
11124 sections->info_or_types.size = bfd_get_section_size (sectp);
11125 }
11126 else if (section_is_p (sectp->name, &names->line_dwo))
11127 {
11128 /* There can be only one. */
049412e3 11129 if (sections->line.s.section != NULL)
80626a55 11130 return 0;
049412e3 11131 sections->line.s.section = sectp;
80626a55
DE
11132 sections->line.size = bfd_get_section_size (sectp);
11133 }
11134 else if (section_is_p (sectp->name, &names->loc_dwo))
11135 {
11136 /* There can be only one. */
049412e3 11137 if (sections->loc.s.section != NULL)
80626a55 11138 return 0;
049412e3 11139 sections->loc.s.section = sectp;
80626a55
DE
11140 sections->loc.size = bfd_get_section_size (sectp);
11141 }
11142 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11143 {
11144 /* There can be only one. */
049412e3 11145 if (sections->macinfo.s.section != NULL)
80626a55 11146 return 0;
049412e3 11147 sections->macinfo.s.section = sectp;
80626a55
DE
11148 sections->macinfo.size = bfd_get_section_size (sectp);
11149 }
11150 else if (section_is_p (sectp->name, &names->macro_dwo))
11151 {
11152 /* There can be only one. */
049412e3 11153 if (sections->macro.s.section != NULL)
80626a55 11154 return 0;
049412e3 11155 sections->macro.s.section = sectp;
80626a55
DE
11156 sections->macro.size = bfd_get_section_size (sectp);
11157 }
11158 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11159 {
11160 /* There can be only one. */
049412e3 11161 if (sections->str_offsets.s.section != NULL)
80626a55 11162 return 0;
049412e3 11163 sections->str_offsets.s.section = sectp;
80626a55
DE
11164 sections->str_offsets.size = bfd_get_section_size (sectp);
11165 }
11166 else
11167 {
11168 /* No other kind of section is valid. */
11169 return 0;
11170 }
11171
11172 return 1;
11173}
11174
73869dc2
DE
11175/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11176 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11177 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11178 This is for DWP version 1 files. */
80626a55
DE
11179
11180static struct dwo_unit *
73869dc2
DE
11181create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
11182 uint32_t unit_index,
11183 const char *comp_dir,
11184 ULONGEST signature, int is_debug_types)
80626a55
DE
11185{
11186 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
11187 const struct dwp_hash_table *dwp_htab =
11188 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
11189 bfd *dbfd = dwp_file->dbfd;
11190 const char *kind = is_debug_types ? "TU" : "CU";
11191 struct dwo_file *dwo_file;
11192 struct dwo_unit *dwo_unit;
73869dc2 11193 struct virtual_v1_dwo_sections sections;
80626a55 11194 void **dwo_file_slot;
80626a55
DE
11195 int i;
11196
73869dc2
DE
11197 gdb_assert (dwp_file->version == 1);
11198
b4f54984 11199 if (dwarf_read_debug)
80626a55 11200 {
73869dc2 11201 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11202 kind,
73869dc2 11203 pulongest (unit_index), hex_string (signature),
80626a55
DE
11204 dwp_file->name);
11205 }
11206
19ac8c2e 11207 /* Fetch the sections of this DWO unit.
80626a55
DE
11208 Put a limit on the number of sections we look for so that bad data
11209 doesn't cause us to loop forever. */
11210
73869dc2 11211#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11212 (1 /* .debug_info or .debug_types */ \
11213 + 1 /* .debug_abbrev */ \
11214 + 1 /* .debug_line */ \
11215 + 1 /* .debug_loc */ \
11216 + 1 /* .debug_str_offsets */ \
19ac8c2e 11217 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11218 + 1 /* trailing zero */)
11219
11220 memset (&sections, 0, sizeof (sections));
80626a55 11221
73869dc2 11222 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11223 {
11224 asection *sectp;
11225 uint32_t section_nr =
11226 read_4_bytes (dbfd,
73869dc2
DE
11227 dwp_htab->section_pool.v1.indices
11228 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11229
11230 if (section_nr == 0)
11231 break;
11232 if (section_nr >= dwp_file->num_sections)
11233 {
11234 error (_("Dwarf Error: bad DWP hash table, section number too large"
11235 " [in module %s]"),
11236 dwp_file->name);
11237 }
11238
11239 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11240 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11241 {
11242 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11243 " [in module %s]"),
11244 dwp_file->name);
11245 }
11246 }
11247
11248 if (i < 2
a32a8923
DE
11249 || dwarf2_section_empty_p (&sections.info_or_types)
11250 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
11251 {
11252 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11253 " [in module %s]"),
11254 dwp_file->name);
11255 }
73869dc2 11256 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11257 {
11258 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11259 " [in module %s]"),
11260 dwp_file->name);
11261 }
11262
11263 /* It's easier for the rest of the code if we fake a struct dwo_file and
11264 have dwo_unit "live" in that. At least for now.
11265
11266 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11267 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11268 file, we can combine them back into a virtual DWO file to save space
11269 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11270 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11271
791afaa2
TT
11272 std::string virtual_dwo_name =
11273 string_printf ("virtual-dwo/%d-%d-%d-%d",
11274 get_section_id (&sections.abbrev),
11275 get_section_id (&sections.line),
11276 get_section_id (&sections.loc),
11277 get_section_id (&sections.str_offsets));
80626a55 11278 /* Can we use an existing virtual DWO file? */
791afaa2 11279 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
80626a55
DE
11280 /* Create one if necessary. */
11281 if (*dwo_file_slot == NULL)
11282 {
b4f54984 11283 if (dwarf_read_debug)
80626a55
DE
11284 {
11285 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11286 virtual_dwo_name.c_str ());
80626a55
DE
11287 }
11288 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
11289 dwo_file->dwo_name
11290 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
11291 virtual_dwo_name.c_str (),
11292 virtual_dwo_name.size ());
0ac5b59e 11293 dwo_file->comp_dir = comp_dir;
80626a55
DE
11294 dwo_file->sections.abbrev = sections.abbrev;
11295 dwo_file->sections.line = sections.line;
11296 dwo_file->sections.loc = sections.loc;
11297 dwo_file->sections.macinfo = sections.macinfo;
11298 dwo_file->sections.macro = sections.macro;
11299 dwo_file->sections.str_offsets = sections.str_offsets;
11300 /* The "str" section is global to the entire DWP file. */
11301 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11302 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11303 there's no need to record it in dwo_file.
11304 Also, we can't simply record type sections in dwo_file because
11305 we record a pointer into the vector in dwo_unit. As we collect more
11306 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11307 for it, invalidating all copies of pointers into the previous
11308 contents. */
80626a55
DE
11309 *dwo_file_slot = dwo_file;
11310 }
11311 else
11312 {
b4f54984 11313 if (dwarf_read_debug)
80626a55
DE
11314 {
11315 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11316 virtual_dwo_name.c_str ());
80626a55 11317 }
9a3c8263 11318 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11319 }
80626a55
DE
11320
11321 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11322 dwo_unit->dwo_file = dwo_file;
11323 dwo_unit->signature = signature;
8d749320
SM
11324 dwo_unit->section =
11325 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 11326 *dwo_unit->section = sections.info_or_types;
57d63ce2 11327 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11328
11329 return dwo_unit;
11330}
11331
73869dc2
DE
11332/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11333 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11334 piece within that section used by a TU/CU, return a virtual section
11335 of just that piece. */
11336
11337static struct dwarf2_section_info
11338create_dwp_v2_section (struct dwarf2_section_info *section,
11339 bfd_size_type offset, bfd_size_type size)
11340{
11341 struct dwarf2_section_info result;
11342 asection *sectp;
11343
11344 gdb_assert (section != NULL);
11345 gdb_assert (!section->is_virtual);
11346
11347 memset (&result, 0, sizeof (result));
11348 result.s.containing_section = section;
11349 result.is_virtual = 1;
11350
11351 if (size == 0)
11352 return result;
11353
11354 sectp = get_section_bfd_section (section);
11355
11356 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11357 bounds of the real section. This is a pretty-rare event, so just
11358 flag an error (easier) instead of a warning and trying to cope. */
11359 if (sectp == NULL
11360 || offset + size > bfd_get_section_size (sectp))
11361 {
11362 bfd *abfd = sectp->owner;
11363
11364 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11365 " in section %s [in module %s]"),
11366 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
11367 objfile_name (dwarf2_per_objfile->objfile));
11368 }
11369
11370 result.virtual_offset = offset;
11371 result.size = size;
11372 return result;
11373}
11374
11375/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11376 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11377 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11378 This is for DWP version 2 files. */
11379
11380static struct dwo_unit *
11381create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
11382 uint32_t unit_index,
11383 const char *comp_dir,
11384 ULONGEST signature, int is_debug_types)
11385{
11386 struct objfile *objfile = dwarf2_per_objfile->objfile;
11387 const struct dwp_hash_table *dwp_htab =
11388 is_debug_types ? dwp_file->tus : dwp_file->cus;
11389 bfd *dbfd = dwp_file->dbfd;
11390 const char *kind = is_debug_types ? "TU" : "CU";
11391 struct dwo_file *dwo_file;
11392 struct dwo_unit *dwo_unit;
11393 struct virtual_v2_dwo_sections sections;
11394 void **dwo_file_slot;
73869dc2
DE
11395 int i;
11396
11397 gdb_assert (dwp_file->version == 2);
11398
b4f54984 11399 if (dwarf_read_debug)
73869dc2
DE
11400 {
11401 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11402 kind,
11403 pulongest (unit_index), hex_string (signature),
11404 dwp_file->name);
11405 }
11406
11407 /* Fetch the section offsets of this DWO unit. */
11408
11409 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11410
11411 for (i = 0; i < dwp_htab->nr_columns; ++i)
11412 {
11413 uint32_t offset = read_4_bytes (dbfd,
11414 dwp_htab->section_pool.v2.offsets
11415 + (((unit_index - 1) * dwp_htab->nr_columns
11416 + i)
11417 * sizeof (uint32_t)));
11418 uint32_t size = read_4_bytes (dbfd,
11419 dwp_htab->section_pool.v2.sizes
11420 + (((unit_index - 1) * dwp_htab->nr_columns
11421 + i)
11422 * sizeof (uint32_t)));
11423
11424 switch (dwp_htab->section_pool.v2.section_ids[i])
11425 {
11426 case DW_SECT_INFO:
11427 case DW_SECT_TYPES:
11428 sections.info_or_types_offset = offset;
11429 sections.info_or_types_size = size;
11430 break;
11431 case DW_SECT_ABBREV:
11432 sections.abbrev_offset = offset;
11433 sections.abbrev_size = size;
11434 break;
11435 case DW_SECT_LINE:
11436 sections.line_offset = offset;
11437 sections.line_size = size;
11438 break;
11439 case DW_SECT_LOC:
11440 sections.loc_offset = offset;
11441 sections.loc_size = size;
11442 break;
11443 case DW_SECT_STR_OFFSETS:
11444 sections.str_offsets_offset = offset;
11445 sections.str_offsets_size = size;
11446 break;
11447 case DW_SECT_MACINFO:
11448 sections.macinfo_offset = offset;
11449 sections.macinfo_size = size;
11450 break;
11451 case DW_SECT_MACRO:
11452 sections.macro_offset = offset;
11453 sections.macro_size = size;
11454 break;
11455 }
11456 }
11457
11458 /* It's easier for the rest of the code if we fake a struct dwo_file and
11459 have dwo_unit "live" in that. At least for now.
11460
11461 The DWP file can be made up of a random collection of CUs and TUs.
11462 However, for each CU + set of TUs that came from the same original DWO
11463 file, we can combine them back into a virtual DWO file to save space
11464 (fewer struct dwo_file objects to allocate). Remember that for really
11465 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11466
791afaa2
TT
11467 std::string virtual_dwo_name =
11468 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11469 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11470 (long) (sections.line_size ? sections.line_offset : 0),
11471 (long) (sections.loc_size ? sections.loc_offset : 0),
11472 (long) (sections.str_offsets_size
11473 ? sections.str_offsets_offset : 0));
73869dc2 11474 /* Can we use an existing virtual DWO file? */
791afaa2 11475 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
73869dc2
DE
11476 /* Create one if necessary. */
11477 if (*dwo_file_slot == NULL)
11478 {
b4f54984 11479 if (dwarf_read_debug)
73869dc2
DE
11480 {
11481 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11482 virtual_dwo_name.c_str ());
73869dc2
DE
11483 }
11484 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
11485 dwo_file->dwo_name
11486 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
11487 virtual_dwo_name.c_str (),
11488 virtual_dwo_name.size ());
73869dc2
DE
11489 dwo_file->comp_dir = comp_dir;
11490 dwo_file->sections.abbrev =
11491 create_dwp_v2_section (&dwp_file->sections.abbrev,
11492 sections.abbrev_offset, sections.abbrev_size);
11493 dwo_file->sections.line =
11494 create_dwp_v2_section (&dwp_file->sections.line,
11495 sections.line_offset, sections.line_size);
11496 dwo_file->sections.loc =
11497 create_dwp_v2_section (&dwp_file->sections.loc,
11498 sections.loc_offset, sections.loc_size);
11499 dwo_file->sections.macinfo =
11500 create_dwp_v2_section (&dwp_file->sections.macinfo,
11501 sections.macinfo_offset, sections.macinfo_size);
11502 dwo_file->sections.macro =
11503 create_dwp_v2_section (&dwp_file->sections.macro,
11504 sections.macro_offset, sections.macro_size);
11505 dwo_file->sections.str_offsets =
11506 create_dwp_v2_section (&dwp_file->sections.str_offsets,
11507 sections.str_offsets_offset,
11508 sections.str_offsets_size);
11509 /* The "str" section is global to the entire DWP file. */
11510 dwo_file->sections.str = dwp_file->sections.str;
11511 /* The info or types section is assigned below to dwo_unit,
11512 there's no need to record it in dwo_file.
11513 Also, we can't simply record type sections in dwo_file because
11514 we record a pointer into the vector in dwo_unit. As we collect more
11515 types we'll grow the vector and eventually have to reallocate space
11516 for it, invalidating all copies of pointers into the previous
11517 contents. */
11518 *dwo_file_slot = dwo_file;
11519 }
11520 else
11521 {
b4f54984 11522 if (dwarf_read_debug)
73869dc2
DE
11523 {
11524 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11525 virtual_dwo_name.c_str ());
73869dc2 11526 }
9a3c8263 11527 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11528 }
73869dc2
DE
11529
11530 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11531 dwo_unit->dwo_file = dwo_file;
11532 dwo_unit->signature = signature;
8d749320
SM
11533 dwo_unit->section =
11534 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
73869dc2
DE
11535 *dwo_unit->section = create_dwp_v2_section (is_debug_types
11536 ? &dwp_file->sections.types
11537 : &dwp_file->sections.info,
11538 sections.info_or_types_offset,
11539 sections.info_or_types_size);
11540 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11541
11542 return dwo_unit;
11543}
11544
57d63ce2
DE
11545/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11546 Returns NULL if the signature isn't found. */
80626a55
DE
11547
11548static struct dwo_unit *
57d63ce2
DE
11549lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
11550 ULONGEST signature, int is_debug_types)
80626a55 11551{
57d63ce2
DE
11552 const struct dwp_hash_table *dwp_htab =
11553 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 11554 bfd *dbfd = dwp_file->dbfd;
57d63ce2 11555 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11556 uint32_t hash = signature & mask;
11557 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11558 unsigned int i;
11559 void **slot;
870f88f7 11560 struct dwo_unit find_dwo_cu;
80626a55
DE
11561
11562 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11563 find_dwo_cu.signature = signature;
19ac8c2e
DE
11564 slot = htab_find_slot (is_debug_types
11565 ? dwp_file->loaded_tus
11566 : dwp_file->loaded_cus,
11567 &find_dwo_cu, INSERT);
80626a55
DE
11568
11569 if (*slot != NULL)
9a3c8263 11570 return (struct dwo_unit *) *slot;
80626a55
DE
11571
11572 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11573 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11574 {
11575 ULONGEST signature_in_table;
11576
11577 signature_in_table =
57d63ce2 11578 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
11579 if (signature_in_table == signature)
11580 {
57d63ce2
DE
11581 uint32_t unit_index =
11582 read_4_bytes (dbfd,
11583 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 11584
73869dc2
DE
11585 if (dwp_file->version == 1)
11586 {
11587 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
11588 comp_dir, signature,
11589 is_debug_types);
11590 }
11591 else
11592 {
11593 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
11594 comp_dir, signature,
11595 is_debug_types);
11596 }
9a3c8263 11597 return (struct dwo_unit *) *slot;
80626a55
DE
11598 }
11599 if (signature_in_table == 0)
11600 return NULL;
11601 hash = (hash + hash2) & mask;
11602 }
11603
11604 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11605 " [in module %s]"),
11606 dwp_file->name);
11607}
11608
ab5088bf 11609/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
11610 Open the file specified by FILE_NAME and hand it off to BFD for
11611 preliminary analysis. Return a newly initialized bfd *, which
11612 includes a canonicalized copy of FILE_NAME.
80626a55 11613 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
11614 SEARCH_CWD is true if the current directory is to be searched.
11615 It will be searched before debug-file-directory.
13aaf454
DE
11616 If successful, the file is added to the bfd include table of the
11617 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 11618 If unable to find/open the file, return NULL.
3019eac3
DE
11619 NOTE: This function is derived from symfile_bfd_open. */
11620
192b62ce 11621static gdb_bfd_ref_ptr
6ac97d4c 11622try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3 11623{
80626a55 11624 int desc, flags;
3019eac3 11625 char *absolute_name;
9c02c129
DE
11626 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11627 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11628 to debug_file_directory. */
11629 char *search_path;
11630 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11631
6ac97d4c
DE
11632 if (search_cwd)
11633 {
11634 if (*debug_file_directory != '\0')
11635 search_path = concat (".", dirname_separator_string,
b36cec19 11636 debug_file_directory, (char *) NULL);
6ac97d4c
DE
11637 else
11638 search_path = xstrdup (".");
11639 }
9c02c129 11640 else
6ac97d4c 11641 search_path = xstrdup (debug_file_directory);
3019eac3 11642
492c0ab7 11643 flags = OPF_RETURN_REALPATH;
80626a55
DE
11644 if (is_dwp)
11645 flags |= OPF_SEARCH_IN_PATH;
9c02c129 11646 desc = openp (search_path, flags, file_name,
3019eac3 11647 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 11648 xfree (search_path);
3019eac3
DE
11649 if (desc < 0)
11650 return NULL;
11651
192b62ce 11652 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 11653 xfree (absolute_name);
9c02c129
DE
11654 if (sym_bfd == NULL)
11655 return NULL;
192b62ce 11656 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 11657
192b62ce
TT
11658 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11659 return NULL;
3019eac3 11660
13aaf454
DE
11661 /* Success. Record the bfd as having been included by the objfile's bfd.
11662 This is important because things like demangled_names_hash lives in the
11663 objfile's per_bfd space and may have references to things like symbol
11664 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 11665 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 11666
3019eac3
DE
11667 return sym_bfd;
11668}
11669
ab5088bf 11670/* Try to open DWO file FILE_NAME.
3019eac3
DE
11671 COMP_DIR is the DW_AT_comp_dir attribute.
11672 The result is the bfd handle of the file.
11673 If there is a problem finding or opening the file, return NULL.
11674 Upon success, the canonicalized path of the file is stored in the bfd,
11675 same as symfile_bfd_open. */
11676
192b62ce 11677static gdb_bfd_ref_ptr
ab5088bf 11678open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3 11679{
80626a55 11680 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 11681 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
11682
11683 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11684
11685 if (comp_dir != NULL)
11686 {
b36cec19
PA
11687 char *path_to_try = concat (comp_dir, SLASH_STRING,
11688 file_name, (char *) NULL);
3019eac3
DE
11689
11690 /* NOTE: If comp_dir is a relative path, this will also try the
11691 search path, which seems useful. */
192b62ce
TT
11692 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
11693 1 /*search_cwd*/));
3019eac3
DE
11694 xfree (path_to_try);
11695 if (abfd != NULL)
11696 return abfd;
11697 }
11698
11699 /* That didn't work, try debug-file-directory, which, despite its name,
11700 is a list of paths. */
11701
11702 if (*debug_file_directory == '\0')
11703 return NULL;
11704
6ac97d4c 11705 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
11706}
11707
80626a55
DE
11708/* This function is mapped across the sections and remembers the offset and
11709 size of each of the DWO debugging sections we are interested in. */
11710
11711static void
11712dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
11713{
9a3c8263 11714 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
11715 const struct dwop_section_names *names = &dwop_section_names;
11716
11717 if (section_is_p (sectp->name, &names->abbrev_dwo))
11718 {
049412e3 11719 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
11720 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
11721 }
11722 else if (section_is_p (sectp->name, &names->info_dwo))
11723 {
049412e3 11724 dwo_sections->info.s.section = sectp;
80626a55
DE
11725 dwo_sections->info.size = bfd_get_section_size (sectp);
11726 }
11727 else if (section_is_p (sectp->name, &names->line_dwo))
11728 {
049412e3 11729 dwo_sections->line.s.section = sectp;
80626a55
DE
11730 dwo_sections->line.size = bfd_get_section_size (sectp);
11731 }
11732 else if (section_is_p (sectp->name, &names->loc_dwo))
11733 {
049412e3 11734 dwo_sections->loc.s.section = sectp;
80626a55
DE
11735 dwo_sections->loc.size = bfd_get_section_size (sectp);
11736 }
11737 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11738 {
049412e3 11739 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
11740 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
11741 }
11742 else if (section_is_p (sectp->name, &names->macro_dwo))
11743 {
049412e3 11744 dwo_sections->macro.s.section = sectp;
80626a55
DE
11745 dwo_sections->macro.size = bfd_get_section_size (sectp);
11746 }
11747 else if (section_is_p (sectp->name, &names->str_dwo))
11748 {
049412e3 11749 dwo_sections->str.s.section = sectp;
80626a55
DE
11750 dwo_sections->str.size = bfd_get_section_size (sectp);
11751 }
11752 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11753 {
049412e3 11754 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
11755 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
11756 }
11757 else if (section_is_p (sectp->name, &names->types_dwo))
11758 {
11759 struct dwarf2_section_info type_section;
11760
11761 memset (&type_section, 0, sizeof (type_section));
049412e3 11762 type_section.s.section = sectp;
80626a55
DE
11763 type_section.size = bfd_get_section_size (sectp);
11764 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
11765 &type_section);
11766 }
11767}
11768
ab5088bf 11769/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 11770 by PER_CU. This is for the non-DWP case.
80626a55 11771 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
11772
11773static struct dwo_file *
0ac5b59e
DE
11774open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
11775 const char *dwo_name, const char *comp_dir)
3019eac3
DE
11776{
11777 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 11778 struct dwo_file *dwo_file;
3019eac3
DE
11779 struct cleanup *cleanups;
11780
192b62ce 11781 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
80626a55
DE
11782 if (dbfd == NULL)
11783 {
b4f54984 11784 if (dwarf_read_debug)
80626a55
DE
11785 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
11786 return NULL;
11787 }
11788 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
11789 dwo_file->dwo_name = dwo_name;
11790 dwo_file->comp_dir = comp_dir;
192b62ce 11791 dwo_file->dbfd = dbfd.release ();
3019eac3
DE
11792
11793 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
11794
192b62ce
TT
11795 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
11796 &dwo_file->sections);
3019eac3 11797
33c5cd75 11798 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
3019eac3 11799
78d4d2c5
JK
11800 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
11801 dwo_file->tus);
3019eac3
DE
11802
11803 discard_cleanups (cleanups);
11804
b4f54984 11805 if (dwarf_read_debug)
80626a55
DE
11806 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
11807
3019eac3
DE
11808 return dwo_file;
11809}
11810
80626a55 11811/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
11812 size of each of the DWP debugging sections common to version 1 and 2 that
11813 we are interested in. */
3019eac3 11814
80626a55 11815static void
73869dc2
DE
11816dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
11817 void *dwp_file_ptr)
3019eac3 11818{
9a3c8263 11819 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
11820 const struct dwop_section_names *names = &dwop_section_names;
11821 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 11822
80626a55 11823 /* Record the ELF section number for later lookup: this is what the
73869dc2 11824 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
11825 gdb_assert (elf_section_nr < dwp_file->num_sections);
11826 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 11827
80626a55
DE
11828 /* Look for specific sections that we need. */
11829 if (section_is_p (sectp->name, &names->str_dwo))
11830 {
049412e3 11831 dwp_file->sections.str.s.section = sectp;
80626a55
DE
11832 dwp_file->sections.str.size = bfd_get_section_size (sectp);
11833 }
11834 else if (section_is_p (sectp->name, &names->cu_index))
11835 {
049412e3 11836 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
11837 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
11838 }
11839 else if (section_is_p (sectp->name, &names->tu_index))
11840 {
049412e3 11841 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
11842 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
11843 }
11844}
3019eac3 11845
73869dc2
DE
11846/* This function is mapped across the sections and remembers the offset and
11847 size of each of the DWP version 2 debugging sections that we are interested
11848 in. This is split into a separate function because we don't know if we
11849 have version 1 or 2 until we parse the cu_index/tu_index sections. */
11850
11851static void
11852dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
11853{
9a3c8263 11854 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
11855 const struct dwop_section_names *names = &dwop_section_names;
11856 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11857
11858 /* Record the ELF section number for later lookup: this is what the
11859 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11860 gdb_assert (elf_section_nr < dwp_file->num_sections);
11861 dwp_file->elf_sections[elf_section_nr] = sectp;
11862
11863 /* Look for specific sections that we need. */
11864 if (section_is_p (sectp->name, &names->abbrev_dwo))
11865 {
049412e3 11866 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
11867 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
11868 }
11869 else if (section_is_p (sectp->name, &names->info_dwo))
11870 {
049412e3 11871 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
11872 dwp_file->sections.info.size = bfd_get_section_size (sectp);
11873 }
11874 else if (section_is_p (sectp->name, &names->line_dwo))
11875 {
049412e3 11876 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
11877 dwp_file->sections.line.size = bfd_get_section_size (sectp);
11878 }
11879 else if (section_is_p (sectp->name, &names->loc_dwo))
11880 {
049412e3 11881 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
11882 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
11883 }
11884 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11885 {
049412e3 11886 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
11887 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
11888 }
11889 else if (section_is_p (sectp->name, &names->macro_dwo))
11890 {
049412e3 11891 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
11892 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
11893 }
11894 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11895 {
049412e3 11896 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
11897 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
11898 }
11899 else if (section_is_p (sectp->name, &names->types_dwo))
11900 {
049412e3 11901 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
11902 dwp_file->sections.types.size = bfd_get_section_size (sectp);
11903 }
11904}
11905
80626a55 11906/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 11907
80626a55
DE
11908static hashval_t
11909hash_dwp_loaded_cutus (const void *item)
11910{
9a3c8263 11911 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 11912
80626a55
DE
11913 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11914 return dwo_unit->signature;
3019eac3
DE
11915}
11916
80626a55 11917/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 11918
80626a55
DE
11919static int
11920eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 11921{
9a3c8263
SM
11922 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11923 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 11924
80626a55
DE
11925 return dua->signature == dub->signature;
11926}
3019eac3 11927
80626a55 11928/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 11929
80626a55
DE
11930static htab_t
11931allocate_dwp_loaded_cutus_table (struct objfile *objfile)
11932{
11933 return htab_create_alloc_ex (3,
11934 hash_dwp_loaded_cutus,
11935 eq_dwp_loaded_cutus,
11936 NULL,
11937 &objfile->objfile_obstack,
11938 hashtab_obstack_allocate,
11939 dummy_obstack_deallocate);
11940}
3019eac3 11941
ab5088bf
DE
11942/* Try to open DWP file FILE_NAME.
11943 The result is the bfd handle of the file.
11944 If there is a problem finding or opening the file, return NULL.
11945 Upon success, the canonicalized path of the file is stored in the bfd,
11946 same as symfile_bfd_open. */
11947
192b62ce 11948static gdb_bfd_ref_ptr
ab5088bf
DE
11949open_dwp_file (const char *file_name)
11950{
192b62ce
TT
11951 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
11952 1 /*search_cwd*/));
6ac97d4c
DE
11953 if (abfd != NULL)
11954 return abfd;
11955
11956 /* Work around upstream bug 15652.
11957 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11958 [Whether that's a "bug" is debatable, but it is getting in our way.]
11959 We have no real idea where the dwp file is, because gdb's realpath-ing
11960 of the executable's path may have discarded the needed info.
11961 [IWBN if the dwp file name was recorded in the executable, akin to
11962 .gnu_debuglink, but that doesn't exist yet.]
11963 Strip the directory from FILE_NAME and search again. */
11964 if (*debug_file_directory != '\0')
11965 {
11966 /* Don't implicitly search the current directory here.
11967 If the user wants to search "." to handle this case,
11968 it must be added to debug-file-directory. */
11969 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
11970 0 /*search_cwd*/);
11971 }
11972
11973 return NULL;
ab5088bf
DE
11974}
11975
80626a55
DE
11976/* Initialize the use of the DWP file for the current objfile.
11977 By convention the name of the DWP file is ${objfile}.dwp.
11978 The result is NULL if it can't be found. */
a766d390 11979
80626a55 11980static struct dwp_file *
ab5088bf 11981open_and_init_dwp_file (void)
80626a55
DE
11982{
11983 struct objfile *objfile = dwarf2_per_objfile->objfile;
11984 struct dwp_file *dwp_file;
80626a55 11985
82bf32bc
JK
11986 /* Try to find first .dwp for the binary file before any symbolic links
11987 resolving. */
6c447423
DE
11988
11989 /* If the objfile is a debug file, find the name of the real binary
11990 file and get the name of dwp file from there. */
d721ba37 11991 std::string dwp_name;
6c447423
DE
11992 if (objfile->separate_debug_objfile_backlink != NULL)
11993 {
11994 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11995 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 11996
d721ba37 11997 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
11998 }
11999 else
d721ba37
PA
12000 dwp_name = objfile->original_name;
12001
12002 dwp_name += ".dwp";
80626a55 12003
d721ba37 12004 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
82bf32bc
JK
12005 if (dbfd == NULL
12006 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12007 {
12008 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12009 dwp_name = objfile_name (objfile);
12010 dwp_name += ".dwp";
12011 dbfd = open_dwp_file (dwp_name.c_str ());
82bf32bc
JK
12012 }
12013
80626a55
DE
12014 if (dbfd == NULL)
12015 {
b4f54984 12016 if (dwarf_read_debug)
d721ba37 12017 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 12018 return NULL;
3019eac3 12019 }
80626a55 12020 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
12021 dwp_file->name = bfd_get_filename (dbfd.get ());
12022 dwp_file->dbfd = dbfd.release ();
c906108c 12023
80626a55 12024 /* +1: section 0 is unused */
192b62ce 12025 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
12026 dwp_file->elf_sections =
12027 OBSTACK_CALLOC (&objfile->objfile_obstack,
12028 dwp_file->num_sections, asection *);
12029
192b62ce
TT
12030 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
12031 dwp_file);
80626a55
DE
12032
12033 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
12034
12035 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
12036
73869dc2 12037 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12038 if (dwp_file->cus && dwp_file->tus
12039 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12040 {
12041 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12042 pretty bizarre. We use pulongest here because that's the established
4d65956b 12043 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12044 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12045 " TU version %s [in DWP file %s]"),
12046 pulongest (dwp_file->cus->version),
d721ba37 12047 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12048 }
08302ed2
DE
12049
12050 if (dwp_file->cus)
12051 dwp_file->version = dwp_file->cus->version;
12052 else if (dwp_file->tus)
12053 dwp_file->version = dwp_file->tus->version;
12054 else
12055 dwp_file->version = 2;
73869dc2
DE
12056
12057 if (dwp_file->version == 2)
192b62ce
TT
12058 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
12059 dwp_file);
73869dc2 12060
19ac8c2e
DE
12061 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
12062 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 12063
b4f54984 12064 if (dwarf_read_debug)
80626a55
DE
12065 {
12066 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12067 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12068 " %s CUs, %s TUs\n",
12069 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12070 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12071 }
12072
12073 return dwp_file;
3019eac3 12074}
c906108c 12075
ab5088bf
DE
12076/* Wrapper around open_and_init_dwp_file, only open it once. */
12077
12078static struct dwp_file *
12079get_dwp_file (void)
12080{
12081 if (! dwarf2_per_objfile->dwp_checked)
12082 {
12083 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
12084 dwarf2_per_objfile->dwp_checked = 1;
12085 }
12086 return dwarf2_per_objfile->dwp_file;
12087}
12088
80626a55
DE
12089/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12090 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12091 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12092 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12093 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12094
12095 This is called, for example, when wanting to read a variable with a
12096 complex location. Therefore we don't want to do file i/o for every call.
12097 Therefore we don't want to look for a DWO file on every call.
12098 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12099 then we check if we've already seen DWO_NAME, and only THEN do we check
12100 for a DWO file.
12101
1c658ad5 12102 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12103 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12104
3019eac3 12105static struct dwo_unit *
80626a55
DE
12106lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12107 const char *dwo_name, const char *comp_dir,
12108 ULONGEST signature, int is_debug_types)
3019eac3
DE
12109{
12110 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12111 const char *kind = is_debug_types ? "TU" : "CU";
12112 void **dwo_file_slot;
3019eac3 12113 struct dwo_file *dwo_file;
80626a55 12114 struct dwp_file *dwp_file;
cb1df416 12115
6a506a2d
DE
12116 /* First see if there's a DWP file.
12117 If we have a DWP file but didn't find the DWO inside it, don't
12118 look for the original DWO file. It makes gdb behave differently
12119 depending on whether one is debugging in the build tree. */
cf2c3c16 12120
ab5088bf 12121 dwp_file = get_dwp_file ();
80626a55 12122 if (dwp_file != NULL)
cf2c3c16 12123 {
80626a55
DE
12124 const struct dwp_hash_table *dwp_htab =
12125 is_debug_types ? dwp_file->tus : dwp_file->cus;
12126
12127 if (dwp_htab != NULL)
12128 {
12129 struct dwo_unit *dwo_cutu =
57d63ce2
DE
12130 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
12131 signature, is_debug_types);
80626a55
DE
12132
12133 if (dwo_cutu != NULL)
12134 {
b4f54984 12135 if (dwarf_read_debug)
80626a55
DE
12136 {
12137 fprintf_unfiltered (gdb_stdlog,
12138 "Virtual DWO %s %s found: @%s\n",
12139 kind, hex_string (signature),
12140 host_address_to_string (dwo_cutu));
12141 }
12142 return dwo_cutu;
12143 }
12144 }
12145 }
6a506a2d 12146 else
80626a55 12147 {
6a506a2d 12148 /* No DWP file, look for the DWO file. */
80626a55 12149
6a506a2d
DE
12150 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
12151 if (*dwo_file_slot == NULL)
80626a55 12152 {
6a506a2d
DE
12153 /* Read in the file and build a table of the CUs/TUs it contains. */
12154 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12155 }
6a506a2d 12156 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12157 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12158
6a506a2d 12159 if (dwo_file != NULL)
19c3d4c9 12160 {
6a506a2d
DE
12161 struct dwo_unit *dwo_cutu = NULL;
12162
12163 if (is_debug_types && dwo_file->tus)
12164 {
12165 struct dwo_unit find_dwo_cutu;
12166
12167 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12168 find_dwo_cutu.signature = signature;
9a3c8263
SM
12169 dwo_cutu
12170 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 12171 }
33c5cd75 12172 else if (!is_debug_types && dwo_file->cus)
80626a55 12173 {
33c5cd75
DB
12174 struct dwo_unit find_dwo_cutu;
12175
12176 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12177 find_dwo_cutu.signature = signature;
12178 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
12179 &find_dwo_cutu);
6a506a2d
DE
12180 }
12181
12182 if (dwo_cutu != NULL)
12183 {
b4f54984 12184 if (dwarf_read_debug)
6a506a2d
DE
12185 {
12186 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12187 kind, dwo_name, hex_string (signature),
12188 host_address_to_string (dwo_cutu));
12189 }
12190 return dwo_cutu;
80626a55
DE
12191 }
12192 }
2e276125 12193 }
9cdd5dbd 12194
80626a55
DE
12195 /* We didn't find it. This could mean a dwo_id mismatch, or
12196 someone deleted the DWO/DWP file, or the search path isn't set up
12197 correctly to find the file. */
12198
b4f54984 12199 if (dwarf_read_debug)
80626a55
DE
12200 {
12201 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12202 kind, dwo_name, hex_string (signature));
12203 }
3019eac3 12204
6656a72d
DE
12205 /* This is a warning and not a complaint because it can be caused by
12206 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12207 {
12208 /* Print the name of the DWP file if we looked there, helps the user
12209 better diagnose the problem. */
791afaa2 12210 std::string dwp_text;
43942612
DE
12211
12212 if (dwp_file != NULL)
791afaa2
TT
12213 dwp_text = string_printf (" [in DWP file %s]",
12214 lbasename (dwp_file->name));
43942612
DE
12215
12216 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
12217 " [in module %s]"),
12218 kind, dwo_name, hex_string (signature),
791afaa2 12219 dwp_text.c_str (),
43942612 12220 this_unit->is_debug_types ? "TU" : "CU",
9c541725 12221 to_underlying (this_unit->sect_off), objfile_name (objfile));
43942612 12222 }
3019eac3 12223 return NULL;
5fb290d7
DJ
12224}
12225
80626a55
DE
12226/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12227 See lookup_dwo_cutu_unit for details. */
12228
12229static struct dwo_unit *
12230lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12231 const char *dwo_name, const char *comp_dir,
12232 ULONGEST signature)
12233{
12234 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12235}
12236
12237/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12238 See lookup_dwo_cutu_unit for details. */
12239
12240static struct dwo_unit *
12241lookup_dwo_type_unit (struct signatured_type *this_tu,
12242 const char *dwo_name, const char *comp_dir)
12243{
12244 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12245}
12246
89e63ee4
DE
12247/* Traversal function for queue_and_load_all_dwo_tus. */
12248
12249static int
12250queue_and_load_dwo_tu (void **slot, void *info)
12251{
12252 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12253 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12254 ULONGEST signature = dwo_unit->signature;
12255 struct signatured_type *sig_type =
12256 lookup_dwo_signatured_type (per_cu->cu, signature);
12257
12258 if (sig_type != NULL)
12259 {
12260 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12261
12262 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12263 a real dependency of PER_CU on SIG_TYPE. That is detected later
12264 while processing PER_CU. */
12265 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12266 load_full_type_unit (sig_cu);
12267 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
12268 }
12269
12270 return 1;
12271}
12272
12273/* Queue all TUs contained in the DWO of PER_CU to be read in.
12274 The DWO may have the only definition of the type, though it may not be
12275 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12276 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12277
12278static void
12279queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12280{
12281 struct dwo_unit *dwo_unit;
12282 struct dwo_file *dwo_file;
12283
12284 gdb_assert (!per_cu->is_debug_types);
12285 gdb_assert (get_dwp_file () == NULL);
12286 gdb_assert (per_cu->cu != NULL);
12287
12288 dwo_unit = per_cu->cu->dwo_unit;
12289 gdb_assert (dwo_unit != NULL);
12290
12291 dwo_file = dwo_unit->dwo_file;
12292 if (dwo_file->tus != NULL)
12293 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
12294}
12295
3019eac3
DE
12296/* Free all resources associated with DWO_FILE.
12297 Close the DWO file and munmap the sections.
12298 All memory should be on the objfile obstack. */
348e048f
DE
12299
12300static void
3019eac3 12301free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 12302{
348e048f 12303
5c6fa7ab 12304 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 12305 gdb_bfd_unref (dwo_file->dbfd);
348e048f 12306
3019eac3
DE
12307 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
12308}
348e048f 12309
3019eac3 12310/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 12311
3019eac3
DE
12312static void
12313free_dwo_file_cleanup (void *arg)
12314{
12315 struct dwo_file *dwo_file = (struct dwo_file *) arg;
12316 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 12317
3019eac3
DE
12318 free_dwo_file (dwo_file, objfile);
12319}
348e048f 12320
3019eac3 12321/* Traversal function for free_dwo_files. */
2ab95328 12322
3019eac3
DE
12323static int
12324free_dwo_file_from_slot (void **slot, void *info)
12325{
12326 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
12327 struct objfile *objfile = (struct objfile *) info;
348e048f 12328
3019eac3 12329 free_dwo_file (dwo_file, objfile);
348e048f 12330
3019eac3
DE
12331 return 1;
12332}
348e048f 12333
3019eac3 12334/* Free all resources associated with DWO_FILES. */
348e048f 12335
3019eac3
DE
12336static void
12337free_dwo_files (htab_t dwo_files, struct objfile *objfile)
12338{
12339 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 12340}
3019eac3
DE
12341\f
12342/* Read in various DIEs. */
348e048f 12343
d389af10 12344/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12345 Inherit only the children of the DW_AT_abstract_origin DIE not being
12346 already referenced by DW_AT_abstract_origin from the children of the
12347 current DIE. */
d389af10
JK
12348
12349static void
12350inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12351{
12352 struct die_info *child_die;
791afaa2 12353 sect_offset *offsetp;
d389af10
JK
12354 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12355 struct die_info *origin_die;
12356 /* Iterator of the ORIGIN_DIE children. */
12357 struct die_info *origin_child_die;
d389af10 12358 struct attribute *attr;
cd02d79d
PA
12359 struct dwarf2_cu *origin_cu;
12360 struct pending **origin_previous_list_in_scope;
d389af10
JK
12361
12362 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12363 if (!attr)
12364 return;
12365
cd02d79d
PA
12366 /* Note that following die references may follow to a die in a
12367 different cu. */
12368
12369 origin_cu = cu;
12370 origin_die = follow_die_ref (die, attr, &origin_cu);
12371
12372 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12373 symbols in. */
12374 origin_previous_list_in_scope = origin_cu->list_in_scope;
12375 origin_cu->list_in_scope = cu->list_in_scope;
12376
edb3359d
DJ
12377 if (die->tag != origin_die->tag
12378 && !(die->tag == DW_TAG_inlined_subroutine
12379 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
12380 complaint (&symfile_complaints,
12381 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9c541725
PA
12382 to_underlying (die->sect_off),
12383 to_underlying (origin_die->sect_off));
d389af10 12384
791afaa2 12385 std::vector<sect_offset> offsets;
d389af10 12386
3ea89b92
PMR
12387 for (child_die = die->child;
12388 child_die && child_die->tag;
12389 child_die = sibling_die (child_die))
12390 {
12391 struct die_info *child_origin_die;
12392 struct dwarf2_cu *child_origin_cu;
12393
12394 /* We are trying to process concrete instance entries:
216f72a1 12395 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12396 it's not relevant to our analysis here. i.e. detecting DIEs that are
12397 present in the abstract instance but not referenced in the concrete
12398 one. */
216f72a1
JK
12399 if (child_die->tag == DW_TAG_call_site
12400 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12401 continue;
12402
c38f313d
DJ
12403 /* For each CHILD_DIE, find the corresponding child of
12404 ORIGIN_DIE. If there is more than one layer of
12405 DW_AT_abstract_origin, follow them all; there shouldn't be,
12406 but GCC versions at least through 4.4 generate this (GCC PR
12407 40573). */
3ea89b92
PMR
12408 child_origin_die = child_die;
12409 child_origin_cu = cu;
c38f313d
DJ
12410 while (1)
12411 {
cd02d79d
PA
12412 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12413 child_origin_cu);
c38f313d
DJ
12414 if (attr == NULL)
12415 break;
cd02d79d
PA
12416 child_origin_die = follow_die_ref (child_origin_die, attr,
12417 &child_origin_cu);
c38f313d
DJ
12418 }
12419
d389af10
JK
12420 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12421 counterpart may exist. */
c38f313d 12422 if (child_origin_die != child_die)
d389af10 12423 {
edb3359d
DJ
12424 if (child_die->tag != child_origin_die->tag
12425 && !(child_die->tag == DW_TAG_inlined_subroutine
12426 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
12427 complaint (&symfile_complaints,
12428 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
12429 "different tags"),
12430 to_underlying (child_die->sect_off),
12431 to_underlying (child_origin_die->sect_off));
c38f313d
DJ
12432 if (child_origin_die->parent != origin_die)
12433 complaint (&symfile_complaints,
12434 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
12435 "different parents"),
12436 to_underlying (child_die->sect_off),
12437 to_underlying (child_origin_die->sect_off));
c38f313d 12438 else
791afaa2 12439 offsets.push_back (child_origin_die->sect_off);
d389af10 12440 }
d389af10 12441 }
791afaa2
TT
12442 std::sort (offsets.begin (), offsets.end ());
12443 sect_offset *offsets_end = offsets.data () + offsets.size ();
12444 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12445 if (offsetp[-1] == *offsetp)
3e43a32a
MS
12446 complaint (&symfile_complaints,
12447 _("Multiple children of DIE 0x%x refer "
12448 "to DIE 0x%x as their abstract origin"),
9c541725 12449 to_underlying (die->sect_off), to_underlying (*offsetp));
d389af10 12450
791afaa2 12451 offsetp = offsets.data ();
d389af10
JK
12452 origin_child_die = origin_die->child;
12453 while (origin_child_die && origin_child_die->tag)
12454 {
12455 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12456 while (offsetp < offsets_end
9c541725 12457 && *offsetp < origin_child_die->sect_off)
d389af10 12458 offsetp++;
b64f50a1 12459 if (offsetp >= offsets_end
9c541725 12460 || *offsetp > origin_child_die->sect_off)
d389af10 12461 {
adde2bff
DE
12462 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12463 Check whether we're already processing ORIGIN_CHILD_DIE.
12464 This can happen with mutually referenced abstract_origins.
12465 PR 16581. */
12466 if (!origin_child_die->in_process)
12467 process_die (origin_child_die, origin_cu);
d389af10
JK
12468 }
12469 origin_child_die = sibling_die (origin_child_die);
12470 }
cd02d79d 12471 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
12472}
12473
c906108c 12474static void
e7c27a73 12475read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12476{
e7c27a73 12477 struct objfile *objfile = cu->objfile;
3e29f34a 12478 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 12479 struct context_stack *newobj;
c906108c
SS
12480 CORE_ADDR lowpc;
12481 CORE_ADDR highpc;
12482 struct die_info *child_die;
edb3359d 12483 struct attribute *attr, *call_line, *call_file;
15d034d0 12484 const char *name;
e142c38c 12485 CORE_ADDR baseaddr;
801e3a5b 12486 struct block *block;
edb3359d 12487 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12488 std::vector<struct symbol *> template_args;
34eaf542 12489 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12490
12491 if (inlined_func)
12492 {
12493 /* If we do not have call site information, we can't show the
12494 caller of this inlined function. That's too confusing, so
12495 only use the scope for local variables. */
12496 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12497 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12498 if (call_line == NULL || call_file == NULL)
12499 {
12500 read_lexical_block_scope (die, cu);
12501 return;
12502 }
12503 }
c906108c 12504
e142c38c
DJ
12505 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12506
94af9270 12507 name = dwarf2_name (die, cu);
c906108c 12508
e8d05480
JB
12509 /* Ignore functions with missing or empty names. These are actually
12510 illegal according to the DWARF standard. */
12511 if (name == NULL)
12512 {
12513 complaint (&symfile_complaints,
b64f50a1 12514 _("missing name for subprogram DIE at %d"),
9c541725 12515 to_underlying (die->sect_off));
e8d05480
JB
12516 return;
12517 }
12518
12519 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12520 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12521 <= PC_BOUNDS_INVALID)
e8d05480 12522 {
ae4d0c03
PM
12523 attr = dwarf2_attr (die, DW_AT_external, cu);
12524 if (!attr || !DW_UNSND (attr))
12525 complaint (&symfile_complaints,
3e43a32a
MS
12526 _("cannot get low and high bounds "
12527 "for subprogram DIE at %d"),
9c541725 12528 to_underlying (die->sect_off));
e8d05480
JB
12529 return;
12530 }
c906108c 12531
3e29f34a
MR
12532 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12533 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12534
34eaf542
TT
12535 /* If we have any template arguments, then we must allocate a
12536 different sort of symbol. */
12537 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12538 {
12539 if (child_die->tag == DW_TAG_template_type_param
12540 || child_die->tag == DW_TAG_template_value_param)
12541 {
e623cf5d 12542 templ_func = allocate_template_symbol (objfile);
cf724bc9 12543 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
12544 break;
12545 }
12546 }
12547
fe978cb0
PA
12548 newobj = push_context (0, lowpc);
12549 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 12550 (struct symbol *) templ_func);
4c2df51b 12551
4cecd739
DJ
12552 /* If there is a location expression for DW_AT_frame_base, record
12553 it. */
e142c38c 12554 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 12555 if (attr)
fe978cb0 12556 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 12557
63e43d3a
PMR
12558 /* If there is a location for the static link, record it. */
12559 newobj->static_link = NULL;
12560 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12561 if (attr)
12562 {
224c3ddb
SM
12563 newobj->static_link
12564 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
12565 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
12566 }
12567
e142c38c 12568 cu->list_in_scope = &local_symbols;
c906108c 12569
639d11d3 12570 if (die->child != NULL)
c906108c 12571 {
639d11d3 12572 child_die = die->child;
c906108c
SS
12573 while (child_die && child_die->tag)
12574 {
34eaf542
TT
12575 if (child_die->tag == DW_TAG_template_type_param
12576 || child_die->tag == DW_TAG_template_value_param)
12577 {
12578 struct symbol *arg = new_symbol (child_die, NULL, cu);
12579
f1078f66 12580 if (arg != NULL)
2f4732b0 12581 template_args.push_back (arg);
34eaf542
TT
12582 }
12583 else
12584 process_die (child_die, cu);
c906108c
SS
12585 child_die = sibling_die (child_die);
12586 }
12587 }
12588
d389af10
JK
12589 inherit_abstract_dies (die, cu);
12590
4a811a97
UW
12591 /* If we have a DW_AT_specification, we might need to import using
12592 directives from the context of the specification DIE. See the
12593 comment in determine_prefix. */
12594 if (cu->language == language_cplus
12595 && dwarf2_attr (die, DW_AT_specification, cu))
12596 {
12597 struct dwarf2_cu *spec_cu = cu;
12598 struct die_info *spec_die = die_specification (die, &spec_cu);
12599
12600 while (spec_die)
12601 {
12602 child_die = spec_die->child;
12603 while (child_die && child_die->tag)
12604 {
12605 if (child_die->tag == DW_TAG_imported_module)
12606 process_die (child_die, spec_cu);
12607 child_die = sibling_die (child_die);
12608 }
12609
12610 /* In some cases, GCC generates specification DIEs that
12611 themselves contain DW_AT_specification attributes. */
12612 spec_die = die_specification (spec_die, &spec_cu);
12613 }
12614 }
12615
fe978cb0 12616 newobj = pop_context ();
c906108c 12617 /* Make a block for the local symbols within. */
fe978cb0 12618 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 12619 newobj->static_link, lowpc, highpc);
801e3a5b 12620
df8a16a1 12621 /* For C++, set the block's scope. */
45280282
IB
12622 if ((cu->language == language_cplus
12623 || cu->language == language_fortran
c44af4eb
TT
12624 || cu->language == language_d
12625 || cu->language == language_rust)
4d4ec4e5 12626 && cu->processing_has_namespace_info)
195a3f6c
TT
12627 block_set_scope (block, determine_prefix (die, cu),
12628 &objfile->objfile_obstack);
df8a16a1 12629
801e3a5b
JB
12630 /* If we have address ranges, record them. */
12631 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 12632
fe978cb0 12633 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 12634
34eaf542 12635 /* Attach template arguments to function. */
2f4732b0 12636 if (!template_args.empty ())
34eaf542
TT
12637 {
12638 gdb_assert (templ_func != NULL);
12639
2f4732b0 12640 templ_func->n_template_arguments = template_args.size ();
34eaf542 12641 templ_func->template_arguments
8d749320
SM
12642 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12643 templ_func->n_template_arguments);
34eaf542 12644 memcpy (templ_func->template_arguments,
2f4732b0 12645 template_args.data (),
34eaf542 12646 (templ_func->n_template_arguments * sizeof (struct symbol *)));
34eaf542
TT
12647 }
12648
208d8187
JB
12649 /* In C++, we can have functions nested inside functions (e.g., when
12650 a function declares a class that has methods). This means that
12651 when we finish processing a function scope, we may need to go
12652 back to building a containing block's symbol lists. */
fe978cb0 12653 local_symbols = newobj->locals;
22cee43f 12654 local_using_directives = newobj->local_using_directives;
208d8187 12655
921e78cf
JB
12656 /* If we've finished processing a top-level function, subsequent
12657 symbols go in the file symbol list. */
12658 if (outermost_context_p ())
e142c38c 12659 cu->list_in_scope = &file_symbols;
c906108c
SS
12660}
12661
12662/* Process all the DIES contained within a lexical block scope. Start
12663 a new scope, process the dies, and then close the scope. */
12664
12665static void
e7c27a73 12666read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12667{
e7c27a73 12668 struct objfile *objfile = cu->objfile;
3e29f34a 12669 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 12670 struct context_stack *newobj;
c906108c
SS
12671 CORE_ADDR lowpc, highpc;
12672 struct die_info *child_die;
e142c38c
DJ
12673 CORE_ADDR baseaddr;
12674
12675 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
12676
12677 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
12678 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12679 as multiple lexical blocks? Handling children in a sane way would
6e70227d 12680 be nasty. Might be easier to properly extend generic blocks to
af34e669 12681 describe ranges. */
e385593e
JK
12682 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
12683 {
12684 case PC_BOUNDS_NOT_PRESENT:
12685 /* DW_TAG_lexical_block has no attributes, process its children as if
12686 there was no wrapping by that DW_TAG_lexical_block.
12687 GCC does no longer produces such DWARF since GCC r224161. */
12688 for (child_die = die->child;
12689 child_die != NULL && child_die->tag;
12690 child_die = sibling_die (child_die))
12691 process_die (child_die, cu);
12692 return;
12693 case PC_BOUNDS_INVALID:
12694 return;
12695 }
3e29f34a
MR
12696 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12697 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
12698
12699 push_context (0, lowpc);
639d11d3 12700 if (die->child != NULL)
c906108c 12701 {
639d11d3 12702 child_die = die->child;
c906108c
SS
12703 while (child_die && child_die->tag)
12704 {
e7c27a73 12705 process_die (child_die, cu);
c906108c
SS
12706 child_die = sibling_die (child_die);
12707 }
12708 }
3ea89b92 12709 inherit_abstract_dies (die, cu);
fe978cb0 12710 newobj = pop_context ();
c906108c 12711
22cee43f 12712 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 12713 {
801e3a5b 12714 struct block *block
63e43d3a 12715 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 12716 newobj->start_addr, highpc);
801e3a5b
JB
12717
12718 /* Note that recording ranges after traversing children, as we
12719 do here, means that recording a parent's ranges entails
12720 walking across all its children's ranges as they appear in
12721 the address map, which is quadratic behavior.
12722
12723 It would be nicer to record the parent's ranges before
12724 traversing its children, simply overriding whatever you find
12725 there. But since we don't even decide whether to create a
12726 block until after we've traversed its children, that's hard
12727 to do. */
12728 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 12729 }
fe978cb0 12730 local_symbols = newobj->locals;
22cee43f 12731 local_using_directives = newobj->local_using_directives;
c906108c
SS
12732}
12733
216f72a1 12734/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
12735
12736static void
12737read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
12738{
12739 struct objfile *objfile = cu->objfile;
12740 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12741 CORE_ADDR pc, baseaddr;
12742 struct attribute *attr;
12743 struct call_site *call_site, call_site_local;
12744 void **slot;
12745 int nparams;
12746 struct die_info *child_die;
12747
12748 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12749
216f72a1
JK
12750 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
12751 if (attr == NULL)
12752 {
12753 /* This was a pre-DWARF-5 GNU extension alias
12754 for DW_AT_call_return_pc. */
12755 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12756 }
96408a79
SA
12757 if (!attr)
12758 {
12759 complaint (&symfile_complaints,
216f72a1 12760 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 12761 "DIE 0x%x [in module %s]"),
9c541725 12762 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12763 return;
12764 }
31aa7e4e 12765 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 12766 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
12767
12768 if (cu->call_site_htab == NULL)
12769 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
12770 NULL, &objfile->objfile_obstack,
12771 hashtab_obstack_allocate, NULL);
12772 call_site_local.pc = pc;
12773 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
12774 if (*slot != NULL)
12775 {
12776 complaint (&symfile_complaints,
216f72a1 12777 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 12778 "DIE 0x%x [in module %s]"),
9c541725 12779 paddress (gdbarch, pc), to_underlying (die->sect_off),
4262abfb 12780 objfile_name (objfile));
96408a79
SA
12781 return;
12782 }
12783
12784 /* Count parameters at the caller. */
12785
12786 nparams = 0;
12787 for (child_die = die->child; child_die && child_die->tag;
12788 child_die = sibling_die (child_die))
12789 {
216f72a1
JK
12790 if (child_die->tag != DW_TAG_call_site_parameter
12791 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
12792 {
12793 complaint (&symfile_complaints,
216f72a1
JK
12794 _("Tag %d is not DW_TAG_call_site_parameter in "
12795 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 12796 child_die->tag, to_underlying (child_die->sect_off),
4262abfb 12797 objfile_name (objfile));
96408a79
SA
12798 continue;
12799 }
12800
12801 nparams++;
12802 }
12803
224c3ddb
SM
12804 call_site
12805 = ((struct call_site *)
12806 obstack_alloc (&objfile->objfile_obstack,
12807 sizeof (*call_site)
12808 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
12809 *slot = call_site;
12810 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
12811 call_site->pc = pc;
12812
216f72a1
JK
12813 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
12814 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
12815 {
12816 struct die_info *func_die;
12817
12818 /* Skip also over DW_TAG_inlined_subroutine. */
12819 for (func_die = die->parent;
12820 func_die && func_die->tag != DW_TAG_subprogram
12821 && func_die->tag != DW_TAG_subroutine_type;
12822 func_die = func_die->parent);
12823
216f72a1
JK
12824 /* DW_AT_call_all_calls is a superset
12825 of DW_AT_call_all_tail_calls. */
96408a79 12826 if (func_die
216f72a1 12827 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 12828 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 12829 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
12830 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12831 {
12832 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12833 not complete. But keep CALL_SITE for look ups via call_site_htab,
12834 both the initial caller containing the real return address PC and
12835 the final callee containing the current PC of a chain of tail
12836 calls do not need to have the tail call list complete. But any
12837 function candidate for a virtual tail call frame searched via
12838 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12839 determined unambiguously. */
12840 }
12841 else
12842 {
12843 struct type *func_type = NULL;
12844
12845 if (func_die)
12846 func_type = get_die_type (func_die, cu);
12847 if (func_type != NULL)
12848 {
12849 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
12850
12851 /* Enlist this call site to the function. */
12852 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12853 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12854 }
12855 else
12856 complaint (&symfile_complaints,
216f72a1 12857 _("Cannot find function owning DW_TAG_call_site "
96408a79 12858 "DIE 0x%x [in module %s]"),
9c541725 12859 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12860 }
12861 }
12862
216f72a1
JK
12863 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12864 if (attr == NULL)
12865 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12866 if (attr == NULL)
12867 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 12868 if (attr == NULL)
216f72a1
JK
12869 {
12870 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12871 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12872 }
96408a79
SA
12873 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
12874 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
12875 /* Keep NULL DWARF_BLOCK. */;
12876 else if (attr_form_is_block (attr))
12877 {
12878 struct dwarf2_locexpr_baton *dlbaton;
12879
8d749320 12880 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
12881 dlbaton->data = DW_BLOCK (attr)->data;
12882 dlbaton->size = DW_BLOCK (attr)->size;
12883 dlbaton->per_cu = cu->per_cu;
12884
12885 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
12886 }
7771576e 12887 else if (attr_form_is_ref (attr))
96408a79 12888 {
96408a79
SA
12889 struct dwarf2_cu *target_cu = cu;
12890 struct die_info *target_die;
12891
ac9ec31b 12892 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
12893 gdb_assert (target_cu->objfile == objfile);
12894 if (die_is_declaration (target_die, target_cu))
12895 {
7d45c7c3 12896 const char *target_physname;
9112db09
JK
12897
12898 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 12899 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 12900 if (target_physname == NULL)
9112db09 12901 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
12902 if (target_physname == NULL)
12903 complaint (&symfile_complaints,
216f72a1 12904 _("DW_AT_call_target target DIE has invalid "
96408a79 12905 "physname, for referencing DIE 0x%x [in module %s]"),
9c541725 12906 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 12907 else
7d455152 12908 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
12909 }
12910 else
12911 {
12912 CORE_ADDR lowpc;
12913
12914 /* DW_AT_entry_pc should be preferred. */
3a2b436a 12915 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 12916 <= PC_BOUNDS_INVALID)
96408a79 12917 complaint (&symfile_complaints,
216f72a1 12918 _("DW_AT_call_target target DIE has invalid "
96408a79 12919 "low pc, for referencing DIE 0x%x [in module %s]"),
9c541725 12920 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 12921 else
3e29f34a
MR
12922 {
12923 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12924 SET_FIELD_PHYSADDR (call_site->target, lowpc);
12925 }
96408a79
SA
12926 }
12927 }
12928 else
12929 complaint (&symfile_complaints,
216f72a1 12930 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 12931 "block nor reference, for DIE 0x%x [in module %s]"),
9c541725 12932 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12933
12934 call_site->per_cu = cu->per_cu;
12935
12936 for (child_die = die->child;
12937 child_die && child_die->tag;
12938 child_die = sibling_die (child_die))
12939 {
96408a79 12940 struct call_site_parameter *parameter;
1788b2d3 12941 struct attribute *loc, *origin;
96408a79 12942
216f72a1
JK
12943 if (child_die->tag != DW_TAG_call_site_parameter
12944 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
12945 {
12946 /* Already printed the complaint above. */
12947 continue;
12948 }
12949
12950 gdb_assert (call_site->parameter_count < nparams);
12951 parameter = &call_site->parameter[call_site->parameter_count];
12952
1788b2d3
JK
12953 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12954 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 12955 register is contained in DW_AT_call_value. */
96408a79 12956
24c5c679 12957 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
12958 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12959 if (origin == NULL)
12960 {
12961 /* This was a pre-DWARF-5 GNU extension alias
12962 for DW_AT_call_parameter. */
12963 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12964 }
7771576e 12965 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 12966 {
1788b2d3 12967 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
12968
12969 sect_offset sect_off
12970 = (sect_offset) dwarf2_get_ref_die_offset (origin);
12971 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
12972 {
12973 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12974 binding can be done only inside one CU. Such referenced DIE
12975 therefore cannot be even moved to DW_TAG_partial_unit. */
12976 complaint (&symfile_complaints,
216f72a1
JK
12977 _("DW_AT_call_parameter offset is not in CU for "
12978 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12979 to_underlying (child_die->sect_off),
12980 objfile_name (objfile));
d76b7dbc
JK
12981 continue;
12982 }
9c541725
PA
12983 parameter->u.param_cu_off
12984 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
12985 }
12986 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
12987 {
12988 complaint (&symfile_complaints,
12989 _("No DW_FORM_block* DW_AT_location for "
216f72a1 12990 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 12991 to_underlying (child_die->sect_off), objfile_name (objfile));
96408a79
SA
12992 continue;
12993 }
24c5c679 12994 else
96408a79 12995 {
24c5c679
JK
12996 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12997 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12998 if (parameter->u.dwarf_reg != -1)
12999 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13000 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13001 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13002 &parameter->u.fb_offset))
13003 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13004 else
13005 {
13006 complaint (&symfile_complaints,
13007 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
13008 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 13009 "DW_TAG_call_site child DIE 0x%x "
24c5c679 13010 "[in module %s]"),
9c541725
PA
13011 to_underlying (child_die->sect_off),
13012 objfile_name (objfile));
24c5c679
JK
13013 continue;
13014 }
96408a79
SA
13015 }
13016
216f72a1
JK
13017 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13018 if (attr == NULL)
13019 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
13020 if (!attr_form_is_block (attr))
13021 {
13022 complaint (&symfile_complaints,
216f72a1
JK
13023 _("No DW_FORM_block* DW_AT_call_value for "
13024 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
13025 to_underlying (child_die->sect_off),
13026 objfile_name (objfile));
96408a79
SA
13027 continue;
13028 }
13029 parameter->value = DW_BLOCK (attr)->data;
13030 parameter->value_size = DW_BLOCK (attr)->size;
13031
13032 /* Parameters are not pre-cleared by memset above. */
13033 parameter->data_value = NULL;
13034 parameter->data_value_size = 0;
13035 call_site->parameter_count++;
13036
216f72a1
JK
13037 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13038 if (attr == NULL)
13039 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
13040 if (attr)
13041 {
13042 if (!attr_form_is_block (attr))
13043 complaint (&symfile_complaints,
216f72a1
JK
13044 _("No DW_FORM_block* DW_AT_call_data_value for "
13045 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
13046 to_underlying (child_die->sect_off),
13047 objfile_name (objfile));
96408a79
SA
13048 else
13049 {
13050 parameter->data_value = DW_BLOCK (attr)->data;
13051 parameter->data_value_size = DW_BLOCK (attr)->size;
13052 }
13053 }
13054 }
13055}
13056
71a3c369
TT
13057/* Helper function for read_variable. If DIE represents a virtual
13058 table, then return the type of the concrete object that is
13059 associated with the virtual table. Otherwise, return NULL. */
13060
13061static struct type *
13062rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13063{
13064 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13065 if (attr == NULL)
13066 return NULL;
13067
13068 /* Find the type DIE. */
13069 struct die_info *type_die = NULL;
13070 struct dwarf2_cu *type_cu = cu;
13071
13072 if (attr_form_is_ref (attr))
13073 type_die = follow_die_ref (die, attr, &type_cu);
13074 if (type_die == NULL)
13075 return NULL;
13076
13077 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13078 return NULL;
13079 return die_containing_type (type_die, type_cu);
13080}
13081
13082/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13083
13084static void
13085read_variable (struct die_info *die, struct dwarf2_cu *cu)
13086{
13087 struct rust_vtable_symbol *storage = NULL;
13088
13089 if (cu->language == language_rust)
13090 {
13091 struct type *containing_type = rust_containing_type (die, cu);
13092
13093 if (containing_type != NULL)
13094 {
13095 struct objfile *objfile = cu->objfile;
13096
13097 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
13098 struct rust_vtable_symbol);
13099 initialize_objfile_symbol (storage);
13100 storage->concrete_type = containing_type;
cf724bc9 13101 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13102 }
13103 }
13104
13105 new_symbol_full (die, NULL, cu, storage);
13106}
13107
43988095
JK
13108/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13109 reading .debug_rnglists.
13110 Callback's type should be:
13111 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13112 Return true if the attributes are present and valid, otherwise,
13113 return false. */
13114
13115template <typename Callback>
13116static bool
13117dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13118 Callback &&callback)
13119{
13120 struct objfile *objfile = cu->objfile;
13121 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13122 struct comp_unit_head *cu_header = &cu->header;
13123 bfd *obfd = objfile->obfd;
13124 unsigned int addr_size = cu_header->addr_size;
13125 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13126 /* Base address selection entry. */
13127 CORE_ADDR base;
13128 int found_base;
13129 unsigned int dummy;
13130 const gdb_byte *buffer;
13131 CORE_ADDR low = 0;
13132 CORE_ADDR high = 0;
13133 CORE_ADDR baseaddr;
13134 bool overflow = false;
13135
13136 found_base = cu->base_known;
13137 base = cu->base_address;
13138
13139 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
13140 if (offset >= dwarf2_per_objfile->rnglists.size)
13141 {
13142 complaint (&symfile_complaints,
13143 _("Offset %d out of bounds for DW_AT_ranges attribute"),
13144 offset);
13145 return false;
13146 }
13147 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13148
13149 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13150
13151 while (1)
13152 {
7814882a
JK
13153 /* Initialize it due to a false compiler warning. */
13154 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13155 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13156 + dwarf2_per_objfile->rnglists.size);
13157 unsigned int bytes_read;
13158
13159 if (buffer == buf_end)
13160 {
13161 overflow = true;
13162 break;
13163 }
13164 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13165 switch (rlet)
13166 {
13167 case DW_RLE_end_of_list:
13168 break;
13169 case DW_RLE_base_address:
13170 if (buffer + cu->header.addr_size > buf_end)
13171 {
13172 overflow = true;
13173 break;
13174 }
13175 base = read_address (obfd, buffer, cu, &bytes_read);
13176 found_base = 1;
13177 buffer += bytes_read;
13178 break;
13179 case DW_RLE_start_length:
13180 if (buffer + cu->header.addr_size > buf_end)
13181 {
13182 overflow = true;
13183 break;
13184 }
13185 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13186 buffer += bytes_read;
13187 range_end = (range_beginning
13188 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13189 buffer += bytes_read;
13190 if (buffer > buf_end)
13191 {
13192 overflow = true;
13193 break;
13194 }
13195 break;
13196 case DW_RLE_offset_pair:
13197 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13198 buffer += bytes_read;
13199 if (buffer > buf_end)
13200 {
13201 overflow = true;
13202 break;
13203 }
13204 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13205 buffer += bytes_read;
13206 if (buffer > buf_end)
13207 {
13208 overflow = true;
13209 break;
13210 }
13211 break;
13212 case DW_RLE_start_end:
13213 if (buffer + 2 * cu->header.addr_size > buf_end)
13214 {
13215 overflow = true;
13216 break;
13217 }
13218 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13219 buffer += bytes_read;
13220 range_end = read_address (obfd, buffer, cu, &bytes_read);
13221 buffer += bytes_read;
13222 break;
13223 default:
13224 complaint (&symfile_complaints,
13225 _("Invalid .debug_rnglists data (no base address)"));
13226 return false;
13227 }
13228 if (rlet == DW_RLE_end_of_list || overflow)
13229 break;
13230 if (rlet == DW_RLE_base_address)
13231 continue;
13232
13233 if (!found_base)
13234 {
13235 /* We have no valid base address for the ranges
13236 data. */
13237 complaint (&symfile_complaints,
13238 _("Invalid .debug_rnglists data (no base address)"));
13239 return false;
13240 }
13241
13242 if (range_beginning > range_end)
13243 {
13244 /* Inverted range entries are invalid. */
13245 complaint (&symfile_complaints,
13246 _("Invalid .debug_rnglists data (inverted range)"));
13247 return false;
13248 }
13249
13250 /* Empty range entries have no effect. */
13251 if (range_beginning == range_end)
13252 continue;
13253
13254 range_beginning += base;
13255 range_end += base;
13256
13257 /* A not-uncommon case of bad debug info.
13258 Don't pollute the addrmap with bad data. */
13259 if (range_beginning + baseaddr == 0
13260 && !dwarf2_per_objfile->has_section_at_zero)
13261 {
13262 complaint (&symfile_complaints,
13263 _(".debug_rnglists entry has start address of zero"
13264 " [in module %s]"), objfile_name (objfile));
13265 continue;
13266 }
13267
13268 callback (range_beginning, range_end);
13269 }
13270
13271 if (overflow)
13272 {
13273 complaint (&symfile_complaints,
13274 _("Offset %d is not terminated "
13275 "for DW_AT_ranges attribute"),
13276 offset);
13277 return false;
13278 }
13279
13280 return true;
13281}
13282
13283/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13284 Callback's type should be:
13285 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13286 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13287
43988095 13288template <typename Callback>
43039443 13289static int
5f46c5a5 13290dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13291 Callback &&callback)
43039443
JK
13292{
13293 struct objfile *objfile = cu->objfile;
3e29f34a 13294 struct gdbarch *gdbarch = get_objfile_arch (objfile);
43039443
JK
13295 struct comp_unit_head *cu_header = &cu->header;
13296 bfd *obfd = objfile->obfd;
13297 unsigned int addr_size = cu_header->addr_size;
13298 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13299 /* Base address selection entry. */
13300 CORE_ADDR base;
13301 int found_base;
13302 unsigned int dummy;
d521ce57 13303 const gdb_byte *buffer;
ff013f42 13304 CORE_ADDR baseaddr;
43039443 13305
43988095
JK
13306 if (cu_header->version >= 5)
13307 return dwarf2_rnglists_process (offset, cu, callback);
13308
d00adf39
DE
13309 found_base = cu->base_known;
13310 base = cu->base_address;
43039443 13311
be391dca 13312 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 13313 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
13314 {
13315 complaint (&symfile_complaints,
13316 _("Offset %d out of bounds for DW_AT_ranges attribute"),
13317 offset);
13318 return 0;
13319 }
dce234bc 13320 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 13321
e7030f15 13322 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 13323
43039443
JK
13324 while (1)
13325 {
13326 CORE_ADDR range_beginning, range_end;
13327
13328 range_beginning = read_address (obfd, buffer, cu, &dummy);
13329 buffer += addr_size;
13330 range_end = read_address (obfd, buffer, cu, &dummy);
13331 buffer += addr_size;
13332 offset += 2 * addr_size;
13333
13334 /* An end of list marker is a pair of zero addresses. */
13335 if (range_beginning == 0 && range_end == 0)
13336 /* Found the end of list entry. */
13337 break;
13338
13339 /* Each base address selection entry is a pair of 2 values.
13340 The first is the largest possible address, the second is
13341 the base address. Check for a base address here. */
13342 if ((range_beginning & mask) == mask)
13343 {
28d2bfb9
AB
13344 /* If we found the largest possible address, then we already
13345 have the base address in range_end. */
13346 base = range_end;
43039443
JK
13347 found_base = 1;
13348 continue;
13349 }
13350
13351 if (!found_base)
13352 {
13353 /* We have no valid base address for the ranges
13354 data. */
13355 complaint (&symfile_complaints,
13356 _("Invalid .debug_ranges data (no base address)"));
13357 return 0;
13358 }
13359
9277c30c
UW
13360 if (range_beginning > range_end)
13361 {
13362 /* Inverted range entries are invalid. */
13363 complaint (&symfile_complaints,
13364 _("Invalid .debug_ranges data (inverted range)"));
13365 return 0;
13366 }
13367
13368 /* Empty range entries have no effect. */
13369 if (range_beginning == range_end)
13370 continue;
13371
43039443
JK
13372 range_beginning += base;
13373 range_end += base;
13374
01093045
DE
13375 /* A not-uncommon case of bad debug info.
13376 Don't pollute the addrmap with bad data. */
13377 if (range_beginning + baseaddr == 0
13378 && !dwarf2_per_objfile->has_section_at_zero)
13379 {
13380 complaint (&symfile_complaints,
13381 _(".debug_ranges entry has start address of zero"
4262abfb 13382 " [in module %s]"), objfile_name (objfile));
01093045
DE
13383 continue;
13384 }
13385
5f46c5a5
JK
13386 callback (range_beginning, range_end);
13387 }
13388
13389 return 1;
13390}
13391
13392/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13393 Return 1 if the attributes are present and valid, otherwise, return 0.
13394 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13395
13396static int
13397dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13398 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13399 struct partial_symtab *ranges_pst)
13400{
13401 struct objfile *objfile = cu->objfile;
13402 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13403 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
13404 SECT_OFF_TEXT (objfile));
13405 int low_set = 0;
13406 CORE_ADDR low = 0;
13407 CORE_ADDR high = 0;
13408 int retval;
13409
13410 retval = dwarf2_ranges_process (offset, cu,
13411 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13412 {
9277c30c 13413 if (ranges_pst != NULL)
3e29f34a
MR
13414 {
13415 CORE_ADDR lowpc;
13416 CORE_ADDR highpc;
13417
13418 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
13419 range_beginning + baseaddr);
13420 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
13421 range_end + baseaddr);
13422 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
13423 ranges_pst);
13424 }
ff013f42 13425
43039443
JK
13426 /* FIXME: This is recording everything as a low-high
13427 segment of consecutive addresses. We should have a
13428 data structure for discontiguous block ranges
13429 instead. */
13430 if (! low_set)
13431 {
13432 low = range_beginning;
13433 high = range_end;
13434 low_set = 1;
13435 }
13436 else
13437 {
13438 if (range_beginning < low)
13439 low = range_beginning;
13440 if (range_end > high)
13441 high = range_end;
13442 }
5f46c5a5
JK
13443 });
13444 if (!retval)
13445 return 0;
43039443
JK
13446
13447 if (! low_set)
13448 /* If the first entry is an end-of-list marker, the range
13449 describes an empty scope, i.e. no instructions. */
13450 return 0;
13451
13452 if (low_return)
13453 *low_return = low;
13454 if (high_return)
13455 *high_return = high;
13456 return 1;
13457}
13458
3a2b436a
JK
13459/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13460 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13461 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13462
3a2b436a 13463static enum pc_bounds_kind
af34e669 13464dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
13465 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13466 struct partial_symtab *pst)
c906108c
SS
13467{
13468 struct attribute *attr;
91da1414 13469 struct attribute *attr_high;
af34e669
DJ
13470 CORE_ADDR low = 0;
13471 CORE_ADDR high = 0;
e385593e 13472 enum pc_bounds_kind ret;
c906108c 13473
91da1414
MW
13474 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13475 if (attr_high)
af34e669 13476 {
e142c38c 13477 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 13478 if (attr)
91da1414 13479 {
31aa7e4e
JB
13480 low = attr_value_as_address (attr);
13481 high = attr_value_as_address (attr_high);
13482 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
13483 high += low;
91da1414 13484 }
af34e669
DJ
13485 else
13486 /* Found high w/o low attribute. */
e385593e 13487 return PC_BOUNDS_INVALID;
af34e669
DJ
13488
13489 /* Found consecutive range of addresses. */
3a2b436a 13490 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13491 }
c906108c 13492 else
af34e669 13493 {
e142c38c 13494 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
13495 if (attr != NULL)
13496 {
ab435259
DE
13497 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
13498 We take advantage of the fact that DW_AT_ranges does not appear
13499 in DW_TAG_compile_unit of DWO files. */
13500 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13501 unsigned int ranges_offset = (DW_UNSND (attr)
13502 + (need_ranges_base
13503 ? cu->ranges_base
13504 : 0));
2e3cf129 13505
af34e669 13506 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 13507 .debug_ranges section. */
2e3cf129 13508 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 13509 return PC_BOUNDS_INVALID;
43039443 13510 /* Found discontinuous range of addresses. */
3a2b436a 13511 ret = PC_BOUNDS_RANGES;
af34e669 13512 }
e385593e
JK
13513 else
13514 return PC_BOUNDS_NOT_PRESENT;
af34e669 13515 }
c906108c 13516
9373cf26
JK
13517 /* read_partial_die has also the strict LOW < HIGH requirement. */
13518 if (high <= low)
e385593e 13519 return PC_BOUNDS_INVALID;
c906108c
SS
13520
13521 /* When using the GNU linker, .gnu.linkonce. sections are used to
13522 eliminate duplicate copies of functions and vtables and such.
13523 The linker will arbitrarily choose one and discard the others.
13524 The AT_*_pc values for such functions refer to local labels in
13525 these sections. If the section from that file was discarded, the
13526 labels are not in the output, so the relocs get a value of 0.
13527 If this is a discarded function, mark the pc bounds as invalid,
13528 so that GDB will ignore it. */
72dca2f5 13529 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 13530 return PC_BOUNDS_INVALID;
c906108c
SS
13531
13532 *lowpc = low;
96408a79
SA
13533 if (highpc)
13534 *highpc = high;
af34e669 13535 return ret;
c906108c
SS
13536}
13537
b084d499
JB
13538/* Assuming that DIE represents a subprogram DIE or a lexical block, get
13539 its low and high PC addresses. Do nothing if these addresses could not
13540 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13541 and HIGHPC to the high address if greater than HIGHPC. */
13542
13543static void
13544dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13545 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13546 struct dwarf2_cu *cu)
13547{
13548 CORE_ADDR low, high;
13549 struct die_info *child = die->child;
13550
e385593e 13551 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 13552 {
325fac50
PA
13553 *lowpc = std::min (*lowpc, low);
13554 *highpc = std::max (*highpc, high);
b084d499
JB
13555 }
13556
13557 /* If the language does not allow nested subprograms (either inside
13558 subprograms or lexical blocks), we're done. */
13559 if (cu->language != language_ada)
13560 return;
6e70227d 13561
b084d499
JB
13562 /* Check all the children of the given DIE. If it contains nested
13563 subprograms, then check their pc bounds. Likewise, we need to
13564 check lexical blocks as well, as they may also contain subprogram
13565 definitions. */
13566 while (child && child->tag)
13567 {
13568 if (child->tag == DW_TAG_subprogram
13569 || child->tag == DW_TAG_lexical_block)
13570 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13571 child = sibling_die (child);
13572 }
13573}
13574
fae299cd
DC
13575/* Get the low and high pc's represented by the scope DIE, and store
13576 them in *LOWPC and *HIGHPC. If the correct values can't be
13577 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13578
13579static void
13580get_scope_pc_bounds (struct die_info *die,
13581 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13582 struct dwarf2_cu *cu)
13583{
13584 CORE_ADDR best_low = (CORE_ADDR) -1;
13585 CORE_ADDR best_high = (CORE_ADDR) 0;
13586 CORE_ADDR current_low, current_high;
13587
3a2b436a 13588 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 13589 >= PC_BOUNDS_RANGES)
fae299cd
DC
13590 {
13591 best_low = current_low;
13592 best_high = current_high;
13593 }
13594 else
13595 {
13596 struct die_info *child = die->child;
13597
13598 while (child && child->tag)
13599 {
13600 switch (child->tag) {
13601 case DW_TAG_subprogram:
b084d499 13602 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
13603 break;
13604 case DW_TAG_namespace:
f55ee35c 13605 case DW_TAG_module:
fae299cd
DC
13606 /* FIXME: carlton/2004-01-16: Should we do this for
13607 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13608 that current GCC's always emit the DIEs corresponding
13609 to definitions of methods of classes as children of a
13610 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13611 the DIEs giving the declarations, which could be
13612 anywhere). But I don't see any reason why the
13613 standards says that they have to be there. */
13614 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13615
13616 if (current_low != ((CORE_ADDR) -1))
13617 {
325fac50
PA
13618 best_low = std::min (best_low, current_low);
13619 best_high = std::max (best_high, current_high);
fae299cd
DC
13620 }
13621 break;
13622 default:
0963b4bd 13623 /* Ignore. */
fae299cd
DC
13624 break;
13625 }
13626
13627 child = sibling_die (child);
13628 }
13629 }
13630
13631 *lowpc = best_low;
13632 *highpc = best_high;
13633}
13634
801e3a5b
JB
13635/* Record the address ranges for BLOCK, offset by BASEADDR, as given
13636 in DIE. */
380bca97 13637
801e3a5b
JB
13638static void
13639dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13640 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13641{
bb5ed363 13642 struct objfile *objfile = cu->objfile;
3e29f34a 13643 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 13644 struct attribute *attr;
91da1414 13645 struct attribute *attr_high;
801e3a5b 13646
91da1414
MW
13647 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13648 if (attr_high)
801e3a5b 13649 {
801e3a5b
JB
13650 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13651 if (attr)
13652 {
31aa7e4e
JB
13653 CORE_ADDR low = attr_value_as_address (attr);
13654 CORE_ADDR high = attr_value_as_address (attr_high);
13655
13656 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
13657 high += low;
9a619af0 13658
3e29f34a
MR
13659 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13660 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13661 record_block_range (block, low, high - 1);
801e3a5b
JB
13662 }
13663 }
13664
13665 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13666 if (attr)
13667 {
bb5ed363 13668 bfd *obfd = objfile->obfd;
ab435259
DE
13669 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
13670 We take advantage of the fact that DW_AT_ranges does not appear
13671 in DW_TAG_compile_unit of DWO files. */
13672 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
13673
13674 /* The value of the DW_AT_ranges attribute is the offset of the
13675 address range list in the .debug_ranges section. */
ab435259
DE
13676 unsigned long offset = (DW_UNSND (attr)
13677 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 13678 const gdb_byte *buffer;
801e3a5b
JB
13679
13680 /* For some target architectures, but not others, the
13681 read_address function sign-extends the addresses it returns.
13682 To recognize base address selection entries, we need a
13683 mask. */
13684 unsigned int addr_size = cu->header.addr_size;
13685 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13686
13687 /* The base address, to which the next pair is relative. Note
13688 that this 'base' is a DWARF concept: most entries in a range
13689 list are relative, to reduce the number of relocs against the
13690 debugging information. This is separate from this function's
13691 'baseaddr' argument, which GDB uses to relocate debugging
13692 information from a shared library based on the address at
13693 which the library was loaded. */
d00adf39
DE
13694 CORE_ADDR base = cu->base_address;
13695 int base_known = cu->base_known;
801e3a5b 13696
5f46c5a5
JK
13697 dwarf2_ranges_process (offset, cu,
13698 [&] (CORE_ADDR start, CORE_ADDR end)
13699 {
58fdfd2c
JK
13700 start += baseaddr;
13701 end += baseaddr;
5f46c5a5
JK
13702 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13703 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13704 record_block_range (block, start, end - 1);
13705 });
801e3a5b
JB
13706 }
13707}
13708
685b1105
JK
13709/* Check whether the producer field indicates either of GCC < 4.6, or the
13710 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 13711
685b1105
JK
13712static void
13713check_producer (struct dwarf2_cu *cu)
60d5a603 13714{
38360086 13715 int major, minor;
60d5a603
JK
13716
13717 if (cu->producer == NULL)
13718 {
13719 /* For unknown compilers expect their behavior is DWARF version
13720 compliant.
13721
13722 GCC started to support .debug_types sections by -gdwarf-4 since
13723 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13724 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13725 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13726 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 13727 }
b1ffba5a 13728 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 13729 {
38360086
MW
13730 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13731 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 13732 }
5230b05a
WT
13733 else if (producer_is_icc (cu->producer, &major, &minor))
13734 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
13735 else
13736 {
13737 /* For other non-GCC compilers, expect their behavior is DWARF version
13738 compliant. */
60d5a603
JK
13739 }
13740
ba919b58 13741 cu->checked_producer = 1;
685b1105 13742}
ba919b58 13743
685b1105
JK
13744/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13745 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13746 during 4.6.0 experimental. */
13747
13748static int
13749producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
13750{
13751 if (!cu->checked_producer)
13752 check_producer (cu);
13753
13754 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
13755}
13756
13757/* Return the default accessibility type if it is not overriden by
13758 DW_AT_accessibility. */
13759
13760static enum dwarf_access_attribute
13761dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
13762{
13763 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
13764 {
13765 /* The default DWARF 2 accessibility for members is public, the default
13766 accessibility for inheritance is private. */
13767
13768 if (die->tag != DW_TAG_inheritance)
13769 return DW_ACCESS_public;
13770 else
13771 return DW_ACCESS_private;
13772 }
13773 else
13774 {
13775 /* DWARF 3+ defines the default accessibility a different way. The same
13776 rules apply now for DW_TAG_inheritance as for the members and it only
13777 depends on the container kind. */
13778
13779 if (die->parent->tag == DW_TAG_class_type)
13780 return DW_ACCESS_private;
13781 else
13782 return DW_ACCESS_public;
13783 }
13784}
13785
74ac6d43
TT
13786/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
13787 offset. If the attribute was not found return 0, otherwise return
13788 1. If it was found but could not properly be handled, set *OFFSET
13789 to 0. */
13790
13791static int
13792handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
13793 LONGEST *offset)
13794{
13795 struct attribute *attr;
13796
13797 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13798 if (attr != NULL)
13799 {
13800 *offset = 0;
13801
13802 /* Note that we do not check for a section offset first here.
13803 This is because DW_AT_data_member_location is new in DWARF 4,
13804 so if we see it, we can assume that a constant form is really
13805 a constant and not a section offset. */
13806 if (attr_form_is_constant (attr))
13807 *offset = dwarf2_get_attr_constant_value (attr, 0);
13808 else if (attr_form_is_section_offset (attr))
13809 dwarf2_complex_location_expr_complaint ();
13810 else if (attr_form_is_block (attr))
13811 *offset = decode_locdesc (DW_BLOCK (attr), cu);
13812 else
13813 dwarf2_complex_location_expr_complaint ();
13814
13815 return 1;
13816 }
13817
13818 return 0;
13819}
13820
c906108c
SS
13821/* Add an aggregate field to the field list. */
13822
13823static void
107d2387 13824dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 13825 struct dwarf2_cu *cu)
6e70227d 13826{
e7c27a73 13827 struct objfile *objfile = cu->objfile;
5e2b427d 13828 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13829 struct nextfield *new_field;
13830 struct attribute *attr;
13831 struct field *fp;
15d034d0 13832 const char *fieldname = "";
c906108c
SS
13833
13834 /* Allocate a new field list entry and link it in. */
8d749320 13835 new_field = XNEW (struct nextfield);
b8c9b27d 13836 make_cleanup (xfree, new_field);
c906108c 13837 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
13838
13839 if (die->tag == DW_TAG_inheritance)
13840 {
13841 new_field->next = fip->baseclasses;
13842 fip->baseclasses = new_field;
13843 }
13844 else
13845 {
13846 new_field->next = fip->fields;
13847 fip->fields = new_field;
13848 }
c906108c
SS
13849 fip->nfields++;
13850
e142c38c 13851 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
13852 if (attr)
13853 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
13854 else
13855 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
13856 if (new_field->accessibility != DW_ACCESS_public)
13857 fip->non_public_fields = 1;
60d5a603 13858
e142c38c 13859 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
13860 if (attr)
13861 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
13862 else
13863 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
13864
13865 fp = &new_field->field;
a9a9bd0f 13866
e142c38c 13867 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 13868 {
74ac6d43
TT
13869 LONGEST offset;
13870
a9a9bd0f 13871 /* Data member other than a C++ static data member. */
6e70227d 13872
c906108c 13873 /* Get type of field. */
e7c27a73 13874 fp->type = die_type (die, cu);
c906108c 13875
d6a843b5 13876 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 13877
c906108c 13878 /* Get bit size of field (zero if none). */
e142c38c 13879 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
13880 if (attr)
13881 {
13882 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
13883 }
13884 else
13885 {
13886 FIELD_BITSIZE (*fp) = 0;
13887 }
13888
13889 /* Get bit offset of field. */
74ac6d43
TT
13890 if (handle_data_member_location (die, cu, &offset))
13891 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 13892 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
13893 if (attr)
13894 {
5e2b427d 13895 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
13896 {
13897 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
13898 additional bit offset from the MSB of the containing
13899 anonymous object to the MSB of the field. We don't
13900 have to do anything special since we don't need to
13901 know the size of the anonymous object. */
f41f5e61 13902 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
13903 }
13904 else
13905 {
13906 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
13907 MSB of the anonymous object, subtract off the number of
13908 bits from the MSB of the field to the MSB of the
13909 object, and then subtract off the number of bits of
13910 the field itself. The result is the bit offset of
13911 the LSB of the field. */
c906108c
SS
13912 int anonymous_size;
13913 int bit_offset = DW_UNSND (attr);
13914
e142c38c 13915 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13916 if (attr)
13917 {
13918 /* The size of the anonymous object containing
13919 the bit field is explicit, so use the
13920 indicated size (in bytes). */
13921 anonymous_size = DW_UNSND (attr);
13922 }
13923 else
13924 {
13925 /* The size of the anonymous object containing
13926 the bit field must be inferred from the type
13927 attribute of the data member containing the
13928 bit field. */
13929 anonymous_size = TYPE_LENGTH (fp->type);
13930 }
f41f5e61
PA
13931 SET_FIELD_BITPOS (*fp,
13932 (FIELD_BITPOS (*fp)
13933 + anonymous_size * bits_per_byte
13934 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
13935 }
13936 }
da5b30da
AA
13937 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13938 if (attr != NULL)
13939 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
13940 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
13941
13942 /* Get name of field. */
39cbfefa
DJ
13943 fieldname = dwarf2_name (die, cu);
13944 if (fieldname == NULL)
13945 fieldname = "";
d8151005
DJ
13946
13947 /* The name is already allocated along with this objfile, so we don't
13948 need to duplicate it for the type. */
13949 fp->name = fieldname;
c906108c
SS
13950
13951 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 13952 pointer or virtual base class pointer) to private. */
e142c38c 13953 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 13954 {
d48cc9dd 13955 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
13956 new_field->accessibility = DW_ACCESS_private;
13957 fip->non_public_fields = 1;
13958 }
13959 }
a9a9bd0f 13960 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 13961 {
a9a9bd0f
DC
13962 /* C++ static member. */
13963
13964 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13965 is a declaration, but all versions of G++ as of this writing
13966 (so through at least 3.2.1) incorrectly generate
13967 DW_TAG_variable tags. */
6e70227d 13968
ff355380 13969 const char *physname;
c906108c 13970
a9a9bd0f 13971 /* Get name of field. */
39cbfefa
DJ
13972 fieldname = dwarf2_name (die, cu);
13973 if (fieldname == NULL)
c906108c
SS
13974 return;
13975
254e6b9e 13976 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
13977 if (attr
13978 /* Only create a symbol if this is an external value.
13979 new_symbol checks this and puts the value in the global symbol
13980 table, which we want. If it is not external, new_symbol
13981 will try to put the value in cu->list_in_scope which is wrong. */
13982 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
13983 {
13984 /* A static const member, not much different than an enum as far as
13985 we're concerned, except that we can support more types. */
13986 new_symbol (die, NULL, cu);
13987 }
13988
2df3850c 13989 /* Get physical name. */
ff355380 13990 physname = dwarf2_physname (fieldname, die, cu);
c906108c 13991
d8151005
DJ
13992 /* The name is already allocated along with this objfile, so we don't
13993 need to duplicate it for the type. */
13994 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 13995 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 13996 FIELD_NAME (*fp) = fieldname;
c906108c
SS
13997 }
13998 else if (die->tag == DW_TAG_inheritance)
13999 {
74ac6d43 14000 LONGEST offset;
d4b96c9a 14001
74ac6d43
TT
14002 /* C++ base class field. */
14003 if (handle_data_member_location (die, cu, &offset))
14004 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 14005 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14006 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
14007 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
14008 fip->nbaseclasses++;
14009 }
14010}
14011
98751a41
JK
14012/* Add a typedef defined in the scope of the FIP's class. */
14013
14014static void
14015dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
14016 struct dwarf2_cu *cu)
6e70227d 14017{
98751a41 14018 struct typedef_field_list *new_field;
98751a41 14019 struct typedef_field *fp;
98751a41
JK
14020
14021 /* Allocate a new field list entry and link it in. */
8d749320 14022 new_field = XCNEW (struct typedef_field_list);
98751a41
JK
14023 make_cleanup (xfree, new_field);
14024
14025 gdb_assert (die->tag == DW_TAG_typedef);
14026
14027 fp = &new_field->field;
14028
14029 /* Get name of field. */
14030 fp->name = dwarf2_name (die, cu);
14031 if (fp->name == NULL)
14032 return;
14033
14034 fp->type = read_type_die (die, cu);
14035
c191a687
KS
14036 /* Save accessibility. */
14037 enum dwarf_access_attribute accessibility;
14038 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14039 if (attr != NULL)
14040 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14041 else
14042 accessibility = dwarf2_default_access_attribute (die, cu);
14043 switch (accessibility)
14044 {
14045 case DW_ACCESS_public:
14046 /* The assumed value if neither private nor protected. */
14047 break;
14048 case DW_ACCESS_private:
14049 fp->is_private = 1;
14050 break;
14051 case DW_ACCESS_protected:
14052 fp->is_protected = 1;
14053 break;
14054 default:
37534686
KS
14055 complaint (&symfile_complaints,
14056 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14057 }
14058
98751a41
JK
14059 new_field->next = fip->typedef_field_list;
14060 fip->typedef_field_list = new_field;
14061 fip->typedef_field_list_count++;
14062}
14063
c906108c
SS
14064/* Create the vector of fields, and attach it to the type. */
14065
14066static void
fba45db2 14067dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14068 struct dwarf2_cu *cu)
c906108c
SS
14069{
14070 int nfields = fip->nfields;
14071
14072 /* Record the field count, allocate space for the array of fields,
14073 and create blank accessibility bitfields if necessary. */
14074 TYPE_NFIELDS (type) = nfields;
14075 TYPE_FIELDS (type) = (struct field *)
14076 TYPE_ALLOC (type, sizeof (struct field) * nfields);
14077 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
14078
b4ba55a1 14079 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14080 {
14081 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14082
14083 TYPE_FIELD_PRIVATE_BITS (type) =
14084 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14085 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14086
14087 TYPE_FIELD_PROTECTED_BITS (type) =
14088 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14089 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14090
774b6a14
TT
14091 TYPE_FIELD_IGNORE_BITS (type) =
14092 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14093 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14094 }
14095
14096 /* If the type has baseclasses, allocate and clear a bit vector for
14097 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 14098 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
14099 {
14100 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 14101 unsigned char *pointer;
c906108c
SS
14102
14103 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14104 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14105 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
14106 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
14107 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
14108 }
14109
3e43a32a
MS
14110 /* Copy the saved-up fields into the field vector. Start from the head of
14111 the list, adding to the tail of the field array, so that they end up in
14112 the same order in the array in which they were added to the list. */
c906108c
SS
14113 while (nfields-- > 0)
14114 {
7d0ccb61
DJ
14115 struct nextfield *fieldp;
14116
14117 if (fip->fields)
14118 {
14119 fieldp = fip->fields;
14120 fip->fields = fieldp->next;
14121 }
14122 else
14123 {
14124 fieldp = fip->baseclasses;
14125 fip->baseclasses = fieldp->next;
14126 }
14127
14128 TYPE_FIELD (type, nfields) = fieldp->field;
14129 switch (fieldp->accessibility)
c906108c 14130 {
c5aa993b 14131 case DW_ACCESS_private:
b4ba55a1
JB
14132 if (cu->language != language_ada)
14133 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 14134 break;
c906108c 14135
c5aa993b 14136 case DW_ACCESS_protected:
b4ba55a1
JB
14137 if (cu->language != language_ada)
14138 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 14139 break;
c906108c 14140
c5aa993b
JM
14141 case DW_ACCESS_public:
14142 break;
c906108c 14143
c5aa993b
JM
14144 default:
14145 /* Unknown accessibility. Complain and treat it as public. */
14146 {
e2e0b3e5 14147 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 14148 fieldp->accessibility);
c5aa993b
JM
14149 }
14150 break;
c906108c
SS
14151 }
14152 if (nfields < fip->nbaseclasses)
14153 {
7d0ccb61 14154 switch (fieldp->virtuality)
c906108c 14155 {
c5aa993b
JM
14156 case DW_VIRTUALITY_virtual:
14157 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14158 if (cu->language == language_ada)
a73c6dcd 14159 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
14160 SET_TYPE_FIELD_VIRTUAL (type, nfields);
14161 break;
c906108c
SS
14162 }
14163 }
c906108c
SS
14164 }
14165}
14166
7d27a96d
TT
14167/* Return true if this member function is a constructor, false
14168 otherwise. */
14169
14170static int
14171dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14172{
14173 const char *fieldname;
fe978cb0 14174 const char *type_name;
7d27a96d
TT
14175 int len;
14176
14177 if (die->parent == NULL)
14178 return 0;
14179
14180 if (die->parent->tag != DW_TAG_structure_type
14181 && die->parent->tag != DW_TAG_union_type
14182 && die->parent->tag != DW_TAG_class_type)
14183 return 0;
14184
14185 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14186 type_name = dwarf2_name (die->parent, cu);
14187 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14188 return 0;
14189
14190 len = strlen (fieldname);
fe978cb0
PA
14191 return (strncmp (fieldname, type_name, len) == 0
14192 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14193}
14194
c906108c
SS
14195/* Add a member function to the proper fieldlist. */
14196
14197static void
107d2387 14198dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14199 struct type *type, struct dwarf2_cu *cu)
c906108c 14200{
e7c27a73 14201 struct objfile *objfile = cu->objfile;
c906108c
SS
14202 struct attribute *attr;
14203 struct fnfieldlist *flp;
14204 int i;
14205 struct fn_field *fnp;
15d034d0 14206 const char *fieldname;
c906108c 14207 struct nextfnfield *new_fnfield;
f792889a 14208 struct type *this_type;
60d5a603 14209 enum dwarf_access_attribute accessibility;
c906108c 14210
b4ba55a1 14211 if (cu->language == language_ada)
a73c6dcd 14212 error (_("unexpected member function in Ada type"));
b4ba55a1 14213
2df3850c 14214 /* Get name of member function. */
39cbfefa
DJ
14215 fieldname = dwarf2_name (die, cu);
14216 if (fieldname == NULL)
2df3850c 14217 return;
c906108c 14218
c906108c
SS
14219 /* Look up member function name in fieldlist. */
14220 for (i = 0; i < fip->nfnfields; i++)
14221 {
27bfe10e 14222 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
14223 break;
14224 }
14225
14226 /* Create new list element if necessary. */
14227 if (i < fip->nfnfields)
14228 flp = &fip->fnfieldlists[i];
14229 else
14230 {
14231 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
14232 {
14233 fip->fnfieldlists = (struct fnfieldlist *)
14234 xrealloc (fip->fnfieldlists,
14235 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 14236 * sizeof (struct fnfieldlist));
c906108c 14237 if (fip->nfnfields == 0)
c13c43fd 14238 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
14239 }
14240 flp = &fip->fnfieldlists[fip->nfnfields];
14241 flp->name = fieldname;
14242 flp->length = 0;
14243 flp->head = NULL;
3da10d80 14244 i = fip->nfnfields++;
c906108c
SS
14245 }
14246
14247 /* Create a new member function field and chain it to the field list
0963b4bd 14248 entry. */
8d749320 14249 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 14250 make_cleanup (xfree, new_fnfield);
c906108c
SS
14251 memset (new_fnfield, 0, sizeof (struct nextfnfield));
14252 new_fnfield->next = flp->head;
14253 flp->head = new_fnfield;
14254 flp->length++;
14255
14256 /* Fill in the member function field info. */
14257 fnp = &new_fnfield->fnfield;
3da10d80
KS
14258
14259 /* Delay processing of the physname until later. */
9c37b5ae 14260 if (cu->language == language_cplus)
3da10d80
KS
14261 {
14262 add_to_method_list (type, i, flp->length - 1, fieldname,
14263 die, cu);
14264 }
14265 else
14266 {
1d06ead6 14267 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14268 fnp->physname = physname ? physname : "";
14269 }
14270
c906108c 14271 fnp->type = alloc_type (objfile);
f792889a
DJ
14272 this_type = read_type_die (die, cu);
14273 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 14274 {
f792889a 14275 int nparams = TYPE_NFIELDS (this_type);
c906108c 14276
f792889a 14277 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14278 of the method itself (TYPE_CODE_METHOD). */
14279 smash_to_method_type (fnp->type, type,
f792889a
DJ
14280 TYPE_TARGET_TYPE (this_type),
14281 TYPE_FIELDS (this_type),
14282 TYPE_NFIELDS (this_type),
14283 TYPE_VARARGS (this_type));
c906108c
SS
14284
14285 /* Handle static member functions.
c5aa993b 14286 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
14287 member functions. G++ helps GDB by marking the first
14288 parameter for non-static member functions (which is the this
14289 pointer) as artificial. We obtain this information from
14290 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 14291 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
14292 fnp->voffset = VOFFSET_STATIC;
14293 }
14294 else
e2e0b3e5 14295 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 14296 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
14297
14298 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 14299 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 14300 fnp->fcontext = die_containing_type (die, cu);
c906108c 14301
3e43a32a
MS
14302 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14303 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
14304
14305 /* Get accessibility. */
e142c38c 14306 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 14307 if (attr)
aead7601 14308 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
14309 else
14310 accessibility = dwarf2_default_access_attribute (die, cu);
14311 switch (accessibility)
c906108c 14312 {
60d5a603
JK
14313 case DW_ACCESS_private:
14314 fnp->is_private = 1;
14315 break;
14316 case DW_ACCESS_protected:
14317 fnp->is_protected = 1;
14318 break;
c906108c
SS
14319 }
14320
b02dede2 14321 /* Check for artificial methods. */
e142c38c 14322 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
14323 if (attr && DW_UNSND (attr) != 0)
14324 fnp->is_artificial = 1;
14325
7d27a96d
TT
14326 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14327
0d564a31 14328 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
14329 function. For older versions of GCC, this is an offset in the
14330 appropriate virtual table, as specified by DW_AT_containing_type.
14331 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
14332 to the object address. */
14333
e142c38c 14334 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 14335 if (attr)
8e19ed76 14336 {
aec5aa8b 14337 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 14338 {
aec5aa8b
TT
14339 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14340 {
14341 /* Old-style GCC. */
14342 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14343 }
14344 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14345 || (DW_BLOCK (attr)->size > 1
14346 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14347 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14348 {
aec5aa8b
TT
14349 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14350 if ((fnp->voffset % cu->header.addr_size) != 0)
14351 dwarf2_complex_location_expr_complaint ();
14352 else
14353 fnp->voffset /= cu->header.addr_size;
14354 fnp->voffset += 2;
14355 }
14356 else
14357 dwarf2_complex_location_expr_complaint ();
14358
14359 if (!fnp->fcontext)
7e993ebf
KS
14360 {
14361 /* If there is no `this' field and no DW_AT_containing_type,
14362 we cannot actually find a base class context for the
14363 vtable! */
14364 if (TYPE_NFIELDS (this_type) == 0
14365 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14366 {
14367 complaint (&symfile_complaints,
14368 _("cannot determine context for virtual member "
14369 "function \"%s\" (offset %d)"),
9c541725 14370 fieldname, to_underlying (die->sect_off));
7e993ebf
KS
14371 }
14372 else
14373 {
14374 fnp->fcontext
14375 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14376 }
14377 }
aec5aa8b 14378 }
3690dd37 14379 else if (attr_form_is_section_offset (attr))
8e19ed76 14380 {
4d3c2250 14381 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14382 }
14383 else
14384 {
4d3c2250
KB
14385 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14386 fieldname);
8e19ed76 14387 }
0d564a31 14388 }
d48cc9dd
DJ
14389 else
14390 {
14391 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14392 if (attr && DW_UNSND (attr))
14393 {
14394 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14395 complaint (&symfile_complaints,
3e43a32a
MS
14396 _("Member function \"%s\" (offset %d) is virtual "
14397 "but the vtable offset is not specified"),
9c541725 14398 fieldname, to_underlying (die->sect_off));
9655fd1a 14399 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
14400 TYPE_CPLUS_DYNAMIC (type) = 1;
14401 }
14402 }
c906108c
SS
14403}
14404
14405/* Create the vector of member function fields, and attach it to the type. */
14406
14407static void
fba45db2 14408dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14409 struct dwarf2_cu *cu)
c906108c
SS
14410{
14411 struct fnfieldlist *flp;
c906108c
SS
14412 int i;
14413
b4ba55a1 14414 if (cu->language == language_ada)
a73c6dcd 14415 error (_("unexpected member functions in Ada type"));
b4ba55a1 14416
c906108c
SS
14417 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14418 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14419 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
14420
14421 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
14422 {
14423 struct nextfnfield *nfp = flp->head;
14424 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14425 int k;
14426
14427 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
14428 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
14429 fn_flp->fn_fields = (struct fn_field *)
14430 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
14431 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 14432 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
14433 }
14434
14435 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
14436}
14437
1168df01
JB
14438/* Returns non-zero if NAME is the name of a vtable member in CU's
14439 language, zero otherwise. */
14440static int
14441is_vtable_name (const char *name, struct dwarf2_cu *cu)
14442{
14443 static const char vptr[] = "_vptr";
987504bb 14444 static const char vtable[] = "vtable";
1168df01 14445
9c37b5ae
TT
14446 /* Look for the C++ form of the vtable. */
14447 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
14448 return 1;
14449
14450 return 0;
14451}
14452
c0dd20ea 14453/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
14454 functions, with the ABI-specified layout. If TYPE describes
14455 such a structure, smash it into a member function type.
61049d3b
DJ
14456
14457 GCC shouldn't do this; it should just output pointer to member DIEs.
14458 This is GCC PR debug/28767. */
c0dd20ea 14459
0b92b5bb
TT
14460static void
14461quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 14462{
09e2d7c7 14463 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
14464
14465 /* Check for a structure with no name and two children. */
0b92b5bb
TT
14466 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14467 return;
c0dd20ea
DJ
14468
14469 /* Check for __pfn and __delta members. */
0b92b5bb
TT
14470 if (TYPE_FIELD_NAME (type, 0) == NULL
14471 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14472 || TYPE_FIELD_NAME (type, 1) == NULL
14473 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14474 return;
c0dd20ea
DJ
14475
14476 /* Find the type of the method. */
0b92b5bb 14477 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
14478 if (pfn_type == NULL
14479 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14480 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 14481 return;
c0dd20ea
DJ
14482
14483 /* Look for the "this" argument. */
14484 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14485 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 14486 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 14487 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 14488 return;
c0dd20ea 14489
09e2d7c7 14490 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 14491 new_type = alloc_type (objfile);
09e2d7c7 14492 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
14493 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14494 TYPE_VARARGS (pfn_type));
0b92b5bb 14495 smash_to_methodptr_type (type, new_type);
c0dd20ea 14496}
1168df01 14497
685b1105 14498
c906108c 14499/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
14500 (definition) to create a type for the structure or union. Fill in
14501 the type's name and general properties; the members will not be
83655187
DE
14502 processed until process_structure_scope. A symbol table entry for
14503 the type will also not be done until process_structure_scope (assuming
14504 the type has a name).
c906108c 14505
c767944b
DJ
14506 NOTE: we need to call these functions regardless of whether or not the
14507 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 14508 structure or union. This gets the type entered into our set of
83655187 14509 user defined types. */
c906108c 14510
f792889a 14511static struct type *
134d01f1 14512read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14513{
e7c27a73 14514 struct objfile *objfile = cu->objfile;
c906108c
SS
14515 struct type *type;
14516 struct attribute *attr;
15d034d0 14517 const char *name;
c906108c 14518
348e048f
DE
14519 /* If the definition of this type lives in .debug_types, read that type.
14520 Don't follow DW_AT_specification though, that will take us back up
14521 the chain and we want to go down. */
45e58e77 14522 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
14523 if (attr)
14524 {
ac9ec31b 14525 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 14526
ac9ec31b 14527 /* The type's CU may not be the same as CU.
02142a6c 14528 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
14529 return set_die_type (die, type, cu);
14530 }
14531
c0dd20ea 14532 type = alloc_type (objfile);
c906108c 14533 INIT_CPLUS_SPECIFIC (type);
93311388 14534
39cbfefa
DJ
14535 name = dwarf2_name (die, cu);
14536 if (name != NULL)
c906108c 14537 {
987504bb 14538 if (cu->language == language_cplus
c44af4eb
TT
14539 || cu->language == language_d
14540 || cu->language == language_rust)
63d06c5c 14541 {
15d034d0 14542 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
14543
14544 /* dwarf2_full_name might have already finished building the DIE's
14545 type. If so, there is no need to continue. */
14546 if (get_die_type (die, cu) != NULL)
14547 return get_die_type (die, cu);
14548
14549 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
14550 if (die->tag == DW_TAG_structure_type
14551 || die->tag == DW_TAG_class_type)
14552 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
14553 }
14554 else
14555 {
d8151005
DJ
14556 /* The name is already allocated along with this objfile, so
14557 we don't need to duplicate it for the type. */
7d455152 14558 TYPE_TAG_NAME (type) = name;
94af9270
KS
14559 if (die->tag == DW_TAG_class_type)
14560 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 14561 }
c906108c
SS
14562 }
14563
14564 if (die->tag == DW_TAG_structure_type)
14565 {
14566 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14567 }
14568 else if (die->tag == DW_TAG_union_type)
14569 {
14570 TYPE_CODE (type) = TYPE_CODE_UNION;
14571 }
14572 else
14573 {
4753d33b 14574 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
14575 }
14576
0cc2414c
TT
14577 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
14578 TYPE_DECLARED_CLASS (type) = 1;
14579
e142c38c 14580 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14581 if (attr)
14582 {
155bfbd3
JB
14583 if (attr_form_is_constant (attr))
14584 TYPE_LENGTH (type) = DW_UNSND (attr);
14585 else
14586 {
14587 /* For the moment, dynamic type sizes are not supported
14588 by GDB's struct type. The actual size is determined
14589 on-demand when resolving the type of a given object,
14590 so set the type's length to zero for now. Otherwise,
14591 we record an expression as the length, and that expression
14592 could lead to a very large value, which could eventually
14593 lead to us trying to allocate that much memory when creating
14594 a value of that type. */
14595 TYPE_LENGTH (type) = 0;
14596 }
c906108c
SS
14597 }
14598 else
14599 {
14600 TYPE_LENGTH (type) = 0;
14601 }
14602
5230b05a 14603 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 14604 {
5230b05a
WT
14605 /* ICC<14 does not output the required DW_AT_declaration on
14606 incomplete types, but gives them a size of zero. */
422b1cb0 14607 TYPE_STUB (type) = 1;
685b1105
JK
14608 }
14609 else
14610 TYPE_STUB_SUPPORTED (type) = 1;
14611
dc718098 14612 if (die_is_declaration (die, cu))
876cecd0 14613 TYPE_STUB (type) = 1;
a6c727b2
DJ
14614 else if (attr == NULL && die->child == NULL
14615 && producer_is_realview (cu->producer))
14616 /* RealView does not output the required DW_AT_declaration
14617 on incomplete types. */
14618 TYPE_STUB (type) = 1;
dc718098 14619
c906108c
SS
14620 /* We need to add the type field to the die immediately so we don't
14621 infinitely recurse when dealing with pointers to the structure
0963b4bd 14622 type within the structure itself. */
1c379e20 14623 set_die_type (die, type, cu);
c906108c 14624
7e314c57
JK
14625 /* set_die_type should be already done. */
14626 set_descriptive_type (type, die, cu);
14627
c767944b
DJ
14628 return type;
14629}
14630
14631/* Finish creating a structure or union type, including filling in
14632 its members and creating a symbol for it. */
14633
14634static void
14635process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
14636{
14637 struct objfile *objfile = cu->objfile;
ca040673 14638 struct die_info *child_die;
c767944b
DJ
14639 struct type *type;
14640
14641 type = get_die_type (die, cu);
14642 if (type == NULL)
14643 type = read_structure_type (die, cu);
14644
e142c38c 14645 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
14646 {
14647 struct field_info fi;
2f4732b0 14648 std::vector<struct symbol *> template_args;
c767944b 14649 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
14650
14651 memset (&fi, 0, sizeof (struct field_info));
14652
639d11d3 14653 child_die = die->child;
c906108c
SS
14654
14655 while (child_die && child_die->tag)
14656 {
a9a9bd0f
DC
14657 if (child_die->tag == DW_TAG_member
14658 || child_die->tag == DW_TAG_variable)
c906108c 14659 {
a9a9bd0f
DC
14660 /* NOTE: carlton/2002-11-05: A C++ static data member
14661 should be a DW_TAG_member that is a declaration, but
14662 all versions of G++ as of this writing (so through at
14663 least 3.2.1) incorrectly generate DW_TAG_variable
14664 tags for them instead. */
e7c27a73 14665 dwarf2_add_field (&fi, child_die, cu);
c906108c 14666 }
8713b1b1 14667 else if (child_die->tag == DW_TAG_subprogram)
c906108c 14668 {
e98c9e7c
TT
14669 /* Rust doesn't have member functions in the C++ sense.
14670 However, it does emit ordinary functions as children
14671 of a struct DIE. */
14672 if (cu->language == language_rust)
14673 read_func_scope (child_die, cu);
14674 else
14675 {
14676 /* C++ member function. */
14677 dwarf2_add_member_fn (&fi, child_die, type, cu);
14678 }
c906108c
SS
14679 }
14680 else if (child_die->tag == DW_TAG_inheritance)
14681 {
14682 /* C++ base class field. */
e7c27a73 14683 dwarf2_add_field (&fi, child_die, cu);
c906108c 14684 }
98751a41
JK
14685 else if (child_die->tag == DW_TAG_typedef)
14686 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
14687 else if (child_die->tag == DW_TAG_template_type_param
14688 || child_die->tag == DW_TAG_template_value_param)
14689 {
14690 struct symbol *arg = new_symbol (child_die, NULL, cu);
14691
f1078f66 14692 if (arg != NULL)
2f4732b0 14693 template_args.push_back (arg);
34eaf542
TT
14694 }
14695
c906108c
SS
14696 child_die = sibling_die (child_die);
14697 }
14698
34eaf542 14699 /* Attach template arguments to type. */
2f4732b0 14700 if (!template_args.empty ())
34eaf542
TT
14701 {
14702 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 14703 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 14704 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
14705 = XOBNEWVEC (&objfile->objfile_obstack,
14706 struct symbol *,
14707 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 14708 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 14709 template_args.data (),
34eaf542
TT
14710 (TYPE_N_TEMPLATE_ARGUMENTS (type)
14711 * sizeof (struct symbol *)));
34eaf542
TT
14712 }
14713
c906108c
SS
14714 /* Attach fields and member functions to the type. */
14715 if (fi.nfields)
e7c27a73 14716 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
14717 if (fi.nfnfields)
14718 {
e7c27a73 14719 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 14720
c5aa993b 14721 /* Get the type which refers to the base class (possibly this
c906108c 14722 class itself) which contains the vtable pointer for the current
0d564a31
DJ
14723 class from the DW_AT_containing_type attribute. This use of
14724 DW_AT_containing_type is a GNU extension. */
c906108c 14725
e142c38c 14726 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 14727 {
e7c27a73 14728 struct type *t = die_containing_type (die, cu);
c906108c 14729
ae6ae975 14730 set_type_vptr_basetype (type, t);
c906108c
SS
14731 if (type == t)
14732 {
c906108c
SS
14733 int i;
14734
14735 /* Our own class provides vtbl ptr. */
14736 for (i = TYPE_NFIELDS (t) - 1;
14737 i >= TYPE_N_BASECLASSES (t);
14738 --i)
14739 {
0d5cff50 14740 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 14741
1168df01 14742 if (is_vtable_name (fieldname, cu))
c906108c 14743 {
ae6ae975 14744 set_type_vptr_fieldno (type, i);
c906108c
SS
14745 break;
14746 }
14747 }
14748
14749 /* Complain if virtual function table field not found. */
14750 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 14751 complaint (&symfile_complaints,
3e43a32a
MS
14752 _("virtual function table pointer "
14753 "not found when defining class '%s'"),
4d3c2250
KB
14754 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
14755 "");
c906108c
SS
14756 }
14757 else
14758 {
ae6ae975 14759 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
14760 }
14761 }
f6235d4c 14762 else if (cu->producer
61012eef 14763 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
14764 {
14765 /* The IBM XLC compiler does not provide direct indication
14766 of the containing type, but the vtable pointer is
14767 always named __vfp. */
14768
14769 int i;
14770
14771 for (i = TYPE_NFIELDS (type) - 1;
14772 i >= TYPE_N_BASECLASSES (type);
14773 --i)
14774 {
14775 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
14776 {
ae6ae975
DE
14777 set_type_vptr_fieldno (type, i);
14778 set_type_vptr_basetype (type, type);
f6235d4c
EZ
14779 break;
14780 }
14781 }
14782 }
c906108c 14783 }
98751a41
JK
14784
14785 /* Copy fi.typedef_field_list linked list elements content into the
14786 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
14787 if (fi.typedef_field_list)
14788 {
14789 int i = fi.typedef_field_list_count;
14790
a0d7a4ff 14791 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 14792 TYPE_TYPEDEF_FIELD_ARRAY (type)
224c3ddb
SM
14793 = ((struct typedef_field *)
14794 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
14795 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
14796
14797 /* Reverse the list order to keep the debug info elements order. */
14798 while (--i >= 0)
14799 {
14800 struct typedef_field *dest, *src;
6e70227d 14801
98751a41
JK
14802 dest = &TYPE_TYPEDEF_FIELD (type, i);
14803 src = &fi.typedef_field_list->field;
14804 fi.typedef_field_list = fi.typedef_field_list->next;
14805 *dest = *src;
14806 }
14807 }
c767944b
DJ
14808
14809 do_cleanups (back_to);
c906108c 14810 }
63d06c5c 14811
bb5ed363 14812 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 14813
90aeadfc
DC
14814 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
14815 snapshots) has been known to create a die giving a declaration
14816 for a class that has, as a child, a die giving a definition for a
14817 nested class. So we have to process our children even if the
14818 current die is a declaration. Normally, of course, a declaration
14819 won't have any children at all. */
134d01f1 14820
ca040673
DE
14821 child_die = die->child;
14822
90aeadfc
DC
14823 while (child_die != NULL && child_die->tag)
14824 {
14825 if (child_die->tag == DW_TAG_member
14826 || child_die->tag == DW_TAG_variable
34eaf542
TT
14827 || child_die->tag == DW_TAG_inheritance
14828 || child_die->tag == DW_TAG_template_value_param
14829 || child_die->tag == DW_TAG_template_type_param)
134d01f1 14830 {
90aeadfc 14831 /* Do nothing. */
134d01f1 14832 }
90aeadfc
DC
14833 else
14834 process_die (child_die, cu);
134d01f1 14835
90aeadfc 14836 child_die = sibling_die (child_die);
134d01f1
DJ
14837 }
14838
fa4028e9
JB
14839 /* Do not consider external references. According to the DWARF standard,
14840 these DIEs are identified by the fact that they have no byte_size
14841 attribute, and a declaration attribute. */
14842 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
14843 || !die_is_declaration (die, cu))
c767944b 14844 new_symbol (die, type, cu);
134d01f1
DJ
14845}
14846
55426c9d
JB
14847/* Assuming DIE is an enumeration type, and TYPE is its associated type,
14848 update TYPE using some information only available in DIE's children. */
14849
14850static void
14851update_enumeration_type_from_children (struct die_info *die,
14852 struct type *type,
14853 struct dwarf2_cu *cu)
14854{
60f7655a 14855 struct die_info *child_die;
55426c9d
JB
14856 int unsigned_enum = 1;
14857 int flag_enum = 1;
14858 ULONGEST mask = 0;
55426c9d 14859
8268c778 14860 auto_obstack obstack;
55426c9d 14861
60f7655a
DE
14862 for (child_die = die->child;
14863 child_die != NULL && child_die->tag;
14864 child_die = sibling_die (child_die))
55426c9d
JB
14865 {
14866 struct attribute *attr;
14867 LONGEST value;
14868 const gdb_byte *bytes;
14869 struct dwarf2_locexpr_baton *baton;
14870 const char *name;
60f7655a 14871
55426c9d
JB
14872 if (child_die->tag != DW_TAG_enumerator)
14873 continue;
14874
14875 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
14876 if (attr == NULL)
14877 continue;
14878
14879 name = dwarf2_name (child_die, cu);
14880 if (name == NULL)
14881 name = "<anonymous enumerator>";
14882
14883 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
14884 &value, &bytes, &baton);
14885 if (value < 0)
14886 {
14887 unsigned_enum = 0;
14888 flag_enum = 0;
14889 }
14890 else if ((mask & value) != 0)
14891 flag_enum = 0;
14892 else
14893 mask |= value;
14894
14895 /* If we already know that the enum type is neither unsigned, nor
14896 a flag type, no need to look at the rest of the enumerates. */
14897 if (!unsigned_enum && !flag_enum)
14898 break;
55426c9d
JB
14899 }
14900
14901 if (unsigned_enum)
14902 TYPE_UNSIGNED (type) = 1;
14903 if (flag_enum)
14904 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
14905}
14906
134d01f1
DJ
14907/* Given a DW_AT_enumeration_type die, set its type. We do not
14908 complete the type's fields yet, or create any symbols. */
c906108c 14909
f792889a 14910static struct type *
134d01f1 14911read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14912{
e7c27a73 14913 struct objfile *objfile = cu->objfile;
c906108c 14914 struct type *type;
c906108c 14915 struct attribute *attr;
0114d602 14916 const char *name;
134d01f1 14917
348e048f
DE
14918 /* If the definition of this type lives in .debug_types, read that type.
14919 Don't follow DW_AT_specification though, that will take us back up
14920 the chain and we want to go down. */
45e58e77 14921 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
14922 if (attr)
14923 {
ac9ec31b 14924 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 14925
ac9ec31b 14926 /* The type's CU may not be the same as CU.
02142a6c 14927 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
14928 return set_die_type (die, type, cu);
14929 }
14930
c906108c
SS
14931 type = alloc_type (objfile);
14932
14933 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 14934 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 14935 if (name != NULL)
7d455152 14936 TYPE_TAG_NAME (type) = name;
c906108c 14937
0626fc76
TT
14938 attr = dwarf2_attr (die, DW_AT_type, cu);
14939 if (attr != NULL)
14940 {
14941 struct type *underlying_type = die_type (die, cu);
14942
14943 TYPE_TARGET_TYPE (type) = underlying_type;
14944 }
14945
e142c38c 14946 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14947 if (attr)
14948 {
14949 TYPE_LENGTH (type) = DW_UNSND (attr);
14950 }
14951 else
14952 {
14953 TYPE_LENGTH (type) = 0;
14954 }
14955
137033e9
JB
14956 /* The enumeration DIE can be incomplete. In Ada, any type can be
14957 declared as private in the package spec, and then defined only
14958 inside the package body. Such types are known as Taft Amendment
14959 Types. When another package uses such a type, an incomplete DIE
14960 may be generated by the compiler. */
02eb380e 14961 if (die_is_declaration (die, cu))
876cecd0 14962 TYPE_STUB (type) = 1;
02eb380e 14963
0626fc76
TT
14964 /* Finish the creation of this type by using the enum's children.
14965 We must call this even when the underlying type has been provided
14966 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
14967 update_enumeration_type_from_children (die, type, cu);
14968
0626fc76
TT
14969 /* If this type has an underlying type that is not a stub, then we
14970 may use its attributes. We always use the "unsigned" attribute
14971 in this situation, because ordinarily we guess whether the type
14972 is unsigned -- but the guess can be wrong and the underlying type
14973 can tell us the reality. However, we defer to a local size
14974 attribute if one exists, because this lets the compiler override
14975 the underlying type if needed. */
14976 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
14977 {
14978 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
14979 if (TYPE_LENGTH (type) == 0)
14980 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
14981 }
14982
3d567982
TT
14983 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
14984
f792889a 14985 return set_die_type (die, type, cu);
134d01f1
DJ
14986}
14987
14988/* Given a pointer to a die which begins an enumeration, process all
14989 the dies that define the members of the enumeration, and create the
14990 symbol for the enumeration type.
14991
14992 NOTE: We reverse the order of the element list. */
14993
14994static void
14995process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
14996{
f792889a 14997 struct type *this_type;
134d01f1 14998
f792889a
DJ
14999 this_type = get_die_type (die, cu);
15000 if (this_type == NULL)
15001 this_type = read_enumeration_type (die, cu);
9dc481d3 15002
639d11d3 15003 if (die->child != NULL)
c906108c 15004 {
9dc481d3
DE
15005 struct die_info *child_die;
15006 struct symbol *sym;
15007 struct field *fields = NULL;
15008 int num_fields = 0;
15d034d0 15009 const char *name;
9dc481d3 15010
639d11d3 15011 child_die = die->child;
c906108c
SS
15012 while (child_die && child_die->tag)
15013 {
15014 if (child_die->tag != DW_TAG_enumerator)
15015 {
e7c27a73 15016 process_die (child_die, cu);
c906108c
SS
15017 }
15018 else
15019 {
39cbfefa
DJ
15020 name = dwarf2_name (child_die, cu);
15021 if (name)
c906108c 15022 {
f792889a 15023 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
15024
15025 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
15026 {
15027 fields = (struct field *)
15028 xrealloc (fields,
15029 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 15030 * sizeof (struct field));
c906108c
SS
15031 }
15032
3567439c 15033 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 15034 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 15035 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
15036 FIELD_BITSIZE (fields[num_fields]) = 0;
15037
15038 num_fields++;
15039 }
15040 }
15041
15042 child_die = sibling_die (child_die);
15043 }
15044
15045 if (num_fields)
15046 {
f792889a
DJ
15047 TYPE_NFIELDS (this_type) = num_fields;
15048 TYPE_FIELDS (this_type) = (struct field *)
15049 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
15050 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 15051 sizeof (struct field) * num_fields);
b8c9b27d 15052 xfree (fields);
c906108c 15053 }
c906108c 15054 }
134d01f1 15055
6c83ed52
TT
15056 /* If we are reading an enum from a .debug_types unit, and the enum
15057 is a declaration, and the enum is not the signatured type in the
15058 unit, then we do not want to add a symbol for it. Adding a
15059 symbol would in some cases obscure the true definition of the
15060 enum, giving users an incomplete type when the definition is
15061 actually available. Note that we do not want to do this for all
15062 enums which are just declarations, because C++0x allows forward
15063 enum declarations. */
3019eac3 15064 if (cu->per_cu->is_debug_types
6c83ed52
TT
15065 && die_is_declaration (die, cu))
15066 {
52dc124a 15067 struct signatured_type *sig_type;
6c83ed52 15068
c0f78cd4 15069 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
15070 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15071 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
15072 return;
15073 }
15074
f792889a 15075 new_symbol (die, this_type, cu);
c906108c
SS
15076}
15077
15078/* Extract all information from a DW_TAG_array_type DIE and put it in
15079 the DIE's type field. For now, this only handles one dimensional
15080 arrays. */
15081
f792889a 15082static struct type *
e7c27a73 15083read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15084{
e7c27a73 15085 struct objfile *objfile = cu->objfile;
c906108c 15086 struct die_info *child_die;
7e314c57 15087 struct type *type;
c906108c 15088 struct type *element_type, *range_type, *index_type;
c906108c 15089 struct attribute *attr;
15d034d0 15090 const char *name;
dc53a7ad 15091 unsigned int bit_stride = 0;
c906108c 15092
e7c27a73 15093 element_type = die_type (die, cu);
c906108c 15094
7e314c57
JK
15095 /* The die_type call above may have already set the type for this DIE. */
15096 type = get_die_type (die, cu);
15097 if (type)
15098 return type;
15099
dc53a7ad
JB
15100 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15101 if (attr != NULL)
15102 bit_stride = DW_UNSND (attr) * 8;
15103
15104 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15105 if (attr != NULL)
15106 bit_stride = DW_UNSND (attr);
15107
c906108c
SS
15108 /* Irix 6.2 native cc creates array types without children for
15109 arrays with unspecified length. */
639d11d3 15110 if (die->child == NULL)
c906108c 15111 {
46bf5051 15112 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 15113 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
15114 type = create_array_type_with_stride (NULL, element_type, range_type,
15115 bit_stride);
f792889a 15116 return set_die_type (die, type, cu);
c906108c
SS
15117 }
15118
791afaa2 15119 std::vector<struct type *> range_types;
639d11d3 15120 child_die = die->child;
c906108c
SS
15121 while (child_die && child_die->tag)
15122 {
15123 if (child_die->tag == DW_TAG_subrange_type)
15124 {
f792889a 15125 struct type *child_type = read_type_die (child_die, cu);
9a619af0 15126
f792889a 15127 if (child_type != NULL)
a02abb62 15128 {
0963b4bd
MS
15129 /* The range type was succesfully read. Save it for the
15130 array type creation. */
791afaa2 15131 range_types.push_back (child_type);
a02abb62 15132 }
c906108c
SS
15133 }
15134 child_die = sibling_die (child_die);
15135 }
15136
15137 /* Dwarf2 dimensions are output from left to right, create the
15138 necessary array types in backwards order. */
7ca2d3a3 15139
c906108c 15140 type = element_type;
7ca2d3a3
DL
15141
15142 if (read_array_order (die, cu) == DW_ORD_col_major)
15143 {
15144 int i = 0;
9a619af0 15145
791afaa2 15146 while (i < range_types.size ())
dc53a7ad
JB
15147 type = create_array_type_with_stride (NULL, type, range_types[i++],
15148 bit_stride);
7ca2d3a3
DL
15149 }
15150 else
15151 {
791afaa2 15152 size_t ndim = range_types.size ();
7ca2d3a3 15153 while (ndim-- > 0)
dc53a7ad
JB
15154 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15155 bit_stride);
7ca2d3a3 15156 }
c906108c 15157
f5f8a009
EZ
15158 /* Understand Dwarf2 support for vector types (like they occur on
15159 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15160 array type. This is not part of the Dwarf2/3 standard yet, but a
15161 custom vendor extension. The main difference between a regular
15162 array and the vector variant is that vectors are passed by value
15163 to functions. */
e142c38c 15164 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 15165 if (attr)
ea37ba09 15166 make_vector_type (type);
f5f8a009 15167
dbc98a8b
KW
15168 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15169 implementation may choose to implement triple vectors using this
15170 attribute. */
15171 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15172 if (attr)
15173 {
15174 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15175 TYPE_LENGTH (type) = DW_UNSND (attr);
15176 else
3e43a32a
MS
15177 complaint (&symfile_complaints,
15178 _("DW_AT_byte_size for array type smaller "
15179 "than the total size of elements"));
dbc98a8b
KW
15180 }
15181
39cbfefa
DJ
15182 name = dwarf2_name (die, cu);
15183 if (name)
15184 TYPE_NAME (type) = name;
6e70227d 15185
0963b4bd 15186 /* Install the type in the die. */
7e314c57
JK
15187 set_die_type (die, type, cu);
15188
15189 /* set_die_type should be already done. */
b4ba55a1
JB
15190 set_descriptive_type (type, die, cu);
15191
7e314c57 15192 return type;
c906108c
SS
15193}
15194
7ca2d3a3 15195static enum dwarf_array_dim_ordering
6e70227d 15196read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
15197{
15198 struct attribute *attr;
15199
15200 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15201
aead7601
SM
15202 if (attr)
15203 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 15204
0963b4bd
MS
15205 /* GNU F77 is a special case, as at 08/2004 array type info is the
15206 opposite order to the dwarf2 specification, but data is still
15207 laid out as per normal fortran.
7ca2d3a3 15208
0963b4bd
MS
15209 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15210 version checking. */
7ca2d3a3 15211
905e0470
PM
15212 if (cu->language == language_fortran
15213 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
15214 {
15215 return DW_ORD_row_major;
15216 }
15217
6e70227d 15218 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
15219 {
15220 case array_column_major:
15221 return DW_ORD_col_major;
15222 case array_row_major:
15223 default:
15224 return DW_ORD_row_major;
15225 };
15226}
15227
72019c9c 15228/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 15229 the DIE's type field. */
72019c9c 15230
f792889a 15231static struct type *
72019c9c
GM
15232read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15233{
7e314c57
JK
15234 struct type *domain_type, *set_type;
15235 struct attribute *attr;
f792889a 15236
7e314c57
JK
15237 domain_type = die_type (die, cu);
15238
15239 /* The die_type call above may have already set the type for this DIE. */
15240 set_type = get_die_type (die, cu);
15241 if (set_type)
15242 return set_type;
15243
15244 set_type = create_set_type (NULL, domain_type);
15245
15246 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
15247 if (attr)
15248 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 15249
f792889a 15250 return set_die_type (die, set_type, cu);
72019c9c 15251}
7ca2d3a3 15252
0971de02
TT
15253/* A helper for read_common_block that creates a locexpr baton.
15254 SYM is the symbol which we are marking as computed.
15255 COMMON_DIE is the DIE for the common block.
15256 COMMON_LOC is the location expression attribute for the common
15257 block itself.
15258 MEMBER_LOC is the location expression attribute for the particular
15259 member of the common block that we are processing.
15260 CU is the CU from which the above come. */
15261
15262static void
15263mark_common_block_symbol_computed (struct symbol *sym,
15264 struct die_info *common_die,
15265 struct attribute *common_loc,
15266 struct attribute *member_loc,
15267 struct dwarf2_cu *cu)
15268{
15269 struct objfile *objfile = dwarf2_per_objfile->objfile;
15270 struct dwarf2_locexpr_baton *baton;
15271 gdb_byte *ptr;
15272 unsigned int cu_off;
15273 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15274 LONGEST offset = 0;
15275
15276 gdb_assert (common_loc && member_loc);
15277 gdb_assert (attr_form_is_block (common_loc));
15278 gdb_assert (attr_form_is_block (member_loc)
15279 || attr_form_is_constant (member_loc));
15280
8d749320 15281 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
15282 baton->per_cu = cu->per_cu;
15283 gdb_assert (baton->per_cu);
15284
15285 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15286
15287 if (attr_form_is_constant (member_loc))
15288 {
15289 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15290 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15291 }
15292 else
15293 baton->size += DW_BLOCK (member_loc)->size;
15294
224c3ddb 15295 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
15296 baton->data = ptr;
15297
15298 *ptr++ = DW_OP_call4;
9c541725 15299 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
15300 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15301 ptr += 4;
15302
15303 if (attr_form_is_constant (member_loc))
15304 {
15305 *ptr++ = DW_OP_addr;
15306 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15307 ptr += cu->header.addr_size;
15308 }
15309 else
15310 {
15311 /* We have to copy the data here, because DW_OP_call4 will only
15312 use a DW_AT_location attribute. */
15313 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15314 ptr += DW_BLOCK (member_loc)->size;
15315 }
15316
15317 *ptr++ = DW_OP_plus;
15318 gdb_assert (ptr - baton->data == baton->size);
15319
0971de02 15320 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 15321 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
15322}
15323
4357ac6c
TT
15324/* Create appropriate locally-scoped variables for all the
15325 DW_TAG_common_block entries. Also create a struct common_block
15326 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15327 is used to sepate the common blocks name namespace from regular
15328 variable names. */
c906108c
SS
15329
15330static void
e7c27a73 15331read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15332{
0971de02
TT
15333 struct attribute *attr;
15334
15335 attr = dwarf2_attr (die, DW_AT_location, cu);
15336 if (attr)
15337 {
15338 /* Support the .debug_loc offsets. */
15339 if (attr_form_is_block (attr))
15340 {
15341 /* Ok. */
15342 }
15343 else if (attr_form_is_section_offset (attr))
15344 {
15345 dwarf2_complex_location_expr_complaint ();
15346 attr = NULL;
15347 }
15348 else
15349 {
15350 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15351 "common block member");
15352 attr = NULL;
15353 }
15354 }
15355
639d11d3 15356 if (die->child != NULL)
c906108c 15357 {
4357ac6c
TT
15358 struct objfile *objfile = cu->objfile;
15359 struct die_info *child_die;
15360 size_t n_entries = 0, size;
15361 struct common_block *common_block;
15362 struct symbol *sym;
74ac6d43 15363
4357ac6c
TT
15364 for (child_die = die->child;
15365 child_die && child_die->tag;
15366 child_die = sibling_die (child_die))
15367 ++n_entries;
15368
15369 size = (sizeof (struct common_block)
15370 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
15371 common_block
15372 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15373 size);
4357ac6c
TT
15374 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15375 common_block->n_entries = 0;
15376
15377 for (child_die = die->child;
15378 child_die && child_die->tag;
15379 child_die = sibling_die (child_die))
15380 {
15381 /* Create the symbol in the DW_TAG_common_block block in the current
15382 symbol scope. */
e7c27a73 15383 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
15384 if (sym != NULL)
15385 {
15386 struct attribute *member_loc;
15387
15388 common_block->contents[common_block->n_entries++] = sym;
15389
15390 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15391 cu);
15392 if (member_loc)
15393 {
15394 /* GDB has handled this for a long time, but it is
15395 not specified by DWARF. It seems to have been
15396 emitted by gfortran at least as recently as:
15397 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15398 complaint (&symfile_complaints,
15399 _("Variable in common block has "
15400 "DW_AT_data_member_location "
15401 "- DIE at 0x%x [in module %s]"),
9c541725 15402 to_underlying (child_die->sect_off),
4262abfb 15403 objfile_name (cu->objfile));
0971de02
TT
15404
15405 if (attr_form_is_section_offset (member_loc))
15406 dwarf2_complex_location_expr_complaint ();
15407 else if (attr_form_is_constant (member_loc)
15408 || attr_form_is_block (member_loc))
15409 {
15410 if (attr)
15411 mark_common_block_symbol_computed (sym, die, attr,
15412 member_loc, cu);
15413 }
15414 else
15415 dwarf2_complex_location_expr_complaint ();
15416 }
15417 }
c906108c 15418 }
4357ac6c
TT
15419
15420 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
15421 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
15422 }
15423}
15424
0114d602 15425/* Create a type for a C++ namespace. */
d9fa45fe 15426
0114d602
DJ
15427static struct type *
15428read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 15429{
e7c27a73 15430 struct objfile *objfile = cu->objfile;
0114d602 15431 const char *previous_prefix, *name;
9219021c 15432 int is_anonymous;
0114d602
DJ
15433 struct type *type;
15434
15435 /* For extensions, reuse the type of the original namespace. */
15436 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
15437 {
15438 struct die_info *ext_die;
15439 struct dwarf2_cu *ext_cu = cu;
9a619af0 15440
0114d602
DJ
15441 ext_die = dwarf2_extension (die, &ext_cu);
15442 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
15443
15444 /* EXT_CU may not be the same as CU.
02142a6c 15445 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
15446 return set_die_type (die, type, cu);
15447 }
9219021c 15448
e142c38c 15449 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
15450
15451 /* Now build the name of the current namespace. */
15452
0114d602
DJ
15453 previous_prefix = determine_prefix (die, cu);
15454 if (previous_prefix[0] != '\0')
15455 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 15456 previous_prefix, name, 0, cu);
0114d602
DJ
15457
15458 /* Create the type. */
19f392bc 15459 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
15460 TYPE_TAG_NAME (type) = TYPE_NAME (type);
15461
60531b24 15462 return set_die_type (die, type, cu);
0114d602
DJ
15463}
15464
22cee43f 15465/* Read a namespace scope. */
0114d602
DJ
15466
15467static void
15468read_namespace (struct die_info *die, struct dwarf2_cu *cu)
15469{
15470 struct objfile *objfile = cu->objfile;
0114d602 15471 int is_anonymous;
9219021c 15472
5c4e30ca
DC
15473 /* Add a symbol associated to this if we haven't seen the namespace
15474 before. Also, add a using directive if it's an anonymous
15475 namespace. */
9219021c 15476
f2f0e013 15477 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
15478 {
15479 struct type *type;
15480
0114d602 15481 type = read_type_die (die, cu);
e7c27a73 15482 new_symbol (die, type, cu);
5c4e30ca 15483
e8e80198 15484 namespace_name (die, &is_anonymous, cu);
5c4e30ca 15485 if (is_anonymous)
0114d602
DJ
15486 {
15487 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 15488
eb1e02fd 15489 std::vector<const char *> excludes;
22cee43f
PMR
15490 add_using_directive (using_directives (cu->language),
15491 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 15492 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 15493 }
5c4e30ca 15494 }
9219021c 15495
639d11d3 15496 if (die->child != NULL)
d9fa45fe 15497 {
639d11d3 15498 struct die_info *child_die = die->child;
6e70227d 15499
d9fa45fe
DC
15500 while (child_die && child_die->tag)
15501 {
e7c27a73 15502 process_die (child_die, cu);
d9fa45fe
DC
15503 child_die = sibling_die (child_die);
15504 }
15505 }
38d518c9
EZ
15506}
15507
f55ee35c
JK
15508/* Read a Fortran module as type. This DIE can be only a declaration used for
15509 imported module. Still we need that type as local Fortran "use ... only"
15510 declaration imports depend on the created type in determine_prefix. */
15511
15512static struct type *
15513read_module_type (struct die_info *die, struct dwarf2_cu *cu)
15514{
15515 struct objfile *objfile = cu->objfile;
15d034d0 15516 const char *module_name;
f55ee35c
JK
15517 struct type *type;
15518
15519 module_name = dwarf2_name (die, cu);
15520 if (!module_name)
3e43a32a
MS
15521 complaint (&symfile_complaints,
15522 _("DW_TAG_module has no name, offset 0x%x"),
9c541725 15523 to_underlying (die->sect_off));
19f392bc 15524 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
15525
15526 /* determine_prefix uses TYPE_TAG_NAME. */
15527 TYPE_TAG_NAME (type) = TYPE_NAME (type);
15528
15529 return set_die_type (die, type, cu);
15530}
15531
5d7cb8df
JK
15532/* Read a Fortran module. */
15533
15534static void
15535read_module (struct die_info *die, struct dwarf2_cu *cu)
15536{
15537 struct die_info *child_die = die->child;
530e8392
KB
15538 struct type *type;
15539
15540 type = read_type_die (die, cu);
15541 new_symbol (die, type, cu);
5d7cb8df 15542
5d7cb8df
JK
15543 while (child_die && child_die->tag)
15544 {
15545 process_die (child_die, cu);
15546 child_die = sibling_die (child_die);
15547 }
15548}
15549
38d518c9
EZ
15550/* Return the name of the namespace represented by DIE. Set
15551 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
15552 namespace. */
15553
15554static const char *
e142c38c 15555namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
15556{
15557 struct die_info *current_die;
15558 const char *name = NULL;
15559
15560 /* Loop through the extensions until we find a name. */
15561
15562 for (current_die = die;
15563 current_die != NULL;
f2f0e013 15564 current_die = dwarf2_extension (die, &cu))
38d518c9 15565 {
96553a0c
DE
15566 /* We don't use dwarf2_name here so that we can detect the absence
15567 of a name -> anonymous namespace. */
7d45c7c3 15568 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 15569
38d518c9
EZ
15570 if (name != NULL)
15571 break;
15572 }
15573
15574 /* Is it an anonymous namespace? */
15575
15576 *is_anonymous = (name == NULL);
15577 if (*is_anonymous)
2b1dbab0 15578 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
15579
15580 return name;
d9fa45fe
DC
15581}
15582
c906108c
SS
15583/* Extract all information from a DW_TAG_pointer_type DIE and add to
15584 the user defined type vector. */
15585
f792889a 15586static struct type *
e7c27a73 15587read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15588{
5e2b427d 15589 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 15590 struct comp_unit_head *cu_header = &cu->header;
c906108c 15591 struct type *type;
8b2dbe47
KB
15592 struct attribute *attr_byte_size;
15593 struct attribute *attr_address_class;
15594 int byte_size, addr_class;
7e314c57
JK
15595 struct type *target_type;
15596
15597 target_type = die_type (die, cu);
c906108c 15598
7e314c57
JK
15599 /* The die_type call above may have already set the type for this DIE. */
15600 type = get_die_type (die, cu);
15601 if (type)
15602 return type;
15603
15604 type = lookup_pointer_type (target_type);
8b2dbe47 15605
e142c38c 15606 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
15607 if (attr_byte_size)
15608 byte_size = DW_UNSND (attr_byte_size);
c906108c 15609 else
8b2dbe47
KB
15610 byte_size = cu_header->addr_size;
15611
e142c38c 15612 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
15613 if (attr_address_class)
15614 addr_class = DW_UNSND (attr_address_class);
15615 else
15616 addr_class = DW_ADDR_none;
15617
15618 /* If the pointer size or address class is different than the
15619 default, create a type variant marked as such and set the
15620 length accordingly. */
15621 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 15622 {
5e2b427d 15623 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
15624 {
15625 int type_flags;
15626
849957d9 15627 type_flags = gdbarch_address_class_type_flags
5e2b427d 15628 (gdbarch, byte_size, addr_class);
876cecd0
TT
15629 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
15630 == 0);
8b2dbe47
KB
15631 type = make_type_with_address_space (type, type_flags);
15632 }
15633 else if (TYPE_LENGTH (type) != byte_size)
15634 {
3e43a32a
MS
15635 complaint (&symfile_complaints,
15636 _("invalid pointer size %d"), byte_size);
8b2dbe47 15637 }
6e70227d 15638 else
9a619af0
MS
15639 {
15640 /* Should we also complain about unhandled address classes? */
15641 }
c906108c 15642 }
8b2dbe47
KB
15643
15644 TYPE_LENGTH (type) = byte_size;
f792889a 15645 return set_die_type (die, type, cu);
c906108c
SS
15646}
15647
15648/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
15649 the user defined type vector. */
15650
f792889a 15651static struct type *
e7c27a73 15652read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
15653{
15654 struct type *type;
15655 struct type *to_type;
15656 struct type *domain;
15657
e7c27a73
DJ
15658 to_type = die_type (die, cu);
15659 domain = die_containing_type (die, cu);
0d5de010 15660
7e314c57
JK
15661 /* The calls above may have already set the type for this DIE. */
15662 type = get_die_type (die, cu);
15663 if (type)
15664 return type;
15665
0d5de010
DJ
15666 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
15667 type = lookup_methodptr_type (to_type);
7078baeb
TT
15668 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
15669 {
15670 struct type *new_type = alloc_type (cu->objfile);
15671
15672 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
15673 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
15674 TYPE_VARARGS (to_type));
15675 type = lookup_methodptr_type (new_type);
15676 }
0d5de010
DJ
15677 else
15678 type = lookup_memberptr_type (to_type, domain);
c906108c 15679
f792889a 15680 return set_die_type (die, type, cu);
c906108c
SS
15681}
15682
4297a3f0 15683/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
15684 the user defined type vector. */
15685
f792889a 15686static struct type *
4297a3f0
AV
15687read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
15688 enum type_code refcode)
c906108c 15689{
e7c27a73 15690 struct comp_unit_head *cu_header = &cu->header;
7e314c57 15691 struct type *type, *target_type;
c906108c
SS
15692 struct attribute *attr;
15693
4297a3f0
AV
15694 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
15695
7e314c57
JK
15696 target_type = die_type (die, cu);
15697
15698 /* The die_type call above may have already set the type for this DIE. */
15699 type = get_die_type (die, cu);
15700 if (type)
15701 return type;
15702
4297a3f0 15703 type = lookup_reference_type (target_type, refcode);
e142c38c 15704 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15705 if (attr)
15706 {
15707 TYPE_LENGTH (type) = DW_UNSND (attr);
15708 }
15709 else
15710 {
107d2387 15711 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 15712 }
f792889a 15713 return set_die_type (die, type, cu);
c906108c
SS
15714}
15715
cf363f18
MW
15716/* Add the given cv-qualifiers to the element type of the array. GCC
15717 outputs DWARF type qualifiers that apply to an array, not the
15718 element type. But GDB relies on the array element type to carry
15719 the cv-qualifiers. This mimics section 6.7.3 of the C99
15720 specification. */
15721
15722static struct type *
15723add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
15724 struct type *base_type, int cnst, int voltl)
15725{
15726 struct type *el_type, *inner_array;
15727
15728 base_type = copy_type (base_type);
15729 inner_array = base_type;
15730
15731 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
15732 {
15733 TYPE_TARGET_TYPE (inner_array) =
15734 copy_type (TYPE_TARGET_TYPE (inner_array));
15735 inner_array = TYPE_TARGET_TYPE (inner_array);
15736 }
15737
15738 el_type = TYPE_TARGET_TYPE (inner_array);
15739 cnst |= TYPE_CONST (el_type);
15740 voltl |= TYPE_VOLATILE (el_type);
15741 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
15742
15743 return set_die_type (die, base_type, cu);
15744}
15745
f792889a 15746static struct type *
e7c27a73 15747read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15748{
f792889a 15749 struct type *base_type, *cv_type;
c906108c 15750
e7c27a73 15751 base_type = die_type (die, cu);
7e314c57
JK
15752
15753 /* The die_type call above may have already set the type for this DIE. */
15754 cv_type = get_die_type (die, cu);
15755 if (cv_type)
15756 return cv_type;
15757
2f608a3a
KW
15758 /* In case the const qualifier is applied to an array type, the element type
15759 is so qualified, not the array type (section 6.7.3 of C99). */
15760 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 15761 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 15762
f792889a
DJ
15763 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
15764 return set_die_type (die, cv_type, cu);
c906108c
SS
15765}
15766
f792889a 15767static struct type *
e7c27a73 15768read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15769{
f792889a 15770 struct type *base_type, *cv_type;
c906108c 15771
e7c27a73 15772 base_type = die_type (die, cu);
7e314c57
JK
15773
15774 /* The die_type call above may have already set the type for this DIE. */
15775 cv_type = get_die_type (die, cu);
15776 if (cv_type)
15777 return cv_type;
15778
cf363f18
MW
15779 /* In case the volatile qualifier is applied to an array type, the
15780 element type is so qualified, not the array type (section 6.7.3
15781 of C99). */
15782 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
15783 return add_array_cv_type (die, cu, base_type, 0, 1);
15784
f792889a
DJ
15785 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
15786 return set_die_type (die, cv_type, cu);
c906108c
SS
15787}
15788
06d66ee9
TT
15789/* Handle DW_TAG_restrict_type. */
15790
15791static struct type *
15792read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
15793{
15794 struct type *base_type, *cv_type;
15795
15796 base_type = die_type (die, cu);
15797
15798 /* The die_type call above may have already set the type for this DIE. */
15799 cv_type = get_die_type (die, cu);
15800 if (cv_type)
15801 return cv_type;
15802
15803 cv_type = make_restrict_type (base_type);
15804 return set_die_type (die, cv_type, cu);
15805}
15806
a2c2acaf
MW
15807/* Handle DW_TAG_atomic_type. */
15808
15809static struct type *
15810read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
15811{
15812 struct type *base_type, *cv_type;
15813
15814 base_type = die_type (die, cu);
15815
15816 /* The die_type call above may have already set the type for this DIE. */
15817 cv_type = get_die_type (die, cu);
15818 if (cv_type)
15819 return cv_type;
15820
15821 cv_type = make_atomic_type (base_type);
15822 return set_die_type (die, cv_type, cu);
15823}
15824
c906108c
SS
15825/* Extract all information from a DW_TAG_string_type DIE and add to
15826 the user defined type vector. It isn't really a user defined type,
15827 but it behaves like one, with other DIE's using an AT_user_def_type
15828 attribute to reference it. */
15829
f792889a 15830static struct type *
e7c27a73 15831read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15832{
e7c27a73 15833 struct objfile *objfile = cu->objfile;
3b7538c0 15834 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15835 struct type *type, *range_type, *index_type, *char_type;
15836 struct attribute *attr;
15837 unsigned int length;
15838
e142c38c 15839 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
15840 if (attr)
15841 {
15842 length = DW_UNSND (attr);
15843 }
15844 else
15845 {
0963b4bd 15846 /* Check for the DW_AT_byte_size attribute. */
e142c38c 15847 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
15848 if (attr)
15849 {
15850 length = DW_UNSND (attr);
15851 }
15852 else
15853 {
15854 length = 1;
15855 }
c906108c 15856 }
6ccb9162 15857
46bf5051 15858 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 15859 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
15860 char_type = language_string_char_type (cu->language_defn, gdbarch);
15861 type = create_string_type (NULL, char_type, range_type);
6ccb9162 15862
f792889a 15863 return set_die_type (die, type, cu);
c906108c
SS
15864}
15865
4d804846
JB
15866/* Assuming that DIE corresponds to a function, returns nonzero
15867 if the function is prototyped. */
15868
15869static int
15870prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
15871{
15872 struct attribute *attr;
15873
15874 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
15875 if (attr && (DW_UNSND (attr) != 0))
15876 return 1;
15877
15878 /* The DWARF standard implies that the DW_AT_prototyped attribute
15879 is only meaninful for C, but the concept also extends to other
15880 languages that allow unprototyped functions (Eg: Objective C).
15881 For all other languages, assume that functions are always
15882 prototyped. */
15883 if (cu->language != language_c
15884 && cu->language != language_objc
15885 && cu->language != language_opencl)
15886 return 1;
15887
15888 /* RealView does not emit DW_AT_prototyped. We can not distinguish
15889 prototyped and unprototyped functions; default to prototyped,
15890 since that is more common in modern code (and RealView warns
15891 about unprototyped functions). */
15892 if (producer_is_realview (cu->producer))
15893 return 1;
15894
15895 return 0;
15896}
15897
c906108c
SS
15898/* Handle DIES due to C code like:
15899
15900 struct foo
c5aa993b
JM
15901 {
15902 int (*funcp)(int a, long l);
15903 int b;
15904 };
c906108c 15905
0963b4bd 15906 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 15907
f792889a 15908static struct type *
e7c27a73 15909read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15910{
bb5ed363 15911 struct objfile *objfile = cu->objfile;
0963b4bd
MS
15912 struct type *type; /* Type that this function returns. */
15913 struct type *ftype; /* Function that returns above type. */
c906108c
SS
15914 struct attribute *attr;
15915
e7c27a73 15916 type = die_type (die, cu);
7e314c57
JK
15917
15918 /* The die_type call above may have already set the type for this DIE. */
15919 ftype = get_die_type (die, cu);
15920 if (ftype)
15921 return ftype;
15922
0c8b41f1 15923 ftype = lookup_function_type (type);
c906108c 15924
4d804846 15925 if (prototyped_function_p (die, cu))
a6c727b2 15926 TYPE_PROTOTYPED (ftype) = 1;
c906108c 15927
c055b101
CV
15928 /* Store the calling convention in the type if it's available in
15929 the subroutine die. Otherwise set the calling convention to
15930 the default value DW_CC_normal. */
15931 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
15932 if (attr)
15933 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
15934 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
15935 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
15936 else
15937 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 15938
743649fd
MW
15939 /* Record whether the function returns normally to its caller or not
15940 if the DWARF producer set that information. */
15941 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
15942 if (attr && (DW_UNSND (attr) != 0))
15943 TYPE_NO_RETURN (ftype) = 1;
15944
76c10ea2
GM
15945 /* We need to add the subroutine type to the die immediately so
15946 we don't infinitely recurse when dealing with parameters
0963b4bd 15947 declared as the same subroutine type. */
76c10ea2 15948 set_die_type (die, ftype, cu);
6e70227d 15949
639d11d3 15950 if (die->child != NULL)
c906108c 15951 {
bb5ed363 15952 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 15953 struct die_info *child_die;
8072405b 15954 int nparams, iparams;
c906108c
SS
15955
15956 /* Count the number of parameters.
15957 FIXME: GDB currently ignores vararg functions, but knows about
15958 vararg member functions. */
8072405b 15959 nparams = 0;
639d11d3 15960 child_die = die->child;
c906108c
SS
15961 while (child_die && child_die->tag)
15962 {
15963 if (child_die->tag == DW_TAG_formal_parameter)
15964 nparams++;
15965 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 15966 TYPE_VARARGS (ftype) = 1;
c906108c
SS
15967 child_die = sibling_die (child_die);
15968 }
15969
15970 /* Allocate storage for parameters and fill them in. */
15971 TYPE_NFIELDS (ftype) = nparams;
15972 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 15973 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 15974
8072405b
JK
15975 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
15976 even if we error out during the parameters reading below. */
15977 for (iparams = 0; iparams < nparams; iparams++)
15978 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
15979
15980 iparams = 0;
639d11d3 15981 child_die = die->child;
c906108c
SS
15982 while (child_die && child_die->tag)
15983 {
15984 if (child_die->tag == DW_TAG_formal_parameter)
15985 {
3ce3b1ba
PA
15986 struct type *arg_type;
15987
15988 /* DWARF version 2 has no clean way to discern C++
15989 static and non-static member functions. G++ helps
15990 GDB by marking the first parameter for non-static
15991 member functions (which is the this pointer) as
15992 artificial. We pass this information to
15993 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
15994
15995 DWARF version 3 added DW_AT_object_pointer, which GCC
15996 4.5 does not yet generate. */
e142c38c 15997 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
15998 if (attr)
15999 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16000 else
9c37b5ae 16001 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
16002 arg_type = die_type (child_die, cu);
16003
16004 /* RealView does not mark THIS as const, which the testsuite
16005 expects. GCC marks THIS as const in method definitions,
16006 but not in the class specifications (GCC PR 43053). */
16007 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16008 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16009 {
16010 int is_this = 0;
16011 struct dwarf2_cu *arg_cu = cu;
16012 const char *name = dwarf2_name (child_die, cu);
16013
16014 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16015 if (attr)
16016 {
16017 /* If the compiler emits this, use it. */
16018 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16019 is_this = 1;
16020 }
16021 else if (name && strcmp (name, "this") == 0)
16022 /* Function definitions will have the argument names. */
16023 is_this = 1;
16024 else if (name == NULL && iparams == 0)
16025 /* Declarations may not have the names, so like
16026 elsewhere in GDB, assume an artificial first
16027 argument is "this". */
16028 is_this = 1;
16029
16030 if (is_this)
16031 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16032 arg_type, 0);
16033 }
16034
16035 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
16036 iparams++;
16037 }
16038 child_die = sibling_die (child_die);
16039 }
16040 }
16041
76c10ea2 16042 return ftype;
c906108c
SS
16043}
16044
f792889a 16045static struct type *
e7c27a73 16046read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16047{
e7c27a73 16048 struct objfile *objfile = cu->objfile;
0114d602 16049 const char *name = NULL;
3c8e0968 16050 struct type *this_type, *target_type;
c906108c 16051
94af9270 16052 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
16053 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16054 TYPE_TARGET_STUB (this_type) = 1;
f792889a 16055 set_die_type (die, this_type, cu);
3c8e0968
DE
16056 target_type = die_type (die, cu);
16057 if (target_type != this_type)
16058 TYPE_TARGET_TYPE (this_type) = target_type;
16059 else
16060 {
16061 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16062 spec and cause infinite loops in GDB. */
16063 complaint (&symfile_complaints,
16064 _("Self-referential DW_TAG_typedef "
16065 "- DIE at 0x%x [in module %s]"),
9c541725 16066 to_underlying (die->sect_off), objfile_name (objfile));
3c8e0968
DE
16067 TYPE_TARGET_TYPE (this_type) = NULL;
16068 }
f792889a 16069 return this_type;
c906108c
SS
16070}
16071
9b790ce7
UW
16072/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16073 (which may be different from NAME) to the architecture back-end to allow
16074 it to guess the correct format if necessary. */
16075
16076static struct type *
16077dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16078 const char *name_hint)
16079{
16080 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16081 const struct floatformat **format;
16082 struct type *type;
16083
16084 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16085 if (format)
16086 type = init_float_type (objfile, bits, name, format);
16087 else
77b7c781 16088 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
16089
16090 return type;
16091}
16092
c906108c
SS
16093/* Find a representation of a given base type and install
16094 it in the TYPE field of the die. */
16095
f792889a 16096static struct type *
e7c27a73 16097read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16098{
e7c27a73 16099 struct objfile *objfile = cu->objfile;
c906108c
SS
16100 struct type *type;
16101 struct attribute *attr;
19f392bc 16102 int encoding = 0, bits = 0;
15d034d0 16103 const char *name;
c906108c 16104
e142c38c 16105 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
16106 if (attr)
16107 {
16108 encoding = DW_UNSND (attr);
16109 }
e142c38c 16110 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16111 if (attr)
16112 {
19f392bc 16113 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 16114 }
39cbfefa 16115 name = dwarf2_name (die, cu);
6ccb9162 16116 if (!name)
c906108c 16117 {
6ccb9162
UW
16118 complaint (&symfile_complaints,
16119 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 16120 }
6ccb9162
UW
16121
16122 switch (encoding)
c906108c 16123 {
6ccb9162
UW
16124 case DW_ATE_address:
16125 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 16126 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 16127 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
16128 break;
16129 case DW_ATE_boolean:
19f392bc 16130 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
16131 break;
16132 case DW_ATE_complex_float:
9b790ce7 16133 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 16134 type = init_complex_type (objfile, name, type);
6ccb9162
UW
16135 break;
16136 case DW_ATE_decimal_float:
19f392bc 16137 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
16138 break;
16139 case DW_ATE_float:
9b790ce7 16140 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
16141 break;
16142 case DW_ATE_signed:
19f392bc 16143 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
16144 break;
16145 case DW_ATE_unsigned:
3b2b8fea
TT
16146 if (cu->language == language_fortran
16147 && name
61012eef 16148 && startswith (name, "character("))
19f392bc
UW
16149 type = init_character_type (objfile, bits, 1, name);
16150 else
16151 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
16152 break;
16153 case DW_ATE_signed_char:
6e70227d 16154 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
16155 || cu->language == language_pascal
16156 || cu->language == language_fortran)
19f392bc
UW
16157 type = init_character_type (objfile, bits, 0, name);
16158 else
16159 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
16160 break;
16161 case DW_ATE_unsigned_char:
868a0084 16162 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 16163 || cu->language == language_pascal
c44af4eb
TT
16164 || cu->language == language_fortran
16165 || cu->language == language_rust)
19f392bc
UW
16166 type = init_character_type (objfile, bits, 1, name);
16167 else
16168 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 16169 break;
75079b2b 16170 case DW_ATE_UTF:
53e710ac
PA
16171 {
16172 gdbarch *arch = get_objfile_arch (objfile);
16173
16174 if (bits == 16)
16175 type = builtin_type (arch)->builtin_char16;
16176 else if (bits == 32)
16177 type = builtin_type (arch)->builtin_char32;
16178 else
16179 {
16180 complaint (&symfile_complaints,
16181 _("unsupported DW_ATE_UTF bit size: '%d'"),
16182 bits);
16183 type = init_integer_type (objfile, bits, 1, name);
16184 }
16185 return set_die_type (die, type, cu);
16186 }
75079b2b
TT
16187 break;
16188
6ccb9162
UW
16189 default:
16190 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
16191 dwarf_type_encoding_name (encoding));
77b7c781 16192 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 16193 break;
c906108c 16194 }
6ccb9162 16195
0114d602 16196 if (name && strcmp (name, "char") == 0)
876cecd0 16197 TYPE_NOSIGN (type) = 1;
0114d602 16198
f792889a 16199 return set_die_type (die, type, cu);
c906108c
SS
16200}
16201
80180f79
SA
16202/* Parse dwarf attribute if it's a block, reference or constant and put the
16203 resulting value of the attribute into struct bound_prop.
16204 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
16205
16206static int
16207attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
16208 struct dwarf2_cu *cu, struct dynamic_prop *prop)
16209{
16210 struct dwarf2_property_baton *baton;
16211 struct obstack *obstack = &cu->objfile->objfile_obstack;
16212
16213 if (attr == NULL || prop == NULL)
16214 return 0;
16215
16216 if (attr_form_is_block (attr))
16217 {
8d749320 16218 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
16219 baton->referenced_type = NULL;
16220 baton->locexpr.per_cu = cu->per_cu;
16221 baton->locexpr.size = DW_BLOCK (attr)->size;
16222 baton->locexpr.data = DW_BLOCK (attr)->data;
16223 prop->data.baton = baton;
16224 prop->kind = PROP_LOCEXPR;
16225 gdb_assert (prop->data.baton != NULL);
16226 }
16227 else if (attr_form_is_ref (attr))
16228 {
16229 struct dwarf2_cu *target_cu = cu;
16230 struct die_info *target_die;
16231 struct attribute *target_attr;
16232
16233 target_die = follow_die_ref (die, attr, &target_cu);
16234 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
16235 if (target_attr == NULL)
16236 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
16237 target_cu);
80180f79
SA
16238 if (target_attr == NULL)
16239 return 0;
16240
df25ebbd 16241 switch (target_attr->name)
80180f79 16242 {
df25ebbd
JB
16243 case DW_AT_location:
16244 if (attr_form_is_section_offset (target_attr))
16245 {
8d749320 16246 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
16247 baton->referenced_type = die_type (target_die, target_cu);
16248 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
16249 prop->data.baton = baton;
16250 prop->kind = PROP_LOCLIST;
16251 gdb_assert (prop->data.baton != NULL);
16252 }
16253 else if (attr_form_is_block (target_attr))
16254 {
8d749320 16255 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
16256 baton->referenced_type = die_type (target_die, target_cu);
16257 baton->locexpr.per_cu = cu->per_cu;
16258 baton->locexpr.size = DW_BLOCK (target_attr)->size;
16259 baton->locexpr.data = DW_BLOCK (target_attr)->data;
16260 prop->data.baton = baton;
16261 prop->kind = PROP_LOCEXPR;
16262 gdb_assert (prop->data.baton != NULL);
16263 }
16264 else
16265 {
16266 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16267 "dynamic property");
16268 return 0;
16269 }
16270 break;
16271 case DW_AT_data_member_location:
16272 {
16273 LONGEST offset;
16274
16275 if (!handle_data_member_location (target_die, target_cu,
16276 &offset))
16277 return 0;
16278
8d749320 16279 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
16280 baton->referenced_type = read_type_die (target_die->parent,
16281 target_cu);
df25ebbd
JB
16282 baton->offset_info.offset = offset;
16283 baton->offset_info.type = die_type (target_die, target_cu);
16284 prop->data.baton = baton;
16285 prop->kind = PROP_ADDR_OFFSET;
16286 break;
16287 }
80180f79
SA
16288 }
16289 }
16290 else if (attr_form_is_constant (attr))
16291 {
16292 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
16293 prop->kind = PROP_CONST;
16294 }
16295 else
16296 {
16297 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
16298 dwarf2_name (die, cu));
16299 return 0;
16300 }
16301
16302 return 1;
16303}
16304
a02abb62
JB
16305/* Read the given DW_AT_subrange DIE. */
16306
f792889a 16307static struct type *
a02abb62
JB
16308read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
16309{
4c9ad8c2 16310 struct type *base_type, *orig_base_type;
a02abb62
JB
16311 struct type *range_type;
16312 struct attribute *attr;
729efb13 16313 struct dynamic_prop low, high;
4fae6e18 16314 int low_default_is_valid;
c451ebe5 16315 int high_bound_is_count = 0;
15d034d0 16316 const char *name;
43bbcdc2 16317 LONGEST negative_mask;
e77813c8 16318
4c9ad8c2
TT
16319 orig_base_type = die_type (die, cu);
16320 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
16321 whereas the real type might be. So, we use ORIG_BASE_TYPE when
16322 creating the range type, but we use the result of check_typedef
16323 when examining properties of the type. */
16324 base_type = check_typedef (orig_base_type);
a02abb62 16325
7e314c57
JK
16326 /* The die_type call above may have already set the type for this DIE. */
16327 range_type = get_die_type (die, cu);
16328 if (range_type)
16329 return range_type;
16330
729efb13
SA
16331 low.kind = PROP_CONST;
16332 high.kind = PROP_CONST;
16333 high.data.const_val = 0;
16334
4fae6e18
JK
16335 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
16336 omitting DW_AT_lower_bound. */
16337 switch (cu->language)
6e70227d 16338 {
4fae6e18
JK
16339 case language_c:
16340 case language_cplus:
729efb13 16341 low.data.const_val = 0;
4fae6e18
JK
16342 low_default_is_valid = 1;
16343 break;
16344 case language_fortran:
729efb13 16345 low.data.const_val = 1;
4fae6e18
JK
16346 low_default_is_valid = 1;
16347 break;
16348 case language_d:
4fae6e18 16349 case language_objc:
c44af4eb 16350 case language_rust:
729efb13 16351 low.data.const_val = 0;
4fae6e18
JK
16352 low_default_is_valid = (cu->header.version >= 4);
16353 break;
16354 case language_ada:
16355 case language_m2:
16356 case language_pascal:
729efb13 16357 low.data.const_val = 1;
4fae6e18
JK
16358 low_default_is_valid = (cu->header.version >= 4);
16359 break;
16360 default:
729efb13 16361 low.data.const_val = 0;
4fae6e18
JK
16362 low_default_is_valid = 0;
16363 break;
a02abb62
JB
16364 }
16365
e142c38c 16366 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 16367 if (attr)
11c1ba78 16368 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
16369 else if (!low_default_is_valid)
16370 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
16371 "- DIE at 0x%x [in module %s]"),
9c541725 16372 to_underlying (die->sect_off), objfile_name (cu->objfile));
a02abb62 16373
e142c38c 16374 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 16375 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
16376 {
16377 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 16378 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 16379 {
c451ebe5
SA
16380 /* If bounds are constant do the final calculation here. */
16381 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
16382 high.data.const_val = low.data.const_val + high.data.const_val - 1;
16383 else
16384 high_bound_is_count = 1;
c2ff108b 16385 }
e77813c8
PM
16386 }
16387
16388 /* Dwarf-2 specifications explicitly allows to create subrange types
16389 without specifying a base type.
16390 In that case, the base type must be set to the type of
16391 the lower bound, upper bound or count, in that order, if any of these
16392 three attributes references an object that has a type.
16393 If no base type is found, the Dwarf-2 specifications say that
16394 a signed integer type of size equal to the size of an address should
16395 be used.
16396 For the following C code: `extern char gdb_int [];'
16397 GCC produces an empty range DIE.
16398 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 16399 high bound or count are not yet handled by this code. */
e77813c8
PM
16400 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
16401 {
16402 struct objfile *objfile = cu->objfile;
16403 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16404 int addr_size = gdbarch_addr_bit (gdbarch) /8;
16405 struct type *int_type = objfile_type (objfile)->builtin_int;
16406
16407 /* Test "int", "long int", and "long long int" objfile types,
16408 and select the first one having a size above or equal to the
16409 architecture address size. */
16410 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16411 base_type = int_type;
16412 else
16413 {
16414 int_type = objfile_type (objfile)->builtin_long;
16415 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16416 base_type = int_type;
16417 else
16418 {
16419 int_type = objfile_type (objfile)->builtin_long_long;
16420 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16421 base_type = int_type;
16422 }
16423 }
16424 }
a02abb62 16425
dbb9c2b1
JB
16426 /* Normally, the DWARF producers are expected to use a signed
16427 constant form (Eg. DW_FORM_sdata) to express negative bounds.
16428 But this is unfortunately not always the case, as witnessed
16429 with GCC, for instance, where the ambiguous DW_FORM_dataN form
16430 is used instead. To work around that ambiguity, we treat
16431 the bounds as signed, and thus sign-extend their values, when
16432 the base type is signed. */
6e70227d 16433 negative_mask =
66c6502d 16434 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
16435 if (low.kind == PROP_CONST
16436 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
16437 low.data.const_val |= negative_mask;
16438 if (high.kind == PROP_CONST
16439 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
16440 high.data.const_val |= negative_mask;
43bbcdc2 16441
729efb13 16442 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 16443
c451ebe5
SA
16444 if (high_bound_is_count)
16445 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
16446
c2ff108b
JK
16447 /* Ada expects an empty array on no boundary attributes. */
16448 if (attr == NULL && cu->language != language_ada)
729efb13 16449 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 16450
39cbfefa
DJ
16451 name = dwarf2_name (die, cu);
16452 if (name)
16453 TYPE_NAME (range_type) = name;
6e70227d 16454
e142c38c 16455 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
16456 if (attr)
16457 TYPE_LENGTH (range_type) = DW_UNSND (attr);
16458
7e314c57
JK
16459 set_die_type (die, range_type, cu);
16460
16461 /* set_die_type should be already done. */
b4ba55a1
JB
16462 set_descriptive_type (range_type, die, cu);
16463
7e314c57 16464 return range_type;
a02abb62 16465}
6e70227d 16466
f792889a 16467static struct type *
81a17f79
JB
16468read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
16469{
16470 struct type *type;
81a17f79 16471
81a17f79
JB
16472 /* For now, we only support the C meaning of an unspecified type: void. */
16473
19f392bc 16474 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 16475 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 16476
f792889a 16477 return set_die_type (die, type, cu);
81a17f79 16478}
a02abb62 16479
639d11d3
DC
16480/* Read a single die and all its descendents. Set the die's sibling
16481 field to NULL; set other fields in the die correctly, and set all
16482 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
16483 location of the info_ptr after reading all of those dies. PARENT
16484 is the parent of the die in question. */
16485
16486static struct die_info *
dee91e82 16487read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
16488 const gdb_byte *info_ptr,
16489 const gdb_byte **new_info_ptr,
dee91e82 16490 struct die_info *parent)
639d11d3
DC
16491{
16492 struct die_info *die;
d521ce57 16493 const gdb_byte *cur_ptr;
639d11d3
DC
16494 int has_children;
16495
bf6af496 16496 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
16497 if (die == NULL)
16498 {
16499 *new_info_ptr = cur_ptr;
16500 return NULL;
16501 }
93311388 16502 store_in_ref_table (die, reader->cu);
639d11d3
DC
16503
16504 if (has_children)
bf6af496 16505 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
16506 else
16507 {
16508 die->child = NULL;
16509 *new_info_ptr = cur_ptr;
16510 }
16511
16512 die->sibling = NULL;
16513 die->parent = parent;
16514 return die;
16515}
16516
16517/* Read a die, all of its descendents, and all of its siblings; set
16518 all of the fields of all of the dies correctly. Arguments are as
16519 in read_die_and_children. */
16520
16521static struct die_info *
bf6af496 16522read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
16523 const gdb_byte *info_ptr,
16524 const gdb_byte **new_info_ptr,
bf6af496 16525 struct die_info *parent)
639d11d3
DC
16526{
16527 struct die_info *first_die, *last_sibling;
d521ce57 16528 const gdb_byte *cur_ptr;
639d11d3 16529
c906108c 16530 cur_ptr = info_ptr;
639d11d3
DC
16531 first_die = last_sibling = NULL;
16532
16533 while (1)
c906108c 16534 {
639d11d3 16535 struct die_info *die
dee91e82 16536 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 16537
1d325ec1 16538 if (die == NULL)
c906108c 16539 {
639d11d3
DC
16540 *new_info_ptr = cur_ptr;
16541 return first_die;
c906108c 16542 }
1d325ec1
DJ
16543
16544 if (!first_die)
16545 first_die = die;
c906108c 16546 else
1d325ec1
DJ
16547 last_sibling->sibling = die;
16548
16549 last_sibling = die;
c906108c 16550 }
c906108c
SS
16551}
16552
bf6af496
DE
16553/* Read a die, all of its descendents, and all of its siblings; set
16554 all of the fields of all of the dies correctly. Arguments are as
16555 in read_die_and_children.
16556 This the main entry point for reading a DIE and all its children. */
16557
16558static struct die_info *
16559read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
16560 const gdb_byte *info_ptr,
16561 const gdb_byte **new_info_ptr,
bf6af496
DE
16562 struct die_info *parent)
16563{
16564 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
16565 new_info_ptr, parent);
16566
b4f54984 16567 if (dwarf_die_debug)
bf6af496
DE
16568 {
16569 fprintf_unfiltered (gdb_stdlog,
16570 "Read die from %s@0x%x of %s:\n",
a32a8923 16571 get_section_name (reader->die_section),
bf6af496
DE
16572 (unsigned) (info_ptr - reader->die_section->buffer),
16573 bfd_get_filename (reader->abfd));
b4f54984 16574 dump_die (die, dwarf_die_debug);
bf6af496
DE
16575 }
16576
16577 return die;
16578}
16579
3019eac3
DE
16580/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
16581 attributes.
16582 The caller is responsible for filling in the extra attributes
16583 and updating (*DIEP)->num_attrs.
16584 Set DIEP to point to a newly allocated die with its information,
16585 except for its child, sibling, and parent fields.
16586 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 16587
d521ce57 16588static const gdb_byte *
3019eac3 16589read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 16590 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 16591 int *has_children, int num_extra_attrs)
93311388 16592{
b64f50a1 16593 unsigned int abbrev_number, bytes_read, i;
93311388
DE
16594 struct abbrev_info *abbrev;
16595 struct die_info *die;
16596 struct dwarf2_cu *cu = reader->cu;
16597 bfd *abfd = reader->abfd;
16598
9c541725 16599 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
16600 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16601 info_ptr += bytes_read;
16602 if (!abbrev_number)
16603 {
16604 *diep = NULL;
16605 *has_children = 0;
16606 return info_ptr;
16607 }
16608
433df2d4 16609 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 16610 if (!abbrev)
348e048f
DE
16611 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
16612 abbrev_number,
16613 bfd_get_filename (abfd));
16614
3019eac3 16615 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 16616 die->sect_off = sect_off;
93311388
DE
16617 die->tag = abbrev->tag;
16618 die->abbrev = abbrev_number;
16619
3019eac3
DE
16620 /* Make the result usable.
16621 The caller needs to update num_attrs after adding the extra
16622 attributes. */
93311388
DE
16623 die->num_attrs = abbrev->num_attrs;
16624
16625 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
16626 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
16627 info_ptr);
93311388
DE
16628
16629 *diep = die;
16630 *has_children = abbrev->has_children;
16631 return info_ptr;
16632}
16633
3019eac3
DE
16634/* Read a die and all its attributes.
16635 Set DIEP to point to a newly allocated die with its information,
16636 except for its child, sibling, and parent fields.
16637 Set HAS_CHILDREN to tell whether the die has children or not. */
16638
d521ce57 16639static const gdb_byte *
3019eac3 16640read_full_die (const struct die_reader_specs *reader,
d521ce57 16641 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
16642 int *has_children)
16643{
d521ce57 16644 const gdb_byte *result;
bf6af496
DE
16645
16646 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
16647
b4f54984 16648 if (dwarf_die_debug)
bf6af496
DE
16649 {
16650 fprintf_unfiltered (gdb_stdlog,
16651 "Read die from %s@0x%x of %s:\n",
a32a8923 16652 get_section_name (reader->die_section),
bf6af496
DE
16653 (unsigned) (info_ptr - reader->die_section->buffer),
16654 bfd_get_filename (reader->abfd));
b4f54984 16655 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
16656 }
16657
16658 return result;
3019eac3 16659}
433df2d4
DE
16660\f
16661/* Abbreviation tables.
3019eac3 16662
433df2d4 16663 In DWARF version 2, the description of the debugging information is
c906108c
SS
16664 stored in a separate .debug_abbrev section. Before we read any
16665 dies from a section we read in all abbreviations and install them
433df2d4
DE
16666 in a hash table. */
16667
16668/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
16669
16670static struct abbrev_info *
16671abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
16672{
16673 struct abbrev_info *abbrev;
16674
8d749320 16675 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 16676 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 16677
433df2d4
DE
16678 return abbrev;
16679}
16680
16681/* Add an abbreviation to the table. */
c906108c
SS
16682
16683static void
433df2d4
DE
16684abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
16685 unsigned int abbrev_number,
16686 struct abbrev_info *abbrev)
16687{
16688 unsigned int hash_number;
16689
16690 hash_number = abbrev_number % ABBREV_HASH_SIZE;
16691 abbrev->next = abbrev_table->abbrevs[hash_number];
16692 abbrev_table->abbrevs[hash_number] = abbrev;
16693}
dee91e82 16694
433df2d4
DE
16695/* Look up an abbrev in the table.
16696 Returns NULL if the abbrev is not found. */
16697
16698static struct abbrev_info *
16699abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
16700 unsigned int abbrev_number)
c906108c 16701{
433df2d4
DE
16702 unsigned int hash_number;
16703 struct abbrev_info *abbrev;
16704
16705 hash_number = abbrev_number % ABBREV_HASH_SIZE;
16706 abbrev = abbrev_table->abbrevs[hash_number];
16707
16708 while (abbrev)
16709 {
16710 if (abbrev->number == abbrev_number)
16711 return abbrev;
16712 abbrev = abbrev->next;
16713 }
16714 return NULL;
16715}
16716
16717/* Read in an abbrev table. */
16718
16719static struct abbrev_table *
16720abbrev_table_read_table (struct dwarf2_section_info *section,
9c541725 16721 sect_offset sect_off)
433df2d4
DE
16722{
16723 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 16724 bfd *abfd = get_section_bfd_owner (section);
433df2d4 16725 struct abbrev_table *abbrev_table;
d521ce57 16726 const gdb_byte *abbrev_ptr;
c906108c
SS
16727 struct abbrev_info *cur_abbrev;
16728 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 16729 unsigned int abbrev_form;
f3dd6933
DJ
16730 struct attr_abbrev *cur_attrs;
16731 unsigned int allocated_attrs;
c906108c 16732
70ba0933 16733 abbrev_table = XNEW (struct abbrev_table);
9c541725 16734 abbrev_table->sect_off = sect_off;
433df2d4 16735 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
16736 abbrev_table->abbrevs =
16737 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
16738 ABBREV_HASH_SIZE);
433df2d4
DE
16739 memset (abbrev_table->abbrevs, 0,
16740 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 16741
433df2d4 16742 dwarf2_read_section (objfile, section);
9c541725 16743 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
16744 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16745 abbrev_ptr += bytes_read;
16746
f3dd6933 16747 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 16748 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 16749
0963b4bd 16750 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
16751 while (abbrev_number)
16752 {
433df2d4 16753 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
16754
16755 /* read in abbrev header */
16756 cur_abbrev->number = abbrev_number;
aead7601
SM
16757 cur_abbrev->tag
16758 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
16759 abbrev_ptr += bytes_read;
16760 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
16761 abbrev_ptr += 1;
16762
16763 /* now read in declarations */
22d2f3ab 16764 for (;;)
c906108c 16765 {
43988095
JK
16766 LONGEST implicit_const;
16767
22d2f3ab
JK
16768 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16769 abbrev_ptr += bytes_read;
16770 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16771 abbrev_ptr += bytes_read;
43988095
JK
16772 if (abbrev_form == DW_FORM_implicit_const)
16773 {
16774 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
16775 &bytes_read);
16776 abbrev_ptr += bytes_read;
16777 }
16778 else
16779 {
16780 /* Initialize it due to a false compiler warning. */
16781 implicit_const = -1;
16782 }
22d2f3ab
JK
16783
16784 if (abbrev_name == 0)
16785 break;
16786
f3dd6933 16787 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 16788 {
f3dd6933
DJ
16789 allocated_attrs += ATTR_ALLOC_CHUNK;
16790 cur_attrs
224c3ddb 16791 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 16792 }
ae038cb0 16793
aead7601
SM
16794 cur_attrs[cur_abbrev->num_attrs].name
16795 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 16796 cur_attrs[cur_abbrev->num_attrs].form
aead7601 16797 = (enum dwarf_form) abbrev_form;
43988095 16798 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 16799 ++cur_abbrev->num_attrs;
c906108c
SS
16800 }
16801
8d749320
SM
16802 cur_abbrev->attrs =
16803 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
16804 cur_abbrev->num_attrs);
f3dd6933
DJ
16805 memcpy (cur_abbrev->attrs, cur_attrs,
16806 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
16807
433df2d4 16808 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
16809
16810 /* Get next abbreviation.
16811 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
16812 always properly terminated with an abbrev number of 0.
16813 Exit loop if we encounter an abbreviation which we have
16814 already read (which means we are about to read the abbreviations
16815 for the next compile unit) or if the end of the abbreviation
16816 table is reached. */
433df2d4 16817 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
16818 break;
16819 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16820 abbrev_ptr += bytes_read;
433df2d4 16821 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
16822 break;
16823 }
f3dd6933
DJ
16824
16825 xfree (cur_attrs);
433df2d4 16826 return abbrev_table;
c906108c
SS
16827}
16828
433df2d4 16829/* Free the resources held by ABBREV_TABLE. */
c906108c 16830
c906108c 16831static void
433df2d4 16832abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 16833{
433df2d4
DE
16834 obstack_free (&abbrev_table->abbrev_obstack, NULL);
16835 xfree (abbrev_table);
c906108c
SS
16836}
16837
f4dc4d17
DE
16838/* Same as abbrev_table_free but as a cleanup.
16839 We pass in a pointer to the pointer to the table so that we can
16840 set the pointer to NULL when we're done. It also simplifies
73051182 16841 build_type_psymtabs_1. */
f4dc4d17
DE
16842
16843static void
16844abbrev_table_free_cleanup (void *table_ptr)
16845{
9a3c8263 16846 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
16847
16848 if (*abbrev_table_ptr != NULL)
16849 abbrev_table_free (*abbrev_table_ptr);
16850 *abbrev_table_ptr = NULL;
16851}
16852
433df2d4
DE
16853/* Read the abbrev table for CU from ABBREV_SECTION. */
16854
16855static void
16856dwarf2_read_abbrevs (struct dwarf2_cu *cu,
16857 struct dwarf2_section_info *abbrev_section)
c906108c 16858{
433df2d4 16859 cu->abbrev_table =
9c541725 16860 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
433df2d4 16861}
c906108c 16862
433df2d4 16863/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 16864
433df2d4
DE
16865static void
16866dwarf2_free_abbrev_table (void *ptr_to_cu)
16867{
9a3c8263 16868 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 16869
a2ce51a0
DE
16870 if (cu->abbrev_table != NULL)
16871 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
16872 /* Set this to NULL so that we SEGV if we try to read it later,
16873 and also because free_comp_unit verifies this is NULL. */
16874 cu->abbrev_table = NULL;
16875}
16876\f
72bf9492
DJ
16877/* Returns nonzero if TAG represents a type that we might generate a partial
16878 symbol for. */
16879
16880static int
16881is_type_tag_for_partial (int tag)
16882{
16883 switch (tag)
16884 {
16885#if 0
16886 /* Some types that would be reasonable to generate partial symbols for,
16887 that we don't at present. */
16888 case DW_TAG_array_type:
16889 case DW_TAG_file_type:
16890 case DW_TAG_ptr_to_member_type:
16891 case DW_TAG_set_type:
16892 case DW_TAG_string_type:
16893 case DW_TAG_subroutine_type:
16894#endif
16895 case DW_TAG_base_type:
16896 case DW_TAG_class_type:
680b30c7 16897 case DW_TAG_interface_type:
72bf9492
DJ
16898 case DW_TAG_enumeration_type:
16899 case DW_TAG_structure_type:
16900 case DW_TAG_subrange_type:
16901 case DW_TAG_typedef:
16902 case DW_TAG_union_type:
16903 return 1;
16904 default:
16905 return 0;
16906 }
16907}
16908
16909/* Load all DIEs that are interesting for partial symbols into memory. */
16910
16911static struct partial_die_info *
dee91e82 16912load_partial_dies (const struct die_reader_specs *reader,
d521ce57 16913 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 16914{
dee91e82 16915 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16916 struct objfile *objfile = cu->objfile;
72bf9492
DJ
16917 struct partial_die_info *part_die;
16918 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
16919 struct abbrev_info *abbrev;
16920 unsigned int bytes_read;
5afb4e99 16921 unsigned int load_all = 0;
72bf9492
DJ
16922 int nesting_level = 1;
16923
16924 parent_die = NULL;
16925 last_die = NULL;
16926
7adf1e79
DE
16927 gdb_assert (cu->per_cu != NULL);
16928 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
16929 load_all = 1;
16930
72bf9492
DJ
16931 cu->partial_dies
16932 = htab_create_alloc_ex (cu->header.length / 12,
16933 partial_die_hash,
16934 partial_die_eq,
16935 NULL,
16936 &cu->comp_unit_obstack,
16937 hashtab_obstack_allocate,
16938 dummy_obstack_deallocate);
16939
8d749320 16940 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16941
16942 while (1)
16943 {
16944 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
16945
16946 /* A NULL abbrev means the end of a series of children. */
16947 if (abbrev == NULL)
16948 {
16949 if (--nesting_level == 0)
16950 {
16951 /* PART_DIE was probably the last thing allocated on the
16952 comp_unit_obstack, so we could call obstack_free
16953 here. We don't do that because the waste is small,
16954 and will be cleaned up when we're done with this
16955 compilation unit. This way, we're also more robust
16956 against other users of the comp_unit_obstack. */
16957 return first_die;
16958 }
16959 info_ptr += bytes_read;
16960 last_die = parent_die;
16961 parent_die = parent_die->die_parent;
16962 continue;
16963 }
16964
98bfdba5
PA
16965 /* Check for template arguments. We never save these; if
16966 they're seen, we just mark the parent, and go on our way. */
16967 if (parent_die != NULL
16968 && cu->language == language_cplus
16969 && (abbrev->tag == DW_TAG_template_type_param
16970 || abbrev->tag == DW_TAG_template_value_param))
16971 {
16972 parent_die->has_template_arguments = 1;
16973
16974 if (!load_all)
16975 {
16976 /* We don't need a partial DIE for the template argument. */
dee91e82 16977 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
16978 continue;
16979 }
16980 }
16981
0d99eb77 16982 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
16983 Skip their other children. */
16984 if (!load_all
16985 && cu->language == language_cplus
16986 && parent_die != NULL
16987 && parent_die->tag == DW_TAG_subprogram)
16988 {
dee91e82 16989 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
16990 continue;
16991 }
16992
5afb4e99
DJ
16993 /* Check whether this DIE is interesting enough to save. Normally
16994 we would not be interested in members here, but there may be
16995 later variables referencing them via DW_AT_specification (for
16996 static members). */
16997 if (!load_all
16998 && !is_type_tag_for_partial (abbrev->tag)
72929c62 16999 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
17000 && abbrev->tag != DW_TAG_enumerator
17001 && abbrev->tag != DW_TAG_subprogram
bc30ff58 17002 && abbrev->tag != DW_TAG_lexical_block
72bf9492 17003 && abbrev->tag != DW_TAG_variable
5afb4e99 17004 && abbrev->tag != DW_TAG_namespace
f55ee35c 17005 && abbrev->tag != DW_TAG_module
95554aad 17006 && abbrev->tag != DW_TAG_member
74921315
KS
17007 && abbrev->tag != DW_TAG_imported_unit
17008 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
17009 {
17010 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17011 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
17012 continue;
17013 }
17014
dee91e82
DE
17015 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
17016 info_ptr);
72bf9492
DJ
17017
17018 /* This two-pass algorithm for processing partial symbols has a
17019 high cost in cache pressure. Thus, handle some simple cases
17020 here which cover the majority of C partial symbols. DIEs
17021 which neither have specification tags in them, nor could have
17022 specification tags elsewhere pointing at them, can simply be
17023 processed and discarded.
17024
17025 This segment is also optional; scan_partial_symbols and
17026 add_partial_symbol will handle these DIEs if we chain
17027 them in normally. When compilers which do not emit large
17028 quantities of duplicate debug information are more common,
17029 this code can probably be removed. */
17030
17031 /* Any complete simple types at the top level (pretty much all
17032 of them, for a language without namespaces), can be processed
17033 directly. */
17034 if (parent_die == NULL
17035 && part_die->has_specification == 0
17036 && part_die->is_declaration == 0
d8228535 17037 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
17038 || part_die->tag == DW_TAG_base_type
17039 || part_die->tag == DW_TAG_subrange_type))
17040 {
17041 if (building_psymtab && part_die->name != NULL)
04a679b8 17042 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 17043 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 17044 &objfile->static_psymbols,
1762568f 17045 0, cu->language, objfile);
dee91e82 17046 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
17047 continue;
17048 }
17049
d8228535
JK
17050 /* The exception for DW_TAG_typedef with has_children above is
17051 a workaround of GCC PR debug/47510. In the case of this complaint
17052 type_name_no_tag_or_error will error on such types later.
17053
17054 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17055 it could not find the child DIEs referenced later, this is checked
17056 above. In correct DWARF DW_TAG_typedef should have no children. */
17057
17058 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
17059 complaint (&symfile_complaints,
17060 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17061 "- DIE at 0x%x [in module %s]"),
9c541725 17062 to_underlying (part_die->sect_off), objfile_name (objfile));
d8228535 17063
72bf9492
DJ
17064 /* If we're at the second level, and we're an enumerator, and
17065 our parent has no specification (meaning possibly lives in a
17066 namespace elsewhere), then we can add the partial symbol now
17067 instead of queueing it. */
17068 if (part_die->tag == DW_TAG_enumerator
17069 && parent_die != NULL
17070 && parent_die->die_parent == NULL
17071 && parent_die->tag == DW_TAG_enumeration_type
17072 && parent_die->has_specification == 0)
17073 {
17074 if (part_die->name == NULL)
3e43a32a
MS
17075 complaint (&symfile_complaints,
17076 _("malformed enumerator DIE ignored"));
72bf9492 17077 else if (building_psymtab)
04a679b8 17078 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 17079 VAR_DOMAIN, LOC_CONST,
9c37b5ae 17080 cu->language == language_cplus
bb5ed363
DE
17081 ? &objfile->global_psymbols
17082 : &objfile->static_psymbols,
1762568f 17083 0, cu->language, objfile);
72bf9492 17084
dee91e82 17085 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
17086 continue;
17087 }
17088
17089 /* We'll save this DIE so link it in. */
17090 part_die->die_parent = parent_die;
17091 part_die->die_sibling = NULL;
17092 part_die->die_child = NULL;
17093
17094 if (last_die && last_die == parent_die)
17095 last_die->die_child = part_die;
17096 else if (last_die)
17097 last_die->die_sibling = part_die;
17098
17099 last_die = part_die;
17100
17101 if (first_die == NULL)
17102 first_die = part_die;
17103
17104 /* Maybe add the DIE to the hash table. Not all DIEs that we
17105 find interesting need to be in the hash table, because we
17106 also have the parent/sibling/child chains; only those that we
17107 might refer to by offset later during partial symbol reading.
17108
17109 For now this means things that might have be the target of a
17110 DW_AT_specification, DW_AT_abstract_origin, or
17111 DW_AT_extension. DW_AT_extension will refer only to
17112 namespaces; DW_AT_abstract_origin refers to functions (and
17113 many things under the function DIE, but we do not recurse
17114 into function DIEs during partial symbol reading) and
17115 possibly variables as well; DW_AT_specification refers to
17116 declarations. Declarations ought to have the DW_AT_declaration
17117 flag. It happens that GCC forgets to put it in sometimes, but
17118 only for functions, not for types.
17119
17120 Adding more things than necessary to the hash table is harmless
17121 except for the performance cost. Adding too few will result in
5afb4e99
DJ
17122 wasted time in find_partial_die, when we reread the compilation
17123 unit with load_all_dies set. */
72bf9492 17124
5afb4e99 17125 if (load_all
72929c62 17126 || abbrev->tag == DW_TAG_constant
5afb4e99 17127 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
17128 || abbrev->tag == DW_TAG_variable
17129 || abbrev->tag == DW_TAG_namespace
17130 || part_die->is_declaration)
17131 {
17132 void **slot;
17133
17134 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
17135 to_underlying (part_die->sect_off),
17136 INSERT);
72bf9492
DJ
17137 *slot = part_die;
17138 }
17139
8d749320 17140 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
17141
17142 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 17143 we have no reason to follow the children of structures; for other
98bfdba5
PA
17144 languages we have to, so that we can get at method physnames
17145 to infer fully qualified class names, for DW_AT_specification,
17146 and for C++ template arguments. For C++, we also look one level
17147 inside functions to find template arguments (if the name of the
17148 function does not already contain the template arguments).
bc30ff58
JB
17149
17150 For Ada, we need to scan the children of subprograms and lexical
17151 blocks as well because Ada allows the definition of nested
17152 entities that could be interesting for the debugger, such as
17153 nested subprograms for instance. */
72bf9492 17154 if (last_die->has_children
5afb4e99
DJ
17155 && (load_all
17156 || last_die->tag == DW_TAG_namespace
f55ee35c 17157 || last_die->tag == DW_TAG_module
72bf9492 17158 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
17159 || (cu->language == language_cplus
17160 && last_die->tag == DW_TAG_subprogram
17161 && (last_die->name == NULL
17162 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
17163 || (cu->language != language_c
17164 && (last_die->tag == DW_TAG_class_type
680b30c7 17165 || last_die->tag == DW_TAG_interface_type
72bf9492 17166 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
17167 || last_die->tag == DW_TAG_union_type))
17168 || (cu->language == language_ada
17169 && (last_die->tag == DW_TAG_subprogram
17170 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
17171 {
17172 nesting_level++;
17173 parent_die = last_die;
17174 continue;
17175 }
17176
17177 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17178 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
17179
17180 /* Back to the top, do it again. */
17181 }
17182}
17183
c906108c
SS
17184/* Read a minimal amount of information into the minimal die structure. */
17185
d521ce57 17186static const gdb_byte *
dee91e82
DE
17187read_partial_die (const struct die_reader_specs *reader,
17188 struct partial_die_info *part_die,
17189 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 17190 const gdb_byte *info_ptr)
c906108c 17191{
dee91e82 17192 struct dwarf2_cu *cu = reader->cu;
bb5ed363 17193 struct objfile *objfile = cu->objfile;
d521ce57 17194 const gdb_byte *buffer = reader->buffer;
fa238c03 17195 unsigned int i;
c906108c 17196 struct attribute attr;
c5aa993b 17197 int has_low_pc_attr = 0;
c906108c 17198 int has_high_pc_attr = 0;
91da1414 17199 int high_pc_relative = 0;
c906108c 17200
72bf9492 17201 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 17202
9c541725 17203 part_die->sect_off = (sect_offset) (info_ptr - buffer);
72bf9492
DJ
17204
17205 info_ptr += abbrev_len;
17206
17207 if (abbrev == NULL)
17208 return info_ptr;
17209
c906108c
SS
17210 part_die->tag = abbrev->tag;
17211 part_die->has_children = abbrev->has_children;
c906108c
SS
17212
17213 for (i = 0; i < abbrev->num_attrs; ++i)
17214 {
dee91e82 17215 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
17216
17217 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 17218 partial symbol table. */
c906108c
SS
17219 switch (attr.name)
17220 {
17221 case DW_AT_name:
71c25dea
TT
17222 switch (part_die->tag)
17223 {
17224 case DW_TAG_compile_unit:
95554aad 17225 case DW_TAG_partial_unit:
348e048f 17226 case DW_TAG_type_unit:
71c25dea
TT
17227 /* Compilation units have a DW_AT_name that is a filename, not
17228 a source language identifier. */
17229 case DW_TAG_enumeration_type:
17230 case DW_TAG_enumerator:
17231 /* These tags always have simple identifiers already; no need
17232 to canonicalize them. */
17233 part_die->name = DW_STRING (&attr);
17234 break;
17235 default:
17236 part_die->name
17237 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 17238 &objfile->per_bfd->storage_obstack);
71c25dea
TT
17239 break;
17240 }
c906108c 17241 break;
31ef98ae 17242 case DW_AT_linkage_name:
c906108c 17243 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
17244 /* Note that both forms of linkage name might appear. We
17245 assume they will be the same, and we only store the last
17246 one we see. */
94af9270
KS
17247 if (cu->language == language_ada)
17248 part_die->name = DW_STRING (&attr);
abc72ce4 17249 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
17250 break;
17251 case DW_AT_low_pc:
17252 has_low_pc_attr = 1;
31aa7e4e 17253 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
17254 break;
17255 case DW_AT_high_pc:
17256 has_high_pc_attr = 1;
31aa7e4e
JB
17257 part_die->highpc = attr_value_as_address (&attr);
17258 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
17259 high_pc_relative = 1;
c906108c
SS
17260 break;
17261 case DW_AT_location:
0963b4bd 17262 /* Support the .debug_loc offsets. */
8e19ed76
PS
17263 if (attr_form_is_block (&attr))
17264 {
95554aad 17265 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 17266 }
3690dd37 17267 else if (attr_form_is_section_offset (&attr))
8e19ed76 17268 {
4d3c2250 17269 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
17270 }
17271 else
17272 {
4d3c2250
KB
17273 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17274 "partial symbol information");
8e19ed76 17275 }
c906108c 17276 break;
c906108c
SS
17277 case DW_AT_external:
17278 part_die->is_external = DW_UNSND (&attr);
17279 break;
17280 case DW_AT_declaration:
17281 part_die->is_declaration = DW_UNSND (&attr);
17282 break;
17283 case DW_AT_type:
17284 part_die->has_type = 1;
17285 break;
17286 case DW_AT_abstract_origin:
17287 case DW_AT_specification:
72bf9492
DJ
17288 case DW_AT_extension:
17289 part_die->has_specification = 1;
c764a876 17290 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
17291 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17292 || cu->per_cu->is_dwz);
c906108c
SS
17293 break;
17294 case DW_AT_sibling:
17295 /* Ignore absolute siblings, they might point outside of
17296 the current compile unit. */
17297 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
17298 complaint (&symfile_complaints,
17299 _("ignoring absolute DW_AT_sibling"));
c906108c 17300 else
b9502d3f 17301 {
9c541725
PA
17302 sect_offset off = dwarf2_get_ref_die_offset (&attr);
17303 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
17304
17305 if (sibling_ptr < info_ptr)
17306 complaint (&symfile_complaints,
17307 _("DW_AT_sibling points backwards"));
22869d73
KS
17308 else if (sibling_ptr > reader->buffer_end)
17309 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
17310 else
17311 part_die->sibling = sibling_ptr;
17312 }
c906108c 17313 break;
fa4028e9
JB
17314 case DW_AT_byte_size:
17315 part_die->has_byte_size = 1;
17316 break;
ff908ebf
AW
17317 case DW_AT_const_value:
17318 part_die->has_const_value = 1;
17319 break;
68511cec
CES
17320 case DW_AT_calling_convention:
17321 /* DWARF doesn't provide a way to identify a program's source-level
17322 entry point. DW_AT_calling_convention attributes are only meant
17323 to describe functions' calling conventions.
17324
17325 However, because it's a necessary piece of information in
0c1b455e
TT
17326 Fortran, and before DWARF 4 DW_CC_program was the only
17327 piece of debugging information whose definition refers to
17328 a 'main program' at all, several compilers marked Fortran
17329 main programs with DW_CC_program --- even when those
17330 functions use the standard calling conventions.
17331
17332 Although DWARF now specifies a way to provide this
17333 information, we support this practice for backward
17334 compatibility. */
68511cec 17335 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
17336 && cu->language == language_fortran)
17337 part_die->main_subprogram = 1;
68511cec 17338 break;
481860b3
GB
17339 case DW_AT_inline:
17340 if (DW_UNSND (&attr) == DW_INL_inlined
17341 || DW_UNSND (&attr) == DW_INL_declared_inlined)
17342 part_die->may_be_inlined = 1;
17343 break;
95554aad
TT
17344
17345 case DW_AT_import:
17346 if (part_die->tag == DW_TAG_imported_unit)
36586728 17347 {
9c541725 17348 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
36586728
TT
17349 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17350 || cu->per_cu->is_dwz);
17351 }
95554aad
TT
17352 break;
17353
0c1b455e
TT
17354 case DW_AT_main_subprogram:
17355 part_die->main_subprogram = DW_UNSND (&attr);
17356 break;
17357
c906108c
SS
17358 default:
17359 break;
17360 }
17361 }
17362
91da1414
MW
17363 if (high_pc_relative)
17364 part_die->highpc += part_die->lowpc;
17365
9373cf26
JK
17366 if (has_low_pc_attr && has_high_pc_attr)
17367 {
17368 /* When using the GNU linker, .gnu.linkonce. sections are used to
17369 eliminate duplicate copies of functions and vtables and such.
17370 The linker will arbitrarily choose one and discard the others.
17371 The AT_*_pc values for such functions refer to local labels in
17372 these sections. If the section from that file was discarded, the
17373 labels are not in the output, so the relocs get a value of 0.
17374 If this is a discarded function, mark the pc bounds as invalid,
17375 so that GDB will ignore it. */
17376 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
17377 {
bb5ed363 17378 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
17379
17380 complaint (&symfile_complaints,
17381 _("DW_AT_low_pc %s is zero "
17382 "for DIE at 0x%x [in module %s]"),
17383 paddress (gdbarch, part_die->lowpc),
9c541725 17384 to_underlying (part_die->sect_off), objfile_name (objfile));
9373cf26
JK
17385 }
17386 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
17387 else if (part_die->lowpc >= part_die->highpc)
17388 {
bb5ed363 17389 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
17390
17391 complaint (&symfile_complaints,
17392 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
17393 "for DIE at 0x%x [in module %s]"),
17394 paddress (gdbarch, part_die->lowpc),
17395 paddress (gdbarch, part_die->highpc),
9c541725
PA
17396 to_underlying (part_die->sect_off),
17397 objfile_name (objfile));
9373cf26
JK
17398 }
17399 else
17400 part_die->has_pc_info = 1;
17401 }
85cbf3d3 17402
c906108c
SS
17403 return info_ptr;
17404}
17405
72bf9492
DJ
17406/* Find a cached partial DIE at OFFSET in CU. */
17407
17408static struct partial_die_info *
9c541725 17409find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
72bf9492
DJ
17410{
17411 struct partial_die_info *lookup_die = NULL;
17412 struct partial_die_info part_die;
17413
9c541725 17414 part_die.sect_off = sect_off;
9a3c8263
SM
17415 lookup_die = ((struct partial_die_info *)
17416 htab_find_with_hash (cu->partial_dies, &part_die,
9c541725 17417 to_underlying (sect_off)));
72bf9492 17418
72bf9492
DJ
17419 return lookup_die;
17420}
17421
348e048f
DE
17422/* Find a partial DIE at OFFSET, which may or may not be in CU,
17423 except in the case of .debug_types DIEs which do not reference
17424 outside their CU (they do however referencing other types via
55f1336d 17425 DW_FORM_ref_sig8). */
72bf9492
DJ
17426
17427static struct partial_die_info *
9c541725 17428find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 17429{
bb5ed363 17430 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
17431 struct dwarf2_per_cu_data *per_cu = NULL;
17432 struct partial_die_info *pd = NULL;
72bf9492 17433
36586728 17434 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 17435 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 17436 {
9c541725 17437 pd = find_partial_die_in_comp_unit (sect_off, cu);
5afb4e99
DJ
17438 if (pd != NULL)
17439 return pd;
0d99eb77
DE
17440 /* We missed recording what we needed.
17441 Load all dies and try again. */
17442 per_cu = cu->per_cu;
5afb4e99 17443 }
0d99eb77
DE
17444 else
17445 {
17446 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 17447 if (cu->per_cu->is_debug_types)
0d99eb77 17448 {
9c541725
PA
17449 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
17450 " external reference to offset 0x%x [in module %s].\n"),
17451 to_underlying (cu->header.sect_off), to_underlying (sect_off),
0d99eb77
DE
17452 bfd_get_filename (objfile->obfd));
17453 }
9c541725 17454 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 17455 objfile);
72bf9492 17456
0d99eb77
DE
17457 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
17458 load_partial_comp_unit (per_cu);
ae038cb0 17459
0d99eb77 17460 per_cu->cu->last_used = 0;
9c541725 17461 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
0d99eb77 17462 }
5afb4e99 17463
dee91e82
DE
17464 /* If we didn't find it, and not all dies have been loaded,
17465 load them all and try again. */
17466
5afb4e99
DJ
17467 if (pd == NULL && per_cu->load_all_dies == 0)
17468 {
5afb4e99 17469 per_cu->load_all_dies = 1;
fd820528
DE
17470
17471 /* This is nasty. When we reread the DIEs, somewhere up the call chain
17472 THIS_CU->cu may already be in use. So we can't just free it and
17473 replace its DIEs with the ones we read in. Instead, we leave those
17474 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
17475 and clobber THIS_CU->cu->partial_dies with the hash table for the new
17476 set. */
dee91e82 17477 load_partial_comp_unit (per_cu);
5afb4e99 17478
9c541725 17479 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
5afb4e99
DJ
17480 }
17481
17482 if (pd == NULL)
17483 internal_error (__FILE__, __LINE__,
3e43a32a
MS
17484 _("could not find partial DIE 0x%x "
17485 "in cache [from module %s]\n"),
9c541725 17486 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 17487 return pd;
72bf9492
DJ
17488}
17489
abc72ce4
DE
17490/* See if we can figure out if the class lives in a namespace. We do
17491 this by looking for a member function; its demangled name will
17492 contain namespace info, if there is any. */
17493
17494static void
17495guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
17496 struct dwarf2_cu *cu)
17497{
17498 /* NOTE: carlton/2003-10-07: Getting the info this way changes
17499 what template types look like, because the demangler
17500 frequently doesn't give the same name as the debug info. We
17501 could fix this by only using the demangled name to get the
17502 prefix (but see comment in read_structure_type). */
17503
17504 struct partial_die_info *real_pdi;
17505 struct partial_die_info *child_pdi;
17506
17507 /* If this DIE (this DIE's specification, if any) has a parent, then
17508 we should not do this. We'll prepend the parent's fully qualified
17509 name when we create the partial symbol. */
17510
17511 real_pdi = struct_pdi;
17512 while (real_pdi->has_specification)
36586728
TT
17513 real_pdi = find_partial_die (real_pdi->spec_offset,
17514 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
17515
17516 if (real_pdi->die_parent != NULL)
17517 return;
17518
17519 for (child_pdi = struct_pdi->die_child;
17520 child_pdi != NULL;
17521 child_pdi = child_pdi->die_sibling)
17522 {
17523 if (child_pdi->tag == DW_TAG_subprogram
17524 && child_pdi->linkage_name != NULL)
17525 {
17526 char *actual_class_name
17527 = language_class_name_from_physname (cu->language_defn,
17528 child_pdi->linkage_name);
17529 if (actual_class_name != NULL)
17530 {
17531 struct_pdi->name
224c3ddb
SM
17532 = ((const char *)
17533 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
17534 actual_class_name,
17535 strlen (actual_class_name)));
abc72ce4
DE
17536 xfree (actual_class_name);
17537 }
17538 break;
17539 }
17540 }
17541}
17542
72bf9492
DJ
17543/* Adjust PART_DIE before generating a symbol for it. This function
17544 may set the is_external flag or change the DIE's name. */
17545
17546static void
17547fixup_partial_die (struct partial_die_info *part_die,
17548 struct dwarf2_cu *cu)
17549{
abc72ce4
DE
17550 /* Once we've fixed up a die, there's no point in doing so again.
17551 This also avoids a memory leak if we were to call
17552 guess_partial_die_structure_name multiple times. */
17553 if (part_die->fixup_called)
17554 return;
17555
72bf9492
DJ
17556 /* If we found a reference attribute and the DIE has no name, try
17557 to find a name in the referred to DIE. */
17558
17559 if (part_die->name == NULL && part_die->has_specification)
17560 {
17561 struct partial_die_info *spec_die;
72bf9492 17562
36586728
TT
17563 spec_die = find_partial_die (part_die->spec_offset,
17564 part_die->spec_is_dwz, cu);
72bf9492 17565
10b3939b 17566 fixup_partial_die (spec_die, cu);
72bf9492
DJ
17567
17568 if (spec_die->name)
17569 {
17570 part_die->name = spec_die->name;
17571
17572 /* Copy DW_AT_external attribute if it is set. */
17573 if (spec_die->is_external)
17574 part_die->is_external = spec_die->is_external;
17575 }
17576 }
17577
17578 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
17579
17580 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 17581 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 17582
abc72ce4
DE
17583 /* If there is no parent die to provide a namespace, and there are
17584 children, see if we can determine the namespace from their linkage
122d1940 17585 name. */
abc72ce4 17586 if (cu->language == language_cplus
8b70b953 17587 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
17588 && part_die->die_parent == NULL
17589 && part_die->has_children
17590 && (part_die->tag == DW_TAG_class_type
17591 || part_die->tag == DW_TAG_structure_type
17592 || part_die->tag == DW_TAG_union_type))
17593 guess_partial_die_structure_name (part_die, cu);
17594
53832f31
TT
17595 /* GCC might emit a nameless struct or union that has a linkage
17596 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17597 if (part_die->name == NULL
96408a79
SA
17598 && (part_die->tag == DW_TAG_class_type
17599 || part_die->tag == DW_TAG_interface_type
17600 || part_die->tag == DW_TAG_structure_type
17601 || part_die->tag == DW_TAG_union_type)
53832f31
TT
17602 && part_die->linkage_name != NULL)
17603 {
17604 char *demangled;
17605
8de20a37 17606 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
17607 if (demangled)
17608 {
96408a79
SA
17609 const char *base;
17610
17611 /* Strip any leading namespaces/classes, keep only the base name.
17612 DW_AT_name for named DIEs does not contain the prefixes. */
17613 base = strrchr (demangled, ':');
17614 if (base && base > demangled && base[-1] == ':')
17615 base++;
17616 else
17617 base = demangled;
17618
34a68019 17619 part_die->name
224c3ddb
SM
17620 = ((const char *)
17621 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
17622 base, strlen (base)));
53832f31
TT
17623 xfree (demangled);
17624 }
17625 }
17626
abc72ce4 17627 part_die->fixup_called = 1;
72bf9492
DJ
17628}
17629
a8329558 17630/* Read an attribute value described by an attribute form. */
c906108c 17631
d521ce57 17632static const gdb_byte *
dee91e82
DE
17633read_attribute_value (const struct die_reader_specs *reader,
17634 struct attribute *attr, unsigned form,
43988095 17635 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 17636{
dee91e82 17637 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
17638 struct objfile *objfile = cu->objfile;
17639 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 17640 bfd *abfd = reader->abfd;
e7c27a73 17641 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
17642 unsigned int bytes_read;
17643 struct dwarf_block *blk;
17644
aead7601 17645 attr->form = (enum dwarf_form) form;
a8329558 17646 switch (form)
c906108c 17647 {
c906108c 17648 case DW_FORM_ref_addr:
ae411497 17649 if (cu->header.version == 2)
4568ecf9 17650 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 17651 else
4568ecf9
DE
17652 DW_UNSND (attr) = read_offset (abfd, info_ptr,
17653 &cu->header, &bytes_read);
ae411497
TT
17654 info_ptr += bytes_read;
17655 break;
36586728
TT
17656 case DW_FORM_GNU_ref_alt:
17657 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
17658 info_ptr += bytes_read;
17659 break;
ae411497 17660 case DW_FORM_addr:
e7c27a73 17661 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 17662 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 17663 info_ptr += bytes_read;
c906108c
SS
17664 break;
17665 case DW_FORM_block2:
7b5a2f43 17666 blk = dwarf_alloc_block (cu);
c906108c
SS
17667 blk->size = read_2_bytes (abfd, info_ptr);
17668 info_ptr += 2;
17669 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17670 info_ptr += blk->size;
17671 DW_BLOCK (attr) = blk;
17672 break;
17673 case DW_FORM_block4:
7b5a2f43 17674 blk = dwarf_alloc_block (cu);
c906108c
SS
17675 blk->size = read_4_bytes (abfd, info_ptr);
17676 info_ptr += 4;
17677 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17678 info_ptr += blk->size;
17679 DW_BLOCK (attr) = blk;
17680 break;
17681 case DW_FORM_data2:
17682 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
17683 info_ptr += 2;
17684 break;
17685 case DW_FORM_data4:
17686 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
17687 info_ptr += 4;
17688 break;
17689 case DW_FORM_data8:
17690 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
17691 info_ptr += 8;
17692 break;
0224619f
JK
17693 case DW_FORM_data16:
17694 blk = dwarf_alloc_block (cu);
17695 blk->size = 16;
17696 blk->data = read_n_bytes (abfd, info_ptr, 16);
17697 info_ptr += 16;
17698 DW_BLOCK (attr) = blk;
17699 break;
2dc7f7b3
TT
17700 case DW_FORM_sec_offset:
17701 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
17702 info_ptr += bytes_read;
17703 break;
c906108c 17704 case DW_FORM_string:
9b1c24c8 17705 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 17706 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
17707 info_ptr += bytes_read;
17708 break;
4bdf3d34 17709 case DW_FORM_strp:
36586728
TT
17710 if (!cu->per_cu->is_dwz)
17711 {
17712 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
17713 &bytes_read);
17714 DW_STRING_IS_CANONICAL (attr) = 0;
17715 info_ptr += bytes_read;
17716 break;
17717 }
17718 /* FALLTHROUGH */
43988095
JK
17719 case DW_FORM_line_strp:
17720 if (!cu->per_cu->is_dwz)
17721 {
17722 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
17723 cu_header, &bytes_read);
17724 DW_STRING_IS_CANONICAL (attr) = 0;
17725 info_ptr += bytes_read;
17726 break;
17727 }
17728 /* FALLTHROUGH */
36586728
TT
17729 case DW_FORM_GNU_strp_alt:
17730 {
17731 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17732 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
17733 &bytes_read);
17734
17735 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
17736 DW_STRING_IS_CANONICAL (attr) = 0;
17737 info_ptr += bytes_read;
17738 }
4bdf3d34 17739 break;
2dc7f7b3 17740 case DW_FORM_exprloc:
c906108c 17741 case DW_FORM_block:
7b5a2f43 17742 blk = dwarf_alloc_block (cu);
c906108c
SS
17743 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17744 info_ptr += bytes_read;
17745 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17746 info_ptr += blk->size;
17747 DW_BLOCK (attr) = blk;
17748 break;
17749 case DW_FORM_block1:
7b5a2f43 17750 blk = dwarf_alloc_block (cu);
c906108c
SS
17751 blk->size = read_1_byte (abfd, info_ptr);
17752 info_ptr += 1;
17753 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17754 info_ptr += blk->size;
17755 DW_BLOCK (attr) = blk;
17756 break;
17757 case DW_FORM_data1:
17758 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
17759 info_ptr += 1;
17760 break;
17761 case DW_FORM_flag:
17762 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
17763 info_ptr += 1;
17764 break;
2dc7f7b3
TT
17765 case DW_FORM_flag_present:
17766 DW_UNSND (attr) = 1;
17767 break;
c906108c
SS
17768 case DW_FORM_sdata:
17769 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17770 info_ptr += bytes_read;
17771 break;
17772 case DW_FORM_udata:
17773 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17774 info_ptr += bytes_read;
17775 break;
17776 case DW_FORM_ref1:
9c541725 17777 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17778 + read_1_byte (abfd, info_ptr));
c906108c
SS
17779 info_ptr += 1;
17780 break;
17781 case DW_FORM_ref2:
9c541725 17782 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17783 + read_2_bytes (abfd, info_ptr));
c906108c
SS
17784 info_ptr += 2;
17785 break;
17786 case DW_FORM_ref4:
9c541725 17787 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17788 + read_4_bytes (abfd, info_ptr));
c906108c
SS
17789 info_ptr += 4;
17790 break;
613e1657 17791 case DW_FORM_ref8:
9c541725 17792 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17793 + read_8_bytes (abfd, info_ptr));
613e1657
KB
17794 info_ptr += 8;
17795 break;
55f1336d 17796 case DW_FORM_ref_sig8:
ac9ec31b 17797 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
17798 info_ptr += 8;
17799 break;
c906108c 17800 case DW_FORM_ref_udata:
9c541725 17801 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17802 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
17803 info_ptr += bytes_read;
17804 break;
c906108c 17805 case DW_FORM_indirect:
a8329558
KW
17806 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17807 info_ptr += bytes_read;
43988095
JK
17808 if (form == DW_FORM_implicit_const)
17809 {
17810 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17811 info_ptr += bytes_read;
17812 }
17813 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
17814 info_ptr);
17815 break;
17816 case DW_FORM_implicit_const:
17817 DW_SND (attr) = implicit_const;
a8329558 17818 break;
3019eac3
DE
17819 case DW_FORM_GNU_addr_index:
17820 if (reader->dwo_file == NULL)
17821 {
17822 /* For now flag a hard error.
17823 Later we can turn this into a complaint. */
17824 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
17825 dwarf_form_name (form),
17826 bfd_get_filename (abfd));
17827 }
17828 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
17829 info_ptr += bytes_read;
17830 break;
17831 case DW_FORM_GNU_str_index:
17832 if (reader->dwo_file == NULL)
17833 {
17834 /* For now flag a hard error.
17835 Later we can turn this into a complaint if warranted. */
17836 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
17837 dwarf_form_name (form),
17838 bfd_get_filename (abfd));
17839 }
17840 {
17841 ULONGEST str_index =
17842 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17843
342587c4 17844 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
17845 DW_STRING_IS_CANONICAL (attr) = 0;
17846 info_ptr += bytes_read;
17847 }
17848 break;
c906108c 17849 default:
8a3fe4f8 17850 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
17851 dwarf_form_name (form),
17852 bfd_get_filename (abfd));
c906108c 17853 }
28e94949 17854
36586728 17855 /* Super hack. */
7771576e 17856 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
17857 attr->form = DW_FORM_GNU_ref_alt;
17858
28e94949
JB
17859 /* We have seen instances where the compiler tried to emit a byte
17860 size attribute of -1 which ended up being encoded as an unsigned
17861 0xffffffff. Although 0xffffffff is technically a valid size value,
17862 an object of this size seems pretty unlikely so we can relatively
17863 safely treat these cases as if the size attribute was invalid and
17864 treat them as zero by default. */
17865 if (attr->name == DW_AT_byte_size
17866 && form == DW_FORM_data4
17867 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
17868 {
17869 complaint
17870 (&symfile_complaints,
43bbcdc2
PH
17871 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
17872 hex_string (DW_UNSND (attr)));
01c66ae6
JB
17873 DW_UNSND (attr) = 0;
17874 }
28e94949 17875
c906108c
SS
17876 return info_ptr;
17877}
17878
a8329558
KW
17879/* Read an attribute described by an abbreviated attribute. */
17880
d521ce57 17881static const gdb_byte *
dee91e82
DE
17882read_attribute (const struct die_reader_specs *reader,
17883 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 17884 const gdb_byte *info_ptr)
a8329558
KW
17885{
17886 attr->name = abbrev->name;
43988095
JK
17887 return read_attribute_value (reader, attr, abbrev->form,
17888 abbrev->implicit_const, info_ptr);
a8329558
KW
17889}
17890
0963b4bd 17891/* Read dwarf information from a buffer. */
c906108c
SS
17892
17893static unsigned int
a1855c1d 17894read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 17895{
fe1b8b76 17896 return bfd_get_8 (abfd, buf);
c906108c
SS
17897}
17898
17899static int
a1855c1d 17900read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 17901{
fe1b8b76 17902 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
17903}
17904
17905static unsigned int
a1855c1d 17906read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17907{
fe1b8b76 17908 return bfd_get_16 (abfd, buf);
c906108c
SS
17909}
17910
21ae7a4d 17911static int
a1855c1d 17912read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
17913{
17914 return bfd_get_signed_16 (abfd, buf);
17915}
17916
c906108c 17917static unsigned int
a1855c1d 17918read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17919{
fe1b8b76 17920 return bfd_get_32 (abfd, buf);
c906108c
SS
17921}
17922
21ae7a4d 17923static int
a1855c1d 17924read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
17925{
17926 return bfd_get_signed_32 (abfd, buf);
17927}
17928
93311388 17929static ULONGEST
a1855c1d 17930read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17931{
fe1b8b76 17932 return bfd_get_64 (abfd, buf);
c906108c
SS
17933}
17934
17935static CORE_ADDR
d521ce57 17936read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 17937 unsigned int *bytes_read)
c906108c 17938{
e7c27a73 17939 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
17940 CORE_ADDR retval = 0;
17941
107d2387 17942 if (cu_header->signed_addr_p)
c906108c 17943 {
107d2387
AC
17944 switch (cu_header->addr_size)
17945 {
17946 case 2:
fe1b8b76 17947 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
17948 break;
17949 case 4:
fe1b8b76 17950 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
17951 break;
17952 case 8:
fe1b8b76 17953 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
17954 break;
17955 default:
8e65ff28 17956 internal_error (__FILE__, __LINE__,
e2e0b3e5 17957 _("read_address: bad switch, signed [in module %s]"),
659b0389 17958 bfd_get_filename (abfd));
107d2387
AC
17959 }
17960 }
17961 else
17962 {
17963 switch (cu_header->addr_size)
17964 {
17965 case 2:
fe1b8b76 17966 retval = bfd_get_16 (abfd, buf);
107d2387
AC
17967 break;
17968 case 4:
fe1b8b76 17969 retval = bfd_get_32 (abfd, buf);
107d2387
AC
17970 break;
17971 case 8:
fe1b8b76 17972 retval = bfd_get_64 (abfd, buf);
107d2387
AC
17973 break;
17974 default:
8e65ff28 17975 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
17976 _("read_address: bad switch, "
17977 "unsigned [in module %s]"),
659b0389 17978 bfd_get_filename (abfd));
107d2387 17979 }
c906108c 17980 }
64367e0a 17981
107d2387
AC
17982 *bytes_read = cu_header->addr_size;
17983 return retval;
c906108c
SS
17984}
17985
f7ef9339 17986/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
17987 specification allows the initial length to take up either 4 bytes
17988 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
17989 bytes describe the length and all offsets will be 8 bytes in length
17990 instead of 4.
17991
f7ef9339
KB
17992 An older, non-standard 64-bit format is also handled by this
17993 function. The older format in question stores the initial length
17994 as an 8-byte quantity without an escape value. Lengths greater
17995 than 2^32 aren't very common which means that the initial 4 bytes
17996 is almost always zero. Since a length value of zero doesn't make
17997 sense for the 32-bit format, this initial zero can be considered to
17998 be an escape value which indicates the presence of the older 64-bit
17999 format. As written, the code can't detect (old format) lengths
917c78fc
MK
18000 greater than 4GB. If it becomes necessary to handle lengths
18001 somewhat larger than 4GB, we could allow other small values (such
18002 as the non-sensical values of 1, 2, and 3) to also be used as
18003 escape values indicating the presence of the old format.
f7ef9339 18004
917c78fc
MK
18005 The value returned via bytes_read should be used to increment the
18006 relevant pointer after calling read_initial_length().
c764a876 18007
613e1657
KB
18008 [ Note: read_initial_length() and read_offset() are based on the
18009 document entitled "DWARF Debugging Information Format", revision
f7ef9339 18010 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
18011 from:
18012
f7ef9339 18013 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 18014
613e1657
KB
18015 This document is only a draft and is subject to change. (So beware.)
18016
f7ef9339 18017 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
18018 determined empirically by examining 64-bit ELF files produced by
18019 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
18020
18021 - Kevin, July 16, 2002
613e1657
KB
18022 ] */
18023
18024static LONGEST
d521ce57 18025read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 18026{
fe1b8b76 18027 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 18028
dd373385 18029 if (length == 0xffffffff)
613e1657 18030 {
fe1b8b76 18031 length = bfd_get_64 (abfd, buf + 4);
613e1657 18032 *bytes_read = 12;
613e1657 18033 }
dd373385 18034 else if (length == 0)
f7ef9339 18035 {
dd373385 18036 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 18037 length = bfd_get_64 (abfd, buf);
f7ef9339 18038 *bytes_read = 8;
f7ef9339 18039 }
613e1657
KB
18040 else
18041 {
18042 *bytes_read = 4;
613e1657
KB
18043 }
18044
c764a876
DE
18045 return length;
18046}
dd373385 18047
c764a876
DE
18048/* Cover function for read_initial_length.
18049 Returns the length of the object at BUF, and stores the size of the
18050 initial length in *BYTES_READ and stores the size that offsets will be in
18051 *OFFSET_SIZE.
18052 If the initial length size is not equivalent to that specified in
18053 CU_HEADER then issue a complaint.
18054 This is useful when reading non-comp-unit headers. */
dd373385 18055
c764a876 18056static LONGEST
d521ce57 18057read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
18058 const struct comp_unit_head *cu_header,
18059 unsigned int *bytes_read,
18060 unsigned int *offset_size)
18061{
18062 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18063
18064 gdb_assert (cu_header->initial_length_size == 4
18065 || cu_header->initial_length_size == 8
18066 || cu_header->initial_length_size == 12);
18067
18068 if (cu_header->initial_length_size != *bytes_read)
18069 complaint (&symfile_complaints,
18070 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 18071
c764a876 18072 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 18073 return length;
613e1657
KB
18074}
18075
18076/* Read an offset from the data stream. The size of the offset is
917c78fc 18077 given by cu_header->offset_size. */
613e1657
KB
18078
18079static LONGEST
d521ce57
TT
18080read_offset (bfd *abfd, const gdb_byte *buf,
18081 const struct comp_unit_head *cu_header,
891d2f0b 18082 unsigned int *bytes_read)
c764a876
DE
18083{
18084 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 18085
c764a876
DE
18086 *bytes_read = cu_header->offset_size;
18087 return offset;
18088}
18089
18090/* Read an offset from the data stream. */
18091
18092static LONGEST
d521ce57 18093read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
18094{
18095 LONGEST retval = 0;
18096
c764a876 18097 switch (offset_size)
613e1657
KB
18098 {
18099 case 4:
fe1b8b76 18100 retval = bfd_get_32 (abfd, buf);
613e1657
KB
18101 break;
18102 case 8:
fe1b8b76 18103 retval = bfd_get_64 (abfd, buf);
613e1657
KB
18104 break;
18105 default:
8e65ff28 18106 internal_error (__FILE__, __LINE__,
c764a876 18107 _("read_offset_1: bad switch [in module %s]"),
659b0389 18108 bfd_get_filename (abfd));
613e1657
KB
18109 }
18110
917c78fc 18111 return retval;
613e1657
KB
18112}
18113
d521ce57
TT
18114static const gdb_byte *
18115read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
18116{
18117 /* If the size of a host char is 8 bits, we can return a pointer
18118 to the buffer, otherwise we have to copy the data to a buffer
18119 allocated on the temporary obstack. */
4bdf3d34 18120 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 18121 return buf;
c906108c
SS
18122}
18123
d521ce57
TT
18124static const char *
18125read_direct_string (bfd *abfd, const gdb_byte *buf,
18126 unsigned int *bytes_read_ptr)
c906108c
SS
18127{
18128 /* If the size of a host char is 8 bits, we can return a pointer
18129 to the string, otherwise we have to copy the string to a buffer
18130 allocated on the temporary obstack. */
4bdf3d34 18131 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
18132 if (*buf == '\0')
18133 {
18134 *bytes_read_ptr = 1;
18135 return NULL;
18136 }
d521ce57
TT
18137 *bytes_read_ptr = strlen ((const char *) buf) + 1;
18138 return (const char *) buf;
4bdf3d34
JJ
18139}
18140
43988095
JK
18141/* Return pointer to string at section SECT offset STR_OFFSET with error
18142 reporting strings FORM_NAME and SECT_NAME. */
18143
d521ce57 18144static const char *
43988095
JK
18145read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
18146 struct dwarf2_section_info *sect,
18147 const char *form_name,
18148 const char *sect_name)
18149{
18150 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
18151 if (sect->buffer == NULL)
18152 error (_("%s used without %s section [in module %s]"),
18153 form_name, sect_name, bfd_get_filename (abfd));
18154 if (str_offset >= sect->size)
18155 error (_("%s pointing outside of %s section [in module %s]"),
18156 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 18157 gdb_assert (HOST_CHAR_BIT == 8);
43988095 18158 if (sect->buffer[str_offset] == '\0')
4bdf3d34 18159 return NULL;
43988095
JK
18160 return (const char *) (sect->buffer + str_offset);
18161}
18162
18163/* Return pointer to string at .debug_str offset STR_OFFSET. */
18164
18165static const char *
18166read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
18167{
18168 return read_indirect_string_at_offset_from (abfd, str_offset,
18169 &dwarf2_per_objfile->str,
18170 "DW_FORM_strp", ".debug_str");
18171}
18172
18173/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18174
18175static const char *
18176read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
18177{
18178 return read_indirect_string_at_offset_from (abfd, str_offset,
18179 &dwarf2_per_objfile->line_str,
18180 "DW_FORM_line_strp",
18181 ".debug_line_str");
c906108c
SS
18182}
18183
36586728
TT
18184/* Read a string at offset STR_OFFSET in the .debug_str section from
18185 the .dwz file DWZ. Throw an error if the offset is too large. If
18186 the string consists of a single NUL byte, return NULL; otherwise
18187 return a pointer to the string. */
18188
d521ce57 18189static const char *
36586728
TT
18190read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
18191{
18192 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
18193
18194 if (dwz->str.buffer == NULL)
18195 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18196 "section [in module %s]"),
18197 bfd_get_filename (dwz->dwz_bfd));
18198 if (str_offset >= dwz->str.size)
18199 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18200 ".debug_str section [in module %s]"),
18201 bfd_get_filename (dwz->dwz_bfd));
18202 gdb_assert (HOST_CHAR_BIT == 8);
18203 if (dwz->str.buffer[str_offset] == '\0')
18204 return NULL;
d521ce57 18205 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
18206}
18207
43988095
JK
18208/* Return pointer to string at .debug_str offset as read from BUF.
18209 BUF is assumed to be in a compilation unit described by CU_HEADER.
18210 Return *BYTES_READ_PTR count of bytes read from BUF. */
18211
d521ce57
TT
18212static const char *
18213read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
18214 const struct comp_unit_head *cu_header,
18215 unsigned int *bytes_read_ptr)
18216{
18217 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
18218
18219 return read_indirect_string_at_offset (abfd, str_offset);
18220}
18221
43988095
JK
18222/* Return pointer to string at .debug_line_str offset as read from BUF.
18223 BUF is assumed to be in a compilation unit described by CU_HEADER.
18224 Return *BYTES_READ_PTR count of bytes read from BUF. */
18225
18226static const char *
18227read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
18228 const struct comp_unit_head *cu_header,
18229 unsigned int *bytes_read_ptr)
18230{
18231 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
18232
18233 return read_indirect_line_string_at_offset (abfd, str_offset);
18234}
18235
18236ULONGEST
d521ce57 18237read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 18238 unsigned int *bytes_read_ptr)
c906108c 18239{
12df843f 18240 ULONGEST result;
ce5d95e1 18241 unsigned int num_read;
870f88f7 18242 int shift;
c906108c
SS
18243 unsigned char byte;
18244
18245 result = 0;
18246 shift = 0;
18247 num_read = 0;
c906108c
SS
18248 while (1)
18249 {
fe1b8b76 18250 byte = bfd_get_8 (abfd, buf);
c906108c
SS
18251 buf++;
18252 num_read++;
12df843f 18253 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
18254 if ((byte & 128) == 0)
18255 {
18256 break;
18257 }
18258 shift += 7;
18259 }
18260 *bytes_read_ptr = num_read;
18261 return result;
18262}
18263
12df843f 18264static LONGEST
d521ce57
TT
18265read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
18266 unsigned int *bytes_read_ptr)
c906108c 18267{
12df843f 18268 LONGEST result;
870f88f7 18269 int shift, num_read;
c906108c
SS
18270 unsigned char byte;
18271
18272 result = 0;
18273 shift = 0;
c906108c 18274 num_read = 0;
c906108c
SS
18275 while (1)
18276 {
fe1b8b76 18277 byte = bfd_get_8 (abfd, buf);
c906108c
SS
18278 buf++;
18279 num_read++;
12df843f 18280 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
18281 shift += 7;
18282 if ((byte & 128) == 0)
18283 {
18284 break;
18285 }
18286 }
77e0b926 18287 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 18288 result |= -(((LONGEST) 1) << shift);
c906108c
SS
18289 *bytes_read_ptr = num_read;
18290 return result;
18291}
18292
3019eac3
DE
18293/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18294 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
18295 ADDR_SIZE is the size of addresses from the CU header. */
18296
18297static CORE_ADDR
18298read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
18299{
18300 struct objfile *objfile = dwarf2_per_objfile->objfile;
18301 bfd *abfd = objfile->obfd;
18302 const gdb_byte *info_ptr;
18303
18304 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
18305 if (dwarf2_per_objfile->addr.buffer == NULL)
18306 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 18307 objfile_name (objfile));
3019eac3
DE
18308 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
18309 error (_("DW_FORM_addr_index pointing outside of "
18310 ".debug_addr section [in module %s]"),
4262abfb 18311 objfile_name (objfile));
3019eac3
DE
18312 info_ptr = (dwarf2_per_objfile->addr.buffer
18313 + addr_base + addr_index * addr_size);
18314 if (addr_size == 4)
18315 return bfd_get_32 (abfd, info_ptr);
18316 else
18317 return bfd_get_64 (abfd, info_ptr);
18318}
18319
18320/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18321
18322static CORE_ADDR
18323read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18324{
18325 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
18326}
18327
18328/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18329
18330static CORE_ADDR
d521ce57 18331read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
18332 unsigned int *bytes_read)
18333{
18334 bfd *abfd = cu->objfile->obfd;
18335 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18336
18337 return read_addr_index (cu, addr_index);
18338}
18339
18340/* Data structure to pass results from dwarf2_read_addr_index_reader
18341 back to dwarf2_read_addr_index. */
18342
18343struct dwarf2_read_addr_index_data
18344{
18345 ULONGEST addr_base;
18346 int addr_size;
18347};
18348
18349/* die_reader_func for dwarf2_read_addr_index. */
18350
18351static void
18352dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 18353 const gdb_byte *info_ptr,
3019eac3
DE
18354 struct die_info *comp_unit_die,
18355 int has_children,
18356 void *data)
18357{
18358 struct dwarf2_cu *cu = reader->cu;
18359 struct dwarf2_read_addr_index_data *aidata =
18360 (struct dwarf2_read_addr_index_data *) data;
18361
18362 aidata->addr_base = cu->addr_base;
18363 aidata->addr_size = cu->header.addr_size;
18364}
18365
18366/* Given an index in .debug_addr, fetch the value.
18367 NOTE: This can be called during dwarf expression evaluation,
18368 long after the debug information has been read, and thus per_cu->cu
18369 may no longer exist. */
18370
18371CORE_ADDR
18372dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
18373 unsigned int addr_index)
18374{
18375 struct objfile *objfile = per_cu->objfile;
18376 struct dwarf2_cu *cu = per_cu->cu;
18377 ULONGEST addr_base;
18378 int addr_size;
18379
18380 /* This is intended to be called from outside this file. */
18381 dw2_setup (objfile);
18382
18383 /* We need addr_base and addr_size.
18384 If we don't have PER_CU->cu, we have to get it.
18385 Nasty, but the alternative is storing the needed info in PER_CU,
18386 which at this point doesn't seem justified: it's not clear how frequently
18387 it would get used and it would increase the size of every PER_CU.
18388 Entry points like dwarf2_per_cu_addr_size do a similar thing
18389 so we're not in uncharted territory here.
18390 Alas we need to be a bit more complicated as addr_base is contained
18391 in the DIE.
18392
18393 We don't need to read the entire CU(/TU).
18394 We just need the header and top level die.
a1b64ce1 18395
3019eac3 18396 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 18397 For now we skip this optimization. */
3019eac3
DE
18398
18399 if (cu != NULL)
18400 {
18401 addr_base = cu->addr_base;
18402 addr_size = cu->header.addr_size;
18403 }
18404 else
18405 {
18406 struct dwarf2_read_addr_index_data aidata;
18407
a1b64ce1
DE
18408 /* Note: We can't use init_cutu_and_read_dies_simple here,
18409 we need addr_base. */
18410 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
18411 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
18412 addr_base = aidata.addr_base;
18413 addr_size = aidata.addr_size;
18414 }
18415
18416 return read_addr_index_1 (addr_index, addr_base, addr_size);
18417}
18418
57d63ce2
DE
18419/* Given a DW_FORM_GNU_str_index, fetch the string.
18420 This is only used by the Fission support. */
3019eac3 18421
d521ce57 18422static const char *
342587c4 18423read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
18424{
18425 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 18426 const char *objf_name = objfile_name (objfile);
3019eac3 18427 bfd *abfd = objfile->obfd;
342587c4 18428 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
18429 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
18430 struct dwarf2_section_info *str_offsets_section =
18431 &reader->dwo_file->sections.str_offsets;
d521ce57 18432 const gdb_byte *info_ptr;
3019eac3 18433 ULONGEST str_offset;
57d63ce2 18434 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 18435
73869dc2
DE
18436 dwarf2_read_section (objfile, str_section);
18437 dwarf2_read_section (objfile, str_offsets_section);
18438 if (str_section->buffer == NULL)
57d63ce2 18439 error (_("%s used without .debug_str.dwo section"
9c541725
PA
18440 " in CU at offset 0x%x [in module %s]"),
18441 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 18442 if (str_offsets_section->buffer == NULL)
57d63ce2 18443 error (_("%s used without .debug_str_offsets.dwo section"
9c541725
PA
18444 " in CU at offset 0x%x [in module %s]"),
18445 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 18446 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 18447 error (_("%s pointing outside of .debug_str_offsets.dwo"
9c541725
PA
18448 " section in CU at offset 0x%x [in module %s]"),
18449 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 18450 info_ptr = (str_offsets_section->buffer
3019eac3
DE
18451 + str_index * cu->header.offset_size);
18452 if (cu->header.offset_size == 4)
18453 str_offset = bfd_get_32 (abfd, info_ptr);
18454 else
18455 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 18456 if (str_offset >= str_section->size)
57d63ce2 18457 error (_("Offset from %s pointing outside of"
9c541725
PA
18458 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
18459 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 18460 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
18461}
18462
3019eac3
DE
18463/* Return the length of an LEB128 number in BUF. */
18464
18465static int
18466leb128_size (const gdb_byte *buf)
18467{
18468 const gdb_byte *begin = buf;
18469 gdb_byte byte;
18470
18471 while (1)
18472 {
18473 byte = *buf++;
18474 if ((byte & 128) == 0)
18475 return buf - begin;
18476 }
18477}
18478
c906108c 18479static void
e142c38c 18480set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
18481{
18482 switch (lang)
18483 {
18484 case DW_LANG_C89:
76bee0cc 18485 case DW_LANG_C99:
0cfd832f 18486 case DW_LANG_C11:
c906108c 18487 case DW_LANG_C:
d1be3247 18488 case DW_LANG_UPC:
e142c38c 18489 cu->language = language_c;
c906108c 18490 break;
9c37b5ae 18491 case DW_LANG_Java:
c906108c 18492 case DW_LANG_C_plus_plus:
0cfd832f
MW
18493 case DW_LANG_C_plus_plus_11:
18494 case DW_LANG_C_plus_plus_14:
e142c38c 18495 cu->language = language_cplus;
c906108c 18496 break;
6aecb9c2
JB
18497 case DW_LANG_D:
18498 cu->language = language_d;
18499 break;
c906108c
SS
18500 case DW_LANG_Fortran77:
18501 case DW_LANG_Fortran90:
b21b22e0 18502 case DW_LANG_Fortran95:
f7de9aab
MW
18503 case DW_LANG_Fortran03:
18504 case DW_LANG_Fortran08:
e142c38c 18505 cu->language = language_fortran;
c906108c 18506 break;
a766d390
DE
18507 case DW_LANG_Go:
18508 cu->language = language_go;
18509 break;
c906108c 18510 case DW_LANG_Mips_Assembler:
e142c38c 18511 cu->language = language_asm;
c906108c
SS
18512 break;
18513 case DW_LANG_Ada83:
8aaf0b47 18514 case DW_LANG_Ada95:
bc5f45f8
JB
18515 cu->language = language_ada;
18516 break;
72019c9c
GM
18517 case DW_LANG_Modula2:
18518 cu->language = language_m2;
18519 break;
fe8e67fd
PM
18520 case DW_LANG_Pascal83:
18521 cu->language = language_pascal;
18522 break;
22566fbd
DJ
18523 case DW_LANG_ObjC:
18524 cu->language = language_objc;
18525 break;
c44af4eb
TT
18526 case DW_LANG_Rust:
18527 case DW_LANG_Rust_old:
18528 cu->language = language_rust;
18529 break;
c906108c
SS
18530 case DW_LANG_Cobol74:
18531 case DW_LANG_Cobol85:
c906108c 18532 default:
e142c38c 18533 cu->language = language_minimal;
c906108c
SS
18534 break;
18535 }
e142c38c 18536 cu->language_defn = language_def (cu->language);
c906108c
SS
18537}
18538
18539/* Return the named attribute or NULL if not there. */
18540
18541static struct attribute *
e142c38c 18542dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 18543{
a48e046c 18544 for (;;)
c906108c 18545 {
a48e046c
TT
18546 unsigned int i;
18547 struct attribute *spec = NULL;
18548
18549 for (i = 0; i < die->num_attrs; ++i)
18550 {
18551 if (die->attrs[i].name == name)
18552 return &die->attrs[i];
18553 if (die->attrs[i].name == DW_AT_specification
18554 || die->attrs[i].name == DW_AT_abstract_origin)
18555 spec = &die->attrs[i];
18556 }
18557
18558 if (!spec)
18559 break;
c906108c 18560
f2f0e013 18561 die = follow_die_ref (die, spec, &cu);
f2f0e013 18562 }
c5aa993b 18563
c906108c
SS
18564 return NULL;
18565}
18566
348e048f
DE
18567/* Return the named attribute or NULL if not there,
18568 but do not follow DW_AT_specification, etc.
18569 This is for use in contexts where we're reading .debug_types dies.
18570 Following DW_AT_specification, DW_AT_abstract_origin will take us
18571 back up the chain, and we want to go down. */
18572
18573static struct attribute *
45e58e77 18574dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
18575{
18576 unsigned int i;
18577
18578 for (i = 0; i < die->num_attrs; ++i)
18579 if (die->attrs[i].name == name)
18580 return &die->attrs[i];
18581
18582 return NULL;
18583}
18584
7d45c7c3
KB
18585/* Return the string associated with a string-typed attribute, or NULL if it
18586 is either not found or is of an incorrect type. */
18587
18588static const char *
18589dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18590{
18591 struct attribute *attr;
18592 const char *str = NULL;
18593
18594 attr = dwarf2_attr (die, name, cu);
18595
18596 if (attr != NULL)
18597 {
43988095 18598 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
18599 || attr->form == DW_FORM_string
18600 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 18601 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
18602 str = DW_STRING (attr);
18603 else
18604 complaint (&symfile_complaints,
18605 _("string type expected for attribute %s for "
18606 "DIE at 0x%x in module %s"),
9c541725 18607 dwarf_attr_name (name), to_underlying (die->sect_off),
7d45c7c3
KB
18608 objfile_name (cu->objfile));
18609 }
18610
18611 return str;
18612}
18613
05cf31d1
JB
18614/* Return non-zero iff the attribute NAME is defined for the given DIE,
18615 and holds a non-zero value. This function should only be used for
2dc7f7b3 18616 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
18617
18618static int
18619dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
18620{
18621 struct attribute *attr = dwarf2_attr (die, name, cu);
18622
18623 return (attr && DW_UNSND (attr));
18624}
18625
3ca72b44 18626static int
e142c38c 18627die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 18628{
05cf31d1
JB
18629 /* A DIE is a declaration if it has a DW_AT_declaration attribute
18630 which value is non-zero. However, we have to be careful with
18631 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
18632 (via dwarf2_flag_true_p) follows this attribute. So we may
18633 end up accidently finding a declaration attribute that belongs
18634 to a different DIE referenced by the specification attribute,
18635 even though the given DIE does not have a declaration attribute. */
18636 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
18637 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
18638}
18639
63d06c5c 18640/* Return the die giving the specification for DIE, if there is
f2f0e013 18641 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
18642 containing the return value on output. If there is no
18643 specification, but there is an abstract origin, that is
18644 returned. */
63d06c5c
DC
18645
18646static struct die_info *
f2f0e013 18647die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 18648{
f2f0e013
DJ
18649 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
18650 *spec_cu);
63d06c5c 18651
edb3359d
DJ
18652 if (spec_attr == NULL)
18653 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
18654
63d06c5c
DC
18655 if (spec_attr == NULL)
18656 return NULL;
18657 else
f2f0e013 18658 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 18659}
c906108c 18660
527f3840
JK
18661/* Stub for free_line_header to match void * callback types. */
18662
18663static void
18664free_line_header_voidp (void *arg)
18665{
9a3c8263 18666 struct line_header *lh = (struct line_header *) arg;
527f3840 18667
fff8551c 18668 delete lh;
527f3840
JK
18669}
18670
fff8551c
PA
18671void
18672line_header::add_include_dir (const char *include_dir)
c906108c 18673{
27e0867f 18674 if (dwarf_line_debug >= 2)
fff8551c
PA
18675 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
18676 include_dirs.size () + 1, include_dir);
27e0867f 18677
fff8551c 18678 include_dirs.push_back (include_dir);
debd256d 18679}
6e70227d 18680
fff8551c
PA
18681void
18682line_header::add_file_name (const char *name,
ecfb656c 18683 dir_index d_index,
fff8551c
PA
18684 unsigned int mod_time,
18685 unsigned int length)
debd256d 18686{
27e0867f
DE
18687 if (dwarf_line_debug >= 2)
18688 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 18689 (unsigned) file_names.size () + 1, name);
27e0867f 18690
ecfb656c 18691 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 18692}
6e70227d 18693
83769d0b 18694/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
18695
18696static struct dwarf2_section_info *
18697get_debug_line_section (struct dwarf2_cu *cu)
18698{
18699 struct dwarf2_section_info *section;
18700
18701 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
18702 DWO file. */
18703 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18704 section = &cu->dwo_unit->dwo_file->sections.line;
18705 else if (cu->per_cu->is_dwz)
18706 {
18707 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18708
18709 section = &dwz->line;
18710 }
18711 else
18712 section = &dwarf2_per_objfile->line;
18713
18714 return section;
18715}
18716
43988095
JK
18717/* Read directory or file name entry format, starting with byte of
18718 format count entries, ULEB128 pairs of entry formats, ULEB128 of
18719 entries count and the entries themselves in the described entry
18720 format. */
18721
18722static void
18723read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
18724 struct line_header *lh,
18725 const struct comp_unit_head *cu_header,
18726 void (*callback) (struct line_header *lh,
18727 const char *name,
ecfb656c 18728 dir_index d_index,
43988095
JK
18729 unsigned int mod_time,
18730 unsigned int length))
18731{
18732 gdb_byte format_count, formati;
18733 ULONGEST data_count, datai;
18734 const gdb_byte *buf = *bufp;
18735 const gdb_byte *format_header_data;
18736 int i;
18737 unsigned int bytes_read;
18738
18739 format_count = read_1_byte (abfd, buf);
18740 buf += 1;
18741 format_header_data = buf;
18742 for (formati = 0; formati < format_count; formati++)
18743 {
18744 read_unsigned_leb128 (abfd, buf, &bytes_read);
18745 buf += bytes_read;
18746 read_unsigned_leb128 (abfd, buf, &bytes_read);
18747 buf += bytes_read;
18748 }
18749
18750 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
18751 buf += bytes_read;
18752 for (datai = 0; datai < data_count; datai++)
18753 {
18754 const gdb_byte *format = format_header_data;
18755 struct file_entry fe;
18756
43988095
JK
18757 for (formati = 0; formati < format_count; formati++)
18758 {
ecfb656c 18759 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 18760 format += bytes_read;
43988095 18761
ecfb656c 18762 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 18763 format += bytes_read;
ecfb656c
PA
18764
18765 gdb::optional<const char *> string;
18766 gdb::optional<unsigned int> uint;
18767
43988095
JK
18768 switch (form)
18769 {
18770 case DW_FORM_string:
ecfb656c 18771 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
18772 buf += bytes_read;
18773 break;
18774
18775 case DW_FORM_line_strp:
ecfb656c
PA
18776 string.emplace (read_indirect_line_string (abfd, buf,
18777 cu_header,
18778 &bytes_read));
43988095
JK
18779 buf += bytes_read;
18780 break;
18781
18782 case DW_FORM_data1:
ecfb656c 18783 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
18784 buf += 1;
18785 break;
18786
18787 case DW_FORM_data2:
ecfb656c 18788 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
18789 buf += 2;
18790 break;
18791
18792 case DW_FORM_data4:
ecfb656c 18793 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
18794 buf += 4;
18795 break;
18796
18797 case DW_FORM_data8:
ecfb656c 18798 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
18799 buf += 8;
18800 break;
18801
18802 case DW_FORM_udata:
ecfb656c 18803 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
18804 buf += bytes_read;
18805 break;
18806
18807 case DW_FORM_block:
18808 /* It is valid only for DW_LNCT_timestamp which is ignored by
18809 current GDB. */
18810 break;
18811 }
ecfb656c
PA
18812
18813 switch (content_type)
18814 {
18815 case DW_LNCT_path:
18816 if (string.has_value ())
18817 fe.name = *string;
18818 break;
18819 case DW_LNCT_directory_index:
18820 if (uint.has_value ())
18821 fe.d_index = (dir_index) *uint;
18822 break;
18823 case DW_LNCT_timestamp:
18824 if (uint.has_value ())
18825 fe.mod_time = *uint;
18826 break;
18827 case DW_LNCT_size:
18828 if (uint.has_value ())
18829 fe.length = *uint;
18830 break;
18831 case DW_LNCT_MD5:
18832 break;
18833 default:
18834 complaint (&symfile_complaints,
18835 _("Unknown format content type %s"),
18836 pulongest (content_type));
18837 }
43988095
JK
18838 }
18839
ecfb656c 18840 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
18841 }
18842
18843 *bufp = buf;
18844}
18845
debd256d 18846/* Read the statement program header starting at OFFSET in
3019eac3 18847 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 18848 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
18849 Returns NULL if there is a problem reading the header, e.g., if it
18850 has a version we don't understand.
debd256d
JB
18851
18852 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
18853 the returned object point into the dwarf line section buffer,
18854 and must not be freed. */
ae2de4f8 18855
fff8551c 18856static line_header_up
9c541725 18857dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 18858{
d521ce57 18859 const gdb_byte *line_ptr;
c764a876 18860 unsigned int bytes_read, offset_size;
debd256d 18861 int i;
d521ce57 18862 const char *cur_dir, *cur_file;
3019eac3
DE
18863 struct dwarf2_section_info *section;
18864 bfd *abfd;
18865
36586728 18866 section = get_debug_line_section (cu);
3019eac3
DE
18867 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
18868 if (section->buffer == NULL)
debd256d 18869 {
3019eac3
DE
18870 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18871 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
18872 else
18873 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
18874 return 0;
18875 }
18876
fceca515
DE
18877 /* We can't do this until we know the section is non-empty.
18878 Only then do we know we have such a section. */
a32a8923 18879 abfd = get_section_bfd_owner (section);
fceca515 18880
a738430d
MK
18881 /* Make sure that at least there's room for the total_length field.
18882 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 18883 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 18884 {
4d3c2250 18885 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
18886 return 0;
18887 }
18888
fff8551c 18889 line_header_up lh (new line_header ());
debd256d 18890
9c541725 18891 lh->sect_off = sect_off;
527f3840
JK
18892 lh->offset_in_dwz = cu->per_cu->is_dwz;
18893
9c541725 18894 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 18895
a738430d 18896 /* Read in the header. */
6e70227d 18897 lh->total_length =
c764a876
DE
18898 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
18899 &bytes_read, &offset_size);
debd256d 18900 line_ptr += bytes_read;
3019eac3 18901 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 18902 {
4d3c2250 18903 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
18904 return 0;
18905 }
18906 lh->statement_program_end = line_ptr + lh->total_length;
18907 lh->version = read_2_bytes (abfd, line_ptr);
18908 line_ptr += 2;
43988095 18909 if (lh->version > 5)
cd366ee8
DE
18910 {
18911 /* This is a version we don't understand. The format could have
18912 changed in ways we don't handle properly so just punt. */
18913 complaint (&symfile_complaints,
18914 _("unsupported version in .debug_line section"));
18915 return NULL;
18916 }
43988095
JK
18917 if (lh->version >= 5)
18918 {
18919 gdb_byte segment_selector_size;
18920
18921 /* Skip address size. */
18922 read_1_byte (abfd, line_ptr);
18923 line_ptr += 1;
18924
18925 segment_selector_size = read_1_byte (abfd, line_ptr);
18926 line_ptr += 1;
18927 if (segment_selector_size != 0)
18928 {
18929 complaint (&symfile_complaints,
18930 _("unsupported segment selector size %u "
18931 "in .debug_line section"),
18932 segment_selector_size);
18933 return NULL;
18934 }
18935 }
c764a876
DE
18936 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
18937 line_ptr += offset_size;
debd256d
JB
18938 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
18939 line_ptr += 1;
2dc7f7b3
TT
18940 if (lh->version >= 4)
18941 {
18942 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
18943 line_ptr += 1;
18944 }
18945 else
18946 lh->maximum_ops_per_instruction = 1;
18947
18948 if (lh->maximum_ops_per_instruction == 0)
18949 {
18950 lh->maximum_ops_per_instruction = 1;
18951 complaint (&symfile_complaints,
3e43a32a
MS
18952 _("invalid maximum_ops_per_instruction "
18953 "in `.debug_line' section"));
2dc7f7b3
TT
18954 }
18955
debd256d
JB
18956 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
18957 line_ptr += 1;
18958 lh->line_base = read_1_signed_byte (abfd, line_ptr);
18959 line_ptr += 1;
18960 lh->line_range = read_1_byte (abfd, line_ptr);
18961 line_ptr += 1;
18962 lh->opcode_base = read_1_byte (abfd, line_ptr);
18963 line_ptr += 1;
fff8551c 18964 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
18965
18966 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
18967 for (i = 1; i < lh->opcode_base; ++i)
18968 {
18969 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
18970 line_ptr += 1;
18971 }
18972
43988095 18973 if (lh->version >= 5)
debd256d 18974 {
43988095 18975 /* Read directory table. */
fff8551c
PA
18976 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18977 [] (struct line_header *lh, const char *name,
ecfb656c 18978 dir_index d_index, unsigned int mod_time,
fff8551c
PA
18979 unsigned int length)
18980 {
18981 lh->add_include_dir (name);
18982 });
debd256d 18983
43988095 18984 /* Read file name table. */
fff8551c
PA
18985 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18986 [] (struct line_header *lh, const char *name,
ecfb656c 18987 dir_index d_index, unsigned int mod_time,
fff8551c
PA
18988 unsigned int length)
18989 {
ecfb656c 18990 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 18991 });
43988095
JK
18992 }
18993 else
debd256d 18994 {
43988095
JK
18995 /* Read directory table. */
18996 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18997 {
18998 line_ptr += bytes_read;
fff8551c 18999 lh->add_include_dir (cur_dir);
43988095 19000 }
debd256d
JB
19001 line_ptr += bytes_read;
19002
43988095
JK
19003 /* Read file name table. */
19004 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19005 {
ecfb656c
PA
19006 unsigned int mod_time, length;
19007 dir_index d_index;
43988095
JK
19008
19009 line_ptr += bytes_read;
ecfb656c 19010 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
19011 line_ptr += bytes_read;
19012 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19013 line_ptr += bytes_read;
19014 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19015 line_ptr += bytes_read;
19016
ecfb656c 19017 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
19018 }
19019 line_ptr += bytes_read;
debd256d 19020 }
6e70227d 19021 lh->statement_program_start = line_ptr;
debd256d 19022
3019eac3 19023 if (line_ptr > (section->buffer + section->size))
4d3c2250 19024 complaint (&symfile_complaints,
3e43a32a
MS
19025 _("line number info header doesn't "
19026 "fit in `.debug_line' section"));
debd256d 19027
debd256d
JB
19028 return lh;
19029}
c906108c 19030
c6da4cef
DE
19031/* Subroutine of dwarf_decode_lines to simplify it.
19032 Return the file name of the psymtab for included file FILE_INDEX
19033 in line header LH of PST.
19034 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19035 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
19036 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
19037
19038 The function creates dangling cleanup registration. */
c6da4cef 19039
d521ce57 19040static const char *
c6da4cef
DE
19041psymtab_include_file_name (const struct line_header *lh, int file_index,
19042 const struct partial_symtab *pst,
19043 const char *comp_dir)
19044{
8c43009f 19045 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
19046 const char *include_name = fe.name;
19047 const char *include_name_to_compare = include_name;
72b9f47f
TT
19048 const char *pst_filename;
19049 char *copied_name = NULL;
c6da4cef
DE
19050 int file_is_pst;
19051
8c43009f 19052 const char *dir_name = fe.include_dir (lh);
c6da4cef
DE
19053
19054 if (!IS_ABSOLUTE_PATH (include_name)
19055 && (dir_name != NULL || comp_dir != NULL))
19056 {
19057 /* Avoid creating a duplicate psymtab for PST.
19058 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19059 Before we do the comparison, however, we need to account
19060 for DIR_NAME and COMP_DIR.
19061 First prepend dir_name (if non-NULL). If we still don't
19062 have an absolute path prepend comp_dir (if non-NULL).
19063 However, the directory we record in the include-file's
19064 psymtab does not contain COMP_DIR (to match the
19065 corresponding symtab(s)).
19066
19067 Example:
19068
19069 bash$ cd /tmp
19070 bash$ gcc -g ./hello.c
19071 include_name = "hello.c"
19072 dir_name = "."
19073 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19074 DW_AT_name = "./hello.c"
19075
19076 */
c6da4cef
DE
19077
19078 if (dir_name != NULL)
19079 {
d521ce57
TT
19080 char *tem = concat (dir_name, SLASH_STRING,
19081 include_name, (char *)NULL);
19082
19083 make_cleanup (xfree, tem);
19084 include_name = tem;
c6da4cef 19085 include_name_to_compare = include_name;
c6da4cef
DE
19086 }
19087 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19088 {
d521ce57
TT
19089 char *tem = concat (comp_dir, SLASH_STRING,
19090 include_name, (char *)NULL);
19091
19092 make_cleanup (xfree, tem);
19093 include_name_to_compare = tem;
c6da4cef
DE
19094 }
19095 }
19096
19097 pst_filename = pst->filename;
19098 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19099 {
72b9f47f
TT
19100 copied_name = concat (pst->dirname, SLASH_STRING,
19101 pst_filename, (char *)NULL);
19102 pst_filename = copied_name;
c6da4cef
DE
19103 }
19104
1e3fad37 19105 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19106
72b9f47f
TT
19107 if (copied_name != NULL)
19108 xfree (copied_name);
c6da4cef
DE
19109
19110 if (file_is_pst)
19111 return NULL;
19112 return include_name;
19113}
19114
d9b3de22
DE
19115/* State machine to track the state of the line number program. */
19116
6f77053d 19117class lnp_state_machine
d9b3de22 19118{
6f77053d
PA
19119public:
19120 /* Initialize a machine state for the start of a line number
19121 program. */
19122 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
19123
8c43009f
PA
19124 file_entry *current_file ()
19125 {
19126 /* lh->file_names is 0-based, but the file name numbers in the
19127 statement program are 1-based. */
6f77053d
PA
19128 return m_line_header->file_name_at (m_file);
19129 }
19130
19131 /* Record the line in the state machine. END_SEQUENCE is true if
19132 we're processing the end of a sequence. */
19133 void record_line (bool end_sequence);
19134
19135 /* Check address and if invalid nop-out the rest of the lines in this
19136 sequence. */
19137 void check_line_address (struct dwarf2_cu *cu,
19138 const gdb_byte *line_ptr,
19139 CORE_ADDR lowpc, CORE_ADDR address);
19140
19141 void handle_set_discriminator (unsigned int discriminator)
19142 {
19143 m_discriminator = discriminator;
19144 m_line_has_non_zero_discriminator |= discriminator != 0;
19145 }
19146
19147 /* Handle DW_LNE_set_address. */
19148 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19149 {
19150 m_op_index = 0;
19151 address += baseaddr;
19152 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19153 }
19154
19155 /* Handle DW_LNS_advance_pc. */
19156 void handle_advance_pc (CORE_ADDR adjust);
19157
19158 /* Handle a special opcode. */
19159 void handle_special_opcode (unsigned char op_code);
19160
19161 /* Handle DW_LNS_advance_line. */
19162 void handle_advance_line (int line_delta)
19163 {
19164 advance_line (line_delta);
19165 }
19166
19167 /* Handle DW_LNS_set_file. */
19168 void handle_set_file (file_name_index file);
19169
19170 /* Handle DW_LNS_negate_stmt. */
19171 void handle_negate_stmt ()
19172 {
19173 m_is_stmt = !m_is_stmt;
19174 }
19175
19176 /* Handle DW_LNS_const_add_pc. */
19177 void handle_const_add_pc ();
19178
19179 /* Handle DW_LNS_fixed_advance_pc. */
19180 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19181 {
19182 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19183 m_op_index = 0;
19184 }
19185
19186 /* Handle DW_LNS_copy. */
19187 void handle_copy ()
19188 {
19189 record_line (false);
19190 m_discriminator = 0;
19191 }
19192
19193 /* Handle DW_LNE_end_sequence. */
19194 void handle_end_sequence ()
19195 {
19196 m_record_line_callback = ::record_line;
19197 }
19198
19199private:
19200 /* Advance the line by LINE_DELTA. */
19201 void advance_line (int line_delta)
19202 {
19203 m_line += line_delta;
19204
19205 if (line_delta != 0)
19206 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19207 }
19208
6f77053d
PA
19209 gdbarch *m_gdbarch;
19210
19211 /* True if we're recording lines.
19212 Otherwise we're building partial symtabs and are just interested in
19213 finding include files mentioned by the line number program. */
19214 bool m_record_lines_p;
19215
8c43009f 19216 /* The line number header. */
6f77053d 19217 line_header *m_line_header;
8c43009f 19218
6f77053d
PA
19219 /* These are part of the standard DWARF line number state machine,
19220 and initialized according to the DWARF spec. */
d9b3de22 19221
6f77053d 19222 unsigned char m_op_index = 0;
8c43009f 19223 /* The line table index (1-based) of the current file. */
6f77053d
PA
19224 file_name_index m_file = (file_name_index) 1;
19225 unsigned int m_line = 1;
19226
19227 /* These are initialized in the constructor. */
19228
19229 CORE_ADDR m_address;
19230 bool m_is_stmt;
19231 unsigned int m_discriminator;
d9b3de22
DE
19232
19233 /* Additional bits of state we need to track. */
19234
19235 /* The last file that we called dwarf2_start_subfile for.
19236 This is only used for TLLs. */
6f77053d 19237 unsigned int m_last_file = 0;
d9b3de22 19238 /* The last file a line number was recorded for. */
6f77053d 19239 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
19240
19241 /* The function to call to record a line. */
6f77053d 19242 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
19243
19244 /* The last line number that was recorded, used to coalesce
19245 consecutive entries for the same line. This can happen, for
19246 example, when discriminators are present. PR 17276. */
6f77053d
PA
19247 unsigned int m_last_line = 0;
19248 bool m_line_has_non_zero_discriminator = false;
8c43009f 19249};
d9b3de22 19250
6f77053d
PA
19251void
19252lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19253{
19254 CORE_ADDR addr_adj = (((m_op_index + adjust)
19255 / m_line_header->maximum_ops_per_instruction)
19256 * m_line_header->minimum_instruction_length);
19257 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19258 m_op_index = ((m_op_index + adjust)
19259 % m_line_header->maximum_ops_per_instruction);
19260}
d9b3de22 19261
6f77053d
PA
19262void
19263lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19264{
6f77053d
PA
19265 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19266 CORE_ADDR addr_adj = (((m_op_index
19267 + (adj_opcode / m_line_header->line_range))
19268 / m_line_header->maximum_ops_per_instruction)
19269 * m_line_header->minimum_instruction_length);
19270 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19271 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
19272 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19273
6f77053d
PA
19274 int line_delta = (m_line_header->line_base
19275 + (adj_opcode % m_line_header->line_range));
19276 advance_line (line_delta);
19277 record_line (false);
19278 m_discriminator = 0;
19279}
d9b3de22 19280
6f77053d
PA
19281void
19282lnp_state_machine::handle_set_file (file_name_index file)
19283{
19284 m_file = file;
19285
19286 const file_entry *fe = current_file ();
19287 if (fe == NULL)
19288 dwarf2_debug_line_missing_file_complaint ();
19289 else if (m_record_lines_p)
19290 {
19291 const char *dir = fe->include_dir (m_line_header);
19292
19293 m_last_subfile = current_subfile;
19294 m_line_has_non_zero_discriminator = m_discriminator != 0;
19295 dwarf2_start_subfile (fe->name, dir);
19296 }
19297}
19298
19299void
19300lnp_state_machine::handle_const_add_pc ()
19301{
19302 CORE_ADDR adjust
19303 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19304
19305 CORE_ADDR addr_adj
19306 = (((m_op_index + adjust)
19307 / m_line_header->maximum_ops_per_instruction)
19308 * m_line_header->minimum_instruction_length);
19309
19310 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19311 m_op_index = ((m_op_index + adjust)
19312 % m_line_header->maximum_ops_per_instruction);
19313}
d9b3de22 19314
c91513d8
PP
19315/* Ignore this record_line request. */
19316
19317static void
19318noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
19319{
19320 return;
19321}
19322
a05a36a5
DE
19323/* Return non-zero if we should add LINE to the line number table.
19324 LINE is the line to add, LAST_LINE is the last line that was added,
19325 LAST_SUBFILE is the subfile for LAST_LINE.
19326 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19327 had a non-zero discriminator.
19328
19329 We have to be careful in the presence of discriminators.
19330 E.g., for this line:
19331
19332 for (i = 0; i < 100000; i++);
19333
19334 clang can emit four line number entries for that one line,
19335 each with a different discriminator.
19336 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19337
19338 However, we want gdb to coalesce all four entries into one.
19339 Otherwise the user could stepi into the middle of the line and
19340 gdb would get confused about whether the pc really was in the
19341 middle of the line.
19342
19343 Things are further complicated by the fact that two consecutive
19344 line number entries for the same line is a heuristic used by gcc
19345 to denote the end of the prologue. So we can't just discard duplicate
19346 entries, we have to be selective about it. The heuristic we use is
19347 that we only collapse consecutive entries for the same line if at least
19348 one of those entries has a non-zero discriminator. PR 17276.
19349
19350 Note: Addresses in the line number state machine can never go backwards
19351 within one sequence, thus this coalescing is ok. */
19352
19353static int
19354dwarf_record_line_p (unsigned int line, unsigned int last_line,
19355 int line_has_non_zero_discriminator,
19356 struct subfile *last_subfile)
19357{
19358 if (current_subfile != last_subfile)
19359 return 1;
19360 if (line != last_line)
19361 return 1;
19362 /* Same line for the same file that we've seen already.
19363 As a last check, for pr 17276, only record the line if the line
19364 has never had a non-zero discriminator. */
19365 if (!line_has_non_zero_discriminator)
19366 return 1;
19367 return 0;
19368}
19369
252a6764
DE
19370/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
19371 in the line table of subfile SUBFILE. */
19372
19373static void
d9b3de22
DE
19374dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19375 unsigned int line, CORE_ADDR address,
19376 record_line_ftype p_record_line)
252a6764
DE
19377{
19378 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19379
27e0867f
DE
19380 if (dwarf_line_debug)
19381 {
19382 fprintf_unfiltered (gdb_stdlog,
19383 "Recording line %u, file %s, address %s\n",
19384 line, lbasename (subfile->name),
19385 paddress (gdbarch, address));
19386 }
19387
d5962de5 19388 (*p_record_line) (subfile, line, addr);
252a6764
DE
19389}
19390
19391/* Subroutine of dwarf_decode_lines_1 to simplify it.
19392 Mark the end of a set of line number records.
d9b3de22 19393 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
19394 If SUBFILE is NULL the request is ignored. */
19395
19396static void
19397dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19398 CORE_ADDR address, record_line_ftype p_record_line)
19399{
27e0867f
DE
19400 if (subfile == NULL)
19401 return;
19402
19403 if (dwarf_line_debug)
19404 {
19405 fprintf_unfiltered (gdb_stdlog,
19406 "Finishing current line, file %s, address %s\n",
19407 lbasename (subfile->name),
19408 paddress (gdbarch, address));
19409 }
19410
d9b3de22
DE
19411 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
19412}
19413
6f77053d
PA
19414void
19415lnp_state_machine::record_line (bool end_sequence)
d9b3de22 19416{
d9b3de22
DE
19417 if (dwarf_line_debug)
19418 {
19419 fprintf_unfiltered (gdb_stdlog,
19420 "Processing actual line %u: file %u,"
19421 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
19422 m_line, to_underlying (m_file),
19423 paddress (m_gdbarch, m_address),
19424 m_is_stmt, m_discriminator);
d9b3de22
DE
19425 }
19426
6f77053d 19427 file_entry *fe = current_file ();
8c43009f
PA
19428
19429 if (fe == NULL)
d9b3de22
DE
19430 dwarf2_debug_line_missing_file_complaint ();
19431 /* For now we ignore lines not starting on an instruction boundary.
19432 But not when processing end_sequence for compatibility with the
19433 previous version of the code. */
6f77053d 19434 else if (m_op_index == 0 || end_sequence)
d9b3de22 19435 {
8c43009f 19436 fe->included_p = 1;
6f77053d 19437 if (m_record_lines_p && m_is_stmt)
d9b3de22 19438 {
6f77053d 19439 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 19440 {
6f77053d
PA
19441 dwarf_finish_line (m_gdbarch, m_last_subfile,
19442 m_address, m_record_line_callback);
d9b3de22
DE
19443 }
19444
19445 if (!end_sequence)
19446 {
6f77053d
PA
19447 if (dwarf_record_line_p (m_line, m_last_line,
19448 m_line_has_non_zero_discriminator,
19449 m_last_subfile))
d9b3de22 19450 {
6f77053d
PA
19451 dwarf_record_line_1 (m_gdbarch, current_subfile,
19452 m_line, m_address,
19453 m_record_line_callback);
d9b3de22 19454 }
6f77053d
PA
19455 m_last_subfile = current_subfile;
19456 m_last_line = m_line;
d9b3de22
DE
19457 }
19458 }
19459 }
19460}
19461
6f77053d
PA
19462lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
19463 bool record_lines_p)
d9b3de22 19464{
6f77053d
PA
19465 m_gdbarch = arch;
19466 m_record_lines_p = record_lines_p;
19467 m_line_header = lh;
d9b3de22 19468
6f77053d 19469 m_record_line_callback = ::record_line;
d9b3de22 19470
d9b3de22
DE
19471 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19472 was a line entry for it so that the backend has a chance to adjust it
19473 and also record it in case it needs it. This is currently used by MIPS
19474 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
19475 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19476 m_is_stmt = lh->default_is_stmt;
19477 m_discriminator = 0;
252a6764
DE
19478}
19479
6f77053d
PA
19480void
19481lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19482 const gdb_byte *line_ptr,
19483 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
19484{
19485 /* If address < lowpc then it's not a usable value, it's outside the
19486 pc range of the CU. However, we restrict the test to only address
19487 values of zero to preserve GDB's previous behaviour which is to
19488 handle the specific case of a function being GC'd by the linker. */
19489
19490 if (address == 0 && address < lowpc)
19491 {
19492 /* This line table is for a function which has been
19493 GCd by the linker. Ignore it. PR gdb/12528 */
19494
19495 struct objfile *objfile = cu->objfile;
19496 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19497
19498 complaint (&symfile_complaints,
19499 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19500 line_offset, objfile_name (objfile));
6f77053d
PA
19501 m_record_line_callback = noop_record_line;
19502 /* Note: record_line_callback is left as noop_record_line until
19503 we see DW_LNE_end_sequence. */
924c2928
DE
19504 }
19505}
19506
f3f5162e 19507/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
19508 Process the line number information in LH.
19509 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19510 program in order to set included_p for every referenced header. */
debd256d 19511
c906108c 19512static void
43f3e411
DE
19513dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19514 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 19515{
d521ce57
TT
19516 const gdb_byte *line_ptr, *extended_end;
19517 const gdb_byte *line_end;
a8c50c1f 19518 unsigned int bytes_read, extended_len;
699ca60a 19519 unsigned char op_code, extended_op;
e142c38c
DJ
19520 CORE_ADDR baseaddr;
19521 struct objfile *objfile = cu->objfile;
f3f5162e 19522 bfd *abfd = objfile->obfd;
fbf65064 19523 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
19524 /* True if we're recording line info (as opposed to building partial
19525 symtabs and just interested in finding include files mentioned by
19526 the line number program). */
19527 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
19528
19529 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 19530
debd256d
JB
19531 line_ptr = lh->statement_program_start;
19532 line_end = lh->statement_program_end;
c906108c
SS
19533
19534 /* Read the statement sequences until there's nothing left. */
19535 while (line_ptr < line_end)
19536 {
6f77053d
PA
19537 /* The DWARF line number program state machine. Reset the state
19538 machine at the start of each sequence. */
19539 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
19540 bool end_sequence = false;
d9b3de22 19541
8c43009f 19542 if (record_lines_p)
c906108c 19543 {
8c43009f
PA
19544 /* Start a subfile for the current file of the state
19545 machine. */
19546 const file_entry *fe = state_machine.current_file ();
19547
19548 if (fe != NULL)
19549 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
19550 }
19551
a738430d 19552 /* Decode the table. */
d9b3de22 19553 while (line_ptr < line_end && !end_sequence)
c906108c
SS
19554 {
19555 op_code = read_1_byte (abfd, line_ptr);
19556 line_ptr += 1;
9aa1fe7e 19557
debd256d 19558 if (op_code >= lh->opcode_base)
6e70227d 19559 {
8e07a239 19560 /* Special opcode. */
6f77053d 19561 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
19562 }
19563 else switch (op_code)
c906108c
SS
19564 {
19565 case DW_LNS_extended_op:
3e43a32a
MS
19566 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19567 &bytes_read);
473b7be6 19568 line_ptr += bytes_read;
a8c50c1f 19569 extended_end = line_ptr + extended_len;
c906108c
SS
19570 extended_op = read_1_byte (abfd, line_ptr);
19571 line_ptr += 1;
19572 switch (extended_op)
19573 {
19574 case DW_LNE_end_sequence:
6f77053d
PA
19575 state_machine.handle_end_sequence ();
19576 end_sequence = true;
c906108c
SS
19577 break;
19578 case DW_LNE_set_address:
d9b3de22
DE
19579 {
19580 CORE_ADDR address
19581 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 19582 line_ptr += bytes_read;
6f77053d
PA
19583
19584 state_machine.check_line_address (cu, line_ptr,
19585 lowpc, address);
19586 state_machine.handle_set_address (baseaddr, address);
d9b3de22 19587 }
c906108c
SS
19588 break;
19589 case DW_LNE_define_file:
debd256d 19590 {
d521ce57 19591 const char *cur_file;
ecfb656c
PA
19592 unsigned int mod_time, length;
19593 dir_index dindex;
6e70227d 19594
3e43a32a
MS
19595 cur_file = read_direct_string (abfd, line_ptr,
19596 &bytes_read);
debd256d 19597 line_ptr += bytes_read;
ecfb656c 19598 dindex = (dir_index)
debd256d
JB
19599 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19600 line_ptr += bytes_read;
19601 mod_time =
19602 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19603 line_ptr += bytes_read;
19604 length =
19605 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19606 line_ptr += bytes_read;
ecfb656c 19607 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 19608 }
c906108c 19609 break;
d0c6ba3d 19610 case DW_LNE_set_discriminator:
6f77053d
PA
19611 {
19612 /* The discriminator is not interesting to the
19613 debugger; just ignore it. We still need to
19614 check its value though:
19615 if there are consecutive entries for the same
19616 (non-prologue) line we want to coalesce them.
19617 PR 17276. */
19618 unsigned int discr
19619 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19620 line_ptr += bytes_read;
19621
19622 state_machine.handle_set_discriminator (discr);
19623 }
d0c6ba3d 19624 break;
c906108c 19625 default:
4d3c2250 19626 complaint (&symfile_complaints,
e2e0b3e5 19627 _("mangled .debug_line section"));
debd256d 19628 return;
c906108c 19629 }
a8c50c1f
DJ
19630 /* Make sure that we parsed the extended op correctly. If e.g.
19631 we expected a different address size than the producer used,
19632 we may have read the wrong number of bytes. */
19633 if (line_ptr != extended_end)
19634 {
19635 complaint (&symfile_complaints,
19636 _("mangled .debug_line section"));
19637 return;
19638 }
c906108c
SS
19639 break;
19640 case DW_LNS_copy:
6f77053d 19641 state_machine.handle_copy ();
c906108c
SS
19642 break;
19643 case DW_LNS_advance_pc:
2dc7f7b3
TT
19644 {
19645 CORE_ADDR adjust
19646 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 19647 line_ptr += bytes_read;
6f77053d
PA
19648
19649 state_machine.handle_advance_pc (adjust);
2dc7f7b3 19650 }
c906108c
SS
19651 break;
19652 case DW_LNS_advance_line:
a05a36a5
DE
19653 {
19654 int line_delta
19655 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 19656 line_ptr += bytes_read;
6f77053d
PA
19657
19658 state_machine.handle_advance_line (line_delta);
a05a36a5 19659 }
c906108c
SS
19660 break;
19661 case DW_LNS_set_file:
d9b3de22 19662 {
6f77053d 19663 file_name_index file
ecfb656c
PA
19664 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19665 &bytes_read);
d9b3de22 19666 line_ptr += bytes_read;
8c43009f 19667
6f77053d 19668 state_machine.handle_set_file (file);
d9b3de22 19669 }
c906108c
SS
19670 break;
19671 case DW_LNS_set_column:
0ad93d4f 19672 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
19673 line_ptr += bytes_read;
19674 break;
19675 case DW_LNS_negate_stmt:
6f77053d 19676 state_machine.handle_negate_stmt ();
c906108c
SS
19677 break;
19678 case DW_LNS_set_basic_block:
c906108c 19679 break;
c2c6d25f
JM
19680 /* Add to the address register of the state machine the
19681 address increment value corresponding to special opcode
a738430d
MK
19682 255. I.e., this value is scaled by the minimum
19683 instruction length since special opcode 255 would have
b021a221 19684 scaled the increment. */
c906108c 19685 case DW_LNS_const_add_pc:
6f77053d 19686 state_machine.handle_const_add_pc ();
c906108c
SS
19687 break;
19688 case DW_LNS_fixed_advance_pc:
3e29f34a 19689 {
6f77053d 19690 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 19691 line_ptr += 2;
6f77053d
PA
19692
19693 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 19694 }
c906108c 19695 break;
9aa1fe7e 19696 default:
a738430d
MK
19697 {
19698 /* Unknown standard opcode, ignore it. */
9aa1fe7e 19699 int i;
a738430d 19700
debd256d 19701 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
19702 {
19703 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19704 line_ptr += bytes_read;
19705 }
19706 }
c906108c
SS
19707 }
19708 }
d9b3de22
DE
19709
19710 if (!end_sequence)
19711 dwarf2_debug_line_missing_end_sequence_complaint ();
19712
19713 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19714 in which case we still finish recording the last line). */
6f77053d 19715 state_machine.record_line (true);
c906108c 19716 }
f3f5162e
DE
19717}
19718
19719/* Decode the Line Number Program (LNP) for the given line_header
19720 structure and CU. The actual information extracted and the type
19721 of structures created from the LNP depends on the value of PST.
19722
19723 1. If PST is NULL, then this procedure uses the data from the program
19724 to create all necessary symbol tables, and their linetables.
19725
19726 2. If PST is not NULL, this procedure reads the program to determine
19727 the list of files included by the unit represented by PST, and
19728 builds all the associated partial symbol tables.
19729
19730 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19731 It is used for relative paths in the line table.
19732 NOTE: When processing partial symtabs (pst != NULL),
19733 comp_dir == pst->dirname.
19734
19735 NOTE: It is important that psymtabs have the same file name (via strcmp)
19736 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19737 symtab we don't use it in the name of the psymtabs we create.
19738 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
19739 A good testcase for this is mb-inline.exp.
19740
527f3840
JK
19741 LOWPC is the lowest address in CU (or 0 if not known).
19742
19743 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19744 for its PC<->lines mapping information. Otherwise only the filename
19745 table is read in. */
f3f5162e
DE
19746
19747static void
19748dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 19749 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 19750 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
19751{
19752 struct objfile *objfile = cu->objfile;
19753 const int decode_for_pst_p = (pst != NULL);
f3f5162e 19754
527f3840
JK
19755 if (decode_mapping)
19756 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
19757
19758 if (decode_for_pst_p)
19759 {
19760 int file_index;
19761
19762 /* Now that we're done scanning the Line Header Program, we can
19763 create the psymtab of each included file. */
fff8551c 19764 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
19765 if (lh->file_names[file_index].included_p == 1)
19766 {
d521ce57 19767 const char *include_name =
c6da4cef
DE
19768 psymtab_include_file_name (lh, file_index, pst, comp_dir);
19769 if (include_name != NULL)
aaa75496
JB
19770 dwarf2_create_include_psymtab (include_name, pst, objfile);
19771 }
19772 }
cb1df416
DJ
19773 else
19774 {
19775 /* Make sure a symtab is created for every file, even files
19776 which contain only variables (i.e. no code with associated
19777 line numbers). */
43f3e411 19778 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 19779 int i;
cb1df416 19780
fff8551c 19781 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 19782 {
8c43009f 19783 file_entry &fe = lh->file_names[i];
9a619af0 19784
8c43009f 19785 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 19786
cb1df416 19787 if (current_subfile->symtab == NULL)
43f3e411
DE
19788 {
19789 current_subfile->symtab
19790 = allocate_symtab (cust, current_subfile->name);
19791 }
8c43009f 19792 fe.symtab = current_subfile->symtab;
cb1df416
DJ
19793 }
19794 }
c906108c
SS
19795}
19796
19797/* Start a subfile for DWARF. FILENAME is the name of the file and
19798 DIRNAME the name of the source directory which contains FILENAME
4d663531 19799 or NULL if not known.
c906108c
SS
19800 This routine tries to keep line numbers from identical absolute and
19801 relative file names in a common subfile.
19802
19803 Using the `list' example from the GDB testsuite, which resides in
19804 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19805 of /srcdir/list0.c yields the following debugging information for list0.c:
19806
c5aa993b 19807 DW_AT_name: /srcdir/list0.c
4d663531 19808 DW_AT_comp_dir: /compdir
357e46e7 19809 files.files[0].name: list0.h
c5aa993b 19810 files.files[0].dir: /srcdir
357e46e7 19811 files.files[1].name: list0.c
c5aa993b 19812 files.files[1].dir: /srcdir
c906108c
SS
19813
19814 The line number information for list0.c has to end up in a single
4f1520fb
FR
19815 subfile, so that `break /srcdir/list0.c:1' works as expected.
19816 start_subfile will ensure that this happens provided that we pass the
19817 concatenation of files.files[1].dir and files.files[1].name as the
19818 subfile's name. */
c906108c
SS
19819
19820static void
4d663531 19821dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 19822{
d521ce57 19823 char *copy = NULL;
4f1520fb 19824
4d663531 19825 /* In order not to lose the line information directory,
4f1520fb
FR
19826 we concatenate it to the filename when it makes sense.
19827 Note that the Dwarf3 standard says (speaking of filenames in line
19828 information): ``The directory index is ignored for file names
19829 that represent full path names''. Thus ignoring dirname in the
19830 `else' branch below isn't an issue. */
c906108c 19831
d5166ae1 19832 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
19833 {
19834 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
19835 filename = copy;
19836 }
c906108c 19837
4d663531 19838 start_subfile (filename);
4f1520fb 19839
d521ce57
TT
19840 if (copy != NULL)
19841 xfree (copy);
c906108c
SS
19842}
19843
f4dc4d17
DE
19844/* Start a symtab for DWARF.
19845 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
19846
43f3e411 19847static struct compunit_symtab *
f4dc4d17 19848dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 19849 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 19850{
43f3e411 19851 struct compunit_symtab *cust
5ffa0793 19852 = start_symtab (cu->objfile, name, comp_dir, low_pc, cu->language);
43f3e411 19853
f4dc4d17
DE
19854 record_debugformat ("DWARF 2");
19855 record_producer (cu->producer);
19856
19857 /* We assume that we're processing GCC output. */
19858 processing_gcc_compilation = 2;
19859
4d4ec4e5 19860 cu->processing_has_namespace_info = 0;
43f3e411
DE
19861
19862 return cust;
f4dc4d17
DE
19863}
19864
4c2df51b
DJ
19865static void
19866var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 19867 struct dwarf2_cu *cu)
4c2df51b 19868{
e7c27a73
DJ
19869 struct objfile *objfile = cu->objfile;
19870 struct comp_unit_head *cu_header = &cu->header;
19871
4c2df51b
DJ
19872 /* NOTE drow/2003-01-30: There used to be a comment and some special
19873 code here to turn a symbol with DW_AT_external and a
19874 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19875 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19876 with some versions of binutils) where shared libraries could have
19877 relocations against symbols in their debug information - the
19878 minimal symbol would have the right address, but the debug info
19879 would not. It's no longer necessary, because we will explicitly
19880 apply relocations when we read in the debug information now. */
19881
19882 /* A DW_AT_location attribute with no contents indicates that a
19883 variable has been optimized away. */
19884 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
19885 {
f1e6e072 19886 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
19887 return;
19888 }
19889
19890 /* Handle one degenerate form of location expression specially, to
19891 preserve GDB's previous behavior when section offsets are
3019eac3
DE
19892 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
19893 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
19894
19895 if (attr_form_is_block (attr)
3019eac3
DE
19896 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19897 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19898 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19899 && (DW_BLOCK (attr)->size
19900 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 19901 {
891d2f0b 19902 unsigned int dummy;
4c2df51b 19903
3019eac3
DE
19904 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
19905 SYMBOL_VALUE_ADDRESS (sym) =
19906 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
19907 else
19908 SYMBOL_VALUE_ADDRESS (sym) =
19909 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 19910 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
19911 fixup_symbol_section (sym, objfile);
19912 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
19913 SYMBOL_SECTION (sym));
4c2df51b
DJ
19914 return;
19915 }
19916
19917 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
19918 expression evaluator, and use LOC_COMPUTED only when necessary
19919 (i.e. when the value of a register or memory location is
19920 referenced, or a thread-local block, etc.). Then again, it might
19921 not be worthwhile. I'm assuming that it isn't unless performance
19922 or memory numbers show me otherwise. */
19923
f1e6e072 19924 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 19925
f1e6e072 19926 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 19927 cu->has_loclist = 1;
4c2df51b
DJ
19928}
19929
c906108c
SS
19930/* Given a pointer to a DWARF information entry, figure out if we need
19931 to make a symbol table entry for it, and if so, create a new entry
19932 and return a pointer to it.
19933 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
19934 used the passed type.
19935 If SPACE is not NULL, use it to hold the new symbol. If it is
19936 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
19937
19938static struct symbol *
34eaf542
TT
19939new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
19940 struct symbol *space)
c906108c 19941{
e7c27a73 19942 struct objfile *objfile = cu->objfile;
3e29f34a 19943 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 19944 struct symbol *sym = NULL;
15d034d0 19945 const char *name;
c906108c
SS
19946 struct attribute *attr = NULL;
19947 struct attribute *attr2 = NULL;
e142c38c 19948 CORE_ADDR baseaddr;
e37fd15a
SW
19949 struct pending **list_to_add = NULL;
19950
edb3359d 19951 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
19952
19953 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 19954
94af9270 19955 name = dwarf2_name (die, cu);
c906108c
SS
19956 if (name)
19957 {
94af9270 19958 const char *linkagename;
34eaf542 19959 int suppress_add = 0;
94af9270 19960
34eaf542
TT
19961 if (space)
19962 sym = space;
19963 else
e623cf5d 19964 sym = allocate_symbol (objfile);
c906108c 19965 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
19966
19967 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 19968 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
19969 linkagename = dwarf2_physname (name, die, cu);
19970 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 19971
f55ee35c
JK
19972 /* Fortran does not have mangling standard and the mangling does differ
19973 between gfortran, iFort etc. */
19974 if (cu->language == language_fortran
b250c185 19975 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 19976 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 19977 dwarf2_full_name (name, die, cu),
29df156d 19978 NULL);
f55ee35c 19979
c906108c 19980 /* Default assumptions.
c5aa993b 19981 Use the passed type or decode it from the die. */
176620f1 19982 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 19983 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
19984 if (type != NULL)
19985 SYMBOL_TYPE (sym) = type;
19986 else
e7c27a73 19987 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
19988 attr = dwarf2_attr (die,
19989 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
19990 cu);
c906108c
SS
19991 if (attr)
19992 {
19993 SYMBOL_LINE (sym) = DW_UNSND (attr);
19994 }
cb1df416 19995
edb3359d
DJ
19996 attr = dwarf2_attr (die,
19997 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
19998 cu);
cb1df416
DJ
19999 if (attr)
20000 {
ecfb656c 20001 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20002 struct file_entry *fe;
9a619af0 20003
ecfb656c
PA
20004 if (cu->line_header != NULL)
20005 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20006 else
20007 fe = NULL;
20008
20009 if (fe == NULL)
cb1df416
DJ
20010 complaint (&symfile_complaints,
20011 _("file index out of range"));
8c43009f
PA
20012 else
20013 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20014 }
20015
c906108c
SS
20016 switch (die->tag)
20017 {
20018 case DW_TAG_label:
e142c38c 20019 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 20020 if (attr)
3e29f34a
MR
20021 {
20022 CORE_ADDR addr;
20023
20024 addr = attr_value_as_address (attr);
20025 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20026 SYMBOL_VALUE_ADDRESS (sym) = addr;
20027 }
0f5238ed
TT
20028 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20029 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20030 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 20031 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20032 break;
20033 case DW_TAG_subprogram:
20034 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20035 finish_block. */
f1e6e072 20036 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20037 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
20038 if ((attr2 && (DW_UNSND (attr2) != 0))
20039 || cu->language == language_ada)
c906108c 20040 {
2cfa0c8d
JB
20041 /* Subprograms marked external are stored as a global symbol.
20042 Ada subprograms, whether marked external or not, are always
20043 stored as a global symbol, because we want to be able to
20044 access them globally. For instance, we want to be able
20045 to break on a nested subprogram without having to
20046 specify the context. */
e37fd15a 20047 list_to_add = &global_symbols;
c906108c
SS
20048 }
20049 else
20050 {
e37fd15a 20051 list_to_add = cu->list_in_scope;
c906108c
SS
20052 }
20053 break;
edb3359d
DJ
20054 case DW_TAG_inlined_subroutine:
20055 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20056 finish_block. */
f1e6e072 20057 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20058 SYMBOL_INLINED (sym) = 1;
481860b3 20059 list_to_add = cu->list_in_scope;
edb3359d 20060 break;
34eaf542
TT
20061 case DW_TAG_template_value_param:
20062 suppress_add = 1;
20063 /* Fall through. */
72929c62 20064 case DW_TAG_constant:
c906108c 20065 case DW_TAG_variable:
254e6b9e 20066 case DW_TAG_member:
0963b4bd
MS
20067 /* Compilation with minimal debug info may result in
20068 variables with missing type entries. Change the
20069 misleading `void' type to something sensible. */
c906108c 20070 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 20071 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20072
e142c38c 20073 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20074 /* In the case of DW_TAG_member, we should only be called for
20075 static const members. */
20076 if (die->tag == DW_TAG_member)
20077 {
3863f96c
DE
20078 /* dwarf2_add_field uses die_is_declaration,
20079 so we do the same. */
254e6b9e
DE
20080 gdb_assert (die_is_declaration (die, cu));
20081 gdb_assert (attr);
20082 }
c906108c
SS
20083 if (attr)
20084 {
e7c27a73 20085 dwarf2_const_value (attr, sym, cu);
e142c38c 20086 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20087 if (!suppress_add)
34eaf542
TT
20088 {
20089 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 20090 list_to_add = &global_symbols;
34eaf542 20091 else
e37fd15a 20092 list_to_add = cu->list_in_scope;
34eaf542 20093 }
c906108c
SS
20094 break;
20095 }
e142c38c 20096 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
20097 if (attr)
20098 {
e7c27a73 20099 var_decode_location (attr, sym, cu);
e142c38c 20100 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20101
20102 /* Fortran explicitly imports any global symbols to the local
20103 scope by DW_TAG_common_block. */
20104 if (cu->language == language_fortran && die->parent
20105 && die->parent->tag == DW_TAG_common_block)
20106 attr2 = NULL;
20107
caac4577
JG
20108 if (SYMBOL_CLASS (sym) == LOC_STATIC
20109 && SYMBOL_VALUE_ADDRESS (sym) == 0
20110 && !dwarf2_per_objfile->has_section_at_zero)
20111 {
20112 /* When a static variable is eliminated by the linker,
20113 the corresponding debug information is not stripped
20114 out, but the variable address is set to null;
20115 do not add such variables into symbol table. */
20116 }
20117 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20118 {
f55ee35c
JK
20119 /* Workaround gfortran PR debug/40040 - it uses
20120 DW_AT_location for variables in -fPIC libraries which may
20121 get overriden by other libraries/executable and get
20122 a different address. Resolve it by the minimal symbol
20123 which may come from inferior's executable using copy
20124 relocation. Make this workaround only for gfortran as for
20125 other compilers GDB cannot guess the minimal symbol
20126 Fortran mangling kind. */
20127 if (cu->language == language_fortran && die->parent
20128 && die->parent->tag == DW_TAG_module
20129 && cu->producer
28586665 20130 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 20131 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 20132
1c809c68
TT
20133 /* A variable with DW_AT_external is never static,
20134 but it may be block-scoped. */
20135 list_to_add = (cu->list_in_scope == &file_symbols
20136 ? &global_symbols : cu->list_in_scope);
1c809c68 20137 }
c906108c 20138 else
e37fd15a 20139 list_to_add = cu->list_in_scope;
c906108c
SS
20140 }
20141 else
20142 {
20143 /* We do not know the address of this symbol.
c5aa993b
JM
20144 If it is an external symbol and we have type information
20145 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20146 The address of the variable will then be determined from
20147 the minimal symbol table whenever the variable is
20148 referenced. */
e142c38c 20149 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20150
20151 /* Fortran explicitly imports any global symbols to the local
20152 scope by DW_TAG_common_block. */
20153 if (cu->language == language_fortran && die->parent
20154 && die->parent->tag == DW_TAG_common_block)
20155 {
20156 /* SYMBOL_CLASS doesn't matter here because
20157 read_common_block is going to reset it. */
20158 if (!suppress_add)
20159 list_to_add = cu->list_in_scope;
20160 }
20161 else if (attr2 && (DW_UNSND (attr2) != 0)
20162 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20163 {
0fe7935b
DJ
20164 /* A variable with DW_AT_external is never static, but it
20165 may be block-scoped. */
20166 list_to_add = (cu->list_in_scope == &file_symbols
20167 ? &global_symbols : cu->list_in_scope);
20168
f1e6e072 20169 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20170 }
442ddf59
JK
20171 else if (!die_is_declaration (die, cu))
20172 {
20173 /* Use the default LOC_OPTIMIZED_OUT class. */
20174 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20175 if (!suppress_add)
20176 list_to_add = cu->list_in_scope;
442ddf59 20177 }
c906108c
SS
20178 }
20179 break;
20180 case DW_TAG_formal_parameter:
edb3359d
DJ
20181 /* If we are inside a function, mark this as an argument. If
20182 not, we might be looking at an argument to an inlined function
20183 when we do not have enough information to show inlined frames;
20184 pretend it's a local variable in that case so that the user can
20185 still see it. */
20186 if (context_stack_depth > 0
20187 && context_stack[context_stack_depth - 1].name != NULL)
20188 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 20189 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
20190 if (attr)
20191 {
e7c27a73 20192 var_decode_location (attr, sym, cu);
c906108c 20193 }
e142c38c 20194 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
20195 if (attr)
20196 {
e7c27a73 20197 dwarf2_const_value (attr, sym, cu);
c906108c 20198 }
f346a30d 20199
e37fd15a 20200 list_to_add = cu->list_in_scope;
c906108c
SS
20201 break;
20202 case DW_TAG_unspecified_parameters:
20203 /* From varargs functions; gdb doesn't seem to have any
20204 interest in this information, so just ignore it for now.
20205 (FIXME?) */
20206 break;
34eaf542
TT
20207 case DW_TAG_template_type_param:
20208 suppress_add = 1;
20209 /* Fall through. */
c906108c 20210 case DW_TAG_class_type:
680b30c7 20211 case DW_TAG_interface_type:
c906108c
SS
20212 case DW_TAG_structure_type:
20213 case DW_TAG_union_type:
72019c9c 20214 case DW_TAG_set_type:
c906108c 20215 case DW_TAG_enumeration_type:
f1e6e072 20216 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20217 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20218
63d06c5c 20219 {
9c37b5ae 20220 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20221 really ever be static objects: otherwise, if you try
20222 to, say, break of a class's method and you're in a file
20223 which doesn't mention that class, it won't work unless
20224 the check for all static symbols in lookup_symbol_aux
20225 saves you. See the OtherFileClass tests in
20226 gdb.c++/namespace.exp. */
20227
e37fd15a 20228 if (!suppress_add)
34eaf542 20229 {
34eaf542 20230 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 20231 && cu->language == language_cplus
34eaf542 20232 ? &global_symbols : cu->list_in_scope);
63d06c5c 20233
64382290 20234 /* The semantics of C++ state that "struct foo {
9c37b5ae 20235 ... }" also defines a typedef for "foo". */
64382290 20236 if (cu->language == language_cplus
45280282 20237 || cu->language == language_ada
c44af4eb
TT
20238 || cu->language == language_d
20239 || cu->language == language_rust)
64382290
TT
20240 {
20241 /* The symbol's name is already allocated along
20242 with this objfile, so we don't need to
20243 duplicate it for the type. */
20244 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20245 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
20246 }
63d06c5c
DC
20247 }
20248 }
c906108c
SS
20249 break;
20250 case DW_TAG_typedef:
f1e6e072 20251 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20252 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20253 list_to_add = cu->list_in_scope;
63d06c5c 20254 break;
c906108c 20255 case DW_TAG_base_type:
a02abb62 20256 case DW_TAG_subrange_type:
f1e6e072 20257 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20258 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20259 list_to_add = cu->list_in_scope;
c906108c
SS
20260 break;
20261 case DW_TAG_enumerator:
e142c38c 20262 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
20263 if (attr)
20264 {
e7c27a73 20265 dwarf2_const_value (attr, sym, cu);
c906108c 20266 }
63d06c5c
DC
20267 {
20268 /* NOTE: carlton/2003-11-10: See comment above in the
20269 DW_TAG_class_type, etc. block. */
20270
e142c38c 20271 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 20272 && cu->language == language_cplus
e142c38c 20273 ? &global_symbols : cu->list_in_scope);
63d06c5c 20274 }
c906108c 20275 break;
74921315 20276 case DW_TAG_imported_declaration:
5c4e30ca 20277 case DW_TAG_namespace:
f1e6e072 20278 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 20279 list_to_add = &global_symbols;
5c4e30ca 20280 break;
530e8392
KB
20281 case DW_TAG_module:
20282 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20283 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20284 list_to_add = &global_symbols;
20285 break;
4357ac6c 20286 case DW_TAG_common_block:
f1e6e072 20287 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
20288 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20289 add_symbol_to_list (sym, cu->list_in_scope);
20290 break;
c906108c
SS
20291 default:
20292 /* Not a tag we recognize. Hopefully we aren't processing
20293 trash data, but since we must specifically ignore things
20294 we don't recognize, there is nothing else we should do at
0963b4bd 20295 this point. */
e2e0b3e5 20296 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 20297 dwarf_tag_name (die->tag));
c906108c
SS
20298 break;
20299 }
df8a16a1 20300
e37fd15a
SW
20301 if (suppress_add)
20302 {
20303 sym->hash_next = objfile->template_symbols;
20304 objfile->template_symbols = sym;
20305 list_to_add = NULL;
20306 }
20307
20308 if (list_to_add != NULL)
20309 add_symbol_to_list (sym, list_to_add);
20310
df8a16a1
DJ
20311 /* For the benefit of old versions of GCC, check for anonymous
20312 namespaces based on the demangled name. */
4d4ec4e5 20313 if (!cu->processing_has_namespace_info
94af9270 20314 && cu->language == language_cplus)
a10964d1 20315 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
20316 }
20317 return (sym);
20318}
20319
34eaf542
TT
20320/* A wrapper for new_symbol_full that always allocates a new symbol. */
20321
20322static struct symbol *
20323new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20324{
20325 return new_symbol_full (die, type, cu, NULL);
20326}
20327
98bfdba5
PA
20328/* Given an attr with a DW_FORM_dataN value in host byte order,
20329 zero-extend it as appropriate for the symbol's type. The DWARF
20330 standard (v4) is not entirely clear about the meaning of using
20331 DW_FORM_dataN for a constant with a signed type, where the type is
20332 wider than the data. The conclusion of a discussion on the DWARF
20333 list was that this is unspecified. We choose to always zero-extend
20334 because that is the interpretation long in use by GCC. */
c906108c 20335
98bfdba5 20336static gdb_byte *
ff39bb5e 20337dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 20338 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 20339{
e7c27a73 20340 struct objfile *objfile = cu->objfile;
e17a4113
UW
20341 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20342 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
20343 LONGEST l = DW_UNSND (attr);
20344
20345 if (bits < sizeof (*value) * 8)
20346 {
20347 l &= ((LONGEST) 1 << bits) - 1;
20348 *value = l;
20349 }
20350 else if (bits == sizeof (*value) * 8)
20351 *value = l;
20352 else
20353 {
224c3ddb 20354 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
20355 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20356 return bytes;
20357 }
20358
20359 return NULL;
20360}
20361
20362/* Read a constant value from an attribute. Either set *VALUE, or if
20363 the value does not fit in *VALUE, set *BYTES - either already
20364 allocated on the objfile obstack, or newly allocated on OBSTACK,
20365 or, set *BATON, if we translated the constant to a location
20366 expression. */
20367
20368static void
ff39bb5e 20369dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
20370 const char *name, struct obstack *obstack,
20371 struct dwarf2_cu *cu,
d521ce57 20372 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
20373 struct dwarf2_locexpr_baton **baton)
20374{
20375 struct objfile *objfile = cu->objfile;
20376 struct comp_unit_head *cu_header = &cu->header;
c906108c 20377 struct dwarf_block *blk;
98bfdba5
PA
20378 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20379 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20380
20381 *value = 0;
20382 *bytes = NULL;
20383 *baton = NULL;
c906108c
SS
20384
20385 switch (attr->form)
20386 {
20387 case DW_FORM_addr:
3019eac3 20388 case DW_FORM_GNU_addr_index:
ac56253d 20389 {
ac56253d
TT
20390 gdb_byte *data;
20391
98bfdba5
PA
20392 if (TYPE_LENGTH (type) != cu_header->addr_size)
20393 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 20394 cu_header->addr_size,
98bfdba5 20395 TYPE_LENGTH (type));
ac56253d
TT
20396 /* Symbols of this form are reasonably rare, so we just
20397 piggyback on the existing location code rather than writing
20398 a new implementation of symbol_computed_ops. */
8d749320 20399 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
20400 (*baton)->per_cu = cu->per_cu;
20401 gdb_assert ((*baton)->per_cu);
ac56253d 20402
98bfdba5 20403 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 20404 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 20405 (*baton)->data = data;
ac56253d
TT
20406
20407 data[0] = DW_OP_addr;
20408 store_unsigned_integer (&data[1], cu_header->addr_size,
20409 byte_order, DW_ADDR (attr));
20410 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 20411 }
c906108c 20412 break;
4ac36638 20413 case DW_FORM_string:
93b5768b 20414 case DW_FORM_strp:
3019eac3 20415 case DW_FORM_GNU_str_index:
36586728 20416 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
20417 /* DW_STRING is already allocated on the objfile obstack, point
20418 directly to it. */
d521ce57 20419 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 20420 break;
c906108c
SS
20421 case DW_FORM_block1:
20422 case DW_FORM_block2:
20423 case DW_FORM_block4:
20424 case DW_FORM_block:
2dc7f7b3 20425 case DW_FORM_exprloc:
0224619f 20426 case DW_FORM_data16:
c906108c 20427 blk = DW_BLOCK (attr);
98bfdba5
PA
20428 if (TYPE_LENGTH (type) != blk->size)
20429 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20430 TYPE_LENGTH (type));
20431 *bytes = blk->data;
c906108c 20432 break;
2df3850c
JM
20433
20434 /* The DW_AT_const_value attributes are supposed to carry the
20435 symbol's value "represented as it would be on the target
20436 architecture." By the time we get here, it's already been
20437 converted to host endianness, so we just need to sign- or
20438 zero-extend it as appropriate. */
20439 case DW_FORM_data1:
3aef2284 20440 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 20441 break;
c906108c 20442 case DW_FORM_data2:
3aef2284 20443 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 20444 break;
c906108c 20445 case DW_FORM_data4:
3aef2284 20446 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 20447 break;
c906108c 20448 case DW_FORM_data8:
3aef2284 20449 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
20450 break;
20451
c906108c 20452 case DW_FORM_sdata:
663c44ac 20453 case DW_FORM_implicit_const:
98bfdba5 20454 *value = DW_SND (attr);
2df3850c
JM
20455 break;
20456
c906108c 20457 case DW_FORM_udata:
98bfdba5 20458 *value = DW_UNSND (attr);
c906108c 20459 break;
2df3850c 20460
c906108c 20461 default:
4d3c2250 20462 complaint (&symfile_complaints,
e2e0b3e5 20463 _("unsupported const value attribute form: '%s'"),
4d3c2250 20464 dwarf_form_name (attr->form));
98bfdba5 20465 *value = 0;
c906108c
SS
20466 break;
20467 }
20468}
20469
2df3850c 20470
98bfdba5
PA
20471/* Copy constant value from an attribute to a symbol. */
20472
2df3850c 20473static void
ff39bb5e 20474dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 20475 struct dwarf2_cu *cu)
2df3850c 20476{
98bfdba5 20477 struct objfile *objfile = cu->objfile;
12df843f 20478 LONGEST value;
d521ce57 20479 const gdb_byte *bytes;
98bfdba5 20480 struct dwarf2_locexpr_baton *baton;
2df3850c 20481
98bfdba5
PA
20482 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20483 SYMBOL_PRINT_NAME (sym),
20484 &objfile->objfile_obstack, cu,
20485 &value, &bytes, &baton);
2df3850c 20486
98bfdba5
PA
20487 if (baton != NULL)
20488 {
98bfdba5 20489 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 20490 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
20491 }
20492 else if (bytes != NULL)
20493 {
20494 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 20495 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
20496 }
20497 else
20498 {
20499 SYMBOL_VALUE (sym) = value;
f1e6e072 20500 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 20501 }
2df3850c
JM
20502}
20503
c906108c
SS
20504/* Return the type of the die in question using its DW_AT_type attribute. */
20505
20506static struct type *
e7c27a73 20507die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20508{
c906108c 20509 struct attribute *type_attr;
c906108c 20510
e142c38c 20511 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
20512 if (!type_attr)
20513 {
20514 /* A missing DW_AT_type represents a void type. */
46bf5051 20515 return objfile_type (cu->objfile)->builtin_void;
c906108c 20516 }
348e048f 20517
673bfd45 20518 return lookup_die_type (die, type_attr, cu);
c906108c
SS
20519}
20520
b4ba55a1
JB
20521/* True iff CU's producer generates GNAT Ada auxiliary information
20522 that allows to find parallel types through that information instead
20523 of having to do expensive parallel lookups by type name. */
20524
20525static int
20526need_gnat_info (struct dwarf2_cu *cu)
20527{
20528 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
20529 of GNAT produces this auxiliary information, without any indication
20530 that it is produced. Part of enhancing the FSF version of GNAT
20531 to produce that information will be to put in place an indicator
20532 that we can use in order to determine whether the descriptive type
20533 info is available or not. One suggestion that has been made is
20534 to use a new attribute, attached to the CU die. For now, assume
20535 that the descriptive type info is not available. */
20536 return 0;
20537}
20538
b4ba55a1
JB
20539/* Return the auxiliary type of the die in question using its
20540 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20541 attribute is not present. */
20542
20543static struct type *
20544die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20545{
b4ba55a1 20546 struct attribute *type_attr;
b4ba55a1
JB
20547
20548 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20549 if (!type_attr)
20550 return NULL;
20551
673bfd45 20552 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
20553}
20554
20555/* If DIE has a descriptive_type attribute, then set the TYPE's
20556 descriptive type accordingly. */
20557
20558static void
20559set_descriptive_type (struct type *type, struct die_info *die,
20560 struct dwarf2_cu *cu)
20561{
20562 struct type *descriptive_type = die_descriptive_type (die, cu);
20563
20564 if (descriptive_type)
20565 {
20566 ALLOCATE_GNAT_AUX_TYPE (type);
20567 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20568 }
20569}
20570
c906108c
SS
20571/* Return the containing type of the die in question using its
20572 DW_AT_containing_type attribute. */
20573
20574static struct type *
e7c27a73 20575die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20576{
c906108c 20577 struct attribute *type_attr;
c906108c 20578
e142c38c 20579 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
20580 if (!type_attr)
20581 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 20582 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 20583
673bfd45 20584 return lookup_die_type (die, type_attr, cu);
c906108c
SS
20585}
20586
ac9ec31b
DE
20587/* Return an error marker type to use for the ill formed type in DIE/CU. */
20588
20589static struct type *
20590build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20591{
20592 struct objfile *objfile = dwarf2_per_objfile->objfile;
20593 char *message, *saved;
20594
20595 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 20596 objfile_name (objfile),
9c541725
PA
20597 to_underlying (cu->header.sect_off),
20598 to_underlying (die->sect_off));
224c3ddb
SM
20599 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
20600 message, strlen (message));
ac9ec31b
DE
20601 xfree (message);
20602
19f392bc 20603 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
20604}
20605
673bfd45 20606/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
20607 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20608 DW_AT_containing_type.
673bfd45
DE
20609 If there is no type substitute an error marker. */
20610
c906108c 20611static struct type *
ff39bb5e 20612lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 20613 struct dwarf2_cu *cu)
c906108c 20614{
bb5ed363 20615 struct objfile *objfile = cu->objfile;
f792889a
DJ
20616 struct type *this_type;
20617
ac9ec31b
DE
20618 gdb_assert (attr->name == DW_AT_type
20619 || attr->name == DW_AT_GNAT_descriptive_type
20620 || attr->name == DW_AT_containing_type);
20621
673bfd45
DE
20622 /* First see if we have it cached. */
20623
36586728
TT
20624 if (attr->form == DW_FORM_GNU_ref_alt)
20625 {
20626 struct dwarf2_per_cu_data *per_cu;
9c541725 20627 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 20628
9c541725
PA
20629 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
20630 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 20631 }
7771576e 20632 else if (attr_form_is_ref (attr))
673bfd45 20633 {
9c541725 20634 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 20635
9c541725 20636 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 20637 }
55f1336d 20638 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 20639 {
ac9ec31b 20640 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 20641
ac9ec31b 20642 return get_signatured_type (die, signature, cu);
673bfd45
DE
20643 }
20644 else
20645 {
ac9ec31b
DE
20646 complaint (&symfile_complaints,
20647 _("Dwarf Error: Bad type attribute %s in DIE"
20648 " at 0x%x [in module %s]"),
9c541725 20649 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
4262abfb 20650 objfile_name (objfile));
ac9ec31b 20651 return build_error_marker_type (cu, die);
673bfd45
DE
20652 }
20653
20654 /* If not cached we need to read it in. */
20655
20656 if (this_type == NULL)
20657 {
ac9ec31b 20658 struct die_info *type_die = NULL;
673bfd45
DE
20659 struct dwarf2_cu *type_cu = cu;
20660
7771576e 20661 if (attr_form_is_ref (attr))
ac9ec31b
DE
20662 type_die = follow_die_ref (die, attr, &type_cu);
20663 if (type_die == NULL)
20664 return build_error_marker_type (cu, die);
20665 /* If we find the type now, it's probably because the type came
3019eac3
DE
20666 from an inter-CU reference and the type's CU got expanded before
20667 ours. */
ac9ec31b 20668 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
20669 }
20670
20671 /* If we still don't have a type use an error marker. */
20672
20673 if (this_type == NULL)
ac9ec31b 20674 return build_error_marker_type (cu, die);
673bfd45 20675
f792889a 20676 return this_type;
c906108c
SS
20677}
20678
673bfd45
DE
20679/* Return the type in DIE, CU.
20680 Returns NULL for invalid types.
20681
02142a6c 20682 This first does a lookup in die_type_hash,
673bfd45
DE
20683 and only reads the die in if necessary.
20684
20685 NOTE: This can be called when reading in partial or full symbols. */
20686
f792889a 20687static struct type *
e7c27a73 20688read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20689{
f792889a
DJ
20690 struct type *this_type;
20691
20692 this_type = get_die_type (die, cu);
20693 if (this_type)
20694 return this_type;
20695
673bfd45
DE
20696 return read_type_die_1 (die, cu);
20697}
20698
20699/* Read the type in DIE, CU.
20700 Returns NULL for invalid types. */
20701
20702static struct type *
20703read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20704{
20705 struct type *this_type = NULL;
20706
c906108c
SS
20707 switch (die->tag)
20708 {
20709 case DW_TAG_class_type:
680b30c7 20710 case DW_TAG_interface_type:
c906108c
SS
20711 case DW_TAG_structure_type:
20712 case DW_TAG_union_type:
f792889a 20713 this_type = read_structure_type (die, cu);
c906108c
SS
20714 break;
20715 case DW_TAG_enumeration_type:
f792889a 20716 this_type = read_enumeration_type (die, cu);
c906108c
SS
20717 break;
20718 case DW_TAG_subprogram:
20719 case DW_TAG_subroutine_type:
edb3359d 20720 case DW_TAG_inlined_subroutine:
f792889a 20721 this_type = read_subroutine_type (die, cu);
c906108c
SS
20722 break;
20723 case DW_TAG_array_type:
f792889a 20724 this_type = read_array_type (die, cu);
c906108c 20725 break;
72019c9c 20726 case DW_TAG_set_type:
f792889a 20727 this_type = read_set_type (die, cu);
72019c9c 20728 break;
c906108c 20729 case DW_TAG_pointer_type:
f792889a 20730 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
20731 break;
20732 case DW_TAG_ptr_to_member_type:
f792889a 20733 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
20734 break;
20735 case DW_TAG_reference_type:
4297a3f0
AV
20736 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20737 break;
20738 case DW_TAG_rvalue_reference_type:
20739 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
20740 break;
20741 case DW_TAG_const_type:
f792889a 20742 this_type = read_tag_const_type (die, cu);
c906108c
SS
20743 break;
20744 case DW_TAG_volatile_type:
f792889a 20745 this_type = read_tag_volatile_type (die, cu);
c906108c 20746 break;
06d66ee9
TT
20747 case DW_TAG_restrict_type:
20748 this_type = read_tag_restrict_type (die, cu);
20749 break;
c906108c 20750 case DW_TAG_string_type:
f792889a 20751 this_type = read_tag_string_type (die, cu);
c906108c
SS
20752 break;
20753 case DW_TAG_typedef:
f792889a 20754 this_type = read_typedef (die, cu);
c906108c 20755 break;
a02abb62 20756 case DW_TAG_subrange_type:
f792889a 20757 this_type = read_subrange_type (die, cu);
a02abb62 20758 break;
c906108c 20759 case DW_TAG_base_type:
f792889a 20760 this_type = read_base_type (die, cu);
c906108c 20761 break;
81a17f79 20762 case DW_TAG_unspecified_type:
f792889a 20763 this_type = read_unspecified_type (die, cu);
81a17f79 20764 break;
0114d602
DJ
20765 case DW_TAG_namespace:
20766 this_type = read_namespace_type (die, cu);
20767 break;
f55ee35c
JK
20768 case DW_TAG_module:
20769 this_type = read_module_type (die, cu);
20770 break;
a2c2acaf
MW
20771 case DW_TAG_atomic_type:
20772 this_type = read_tag_atomic_type (die, cu);
20773 break;
c906108c 20774 default:
3e43a32a
MS
20775 complaint (&symfile_complaints,
20776 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 20777 dwarf_tag_name (die->tag));
c906108c
SS
20778 break;
20779 }
63d06c5c 20780
f792889a 20781 return this_type;
63d06c5c
DC
20782}
20783
abc72ce4
DE
20784/* See if we can figure out if the class lives in a namespace. We do
20785 this by looking for a member function; its demangled name will
20786 contain namespace info, if there is any.
20787 Return the computed name or NULL.
20788 Space for the result is allocated on the objfile's obstack.
20789 This is the full-die version of guess_partial_die_structure_name.
20790 In this case we know DIE has no useful parent. */
20791
20792static char *
20793guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20794{
20795 struct die_info *spec_die;
20796 struct dwarf2_cu *spec_cu;
20797 struct die_info *child;
20798
20799 spec_cu = cu;
20800 spec_die = die_specification (die, &spec_cu);
20801 if (spec_die != NULL)
20802 {
20803 die = spec_die;
20804 cu = spec_cu;
20805 }
20806
20807 for (child = die->child;
20808 child != NULL;
20809 child = child->sibling)
20810 {
20811 if (child->tag == DW_TAG_subprogram)
20812 {
73b9be8b 20813 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 20814
7d45c7c3 20815 if (linkage_name != NULL)
abc72ce4
DE
20816 {
20817 char *actual_name
20818 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 20819 linkage_name);
abc72ce4
DE
20820 char *name = NULL;
20821
20822 if (actual_name != NULL)
20823 {
15d034d0 20824 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
20825
20826 if (die_name != NULL
20827 && strcmp (die_name, actual_name) != 0)
20828 {
20829 /* Strip off the class name from the full name.
20830 We want the prefix. */
20831 int die_name_len = strlen (die_name);
20832 int actual_name_len = strlen (actual_name);
20833
20834 /* Test for '::' as a sanity check. */
20835 if (actual_name_len > die_name_len + 2
3e43a32a
MS
20836 && actual_name[actual_name_len
20837 - die_name_len - 1] == ':')
224c3ddb
SM
20838 name = (char *) obstack_copy0 (
20839 &cu->objfile->per_bfd->storage_obstack,
20840 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
20841 }
20842 }
20843 xfree (actual_name);
20844 return name;
20845 }
20846 }
20847 }
20848
20849 return NULL;
20850}
20851
96408a79
SA
20852/* GCC might emit a nameless typedef that has a linkage name. Determine the
20853 prefix part in such case. See
20854 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20855
a121b7c1 20856static const char *
96408a79
SA
20857anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
20858{
20859 struct attribute *attr;
e6a959d6 20860 const char *base;
96408a79
SA
20861
20862 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
20863 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
20864 return NULL;
20865
7d45c7c3 20866 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
20867 return NULL;
20868
73b9be8b 20869 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
20870 if (attr == NULL || DW_STRING (attr) == NULL)
20871 return NULL;
20872
20873 /* dwarf2_name had to be already called. */
20874 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20875
20876 /* Strip the base name, keep any leading namespaces/classes. */
20877 base = strrchr (DW_STRING (attr), ':');
20878 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20879 return "";
20880
224c3ddb
SM
20881 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20882 DW_STRING (attr),
20883 &base[-1] - DW_STRING (attr));
96408a79
SA
20884}
20885
fdde2d81 20886/* Return the name of the namespace/class that DIE is defined within,
0114d602 20887 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 20888
0114d602
DJ
20889 For example, if we're within the method foo() in the following
20890 code:
20891
20892 namespace N {
20893 class C {
20894 void foo () {
20895 }
20896 };
20897 }
20898
20899 then determine_prefix on foo's die will return "N::C". */
fdde2d81 20900
0d5cff50 20901static const char *
e142c38c 20902determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 20903{
0114d602
DJ
20904 struct die_info *parent, *spec_die;
20905 struct dwarf2_cu *spec_cu;
20906 struct type *parent_type;
a121b7c1 20907 const char *retval;
63d06c5c 20908
9c37b5ae 20909 if (cu->language != language_cplus
c44af4eb
TT
20910 && cu->language != language_fortran && cu->language != language_d
20911 && cu->language != language_rust)
0114d602
DJ
20912 return "";
20913
96408a79
SA
20914 retval = anonymous_struct_prefix (die, cu);
20915 if (retval)
20916 return retval;
20917
0114d602
DJ
20918 /* We have to be careful in the presence of DW_AT_specification.
20919 For example, with GCC 3.4, given the code
20920
20921 namespace N {
20922 void foo() {
20923 // Definition of N::foo.
20924 }
20925 }
20926
20927 then we'll have a tree of DIEs like this:
20928
20929 1: DW_TAG_compile_unit
20930 2: DW_TAG_namespace // N
20931 3: DW_TAG_subprogram // declaration of N::foo
20932 4: DW_TAG_subprogram // definition of N::foo
20933 DW_AT_specification // refers to die #3
20934
20935 Thus, when processing die #4, we have to pretend that we're in
20936 the context of its DW_AT_specification, namely the contex of die
20937 #3. */
20938 spec_cu = cu;
20939 spec_die = die_specification (die, &spec_cu);
20940 if (spec_die == NULL)
20941 parent = die->parent;
20942 else
63d06c5c 20943 {
0114d602
DJ
20944 parent = spec_die->parent;
20945 cu = spec_cu;
63d06c5c 20946 }
0114d602
DJ
20947
20948 if (parent == NULL)
20949 return "";
98bfdba5
PA
20950 else if (parent->building_fullname)
20951 {
20952 const char *name;
20953 const char *parent_name;
20954
20955 /* It has been seen on RealView 2.2 built binaries,
20956 DW_TAG_template_type_param types actually _defined_ as
20957 children of the parent class:
20958
20959 enum E {};
20960 template class <class Enum> Class{};
20961 Class<enum E> class_e;
20962
20963 1: DW_TAG_class_type (Class)
20964 2: DW_TAG_enumeration_type (E)
20965 3: DW_TAG_enumerator (enum1:0)
20966 3: DW_TAG_enumerator (enum2:1)
20967 ...
20968 2: DW_TAG_template_type_param
20969 DW_AT_type DW_FORM_ref_udata (E)
20970
20971 Besides being broken debug info, it can put GDB into an
20972 infinite loop. Consider:
20973
20974 When we're building the full name for Class<E>, we'll start
20975 at Class, and go look over its template type parameters,
20976 finding E. We'll then try to build the full name of E, and
20977 reach here. We're now trying to build the full name of E,
20978 and look over the parent DIE for containing scope. In the
20979 broken case, if we followed the parent DIE of E, we'd again
20980 find Class, and once again go look at its template type
20981 arguments, etc., etc. Simply don't consider such parent die
20982 as source-level parent of this die (it can't be, the language
20983 doesn't allow it), and break the loop here. */
20984 name = dwarf2_name (die, cu);
20985 parent_name = dwarf2_name (parent, cu);
20986 complaint (&symfile_complaints,
20987 _("template param type '%s' defined within parent '%s'"),
20988 name ? name : "<unknown>",
20989 parent_name ? parent_name : "<unknown>");
20990 return "";
20991 }
63d06c5c 20992 else
0114d602
DJ
20993 switch (parent->tag)
20994 {
63d06c5c 20995 case DW_TAG_namespace:
0114d602 20996 parent_type = read_type_die (parent, cu);
acebe513
UW
20997 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
20998 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
20999 Work around this problem here. */
21000 if (cu->language == language_cplus
21001 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
21002 return "";
0114d602
DJ
21003 /* We give a name to even anonymous namespaces. */
21004 return TYPE_TAG_NAME (parent_type);
63d06c5c 21005 case DW_TAG_class_type:
680b30c7 21006 case DW_TAG_interface_type:
63d06c5c 21007 case DW_TAG_structure_type:
0114d602 21008 case DW_TAG_union_type:
f55ee35c 21009 case DW_TAG_module:
0114d602
DJ
21010 parent_type = read_type_die (parent, cu);
21011 if (TYPE_TAG_NAME (parent_type) != NULL)
21012 return TYPE_TAG_NAME (parent_type);
21013 else
21014 /* An anonymous structure is only allowed non-static data
21015 members; no typedefs, no member functions, et cetera.
21016 So it does not need a prefix. */
21017 return "";
abc72ce4 21018 case DW_TAG_compile_unit:
95554aad 21019 case DW_TAG_partial_unit:
abc72ce4
DE
21020 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21021 if (cu->language == language_cplus
8b70b953 21022 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
21023 && die->child != NULL
21024 && (die->tag == DW_TAG_class_type
21025 || die->tag == DW_TAG_structure_type
21026 || die->tag == DW_TAG_union_type))
21027 {
21028 char *name = guess_full_die_structure_name (die, cu);
21029 if (name != NULL)
21030 return name;
21031 }
21032 return "";
3d567982
TT
21033 case DW_TAG_enumeration_type:
21034 parent_type = read_type_die (parent, cu);
21035 if (TYPE_DECLARED_CLASS (parent_type))
21036 {
21037 if (TYPE_TAG_NAME (parent_type) != NULL)
21038 return TYPE_TAG_NAME (parent_type);
21039 return "";
21040 }
21041 /* Fall through. */
63d06c5c 21042 default:
8176b9b8 21043 return determine_prefix (parent, cu);
63d06c5c 21044 }
63d06c5c
DC
21045}
21046
3e43a32a
MS
21047/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21048 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21049 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21050 an obconcat, otherwise allocate storage for the result. The CU argument is
21051 used to determine the language and hence, the appropriate separator. */
987504bb 21052
f55ee35c 21053#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21054
21055static char *
f55ee35c
JK
21056typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21057 int physname, struct dwarf2_cu *cu)
63d06c5c 21058{
f55ee35c 21059 const char *lead = "";
5c315b68 21060 const char *sep;
63d06c5c 21061
3e43a32a
MS
21062 if (suffix == NULL || suffix[0] == '\0'
21063 || prefix == NULL || prefix[0] == '\0')
987504bb 21064 sep = "";
45280282
IB
21065 else if (cu->language == language_d)
21066 {
21067 /* For D, the 'main' function could be defined in any module, but it
21068 should never be prefixed. */
21069 if (strcmp (suffix, "D main") == 0)
21070 {
21071 prefix = "";
21072 sep = "";
21073 }
21074 else
21075 sep = ".";
21076 }
f55ee35c
JK
21077 else if (cu->language == language_fortran && physname)
21078 {
21079 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21080 DW_AT_MIPS_linkage_name is preferred and used instead. */
21081
21082 lead = "__";
21083 sep = "_MOD_";
21084 }
987504bb
JJ
21085 else
21086 sep = "::";
63d06c5c 21087
6dd47d34
DE
21088 if (prefix == NULL)
21089 prefix = "";
21090 if (suffix == NULL)
21091 suffix = "";
21092
987504bb
JJ
21093 if (obs == NULL)
21094 {
3e43a32a 21095 char *retval
224c3ddb
SM
21096 = ((char *)
21097 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21098
f55ee35c
JK
21099 strcpy (retval, lead);
21100 strcat (retval, prefix);
6dd47d34
DE
21101 strcat (retval, sep);
21102 strcat (retval, suffix);
63d06c5c
DC
21103 return retval;
21104 }
987504bb
JJ
21105 else
21106 {
21107 /* We have an obstack. */
f55ee35c 21108 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21109 }
63d06c5c
DC
21110}
21111
c906108c
SS
21112/* Return sibling of die, NULL if no sibling. */
21113
f9aca02d 21114static struct die_info *
fba45db2 21115sibling_die (struct die_info *die)
c906108c 21116{
639d11d3 21117 return die->sibling;
c906108c
SS
21118}
21119
71c25dea
TT
21120/* Get name of a die, return NULL if not found. */
21121
15d034d0
TT
21122static const char *
21123dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
21124 struct obstack *obstack)
21125{
21126 if (name && cu->language == language_cplus)
21127 {
2f408ecb 21128 std::string canon_name = cp_canonicalize_string (name);
71c25dea 21129
2f408ecb 21130 if (!canon_name.empty ())
71c25dea 21131 {
2f408ecb
PA
21132 if (canon_name != name)
21133 name = (const char *) obstack_copy0 (obstack,
21134 canon_name.c_str (),
21135 canon_name.length ());
71c25dea
TT
21136 }
21137 }
21138
21139 return name;
c906108c
SS
21140}
21141
96553a0c
DE
21142/* Get name of a die, return NULL if not found.
21143 Anonymous namespaces are converted to their magic string. */
9219021c 21144
15d034d0 21145static const char *
e142c38c 21146dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21147{
21148 struct attribute *attr;
21149
e142c38c 21150 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21151 if ((!attr || !DW_STRING (attr))
96553a0c 21152 && die->tag != DW_TAG_namespace
53832f31
TT
21153 && die->tag != DW_TAG_class_type
21154 && die->tag != DW_TAG_interface_type
21155 && die->tag != DW_TAG_structure_type
21156 && die->tag != DW_TAG_union_type)
71c25dea
TT
21157 return NULL;
21158
21159 switch (die->tag)
21160 {
21161 case DW_TAG_compile_unit:
95554aad 21162 case DW_TAG_partial_unit:
71c25dea
TT
21163 /* Compilation units have a DW_AT_name that is a filename, not
21164 a source language identifier. */
21165 case DW_TAG_enumeration_type:
21166 case DW_TAG_enumerator:
21167 /* These tags always have simple identifiers already; no need
21168 to canonicalize them. */
21169 return DW_STRING (attr);
907af001 21170
96553a0c
DE
21171 case DW_TAG_namespace:
21172 if (attr != NULL && DW_STRING (attr) != NULL)
21173 return DW_STRING (attr);
21174 return CP_ANONYMOUS_NAMESPACE_STR;
21175
907af001
UW
21176 case DW_TAG_class_type:
21177 case DW_TAG_interface_type:
21178 case DW_TAG_structure_type:
21179 case DW_TAG_union_type:
21180 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21181 structures or unions. These were of the form "._%d" in GCC 4.1,
21182 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21183 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21184 if (attr && DW_STRING (attr)
61012eef
GB
21185 && (startswith (DW_STRING (attr), "._")
21186 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21187 return NULL;
53832f31
TT
21188
21189 /* GCC might emit a nameless typedef that has a linkage name. See
21190 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21191 if (!attr || DW_STRING (attr) == NULL)
21192 {
df5c6c50 21193 char *demangled = NULL;
53832f31 21194
73b9be8b 21195 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21196 if (attr == NULL || DW_STRING (attr) == NULL)
21197 return NULL;
21198
df5c6c50
JK
21199 /* Avoid demangling DW_STRING (attr) the second time on a second
21200 call for the same DIE. */
21201 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 21202 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
21203
21204 if (demangled)
21205 {
e6a959d6 21206 const char *base;
96408a79 21207
53832f31 21208 /* FIXME: we already did this for the partial symbol... */
34a68019 21209 DW_STRING (attr)
224c3ddb
SM
21210 = ((const char *)
21211 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
21212 demangled, strlen (demangled)));
53832f31
TT
21213 DW_STRING_IS_CANONICAL (attr) = 1;
21214 xfree (demangled);
96408a79
SA
21215
21216 /* Strip any leading namespaces/classes, keep only the base name.
21217 DW_AT_name for named DIEs does not contain the prefixes. */
21218 base = strrchr (DW_STRING (attr), ':');
21219 if (base && base > DW_STRING (attr) && base[-1] == ':')
21220 return &base[1];
21221 else
21222 return DW_STRING (attr);
53832f31
TT
21223 }
21224 }
907af001
UW
21225 break;
21226
71c25dea 21227 default:
907af001
UW
21228 break;
21229 }
21230
21231 if (!DW_STRING_IS_CANONICAL (attr))
21232 {
21233 DW_STRING (attr)
21234 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 21235 &cu->objfile->per_bfd->storage_obstack);
907af001 21236 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21237 }
907af001 21238 return DW_STRING (attr);
9219021c
DC
21239}
21240
21241/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21242 is none. *EXT_CU is the CU containing DIE on input, and the CU
21243 containing the return value on output. */
9219021c
DC
21244
21245static struct die_info *
f2f0e013 21246dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21247{
21248 struct attribute *attr;
9219021c 21249
f2f0e013 21250 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21251 if (attr == NULL)
21252 return NULL;
21253
f2f0e013 21254 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21255}
21256
c906108c
SS
21257/* Convert a DIE tag into its string name. */
21258
f39c6ffd 21259static const char *
aa1ee363 21260dwarf_tag_name (unsigned tag)
c906108c 21261{
f39c6ffd
TT
21262 const char *name = get_DW_TAG_name (tag);
21263
21264 if (name == NULL)
21265 return "DW_TAG_<unknown>";
21266
21267 return name;
c906108c
SS
21268}
21269
21270/* Convert a DWARF attribute code into its string name. */
21271
f39c6ffd 21272static const char *
aa1ee363 21273dwarf_attr_name (unsigned attr)
c906108c 21274{
f39c6ffd
TT
21275 const char *name;
21276
c764a876 21277#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
21278 if (attr == DW_AT_MIPS_fde)
21279 return "DW_AT_MIPS_fde";
21280#else
21281 if (attr == DW_AT_HP_block_index)
21282 return "DW_AT_HP_block_index";
c764a876 21283#endif
f39c6ffd
TT
21284
21285 name = get_DW_AT_name (attr);
21286
21287 if (name == NULL)
21288 return "DW_AT_<unknown>";
21289
21290 return name;
c906108c
SS
21291}
21292
21293/* Convert a DWARF value form code into its string name. */
21294
f39c6ffd 21295static const char *
aa1ee363 21296dwarf_form_name (unsigned form)
c906108c 21297{
f39c6ffd
TT
21298 const char *name = get_DW_FORM_name (form);
21299
21300 if (name == NULL)
21301 return "DW_FORM_<unknown>";
21302
21303 return name;
c906108c
SS
21304}
21305
a121b7c1 21306static const char *
fba45db2 21307dwarf_bool_name (unsigned mybool)
c906108c
SS
21308{
21309 if (mybool)
21310 return "TRUE";
21311 else
21312 return "FALSE";
21313}
21314
21315/* Convert a DWARF type code into its string name. */
21316
f39c6ffd 21317static const char *
aa1ee363 21318dwarf_type_encoding_name (unsigned enc)
c906108c 21319{
f39c6ffd 21320 const char *name = get_DW_ATE_name (enc);
c906108c 21321
f39c6ffd
TT
21322 if (name == NULL)
21323 return "DW_ATE_<unknown>";
c906108c 21324
f39c6ffd 21325 return name;
c906108c 21326}
c906108c 21327
f9aca02d 21328static void
d97bc12b 21329dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21330{
21331 unsigned int i;
21332
d97bc12b
DE
21333 print_spaces (indent, f);
21334 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
9c541725
PA
21335 dwarf_tag_name (die->tag), die->abbrev,
21336 to_underlying (die->sect_off));
d97bc12b
DE
21337
21338 if (die->parent != NULL)
21339 {
21340 print_spaces (indent, f);
21341 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9c541725 21342 to_underlying (die->parent->sect_off));
d97bc12b
DE
21343 }
21344
21345 print_spaces (indent, f);
21346 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21347 dwarf_bool_name (die->child != NULL));
c906108c 21348
d97bc12b
DE
21349 print_spaces (indent, f);
21350 fprintf_unfiltered (f, " attributes:\n");
21351
c906108c
SS
21352 for (i = 0; i < die->num_attrs; ++i)
21353 {
d97bc12b
DE
21354 print_spaces (indent, f);
21355 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21356 dwarf_attr_name (die->attrs[i].name),
21357 dwarf_form_name (die->attrs[i].form));
d97bc12b 21358
c906108c
SS
21359 switch (die->attrs[i].form)
21360 {
c906108c 21361 case DW_FORM_addr:
3019eac3 21362 case DW_FORM_GNU_addr_index:
d97bc12b 21363 fprintf_unfiltered (f, "address: ");
5af949e3 21364 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
21365 break;
21366 case DW_FORM_block2:
21367 case DW_FORM_block4:
21368 case DW_FORM_block:
21369 case DW_FORM_block1:
56eb65bd
SP
21370 fprintf_unfiltered (f, "block: size %s",
21371 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 21372 break;
2dc7f7b3 21373 case DW_FORM_exprloc:
56eb65bd
SP
21374 fprintf_unfiltered (f, "expression: size %s",
21375 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 21376 break;
0224619f
JK
21377 case DW_FORM_data16:
21378 fprintf_unfiltered (f, "constant of 16 bytes");
21379 break;
4568ecf9
DE
21380 case DW_FORM_ref_addr:
21381 fprintf_unfiltered (f, "ref address: ");
21382 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21383 break;
36586728
TT
21384 case DW_FORM_GNU_ref_alt:
21385 fprintf_unfiltered (f, "alt ref address: ");
21386 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21387 break;
10b3939b
DJ
21388 case DW_FORM_ref1:
21389 case DW_FORM_ref2:
21390 case DW_FORM_ref4:
4568ecf9
DE
21391 case DW_FORM_ref8:
21392 case DW_FORM_ref_udata:
d97bc12b 21393 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 21394 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 21395 break;
c906108c
SS
21396 case DW_FORM_data1:
21397 case DW_FORM_data2:
21398 case DW_FORM_data4:
ce5d95e1 21399 case DW_FORM_data8:
c906108c
SS
21400 case DW_FORM_udata:
21401 case DW_FORM_sdata:
43bbcdc2
PH
21402 fprintf_unfiltered (f, "constant: %s",
21403 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 21404 break;
2dc7f7b3
TT
21405 case DW_FORM_sec_offset:
21406 fprintf_unfiltered (f, "section offset: %s",
21407 pulongest (DW_UNSND (&die->attrs[i])));
21408 break;
55f1336d 21409 case DW_FORM_ref_sig8:
ac9ec31b
DE
21410 fprintf_unfiltered (f, "signature: %s",
21411 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 21412 break;
c906108c 21413 case DW_FORM_string:
4bdf3d34 21414 case DW_FORM_strp:
43988095 21415 case DW_FORM_line_strp:
3019eac3 21416 case DW_FORM_GNU_str_index:
36586728 21417 case DW_FORM_GNU_strp_alt:
8285870a 21418 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 21419 DW_STRING (&die->attrs[i])
8285870a
JK
21420 ? DW_STRING (&die->attrs[i]) : "",
21421 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
21422 break;
21423 case DW_FORM_flag:
21424 if (DW_UNSND (&die->attrs[i]))
d97bc12b 21425 fprintf_unfiltered (f, "flag: TRUE");
c906108c 21426 else
d97bc12b 21427 fprintf_unfiltered (f, "flag: FALSE");
c906108c 21428 break;
2dc7f7b3
TT
21429 case DW_FORM_flag_present:
21430 fprintf_unfiltered (f, "flag: TRUE");
21431 break;
a8329558 21432 case DW_FORM_indirect:
0963b4bd
MS
21433 /* The reader will have reduced the indirect form to
21434 the "base form" so this form should not occur. */
3e43a32a
MS
21435 fprintf_unfiltered (f,
21436 "unexpected attribute form: DW_FORM_indirect");
a8329558 21437 break;
663c44ac
JK
21438 case DW_FORM_implicit_const:
21439 fprintf_unfiltered (f, "constant: %s",
21440 plongest (DW_SND (&die->attrs[i])));
21441 break;
c906108c 21442 default:
d97bc12b 21443 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 21444 die->attrs[i].form);
d97bc12b 21445 break;
c906108c 21446 }
d97bc12b 21447 fprintf_unfiltered (f, "\n");
c906108c
SS
21448 }
21449}
21450
f9aca02d 21451static void
d97bc12b 21452dump_die_for_error (struct die_info *die)
c906108c 21453{
d97bc12b
DE
21454 dump_die_shallow (gdb_stderr, 0, die);
21455}
21456
21457static void
21458dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21459{
21460 int indent = level * 4;
21461
21462 gdb_assert (die != NULL);
21463
21464 if (level >= max_level)
21465 return;
21466
21467 dump_die_shallow (f, indent, die);
21468
21469 if (die->child != NULL)
c906108c 21470 {
d97bc12b
DE
21471 print_spaces (indent, f);
21472 fprintf_unfiltered (f, " Children:");
21473 if (level + 1 < max_level)
21474 {
21475 fprintf_unfiltered (f, "\n");
21476 dump_die_1 (f, level + 1, max_level, die->child);
21477 }
21478 else
21479 {
3e43a32a
MS
21480 fprintf_unfiltered (f,
21481 " [not printed, max nesting level reached]\n");
d97bc12b
DE
21482 }
21483 }
21484
21485 if (die->sibling != NULL && level > 0)
21486 {
21487 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
21488 }
21489}
21490
d97bc12b
DE
21491/* This is called from the pdie macro in gdbinit.in.
21492 It's not static so gcc will keep a copy callable from gdb. */
21493
21494void
21495dump_die (struct die_info *die, int max_level)
21496{
21497 dump_die_1 (gdb_stdlog, 0, max_level, die);
21498}
21499
f9aca02d 21500static void
51545339 21501store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21502{
51545339 21503 void **slot;
c906108c 21504
9c541725
PA
21505 slot = htab_find_slot_with_hash (cu->die_hash, die,
21506 to_underlying (die->sect_off),
b64f50a1 21507 INSERT);
51545339
DJ
21508
21509 *slot = die;
c906108c
SS
21510}
21511
b64f50a1
JK
21512/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
21513 required kind. */
21514
21515static sect_offset
ff39bb5e 21516dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 21517{
7771576e 21518 if (attr_form_is_ref (attr))
9c541725 21519 return (sect_offset) DW_UNSND (attr);
93311388
DE
21520
21521 complaint (&symfile_complaints,
21522 _("unsupported die ref attribute form: '%s'"),
21523 dwarf_form_name (attr->form));
9c541725 21524 return {};
c906108c
SS
21525}
21526
43bbcdc2
PH
21527/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
21528 * the value held by the attribute is not constant. */
a02abb62 21529
43bbcdc2 21530static LONGEST
ff39bb5e 21531dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 21532{
663c44ac 21533 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
21534 return DW_SND (attr);
21535 else if (attr->form == DW_FORM_udata
21536 || attr->form == DW_FORM_data1
21537 || attr->form == DW_FORM_data2
21538 || attr->form == DW_FORM_data4
21539 || attr->form == DW_FORM_data8)
21540 return DW_UNSND (attr);
21541 else
21542 {
0224619f 21543 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
21544 complaint (&symfile_complaints,
21545 _("Attribute value is not a constant (%s)"),
a02abb62
JB
21546 dwarf_form_name (attr->form));
21547 return default_value;
21548 }
21549}
21550
348e048f
DE
21551/* Follow reference or signature attribute ATTR of SRC_DIE.
21552 On entry *REF_CU is the CU of SRC_DIE.
21553 On exit *REF_CU is the CU of the result. */
21554
21555static struct die_info *
ff39bb5e 21556follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
21557 struct dwarf2_cu **ref_cu)
21558{
21559 struct die_info *die;
21560
7771576e 21561 if (attr_form_is_ref (attr))
348e048f 21562 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 21563 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
21564 die = follow_die_sig (src_die, attr, ref_cu);
21565 else
21566 {
21567 dump_die_for_error (src_die);
21568 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 21569 objfile_name ((*ref_cu)->objfile));
348e048f
DE
21570 }
21571
21572 return die;
03dd20cc
DJ
21573}
21574
5c631832 21575/* Follow reference OFFSET.
673bfd45
DE
21576 On entry *REF_CU is the CU of the source die referencing OFFSET.
21577 On exit *REF_CU is the CU of the result.
21578 Returns NULL if OFFSET is invalid. */
f504f079 21579
f9aca02d 21580static struct die_info *
9c541725 21581follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 21582 struct dwarf2_cu **ref_cu)
c906108c 21583{
10b3939b 21584 struct die_info temp_die;
f2f0e013 21585 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 21586
348e048f
DE
21587 gdb_assert (cu->per_cu != NULL);
21588
98bfdba5
PA
21589 target_cu = cu;
21590
3019eac3 21591 if (cu->per_cu->is_debug_types)
348e048f
DE
21592 {
21593 /* .debug_types CUs cannot reference anything outside their CU.
21594 If they need to, they have to reference a signatured type via
55f1336d 21595 DW_FORM_ref_sig8. */
9c541725 21596 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 21597 return NULL;
348e048f 21598 }
36586728 21599 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 21600 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
21601 {
21602 struct dwarf2_per_cu_data *per_cu;
9a619af0 21603
9c541725 21604 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 21605 cu->objfile);
03dd20cc
DJ
21606
21607 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
21608 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21609 load_full_comp_unit (per_cu, cu->language);
03dd20cc 21610
10b3939b
DJ
21611 target_cu = per_cu->cu;
21612 }
98bfdba5
PA
21613 else if (cu->dies == NULL)
21614 {
21615 /* We're loading full DIEs during partial symbol reading. */
21616 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 21617 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 21618 }
c906108c 21619
f2f0e013 21620 *ref_cu = target_cu;
9c541725 21621 temp_die.sect_off = sect_off;
9a3c8263 21622 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
21623 &temp_die,
21624 to_underlying (sect_off));
5c631832 21625}
10b3939b 21626
5c631832
JK
21627/* Follow reference attribute ATTR of SRC_DIE.
21628 On entry *REF_CU is the CU of SRC_DIE.
21629 On exit *REF_CU is the CU of the result. */
21630
21631static struct die_info *
ff39bb5e 21632follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
21633 struct dwarf2_cu **ref_cu)
21634{
9c541725 21635 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
21636 struct dwarf2_cu *cu = *ref_cu;
21637 struct die_info *die;
21638
9c541725 21639 die = follow_die_offset (sect_off,
36586728
TT
21640 (attr->form == DW_FORM_GNU_ref_alt
21641 || cu->per_cu->is_dwz),
21642 ref_cu);
5c631832
JK
21643 if (!die)
21644 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
21645 "at 0x%x [in module %s]"),
9c541725 21646 to_underlying (sect_off), to_underlying (src_die->sect_off),
4262abfb 21647 objfile_name (cu->objfile));
348e048f 21648
5c631832
JK
21649 return die;
21650}
21651
9c541725 21652/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b
JK
21653 Returned value is intended for DW_OP_call*. Returned
21654 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
21655
21656struct dwarf2_locexpr_baton
9c541725 21657dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
21658 struct dwarf2_per_cu_data *per_cu,
21659 CORE_ADDR (*get_frame_pc) (void *baton),
21660 void *baton)
5c631832 21661{
918dd910 21662 struct dwarf2_cu *cu;
5c631832
JK
21663 struct die_info *die;
21664 struct attribute *attr;
21665 struct dwarf2_locexpr_baton retval;
21666
8cf6f0b1
TT
21667 dw2_setup (per_cu->objfile);
21668
918dd910
JK
21669 if (per_cu->cu == NULL)
21670 load_cu (per_cu);
21671 cu = per_cu->cu;
cc12ce38
DE
21672 if (cu == NULL)
21673 {
21674 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21675 Instead just throw an error, not much else we can do. */
21676 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 21677 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 21678 }
918dd910 21679
9c541725 21680 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832
JK
21681 if (!die)
21682 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 21683 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
21684
21685 attr = dwarf2_attr (die, DW_AT_location, cu);
21686 if (!attr)
21687 {
e103e986
JK
21688 /* DWARF: "If there is no such attribute, then there is no effect.".
21689 DATA is ignored if SIZE is 0. */
5c631832 21690
e103e986 21691 retval.data = NULL;
5c631832
JK
21692 retval.size = 0;
21693 }
8cf6f0b1
TT
21694 else if (attr_form_is_section_offset (attr))
21695 {
21696 struct dwarf2_loclist_baton loclist_baton;
21697 CORE_ADDR pc = (*get_frame_pc) (baton);
21698 size_t size;
21699
21700 fill_in_loclist_baton (cu, &loclist_baton, attr);
21701
21702 retval.data = dwarf2_find_location_expression (&loclist_baton,
21703 &size, pc);
21704 retval.size = size;
21705 }
5c631832
JK
21706 else
21707 {
21708 if (!attr_form_is_block (attr))
21709 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
21710 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9c541725 21711 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
21712
21713 retval.data = DW_BLOCK (attr)->data;
21714 retval.size = DW_BLOCK (attr)->size;
21715 }
21716 retval.per_cu = cu->per_cu;
918dd910 21717
918dd910
JK
21718 age_cached_comp_units ();
21719
5c631832 21720 return retval;
348e048f
DE
21721}
21722
8b9737bf
TT
21723/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
21724 offset. */
21725
21726struct dwarf2_locexpr_baton
21727dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21728 struct dwarf2_per_cu_data *per_cu,
21729 CORE_ADDR (*get_frame_pc) (void *baton),
21730 void *baton)
21731{
9c541725 21732 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 21733
9c541725 21734 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
21735}
21736
b6807d98
TT
21737/* Write a constant of a given type as target-ordered bytes into
21738 OBSTACK. */
21739
21740static const gdb_byte *
21741write_constant_as_bytes (struct obstack *obstack,
21742 enum bfd_endian byte_order,
21743 struct type *type,
21744 ULONGEST value,
21745 LONGEST *len)
21746{
21747 gdb_byte *result;
21748
21749 *len = TYPE_LENGTH (type);
224c3ddb 21750 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
21751 store_unsigned_integer (result, *len, byte_order, value);
21752
21753 return result;
21754}
21755
21756/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
21757 pointer to the constant bytes and set LEN to the length of the
21758 data. If memory is needed, allocate it on OBSTACK. If the DIE
21759 does not have a DW_AT_const_value, return NULL. */
21760
21761const gdb_byte *
9c541725 21762dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
21763 struct dwarf2_per_cu_data *per_cu,
21764 struct obstack *obstack,
21765 LONGEST *len)
21766{
21767 struct dwarf2_cu *cu;
21768 struct die_info *die;
21769 struct attribute *attr;
21770 const gdb_byte *result = NULL;
21771 struct type *type;
21772 LONGEST value;
21773 enum bfd_endian byte_order;
21774
21775 dw2_setup (per_cu->objfile);
21776
21777 if (per_cu->cu == NULL)
21778 load_cu (per_cu);
21779 cu = per_cu->cu;
cc12ce38
DE
21780 if (cu == NULL)
21781 {
21782 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21783 Instead just throw an error, not much else we can do. */
21784 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 21785 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 21786 }
b6807d98 21787
9c541725 21788 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98
TT
21789 if (!die)
21790 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 21791 to_underlying (sect_off), objfile_name (per_cu->objfile));
b6807d98
TT
21792
21793
21794 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21795 if (attr == NULL)
21796 return NULL;
21797
21798 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
21799 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21800
21801 switch (attr->form)
21802 {
21803 case DW_FORM_addr:
21804 case DW_FORM_GNU_addr_index:
21805 {
21806 gdb_byte *tem;
21807
21808 *len = cu->header.addr_size;
224c3ddb 21809 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
21810 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
21811 result = tem;
21812 }
21813 break;
21814 case DW_FORM_string:
21815 case DW_FORM_strp:
21816 case DW_FORM_GNU_str_index:
21817 case DW_FORM_GNU_strp_alt:
21818 /* DW_STRING is already allocated on the objfile obstack, point
21819 directly to it. */
21820 result = (const gdb_byte *) DW_STRING (attr);
21821 *len = strlen (DW_STRING (attr));
21822 break;
21823 case DW_FORM_block1:
21824 case DW_FORM_block2:
21825 case DW_FORM_block4:
21826 case DW_FORM_block:
21827 case DW_FORM_exprloc:
0224619f 21828 case DW_FORM_data16:
b6807d98
TT
21829 result = DW_BLOCK (attr)->data;
21830 *len = DW_BLOCK (attr)->size;
21831 break;
21832
21833 /* The DW_AT_const_value attributes are supposed to carry the
21834 symbol's value "represented as it would be on the target
21835 architecture." By the time we get here, it's already been
21836 converted to host endianness, so we just need to sign- or
21837 zero-extend it as appropriate. */
21838 case DW_FORM_data1:
21839 type = die_type (die, cu);
21840 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
21841 if (result == NULL)
21842 result = write_constant_as_bytes (obstack, byte_order,
21843 type, value, len);
21844 break;
21845 case DW_FORM_data2:
21846 type = die_type (die, cu);
21847 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
21848 if (result == NULL)
21849 result = write_constant_as_bytes (obstack, byte_order,
21850 type, value, len);
21851 break;
21852 case DW_FORM_data4:
21853 type = die_type (die, cu);
21854 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
21855 if (result == NULL)
21856 result = write_constant_as_bytes (obstack, byte_order,
21857 type, value, len);
21858 break;
21859 case DW_FORM_data8:
21860 type = die_type (die, cu);
21861 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
21862 if (result == NULL)
21863 result = write_constant_as_bytes (obstack, byte_order,
21864 type, value, len);
21865 break;
21866
21867 case DW_FORM_sdata:
663c44ac 21868 case DW_FORM_implicit_const:
b6807d98
TT
21869 type = die_type (die, cu);
21870 result = write_constant_as_bytes (obstack, byte_order,
21871 type, DW_SND (attr), len);
21872 break;
21873
21874 case DW_FORM_udata:
21875 type = die_type (die, cu);
21876 result = write_constant_as_bytes (obstack, byte_order,
21877 type, DW_UNSND (attr), len);
21878 break;
21879
21880 default:
21881 complaint (&symfile_complaints,
21882 _("unsupported const value attribute form: '%s'"),
21883 dwarf_form_name (attr->form));
21884 break;
21885 }
21886
21887 return result;
21888}
21889
7942e96e
AA
21890/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
21891 valid type for this die is found. */
21892
21893struct type *
9c541725 21894dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
21895 struct dwarf2_per_cu_data *per_cu)
21896{
21897 struct dwarf2_cu *cu;
21898 struct die_info *die;
21899
21900 dw2_setup (per_cu->objfile);
21901
21902 if (per_cu->cu == NULL)
21903 load_cu (per_cu);
21904 cu = per_cu->cu;
21905 if (!cu)
21906 return NULL;
21907
9c541725 21908 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
21909 if (!die)
21910 return NULL;
21911
21912 return die_type (die, cu);
21913}
21914
8a9b8146
TT
21915/* Return the type of the DIE at DIE_OFFSET in the CU named by
21916 PER_CU. */
21917
21918struct type *
b64f50a1 21919dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
21920 struct dwarf2_per_cu_data *per_cu)
21921{
8a9b8146 21922 dw2_setup (per_cu->objfile);
b64f50a1 21923
9c541725 21924 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 21925 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
21926}
21927
ac9ec31b 21928/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 21929 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
21930 On exit *REF_CU is the CU of the result.
21931 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
21932
21933static struct die_info *
ac9ec31b
DE
21934follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21935 struct dwarf2_cu **ref_cu)
348e048f 21936{
348e048f 21937 struct die_info temp_die;
348e048f
DE
21938 struct dwarf2_cu *sig_cu;
21939 struct die_info *die;
21940
ac9ec31b
DE
21941 /* While it might be nice to assert sig_type->type == NULL here,
21942 we can get here for DW_AT_imported_declaration where we need
21943 the DIE not the type. */
348e048f
DE
21944
21945 /* If necessary, add it to the queue and load its DIEs. */
21946
95554aad 21947 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 21948 read_signatured_type (sig_type);
348e048f 21949
348e048f 21950 sig_cu = sig_type->per_cu.cu;
69d751e3 21951 gdb_assert (sig_cu != NULL);
9c541725
PA
21952 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
21953 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 21954 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 21955 to_underlying (temp_die.sect_off));
348e048f
DE
21956 if (die)
21957 {
796a7ff8
DE
21958 /* For .gdb_index version 7 keep track of included TUs.
21959 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
21960 if (dwarf2_per_objfile->index_table != NULL
21961 && dwarf2_per_objfile->index_table->version <= 7)
21962 {
21963 VEC_safe_push (dwarf2_per_cu_ptr,
21964 (*ref_cu)->per_cu->imported_symtabs,
21965 sig_cu->per_cu);
21966 }
21967
348e048f
DE
21968 *ref_cu = sig_cu;
21969 return die;
21970 }
21971
ac9ec31b
DE
21972 return NULL;
21973}
21974
21975/* Follow signatured type referenced by ATTR in SRC_DIE.
21976 On entry *REF_CU is the CU of SRC_DIE.
21977 On exit *REF_CU is the CU of the result.
21978 The result is the DIE of the type.
21979 If the referenced type cannot be found an error is thrown. */
21980
21981static struct die_info *
ff39bb5e 21982follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
21983 struct dwarf2_cu **ref_cu)
21984{
21985 ULONGEST signature = DW_SIGNATURE (attr);
21986 struct signatured_type *sig_type;
21987 struct die_info *die;
21988
21989 gdb_assert (attr->form == DW_FORM_ref_sig8);
21990
a2ce51a0 21991 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
21992 /* sig_type will be NULL if the signatured type is missing from
21993 the debug info. */
21994 if (sig_type == NULL)
21995 {
21996 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
21997 " from DIE at 0x%x [in module %s]"),
9c541725 21998 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 21999 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
22000 }
22001
22002 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22003 if (die == NULL)
22004 {
22005 dump_die_for_error (src_die);
22006 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22007 " from DIE at 0x%x [in module %s]"),
9c541725 22008 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 22009 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
22010 }
22011
22012 return die;
22013}
22014
22015/* Get the type specified by SIGNATURE referenced in DIE/CU,
22016 reading in and processing the type unit if necessary. */
22017
22018static struct type *
22019get_signatured_type (struct die_info *die, ULONGEST signature,
22020 struct dwarf2_cu *cu)
22021{
22022 struct signatured_type *sig_type;
22023 struct dwarf2_cu *type_cu;
22024 struct die_info *type_die;
22025 struct type *type;
22026
a2ce51a0 22027 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22028 /* sig_type will be NULL if the signatured type is missing from
22029 the debug info. */
22030 if (sig_type == NULL)
22031 {
22032 complaint (&symfile_complaints,
22033 _("Dwarf Error: Cannot find signatured DIE %s referenced"
22034 " from DIE at 0x%x [in module %s]"),
9c541725 22035 hex_string (signature), to_underlying (die->sect_off),
4262abfb 22036 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22037 return build_error_marker_type (cu, die);
22038 }
22039
22040 /* If we already know the type we're done. */
22041 if (sig_type->type != NULL)
22042 return sig_type->type;
22043
22044 type_cu = cu;
22045 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22046 if (type_die != NULL)
22047 {
22048 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22049 is created. This is important, for example, because for c++ classes
22050 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22051 type = read_type_die (type_die, type_cu);
22052 if (type == NULL)
22053 {
22054 complaint (&symfile_complaints,
22055 _("Dwarf Error: Cannot build signatured type %s"
22056 " referenced from DIE at 0x%x [in module %s]"),
9c541725 22057 hex_string (signature), to_underlying (die->sect_off),
4262abfb 22058 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22059 type = build_error_marker_type (cu, die);
22060 }
22061 }
22062 else
22063 {
22064 complaint (&symfile_complaints,
22065 _("Dwarf Error: Problem reading signatured DIE %s referenced"
22066 " from DIE at 0x%x [in module %s]"),
9c541725 22067 hex_string (signature), to_underlying (die->sect_off),
4262abfb 22068 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22069 type = build_error_marker_type (cu, die);
22070 }
22071 sig_type->type = type;
22072
22073 return type;
22074}
22075
22076/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22077 reading in and processing the type unit if necessary. */
22078
22079static struct type *
ff39bb5e 22080get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22081 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22082{
22083 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 22084 if (attr_form_is_ref (attr))
ac9ec31b
DE
22085 {
22086 struct dwarf2_cu *type_cu = cu;
22087 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22088
22089 return read_type_die (type_die, type_cu);
22090 }
22091 else if (attr->form == DW_FORM_ref_sig8)
22092 {
22093 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22094 }
22095 else
22096 {
22097 complaint (&symfile_complaints,
22098 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22099 " at 0x%x [in module %s]"),
9c541725 22100 dwarf_form_name (attr->form), to_underlying (die->sect_off),
4262abfb 22101 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22102 return build_error_marker_type (cu, die);
22103 }
348e048f
DE
22104}
22105
e5fe5e75 22106/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22107
22108static void
e5fe5e75 22109load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 22110{
52dc124a 22111 struct signatured_type *sig_type;
348e048f 22112
f4dc4d17
DE
22113 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22114 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
22115
6721b2ec
DE
22116 /* We have the per_cu, but we need the signatured_type.
22117 Fortunately this is an easy translation. */
22118 gdb_assert (per_cu->is_debug_types);
22119 sig_type = (struct signatured_type *) per_cu;
348e048f 22120
6721b2ec 22121 gdb_assert (per_cu->cu == NULL);
348e048f 22122
52dc124a 22123 read_signatured_type (sig_type);
348e048f 22124
6721b2ec 22125 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22126}
22127
dee91e82
DE
22128/* die_reader_func for read_signatured_type.
22129 This is identical to load_full_comp_unit_reader,
22130 but is kept separate for now. */
348e048f
DE
22131
22132static void
dee91e82 22133read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 22134 const gdb_byte *info_ptr,
dee91e82
DE
22135 struct die_info *comp_unit_die,
22136 int has_children,
22137 void *data)
348e048f 22138{
dee91e82 22139 struct dwarf2_cu *cu = reader->cu;
348e048f 22140
dee91e82
DE
22141 gdb_assert (cu->die_hash == NULL);
22142 cu->die_hash =
22143 htab_create_alloc_ex (cu->header.length / 12,
22144 die_hash,
22145 die_eq,
22146 NULL,
22147 &cu->comp_unit_obstack,
22148 hashtab_obstack_allocate,
22149 dummy_obstack_deallocate);
348e048f 22150
dee91e82
DE
22151 if (has_children)
22152 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
22153 &info_ptr, comp_unit_die);
22154 cu->dies = comp_unit_die;
22155 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
22156
22157 /* We try not to read any attributes in this function, because not
9cdd5dbd 22158 all CUs needed for references have been loaded yet, and symbol
348e048f 22159 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
22160 or we won't be able to build types correctly.
22161 Similarly, if we do not read the producer, we can not apply
22162 producer-specific interpretation. */
95554aad 22163 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 22164}
348e048f 22165
3019eac3
DE
22166/* Read in a signatured type and build its CU and DIEs.
22167 If the type is a stub for the real type in a DWO file,
22168 read in the real type from the DWO file as well. */
dee91e82
DE
22169
22170static void
22171read_signatured_type (struct signatured_type *sig_type)
22172{
22173 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22174
3019eac3 22175 gdb_assert (per_cu->is_debug_types);
dee91e82 22176 gdb_assert (per_cu->cu == NULL);
348e048f 22177
f4dc4d17
DE
22178 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
22179 read_signatured_type_reader, NULL);
7ee85ab1 22180 sig_type->per_cu.tu_read = 1;
c906108c
SS
22181}
22182
c906108c
SS
22183/* Decode simple location descriptions.
22184 Given a pointer to a dwarf block that defines a location, compute
22185 the location and return the value.
22186
4cecd739
DJ
22187 NOTE drow/2003-11-18: This function is called in two situations
22188 now: for the address of static or global variables (partial symbols
22189 only) and for offsets into structures which are expected to be
22190 (more or less) constant. The partial symbol case should go away,
22191 and only the constant case should remain. That will let this
22192 function complain more accurately. A few special modes are allowed
22193 without complaint for global variables (for instance, global
22194 register values and thread-local values).
c906108c
SS
22195
22196 A location description containing no operations indicates that the
4cecd739 22197 object is optimized out. The return value is 0 for that case.
6b992462
DJ
22198 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22199 callers will only want a very basic result and this can become a
21ae7a4d
JK
22200 complaint.
22201
22202 Note that stack[0] is unused except as a default error return. */
c906108c
SS
22203
22204static CORE_ADDR
e7c27a73 22205decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 22206{
e7c27a73 22207 struct objfile *objfile = cu->objfile;
56eb65bd
SP
22208 size_t i;
22209 size_t size = blk->size;
d521ce57 22210 const gdb_byte *data = blk->data;
21ae7a4d
JK
22211 CORE_ADDR stack[64];
22212 int stacki;
22213 unsigned int bytes_read, unsnd;
22214 gdb_byte op;
c906108c 22215
21ae7a4d
JK
22216 i = 0;
22217 stacki = 0;
22218 stack[stacki] = 0;
22219 stack[++stacki] = 0;
22220
22221 while (i < size)
22222 {
22223 op = data[i++];
22224 switch (op)
22225 {
22226 case DW_OP_lit0:
22227 case DW_OP_lit1:
22228 case DW_OP_lit2:
22229 case DW_OP_lit3:
22230 case DW_OP_lit4:
22231 case DW_OP_lit5:
22232 case DW_OP_lit6:
22233 case DW_OP_lit7:
22234 case DW_OP_lit8:
22235 case DW_OP_lit9:
22236 case DW_OP_lit10:
22237 case DW_OP_lit11:
22238 case DW_OP_lit12:
22239 case DW_OP_lit13:
22240 case DW_OP_lit14:
22241 case DW_OP_lit15:
22242 case DW_OP_lit16:
22243 case DW_OP_lit17:
22244 case DW_OP_lit18:
22245 case DW_OP_lit19:
22246 case DW_OP_lit20:
22247 case DW_OP_lit21:
22248 case DW_OP_lit22:
22249 case DW_OP_lit23:
22250 case DW_OP_lit24:
22251 case DW_OP_lit25:
22252 case DW_OP_lit26:
22253 case DW_OP_lit27:
22254 case DW_OP_lit28:
22255 case DW_OP_lit29:
22256 case DW_OP_lit30:
22257 case DW_OP_lit31:
22258 stack[++stacki] = op - DW_OP_lit0;
22259 break;
f1bea926 22260
21ae7a4d
JK
22261 case DW_OP_reg0:
22262 case DW_OP_reg1:
22263 case DW_OP_reg2:
22264 case DW_OP_reg3:
22265 case DW_OP_reg4:
22266 case DW_OP_reg5:
22267 case DW_OP_reg6:
22268 case DW_OP_reg7:
22269 case DW_OP_reg8:
22270 case DW_OP_reg9:
22271 case DW_OP_reg10:
22272 case DW_OP_reg11:
22273 case DW_OP_reg12:
22274 case DW_OP_reg13:
22275 case DW_OP_reg14:
22276 case DW_OP_reg15:
22277 case DW_OP_reg16:
22278 case DW_OP_reg17:
22279 case DW_OP_reg18:
22280 case DW_OP_reg19:
22281 case DW_OP_reg20:
22282 case DW_OP_reg21:
22283 case DW_OP_reg22:
22284 case DW_OP_reg23:
22285 case DW_OP_reg24:
22286 case DW_OP_reg25:
22287 case DW_OP_reg26:
22288 case DW_OP_reg27:
22289 case DW_OP_reg28:
22290 case DW_OP_reg29:
22291 case DW_OP_reg30:
22292 case DW_OP_reg31:
22293 stack[++stacki] = op - DW_OP_reg0;
22294 if (i < size)
22295 dwarf2_complex_location_expr_complaint ();
22296 break;
c906108c 22297
21ae7a4d
JK
22298 case DW_OP_regx:
22299 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22300 i += bytes_read;
22301 stack[++stacki] = unsnd;
22302 if (i < size)
22303 dwarf2_complex_location_expr_complaint ();
22304 break;
c906108c 22305
21ae7a4d
JK
22306 case DW_OP_addr:
22307 stack[++stacki] = read_address (objfile->obfd, &data[i],
22308 cu, &bytes_read);
22309 i += bytes_read;
22310 break;
d53d4ac5 22311
21ae7a4d
JK
22312 case DW_OP_const1u:
22313 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22314 i += 1;
22315 break;
22316
22317 case DW_OP_const1s:
22318 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22319 i += 1;
22320 break;
22321
22322 case DW_OP_const2u:
22323 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22324 i += 2;
22325 break;
22326
22327 case DW_OP_const2s:
22328 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22329 i += 2;
22330 break;
d53d4ac5 22331
21ae7a4d
JK
22332 case DW_OP_const4u:
22333 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22334 i += 4;
22335 break;
22336
22337 case DW_OP_const4s:
22338 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22339 i += 4;
22340 break;
22341
585861ea
JK
22342 case DW_OP_const8u:
22343 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22344 i += 8;
22345 break;
22346
21ae7a4d
JK
22347 case DW_OP_constu:
22348 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22349 &bytes_read);
22350 i += bytes_read;
22351 break;
22352
22353 case DW_OP_consts:
22354 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22355 i += bytes_read;
22356 break;
22357
22358 case DW_OP_dup:
22359 stack[stacki + 1] = stack[stacki];
22360 stacki++;
22361 break;
22362
22363 case DW_OP_plus:
22364 stack[stacki - 1] += stack[stacki];
22365 stacki--;
22366 break;
22367
22368 case DW_OP_plus_uconst:
22369 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22370 &bytes_read);
22371 i += bytes_read;
22372 break;
22373
22374 case DW_OP_minus:
22375 stack[stacki - 1] -= stack[stacki];
22376 stacki--;
22377 break;
22378
22379 case DW_OP_deref:
22380 /* If we're not the last op, then we definitely can't encode
22381 this using GDB's address_class enum. This is valid for partial
22382 global symbols, although the variable's address will be bogus
22383 in the psymtab. */
22384 if (i < size)
22385 dwarf2_complex_location_expr_complaint ();
22386 break;
22387
22388 case DW_OP_GNU_push_tls_address:
4aa4e28b 22389 case DW_OP_form_tls_address:
21ae7a4d
JK
22390 /* The top of the stack has the offset from the beginning
22391 of the thread control block at which the variable is located. */
22392 /* Nothing should follow this operator, so the top of stack would
22393 be returned. */
22394 /* This is valid for partial global symbols, but the variable's
585861ea
JK
22395 address will be bogus in the psymtab. Make it always at least
22396 non-zero to not look as a variable garbage collected by linker
22397 which have DW_OP_addr 0. */
21ae7a4d
JK
22398 if (i < size)
22399 dwarf2_complex_location_expr_complaint ();
585861ea 22400 stack[stacki]++;
21ae7a4d
JK
22401 break;
22402
22403 case DW_OP_GNU_uninit:
22404 break;
22405
3019eac3 22406 case DW_OP_GNU_addr_index:
49f6c839 22407 case DW_OP_GNU_const_index:
3019eac3
DE
22408 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22409 &bytes_read);
22410 i += bytes_read;
22411 break;
22412
21ae7a4d
JK
22413 default:
22414 {
f39c6ffd 22415 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
22416
22417 if (name)
22418 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
22419 name);
22420 else
22421 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
22422 op);
22423 }
22424
22425 return (stack[stacki]);
d53d4ac5 22426 }
3c6e0cb3 22427
21ae7a4d
JK
22428 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22429 outside of the allocated space. Also enforce minimum>0. */
22430 if (stacki >= ARRAY_SIZE (stack) - 1)
22431 {
22432 complaint (&symfile_complaints,
22433 _("location description stack overflow"));
22434 return 0;
22435 }
22436
22437 if (stacki <= 0)
22438 {
22439 complaint (&symfile_complaints,
22440 _("location description stack underflow"));
22441 return 0;
22442 }
22443 }
22444 return (stack[stacki]);
c906108c
SS
22445}
22446
22447/* memory allocation interface */
22448
c906108c 22449static struct dwarf_block *
7b5a2f43 22450dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 22451{
8d749320 22452 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
22453}
22454
c906108c 22455static struct die_info *
b60c80d6 22456dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
22457{
22458 struct die_info *die;
b60c80d6
DJ
22459 size_t size = sizeof (struct die_info);
22460
22461 if (num_attrs > 1)
22462 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 22463
b60c80d6 22464 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
22465 memset (die, 0, sizeof (struct die_info));
22466 return (die);
22467}
2e276125
JB
22468
22469\f
22470/* Macro support. */
22471
233d95b5
JK
22472/* Return file name relative to the compilation directory of file number I in
22473 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 22474 responsible for freeing it. */
233d95b5 22475
2e276125 22476static char *
233d95b5 22477file_file_name (int file, struct line_header *lh)
2e276125 22478{
6a83a1e6
EZ
22479 /* Is the file number a valid index into the line header's file name
22480 table? Remember that file numbers start with one, not zero. */
fff8551c 22481 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 22482 {
8c43009f 22483 const file_entry &fe = lh->file_names[file - 1];
6e70227d 22484
8c43009f
PA
22485 if (!IS_ABSOLUTE_PATH (fe.name))
22486 {
22487 const char *dir = fe.include_dir (lh);
22488 if (dir != NULL)
22489 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
22490 }
22491 return xstrdup (fe.name);
6a83a1e6 22492 }
2e276125
JB
22493 else
22494 {
6a83a1e6
EZ
22495 /* The compiler produced a bogus file number. We can at least
22496 record the macro definitions made in the file, even if we
22497 won't be able to find the file by name. */
22498 char fake_name[80];
9a619af0 22499
8c042590
PM
22500 xsnprintf (fake_name, sizeof (fake_name),
22501 "<bad macro file number %d>", file);
2e276125 22502
6e70227d 22503 complaint (&symfile_complaints,
6a83a1e6
EZ
22504 _("bad file number in macro information (%d)"),
22505 file);
2e276125 22506
6a83a1e6 22507 return xstrdup (fake_name);
2e276125
JB
22508 }
22509}
22510
233d95b5
JK
22511/* Return the full name of file number I in *LH's file name table.
22512 Use COMP_DIR as the name of the current directory of the
22513 compilation. The result is allocated using xmalloc; the caller is
22514 responsible for freeing it. */
22515static char *
22516file_full_name (int file, struct line_header *lh, const char *comp_dir)
22517{
22518 /* Is the file number a valid index into the line header's file name
22519 table? Remember that file numbers start with one, not zero. */
fff8551c 22520 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
22521 {
22522 char *relative = file_file_name (file, lh);
22523
22524 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
22525 return relative;
b36cec19
PA
22526 return reconcat (relative, comp_dir, SLASH_STRING,
22527 relative, (char *) NULL);
233d95b5
JK
22528 }
22529 else
22530 return file_file_name (file, lh);
22531}
22532
2e276125
JB
22533
22534static struct macro_source_file *
22535macro_start_file (int file, int line,
22536 struct macro_source_file *current_file,
43f3e411 22537 struct line_header *lh)
2e276125 22538{
233d95b5
JK
22539 /* File name relative to the compilation directory of this source file. */
22540 char *file_name = file_file_name (file, lh);
2e276125 22541
2e276125 22542 if (! current_file)
abc9d0dc 22543 {
fc474241
DE
22544 /* Note: We don't create a macro table for this compilation unit
22545 at all until we actually get a filename. */
43f3e411 22546 struct macro_table *macro_table = get_macro_table ();
fc474241 22547
abc9d0dc
TT
22548 /* If we have no current file, then this must be the start_file
22549 directive for the compilation unit's main source file. */
fc474241
DE
22550 current_file = macro_set_main (macro_table, file_name);
22551 macro_define_special (macro_table);
abc9d0dc 22552 }
2e276125 22553 else
233d95b5 22554 current_file = macro_include (current_file, line, file_name);
2e276125 22555
233d95b5 22556 xfree (file_name);
6e70227d 22557
2e276125
JB
22558 return current_file;
22559}
22560
2e276125
JB
22561static const char *
22562consume_improper_spaces (const char *p, const char *body)
22563{
22564 if (*p == ' ')
22565 {
4d3c2250 22566 complaint (&symfile_complaints,
3e43a32a
MS
22567 _("macro definition contains spaces "
22568 "in formal argument list:\n`%s'"),
4d3c2250 22569 body);
2e276125
JB
22570
22571 while (*p == ' ')
22572 p++;
22573 }
22574
22575 return p;
22576}
22577
22578
22579static void
22580parse_macro_definition (struct macro_source_file *file, int line,
22581 const char *body)
22582{
22583 const char *p;
22584
22585 /* The body string takes one of two forms. For object-like macro
22586 definitions, it should be:
22587
22588 <macro name> " " <definition>
22589
22590 For function-like macro definitions, it should be:
22591
22592 <macro name> "() " <definition>
22593 or
22594 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
22595
22596 Spaces may appear only where explicitly indicated, and in the
22597 <definition>.
22598
22599 The Dwarf 2 spec says that an object-like macro's name is always
22600 followed by a space, but versions of GCC around March 2002 omit
6e70227d 22601 the space when the macro's definition is the empty string.
2e276125
JB
22602
22603 The Dwarf 2 spec says that there should be no spaces between the
22604 formal arguments in a function-like macro's formal argument list,
22605 but versions of GCC around March 2002 include spaces after the
22606 commas. */
22607
22608
22609 /* Find the extent of the macro name. The macro name is terminated
22610 by either a space or null character (for an object-like macro) or
22611 an opening paren (for a function-like macro). */
22612 for (p = body; *p; p++)
22613 if (*p == ' ' || *p == '(')
22614 break;
22615
22616 if (*p == ' ' || *p == '\0')
22617 {
22618 /* It's an object-like macro. */
22619 int name_len = p - body;
3f8a7804 22620 char *name = savestring (body, name_len);
2e276125
JB
22621 const char *replacement;
22622
22623 if (*p == ' ')
22624 replacement = body + name_len + 1;
22625 else
22626 {
4d3c2250 22627 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22628 replacement = body + name_len;
22629 }
6e70227d 22630
2e276125
JB
22631 macro_define_object (file, line, name, replacement);
22632
22633 xfree (name);
22634 }
22635 else if (*p == '(')
22636 {
22637 /* It's a function-like macro. */
3f8a7804 22638 char *name = savestring (body, p - body);
2e276125
JB
22639 int argc = 0;
22640 int argv_size = 1;
8d749320 22641 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
22642
22643 p++;
22644
22645 p = consume_improper_spaces (p, body);
22646
22647 /* Parse the formal argument list. */
22648 while (*p && *p != ')')
22649 {
22650 /* Find the extent of the current argument name. */
22651 const char *arg_start = p;
22652
22653 while (*p && *p != ',' && *p != ')' && *p != ' ')
22654 p++;
22655
22656 if (! *p || p == arg_start)
4d3c2250 22657 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22658 else
22659 {
22660 /* Make sure argv has room for the new argument. */
22661 if (argc >= argv_size)
22662 {
22663 argv_size *= 2;
224c3ddb 22664 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
22665 }
22666
3f8a7804 22667 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
22668 }
22669
22670 p = consume_improper_spaces (p, body);
22671
22672 /* Consume the comma, if present. */
22673 if (*p == ',')
22674 {
22675 p++;
22676
22677 p = consume_improper_spaces (p, body);
22678 }
22679 }
22680
22681 if (*p == ')')
22682 {
22683 p++;
22684
22685 if (*p == ' ')
22686 /* Perfectly formed definition, no complaints. */
22687 macro_define_function (file, line, name,
6e70227d 22688 argc, (const char **) argv,
2e276125
JB
22689 p + 1);
22690 else if (*p == '\0')
22691 {
22692 /* Complain, but do define it. */
4d3c2250 22693 dwarf2_macro_malformed_definition_complaint (body);
2e276125 22694 macro_define_function (file, line, name,
6e70227d 22695 argc, (const char **) argv,
2e276125
JB
22696 p);
22697 }
22698 else
22699 /* Just complain. */
4d3c2250 22700 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22701 }
22702 else
22703 /* Just complain. */
4d3c2250 22704 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22705
22706 xfree (name);
22707 {
22708 int i;
22709
22710 for (i = 0; i < argc; i++)
22711 xfree (argv[i]);
22712 }
22713 xfree (argv);
22714 }
22715 else
4d3c2250 22716 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22717}
22718
cf2c3c16
TT
22719/* Skip some bytes from BYTES according to the form given in FORM.
22720 Returns the new pointer. */
2e276125 22721
d521ce57
TT
22722static const gdb_byte *
22723skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
22724 enum dwarf_form form,
22725 unsigned int offset_size,
22726 struct dwarf2_section_info *section)
2e276125 22727{
cf2c3c16 22728 unsigned int bytes_read;
2e276125 22729
cf2c3c16 22730 switch (form)
2e276125 22731 {
cf2c3c16
TT
22732 case DW_FORM_data1:
22733 case DW_FORM_flag:
22734 ++bytes;
22735 break;
22736
22737 case DW_FORM_data2:
22738 bytes += 2;
22739 break;
22740
22741 case DW_FORM_data4:
22742 bytes += 4;
22743 break;
22744
22745 case DW_FORM_data8:
22746 bytes += 8;
22747 break;
22748
0224619f
JK
22749 case DW_FORM_data16:
22750 bytes += 16;
22751 break;
22752
cf2c3c16
TT
22753 case DW_FORM_string:
22754 read_direct_string (abfd, bytes, &bytes_read);
22755 bytes += bytes_read;
22756 break;
22757
22758 case DW_FORM_sec_offset:
22759 case DW_FORM_strp:
36586728 22760 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
22761 bytes += offset_size;
22762 break;
22763
22764 case DW_FORM_block:
22765 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
22766 bytes += bytes_read;
22767 break;
22768
22769 case DW_FORM_block1:
22770 bytes += 1 + read_1_byte (abfd, bytes);
22771 break;
22772 case DW_FORM_block2:
22773 bytes += 2 + read_2_bytes (abfd, bytes);
22774 break;
22775 case DW_FORM_block4:
22776 bytes += 4 + read_4_bytes (abfd, bytes);
22777 break;
22778
22779 case DW_FORM_sdata:
22780 case DW_FORM_udata:
3019eac3
DE
22781 case DW_FORM_GNU_addr_index:
22782 case DW_FORM_GNU_str_index:
d521ce57 22783 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
22784 if (bytes == NULL)
22785 {
22786 dwarf2_section_buffer_overflow_complaint (section);
22787 return NULL;
22788 }
cf2c3c16
TT
22789 break;
22790
663c44ac
JK
22791 case DW_FORM_implicit_const:
22792 break;
22793
cf2c3c16
TT
22794 default:
22795 {
22796 complain:
22797 complaint (&symfile_complaints,
22798 _("invalid form 0x%x in `%s'"),
a32a8923 22799 form, get_section_name (section));
cf2c3c16
TT
22800 return NULL;
22801 }
2e276125
JB
22802 }
22803
cf2c3c16
TT
22804 return bytes;
22805}
757a13d0 22806
cf2c3c16
TT
22807/* A helper for dwarf_decode_macros that handles skipping an unknown
22808 opcode. Returns an updated pointer to the macro data buffer; or,
22809 on error, issues a complaint and returns NULL. */
757a13d0 22810
d521ce57 22811static const gdb_byte *
cf2c3c16 22812skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
22813 const gdb_byte **opcode_definitions,
22814 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
22815 bfd *abfd,
22816 unsigned int offset_size,
22817 struct dwarf2_section_info *section)
22818{
22819 unsigned int bytes_read, i;
22820 unsigned long arg;
d521ce57 22821 const gdb_byte *defn;
2e276125 22822
cf2c3c16 22823 if (opcode_definitions[opcode] == NULL)
2e276125 22824 {
cf2c3c16
TT
22825 complaint (&symfile_complaints,
22826 _("unrecognized DW_MACFINO opcode 0x%x"),
22827 opcode);
22828 return NULL;
22829 }
2e276125 22830
cf2c3c16
TT
22831 defn = opcode_definitions[opcode];
22832 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
22833 defn += bytes_read;
2e276125 22834
cf2c3c16
TT
22835 for (i = 0; i < arg; ++i)
22836 {
aead7601
SM
22837 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
22838 (enum dwarf_form) defn[i], offset_size,
f664829e 22839 section);
cf2c3c16
TT
22840 if (mac_ptr == NULL)
22841 {
22842 /* skip_form_bytes already issued the complaint. */
22843 return NULL;
22844 }
22845 }
757a13d0 22846
cf2c3c16
TT
22847 return mac_ptr;
22848}
757a13d0 22849
cf2c3c16
TT
22850/* A helper function which parses the header of a macro section.
22851 If the macro section is the extended (for now called "GNU") type,
22852 then this updates *OFFSET_SIZE. Returns a pointer to just after
22853 the header, or issues a complaint and returns NULL on error. */
757a13d0 22854
d521ce57
TT
22855static const gdb_byte *
22856dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 22857 bfd *abfd,
d521ce57 22858 const gdb_byte *mac_ptr,
cf2c3c16
TT
22859 unsigned int *offset_size,
22860 int section_is_gnu)
22861{
22862 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 22863
cf2c3c16
TT
22864 if (section_is_gnu)
22865 {
22866 unsigned int version, flags;
757a13d0 22867
cf2c3c16 22868 version = read_2_bytes (abfd, mac_ptr);
0af92d60 22869 if (version != 4 && version != 5)
cf2c3c16
TT
22870 {
22871 complaint (&symfile_complaints,
22872 _("unrecognized version `%d' in .debug_macro section"),
22873 version);
22874 return NULL;
22875 }
22876 mac_ptr += 2;
757a13d0 22877
cf2c3c16
TT
22878 flags = read_1_byte (abfd, mac_ptr);
22879 ++mac_ptr;
22880 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 22881
cf2c3c16
TT
22882 if ((flags & 2) != 0)
22883 /* We don't need the line table offset. */
22884 mac_ptr += *offset_size;
757a13d0 22885
cf2c3c16
TT
22886 /* Vendor opcode descriptions. */
22887 if ((flags & 4) != 0)
22888 {
22889 unsigned int i, count;
757a13d0 22890
cf2c3c16
TT
22891 count = read_1_byte (abfd, mac_ptr);
22892 ++mac_ptr;
22893 for (i = 0; i < count; ++i)
22894 {
22895 unsigned int opcode, bytes_read;
22896 unsigned long arg;
22897
22898 opcode = read_1_byte (abfd, mac_ptr);
22899 ++mac_ptr;
22900 opcode_definitions[opcode] = mac_ptr;
22901 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22902 mac_ptr += bytes_read;
22903 mac_ptr += arg;
22904 }
757a13d0 22905 }
cf2c3c16 22906 }
757a13d0 22907
cf2c3c16
TT
22908 return mac_ptr;
22909}
757a13d0 22910
cf2c3c16 22911/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 22912 including DW_MACRO_import. */
cf2c3c16
TT
22913
22914static void
d521ce57
TT
22915dwarf_decode_macro_bytes (bfd *abfd,
22916 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 22917 struct macro_source_file *current_file,
43f3e411 22918 struct line_header *lh,
cf2c3c16 22919 struct dwarf2_section_info *section,
36586728 22920 int section_is_gnu, int section_is_dwz,
cf2c3c16 22921 unsigned int offset_size,
8fc3fc34 22922 htab_t include_hash)
cf2c3c16 22923{
4d663531 22924 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
22925 enum dwarf_macro_record_type macinfo_type;
22926 int at_commandline;
d521ce57 22927 const gdb_byte *opcode_definitions[256];
757a13d0 22928
cf2c3c16
TT
22929 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22930 &offset_size, section_is_gnu);
22931 if (mac_ptr == NULL)
22932 {
22933 /* We already issued a complaint. */
22934 return;
22935 }
757a13d0
JK
22936
22937 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
22938 GDB is still reading the definitions from command line. First
22939 DW_MACINFO_start_file will need to be ignored as it was already executed
22940 to create CURRENT_FILE for the main source holding also the command line
22941 definitions. On first met DW_MACINFO_start_file this flag is reset to
22942 normally execute all the remaining DW_MACINFO_start_file macinfos. */
22943
22944 at_commandline = 1;
22945
22946 do
22947 {
22948 /* Do we at least have room for a macinfo type byte? */
22949 if (mac_ptr >= mac_end)
22950 {
f664829e 22951 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
22952 break;
22953 }
22954
aead7601 22955 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
22956 mac_ptr++;
22957
cf2c3c16
TT
22958 /* Note that we rely on the fact that the corresponding GNU and
22959 DWARF constants are the same. */
757a13d0
JK
22960 switch (macinfo_type)
22961 {
22962 /* A zero macinfo type indicates the end of the macro
22963 information. */
22964 case 0:
22965 break;
2e276125 22966
0af92d60
JK
22967 case DW_MACRO_define:
22968 case DW_MACRO_undef:
22969 case DW_MACRO_define_strp:
22970 case DW_MACRO_undef_strp:
22971 case DW_MACRO_define_sup:
22972 case DW_MACRO_undef_sup:
2e276125 22973 {
891d2f0b 22974 unsigned int bytes_read;
2e276125 22975 int line;
d521ce57 22976 const char *body;
cf2c3c16 22977 int is_define;
2e276125 22978
cf2c3c16
TT
22979 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22980 mac_ptr += bytes_read;
22981
0af92d60
JK
22982 if (macinfo_type == DW_MACRO_define
22983 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
22984 {
22985 body = read_direct_string (abfd, mac_ptr, &bytes_read);
22986 mac_ptr += bytes_read;
22987 }
22988 else
22989 {
22990 LONGEST str_offset;
22991
22992 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
22993 mac_ptr += offset_size;
2e276125 22994
0af92d60
JK
22995 if (macinfo_type == DW_MACRO_define_sup
22996 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 22997 || section_is_dwz)
36586728
TT
22998 {
22999 struct dwz_file *dwz = dwarf2_get_dwz_file ();
23000
23001 body = read_indirect_string_from_dwz (dwz, str_offset);
23002 }
23003 else
23004 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
23005 }
23006
0af92d60
JK
23007 is_define = (macinfo_type == DW_MACRO_define
23008 || macinfo_type == DW_MACRO_define_strp
23009 || macinfo_type == DW_MACRO_define_sup);
2e276125 23010 if (! current_file)
757a13d0
JK
23011 {
23012 /* DWARF violation as no main source is present. */
23013 complaint (&symfile_complaints,
23014 _("debug info with no main source gives macro %s "
23015 "on line %d: %s"),
cf2c3c16
TT
23016 is_define ? _("definition") : _("undefinition"),
23017 line, body);
757a13d0
JK
23018 break;
23019 }
3e43a32a
MS
23020 if ((line == 0 && !at_commandline)
23021 || (line != 0 && at_commandline))
4d3c2250 23022 complaint (&symfile_complaints,
757a13d0
JK
23023 _("debug info gives %s macro %s with %s line %d: %s"),
23024 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 23025 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
23026 line == 0 ? _("zero") : _("non-zero"), line, body);
23027
cf2c3c16 23028 if (is_define)
757a13d0 23029 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
23030 else
23031 {
0af92d60
JK
23032 gdb_assert (macinfo_type == DW_MACRO_undef
23033 || macinfo_type == DW_MACRO_undef_strp
23034 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
23035 macro_undef (current_file, line, body);
23036 }
2e276125
JB
23037 }
23038 break;
23039
0af92d60 23040 case DW_MACRO_start_file:
2e276125 23041 {
891d2f0b 23042 unsigned int bytes_read;
2e276125
JB
23043 int line, file;
23044
23045 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23046 mac_ptr += bytes_read;
23047 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23048 mac_ptr += bytes_read;
23049
3e43a32a
MS
23050 if ((line == 0 && !at_commandline)
23051 || (line != 0 && at_commandline))
757a13d0
JK
23052 complaint (&symfile_complaints,
23053 _("debug info gives source %d included "
23054 "from %s at %s line %d"),
23055 file, at_commandline ? _("command-line") : _("file"),
23056 line == 0 ? _("zero") : _("non-zero"), line);
23057
23058 if (at_commandline)
23059 {
0af92d60 23060 /* This DW_MACRO_start_file was executed in the
cf2c3c16 23061 pass one. */
757a13d0
JK
23062 at_commandline = 0;
23063 }
23064 else
43f3e411 23065 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
23066 }
23067 break;
23068
0af92d60 23069 case DW_MACRO_end_file:
2e276125 23070 if (! current_file)
4d3c2250 23071 complaint (&symfile_complaints,
3e43a32a
MS
23072 _("macro debug info has an unmatched "
23073 "`close_file' directive"));
2e276125
JB
23074 else
23075 {
23076 current_file = current_file->included_by;
23077 if (! current_file)
23078 {
cf2c3c16 23079 enum dwarf_macro_record_type next_type;
2e276125
JB
23080
23081 /* GCC circa March 2002 doesn't produce the zero
23082 type byte marking the end of the compilation
23083 unit. Complain if it's not there, but exit no
23084 matter what. */
23085
23086 /* Do we at least have room for a macinfo type byte? */
23087 if (mac_ptr >= mac_end)
23088 {
f664829e 23089 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
23090 return;
23091 }
23092
23093 /* We don't increment mac_ptr here, so this is just
23094 a look-ahead. */
aead7601
SM
23095 next_type
23096 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23097 mac_ptr);
2e276125 23098 if (next_type != 0)
4d3c2250 23099 complaint (&symfile_complaints,
3e43a32a
MS
23100 _("no terminating 0-type entry for "
23101 "macros in `.debug_macinfo' section"));
2e276125
JB
23102
23103 return;
23104 }
23105 }
23106 break;
23107
0af92d60
JK
23108 case DW_MACRO_import:
23109 case DW_MACRO_import_sup:
cf2c3c16
TT
23110 {
23111 LONGEST offset;
8fc3fc34 23112 void **slot;
a036ba48
TT
23113 bfd *include_bfd = abfd;
23114 struct dwarf2_section_info *include_section = section;
d521ce57 23115 const gdb_byte *include_mac_end = mac_end;
a036ba48 23116 int is_dwz = section_is_dwz;
d521ce57 23117 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
23118
23119 offset = read_offset_1 (abfd, mac_ptr, offset_size);
23120 mac_ptr += offset_size;
23121
0af92d60 23122 if (macinfo_type == DW_MACRO_import_sup)
a036ba48
TT
23123 {
23124 struct dwz_file *dwz = dwarf2_get_dwz_file ();
23125
4d663531 23126 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 23127
a036ba48 23128 include_section = &dwz->macro;
a32a8923 23129 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
23130 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23131 is_dwz = 1;
23132 }
23133
23134 new_mac_ptr = include_section->buffer + offset;
23135 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23136
8fc3fc34
TT
23137 if (*slot != NULL)
23138 {
23139 /* This has actually happened; see
23140 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
23141 complaint (&symfile_complaints,
0af92d60 23142 _("recursive DW_MACRO_import in "
8fc3fc34
TT
23143 ".debug_macro section"));
23144 }
23145 else
23146 {
d521ce57 23147 *slot = (void *) new_mac_ptr;
36586728 23148
a036ba48 23149 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 23150 include_mac_end, current_file, lh,
36586728 23151 section, section_is_gnu, is_dwz,
4d663531 23152 offset_size, include_hash);
8fc3fc34 23153
d521ce57 23154 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 23155 }
cf2c3c16
TT
23156 }
23157 break;
23158
2e276125 23159 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
23160 if (!section_is_gnu)
23161 {
23162 unsigned int bytes_read;
2e276125 23163
ac298888
TT
23164 /* This reads the constant, but since we don't recognize
23165 any vendor extensions, we ignore it. */
23166 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
23167 mac_ptr += bytes_read;
23168 read_direct_string (abfd, mac_ptr, &bytes_read);
23169 mac_ptr += bytes_read;
2e276125 23170
cf2c3c16
TT
23171 /* We don't recognize any vendor extensions. */
23172 break;
23173 }
23174 /* FALLTHROUGH */
23175
23176 default:
23177 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 23178 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
23179 section);
23180 if (mac_ptr == NULL)
23181 return;
23182 break;
2e276125 23183 }
757a13d0 23184 } while (macinfo_type != 0);
2e276125 23185}
8e19ed76 23186
cf2c3c16 23187static void
09262596 23188dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 23189 int section_is_gnu)
cf2c3c16 23190{
bb5ed363 23191 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
23192 struct line_header *lh = cu->line_header;
23193 bfd *abfd;
d521ce57 23194 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
23195 struct macro_source_file *current_file = 0;
23196 enum dwarf_macro_record_type macinfo_type;
23197 unsigned int offset_size = cu->header.offset_size;
d521ce57 23198 const gdb_byte *opcode_definitions[256];
8fc3fc34 23199 void **slot;
09262596
DE
23200 struct dwarf2_section_info *section;
23201 const char *section_name;
23202
23203 if (cu->dwo_unit != NULL)
23204 {
23205 if (section_is_gnu)
23206 {
23207 section = &cu->dwo_unit->dwo_file->sections.macro;
23208 section_name = ".debug_macro.dwo";
23209 }
23210 else
23211 {
23212 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23213 section_name = ".debug_macinfo.dwo";
23214 }
23215 }
23216 else
23217 {
23218 if (section_is_gnu)
23219 {
23220 section = &dwarf2_per_objfile->macro;
23221 section_name = ".debug_macro";
23222 }
23223 else
23224 {
23225 section = &dwarf2_per_objfile->macinfo;
23226 section_name = ".debug_macinfo";
23227 }
23228 }
cf2c3c16 23229
bb5ed363 23230 dwarf2_read_section (objfile, section);
cf2c3c16
TT
23231 if (section->buffer == NULL)
23232 {
fceca515 23233 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
23234 return;
23235 }
a32a8923 23236 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
23237
23238 /* First pass: Find the name of the base filename.
23239 This filename is needed in order to process all macros whose definition
23240 (or undefinition) comes from the command line. These macros are defined
23241 before the first DW_MACINFO_start_file entry, and yet still need to be
23242 associated to the base file.
23243
23244 To determine the base file name, we scan the macro definitions until we
23245 reach the first DW_MACINFO_start_file entry. We then initialize
23246 CURRENT_FILE accordingly so that any macro definition found before the
23247 first DW_MACINFO_start_file can still be associated to the base file. */
23248
23249 mac_ptr = section->buffer + offset;
23250 mac_end = section->buffer + section->size;
23251
23252 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23253 &offset_size, section_is_gnu);
23254 if (mac_ptr == NULL)
23255 {
23256 /* We already issued a complaint. */
23257 return;
23258 }
23259
23260 do
23261 {
23262 /* Do we at least have room for a macinfo type byte? */
23263 if (mac_ptr >= mac_end)
23264 {
23265 /* Complaint is printed during the second pass as GDB will probably
23266 stop the first pass earlier upon finding
23267 DW_MACINFO_start_file. */
23268 break;
23269 }
23270
aead7601 23271 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
23272 mac_ptr++;
23273
23274 /* Note that we rely on the fact that the corresponding GNU and
23275 DWARF constants are the same. */
23276 switch (macinfo_type)
23277 {
23278 /* A zero macinfo type indicates the end of the macro
23279 information. */
23280 case 0:
23281 break;
23282
0af92d60
JK
23283 case DW_MACRO_define:
23284 case DW_MACRO_undef:
cf2c3c16
TT
23285 /* Only skip the data by MAC_PTR. */
23286 {
23287 unsigned int bytes_read;
23288
23289 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23290 mac_ptr += bytes_read;
23291 read_direct_string (abfd, mac_ptr, &bytes_read);
23292 mac_ptr += bytes_read;
23293 }
23294 break;
23295
0af92d60 23296 case DW_MACRO_start_file:
cf2c3c16
TT
23297 {
23298 unsigned int bytes_read;
23299 int line, file;
23300
23301 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23302 mac_ptr += bytes_read;
23303 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23304 mac_ptr += bytes_read;
23305
43f3e411 23306 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
23307 }
23308 break;
23309
0af92d60 23310 case DW_MACRO_end_file:
cf2c3c16
TT
23311 /* No data to skip by MAC_PTR. */
23312 break;
23313
0af92d60
JK
23314 case DW_MACRO_define_strp:
23315 case DW_MACRO_undef_strp:
23316 case DW_MACRO_define_sup:
23317 case DW_MACRO_undef_sup:
cf2c3c16
TT
23318 {
23319 unsigned int bytes_read;
23320
23321 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23322 mac_ptr += bytes_read;
23323 mac_ptr += offset_size;
23324 }
23325 break;
23326
0af92d60
JK
23327 case DW_MACRO_import:
23328 case DW_MACRO_import_sup:
cf2c3c16 23329 /* Note that, according to the spec, a transparent include
0af92d60 23330 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
23331 skip this opcode. */
23332 mac_ptr += offset_size;
23333 break;
23334
23335 case DW_MACINFO_vendor_ext:
23336 /* Only skip the data by MAC_PTR. */
23337 if (!section_is_gnu)
23338 {
23339 unsigned int bytes_read;
23340
23341 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23342 mac_ptr += bytes_read;
23343 read_direct_string (abfd, mac_ptr, &bytes_read);
23344 mac_ptr += bytes_read;
23345 }
23346 /* FALLTHROUGH */
23347
23348 default:
23349 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 23350 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
23351 section);
23352 if (mac_ptr == NULL)
23353 return;
23354 break;
23355 }
23356 } while (macinfo_type != 0 && current_file == NULL);
23357
23358 /* Second pass: Process all entries.
23359
23360 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23361 command-line macro definitions/undefinitions. This flag is unset when we
23362 reach the first DW_MACINFO_start_file entry. */
23363
fc4007c9
TT
23364 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23365 htab_eq_pointer,
23366 NULL, xcalloc, xfree));
8fc3fc34 23367 mac_ptr = section->buffer + offset;
fc4007c9 23368 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 23369 *slot = (void *) mac_ptr;
8fc3fc34 23370 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 23371 current_file, lh, section,
fc4007c9
TT
23372 section_is_gnu, 0, offset_size,
23373 include_hash.get ());
cf2c3c16
TT
23374}
23375
8e19ed76 23376/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 23377 if so return true else false. */
380bca97 23378
8e19ed76 23379static int
6e5a29e1 23380attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
23381{
23382 return (attr == NULL ? 0 :
23383 attr->form == DW_FORM_block1
23384 || attr->form == DW_FORM_block2
23385 || attr->form == DW_FORM_block4
2dc7f7b3
TT
23386 || attr->form == DW_FORM_block
23387 || attr->form == DW_FORM_exprloc);
8e19ed76 23388}
4c2df51b 23389
c6a0999f
JB
23390/* Return non-zero if ATTR's value is a section offset --- classes
23391 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
23392 You may use DW_UNSND (attr) to retrieve such offsets.
23393
23394 Section 7.5.4, "Attribute Encodings", explains that no attribute
23395 may have a value that belongs to more than one of these classes; it
23396 would be ambiguous if we did, because we use the same forms for all
23397 of them. */
380bca97 23398
3690dd37 23399static int
6e5a29e1 23400attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
23401{
23402 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
23403 || attr->form == DW_FORM_data8
23404 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
23405}
23406
3690dd37
JB
23407/* Return non-zero if ATTR's value falls in the 'constant' class, or
23408 zero otherwise. When this function returns true, you can apply
23409 dwarf2_get_attr_constant_value to it.
23410
23411 However, note that for some attributes you must check
23412 attr_form_is_section_offset before using this test. DW_FORM_data4
23413 and DW_FORM_data8 are members of both the constant class, and of
23414 the classes that contain offsets into other debug sections
23415 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
23416 that, if an attribute's can be either a constant or one of the
23417 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
23418 taken as section offsets, not constants.
23419
23420 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
23421 cannot handle that. */
380bca97 23422
3690dd37 23423static int
6e5a29e1 23424attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
23425{
23426 switch (attr->form)
23427 {
23428 case DW_FORM_sdata:
23429 case DW_FORM_udata:
23430 case DW_FORM_data1:
23431 case DW_FORM_data2:
23432 case DW_FORM_data4:
23433 case DW_FORM_data8:
663c44ac 23434 case DW_FORM_implicit_const:
3690dd37
JB
23435 return 1;
23436 default:
23437 return 0;
23438 }
23439}
23440
7771576e
SA
23441
23442/* DW_ADDR is always stored already as sect_offset; despite for the forms
23443 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
23444
23445static int
6e5a29e1 23446attr_form_is_ref (const struct attribute *attr)
7771576e
SA
23447{
23448 switch (attr->form)
23449 {
23450 case DW_FORM_ref_addr:
23451 case DW_FORM_ref1:
23452 case DW_FORM_ref2:
23453 case DW_FORM_ref4:
23454 case DW_FORM_ref8:
23455 case DW_FORM_ref_udata:
23456 case DW_FORM_GNU_ref_alt:
23457 return 1;
23458 default:
23459 return 0;
23460 }
23461}
23462
3019eac3
DE
23463/* Return the .debug_loc section to use for CU.
23464 For DWO files use .debug_loc.dwo. */
23465
23466static struct dwarf2_section_info *
23467cu_debug_loc_section (struct dwarf2_cu *cu)
23468{
23469 if (cu->dwo_unit)
43988095
JK
23470 {
23471 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23472
23473 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23474 }
23475 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23476 : &dwarf2_per_objfile->loc);
3019eac3
DE
23477}
23478
8cf6f0b1
TT
23479/* A helper function that fills in a dwarf2_loclist_baton. */
23480
23481static void
23482fill_in_loclist_baton (struct dwarf2_cu *cu,
23483 struct dwarf2_loclist_baton *baton,
ff39bb5e 23484 const struct attribute *attr)
8cf6f0b1 23485{
3019eac3
DE
23486 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23487
23488 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
23489
23490 baton->per_cu = cu->per_cu;
23491 gdb_assert (baton->per_cu);
23492 /* We don't know how long the location list is, but make sure we
23493 don't run off the edge of the section. */
3019eac3
DE
23494 baton->size = section->size - DW_UNSND (attr);
23495 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 23496 baton->base_address = cu->base_address;
f664829e 23497 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23498}
23499
4c2df51b 23500static void
ff39bb5e 23501dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23502 struct dwarf2_cu *cu, int is_block)
4c2df51b 23503{
bb5ed363 23504 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23505 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23506
3690dd37 23507 if (attr_form_is_section_offset (attr)
3019eac3 23508 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23509 the section. If so, fall through to the complaint in the
23510 other branch. */
3019eac3 23511 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 23512 {
0d53c4c4 23513 struct dwarf2_loclist_baton *baton;
4c2df51b 23514
8d749320 23515 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23516
8cf6f0b1 23517 fill_in_loclist_baton (cu, baton, attr);
be391dca 23518
d00adf39 23519 if (cu->base_known == 0)
0d53c4c4 23520 complaint (&symfile_complaints,
3e43a32a
MS
23521 _("Location list used without "
23522 "specifying the CU base address."));
4c2df51b 23523
f1e6e072
TT
23524 SYMBOL_ACLASS_INDEX (sym) = (is_block
23525 ? dwarf2_loclist_block_index
23526 : dwarf2_loclist_index);
0d53c4c4
DJ
23527 SYMBOL_LOCATION_BATON (sym) = baton;
23528 }
23529 else
23530 {
23531 struct dwarf2_locexpr_baton *baton;
23532
8d749320 23533 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
23534 baton->per_cu = cu->per_cu;
23535 gdb_assert (baton->per_cu);
0d53c4c4
DJ
23536
23537 if (attr_form_is_block (attr))
23538 {
23539 /* Note that we're just copying the block's data pointer
23540 here, not the actual data. We're still pointing into the
6502dd73
DJ
23541 info_buffer for SYM's objfile; right now we never release
23542 that buffer, but when we do clean up properly this may
23543 need to change. */
0d53c4c4
DJ
23544 baton->size = DW_BLOCK (attr)->size;
23545 baton->data = DW_BLOCK (attr)->data;
23546 }
23547 else
23548 {
23549 dwarf2_invalid_attrib_class_complaint ("location description",
23550 SYMBOL_NATURAL_NAME (sym));
23551 baton->size = 0;
0d53c4c4 23552 }
6e70227d 23553
f1e6e072
TT
23554 SYMBOL_ACLASS_INDEX (sym) = (is_block
23555 ? dwarf2_locexpr_block_index
23556 : dwarf2_locexpr_index);
0d53c4c4
DJ
23557 SYMBOL_LOCATION_BATON (sym) = baton;
23558 }
4c2df51b 23559}
6502dd73 23560
9aa1f1e3
TT
23561/* Return the OBJFILE associated with the compilation unit CU. If CU
23562 came from a separate debuginfo file, then the master objfile is
23563 returned. */
ae0d2f24
UW
23564
23565struct objfile *
23566dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
23567{
9291a0cd 23568 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
23569
23570 /* Return the master objfile, so that we can report and look up the
23571 correct file containing this variable. */
23572 if (objfile->separate_debug_objfile_backlink)
23573 objfile = objfile->separate_debug_objfile_backlink;
23574
23575 return objfile;
23576}
23577
96408a79
SA
23578/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23579 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23580 CU_HEADERP first. */
23581
23582static const struct comp_unit_head *
23583per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23584 struct dwarf2_per_cu_data *per_cu)
23585{
d521ce57 23586 const gdb_byte *info_ptr;
96408a79
SA
23587
23588 if (per_cu->cu)
23589 return &per_cu->cu->header;
23590
9c541725 23591 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
23592
23593 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
23594 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23595 rcuh_kind::COMPILE);
96408a79
SA
23596
23597 return cu_headerp;
23598}
23599
ae0d2f24
UW
23600/* Return the address size given in the compilation unit header for CU. */
23601
98714339 23602int
ae0d2f24
UW
23603dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
23604{
96408a79
SA
23605 struct comp_unit_head cu_header_local;
23606 const struct comp_unit_head *cu_headerp;
c471e790 23607
96408a79
SA
23608 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23609
23610 return cu_headerp->addr_size;
ae0d2f24
UW
23611}
23612
9eae7c52
TT
23613/* Return the offset size given in the compilation unit header for CU. */
23614
23615int
23616dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
23617{
96408a79
SA
23618 struct comp_unit_head cu_header_local;
23619 const struct comp_unit_head *cu_headerp;
9c6c53f7 23620
96408a79
SA
23621 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23622
23623 return cu_headerp->offset_size;
23624}
23625
23626/* See its dwarf2loc.h declaration. */
23627
23628int
23629dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
23630{
23631 struct comp_unit_head cu_header_local;
23632 const struct comp_unit_head *cu_headerp;
23633
23634 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23635
23636 if (cu_headerp->version == 2)
23637 return cu_headerp->addr_size;
23638 else
23639 return cu_headerp->offset_size;
181cebd4
JK
23640}
23641
9aa1f1e3
TT
23642/* Return the text offset of the CU. The returned offset comes from
23643 this CU's objfile. If this objfile came from a separate debuginfo
23644 file, then the offset may be different from the corresponding
23645 offset in the parent objfile. */
23646
23647CORE_ADDR
23648dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
23649{
bb3fa9d0 23650 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
23651
23652 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23653}
23654
43988095
JK
23655/* Return DWARF version number of PER_CU. */
23656
23657short
23658dwarf2_version (struct dwarf2_per_cu_data *per_cu)
23659{
23660 return per_cu->dwarf_version;
23661}
23662
348e048f
DE
23663/* Locate the .debug_info compilation unit from CU's objfile which contains
23664 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
23665
23666static struct dwarf2_per_cu_data *
9c541725 23667dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 23668 unsigned int offset_in_dwz,
ae038cb0
DJ
23669 struct objfile *objfile)
23670{
23671 struct dwarf2_per_cu_data *this_cu;
23672 int low, high;
36586728 23673 const sect_offset *cu_off;
ae038cb0 23674
ae038cb0
DJ
23675 low = 0;
23676 high = dwarf2_per_objfile->n_comp_units - 1;
23677 while (high > low)
23678 {
36586728 23679 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23680 int mid = low + (high - low) / 2;
9a619af0 23681
36586728 23682 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 23683 cu_off = &mid_cu->sect_off;
36586728 23684 if (mid_cu->is_dwz > offset_in_dwz
9c541725 23685 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
23686 high = mid;
23687 else
23688 low = mid + 1;
23689 }
23690 gdb_assert (low == high);
36586728 23691 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
23692 cu_off = &this_cu->sect_off;
23693 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 23694 {
36586728 23695 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23696 error (_("Dwarf Error: could not find partial DIE containing "
9c541725
PA
23697 "offset 0x%x [in module %s]"),
23698 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
10b3939b 23699
9c541725
PA
23700 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23701 <= sect_off);
ae038cb0
DJ
23702 return dwarf2_per_objfile->all_comp_units[low-1];
23703 }
23704 else
23705 {
23706 this_cu = dwarf2_per_objfile->all_comp_units[low];
23707 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725
PA
23708 && sect_off >= this_cu->sect_off + this_cu->length)
23709 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
23710 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23711 return this_cu;
23712 }
23713}
23714
23745b47 23715/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 23716
9816fde3 23717static void
23745b47 23718init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 23719{
9816fde3 23720 memset (cu, 0, sizeof (*cu));
23745b47
DE
23721 per_cu->cu = cu;
23722 cu->per_cu = per_cu;
23723 cu->objfile = per_cu->objfile;
93311388 23724 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
23725}
23726
23727/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23728
23729static void
95554aad
TT
23730prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23731 enum language pretend_language)
9816fde3
JK
23732{
23733 struct attribute *attr;
23734
23735 /* Set the language we're debugging. */
23736 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23737 if (attr)
23738 set_cu_language (DW_UNSND (attr), cu);
23739 else
9cded63f 23740 {
95554aad 23741 cu->language = pretend_language;
9cded63f
TT
23742 cu->language_defn = language_def (cu->language);
23743 }
dee91e82 23744
7d45c7c3 23745 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23746}
23747
ae038cb0
DJ
23748/* Release one cached compilation unit, CU. We unlink it from the tree
23749 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
23750 the caller is responsible for that.
23751 NOTE: DATA is a void * because this function is also used as a
23752 cleanup routine. */
ae038cb0
DJ
23753
23754static void
68dc6402 23755free_heap_comp_unit (void *data)
ae038cb0 23756{
9a3c8263 23757 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 23758
23745b47
DE
23759 gdb_assert (cu->per_cu != NULL);
23760 cu->per_cu->cu = NULL;
ae038cb0
DJ
23761 cu->per_cu = NULL;
23762
23763 obstack_free (&cu->comp_unit_obstack, NULL);
23764
23765 xfree (cu);
23766}
23767
72bf9492 23768/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 23769 when we're finished with it. We can't free the pointer itself, but be
dee91e82 23770 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
23771
23772static void
23773free_stack_comp_unit (void *data)
23774{
9a3c8263 23775 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 23776
23745b47
DE
23777 gdb_assert (cu->per_cu != NULL);
23778 cu->per_cu->cu = NULL;
23779 cu->per_cu = NULL;
23780
72bf9492
DJ
23781 obstack_free (&cu->comp_unit_obstack, NULL);
23782 cu->partial_dies = NULL;
ae038cb0
DJ
23783}
23784
23785/* Free all cached compilation units. */
23786
23787static void
23788free_cached_comp_units (void *data)
23789{
330cdd98 23790 dwarf2_per_objfile->free_cached_comp_units ();
ae038cb0
DJ
23791}
23792
23793/* Increase the age counter on each cached compilation unit, and free
23794 any that are too old. */
23795
23796static void
23797age_cached_comp_units (void)
23798{
23799 struct dwarf2_per_cu_data *per_cu, **last_chain;
23800
23801 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23802 per_cu = dwarf2_per_objfile->read_in_chain;
23803 while (per_cu != NULL)
23804 {
23805 per_cu->cu->last_used ++;
b4f54984 23806 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23807 dwarf2_mark (per_cu->cu);
23808 per_cu = per_cu->cu->read_in_chain;
23809 }
23810
23811 per_cu = dwarf2_per_objfile->read_in_chain;
23812 last_chain = &dwarf2_per_objfile->read_in_chain;
23813 while (per_cu != NULL)
23814 {
23815 struct dwarf2_per_cu_data *next_cu;
23816
23817 next_cu = per_cu->cu->read_in_chain;
23818
23819 if (!per_cu->cu->mark)
23820 {
68dc6402 23821 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
23822 *last_chain = next_cu;
23823 }
23824 else
23825 last_chain = &per_cu->cu->read_in_chain;
23826
23827 per_cu = next_cu;
23828 }
23829}
23830
23831/* Remove a single compilation unit from the cache. */
23832
23833static void
dee91e82 23834free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
23835{
23836 struct dwarf2_per_cu_data *per_cu, **last_chain;
23837
23838 per_cu = dwarf2_per_objfile->read_in_chain;
23839 last_chain = &dwarf2_per_objfile->read_in_chain;
23840 while (per_cu != NULL)
23841 {
23842 struct dwarf2_per_cu_data *next_cu;
23843
23844 next_cu = per_cu->cu->read_in_chain;
23845
dee91e82 23846 if (per_cu == target_per_cu)
ae038cb0 23847 {
68dc6402 23848 free_heap_comp_unit (per_cu->cu);
dee91e82 23849 per_cu->cu = NULL;
ae038cb0
DJ
23850 *last_chain = next_cu;
23851 break;
23852 }
23853 else
23854 last_chain = &per_cu->cu->read_in_chain;
23855
23856 per_cu = next_cu;
23857 }
23858}
23859
fe3e1990
DJ
23860/* Release all extra memory associated with OBJFILE. */
23861
23862void
23863dwarf2_free_objfile (struct objfile *objfile)
23864{
9a3c8263
SM
23865 dwarf2_per_objfile
23866 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23867 dwarf2_objfile_data_key);
fe3e1990
DJ
23868
23869 if (dwarf2_per_objfile == NULL)
23870 return;
23871
330cdd98 23872 dwarf2_per_objfile->~dwarf2_per_objfile ();
fe3e1990
DJ
23873}
23874
dee91e82
DE
23875/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23876 We store these in a hash table separate from the DIEs, and preserve them
23877 when the DIEs are flushed out of cache.
23878
23879 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23880 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23881 or the type may come from a DWO file. Furthermore, while it's more logical
23882 to use per_cu->section+offset, with Fission the section with the data is in
23883 the DWO file but we don't know that section at the point we need it.
23884 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23885 because we can enter the lookup routine, get_die_type_at_offset, from
23886 outside this file, and thus won't necessarily have PER_CU->cu.
23887 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23888
dee91e82 23889struct dwarf2_per_cu_offset_and_type
1c379e20 23890{
dee91e82 23891 const struct dwarf2_per_cu_data *per_cu;
9c541725 23892 sect_offset sect_off;
1c379e20
DJ
23893 struct type *type;
23894};
23895
dee91e82 23896/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23897
23898static hashval_t
dee91e82 23899per_cu_offset_and_type_hash (const void *item)
1c379e20 23900{
9a3c8263
SM
23901 const struct dwarf2_per_cu_offset_and_type *ofs
23902 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23903
9c541725 23904 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23905}
23906
dee91e82 23907/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23908
23909static int
dee91e82 23910per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23911{
9a3c8263
SM
23912 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23913 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23914 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23915 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23916
dee91e82 23917 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23918 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23919}
23920
23921/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23922 table if necessary. For convenience, return TYPE.
23923
23924 The DIEs reading must have careful ordering to:
23925 * Not cause infite loops trying to read in DIEs as a prerequisite for
23926 reading current DIE.
23927 * Not trying to dereference contents of still incompletely read in types
23928 while reading in other DIEs.
23929 * Enable referencing still incompletely read in types just by a pointer to
23930 the type without accessing its fields.
23931
23932 Therefore caller should follow these rules:
23933 * Try to fetch any prerequisite types we may need to build this DIE type
23934 before building the type and calling set_die_type.
e71ec853 23935 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23936 possible before fetching more types to complete the current type.
23937 * Make the type as complete as possible before fetching more types. */
1c379e20 23938
f792889a 23939static struct type *
1c379e20
DJ
23940set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23941{
dee91e82 23942 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 23943 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
23944 struct attribute *attr;
23945 struct dynamic_prop prop;
1c379e20 23946
b4ba55a1
JB
23947 /* For Ada types, make sure that the gnat-specific data is always
23948 initialized (if not already set). There are a few types where
23949 we should not be doing so, because the type-specific area is
23950 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23951 where the type-specific area is used to store the floatformat).
23952 But this is not a problem, because the gnat-specific information
23953 is actually not needed for these types. */
23954 if (need_gnat_info (cu)
23955 && TYPE_CODE (type) != TYPE_CODE_FUNC
23956 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
23957 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23958 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23959 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
23960 && !HAVE_GNAT_AUX_INFO (type))
23961 INIT_GNAT_SPECIFIC (type);
23962
3f2f83dd
KB
23963 /* Read DW_AT_allocated and set in type. */
23964 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23965 if (attr_form_is_block (attr))
23966 {
23967 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23968 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
23969 }
23970 else if (attr != NULL)
23971 {
23972 complaint (&symfile_complaints,
9c541725
PA
23973 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
23974 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23975 to_underlying (die->sect_off));
3f2f83dd
KB
23976 }
23977
23978 /* Read DW_AT_associated and set in type. */
23979 attr = dwarf2_attr (die, DW_AT_associated, cu);
23980 if (attr_form_is_block (attr))
23981 {
23982 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23983 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
23984 }
23985 else if (attr != NULL)
23986 {
23987 complaint (&symfile_complaints,
9c541725
PA
23988 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
23989 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23990 to_underlying (die->sect_off));
3f2f83dd
KB
23991 }
23992
3cdcd0ce
JB
23993 /* Read DW_AT_data_location and set in type. */
23994 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23995 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 23996 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 23997
dee91e82 23998 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23999 {
dee91e82
DE
24000 dwarf2_per_objfile->die_type_hash =
24001 htab_create_alloc_ex (127,
24002 per_cu_offset_and_type_hash,
24003 per_cu_offset_and_type_eq,
24004 NULL,
24005 &objfile->objfile_obstack,
24006 hashtab_obstack_allocate,
24007 dummy_obstack_deallocate);
f792889a 24008 }
1c379e20 24009
dee91e82 24010 ofs.per_cu = cu->per_cu;
9c541725 24011 ofs.sect_off = die->sect_off;
1c379e20 24012 ofs.type = type;
dee91e82
DE
24013 slot = (struct dwarf2_per_cu_offset_and_type **)
24014 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
24015 if (*slot)
24016 complaint (&symfile_complaints,
24017 _("A problem internal to GDB: DIE 0x%x has type already set"),
9c541725 24018 to_underlying (die->sect_off));
8d749320
SM
24019 *slot = XOBNEW (&objfile->objfile_obstack,
24020 struct dwarf2_per_cu_offset_and_type);
1c379e20 24021 **slot = ofs;
f792889a 24022 return type;
1c379e20
DJ
24023}
24024
9c541725 24025/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 24026 or return NULL if the die does not have a saved type. */
1c379e20
DJ
24027
24028static struct type *
9c541725 24029get_die_type_at_offset (sect_offset sect_off,
673bfd45 24030 struct dwarf2_per_cu_data *per_cu)
1c379e20 24031{
dee91e82 24032 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 24033
dee91e82 24034 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 24035 return NULL;
1c379e20 24036
dee91e82 24037 ofs.per_cu = per_cu;
9c541725 24038 ofs.sect_off = sect_off;
9a3c8263
SM
24039 slot = ((struct dwarf2_per_cu_offset_and_type *)
24040 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
24041 if (slot)
24042 return slot->type;
24043 else
24044 return NULL;
24045}
24046
02142a6c 24047/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
24048 or return NULL if DIE does not have a saved type. */
24049
24050static struct type *
24051get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24052{
9c541725 24053 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
24054}
24055
10b3939b
DJ
24056/* Add a dependence relationship from CU to REF_PER_CU. */
24057
24058static void
24059dwarf2_add_dependence (struct dwarf2_cu *cu,
24060 struct dwarf2_per_cu_data *ref_per_cu)
24061{
24062 void **slot;
24063
24064 if (cu->dependencies == NULL)
24065 cu->dependencies
24066 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24067 NULL, &cu->comp_unit_obstack,
24068 hashtab_obstack_allocate,
24069 dummy_obstack_deallocate);
24070
24071 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24072 if (*slot == NULL)
24073 *slot = ref_per_cu;
24074}
1c379e20 24075
f504f079
DE
24076/* Subroutine of dwarf2_mark to pass to htab_traverse.
24077 Set the mark field in every compilation unit in the
ae038cb0
DJ
24078 cache that we must keep because we are keeping CU. */
24079
10b3939b
DJ
24080static int
24081dwarf2_mark_helper (void **slot, void *data)
24082{
24083 struct dwarf2_per_cu_data *per_cu;
24084
24085 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
24086
24087 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24088 reading of the chain. As such dependencies remain valid it is not much
24089 useful to track and undo them during QUIT cleanups. */
24090 if (per_cu->cu == NULL)
24091 return 1;
24092
10b3939b
DJ
24093 if (per_cu->cu->mark)
24094 return 1;
24095 per_cu->cu->mark = 1;
24096
24097 if (per_cu->cu->dependencies != NULL)
24098 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24099
24100 return 1;
24101}
24102
f504f079
DE
24103/* Set the mark field in CU and in every other compilation unit in the
24104 cache that we must keep because we are keeping CU. */
24105
ae038cb0
DJ
24106static void
24107dwarf2_mark (struct dwarf2_cu *cu)
24108{
24109 if (cu->mark)
24110 return;
24111 cu->mark = 1;
10b3939b
DJ
24112 if (cu->dependencies != NULL)
24113 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
24114}
24115
24116static void
24117dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24118{
24119 while (per_cu)
24120 {
24121 per_cu->cu->mark = 0;
24122 per_cu = per_cu->cu->read_in_chain;
24123 }
72bf9492
DJ
24124}
24125
72bf9492
DJ
24126/* Trivial hash function for partial_die_info: the hash value of a DIE
24127 is its offset in .debug_info for this objfile. */
24128
24129static hashval_t
24130partial_die_hash (const void *item)
24131{
9a3c8263
SM
24132 const struct partial_die_info *part_die
24133 = (const struct partial_die_info *) item;
9a619af0 24134
9c541725 24135 return to_underlying (part_die->sect_off);
72bf9492
DJ
24136}
24137
24138/* Trivial comparison function for partial_die_info structures: two DIEs
24139 are equal if they have the same offset. */
24140
24141static int
24142partial_die_eq (const void *item_lhs, const void *item_rhs)
24143{
9a3c8263
SM
24144 const struct partial_die_info *part_die_lhs
24145 = (const struct partial_die_info *) item_lhs;
24146 const struct partial_die_info *part_die_rhs
24147 = (const struct partial_die_info *) item_rhs;
9a619af0 24148
9c541725 24149 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
24150}
24151
b4f54984
DE
24152static struct cmd_list_element *set_dwarf_cmdlist;
24153static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
24154
24155static void
981a3fb3 24156set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 24157{
b4f54984 24158 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 24159 gdb_stdout);
ae038cb0
DJ
24160}
24161
24162static void
981a3fb3 24163show_dwarf_cmd (const char *args, int from_tty)
6e70227d 24164{
b4f54984 24165 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
24166}
24167
4bf44c1c 24168/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
24169
24170static void
c1bd65d0 24171dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 24172{
9a3c8263 24173 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 24174 int ix;
8b70b953 24175
626f2d1c
TT
24176 /* Make sure we don't accidentally use dwarf2_per_objfile while
24177 cleaning up. */
24178 dwarf2_per_objfile = NULL;
24179
59b0c7c1
JB
24180 for (ix = 0; ix < data->n_comp_units; ++ix)
24181 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 24182
59b0c7c1 24183 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 24184 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
24185 data->all_type_units[ix]->per_cu.imported_symtabs);
24186 xfree (data->all_type_units);
95554aad 24187
8b70b953 24188 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
24189
24190 if (data->dwo_files)
24191 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
24192 if (data->dwp_file)
24193 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
24194
24195 if (data->dwz_file && data->dwz_file->dwz_bfd)
24196 gdb_bfd_unref (data->dwz_file->dwz_bfd);
3f563c84
PA
24197
24198 if (data->index_table != NULL)
24199 data->index_table->~mapped_index ();
9291a0cd
TT
24200}
24201
24202\f
ae2de4f8 24203/* The "save gdb-index" command. */
9291a0cd 24204
bc8f2430
JK
24205/* In-memory buffer to prepare data to be written later to a file. */
24206class data_buf
9291a0cd 24207{
bc8f2430 24208public:
bc8f2430
JK
24209 /* Copy DATA to the end of the buffer. */
24210 template<typename T>
24211 void append_data (const T &data)
24212 {
24213 std::copy (reinterpret_cast<const gdb_byte *> (&data),
24214 reinterpret_cast<const gdb_byte *> (&data + 1),
c2f134ac 24215 grow (sizeof (data)));
bc8f2430 24216 }
b89be57b 24217
c2f134ac
PA
24218 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
24219 terminating zero is appended too. */
bc8f2430
JK
24220 void append_cstr0 (const char *cstr)
24221 {
24222 const size_t size = strlen (cstr) + 1;
c2f134ac
PA
24223 std::copy (cstr, cstr + size, grow (size));
24224 }
24225
24226 /* Accept a host-format integer in VAL and append it to the buffer
24227 as a target-format integer which is LEN bytes long. */
24228 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
24229 {
24230 ::store_unsigned_integer (grow (len), len, byte_order, val);
bc8f2430 24231 }
9291a0cd 24232
bc8f2430
JK
24233 /* Return the size of the buffer. */
24234 size_t size () const
24235 {
24236 return m_vec.size ();
24237 }
24238
24239 /* Write the buffer to FILE. */
24240 void file_write (FILE *file) const
24241 {
a81e6d4d
PA
24242 if (::fwrite (m_vec.data (), 1, m_vec.size (), file) != m_vec.size ())
24243 error (_("couldn't write data to file"));
bc8f2430
JK
24244 }
24245
24246private:
c2f134ac
PA
24247 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
24248 the start of the new block. */
24249 gdb_byte *grow (size_t size)
24250 {
24251 m_vec.resize (m_vec.size () + size);
24252 return &*m_vec.end () - size;
24253 }
24254
d5722aa2 24255 gdb::byte_vector m_vec;
bc8f2430 24256};
9291a0cd
TT
24257
24258/* An entry in the symbol table. */
24259struct symtab_index_entry
24260{
24261 /* The name of the symbol. */
24262 const char *name;
24263 /* The offset of the name in the constant pool. */
24264 offset_type index_offset;
24265 /* A sorted vector of the indices of all the CUs that hold an object
24266 of this name. */
bc8f2430 24267 std::vector<offset_type> cu_indices;
9291a0cd
TT
24268};
24269
24270/* The symbol table. This is a power-of-2-sized hash table. */
24271struct mapped_symtab
24272{
bc8f2430
JK
24273 mapped_symtab ()
24274 {
24275 data.resize (1024);
24276 }
b89be57b 24277
bc8f2430 24278 offset_type n_elements = 0;
4b76cda9 24279 std::vector<symtab_index_entry> data;
bc8f2430 24280};
9291a0cd 24281
bc8f2430 24282/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
559a7a62
JK
24283 the slot.
24284
24285 Function is used only during write_hash_table so no index format backward
24286 compatibility is needed. */
b89be57b 24287
4b76cda9 24288static symtab_index_entry &
9291a0cd
TT
24289find_slot (struct mapped_symtab *symtab, const char *name)
24290{
559a7a62 24291 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd 24292
bc8f2430
JK
24293 index = hash & (symtab->data.size () - 1);
24294 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
9291a0cd
TT
24295
24296 for (;;)
24297 {
4b76cda9
PA
24298 if (symtab->data[index].name == NULL
24299 || strcmp (name, symtab->data[index].name) == 0)
bc8f2430
JK
24300 return symtab->data[index];
24301 index = (index + step) & (symtab->data.size () - 1);
9291a0cd
TT
24302 }
24303}
24304
24305/* Expand SYMTAB's hash table. */
b89be57b 24306
9291a0cd
TT
24307static void
24308hash_expand (struct mapped_symtab *symtab)
24309{
bc8f2430 24310 auto old_entries = std::move (symtab->data);
9291a0cd 24311
bc8f2430
JK
24312 symtab->data.clear ();
24313 symtab->data.resize (old_entries.size () * 2);
9291a0cd 24314
bc8f2430 24315 for (auto &it : old_entries)
4b76cda9 24316 if (it.name != NULL)
bc8f2430 24317 {
4b76cda9 24318 auto &ref = find_slot (symtab, it.name);
bc8f2430
JK
24319 ref = std::move (it);
24320 }
9291a0cd
TT
24321}
24322
156942c7
DE
24323/* Add an entry to SYMTAB. NAME is the name of the symbol.
24324 CU_INDEX is the index of the CU in which the symbol appears.
24325 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 24326
9291a0cd
TT
24327static void
24328add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 24329 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
24330 offset_type cu_index)
24331{
156942c7 24332 offset_type cu_index_and_attrs;
9291a0cd
TT
24333
24334 ++symtab->n_elements;
bc8f2430 24335 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
9291a0cd
TT
24336 hash_expand (symtab);
24337
4b76cda9
PA
24338 symtab_index_entry &slot = find_slot (symtab, name);
24339 if (slot.name == NULL)
9291a0cd 24340 {
4b76cda9 24341 slot.name = name;
156942c7 24342 /* index_offset is set later. */
9291a0cd 24343 }
156942c7
DE
24344
24345 cu_index_and_attrs = 0;
24346 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
24347 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
24348 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
24349
24350 /* We don't want to record an index value twice as we want to avoid the
24351 duplication.
24352 We process all global symbols and then all static symbols
24353 (which would allow us to avoid the duplication by only having to check
24354 the last entry pushed), but a symbol could have multiple kinds in one CU.
24355 To keep things simple we don't worry about the duplication here and
24356 sort and uniqufy the list after we've processed all symbols. */
4b76cda9 24357 slot.cu_indices.push_back (cu_index_and_attrs);
156942c7
DE
24358}
24359
24360/* Sort and remove duplicates of all symbols' cu_indices lists. */
24361
24362static void
24363uniquify_cu_indices (struct mapped_symtab *symtab)
24364{
4b76cda9 24365 for (auto &entry : symtab->data)
156942c7 24366 {
4b76cda9 24367 if (entry.name != NULL && !entry.cu_indices.empty ())
156942c7 24368 {
4b76cda9 24369 auto &cu_indices = entry.cu_indices;
6fd931f2
PA
24370 std::sort (cu_indices.begin (), cu_indices.end ());
24371 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
24372 cu_indices.erase (from, cu_indices.end ());
156942c7
DE
24373 }
24374 }
9291a0cd
TT
24375}
24376
bc8f2430
JK
24377/* A form of 'const char *' suitable for container keys. Only the
24378 pointer is stored. The strings themselves are compared, not the
24379 pointers. */
24380class c_str_view
9291a0cd 24381{
bc8f2430
JK
24382public:
24383 c_str_view (const char *cstr)
24384 : m_cstr (cstr)
24385 {}
9291a0cd 24386
bc8f2430
JK
24387 bool operator== (const c_str_view &other) const
24388 {
24389 return strcmp (m_cstr, other.m_cstr) == 0;
24390 }
9291a0cd 24391
bc8f2430
JK
24392private:
24393 friend class c_str_view_hasher;
24394 const char *const m_cstr;
24395};
9291a0cd 24396
bc8f2430
JK
24397/* A std::unordered_map::hasher for c_str_view that uses the right
24398 hash function for strings in a mapped index. */
24399class c_str_view_hasher
24400{
24401public:
24402 size_t operator () (const c_str_view &x) const
24403 {
24404 return mapped_index_string_hash (INT_MAX, x.m_cstr);
24405 }
24406};
b89be57b 24407
bc8f2430
JK
24408/* A std::unordered_map::hasher for std::vector<>. */
24409template<typename T>
24410class vector_hasher
9291a0cd 24411{
bc8f2430
JK
24412public:
24413 size_t operator () (const std::vector<T> &key) const
24414 {
24415 return iterative_hash (key.data (),
24416 sizeof (key.front ()) * key.size (), 0);
24417 }
24418};
9291a0cd 24419
bc8f2430
JK
24420/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
24421 constant pool entries going into the data buffer CPOOL. */
3876f04e 24422
bc8f2430
JK
24423static void
24424write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
24425{
24426 {
24427 /* Elements are sorted vectors of the indices of all the CUs that
24428 hold an object of this name. */
24429 std::unordered_map<std::vector<offset_type>, offset_type,
24430 vector_hasher<offset_type>>
24431 symbol_hash_table;
24432
24433 /* We add all the index vectors to the constant pool first, to
24434 ensure alignment is ok. */
4b76cda9 24435 for (symtab_index_entry &entry : symtab->data)
bc8f2430 24436 {
4b76cda9 24437 if (entry.name == NULL)
bc8f2430 24438 continue;
4b76cda9 24439 gdb_assert (entry.index_offset == 0);
70a1152b
PA
24440
24441 /* Finding before inserting is faster than always trying to
24442 insert, because inserting always allocates a node, does the
24443 lookup, and then destroys the new node if another node
24444 already had the same key. C++17 try_emplace will avoid
24445 this. */
24446 const auto found
4b76cda9 24447 = symbol_hash_table.find (entry.cu_indices);
70a1152b
PA
24448 if (found != symbol_hash_table.end ())
24449 {
4b76cda9 24450 entry.index_offset = found->second;
70a1152b
PA
24451 continue;
24452 }
24453
4b76cda9
PA
24454 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
24455 entry.index_offset = cpool.size ();
24456 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
24457 for (const auto index : entry.cu_indices)
24458 cpool.append_data (MAYBE_SWAP (index));
bc8f2430
JK
24459 }
24460 }
9291a0cd
TT
24461
24462 /* Now write out the hash table. */
bc8f2430 24463 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
4b76cda9 24464 for (const auto &entry : symtab->data)
9291a0cd
TT
24465 {
24466 offset_type str_off, vec_off;
24467
4b76cda9 24468 if (entry.name != NULL)
9291a0cd 24469 {
4b76cda9 24470 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
bc8f2430 24471 if (insertpair.second)
4b76cda9 24472 cpool.append_cstr0 (entry.name);
bc8f2430 24473 str_off = insertpair.first->second;
4b76cda9 24474 vec_off = entry.index_offset;
9291a0cd
TT
24475 }
24476 else
24477 {
24478 /* While 0 is a valid constant pool index, it is not valid
24479 to have 0 for both offsets. */
24480 str_off = 0;
24481 vec_off = 0;
24482 }
24483
bc8f2430
JK
24484 output.append_data (MAYBE_SWAP (str_off));
24485 output.append_data (MAYBE_SWAP (vec_off));
9291a0cd 24486 }
9291a0cd
TT
24487}
24488
bc8f2430 24489typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
0a5429f6
DE
24490
24491/* Helper struct for building the address table. */
24492struct addrmap_index_data
24493{
bc8f2430
JK
24494 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
24495 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
24496 {}
24497
0a5429f6 24498 struct objfile *objfile;
bc8f2430
JK
24499 data_buf &addr_vec;
24500 psym_index_map &cu_index_htab;
0a5429f6
DE
24501
24502 /* Non-zero if the previous_* fields are valid.
24503 We can't write an entry until we see the next entry (since it is only then
24504 that we know the end of the entry). */
24505 int previous_valid;
24506 /* Index of the CU in the table of all CUs in the index file. */
24507 unsigned int previous_cu_index;
0963b4bd 24508 /* Start address of the CU. */
0a5429f6
DE
24509 CORE_ADDR previous_cu_start;
24510};
24511
bc8f2430 24512/* Write an address entry to ADDR_VEC. */
b89be57b 24513
9291a0cd 24514static void
bc8f2430 24515add_address_entry (struct objfile *objfile, data_buf &addr_vec,
0a5429f6 24516 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 24517{
9291a0cd
TT
24518 CORE_ADDR baseaddr;
24519
24520 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24521
c2f134ac
PA
24522 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
24523 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
bc8f2430 24524 addr_vec.append_data (MAYBE_SWAP (cu_index));
0a5429f6
DE
24525}
24526
24527/* Worker function for traversing an addrmap to build the address table. */
24528
24529static int
24530add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
24531{
9a3c8263
SM
24532 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
24533 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
24534
24535 if (data->previous_valid)
bc8f2430 24536 add_address_entry (data->objfile, data->addr_vec,
0a5429f6
DE
24537 data->previous_cu_start, start_addr,
24538 data->previous_cu_index);
24539
24540 data->previous_cu_start = start_addr;
24541 if (pst != NULL)
24542 {
bc8f2430
JK
24543 const auto it = data->cu_index_htab.find (pst);
24544 gdb_assert (it != data->cu_index_htab.cend ());
24545 data->previous_cu_index = it->second;
0a5429f6
DE
24546 data->previous_valid = 1;
24547 }
24548 else
bc8f2430 24549 data->previous_valid = 0;
0a5429f6
DE
24550
24551 return 0;
24552}
24553
bc8f2430 24554/* Write OBJFILE's address map to ADDR_VEC.
0a5429f6
DE
24555 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
24556 in the index file. */
24557
24558static void
bc8f2430
JK
24559write_address_map (struct objfile *objfile, data_buf &addr_vec,
24560 psym_index_map &cu_index_htab)
0a5429f6 24561{
bc8f2430 24562 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
0a5429f6
DE
24563
24564 /* When writing the address table, we have to cope with the fact that
24565 the addrmap iterator only provides the start of a region; we have to
24566 wait until the next invocation to get the start of the next region. */
24567
24568 addrmap_index_data.objfile = objfile;
0a5429f6
DE
24569 addrmap_index_data.previous_valid = 0;
24570
24571 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
24572 &addrmap_index_data);
24573
24574 /* It's highly unlikely the last entry (end address = 0xff...ff)
24575 is valid, but we should still handle it.
24576 The end address is recorded as the start of the next region, but that
24577 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
24578 anyway. */
24579 if (addrmap_index_data.previous_valid)
bc8f2430 24580 add_address_entry (objfile, addr_vec,
0a5429f6
DE
24581 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
24582 addrmap_index_data.previous_cu_index);
9291a0cd
TT
24583}
24584
156942c7
DE
24585/* Return the symbol kind of PSYM. */
24586
24587static gdb_index_symbol_kind
24588symbol_kind (struct partial_symbol *psym)
24589{
24590 domain_enum domain = PSYMBOL_DOMAIN (psym);
24591 enum address_class aclass = PSYMBOL_CLASS (psym);
24592
24593 switch (domain)
24594 {
24595 case VAR_DOMAIN:
24596 switch (aclass)
24597 {
24598 case LOC_BLOCK:
24599 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
24600 case LOC_TYPEDEF:
24601 return GDB_INDEX_SYMBOL_KIND_TYPE;
24602 case LOC_COMPUTED:
24603 case LOC_CONST_BYTES:
24604 case LOC_OPTIMIZED_OUT:
24605 case LOC_STATIC:
24606 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
24607 case LOC_CONST:
24608 /* Note: It's currently impossible to recognize psyms as enum values
24609 short of reading the type info. For now punt. */
24610 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
24611 default:
24612 /* There are other LOC_FOO values that one might want to classify
24613 as variables, but dwarf2read.c doesn't currently use them. */
24614 return GDB_INDEX_SYMBOL_KIND_OTHER;
24615 }
24616 case STRUCT_DOMAIN:
24617 return GDB_INDEX_SYMBOL_KIND_TYPE;
24618 default:
24619 return GDB_INDEX_SYMBOL_KIND_OTHER;
24620 }
24621}
24622
9291a0cd 24623/* Add a list of partial symbols to SYMTAB. */
b89be57b 24624
9291a0cd
TT
24625static void
24626write_psymbols (struct mapped_symtab *symtab,
bc8f2430 24627 std::unordered_set<partial_symbol *> &psyms_seen,
9291a0cd
TT
24628 struct partial_symbol **psymp,
24629 int count,
987d643c
TT
24630 offset_type cu_index,
24631 int is_static)
9291a0cd
TT
24632{
24633 for (; count-- > 0; ++psymp)
24634 {
156942c7 24635 struct partial_symbol *psym = *psymp;
987d643c 24636
156942c7 24637 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 24638 error (_("Ada is not currently supported by the index"));
987d643c 24639
987d643c 24640 /* Only add a given psymbol once. */
bc8f2430 24641 if (psyms_seen.insert (psym).second)
987d643c 24642 {
156942c7
DE
24643 gdb_index_symbol_kind kind = symbol_kind (psym);
24644
156942c7
DE
24645 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
24646 is_static, kind, cu_index);
987d643c 24647 }
9291a0cd
TT
24648 }
24649}
24650
1fd400ff
TT
24651/* A helper struct used when iterating over debug_types. */
24652struct signatured_type_index_data
24653{
bc8f2430
JK
24654 signatured_type_index_data (data_buf &types_list_,
24655 std::unordered_set<partial_symbol *> &psyms_seen_)
24656 : types_list (types_list_), psyms_seen (psyms_seen_)
24657 {}
24658
1fd400ff
TT
24659 struct objfile *objfile;
24660 struct mapped_symtab *symtab;
bc8f2430
JK
24661 data_buf &types_list;
24662 std::unordered_set<partial_symbol *> &psyms_seen;
1fd400ff
TT
24663 int cu_index;
24664};
24665
24666/* A helper function that writes a single signatured_type to an
24667 obstack. */
b89be57b 24668
1fd400ff
TT
24669static int
24670write_one_signatured_type (void **slot, void *d)
24671{
9a3c8263
SM
24672 struct signatured_type_index_data *info
24673 = (struct signatured_type_index_data *) d;
1fd400ff 24674 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 24675 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
24676
24677 write_psymbols (info->symtab,
987d643c 24678 info->psyms_seen,
af5bf4ad 24679 &info->objfile->global_psymbols[psymtab->globals_offset],
987d643c
TT
24680 psymtab->n_global_syms, info->cu_index,
24681 0);
1fd400ff 24682 write_psymbols (info->symtab,
987d643c 24683 info->psyms_seen,
af5bf4ad 24684 &info->objfile->static_psymbols[psymtab->statics_offset],
987d643c
TT
24685 psymtab->n_static_syms, info->cu_index,
24686 1);
1fd400ff 24687
c2f134ac
PA
24688 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
24689 to_underlying (entry->per_cu.sect_off));
24690 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
24691 to_underlying (entry->type_offset_in_tu));
24692 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
1fd400ff
TT
24693
24694 ++info->cu_index;
24695
24696 return 1;
24697}
24698
e8f8bcb3
PA
24699/* Recurse into all "included" dependencies and count their symbols as
24700 if they appeared in this psymtab. */
24701
24702static void
24703recursively_count_psymbols (struct partial_symtab *psymtab,
24704 size_t &psyms_seen)
24705{
24706 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
24707 if (psymtab->dependencies[i]->user != NULL)
24708 recursively_count_psymbols (psymtab->dependencies[i],
24709 psyms_seen);
24710
24711 psyms_seen += psymtab->n_global_syms;
24712 psyms_seen += psymtab->n_static_syms;
24713}
24714
95554aad
TT
24715/* Recurse into all "included" dependencies and write their symbols as
24716 if they appeared in this psymtab. */
24717
24718static void
24719recursively_write_psymbols (struct objfile *objfile,
24720 struct partial_symtab *psymtab,
24721 struct mapped_symtab *symtab,
bc8f2430 24722 std::unordered_set<partial_symbol *> &psyms_seen,
95554aad
TT
24723 offset_type cu_index)
24724{
24725 int i;
24726
24727 for (i = 0; i < psymtab->number_of_dependencies; ++i)
24728 if (psymtab->dependencies[i]->user != NULL)
24729 recursively_write_psymbols (objfile, psymtab->dependencies[i],
24730 symtab, psyms_seen, cu_index);
24731
24732 write_psymbols (symtab,
24733 psyms_seen,
af5bf4ad 24734 &objfile->global_psymbols[psymtab->globals_offset],
95554aad
TT
24735 psymtab->n_global_syms, cu_index,
24736 0);
24737 write_psymbols (symtab,
24738 psyms_seen,
af5bf4ad 24739 &objfile->static_psymbols[psymtab->statics_offset],
95554aad
TT
24740 psymtab->n_static_syms, cu_index,
24741 1);
24742}
24743
9291a0cd 24744/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 24745
9291a0cd
TT
24746static void
24747write_psymtabs_to_index (struct objfile *objfile, const char *dir)
24748{
9291a0cd
TT
24749 if (dwarf2_per_objfile->using_index)
24750 error (_("Cannot use an index to create the index"));
24751
8b70b953
TT
24752 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
24753 error (_("Cannot make an index when the file has multiple .debug_types sections"));
24754
260b681b
DE
24755 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
24756 return;
24757
bc8f2430 24758 struct stat st;
4262abfb
JK
24759 if (stat (objfile_name (objfile), &st) < 0)
24760 perror_with_name (objfile_name (objfile));
9291a0cd 24761
bc8f2430
JK
24762 std::string filename (std::string (dir) + SLASH_STRING
24763 + lbasename (objfile_name (objfile)) + INDEX_SUFFIX);
9291a0cd 24764
d419f42d 24765 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
9291a0cd 24766 if (!out_file)
bc8f2430 24767 error (_("Can't open `%s' for writing"), filename.c_str ());
9291a0cd 24768
16b7a719
PA
24769 /* Order matters here; we want FILE to be closed before FILENAME is
24770 unlinked, because on MS-Windows one cannot delete a file that is
24771 still open. (Don't call anything here that might throw until
24772 file_closer is created.) */
bc8f2430 24773 gdb::unlinker unlink_file (filename.c_str ());
d419f42d 24774 gdb_file_up close_out_file (out_file);
9291a0cd 24775
bc8f2430
JK
24776 mapped_symtab symtab;
24777 data_buf cu_list;
987d643c 24778
0a5429f6
DE
24779 /* While we're scanning CU's create a table that maps a psymtab pointer
24780 (which is what addrmap records) to its index (which is what is recorded
24781 in the index file). This will later be needed to write the address
24782 table. */
bc8f2430
JK
24783 psym_index_map cu_index_htab;
24784 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
0a5429f6
DE
24785
24786 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
24787 work here. Also, the debug_types entries do not appear in
24788 all_comp_units, but only in their own hash table. */
e8f8bcb3
PA
24789
24790 /* The psyms_seen set is potentially going to be largish (~40k
24791 elements when indexing a -g3 build of GDB itself). Estimate the
24792 number of elements in order to avoid too many rehashes, which
24793 require rebuilding buckets and thus many trips to
24794 malloc/free. */
24795 size_t psyms_count = 0;
24796 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24797 {
24798 struct dwarf2_per_cu_data *per_cu
24799 = dwarf2_per_objfile->all_comp_units[i];
24800 struct partial_symtab *psymtab = per_cu->v.psymtab;
24801
24802 if (psymtab != NULL && psymtab->user == NULL)
24803 recursively_count_psymbols (psymtab, psyms_count);
24804 }
24805 /* Generating an index for gdb itself shows a ratio of
24806 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
24807 std::unordered_set<partial_symbol *> psyms_seen (psyms_count / 4);
bc8f2430 24808 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 24809 {
3e43a32a
MS
24810 struct dwarf2_per_cu_data *per_cu
24811 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 24812 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 24813
92fac807
JK
24814 /* CU of a shared file from 'dwz -m' may be unused by this main file.
24815 It may be referenced from a local scope but in such case it does not
24816 need to be present in .gdb_index. */
24817 if (psymtab == NULL)
24818 continue;
24819
95554aad 24820 if (psymtab->user == NULL)
bc8f2430
JK
24821 recursively_write_psymbols (objfile, psymtab, &symtab,
24822 psyms_seen, i);
9291a0cd 24823
bc8f2430
JK
24824 const auto insertpair = cu_index_htab.emplace (psymtab, i);
24825 gdb_assert (insertpair.second);
9291a0cd 24826
c2f134ac
PA
24827 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
24828 to_underlying (per_cu->sect_off));
24829 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
24830 }
24831
0a5429f6 24832 /* Dump the address map. */
bc8f2430
JK
24833 data_buf addr_vec;
24834 write_address_map (objfile, addr_vec, cu_index_htab);
0a5429f6 24835
1fd400ff 24836 /* Write out the .debug_type entries, if any. */
bc8f2430 24837 data_buf types_cu_list;
1fd400ff
TT
24838 if (dwarf2_per_objfile->signatured_types)
24839 {
bc8f2430
JK
24840 signatured_type_index_data sig_data (types_cu_list,
24841 psyms_seen);
1fd400ff
TT
24842
24843 sig_data.objfile = objfile;
bc8f2430 24844 sig_data.symtab = &symtab;
1fd400ff
TT
24845 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
24846 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
24847 write_one_signatured_type, &sig_data);
24848 }
24849
156942c7
DE
24850 /* Now that we've processed all symbols we can shrink their cu_indices
24851 lists. */
bc8f2430 24852 uniquify_cu_indices (&symtab);
156942c7 24853
bc8f2430
JK
24854 data_buf symtab_vec, constant_pool;
24855 write_hash_table (&symtab, symtab_vec, constant_pool);
9291a0cd 24856
bc8f2430
JK
24857 data_buf contents;
24858 const offset_type size_of_contents = 6 * sizeof (offset_type);
24859 offset_type total_len = size_of_contents;
9291a0cd
TT
24860
24861 /* The version number. */
bc8f2430 24862 contents.append_data (MAYBE_SWAP (8));
9291a0cd
TT
24863
24864 /* The offset of the CU list from the start of the file. */
bc8f2430
JK
24865 contents.append_data (MAYBE_SWAP (total_len));
24866 total_len += cu_list.size ();
9291a0cd 24867
1fd400ff 24868 /* The offset of the types CU list from the start of the file. */
bc8f2430
JK
24869 contents.append_data (MAYBE_SWAP (total_len));
24870 total_len += types_cu_list.size ();
1fd400ff 24871
9291a0cd 24872 /* The offset of the address table from the start of the file. */
bc8f2430
JK
24873 contents.append_data (MAYBE_SWAP (total_len));
24874 total_len += addr_vec.size ();
9291a0cd
TT
24875
24876 /* The offset of the symbol table from the start of the file. */
bc8f2430
JK
24877 contents.append_data (MAYBE_SWAP (total_len));
24878 total_len += symtab_vec.size ();
9291a0cd
TT
24879
24880 /* The offset of the constant pool from the start of the file. */
bc8f2430
JK
24881 contents.append_data (MAYBE_SWAP (total_len));
24882 total_len += constant_pool.size ();
9291a0cd 24883
bc8f2430 24884 gdb_assert (contents.size () == size_of_contents);
9291a0cd 24885
bc8f2430
JK
24886 contents.file_write (out_file);
24887 cu_list.file_write (out_file);
24888 types_cu_list.file_write (out_file);
24889 addr_vec.file_write (out_file);
24890 symtab_vec.file_write (out_file);
24891 constant_pool.file_write (out_file);
9291a0cd 24892
bef155c3
TT
24893 /* We want to keep the file. */
24894 unlink_file.keep ();
9291a0cd
TT
24895}
24896
90476074
TT
24897/* Implementation of the `save gdb-index' command.
24898
24899 Note that the file format used by this command is documented in the
24900 GDB manual. Any changes here must be documented there. */
11570e71 24901
9291a0cd 24902static void
8384c356 24903save_gdb_index_command (const char *arg, int from_tty)
9291a0cd
TT
24904{
24905 struct objfile *objfile;
24906
24907 if (!arg || !*arg)
96d19272 24908 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
24909
24910 ALL_OBJFILES (objfile)
24911 {
24912 struct stat st;
24913
24914 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 24915 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
24916 continue;
24917
9a3c8263
SM
24918 dwarf2_per_objfile
24919 = (struct dwarf2_per_objfile *) objfile_data (objfile,
24920 dwarf2_objfile_data_key);
9291a0cd
TT
24921 if (dwarf2_per_objfile)
24922 {
9291a0cd 24923
492d29ea 24924 TRY
9291a0cd
TT
24925 {
24926 write_psymtabs_to_index (objfile, arg);
24927 }
492d29ea
PA
24928 CATCH (except, RETURN_MASK_ERROR)
24929 {
24930 exception_fprintf (gdb_stderr, except,
24931 _("Error while writing index for `%s': "),
24932 objfile_name (objfile));
24933 }
24934 END_CATCH
9291a0cd
TT
24935 }
24936 }
dce234bc
PP
24937}
24938
9291a0cd
TT
24939\f
24940
b4f54984 24941int dwarf_always_disassemble;
9eae7c52
TT
24942
24943static void
b4f54984
DE
24944show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24945 struct cmd_list_element *c, const char *value)
9eae7c52 24946{
3e43a32a
MS
24947 fprintf_filtered (file,
24948 _("Whether to always disassemble "
24949 "DWARF expressions is %s.\n"),
9eae7c52
TT
24950 value);
24951}
24952
900e11f9
JK
24953static void
24954show_check_physname (struct ui_file *file, int from_tty,
24955 struct cmd_list_element *c, const char *value)
24956{
24957 fprintf_filtered (file,
24958 _("Whether to check \"physname\" is %s.\n"),
24959 value);
24960}
24961
6502dd73
DJ
24962void
24963_initialize_dwarf2_read (void)
24964{
96d19272
JK
24965 struct cmd_list_element *c;
24966
dce234bc 24967 dwarf2_objfile_data_key
c1bd65d0 24968 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 24969
b4f54984
DE
24970 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24971Set DWARF specific variables.\n\
24972Configure DWARF variables such as the cache size"),
24973 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
24974 0/*allow-unknown*/, &maintenance_set_cmdlist);
24975
b4f54984
DE
24976 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24977Show DWARF specific variables\n\
24978Show DWARF variables such as the cache size"),
24979 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
24980 0/*allow-unknown*/, &maintenance_show_cmdlist);
24981
24982 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
24983 &dwarf_max_cache_age, _("\
24984Set the upper bound on the age of cached DWARF compilation units."), _("\
24985Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
24986A higher limit means that cached compilation units will be stored\n\
24987in memory longer, and more total memory will be used. Zero disables\n\
24988caching, which can slow down startup."),
2c5b56ce 24989 NULL,
b4f54984
DE
24990 show_dwarf_max_cache_age,
24991 &set_dwarf_cmdlist,
24992 &show_dwarf_cmdlist);
d97bc12b 24993
9eae7c52 24994 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 24995 &dwarf_always_disassemble, _("\
9eae7c52
TT
24996Set whether `info address' always disassembles DWARF expressions."), _("\
24997Show whether `info address' always disassembles DWARF expressions."), _("\
24998When enabled, DWARF expressions are always printed in an assembly-like\n\
24999syntax. When disabled, expressions will be printed in a more\n\
25000conversational style, when possible."),
25001 NULL,
b4f54984
DE
25002 show_dwarf_always_disassemble,
25003 &set_dwarf_cmdlist,
25004 &show_dwarf_cmdlist);
25005
25006 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25007Set debugging of the DWARF reader."), _("\
25008Show debugging of the DWARF reader."), _("\
25009When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
25010reading and symtab expansion. A value of 1 (one) provides basic\n\
25011information. A value greater than 1 provides more verbose information."),
45cfd468
DE
25012 NULL,
25013 NULL,
25014 &setdebuglist, &showdebuglist);
25015
b4f54984
DE
25016 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25017Set debugging of the DWARF DIE reader."), _("\
25018Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
25019When enabled (non-zero), DIEs are dumped after they are read in.\n\
25020The value is the maximum depth to print."),
ccce17b0
YQ
25021 NULL,
25022 NULL,
25023 &setdebuglist, &showdebuglist);
9291a0cd 25024
27e0867f
DE
25025 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25026Set debugging of the dwarf line reader."), _("\
25027Show debugging of the dwarf line reader."), _("\
25028When enabled (non-zero), line number entries are dumped as they are read in.\n\
25029A value of 1 (one) provides basic information.\n\
25030A value greater than 1 provides more verbose information."),
25031 NULL,
25032 NULL,
25033 &setdebuglist, &showdebuglist);
25034
900e11f9
JK
25035 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25036Set cross-checking of \"physname\" code against demangler."), _("\
25037Show cross-checking of \"physname\" code against demangler."), _("\
25038When enabled, GDB's internal \"physname\" code is checked against\n\
25039the demangler."),
25040 NULL, show_check_physname,
25041 &setdebuglist, &showdebuglist);
25042
e615022a
DE
25043 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25044 no_class, &use_deprecated_index_sections, _("\
25045Set whether to use deprecated gdb_index sections."), _("\
25046Show whether to use deprecated gdb_index sections."), _("\
25047When enabled, deprecated .gdb_index sections are used anyway.\n\
25048Normally they are ignored either because of a missing feature or\n\
25049performance issue.\n\
25050Warning: This option must be enabled before gdb reads the file."),
25051 NULL,
25052 NULL,
25053 &setlist, &showlist);
25054
96d19272 25055 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 25056 _("\
fc1a9d6e 25057Save a gdb-index file.\n\
11570e71 25058Usage: save gdb-index DIRECTORY"),
96d19272
JK
25059 &save_cmdlist);
25060 set_cmd_completer (c, filename_completer);
f1e6e072
TT
25061
25062 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25063 &dwarf2_locexpr_funcs);
25064 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25065 &dwarf2_loclist_funcs);
25066
25067 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25068 &dwarf2_block_frame_base_locexpr_funcs);
25069 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25070 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25071
25072#if GDB_SELF_TEST
25073 selftests::register_test ("dw2_expand_symtabs_matching",
25074 selftests::dw2_expand_symtabs_matching::run_test);
25075#endif
6502dd73 25076}
This page took 4.536711 seconds and 4 git commands to generate.