Give Palmer co-credit for last patch.
[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{
9a3c8263
SM
2322 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2323 objfile_data (objfile, dwarf2_objfile_data_key));
be391dca
TT
2324 if (!dwarf2_per_objfile)
2325 {
2326 /* Initialize per-objfile state. */
2327 struct dwarf2_per_objfile *data
8d749320 2328 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
9a619af0 2329
330cdd98
PA
2330 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2331 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
be391dca 2332 }
73869dc2 2333 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2334 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2335 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2336 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2337}
2338
2339/* Return the containing section of virtual section SECTION. */
2340
2341static struct dwarf2_section_info *
2342get_containing_section (const struct dwarf2_section_info *section)
2343{
2344 gdb_assert (section->is_virtual);
2345 return section->s.containing_section;
c906108c
SS
2346}
2347
a32a8923
DE
2348/* Return the bfd owner of SECTION. */
2349
2350static struct bfd *
2351get_section_bfd_owner (const struct dwarf2_section_info *section)
2352{
73869dc2
DE
2353 if (section->is_virtual)
2354 {
2355 section = get_containing_section (section);
2356 gdb_assert (!section->is_virtual);
2357 }
049412e3 2358 return section->s.section->owner;
a32a8923
DE
2359}
2360
2361/* Return the bfd section of SECTION.
2362 Returns NULL if the section is not present. */
2363
2364static asection *
2365get_section_bfd_section (const struct dwarf2_section_info *section)
2366{
73869dc2
DE
2367 if (section->is_virtual)
2368 {
2369 section = get_containing_section (section);
2370 gdb_assert (!section->is_virtual);
2371 }
049412e3 2372 return section->s.section;
a32a8923
DE
2373}
2374
2375/* Return the name of SECTION. */
2376
2377static const char *
2378get_section_name (const struct dwarf2_section_info *section)
2379{
2380 asection *sectp = get_section_bfd_section (section);
2381
2382 gdb_assert (sectp != NULL);
2383 return bfd_section_name (get_section_bfd_owner (section), sectp);
2384}
2385
2386/* Return the name of the file SECTION is in. */
2387
2388static const char *
2389get_section_file_name (const struct dwarf2_section_info *section)
2390{
2391 bfd *abfd = get_section_bfd_owner (section);
2392
2393 return bfd_get_filename (abfd);
2394}
2395
2396/* Return the id of SECTION.
2397 Returns 0 if SECTION doesn't exist. */
2398
2399static int
2400get_section_id (const struct dwarf2_section_info *section)
2401{
2402 asection *sectp = get_section_bfd_section (section);
2403
2404 if (sectp == NULL)
2405 return 0;
2406 return sectp->id;
2407}
2408
2409/* Return the flags of SECTION.
73869dc2 2410 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2411
2412static int
2413get_section_flags (const struct dwarf2_section_info *section)
2414{
2415 asection *sectp = get_section_bfd_section (section);
2416
2417 gdb_assert (sectp != NULL);
2418 return bfd_get_section_flags (sectp->owner, sectp);
2419}
2420
251d32d9
TG
2421/* When loading sections, we look either for uncompressed section or for
2422 compressed section names. */
233a11ab
CS
2423
2424static int
251d32d9
TG
2425section_is_p (const char *section_name,
2426 const struct dwarf2_section_names *names)
233a11ab 2427{
251d32d9
TG
2428 if (names->normal != NULL
2429 && strcmp (section_name, names->normal) == 0)
2430 return 1;
2431 if (names->compressed != NULL
2432 && strcmp (section_name, names->compressed) == 0)
2433 return 1;
2434 return 0;
233a11ab
CS
2435}
2436
330cdd98 2437/* See declaration. */
c906108c 2438
330cdd98
PA
2439void
2440dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2441 const dwarf2_debug_sections &names)
c906108c 2442{
dc7650b8 2443 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2444
dc7650b8
JK
2445 if ((aflag & SEC_HAS_CONTENTS) == 0)
2446 {
2447 }
330cdd98 2448 else if (section_is_p (sectp->name, &names.info))
c906108c 2449 {
330cdd98
PA
2450 this->info.s.section = sectp;
2451 this->info.size = bfd_get_section_size (sectp);
c906108c 2452 }
330cdd98 2453 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2454 {
330cdd98
PA
2455 this->abbrev.s.section = sectp;
2456 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2457 }
330cdd98 2458 else if (section_is_p (sectp->name, &names.line))
c906108c 2459 {
330cdd98
PA
2460 this->line.s.section = sectp;
2461 this->line.size = bfd_get_section_size (sectp);
c906108c 2462 }
330cdd98 2463 else if (section_is_p (sectp->name, &names.loc))
c906108c 2464 {
330cdd98
PA
2465 this->loc.s.section = sectp;
2466 this->loc.size = bfd_get_section_size (sectp);
c906108c 2467 }
330cdd98 2468 else if (section_is_p (sectp->name, &names.loclists))
43988095 2469 {
330cdd98
PA
2470 this->loclists.s.section = sectp;
2471 this->loclists.size = bfd_get_section_size (sectp);
43988095 2472 }
330cdd98 2473 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2474 {
330cdd98
PA
2475 this->macinfo.s.section = sectp;
2476 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2477 }
330cdd98 2478 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2479 {
330cdd98
PA
2480 this->macro.s.section = sectp;
2481 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2482 }
330cdd98 2483 else if (section_is_p (sectp->name, &names.str))
c906108c 2484 {
330cdd98
PA
2485 this->str.s.section = sectp;
2486 this->str.size = bfd_get_section_size (sectp);
c906108c 2487 }
330cdd98 2488 else if (section_is_p (sectp->name, &names.line_str))
43988095 2489 {
330cdd98
PA
2490 this->line_str.s.section = sectp;
2491 this->line_str.size = bfd_get_section_size (sectp);
43988095 2492 }
330cdd98 2493 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2494 {
330cdd98
PA
2495 this->addr.s.section = sectp;
2496 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2497 }
330cdd98 2498 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2499 {
330cdd98
PA
2500 this->frame.s.section = sectp;
2501 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2502 }
330cdd98 2503 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2504 {
330cdd98
PA
2505 this->eh_frame.s.section = sectp;
2506 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2507 }
330cdd98 2508 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2509 {
330cdd98
PA
2510 this->ranges.s.section = sectp;
2511 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2512 }
330cdd98 2513 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2514 {
330cdd98
PA
2515 this->rnglists.s.section = sectp;
2516 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2517 }
330cdd98 2518 else if (section_is_p (sectp->name, &names.types))
348e048f 2519 {
8b70b953
TT
2520 struct dwarf2_section_info type_section;
2521
2522 memset (&type_section, 0, sizeof (type_section));
049412e3 2523 type_section.s.section = sectp;
8b70b953
TT
2524 type_section.size = bfd_get_section_size (sectp);
2525
330cdd98 2526 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2527 &type_section);
348e048f 2528 }
330cdd98 2529 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2530 {
330cdd98
PA
2531 this->gdb_index.s.section = sectp;
2532 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2533 }
dce234bc 2534
b4e1fd61 2535 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2536 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2537 this->has_section_at_zero = true;
c906108c
SS
2538}
2539
fceca515
DE
2540/* A helper function that decides whether a section is empty,
2541 or not present. */
9e0ac564
TT
2542
2543static int
19ac8c2e 2544dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2545{
73869dc2
DE
2546 if (section->is_virtual)
2547 return section->size == 0;
049412e3 2548 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2549}
2550
3019eac3
DE
2551/* Read the contents of the section INFO.
2552 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2553 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2554 of the DWO file.
dce234bc 2555 If the section is compressed, uncompress it before returning. */
c906108c 2556
dce234bc
PP
2557static void
2558dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2559{
a32a8923 2560 asection *sectp;
3019eac3 2561 bfd *abfd;
dce234bc 2562 gdb_byte *buf, *retbuf;
c906108c 2563
be391dca
TT
2564 if (info->readin)
2565 return;
dce234bc 2566 info->buffer = NULL;
be391dca 2567 info->readin = 1;
188dd5d6 2568
9e0ac564 2569 if (dwarf2_section_empty_p (info))
dce234bc 2570 return;
c906108c 2571
a32a8923 2572 sectp = get_section_bfd_section (info);
3019eac3 2573
73869dc2
DE
2574 /* If this is a virtual section we need to read in the real one first. */
2575 if (info->is_virtual)
2576 {
2577 struct dwarf2_section_info *containing_section =
2578 get_containing_section (info);
2579
2580 gdb_assert (sectp != NULL);
2581 if ((sectp->flags & SEC_RELOC) != 0)
2582 {
2583 error (_("Dwarf Error: DWP format V2 with relocations is not"
2584 " supported in section %s [in module %s]"),
2585 get_section_name (info), get_section_file_name (info));
2586 }
2587 dwarf2_read_section (objfile, containing_section);
2588 /* Other code should have already caught virtual sections that don't
2589 fit. */
2590 gdb_assert (info->virtual_offset + info->size
2591 <= containing_section->size);
2592 /* If the real section is empty or there was a problem reading the
2593 section we shouldn't get here. */
2594 gdb_assert (containing_section->buffer != NULL);
2595 info->buffer = containing_section->buffer + info->virtual_offset;
2596 return;
2597 }
2598
4bf44c1c
TT
2599 /* If the section has relocations, we must read it ourselves.
2600 Otherwise we attach it to the BFD. */
2601 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2602 {
d521ce57 2603 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2604 return;
dce234bc 2605 }
dce234bc 2606
224c3ddb 2607 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2608 info->buffer = buf;
dce234bc
PP
2609
2610 /* When debugging .o files, we may need to apply relocations; see
2611 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2612 We never compress sections in .o files, so we only need to
2613 try this when the section is not compressed. */
ac8035ab 2614 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2615 if (retbuf != NULL)
2616 {
2617 info->buffer = retbuf;
2618 return;
2619 }
2620
a32a8923
DE
2621 abfd = get_section_bfd_owner (info);
2622 gdb_assert (abfd != NULL);
2623
dce234bc
PP
2624 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2625 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2626 {
2627 error (_("Dwarf Error: Can't read DWARF data"
2628 " in section %s [in module %s]"),
2629 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2630 }
dce234bc
PP
2631}
2632
9e0ac564
TT
2633/* A helper function that returns the size of a section in a safe way.
2634 If you are positive that the section has been read before using the
2635 size, then it is safe to refer to the dwarf2_section_info object's
2636 "size" field directly. In other cases, you must call this
2637 function, because for compressed sections the size field is not set
2638 correctly until the section has been read. */
2639
2640static bfd_size_type
2641dwarf2_section_size (struct objfile *objfile,
2642 struct dwarf2_section_info *info)
2643{
2644 if (!info->readin)
2645 dwarf2_read_section (objfile, info);
2646 return info->size;
2647}
2648
dce234bc 2649/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2650 SECTION_NAME. */
af34e669 2651
dce234bc 2652void
3017a003
TG
2653dwarf2_get_section_info (struct objfile *objfile,
2654 enum dwarf2_section_enum sect,
d521ce57 2655 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2656 bfd_size_type *sizep)
2657{
2658 struct dwarf2_per_objfile *data
9a3c8263
SM
2659 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2660 dwarf2_objfile_data_key);
dce234bc 2661 struct dwarf2_section_info *info;
a3b2a86b
TT
2662
2663 /* We may see an objfile without any DWARF, in which case we just
2664 return nothing. */
2665 if (data == NULL)
2666 {
2667 *sectp = NULL;
2668 *bufp = NULL;
2669 *sizep = 0;
2670 return;
2671 }
3017a003
TG
2672 switch (sect)
2673 {
2674 case DWARF2_DEBUG_FRAME:
2675 info = &data->frame;
2676 break;
2677 case DWARF2_EH_FRAME:
2678 info = &data->eh_frame;
2679 break;
2680 default:
2681 gdb_assert_not_reached ("unexpected section");
2682 }
dce234bc 2683
9e0ac564 2684 dwarf2_read_section (objfile, info);
dce234bc 2685
a32a8923 2686 *sectp = get_section_bfd_section (info);
dce234bc
PP
2687 *bufp = info->buffer;
2688 *sizep = info->size;
2689}
2690
36586728
TT
2691/* A helper function to find the sections for a .dwz file. */
2692
2693static void
2694locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2695{
9a3c8263 2696 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2697
2698 /* Note that we only support the standard ELF names, because .dwz
2699 is ELF-only (at the time of writing). */
2700 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2701 {
049412e3 2702 dwz_file->abbrev.s.section = sectp;
36586728
TT
2703 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2704 }
2705 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2706 {
049412e3 2707 dwz_file->info.s.section = sectp;
36586728
TT
2708 dwz_file->info.size = bfd_get_section_size (sectp);
2709 }
2710 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2711 {
049412e3 2712 dwz_file->str.s.section = sectp;
36586728
TT
2713 dwz_file->str.size = bfd_get_section_size (sectp);
2714 }
2715 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2716 {
049412e3 2717 dwz_file->line.s.section = sectp;
36586728
TT
2718 dwz_file->line.size = bfd_get_section_size (sectp);
2719 }
2720 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2721 {
049412e3 2722 dwz_file->macro.s.section = sectp;
36586728
TT
2723 dwz_file->macro.size = bfd_get_section_size (sectp);
2724 }
2ec9a5e0
TT
2725 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2726 {
049412e3 2727 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2728 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2729 }
36586728
TT
2730}
2731
4db1a1dc
TT
2732/* Open the separate '.dwz' debug file, if needed. Return NULL if
2733 there is no .gnu_debugaltlink section in the file. Error if there
2734 is such a section but the file cannot be found. */
36586728
TT
2735
2736static struct dwz_file *
2737dwarf2_get_dwz_file (void)
2738{
36586728
TT
2739 const char *filename;
2740 struct dwz_file *result;
acd13123 2741 bfd_size_type buildid_len_arg;
dc294be5
TT
2742 size_t buildid_len;
2743 bfd_byte *buildid;
36586728
TT
2744
2745 if (dwarf2_per_objfile->dwz_file != NULL)
2746 return dwarf2_per_objfile->dwz_file;
2747
4db1a1dc 2748 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2749 gdb::unique_xmalloc_ptr<char> data
2750 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2751 &buildid_len_arg, &buildid));
4db1a1dc
TT
2752 if (data == NULL)
2753 {
2754 if (bfd_get_error () == bfd_error_no_error)
2755 return NULL;
2756 error (_("could not read '.gnu_debugaltlink' section: %s"),
2757 bfd_errmsg (bfd_get_error ()));
2758 }
791afaa2
TT
2759
2760 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2761
acd13123
TT
2762 buildid_len = (size_t) buildid_len_arg;
2763
791afaa2 2764 filename = data.get ();
d721ba37
PA
2765
2766 std::string abs_storage;
36586728
TT
2767 if (!IS_ABSOLUTE_PATH (filename))
2768 {
14278e1f
TT
2769 gdb::unique_xmalloc_ptr<char> abs
2770 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2771
14278e1f 2772 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2773 filename = abs_storage.c_str ();
36586728
TT
2774 }
2775
dc294be5
TT
2776 /* First try the file name given in the section. If that doesn't
2777 work, try to use the build-id instead. */
192b62ce 2778 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2779 if (dwz_bfd != NULL)
36586728 2780 {
192b62ce
TT
2781 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2782 dwz_bfd.release ();
36586728
TT
2783 }
2784
dc294be5
TT
2785 if (dwz_bfd == NULL)
2786 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2787
2788 if (dwz_bfd == NULL)
2789 error (_("could not find '.gnu_debugaltlink' file for %s"),
2790 objfile_name (dwarf2_per_objfile->objfile));
2791
36586728
TT
2792 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2793 struct dwz_file);
192b62ce 2794 result->dwz_bfd = dwz_bfd.release ();
36586728 2795
192b62ce 2796 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728 2797
192b62ce 2798 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2799 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2800 return result;
2801}
9291a0cd 2802\f
7b9f3c50
DE
2803/* DWARF quick_symbols_functions support. */
2804
2805/* TUs can share .debug_line entries, and there can be a lot more TUs than
2806 unique line tables, so we maintain a separate table of all .debug_line
2807 derived entries to support the sharing.
2808 All the quick functions need is the list of file names. We discard the
2809 line_header when we're done and don't need to record it here. */
2810struct quick_file_names
2811{
094b34ac
DE
2812 /* The data used to construct the hash key. */
2813 struct stmt_list_hash hash;
7b9f3c50
DE
2814
2815 /* The number of entries in file_names, real_names. */
2816 unsigned int num_file_names;
2817
2818 /* The file names from the line table, after being run through
2819 file_full_name. */
2820 const char **file_names;
2821
2822 /* The file names from the line table after being run through
2823 gdb_realpath. These are computed lazily. */
2824 const char **real_names;
2825};
2826
2827/* When using the index (and thus not using psymtabs), each CU has an
2828 object of this type. This is used to hold information needed by
2829 the various "quick" methods. */
2830struct dwarf2_per_cu_quick_data
2831{
2832 /* The file table. This can be NULL if there was no file table
2833 or it's currently not read in.
2834 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2835 struct quick_file_names *file_names;
2836
2837 /* The corresponding symbol table. This is NULL if symbols for this
2838 CU have not yet been read. */
43f3e411 2839 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2840
2841 /* A temporary mark bit used when iterating over all CUs in
2842 expand_symtabs_matching. */
2843 unsigned int mark : 1;
2844
2845 /* True if we've tried to read the file table and found there isn't one.
2846 There will be no point in trying to read it again next time. */
2847 unsigned int no_file_data : 1;
2848};
2849
094b34ac
DE
2850/* Utility hash function for a stmt_list_hash. */
2851
2852static hashval_t
2853hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2854{
2855 hashval_t v = 0;
2856
2857 if (stmt_list_hash->dwo_unit != NULL)
2858 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2859 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2860 return v;
2861}
2862
2863/* Utility equality function for a stmt_list_hash. */
2864
2865static int
2866eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2867 const struct stmt_list_hash *rhs)
2868{
2869 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2870 return 0;
2871 if (lhs->dwo_unit != NULL
2872 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2873 return 0;
2874
9c541725 2875 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2876}
2877
7b9f3c50
DE
2878/* Hash function for a quick_file_names. */
2879
2880static hashval_t
2881hash_file_name_entry (const void *e)
2882{
9a3c8263
SM
2883 const struct quick_file_names *file_data
2884 = (const struct quick_file_names *) e;
7b9f3c50 2885
094b34ac 2886 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2887}
2888
2889/* Equality function for a quick_file_names. */
2890
2891static int
2892eq_file_name_entry (const void *a, const void *b)
2893{
9a3c8263
SM
2894 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2895 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2896
094b34ac 2897 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2898}
2899
2900/* Delete function for a quick_file_names. */
2901
2902static void
2903delete_file_name_entry (void *e)
2904{
9a3c8263 2905 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2906 int i;
2907
2908 for (i = 0; i < file_data->num_file_names; ++i)
2909 {
2910 xfree ((void*) file_data->file_names[i]);
2911 if (file_data->real_names)
2912 xfree ((void*) file_data->real_names[i]);
2913 }
2914
2915 /* The space for the struct itself lives on objfile_obstack,
2916 so we don't free it here. */
2917}
2918
2919/* Create a quick_file_names hash table. */
2920
2921static htab_t
2922create_quick_file_names_table (unsigned int nr_initial_entries)
2923{
2924 return htab_create_alloc (nr_initial_entries,
2925 hash_file_name_entry, eq_file_name_entry,
2926 delete_file_name_entry, xcalloc, xfree);
2927}
9291a0cd 2928
918dd910
JK
2929/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2930 have to be created afterwards. You should call age_cached_comp_units after
2931 processing PER_CU->CU. dw2_setup must have been already called. */
2932
2933static void
2934load_cu (struct dwarf2_per_cu_data *per_cu)
2935{
3019eac3 2936 if (per_cu->is_debug_types)
e5fe5e75 2937 load_full_type_unit (per_cu);
918dd910 2938 else
95554aad 2939 load_full_comp_unit (per_cu, language_minimal);
918dd910 2940
cc12ce38
DE
2941 if (per_cu->cu == NULL)
2942 return; /* Dummy CU. */
2dc860c0
DE
2943
2944 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2945}
2946
a0f42c21 2947/* Read in the symbols for PER_CU. */
2fdf6df6 2948
9291a0cd 2949static void
a0f42c21 2950dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2951{
2952 struct cleanup *back_to;
2953
f4dc4d17
DE
2954 /* Skip type_unit_groups, reading the type units they contain
2955 is handled elsewhere. */
2956 if (IS_TYPE_UNIT_GROUP (per_cu))
2957 return;
2958
9291a0cd
TT
2959 back_to = make_cleanup (dwarf2_release_queue, NULL);
2960
95554aad 2961 if (dwarf2_per_objfile->using_index
43f3e411 2962 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2963 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2964 {
2965 queue_comp_unit (per_cu, language_minimal);
2966 load_cu (per_cu);
89e63ee4
DE
2967
2968 /* If we just loaded a CU from a DWO, and we're working with an index
2969 that may badly handle TUs, load all the TUs in that DWO as well.
2970 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2971 if (!per_cu->is_debug_types
cc12ce38 2972 && per_cu->cu != NULL
89e63ee4
DE
2973 && per_cu->cu->dwo_unit != NULL
2974 && dwarf2_per_objfile->index_table != NULL
2975 && dwarf2_per_objfile->index_table->version <= 7
2976 /* DWP files aren't supported yet. */
2977 && get_dwp_file () == NULL)
2978 queue_and_load_all_dwo_tus (per_cu);
95554aad 2979 }
9291a0cd 2980
a0f42c21 2981 process_queue ();
9291a0cd
TT
2982
2983 /* Age the cache, releasing compilation units that have not
2984 been used recently. */
2985 age_cached_comp_units ();
2986
2987 do_cleanups (back_to);
2988}
2989
2990/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2991 the objfile from which this CU came. Returns the resulting symbol
2992 table. */
2fdf6df6 2993
43f3e411 2994static struct compunit_symtab *
a0f42c21 2995dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2996{
95554aad 2997 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2998 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
2999 {
3000 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
c83dd867 3001 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 3002 dw2_do_instantiate_symtab (per_cu);
95554aad 3003 process_cu_includes ();
9291a0cd
TT
3004 do_cleanups (back_to);
3005 }
f194fefb 3006
43f3e411 3007 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
3008}
3009
8832e7e3 3010/* Return the CU/TU given its index.
f4dc4d17
DE
3011
3012 This is intended for loops like:
3013
3014 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3015 + dwarf2_per_objfile->n_type_units); ++i)
3016 {
8832e7e3 3017 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
3018
3019 ...;
3020 }
3021*/
2fdf6df6 3022
1fd400ff 3023static struct dwarf2_per_cu_data *
8832e7e3 3024dw2_get_cutu (int index)
1fd400ff
TT
3025{
3026 if (index >= dwarf2_per_objfile->n_comp_units)
3027 {
f4dc4d17 3028 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
3029 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3030 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
3031 }
3032
3033 return dwarf2_per_objfile->all_comp_units[index];
3034}
3035
8832e7e3
DE
3036/* Return the CU given its index.
3037 This differs from dw2_get_cutu in that it's for when you know INDEX
3038 refers to a CU. */
f4dc4d17
DE
3039
3040static struct dwarf2_per_cu_data *
8832e7e3 3041dw2_get_cu (int index)
f4dc4d17 3042{
8832e7e3 3043 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 3044
1fd400ff
TT
3045 return dwarf2_per_objfile->all_comp_units[index];
3046}
3047
2ec9a5e0
TT
3048/* A helper for create_cus_from_index that handles a given list of
3049 CUs. */
2fdf6df6 3050
74a0d9f6 3051static void
2ec9a5e0
TT
3052create_cus_from_index_list (struct objfile *objfile,
3053 const gdb_byte *cu_list, offset_type n_elements,
3054 struct dwarf2_section_info *section,
3055 int is_dwz,
3056 int base_offset)
9291a0cd
TT
3057{
3058 offset_type i;
9291a0cd 3059
2ec9a5e0 3060 for (i = 0; i < n_elements; i += 2)
9291a0cd 3061 {
74a0d9f6 3062 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3063
3064 sect_offset sect_off
3065 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3066 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3067 cu_list += 2 * 8;
3068
9c541725
PA
3069 dwarf2_per_cu_data *the_cu
3070 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3071 struct dwarf2_per_cu_data);
3072 the_cu->sect_off = sect_off;
9291a0cd
TT
3073 the_cu->length = length;
3074 the_cu->objfile = objfile;
8a0459fd 3075 the_cu->section = section;
9291a0cd
TT
3076 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3077 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
3078 the_cu->is_dwz = is_dwz;
3079 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 3080 }
9291a0cd
TT
3081}
3082
2ec9a5e0 3083/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3084 the CU objects for this objfile. */
2ec9a5e0 3085
74a0d9f6 3086static void
2ec9a5e0
TT
3087create_cus_from_index (struct objfile *objfile,
3088 const gdb_byte *cu_list, offset_type cu_list_elements,
3089 const gdb_byte *dwz_list, offset_type dwz_elements)
3090{
3091 struct dwz_file *dwz;
3092
3093 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
3094 dwarf2_per_objfile->all_comp_units =
3095 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3096 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 3097
74a0d9f6
JK
3098 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3099 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
3100
3101 if (dwz_elements == 0)
74a0d9f6 3102 return;
2ec9a5e0
TT
3103
3104 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
3105 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3106 cu_list_elements / 2);
2ec9a5e0
TT
3107}
3108
1fd400ff 3109/* Create the signatured type hash table from the index. */
673bfd45 3110
74a0d9f6 3111static void
673bfd45 3112create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3113 struct dwarf2_section_info *section,
673bfd45
DE
3114 const gdb_byte *bytes,
3115 offset_type elements)
1fd400ff
TT
3116{
3117 offset_type i;
673bfd45 3118 htab_t sig_types_hash;
1fd400ff 3119
6aa5f3a6
DE
3120 dwarf2_per_objfile->n_type_units
3121 = dwarf2_per_objfile->n_allocated_type_units
3122 = elements / 3;
8d749320
SM
3123 dwarf2_per_objfile->all_type_units =
3124 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3125
673bfd45 3126 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3127
3128 for (i = 0; i < elements; i += 3)
3129 {
52dc124a 3130 struct signatured_type *sig_type;
9c541725 3131 ULONGEST signature;
1fd400ff 3132 void **slot;
9c541725 3133 cu_offset type_offset_in_tu;
1fd400ff 3134
74a0d9f6 3135 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3136 sect_offset sect_off
3137 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3138 type_offset_in_tu
3139 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3140 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3141 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3142 bytes += 3 * 8;
3143
52dc124a 3144 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3145 struct signatured_type);
52dc124a 3146 sig_type->signature = signature;
9c541725 3147 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3148 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3149 sig_type->per_cu.section = section;
9c541725 3150 sig_type->per_cu.sect_off = sect_off;
52dc124a
DE
3151 sig_type->per_cu.objfile = objfile;
3152 sig_type->per_cu.v.quick
1fd400ff
TT
3153 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3154 struct dwarf2_per_cu_quick_data);
3155
52dc124a
DE
3156 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3157 *slot = sig_type;
1fd400ff 3158
b4dd5633 3159 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3160 }
3161
673bfd45 3162 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3163}
3164
9291a0cd
TT
3165/* Read the address map data from the mapped index, and use it to
3166 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3167
9291a0cd
TT
3168static void
3169create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3170{
3e29f34a 3171 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3172 const gdb_byte *iter, *end;
9291a0cd 3173 struct addrmap *mutable_map;
9291a0cd
TT
3174 CORE_ADDR baseaddr;
3175
8268c778
PA
3176 auto_obstack temp_obstack;
3177
9291a0cd
TT
3178 mutable_map = addrmap_create_mutable (&temp_obstack);
3179
3180 iter = index->address_table;
3181 end = iter + index->address_table_size;
3182
3183 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3184
3185 while (iter < end)
3186 {
3187 ULONGEST hi, lo, cu_index;
3188 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3189 iter += 8;
3190 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3191 iter += 8;
3192 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3193 iter += 4;
f652bce2 3194
24a55014 3195 if (lo > hi)
f652bce2 3196 {
24a55014
DE
3197 complaint (&symfile_complaints,
3198 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3199 hex_string (lo), hex_string (hi));
24a55014 3200 continue;
f652bce2 3201 }
24a55014
DE
3202
3203 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3204 {
3205 complaint (&symfile_complaints,
3206 _(".gdb_index address table has invalid CU number %u"),
3207 (unsigned) cu_index);
24a55014 3208 continue;
f652bce2 3209 }
24a55014 3210
3e29f34a
MR
3211 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3212 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3213 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
9291a0cd
TT
3214 }
3215
3216 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3217 &objfile->objfile_obstack);
9291a0cd
TT
3218}
3219
59d7bcaf
JK
3220/* The hash function for strings in the mapped index. This is the same as
3221 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3222 implementation. This is necessary because the hash function is tied to the
3223 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3224 SYMBOL_HASH_NEXT.
3225
3226 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3227
9291a0cd 3228static hashval_t
559a7a62 3229mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3230{
3231 const unsigned char *str = (const unsigned char *) p;
3232 hashval_t r = 0;
3233 unsigned char c;
3234
3235 while ((c = *str++) != 0)
559a7a62
JK
3236 {
3237 if (index_version >= 5)
3238 c = tolower (c);
3239 r = r * 67 + c - 113;
3240 }
9291a0cd
TT
3241
3242 return r;
3243}
3244
3245/* Find a slot in the mapped index INDEX for the object named NAME.
3246 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3247 constant pool and return true. If NAME cannot be found, return
3248 false. */
2fdf6df6 3249
109483d9 3250static bool
9291a0cd
TT
3251find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3252 offset_type **vec_out)
3253{
0cf03b49 3254 offset_type hash;
9291a0cd 3255 offset_type slot, step;
559a7a62 3256 int (*cmp) (const char *, const char *);
9291a0cd 3257
791afaa2 3258 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3259 if (current_language->la_language == language_cplus
45280282
IB
3260 || current_language->la_language == language_fortran
3261 || current_language->la_language == language_d)
0cf03b49
JK
3262 {
3263 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3264 not contain any. */
a8719064 3265
72998fb3 3266 if (strchr (name, '(') != NULL)
0cf03b49 3267 {
109483d9 3268 without_params = cp_remove_params (name);
0cf03b49 3269
72998fb3 3270 if (without_params != NULL)
791afaa2 3271 name = without_params.get ();
0cf03b49
JK
3272 }
3273 }
3274
559a7a62 3275 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3276 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3277 simulate our NAME being searched is also lowercased. */
3278 hash = mapped_index_string_hash ((index->version == 4
3279 && case_sensitivity == case_sensitive_off
3280 ? 5 : index->version),
3281 name);
3282
3876f04e
DE
3283 slot = hash & (index->symbol_table_slots - 1);
3284 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 3285 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3286
3287 for (;;)
3288 {
3289 /* Convert a slot number to an offset into the table. */
3290 offset_type i = 2 * slot;
3291 const char *str;
3876f04e 3292 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
109483d9 3293 return false;
9291a0cd 3294
3876f04e 3295 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 3296 if (!cmp (name, str))
9291a0cd
TT
3297 {
3298 *vec_out = (offset_type *) (index->constant_pool
3876f04e 3299 + MAYBE_SWAP (index->symbol_table[i + 1]));
109483d9 3300 return true;
9291a0cd
TT
3301 }
3302
3876f04e 3303 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
3304 }
3305}
3306
2ec9a5e0
TT
3307/* A helper function that reads the .gdb_index from SECTION and fills
3308 in MAP. FILENAME is the name of the file containing the section;
3309 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3310 ok to use deprecated sections.
3311
3312 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3313 out parameters that are filled in with information about the CU and
3314 TU lists in the section.
3315
3316 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3317
9291a0cd 3318static int
2ec9a5e0
TT
3319read_index_from_section (struct objfile *objfile,
3320 const char *filename,
3321 int deprecated_ok,
3322 struct dwarf2_section_info *section,
3323 struct mapped_index *map,
3324 const gdb_byte **cu_list,
3325 offset_type *cu_list_elements,
3326 const gdb_byte **types_list,
3327 offset_type *types_list_elements)
9291a0cd 3328{
948f8e3d 3329 const gdb_byte *addr;
2ec9a5e0 3330 offset_type version;
b3b272e1 3331 offset_type *metadata;
1fd400ff 3332 int i;
9291a0cd 3333
2ec9a5e0 3334 if (dwarf2_section_empty_p (section))
9291a0cd 3335 return 0;
82430852
JK
3336
3337 /* Older elfutils strip versions could keep the section in the main
3338 executable while splitting it for the separate debug info file. */
a32a8923 3339 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3340 return 0;
3341
2ec9a5e0 3342 dwarf2_read_section (objfile, section);
9291a0cd 3343
2ec9a5e0 3344 addr = section->buffer;
9291a0cd 3345 /* Version check. */
1fd400ff 3346 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3347 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3348 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3349 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3350 indices. */
831adc1f 3351 if (version < 4)
481860b3
GB
3352 {
3353 static int warning_printed = 0;
3354 if (!warning_printed)
3355 {
3356 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3357 filename);
481860b3
GB
3358 warning_printed = 1;
3359 }
3360 return 0;
3361 }
3362 /* Index version 4 uses a different hash function than index version
3363 5 and later.
3364
3365 Versions earlier than 6 did not emit psymbols for inlined
3366 functions. Using these files will cause GDB not to be able to
3367 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3368 indices unless the user has done
3369 "set use-deprecated-index-sections on". */
2ec9a5e0 3370 if (version < 6 && !deprecated_ok)
481860b3
GB
3371 {
3372 static int warning_printed = 0;
3373 if (!warning_printed)
3374 {
e615022a
DE
3375 warning (_("\
3376Skipping deprecated .gdb_index section in %s.\n\
3377Do \"set use-deprecated-index-sections on\" before the file is read\n\
3378to use the section anyway."),
2ec9a5e0 3379 filename);
481860b3
GB
3380 warning_printed = 1;
3381 }
3382 return 0;
3383 }
796a7ff8 3384 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3385 of the TU (for symbols coming from TUs),
3386 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3387 Plus gold-generated indices can have duplicate entries for global symbols,
3388 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3389 These are just performance bugs, and we can't distinguish gdb-generated
3390 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3391
481860b3 3392 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3393 longer backward compatible. */
796a7ff8 3394 if (version > 8)
594e8718 3395 return 0;
9291a0cd 3396
559a7a62 3397 map->version = version;
2ec9a5e0 3398 map->total_size = section->size;
9291a0cd
TT
3399
3400 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3401
3402 i = 0;
2ec9a5e0
TT
3403 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3404 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3405 / 8);
1fd400ff
TT
3406 ++i;
3407
2ec9a5e0
TT
3408 *types_list = addr + MAYBE_SWAP (metadata[i]);
3409 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3410 - MAYBE_SWAP (metadata[i]))
3411 / 8);
987d643c 3412 ++i;
1fd400ff
TT
3413
3414 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3415 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3416 - MAYBE_SWAP (metadata[i]));
3417 ++i;
3418
3876f04e
DE
3419 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3420 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3421 - MAYBE_SWAP (metadata[i]))
3422 / (2 * sizeof (offset_type)));
1fd400ff 3423 ++i;
9291a0cd 3424
f9d83a0b 3425 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3426
2ec9a5e0
TT
3427 return 1;
3428}
3429
3430
3431/* Read the index file. If everything went ok, initialize the "quick"
3432 elements of all the CUs and return 1. Otherwise, return 0. */
3433
3434static int
3435dwarf2_read_index (struct objfile *objfile)
3436{
3437 struct mapped_index local_map, *map;
3438 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3439 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3440 struct dwz_file *dwz;
2ec9a5e0 3441
4262abfb 3442 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3443 use_deprecated_index_sections,
3444 &dwarf2_per_objfile->gdb_index, &local_map,
3445 &cu_list, &cu_list_elements,
3446 &types_list, &types_list_elements))
3447 return 0;
3448
0fefef59 3449 /* Don't use the index if it's empty. */
2ec9a5e0 3450 if (local_map.symbol_table_slots == 0)
0fefef59
DE
3451 return 0;
3452
2ec9a5e0
TT
3453 /* If there is a .dwz file, read it so we can get its CU list as
3454 well. */
4db1a1dc
TT
3455 dwz = dwarf2_get_dwz_file ();
3456 if (dwz != NULL)
2ec9a5e0 3457 {
2ec9a5e0
TT
3458 struct mapped_index dwz_map;
3459 const gdb_byte *dwz_types_ignore;
3460 offset_type dwz_types_elements_ignore;
3461
3462 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3463 1,
3464 &dwz->gdb_index, &dwz_map,
3465 &dwz_list, &dwz_list_elements,
3466 &dwz_types_ignore,
3467 &dwz_types_elements_ignore))
3468 {
3469 warning (_("could not read '.gdb_index' section from %s; skipping"),
3470 bfd_get_filename (dwz->dwz_bfd));
3471 return 0;
3472 }
3473 }
3474
74a0d9f6
JK
3475 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3476 dwz_list_elements);
1fd400ff 3477
8b70b953
TT
3478 if (types_list_elements)
3479 {
3480 struct dwarf2_section_info *section;
3481
3482 /* We can only handle a single .debug_types when we have an
3483 index. */
3484 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3485 return 0;
3486
3487 section = VEC_index (dwarf2_section_info_def,
3488 dwarf2_per_objfile->types, 0);
3489
74a0d9f6
JK
3490 create_signatured_type_table_from_index (objfile, section, types_list,
3491 types_list_elements);
8b70b953 3492 }
9291a0cd 3493
2ec9a5e0
TT
3494 create_addrmap_from_index (objfile, &local_map);
3495
8d749320 3496 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3f563c84 3497 map = new (map) mapped_index ();
2ec9a5e0 3498 *map = local_map;
9291a0cd
TT
3499
3500 dwarf2_per_objfile->index_table = map;
3501 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3502 dwarf2_per_objfile->quick_file_names_table =
3503 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3504
3505 return 1;
3506}
3507
3508/* A helper for the "quick" functions which sets the global
3509 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 3510
9291a0cd
TT
3511static void
3512dw2_setup (struct objfile *objfile)
3513{
9a3c8263
SM
3514 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3515 objfile_data (objfile, dwarf2_objfile_data_key));
9291a0cd
TT
3516 gdb_assert (dwarf2_per_objfile);
3517}
3518
dee91e82 3519/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3520
dee91e82
DE
3521static void
3522dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3523 const gdb_byte *info_ptr,
dee91e82
DE
3524 struct die_info *comp_unit_die,
3525 int has_children,
3526 void *data)
9291a0cd 3527{
dee91e82
DE
3528 struct dwarf2_cu *cu = reader->cu;
3529 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3530 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3531 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3532 struct attribute *attr;
dee91e82 3533 int i;
7b9f3c50
DE
3534 void **slot;
3535 struct quick_file_names *qfn;
9291a0cd 3536
0186c6a7
DE
3537 gdb_assert (! this_cu->is_debug_types);
3538
07261596
TT
3539 /* Our callers never want to match partial units -- instead they
3540 will match the enclosing full CU. */
3541 if (comp_unit_die->tag == DW_TAG_partial_unit)
3542 {
3543 this_cu->v.quick->no_file_data = 1;
3544 return;
3545 }
3546
0186c6a7 3547 lh_cu = this_cu;
7b9f3c50 3548 slot = NULL;
dee91e82 3549
fff8551c 3550 line_header_up lh;
9c541725 3551 sect_offset line_offset {};
fff8551c 3552
dee91e82 3553 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3554 if (attr)
3555 {
7b9f3c50
DE
3556 struct quick_file_names find_entry;
3557
9c541725 3558 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3559
3560 /* We may have already read in this line header (TU line header sharing).
3561 If we have we're done. */
094b34ac 3562 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3563 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3564 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3565 &find_entry, INSERT);
3566 if (*slot != NULL)
3567 {
9a3c8263 3568 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3569 return;
7b9f3c50
DE
3570 }
3571
3019eac3 3572 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3573 }
3574 if (lh == NULL)
3575 {
094b34ac 3576 lh_cu->v.quick->no_file_data = 1;
dee91e82 3577 return;
9291a0cd
TT
3578 }
3579
8d749320 3580 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3581 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3582 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3583 gdb_assert (slot != NULL);
3584 *slot = qfn;
9291a0cd 3585
d721ba37 3586 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3587
fff8551c 3588 qfn->num_file_names = lh->file_names.size ();
8d749320 3589 qfn->file_names =
fff8551c
PA
3590 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3591 for (i = 0; i < lh->file_names.size (); ++i)
3592 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3593 qfn->real_names = NULL;
9291a0cd 3594
094b34ac 3595 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3596}
3597
3598/* A helper for the "quick" functions which attempts to read the line
3599 table for THIS_CU. */
3600
3601static struct quick_file_names *
e4a48d9d 3602dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3603{
0186c6a7
DE
3604 /* This should never be called for TUs. */
3605 gdb_assert (! this_cu->is_debug_types);
3606 /* Nor type unit groups. */
3607 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3608
dee91e82
DE
3609 if (this_cu->v.quick->file_names != NULL)
3610 return this_cu->v.quick->file_names;
3611 /* If we know there is no line data, no point in looking again. */
3612 if (this_cu->v.quick->no_file_data)
3613 return NULL;
3614
0186c6a7 3615 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3616
3617 if (this_cu->v.quick->no_file_data)
3618 return NULL;
3619 return this_cu->v.quick->file_names;
9291a0cd
TT
3620}
3621
3622/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3623 real path for a given file name from the line table. */
2fdf6df6 3624
9291a0cd 3625static const char *
7b9f3c50
DE
3626dw2_get_real_path (struct objfile *objfile,
3627 struct quick_file_names *qfn, int index)
9291a0cd 3628{
7b9f3c50
DE
3629 if (qfn->real_names == NULL)
3630 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3631 qfn->num_file_names, const char *);
9291a0cd 3632
7b9f3c50 3633 if (qfn->real_names[index] == NULL)
14278e1f 3634 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3635
7b9f3c50 3636 return qfn->real_names[index];
9291a0cd
TT
3637}
3638
3639static struct symtab *
3640dw2_find_last_source_symtab (struct objfile *objfile)
3641{
43f3e411 3642 struct compunit_symtab *cust;
9291a0cd 3643 int index;
ae2de4f8 3644
9291a0cd
TT
3645 dw2_setup (objfile);
3646 index = dwarf2_per_objfile->n_comp_units - 1;
43f3e411
DE
3647 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3648 if (cust == NULL)
3649 return NULL;
3650 return compunit_primary_filetab (cust);
9291a0cd
TT
3651}
3652
7b9f3c50
DE
3653/* Traversal function for dw2_forget_cached_source_info. */
3654
3655static int
3656dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3657{
7b9f3c50 3658 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3659
7b9f3c50 3660 if (file_data->real_names)
9291a0cd 3661 {
7b9f3c50 3662 int i;
9291a0cd 3663
7b9f3c50 3664 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3665 {
7b9f3c50
DE
3666 xfree ((void*) file_data->real_names[i]);
3667 file_data->real_names[i] = NULL;
9291a0cd
TT
3668 }
3669 }
7b9f3c50
DE
3670
3671 return 1;
3672}
3673
3674static void
3675dw2_forget_cached_source_info (struct objfile *objfile)
3676{
3677 dw2_setup (objfile);
3678
3679 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3680 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3681}
3682
f8eba3c6
TT
3683/* Helper function for dw2_map_symtabs_matching_filename that expands
3684 the symtabs and calls the iterator. */
3685
3686static int
3687dw2_map_expand_apply (struct objfile *objfile,
3688 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3689 const char *name, const char *real_path,
14bc53a8 3690 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3691{
43f3e411 3692 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3693
3694 /* Don't visit already-expanded CUs. */
43f3e411 3695 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3696 return 0;
3697
3698 /* This may expand more than one symtab, and we want to iterate over
3699 all of them. */
a0f42c21 3700 dw2_instantiate_symtab (per_cu);
f8eba3c6 3701
14bc53a8
PA
3702 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3703 last_made, callback);
f8eba3c6
TT
3704}
3705
3706/* Implementation of the map_symtabs_matching_filename method. */
3707
14bc53a8
PA
3708static bool
3709dw2_map_symtabs_matching_filename
3710 (struct objfile *objfile, const char *name, const char *real_path,
3711 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
3712{
3713 int i;
c011a4f4 3714 const char *name_basename = lbasename (name);
9291a0cd
TT
3715
3716 dw2_setup (objfile);
ae2de4f8 3717
848e3e78
DE
3718 /* The rule is CUs specify all the files, including those used by
3719 any TU, so there's no need to scan TUs here. */
f4dc4d17 3720
848e3e78 3721 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3722 {
3723 int j;
8832e7e3 3724 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3725 struct quick_file_names *file_data;
9291a0cd 3726
3d7bb9d9 3727 /* We only need to look at symtabs not already expanded. */
43f3e411 3728 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3729 continue;
3730
e4a48d9d 3731 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3732 if (file_data == NULL)
9291a0cd
TT
3733 continue;
3734
7b9f3c50 3735 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3736 {
7b9f3c50 3737 const char *this_name = file_data->file_names[j];
da235a7c 3738 const char *this_real_name;
9291a0cd 3739
af529f8f 3740 if (compare_filenames_for_search (this_name, name))
9291a0cd 3741 {
f5b95b50 3742 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3743 callback))
3744 return true;
288e77a7 3745 continue;
4aac40c8 3746 }
9291a0cd 3747
c011a4f4
DE
3748 /* Before we invoke realpath, which can get expensive when many
3749 files are involved, do a quick comparison of the basenames. */
3750 if (! basenames_may_differ
3751 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3752 continue;
3753
da235a7c
JK
3754 this_real_name = dw2_get_real_path (objfile, file_data, j);
3755 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3756 {
da235a7c 3757 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3758 callback))
3759 return true;
288e77a7 3760 continue;
da235a7c 3761 }
9291a0cd 3762
da235a7c
JK
3763 if (real_path != NULL)
3764 {
af529f8f
JK
3765 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3766 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3767 if (this_real_name != NULL
af529f8f 3768 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3769 {
f5b95b50 3770 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3771 callback))
3772 return true;
288e77a7 3773 continue;
9291a0cd
TT
3774 }
3775 }
3776 }
3777 }
3778
14bc53a8 3779 return false;
9291a0cd
TT
3780}
3781
da51c347
DE
3782/* Struct used to manage iterating over all CUs looking for a symbol. */
3783
3784struct dw2_symtab_iterator
9291a0cd 3785{
da51c347
DE
3786 /* The internalized form of .gdb_index. */
3787 struct mapped_index *index;
3788 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3789 int want_specific_block;
3790 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3791 Unused if !WANT_SPECIFIC_BLOCK. */
3792 int block_index;
3793 /* The kind of symbol we're looking for. */
3794 domain_enum domain;
3795 /* The list of CUs from the index entry of the symbol,
3796 or NULL if not found. */
3797 offset_type *vec;
3798 /* The next element in VEC to look at. */
3799 int next;
3800 /* The number of elements in VEC, or zero if there is no match. */
3801 int length;
8943b874
DE
3802 /* Have we seen a global version of the symbol?
3803 If so we can ignore all further global instances.
3804 This is to work around gold/15646, inefficient gold-generated
3805 indices. */
3806 int global_seen;
da51c347 3807};
9291a0cd 3808
da51c347
DE
3809/* Initialize the index symtab iterator ITER.
3810 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3811 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3812
9291a0cd 3813static void
da51c347
DE
3814dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3815 struct mapped_index *index,
3816 int want_specific_block,
3817 int block_index,
3818 domain_enum domain,
3819 const char *name)
3820{
3821 iter->index = index;
3822 iter->want_specific_block = want_specific_block;
3823 iter->block_index = block_index;
3824 iter->domain = domain;
3825 iter->next = 0;
8943b874 3826 iter->global_seen = 0;
da51c347
DE
3827
3828 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3829 iter->length = MAYBE_SWAP (*iter->vec);
3830 else
3831 {
3832 iter->vec = NULL;
3833 iter->length = 0;
3834 }
3835}
3836
3837/* Return the next matching CU or NULL if there are no more. */
3838
3839static struct dwarf2_per_cu_data *
3840dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3841{
3842 for ( ; iter->next < iter->length; ++iter->next)
3843 {
3844 offset_type cu_index_and_attrs =
3845 MAYBE_SWAP (iter->vec[iter->next + 1]);
3846 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3847 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3848 int want_static = iter->block_index != GLOBAL_BLOCK;
3849 /* This value is only valid for index versions >= 7. */
3850 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3851 gdb_index_symbol_kind symbol_kind =
3852 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3853 /* Only check the symbol attributes if they're present.
3854 Indices prior to version 7 don't record them,
3855 and indices >= 7 may elide them for certain symbols
3856 (gold does this). */
3857 int attrs_valid =
3858 (iter->index->version >= 7
3859 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3860
3190f0c6
DE
3861 /* Don't crash on bad data. */
3862 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3863 + dwarf2_per_objfile->n_type_units))
3864 {
3865 complaint (&symfile_complaints,
3866 _(".gdb_index entry has bad CU index"
4262abfb
JK
3867 " [in module %s]"),
3868 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3869 continue;
3870 }
3871
8832e7e3 3872 per_cu = dw2_get_cutu (cu_index);
3190f0c6 3873
da51c347 3874 /* Skip if already read in. */
43f3e411 3875 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3876 continue;
3877
8943b874
DE
3878 /* Check static vs global. */
3879 if (attrs_valid)
3880 {
3881 if (iter->want_specific_block
3882 && want_static != is_static)
3883 continue;
3884 /* Work around gold/15646. */
3885 if (!is_static && iter->global_seen)
3886 continue;
3887 if (!is_static)
3888 iter->global_seen = 1;
3889 }
da51c347
DE
3890
3891 /* Only check the symbol's kind if it has one. */
3892 if (attrs_valid)
3893 {
3894 switch (iter->domain)
3895 {
3896 case VAR_DOMAIN:
3897 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3898 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3899 /* Some types are also in VAR_DOMAIN. */
3900 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3901 continue;
3902 break;
3903 case STRUCT_DOMAIN:
3904 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3905 continue;
3906 break;
3907 case LABEL_DOMAIN:
3908 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3909 continue;
3910 break;
3911 default:
3912 break;
3913 }
3914 }
3915
3916 ++iter->next;
3917 return per_cu;
3918 }
3919
3920 return NULL;
3921}
3922
43f3e411 3923static struct compunit_symtab *
da51c347
DE
3924dw2_lookup_symbol (struct objfile *objfile, int block_index,
3925 const char *name, domain_enum domain)
9291a0cd 3926{
43f3e411 3927 struct compunit_symtab *stab_best = NULL;
156942c7
DE
3928 struct mapped_index *index;
3929
9291a0cd
TT
3930 dw2_setup (objfile);
3931
b5ec771e
PA
3932 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3933
156942c7
DE
3934 index = dwarf2_per_objfile->index_table;
3935
da51c347 3936 /* index is NULL if OBJF_READNOW. */
156942c7 3937 if (index)
9291a0cd 3938 {
da51c347
DE
3939 struct dw2_symtab_iterator iter;
3940 struct dwarf2_per_cu_data *per_cu;
3941
3942 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3943
da51c347 3944 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3945 {
b2e2f908 3946 struct symbol *sym, *with_opaque = NULL;
43f3e411
DE
3947 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3948 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
f194fefb 3949 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3950
b2e2f908
DE
3951 sym = block_find_symbol (block, name, domain,
3952 block_find_non_opaque_type_preferred,
3953 &with_opaque);
3954
da51c347
DE
3955 /* Some caution must be observed with overloaded functions
3956 and methods, since the index will not contain any overload
3957 information (but NAME might contain it). */
da51c347 3958
b2e2f908 3959 if (sym != NULL
b5ec771e 3960 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
b2e2f908
DE
3961 return stab;
3962 if (with_opaque != NULL
b5ec771e 3963 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
b2e2f908 3964 stab_best = stab;
da51c347
DE
3965
3966 /* Keep looking through other CUs. */
9291a0cd
TT
3967 }
3968 }
9291a0cd 3969
da51c347 3970 return stab_best;
9291a0cd
TT
3971}
3972
3973static void
3974dw2_print_stats (struct objfile *objfile)
3975{
e4a48d9d 3976 int i, total, count;
9291a0cd
TT
3977
3978 dw2_setup (objfile);
e4a48d9d 3979 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3980 count = 0;
e4a48d9d 3981 for (i = 0; i < total; ++i)
9291a0cd 3982 {
8832e7e3 3983 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3984
43f3e411 3985 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3986 ++count;
3987 }
e4a48d9d 3988 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3989 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3990}
3991
779bd270
DE
3992/* This dumps minimal information about the index.
3993 It is called via "mt print objfiles".
3994 One use is to verify .gdb_index has been loaded by the
3995 gdb.dwarf2/gdb-index.exp testcase. */
3996
9291a0cd
TT
3997static void
3998dw2_dump (struct objfile *objfile)
3999{
779bd270
DE
4000 dw2_setup (objfile);
4001 gdb_assert (dwarf2_per_objfile->using_index);
4002 printf_filtered (".gdb_index:");
4003 if (dwarf2_per_objfile->index_table != NULL)
4004 {
4005 printf_filtered (" version %d\n",
4006 dwarf2_per_objfile->index_table->version);
4007 }
4008 else
4009 printf_filtered (" faked for \"readnow\"\n");
4010 printf_filtered ("\n");
9291a0cd
TT
4011}
4012
4013static void
3189cb12
DE
4014dw2_relocate (struct objfile *objfile,
4015 const struct section_offsets *new_offsets,
4016 const struct section_offsets *delta)
9291a0cd
TT
4017{
4018 /* There's nothing to relocate here. */
4019}
4020
4021static void
4022dw2_expand_symtabs_for_function (struct objfile *objfile,
4023 const char *func_name)
4024{
da51c347
DE
4025 struct mapped_index *index;
4026
4027 dw2_setup (objfile);
4028
4029 index = dwarf2_per_objfile->index_table;
4030
4031 /* index is NULL if OBJF_READNOW. */
4032 if (index)
4033 {
4034 struct dw2_symtab_iterator iter;
4035 struct dwarf2_per_cu_data *per_cu;
4036
4037 /* Note: It doesn't matter what we pass for block_index here. */
4038 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4039 func_name);
4040
4041 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4042 dw2_instantiate_symtab (per_cu);
4043 }
9291a0cd
TT
4044}
4045
4046static void
4047dw2_expand_all_symtabs (struct objfile *objfile)
4048{
4049 int i;
4050
4051 dw2_setup (objfile);
1fd400ff
TT
4052
4053 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4054 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4055 {
8832e7e3 4056 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4057
a0f42c21 4058 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4059 }
4060}
4061
4062static void
652a8996
JK
4063dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4064 const char *fullname)
9291a0cd
TT
4065{
4066 int i;
4067
4068 dw2_setup (objfile);
d4637a04
DE
4069
4070 /* We don't need to consider type units here.
4071 This is only called for examining code, e.g. expand_line_sal.
4072 There can be an order of magnitude (or more) more type units
4073 than comp units, and we avoid them if we can. */
4074
4075 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4076 {
4077 int j;
8832e7e3 4078 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7b9f3c50 4079 struct quick_file_names *file_data;
9291a0cd 4080
3d7bb9d9 4081 /* We only need to look at symtabs not already expanded. */
43f3e411 4082 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4083 continue;
4084
e4a48d9d 4085 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4086 if (file_data == NULL)
9291a0cd
TT
4087 continue;
4088
7b9f3c50 4089 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4090 {
652a8996
JK
4091 const char *this_fullname = file_data->file_names[j];
4092
4093 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4094 {
a0f42c21 4095 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4096 break;
4097 }
4098 }
4099 }
4100}
4101
9291a0cd 4102static void
ade7ed9e 4103dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4104 const char * name, domain_enum domain,
ade7ed9e 4105 int global,
40658b94
PH
4106 int (*callback) (struct block *,
4107 struct symbol *, void *),
b5ec771e 4108 void *data, symbol_name_match_type match,
2edb89d3 4109 symbol_compare_ftype *ordered_compare)
9291a0cd 4110{
40658b94 4111 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4112 current language is Ada for a non-Ada objfile using GNU index. As Ada
4113 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4114}
4115
b5ec771e
PA
4116/* Symbol name matcher for .gdb_index names.
4117
4118 Symbol names in .gdb_index have a few particularities:
4119
4120 - There's no indication of which is the language of each symbol.
4121
4122 Since each language has its own symbol name matching algorithm,
4123 and we don't know which language is the right one, we must match
3f563c84
PA
4124 each symbol against all languages. This would be a potential
4125 performance problem if it were not mitigated by the
4126 mapped_index::name_components lookup table, which significantly
4127 reduces the number of times we need to call into this matcher,
4128 making it a non-issue.
b5ec771e
PA
4129
4130 - Symbol names in the index have no overload (parameter)
4131 information. I.e., in C++, "foo(int)" and "foo(long)" both
4132 appear as "foo" in the index, for example.
4133
4134 This means that the lookup names passed to the symbol name
4135 matcher functions must have no parameter information either
4136 because (e.g.) symbol search name "foo" does not match
4137 lookup-name "foo(int)" [while swapping search name for lookup
4138 name would match].
4139*/
4140class gdb_index_symbol_name_matcher
4141{
4142public:
4143 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4144 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4145
4146 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4147 Returns true if any matcher matches. */
4148 bool matches (const char *symbol_name);
4149
4150private:
4151 /* A reference to the lookup name we're matching against. */
4152 const lookup_name_info &m_lookup_name;
4153
4154 /* A vector holding all the different symbol name matchers, for all
4155 languages. */
4156 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4157};
4158
4159gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4160 (const lookup_name_info &lookup_name)
4161 : m_lookup_name (lookup_name)
4162{
4163 /* Prepare the vector of comparison functions upfront, to avoid
4164 doing the same work for each symbol. Care is taken to avoid
4165 matching with the same matcher more than once if/when multiple
4166 languages use the same matcher function. */
4167 auto &matchers = m_symbol_name_matcher_funcs;
4168 matchers.reserve (nr_languages);
4169
4170 matchers.push_back (default_symbol_name_matcher);
4171
4172 for (int i = 0; i < nr_languages; i++)
4173 {
4174 const language_defn *lang = language_def ((enum language) i);
4175 if (lang->la_get_symbol_name_matcher != NULL)
4176 {
4177 symbol_name_matcher_ftype *name_matcher
4178 = lang->la_get_symbol_name_matcher (m_lookup_name);
4179
4180 /* Don't insert the same comparison routine more than once.
4181 Note that we do this linear walk instead of a cheaper
4182 sorted insert, or use a std::set or something like that,
4183 because relative order of function addresses is not
4184 stable. This is not a problem in practice because the
4185 number of supported languages is low, and the cost here
4186 is tiny compared to the number of searches we'll do
4187 afterwards using this object. */
4188 if (std::find (matchers.begin (), matchers.end (), name_matcher)
4189 == matchers.end ())
4190 matchers.push_back (name_matcher);
4191 }
4192 }
4193}
4194
4195bool
4196gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4197{
4198 for (auto matches_name : m_symbol_name_matcher_funcs)
4199 if (matches_name (symbol_name, m_lookup_name, NULL))
4200 return true;
4201
4202 return false;
4203}
4204
e1ef7d7a
PA
4205/* Starting from a search name, return the string that finds the upper
4206 bound of all strings that start with SEARCH_NAME in a sorted name
4207 list. Returns the empty string to indicate that the upper bound is
4208 the end of the list. */
4209
4210static std::string
4211make_sort_after_prefix_name (const char *search_name)
4212{
4213 /* When looking to complete "func", we find the upper bound of all
4214 symbols that start with "func" by looking for where we'd insert
4215 the closest string that would follow "func" in lexicographical
4216 order. Usually, that's "func"-with-last-character-incremented,
4217 i.e. "fund". Mind non-ASCII characters, though. Usually those
4218 will be UTF-8 multi-byte sequences, but we can't be certain.
4219 Especially mind the 0xff character, which is a valid character in
4220 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4221 rule out compilers allowing it in identifiers. Note that
4222 conveniently, strcmp/strcasecmp are specified to compare
4223 characters interpreted as unsigned char. So what we do is treat
4224 the whole string as a base 256 number composed of a sequence of
4225 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4226 to 0, and carries 1 to the following more-significant position.
4227 If the very first character in SEARCH_NAME ends up incremented
4228 and carries/overflows, then the upper bound is the end of the
4229 list. The string after the empty string is also the empty
4230 string.
4231
4232 Some examples of this operation:
4233
4234 SEARCH_NAME => "+1" RESULT
4235
4236 "abc" => "abd"
4237 "ab\xff" => "ac"
4238 "\xff" "a" "\xff" => "\xff" "b"
4239 "\xff" => ""
4240 "\xff\xff" => ""
4241 "" => ""
4242
4243 Then, with these symbols for example:
4244
4245 func
4246 func1
4247 fund
4248
4249 completing "func" looks for symbols between "func" and
4250 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4251 which finds "func" and "func1", but not "fund".
4252
4253 And with:
4254
4255 funcÿ (Latin1 'ÿ' [0xff])
4256 funcÿ1
4257 fund
4258
4259 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4260 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4261
4262 And with:
4263
4264 ÿÿ (Latin1 'ÿ' [0xff])
4265 ÿÿ1
4266
4267 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4268 the end of the list.
4269 */
4270 std::string after = search_name;
4271 while (!after.empty () && (unsigned char) after.back () == 0xff)
4272 after.pop_back ();
4273 if (!after.empty ())
4274 after.back () = (unsigned char) after.back () + 1;
4275 return after;
4276}
4277
5c58de74 4278/* See declaration. */
61d96d7e 4279
5c58de74
PA
4280std::pair<std::vector<name_component>::const_iterator,
4281 std::vector<name_component>::const_iterator>
4282mapped_index::find_name_components_bounds
4283 (const lookup_name_info &lookup_name_without_params) const
3f563c84 4284{
5c58de74
PA
4285 auto *name_cmp
4286 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84
PA
4287
4288 const char *cplus
c62446b1 4289 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4290
3f563c84
PA
4291 /* Comparison function object for lower_bound that matches against a
4292 given symbol name. */
4293 auto lookup_compare_lower = [&] (const name_component &elem,
4294 const char *name)
4295 {
5c58de74 4296 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4297 const char *elem_name = elem_qualified + elem.name_offset;
4298 return name_cmp (elem_name, name) < 0;
4299 };
4300
4301 /* Comparison function object for upper_bound that matches against a
4302 given symbol name. */
4303 auto lookup_compare_upper = [&] (const char *name,
4304 const name_component &elem)
4305 {
5c58de74 4306 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4307 const char *elem_name = elem_qualified + elem.name_offset;
4308 return name_cmp (name, elem_name) < 0;
4309 };
4310
5c58de74
PA
4311 auto begin = this->name_components.begin ();
4312 auto end = this->name_components.end ();
3f563c84
PA
4313
4314 /* Find the lower bound. */
4315 auto lower = [&] ()
4316 {
5c58de74 4317 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4318 return begin;
4319 else
4320 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4321 } ();
4322
4323 /* Find the upper bound. */
4324 auto upper = [&] ()
4325 {
5c58de74 4326 if (lookup_name_without_params.completion_mode ())
3f563c84 4327 {
e1ef7d7a
PA
4328 /* In completion mode, we want UPPER to point past all
4329 symbols names that have the same prefix. I.e., with
4330 these symbols, and completing "func":
4331
4332 function << lower bound
4333 function1
4334 other_function << upper bound
4335
4336 We find the upper bound by looking for the insertion
4337 point of "func"-with-last-character-incremented,
4338 i.e. "fund". */
4339 std::string after = make_sort_after_prefix_name (cplus);
4340 if (after.empty ())
3f563c84 4341 return end;
e6b2f5ef
PA
4342 return std::lower_bound (lower, end, after.c_str (),
4343 lookup_compare_lower);
3f563c84
PA
4344 }
4345 else
4346 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4347 } ();
4348
5c58de74
PA
4349 return {lower, upper};
4350}
4351
4352/* See declaration. */
4353
4354void
4355mapped_index::build_name_components ()
4356{
4357 if (!this->name_components.empty ())
4358 return;
4359
4360 this->name_components_casing = case_sensitivity;
4361 auto *name_cmp
4362 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4363
4364 /* The code below only knows how to break apart components of C++
4365 symbol names (and other languages that use '::' as
4366 namespace/module separator). If we add support for wild matching
4367 to some language that uses some other operator (E.g., Ada, Go and
4368 D use '.'), then we'll need to try splitting the symbol name
4369 according to that language too. Note that Ada does support wild
4370 matching, but doesn't currently support .gdb_index. */
4371 for (size_t iter = 0; iter < this->symbol_table_slots; ++iter)
4372 {
4373 offset_type idx = 2 * iter;
4374
4375 if (this->symbol_table[idx] == 0
4376 && this->symbol_table[idx + 1] == 0)
4377 continue;
4378
4379 const char *name = this->symbol_name_at (idx);
4380
4381 /* Add each name component to the name component table. */
4382 unsigned int previous_len = 0;
4383 for (unsigned int current_len = cp_find_first_component (name);
4384 name[current_len] != '\0';
4385 current_len += cp_find_first_component (name + current_len))
4386 {
4387 gdb_assert (name[current_len] == ':');
4388 this->name_components.push_back ({previous_len, idx});
4389 /* Skip the '::'. */
4390 current_len += 2;
4391 previous_len = current_len;
4392 }
4393 this->name_components.push_back ({previous_len, idx});
4394 }
4395
4396 /* Sort name_components elements by name. */
4397 auto name_comp_compare = [&] (const name_component &left,
4398 const name_component &right)
4399 {
4400 const char *left_qualified = this->symbol_name_at (left.idx);
4401 const char *right_qualified = this->symbol_name_at (right.idx);
4402
4403 const char *left_name = left_qualified + left.name_offset;
4404 const char *right_name = right_qualified + right.name_offset;
4405
4406 return name_cmp (left_name, right_name) < 0;
4407 };
4408
4409 std::sort (this->name_components.begin (),
4410 this->name_components.end (),
4411 name_comp_compare);
4412}
4413
4414/* Helper for dw2_expand_symtabs_matching that works with a
4415 mapped_index instead of the containing objfile. This is split to a
4416 separate function in order to be able to unit test the
4417 name_components matching using a mock mapped_index. For each
4418 symbol name that matches, calls MATCH_CALLBACK, passing it the
4419 symbol's index in the mapped_index symbol table. */
4420
4421static void
4422dw2_expand_symtabs_matching_symbol
4423 (mapped_index &index,
4424 const lookup_name_info &lookup_name_in,
4425 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4426 enum search_domain kind,
4427 gdb::function_view<void (offset_type)> match_callback)
4428{
4429 lookup_name_info lookup_name_without_params
4430 = lookup_name_in.make_ignore_params ();
4431 gdb_index_symbol_name_matcher lookup_name_matcher
4432 (lookup_name_without_params);
4433
4434 /* Build the symbol name component sorted vector, if we haven't
4435 yet. */
4436 index.build_name_components ();
4437
4438 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4439
3f563c84
PA
4440 /* Now for each symbol name in range, check to see if we have a name
4441 match, and if so, call the MATCH_CALLBACK callback. */
4442
4443 /* The same symbol may appear more than once in the range though.
4444 E.g., if we're looking for symbols that complete "w", and we have
4445 a symbol named "w1::w2", we'll find the two name components for
4446 that same symbol in the range. To be sure we only call the
4447 callback once per symbol, we first collect the symbol name
4448 indexes that matched in a temporary vector and ignore
4449 duplicates. */
4450 std::vector<offset_type> matches;
5c58de74 4451 matches.reserve (std::distance (bounds.first, bounds.second));
3f563c84 4452
5c58de74 4453 for (; bounds.first != bounds.second; ++bounds.first)
3f563c84 4454 {
5c58de74 4455 const char *qualified = index.symbol_name_at (bounds.first->idx);
3f563c84
PA
4456
4457 if (!lookup_name_matcher.matches (qualified)
4458 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4459 continue;
4460
5c58de74 4461 matches.push_back (bounds.first->idx);
3f563c84
PA
4462 }
4463
4464 std::sort (matches.begin (), matches.end ());
4465
4466 /* Finally call the callback, once per match. */
4467 ULONGEST prev = -1;
4468 for (offset_type idx : matches)
4469 {
4470 if (prev != idx)
4471 {
4472 match_callback (idx);
4473 prev = idx;
4474 }
4475 }
4476
4477 /* Above we use a type wider than idx's for 'prev', since 0 and
4478 (offset_type)-1 are both possible values. */
4479 static_assert (sizeof (prev) > sizeof (offset_type), "");
4480}
4481
c62446b1
PA
4482#if GDB_SELF_TEST
4483
4484namespace selftests { namespace dw2_expand_symtabs_matching {
4485
4486/* A wrapper around mapped_index that builds a mock mapped_index, from
4487 the symbol list passed as parameter to the constructor. */
4488class mock_mapped_index
4489{
4490public:
4491 template<size_t N>
4492 mock_mapped_index (const char *(&symbols)[N])
4493 : mock_mapped_index (symbols, N)
4494 {}
4495
4496 /* Access the built index. */
4497 mapped_index &index ()
4498 { return m_index; }
4499
4500 /* Disable copy. */
4501 mock_mapped_index(const mock_mapped_index &) = delete;
4502 void operator= (const mock_mapped_index &) = delete;
4503
4504private:
4505 mock_mapped_index (const char **symbols, size_t symbols_size)
4506 {
4507 /* No string can live at offset zero. Add a dummy entry. */
4508 obstack_grow_str0 (&m_constant_pool, "");
4509
4510 for (size_t i = 0; i < symbols_size; i++)
4511 {
4512 const char *sym = symbols[i];
4513 size_t offset = obstack_object_size (&m_constant_pool);
4514 obstack_grow_str0 (&m_constant_pool, sym);
4515 m_symbol_table.push_back (offset);
4516 m_symbol_table.push_back (0);
4517 };
4518
4519 m_index.constant_pool = (const char *) obstack_base (&m_constant_pool);
4520 m_index.symbol_table = m_symbol_table.data ();
4521 m_index.symbol_table_slots = m_symbol_table.size () / 2;
4522 }
4523
4524public:
4525 /* The built mapped_index. */
4526 mapped_index m_index{};
4527
4528 /* The storage that the built mapped_index uses for symbol and
4529 constant pool tables. */
4530 std::vector<offset_type> m_symbol_table;
4531 auto_obstack m_constant_pool;
4532};
4533
4534/* Convenience function that converts a NULL pointer to a "<null>"
4535 string, to pass to print routines. */
4536
4537static const char *
4538string_or_null (const char *str)
4539{
4540 return str != NULL ? str : "<null>";
4541}
4542
4543/* Check if a lookup_name_info built from
4544 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4545 index. EXPECTED_LIST is the list of expected matches, in expected
4546 matching order. If no match expected, then an empty list is
4547 specified. Returns true on success. On failure prints a warning
4548 indicating the file:line that failed, and returns false. */
4549
4550static bool
4551check_match (const char *file, int line,
4552 mock_mapped_index &mock_index,
4553 const char *name, symbol_name_match_type match_type,
4554 bool completion_mode,
4555 std::initializer_list<const char *> expected_list)
4556{
4557 lookup_name_info lookup_name (name, match_type, completion_mode);
4558
4559 bool matched = true;
4560
4561 auto mismatch = [&] (const char *expected_str,
4562 const char *got)
4563 {
4564 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4565 "expected=\"%s\", got=\"%s\"\n"),
4566 file, line,
4567 (match_type == symbol_name_match_type::FULL
4568 ? "FULL" : "WILD"),
4569 name, string_or_null (expected_str), string_or_null (got));
4570 matched = false;
4571 };
4572
4573 auto expected_it = expected_list.begin ();
4574 auto expected_end = expected_list.end ();
4575
4576 dw2_expand_symtabs_matching_symbol (mock_index.index (), lookup_name,
4577 NULL, ALL_DOMAIN,
4578 [&] (offset_type idx)
4579 {
4580 const char *matched_name = mock_index.index ().symbol_name_at (idx);
4581 const char *expected_str
4582 = expected_it == expected_end ? NULL : *expected_it++;
4583
4584 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4585 mismatch (expected_str, matched_name);
4586 });
4587
4588 const char *expected_str
4589 = expected_it == expected_end ? NULL : *expected_it++;
4590 if (expected_str != NULL)
4591 mismatch (expected_str, NULL);
4592
4593 return matched;
4594}
4595
4596/* The symbols added to the mock mapped_index for testing (in
4597 canonical form). */
4598static const char *test_symbols[] = {
4599 "function",
4600 "std::bar",
4601 "std::zfunction",
4602 "std::zfunction2",
4603 "w1::w2",
4604 "ns::foo<char*>",
4605 "ns::foo<int>",
4606 "ns::foo<long>",
4607
e1ef7d7a
PA
4608 /* These are used to check that the increment-last-char in the
4609 matching algorithm for completion doesn't match "t1_fund" when
4610 completing "t1_func". */
4611 "t1_func",
4612 "t1_func1",
4613 "t1_fund",
4614 "t1_fund1",
4615
4616 /* A UTF-8 name with multi-byte sequences to make sure that
4617 cp-name-parser understands this as a single identifier ("função"
4618 is "function" in PT). */
4619 u8"u8função",
4620
4621 /* \377 (0xff) is Latin1 'ÿ'. */
4622 "yfunc\377",
4623
4624 /* \377 (0xff) is Latin1 'ÿ'. */
4625 "\377",
4626 "\377\377123",
4627
c62446b1
PA
4628 /* A name with all sorts of complications. Starts with "z" to make
4629 it easier for the completion tests below. */
4630#define Z_SYM_NAME \
4631 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4632 "::tuple<(anonymous namespace)::ui*, " \
4633 "std::default_delete<(anonymous namespace)::ui>, void>"
4634
4635 Z_SYM_NAME
4636};
4637
5c58de74
PA
4638/* Returns true if the mapped_index::find_name_component_bounds method
4639 finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME, in
4640 completion mode. */
4641
4642static bool
4643check_find_bounds_finds (mapped_index &index,
4644 const char *search_name,
4645 gdb::array_view<const char *> expected_syms)
4646{
4647 lookup_name_info lookup_name (search_name,
4648 symbol_name_match_type::FULL, true);
4649
4650 auto bounds = index.find_name_components_bounds (lookup_name);
4651
4652 size_t distance = std::distance (bounds.first, bounds.second);
4653 if (distance != expected_syms.size ())
4654 return false;
4655
4656 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4657 {
4658 auto nc_elem = bounds.first + exp_elem;
4659 const char *qualified = index.symbol_name_at (nc_elem->idx);
4660 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4661 return false;
4662 }
4663
4664 return true;
4665}
4666
4667/* Test the lower-level mapped_index::find_name_component_bounds
4668 method. */
4669
c62446b1 4670static void
5c58de74
PA
4671test_mapped_index_find_name_component_bounds ()
4672{
4673 mock_mapped_index mock_index (test_symbols);
4674
4675 mock_index.index ().build_name_components ();
4676
4677 /* Test the lower-level mapped_index::find_name_component_bounds
4678 method in completion mode. */
4679 {
4680 static const char *expected_syms[] = {
4681 "t1_func",
4682 "t1_func1",
5c58de74
PA
4683 };
4684
4685 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
4686 "t1_func", expected_syms));
4687 }
4688
4689 /* Check that the increment-last-char in the name matching algorithm
4690 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4691 {
4692 static const char *expected_syms1[] = {
4693 "\377",
4694 "\377\377123",
4695 };
4696 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
4697 "\377", expected_syms1));
4698
4699 static const char *expected_syms2[] = {
4700 "\377\377123",
4701 };
4702 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
4703 "\377\377", expected_syms2));
4704 }
4705}
4706
4707/* Test dw2_expand_symtabs_matching_symbol. */
4708
4709static void
4710test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4711{
4712 mock_mapped_index mock_index (test_symbols);
4713
4714 /* We let all tests run until the end even if some fails, for debug
4715 convenience. */
4716 bool any_mismatch = false;
4717
4718 /* Create the expected symbols list (an initializer_list). Needed
4719 because lists have commas, and we need to pass them to CHECK,
4720 which is a macro. */
4721#define EXPECT(...) { __VA_ARGS__ }
4722
4723 /* Wrapper for check_match that passes down the current
4724 __FILE__/__LINE__. */
4725#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4726 any_mismatch |= !check_match (__FILE__, __LINE__, \
4727 mock_index, \
4728 NAME, MATCH_TYPE, COMPLETION_MODE, \
4729 EXPECTED_LIST)
4730
4731 /* Identity checks. */
4732 for (const char *sym : test_symbols)
4733 {
4734 /* Should be able to match all existing symbols. */
4735 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4736 EXPECT (sym));
4737
4738 /* Should be able to match all existing symbols with
4739 parameters. */
4740 std::string with_params = std::string (sym) + "(int)";
4741 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4742 EXPECT (sym));
4743
4744 /* Should be able to match all existing symbols with
4745 parameters and qualifiers. */
4746 with_params = std::string (sym) + " ( int ) const";
4747 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4748 EXPECT (sym));
4749
4750 /* This should really find sym, but cp-name-parser.y doesn't
4751 know about lvalue/rvalue qualifiers yet. */
4752 with_params = std::string (sym) + " ( int ) &&";
4753 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4754 {});
4755 }
4756
e1ef7d7a
PA
4757 /* Check that the name matching algorithm for completion doesn't get
4758 confused with Latin1 'ÿ' / 0xff. */
4759 {
4760 static const char str[] = "\377";
4761 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4762 EXPECT ("\377", "\377\377123"));
4763 }
4764
4765 /* Check that the increment-last-char in the matching algorithm for
4766 completion doesn't match "t1_fund" when completing "t1_func". */
4767 {
4768 static const char str[] = "t1_func";
4769 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4770 EXPECT ("t1_func", "t1_func1"));
4771 }
4772
c62446b1
PA
4773 /* Check that completion mode works at each prefix of the expected
4774 symbol name. */
4775 {
4776 static const char str[] = "function(int)";
4777 size_t len = strlen (str);
4778 std::string lookup;
4779
4780 for (size_t i = 1; i < len; i++)
4781 {
4782 lookup.assign (str, i);
4783 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4784 EXPECT ("function"));
4785 }
4786 }
4787
4788 /* While "w" is a prefix of both components, the match function
4789 should still only be called once. */
4790 {
4791 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4792 EXPECT ("w1::w2"));
4793 }
4794
4795 /* Same, with a "complicated" symbol. */
4796 {
4797 static const char str[] = Z_SYM_NAME;
4798 size_t len = strlen (str);
4799 std::string lookup;
4800
4801 for (size_t i = 1; i < len; i++)
4802 {
4803 lookup.assign (str, i);
4804 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4805 EXPECT (Z_SYM_NAME));
4806 }
4807 }
4808
4809 /* In FULL mode, an incomplete symbol doesn't match. */
4810 {
4811 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4812 {});
4813 }
4814
4815 /* A complete symbol with parameters matches any overload, since the
4816 index has no overload info. */
4817 {
4818 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4819 EXPECT ("std::zfunction", "std::zfunction2"));
4820 }
4821
4822 /* Check that whitespace is ignored appropriately. A symbol with a
4823 template argument list. */
4824 {
4825 static const char expected[] = "ns::foo<int>";
4826 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4827 EXPECT (expected));
4828 }
4829
4830 /* Check that whitespace is ignored appropriately. A symbol with a
4831 template argument list that includes a pointer. */
4832 {
4833 static const char expected[] = "ns::foo<char*>";
4834 /* Try both completion and non-completion modes. */
4835 static const bool completion_mode[2] = {false, true};
4836 for (size_t i = 0; i < 2; i++)
4837 {
4838 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4839 completion_mode[i], EXPECT (expected));
4840
4841 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4842 completion_mode[i], EXPECT (expected));
4843 }
4844 }
4845
4846 {
4847 /* Check method qualifiers are ignored. */
4848 static const char expected[] = "ns::foo<char*>";
4849 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4850 symbol_name_match_type::FULL, true, EXPECT (expected));
4851 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4852 symbol_name_match_type::FULL, true, EXPECT (expected));
4853 }
4854
4855 /* Test lookup names that don't match anything. */
4856 {
4857 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4858 {});
4859 }
4860
4861 SELF_CHECK (!any_mismatch);
4862
4863#undef EXPECT
4864#undef CHECK_MATCH
4865}
4866
5c58de74
PA
4867static void
4868run_test ()
4869{
4870 test_mapped_index_find_name_component_bounds ();
4871 test_dw2_expand_symtabs_matching_symbol ();
4872}
4873
c62446b1
PA
4874}} // namespace selftests::dw2_expand_symtabs_matching
4875
4876#endif /* GDB_SELF_TEST */
4877
3f563c84
PA
4878/* Helper for dw2_expand_matching symtabs. Called on each symbol
4879 matched, to expand corresponding CUs that were marked. IDX is the
4880 index of the symbol name that matched. */
4881
4882static void
4883dw2_expand_marked_cus
4884 (mapped_index &index, offset_type idx,
4885 struct objfile *objfile,
4886 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4887 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4888 search_domain kind)
4889{
4890 const char *name;
4891 offset_type *vec, vec_len, vec_idx;
4892 bool global_seen = false;
4893
61920122
PA
4894 vec = (offset_type *) (index.constant_pool
4895 + MAYBE_SWAP (index.symbol_table[idx + 1]));
4896 vec_len = MAYBE_SWAP (vec[0]);
4897 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4898 {
4899 struct dwarf2_per_cu_data *per_cu;
4900 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4901 /* This value is only valid for index versions >= 7. */
4902 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4903 gdb_index_symbol_kind symbol_kind =
4904 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4905 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4906 /* Only check the symbol attributes if they're present.
4907 Indices prior to version 7 don't record them,
4908 and indices >= 7 may elide them for certain symbols
4909 (gold does this). */
4910 int attrs_valid =
4911 (index.version >= 7
4912 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4913
4914 /* Work around gold/15646. */
4915 if (attrs_valid)
9291a0cd 4916 {
61920122
PA
4917 if (!is_static && global_seen)
4918 continue;
4919 if (!is_static)
4920 global_seen = true;
4921 }
3190f0c6 4922
61920122
PA
4923 /* Only check the symbol's kind if it has one. */
4924 if (attrs_valid)
4925 {
4926 switch (kind)
8943b874 4927 {
61920122
PA
4928 case VARIABLES_DOMAIN:
4929 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4930 continue;
4931 break;
4932 case FUNCTIONS_DOMAIN:
4933 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4934 continue;
61920122
PA
4935 break;
4936 case TYPES_DOMAIN:
4937 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4938 continue;
4939 break;
4940 default:
4941 break;
8943b874 4942 }
61920122 4943 }
8943b874 4944
61920122
PA
4945 /* Don't crash on bad data. */
4946 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4947 + dwarf2_per_objfile->n_type_units))
4948 {
4949 complaint (&symfile_complaints,
4950 _(".gdb_index entry has bad CU index"
4951 " [in module %s]"), objfile_name (objfile));
4952 continue;
4953 }
4954
4955 per_cu = dw2_get_cutu (cu_index);
4956 if (file_matcher == NULL || per_cu->v.quick->mark)
4957 {
4958 int symtab_was_null =
4959 (per_cu->v.quick->compunit_symtab == NULL);
4960
4961 dw2_instantiate_symtab (per_cu);
4962
4963 if (expansion_notify != NULL
4964 && symtab_was_null
4965 && per_cu->v.quick->compunit_symtab != NULL)
4966 expansion_notify (per_cu->v.quick->compunit_symtab);
4967 }
4968 }
4969}
4970
4971static void
4972dw2_expand_symtabs_matching
4973 (struct objfile *objfile,
4974 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4975 const lookup_name_info &lookup_name,
4976 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4977 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4978 enum search_domain kind)
4979{
4980 int i;
4981 offset_type iter;
4982
4983 dw2_setup (objfile);
4984
4985 /* index_table is NULL if OBJF_READNOW. */
4986 if (!dwarf2_per_objfile->index_table)
4987 return;
4988
4989 if (file_matcher != NULL)
4990 {
4991 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4992 htab_eq_pointer,
4993 NULL, xcalloc, xfree));
4994 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4995 htab_eq_pointer,
4996 NULL, xcalloc, xfree));
4997
4998 /* The rule is CUs specify all the files, including those used by
4999 any TU, so there's no need to scan TUs here. */
5000
5001 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5002 {
5003 int j;
5004 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5005 struct quick_file_names *file_data;
5006 void **slot;
5007
5008 QUIT;
5009
5010 per_cu->v.quick->mark = 0;
156942c7 5011
61920122
PA
5012 /* We only need to look at symtabs not already expanded. */
5013 if (per_cu->v.quick->compunit_symtab)
5014 continue;
5015
5016 file_data = dw2_get_file_names (per_cu);
5017 if (file_data == NULL)
5018 continue;
5019
5020 if (htab_find (visited_not_found.get (), file_data) != NULL)
5021 continue;
5022 else if (htab_find (visited_found.get (), file_data) != NULL)
3190f0c6 5023 {
61920122 5024 per_cu->v.quick->mark = 1;
3190f0c6
DE
5025 continue;
5026 }
5027
61920122 5028 for (j = 0; j < file_data->num_file_names; ++j)
276d885b 5029 {
61920122 5030 const char *this_real_name;
276d885b 5031
61920122
PA
5032 if (file_matcher (file_data->file_names[j], false))
5033 {
5034 per_cu->v.quick->mark = 1;
5035 break;
5036 }
5037
5038 /* Before we invoke realpath, which can get expensive when many
5039 files are involved, do a quick comparison of the basenames. */
5040 if (!basenames_may_differ
5041 && !file_matcher (lbasename (file_data->file_names[j]),
5042 true))
5043 continue;
276d885b 5044
61920122
PA
5045 this_real_name = dw2_get_real_path (objfile, file_data, j);
5046 if (file_matcher (this_real_name, false))
276d885b 5047 {
61920122
PA
5048 per_cu->v.quick->mark = 1;
5049 break;
276d885b
GB
5050 }
5051 }
61920122
PA
5052
5053 slot = htab_find_slot (per_cu->v.quick->mark
5054 ? visited_found.get ()
5055 : visited_not_found.get (),
5056 file_data, INSERT);
5057 *slot = file_data;
9291a0cd 5058 }
61920122
PA
5059 }
5060
5061 mapped_index &index = *dwarf2_per_objfile->index_table;
5062
5063 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5064 symbol_matcher,
5065 kind, [&] (offset_type idx)
5066 {
5067 dw2_expand_marked_cus (index, idx, objfile, file_matcher,
5068 expansion_notify, kind);
5069 });
9291a0cd
TT
5070}
5071
43f3e411 5072/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
9703b513
TT
5073 symtab. */
5074
43f3e411
DE
5075static struct compunit_symtab *
5076recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5077 CORE_ADDR pc)
9703b513
TT
5078{
5079 int i;
5080
43f3e411
DE
5081 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5082 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5083 return cust;
9703b513 5084
43f3e411 5085 if (cust->includes == NULL)
a3ec0bb1
DE
5086 return NULL;
5087
43f3e411 5088 for (i = 0; cust->includes[i]; ++i)
9703b513 5089 {
43f3e411 5090 struct compunit_symtab *s = cust->includes[i];
9703b513 5091
43f3e411 5092 s = recursively_find_pc_sect_compunit_symtab (s, pc);
9703b513
TT
5093 if (s != NULL)
5094 return s;
5095 }
5096
5097 return NULL;
5098}
5099
43f3e411
DE
5100static struct compunit_symtab *
5101dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5102 struct bound_minimal_symbol msymbol,
5103 CORE_ADDR pc,
5104 struct obj_section *section,
5105 int warn_if_readin)
9291a0cd
TT
5106{
5107 struct dwarf2_per_cu_data *data;
43f3e411 5108 struct compunit_symtab *result;
9291a0cd
TT
5109
5110 dw2_setup (objfile);
5111
5112 if (!objfile->psymtabs_addrmap)
5113 return NULL;
5114
9a3c8263
SM
5115 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5116 pc);
9291a0cd
TT
5117 if (!data)
5118 return NULL;
5119
43f3e411 5120 if (warn_if_readin && data->v.quick->compunit_symtab)
abebb8b0 5121 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
5122 paddress (get_objfile_arch (objfile), pc));
5123
43f3e411
DE
5124 result
5125 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5126 pc);
9703b513
TT
5127 gdb_assert (result != NULL);
5128 return result;
9291a0cd
TT
5129}
5130
9291a0cd 5131static void
44b13c5a 5132dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 5133 void *data, int need_fullname)
9291a0cd 5134{
9291a0cd 5135 dw2_setup (objfile);
ae2de4f8 5136
bbf2f4df 5137 if (!dwarf2_per_objfile->filenames_cache)
24c79950 5138 {
bbf2f4df 5139 dwarf2_per_objfile->filenames_cache.emplace ();
24c79950 5140
bbf2f4df
PA
5141 htab_up visited (htab_create_alloc (10,
5142 htab_hash_pointer, htab_eq_pointer,
5143 NULL, xcalloc, xfree));
24c79950 5144
bbf2f4df
PA
5145 /* The rule is CUs specify all the files, including those used
5146 by any TU, so there's no need to scan TUs here. We can
5147 ignore file names coming from already-expanded CUs. */
24c79950 5148
bbf2f4df
PA
5149 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5150 {
5151 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 5152
bbf2f4df
PA
5153 if (per_cu->v.quick->compunit_symtab)
5154 {
5155 void **slot = htab_find_slot (visited.get (),
5156 per_cu->v.quick->file_names,
5157 INSERT);
9291a0cd 5158
bbf2f4df
PA
5159 *slot = per_cu->v.quick->file_names;
5160 }
24c79950 5161 }
24c79950 5162
bbf2f4df 5163 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 5164 {
bbf2f4df
PA
5165 int j;
5166 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5167 struct quick_file_names *file_data;
5168 void **slot;
5169
5170 /* We only need to look at symtabs not already expanded. */
5171 if (per_cu->v.quick->compunit_symtab)
5172 continue;
74e2f255 5173
bbf2f4df
PA
5174 file_data = dw2_get_file_names (per_cu);
5175 if (file_data == NULL)
5176 continue;
5177
5178 slot = htab_find_slot (visited.get (), file_data, INSERT);
5179 if (*slot)
5180 {
5181 /* Already visited. */
5182 continue;
5183 }
5184 *slot = file_data;
5185
5186 for (int j = 0; j < file_data->num_file_names; ++j)
5187 {
5188 const char *filename = file_data->file_names[j];
5189 dwarf2_per_objfile->filenames_cache->seen (filename);
5190 }
9291a0cd
TT
5191 }
5192 }
bbf2f4df
PA
5193
5194 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5195 {
14278e1f 5196 gdb::unique_xmalloc_ptr<char> this_real_name;
bbf2f4df
PA
5197
5198 if (need_fullname)
5199 this_real_name = gdb_realpath (filename);
14278e1f 5200 (*fun) (filename, this_real_name.get (), data);
bbf2f4df 5201 });
9291a0cd
TT
5202}
5203
5204static int
5205dw2_has_symbols (struct objfile *objfile)
5206{
5207 return 1;
5208}
5209
5210const struct quick_symbol_functions dwarf2_gdb_index_functions =
5211{
5212 dw2_has_symbols,
5213 dw2_find_last_source_symtab,
5214 dw2_forget_cached_source_info,
f8eba3c6 5215 dw2_map_symtabs_matching_filename,
9291a0cd 5216 dw2_lookup_symbol,
9291a0cd
TT
5217 dw2_print_stats,
5218 dw2_dump,
5219 dw2_relocate,
5220 dw2_expand_symtabs_for_function,
5221 dw2_expand_all_symtabs,
652a8996 5222 dw2_expand_symtabs_with_fullname,
40658b94 5223 dw2_map_matching_symbols,
9291a0cd 5224 dw2_expand_symtabs_matching,
43f3e411 5225 dw2_find_pc_sect_compunit_symtab,
71a3c369 5226 NULL,
9291a0cd
TT
5227 dw2_map_symbol_filenames
5228};
5229
5230/* Initialize for reading DWARF for this objfile. Return 0 if this
5231 file will use psymtabs, or 1 if using the GNU index. */
5232
5233int
5234dwarf2_initialize_objfile (struct objfile *objfile)
5235{
5236 /* If we're about to read full symbols, don't bother with the
5237 indices. In this case we also don't care if some other debug
5238 format is making psymtabs, because they are all about to be
5239 expanded anyway. */
5240 if ((objfile->flags & OBJF_READNOW))
5241 {
5242 int i;
5243
5244 dwarf2_per_objfile->using_index = 1;
5245 create_all_comp_units (objfile);
0e50663e 5246 create_all_type_units (objfile);
7b9f3c50
DE
5247 dwarf2_per_objfile->quick_file_names_table =
5248 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 5249
1fd400ff 5250 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 5251 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 5252 {
8832e7e3 5253 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 5254
e254ef6a
DE
5255 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5256 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5257 }
5258
5259 /* Return 1 so that gdb sees the "quick" functions. However,
5260 these functions will be no-ops because we will have expanded
5261 all symtabs. */
5262 return 1;
5263 }
5264
5265 if (dwarf2_read_index (objfile))
5266 return 1;
5267
9291a0cd
TT
5268 return 0;
5269}
5270
5271\f
5272
dce234bc
PP
5273/* Build a partial symbol table. */
5274
5275void
f29dff0a 5276dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5277{
c9bf0622 5278
af5bf4ad
SM
5279 if (objfile->global_psymbols.capacity () == 0
5280 && objfile->static_psymbols.capacity () == 0)
5281 init_psymbol_list (objfile, 1024);
c906108c 5282
492d29ea 5283 TRY
c9bf0622
TT
5284 {
5285 /* This isn't really ideal: all the data we allocate on the
5286 objfile's obstack is still uselessly kept around. However,
5287 freeing it seems unsafe. */
906768f9 5288 psymtab_discarder psymtabs (objfile);
c9bf0622 5289 dwarf2_build_psymtabs_hard (objfile);
906768f9 5290 psymtabs.keep ();
c9bf0622 5291 }
492d29ea
PA
5292 CATCH (except, RETURN_MASK_ERROR)
5293 {
5294 exception_print (gdb_stderr, except);
5295 }
5296 END_CATCH
c906108c 5297}
c906108c 5298
1ce1cefd
DE
5299/* Return the total length of the CU described by HEADER. */
5300
5301static unsigned int
5302get_cu_length (const struct comp_unit_head *header)
5303{
5304 return header->initial_length_size + header->length;
5305}
5306
9c541725 5307/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 5308
9c541725
PA
5309static inline bool
5310offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 5311{
9c541725
PA
5312 sect_offset bottom = cu_header->sect_off;
5313 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 5314
9c541725 5315 return sect_off >= bottom && sect_off < top;
45452591
DE
5316}
5317
3b80fe9b
DE
5318/* Find the base address of the compilation unit for range lists and
5319 location lists. It will normally be specified by DW_AT_low_pc.
5320 In DWARF-3 draft 4, the base address could be overridden by
5321 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5322 compilation units with discontinuous ranges. */
5323
5324static void
5325dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5326{
5327 struct attribute *attr;
5328
5329 cu->base_known = 0;
5330 cu->base_address = 0;
5331
5332 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5333 if (attr)
5334 {
31aa7e4e 5335 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
5336 cu->base_known = 1;
5337 }
5338 else
5339 {
5340 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5341 if (attr)
5342 {
31aa7e4e 5343 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
5344 cu->base_known = 1;
5345 }
5346 }
5347}
5348
93311388 5349/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 5350 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
5351 NOTE: This leaves members offset, first_die_offset to be filled in
5352 by the caller. */
107d2387 5353
d521ce57 5354static const gdb_byte *
107d2387 5355read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
5356 const gdb_byte *info_ptr,
5357 struct dwarf2_section_info *section,
5358 rcuh_kind section_kind)
107d2387
AC
5359{
5360 int signed_addr;
891d2f0b 5361 unsigned int bytes_read;
43988095
JK
5362 const char *filename = get_section_file_name (section);
5363 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
5364
5365 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
5366 cu_header->initial_length_size = bytes_read;
5367 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 5368 info_ptr += bytes_read;
107d2387
AC
5369 cu_header->version = read_2_bytes (abfd, info_ptr);
5370 info_ptr += 2;
43988095
JK
5371 if (cu_header->version < 5)
5372 switch (section_kind)
5373 {
5374 case rcuh_kind::COMPILE:
5375 cu_header->unit_type = DW_UT_compile;
5376 break;
5377 case rcuh_kind::TYPE:
5378 cu_header->unit_type = DW_UT_type;
5379 break;
5380 default:
5381 internal_error (__FILE__, __LINE__,
5382 _("read_comp_unit_head: invalid section_kind"));
5383 }
5384 else
5385 {
5386 cu_header->unit_type = static_cast<enum dwarf_unit_type>
5387 (read_1_byte (abfd, info_ptr));
5388 info_ptr += 1;
5389 switch (cu_header->unit_type)
5390 {
5391 case DW_UT_compile:
5392 if (section_kind != rcuh_kind::COMPILE)
5393 error (_("Dwarf Error: wrong unit_type in compilation unit header "
5394 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
5395 filename);
5396 break;
5397 case DW_UT_type:
5398 section_kind = rcuh_kind::TYPE;
5399 break;
5400 default:
5401 error (_("Dwarf Error: wrong unit_type in compilation unit header "
5402 "(is %d, should be %d or %d) [in module %s]"),
5403 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
5404 }
5405
5406 cu_header->addr_size = read_1_byte (abfd, info_ptr);
5407 info_ptr += 1;
5408 }
9c541725
PA
5409 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
5410 cu_header,
5411 &bytes_read);
613e1657 5412 info_ptr += bytes_read;
43988095
JK
5413 if (cu_header->version < 5)
5414 {
5415 cu_header->addr_size = read_1_byte (abfd, info_ptr);
5416 info_ptr += 1;
5417 }
107d2387
AC
5418 signed_addr = bfd_get_sign_extend_vma (abfd);
5419 if (signed_addr < 0)
8e65ff28 5420 internal_error (__FILE__, __LINE__,
e2e0b3e5 5421 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 5422 cu_header->signed_addr_p = signed_addr;
c764a876 5423
43988095
JK
5424 if (section_kind == rcuh_kind::TYPE)
5425 {
5426 LONGEST type_offset;
5427
5428 cu_header->signature = read_8_bytes (abfd, info_ptr);
5429 info_ptr += 8;
5430
5431 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
5432 info_ptr += bytes_read;
9c541725
PA
5433 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
5434 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
5435 error (_("Dwarf Error: Too big type_offset in compilation unit "
5436 "header (is %s) [in module %s]"), plongest (type_offset),
5437 filename);
5438 }
5439
107d2387
AC
5440 return info_ptr;
5441}
5442
36586728
TT
5443/* Helper function that returns the proper abbrev section for
5444 THIS_CU. */
5445
5446static struct dwarf2_section_info *
5447get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5448{
5449 struct dwarf2_section_info *abbrev;
5450
5451 if (this_cu->is_dwz)
5452 abbrev = &dwarf2_get_dwz_file ()->abbrev;
5453 else
5454 abbrev = &dwarf2_per_objfile->abbrev;
5455
5456 return abbrev;
5457}
5458
9ff913ba
DE
5459/* Subroutine of read_and_check_comp_unit_head and
5460 read_and_check_type_unit_head to simplify them.
5461 Perform various error checking on the header. */
5462
5463static void
5464error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
5465 struct dwarf2_section_info *section,
5466 struct dwarf2_section_info *abbrev_section)
9ff913ba 5467{
a32a8923 5468 const char *filename = get_section_file_name (section);
9ff913ba 5469
43988095 5470 if (header->version < 2 || header->version > 5)
9ff913ba 5471 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 5472 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
5473 filename);
5474
9c541725 5475 if (to_underlying (header->abbrev_sect_off)
36586728 5476 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9c541725
PA
5477 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
5478 "(offset 0x%x + 6) [in module %s]"),
5479 to_underlying (header->abbrev_sect_off),
5480 to_underlying (header->sect_off),
9ff913ba
DE
5481 filename);
5482
9c541725 5483 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 5484 avoid potential 32-bit overflow. */
9c541725 5485 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 5486 > section->size)
9c541725
PA
5487 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
5488 "(offset 0x%x + 0) [in module %s]"),
5489 header->length, to_underlying (header->sect_off),
9ff913ba
DE
5490 filename);
5491}
5492
5493/* Read in a CU/TU header and perform some basic error checking.
5494 The contents of the header are stored in HEADER.
5495 The result is a pointer to the start of the first DIE. */
adabb602 5496
d521ce57 5497static const gdb_byte *
9ff913ba
DE
5498read_and_check_comp_unit_head (struct comp_unit_head *header,
5499 struct dwarf2_section_info *section,
4bdcc0c1 5500 struct dwarf2_section_info *abbrev_section,
d521ce57 5501 const gdb_byte *info_ptr,
43988095 5502 rcuh_kind section_kind)
72bf9492 5503{
d521ce57 5504 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 5505 bfd *abfd = get_section_bfd_owner (section);
72bf9492 5506
9c541725 5507 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 5508
43988095 5509 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 5510
9c541725 5511 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 5512
4bdcc0c1 5513 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
5514
5515 return info_ptr;
348e048f
DE
5516}
5517
f4dc4d17
DE
5518/* Fetch the abbreviation table offset from a comp or type unit header. */
5519
5520static sect_offset
5521read_abbrev_offset (struct dwarf2_section_info *section,
9c541725 5522 sect_offset sect_off)
f4dc4d17 5523{
a32a8923 5524 bfd *abfd = get_section_bfd_owner (section);
d521ce57 5525 const gdb_byte *info_ptr;
ac298888 5526 unsigned int initial_length_size, offset_size;
43988095 5527 uint16_t version;
f4dc4d17
DE
5528
5529 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 5530 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5531 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5532 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5533 info_ptr += initial_length_size;
5534
5535 version = read_2_bytes (abfd, info_ptr);
5536 info_ptr += 2;
5537 if (version >= 5)
5538 {
5539 /* Skip unit type and address size. */
5540 info_ptr += 2;
5541 }
5542
9c541725 5543 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
5544}
5545
aaa75496
JB
5546/* Allocate a new partial symtab for file named NAME and mark this new
5547 partial symtab as being an include of PST. */
5548
5549static void
d521ce57 5550dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
5551 struct objfile *objfile)
5552{
5553 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
5554
fbd9ab74
JK
5555 if (!IS_ABSOLUTE_PATH (subpst->filename))
5556 {
5557 /* It shares objfile->objfile_obstack. */
5558 subpst->dirname = pst->dirname;
5559 }
5560
aaa75496
JB
5561 subpst->textlow = 0;
5562 subpst->texthigh = 0;
5563
8d749320
SM
5564 subpst->dependencies
5565 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
5566 subpst->dependencies[0] = pst;
5567 subpst->number_of_dependencies = 1;
5568
5569 subpst->globals_offset = 0;
5570 subpst->n_global_syms = 0;
5571 subpst->statics_offset = 0;
5572 subpst->n_static_syms = 0;
43f3e411 5573 subpst->compunit_symtab = NULL;
aaa75496
JB
5574 subpst->read_symtab = pst->read_symtab;
5575 subpst->readin = 0;
5576
5577 /* No private part is necessary for include psymtabs. This property
5578 can be used to differentiate between such include psymtabs and
10b3939b 5579 the regular ones. */
58a9656e 5580 subpst->read_symtab_private = NULL;
aaa75496
JB
5581}
5582
5583/* Read the Line Number Program data and extract the list of files
5584 included by the source file represented by PST. Build an include
d85a05f0 5585 partial symtab for each of these included files. */
aaa75496
JB
5586
5587static void
5588dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
5589 struct die_info *die,
5590 struct partial_symtab *pst)
aaa75496 5591{
fff8551c 5592 line_header_up lh;
d85a05f0 5593 struct attribute *attr;
aaa75496 5594
d85a05f0
DJ
5595 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5596 if (attr)
9c541725 5597 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
5598 if (lh == NULL)
5599 return; /* No linetable, so no includes. */
5600
c6da4cef 5601 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 5602 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
5603}
5604
348e048f 5605static hashval_t
52dc124a 5606hash_signatured_type (const void *item)
348e048f 5607{
9a3c8263
SM
5608 const struct signatured_type *sig_type
5609 = (const struct signatured_type *) item;
9a619af0 5610
348e048f 5611 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 5612 return sig_type->signature;
348e048f
DE
5613}
5614
5615static int
52dc124a 5616eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 5617{
9a3c8263
SM
5618 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5619 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 5620
348e048f
DE
5621 return lhs->signature == rhs->signature;
5622}
5623
1fd400ff
TT
5624/* Allocate a hash table for signatured types. */
5625
5626static htab_t
673bfd45 5627allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
5628{
5629 return htab_create_alloc_ex (41,
52dc124a
DE
5630 hash_signatured_type,
5631 eq_signatured_type,
1fd400ff
TT
5632 NULL,
5633 &objfile->objfile_obstack,
5634 hashtab_obstack_allocate,
5635 dummy_obstack_deallocate);
5636}
5637
d467dd73 5638/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
5639
5640static int
d467dd73 5641add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 5642{
9a3c8263
SM
5643 struct signatured_type *sigt = (struct signatured_type *) *slot;
5644 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 5645
b4dd5633 5646 **datap = sigt;
1fd400ff
TT
5647 ++*datap;
5648
5649 return 1;
5650}
5651
78d4d2c5 5652/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
5653 and fill them into TYPES_HTAB. It will process only type units,
5654 therefore DW_UT_type. */
c88ee1f0 5655
78d4d2c5
JK
5656static void
5657create_debug_type_hash_table (struct dwo_file *dwo_file,
43988095
JK
5658 dwarf2_section_info *section, htab_t &types_htab,
5659 rcuh_kind section_kind)
348e048f 5660{
3019eac3 5661 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 5662 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
5663 bfd *abfd;
5664 const gdb_byte *info_ptr, *end_ptr;
348e048f 5665
4bdcc0c1
DE
5666 abbrev_section = (dwo_file != NULL
5667 ? &dwo_file->sections.abbrev
5668 : &dwarf2_per_objfile->abbrev);
5669
b4f54984 5670 if (dwarf_read_debug)
43988095
JK
5671 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
5672 get_section_name (section),
a32a8923 5673 get_section_file_name (abbrev_section));
09406207 5674
78d4d2c5
JK
5675 dwarf2_read_section (objfile, section);
5676 info_ptr = section->buffer;
348e048f 5677
78d4d2c5
JK
5678 if (info_ptr == NULL)
5679 return;
348e048f 5680
78d4d2c5
JK
5681 /* We can't set abfd until now because the section may be empty or
5682 not present, in which case the bfd is unknown. */
5683 abfd = get_section_bfd_owner (section);
348e048f 5684
78d4d2c5
JK
5685 /* We don't use init_cutu_and_read_dies_simple, or some such, here
5686 because we don't need to read any dies: the signature is in the
5687 header. */
3019eac3 5688
78d4d2c5
JK
5689 end_ptr = info_ptr + section->size;
5690 while (info_ptr < end_ptr)
5691 {
78d4d2c5
JK
5692 struct signatured_type *sig_type;
5693 struct dwo_unit *dwo_tu;
5694 void **slot;
5695 const gdb_byte *ptr = info_ptr;
5696 struct comp_unit_head header;
5697 unsigned int length;
8b70b953 5698
9c541725 5699 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 5700
a49dd8dd
JK
5701 /* Initialize it due to a false compiler warning. */
5702 header.signature = -1;
9c541725 5703 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 5704
78d4d2c5
JK
5705 /* We need to read the type's signature in order to build the hash
5706 table, but we don't need anything else just yet. */
348e048f 5707
43988095
JK
5708 ptr = read_and_check_comp_unit_head (&header, section,
5709 abbrev_section, ptr, section_kind);
348e048f 5710
78d4d2c5 5711 length = get_cu_length (&header);
6caca83c 5712
78d4d2c5
JK
5713 /* Skip dummy type units. */
5714 if (ptr >= info_ptr + length
43988095
JK
5715 || peek_abbrev_code (abfd, ptr) == 0
5716 || header.unit_type != DW_UT_type)
78d4d2c5
JK
5717 {
5718 info_ptr += length;
5719 continue;
5720 }
dee91e82 5721
78d4d2c5
JK
5722 if (types_htab == NULL)
5723 {
5724 if (dwo_file)
5725 types_htab = allocate_dwo_unit_table (objfile);
5726 else
5727 types_htab = allocate_signatured_type_table (objfile);
5728 }
8b70b953 5729
78d4d2c5
JK
5730 if (dwo_file)
5731 {
5732 sig_type = NULL;
5733 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5734 struct dwo_unit);
5735 dwo_tu->dwo_file = dwo_file;
43988095 5736 dwo_tu->signature = header.signature;
9c541725 5737 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 5738 dwo_tu->section = section;
9c541725 5739 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
5740 dwo_tu->length = length;
5741 }
5742 else
5743 {
5744 /* N.B.: type_offset is not usable if this type uses a DWO file.
5745 The real type_offset is in the DWO file. */
5746 dwo_tu = NULL;
5747 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5748 struct signatured_type);
43988095 5749 sig_type->signature = header.signature;
9c541725 5750 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
5751 sig_type->per_cu.objfile = objfile;
5752 sig_type->per_cu.is_debug_types = 1;
5753 sig_type->per_cu.section = section;
9c541725 5754 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
5755 sig_type->per_cu.length = length;
5756 }
5757
5758 slot = htab_find_slot (types_htab,
5759 dwo_file ? (void*) dwo_tu : (void *) sig_type,
5760 INSERT);
5761 gdb_assert (slot != NULL);
5762 if (*slot != NULL)
5763 {
9c541725 5764 sect_offset dup_sect_off;
0349ea22 5765
3019eac3
DE
5766 if (dwo_file)
5767 {
78d4d2c5
JK
5768 const struct dwo_unit *dup_tu
5769 = (const struct dwo_unit *) *slot;
5770
9c541725 5771 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
5772 }
5773 else
5774 {
78d4d2c5
JK
5775 const struct signatured_type *dup_tu
5776 = (const struct signatured_type *) *slot;
5777
9c541725 5778 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 5779 }
8b70b953 5780
78d4d2c5
JK
5781 complaint (&symfile_complaints,
5782 _("debug type entry at offset 0x%x is duplicate to"
5783 " the entry at offset 0x%x, signature %s"),
9c541725 5784 to_underlying (sect_off), to_underlying (dup_sect_off),
43988095 5785 hex_string (header.signature));
78d4d2c5
JK
5786 }
5787 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 5788
78d4d2c5
JK
5789 if (dwarf_read_debug > 1)
5790 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
9c541725 5791 to_underlying (sect_off),
43988095 5792 hex_string (header.signature));
3019eac3 5793
78d4d2c5
JK
5794 info_ptr += length;
5795 }
5796}
3019eac3 5797
78d4d2c5
JK
5798/* Create the hash table of all entries in the .debug_types
5799 (or .debug_types.dwo) section(s).
5800 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
5801 otherwise it is NULL.
b3c8eb43 5802
78d4d2c5 5803 The result is a pointer to the hash table or NULL if there are no types.
348e048f 5804
78d4d2c5 5805 Note: This function processes DWO files only, not DWP files. */
348e048f 5806
78d4d2c5
JK
5807static void
5808create_debug_types_hash_table (struct dwo_file *dwo_file,
5809 VEC (dwarf2_section_info_def) *types,
5810 htab_t &types_htab)
5811{
5812 int ix;
5813 struct dwarf2_section_info *section;
5814
5815 if (VEC_empty (dwarf2_section_info_def, types))
5816 return;
348e048f 5817
78d4d2c5
JK
5818 for (ix = 0;
5819 VEC_iterate (dwarf2_section_info_def, types, ix, section);
5820 ++ix)
43988095
JK
5821 create_debug_type_hash_table (dwo_file, section, types_htab,
5822 rcuh_kind::TYPE);
3019eac3
DE
5823}
5824
5825/* Create the hash table of all entries in the .debug_types section,
5826 and initialize all_type_units.
5827 The result is zero if there is an error (e.g. missing .debug_types section),
5828 otherwise non-zero. */
5829
5830static int
5831create_all_type_units (struct objfile *objfile)
5832{
78d4d2c5 5833 htab_t types_htab = NULL;
b4dd5633 5834 struct signatured_type **iter;
3019eac3 5835
43988095
JK
5836 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
5837 rcuh_kind::COMPILE);
78d4d2c5 5838 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
3019eac3
DE
5839 if (types_htab == NULL)
5840 {
5841 dwarf2_per_objfile->signatured_types = NULL;
5842 return 0;
5843 }
5844
348e048f
DE
5845 dwarf2_per_objfile->signatured_types = types_htab;
5846
6aa5f3a6
DE
5847 dwarf2_per_objfile->n_type_units
5848 = dwarf2_per_objfile->n_allocated_type_units
5849 = htab_elements (types_htab);
8d749320
SM
5850 dwarf2_per_objfile->all_type_units =
5851 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
5852 iter = &dwarf2_per_objfile->all_type_units[0];
5853 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
5854 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
5855 == dwarf2_per_objfile->n_type_units);
1fd400ff 5856
348e048f
DE
5857 return 1;
5858}
5859
6aa5f3a6
DE
5860/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
5861 If SLOT is non-NULL, it is the entry to use in the hash table.
5862 Otherwise we find one. */
5863
5864static struct signatured_type *
5865add_type_unit (ULONGEST sig, void **slot)
5866{
5867 struct objfile *objfile = dwarf2_per_objfile->objfile;
5868 int n_type_units = dwarf2_per_objfile->n_type_units;
5869 struct signatured_type *sig_type;
5870
5871 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
5872 ++n_type_units;
5873 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
5874 {
5875 if (dwarf2_per_objfile->n_allocated_type_units == 0)
5876 dwarf2_per_objfile->n_allocated_type_units = 1;
5877 dwarf2_per_objfile->n_allocated_type_units *= 2;
5878 dwarf2_per_objfile->all_type_units
224c3ddb
SM
5879 = XRESIZEVEC (struct signatured_type *,
5880 dwarf2_per_objfile->all_type_units,
5881 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
5882 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5883 }
5884 dwarf2_per_objfile->n_type_units = n_type_units;
5885
5886 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5887 struct signatured_type);
5888 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5889 sig_type->signature = sig;
5890 sig_type->per_cu.is_debug_types = 1;
5891 if (dwarf2_per_objfile->using_index)
5892 {
5893 sig_type->per_cu.v.quick =
5894 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5895 struct dwarf2_per_cu_quick_data);
5896 }
5897
5898 if (slot == NULL)
5899 {
5900 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5901 sig_type, INSERT);
5902 }
5903 gdb_assert (*slot == NULL);
5904 *slot = sig_type;
5905 /* The rest of sig_type must be filled in by the caller. */
5906 return sig_type;
5907}
5908
a2ce51a0
DE
5909/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5910 Fill in SIG_ENTRY with DWO_ENTRY. */
5911
5912static void
5913fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5914 struct signatured_type *sig_entry,
5915 struct dwo_unit *dwo_entry)
5916{
7ee85ab1 5917 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
5918 gdb_assert (! sig_entry->per_cu.queued);
5919 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
5920 if (dwarf2_per_objfile->using_index)
5921 {
5922 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 5923 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
5924 }
5925 else
5926 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 5927 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 5928 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 5929 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
5930 gdb_assert (sig_entry->dwo_unit == NULL);
5931
5932 sig_entry->per_cu.section = dwo_entry->section;
9c541725 5933 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
5934 sig_entry->per_cu.length = dwo_entry->length;
5935 sig_entry->per_cu.reading_dwo_directly = 1;
5936 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
5937 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5938 sig_entry->dwo_unit = dwo_entry;
5939}
5940
5941/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
5942 If we haven't read the TU yet, create the signatured_type data structure
5943 for a TU to be read in directly from a DWO file, bypassing the stub.
5944 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5945 using .gdb_index, then when reading a CU we want to stay in the DWO file
5946 containing that CU. Otherwise we could end up reading several other DWO
5947 files (due to comdat folding) to process the transitive closure of all the
5948 mentioned TUs, and that can be slow. The current DWO file will have every
5949 type signature that it needs.
a2ce51a0
DE
5950 We only do this for .gdb_index because in the psymtab case we already have
5951 to read all the DWOs to build the type unit groups. */
5952
5953static struct signatured_type *
5954lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5955{
5956 struct objfile *objfile = dwarf2_per_objfile->objfile;
5957 struct dwo_file *dwo_file;
5958 struct dwo_unit find_dwo_entry, *dwo_entry;
5959 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5960 void **slot;
a2ce51a0
DE
5961
5962 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5963
6aa5f3a6
DE
5964 /* If TU skeletons have been removed then we may not have read in any
5965 TUs yet. */
5966 if (dwarf2_per_objfile->signatured_types == NULL)
5967 {
5968 dwarf2_per_objfile->signatured_types
5969 = allocate_signatured_type_table (objfile);
5970 }
a2ce51a0
DE
5971
5972 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
5973 Use the global signatured_types array to do our own comdat-folding
5974 of types. If this is the first time we're reading this TU, and
5975 the TU has an entry in .gdb_index, replace the recorded data from
5976 .gdb_index with this TU. */
a2ce51a0 5977
a2ce51a0 5978 find_sig_entry.signature = sig;
6aa5f3a6
DE
5979 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5980 &find_sig_entry, INSERT);
9a3c8263 5981 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
5982
5983 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
5984 read. Don't reassign the global entry to point to this DWO if that's
5985 the case. Also note that if the TU is already being read, it may not
5986 have come from a DWO, the program may be a mix of Fission-compiled
5987 code and non-Fission-compiled code. */
5988
5989 /* Have we already tried to read this TU?
5990 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5991 needn't exist in the global table yet). */
5992 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
5993 return sig_entry;
5994
6aa5f3a6
DE
5995 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5996 dwo_unit of the TU itself. */
5997 dwo_file = cu->dwo_unit->dwo_file;
5998
a2ce51a0
DE
5999 /* Ok, this is the first time we're reading this TU. */
6000 if (dwo_file->tus == NULL)
6001 return NULL;
6002 find_dwo_entry.signature = sig;
9a3c8263 6003 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
6004 if (dwo_entry == NULL)
6005 return NULL;
6006
6aa5f3a6
DE
6007 /* If the global table doesn't have an entry for this TU, add one. */
6008 if (sig_entry == NULL)
6009 sig_entry = add_type_unit (sig, slot);
6010
a2ce51a0 6011 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 6012 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6013 return sig_entry;
6014}
6015
a2ce51a0
DE
6016/* Subroutine of lookup_signatured_type.
6017 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6018 then try the DWP file. If the TU stub (skeleton) has been removed then
6019 it won't be in .gdb_index. */
a2ce51a0
DE
6020
6021static struct signatured_type *
6022lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6023{
6024 struct objfile *objfile = dwarf2_per_objfile->objfile;
6025 struct dwp_file *dwp_file = get_dwp_file ();
6026 struct dwo_unit *dwo_entry;
6027 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6028 void **slot;
a2ce51a0
DE
6029
6030 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6031 gdb_assert (dwp_file != NULL);
6032
6aa5f3a6
DE
6033 /* If TU skeletons have been removed then we may not have read in any
6034 TUs yet. */
6035 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 6036 {
6aa5f3a6
DE
6037 dwarf2_per_objfile->signatured_types
6038 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
6039 }
6040
6aa5f3a6
DE
6041 find_sig_entry.signature = sig;
6042 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6043 &find_sig_entry, INSERT);
9a3c8263 6044 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6045
6046 /* Have we already tried to read this TU?
6047 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6048 needn't exist in the global table yet). */
6049 if (sig_entry != NULL)
6050 return sig_entry;
6051
a2ce51a0
DE
6052 if (dwp_file->tus == NULL)
6053 return NULL;
57d63ce2
DE
6054 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
6055 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6056 if (dwo_entry == NULL)
6057 return NULL;
6058
6aa5f3a6 6059 sig_entry = add_type_unit (sig, slot);
a2ce51a0
DE
6060 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
6061
a2ce51a0
DE
6062 return sig_entry;
6063}
6064
380bca97 6065/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6066 Returns NULL if signature SIG is not present in the table.
6067 It is up to the caller to complain about this. */
348e048f
DE
6068
6069static struct signatured_type *
a2ce51a0 6070lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6071{
a2ce51a0
DE
6072 if (cu->dwo_unit
6073 && dwarf2_per_objfile->using_index)
6074 {
6075 /* We're in a DWO/DWP file, and we're using .gdb_index.
6076 These cases require special processing. */
6077 if (get_dwp_file () == NULL)
6078 return lookup_dwo_signatured_type (cu, sig);
6079 else
6080 return lookup_dwp_signatured_type (cu, sig);
6081 }
6082 else
6083 {
6084 struct signatured_type find_entry, *entry;
348e048f 6085
a2ce51a0
DE
6086 if (dwarf2_per_objfile->signatured_types == NULL)
6087 return NULL;
6088 find_entry.signature = sig;
9a3c8263
SM
6089 entry = ((struct signatured_type *)
6090 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
6091 return entry;
6092 }
348e048f 6093}
42e7ad6c
DE
6094\f
6095/* Low level DIE reading support. */
348e048f 6096
d85a05f0
DJ
6097/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6098
6099static void
6100init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6101 struct dwarf2_cu *cu,
3019eac3
DE
6102 struct dwarf2_section_info *section,
6103 struct dwo_file *dwo_file)
d85a05f0 6104{
fceca515 6105 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 6106 reader->abfd = get_section_bfd_owner (section);
d85a05f0 6107 reader->cu = cu;
3019eac3 6108 reader->dwo_file = dwo_file;
dee91e82
DE
6109 reader->die_section = section;
6110 reader->buffer = section->buffer;
f664829e 6111 reader->buffer_end = section->buffer + section->size;
a2ce51a0 6112 reader->comp_dir = NULL;
d85a05f0
DJ
6113}
6114
b0c7bfa9
DE
6115/* Subroutine of init_cutu_and_read_dies to simplify it.
6116 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6117 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
6118 already.
6119
6120 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6121 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6122 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6123 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6124 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6125 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
6126 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
6127 are filled in with the info of the DIE from the DWO file.
6128 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
6129 provided an abbrev table to use.
6130 The result is non-zero if a valid (non-dummy) DIE was found. */
6131
6132static int
6133read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6134 struct dwo_unit *dwo_unit,
6135 int abbrev_table_provided,
6136 struct die_info *stub_comp_unit_die,
a2ce51a0 6137 const char *stub_comp_dir,
b0c7bfa9 6138 struct die_reader_specs *result_reader,
d521ce57 6139 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
6140 struct die_info **result_comp_unit_die,
6141 int *result_has_children)
6142{
6143 struct objfile *objfile = dwarf2_per_objfile->objfile;
6144 struct dwarf2_cu *cu = this_cu->cu;
6145 struct dwarf2_section_info *section;
6146 bfd *abfd;
d521ce57 6147 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6148 ULONGEST signature; /* Or dwo_id. */
6149 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6150 int i,num_extra_attrs;
6151 struct dwarf2_section_info *dwo_abbrev_section;
6152 struct attribute *attr;
6153 struct die_info *comp_unit_die;
6154
b0aeadb3
DE
6155 /* At most one of these may be provided. */
6156 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6157
b0c7bfa9
DE
6158 /* These attributes aren't processed until later:
6159 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6160 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6161 referenced later. However, these attributes are found in the stub
6162 which we won't have later. In order to not impose this complication
6163 on the rest of the code, we read them here and copy them to the
6164 DWO CU/TU die. */
b0c7bfa9
DE
6165
6166 stmt_list = NULL;
6167 low_pc = NULL;
6168 high_pc = NULL;
6169 ranges = NULL;
6170 comp_dir = NULL;
6171
6172 if (stub_comp_unit_die != NULL)
6173 {
6174 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6175 DWO file. */
6176 if (! this_cu->is_debug_types)
6177 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6178 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6179 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6180 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6181 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6182
6183 /* There should be a DW_AT_addr_base attribute here (if needed).
6184 We need the value before we can process DW_FORM_GNU_addr_index. */
6185 cu->addr_base = 0;
6186 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
6187 if (attr)
6188 cu->addr_base = DW_UNSND (attr);
6189
6190 /* There should be a DW_AT_ranges_base attribute here (if needed).
6191 We need the value before we can process DW_AT_ranges. */
6192 cu->ranges_base = 0;
6193 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
6194 if (attr)
6195 cu->ranges_base = DW_UNSND (attr);
6196 }
a2ce51a0
DE
6197 else if (stub_comp_dir != NULL)
6198 {
6199 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6200 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6201 comp_dir->name = DW_AT_comp_dir;
6202 comp_dir->form = DW_FORM_string;
6203 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6204 DW_STRING (comp_dir) = stub_comp_dir;
6205 }
b0c7bfa9
DE
6206
6207 /* Set up for reading the DWO CU/TU. */
6208 cu->dwo_unit = dwo_unit;
6209 section = dwo_unit->section;
6210 dwarf2_read_section (objfile, section);
a32a8923 6211 abfd = get_section_bfd_owner (section);
9c541725
PA
6212 begin_info_ptr = info_ptr = (section->buffer
6213 + to_underlying (dwo_unit->sect_off));
b0c7bfa9
DE
6214 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6215 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
6216
6217 if (this_cu->is_debug_types)
6218 {
b0c7bfa9
DE
6219 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6220
43988095 6221 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
b0c7bfa9 6222 dwo_abbrev_section,
43988095 6223 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6224 /* This is not an assert because it can be caused by bad debug info. */
43988095 6225 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6226 {
6227 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6228 " TU at offset 0x%x [in module %s]"),
6229 hex_string (sig_type->signature),
43988095 6230 hex_string (cu->header.signature),
9c541725 6231 to_underlying (dwo_unit->sect_off),
a2ce51a0
DE
6232 bfd_get_filename (abfd));
6233 }
9c541725 6234 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6235 /* For DWOs coming from DWP files, we don't know the CU length
6236 nor the type's offset in the TU until now. */
6237 dwo_unit->length = get_cu_length (&cu->header);
9c541725 6238 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6239
6240 /* Establish the type offset that can be used to lookup the type.
6241 For DWO files, we don't know it until now. */
9c541725
PA
6242 sig_type->type_offset_in_section
6243 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6244 }
6245 else
6246 {
6247 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6248 dwo_abbrev_section,
43988095 6249 info_ptr, rcuh_kind::COMPILE);
9c541725 6250 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6251 /* For DWOs coming from DWP files, we don't know the CU length
6252 until now. */
6253 dwo_unit->length = get_cu_length (&cu->header);
6254 }
6255
02142a6c
DE
6256 /* Replace the CU's original abbrev table with the DWO's.
6257 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
6258 if (abbrev_table_provided)
6259 {
6260 /* Don't free the provided abbrev table, the caller of
6261 init_cutu_and_read_dies owns it. */
6262 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 6263 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
6264 make_cleanup (dwarf2_free_abbrev_table, cu);
6265 }
6266 else
6267 {
6268 dwarf2_free_abbrev_table (cu);
6269 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 6270 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
6271 }
6272
6273 /* Read in the die, but leave space to copy over the attributes
6274 from the stub. This has the benefit of simplifying the rest of
6275 the code - all the work to maintain the illusion of a single
6276 DW_TAG_{compile,type}_unit DIE is done here. */
6277 num_extra_attrs = ((stmt_list != NULL)
6278 + (low_pc != NULL)
6279 + (high_pc != NULL)
6280 + (ranges != NULL)
6281 + (comp_dir != NULL));
6282 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6283 result_has_children, num_extra_attrs);
6284
6285 /* Copy over the attributes from the stub to the DIE we just read in. */
6286 comp_unit_die = *result_comp_unit_die;
6287 i = comp_unit_die->num_attrs;
6288 if (stmt_list != NULL)
6289 comp_unit_die->attrs[i++] = *stmt_list;
6290 if (low_pc != NULL)
6291 comp_unit_die->attrs[i++] = *low_pc;
6292 if (high_pc != NULL)
6293 comp_unit_die->attrs[i++] = *high_pc;
6294 if (ranges != NULL)
6295 comp_unit_die->attrs[i++] = *ranges;
6296 if (comp_dir != NULL)
6297 comp_unit_die->attrs[i++] = *comp_dir;
6298 comp_unit_die->num_attrs += num_extra_attrs;
6299
b4f54984 6300 if (dwarf_die_debug)
bf6af496
DE
6301 {
6302 fprintf_unfiltered (gdb_stdlog,
6303 "Read die from %s@0x%x of %s:\n",
a32a8923 6304 get_section_name (section),
bf6af496
DE
6305 (unsigned) (begin_info_ptr - section->buffer),
6306 bfd_get_filename (abfd));
b4f54984 6307 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6308 }
6309
a2ce51a0
DE
6310 /* Save the comp_dir attribute. If there is no DWP file then we'll read
6311 TUs by skipping the stub and going directly to the entry in the DWO file.
6312 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
6313 to get it via circuitous means. Blech. */
6314 if (comp_dir != NULL)
6315 result_reader->comp_dir = DW_STRING (comp_dir);
6316
b0c7bfa9
DE
6317 /* Skip dummy compilation units. */
6318 if (info_ptr >= begin_info_ptr + dwo_unit->length
6319 || peek_abbrev_code (abfd, info_ptr) == 0)
6320 return 0;
6321
6322 *result_info_ptr = info_ptr;
6323 return 1;
6324}
6325
6326/* Subroutine of init_cutu_and_read_dies to simplify it.
6327 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6328 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6329
6330static struct dwo_unit *
6331lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6332 struct die_info *comp_unit_die)
6333{
6334 struct dwarf2_cu *cu = this_cu->cu;
6335 struct attribute *attr;
6336 ULONGEST signature;
6337 struct dwo_unit *dwo_unit;
6338 const char *comp_dir, *dwo_name;
6339
a2ce51a0
DE
6340 gdb_assert (cu != NULL);
6341
b0c7bfa9 6342 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
6343 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
6344 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6345
6346 if (this_cu->is_debug_types)
6347 {
6348 struct signatured_type *sig_type;
6349
6350 /* Since this_cu is the first member of struct signatured_type,
6351 we can go from a pointer to one to a pointer to the other. */
6352 sig_type = (struct signatured_type *) this_cu;
6353 signature = sig_type->signature;
6354 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6355 }
6356 else
6357 {
6358 struct attribute *attr;
6359
6360 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6361 if (! attr)
6362 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6363 " [in module %s]"),
4262abfb 6364 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
6365 signature = DW_UNSND (attr);
6366 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6367 signature);
6368 }
6369
b0c7bfa9
DE
6370 return dwo_unit;
6371}
6372
a2ce51a0 6373/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6
DE
6374 See it for a description of the parameters.
6375 Read a TU directly from a DWO file, bypassing the stub.
6376
6377 Note: This function could be a little bit simpler if we shared cleanups
6378 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
6379 to do, so we keep this function self-contained. Or we could move this
6380 into our caller, but it's complex enough already. */
a2ce51a0
DE
6381
6382static void
6aa5f3a6
DE
6383init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6384 int use_existing_cu, int keep,
a2ce51a0
DE
6385 die_reader_func_ftype *die_reader_func,
6386 void *data)
6387{
6388 struct dwarf2_cu *cu;
6389 struct signatured_type *sig_type;
6aa5f3a6 6390 struct cleanup *cleanups, *free_cu_cleanup = NULL;
a2ce51a0
DE
6391 struct die_reader_specs reader;
6392 const gdb_byte *info_ptr;
6393 struct die_info *comp_unit_die;
6394 int has_children;
6395
6396 /* Verify we can do the following downcast, and that we have the
6397 data we need. */
6398 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6399 sig_type = (struct signatured_type *) this_cu;
6400 gdb_assert (sig_type->dwo_unit != NULL);
6401
6402 cleanups = make_cleanup (null_cleanup, NULL);
6403
6aa5f3a6
DE
6404 if (use_existing_cu && this_cu->cu != NULL)
6405 {
6406 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6407 cu = this_cu->cu;
6408 /* There's no need to do the rereading_dwo_cu handling that
6409 init_cutu_and_read_dies does since we don't read the stub. */
6410 }
6411 else
6412 {
6413 /* If !use_existing_cu, this_cu->cu must be NULL. */
6414 gdb_assert (this_cu->cu == NULL);
8d749320 6415 cu = XNEW (struct dwarf2_cu);
6aa5f3a6
DE
6416 init_one_comp_unit (cu, this_cu);
6417 /* If an error occurs while loading, release our storage. */
6418 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
6419 }
6420
6421 /* A future optimization, if needed, would be to use an existing
6422 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6423 could share abbrev tables. */
a2ce51a0
DE
6424
6425 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6426 0 /* abbrev_table_provided */,
6427 NULL /* stub_comp_unit_die */,
6428 sig_type->dwo_unit->dwo_file->comp_dir,
6429 &reader, &info_ptr,
6430 &comp_unit_die, &has_children) == 0)
6431 {
6432 /* Dummy die. */
6433 do_cleanups (cleanups);
6434 return;
6435 }
6436
6437 /* All the "real" work is done here. */
6438 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6439
6aa5f3a6 6440 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
6441 but the alternative is making the latter more complex.
6442 This function is only for the special case of using DWO files directly:
6443 no point in overly complicating the general case just to handle this. */
6aa5f3a6 6444 if (free_cu_cleanup != NULL)
a2ce51a0 6445 {
6aa5f3a6
DE
6446 if (keep)
6447 {
6448 /* We've successfully allocated this compilation unit. Let our
6449 caller clean it up when finished with it. */
6450 discard_cleanups (free_cu_cleanup);
a2ce51a0 6451
6aa5f3a6
DE
6452 /* We can only discard free_cu_cleanup and all subsequent cleanups.
6453 So we have to manually free the abbrev table. */
6454 dwarf2_free_abbrev_table (cu);
a2ce51a0 6455
6aa5f3a6
DE
6456 /* Link this CU into read_in_chain. */
6457 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6458 dwarf2_per_objfile->read_in_chain = this_cu;
6459 }
6460 else
6461 do_cleanups (free_cu_cleanup);
a2ce51a0 6462 }
a2ce51a0
DE
6463
6464 do_cleanups (cleanups);
6465}
6466
fd820528 6467/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6468 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6469
f4dc4d17
DE
6470 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6471 Otherwise the table specified in the comp unit header is read in and used.
6472 This is an optimization for when we already have the abbrev table.
6473
dee91e82
DE
6474 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6475 Otherwise, a new CU is allocated with xmalloc.
6476
6477 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
6478 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
6479
6480 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 6481 linker) then DIE_READER_FUNC will not get called. */
aaa75496 6482
70221824 6483static void
fd820528 6484init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 6485 struct abbrev_table *abbrev_table,
fd820528
DE
6486 int use_existing_cu, int keep,
6487 die_reader_func_ftype *die_reader_func,
6488 void *data)
c906108c 6489{
dee91e82 6490 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6491 struct dwarf2_section_info *section = this_cu->section;
a32a8923 6492 bfd *abfd = get_section_bfd_owner (section);
dee91e82 6493 struct dwarf2_cu *cu;
d521ce57 6494 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 6495 struct die_reader_specs reader;
d85a05f0 6496 struct die_info *comp_unit_die;
dee91e82 6497 int has_children;
d85a05f0 6498 struct attribute *attr;
365156ad 6499 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 6500 struct signatured_type *sig_type = NULL;
4bdcc0c1 6501 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6502 /* Non-zero if CU currently points to a DWO file and we need to
6503 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6504 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6505 int rereading_dwo_cu = 0;
c906108c 6506
b4f54984 6507 if (dwarf_die_debug)
09406207
DE
6508 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
6509 this_cu->is_debug_types ? "type" : "comp",
9c541725 6510 to_underlying (this_cu->sect_off));
09406207 6511
dee91e82
DE
6512 if (use_existing_cu)
6513 gdb_assert (keep);
23745b47 6514
a2ce51a0
DE
6515 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6516 file (instead of going through the stub), short-circuit all of this. */
6517 if (this_cu->reading_dwo_directly)
6518 {
6519 /* Narrow down the scope of possibilities to have to understand. */
6520 gdb_assert (this_cu->is_debug_types);
6521 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
6522 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
6523 die_reader_func, data);
a2ce51a0
DE
6524 return;
6525 }
6526
dee91e82
DE
6527 cleanups = make_cleanup (null_cleanup, NULL);
6528
6529 /* This is cheap if the section is already read in. */
6530 dwarf2_read_section (objfile, section);
6531
9c541725 6532 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6533
6534 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6535
6536 if (use_existing_cu && this_cu->cu != NULL)
6537 {
6538 cu = this_cu->cu;
42e7ad6c
DE
6539 /* If this CU is from a DWO file we need to start over, we need to
6540 refetch the attributes from the skeleton CU.
6541 This could be optimized by retrieving those attributes from when we
6542 were here the first time: the previous comp_unit_die was stored in
6543 comp_unit_obstack. But there's no data yet that we need this
6544 optimization. */
6545 if (cu->dwo_unit != NULL)
6546 rereading_dwo_cu = 1;
dee91e82
DE
6547 }
6548 else
6549 {
6550 /* If !use_existing_cu, this_cu->cu must be NULL. */
6551 gdb_assert (this_cu->cu == NULL);
8d749320 6552 cu = XNEW (struct dwarf2_cu);
dee91e82 6553 init_one_comp_unit (cu, this_cu);
dee91e82 6554 /* If an error occurs while loading, release our storage. */
365156ad 6555 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 6556 }
dee91e82 6557
b0c7bfa9 6558 /* Get the header. */
9c541725 6559 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6560 {
6561 /* We already have the header, there's no need to read it in again. */
9c541725 6562 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6563 }
6564 else
6565 {
3019eac3 6566 if (this_cu->is_debug_types)
dee91e82 6567 {
43988095 6568 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4bdcc0c1 6569 abbrev_section, info_ptr,
43988095 6570 rcuh_kind::TYPE);
dee91e82 6571
42e7ad6c
DE
6572 /* Since per_cu is the first member of struct signatured_type,
6573 we can go from a pointer to one to a pointer to the other. */
6574 sig_type = (struct signatured_type *) this_cu;
43988095 6575 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6576 gdb_assert (sig_type->type_offset_in_tu
6577 == cu->header.type_cu_offset_in_tu);
6578 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6579
42e7ad6c
DE
6580 /* LENGTH has not been set yet for type units if we're
6581 using .gdb_index. */
1ce1cefd 6582 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
6583
6584 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6585 sig_type->type_offset_in_section =
6586 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6587
6588 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6589 }
6590 else
6591 {
4bdcc0c1
DE
6592 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6593 abbrev_section,
43988095
JK
6594 info_ptr,
6595 rcuh_kind::COMPILE);
dee91e82 6596
9c541725 6597 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 6598 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 6599 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6600 }
6601 }
10b3939b 6602
6caca83c 6603 /* Skip dummy compilation units. */
dee91e82 6604 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
6605 || peek_abbrev_code (abfd, info_ptr) == 0)
6606 {
dee91e82 6607 do_cleanups (cleanups);
21b2bd31 6608 return;
6caca83c
CC
6609 }
6610
433df2d4
DE
6611 /* If we don't have them yet, read the abbrevs for this compilation unit.
6612 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
6613 done. Note that it's important that if the CU had an abbrev table
6614 on entry we don't free it when we're done: Somewhere up the call stack
6615 it may be in use. */
f4dc4d17
DE
6616 if (abbrev_table != NULL)
6617 {
6618 gdb_assert (cu->abbrev_table == NULL);
9c541725 6619 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
f4dc4d17
DE
6620 cu->abbrev_table = abbrev_table;
6621 }
6622 else if (cu->abbrev_table == NULL)
dee91e82 6623 {
4bdcc0c1 6624 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
6625 make_cleanup (dwarf2_free_abbrev_table, cu);
6626 }
42e7ad6c
DE
6627 else if (rereading_dwo_cu)
6628 {
6629 dwarf2_free_abbrev_table (cu);
6630 dwarf2_read_abbrevs (cu, abbrev_section);
6631 }
af703f96 6632
dee91e82 6633 /* Read the top level CU/TU die. */
3019eac3 6634 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 6635 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 6636
b0c7bfa9
DE
6637 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6638 from the DWO file.
6639 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6640 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
6641 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
6642 if (attr)
6643 {
3019eac3 6644 struct dwo_unit *dwo_unit;
b0c7bfa9 6645 struct die_info *dwo_comp_unit_die;
3019eac3
DE
6646
6647 if (has_children)
6a506a2d
DE
6648 {
6649 complaint (&symfile_complaints,
6650 _("compilation unit with DW_AT_GNU_dwo_name"
6651 " has children (offset 0x%x) [in module %s]"),
9c541725 6652 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6a506a2d 6653 }
b0c7bfa9 6654 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 6655 if (dwo_unit != NULL)
3019eac3 6656 {
6a506a2d
DE
6657 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6658 abbrev_table != NULL,
a2ce51a0 6659 comp_unit_die, NULL,
6a506a2d
DE
6660 &reader, &info_ptr,
6661 &dwo_comp_unit_die, &has_children) == 0)
6662 {
6663 /* Dummy die. */
6664 do_cleanups (cleanups);
6665 return;
6666 }
6667 comp_unit_die = dwo_comp_unit_die;
6668 }
6669 else
6670 {
6671 /* Yikes, we couldn't find the rest of the DIE, we only have
6672 the stub. A complaint has already been logged. There's
6673 not much more we can do except pass on the stub DIE to
6674 die_reader_func. We don't want to throw an error on bad
6675 debug info. */
3019eac3
DE
6676 }
6677 }
6678
b0c7bfa9 6679 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
6680 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6681
b0c7bfa9 6682 /* Done, clean up. */
365156ad 6683 if (free_cu_cleanup != NULL)
348e048f 6684 {
365156ad
TT
6685 if (keep)
6686 {
6687 /* We've successfully allocated this compilation unit. Let our
6688 caller clean it up when finished with it. */
6689 discard_cleanups (free_cu_cleanup);
dee91e82 6690
365156ad
TT
6691 /* We can only discard free_cu_cleanup and all subsequent cleanups.
6692 So we have to manually free the abbrev table. */
6693 dwarf2_free_abbrev_table (cu);
dee91e82 6694
365156ad
TT
6695 /* Link this CU into read_in_chain. */
6696 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6697 dwarf2_per_objfile->read_in_chain = this_cu;
6698 }
6699 else
6700 do_cleanups (free_cu_cleanup);
348e048f 6701 }
365156ad
TT
6702
6703 do_cleanups (cleanups);
dee91e82
DE
6704}
6705
33e80786
DE
6706/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
6707 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
6708 to have already done the lookup to find the DWO file).
dee91e82
DE
6709
6710 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 6711 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
6712
6713 We fill in THIS_CU->length.
6714
6715 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
6716 linker) then DIE_READER_FUNC will not get called.
6717
6718 THIS_CU->cu is always freed when done.
3019eac3
DE
6719 This is done in order to not leave THIS_CU->cu in a state where we have
6720 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
6721
6722static void
6723init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 6724 struct dwo_file *dwo_file,
dee91e82
DE
6725 die_reader_func_ftype *die_reader_func,
6726 void *data)
6727{
6728 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6729 struct dwarf2_section_info *section = this_cu->section;
a32a8923 6730 bfd *abfd = get_section_bfd_owner (section);
33e80786 6731 struct dwarf2_section_info *abbrev_section;
dee91e82 6732 struct dwarf2_cu cu;
d521ce57 6733 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
6734 struct die_reader_specs reader;
6735 struct cleanup *cleanups;
6736 struct die_info *comp_unit_die;
6737 int has_children;
6738
b4f54984 6739 if (dwarf_die_debug)
09406207
DE
6740 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
6741 this_cu->is_debug_types ? "type" : "comp",
9c541725 6742 to_underlying (this_cu->sect_off));
09406207 6743
dee91e82
DE
6744 gdb_assert (this_cu->cu == NULL);
6745
33e80786
DE
6746 abbrev_section = (dwo_file != NULL
6747 ? &dwo_file->sections.abbrev
6748 : get_abbrev_section_for_cu (this_cu));
6749
dee91e82
DE
6750 /* This is cheap if the section is already read in. */
6751 dwarf2_read_section (objfile, section);
6752
6753 init_one_comp_unit (&cu, this_cu);
6754
6755 cleanups = make_cleanup (free_stack_comp_unit, &cu);
6756
9c541725 6757 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4bdcc0c1
DE
6758 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
6759 abbrev_section, info_ptr,
43988095
JK
6760 (this_cu->is_debug_types
6761 ? rcuh_kind::TYPE
6762 : rcuh_kind::COMPILE));
dee91e82 6763
1ce1cefd 6764 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
6765
6766 /* Skip dummy compilation units. */
6767 if (info_ptr >= begin_info_ptr + this_cu->length
6768 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 6769 {
dee91e82 6770 do_cleanups (cleanups);
21b2bd31 6771 return;
93311388 6772 }
72bf9492 6773
dee91e82
DE
6774 dwarf2_read_abbrevs (&cu, abbrev_section);
6775 make_cleanup (dwarf2_free_abbrev_table, &cu);
6776
3019eac3 6777 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
6778 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
6779
6780 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6781
6782 do_cleanups (cleanups);
6783}
6784
3019eac3
DE
6785/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
6786 does not lookup the specified DWO file.
6787 This cannot be used to read DWO files.
dee91e82
DE
6788
6789 THIS_CU->cu is always freed when done.
3019eac3
DE
6790 This is done in order to not leave THIS_CU->cu in a state where we have
6791 to care whether it refers to the "main" CU or the DWO CU.
6792 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
6793
6794static void
6795init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
6796 die_reader_func_ftype *die_reader_func,
6797 void *data)
6798{
33e80786 6799 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 6800}
0018ea6f
DE
6801\f
6802/* Type Unit Groups.
dee91e82 6803
0018ea6f
DE
6804 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6805 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6806 so that all types coming from the same compilation (.o file) are grouped
6807 together. A future step could be to put the types in the same symtab as
6808 the CU the types ultimately came from. */
ff013f42 6809
f4dc4d17
DE
6810static hashval_t
6811hash_type_unit_group (const void *item)
6812{
9a3c8263
SM
6813 const struct type_unit_group *tu_group
6814 = (const struct type_unit_group *) item;
f4dc4d17 6815
094b34ac 6816 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 6817}
348e048f
DE
6818
6819static int
f4dc4d17 6820eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 6821{
9a3c8263
SM
6822 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6823 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 6824
094b34ac 6825 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 6826}
348e048f 6827
f4dc4d17
DE
6828/* Allocate a hash table for type unit groups. */
6829
6830static htab_t
6831allocate_type_unit_groups_table (void)
6832{
6833 return htab_create_alloc_ex (3,
6834 hash_type_unit_group,
6835 eq_type_unit_group,
6836 NULL,
6837 &dwarf2_per_objfile->objfile->objfile_obstack,
6838 hashtab_obstack_allocate,
6839 dummy_obstack_deallocate);
6840}
dee91e82 6841
f4dc4d17
DE
6842/* Type units that don't have DW_AT_stmt_list are grouped into their own
6843 partial symtabs. We combine several TUs per psymtab to not let the size
6844 of any one psymtab grow too big. */
6845#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6846#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 6847
094b34ac 6848/* Helper routine for get_type_unit_group.
f4dc4d17
DE
6849 Create the type_unit_group object used to hold one or more TUs. */
6850
6851static struct type_unit_group *
094b34ac 6852create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
6853{
6854 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 6855 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 6856 struct type_unit_group *tu_group;
f4dc4d17
DE
6857
6858 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6859 struct type_unit_group);
094b34ac 6860 per_cu = &tu_group->per_cu;
f4dc4d17 6861 per_cu->objfile = objfile;
f4dc4d17 6862
094b34ac
DE
6863 if (dwarf2_per_objfile->using_index)
6864 {
6865 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6866 struct dwarf2_per_cu_quick_data);
094b34ac
DE
6867 }
6868 else
6869 {
9c541725 6870 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
6871 struct partial_symtab *pst;
6872 char *name;
6873
6874 /* Give the symtab a useful name for debug purposes. */
6875 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6876 name = xstrprintf ("<type_units_%d>",
6877 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6878 else
6879 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
6880
6881 pst = create_partial_symtab (per_cu, name);
6882 pst->anonymous = 1;
f4dc4d17 6883
094b34ac
DE
6884 xfree (name);
6885 }
f4dc4d17 6886
094b34ac 6887 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 6888 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
6889
6890 return tu_group;
6891}
6892
094b34ac
DE
6893/* Look up the type_unit_group for type unit CU, and create it if necessary.
6894 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
6895
6896static struct type_unit_group *
ff39bb5e 6897get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
6898{
6899 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6900 struct type_unit_group *tu_group;
6901 void **slot;
6902 unsigned int line_offset;
6903 struct type_unit_group type_unit_group_for_lookup;
6904
6905 if (dwarf2_per_objfile->type_unit_groups == NULL)
6906 {
6907 dwarf2_per_objfile->type_unit_groups =
6908 allocate_type_unit_groups_table ();
6909 }
6910
6911 /* Do we need to create a new group, or can we use an existing one? */
6912
6913 if (stmt_list)
6914 {
6915 line_offset = DW_UNSND (stmt_list);
6916 ++tu_stats->nr_symtab_sharers;
6917 }
6918 else
6919 {
6920 /* Ugh, no stmt_list. Rare, but we have to handle it.
6921 We can do various things here like create one group per TU or
6922 spread them over multiple groups to split up the expansion work.
6923 To avoid worst case scenarios (too many groups or too large groups)
6924 we, umm, group them in bunches. */
6925 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6926 | (tu_stats->nr_stmt_less_type_units
6927 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6928 ++tu_stats->nr_stmt_less_type_units;
6929 }
6930
094b34ac 6931 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 6932 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
6933 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6934 &type_unit_group_for_lookup, INSERT);
6935 if (*slot != NULL)
6936 {
9a3c8263 6937 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
6938 gdb_assert (tu_group != NULL);
6939 }
6940 else
6941 {
9c541725 6942 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 6943 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
6944 *slot = tu_group;
6945 ++tu_stats->nr_symtabs;
6946 }
6947
6948 return tu_group;
6949}
0018ea6f
DE
6950\f
6951/* Partial symbol tables. */
6952
6953/* Create a psymtab named NAME and assign it to PER_CU.
6954
6955 The caller must fill in the following details:
6956 dirname, textlow, texthigh. */
6957
6958static struct partial_symtab *
6959create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
6960{
6961 struct objfile *objfile = per_cu->objfile;
6962 struct partial_symtab *pst;
6963
18a94d75 6964 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
6965 objfile->global_psymbols,
6966 objfile->static_psymbols);
0018ea6f
DE
6967
6968 pst->psymtabs_addrmap_supported = 1;
6969
6970 /* This is the glue that links PST into GDB's symbol API. */
6971 pst->read_symtab_private = per_cu;
6972 pst->read_symtab = dwarf2_read_symtab;
6973 per_cu->v.psymtab = pst;
6974
6975 return pst;
6976}
6977
b93601f3
TT
6978/* The DATA object passed to process_psymtab_comp_unit_reader has this
6979 type. */
6980
6981struct process_psymtab_comp_unit_data
6982{
6983 /* True if we are reading a DW_TAG_partial_unit. */
6984
6985 int want_partial_unit;
6986
6987 /* The "pretend" language that is used if the CU doesn't declare a
6988 language. */
6989
6990 enum language pretend_language;
6991};
6992
0018ea6f
DE
6993/* die_reader_func for process_psymtab_comp_unit. */
6994
6995static void
6996process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6997 const gdb_byte *info_ptr,
0018ea6f
DE
6998 struct die_info *comp_unit_die,
6999 int has_children,
7000 void *data)
7001{
7002 struct dwarf2_cu *cu = reader->cu;
7003 struct objfile *objfile = cu->objfile;
3e29f34a 7004 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7005 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7006 CORE_ADDR baseaddr;
7007 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7008 struct partial_symtab *pst;
3a2b436a 7009 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7010 const char *filename;
9a3c8263
SM
7011 struct process_psymtab_comp_unit_data *info
7012 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 7013
b93601f3 7014 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
7015 return;
7016
7017 gdb_assert (! per_cu->is_debug_types);
7018
b93601f3 7019 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
7020
7021 cu->list_in_scope = &file_symbols;
7022
7023 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
7024 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7025 if (filename == NULL)
0018ea6f 7026 filename = "";
0018ea6f
DE
7027
7028 pst = create_partial_symtab (per_cu, filename);
7029
7030 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7031 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
7032
7033 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7034
7035 dwarf2_find_base_address (comp_unit_die, cu);
7036
7037 /* Possibly set the default values of LOWPC and HIGHPC from
7038 `DW_AT_ranges'. */
3a2b436a
JK
7039 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7040 &best_highpc, cu, pst);
7041 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
7042 /* Store the contiguous range if it is not empty; it can be empty for
7043 CUs with no code. */
7044 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
7045 gdbarch_adjust_dwarf2_addr (gdbarch,
7046 best_lowpc + baseaddr),
7047 gdbarch_adjust_dwarf2_addr (gdbarch,
7048 best_highpc + baseaddr) - 1,
7049 pst);
0018ea6f
DE
7050
7051 /* Check if comp unit has_children.
7052 If so, read the rest of the partial symbols from this comp unit.
7053 If not, there's no more debug_info for this comp unit. */
7054 if (has_children)
7055 {
7056 struct partial_die_info *first_die;
7057 CORE_ADDR lowpc, highpc;
7058
7059 lowpc = ((CORE_ADDR) -1);
7060 highpc = ((CORE_ADDR) 0);
7061
7062 first_die = load_partial_dies (reader, info_ptr, 1);
7063
7064 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7065 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7066
7067 /* If we didn't find a lowpc, set it to highpc to avoid
7068 complaints from `maint check'. */
7069 if (lowpc == ((CORE_ADDR) -1))
7070 lowpc = highpc;
7071
7072 /* If the compilation unit didn't have an explicit address range,
7073 then use the information extracted from its child dies. */
e385593e 7074 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7075 {
7076 best_lowpc = lowpc;
7077 best_highpc = highpc;
7078 }
7079 }
3e29f34a
MR
7080 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
7081 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 7082
8763cede 7083 end_psymtab_common (objfile, pst);
0018ea6f
DE
7084
7085 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
7086 {
7087 int i;
7088 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7089 struct dwarf2_per_cu_data *iter;
7090
7091 /* Fill in 'dependencies' here; we fill in 'users' in a
7092 post-pass. */
7093 pst->number_of_dependencies = len;
8d749320
SM
7094 pst->dependencies =
7095 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
7096 for (i = 0;
7097 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7098 i, iter);
7099 ++i)
7100 pst->dependencies[i] = iter->v.psymtab;
7101
7102 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7103 }
7104
7105 /* Get the list of files included in the current compilation unit,
7106 and build a psymtab for each of them. */
7107 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7108
b4f54984 7109 if (dwarf_read_debug)
0018ea6f
DE
7110 {
7111 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7112
7113 fprintf_unfiltered (gdb_stdlog,
7114 "Psymtab for %s unit @0x%x: %s - %s"
7115 ", %d global, %d static syms\n",
7116 per_cu->is_debug_types ? "type" : "comp",
9c541725 7117 to_underlying (per_cu->sect_off),
0018ea6f
DE
7118 paddress (gdbarch, pst->textlow),
7119 paddress (gdbarch, pst->texthigh),
7120 pst->n_global_syms, pst->n_static_syms);
7121 }
7122}
7123
7124/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7125 Process compilation unit THIS_CU for a psymtab. */
7126
7127static void
7128process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
7129 int want_partial_unit,
7130 enum language pretend_language)
0018ea6f
DE
7131{
7132 /* If this compilation unit was already read in, free the
7133 cached copy in order to read it in again. This is
7134 necessary because we skipped some symbols when we first
7135 read in the compilation unit (see load_partial_dies).
7136 This problem could be avoided, but the benefit is unclear. */
7137 if (this_cu->cu != NULL)
7138 free_one_cached_comp_unit (this_cu);
7139
f1902523
JK
7140 if (this_cu->is_debug_types)
7141 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
7142 NULL);
7143 else
7144 {
7145 process_psymtab_comp_unit_data info;
7146 info.want_partial_unit = want_partial_unit;
7147 info.pretend_language = pretend_language;
7148 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
7149 process_psymtab_comp_unit_reader, &info);
7150 }
0018ea6f
DE
7151
7152 /* Age out any secondary CUs. */
7153 age_cached_comp_units ();
7154}
f4dc4d17
DE
7155
7156/* Reader function for build_type_psymtabs. */
7157
7158static void
7159build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7160 const gdb_byte *info_ptr,
f4dc4d17
DE
7161 struct die_info *type_unit_die,
7162 int has_children,
7163 void *data)
7164{
7165 struct objfile *objfile = dwarf2_per_objfile->objfile;
7166 struct dwarf2_cu *cu = reader->cu;
7167 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7168 struct signatured_type *sig_type;
f4dc4d17
DE
7169 struct type_unit_group *tu_group;
7170 struct attribute *attr;
7171 struct partial_die_info *first_die;
7172 CORE_ADDR lowpc, highpc;
7173 struct partial_symtab *pst;
7174
7175 gdb_assert (data == NULL);
0186c6a7
DE
7176 gdb_assert (per_cu->is_debug_types);
7177 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
7178
7179 if (! has_children)
7180 return;
7181
7182 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 7183 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7184
0186c6a7 7185 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
7186
7187 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7188 cu->list_in_scope = &file_symbols;
7189 pst = create_partial_symtab (per_cu, "");
7190 pst->anonymous = 1;
7191
7192 first_die = load_partial_dies (reader, info_ptr, 1);
7193
7194 lowpc = (CORE_ADDR) -1;
7195 highpc = (CORE_ADDR) 0;
7196 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7197
8763cede 7198 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7199}
7200
73051182
DE
7201/* Struct used to sort TUs by their abbreviation table offset. */
7202
7203struct tu_abbrev_offset
7204{
7205 struct signatured_type *sig_type;
7206 sect_offset abbrev_offset;
7207};
7208
7209/* Helper routine for build_type_psymtabs_1, passed to qsort. */
7210
7211static int
7212sort_tu_by_abbrev_offset (const void *ap, const void *bp)
7213{
9a3c8263
SM
7214 const struct tu_abbrev_offset * const *a
7215 = (const struct tu_abbrev_offset * const*) ap;
7216 const struct tu_abbrev_offset * const *b
7217 = (const struct tu_abbrev_offset * const*) bp;
9c541725
PA
7218 sect_offset aoff = (*a)->abbrev_offset;
7219 sect_offset boff = (*b)->abbrev_offset;
73051182
DE
7220
7221 return (aoff > boff) - (aoff < boff);
7222}
7223
7224/* Efficiently read all the type units.
7225 This does the bulk of the work for build_type_psymtabs.
7226
7227 The efficiency is because we sort TUs by the abbrev table they use and
7228 only read each abbrev table once. In one program there are 200K TUs
7229 sharing 8K abbrev tables.
7230
7231 The main purpose of this function is to support building the
7232 dwarf2_per_objfile->type_unit_groups table.
7233 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7234 can collapse the search space by grouping them by stmt_list.
7235 The savings can be significant, in the same program from above the 200K TUs
7236 share 8K stmt_list tables.
7237
7238 FUNC is expected to call get_type_unit_group, which will create the
7239 struct type_unit_group if necessary and add it to
7240 dwarf2_per_objfile->type_unit_groups. */
7241
7242static void
7243build_type_psymtabs_1 (void)
7244{
73051182
DE
7245 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7246 struct cleanup *cleanups;
7247 struct abbrev_table *abbrev_table;
7248 sect_offset abbrev_offset;
7249 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
7250 int i;
7251
7252 /* It's up to the caller to not call us multiple times. */
7253 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7254
7255 if (dwarf2_per_objfile->n_type_units == 0)
7256 return;
7257
7258 /* TUs typically share abbrev tables, and there can be way more TUs than
7259 abbrev tables. Sort by abbrev table to reduce the number of times we
7260 read each abbrev table in.
7261 Alternatives are to punt or to maintain a cache of abbrev tables.
7262 This is simpler and efficient enough for now.
7263
7264 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7265 symtab to use). Typically TUs with the same abbrev offset have the same
7266 stmt_list value too so in practice this should work well.
7267
7268 The basic algorithm here is:
7269
7270 sort TUs by abbrev table
7271 for each TU with same abbrev table:
7272 read abbrev table if first user
7273 read TU top level DIE
7274 [IWBN if DWO skeletons had DW_AT_stmt_list]
7275 call FUNC */
7276
b4f54984 7277 if (dwarf_read_debug)
73051182
DE
7278 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7279
7280 /* Sort in a separate table to maintain the order of all_type_units
7281 for .gdb_index: TU indices directly index all_type_units. */
7282 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
7283 dwarf2_per_objfile->n_type_units);
7284 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
7285 {
7286 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
7287
7288 sorted_by_abbrev[i].sig_type = sig_type;
7289 sorted_by_abbrev[i].abbrev_offset =
7290 read_abbrev_offset (sig_type->per_cu.section,
9c541725 7291 sig_type->per_cu.sect_off);
73051182
DE
7292 }
7293 cleanups = make_cleanup (xfree, sorted_by_abbrev);
7294 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
7295 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
7296
9c541725 7297 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
7298 abbrev_table = NULL;
7299 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
7300
7301 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
7302 {
7303 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
7304
7305 /* Switch to the next abbrev table if necessary. */
7306 if (abbrev_table == NULL
9c541725 7307 || tu->abbrev_offset != abbrev_offset)
73051182
DE
7308 {
7309 if (abbrev_table != NULL)
7310 {
7311 abbrev_table_free (abbrev_table);
7312 /* Reset to NULL in case abbrev_table_read_table throws
7313 an error: abbrev_table_free_cleanup will get called. */
7314 abbrev_table = NULL;
7315 }
7316 abbrev_offset = tu->abbrev_offset;
7317 abbrev_table =
7318 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
7319 abbrev_offset);
7320 ++tu_stats->nr_uniq_abbrev_tables;
7321 }
7322
7323 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
7324 build_type_psymtabs_reader, NULL);
7325 }
7326
73051182 7327 do_cleanups (cleanups);
6aa5f3a6 7328}
73051182 7329
6aa5f3a6
DE
7330/* Print collected type unit statistics. */
7331
7332static void
7333print_tu_stats (void)
7334{
7335 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7336
7337 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7338 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
7339 dwarf2_per_objfile->n_type_units);
7340 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7341 tu_stats->nr_uniq_abbrev_tables);
7342 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7343 tu_stats->nr_symtabs);
7344 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7345 tu_stats->nr_symtab_sharers);
7346 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7347 tu_stats->nr_stmt_less_type_units);
7348 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7349 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7350}
7351
f4dc4d17
DE
7352/* Traversal function for build_type_psymtabs. */
7353
7354static int
7355build_type_psymtab_dependencies (void **slot, void *info)
7356{
7357 struct objfile *objfile = dwarf2_per_objfile->objfile;
7358 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7359 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 7360 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
7361 int len = VEC_length (sig_type_ptr, tu_group->tus);
7362 struct signatured_type *iter;
f4dc4d17
DE
7363 int i;
7364
7365 gdb_assert (len > 0);
0186c6a7 7366 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
7367
7368 pst->number_of_dependencies = len;
8d749320
SM
7369 pst->dependencies =
7370 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 7371 for (i = 0;
0186c6a7 7372 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
7373 ++i)
7374 {
0186c6a7
DE
7375 gdb_assert (iter->per_cu.is_debug_types);
7376 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7377 iter->type_unit_group = tu_group;
f4dc4d17
DE
7378 }
7379
0186c6a7 7380 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
7381
7382 return 1;
7383}
7384
7385/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7386 Build partial symbol tables for the .debug_types comp-units. */
7387
7388static void
7389build_type_psymtabs (struct objfile *objfile)
7390{
0e50663e 7391 if (! create_all_type_units (objfile))
348e048f
DE
7392 return;
7393
73051182 7394 build_type_psymtabs_1 ();
6aa5f3a6 7395}
f4dc4d17 7396
6aa5f3a6
DE
7397/* Traversal function for process_skeletonless_type_unit.
7398 Read a TU in a DWO file and build partial symbols for it. */
7399
7400static int
7401process_skeletonless_type_unit (void **slot, void *info)
7402{
7403 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9a3c8263 7404 struct objfile *objfile = (struct objfile *) info;
6aa5f3a6
DE
7405 struct signatured_type find_entry, *entry;
7406
7407 /* If this TU doesn't exist in the global table, add it and read it in. */
7408
7409 if (dwarf2_per_objfile->signatured_types == NULL)
7410 {
7411 dwarf2_per_objfile->signatured_types
7412 = allocate_signatured_type_table (objfile);
7413 }
7414
7415 find_entry.signature = dwo_unit->signature;
7416 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
7417 INSERT);
7418 /* If we've already seen this type there's nothing to do. What's happening
7419 is we're doing our own version of comdat-folding here. */
7420 if (*slot != NULL)
7421 return 1;
7422
7423 /* This does the job that create_all_type_units would have done for
7424 this TU. */
7425 entry = add_type_unit (dwo_unit->signature, slot);
7426 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
7427 *slot = entry;
7428
7429 /* This does the job that build_type_psymtabs_1 would have done. */
7430 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
7431 build_type_psymtabs_reader, NULL);
7432
7433 return 1;
7434}
7435
7436/* Traversal function for process_skeletonless_type_units. */
7437
7438static int
7439process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7440{
7441 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7442
7443 if (dwo_file->tus != NULL)
7444 {
7445 htab_traverse_noresize (dwo_file->tus,
7446 process_skeletonless_type_unit, info);
7447 }
7448
7449 return 1;
7450}
7451
7452/* Scan all TUs of DWO files, verifying we've processed them.
7453 This is needed in case a TU was emitted without its skeleton.
7454 Note: This can't be done until we know what all the DWO files are. */
7455
7456static void
7457process_skeletonless_type_units (struct objfile *objfile)
7458{
7459 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7460 if (get_dwp_file () == NULL
7461 && dwarf2_per_objfile->dwo_files != NULL)
7462 {
7463 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
7464 process_dwo_file_for_skeletonless_type_units,
7465 objfile);
7466 }
348e048f
DE
7467}
7468
95554aad
TT
7469/* Compute the 'user' field for each psymtab in OBJFILE. */
7470
7471static void
7472set_partial_user (struct objfile *objfile)
7473{
7474 int i;
7475
7476 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
7477 {
8832e7e3 7478 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
95554aad
TT
7479 struct partial_symtab *pst = per_cu->v.psymtab;
7480 int j;
7481
36586728
TT
7482 if (pst == NULL)
7483 continue;
7484
95554aad
TT
7485 for (j = 0; j < pst->number_of_dependencies; ++j)
7486 {
7487 /* Set the 'user' field only if it is not already set. */
7488 if (pst->dependencies[j]->user == NULL)
7489 pst->dependencies[j]->user = pst;
7490 }
7491 }
7492}
7493
93311388
DE
7494/* Build the partial symbol table by doing a quick pass through the
7495 .debug_info and .debug_abbrev sections. */
72bf9492 7496
93311388 7497static void
c67a9c90 7498dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 7499{
791afaa2 7500 struct cleanup *back_to;
21b2bd31 7501 int i;
93311388 7502
b4f54984 7503 if (dwarf_read_debug)
45cfd468
DE
7504 {
7505 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7506 objfile_name (objfile));
45cfd468
DE
7507 }
7508
98bfdba5
PA
7509 dwarf2_per_objfile->reading_partial_symbols = 1;
7510
be391dca 7511 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 7512
93311388
DE
7513 /* Any cached compilation units will be linked by the per-objfile
7514 read_in_chain. Make sure to free them when we're done. */
7515 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 7516
348e048f
DE
7517 build_type_psymtabs (objfile);
7518
93311388 7519 create_all_comp_units (objfile);
c906108c 7520
60606b2c
TT
7521 /* Create a temporary address map on a temporary obstack. We later
7522 copy this to the final obstack. */
8268c778 7523 auto_obstack temp_obstack;
791afaa2
TT
7524
7525 scoped_restore save_psymtabs_addrmap
7526 = make_scoped_restore (&objfile->psymtabs_addrmap,
7527 addrmap_create_mutable (&temp_obstack));
72bf9492 7528
21b2bd31 7529 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 7530 {
8832e7e3 7531 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
aaa75496 7532
b93601f3 7533 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 7534 }
ff013f42 7535
6aa5f3a6
DE
7536 /* This has to wait until we read the CUs, we need the list of DWOs. */
7537 process_skeletonless_type_units (objfile);
7538
7539 /* Now that all TUs have been processed we can fill in the dependencies. */
7540 if (dwarf2_per_objfile->type_unit_groups != NULL)
7541 {
7542 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
7543 build_type_psymtab_dependencies, NULL);
7544 }
7545
b4f54984 7546 if (dwarf_read_debug)
6aa5f3a6
DE
7547 print_tu_stats ();
7548
95554aad
TT
7549 set_partial_user (objfile);
7550
ff013f42
JK
7551 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
7552 &objfile->objfile_obstack);
791afaa2
TT
7553 /* At this point we want to keep the address map. */
7554 save_psymtabs_addrmap.release ();
ff013f42 7555
ae038cb0 7556 do_cleanups (back_to);
45cfd468 7557
b4f54984 7558 if (dwarf_read_debug)
45cfd468 7559 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7560 objfile_name (objfile));
ae038cb0
DJ
7561}
7562
3019eac3 7563/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
7564
7565static void
dee91e82 7566load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7567 const gdb_byte *info_ptr,
dee91e82
DE
7568 struct die_info *comp_unit_die,
7569 int has_children,
7570 void *data)
ae038cb0 7571{
dee91e82 7572 struct dwarf2_cu *cu = reader->cu;
ae038cb0 7573
95554aad 7574 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 7575
ae038cb0
DJ
7576 /* Check if comp unit has_children.
7577 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 7578 If not, there's no more debug_info for this comp unit. */
d85a05f0 7579 if (has_children)
dee91e82
DE
7580 load_partial_dies (reader, info_ptr, 0);
7581}
98bfdba5 7582
dee91e82
DE
7583/* Load the partial DIEs for a secondary CU into memory.
7584 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7585
dee91e82
DE
7586static void
7587load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7588{
f4dc4d17
DE
7589 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7590 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
7591}
7592
ae038cb0 7593static void
36586728
TT
7594read_comp_units_from_section (struct objfile *objfile,
7595 struct dwarf2_section_info *section,
f1902523 7596 struct dwarf2_section_info *abbrev_section,
36586728
TT
7597 unsigned int is_dwz,
7598 int *n_allocated,
7599 int *n_comp_units,
7600 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 7601{
d521ce57 7602 const gdb_byte *info_ptr;
a32a8923 7603 bfd *abfd = get_section_bfd_owner (section);
be391dca 7604
b4f54984 7605 if (dwarf_read_debug)
bf6af496 7606 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
7607 get_section_name (section),
7608 get_section_file_name (section));
bf6af496 7609
36586728 7610 dwarf2_read_section (objfile, section);
ae038cb0 7611
36586728 7612 info_ptr = section->buffer;
6e70227d 7613
36586728 7614 while (info_ptr < section->buffer + section->size)
ae038cb0 7615 {
ae038cb0 7616 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7617
9c541725 7618 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7619
f1902523
JK
7620 comp_unit_head cu_header;
7621 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
7622 info_ptr, rcuh_kind::COMPILE);
ae038cb0
DJ
7623
7624 /* Save the compilation unit for later lookup. */
f1902523
JK
7625 if (cu_header.unit_type != DW_UT_type)
7626 {
7627 this_cu = XOBNEW (&objfile->objfile_obstack,
7628 struct dwarf2_per_cu_data);
7629 memset (this_cu, 0, sizeof (*this_cu));
7630 }
7631 else
7632 {
7633 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7634 struct signatured_type);
7635 memset (sig_type, 0, sizeof (*sig_type));
7636 sig_type->signature = cu_header.signature;
7637 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7638 this_cu = &sig_type->per_cu;
7639 }
7640 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7641 this_cu->sect_off = sect_off;
f1902523 7642 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7643 this_cu->is_dwz = is_dwz;
9291a0cd 7644 this_cu->objfile = objfile;
8a0459fd 7645 this_cu->section = section;
ae038cb0 7646
36586728 7647 if (*n_comp_units == *n_allocated)
ae038cb0 7648 {
36586728 7649 *n_allocated *= 2;
224c3ddb
SM
7650 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
7651 *all_comp_units, *n_allocated);
ae038cb0 7652 }
36586728
TT
7653 (*all_comp_units)[*n_comp_units] = this_cu;
7654 ++*n_comp_units;
ae038cb0
DJ
7655
7656 info_ptr = info_ptr + this_cu->length;
7657 }
36586728
TT
7658}
7659
7660/* Create a list of all compilation units in OBJFILE.
7661 This is only done for -readnow and building partial symtabs. */
7662
7663static void
7664create_all_comp_units (struct objfile *objfile)
7665{
7666 int n_allocated;
7667 int n_comp_units;
7668 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 7669 struct dwz_file *dwz;
36586728
TT
7670
7671 n_comp_units = 0;
7672 n_allocated = 10;
8d749320 7673 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728 7674
f1902523
JK
7675 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
7676 &dwarf2_per_objfile->abbrev, 0,
36586728
TT
7677 &n_allocated, &n_comp_units, &all_comp_units);
7678
4db1a1dc
TT
7679 dwz = dwarf2_get_dwz_file ();
7680 if (dwz != NULL)
f1902523 7681 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
4db1a1dc
TT
7682 &n_allocated, &n_comp_units,
7683 &all_comp_units);
ae038cb0 7684
8d749320
SM
7685 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
7686 struct dwarf2_per_cu_data *,
7687 n_comp_units);
ae038cb0
DJ
7688 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
7689 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
7690 xfree (all_comp_units);
7691 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
7692}
7693
5734ee8b 7694/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7695 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7696 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7697 DW_AT_ranges). See the comments of add_partial_subprogram on how
7698 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7699
72bf9492
DJ
7700static void
7701scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7702 CORE_ADDR *highpc, int set_addrmap,
7703 struct dwarf2_cu *cu)
c906108c 7704{
72bf9492 7705 struct partial_die_info *pdi;
c906108c 7706
91c24f0a
DC
7707 /* Now, march along the PDI's, descending into ones which have
7708 interesting children but skipping the children of the other ones,
7709 until we reach the end of the compilation unit. */
c906108c 7710
72bf9492 7711 pdi = first_die;
91c24f0a 7712
72bf9492
DJ
7713 while (pdi != NULL)
7714 {
7715 fixup_partial_die (pdi, cu);
c906108c 7716
f55ee35c 7717 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7718 children, so we need to look at them. Ditto for anonymous
7719 enums. */
933c6fe4 7720
72bf9492 7721 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
7722 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7723 || pdi->tag == DW_TAG_imported_unit)
c906108c 7724 {
72bf9492 7725 switch (pdi->tag)
c906108c
SS
7726 {
7727 case DW_TAG_subprogram:
cdc07690 7728 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 7729 break;
72929c62 7730 case DW_TAG_constant:
c906108c
SS
7731 case DW_TAG_variable:
7732 case DW_TAG_typedef:
91c24f0a 7733 case DW_TAG_union_type:
72bf9492 7734 if (!pdi->is_declaration)
63d06c5c 7735 {
72bf9492 7736 add_partial_symbol (pdi, cu);
63d06c5c
DC
7737 }
7738 break;
c906108c 7739 case DW_TAG_class_type:
680b30c7 7740 case DW_TAG_interface_type:
c906108c 7741 case DW_TAG_structure_type:
72bf9492 7742 if (!pdi->is_declaration)
c906108c 7743 {
72bf9492 7744 add_partial_symbol (pdi, cu);
c906108c 7745 }
e98c9e7c
TT
7746 if (cu->language == language_rust && pdi->has_children)
7747 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7748 set_addrmap, cu);
c906108c 7749 break;
91c24f0a 7750 case DW_TAG_enumeration_type:
72bf9492
DJ
7751 if (!pdi->is_declaration)
7752 add_partial_enumeration (pdi, cu);
c906108c
SS
7753 break;
7754 case DW_TAG_base_type:
a02abb62 7755 case DW_TAG_subrange_type:
c906108c 7756 /* File scope base type definitions are added to the partial
c5aa993b 7757 symbol table. */
72bf9492 7758 add_partial_symbol (pdi, cu);
c906108c 7759 break;
d9fa45fe 7760 case DW_TAG_namespace:
cdc07690 7761 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7762 break;
5d7cb8df 7763 case DW_TAG_module:
cdc07690 7764 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7765 break;
95554aad
TT
7766 case DW_TAG_imported_unit:
7767 {
7768 struct dwarf2_per_cu_data *per_cu;
7769
f4dc4d17
DE
7770 /* For now we don't handle imported units in type units. */
7771 if (cu->per_cu->is_debug_types)
7772 {
7773 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7774 " supported in type units [in module %s]"),
4262abfb 7775 objfile_name (cu->objfile));
f4dc4d17
DE
7776 }
7777
9c541725 7778 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
36586728 7779 pdi->is_dwz,
95554aad
TT
7780 cu->objfile);
7781
7782 /* Go read the partial unit, if needed. */
7783 if (per_cu->v.psymtab == NULL)
b93601f3 7784 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 7785
f4dc4d17 7786 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 7787 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
7788 }
7789 break;
74921315
KS
7790 case DW_TAG_imported_declaration:
7791 add_partial_symbol (pdi, cu);
7792 break;
c906108c
SS
7793 default:
7794 break;
7795 }
7796 }
7797
72bf9492
DJ
7798 /* If the die has a sibling, skip to the sibling. */
7799
7800 pdi = pdi->die_sibling;
7801 }
7802}
7803
7804/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7805
72bf9492 7806 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7807 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7808 Enumerators are an exception; they use the scope of their parent
7809 enumeration type, i.e. the name of the enumeration type is not
7810 prepended to the enumerator.
91c24f0a 7811
72bf9492
DJ
7812 There are two complexities. One is DW_AT_specification; in this
7813 case "parent" means the parent of the target of the specification,
7814 instead of the direct parent of the DIE. The other is compilers
7815 which do not emit DW_TAG_namespace; in this case we try to guess
7816 the fully qualified name of structure types from their members'
7817 linkage names. This must be done using the DIE's children rather
7818 than the children of any DW_AT_specification target. We only need
7819 to do this for structures at the top level, i.e. if the target of
7820 any DW_AT_specification (if any; otherwise the DIE itself) does not
7821 have a parent. */
7822
7823/* Compute the scope prefix associated with PDI's parent, in
7824 compilation unit CU. The result will be allocated on CU's
7825 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7826 field. NULL is returned if no prefix is necessary. */
15d034d0 7827static const char *
72bf9492
DJ
7828partial_die_parent_scope (struct partial_die_info *pdi,
7829 struct dwarf2_cu *cu)
7830{
15d034d0 7831 const char *grandparent_scope;
72bf9492 7832 struct partial_die_info *parent, *real_pdi;
91c24f0a 7833
72bf9492
DJ
7834 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7835 then this means the parent of the specification DIE. */
7836
7837 real_pdi = pdi;
72bf9492 7838 while (real_pdi->has_specification)
36586728
TT
7839 real_pdi = find_partial_die (real_pdi->spec_offset,
7840 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
7841
7842 parent = real_pdi->die_parent;
7843 if (parent == NULL)
7844 return NULL;
7845
7846 if (parent->scope_set)
7847 return parent->scope;
7848
7849 fixup_partial_die (parent, cu);
7850
10b3939b 7851 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 7852
acebe513
UW
7853 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7854 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7855 Work around this problem here. */
7856 if (cu->language == language_cplus
6e70227d 7857 && parent->tag == DW_TAG_namespace
acebe513
UW
7858 && strcmp (parent->name, "::") == 0
7859 && grandparent_scope == NULL)
7860 {
7861 parent->scope = NULL;
7862 parent->scope_set = 1;
7863 return NULL;
7864 }
7865
9c6c53f7
SA
7866 if (pdi->tag == DW_TAG_enumerator)
7867 /* Enumerators should not get the name of the enumeration as a prefix. */
7868 parent->scope = grandparent_scope;
7869 else if (parent->tag == DW_TAG_namespace
f55ee35c 7870 || parent->tag == DW_TAG_module
72bf9492
DJ
7871 || parent->tag == DW_TAG_structure_type
7872 || parent->tag == DW_TAG_class_type
680b30c7 7873 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
7874 || parent->tag == DW_TAG_union_type
7875 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
7876 {
7877 if (grandparent_scope == NULL)
7878 parent->scope = parent->name;
7879 else
3e43a32a
MS
7880 parent->scope = typename_concat (&cu->comp_unit_obstack,
7881 grandparent_scope,
f55ee35c 7882 parent->name, 0, cu);
72bf9492 7883 }
72bf9492
DJ
7884 else
7885 {
7886 /* FIXME drow/2004-04-01: What should we be doing with
7887 function-local names? For partial symbols, we should probably be
7888 ignoring them. */
7889 complaint (&symfile_complaints,
e2e0b3e5 7890 _("unhandled containing DIE tag %d for DIE at %d"),
9c541725 7891 parent->tag, to_underlying (pdi->sect_off));
72bf9492 7892 parent->scope = grandparent_scope;
c906108c
SS
7893 }
7894
72bf9492
DJ
7895 parent->scope_set = 1;
7896 return parent->scope;
7897}
7898
7899/* Return the fully scoped name associated with PDI, from compilation unit
7900 CU. The result will be allocated with malloc. */
4568ecf9 7901
72bf9492
DJ
7902static char *
7903partial_die_full_name (struct partial_die_info *pdi,
7904 struct dwarf2_cu *cu)
7905{
15d034d0 7906 const char *parent_scope;
72bf9492 7907
98bfdba5
PA
7908 /* If this is a template instantiation, we can not work out the
7909 template arguments from partial DIEs. So, unfortunately, we have
7910 to go through the full DIEs. At least any work we do building
7911 types here will be reused if full symbols are loaded later. */
7912 if (pdi->has_template_arguments)
7913 {
7914 fixup_partial_die (pdi, cu);
7915
7916 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7917 {
7918 struct die_info *die;
7919 struct attribute attr;
7920 struct dwarf2_cu *ref_cu = cu;
7921
b64f50a1 7922 /* DW_FORM_ref_addr is using section offset. */
b4069958 7923 attr.name = (enum dwarf_attribute) 0;
98bfdba5 7924 attr.form = DW_FORM_ref_addr;
9c541725 7925 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
7926 die = follow_die_ref (NULL, &attr, &ref_cu);
7927
7928 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7929 }
7930 }
7931
72bf9492
DJ
7932 parent_scope = partial_die_parent_scope (pdi, cu);
7933 if (parent_scope == NULL)
7934 return NULL;
7935 else
f55ee35c 7936 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
7937}
7938
7939static void
72bf9492 7940add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 7941{
e7c27a73 7942 struct objfile *objfile = cu->objfile;
3e29f34a 7943 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 7944 CORE_ADDR addr = 0;
15d034d0 7945 const char *actual_name = NULL;
e142c38c 7946 CORE_ADDR baseaddr;
15d034d0 7947 char *built_actual_name;
e142c38c
DJ
7948
7949 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7950
15d034d0
TT
7951 built_actual_name = partial_die_full_name (pdi, cu);
7952 if (built_actual_name != NULL)
7953 actual_name = built_actual_name;
63d06c5c 7954
72bf9492
DJ
7955 if (actual_name == NULL)
7956 actual_name = pdi->name;
7957
c906108c
SS
7958 switch (pdi->tag)
7959 {
7960 case DW_TAG_subprogram:
3e29f34a 7961 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 7962 if (pdi->is_external || cu->language == language_ada)
c906108c 7963 {
2cfa0c8d
JB
7964 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
7965 of the global scope. But in Ada, we want to be able to access
7966 nested procedures globally. So all Ada subprograms are stored
7967 in the global scope. */
f47fb265 7968 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7969 built_actual_name != NULL,
f47fb265
MS
7970 VAR_DOMAIN, LOC_BLOCK,
7971 &objfile->global_psymbols,
1762568f 7972 addr, cu->language, objfile);
c906108c
SS
7973 }
7974 else
7975 {
f47fb265 7976 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7977 built_actual_name != NULL,
f47fb265
MS
7978 VAR_DOMAIN, LOC_BLOCK,
7979 &objfile->static_psymbols,
1762568f 7980 addr, cu->language, objfile);
c906108c 7981 }
0c1b455e
TT
7982
7983 if (pdi->main_subprogram && actual_name != NULL)
7984 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 7985 break;
72929c62
JB
7986 case DW_TAG_constant:
7987 {
af5bf4ad 7988 std::vector<partial_symbol *> *list;
72929c62
JB
7989
7990 if (pdi->is_external)
7991 list = &objfile->global_psymbols;
7992 else
7993 list = &objfile->static_psymbols;
f47fb265 7994 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7995 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 7996 list, 0, cu->language, objfile);
72929c62
JB
7997 }
7998 break;
c906108c 7999 case DW_TAG_variable:
95554aad
TT
8000 if (pdi->d.locdesc)
8001 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8002
95554aad 8003 if (pdi->d.locdesc
caac4577
JG
8004 && addr == 0
8005 && !dwarf2_per_objfile->has_section_at_zero)
8006 {
8007 /* A global or static variable may also have been stripped
8008 out by the linker if unused, in which case its address
8009 will be nullified; do not add such variables into partial
8010 symbol table then. */
8011 }
8012 else if (pdi->is_external)
c906108c
SS
8013 {
8014 /* Global Variable.
8015 Don't enter into the minimal symbol tables as there is
8016 a minimal symbol table entry from the ELF symbols already.
8017 Enter into partial symbol table if it has a location
8018 descriptor or a type.
8019 If the location descriptor is missing, new_symbol will create
8020 a LOC_UNRESOLVED symbol, the address of the variable will then
8021 be determined from the minimal symbol table whenever the variable
8022 is referenced.
8023 The address for the partial symbol table entry is not
8024 used by GDB, but it comes in handy for debugging partial symbol
8025 table building. */
8026
95554aad 8027 if (pdi->d.locdesc || pdi->has_type)
f47fb265 8028 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8029 built_actual_name != NULL,
f47fb265
MS
8030 VAR_DOMAIN, LOC_STATIC,
8031 &objfile->global_psymbols,
1762568f 8032 addr + baseaddr,
f47fb265 8033 cu->language, objfile);
c906108c
SS
8034 }
8035 else
8036 {
ff908ebf
AW
8037 int has_loc = pdi->d.locdesc != NULL;
8038
8039 /* Static Variable. Skip symbols whose value we cannot know (those
8040 without location descriptors or constant values). */
8041 if (!has_loc && !pdi->has_const_value)
decbce07 8042 {
15d034d0 8043 xfree (built_actual_name);
decbce07
MS
8044 return;
8045 }
ff908ebf 8046
f47fb265 8047 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8048 built_actual_name != NULL,
f47fb265
MS
8049 VAR_DOMAIN, LOC_STATIC,
8050 &objfile->static_psymbols,
ff908ebf 8051 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 8052 cu->language, objfile);
c906108c
SS
8053 }
8054 break;
8055 case DW_TAG_typedef:
8056 case DW_TAG_base_type:
a02abb62 8057 case DW_TAG_subrange_type:
38d518c9 8058 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8059 built_actual_name != NULL,
176620f1 8060 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 8061 &objfile->static_psymbols,
1762568f 8062 0, cu->language, objfile);
c906108c 8063 break;
74921315 8064 case DW_TAG_imported_declaration:
72bf9492
DJ
8065 case DW_TAG_namespace:
8066 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8067 built_actual_name != NULL,
72bf9492
DJ
8068 VAR_DOMAIN, LOC_TYPEDEF,
8069 &objfile->global_psymbols,
1762568f 8070 0, cu->language, objfile);
72bf9492 8071 break;
530e8392
KB
8072 case DW_TAG_module:
8073 add_psymbol_to_list (actual_name, strlen (actual_name),
8074 built_actual_name != NULL,
8075 MODULE_DOMAIN, LOC_TYPEDEF,
8076 &objfile->global_psymbols,
1762568f 8077 0, cu->language, objfile);
530e8392 8078 break;
c906108c 8079 case DW_TAG_class_type:
680b30c7 8080 case DW_TAG_interface_type:
c906108c
SS
8081 case DW_TAG_structure_type:
8082 case DW_TAG_union_type:
8083 case DW_TAG_enumeration_type:
fa4028e9
JB
8084 /* Skip external references. The DWARF standard says in the section
8085 about "Structure, Union, and Class Type Entries": "An incomplete
8086 structure, union or class type is represented by a structure,
8087 union or class entry that does not have a byte size attribute
8088 and that has a DW_AT_declaration attribute." */
8089 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 8090 {
15d034d0 8091 xfree (built_actual_name);
decbce07
MS
8092 return;
8093 }
fa4028e9 8094
63d06c5c
DC
8095 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8096 static vs. global. */
38d518c9 8097 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8098 built_actual_name != NULL,
176620f1 8099 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 8100 cu->language == language_cplus
63d06c5c
DC
8101 ? &objfile->global_psymbols
8102 : &objfile->static_psymbols,
1762568f 8103 0, cu->language, objfile);
c906108c 8104
c906108c
SS
8105 break;
8106 case DW_TAG_enumerator:
38d518c9 8107 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8108 built_actual_name != NULL,
176620f1 8109 VAR_DOMAIN, LOC_CONST,
9c37b5ae 8110 cu->language == language_cplus
f6fe98ef
DJ
8111 ? &objfile->global_psymbols
8112 : &objfile->static_psymbols,
1762568f 8113 0, cu->language, objfile);
c906108c
SS
8114 break;
8115 default:
8116 break;
8117 }
5c4e30ca 8118
15d034d0 8119 xfree (built_actual_name);
c906108c
SS
8120}
8121
5c4e30ca
DC
8122/* Read a partial die corresponding to a namespace; also, add a symbol
8123 corresponding to that namespace to the symbol table. NAMESPACE is
8124 the name of the enclosing namespace. */
91c24f0a 8125
72bf9492
DJ
8126static void
8127add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8128 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8129 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8130{
72bf9492 8131 /* Add a symbol for the namespace. */
e7c27a73 8132
72bf9492 8133 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8134
8135 /* Now scan partial symbols in that namespace. */
8136
91c24f0a 8137 if (pdi->has_children)
cdc07690 8138 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8139}
8140
5d7cb8df
JK
8141/* Read a partial die corresponding to a Fortran module. */
8142
8143static void
8144add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8145 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8146{
530e8392
KB
8147 /* Add a symbol for the namespace. */
8148
8149 add_partial_symbol (pdi, cu);
8150
f55ee35c 8151 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8152
8153 if (pdi->has_children)
cdc07690 8154 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8155}
8156
bc30ff58
JB
8157/* Read a partial die corresponding to a subprogram and create a partial
8158 symbol for that subprogram. When the CU language allows it, this
8159 routine also defines a partial symbol for each nested subprogram
cdc07690 8160 that this subprogram contains. If SET_ADDRMAP is true, record the
428fc5fc
YQ
8161 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
8162 and highest PC values found in PDI.
6e70227d 8163
cdc07690
YQ
8164 PDI may also be a lexical block, in which case we simply search
8165 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8166 Again, this is only performed when the CU language allows this
8167 type of definitions. */
8168
8169static void
8170add_partial_subprogram (struct partial_die_info *pdi,
8171 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8172 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58
JB
8173{
8174 if (pdi->tag == DW_TAG_subprogram)
8175 {
8176 if (pdi->has_pc_info)
8177 {
8178 if (pdi->lowpc < *lowpc)
8179 *lowpc = pdi->lowpc;
8180 if (pdi->highpc > *highpc)
8181 *highpc = pdi->highpc;
cdc07690 8182 if (set_addrmap)
5734ee8b 8183 {
5734ee8b 8184 struct objfile *objfile = cu->objfile;
3e29f34a
MR
8185 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8186 CORE_ADDR baseaddr;
8187 CORE_ADDR highpc;
8188 CORE_ADDR lowpc;
5734ee8b
DJ
8189
8190 baseaddr = ANOFFSET (objfile->section_offsets,
8191 SECT_OFF_TEXT (objfile));
3e29f34a
MR
8192 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
8193 pdi->lowpc + baseaddr);
8194 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
8195 pdi->highpc + baseaddr);
8196 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 8197 cu->per_cu->v.psymtab);
5734ee8b 8198 }
481860b3
GB
8199 }
8200
8201 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8202 {
bc30ff58 8203 if (!pdi->is_declaration)
e8d05480
JB
8204 /* Ignore subprogram DIEs that do not have a name, they are
8205 illegal. Do not emit a complaint at this point, we will
8206 do so when we convert this psymtab into a symtab. */
8207 if (pdi->name)
8208 add_partial_symbol (pdi, cu);
bc30ff58
JB
8209 }
8210 }
6e70227d 8211
bc30ff58
JB
8212 if (! pdi->has_children)
8213 return;
8214
8215 if (cu->language == language_ada)
8216 {
8217 pdi = pdi->die_child;
8218 while (pdi != NULL)
8219 {
8220 fixup_partial_die (pdi, cu);
8221 if (pdi->tag == DW_TAG_subprogram
8222 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8223 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8224 pdi = pdi->die_sibling;
8225 }
8226 }
8227}
8228
91c24f0a
DC
8229/* Read a partial die corresponding to an enumeration type. */
8230
72bf9492
DJ
8231static void
8232add_partial_enumeration (struct partial_die_info *enum_pdi,
8233 struct dwarf2_cu *cu)
91c24f0a 8234{
72bf9492 8235 struct partial_die_info *pdi;
91c24f0a
DC
8236
8237 if (enum_pdi->name != NULL)
72bf9492
DJ
8238 add_partial_symbol (enum_pdi, cu);
8239
8240 pdi = enum_pdi->die_child;
8241 while (pdi)
91c24f0a 8242 {
72bf9492 8243 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 8244 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 8245 else
72bf9492
DJ
8246 add_partial_symbol (pdi, cu);
8247 pdi = pdi->die_sibling;
91c24f0a 8248 }
91c24f0a
DC
8249}
8250
6caca83c
CC
8251/* Return the initial uleb128 in the die at INFO_PTR. */
8252
8253static unsigned int
d521ce57 8254peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8255{
8256 unsigned int bytes_read;
8257
8258 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8259}
8260
4bb7a0a7
DJ
8261/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
8262 Return the corresponding abbrev, or NULL if the number is zero (indicating
8263 an empty DIE). In either case *BYTES_READ will be set to the length of
8264 the initial number. */
8265
8266static struct abbrev_info *
d521ce57 8267peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 8268 struct dwarf2_cu *cu)
4bb7a0a7
DJ
8269{
8270 bfd *abfd = cu->objfile->obfd;
8271 unsigned int abbrev_number;
8272 struct abbrev_info *abbrev;
8273
8274 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8275
8276 if (abbrev_number == 0)
8277 return NULL;
8278
433df2d4 8279 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
8280 if (!abbrev)
8281 {
422b9917
DE
8282 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8283 " at offset 0x%x [in module %s]"),
8284 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9c541725 8285 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8286 }
8287
8288 return abbrev;
8289}
8290
93311388
DE
8291/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8292 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8293 DIE. Any children of the skipped DIEs will also be skipped. */
8294
d521ce57
TT
8295static const gdb_byte *
8296skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8297{
dee91e82 8298 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
8299 struct abbrev_info *abbrev;
8300 unsigned int bytes_read;
8301
8302 while (1)
8303 {
8304 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8305 if (abbrev == NULL)
8306 return info_ptr + bytes_read;
8307 else
dee91e82 8308 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8309 }
8310}
8311
93311388
DE
8312/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8313 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8314 abbrev corresponding to that skipped uleb128 should be passed in
8315 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8316 children. */
8317
d521ce57
TT
8318static const gdb_byte *
8319skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8320 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8321{
8322 unsigned int bytes_read;
8323 struct attribute attr;
dee91e82
DE
8324 bfd *abfd = reader->abfd;
8325 struct dwarf2_cu *cu = reader->cu;
d521ce57 8326 const gdb_byte *buffer = reader->buffer;
f664829e 8327 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8328 unsigned int form, i;
8329
8330 for (i = 0; i < abbrev->num_attrs; i++)
8331 {
8332 /* The only abbrev we care about is DW_AT_sibling. */
8333 if (abbrev->attrs[i].name == DW_AT_sibling)
8334 {
dee91e82 8335 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8336 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
8337 complaint (&symfile_complaints,
8338 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8339 else
b9502d3f 8340 {
9c541725
PA
8341 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8342 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8343
8344 if (sibling_ptr < info_ptr)
8345 complaint (&symfile_complaints,
8346 _("DW_AT_sibling points backwards"));
22869d73
KS
8347 else if (sibling_ptr > reader->buffer_end)
8348 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
8349 else
8350 return sibling_ptr;
8351 }
4bb7a0a7
DJ
8352 }
8353
8354 /* If it isn't DW_AT_sibling, skip this attribute. */
8355 form = abbrev->attrs[i].form;
8356 skip_attribute:
8357 switch (form)
8358 {
4bb7a0a7 8359 case DW_FORM_ref_addr:
ae411497
TT
8360 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8361 and later it is offset sized. */
8362 if (cu->header.version == 2)
8363 info_ptr += cu->header.addr_size;
8364 else
8365 info_ptr += cu->header.offset_size;
8366 break;
36586728
TT
8367 case DW_FORM_GNU_ref_alt:
8368 info_ptr += cu->header.offset_size;
8369 break;
ae411497 8370 case DW_FORM_addr:
4bb7a0a7
DJ
8371 info_ptr += cu->header.addr_size;
8372 break;
8373 case DW_FORM_data1:
8374 case DW_FORM_ref1:
8375 case DW_FORM_flag:
8376 info_ptr += 1;
8377 break;
2dc7f7b3 8378 case DW_FORM_flag_present:
43988095 8379 case DW_FORM_implicit_const:
2dc7f7b3 8380 break;
4bb7a0a7
DJ
8381 case DW_FORM_data2:
8382 case DW_FORM_ref2:
8383 info_ptr += 2;
8384 break;
8385 case DW_FORM_data4:
8386 case DW_FORM_ref4:
8387 info_ptr += 4;
8388 break;
8389 case DW_FORM_data8:
8390 case DW_FORM_ref8:
55f1336d 8391 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8392 info_ptr += 8;
8393 break;
0224619f
JK
8394 case DW_FORM_data16:
8395 info_ptr += 16;
8396 break;
4bb7a0a7 8397 case DW_FORM_string:
9b1c24c8 8398 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8399 info_ptr += bytes_read;
8400 break;
2dc7f7b3 8401 case DW_FORM_sec_offset:
4bb7a0a7 8402 case DW_FORM_strp:
36586728 8403 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8404 info_ptr += cu->header.offset_size;
8405 break;
2dc7f7b3 8406 case DW_FORM_exprloc:
4bb7a0a7
DJ
8407 case DW_FORM_block:
8408 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8409 info_ptr += bytes_read;
8410 break;
8411 case DW_FORM_block1:
8412 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8413 break;
8414 case DW_FORM_block2:
8415 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8416 break;
8417 case DW_FORM_block4:
8418 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8419 break;
8420 case DW_FORM_sdata:
8421 case DW_FORM_udata:
8422 case DW_FORM_ref_udata:
3019eac3
DE
8423 case DW_FORM_GNU_addr_index:
8424 case DW_FORM_GNU_str_index:
d521ce57 8425 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8426 break;
8427 case DW_FORM_indirect:
8428 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8429 info_ptr += bytes_read;
8430 /* We need to continue parsing from here, so just go back to
8431 the top. */
8432 goto skip_attribute;
8433
8434 default:
3e43a32a
MS
8435 error (_("Dwarf Error: Cannot handle %s "
8436 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8437 dwarf_form_name (form),
8438 bfd_get_filename (abfd));
8439 }
8440 }
8441
8442 if (abbrev->has_children)
dee91e82 8443 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8444 else
8445 return info_ptr;
8446}
8447
93311388 8448/* Locate ORIG_PDI's sibling.
dee91e82 8449 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8450
d521ce57 8451static const gdb_byte *
dee91e82
DE
8452locate_pdi_sibling (const struct die_reader_specs *reader,
8453 struct partial_die_info *orig_pdi,
d521ce57 8454 const gdb_byte *info_ptr)
91c24f0a
DC
8455{
8456 /* Do we know the sibling already? */
72bf9492 8457
91c24f0a
DC
8458 if (orig_pdi->sibling)
8459 return orig_pdi->sibling;
8460
8461 /* Are there any children to deal with? */
8462
8463 if (!orig_pdi->has_children)
8464 return info_ptr;
8465
4bb7a0a7 8466 /* Skip the children the long way. */
91c24f0a 8467
dee91e82 8468 return skip_children (reader, info_ptr);
91c24f0a
DC
8469}
8470
257e7a09 8471/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8472 not NULL. */
c906108c
SS
8473
8474static void
257e7a09
YQ
8475dwarf2_read_symtab (struct partial_symtab *self,
8476 struct objfile *objfile)
c906108c 8477{
257e7a09 8478 if (self->readin)
c906108c 8479 {
442e4d9c 8480 warning (_("bug: psymtab for %s is already read in."),
257e7a09 8481 self->filename);
442e4d9c
YQ
8482 }
8483 else
8484 {
8485 if (info_verbose)
c906108c 8486 {
442e4d9c 8487 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 8488 self->filename);
442e4d9c 8489 gdb_flush (gdb_stdout);
c906108c 8490 }
c906108c 8491
442e4d9c 8492 /* Restore our global data. */
9a3c8263
SM
8493 dwarf2_per_objfile
8494 = (struct dwarf2_per_objfile *) objfile_data (objfile,
8495 dwarf2_objfile_data_key);
10b3939b 8496
442e4d9c
YQ
8497 /* If this psymtab is constructed from a debug-only objfile, the
8498 has_section_at_zero flag will not necessarily be correct. We
8499 can get the correct value for this flag by looking at the data
8500 associated with the (presumably stripped) associated objfile. */
8501 if (objfile->separate_debug_objfile_backlink)
8502 {
8503 struct dwarf2_per_objfile *dpo_backlink
9a3c8263
SM
8504 = ((struct dwarf2_per_objfile *)
8505 objfile_data (objfile->separate_debug_objfile_backlink,
8506 dwarf2_objfile_data_key));
9a619af0 8507
442e4d9c
YQ
8508 dwarf2_per_objfile->has_section_at_zero
8509 = dpo_backlink->has_section_at_zero;
8510 }
b2ab525c 8511
442e4d9c 8512 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 8513
257e7a09 8514 psymtab_to_symtab_1 (self);
c906108c 8515
442e4d9c
YQ
8516 /* Finish up the debug error message. */
8517 if (info_verbose)
8518 printf_filtered (_("done.\n"));
c906108c 8519 }
95554aad
TT
8520
8521 process_cu_includes ();
c906108c 8522}
9cdd5dbd
DE
8523\f
8524/* Reading in full CUs. */
c906108c 8525
10b3939b
DJ
8526/* Add PER_CU to the queue. */
8527
8528static void
95554aad
TT
8529queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8530 enum language pretend_language)
10b3939b
DJ
8531{
8532 struct dwarf2_queue_item *item;
8533
8534 per_cu->queued = 1;
8d749320 8535 item = XNEW (struct dwarf2_queue_item);
10b3939b 8536 item->per_cu = per_cu;
95554aad 8537 item->pretend_language = pretend_language;
10b3939b
DJ
8538 item->next = NULL;
8539
8540 if (dwarf2_queue == NULL)
8541 dwarf2_queue = item;
8542 else
8543 dwarf2_queue_tail->next = item;
8544
8545 dwarf2_queue_tail = item;
8546}
8547
89e63ee4
DE
8548/* If PER_CU is not yet queued, add it to the queue.
8549 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8550 dependency.
0907af0c 8551 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8552 meaning either PER_CU is already queued or it is already loaded.
8553
8554 N.B. There is an invariant here that if a CU is queued then it is loaded.
8555 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8556
8557static int
89e63ee4 8558maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8559 struct dwarf2_per_cu_data *per_cu,
8560 enum language pretend_language)
8561{
8562 /* We may arrive here during partial symbol reading, if we need full
8563 DIEs to process an unusual case (e.g. template arguments). Do
8564 not queue PER_CU, just tell our caller to load its DIEs. */
8565 if (dwarf2_per_objfile->reading_partial_symbols)
8566 {
8567 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8568 return 1;
8569 return 0;
8570 }
8571
8572 /* Mark the dependence relation so that we don't flush PER_CU
8573 too early. */
89e63ee4
DE
8574 if (dependent_cu != NULL)
8575 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8576
8577 /* If it's already on the queue, we have nothing to do. */
8578 if (per_cu->queued)
8579 return 0;
8580
8581 /* If the compilation unit is already loaded, just mark it as
8582 used. */
8583 if (per_cu->cu != NULL)
8584 {
8585 per_cu->cu->last_used = 0;
8586 return 0;
8587 }
8588
8589 /* Add it to the queue. */
8590 queue_comp_unit (per_cu, pretend_language);
8591
8592 return 1;
8593}
8594
10b3939b
DJ
8595/* Process the queue. */
8596
8597static void
a0f42c21 8598process_queue (void)
10b3939b
DJ
8599{
8600 struct dwarf2_queue_item *item, *next_item;
8601
b4f54984 8602 if (dwarf_read_debug)
45cfd468
DE
8603 {
8604 fprintf_unfiltered (gdb_stdlog,
8605 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8606 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8607 }
8608
03dd20cc
DJ
8609 /* The queue starts out with one item, but following a DIE reference
8610 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
8611 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
8612 {
cc12ce38
DE
8613 if ((dwarf2_per_objfile->using_index
8614 ? !item->per_cu->v.quick->compunit_symtab
8615 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
8616 /* Skip dummy CUs. */
8617 && item->per_cu->cu != NULL)
f4dc4d17
DE
8618 {
8619 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 8620 unsigned int debug_print_threshold;
247f5c4f 8621 char buf[100];
f4dc4d17 8622
247f5c4f 8623 if (per_cu->is_debug_types)
f4dc4d17 8624 {
247f5c4f
DE
8625 struct signatured_type *sig_type =
8626 (struct signatured_type *) per_cu;
8627
8628 sprintf (buf, "TU %s at offset 0x%x",
73be47f5 8629 hex_string (sig_type->signature),
9c541725 8630 to_underlying (per_cu->sect_off));
73be47f5
DE
8631 /* There can be 100s of TUs.
8632 Only print them in verbose mode. */
8633 debug_print_threshold = 2;
f4dc4d17 8634 }
247f5c4f 8635 else
73be47f5 8636 {
9c541725
PA
8637 sprintf (buf, "CU at offset 0x%x",
8638 to_underlying (per_cu->sect_off));
73be47f5
DE
8639 debug_print_threshold = 1;
8640 }
247f5c4f 8641
b4f54984 8642 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8643 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
8644
8645 if (per_cu->is_debug_types)
8646 process_full_type_unit (per_cu, item->pretend_language);
8647 else
8648 process_full_comp_unit (per_cu, item->pretend_language);
8649
b4f54984 8650 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8651 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 8652 }
10b3939b
DJ
8653
8654 item->per_cu->queued = 0;
8655 next_item = item->next;
8656 xfree (item);
8657 }
8658
8659 dwarf2_queue_tail = NULL;
45cfd468 8660
b4f54984 8661 if (dwarf_read_debug)
45cfd468
DE
8662 {
8663 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 8664 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 8665 }
10b3939b
DJ
8666}
8667
8668/* Free all allocated queue entries. This function only releases anything if
8669 an error was thrown; if the queue was processed then it would have been
8670 freed as we went along. */
8671
8672static void
8673dwarf2_release_queue (void *dummy)
8674{
8675 struct dwarf2_queue_item *item, *last;
8676
8677 item = dwarf2_queue;
8678 while (item)
8679 {
8680 /* Anything still marked queued is likely to be in an
8681 inconsistent state, so discard it. */
8682 if (item->per_cu->queued)
8683 {
8684 if (item->per_cu->cu != NULL)
dee91e82 8685 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
8686 item->per_cu->queued = 0;
8687 }
8688
8689 last = item;
8690 item = item->next;
8691 xfree (last);
8692 }
8693
8694 dwarf2_queue = dwarf2_queue_tail = NULL;
8695}
8696
8697/* Read in full symbols for PST, and anything it depends on. */
8698
c906108c 8699static void
fba45db2 8700psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 8701{
10b3939b 8702 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
8703 int i;
8704
95554aad
TT
8705 if (pst->readin)
8706 return;
8707
aaa75496 8708 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
8709 if (!pst->dependencies[i]->readin
8710 && pst->dependencies[i]->user == NULL)
aaa75496
JB
8711 {
8712 /* Inform about additional files that need to be read in. */
8713 if (info_verbose)
8714 {
a3f17187 8715 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
8716 fputs_filtered (" ", gdb_stdout);
8717 wrap_here ("");
8718 fputs_filtered ("and ", gdb_stdout);
8719 wrap_here ("");
8720 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 8721 wrap_here (""); /* Flush output. */
aaa75496
JB
8722 gdb_flush (gdb_stdout);
8723 }
8724 psymtab_to_symtab_1 (pst->dependencies[i]);
8725 }
8726
9a3c8263 8727 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
8728
8729 if (per_cu == NULL)
aaa75496
JB
8730 {
8731 /* It's an include file, no symbols to read for it.
8732 Everything is in the parent symtab. */
8733 pst->readin = 1;
8734 return;
8735 }
c906108c 8736
a0f42c21 8737 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
8738}
8739
dee91e82
DE
8740/* Trivial hash function for die_info: the hash value of a DIE
8741 is its offset in .debug_info for this objfile. */
10b3939b 8742
dee91e82
DE
8743static hashval_t
8744die_hash (const void *item)
10b3939b 8745{
9a3c8263 8746 const struct die_info *die = (const struct die_info *) item;
6502dd73 8747
9c541725 8748 return to_underlying (die->sect_off);
dee91e82 8749}
63d06c5c 8750
dee91e82
DE
8751/* Trivial comparison function for die_info structures: two DIEs
8752 are equal if they have the same offset. */
98bfdba5 8753
dee91e82
DE
8754static int
8755die_eq (const void *item_lhs, const void *item_rhs)
8756{
9a3c8263
SM
8757 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8758 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8759
9c541725 8760 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8761}
c906108c 8762
dee91e82
DE
8763/* die_reader_func for load_full_comp_unit.
8764 This is identical to read_signatured_type_reader,
8765 but is kept separate for now. */
c906108c 8766
dee91e82
DE
8767static void
8768load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8769 const gdb_byte *info_ptr,
dee91e82
DE
8770 struct die_info *comp_unit_die,
8771 int has_children,
8772 void *data)
8773{
8774 struct dwarf2_cu *cu = reader->cu;
9a3c8263 8775 enum language *language_ptr = (enum language *) data;
6caca83c 8776
dee91e82
DE
8777 gdb_assert (cu->die_hash == NULL);
8778 cu->die_hash =
8779 htab_create_alloc_ex (cu->header.length / 12,
8780 die_hash,
8781 die_eq,
8782 NULL,
8783 &cu->comp_unit_obstack,
8784 hashtab_obstack_allocate,
8785 dummy_obstack_deallocate);
e142c38c 8786
dee91e82
DE
8787 if (has_children)
8788 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
8789 &info_ptr, comp_unit_die);
8790 cu->dies = comp_unit_die;
8791 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8792
8793 /* We try not to read any attributes in this function, because not
9cdd5dbd 8794 all CUs needed for references have been loaded yet, and symbol
10b3939b 8795 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8796 or we won't be able to build types correctly.
8797 Similarly, if we do not read the producer, we can not apply
8798 producer-specific interpretation. */
95554aad 8799 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 8800}
10b3939b 8801
dee91e82 8802/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 8803
dee91e82 8804static void
95554aad
TT
8805load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8806 enum language pretend_language)
dee91e82 8807{
3019eac3 8808 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 8809
f4dc4d17
DE
8810 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8811 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
8812}
8813
3da10d80
KS
8814/* Add a DIE to the delayed physname list. */
8815
8816static void
8817add_to_method_list (struct type *type, int fnfield_index, int index,
8818 const char *name, struct die_info *die,
8819 struct dwarf2_cu *cu)
8820{
8821 struct delayed_method_info mi;
8822 mi.type = type;
8823 mi.fnfield_index = fnfield_index;
8824 mi.index = index;
8825 mi.name = name;
8826 mi.die = die;
8827 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
8828}
8829
8830/* A cleanup for freeing the delayed method list. */
8831
8832static void
8833free_delayed_list (void *ptr)
8834{
8835 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
8836 if (cu->method_list != NULL)
8837 {
8838 VEC_free (delayed_method_info, cu->method_list);
8839 cu->method_list = NULL;
8840 }
8841}
8842
3693fdb3
PA
8843/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8844 "const" / "volatile". If so, decrements LEN by the length of the
8845 modifier and return true. Otherwise return false. */
8846
8847template<size_t N>
8848static bool
8849check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8850{
8851 size_t mod_len = sizeof (mod) - 1;
8852 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8853 {
8854 len -= mod_len;
8855 return true;
8856 }
8857 return false;
8858}
8859
3da10d80
KS
8860/* Compute the physnames of any methods on the CU's method list.
8861
8862 The computation of method physnames is delayed in order to avoid the
8863 (bad) condition that one of the method's formal parameters is of an as yet
8864 incomplete type. */
8865
8866static void
8867compute_delayed_physnames (struct dwarf2_cu *cu)
8868{
8869 int i;
8870 struct delayed_method_info *mi;
3693fdb3
PA
8871
8872 /* Only C++ delays computing physnames. */
8873 if (VEC_empty (delayed_method_info, cu->method_list))
8874 return;
8875 gdb_assert (cu->language == language_cplus);
8876
3da10d80
KS
8877 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
8878 {
1d06ead6 8879 const char *physname;
3da10d80
KS
8880 struct fn_fieldlist *fn_flp
8881 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 8882 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
8883 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
8884 = physname ? physname : "";
3693fdb3
PA
8885
8886 /* Since there's no tag to indicate whether a method is a
8887 const/volatile overload, extract that information out of the
8888 demangled name. */
8889 if (physname != NULL)
8890 {
8891 size_t len = strlen (physname);
8892
8893 while (1)
8894 {
8895 if (physname[len] == ')') /* shortcut */
8896 break;
8897 else if (check_modifier (physname, len, " const"))
8898 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
8899 else if (check_modifier (physname, len, " volatile"))
8900 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
8901 else
8902 break;
8903 }
8904 }
3da10d80
KS
8905 }
8906}
8907
a766d390
DE
8908/* Go objects should be embedded in a DW_TAG_module DIE,
8909 and it's not clear if/how imported objects will appear.
8910 To keep Go support simple until that's worked out,
8911 go back through what we've read and create something usable.
8912 We could do this while processing each DIE, and feels kinda cleaner,
8913 but that way is more invasive.
8914 This is to, for example, allow the user to type "p var" or "b main"
8915 without having to specify the package name, and allow lookups
8916 of module.object to work in contexts that use the expression
8917 parser. */
8918
8919static void
8920fixup_go_packaging (struct dwarf2_cu *cu)
8921{
8922 char *package_name = NULL;
8923 struct pending *list;
8924 int i;
8925
8926 for (list = global_symbols; list != NULL; list = list->next)
8927 {
8928 for (i = 0; i < list->nsyms; ++i)
8929 {
8930 struct symbol *sym = list->symbol[i];
8931
8932 if (SYMBOL_LANGUAGE (sym) == language_go
8933 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8934 {
8935 char *this_package_name = go_symbol_package_name (sym);
8936
8937 if (this_package_name == NULL)
8938 continue;
8939 if (package_name == NULL)
8940 package_name = this_package_name;
8941 else
8942 {
8943 if (strcmp (package_name, this_package_name) != 0)
8944 complaint (&symfile_complaints,
8945 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
8946 (symbol_symtab (sym) != NULL
8947 ? symtab_to_filename_for_display
8948 (symbol_symtab (sym))
4262abfb 8949 : objfile_name (cu->objfile)),
a766d390
DE
8950 this_package_name, package_name);
8951 xfree (this_package_name);
8952 }
8953 }
8954 }
8955 }
8956
8957 if (package_name != NULL)
8958 {
8959 struct objfile *objfile = cu->objfile;
34a68019 8960 const char *saved_package_name
224c3ddb
SM
8961 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
8962 package_name,
8963 strlen (package_name));
19f392bc
UW
8964 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8965 saved_package_name);
a766d390
DE
8966 struct symbol *sym;
8967
8968 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8969
e623cf5d 8970 sym = allocate_symbol (objfile);
f85f34ed 8971 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
8972 SYMBOL_SET_NAMES (sym, saved_package_name,
8973 strlen (saved_package_name), 0, objfile);
a766d390
DE
8974 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8975 e.g., "main" finds the "main" module and not C's main(). */
8976 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 8977 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
8978 SYMBOL_TYPE (sym) = type;
8979
8980 add_symbol_to_list (sym, &global_symbols);
8981
8982 xfree (package_name);
8983 }
8984}
8985
95554aad
TT
8986/* Return the symtab for PER_CU. This works properly regardless of
8987 whether we're using the index or psymtabs. */
8988
43f3e411
DE
8989static struct compunit_symtab *
8990get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad
TT
8991{
8992 return (dwarf2_per_objfile->using_index
43f3e411
DE
8993 ? per_cu->v.quick->compunit_symtab
8994 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
8995}
8996
8997/* A helper function for computing the list of all symbol tables
8998 included by PER_CU. */
8999
9000static void
43f3e411 9001recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 9002 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9003 struct dwarf2_per_cu_data *per_cu,
43f3e411 9004 struct compunit_symtab *immediate_parent)
95554aad
TT
9005{
9006 void **slot;
9007 int ix;
43f3e411 9008 struct compunit_symtab *cust;
95554aad
TT
9009 struct dwarf2_per_cu_data *iter;
9010
9011 slot = htab_find_slot (all_children, per_cu, INSERT);
9012 if (*slot != NULL)
9013 {
9014 /* This inclusion and its children have been processed. */
9015 return;
9016 }
9017
9018 *slot = per_cu;
9019 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9020 cust = get_compunit_symtab (per_cu);
9021 if (cust != NULL)
ec94af83
DE
9022 {
9023 /* If this is a type unit only add its symbol table if we haven't
9024 seen it yet (type unit per_cu's can share symtabs). */
9025 if (per_cu->is_debug_types)
9026 {
43f3e411 9027 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9028 if (*slot == NULL)
9029 {
43f3e411
DE
9030 *slot = cust;
9031 VEC_safe_push (compunit_symtab_ptr, *result, cust);
9032 if (cust->user == NULL)
9033 cust->user = immediate_parent;
ec94af83
DE
9034 }
9035 }
9036 else
f9125b6c 9037 {
43f3e411
DE
9038 VEC_safe_push (compunit_symtab_ptr, *result, cust);
9039 if (cust->user == NULL)
9040 cust->user = immediate_parent;
f9125b6c 9041 }
ec94af83 9042 }
95554aad
TT
9043
9044 for (ix = 0;
796a7ff8 9045 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 9046 ++ix)
ec94af83
DE
9047 {
9048 recursively_compute_inclusions (result, all_children,
43f3e411 9049 all_type_symtabs, iter, cust);
ec94af83 9050 }
95554aad
TT
9051}
9052
43f3e411 9053/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9054 PER_CU. */
9055
9056static void
43f3e411 9057compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9058{
f4dc4d17
DE
9059 gdb_assert (! per_cu->is_debug_types);
9060
796a7ff8 9061 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
9062 {
9063 int ix, len;
ec94af83 9064 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
9065 struct compunit_symtab *compunit_symtab_iter;
9066 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 9067 htab_t all_children, all_type_symtabs;
43f3e411 9068 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9069
9070 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9071 if (cust == NULL)
95554aad
TT
9072 return;
9073
9074 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9075 NULL, xcalloc, xfree);
ec94af83
DE
9076 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9077 NULL, xcalloc, xfree);
95554aad
TT
9078
9079 for (ix = 0;
796a7ff8 9080 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 9081 ix, per_cu_iter);
95554aad 9082 ++ix)
ec94af83
DE
9083 {
9084 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 9085 all_type_symtabs, per_cu_iter,
43f3e411 9086 cust);
ec94af83 9087 }
95554aad 9088
ec94af83 9089 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
9090 len = VEC_length (compunit_symtab_ptr, result_symtabs);
9091 cust->includes
8d749320
SM
9092 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
9093 struct compunit_symtab *, len + 1);
95554aad 9094 for (ix = 0;
43f3e411
DE
9095 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
9096 compunit_symtab_iter);
95554aad 9097 ++ix)
43f3e411
DE
9098 cust->includes[ix] = compunit_symtab_iter;
9099 cust->includes[len] = NULL;
95554aad 9100
43f3e411 9101 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 9102 htab_delete (all_children);
ec94af83 9103 htab_delete (all_type_symtabs);
95554aad
TT
9104 }
9105}
9106
9107/* Compute the 'includes' field for the symtabs of all the CUs we just
9108 read. */
9109
9110static void
9111process_cu_includes (void)
9112{
9113 int ix;
9114 struct dwarf2_per_cu_data *iter;
9115
9116 for (ix = 0;
9117 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
9118 ix, iter);
9119 ++ix)
f4dc4d17
DE
9120 {
9121 if (! iter->is_debug_types)
43f3e411 9122 compute_compunit_symtab_includes (iter);
f4dc4d17 9123 }
95554aad
TT
9124
9125 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
9126}
9127
9cdd5dbd 9128/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9129 already been loaded into memory. */
9130
9131static void
95554aad
TT
9132process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9133 enum language pretend_language)
10b3939b 9134{
10b3939b 9135 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 9136 struct objfile *objfile = per_cu->objfile;
3e29f34a 9137 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 9138 CORE_ADDR lowpc, highpc;
43f3e411 9139 struct compunit_symtab *cust;
33c7c59d 9140 struct cleanup *delayed_list_cleanup;
10b3939b 9141 CORE_ADDR baseaddr;
4359dff1 9142 struct block *static_block;
3e29f34a 9143 CORE_ADDR addr;
10b3939b
DJ
9144
9145 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9146
10b3939b 9147 buildsym_init ();
33c7c59d 9148 scoped_free_pendings free_pending;
3da10d80 9149 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
9150
9151 cu->list_in_scope = &file_symbols;
c906108c 9152
95554aad
TT
9153 cu->language = pretend_language;
9154 cu->language_defn = language_def (cu->language);
9155
c906108c 9156 /* Do line number decoding in read_file_scope () */
10b3939b 9157 process_die (cu->dies, cu);
c906108c 9158
a766d390
DE
9159 /* For now fudge the Go package. */
9160 if (cu->language == language_go)
9161 fixup_go_packaging (cu);
9162
3da10d80
KS
9163 /* Now that we have processed all the DIEs in the CU, all the types
9164 should be complete, and it should now be safe to compute all of the
9165 physnames. */
9166 compute_delayed_physnames (cu);
9167 do_cleanups (delayed_list_cleanup);
9168
fae299cd
DC
9169 /* Some compilers don't define a DW_AT_high_pc attribute for the
9170 compilation unit. If the DW_AT_high_pc is missing, synthesize
9171 it, by scanning the DIE's below the compilation unit. */
10b3939b 9172 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9173
3e29f34a
MR
9174 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9175 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9176
9177 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9178 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9179 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9180 addrmap to help ensure it has an accurate map of pc values belonging to
9181 this comp unit. */
9182 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9183
43f3e411
DE
9184 cust = end_symtab_from_static_block (static_block,
9185 SECT_OFF_TEXT (objfile), 0);
c906108c 9186
43f3e411 9187 if (cust != NULL)
c906108c 9188 {
df15bd07 9189 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9190
8be455d7
JK
9191 /* Set symtab language to language from DW_AT_language. If the
9192 compilation is from a C file generated by language preprocessors, do
9193 not set the language if it was already deduced by start_subfile. */
43f3e411 9194 if (!(cu->language == language_c
40e3ad0e 9195 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9196 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9197
9198 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9199 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9200 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9201 there were bugs in prologue debug info, fixed later in GCC-4.5
9202 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9203
9204 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9205 needed, it would be wrong due to missing DW_AT_producer there.
9206
9207 Still one can confuse GDB by using non-standard GCC compilation
9208 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9209 */
ab260dad 9210 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9211 cust->locations_valid = 1;
e0d00bc7
JK
9212
9213 if (gcc_4_minor >= 5)
43f3e411 9214 cust->epilogue_unwind_valid = 1;
96408a79 9215
43f3e411 9216 cust->call_site_htab = cu->call_site_htab;
c906108c 9217 }
9291a0cd
TT
9218
9219 if (dwarf2_per_objfile->using_index)
43f3e411 9220 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
9221 else
9222 {
9223 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 9224 pst->compunit_symtab = cust;
9291a0cd
TT
9225 pst->readin = 1;
9226 }
c906108c 9227
95554aad
TT
9228 /* Push it for inclusion processing later. */
9229 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
f4dc4d17 9230}
45cfd468 9231
f4dc4d17
DE
9232/* Generate full symbol information for type unit PER_CU, whose DIEs have
9233 already been loaded into memory. */
9234
9235static void
9236process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9237 enum language pretend_language)
9238{
9239 struct dwarf2_cu *cu = per_cu->cu;
9240 struct objfile *objfile = per_cu->objfile;
43f3e411 9241 struct compunit_symtab *cust;
33c7c59d 9242 struct cleanup *delayed_list_cleanup;
0186c6a7
DE
9243 struct signatured_type *sig_type;
9244
9245 gdb_assert (per_cu->is_debug_types);
9246 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
9247
9248 buildsym_init ();
33c7c59d 9249 scoped_free_pendings free_pending;
f4dc4d17
DE
9250 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
9251
9252 cu->list_in_scope = &file_symbols;
9253
9254 cu->language = pretend_language;
9255 cu->language_defn = language_def (cu->language);
9256
9257 /* The symbol tables are set up in read_type_unit_scope. */
9258 process_die (cu->dies, cu);
9259
9260 /* For now fudge the Go package. */
9261 if (cu->language == language_go)
9262 fixup_go_packaging (cu);
9263
9264 /* Now that we have processed all the DIEs in the CU, all the types
9265 should be complete, and it should now be safe to compute all of the
9266 physnames. */
9267 compute_delayed_physnames (cu);
9268 do_cleanups (delayed_list_cleanup);
9269
9270 /* TUs share symbol tables.
9271 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9272 of it with end_expandable_symtab. Otherwise, complete the addition of
9273 this TU's symbols to the existing symtab. */
43f3e411 9274 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9275 {
43f3e411
DE
9276 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9277 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9278
43f3e411 9279 if (cust != NULL)
f4dc4d17
DE
9280 {
9281 /* Set symtab language to language from DW_AT_language. If the
9282 compilation is from a C file generated by language preprocessors,
9283 do not set the language if it was already deduced by
9284 start_subfile. */
43f3e411
DE
9285 if (!(cu->language == language_c
9286 && COMPUNIT_FILETABS (cust)->language != language_c))
9287 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9288 }
9289 }
9290 else
9291 {
0ab9ce85 9292 augment_type_symtab ();
43f3e411 9293 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9294 }
9295
9296 if (dwarf2_per_objfile->using_index)
43f3e411 9297 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
9298 else
9299 {
9300 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 9301 pst->compunit_symtab = cust;
f4dc4d17 9302 pst->readin = 1;
45cfd468 9303 }
c906108c
SS
9304}
9305
95554aad
TT
9306/* Process an imported unit DIE. */
9307
9308static void
9309process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9310{
9311 struct attribute *attr;
9312
f4dc4d17
DE
9313 /* For now we don't handle imported units in type units. */
9314 if (cu->per_cu->is_debug_types)
9315 {
9316 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9317 " supported in type units [in module %s]"),
4262abfb 9318 objfile_name (cu->objfile));
f4dc4d17
DE
9319 }
9320
95554aad
TT
9321 attr = dwarf2_attr (die, DW_AT_import, cu);
9322 if (attr != NULL)
9323 {
9c541725
PA
9324 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9325 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9326 dwarf2_per_cu_data *per_cu
9327 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
95554aad 9328
69d751e3 9329 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
9330 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9331 load_full_comp_unit (per_cu, cu->language);
9332
796a7ff8 9333 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
9334 per_cu);
9335 }
9336}
9337
4c8aa72d
PA
9338/* RAII object that represents a process_die scope: i.e.,
9339 starts/finishes processing a DIE. */
9340class process_die_scope
adde2bff 9341{
4c8aa72d
PA
9342public:
9343 process_die_scope (die_info *die, dwarf2_cu *cu)
9344 : m_die (die), m_cu (cu)
9345 {
9346 /* We should only be processing DIEs not already in process. */
9347 gdb_assert (!m_die->in_process);
9348 m_die->in_process = true;
9349 }
8c3cb9fa 9350
4c8aa72d
PA
9351 ~process_die_scope ()
9352 {
9353 m_die->in_process = false;
9354
9355 /* If we're done processing the DIE for the CU that owns the line
9356 header, we don't need the line header anymore. */
9357 if (m_cu->line_header_die_owner == m_die)
9358 {
9359 delete m_cu->line_header;
9360 m_cu->line_header = NULL;
9361 m_cu->line_header_die_owner = NULL;
9362 }
9363 }
9364
9365private:
9366 die_info *m_die;
9367 dwarf2_cu *m_cu;
9368};
adde2bff 9369
c906108c
SS
9370/* Process a die and its children. */
9371
9372static void
e7c27a73 9373process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9374{
4c8aa72d 9375 process_die_scope scope (die, cu);
adde2bff 9376
c906108c
SS
9377 switch (die->tag)
9378 {
9379 case DW_TAG_padding:
9380 break;
9381 case DW_TAG_compile_unit:
95554aad 9382 case DW_TAG_partial_unit:
e7c27a73 9383 read_file_scope (die, cu);
c906108c 9384 break;
348e048f
DE
9385 case DW_TAG_type_unit:
9386 read_type_unit_scope (die, cu);
9387 break;
c906108c 9388 case DW_TAG_subprogram:
c906108c 9389 case DW_TAG_inlined_subroutine:
edb3359d 9390 read_func_scope (die, cu);
c906108c
SS
9391 break;
9392 case DW_TAG_lexical_block:
14898363
L
9393 case DW_TAG_try_block:
9394 case DW_TAG_catch_block:
e7c27a73 9395 read_lexical_block_scope (die, cu);
c906108c 9396 break;
216f72a1 9397 case DW_TAG_call_site:
96408a79
SA
9398 case DW_TAG_GNU_call_site:
9399 read_call_site_scope (die, cu);
9400 break;
c906108c 9401 case DW_TAG_class_type:
680b30c7 9402 case DW_TAG_interface_type:
c906108c
SS
9403 case DW_TAG_structure_type:
9404 case DW_TAG_union_type:
134d01f1 9405 process_structure_scope (die, cu);
c906108c
SS
9406 break;
9407 case DW_TAG_enumeration_type:
134d01f1 9408 process_enumeration_scope (die, cu);
c906108c 9409 break;
134d01f1 9410
f792889a
DJ
9411 /* These dies have a type, but processing them does not create
9412 a symbol or recurse to process the children. Therefore we can
9413 read them on-demand through read_type_die. */
c906108c 9414 case DW_TAG_subroutine_type:
72019c9c 9415 case DW_TAG_set_type:
c906108c 9416 case DW_TAG_array_type:
c906108c 9417 case DW_TAG_pointer_type:
c906108c 9418 case DW_TAG_ptr_to_member_type:
c906108c 9419 case DW_TAG_reference_type:
4297a3f0 9420 case DW_TAG_rvalue_reference_type:
c906108c 9421 case DW_TAG_string_type:
c906108c 9422 break;
134d01f1 9423
c906108c 9424 case DW_TAG_base_type:
a02abb62 9425 case DW_TAG_subrange_type:
cb249c71 9426 case DW_TAG_typedef:
134d01f1
DJ
9427 /* Add a typedef symbol for the type definition, if it has a
9428 DW_AT_name. */
f792889a 9429 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9430 break;
c906108c 9431 case DW_TAG_common_block:
e7c27a73 9432 read_common_block (die, cu);
c906108c
SS
9433 break;
9434 case DW_TAG_common_inclusion:
9435 break;
d9fa45fe 9436 case DW_TAG_namespace:
4d4ec4e5 9437 cu->processing_has_namespace_info = 1;
e7c27a73 9438 read_namespace (die, cu);
d9fa45fe 9439 break;
5d7cb8df 9440 case DW_TAG_module:
4d4ec4e5 9441 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
9442 read_module (die, cu);
9443 break;
d9fa45fe 9444 case DW_TAG_imported_declaration:
74921315
KS
9445 cu->processing_has_namespace_info = 1;
9446 if (read_namespace_alias (die, cu))
9447 break;
9448 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 9449 case DW_TAG_imported_module:
4d4ec4e5 9450 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
9451 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9452 || cu->language != language_fortran))
9453 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
9454 dwarf_tag_name (die->tag));
9455 read_import_statement (die, cu);
d9fa45fe 9456 break;
95554aad
TT
9457
9458 case DW_TAG_imported_unit:
9459 process_imported_unit_die (die, cu);
9460 break;
9461
71a3c369
TT
9462 case DW_TAG_variable:
9463 read_variable (die, cu);
9464 break;
9465
c906108c 9466 default:
e7c27a73 9467 new_symbol (die, NULL, cu);
c906108c
SS
9468 break;
9469 }
9470}
ca69b9e6
DE
9471\f
9472/* DWARF name computation. */
c906108c 9473
94af9270
KS
9474/* A helper function for dwarf2_compute_name which determines whether DIE
9475 needs to have the name of the scope prepended to the name listed in the
9476 die. */
9477
9478static int
9479die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9480{
1c809c68
TT
9481 struct attribute *attr;
9482
94af9270
KS
9483 switch (die->tag)
9484 {
9485 case DW_TAG_namespace:
9486 case DW_TAG_typedef:
9487 case DW_TAG_class_type:
9488 case DW_TAG_interface_type:
9489 case DW_TAG_structure_type:
9490 case DW_TAG_union_type:
9491 case DW_TAG_enumeration_type:
9492 case DW_TAG_enumerator:
9493 case DW_TAG_subprogram:
08a76f8a 9494 case DW_TAG_inlined_subroutine:
94af9270 9495 case DW_TAG_member:
74921315 9496 case DW_TAG_imported_declaration:
94af9270
KS
9497 return 1;
9498
9499 case DW_TAG_variable:
c2b0a229 9500 case DW_TAG_constant:
94af9270
KS
9501 /* We only need to prefix "globally" visible variables. These include
9502 any variable marked with DW_AT_external or any variable that
9503 lives in a namespace. [Variables in anonymous namespaces
9504 require prefixing, but they are not DW_AT_external.] */
9505
9506 if (dwarf2_attr (die, DW_AT_specification, cu))
9507 {
9508 struct dwarf2_cu *spec_cu = cu;
9a619af0 9509
94af9270
KS
9510 return die_needs_namespace (die_specification (die, &spec_cu),
9511 spec_cu);
9512 }
9513
1c809c68 9514 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9515 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9516 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9517 return 0;
9518 /* A variable in a lexical block of some kind does not need a
9519 namespace, even though in C++ such variables may be external
9520 and have a mangled name. */
9521 if (die->parent->tag == DW_TAG_lexical_block
9522 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9523 || die->parent->tag == DW_TAG_catch_block
9524 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9525 return 0;
9526 return 1;
94af9270
KS
9527
9528 default:
9529 return 0;
9530 }
9531}
9532
73b9be8b
KS
9533/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9534 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9535 defined for the given DIE. */
9536
9537static struct attribute *
9538dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9539{
9540 struct attribute *attr;
9541
9542 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9543 if (attr == NULL)
9544 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9545
9546 return attr;
9547}
9548
9549/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9550 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9551 defined for the given DIE. */
9552
9553static const char *
9554dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9555{
9556 const char *linkage_name;
9557
9558 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9559 if (linkage_name == NULL)
9560 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9561
9562 return linkage_name;
9563}
9564
94af9270 9565/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 9566 compute the physname for the object, which include a method's:
9c37b5ae 9567 - formal parameters (C++),
a766d390 9568 - receiver type (Go),
a766d390
DE
9569
9570 The term "physname" is a bit confusing.
9571 For C++, for example, it is the demangled name.
9572 For Go, for example, it's the mangled name.
94af9270 9573
af6b7be1
JB
9574 For Ada, return the DIE's linkage name rather than the fully qualified
9575 name. PHYSNAME is ignored..
9576
94af9270
KS
9577 The result is allocated on the objfile_obstack and canonicalized. */
9578
9579static const char *
15d034d0
TT
9580dwarf2_compute_name (const char *name,
9581 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
9582 int physname)
9583{
bb5ed363
DE
9584 struct objfile *objfile = cu->objfile;
9585
94af9270
KS
9586 if (name == NULL)
9587 name = dwarf2_name (die, cu);
9588
2ee7123e
DE
9589 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9590 but otherwise compute it by typename_concat inside GDB.
9591 FIXME: Actually this is not really true, or at least not always true.
9592 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
9593 Fortran names because there is no mangling standard. So new_symbol_full
9594 will set the demangled name to the result of dwarf2_full_name, and it is
9595 the demangled name that GDB uses if it exists. */
f55ee35c
JK
9596 if (cu->language == language_ada
9597 || (cu->language == language_fortran && physname))
9598 {
9599 /* For Ada unit, we prefer the linkage name over the name, as
9600 the former contains the exported name, which the user expects
9601 to be able to reference. Ideally, we want the user to be able
9602 to reference this entity using either natural or linkage name,
9603 but we haven't started looking at this enhancement yet. */
73b9be8b 9604 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 9605
2ee7123e
DE
9606 if (linkage_name != NULL)
9607 return linkage_name;
f55ee35c
JK
9608 }
9609
94af9270
KS
9610 /* These are the only languages we know how to qualify names in. */
9611 if (name != NULL
9c37b5ae 9612 && (cu->language == language_cplus
c44af4eb
TT
9613 || cu->language == language_fortran || cu->language == language_d
9614 || cu->language == language_rust))
94af9270
KS
9615 {
9616 if (die_needs_namespace (die, cu))
9617 {
9618 long length;
0d5cff50 9619 const char *prefix;
34a68019 9620 const char *canonical_name = NULL;
94af9270 9621
d7e74731
PA
9622 string_file buf;
9623
94af9270 9624 prefix = determine_prefix (die, cu);
94af9270
KS
9625 if (*prefix != '\0')
9626 {
f55ee35c
JK
9627 char *prefixed_name = typename_concat (NULL, prefix, name,
9628 physname, cu);
9a619af0 9629
d7e74731 9630 buf.puts (prefixed_name);
94af9270
KS
9631 xfree (prefixed_name);
9632 }
9633 else
d7e74731 9634 buf.puts (name);
94af9270 9635
98bfdba5
PA
9636 /* Template parameters may be specified in the DIE's DW_AT_name, or
9637 as children with DW_TAG_template_type_param or
9638 DW_TAG_value_type_param. If the latter, add them to the name
9639 here. If the name already has template parameters, then
9640 skip this step; some versions of GCC emit both, and
9641 it is more efficient to use the pre-computed name.
9642
9643 Something to keep in mind about this process: it is very
9644 unlikely, or in some cases downright impossible, to produce
9645 something that will match the mangled name of a function.
9646 If the definition of the function has the same debug info,
9647 we should be able to match up with it anyway. But fallbacks
9648 using the minimal symbol, for instance to find a method
9649 implemented in a stripped copy of libstdc++, will not work.
9650 If we do not have debug info for the definition, we will have to
9651 match them up some other way.
9652
9653 When we do name matching there is a related problem with function
9654 templates; two instantiated function templates are allowed to
9655 differ only by their return types, which we do not add here. */
9656
9657 if (cu->language == language_cplus && strchr (name, '<') == NULL)
9658 {
9659 struct attribute *attr;
9660 struct die_info *child;
9661 int first = 1;
9662
9663 die->building_fullname = 1;
9664
9665 for (child = die->child; child != NULL; child = child->sibling)
9666 {
9667 struct type *type;
12df843f 9668 LONGEST value;
d521ce57 9669 const gdb_byte *bytes;
98bfdba5
PA
9670 struct dwarf2_locexpr_baton *baton;
9671 struct value *v;
9672
9673 if (child->tag != DW_TAG_template_type_param
9674 && child->tag != DW_TAG_template_value_param)
9675 continue;
9676
9677 if (first)
9678 {
d7e74731 9679 buf.puts ("<");
98bfdba5
PA
9680 first = 0;
9681 }
9682 else
d7e74731 9683 buf.puts (", ");
98bfdba5
PA
9684
9685 attr = dwarf2_attr (child, DW_AT_type, cu);
9686 if (attr == NULL)
9687 {
9688 complaint (&symfile_complaints,
9689 _("template parameter missing DW_AT_type"));
d7e74731 9690 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
9691 continue;
9692 }
9693 type = die_type (child, cu);
9694
9695 if (child->tag == DW_TAG_template_type_param)
9696 {
d7e74731 9697 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
9698 continue;
9699 }
9700
9701 attr = dwarf2_attr (child, DW_AT_const_value, cu);
9702 if (attr == NULL)
9703 {
9704 complaint (&symfile_complaints,
3e43a32a
MS
9705 _("template parameter missing "
9706 "DW_AT_const_value"));
d7e74731 9707 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
9708 continue;
9709 }
9710
9711 dwarf2_const_value_attr (attr, type, name,
9712 &cu->comp_unit_obstack, cu,
9713 &value, &bytes, &baton);
9714
9715 if (TYPE_NOSIGN (type))
9716 /* GDB prints characters as NUMBER 'CHAR'. If that's
9717 changed, this can use value_print instead. */
d7e74731 9718 c_printchar (value, type, &buf);
98bfdba5
PA
9719 else
9720 {
9721 struct value_print_options opts;
9722
9723 if (baton != NULL)
9724 v = dwarf2_evaluate_loc_desc (type, NULL,
9725 baton->data,
9726 baton->size,
9727 baton->per_cu);
9728 else if (bytes != NULL)
9729 {
9730 v = allocate_value (type);
9731 memcpy (value_contents_writeable (v), bytes,
9732 TYPE_LENGTH (type));
9733 }
9734 else
9735 v = value_from_longest (type, value);
9736
3e43a32a
MS
9737 /* Specify decimal so that we do not depend on
9738 the radix. */
98bfdba5
PA
9739 get_formatted_print_options (&opts, 'd');
9740 opts.raw = 1;
d7e74731 9741 value_print (v, &buf, &opts);
98bfdba5
PA
9742 release_value (v);
9743 value_free (v);
9744 }
9745 }
9746
9747 die->building_fullname = 0;
9748
9749 if (!first)
9750 {
9751 /* Close the argument list, with a space if necessary
9752 (nested templates). */
d7e74731
PA
9753 if (!buf.empty () && buf.string ().back () == '>')
9754 buf.puts (" >");
98bfdba5 9755 else
d7e74731 9756 buf.puts (">");
98bfdba5
PA
9757 }
9758 }
9759
9c37b5ae 9760 /* For C++ methods, append formal parameter type
94af9270 9761 information, if PHYSNAME. */
6e70227d 9762
94af9270 9763 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 9764 && cu->language == language_cplus)
94af9270
KS
9765 {
9766 struct type *type = read_type_die (die, cu);
9767
d7e74731 9768 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 9769 &type_print_raw_options);
94af9270 9770
9c37b5ae 9771 if (cu->language == language_cplus)
94af9270 9772 {
60430eff
DJ
9773 /* Assume that an artificial first parameter is
9774 "this", but do not crash if it is not. RealView
9775 marks unnamed (and thus unused) parameters as
9776 artificial; there is no way to differentiate
9777 the two cases. */
94af9270
KS
9778 if (TYPE_NFIELDS (type) > 0
9779 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 9780 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
9781 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
9782 0))))
d7e74731 9783 buf.puts (" const");
94af9270
KS
9784 }
9785 }
9786
d7e74731 9787 const std::string &intermediate_name = buf.string ();
94af9270
KS
9788
9789 if (cu->language == language_cplus)
34a68019 9790 canonical_name
322a8516 9791 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
9792 &objfile->per_bfd->storage_obstack);
9793
9794 /* If we only computed INTERMEDIATE_NAME, or if
9795 INTERMEDIATE_NAME is already canonical, then we need to
9796 copy it to the appropriate obstack. */
322a8516 9797 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
9798 name = ((const char *)
9799 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
9800 intermediate_name.c_str (),
9801 intermediate_name.length ()));
34a68019
TT
9802 else
9803 name = canonical_name;
94af9270
KS
9804 }
9805 }
9806
9807 return name;
9808}
9809
0114d602
DJ
9810/* Return the fully qualified name of DIE, based on its DW_AT_name.
9811 If scope qualifiers are appropriate they will be added. The result
34a68019 9812 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
9813 not have a name. NAME may either be from a previous call to
9814 dwarf2_name or NULL.
9815
9c37b5ae 9816 The output string will be canonicalized (if C++). */
0114d602
DJ
9817
9818static const char *
15d034d0 9819dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 9820{
94af9270
KS
9821 return dwarf2_compute_name (name, die, cu, 0);
9822}
0114d602 9823
94af9270
KS
9824/* Construct a physname for the given DIE in CU. NAME may either be
9825 from a previous call to dwarf2_name or NULL. The result will be
9826 allocated on the objfile_objstack or NULL if the DIE does not have a
9827 name.
0114d602 9828
9c37b5ae 9829 The output string will be canonicalized (if C++). */
0114d602 9830
94af9270 9831static const char *
15d034d0 9832dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 9833{
bb5ed363 9834 struct objfile *objfile = cu->objfile;
900e11f9 9835 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
9836 int need_copy = 1;
9837
9838 /* In this case dwarf2_compute_name is just a shortcut not building anything
9839 on its own. */
9840 if (!die_needs_namespace (die, cu))
9841 return dwarf2_compute_name (name, die, cu, 1);
9842
73b9be8b 9843 mangled = dw2_linkage_name (die, cu);
900e11f9 9844
e98c9e7c
TT
9845 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9846 See https://github.com/rust-lang/rust/issues/32925. */
9847 if (cu->language == language_rust && mangled != NULL
9848 && strchr (mangled, '{') != NULL)
9849 mangled = NULL;
9850
900e11f9
JK
9851 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9852 has computed. */
791afaa2 9853 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 9854 if (mangled != NULL)
900e11f9 9855 {
900e11f9
JK
9856 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
9857 type. It is easier for GDB users to search for such functions as
9858 `name(params)' than `long name(params)'. In such case the minimal
9859 symbol names do not match the full symbol names but for template
9860 functions there is never a need to look up their definition from their
9861 declaration so the only disadvantage remains the minimal symbol
9862 variant `long name(params)' does not have the proper inferior type.
9863 */
9864
a766d390
DE
9865 if (cu->language == language_go)
9866 {
9867 /* This is a lie, but we already lie to the caller new_symbol_full.
9868 new_symbol_full assumes we return the mangled name.
9869 This just undoes that lie until things are cleaned up. */
a766d390
DE
9870 }
9871 else
9872 {
791afaa2
TT
9873 demangled.reset (gdb_demangle (mangled,
9874 (DMGL_PARAMS | DMGL_ANSI
9875 | DMGL_RET_DROP)));
a766d390 9876 }
900e11f9 9877 if (demangled)
791afaa2 9878 canon = demangled.get ();
900e11f9
JK
9879 else
9880 {
9881 canon = mangled;
9882 need_copy = 0;
9883 }
9884 }
9885
9886 if (canon == NULL || check_physname)
9887 {
9888 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9889
9890 if (canon != NULL && strcmp (physname, canon) != 0)
9891 {
9892 /* It may not mean a bug in GDB. The compiler could also
9893 compute DW_AT_linkage_name incorrectly. But in such case
9894 GDB would need to be bug-to-bug compatible. */
9895
9896 complaint (&symfile_complaints,
9897 _("Computed physname <%s> does not match demangled <%s> "
9898 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9c541725 9899 physname, canon, mangled, to_underlying (die->sect_off),
4262abfb 9900 objfile_name (objfile));
900e11f9
JK
9901
9902 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9903 is available here - over computed PHYSNAME. It is safer
9904 against both buggy GDB and buggy compilers. */
9905
9906 retval = canon;
9907 }
9908 else
9909 {
9910 retval = physname;
9911 need_copy = 0;
9912 }
9913 }
9914 else
9915 retval = canon;
9916
9917 if (need_copy)
224c3ddb
SM
9918 retval = ((const char *)
9919 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9920 retval, strlen (retval)));
900e11f9 9921
900e11f9 9922 return retval;
0114d602
DJ
9923}
9924
74921315
KS
9925/* Inspect DIE in CU for a namespace alias. If one exists, record
9926 a new symbol for it.
9927
9928 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9929
9930static int
9931read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
9932{
9933 struct attribute *attr;
9934
9935 /* If the die does not have a name, this is not a namespace
9936 alias. */
9937 attr = dwarf2_attr (die, DW_AT_name, cu);
9938 if (attr != NULL)
9939 {
9940 int num;
9941 struct die_info *d = die;
9942 struct dwarf2_cu *imported_cu = cu;
9943
9944 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9945 keep inspecting DIEs until we hit the underlying import. */
9946#define MAX_NESTED_IMPORTED_DECLARATIONS 100
9947 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9948 {
9949 attr = dwarf2_attr (d, DW_AT_import, cu);
9950 if (attr == NULL)
9951 break;
9952
9953 d = follow_die_ref (d, attr, &imported_cu);
9954 if (d->tag != DW_TAG_imported_declaration)
9955 break;
9956 }
9957
9958 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
9959 {
9960 complaint (&symfile_complaints,
9961 _("DIE at 0x%x has too many recursively imported "
9c541725 9962 "declarations"), to_underlying (d->sect_off));
74921315
KS
9963 return 0;
9964 }
9965
9966 if (attr != NULL)
9967 {
9968 struct type *type;
9c541725 9969 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 9970
9c541725 9971 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
9972 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
9973 {
9974 /* This declaration is a global namespace alias. Add
9975 a symbol for it whose type is the aliased namespace. */
9976 new_symbol (die, type, cu);
9977 return 1;
9978 }
9979 }
9980 }
9981
9982 return 0;
9983}
9984
22cee43f
PMR
9985/* Return the using directives repository (global or local?) to use in the
9986 current context for LANGUAGE.
9987
9988 For Ada, imported declarations can materialize renamings, which *may* be
9989 global. However it is impossible (for now?) in DWARF to distinguish
9990 "external" imported declarations and "static" ones. As all imported
9991 declarations seem to be static in all other languages, make them all CU-wide
9992 global only in Ada. */
9993
9994static struct using_direct **
9995using_directives (enum language language)
9996{
9997 if (language == language_ada && context_stack_depth == 0)
9998 return &global_using_directives;
9999 else
10000 return &local_using_directives;
10001}
10002
27aa8d6a
SW
10003/* Read the import statement specified by the given die and record it. */
10004
10005static void
10006read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10007{
bb5ed363 10008 struct objfile *objfile = cu->objfile;
27aa8d6a 10009 struct attribute *import_attr;
32019081 10010 struct die_info *imported_die, *child_die;
de4affc9 10011 struct dwarf2_cu *imported_cu;
27aa8d6a 10012 const char *imported_name;
794684b6 10013 const char *imported_name_prefix;
13387711
SW
10014 const char *canonical_name;
10015 const char *import_alias;
10016 const char *imported_declaration = NULL;
794684b6 10017 const char *import_prefix;
eb1e02fd 10018 std::vector<const char *> excludes;
13387711 10019
27aa8d6a
SW
10020 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10021 if (import_attr == NULL)
10022 {
10023 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
10024 dwarf_tag_name (die->tag));
10025 return;
10026 }
10027
de4affc9
CC
10028 imported_cu = cu;
10029 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10030 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10031 if (imported_name == NULL)
10032 {
10033 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10034
10035 The import in the following code:
10036 namespace A
10037 {
10038 typedef int B;
10039 }
10040
10041 int main ()
10042 {
10043 using A::B;
10044 B b;
10045 return b;
10046 }
10047
10048 ...
10049 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10050 <52> DW_AT_decl_file : 1
10051 <53> DW_AT_decl_line : 6
10052 <54> DW_AT_import : <0x75>
10053 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10054 <59> DW_AT_name : B
10055 <5b> DW_AT_decl_file : 1
10056 <5c> DW_AT_decl_line : 2
10057 <5d> DW_AT_type : <0x6e>
10058 ...
10059 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10060 <76> DW_AT_byte_size : 4
10061 <77> DW_AT_encoding : 5 (signed)
10062
10063 imports the wrong die ( 0x75 instead of 0x58 ).
10064 This case will be ignored until the gcc bug is fixed. */
10065 return;
10066 }
10067
82856980
SW
10068 /* Figure out the local name after import. */
10069 import_alias = dwarf2_name (die, cu);
27aa8d6a 10070
794684b6
SW
10071 /* Figure out where the statement is being imported to. */
10072 import_prefix = determine_prefix (die, cu);
10073
10074 /* Figure out what the scope of the imported die is and prepend it
10075 to the name of the imported die. */
de4affc9 10076 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10077
f55ee35c
JK
10078 if (imported_die->tag != DW_TAG_namespace
10079 && imported_die->tag != DW_TAG_module)
794684b6 10080 {
13387711
SW
10081 imported_declaration = imported_name;
10082 canonical_name = imported_name_prefix;
794684b6 10083 }
13387711 10084 else if (strlen (imported_name_prefix) > 0)
12aaed36 10085 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10086 imported_name_prefix,
10087 (cu->language == language_d ? "." : "::"),
10088 imported_name, (char *) NULL);
13387711
SW
10089 else
10090 canonical_name = imported_name;
794684b6 10091
32019081
JK
10092 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10093 for (child_die = die->child; child_die && child_die->tag;
10094 child_die = sibling_die (child_die))
10095 {
10096 /* DWARF-4: A Fortran use statement with a “rename list” may be
10097 represented by an imported module entry with an import attribute
10098 referring to the module and owned entries corresponding to those
10099 entities that are renamed as part of being imported. */
10100
10101 if (child_die->tag != DW_TAG_imported_declaration)
10102 {
10103 complaint (&symfile_complaints,
10104 _("child DW_TAG_imported_declaration expected "
10105 "- DIE at 0x%x [in module %s]"),
9c541725 10106 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
10107 continue;
10108 }
10109
10110 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10111 if (import_attr == NULL)
10112 {
10113 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
10114 dwarf_tag_name (child_die->tag));
10115 continue;
10116 }
10117
10118 imported_cu = cu;
10119 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10120 &imported_cu);
10121 imported_name = dwarf2_name (imported_die, imported_cu);
10122 if (imported_name == NULL)
10123 {
10124 complaint (&symfile_complaints,
10125 _("child DW_TAG_imported_declaration has unknown "
10126 "imported name - DIE at 0x%x [in module %s]"),
9c541725 10127 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
10128 continue;
10129 }
10130
eb1e02fd 10131 excludes.push_back (imported_name);
32019081
JK
10132
10133 process_die (child_die, cu);
10134 }
10135
22cee43f
PMR
10136 add_using_directive (using_directives (cu->language),
10137 import_prefix,
10138 canonical_name,
10139 import_alias,
10140 imported_declaration,
10141 excludes,
10142 0,
10143 &objfile->objfile_obstack);
27aa8d6a
SW
10144}
10145
5230b05a
WT
10146/* ICC<14 does not output the required DW_AT_declaration on incomplete
10147 types, but gives them a size of zero. Starting with version 14,
10148 ICC is compatible with GCC. */
10149
10150static int
10151producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10152{
10153 if (!cu->checked_producer)
10154 check_producer (cu);
10155
10156 return cu->producer_is_icc_lt_14;
10157}
10158
1b80a9fa
JK
10159/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10160 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10161 this, it was first present in GCC release 4.3.0. */
10162
10163static int
10164producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10165{
10166 if (!cu->checked_producer)
10167 check_producer (cu);
10168
10169 return cu->producer_is_gcc_lt_4_3;
10170}
10171
d721ba37
PA
10172static file_and_directory
10173find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10174{
d721ba37
PA
10175 file_and_directory res;
10176
9291a0cd
TT
10177 /* Find the filename. Do not use dwarf2_name here, since the filename
10178 is not a source language identifier. */
d721ba37
PA
10179 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10180 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10181
d721ba37
PA
10182 if (res.comp_dir == NULL
10183 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10184 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10185 {
d721ba37
PA
10186 res.comp_dir_storage = ldirname (res.name);
10187 if (!res.comp_dir_storage.empty ())
10188 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10189 }
d721ba37 10190 if (res.comp_dir != NULL)
9291a0cd
TT
10191 {
10192 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10193 directory, get rid of it. */
d721ba37 10194 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10195
d721ba37
PA
10196 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10197 res.comp_dir = cp + 1;
9291a0cd
TT
10198 }
10199
d721ba37
PA
10200 if (res.name == NULL)
10201 res.name = "<unknown>";
10202
10203 return res;
9291a0cd
TT
10204}
10205
f4dc4d17
DE
10206/* Handle DW_AT_stmt_list for a compilation unit.
10207 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10208 COMP_DIR is the compilation directory. LOWPC is passed to
10209 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10210
10211static void
10212handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10213 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10214{
527f3840 10215 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 10216 struct attribute *attr;
527f3840
JK
10217 struct line_header line_header_local;
10218 hashval_t line_header_local_hash;
10219 unsigned u;
10220 void **slot;
10221 int decode_mapping;
2ab95328 10222
f4dc4d17
DE
10223 gdb_assert (! cu->per_cu->is_debug_types);
10224
2ab95328 10225 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10226 if (attr == NULL)
10227 return;
10228
9c541725 10229 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10230
10231 /* The line header hash table is only created if needed (it exists to
10232 prevent redundant reading of the line table for partial_units).
10233 If we're given a partial_unit, we'll need it. If we're given a
10234 compile_unit, then use the line header hash table if it's already
10235 created, but don't create one just yet. */
10236
10237 if (dwarf2_per_objfile->line_header_hash == NULL
10238 && die->tag == DW_TAG_partial_unit)
2ab95328 10239 {
527f3840
JK
10240 dwarf2_per_objfile->line_header_hash
10241 = htab_create_alloc_ex (127, line_header_hash_voidp,
10242 line_header_eq_voidp,
10243 free_line_header_voidp,
10244 &objfile->objfile_obstack,
10245 hashtab_obstack_allocate,
10246 dummy_obstack_deallocate);
10247 }
2ab95328 10248
9c541725 10249 line_header_local.sect_off = line_offset;
527f3840
JK
10250 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10251 line_header_local_hash = line_header_hash (&line_header_local);
10252 if (dwarf2_per_objfile->line_header_hash != NULL)
10253 {
10254 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
10255 &line_header_local,
10256 line_header_local_hash, NO_INSERT);
10257
10258 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10259 is not present in *SLOT (since if there is something in *SLOT then
10260 it will be for a partial_unit). */
10261 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10262 {
527f3840 10263 gdb_assert (*slot != NULL);
9a3c8263 10264 cu->line_header = (struct line_header *) *slot;
527f3840 10265 return;
dee91e82 10266 }
2ab95328 10267 }
527f3840
JK
10268
10269 /* dwarf_decode_line_header does not yet provide sufficient information.
10270 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10271 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10272 if (lh == NULL)
527f3840 10273 return;
4c8aa72d
PA
10274
10275 cu->line_header = lh.release ();
10276 cu->line_header_die_owner = die;
527f3840
JK
10277
10278 if (dwarf2_per_objfile->line_header_hash == NULL)
10279 slot = NULL;
10280 else
10281 {
10282 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
10283 &line_header_local,
10284 line_header_local_hash, INSERT);
10285 gdb_assert (slot != NULL);
10286 }
10287 if (slot != NULL && *slot == NULL)
10288 {
10289 /* This newly decoded line number information unit will be owned
10290 by line_header_hash hash table. */
10291 *slot = cu->line_header;
4c8aa72d 10292 cu->line_header_die_owner = NULL;
527f3840
JK
10293 }
10294 else
10295 {
10296 /* We cannot free any current entry in (*slot) as that struct line_header
10297 may be already used by multiple CUs. Create only temporary decoded
10298 line_header for this CU - it may happen at most once for each line
10299 number information unit. And if we're not using line_header_hash
10300 then this is what we want as well. */
10301 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10302 }
10303 decode_mapping = (die->tag != DW_TAG_partial_unit);
10304 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10305 decode_mapping);
fff8551c 10306
2ab95328
TT
10307}
10308
95554aad 10309/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10310
c906108c 10311static void
e7c27a73 10312read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10313{
dee91e82 10314 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10315 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 10316 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10317 CORE_ADDR highpc = ((CORE_ADDR) 0);
10318 struct attribute *attr;
c906108c 10319 struct die_info *child_die;
e142c38c 10320 CORE_ADDR baseaddr;
6e70227d 10321
e142c38c 10322 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10323
fae299cd 10324 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10325
10326 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10327 from finish_block. */
2acceee2 10328 if (lowpc == ((CORE_ADDR) -1))
c906108c 10329 lowpc = highpc;
3e29f34a 10330 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10331
d721ba37 10332 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10333
95554aad 10334 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 10335
f4b8a18d
KW
10336 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10337 standardised yet. As a workaround for the language detection we fall
10338 back to the DW_AT_producer string. */
10339 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10340 cu->language = language_opencl;
10341
3019eac3
DE
10342 /* Similar hack for Go. */
10343 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10344 set_cu_language (DW_LANG_Go, cu);
10345
d721ba37 10346 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10347
10348 /* Decode line number information if present. We do this before
10349 processing child DIEs, so that the line header table is available
10350 for DW_AT_decl_file. */
d721ba37 10351 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10352
10353 /* Process all dies in compilation unit. */
10354 if (die->child != NULL)
10355 {
10356 child_die = die->child;
10357 while (child_die && child_die->tag)
10358 {
10359 process_die (child_die, cu);
10360 child_die = sibling_die (child_die);
10361 }
10362 }
10363
10364 /* Decode macro information, if present. Dwarf 2 macro information
10365 refers to information in the line number info statement program
10366 header, so we can only read it if we've read the header
10367 successfully. */
0af92d60
JK
10368 attr = dwarf2_attr (die, DW_AT_macros, cu);
10369 if (attr == NULL)
10370 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10371 if (attr && cu->line_header)
10372 {
10373 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10374 complaint (&symfile_complaints,
0af92d60 10375 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10376
43f3e411 10377 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10378 }
10379 else
10380 {
10381 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10382 if (attr && cu->line_header)
10383 {
10384 unsigned int macro_offset = DW_UNSND (attr);
10385
43f3e411 10386 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10387 }
10388 }
3019eac3
DE
10389}
10390
f4dc4d17
DE
10391/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
10392 Create the set of symtabs used by this TU, or if this TU is sharing
10393 symtabs with another TU and the symtabs have already been created
10394 then restore those symtabs in the line header.
10395 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
10396
10397static void
f4dc4d17 10398setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 10399{
f4dc4d17
DE
10400 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
10401 struct type_unit_group *tu_group;
10402 int first_time;
3019eac3 10403 struct attribute *attr;
9c541725 10404 unsigned int i;
0186c6a7 10405 struct signatured_type *sig_type;
3019eac3 10406
f4dc4d17 10407 gdb_assert (per_cu->is_debug_types);
0186c6a7 10408 sig_type = (struct signatured_type *) per_cu;
3019eac3 10409
f4dc4d17 10410 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 10411
f4dc4d17 10412 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10413 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
10414 if (sig_type->type_unit_group == NULL)
10415 sig_type->type_unit_group = get_type_unit_group (cu, attr);
10416 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10417
10418 /* If we've already processed this stmt_list there's no real need to
10419 do it again, we could fake it and just recreate the part we need
10420 (file name,index -> symtab mapping). If data shows this optimization
10421 is useful we can do it then. */
43f3e411 10422 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
10423
10424 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10425 debug info. */
fff8551c 10426 line_header_up lh;
f4dc4d17 10427 if (attr != NULL)
3019eac3 10428 {
9c541725 10429 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
10430 lh = dwarf_decode_line_header (line_offset, cu);
10431 }
10432 if (lh == NULL)
10433 {
10434 if (first_time)
10435 dwarf2_start_symtab (cu, "", NULL, 0);
10436 else
10437 {
10438 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 10439 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 10440 }
f4dc4d17 10441 return;
3019eac3
DE
10442 }
10443
4c8aa72d
PA
10444 cu->line_header = lh.release ();
10445 cu->line_header_die_owner = die;
3019eac3 10446
f4dc4d17
DE
10447 if (first_time)
10448 {
43f3e411 10449 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 10450
1fd60fc0
DE
10451 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10452 still initializing it, and our caller (a few levels up)
10453 process_full_type_unit still needs to know if this is the first
10454 time. */
10455
4c8aa72d
PA
10456 tu_group->num_symtabs = cu->line_header->file_names.size ();
10457 tu_group->symtabs = XNEWVEC (struct symtab *,
10458 cu->line_header->file_names.size ());
3019eac3 10459
4c8aa72d 10460 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 10461 {
4c8aa72d 10462 file_entry &fe = cu->line_header->file_names[i];
3019eac3 10463
4c8aa72d 10464 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 10465
f4dc4d17
DE
10466 if (current_subfile->symtab == NULL)
10467 {
4c8aa72d
PA
10468 /* NOTE: start_subfile will recognize when it's been
10469 passed a file it has already seen. So we can't
10470 assume there's a simple mapping from
10471 cu->line_header->file_names to subfiles, plus
10472 cu->line_header->file_names may contain dups. */
43f3e411
DE
10473 current_subfile->symtab
10474 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
10475 }
10476
8c43009f
PA
10477 fe.symtab = current_subfile->symtab;
10478 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
10479 }
10480 }
10481 else
3019eac3 10482 {
0ab9ce85 10483 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 10484
4c8aa72d 10485 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 10486 {
4c8aa72d 10487 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 10488
4c8aa72d 10489 fe.symtab = tu_group->symtabs[i];
f4dc4d17 10490 }
3019eac3
DE
10491 }
10492
f4dc4d17
DE
10493 /* The main symtab is allocated last. Type units don't have DW_AT_name
10494 so they don't have a "real" (so to speak) symtab anyway.
10495 There is later code that will assign the main symtab to all symbols
10496 that don't have one. We need to handle the case of a symbol with a
10497 missing symtab (DW_AT_decl_file) anyway. */
10498}
3019eac3 10499
f4dc4d17
DE
10500/* Process DW_TAG_type_unit.
10501 For TUs we want to skip the first top level sibling if it's not the
10502 actual type being defined by this TU. In this case the first top
10503 level sibling is there to provide context only. */
3019eac3 10504
f4dc4d17
DE
10505static void
10506read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10507{
10508 struct die_info *child_die;
3019eac3 10509
f4dc4d17
DE
10510 prepare_one_comp_unit (cu, die, language_minimal);
10511
10512 /* Initialize (or reinitialize) the machinery for building symtabs.
10513 We do this before processing child DIEs, so that the line header table
10514 is available for DW_AT_decl_file. */
10515 setup_type_unit_groups (die, cu);
10516
10517 if (die->child != NULL)
10518 {
10519 child_die = die->child;
10520 while (child_die && child_die->tag)
10521 {
10522 process_die (child_die, cu);
10523 child_die = sibling_die (child_die);
10524 }
10525 }
3019eac3
DE
10526}
10527\f
80626a55
DE
10528/* DWO/DWP files.
10529
10530 http://gcc.gnu.org/wiki/DebugFission
10531 http://gcc.gnu.org/wiki/DebugFissionDWP
10532
10533 To simplify handling of both DWO files ("object" files with the DWARF info)
10534 and DWP files (a file with the DWOs packaged up into one file), we treat
10535 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10536
10537static hashval_t
10538hash_dwo_file (const void *item)
10539{
9a3c8263 10540 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10541 hashval_t hash;
3019eac3 10542
a2ce51a0
DE
10543 hash = htab_hash_string (dwo_file->dwo_name);
10544 if (dwo_file->comp_dir != NULL)
10545 hash += htab_hash_string (dwo_file->comp_dir);
10546 return hash;
3019eac3
DE
10547}
10548
10549static int
10550eq_dwo_file (const void *item_lhs, const void *item_rhs)
10551{
9a3c8263
SM
10552 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10553 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 10554
a2ce51a0
DE
10555 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10556 return 0;
10557 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10558 return lhs->comp_dir == rhs->comp_dir;
10559 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
10560}
10561
10562/* Allocate a hash table for DWO files. */
10563
10564static htab_t
10565allocate_dwo_file_hash_table (void)
10566{
10567 struct objfile *objfile = dwarf2_per_objfile->objfile;
10568
10569 return htab_create_alloc_ex (41,
10570 hash_dwo_file,
10571 eq_dwo_file,
10572 NULL,
10573 &objfile->objfile_obstack,
10574 hashtab_obstack_allocate,
10575 dummy_obstack_deallocate);
10576}
10577
80626a55
DE
10578/* Lookup DWO file DWO_NAME. */
10579
10580static void **
0ac5b59e 10581lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
10582{
10583 struct dwo_file find_entry;
10584 void **slot;
10585
10586 if (dwarf2_per_objfile->dwo_files == NULL)
10587 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
10588
10589 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
10590 find_entry.dwo_name = dwo_name;
10591 find_entry.comp_dir = comp_dir;
80626a55
DE
10592 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
10593
10594 return slot;
10595}
10596
3019eac3
DE
10597static hashval_t
10598hash_dwo_unit (const void *item)
10599{
9a3c8263 10600 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
10601
10602 /* This drops the top 32 bits of the id, but is ok for a hash. */
10603 return dwo_unit->signature;
10604}
10605
10606static int
10607eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10608{
9a3c8263
SM
10609 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10610 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
10611
10612 /* The signature is assumed to be unique within the DWO file.
10613 So while object file CU dwo_id's always have the value zero,
10614 that's OK, assuming each object file DWO file has only one CU,
10615 and that's the rule for now. */
10616 return lhs->signature == rhs->signature;
10617}
10618
10619/* Allocate a hash table for DWO CUs,TUs.
10620 There is one of these tables for each of CUs,TUs for each DWO file. */
10621
10622static htab_t
10623allocate_dwo_unit_table (struct objfile *objfile)
10624{
10625 /* Start out with a pretty small number.
10626 Generally DWO files contain only one CU and maybe some TUs. */
10627 return htab_create_alloc_ex (3,
10628 hash_dwo_unit,
10629 eq_dwo_unit,
10630 NULL,
10631 &objfile->objfile_obstack,
10632 hashtab_obstack_allocate,
10633 dummy_obstack_deallocate);
10634}
10635
80626a55 10636/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 10637
19c3d4c9 10638struct create_dwo_cu_data
3019eac3
DE
10639{
10640 struct dwo_file *dwo_file;
19c3d4c9 10641 struct dwo_unit dwo_unit;
3019eac3
DE
10642};
10643
19c3d4c9 10644/* die_reader_func for create_dwo_cu. */
3019eac3
DE
10645
10646static void
19c3d4c9
DE
10647create_dwo_cu_reader (const struct die_reader_specs *reader,
10648 const gdb_byte *info_ptr,
10649 struct die_info *comp_unit_die,
10650 int has_children,
10651 void *datap)
3019eac3
DE
10652{
10653 struct dwarf2_cu *cu = reader->cu;
9c541725 10654 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 10655 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 10656 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 10657 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 10658 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 10659 struct attribute *attr;
3019eac3
DE
10660
10661 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
10662 if (attr == NULL)
10663 {
19c3d4c9
DE
10664 complaint (&symfile_complaints,
10665 _("Dwarf Error: debug entry at offset 0x%x is missing"
10666 " its dwo_id [in module %s]"),
9c541725 10667 to_underlying (sect_off), dwo_file->dwo_name);
3019eac3
DE
10668 return;
10669 }
10670
3019eac3
DE
10671 dwo_unit->dwo_file = dwo_file;
10672 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 10673 dwo_unit->section = section;
9c541725 10674 dwo_unit->sect_off = sect_off;
3019eac3
DE
10675 dwo_unit->length = cu->per_cu->length;
10676
b4f54984 10677 if (dwarf_read_debug)
4031ecc5 10678 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9c541725
PA
10679 to_underlying (sect_off),
10680 hex_string (dwo_unit->signature));
3019eac3
DE
10681}
10682
33c5cd75 10683/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 10684 Note: This function processes DWO files only, not DWP files. */
3019eac3 10685
33c5cd75
DB
10686static void
10687create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
10688 htab_t &cus_htab)
3019eac3
DE
10689{
10690 struct objfile *objfile = dwarf2_per_objfile->objfile;
33c5cd75 10691 const struct dwarf2_section_info *abbrev_section = &dwo_file.sections.abbrev;
d521ce57 10692 const gdb_byte *info_ptr, *end_ptr;
3019eac3 10693
33c5cd75
DB
10694 dwarf2_read_section (objfile, &section);
10695 info_ptr = section.buffer;
3019eac3
DE
10696
10697 if (info_ptr == NULL)
33c5cd75 10698 return;
3019eac3 10699
b4f54984 10700 if (dwarf_read_debug)
19c3d4c9
DE
10701 {
10702 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
10703 get_section_name (&section),
10704 get_section_file_name (&section));
19c3d4c9 10705 }
3019eac3 10706
33c5cd75 10707 end_ptr = info_ptr + section.size;
3019eac3
DE
10708 while (info_ptr < end_ptr)
10709 {
10710 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
10711 struct create_dwo_cu_data create_dwo_cu_data;
10712 struct dwo_unit *dwo_unit;
10713 void **slot;
10714 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 10715
19c3d4c9
DE
10716 memset (&create_dwo_cu_data.dwo_unit, 0,
10717 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
10718 memset (&per_cu, 0, sizeof (per_cu));
10719 per_cu.objfile = objfile;
10720 per_cu.is_debug_types = 0;
33c5cd75
DB
10721 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10722 per_cu.section = &section;
c5ed0576 10723 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
10724
10725 init_cutu_and_read_dies_no_follow (
10726 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
10727 info_ptr += per_cu.length;
10728
10729 // If the unit could not be parsed, skip it.
10730 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
10731 continue;
3019eac3 10732
33c5cd75
DB
10733 if (cus_htab == NULL)
10734 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 10735
33c5cd75
DB
10736 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10737 *dwo_unit = create_dwo_cu_data.dwo_unit;
10738 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
10739 gdb_assert (slot != NULL);
10740 if (*slot != NULL)
19c3d4c9 10741 {
33c5cd75
DB
10742 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10743 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 10744
33c5cd75
DB
10745 complaint (&symfile_complaints,
10746 _("debug cu entry at offset 0x%x is duplicate to"
10747 " the entry at offset 0x%x, signature %s"),
10748 to_underlying (sect_off), to_underlying (dup_sect_off),
10749 hex_string (dwo_unit->signature));
19c3d4c9 10750 }
33c5cd75 10751 *slot = (void *)dwo_unit;
3019eac3 10752 }
3019eac3
DE
10753}
10754
80626a55
DE
10755/* DWP file .debug_{cu,tu}_index section format:
10756 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
10757
d2415c6c
DE
10758 DWP Version 1:
10759
80626a55
DE
10760 Both index sections have the same format, and serve to map a 64-bit
10761 signature to a set of section numbers. Each section begins with a header,
10762 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10763 indexes, and a pool of 32-bit section numbers. The index sections will be
10764 aligned at 8-byte boundaries in the file.
10765
d2415c6c
DE
10766 The index section header consists of:
10767
10768 V, 32 bit version number
10769 -, 32 bits unused
10770 N, 32 bit number of compilation units or type units in the index
10771 M, 32 bit number of slots in the hash table
80626a55 10772
d2415c6c 10773 Numbers are recorded using the byte order of the application binary.
80626a55 10774
d2415c6c
DE
10775 The hash table begins at offset 16 in the section, and consists of an array
10776 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10777 order of the application binary). Unused slots in the hash table are 0.
10778 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 10779
d2415c6c
DE
10780 The parallel table begins immediately after the hash table
10781 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10782 array of 32-bit indexes (using the byte order of the application binary),
10783 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10784 table contains a 32-bit index into the pool of section numbers. For unused
10785 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 10786
73869dc2
DE
10787 The pool of section numbers begins immediately following the hash table
10788 (at offset 16 + 12 * M from the beginning of the section). The pool of
10789 section numbers consists of an array of 32-bit words (using the byte order
10790 of the application binary). Each item in the array is indexed starting
10791 from 0. The hash table entry provides the index of the first section
10792 number in the set. Additional section numbers in the set follow, and the
10793 set is terminated by a 0 entry (section number 0 is not used in ELF).
10794
10795 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10796 section must be the first entry in the set, and the .debug_abbrev.dwo must
10797 be the second entry. Other members of the set may follow in any order.
10798
10799 ---
10800
10801 DWP Version 2:
10802
10803 DWP Version 2 combines all the .debug_info, etc. sections into one,
10804 and the entries in the index tables are now offsets into these sections.
10805 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10806 section.
10807
10808 Index Section Contents:
10809 Header
10810 Hash Table of Signatures dwp_hash_table.hash_table
10811 Parallel Table of Indices dwp_hash_table.unit_table
10812 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
10813 Table of Section Sizes dwp_hash_table.v2.sizes
10814
10815 The index section header consists of:
10816
10817 V, 32 bit version number
10818 L, 32 bit number of columns in the table of section offsets
10819 N, 32 bit number of compilation units or type units in the index
10820 M, 32 bit number of slots in the hash table
10821
10822 Numbers are recorded using the byte order of the application binary.
10823
10824 The hash table has the same format as version 1.
10825 The parallel table of indices has the same format as version 1,
10826 except that the entries are origin-1 indices into the table of sections
10827 offsets and the table of section sizes.
10828
10829 The table of offsets begins immediately following the parallel table
10830 (at offset 16 + 12 * M from the beginning of the section). The table is
10831 a two-dimensional array of 32-bit words (using the byte order of the
10832 application binary), with L columns and N+1 rows, in row-major order.
10833 Each row in the array is indexed starting from 0. The first row provides
10834 a key to the remaining rows: each column in this row provides an identifier
10835 for a debug section, and the offsets in the same column of subsequent rows
10836 refer to that section. The section identifiers are:
10837
10838 DW_SECT_INFO 1 .debug_info.dwo
10839 DW_SECT_TYPES 2 .debug_types.dwo
10840 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10841 DW_SECT_LINE 4 .debug_line.dwo
10842 DW_SECT_LOC 5 .debug_loc.dwo
10843 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10844 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10845 DW_SECT_MACRO 8 .debug_macro.dwo
10846
10847 The offsets provided by the CU and TU index sections are the base offsets
10848 for the contributions made by each CU or TU to the corresponding section
10849 in the package file. Each CU and TU header contains an abbrev_offset
10850 field, used to find the abbreviations table for that CU or TU within the
10851 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10852 be interpreted as relative to the base offset given in the index section.
10853 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10854 should be interpreted as relative to the base offset for .debug_line.dwo,
10855 and offsets into other debug sections obtained from DWARF attributes should
10856 also be interpreted as relative to the corresponding base offset.
10857
10858 The table of sizes begins immediately following the table of offsets.
10859 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10860 with L columns and N rows, in row-major order. Each row in the array is
10861 indexed starting from 1 (row 0 is shared by the two tables).
10862
10863 ---
10864
10865 Hash table lookup is handled the same in version 1 and 2:
10866
10867 We assume that N and M will not exceed 2^32 - 1.
10868 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10869
d2415c6c
DE
10870 Given a 64-bit compilation unit signature or a type signature S, an entry
10871 in the hash table is located as follows:
80626a55 10872
d2415c6c
DE
10873 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10874 the low-order k bits all set to 1.
80626a55 10875
d2415c6c 10876 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 10877
d2415c6c
DE
10878 3) If the hash table entry at index H matches the signature, use that
10879 entry. If the hash table entry at index H is unused (all zeroes),
10880 terminate the search: the signature is not present in the table.
80626a55 10881
d2415c6c 10882 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 10883
d2415c6c 10884 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 10885 to stop at an unused slot or find the match. */
80626a55
DE
10886
10887/* Create a hash table to map DWO IDs to their CU/TU entry in
10888 .debug_{info,types}.dwo in DWP_FILE.
10889 Returns NULL if there isn't one.
10890 Note: This function processes DWP files only, not DWO files. */
10891
10892static struct dwp_hash_table *
10893create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
10894{
10895 struct objfile *objfile = dwarf2_per_objfile->objfile;
10896 bfd *dbfd = dwp_file->dbfd;
948f8e3d 10897 const gdb_byte *index_ptr, *index_end;
80626a55 10898 struct dwarf2_section_info *index;
73869dc2 10899 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
10900 struct dwp_hash_table *htab;
10901
10902 if (is_debug_types)
10903 index = &dwp_file->sections.tu_index;
10904 else
10905 index = &dwp_file->sections.cu_index;
10906
10907 if (dwarf2_section_empty_p (index))
10908 return NULL;
10909 dwarf2_read_section (objfile, index);
10910
10911 index_ptr = index->buffer;
10912 index_end = index_ptr + index->size;
10913
10914 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
10915 index_ptr += 4;
10916 if (version == 2)
10917 nr_columns = read_4_bytes (dbfd, index_ptr);
10918 else
10919 nr_columns = 0;
10920 index_ptr += 4;
80626a55
DE
10921 nr_units = read_4_bytes (dbfd, index_ptr);
10922 index_ptr += 4;
10923 nr_slots = read_4_bytes (dbfd, index_ptr);
10924 index_ptr += 4;
10925
73869dc2 10926 if (version != 1 && version != 2)
80626a55 10927 {
21aa081e 10928 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 10929 " [in module %s]"),
21aa081e 10930 pulongest (version), dwp_file->name);
80626a55
DE
10931 }
10932 if (nr_slots != (nr_slots & -nr_slots))
10933 {
21aa081e 10934 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 10935 " is not power of 2 [in module %s]"),
21aa081e 10936 pulongest (nr_slots), dwp_file->name);
80626a55
DE
10937 }
10938
10939 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
10940 htab->version = version;
10941 htab->nr_columns = nr_columns;
80626a55
DE
10942 htab->nr_units = nr_units;
10943 htab->nr_slots = nr_slots;
10944 htab->hash_table = index_ptr;
10945 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
10946
10947 /* Exit early if the table is empty. */
10948 if (nr_slots == 0 || nr_units == 0
10949 || (version == 2 && nr_columns == 0))
10950 {
10951 /* All must be zero. */
10952 if (nr_slots != 0 || nr_units != 0
10953 || (version == 2 && nr_columns != 0))
10954 {
10955 complaint (&symfile_complaints,
10956 _("Empty DWP but nr_slots,nr_units,nr_columns not"
10957 " all zero [in modules %s]"),
10958 dwp_file->name);
10959 }
10960 return htab;
10961 }
10962
10963 if (version == 1)
10964 {
10965 htab->section_pool.v1.indices =
10966 htab->unit_table + sizeof (uint32_t) * nr_slots;
10967 /* It's harder to decide whether the section is too small in v1.
10968 V1 is deprecated anyway so we punt. */
10969 }
10970 else
10971 {
10972 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10973 int *ids = htab->section_pool.v2.section_ids;
10974 /* Reverse map for error checking. */
10975 int ids_seen[DW_SECT_MAX + 1];
10976 int i;
10977
10978 if (nr_columns < 2)
10979 {
10980 error (_("Dwarf Error: bad DWP hash table, too few columns"
10981 " in section table [in module %s]"),
10982 dwp_file->name);
10983 }
10984 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10985 {
10986 error (_("Dwarf Error: bad DWP hash table, too many columns"
10987 " in section table [in module %s]"),
10988 dwp_file->name);
10989 }
10990 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10991 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10992 for (i = 0; i < nr_columns; ++i)
10993 {
10994 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10995
10996 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10997 {
10998 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10999 " in section table [in module %s]"),
11000 id, dwp_file->name);
11001 }
11002 if (ids_seen[id] != -1)
11003 {
11004 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11005 " id %d in section table [in module %s]"),
11006 id, dwp_file->name);
11007 }
11008 ids_seen[id] = i;
11009 ids[i] = id;
11010 }
11011 /* Must have exactly one info or types section. */
11012 if (((ids_seen[DW_SECT_INFO] != -1)
11013 + (ids_seen[DW_SECT_TYPES] != -1))
11014 != 1)
11015 {
11016 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11017 " DWO info/types section [in module %s]"),
11018 dwp_file->name);
11019 }
11020 /* Must have an abbrev section. */
11021 if (ids_seen[DW_SECT_ABBREV] == -1)
11022 {
11023 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11024 " section [in module %s]"),
11025 dwp_file->name);
11026 }
11027 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11028 htab->section_pool.v2.sizes =
11029 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11030 * nr_units * nr_columns);
11031 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11032 * nr_units * nr_columns))
11033 > index_end)
11034 {
11035 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11036 " [in module %s]"),
11037 dwp_file->name);
11038 }
11039 }
80626a55
DE
11040
11041 return htab;
11042}
11043
11044/* Update SECTIONS with the data from SECTP.
11045
11046 This function is like the other "locate" section routines that are
11047 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11048 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11049
11050 The result is non-zero for success, or zero if an error was found. */
11051
11052static int
73869dc2
DE
11053locate_v1_virtual_dwo_sections (asection *sectp,
11054 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11055{
11056 const struct dwop_section_names *names = &dwop_section_names;
11057
11058 if (section_is_p (sectp->name, &names->abbrev_dwo))
11059 {
11060 /* There can be only one. */
049412e3 11061 if (sections->abbrev.s.section != NULL)
80626a55 11062 return 0;
049412e3 11063 sections->abbrev.s.section = sectp;
80626a55
DE
11064 sections->abbrev.size = bfd_get_section_size (sectp);
11065 }
11066 else if (section_is_p (sectp->name, &names->info_dwo)
11067 || section_is_p (sectp->name, &names->types_dwo))
11068 {
11069 /* There can be only one. */
049412e3 11070 if (sections->info_or_types.s.section != NULL)
80626a55 11071 return 0;
049412e3 11072 sections->info_or_types.s.section = sectp;
80626a55
DE
11073 sections->info_or_types.size = bfd_get_section_size (sectp);
11074 }
11075 else if (section_is_p (sectp->name, &names->line_dwo))
11076 {
11077 /* There can be only one. */
049412e3 11078 if (sections->line.s.section != NULL)
80626a55 11079 return 0;
049412e3 11080 sections->line.s.section = sectp;
80626a55
DE
11081 sections->line.size = bfd_get_section_size (sectp);
11082 }
11083 else if (section_is_p (sectp->name, &names->loc_dwo))
11084 {
11085 /* There can be only one. */
049412e3 11086 if (sections->loc.s.section != NULL)
80626a55 11087 return 0;
049412e3 11088 sections->loc.s.section = sectp;
80626a55
DE
11089 sections->loc.size = bfd_get_section_size (sectp);
11090 }
11091 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11092 {
11093 /* There can be only one. */
049412e3 11094 if (sections->macinfo.s.section != NULL)
80626a55 11095 return 0;
049412e3 11096 sections->macinfo.s.section = sectp;
80626a55
DE
11097 sections->macinfo.size = bfd_get_section_size (sectp);
11098 }
11099 else if (section_is_p (sectp->name, &names->macro_dwo))
11100 {
11101 /* There can be only one. */
049412e3 11102 if (sections->macro.s.section != NULL)
80626a55 11103 return 0;
049412e3 11104 sections->macro.s.section = sectp;
80626a55
DE
11105 sections->macro.size = bfd_get_section_size (sectp);
11106 }
11107 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11108 {
11109 /* There can be only one. */
049412e3 11110 if (sections->str_offsets.s.section != NULL)
80626a55 11111 return 0;
049412e3 11112 sections->str_offsets.s.section = sectp;
80626a55
DE
11113 sections->str_offsets.size = bfd_get_section_size (sectp);
11114 }
11115 else
11116 {
11117 /* No other kind of section is valid. */
11118 return 0;
11119 }
11120
11121 return 1;
11122}
11123
73869dc2
DE
11124/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11125 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11126 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11127 This is for DWP version 1 files. */
80626a55
DE
11128
11129static struct dwo_unit *
73869dc2
DE
11130create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
11131 uint32_t unit_index,
11132 const char *comp_dir,
11133 ULONGEST signature, int is_debug_types)
80626a55
DE
11134{
11135 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
11136 const struct dwp_hash_table *dwp_htab =
11137 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
11138 bfd *dbfd = dwp_file->dbfd;
11139 const char *kind = is_debug_types ? "TU" : "CU";
11140 struct dwo_file *dwo_file;
11141 struct dwo_unit *dwo_unit;
73869dc2 11142 struct virtual_v1_dwo_sections sections;
80626a55 11143 void **dwo_file_slot;
80626a55
DE
11144 int i;
11145
73869dc2
DE
11146 gdb_assert (dwp_file->version == 1);
11147
b4f54984 11148 if (dwarf_read_debug)
80626a55 11149 {
73869dc2 11150 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11151 kind,
73869dc2 11152 pulongest (unit_index), hex_string (signature),
80626a55
DE
11153 dwp_file->name);
11154 }
11155
19ac8c2e 11156 /* Fetch the sections of this DWO unit.
80626a55
DE
11157 Put a limit on the number of sections we look for so that bad data
11158 doesn't cause us to loop forever. */
11159
73869dc2 11160#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11161 (1 /* .debug_info or .debug_types */ \
11162 + 1 /* .debug_abbrev */ \
11163 + 1 /* .debug_line */ \
11164 + 1 /* .debug_loc */ \
11165 + 1 /* .debug_str_offsets */ \
19ac8c2e 11166 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11167 + 1 /* trailing zero */)
11168
11169 memset (&sections, 0, sizeof (sections));
80626a55 11170
73869dc2 11171 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11172 {
11173 asection *sectp;
11174 uint32_t section_nr =
11175 read_4_bytes (dbfd,
73869dc2
DE
11176 dwp_htab->section_pool.v1.indices
11177 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11178
11179 if (section_nr == 0)
11180 break;
11181 if (section_nr >= dwp_file->num_sections)
11182 {
11183 error (_("Dwarf Error: bad DWP hash table, section number too large"
11184 " [in module %s]"),
11185 dwp_file->name);
11186 }
11187
11188 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11189 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11190 {
11191 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11192 " [in module %s]"),
11193 dwp_file->name);
11194 }
11195 }
11196
11197 if (i < 2
a32a8923
DE
11198 || dwarf2_section_empty_p (&sections.info_or_types)
11199 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
11200 {
11201 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11202 " [in module %s]"),
11203 dwp_file->name);
11204 }
73869dc2 11205 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11206 {
11207 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11208 " [in module %s]"),
11209 dwp_file->name);
11210 }
11211
11212 /* It's easier for the rest of the code if we fake a struct dwo_file and
11213 have dwo_unit "live" in that. At least for now.
11214
11215 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11216 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11217 file, we can combine them back into a virtual DWO file to save space
11218 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11219 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11220
791afaa2
TT
11221 std::string virtual_dwo_name =
11222 string_printf ("virtual-dwo/%d-%d-%d-%d",
11223 get_section_id (&sections.abbrev),
11224 get_section_id (&sections.line),
11225 get_section_id (&sections.loc),
11226 get_section_id (&sections.str_offsets));
80626a55 11227 /* Can we use an existing virtual DWO file? */
791afaa2 11228 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
80626a55
DE
11229 /* Create one if necessary. */
11230 if (*dwo_file_slot == NULL)
11231 {
b4f54984 11232 if (dwarf_read_debug)
80626a55
DE
11233 {
11234 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11235 virtual_dwo_name.c_str ());
80626a55
DE
11236 }
11237 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
11238 dwo_file->dwo_name
11239 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
11240 virtual_dwo_name.c_str (),
11241 virtual_dwo_name.size ());
0ac5b59e 11242 dwo_file->comp_dir = comp_dir;
80626a55
DE
11243 dwo_file->sections.abbrev = sections.abbrev;
11244 dwo_file->sections.line = sections.line;
11245 dwo_file->sections.loc = sections.loc;
11246 dwo_file->sections.macinfo = sections.macinfo;
11247 dwo_file->sections.macro = sections.macro;
11248 dwo_file->sections.str_offsets = sections.str_offsets;
11249 /* The "str" section is global to the entire DWP file. */
11250 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11251 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11252 there's no need to record it in dwo_file.
11253 Also, we can't simply record type sections in dwo_file because
11254 we record a pointer into the vector in dwo_unit. As we collect more
11255 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11256 for it, invalidating all copies of pointers into the previous
11257 contents. */
80626a55
DE
11258 *dwo_file_slot = dwo_file;
11259 }
11260 else
11261 {
b4f54984 11262 if (dwarf_read_debug)
80626a55
DE
11263 {
11264 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11265 virtual_dwo_name.c_str ());
80626a55 11266 }
9a3c8263 11267 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11268 }
80626a55
DE
11269
11270 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11271 dwo_unit->dwo_file = dwo_file;
11272 dwo_unit->signature = signature;
8d749320
SM
11273 dwo_unit->section =
11274 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 11275 *dwo_unit->section = sections.info_or_types;
57d63ce2 11276 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11277
11278 return dwo_unit;
11279}
11280
73869dc2
DE
11281/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11282 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11283 piece within that section used by a TU/CU, return a virtual section
11284 of just that piece. */
11285
11286static struct dwarf2_section_info
11287create_dwp_v2_section (struct dwarf2_section_info *section,
11288 bfd_size_type offset, bfd_size_type size)
11289{
11290 struct dwarf2_section_info result;
11291 asection *sectp;
11292
11293 gdb_assert (section != NULL);
11294 gdb_assert (!section->is_virtual);
11295
11296 memset (&result, 0, sizeof (result));
11297 result.s.containing_section = section;
11298 result.is_virtual = 1;
11299
11300 if (size == 0)
11301 return result;
11302
11303 sectp = get_section_bfd_section (section);
11304
11305 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11306 bounds of the real section. This is a pretty-rare event, so just
11307 flag an error (easier) instead of a warning and trying to cope. */
11308 if (sectp == NULL
11309 || offset + size > bfd_get_section_size (sectp))
11310 {
11311 bfd *abfd = sectp->owner;
11312
11313 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11314 " in section %s [in module %s]"),
11315 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
11316 objfile_name (dwarf2_per_objfile->objfile));
11317 }
11318
11319 result.virtual_offset = offset;
11320 result.size = size;
11321 return result;
11322}
11323
11324/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11325 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11326 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11327 This is for DWP version 2 files. */
11328
11329static struct dwo_unit *
11330create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
11331 uint32_t unit_index,
11332 const char *comp_dir,
11333 ULONGEST signature, int is_debug_types)
11334{
11335 struct objfile *objfile = dwarf2_per_objfile->objfile;
11336 const struct dwp_hash_table *dwp_htab =
11337 is_debug_types ? dwp_file->tus : dwp_file->cus;
11338 bfd *dbfd = dwp_file->dbfd;
11339 const char *kind = is_debug_types ? "TU" : "CU";
11340 struct dwo_file *dwo_file;
11341 struct dwo_unit *dwo_unit;
11342 struct virtual_v2_dwo_sections sections;
11343 void **dwo_file_slot;
73869dc2
DE
11344 int i;
11345
11346 gdb_assert (dwp_file->version == 2);
11347
b4f54984 11348 if (dwarf_read_debug)
73869dc2
DE
11349 {
11350 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11351 kind,
11352 pulongest (unit_index), hex_string (signature),
11353 dwp_file->name);
11354 }
11355
11356 /* Fetch the section offsets of this DWO unit. */
11357
11358 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11359
11360 for (i = 0; i < dwp_htab->nr_columns; ++i)
11361 {
11362 uint32_t offset = read_4_bytes (dbfd,
11363 dwp_htab->section_pool.v2.offsets
11364 + (((unit_index - 1) * dwp_htab->nr_columns
11365 + i)
11366 * sizeof (uint32_t)));
11367 uint32_t size = read_4_bytes (dbfd,
11368 dwp_htab->section_pool.v2.sizes
11369 + (((unit_index - 1) * dwp_htab->nr_columns
11370 + i)
11371 * sizeof (uint32_t)));
11372
11373 switch (dwp_htab->section_pool.v2.section_ids[i])
11374 {
11375 case DW_SECT_INFO:
11376 case DW_SECT_TYPES:
11377 sections.info_or_types_offset = offset;
11378 sections.info_or_types_size = size;
11379 break;
11380 case DW_SECT_ABBREV:
11381 sections.abbrev_offset = offset;
11382 sections.abbrev_size = size;
11383 break;
11384 case DW_SECT_LINE:
11385 sections.line_offset = offset;
11386 sections.line_size = size;
11387 break;
11388 case DW_SECT_LOC:
11389 sections.loc_offset = offset;
11390 sections.loc_size = size;
11391 break;
11392 case DW_SECT_STR_OFFSETS:
11393 sections.str_offsets_offset = offset;
11394 sections.str_offsets_size = size;
11395 break;
11396 case DW_SECT_MACINFO:
11397 sections.macinfo_offset = offset;
11398 sections.macinfo_size = size;
11399 break;
11400 case DW_SECT_MACRO:
11401 sections.macro_offset = offset;
11402 sections.macro_size = size;
11403 break;
11404 }
11405 }
11406
11407 /* It's easier for the rest of the code if we fake a struct dwo_file and
11408 have dwo_unit "live" in that. At least for now.
11409
11410 The DWP file can be made up of a random collection of CUs and TUs.
11411 However, for each CU + set of TUs that came from the same original DWO
11412 file, we can combine them back into a virtual DWO file to save space
11413 (fewer struct dwo_file objects to allocate). Remember that for really
11414 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11415
791afaa2
TT
11416 std::string virtual_dwo_name =
11417 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11418 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11419 (long) (sections.line_size ? sections.line_offset : 0),
11420 (long) (sections.loc_size ? sections.loc_offset : 0),
11421 (long) (sections.str_offsets_size
11422 ? sections.str_offsets_offset : 0));
73869dc2 11423 /* Can we use an existing virtual DWO file? */
791afaa2 11424 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
73869dc2
DE
11425 /* Create one if necessary. */
11426 if (*dwo_file_slot == NULL)
11427 {
b4f54984 11428 if (dwarf_read_debug)
73869dc2
DE
11429 {
11430 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11431 virtual_dwo_name.c_str ());
73869dc2
DE
11432 }
11433 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
11434 dwo_file->dwo_name
11435 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
11436 virtual_dwo_name.c_str (),
11437 virtual_dwo_name.size ());
73869dc2
DE
11438 dwo_file->comp_dir = comp_dir;
11439 dwo_file->sections.abbrev =
11440 create_dwp_v2_section (&dwp_file->sections.abbrev,
11441 sections.abbrev_offset, sections.abbrev_size);
11442 dwo_file->sections.line =
11443 create_dwp_v2_section (&dwp_file->sections.line,
11444 sections.line_offset, sections.line_size);
11445 dwo_file->sections.loc =
11446 create_dwp_v2_section (&dwp_file->sections.loc,
11447 sections.loc_offset, sections.loc_size);
11448 dwo_file->sections.macinfo =
11449 create_dwp_v2_section (&dwp_file->sections.macinfo,
11450 sections.macinfo_offset, sections.macinfo_size);
11451 dwo_file->sections.macro =
11452 create_dwp_v2_section (&dwp_file->sections.macro,
11453 sections.macro_offset, sections.macro_size);
11454 dwo_file->sections.str_offsets =
11455 create_dwp_v2_section (&dwp_file->sections.str_offsets,
11456 sections.str_offsets_offset,
11457 sections.str_offsets_size);
11458 /* The "str" section is global to the entire DWP file. */
11459 dwo_file->sections.str = dwp_file->sections.str;
11460 /* The info or types section is assigned below to dwo_unit,
11461 there's no need to record it in dwo_file.
11462 Also, we can't simply record type sections in dwo_file because
11463 we record a pointer into the vector in dwo_unit. As we collect more
11464 types we'll grow the vector and eventually have to reallocate space
11465 for it, invalidating all copies of pointers into the previous
11466 contents. */
11467 *dwo_file_slot = dwo_file;
11468 }
11469 else
11470 {
b4f54984 11471 if (dwarf_read_debug)
73869dc2
DE
11472 {
11473 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11474 virtual_dwo_name.c_str ());
73869dc2 11475 }
9a3c8263 11476 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11477 }
73869dc2
DE
11478
11479 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11480 dwo_unit->dwo_file = dwo_file;
11481 dwo_unit->signature = signature;
8d749320
SM
11482 dwo_unit->section =
11483 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
73869dc2
DE
11484 *dwo_unit->section = create_dwp_v2_section (is_debug_types
11485 ? &dwp_file->sections.types
11486 : &dwp_file->sections.info,
11487 sections.info_or_types_offset,
11488 sections.info_or_types_size);
11489 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11490
11491 return dwo_unit;
11492}
11493
57d63ce2
DE
11494/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11495 Returns NULL if the signature isn't found. */
80626a55
DE
11496
11497static struct dwo_unit *
57d63ce2
DE
11498lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
11499 ULONGEST signature, int is_debug_types)
80626a55 11500{
57d63ce2
DE
11501 const struct dwp_hash_table *dwp_htab =
11502 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 11503 bfd *dbfd = dwp_file->dbfd;
57d63ce2 11504 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11505 uint32_t hash = signature & mask;
11506 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11507 unsigned int i;
11508 void **slot;
870f88f7 11509 struct dwo_unit find_dwo_cu;
80626a55
DE
11510
11511 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11512 find_dwo_cu.signature = signature;
19ac8c2e
DE
11513 slot = htab_find_slot (is_debug_types
11514 ? dwp_file->loaded_tus
11515 : dwp_file->loaded_cus,
11516 &find_dwo_cu, INSERT);
80626a55
DE
11517
11518 if (*slot != NULL)
9a3c8263 11519 return (struct dwo_unit *) *slot;
80626a55
DE
11520
11521 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11522 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11523 {
11524 ULONGEST signature_in_table;
11525
11526 signature_in_table =
57d63ce2 11527 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
11528 if (signature_in_table == signature)
11529 {
57d63ce2
DE
11530 uint32_t unit_index =
11531 read_4_bytes (dbfd,
11532 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 11533
73869dc2
DE
11534 if (dwp_file->version == 1)
11535 {
11536 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
11537 comp_dir, signature,
11538 is_debug_types);
11539 }
11540 else
11541 {
11542 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
11543 comp_dir, signature,
11544 is_debug_types);
11545 }
9a3c8263 11546 return (struct dwo_unit *) *slot;
80626a55
DE
11547 }
11548 if (signature_in_table == 0)
11549 return NULL;
11550 hash = (hash + hash2) & mask;
11551 }
11552
11553 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11554 " [in module %s]"),
11555 dwp_file->name);
11556}
11557
ab5088bf 11558/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
11559 Open the file specified by FILE_NAME and hand it off to BFD for
11560 preliminary analysis. Return a newly initialized bfd *, which
11561 includes a canonicalized copy of FILE_NAME.
80626a55 11562 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
11563 SEARCH_CWD is true if the current directory is to be searched.
11564 It will be searched before debug-file-directory.
13aaf454
DE
11565 If successful, the file is added to the bfd include table of the
11566 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 11567 If unable to find/open the file, return NULL.
3019eac3
DE
11568 NOTE: This function is derived from symfile_bfd_open. */
11569
192b62ce 11570static gdb_bfd_ref_ptr
6ac97d4c 11571try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3 11572{
80626a55 11573 int desc, flags;
3019eac3 11574 char *absolute_name;
9c02c129
DE
11575 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11576 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11577 to debug_file_directory. */
11578 char *search_path;
11579 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11580
6ac97d4c
DE
11581 if (search_cwd)
11582 {
11583 if (*debug_file_directory != '\0')
11584 search_path = concat (".", dirname_separator_string,
b36cec19 11585 debug_file_directory, (char *) NULL);
6ac97d4c
DE
11586 else
11587 search_path = xstrdup (".");
11588 }
9c02c129 11589 else
6ac97d4c 11590 search_path = xstrdup (debug_file_directory);
3019eac3 11591
492c0ab7 11592 flags = OPF_RETURN_REALPATH;
80626a55
DE
11593 if (is_dwp)
11594 flags |= OPF_SEARCH_IN_PATH;
9c02c129 11595 desc = openp (search_path, flags, file_name,
3019eac3 11596 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 11597 xfree (search_path);
3019eac3
DE
11598 if (desc < 0)
11599 return NULL;
11600
192b62ce 11601 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 11602 xfree (absolute_name);
9c02c129
DE
11603 if (sym_bfd == NULL)
11604 return NULL;
192b62ce 11605 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 11606
192b62ce
TT
11607 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11608 return NULL;
3019eac3 11609
13aaf454
DE
11610 /* Success. Record the bfd as having been included by the objfile's bfd.
11611 This is important because things like demangled_names_hash lives in the
11612 objfile's per_bfd space and may have references to things like symbol
11613 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 11614 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 11615
3019eac3
DE
11616 return sym_bfd;
11617}
11618
ab5088bf 11619/* Try to open DWO file FILE_NAME.
3019eac3
DE
11620 COMP_DIR is the DW_AT_comp_dir attribute.
11621 The result is the bfd handle of the file.
11622 If there is a problem finding or opening the file, return NULL.
11623 Upon success, the canonicalized path of the file is stored in the bfd,
11624 same as symfile_bfd_open. */
11625
192b62ce 11626static gdb_bfd_ref_ptr
ab5088bf 11627open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3 11628{
80626a55 11629 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 11630 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
11631
11632 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11633
11634 if (comp_dir != NULL)
11635 {
b36cec19
PA
11636 char *path_to_try = concat (comp_dir, SLASH_STRING,
11637 file_name, (char *) NULL);
3019eac3
DE
11638
11639 /* NOTE: If comp_dir is a relative path, this will also try the
11640 search path, which seems useful. */
192b62ce
TT
11641 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
11642 1 /*search_cwd*/));
3019eac3
DE
11643 xfree (path_to_try);
11644 if (abfd != NULL)
11645 return abfd;
11646 }
11647
11648 /* That didn't work, try debug-file-directory, which, despite its name,
11649 is a list of paths. */
11650
11651 if (*debug_file_directory == '\0')
11652 return NULL;
11653
6ac97d4c 11654 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
11655}
11656
80626a55
DE
11657/* This function is mapped across the sections and remembers the offset and
11658 size of each of the DWO debugging sections we are interested in. */
11659
11660static void
11661dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
11662{
9a3c8263 11663 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
11664 const struct dwop_section_names *names = &dwop_section_names;
11665
11666 if (section_is_p (sectp->name, &names->abbrev_dwo))
11667 {
049412e3 11668 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
11669 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
11670 }
11671 else if (section_is_p (sectp->name, &names->info_dwo))
11672 {
049412e3 11673 dwo_sections->info.s.section = sectp;
80626a55
DE
11674 dwo_sections->info.size = bfd_get_section_size (sectp);
11675 }
11676 else if (section_is_p (sectp->name, &names->line_dwo))
11677 {
049412e3 11678 dwo_sections->line.s.section = sectp;
80626a55
DE
11679 dwo_sections->line.size = bfd_get_section_size (sectp);
11680 }
11681 else if (section_is_p (sectp->name, &names->loc_dwo))
11682 {
049412e3 11683 dwo_sections->loc.s.section = sectp;
80626a55
DE
11684 dwo_sections->loc.size = bfd_get_section_size (sectp);
11685 }
11686 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11687 {
049412e3 11688 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
11689 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
11690 }
11691 else if (section_is_p (sectp->name, &names->macro_dwo))
11692 {
049412e3 11693 dwo_sections->macro.s.section = sectp;
80626a55
DE
11694 dwo_sections->macro.size = bfd_get_section_size (sectp);
11695 }
11696 else if (section_is_p (sectp->name, &names->str_dwo))
11697 {
049412e3 11698 dwo_sections->str.s.section = sectp;
80626a55
DE
11699 dwo_sections->str.size = bfd_get_section_size (sectp);
11700 }
11701 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11702 {
049412e3 11703 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
11704 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
11705 }
11706 else if (section_is_p (sectp->name, &names->types_dwo))
11707 {
11708 struct dwarf2_section_info type_section;
11709
11710 memset (&type_section, 0, sizeof (type_section));
049412e3 11711 type_section.s.section = sectp;
80626a55
DE
11712 type_section.size = bfd_get_section_size (sectp);
11713 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
11714 &type_section);
11715 }
11716}
11717
ab5088bf 11718/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 11719 by PER_CU. This is for the non-DWP case.
80626a55 11720 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
11721
11722static struct dwo_file *
0ac5b59e
DE
11723open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
11724 const char *dwo_name, const char *comp_dir)
3019eac3
DE
11725{
11726 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 11727 struct dwo_file *dwo_file;
3019eac3
DE
11728 struct cleanup *cleanups;
11729
192b62ce 11730 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
80626a55
DE
11731 if (dbfd == NULL)
11732 {
b4f54984 11733 if (dwarf_read_debug)
80626a55
DE
11734 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
11735 return NULL;
11736 }
11737 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
11738 dwo_file->dwo_name = dwo_name;
11739 dwo_file->comp_dir = comp_dir;
192b62ce 11740 dwo_file->dbfd = dbfd.release ();
3019eac3
DE
11741
11742 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
11743
192b62ce
TT
11744 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
11745 &dwo_file->sections);
3019eac3 11746
33c5cd75 11747 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
3019eac3 11748
78d4d2c5
JK
11749 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
11750 dwo_file->tus);
3019eac3
DE
11751
11752 discard_cleanups (cleanups);
11753
b4f54984 11754 if (dwarf_read_debug)
80626a55
DE
11755 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
11756
3019eac3
DE
11757 return dwo_file;
11758}
11759
80626a55 11760/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
11761 size of each of the DWP debugging sections common to version 1 and 2 that
11762 we are interested in. */
3019eac3 11763
80626a55 11764static void
73869dc2
DE
11765dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
11766 void *dwp_file_ptr)
3019eac3 11767{
9a3c8263 11768 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
11769 const struct dwop_section_names *names = &dwop_section_names;
11770 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 11771
80626a55 11772 /* Record the ELF section number for later lookup: this is what the
73869dc2 11773 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
11774 gdb_assert (elf_section_nr < dwp_file->num_sections);
11775 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 11776
80626a55
DE
11777 /* Look for specific sections that we need. */
11778 if (section_is_p (sectp->name, &names->str_dwo))
11779 {
049412e3 11780 dwp_file->sections.str.s.section = sectp;
80626a55
DE
11781 dwp_file->sections.str.size = bfd_get_section_size (sectp);
11782 }
11783 else if (section_is_p (sectp->name, &names->cu_index))
11784 {
049412e3 11785 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
11786 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
11787 }
11788 else if (section_is_p (sectp->name, &names->tu_index))
11789 {
049412e3 11790 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
11791 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
11792 }
11793}
3019eac3 11794
73869dc2
DE
11795/* This function is mapped across the sections and remembers the offset and
11796 size of each of the DWP version 2 debugging sections that we are interested
11797 in. This is split into a separate function because we don't know if we
11798 have version 1 or 2 until we parse the cu_index/tu_index sections. */
11799
11800static void
11801dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
11802{
9a3c8263 11803 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
11804 const struct dwop_section_names *names = &dwop_section_names;
11805 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11806
11807 /* Record the ELF section number for later lookup: this is what the
11808 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11809 gdb_assert (elf_section_nr < dwp_file->num_sections);
11810 dwp_file->elf_sections[elf_section_nr] = sectp;
11811
11812 /* Look for specific sections that we need. */
11813 if (section_is_p (sectp->name, &names->abbrev_dwo))
11814 {
049412e3 11815 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
11816 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
11817 }
11818 else if (section_is_p (sectp->name, &names->info_dwo))
11819 {
049412e3 11820 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
11821 dwp_file->sections.info.size = bfd_get_section_size (sectp);
11822 }
11823 else if (section_is_p (sectp->name, &names->line_dwo))
11824 {
049412e3 11825 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
11826 dwp_file->sections.line.size = bfd_get_section_size (sectp);
11827 }
11828 else if (section_is_p (sectp->name, &names->loc_dwo))
11829 {
049412e3 11830 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
11831 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
11832 }
11833 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11834 {
049412e3 11835 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
11836 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
11837 }
11838 else if (section_is_p (sectp->name, &names->macro_dwo))
11839 {
049412e3 11840 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
11841 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
11842 }
11843 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11844 {
049412e3 11845 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
11846 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
11847 }
11848 else if (section_is_p (sectp->name, &names->types_dwo))
11849 {
049412e3 11850 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
11851 dwp_file->sections.types.size = bfd_get_section_size (sectp);
11852 }
11853}
11854
80626a55 11855/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 11856
80626a55
DE
11857static hashval_t
11858hash_dwp_loaded_cutus (const void *item)
11859{
9a3c8263 11860 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 11861
80626a55
DE
11862 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11863 return dwo_unit->signature;
3019eac3
DE
11864}
11865
80626a55 11866/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 11867
80626a55
DE
11868static int
11869eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 11870{
9a3c8263
SM
11871 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11872 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 11873
80626a55
DE
11874 return dua->signature == dub->signature;
11875}
3019eac3 11876
80626a55 11877/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 11878
80626a55
DE
11879static htab_t
11880allocate_dwp_loaded_cutus_table (struct objfile *objfile)
11881{
11882 return htab_create_alloc_ex (3,
11883 hash_dwp_loaded_cutus,
11884 eq_dwp_loaded_cutus,
11885 NULL,
11886 &objfile->objfile_obstack,
11887 hashtab_obstack_allocate,
11888 dummy_obstack_deallocate);
11889}
3019eac3 11890
ab5088bf
DE
11891/* Try to open DWP file FILE_NAME.
11892 The result is the bfd handle of the file.
11893 If there is a problem finding or opening the file, return NULL.
11894 Upon success, the canonicalized path of the file is stored in the bfd,
11895 same as symfile_bfd_open. */
11896
192b62ce 11897static gdb_bfd_ref_ptr
ab5088bf
DE
11898open_dwp_file (const char *file_name)
11899{
192b62ce
TT
11900 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
11901 1 /*search_cwd*/));
6ac97d4c
DE
11902 if (abfd != NULL)
11903 return abfd;
11904
11905 /* Work around upstream bug 15652.
11906 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11907 [Whether that's a "bug" is debatable, but it is getting in our way.]
11908 We have no real idea where the dwp file is, because gdb's realpath-ing
11909 of the executable's path may have discarded the needed info.
11910 [IWBN if the dwp file name was recorded in the executable, akin to
11911 .gnu_debuglink, but that doesn't exist yet.]
11912 Strip the directory from FILE_NAME and search again. */
11913 if (*debug_file_directory != '\0')
11914 {
11915 /* Don't implicitly search the current directory here.
11916 If the user wants to search "." to handle this case,
11917 it must be added to debug-file-directory. */
11918 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
11919 0 /*search_cwd*/);
11920 }
11921
11922 return NULL;
ab5088bf
DE
11923}
11924
80626a55
DE
11925/* Initialize the use of the DWP file for the current objfile.
11926 By convention the name of the DWP file is ${objfile}.dwp.
11927 The result is NULL if it can't be found. */
a766d390 11928
80626a55 11929static struct dwp_file *
ab5088bf 11930open_and_init_dwp_file (void)
80626a55
DE
11931{
11932 struct objfile *objfile = dwarf2_per_objfile->objfile;
11933 struct dwp_file *dwp_file;
80626a55 11934
82bf32bc
JK
11935 /* Try to find first .dwp for the binary file before any symbolic links
11936 resolving. */
6c447423
DE
11937
11938 /* If the objfile is a debug file, find the name of the real binary
11939 file and get the name of dwp file from there. */
d721ba37 11940 std::string dwp_name;
6c447423
DE
11941 if (objfile->separate_debug_objfile_backlink != NULL)
11942 {
11943 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11944 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 11945
d721ba37 11946 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
11947 }
11948 else
d721ba37
PA
11949 dwp_name = objfile->original_name;
11950
11951 dwp_name += ".dwp";
80626a55 11952
d721ba37 11953 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
82bf32bc
JK
11954 if (dbfd == NULL
11955 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11956 {
11957 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
11958 dwp_name = objfile_name (objfile);
11959 dwp_name += ".dwp";
11960 dbfd = open_dwp_file (dwp_name.c_str ());
82bf32bc
JK
11961 }
11962
80626a55
DE
11963 if (dbfd == NULL)
11964 {
b4f54984 11965 if (dwarf_read_debug)
d721ba37 11966 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 11967 return NULL;
3019eac3 11968 }
80626a55 11969 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
11970 dwp_file->name = bfd_get_filename (dbfd.get ());
11971 dwp_file->dbfd = dbfd.release ();
c906108c 11972
80626a55 11973 /* +1: section 0 is unused */
192b62ce 11974 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
11975 dwp_file->elf_sections =
11976 OBSTACK_CALLOC (&objfile->objfile_obstack,
11977 dwp_file->num_sections, asection *);
11978
192b62ce
TT
11979 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
11980 dwp_file);
80626a55
DE
11981
11982 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
11983
11984 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
11985
73869dc2 11986 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
11987 if (dwp_file->cus && dwp_file->tus
11988 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
11989 {
11990 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 11991 pretty bizarre. We use pulongest here because that's the established
4d65956b 11992 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
11993 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11994 " TU version %s [in DWP file %s]"),
11995 pulongest (dwp_file->cus->version),
d721ba37 11996 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 11997 }
08302ed2
DE
11998
11999 if (dwp_file->cus)
12000 dwp_file->version = dwp_file->cus->version;
12001 else if (dwp_file->tus)
12002 dwp_file->version = dwp_file->tus->version;
12003 else
12004 dwp_file->version = 2;
73869dc2
DE
12005
12006 if (dwp_file->version == 2)
192b62ce
TT
12007 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
12008 dwp_file);
73869dc2 12009
19ac8c2e
DE
12010 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
12011 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 12012
b4f54984 12013 if (dwarf_read_debug)
80626a55
DE
12014 {
12015 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12016 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12017 " %s CUs, %s TUs\n",
12018 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12019 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12020 }
12021
12022 return dwp_file;
3019eac3 12023}
c906108c 12024
ab5088bf
DE
12025/* Wrapper around open_and_init_dwp_file, only open it once. */
12026
12027static struct dwp_file *
12028get_dwp_file (void)
12029{
12030 if (! dwarf2_per_objfile->dwp_checked)
12031 {
12032 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
12033 dwarf2_per_objfile->dwp_checked = 1;
12034 }
12035 return dwarf2_per_objfile->dwp_file;
12036}
12037
80626a55
DE
12038/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12039 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12040 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12041 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12042 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12043
12044 This is called, for example, when wanting to read a variable with a
12045 complex location. Therefore we don't want to do file i/o for every call.
12046 Therefore we don't want to look for a DWO file on every call.
12047 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12048 then we check if we've already seen DWO_NAME, and only THEN do we check
12049 for a DWO file.
12050
1c658ad5 12051 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12052 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12053
3019eac3 12054static struct dwo_unit *
80626a55
DE
12055lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12056 const char *dwo_name, const char *comp_dir,
12057 ULONGEST signature, int is_debug_types)
3019eac3
DE
12058{
12059 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12060 const char *kind = is_debug_types ? "TU" : "CU";
12061 void **dwo_file_slot;
3019eac3 12062 struct dwo_file *dwo_file;
80626a55 12063 struct dwp_file *dwp_file;
cb1df416 12064
6a506a2d
DE
12065 /* First see if there's a DWP file.
12066 If we have a DWP file but didn't find the DWO inside it, don't
12067 look for the original DWO file. It makes gdb behave differently
12068 depending on whether one is debugging in the build tree. */
cf2c3c16 12069
ab5088bf 12070 dwp_file = get_dwp_file ();
80626a55 12071 if (dwp_file != NULL)
cf2c3c16 12072 {
80626a55
DE
12073 const struct dwp_hash_table *dwp_htab =
12074 is_debug_types ? dwp_file->tus : dwp_file->cus;
12075
12076 if (dwp_htab != NULL)
12077 {
12078 struct dwo_unit *dwo_cutu =
57d63ce2
DE
12079 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
12080 signature, is_debug_types);
80626a55
DE
12081
12082 if (dwo_cutu != NULL)
12083 {
b4f54984 12084 if (dwarf_read_debug)
80626a55
DE
12085 {
12086 fprintf_unfiltered (gdb_stdlog,
12087 "Virtual DWO %s %s found: @%s\n",
12088 kind, hex_string (signature),
12089 host_address_to_string (dwo_cutu));
12090 }
12091 return dwo_cutu;
12092 }
12093 }
12094 }
6a506a2d 12095 else
80626a55 12096 {
6a506a2d 12097 /* No DWP file, look for the DWO file. */
80626a55 12098
6a506a2d
DE
12099 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
12100 if (*dwo_file_slot == NULL)
80626a55 12101 {
6a506a2d
DE
12102 /* Read in the file and build a table of the CUs/TUs it contains. */
12103 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12104 }
6a506a2d 12105 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12106 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12107
6a506a2d 12108 if (dwo_file != NULL)
19c3d4c9 12109 {
6a506a2d
DE
12110 struct dwo_unit *dwo_cutu = NULL;
12111
12112 if (is_debug_types && dwo_file->tus)
12113 {
12114 struct dwo_unit find_dwo_cutu;
12115
12116 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12117 find_dwo_cutu.signature = signature;
9a3c8263
SM
12118 dwo_cutu
12119 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 12120 }
33c5cd75 12121 else if (!is_debug_types && dwo_file->cus)
80626a55 12122 {
33c5cd75
DB
12123 struct dwo_unit find_dwo_cutu;
12124
12125 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12126 find_dwo_cutu.signature = signature;
12127 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
12128 &find_dwo_cutu);
6a506a2d
DE
12129 }
12130
12131 if (dwo_cutu != NULL)
12132 {
b4f54984 12133 if (dwarf_read_debug)
6a506a2d
DE
12134 {
12135 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12136 kind, dwo_name, hex_string (signature),
12137 host_address_to_string (dwo_cutu));
12138 }
12139 return dwo_cutu;
80626a55
DE
12140 }
12141 }
2e276125 12142 }
9cdd5dbd 12143
80626a55
DE
12144 /* We didn't find it. This could mean a dwo_id mismatch, or
12145 someone deleted the DWO/DWP file, or the search path isn't set up
12146 correctly to find the file. */
12147
b4f54984 12148 if (dwarf_read_debug)
80626a55
DE
12149 {
12150 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12151 kind, dwo_name, hex_string (signature));
12152 }
3019eac3 12153
6656a72d
DE
12154 /* This is a warning and not a complaint because it can be caused by
12155 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12156 {
12157 /* Print the name of the DWP file if we looked there, helps the user
12158 better diagnose the problem. */
791afaa2 12159 std::string dwp_text;
43942612
DE
12160
12161 if (dwp_file != NULL)
791afaa2
TT
12162 dwp_text = string_printf (" [in DWP file %s]",
12163 lbasename (dwp_file->name));
43942612
DE
12164
12165 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
12166 " [in module %s]"),
12167 kind, dwo_name, hex_string (signature),
791afaa2 12168 dwp_text.c_str (),
43942612 12169 this_unit->is_debug_types ? "TU" : "CU",
9c541725 12170 to_underlying (this_unit->sect_off), objfile_name (objfile));
43942612 12171 }
3019eac3 12172 return NULL;
5fb290d7
DJ
12173}
12174
80626a55
DE
12175/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12176 See lookup_dwo_cutu_unit for details. */
12177
12178static struct dwo_unit *
12179lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12180 const char *dwo_name, const char *comp_dir,
12181 ULONGEST signature)
12182{
12183 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12184}
12185
12186/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12187 See lookup_dwo_cutu_unit for details. */
12188
12189static struct dwo_unit *
12190lookup_dwo_type_unit (struct signatured_type *this_tu,
12191 const char *dwo_name, const char *comp_dir)
12192{
12193 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12194}
12195
89e63ee4
DE
12196/* Traversal function for queue_and_load_all_dwo_tus. */
12197
12198static int
12199queue_and_load_dwo_tu (void **slot, void *info)
12200{
12201 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12202 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12203 ULONGEST signature = dwo_unit->signature;
12204 struct signatured_type *sig_type =
12205 lookup_dwo_signatured_type (per_cu->cu, signature);
12206
12207 if (sig_type != NULL)
12208 {
12209 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12210
12211 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12212 a real dependency of PER_CU on SIG_TYPE. That is detected later
12213 while processing PER_CU. */
12214 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12215 load_full_type_unit (sig_cu);
12216 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
12217 }
12218
12219 return 1;
12220}
12221
12222/* Queue all TUs contained in the DWO of PER_CU to be read in.
12223 The DWO may have the only definition of the type, though it may not be
12224 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12225 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12226
12227static void
12228queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12229{
12230 struct dwo_unit *dwo_unit;
12231 struct dwo_file *dwo_file;
12232
12233 gdb_assert (!per_cu->is_debug_types);
12234 gdb_assert (get_dwp_file () == NULL);
12235 gdb_assert (per_cu->cu != NULL);
12236
12237 dwo_unit = per_cu->cu->dwo_unit;
12238 gdb_assert (dwo_unit != NULL);
12239
12240 dwo_file = dwo_unit->dwo_file;
12241 if (dwo_file->tus != NULL)
12242 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
12243}
12244
3019eac3
DE
12245/* Free all resources associated with DWO_FILE.
12246 Close the DWO file and munmap the sections.
12247 All memory should be on the objfile obstack. */
348e048f
DE
12248
12249static void
3019eac3 12250free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 12251{
348e048f 12252
5c6fa7ab 12253 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 12254 gdb_bfd_unref (dwo_file->dbfd);
348e048f 12255
3019eac3
DE
12256 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
12257}
348e048f 12258
3019eac3 12259/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 12260
3019eac3
DE
12261static void
12262free_dwo_file_cleanup (void *arg)
12263{
12264 struct dwo_file *dwo_file = (struct dwo_file *) arg;
12265 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 12266
3019eac3
DE
12267 free_dwo_file (dwo_file, objfile);
12268}
348e048f 12269
3019eac3 12270/* Traversal function for free_dwo_files. */
2ab95328 12271
3019eac3
DE
12272static int
12273free_dwo_file_from_slot (void **slot, void *info)
12274{
12275 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
12276 struct objfile *objfile = (struct objfile *) info;
348e048f 12277
3019eac3 12278 free_dwo_file (dwo_file, objfile);
348e048f 12279
3019eac3
DE
12280 return 1;
12281}
348e048f 12282
3019eac3 12283/* Free all resources associated with DWO_FILES. */
348e048f 12284
3019eac3
DE
12285static void
12286free_dwo_files (htab_t dwo_files, struct objfile *objfile)
12287{
12288 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 12289}
3019eac3
DE
12290\f
12291/* Read in various DIEs. */
348e048f 12292
d389af10 12293/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12294 Inherit only the children of the DW_AT_abstract_origin DIE not being
12295 already referenced by DW_AT_abstract_origin from the children of the
12296 current DIE. */
d389af10
JK
12297
12298static void
12299inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12300{
12301 struct die_info *child_die;
791afaa2 12302 sect_offset *offsetp;
d389af10
JK
12303 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12304 struct die_info *origin_die;
12305 /* Iterator of the ORIGIN_DIE children. */
12306 struct die_info *origin_child_die;
d389af10 12307 struct attribute *attr;
cd02d79d
PA
12308 struct dwarf2_cu *origin_cu;
12309 struct pending **origin_previous_list_in_scope;
d389af10
JK
12310
12311 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12312 if (!attr)
12313 return;
12314
cd02d79d
PA
12315 /* Note that following die references may follow to a die in a
12316 different cu. */
12317
12318 origin_cu = cu;
12319 origin_die = follow_die_ref (die, attr, &origin_cu);
12320
12321 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12322 symbols in. */
12323 origin_previous_list_in_scope = origin_cu->list_in_scope;
12324 origin_cu->list_in_scope = cu->list_in_scope;
12325
edb3359d
DJ
12326 if (die->tag != origin_die->tag
12327 && !(die->tag == DW_TAG_inlined_subroutine
12328 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
12329 complaint (&symfile_complaints,
12330 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9c541725
PA
12331 to_underlying (die->sect_off),
12332 to_underlying (origin_die->sect_off));
d389af10 12333
791afaa2 12334 std::vector<sect_offset> offsets;
d389af10 12335
3ea89b92
PMR
12336 for (child_die = die->child;
12337 child_die && child_die->tag;
12338 child_die = sibling_die (child_die))
12339 {
12340 struct die_info *child_origin_die;
12341 struct dwarf2_cu *child_origin_cu;
12342
12343 /* We are trying to process concrete instance entries:
216f72a1 12344 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12345 it's not relevant to our analysis here. i.e. detecting DIEs that are
12346 present in the abstract instance but not referenced in the concrete
12347 one. */
216f72a1
JK
12348 if (child_die->tag == DW_TAG_call_site
12349 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12350 continue;
12351
c38f313d
DJ
12352 /* For each CHILD_DIE, find the corresponding child of
12353 ORIGIN_DIE. If there is more than one layer of
12354 DW_AT_abstract_origin, follow them all; there shouldn't be,
12355 but GCC versions at least through 4.4 generate this (GCC PR
12356 40573). */
3ea89b92
PMR
12357 child_origin_die = child_die;
12358 child_origin_cu = cu;
c38f313d
DJ
12359 while (1)
12360 {
cd02d79d
PA
12361 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12362 child_origin_cu);
c38f313d
DJ
12363 if (attr == NULL)
12364 break;
cd02d79d
PA
12365 child_origin_die = follow_die_ref (child_origin_die, attr,
12366 &child_origin_cu);
c38f313d
DJ
12367 }
12368
d389af10
JK
12369 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12370 counterpart may exist. */
c38f313d 12371 if (child_origin_die != child_die)
d389af10 12372 {
edb3359d
DJ
12373 if (child_die->tag != child_origin_die->tag
12374 && !(child_die->tag == DW_TAG_inlined_subroutine
12375 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
12376 complaint (&symfile_complaints,
12377 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
12378 "different tags"),
12379 to_underlying (child_die->sect_off),
12380 to_underlying (child_origin_die->sect_off));
c38f313d
DJ
12381 if (child_origin_die->parent != origin_die)
12382 complaint (&symfile_complaints,
12383 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
12384 "different parents"),
12385 to_underlying (child_die->sect_off),
12386 to_underlying (child_origin_die->sect_off));
c38f313d 12387 else
791afaa2 12388 offsets.push_back (child_origin_die->sect_off);
d389af10 12389 }
d389af10 12390 }
791afaa2
TT
12391 std::sort (offsets.begin (), offsets.end ());
12392 sect_offset *offsets_end = offsets.data () + offsets.size ();
12393 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12394 if (offsetp[-1] == *offsetp)
3e43a32a
MS
12395 complaint (&symfile_complaints,
12396 _("Multiple children of DIE 0x%x refer "
12397 "to DIE 0x%x as their abstract origin"),
9c541725 12398 to_underlying (die->sect_off), to_underlying (*offsetp));
d389af10 12399
791afaa2 12400 offsetp = offsets.data ();
d389af10
JK
12401 origin_child_die = origin_die->child;
12402 while (origin_child_die && origin_child_die->tag)
12403 {
12404 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12405 while (offsetp < offsets_end
9c541725 12406 && *offsetp < origin_child_die->sect_off)
d389af10 12407 offsetp++;
b64f50a1 12408 if (offsetp >= offsets_end
9c541725 12409 || *offsetp > origin_child_die->sect_off)
d389af10 12410 {
adde2bff
DE
12411 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12412 Check whether we're already processing ORIGIN_CHILD_DIE.
12413 This can happen with mutually referenced abstract_origins.
12414 PR 16581. */
12415 if (!origin_child_die->in_process)
12416 process_die (origin_child_die, origin_cu);
d389af10
JK
12417 }
12418 origin_child_die = sibling_die (origin_child_die);
12419 }
cd02d79d 12420 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
12421}
12422
c906108c 12423static void
e7c27a73 12424read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12425{
e7c27a73 12426 struct objfile *objfile = cu->objfile;
3e29f34a 12427 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 12428 struct context_stack *newobj;
c906108c
SS
12429 CORE_ADDR lowpc;
12430 CORE_ADDR highpc;
12431 struct die_info *child_die;
edb3359d 12432 struct attribute *attr, *call_line, *call_file;
15d034d0 12433 const char *name;
e142c38c 12434 CORE_ADDR baseaddr;
801e3a5b 12435 struct block *block;
edb3359d 12436 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12437 std::vector<struct symbol *> template_args;
34eaf542 12438 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12439
12440 if (inlined_func)
12441 {
12442 /* If we do not have call site information, we can't show the
12443 caller of this inlined function. That's too confusing, so
12444 only use the scope for local variables. */
12445 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12446 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12447 if (call_line == NULL || call_file == NULL)
12448 {
12449 read_lexical_block_scope (die, cu);
12450 return;
12451 }
12452 }
c906108c 12453
e142c38c
DJ
12454 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12455
94af9270 12456 name = dwarf2_name (die, cu);
c906108c 12457
e8d05480
JB
12458 /* Ignore functions with missing or empty names. These are actually
12459 illegal according to the DWARF standard. */
12460 if (name == NULL)
12461 {
12462 complaint (&symfile_complaints,
b64f50a1 12463 _("missing name for subprogram DIE at %d"),
9c541725 12464 to_underlying (die->sect_off));
e8d05480
JB
12465 return;
12466 }
12467
12468 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12469 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12470 <= PC_BOUNDS_INVALID)
e8d05480 12471 {
ae4d0c03
PM
12472 attr = dwarf2_attr (die, DW_AT_external, cu);
12473 if (!attr || !DW_UNSND (attr))
12474 complaint (&symfile_complaints,
3e43a32a
MS
12475 _("cannot get low and high bounds "
12476 "for subprogram DIE at %d"),
9c541725 12477 to_underlying (die->sect_off));
e8d05480
JB
12478 return;
12479 }
c906108c 12480
3e29f34a
MR
12481 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12482 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12483
34eaf542
TT
12484 /* If we have any template arguments, then we must allocate a
12485 different sort of symbol. */
12486 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12487 {
12488 if (child_die->tag == DW_TAG_template_type_param
12489 || child_die->tag == DW_TAG_template_value_param)
12490 {
e623cf5d 12491 templ_func = allocate_template_symbol (objfile);
cf724bc9 12492 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
12493 break;
12494 }
12495 }
12496
fe978cb0
PA
12497 newobj = push_context (0, lowpc);
12498 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 12499 (struct symbol *) templ_func);
4c2df51b 12500
4cecd739
DJ
12501 /* If there is a location expression for DW_AT_frame_base, record
12502 it. */
e142c38c 12503 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 12504 if (attr)
fe978cb0 12505 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 12506
63e43d3a
PMR
12507 /* If there is a location for the static link, record it. */
12508 newobj->static_link = NULL;
12509 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12510 if (attr)
12511 {
224c3ddb
SM
12512 newobj->static_link
12513 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
12514 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
12515 }
12516
e142c38c 12517 cu->list_in_scope = &local_symbols;
c906108c 12518
639d11d3 12519 if (die->child != NULL)
c906108c 12520 {
639d11d3 12521 child_die = die->child;
c906108c
SS
12522 while (child_die && child_die->tag)
12523 {
34eaf542
TT
12524 if (child_die->tag == DW_TAG_template_type_param
12525 || child_die->tag == DW_TAG_template_value_param)
12526 {
12527 struct symbol *arg = new_symbol (child_die, NULL, cu);
12528
f1078f66 12529 if (arg != NULL)
2f4732b0 12530 template_args.push_back (arg);
34eaf542
TT
12531 }
12532 else
12533 process_die (child_die, cu);
c906108c
SS
12534 child_die = sibling_die (child_die);
12535 }
12536 }
12537
d389af10
JK
12538 inherit_abstract_dies (die, cu);
12539
4a811a97
UW
12540 /* If we have a DW_AT_specification, we might need to import using
12541 directives from the context of the specification DIE. See the
12542 comment in determine_prefix. */
12543 if (cu->language == language_cplus
12544 && dwarf2_attr (die, DW_AT_specification, cu))
12545 {
12546 struct dwarf2_cu *spec_cu = cu;
12547 struct die_info *spec_die = die_specification (die, &spec_cu);
12548
12549 while (spec_die)
12550 {
12551 child_die = spec_die->child;
12552 while (child_die && child_die->tag)
12553 {
12554 if (child_die->tag == DW_TAG_imported_module)
12555 process_die (child_die, spec_cu);
12556 child_die = sibling_die (child_die);
12557 }
12558
12559 /* In some cases, GCC generates specification DIEs that
12560 themselves contain DW_AT_specification attributes. */
12561 spec_die = die_specification (spec_die, &spec_cu);
12562 }
12563 }
12564
fe978cb0 12565 newobj = pop_context ();
c906108c 12566 /* Make a block for the local symbols within. */
fe978cb0 12567 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 12568 newobj->static_link, lowpc, highpc);
801e3a5b 12569
df8a16a1 12570 /* For C++, set the block's scope. */
45280282
IB
12571 if ((cu->language == language_cplus
12572 || cu->language == language_fortran
c44af4eb
TT
12573 || cu->language == language_d
12574 || cu->language == language_rust)
4d4ec4e5 12575 && cu->processing_has_namespace_info)
195a3f6c
TT
12576 block_set_scope (block, determine_prefix (die, cu),
12577 &objfile->objfile_obstack);
df8a16a1 12578
801e3a5b
JB
12579 /* If we have address ranges, record them. */
12580 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 12581
fe978cb0 12582 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 12583
34eaf542 12584 /* Attach template arguments to function. */
2f4732b0 12585 if (!template_args.empty ())
34eaf542
TT
12586 {
12587 gdb_assert (templ_func != NULL);
12588
2f4732b0 12589 templ_func->n_template_arguments = template_args.size ();
34eaf542 12590 templ_func->template_arguments
8d749320
SM
12591 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12592 templ_func->n_template_arguments);
34eaf542 12593 memcpy (templ_func->template_arguments,
2f4732b0 12594 template_args.data (),
34eaf542 12595 (templ_func->n_template_arguments * sizeof (struct symbol *)));
34eaf542
TT
12596 }
12597
208d8187
JB
12598 /* In C++, we can have functions nested inside functions (e.g., when
12599 a function declares a class that has methods). This means that
12600 when we finish processing a function scope, we may need to go
12601 back to building a containing block's symbol lists. */
fe978cb0 12602 local_symbols = newobj->locals;
22cee43f 12603 local_using_directives = newobj->local_using_directives;
208d8187 12604
921e78cf
JB
12605 /* If we've finished processing a top-level function, subsequent
12606 symbols go in the file symbol list. */
12607 if (outermost_context_p ())
e142c38c 12608 cu->list_in_scope = &file_symbols;
c906108c
SS
12609}
12610
12611/* Process all the DIES contained within a lexical block scope. Start
12612 a new scope, process the dies, and then close the scope. */
12613
12614static void
e7c27a73 12615read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12616{
e7c27a73 12617 struct objfile *objfile = cu->objfile;
3e29f34a 12618 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 12619 struct context_stack *newobj;
c906108c
SS
12620 CORE_ADDR lowpc, highpc;
12621 struct die_info *child_die;
e142c38c
DJ
12622 CORE_ADDR baseaddr;
12623
12624 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
12625
12626 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
12627 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12628 as multiple lexical blocks? Handling children in a sane way would
6e70227d 12629 be nasty. Might be easier to properly extend generic blocks to
af34e669 12630 describe ranges. */
e385593e
JK
12631 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
12632 {
12633 case PC_BOUNDS_NOT_PRESENT:
12634 /* DW_TAG_lexical_block has no attributes, process its children as if
12635 there was no wrapping by that DW_TAG_lexical_block.
12636 GCC does no longer produces such DWARF since GCC r224161. */
12637 for (child_die = die->child;
12638 child_die != NULL && child_die->tag;
12639 child_die = sibling_die (child_die))
12640 process_die (child_die, cu);
12641 return;
12642 case PC_BOUNDS_INVALID:
12643 return;
12644 }
3e29f34a
MR
12645 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12646 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
12647
12648 push_context (0, lowpc);
639d11d3 12649 if (die->child != NULL)
c906108c 12650 {
639d11d3 12651 child_die = die->child;
c906108c
SS
12652 while (child_die && child_die->tag)
12653 {
e7c27a73 12654 process_die (child_die, cu);
c906108c
SS
12655 child_die = sibling_die (child_die);
12656 }
12657 }
3ea89b92 12658 inherit_abstract_dies (die, cu);
fe978cb0 12659 newobj = pop_context ();
c906108c 12660
22cee43f 12661 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 12662 {
801e3a5b 12663 struct block *block
63e43d3a 12664 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 12665 newobj->start_addr, highpc);
801e3a5b
JB
12666
12667 /* Note that recording ranges after traversing children, as we
12668 do here, means that recording a parent's ranges entails
12669 walking across all its children's ranges as they appear in
12670 the address map, which is quadratic behavior.
12671
12672 It would be nicer to record the parent's ranges before
12673 traversing its children, simply overriding whatever you find
12674 there. But since we don't even decide whether to create a
12675 block until after we've traversed its children, that's hard
12676 to do. */
12677 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 12678 }
fe978cb0 12679 local_symbols = newobj->locals;
22cee43f 12680 local_using_directives = newobj->local_using_directives;
c906108c
SS
12681}
12682
216f72a1 12683/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
12684
12685static void
12686read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
12687{
12688 struct objfile *objfile = cu->objfile;
12689 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12690 CORE_ADDR pc, baseaddr;
12691 struct attribute *attr;
12692 struct call_site *call_site, call_site_local;
12693 void **slot;
12694 int nparams;
12695 struct die_info *child_die;
12696
12697 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12698
216f72a1
JK
12699 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
12700 if (attr == NULL)
12701 {
12702 /* This was a pre-DWARF-5 GNU extension alias
12703 for DW_AT_call_return_pc. */
12704 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12705 }
96408a79
SA
12706 if (!attr)
12707 {
12708 complaint (&symfile_complaints,
216f72a1 12709 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 12710 "DIE 0x%x [in module %s]"),
9c541725 12711 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12712 return;
12713 }
31aa7e4e 12714 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 12715 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
12716
12717 if (cu->call_site_htab == NULL)
12718 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
12719 NULL, &objfile->objfile_obstack,
12720 hashtab_obstack_allocate, NULL);
12721 call_site_local.pc = pc;
12722 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
12723 if (*slot != NULL)
12724 {
12725 complaint (&symfile_complaints,
216f72a1 12726 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 12727 "DIE 0x%x [in module %s]"),
9c541725 12728 paddress (gdbarch, pc), to_underlying (die->sect_off),
4262abfb 12729 objfile_name (objfile));
96408a79
SA
12730 return;
12731 }
12732
12733 /* Count parameters at the caller. */
12734
12735 nparams = 0;
12736 for (child_die = die->child; child_die && child_die->tag;
12737 child_die = sibling_die (child_die))
12738 {
216f72a1
JK
12739 if (child_die->tag != DW_TAG_call_site_parameter
12740 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
12741 {
12742 complaint (&symfile_complaints,
216f72a1
JK
12743 _("Tag %d is not DW_TAG_call_site_parameter in "
12744 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 12745 child_die->tag, to_underlying (child_die->sect_off),
4262abfb 12746 objfile_name (objfile));
96408a79
SA
12747 continue;
12748 }
12749
12750 nparams++;
12751 }
12752
224c3ddb
SM
12753 call_site
12754 = ((struct call_site *)
12755 obstack_alloc (&objfile->objfile_obstack,
12756 sizeof (*call_site)
12757 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
12758 *slot = call_site;
12759 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
12760 call_site->pc = pc;
12761
216f72a1
JK
12762 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
12763 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
12764 {
12765 struct die_info *func_die;
12766
12767 /* Skip also over DW_TAG_inlined_subroutine. */
12768 for (func_die = die->parent;
12769 func_die && func_die->tag != DW_TAG_subprogram
12770 && func_die->tag != DW_TAG_subroutine_type;
12771 func_die = func_die->parent);
12772
216f72a1
JK
12773 /* DW_AT_call_all_calls is a superset
12774 of DW_AT_call_all_tail_calls. */
96408a79 12775 if (func_die
216f72a1 12776 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 12777 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 12778 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
12779 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12780 {
12781 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12782 not complete. But keep CALL_SITE for look ups via call_site_htab,
12783 both the initial caller containing the real return address PC and
12784 the final callee containing the current PC of a chain of tail
12785 calls do not need to have the tail call list complete. But any
12786 function candidate for a virtual tail call frame searched via
12787 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12788 determined unambiguously. */
12789 }
12790 else
12791 {
12792 struct type *func_type = NULL;
12793
12794 if (func_die)
12795 func_type = get_die_type (func_die, cu);
12796 if (func_type != NULL)
12797 {
12798 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
12799
12800 /* Enlist this call site to the function. */
12801 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12802 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12803 }
12804 else
12805 complaint (&symfile_complaints,
216f72a1 12806 _("Cannot find function owning DW_TAG_call_site "
96408a79 12807 "DIE 0x%x [in module %s]"),
9c541725 12808 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12809 }
12810 }
12811
216f72a1
JK
12812 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12813 if (attr == NULL)
12814 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12815 if (attr == NULL)
12816 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 12817 if (attr == NULL)
216f72a1
JK
12818 {
12819 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12820 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12821 }
96408a79
SA
12822 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
12823 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
12824 /* Keep NULL DWARF_BLOCK. */;
12825 else if (attr_form_is_block (attr))
12826 {
12827 struct dwarf2_locexpr_baton *dlbaton;
12828
8d749320 12829 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
12830 dlbaton->data = DW_BLOCK (attr)->data;
12831 dlbaton->size = DW_BLOCK (attr)->size;
12832 dlbaton->per_cu = cu->per_cu;
12833
12834 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
12835 }
7771576e 12836 else if (attr_form_is_ref (attr))
96408a79 12837 {
96408a79
SA
12838 struct dwarf2_cu *target_cu = cu;
12839 struct die_info *target_die;
12840
ac9ec31b 12841 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
12842 gdb_assert (target_cu->objfile == objfile);
12843 if (die_is_declaration (target_die, target_cu))
12844 {
7d45c7c3 12845 const char *target_physname;
9112db09
JK
12846
12847 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 12848 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 12849 if (target_physname == NULL)
9112db09 12850 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
12851 if (target_physname == NULL)
12852 complaint (&symfile_complaints,
216f72a1 12853 _("DW_AT_call_target target DIE has invalid "
96408a79 12854 "physname, for referencing DIE 0x%x [in module %s]"),
9c541725 12855 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 12856 else
7d455152 12857 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
12858 }
12859 else
12860 {
12861 CORE_ADDR lowpc;
12862
12863 /* DW_AT_entry_pc should be preferred. */
3a2b436a 12864 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 12865 <= PC_BOUNDS_INVALID)
96408a79 12866 complaint (&symfile_complaints,
216f72a1 12867 _("DW_AT_call_target target DIE has invalid "
96408a79 12868 "low pc, for referencing DIE 0x%x [in module %s]"),
9c541725 12869 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 12870 else
3e29f34a
MR
12871 {
12872 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12873 SET_FIELD_PHYSADDR (call_site->target, lowpc);
12874 }
96408a79
SA
12875 }
12876 }
12877 else
12878 complaint (&symfile_complaints,
216f72a1 12879 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 12880 "block nor reference, for DIE 0x%x [in module %s]"),
9c541725 12881 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12882
12883 call_site->per_cu = cu->per_cu;
12884
12885 for (child_die = die->child;
12886 child_die && child_die->tag;
12887 child_die = sibling_die (child_die))
12888 {
96408a79 12889 struct call_site_parameter *parameter;
1788b2d3 12890 struct attribute *loc, *origin;
96408a79 12891
216f72a1
JK
12892 if (child_die->tag != DW_TAG_call_site_parameter
12893 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
12894 {
12895 /* Already printed the complaint above. */
12896 continue;
12897 }
12898
12899 gdb_assert (call_site->parameter_count < nparams);
12900 parameter = &call_site->parameter[call_site->parameter_count];
12901
1788b2d3
JK
12902 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12903 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 12904 register is contained in DW_AT_call_value. */
96408a79 12905
24c5c679 12906 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
12907 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12908 if (origin == NULL)
12909 {
12910 /* This was a pre-DWARF-5 GNU extension alias
12911 for DW_AT_call_parameter. */
12912 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12913 }
7771576e 12914 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 12915 {
1788b2d3 12916 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
12917
12918 sect_offset sect_off
12919 = (sect_offset) dwarf2_get_ref_die_offset (origin);
12920 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
12921 {
12922 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12923 binding can be done only inside one CU. Such referenced DIE
12924 therefore cannot be even moved to DW_TAG_partial_unit. */
12925 complaint (&symfile_complaints,
216f72a1
JK
12926 _("DW_AT_call_parameter offset is not in CU for "
12927 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12928 to_underlying (child_die->sect_off),
12929 objfile_name (objfile));
d76b7dbc
JK
12930 continue;
12931 }
9c541725
PA
12932 parameter->u.param_cu_off
12933 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
12934 }
12935 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
12936 {
12937 complaint (&symfile_complaints,
12938 _("No DW_FORM_block* DW_AT_location for "
216f72a1 12939 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 12940 to_underlying (child_die->sect_off), objfile_name (objfile));
96408a79
SA
12941 continue;
12942 }
24c5c679 12943 else
96408a79 12944 {
24c5c679
JK
12945 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12946 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12947 if (parameter->u.dwarf_reg != -1)
12948 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12949 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12950 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12951 &parameter->u.fb_offset))
12952 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12953 else
12954 {
12955 complaint (&symfile_complaints,
12956 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12957 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 12958 "DW_TAG_call_site child DIE 0x%x "
24c5c679 12959 "[in module %s]"),
9c541725
PA
12960 to_underlying (child_die->sect_off),
12961 objfile_name (objfile));
24c5c679
JK
12962 continue;
12963 }
96408a79
SA
12964 }
12965
216f72a1
JK
12966 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12967 if (attr == NULL)
12968 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
12969 if (!attr_form_is_block (attr))
12970 {
12971 complaint (&symfile_complaints,
216f72a1
JK
12972 _("No DW_FORM_block* DW_AT_call_value for "
12973 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12974 to_underlying (child_die->sect_off),
12975 objfile_name (objfile));
96408a79
SA
12976 continue;
12977 }
12978 parameter->value = DW_BLOCK (attr)->data;
12979 parameter->value_size = DW_BLOCK (attr)->size;
12980
12981 /* Parameters are not pre-cleared by memset above. */
12982 parameter->data_value = NULL;
12983 parameter->data_value_size = 0;
12984 call_site->parameter_count++;
12985
216f72a1
JK
12986 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12987 if (attr == NULL)
12988 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
12989 if (attr)
12990 {
12991 if (!attr_form_is_block (attr))
12992 complaint (&symfile_complaints,
216f72a1
JK
12993 _("No DW_FORM_block* DW_AT_call_data_value for "
12994 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12995 to_underlying (child_die->sect_off),
12996 objfile_name (objfile));
96408a79
SA
12997 else
12998 {
12999 parameter->data_value = DW_BLOCK (attr)->data;
13000 parameter->data_value_size = DW_BLOCK (attr)->size;
13001 }
13002 }
13003 }
13004}
13005
71a3c369
TT
13006/* Helper function for read_variable. If DIE represents a virtual
13007 table, then return the type of the concrete object that is
13008 associated with the virtual table. Otherwise, return NULL. */
13009
13010static struct type *
13011rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13012{
13013 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13014 if (attr == NULL)
13015 return NULL;
13016
13017 /* Find the type DIE. */
13018 struct die_info *type_die = NULL;
13019 struct dwarf2_cu *type_cu = cu;
13020
13021 if (attr_form_is_ref (attr))
13022 type_die = follow_die_ref (die, attr, &type_cu);
13023 if (type_die == NULL)
13024 return NULL;
13025
13026 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13027 return NULL;
13028 return die_containing_type (type_die, type_cu);
13029}
13030
13031/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13032
13033static void
13034read_variable (struct die_info *die, struct dwarf2_cu *cu)
13035{
13036 struct rust_vtable_symbol *storage = NULL;
13037
13038 if (cu->language == language_rust)
13039 {
13040 struct type *containing_type = rust_containing_type (die, cu);
13041
13042 if (containing_type != NULL)
13043 {
13044 struct objfile *objfile = cu->objfile;
13045
13046 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
13047 struct rust_vtable_symbol);
13048 initialize_objfile_symbol (storage);
13049 storage->concrete_type = containing_type;
cf724bc9 13050 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13051 }
13052 }
13053
13054 new_symbol_full (die, NULL, cu, storage);
13055}
13056
43988095
JK
13057/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13058 reading .debug_rnglists.
13059 Callback's type should be:
13060 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13061 Return true if the attributes are present and valid, otherwise,
13062 return false. */
13063
13064template <typename Callback>
13065static bool
13066dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13067 Callback &&callback)
13068{
13069 struct objfile *objfile = cu->objfile;
13070 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13071 struct comp_unit_head *cu_header = &cu->header;
13072 bfd *obfd = objfile->obfd;
13073 unsigned int addr_size = cu_header->addr_size;
13074 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13075 /* Base address selection entry. */
13076 CORE_ADDR base;
13077 int found_base;
13078 unsigned int dummy;
13079 const gdb_byte *buffer;
13080 CORE_ADDR low = 0;
13081 CORE_ADDR high = 0;
13082 CORE_ADDR baseaddr;
13083 bool overflow = false;
13084
13085 found_base = cu->base_known;
13086 base = cu->base_address;
13087
13088 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
13089 if (offset >= dwarf2_per_objfile->rnglists.size)
13090 {
13091 complaint (&symfile_complaints,
13092 _("Offset %d out of bounds for DW_AT_ranges attribute"),
13093 offset);
13094 return false;
13095 }
13096 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13097
13098 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13099
13100 while (1)
13101 {
7814882a
JK
13102 /* Initialize it due to a false compiler warning. */
13103 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13104 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13105 + dwarf2_per_objfile->rnglists.size);
13106 unsigned int bytes_read;
13107
13108 if (buffer == buf_end)
13109 {
13110 overflow = true;
13111 break;
13112 }
13113 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13114 switch (rlet)
13115 {
13116 case DW_RLE_end_of_list:
13117 break;
13118 case DW_RLE_base_address:
13119 if (buffer + cu->header.addr_size > buf_end)
13120 {
13121 overflow = true;
13122 break;
13123 }
13124 base = read_address (obfd, buffer, cu, &bytes_read);
13125 found_base = 1;
13126 buffer += bytes_read;
13127 break;
13128 case DW_RLE_start_length:
13129 if (buffer + cu->header.addr_size > buf_end)
13130 {
13131 overflow = true;
13132 break;
13133 }
13134 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13135 buffer += bytes_read;
13136 range_end = (range_beginning
13137 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13138 buffer += bytes_read;
13139 if (buffer > buf_end)
13140 {
13141 overflow = true;
13142 break;
13143 }
13144 break;
13145 case DW_RLE_offset_pair:
13146 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13147 buffer += bytes_read;
13148 if (buffer > buf_end)
13149 {
13150 overflow = true;
13151 break;
13152 }
13153 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13154 buffer += bytes_read;
13155 if (buffer > buf_end)
13156 {
13157 overflow = true;
13158 break;
13159 }
13160 break;
13161 case DW_RLE_start_end:
13162 if (buffer + 2 * cu->header.addr_size > buf_end)
13163 {
13164 overflow = true;
13165 break;
13166 }
13167 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13168 buffer += bytes_read;
13169 range_end = read_address (obfd, buffer, cu, &bytes_read);
13170 buffer += bytes_read;
13171 break;
13172 default:
13173 complaint (&symfile_complaints,
13174 _("Invalid .debug_rnglists data (no base address)"));
13175 return false;
13176 }
13177 if (rlet == DW_RLE_end_of_list || overflow)
13178 break;
13179 if (rlet == DW_RLE_base_address)
13180 continue;
13181
13182 if (!found_base)
13183 {
13184 /* We have no valid base address for the ranges
13185 data. */
13186 complaint (&symfile_complaints,
13187 _("Invalid .debug_rnglists data (no base address)"));
13188 return false;
13189 }
13190
13191 if (range_beginning > range_end)
13192 {
13193 /* Inverted range entries are invalid. */
13194 complaint (&symfile_complaints,
13195 _("Invalid .debug_rnglists data (inverted range)"));
13196 return false;
13197 }
13198
13199 /* Empty range entries have no effect. */
13200 if (range_beginning == range_end)
13201 continue;
13202
13203 range_beginning += base;
13204 range_end += base;
13205
13206 /* A not-uncommon case of bad debug info.
13207 Don't pollute the addrmap with bad data. */
13208 if (range_beginning + baseaddr == 0
13209 && !dwarf2_per_objfile->has_section_at_zero)
13210 {
13211 complaint (&symfile_complaints,
13212 _(".debug_rnglists entry has start address of zero"
13213 " [in module %s]"), objfile_name (objfile));
13214 continue;
13215 }
13216
13217 callback (range_beginning, range_end);
13218 }
13219
13220 if (overflow)
13221 {
13222 complaint (&symfile_complaints,
13223 _("Offset %d is not terminated "
13224 "for DW_AT_ranges attribute"),
13225 offset);
13226 return false;
13227 }
13228
13229 return true;
13230}
13231
13232/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13233 Callback's type should be:
13234 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13235 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13236
43988095 13237template <typename Callback>
43039443 13238static int
5f46c5a5 13239dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13240 Callback &&callback)
43039443
JK
13241{
13242 struct objfile *objfile = cu->objfile;
3e29f34a 13243 struct gdbarch *gdbarch = get_objfile_arch (objfile);
43039443
JK
13244 struct comp_unit_head *cu_header = &cu->header;
13245 bfd *obfd = objfile->obfd;
13246 unsigned int addr_size = cu_header->addr_size;
13247 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13248 /* Base address selection entry. */
13249 CORE_ADDR base;
13250 int found_base;
13251 unsigned int dummy;
d521ce57 13252 const gdb_byte *buffer;
ff013f42 13253 CORE_ADDR baseaddr;
43039443 13254
43988095
JK
13255 if (cu_header->version >= 5)
13256 return dwarf2_rnglists_process (offset, cu, callback);
13257
d00adf39
DE
13258 found_base = cu->base_known;
13259 base = cu->base_address;
43039443 13260
be391dca 13261 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 13262 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
13263 {
13264 complaint (&symfile_complaints,
13265 _("Offset %d out of bounds for DW_AT_ranges attribute"),
13266 offset);
13267 return 0;
13268 }
dce234bc 13269 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 13270
e7030f15 13271 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 13272
43039443
JK
13273 while (1)
13274 {
13275 CORE_ADDR range_beginning, range_end;
13276
13277 range_beginning = read_address (obfd, buffer, cu, &dummy);
13278 buffer += addr_size;
13279 range_end = read_address (obfd, buffer, cu, &dummy);
13280 buffer += addr_size;
13281 offset += 2 * addr_size;
13282
13283 /* An end of list marker is a pair of zero addresses. */
13284 if (range_beginning == 0 && range_end == 0)
13285 /* Found the end of list entry. */
13286 break;
13287
13288 /* Each base address selection entry is a pair of 2 values.
13289 The first is the largest possible address, the second is
13290 the base address. Check for a base address here. */
13291 if ((range_beginning & mask) == mask)
13292 {
28d2bfb9
AB
13293 /* If we found the largest possible address, then we already
13294 have the base address in range_end. */
13295 base = range_end;
43039443
JK
13296 found_base = 1;
13297 continue;
13298 }
13299
13300 if (!found_base)
13301 {
13302 /* We have no valid base address for the ranges
13303 data. */
13304 complaint (&symfile_complaints,
13305 _("Invalid .debug_ranges data (no base address)"));
13306 return 0;
13307 }
13308
9277c30c
UW
13309 if (range_beginning > range_end)
13310 {
13311 /* Inverted range entries are invalid. */
13312 complaint (&symfile_complaints,
13313 _("Invalid .debug_ranges data (inverted range)"));
13314 return 0;
13315 }
13316
13317 /* Empty range entries have no effect. */
13318 if (range_beginning == range_end)
13319 continue;
13320
43039443
JK
13321 range_beginning += base;
13322 range_end += base;
13323
01093045
DE
13324 /* A not-uncommon case of bad debug info.
13325 Don't pollute the addrmap with bad data. */
13326 if (range_beginning + baseaddr == 0
13327 && !dwarf2_per_objfile->has_section_at_zero)
13328 {
13329 complaint (&symfile_complaints,
13330 _(".debug_ranges entry has start address of zero"
4262abfb 13331 " [in module %s]"), objfile_name (objfile));
01093045
DE
13332 continue;
13333 }
13334
5f46c5a5
JK
13335 callback (range_beginning, range_end);
13336 }
13337
13338 return 1;
13339}
13340
13341/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13342 Return 1 if the attributes are present and valid, otherwise, return 0.
13343 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13344
13345static int
13346dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13347 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13348 struct partial_symtab *ranges_pst)
13349{
13350 struct objfile *objfile = cu->objfile;
13351 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13352 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
13353 SECT_OFF_TEXT (objfile));
13354 int low_set = 0;
13355 CORE_ADDR low = 0;
13356 CORE_ADDR high = 0;
13357 int retval;
13358
13359 retval = dwarf2_ranges_process (offset, cu,
13360 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13361 {
9277c30c 13362 if (ranges_pst != NULL)
3e29f34a
MR
13363 {
13364 CORE_ADDR lowpc;
13365 CORE_ADDR highpc;
13366
13367 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
13368 range_beginning + baseaddr);
13369 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
13370 range_end + baseaddr);
13371 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
13372 ranges_pst);
13373 }
ff013f42 13374
43039443
JK
13375 /* FIXME: This is recording everything as a low-high
13376 segment of consecutive addresses. We should have a
13377 data structure for discontiguous block ranges
13378 instead. */
13379 if (! low_set)
13380 {
13381 low = range_beginning;
13382 high = range_end;
13383 low_set = 1;
13384 }
13385 else
13386 {
13387 if (range_beginning < low)
13388 low = range_beginning;
13389 if (range_end > high)
13390 high = range_end;
13391 }
5f46c5a5
JK
13392 });
13393 if (!retval)
13394 return 0;
43039443
JK
13395
13396 if (! low_set)
13397 /* If the first entry is an end-of-list marker, the range
13398 describes an empty scope, i.e. no instructions. */
13399 return 0;
13400
13401 if (low_return)
13402 *low_return = low;
13403 if (high_return)
13404 *high_return = high;
13405 return 1;
13406}
13407
3a2b436a
JK
13408/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13409 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13410 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13411
3a2b436a 13412static enum pc_bounds_kind
af34e669 13413dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
13414 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13415 struct partial_symtab *pst)
c906108c
SS
13416{
13417 struct attribute *attr;
91da1414 13418 struct attribute *attr_high;
af34e669
DJ
13419 CORE_ADDR low = 0;
13420 CORE_ADDR high = 0;
e385593e 13421 enum pc_bounds_kind ret;
c906108c 13422
91da1414
MW
13423 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13424 if (attr_high)
af34e669 13425 {
e142c38c 13426 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 13427 if (attr)
91da1414 13428 {
31aa7e4e
JB
13429 low = attr_value_as_address (attr);
13430 high = attr_value_as_address (attr_high);
13431 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
13432 high += low;
91da1414 13433 }
af34e669
DJ
13434 else
13435 /* Found high w/o low attribute. */
e385593e 13436 return PC_BOUNDS_INVALID;
af34e669
DJ
13437
13438 /* Found consecutive range of addresses. */
3a2b436a 13439 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13440 }
c906108c 13441 else
af34e669 13442 {
e142c38c 13443 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
13444 if (attr != NULL)
13445 {
ab435259
DE
13446 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
13447 We take advantage of the fact that DW_AT_ranges does not appear
13448 in DW_TAG_compile_unit of DWO files. */
13449 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13450 unsigned int ranges_offset = (DW_UNSND (attr)
13451 + (need_ranges_base
13452 ? cu->ranges_base
13453 : 0));
2e3cf129 13454
af34e669 13455 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 13456 .debug_ranges section. */
2e3cf129 13457 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 13458 return PC_BOUNDS_INVALID;
43039443 13459 /* Found discontinuous range of addresses. */
3a2b436a 13460 ret = PC_BOUNDS_RANGES;
af34e669 13461 }
e385593e
JK
13462 else
13463 return PC_BOUNDS_NOT_PRESENT;
af34e669 13464 }
c906108c 13465
9373cf26
JK
13466 /* read_partial_die has also the strict LOW < HIGH requirement. */
13467 if (high <= low)
e385593e 13468 return PC_BOUNDS_INVALID;
c906108c
SS
13469
13470 /* When using the GNU linker, .gnu.linkonce. sections are used to
13471 eliminate duplicate copies of functions and vtables and such.
13472 The linker will arbitrarily choose one and discard the others.
13473 The AT_*_pc values for such functions refer to local labels in
13474 these sections. If the section from that file was discarded, the
13475 labels are not in the output, so the relocs get a value of 0.
13476 If this is a discarded function, mark the pc bounds as invalid,
13477 so that GDB will ignore it. */
72dca2f5 13478 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 13479 return PC_BOUNDS_INVALID;
c906108c
SS
13480
13481 *lowpc = low;
96408a79
SA
13482 if (highpc)
13483 *highpc = high;
af34e669 13484 return ret;
c906108c
SS
13485}
13486
b084d499
JB
13487/* Assuming that DIE represents a subprogram DIE or a lexical block, get
13488 its low and high PC addresses. Do nothing if these addresses could not
13489 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13490 and HIGHPC to the high address if greater than HIGHPC. */
13491
13492static void
13493dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13494 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13495 struct dwarf2_cu *cu)
13496{
13497 CORE_ADDR low, high;
13498 struct die_info *child = die->child;
13499
e385593e 13500 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 13501 {
325fac50
PA
13502 *lowpc = std::min (*lowpc, low);
13503 *highpc = std::max (*highpc, high);
b084d499
JB
13504 }
13505
13506 /* If the language does not allow nested subprograms (either inside
13507 subprograms or lexical blocks), we're done. */
13508 if (cu->language != language_ada)
13509 return;
6e70227d 13510
b084d499
JB
13511 /* Check all the children of the given DIE. If it contains nested
13512 subprograms, then check their pc bounds. Likewise, we need to
13513 check lexical blocks as well, as they may also contain subprogram
13514 definitions. */
13515 while (child && child->tag)
13516 {
13517 if (child->tag == DW_TAG_subprogram
13518 || child->tag == DW_TAG_lexical_block)
13519 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13520 child = sibling_die (child);
13521 }
13522}
13523
fae299cd
DC
13524/* Get the low and high pc's represented by the scope DIE, and store
13525 them in *LOWPC and *HIGHPC. If the correct values can't be
13526 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13527
13528static void
13529get_scope_pc_bounds (struct die_info *die,
13530 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13531 struct dwarf2_cu *cu)
13532{
13533 CORE_ADDR best_low = (CORE_ADDR) -1;
13534 CORE_ADDR best_high = (CORE_ADDR) 0;
13535 CORE_ADDR current_low, current_high;
13536
3a2b436a 13537 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 13538 >= PC_BOUNDS_RANGES)
fae299cd
DC
13539 {
13540 best_low = current_low;
13541 best_high = current_high;
13542 }
13543 else
13544 {
13545 struct die_info *child = die->child;
13546
13547 while (child && child->tag)
13548 {
13549 switch (child->tag) {
13550 case DW_TAG_subprogram:
b084d499 13551 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
13552 break;
13553 case DW_TAG_namespace:
f55ee35c 13554 case DW_TAG_module:
fae299cd
DC
13555 /* FIXME: carlton/2004-01-16: Should we do this for
13556 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13557 that current GCC's always emit the DIEs corresponding
13558 to definitions of methods of classes as children of a
13559 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13560 the DIEs giving the declarations, which could be
13561 anywhere). But I don't see any reason why the
13562 standards says that they have to be there. */
13563 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13564
13565 if (current_low != ((CORE_ADDR) -1))
13566 {
325fac50
PA
13567 best_low = std::min (best_low, current_low);
13568 best_high = std::max (best_high, current_high);
fae299cd
DC
13569 }
13570 break;
13571 default:
0963b4bd 13572 /* Ignore. */
fae299cd
DC
13573 break;
13574 }
13575
13576 child = sibling_die (child);
13577 }
13578 }
13579
13580 *lowpc = best_low;
13581 *highpc = best_high;
13582}
13583
801e3a5b
JB
13584/* Record the address ranges for BLOCK, offset by BASEADDR, as given
13585 in DIE. */
380bca97 13586
801e3a5b
JB
13587static void
13588dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13589 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13590{
bb5ed363 13591 struct objfile *objfile = cu->objfile;
3e29f34a 13592 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 13593 struct attribute *attr;
91da1414 13594 struct attribute *attr_high;
801e3a5b 13595
91da1414
MW
13596 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13597 if (attr_high)
801e3a5b 13598 {
801e3a5b
JB
13599 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13600 if (attr)
13601 {
31aa7e4e
JB
13602 CORE_ADDR low = attr_value_as_address (attr);
13603 CORE_ADDR high = attr_value_as_address (attr_high);
13604
13605 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
13606 high += low;
9a619af0 13607
3e29f34a
MR
13608 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13609 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13610 record_block_range (block, low, high - 1);
801e3a5b
JB
13611 }
13612 }
13613
13614 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13615 if (attr)
13616 {
bb5ed363 13617 bfd *obfd = objfile->obfd;
ab435259
DE
13618 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
13619 We take advantage of the fact that DW_AT_ranges does not appear
13620 in DW_TAG_compile_unit of DWO files. */
13621 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
13622
13623 /* The value of the DW_AT_ranges attribute is the offset of the
13624 address range list in the .debug_ranges section. */
ab435259
DE
13625 unsigned long offset = (DW_UNSND (attr)
13626 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 13627 const gdb_byte *buffer;
801e3a5b
JB
13628
13629 /* For some target architectures, but not others, the
13630 read_address function sign-extends the addresses it returns.
13631 To recognize base address selection entries, we need a
13632 mask. */
13633 unsigned int addr_size = cu->header.addr_size;
13634 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13635
13636 /* The base address, to which the next pair is relative. Note
13637 that this 'base' is a DWARF concept: most entries in a range
13638 list are relative, to reduce the number of relocs against the
13639 debugging information. This is separate from this function's
13640 'baseaddr' argument, which GDB uses to relocate debugging
13641 information from a shared library based on the address at
13642 which the library was loaded. */
d00adf39
DE
13643 CORE_ADDR base = cu->base_address;
13644 int base_known = cu->base_known;
801e3a5b 13645
5f46c5a5
JK
13646 dwarf2_ranges_process (offset, cu,
13647 [&] (CORE_ADDR start, CORE_ADDR end)
13648 {
58fdfd2c
JK
13649 start += baseaddr;
13650 end += baseaddr;
5f46c5a5
JK
13651 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13652 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13653 record_block_range (block, start, end - 1);
13654 });
801e3a5b
JB
13655 }
13656}
13657
685b1105
JK
13658/* Check whether the producer field indicates either of GCC < 4.6, or the
13659 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 13660
685b1105
JK
13661static void
13662check_producer (struct dwarf2_cu *cu)
60d5a603 13663{
38360086 13664 int major, minor;
60d5a603
JK
13665
13666 if (cu->producer == NULL)
13667 {
13668 /* For unknown compilers expect their behavior is DWARF version
13669 compliant.
13670
13671 GCC started to support .debug_types sections by -gdwarf-4 since
13672 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13673 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13674 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13675 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 13676 }
b1ffba5a 13677 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 13678 {
38360086
MW
13679 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13680 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 13681 }
5230b05a
WT
13682 else if (producer_is_icc (cu->producer, &major, &minor))
13683 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
13684 else
13685 {
13686 /* For other non-GCC compilers, expect their behavior is DWARF version
13687 compliant. */
60d5a603
JK
13688 }
13689
ba919b58 13690 cu->checked_producer = 1;
685b1105 13691}
ba919b58 13692
685b1105
JK
13693/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13694 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13695 during 4.6.0 experimental. */
13696
13697static int
13698producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
13699{
13700 if (!cu->checked_producer)
13701 check_producer (cu);
13702
13703 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
13704}
13705
13706/* Return the default accessibility type if it is not overriden by
13707 DW_AT_accessibility. */
13708
13709static enum dwarf_access_attribute
13710dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
13711{
13712 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
13713 {
13714 /* The default DWARF 2 accessibility for members is public, the default
13715 accessibility for inheritance is private. */
13716
13717 if (die->tag != DW_TAG_inheritance)
13718 return DW_ACCESS_public;
13719 else
13720 return DW_ACCESS_private;
13721 }
13722 else
13723 {
13724 /* DWARF 3+ defines the default accessibility a different way. The same
13725 rules apply now for DW_TAG_inheritance as for the members and it only
13726 depends on the container kind. */
13727
13728 if (die->parent->tag == DW_TAG_class_type)
13729 return DW_ACCESS_private;
13730 else
13731 return DW_ACCESS_public;
13732 }
13733}
13734
74ac6d43
TT
13735/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
13736 offset. If the attribute was not found return 0, otherwise return
13737 1. If it was found but could not properly be handled, set *OFFSET
13738 to 0. */
13739
13740static int
13741handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
13742 LONGEST *offset)
13743{
13744 struct attribute *attr;
13745
13746 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13747 if (attr != NULL)
13748 {
13749 *offset = 0;
13750
13751 /* Note that we do not check for a section offset first here.
13752 This is because DW_AT_data_member_location is new in DWARF 4,
13753 so if we see it, we can assume that a constant form is really
13754 a constant and not a section offset. */
13755 if (attr_form_is_constant (attr))
13756 *offset = dwarf2_get_attr_constant_value (attr, 0);
13757 else if (attr_form_is_section_offset (attr))
13758 dwarf2_complex_location_expr_complaint ();
13759 else if (attr_form_is_block (attr))
13760 *offset = decode_locdesc (DW_BLOCK (attr), cu);
13761 else
13762 dwarf2_complex_location_expr_complaint ();
13763
13764 return 1;
13765 }
13766
13767 return 0;
13768}
13769
c906108c
SS
13770/* Add an aggregate field to the field list. */
13771
13772static void
107d2387 13773dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 13774 struct dwarf2_cu *cu)
6e70227d 13775{
e7c27a73 13776 struct objfile *objfile = cu->objfile;
5e2b427d 13777 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13778 struct nextfield *new_field;
13779 struct attribute *attr;
13780 struct field *fp;
15d034d0 13781 const char *fieldname = "";
c906108c
SS
13782
13783 /* Allocate a new field list entry and link it in. */
8d749320 13784 new_field = XNEW (struct nextfield);
b8c9b27d 13785 make_cleanup (xfree, new_field);
c906108c 13786 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
13787
13788 if (die->tag == DW_TAG_inheritance)
13789 {
13790 new_field->next = fip->baseclasses;
13791 fip->baseclasses = new_field;
13792 }
13793 else
13794 {
13795 new_field->next = fip->fields;
13796 fip->fields = new_field;
13797 }
c906108c
SS
13798 fip->nfields++;
13799
e142c38c 13800 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
13801 if (attr)
13802 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
13803 else
13804 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
13805 if (new_field->accessibility != DW_ACCESS_public)
13806 fip->non_public_fields = 1;
60d5a603 13807
e142c38c 13808 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
13809 if (attr)
13810 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
13811 else
13812 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
13813
13814 fp = &new_field->field;
a9a9bd0f 13815
e142c38c 13816 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 13817 {
74ac6d43
TT
13818 LONGEST offset;
13819
a9a9bd0f 13820 /* Data member other than a C++ static data member. */
6e70227d 13821
c906108c 13822 /* Get type of field. */
e7c27a73 13823 fp->type = die_type (die, cu);
c906108c 13824
d6a843b5 13825 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 13826
c906108c 13827 /* Get bit size of field (zero if none). */
e142c38c 13828 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
13829 if (attr)
13830 {
13831 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
13832 }
13833 else
13834 {
13835 FIELD_BITSIZE (*fp) = 0;
13836 }
13837
13838 /* Get bit offset of field. */
74ac6d43
TT
13839 if (handle_data_member_location (die, cu, &offset))
13840 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 13841 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
13842 if (attr)
13843 {
5e2b427d 13844 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
13845 {
13846 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
13847 additional bit offset from the MSB of the containing
13848 anonymous object to the MSB of the field. We don't
13849 have to do anything special since we don't need to
13850 know the size of the anonymous object. */
f41f5e61 13851 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
13852 }
13853 else
13854 {
13855 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
13856 MSB of the anonymous object, subtract off the number of
13857 bits from the MSB of the field to the MSB of the
13858 object, and then subtract off the number of bits of
13859 the field itself. The result is the bit offset of
13860 the LSB of the field. */
c906108c
SS
13861 int anonymous_size;
13862 int bit_offset = DW_UNSND (attr);
13863
e142c38c 13864 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13865 if (attr)
13866 {
13867 /* The size of the anonymous object containing
13868 the bit field is explicit, so use the
13869 indicated size (in bytes). */
13870 anonymous_size = DW_UNSND (attr);
13871 }
13872 else
13873 {
13874 /* The size of the anonymous object containing
13875 the bit field must be inferred from the type
13876 attribute of the data member containing the
13877 bit field. */
13878 anonymous_size = TYPE_LENGTH (fp->type);
13879 }
f41f5e61
PA
13880 SET_FIELD_BITPOS (*fp,
13881 (FIELD_BITPOS (*fp)
13882 + anonymous_size * bits_per_byte
13883 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
13884 }
13885 }
da5b30da
AA
13886 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13887 if (attr != NULL)
13888 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
13889 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
13890
13891 /* Get name of field. */
39cbfefa
DJ
13892 fieldname = dwarf2_name (die, cu);
13893 if (fieldname == NULL)
13894 fieldname = "";
d8151005
DJ
13895
13896 /* The name is already allocated along with this objfile, so we don't
13897 need to duplicate it for the type. */
13898 fp->name = fieldname;
c906108c
SS
13899
13900 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 13901 pointer or virtual base class pointer) to private. */
e142c38c 13902 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 13903 {
d48cc9dd 13904 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
13905 new_field->accessibility = DW_ACCESS_private;
13906 fip->non_public_fields = 1;
13907 }
13908 }
a9a9bd0f 13909 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 13910 {
a9a9bd0f
DC
13911 /* C++ static member. */
13912
13913 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13914 is a declaration, but all versions of G++ as of this writing
13915 (so through at least 3.2.1) incorrectly generate
13916 DW_TAG_variable tags. */
6e70227d 13917
ff355380 13918 const char *physname;
c906108c 13919
a9a9bd0f 13920 /* Get name of field. */
39cbfefa
DJ
13921 fieldname = dwarf2_name (die, cu);
13922 if (fieldname == NULL)
c906108c
SS
13923 return;
13924
254e6b9e 13925 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
13926 if (attr
13927 /* Only create a symbol if this is an external value.
13928 new_symbol checks this and puts the value in the global symbol
13929 table, which we want. If it is not external, new_symbol
13930 will try to put the value in cu->list_in_scope which is wrong. */
13931 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
13932 {
13933 /* A static const member, not much different than an enum as far as
13934 we're concerned, except that we can support more types. */
13935 new_symbol (die, NULL, cu);
13936 }
13937
2df3850c 13938 /* Get physical name. */
ff355380 13939 physname = dwarf2_physname (fieldname, die, cu);
c906108c 13940
d8151005
DJ
13941 /* The name is already allocated along with this objfile, so we don't
13942 need to duplicate it for the type. */
13943 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 13944 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 13945 FIELD_NAME (*fp) = fieldname;
c906108c
SS
13946 }
13947 else if (die->tag == DW_TAG_inheritance)
13948 {
74ac6d43 13949 LONGEST offset;
d4b96c9a 13950
74ac6d43
TT
13951 /* C++ base class field. */
13952 if (handle_data_member_location (die, cu, &offset))
13953 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 13954 FIELD_BITSIZE (*fp) = 0;
e7c27a73 13955 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
13956 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
13957 fip->nbaseclasses++;
13958 }
13959}
13960
98751a41
JK
13961/* Add a typedef defined in the scope of the FIP's class. */
13962
13963static void
13964dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
13965 struct dwarf2_cu *cu)
6e70227d 13966{
98751a41 13967 struct typedef_field_list *new_field;
98751a41 13968 struct typedef_field *fp;
98751a41
JK
13969
13970 /* Allocate a new field list entry and link it in. */
8d749320 13971 new_field = XCNEW (struct typedef_field_list);
98751a41
JK
13972 make_cleanup (xfree, new_field);
13973
13974 gdb_assert (die->tag == DW_TAG_typedef);
13975
13976 fp = &new_field->field;
13977
13978 /* Get name of field. */
13979 fp->name = dwarf2_name (die, cu);
13980 if (fp->name == NULL)
13981 return;
13982
13983 fp->type = read_type_die (die, cu);
13984
c191a687
KS
13985 /* Save accessibility. */
13986 enum dwarf_access_attribute accessibility;
13987 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13988 if (attr != NULL)
13989 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
13990 else
13991 accessibility = dwarf2_default_access_attribute (die, cu);
13992 switch (accessibility)
13993 {
13994 case DW_ACCESS_public:
13995 /* The assumed value if neither private nor protected. */
13996 break;
13997 case DW_ACCESS_private:
13998 fp->is_private = 1;
13999 break;
14000 case DW_ACCESS_protected:
14001 fp->is_protected = 1;
14002 break;
14003 default:
37534686
KS
14004 complaint (&symfile_complaints,
14005 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14006 }
14007
98751a41
JK
14008 new_field->next = fip->typedef_field_list;
14009 fip->typedef_field_list = new_field;
14010 fip->typedef_field_list_count++;
14011}
14012
c906108c
SS
14013/* Create the vector of fields, and attach it to the type. */
14014
14015static void
fba45db2 14016dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14017 struct dwarf2_cu *cu)
c906108c
SS
14018{
14019 int nfields = fip->nfields;
14020
14021 /* Record the field count, allocate space for the array of fields,
14022 and create blank accessibility bitfields if necessary. */
14023 TYPE_NFIELDS (type) = nfields;
14024 TYPE_FIELDS (type) = (struct field *)
14025 TYPE_ALLOC (type, sizeof (struct field) * nfields);
14026 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
14027
b4ba55a1 14028 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14029 {
14030 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14031
14032 TYPE_FIELD_PRIVATE_BITS (type) =
14033 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14034 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14035
14036 TYPE_FIELD_PROTECTED_BITS (type) =
14037 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14038 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14039
774b6a14
TT
14040 TYPE_FIELD_IGNORE_BITS (type) =
14041 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14042 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14043 }
14044
14045 /* If the type has baseclasses, allocate and clear a bit vector for
14046 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 14047 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
14048 {
14049 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 14050 unsigned char *pointer;
c906108c
SS
14051
14052 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14053 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14054 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
14055 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
14056 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
14057 }
14058
3e43a32a
MS
14059 /* Copy the saved-up fields into the field vector. Start from the head of
14060 the list, adding to the tail of the field array, so that they end up in
14061 the same order in the array in which they were added to the list. */
c906108c
SS
14062 while (nfields-- > 0)
14063 {
7d0ccb61
DJ
14064 struct nextfield *fieldp;
14065
14066 if (fip->fields)
14067 {
14068 fieldp = fip->fields;
14069 fip->fields = fieldp->next;
14070 }
14071 else
14072 {
14073 fieldp = fip->baseclasses;
14074 fip->baseclasses = fieldp->next;
14075 }
14076
14077 TYPE_FIELD (type, nfields) = fieldp->field;
14078 switch (fieldp->accessibility)
c906108c 14079 {
c5aa993b 14080 case DW_ACCESS_private:
b4ba55a1
JB
14081 if (cu->language != language_ada)
14082 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 14083 break;
c906108c 14084
c5aa993b 14085 case DW_ACCESS_protected:
b4ba55a1
JB
14086 if (cu->language != language_ada)
14087 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 14088 break;
c906108c 14089
c5aa993b
JM
14090 case DW_ACCESS_public:
14091 break;
c906108c 14092
c5aa993b
JM
14093 default:
14094 /* Unknown accessibility. Complain and treat it as public. */
14095 {
e2e0b3e5 14096 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 14097 fieldp->accessibility);
c5aa993b
JM
14098 }
14099 break;
c906108c
SS
14100 }
14101 if (nfields < fip->nbaseclasses)
14102 {
7d0ccb61 14103 switch (fieldp->virtuality)
c906108c 14104 {
c5aa993b
JM
14105 case DW_VIRTUALITY_virtual:
14106 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14107 if (cu->language == language_ada)
a73c6dcd 14108 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
14109 SET_TYPE_FIELD_VIRTUAL (type, nfields);
14110 break;
c906108c
SS
14111 }
14112 }
c906108c
SS
14113 }
14114}
14115
7d27a96d
TT
14116/* Return true if this member function is a constructor, false
14117 otherwise. */
14118
14119static int
14120dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14121{
14122 const char *fieldname;
fe978cb0 14123 const char *type_name;
7d27a96d
TT
14124 int len;
14125
14126 if (die->parent == NULL)
14127 return 0;
14128
14129 if (die->parent->tag != DW_TAG_structure_type
14130 && die->parent->tag != DW_TAG_union_type
14131 && die->parent->tag != DW_TAG_class_type)
14132 return 0;
14133
14134 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14135 type_name = dwarf2_name (die->parent, cu);
14136 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14137 return 0;
14138
14139 len = strlen (fieldname);
fe978cb0
PA
14140 return (strncmp (fieldname, type_name, len) == 0
14141 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14142}
14143
c906108c
SS
14144/* Add a member function to the proper fieldlist. */
14145
14146static void
107d2387 14147dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14148 struct type *type, struct dwarf2_cu *cu)
c906108c 14149{
e7c27a73 14150 struct objfile *objfile = cu->objfile;
c906108c
SS
14151 struct attribute *attr;
14152 struct fnfieldlist *flp;
14153 int i;
14154 struct fn_field *fnp;
15d034d0 14155 const char *fieldname;
c906108c 14156 struct nextfnfield *new_fnfield;
f792889a 14157 struct type *this_type;
60d5a603 14158 enum dwarf_access_attribute accessibility;
c906108c 14159
b4ba55a1 14160 if (cu->language == language_ada)
a73c6dcd 14161 error (_("unexpected member function in Ada type"));
b4ba55a1 14162
2df3850c 14163 /* Get name of member function. */
39cbfefa
DJ
14164 fieldname = dwarf2_name (die, cu);
14165 if (fieldname == NULL)
2df3850c 14166 return;
c906108c 14167
c906108c
SS
14168 /* Look up member function name in fieldlist. */
14169 for (i = 0; i < fip->nfnfields; i++)
14170 {
27bfe10e 14171 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
14172 break;
14173 }
14174
14175 /* Create new list element if necessary. */
14176 if (i < fip->nfnfields)
14177 flp = &fip->fnfieldlists[i];
14178 else
14179 {
14180 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
14181 {
14182 fip->fnfieldlists = (struct fnfieldlist *)
14183 xrealloc (fip->fnfieldlists,
14184 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 14185 * sizeof (struct fnfieldlist));
c906108c 14186 if (fip->nfnfields == 0)
c13c43fd 14187 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
14188 }
14189 flp = &fip->fnfieldlists[fip->nfnfields];
14190 flp->name = fieldname;
14191 flp->length = 0;
14192 flp->head = NULL;
3da10d80 14193 i = fip->nfnfields++;
c906108c
SS
14194 }
14195
14196 /* Create a new member function field and chain it to the field list
0963b4bd 14197 entry. */
8d749320 14198 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 14199 make_cleanup (xfree, new_fnfield);
c906108c
SS
14200 memset (new_fnfield, 0, sizeof (struct nextfnfield));
14201 new_fnfield->next = flp->head;
14202 flp->head = new_fnfield;
14203 flp->length++;
14204
14205 /* Fill in the member function field info. */
14206 fnp = &new_fnfield->fnfield;
3da10d80
KS
14207
14208 /* Delay processing of the physname until later. */
9c37b5ae 14209 if (cu->language == language_cplus)
3da10d80
KS
14210 {
14211 add_to_method_list (type, i, flp->length - 1, fieldname,
14212 die, cu);
14213 }
14214 else
14215 {
1d06ead6 14216 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14217 fnp->physname = physname ? physname : "";
14218 }
14219
c906108c 14220 fnp->type = alloc_type (objfile);
f792889a
DJ
14221 this_type = read_type_die (die, cu);
14222 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 14223 {
f792889a 14224 int nparams = TYPE_NFIELDS (this_type);
c906108c 14225
f792889a 14226 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14227 of the method itself (TYPE_CODE_METHOD). */
14228 smash_to_method_type (fnp->type, type,
f792889a
DJ
14229 TYPE_TARGET_TYPE (this_type),
14230 TYPE_FIELDS (this_type),
14231 TYPE_NFIELDS (this_type),
14232 TYPE_VARARGS (this_type));
c906108c
SS
14233
14234 /* Handle static member functions.
c5aa993b 14235 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
14236 member functions. G++ helps GDB by marking the first
14237 parameter for non-static member functions (which is the this
14238 pointer) as artificial. We obtain this information from
14239 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 14240 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
14241 fnp->voffset = VOFFSET_STATIC;
14242 }
14243 else
e2e0b3e5 14244 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 14245 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
14246
14247 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 14248 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 14249 fnp->fcontext = die_containing_type (die, cu);
c906108c 14250
3e43a32a
MS
14251 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14252 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
14253
14254 /* Get accessibility. */
e142c38c 14255 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 14256 if (attr)
aead7601 14257 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
14258 else
14259 accessibility = dwarf2_default_access_attribute (die, cu);
14260 switch (accessibility)
c906108c 14261 {
60d5a603
JK
14262 case DW_ACCESS_private:
14263 fnp->is_private = 1;
14264 break;
14265 case DW_ACCESS_protected:
14266 fnp->is_protected = 1;
14267 break;
c906108c
SS
14268 }
14269
b02dede2 14270 /* Check for artificial methods. */
e142c38c 14271 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
14272 if (attr && DW_UNSND (attr) != 0)
14273 fnp->is_artificial = 1;
14274
7d27a96d
TT
14275 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14276
0d564a31 14277 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
14278 function. For older versions of GCC, this is an offset in the
14279 appropriate virtual table, as specified by DW_AT_containing_type.
14280 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
14281 to the object address. */
14282
e142c38c 14283 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 14284 if (attr)
8e19ed76 14285 {
aec5aa8b 14286 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 14287 {
aec5aa8b
TT
14288 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14289 {
14290 /* Old-style GCC. */
14291 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14292 }
14293 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14294 || (DW_BLOCK (attr)->size > 1
14295 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14296 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14297 {
aec5aa8b
TT
14298 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14299 if ((fnp->voffset % cu->header.addr_size) != 0)
14300 dwarf2_complex_location_expr_complaint ();
14301 else
14302 fnp->voffset /= cu->header.addr_size;
14303 fnp->voffset += 2;
14304 }
14305 else
14306 dwarf2_complex_location_expr_complaint ();
14307
14308 if (!fnp->fcontext)
7e993ebf
KS
14309 {
14310 /* If there is no `this' field and no DW_AT_containing_type,
14311 we cannot actually find a base class context for the
14312 vtable! */
14313 if (TYPE_NFIELDS (this_type) == 0
14314 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14315 {
14316 complaint (&symfile_complaints,
14317 _("cannot determine context for virtual member "
14318 "function \"%s\" (offset %d)"),
9c541725 14319 fieldname, to_underlying (die->sect_off));
7e993ebf
KS
14320 }
14321 else
14322 {
14323 fnp->fcontext
14324 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14325 }
14326 }
aec5aa8b 14327 }
3690dd37 14328 else if (attr_form_is_section_offset (attr))
8e19ed76 14329 {
4d3c2250 14330 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14331 }
14332 else
14333 {
4d3c2250
KB
14334 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14335 fieldname);
8e19ed76 14336 }
0d564a31 14337 }
d48cc9dd
DJ
14338 else
14339 {
14340 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14341 if (attr && DW_UNSND (attr))
14342 {
14343 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14344 complaint (&symfile_complaints,
3e43a32a
MS
14345 _("Member function \"%s\" (offset %d) is virtual "
14346 "but the vtable offset is not specified"),
9c541725 14347 fieldname, to_underlying (die->sect_off));
9655fd1a 14348 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
14349 TYPE_CPLUS_DYNAMIC (type) = 1;
14350 }
14351 }
c906108c
SS
14352}
14353
14354/* Create the vector of member function fields, and attach it to the type. */
14355
14356static void
fba45db2 14357dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14358 struct dwarf2_cu *cu)
c906108c
SS
14359{
14360 struct fnfieldlist *flp;
c906108c
SS
14361 int i;
14362
b4ba55a1 14363 if (cu->language == language_ada)
a73c6dcd 14364 error (_("unexpected member functions in Ada type"));
b4ba55a1 14365
c906108c
SS
14366 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14367 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14368 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
14369
14370 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
14371 {
14372 struct nextfnfield *nfp = flp->head;
14373 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14374 int k;
14375
14376 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
14377 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
14378 fn_flp->fn_fields = (struct fn_field *)
14379 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
14380 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 14381 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
14382 }
14383
14384 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
14385}
14386
1168df01
JB
14387/* Returns non-zero if NAME is the name of a vtable member in CU's
14388 language, zero otherwise. */
14389static int
14390is_vtable_name (const char *name, struct dwarf2_cu *cu)
14391{
14392 static const char vptr[] = "_vptr";
987504bb 14393 static const char vtable[] = "vtable";
1168df01 14394
9c37b5ae
TT
14395 /* Look for the C++ form of the vtable. */
14396 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
14397 return 1;
14398
14399 return 0;
14400}
14401
c0dd20ea 14402/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
14403 functions, with the ABI-specified layout. If TYPE describes
14404 such a structure, smash it into a member function type.
61049d3b
DJ
14405
14406 GCC shouldn't do this; it should just output pointer to member DIEs.
14407 This is GCC PR debug/28767. */
c0dd20ea 14408
0b92b5bb
TT
14409static void
14410quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 14411{
09e2d7c7 14412 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
14413
14414 /* Check for a structure with no name and two children. */
0b92b5bb
TT
14415 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14416 return;
c0dd20ea
DJ
14417
14418 /* Check for __pfn and __delta members. */
0b92b5bb
TT
14419 if (TYPE_FIELD_NAME (type, 0) == NULL
14420 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14421 || TYPE_FIELD_NAME (type, 1) == NULL
14422 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14423 return;
c0dd20ea
DJ
14424
14425 /* Find the type of the method. */
0b92b5bb 14426 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
14427 if (pfn_type == NULL
14428 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14429 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 14430 return;
c0dd20ea
DJ
14431
14432 /* Look for the "this" argument. */
14433 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14434 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 14435 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 14436 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 14437 return;
c0dd20ea 14438
09e2d7c7 14439 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 14440 new_type = alloc_type (objfile);
09e2d7c7 14441 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
14442 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14443 TYPE_VARARGS (pfn_type));
0b92b5bb 14444 smash_to_methodptr_type (type, new_type);
c0dd20ea 14445}
1168df01 14446
685b1105 14447
c906108c 14448/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
14449 (definition) to create a type for the structure or union. Fill in
14450 the type's name and general properties; the members will not be
83655187
DE
14451 processed until process_structure_scope. A symbol table entry for
14452 the type will also not be done until process_structure_scope (assuming
14453 the type has a name).
c906108c 14454
c767944b
DJ
14455 NOTE: we need to call these functions regardless of whether or not the
14456 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 14457 structure or union. This gets the type entered into our set of
83655187 14458 user defined types. */
c906108c 14459
f792889a 14460static struct type *
134d01f1 14461read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14462{
e7c27a73 14463 struct objfile *objfile = cu->objfile;
c906108c
SS
14464 struct type *type;
14465 struct attribute *attr;
15d034d0 14466 const char *name;
c906108c 14467
348e048f
DE
14468 /* If the definition of this type lives in .debug_types, read that type.
14469 Don't follow DW_AT_specification though, that will take us back up
14470 the chain and we want to go down. */
45e58e77 14471 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
14472 if (attr)
14473 {
ac9ec31b 14474 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 14475
ac9ec31b 14476 /* The type's CU may not be the same as CU.
02142a6c 14477 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
14478 return set_die_type (die, type, cu);
14479 }
14480
c0dd20ea 14481 type = alloc_type (objfile);
c906108c 14482 INIT_CPLUS_SPECIFIC (type);
93311388 14483
39cbfefa
DJ
14484 name = dwarf2_name (die, cu);
14485 if (name != NULL)
c906108c 14486 {
987504bb 14487 if (cu->language == language_cplus
c44af4eb
TT
14488 || cu->language == language_d
14489 || cu->language == language_rust)
63d06c5c 14490 {
15d034d0 14491 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
14492
14493 /* dwarf2_full_name might have already finished building the DIE's
14494 type. If so, there is no need to continue. */
14495 if (get_die_type (die, cu) != NULL)
14496 return get_die_type (die, cu);
14497
14498 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
14499 if (die->tag == DW_TAG_structure_type
14500 || die->tag == DW_TAG_class_type)
14501 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
14502 }
14503 else
14504 {
d8151005
DJ
14505 /* The name is already allocated along with this objfile, so
14506 we don't need to duplicate it for the type. */
7d455152 14507 TYPE_TAG_NAME (type) = name;
94af9270
KS
14508 if (die->tag == DW_TAG_class_type)
14509 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 14510 }
c906108c
SS
14511 }
14512
14513 if (die->tag == DW_TAG_structure_type)
14514 {
14515 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14516 }
14517 else if (die->tag == DW_TAG_union_type)
14518 {
14519 TYPE_CODE (type) = TYPE_CODE_UNION;
14520 }
14521 else
14522 {
4753d33b 14523 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
14524 }
14525
0cc2414c
TT
14526 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
14527 TYPE_DECLARED_CLASS (type) = 1;
14528
e142c38c 14529 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14530 if (attr)
14531 {
155bfbd3
JB
14532 if (attr_form_is_constant (attr))
14533 TYPE_LENGTH (type) = DW_UNSND (attr);
14534 else
14535 {
14536 /* For the moment, dynamic type sizes are not supported
14537 by GDB's struct type. The actual size is determined
14538 on-demand when resolving the type of a given object,
14539 so set the type's length to zero for now. Otherwise,
14540 we record an expression as the length, and that expression
14541 could lead to a very large value, which could eventually
14542 lead to us trying to allocate that much memory when creating
14543 a value of that type. */
14544 TYPE_LENGTH (type) = 0;
14545 }
c906108c
SS
14546 }
14547 else
14548 {
14549 TYPE_LENGTH (type) = 0;
14550 }
14551
5230b05a 14552 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 14553 {
5230b05a
WT
14554 /* ICC<14 does not output the required DW_AT_declaration on
14555 incomplete types, but gives them a size of zero. */
422b1cb0 14556 TYPE_STUB (type) = 1;
685b1105
JK
14557 }
14558 else
14559 TYPE_STUB_SUPPORTED (type) = 1;
14560
dc718098 14561 if (die_is_declaration (die, cu))
876cecd0 14562 TYPE_STUB (type) = 1;
a6c727b2
DJ
14563 else if (attr == NULL && die->child == NULL
14564 && producer_is_realview (cu->producer))
14565 /* RealView does not output the required DW_AT_declaration
14566 on incomplete types. */
14567 TYPE_STUB (type) = 1;
dc718098 14568
c906108c
SS
14569 /* We need to add the type field to the die immediately so we don't
14570 infinitely recurse when dealing with pointers to the structure
0963b4bd 14571 type within the structure itself. */
1c379e20 14572 set_die_type (die, type, cu);
c906108c 14573
7e314c57
JK
14574 /* set_die_type should be already done. */
14575 set_descriptive_type (type, die, cu);
14576
c767944b
DJ
14577 return type;
14578}
14579
14580/* Finish creating a structure or union type, including filling in
14581 its members and creating a symbol for it. */
14582
14583static void
14584process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
14585{
14586 struct objfile *objfile = cu->objfile;
ca040673 14587 struct die_info *child_die;
c767944b
DJ
14588 struct type *type;
14589
14590 type = get_die_type (die, cu);
14591 if (type == NULL)
14592 type = read_structure_type (die, cu);
14593
e142c38c 14594 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
14595 {
14596 struct field_info fi;
2f4732b0 14597 std::vector<struct symbol *> template_args;
c767944b 14598 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
14599
14600 memset (&fi, 0, sizeof (struct field_info));
14601
639d11d3 14602 child_die = die->child;
c906108c
SS
14603
14604 while (child_die && child_die->tag)
14605 {
a9a9bd0f
DC
14606 if (child_die->tag == DW_TAG_member
14607 || child_die->tag == DW_TAG_variable)
c906108c 14608 {
a9a9bd0f
DC
14609 /* NOTE: carlton/2002-11-05: A C++ static data member
14610 should be a DW_TAG_member that is a declaration, but
14611 all versions of G++ as of this writing (so through at
14612 least 3.2.1) incorrectly generate DW_TAG_variable
14613 tags for them instead. */
e7c27a73 14614 dwarf2_add_field (&fi, child_die, cu);
c906108c 14615 }
8713b1b1 14616 else if (child_die->tag == DW_TAG_subprogram)
c906108c 14617 {
e98c9e7c
TT
14618 /* Rust doesn't have member functions in the C++ sense.
14619 However, it does emit ordinary functions as children
14620 of a struct DIE. */
14621 if (cu->language == language_rust)
14622 read_func_scope (child_die, cu);
14623 else
14624 {
14625 /* C++ member function. */
14626 dwarf2_add_member_fn (&fi, child_die, type, cu);
14627 }
c906108c
SS
14628 }
14629 else if (child_die->tag == DW_TAG_inheritance)
14630 {
14631 /* C++ base class field. */
e7c27a73 14632 dwarf2_add_field (&fi, child_die, cu);
c906108c 14633 }
98751a41
JK
14634 else if (child_die->tag == DW_TAG_typedef)
14635 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
14636 else if (child_die->tag == DW_TAG_template_type_param
14637 || child_die->tag == DW_TAG_template_value_param)
14638 {
14639 struct symbol *arg = new_symbol (child_die, NULL, cu);
14640
f1078f66 14641 if (arg != NULL)
2f4732b0 14642 template_args.push_back (arg);
34eaf542
TT
14643 }
14644
c906108c
SS
14645 child_die = sibling_die (child_die);
14646 }
14647
34eaf542 14648 /* Attach template arguments to type. */
2f4732b0 14649 if (!template_args.empty ())
34eaf542
TT
14650 {
14651 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 14652 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 14653 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
14654 = XOBNEWVEC (&objfile->objfile_obstack,
14655 struct symbol *,
14656 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 14657 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 14658 template_args.data (),
34eaf542
TT
14659 (TYPE_N_TEMPLATE_ARGUMENTS (type)
14660 * sizeof (struct symbol *)));
34eaf542
TT
14661 }
14662
c906108c
SS
14663 /* Attach fields and member functions to the type. */
14664 if (fi.nfields)
e7c27a73 14665 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
14666 if (fi.nfnfields)
14667 {
e7c27a73 14668 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 14669
c5aa993b 14670 /* Get the type which refers to the base class (possibly this
c906108c 14671 class itself) which contains the vtable pointer for the current
0d564a31
DJ
14672 class from the DW_AT_containing_type attribute. This use of
14673 DW_AT_containing_type is a GNU extension. */
c906108c 14674
e142c38c 14675 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 14676 {
e7c27a73 14677 struct type *t = die_containing_type (die, cu);
c906108c 14678
ae6ae975 14679 set_type_vptr_basetype (type, t);
c906108c
SS
14680 if (type == t)
14681 {
c906108c
SS
14682 int i;
14683
14684 /* Our own class provides vtbl ptr. */
14685 for (i = TYPE_NFIELDS (t) - 1;
14686 i >= TYPE_N_BASECLASSES (t);
14687 --i)
14688 {
0d5cff50 14689 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 14690
1168df01 14691 if (is_vtable_name (fieldname, cu))
c906108c 14692 {
ae6ae975 14693 set_type_vptr_fieldno (type, i);
c906108c
SS
14694 break;
14695 }
14696 }
14697
14698 /* Complain if virtual function table field not found. */
14699 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 14700 complaint (&symfile_complaints,
3e43a32a
MS
14701 _("virtual function table pointer "
14702 "not found when defining class '%s'"),
4d3c2250
KB
14703 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
14704 "");
c906108c
SS
14705 }
14706 else
14707 {
ae6ae975 14708 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
14709 }
14710 }
f6235d4c 14711 else if (cu->producer
61012eef 14712 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
14713 {
14714 /* The IBM XLC compiler does not provide direct indication
14715 of the containing type, but the vtable pointer is
14716 always named __vfp. */
14717
14718 int i;
14719
14720 for (i = TYPE_NFIELDS (type) - 1;
14721 i >= TYPE_N_BASECLASSES (type);
14722 --i)
14723 {
14724 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
14725 {
ae6ae975
DE
14726 set_type_vptr_fieldno (type, i);
14727 set_type_vptr_basetype (type, type);
f6235d4c
EZ
14728 break;
14729 }
14730 }
14731 }
c906108c 14732 }
98751a41
JK
14733
14734 /* Copy fi.typedef_field_list linked list elements content into the
14735 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
14736 if (fi.typedef_field_list)
14737 {
14738 int i = fi.typedef_field_list_count;
14739
a0d7a4ff 14740 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 14741 TYPE_TYPEDEF_FIELD_ARRAY (type)
224c3ddb
SM
14742 = ((struct typedef_field *)
14743 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
14744 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
14745
14746 /* Reverse the list order to keep the debug info elements order. */
14747 while (--i >= 0)
14748 {
14749 struct typedef_field *dest, *src;
6e70227d 14750
98751a41
JK
14751 dest = &TYPE_TYPEDEF_FIELD (type, i);
14752 src = &fi.typedef_field_list->field;
14753 fi.typedef_field_list = fi.typedef_field_list->next;
14754 *dest = *src;
14755 }
14756 }
c767944b
DJ
14757
14758 do_cleanups (back_to);
c906108c 14759 }
63d06c5c 14760
bb5ed363 14761 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 14762
90aeadfc
DC
14763 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
14764 snapshots) has been known to create a die giving a declaration
14765 for a class that has, as a child, a die giving a definition for a
14766 nested class. So we have to process our children even if the
14767 current die is a declaration. Normally, of course, a declaration
14768 won't have any children at all. */
134d01f1 14769
ca040673
DE
14770 child_die = die->child;
14771
90aeadfc
DC
14772 while (child_die != NULL && child_die->tag)
14773 {
14774 if (child_die->tag == DW_TAG_member
14775 || child_die->tag == DW_TAG_variable
34eaf542
TT
14776 || child_die->tag == DW_TAG_inheritance
14777 || child_die->tag == DW_TAG_template_value_param
14778 || child_die->tag == DW_TAG_template_type_param)
134d01f1 14779 {
90aeadfc 14780 /* Do nothing. */
134d01f1 14781 }
90aeadfc
DC
14782 else
14783 process_die (child_die, cu);
134d01f1 14784
90aeadfc 14785 child_die = sibling_die (child_die);
134d01f1
DJ
14786 }
14787
fa4028e9
JB
14788 /* Do not consider external references. According to the DWARF standard,
14789 these DIEs are identified by the fact that they have no byte_size
14790 attribute, and a declaration attribute. */
14791 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
14792 || !die_is_declaration (die, cu))
c767944b 14793 new_symbol (die, type, cu);
134d01f1
DJ
14794}
14795
55426c9d
JB
14796/* Assuming DIE is an enumeration type, and TYPE is its associated type,
14797 update TYPE using some information only available in DIE's children. */
14798
14799static void
14800update_enumeration_type_from_children (struct die_info *die,
14801 struct type *type,
14802 struct dwarf2_cu *cu)
14803{
60f7655a 14804 struct die_info *child_die;
55426c9d
JB
14805 int unsigned_enum = 1;
14806 int flag_enum = 1;
14807 ULONGEST mask = 0;
55426c9d 14808
8268c778 14809 auto_obstack obstack;
55426c9d 14810
60f7655a
DE
14811 for (child_die = die->child;
14812 child_die != NULL && child_die->tag;
14813 child_die = sibling_die (child_die))
55426c9d
JB
14814 {
14815 struct attribute *attr;
14816 LONGEST value;
14817 const gdb_byte *bytes;
14818 struct dwarf2_locexpr_baton *baton;
14819 const char *name;
60f7655a 14820
55426c9d
JB
14821 if (child_die->tag != DW_TAG_enumerator)
14822 continue;
14823
14824 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
14825 if (attr == NULL)
14826 continue;
14827
14828 name = dwarf2_name (child_die, cu);
14829 if (name == NULL)
14830 name = "<anonymous enumerator>";
14831
14832 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
14833 &value, &bytes, &baton);
14834 if (value < 0)
14835 {
14836 unsigned_enum = 0;
14837 flag_enum = 0;
14838 }
14839 else if ((mask & value) != 0)
14840 flag_enum = 0;
14841 else
14842 mask |= value;
14843
14844 /* If we already know that the enum type is neither unsigned, nor
14845 a flag type, no need to look at the rest of the enumerates. */
14846 if (!unsigned_enum && !flag_enum)
14847 break;
55426c9d
JB
14848 }
14849
14850 if (unsigned_enum)
14851 TYPE_UNSIGNED (type) = 1;
14852 if (flag_enum)
14853 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
14854}
14855
134d01f1
DJ
14856/* Given a DW_AT_enumeration_type die, set its type. We do not
14857 complete the type's fields yet, or create any symbols. */
c906108c 14858
f792889a 14859static struct type *
134d01f1 14860read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14861{
e7c27a73 14862 struct objfile *objfile = cu->objfile;
c906108c 14863 struct type *type;
c906108c 14864 struct attribute *attr;
0114d602 14865 const char *name;
134d01f1 14866
348e048f
DE
14867 /* If the definition of this type lives in .debug_types, read that type.
14868 Don't follow DW_AT_specification though, that will take us back up
14869 the chain and we want to go down. */
45e58e77 14870 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
14871 if (attr)
14872 {
ac9ec31b 14873 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 14874
ac9ec31b 14875 /* The type's CU may not be the same as CU.
02142a6c 14876 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
14877 return set_die_type (die, type, cu);
14878 }
14879
c906108c
SS
14880 type = alloc_type (objfile);
14881
14882 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 14883 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 14884 if (name != NULL)
7d455152 14885 TYPE_TAG_NAME (type) = name;
c906108c 14886
0626fc76
TT
14887 attr = dwarf2_attr (die, DW_AT_type, cu);
14888 if (attr != NULL)
14889 {
14890 struct type *underlying_type = die_type (die, cu);
14891
14892 TYPE_TARGET_TYPE (type) = underlying_type;
14893 }
14894
e142c38c 14895 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14896 if (attr)
14897 {
14898 TYPE_LENGTH (type) = DW_UNSND (attr);
14899 }
14900 else
14901 {
14902 TYPE_LENGTH (type) = 0;
14903 }
14904
137033e9
JB
14905 /* The enumeration DIE can be incomplete. In Ada, any type can be
14906 declared as private in the package spec, and then defined only
14907 inside the package body. Such types are known as Taft Amendment
14908 Types. When another package uses such a type, an incomplete DIE
14909 may be generated by the compiler. */
02eb380e 14910 if (die_is_declaration (die, cu))
876cecd0 14911 TYPE_STUB (type) = 1;
02eb380e 14912
0626fc76
TT
14913 /* Finish the creation of this type by using the enum's children.
14914 We must call this even when the underlying type has been provided
14915 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
14916 update_enumeration_type_from_children (die, type, cu);
14917
0626fc76
TT
14918 /* If this type has an underlying type that is not a stub, then we
14919 may use its attributes. We always use the "unsigned" attribute
14920 in this situation, because ordinarily we guess whether the type
14921 is unsigned -- but the guess can be wrong and the underlying type
14922 can tell us the reality. However, we defer to a local size
14923 attribute if one exists, because this lets the compiler override
14924 the underlying type if needed. */
14925 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
14926 {
14927 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
14928 if (TYPE_LENGTH (type) == 0)
14929 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
14930 }
14931
3d567982
TT
14932 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
14933
f792889a 14934 return set_die_type (die, type, cu);
134d01f1
DJ
14935}
14936
14937/* Given a pointer to a die which begins an enumeration, process all
14938 the dies that define the members of the enumeration, and create the
14939 symbol for the enumeration type.
14940
14941 NOTE: We reverse the order of the element list. */
14942
14943static void
14944process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
14945{
f792889a 14946 struct type *this_type;
134d01f1 14947
f792889a
DJ
14948 this_type = get_die_type (die, cu);
14949 if (this_type == NULL)
14950 this_type = read_enumeration_type (die, cu);
9dc481d3 14951
639d11d3 14952 if (die->child != NULL)
c906108c 14953 {
9dc481d3
DE
14954 struct die_info *child_die;
14955 struct symbol *sym;
14956 struct field *fields = NULL;
14957 int num_fields = 0;
15d034d0 14958 const char *name;
9dc481d3 14959
639d11d3 14960 child_die = die->child;
c906108c
SS
14961 while (child_die && child_die->tag)
14962 {
14963 if (child_die->tag != DW_TAG_enumerator)
14964 {
e7c27a73 14965 process_die (child_die, cu);
c906108c
SS
14966 }
14967 else
14968 {
39cbfefa
DJ
14969 name = dwarf2_name (child_die, cu);
14970 if (name)
c906108c 14971 {
f792889a 14972 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
14973
14974 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
14975 {
14976 fields = (struct field *)
14977 xrealloc (fields,
14978 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 14979 * sizeof (struct field));
c906108c
SS
14980 }
14981
3567439c 14982 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 14983 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 14984 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
14985 FIELD_BITSIZE (fields[num_fields]) = 0;
14986
14987 num_fields++;
14988 }
14989 }
14990
14991 child_die = sibling_die (child_die);
14992 }
14993
14994 if (num_fields)
14995 {
f792889a
DJ
14996 TYPE_NFIELDS (this_type) = num_fields;
14997 TYPE_FIELDS (this_type) = (struct field *)
14998 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
14999 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 15000 sizeof (struct field) * num_fields);
b8c9b27d 15001 xfree (fields);
c906108c 15002 }
c906108c 15003 }
134d01f1 15004
6c83ed52
TT
15005 /* If we are reading an enum from a .debug_types unit, and the enum
15006 is a declaration, and the enum is not the signatured type in the
15007 unit, then we do not want to add a symbol for it. Adding a
15008 symbol would in some cases obscure the true definition of the
15009 enum, giving users an incomplete type when the definition is
15010 actually available. Note that we do not want to do this for all
15011 enums which are just declarations, because C++0x allows forward
15012 enum declarations. */
3019eac3 15013 if (cu->per_cu->is_debug_types
6c83ed52
TT
15014 && die_is_declaration (die, cu))
15015 {
52dc124a 15016 struct signatured_type *sig_type;
6c83ed52 15017
c0f78cd4 15018 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
15019 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15020 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
15021 return;
15022 }
15023
f792889a 15024 new_symbol (die, this_type, cu);
c906108c
SS
15025}
15026
15027/* Extract all information from a DW_TAG_array_type DIE and put it in
15028 the DIE's type field. For now, this only handles one dimensional
15029 arrays. */
15030
f792889a 15031static struct type *
e7c27a73 15032read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15033{
e7c27a73 15034 struct objfile *objfile = cu->objfile;
c906108c 15035 struct die_info *child_die;
7e314c57 15036 struct type *type;
c906108c 15037 struct type *element_type, *range_type, *index_type;
c906108c 15038 struct attribute *attr;
15d034d0 15039 const char *name;
dc53a7ad 15040 unsigned int bit_stride = 0;
c906108c 15041
e7c27a73 15042 element_type = die_type (die, cu);
c906108c 15043
7e314c57
JK
15044 /* The die_type call above may have already set the type for this DIE. */
15045 type = get_die_type (die, cu);
15046 if (type)
15047 return type;
15048
dc53a7ad
JB
15049 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15050 if (attr != NULL)
15051 bit_stride = DW_UNSND (attr) * 8;
15052
15053 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15054 if (attr != NULL)
15055 bit_stride = DW_UNSND (attr);
15056
c906108c
SS
15057 /* Irix 6.2 native cc creates array types without children for
15058 arrays with unspecified length. */
639d11d3 15059 if (die->child == NULL)
c906108c 15060 {
46bf5051 15061 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 15062 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
15063 type = create_array_type_with_stride (NULL, element_type, range_type,
15064 bit_stride);
f792889a 15065 return set_die_type (die, type, cu);
c906108c
SS
15066 }
15067
791afaa2 15068 std::vector<struct type *> range_types;
639d11d3 15069 child_die = die->child;
c906108c
SS
15070 while (child_die && child_die->tag)
15071 {
15072 if (child_die->tag == DW_TAG_subrange_type)
15073 {
f792889a 15074 struct type *child_type = read_type_die (child_die, cu);
9a619af0 15075
f792889a 15076 if (child_type != NULL)
a02abb62 15077 {
0963b4bd
MS
15078 /* The range type was succesfully read. Save it for the
15079 array type creation. */
791afaa2 15080 range_types.push_back (child_type);
a02abb62 15081 }
c906108c
SS
15082 }
15083 child_die = sibling_die (child_die);
15084 }
15085
15086 /* Dwarf2 dimensions are output from left to right, create the
15087 necessary array types in backwards order. */
7ca2d3a3 15088
c906108c 15089 type = element_type;
7ca2d3a3
DL
15090
15091 if (read_array_order (die, cu) == DW_ORD_col_major)
15092 {
15093 int i = 0;
9a619af0 15094
791afaa2 15095 while (i < range_types.size ())
dc53a7ad
JB
15096 type = create_array_type_with_stride (NULL, type, range_types[i++],
15097 bit_stride);
7ca2d3a3
DL
15098 }
15099 else
15100 {
791afaa2 15101 size_t ndim = range_types.size ();
7ca2d3a3 15102 while (ndim-- > 0)
dc53a7ad
JB
15103 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15104 bit_stride);
7ca2d3a3 15105 }
c906108c 15106
f5f8a009
EZ
15107 /* Understand Dwarf2 support for vector types (like they occur on
15108 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15109 array type. This is not part of the Dwarf2/3 standard yet, but a
15110 custom vendor extension. The main difference between a regular
15111 array and the vector variant is that vectors are passed by value
15112 to functions. */
e142c38c 15113 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 15114 if (attr)
ea37ba09 15115 make_vector_type (type);
f5f8a009 15116
dbc98a8b
KW
15117 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15118 implementation may choose to implement triple vectors using this
15119 attribute. */
15120 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15121 if (attr)
15122 {
15123 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15124 TYPE_LENGTH (type) = DW_UNSND (attr);
15125 else
3e43a32a
MS
15126 complaint (&symfile_complaints,
15127 _("DW_AT_byte_size for array type smaller "
15128 "than the total size of elements"));
dbc98a8b
KW
15129 }
15130
39cbfefa
DJ
15131 name = dwarf2_name (die, cu);
15132 if (name)
15133 TYPE_NAME (type) = name;
6e70227d 15134
0963b4bd 15135 /* Install the type in the die. */
7e314c57
JK
15136 set_die_type (die, type, cu);
15137
15138 /* set_die_type should be already done. */
b4ba55a1
JB
15139 set_descriptive_type (type, die, cu);
15140
7e314c57 15141 return type;
c906108c
SS
15142}
15143
7ca2d3a3 15144static enum dwarf_array_dim_ordering
6e70227d 15145read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
15146{
15147 struct attribute *attr;
15148
15149 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15150
aead7601
SM
15151 if (attr)
15152 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 15153
0963b4bd
MS
15154 /* GNU F77 is a special case, as at 08/2004 array type info is the
15155 opposite order to the dwarf2 specification, but data is still
15156 laid out as per normal fortran.
7ca2d3a3 15157
0963b4bd
MS
15158 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15159 version checking. */
7ca2d3a3 15160
905e0470
PM
15161 if (cu->language == language_fortran
15162 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
15163 {
15164 return DW_ORD_row_major;
15165 }
15166
6e70227d 15167 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
15168 {
15169 case array_column_major:
15170 return DW_ORD_col_major;
15171 case array_row_major:
15172 default:
15173 return DW_ORD_row_major;
15174 };
15175}
15176
72019c9c 15177/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 15178 the DIE's type field. */
72019c9c 15179
f792889a 15180static struct type *
72019c9c
GM
15181read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15182{
7e314c57
JK
15183 struct type *domain_type, *set_type;
15184 struct attribute *attr;
f792889a 15185
7e314c57
JK
15186 domain_type = die_type (die, cu);
15187
15188 /* The die_type call above may have already set the type for this DIE. */
15189 set_type = get_die_type (die, cu);
15190 if (set_type)
15191 return set_type;
15192
15193 set_type = create_set_type (NULL, domain_type);
15194
15195 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
15196 if (attr)
15197 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 15198
f792889a 15199 return set_die_type (die, set_type, cu);
72019c9c 15200}
7ca2d3a3 15201
0971de02
TT
15202/* A helper for read_common_block that creates a locexpr baton.
15203 SYM is the symbol which we are marking as computed.
15204 COMMON_DIE is the DIE for the common block.
15205 COMMON_LOC is the location expression attribute for the common
15206 block itself.
15207 MEMBER_LOC is the location expression attribute for the particular
15208 member of the common block that we are processing.
15209 CU is the CU from which the above come. */
15210
15211static void
15212mark_common_block_symbol_computed (struct symbol *sym,
15213 struct die_info *common_die,
15214 struct attribute *common_loc,
15215 struct attribute *member_loc,
15216 struct dwarf2_cu *cu)
15217{
15218 struct objfile *objfile = dwarf2_per_objfile->objfile;
15219 struct dwarf2_locexpr_baton *baton;
15220 gdb_byte *ptr;
15221 unsigned int cu_off;
15222 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15223 LONGEST offset = 0;
15224
15225 gdb_assert (common_loc && member_loc);
15226 gdb_assert (attr_form_is_block (common_loc));
15227 gdb_assert (attr_form_is_block (member_loc)
15228 || attr_form_is_constant (member_loc));
15229
8d749320 15230 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
15231 baton->per_cu = cu->per_cu;
15232 gdb_assert (baton->per_cu);
15233
15234 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15235
15236 if (attr_form_is_constant (member_loc))
15237 {
15238 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15239 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15240 }
15241 else
15242 baton->size += DW_BLOCK (member_loc)->size;
15243
224c3ddb 15244 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
15245 baton->data = ptr;
15246
15247 *ptr++ = DW_OP_call4;
9c541725 15248 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
15249 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15250 ptr += 4;
15251
15252 if (attr_form_is_constant (member_loc))
15253 {
15254 *ptr++ = DW_OP_addr;
15255 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15256 ptr += cu->header.addr_size;
15257 }
15258 else
15259 {
15260 /* We have to copy the data here, because DW_OP_call4 will only
15261 use a DW_AT_location attribute. */
15262 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15263 ptr += DW_BLOCK (member_loc)->size;
15264 }
15265
15266 *ptr++ = DW_OP_plus;
15267 gdb_assert (ptr - baton->data == baton->size);
15268
0971de02 15269 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 15270 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
15271}
15272
4357ac6c
TT
15273/* Create appropriate locally-scoped variables for all the
15274 DW_TAG_common_block entries. Also create a struct common_block
15275 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15276 is used to sepate the common blocks name namespace from regular
15277 variable names. */
c906108c
SS
15278
15279static void
e7c27a73 15280read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15281{
0971de02
TT
15282 struct attribute *attr;
15283
15284 attr = dwarf2_attr (die, DW_AT_location, cu);
15285 if (attr)
15286 {
15287 /* Support the .debug_loc offsets. */
15288 if (attr_form_is_block (attr))
15289 {
15290 /* Ok. */
15291 }
15292 else if (attr_form_is_section_offset (attr))
15293 {
15294 dwarf2_complex_location_expr_complaint ();
15295 attr = NULL;
15296 }
15297 else
15298 {
15299 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15300 "common block member");
15301 attr = NULL;
15302 }
15303 }
15304
639d11d3 15305 if (die->child != NULL)
c906108c 15306 {
4357ac6c
TT
15307 struct objfile *objfile = cu->objfile;
15308 struct die_info *child_die;
15309 size_t n_entries = 0, size;
15310 struct common_block *common_block;
15311 struct symbol *sym;
74ac6d43 15312
4357ac6c
TT
15313 for (child_die = die->child;
15314 child_die && child_die->tag;
15315 child_die = sibling_die (child_die))
15316 ++n_entries;
15317
15318 size = (sizeof (struct common_block)
15319 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
15320 common_block
15321 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15322 size);
4357ac6c
TT
15323 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15324 common_block->n_entries = 0;
15325
15326 for (child_die = die->child;
15327 child_die && child_die->tag;
15328 child_die = sibling_die (child_die))
15329 {
15330 /* Create the symbol in the DW_TAG_common_block block in the current
15331 symbol scope. */
e7c27a73 15332 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
15333 if (sym != NULL)
15334 {
15335 struct attribute *member_loc;
15336
15337 common_block->contents[common_block->n_entries++] = sym;
15338
15339 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15340 cu);
15341 if (member_loc)
15342 {
15343 /* GDB has handled this for a long time, but it is
15344 not specified by DWARF. It seems to have been
15345 emitted by gfortran at least as recently as:
15346 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15347 complaint (&symfile_complaints,
15348 _("Variable in common block has "
15349 "DW_AT_data_member_location "
15350 "- DIE at 0x%x [in module %s]"),
9c541725 15351 to_underlying (child_die->sect_off),
4262abfb 15352 objfile_name (cu->objfile));
0971de02
TT
15353
15354 if (attr_form_is_section_offset (member_loc))
15355 dwarf2_complex_location_expr_complaint ();
15356 else if (attr_form_is_constant (member_loc)
15357 || attr_form_is_block (member_loc))
15358 {
15359 if (attr)
15360 mark_common_block_symbol_computed (sym, die, attr,
15361 member_loc, cu);
15362 }
15363 else
15364 dwarf2_complex_location_expr_complaint ();
15365 }
15366 }
c906108c 15367 }
4357ac6c
TT
15368
15369 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
15370 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
15371 }
15372}
15373
0114d602 15374/* Create a type for a C++ namespace. */
d9fa45fe 15375
0114d602
DJ
15376static struct type *
15377read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 15378{
e7c27a73 15379 struct objfile *objfile = cu->objfile;
0114d602 15380 const char *previous_prefix, *name;
9219021c 15381 int is_anonymous;
0114d602
DJ
15382 struct type *type;
15383
15384 /* For extensions, reuse the type of the original namespace. */
15385 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
15386 {
15387 struct die_info *ext_die;
15388 struct dwarf2_cu *ext_cu = cu;
9a619af0 15389
0114d602
DJ
15390 ext_die = dwarf2_extension (die, &ext_cu);
15391 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
15392
15393 /* EXT_CU may not be the same as CU.
02142a6c 15394 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
15395 return set_die_type (die, type, cu);
15396 }
9219021c 15397
e142c38c 15398 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
15399
15400 /* Now build the name of the current namespace. */
15401
0114d602
DJ
15402 previous_prefix = determine_prefix (die, cu);
15403 if (previous_prefix[0] != '\0')
15404 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 15405 previous_prefix, name, 0, cu);
0114d602
DJ
15406
15407 /* Create the type. */
19f392bc 15408 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
15409 TYPE_TAG_NAME (type) = TYPE_NAME (type);
15410
60531b24 15411 return set_die_type (die, type, cu);
0114d602
DJ
15412}
15413
22cee43f 15414/* Read a namespace scope. */
0114d602
DJ
15415
15416static void
15417read_namespace (struct die_info *die, struct dwarf2_cu *cu)
15418{
15419 struct objfile *objfile = cu->objfile;
0114d602 15420 int is_anonymous;
9219021c 15421
5c4e30ca
DC
15422 /* Add a symbol associated to this if we haven't seen the namespace
15423 before. Also, add a using directive if it's an anonymous
15424 namespace. */
9219021c 15425
f2f0e013 15426 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
15427 {
15428 struct type *type;
15429
0114d602 15430 type = read_type_die (die, cu);
e7c27a73 15431 new_symbol (die, type, cu);
5c4e30ca 15432
e8e80198 15433 namespace_name (die, &is_anonymous, cu);
5c4e30ca 15434 if (is_anonymous)
0114d602
DJ
15435 {
15436 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 15437
eb1e02fd 15438 std::vector<const char *> excludes;
22cee43f
PMR
15439 add_using_directive (using_directives (cu->language),
15440 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 15441 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 15442 }
5c4e30ca 15443 }
9219021c 15444
639d11d3 15445 if (die->child != NULL)
d9fa45fe 15446 {
639d11d3 15447 struct die_info *child_die = die->child;
6e70227d 15448
d9fa45fe
DC
15449 while (child_die && child_die->tag)
15450 {
e7c27a73 15451 process_die (child_die, cu);
d9fa45fe
DC
15452 child_die = sibling_die (child_die);
15453 }
15454 }
38d518c9
EZ
15455}
15456
f55ee35c
JK
15457/* Read a Fortran module as type. This DIE can be only a declaration used for
15458 imported module. Still we need that type as local Fortran "use ... only"
15459 declaration imports depend on the created type in determine_prefix. */
15460
15461static struct type *
15462read_module_type (struct die_info *die, struct dwarf2_cu *cu)
15463{
15464 struct objfile *objfile = cu->objfile;
15d034d0 15465 const char *module_name;
f55ee35c
JK
15466 struct type *type;
15467
15468 module_name = dwarf2_name (die, cu);
15469 if (!module_name)
3e43a32a
MS
15470 complaint (&symfile_complaints,
15471 _("DW_TAG_module has no name, offset 0x%x"),
9c541725 15472 to_underlying (die->sect_off));
19f392bc 15473 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
15474
15475 /* determine_prefix uses TYPE_TAG_NAME. */
15476 TYPE_TAG_NAME (type) = TYPE_NAME (type);
15477
15478 return set_die_type (die, type, cu);
15479}
15480
5d7cb8df
JK
15481/* Read a Fortran module. */
15482
15483static void
15484read_module (struct die_info *die, struct dwarf2_cu *cu)
15485{
15486 struct die_info *child_die = die->child;
530e8392
KB
15487 struct type *type;
15488
15489 type = read_type_die (die, cu);
15490 new_symbol (die, type, cu);
5d7cb8df 15491
5d7cb8df
JK
15492 while (child_die && child_die->tag)
15493 {
15494 process_die (child_die, cu);
15495 child_die = sibling_die (child_die);
15496 }
15497}
15498
38d518c9
EZ
15499/* Return the name of the namespace represented by DIE. Set
15500 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
15501 namespace. */
15502
15503static const char *
e142c38c 15504namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
15505{
15506 struct die_info *current_die;
15507 const char *name = NULL;
15508
15509 /* Loop through the extensions until we find a name. */
15510
15511 for (current_die = die;
15512 current_die != NULL;
f2f0e013 15513 current_die = dwarf2_extension (die, &cu))
38d518c9 15514 {
96553a0c
DE
15515 /* We don't use dwarf2_name here so that we can detect the absence
15516 of a name -> anonymous namespace. */
7d45c7c3 15517 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 15518
38d518c9
EZ
15519 if (name != NULL)
15520 break;
15521 }
15522
15523 /* Is it an anonymous namespace? */
15524
15525 *is_anonymous = (name == NULL);
15526 if (*is_anonymous)
2b1dbab0 15527 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
15528
15529 return name;
d9fa45fe
DC
15530}
15531
c906108c
SS
15532/* Extract all information from a DW_TAG_pointer_type DIE and add to
15533 the user defined type vector. */
15534
f792889a 15535static struct type *
e7c27a73 15536read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15537{
5e2b427d 15538 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 15539 struct comp_unit_head *cu_header = &cu->header;
c906108c 15540 struct type *type;
8b2dbe47
KB
15541 struct attribute *attr_byte_size;
15542 struct attribute *attr_address_class;
15543 int byte_size, addr_class;
7e314c57
JK
15544 struct type *target_type;
15545
15546 target_type = die_type (die, cu);
c906108c 15547
7e314c57
JK
15548 /* The die_type call above may have already set the type for this DIE. */
15549 type = get_die_type (die, cu);
15550 if (type)
15551 return type;
15552
15553 type = lookup_pointer_type (target_type);
8b2dbe47 15554
e142c38c 15555 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
15556 if (attr_byte_size)
15557 byte_size = DW_UNSND (attr_byte_size);
c906108c 15558 else
8b2dbe47
KB
15559 byte_size = cu_header->addr_size;
15560
e142c38c 15561 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
15562 if (attr_address_class)
15563 addr_class = DW_UNSND (attr_address_class);
15564 else
15565 addr_class = DW_ADDR_none;
15566
15567 /* If the pointer size or address class is different than the
15568 default, create a type variant marked as such and set the
15569 length accordingly. */
15570 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 15571 {
5e2b427d 15572 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
15573 {
15574 int type_flags;
15575
849957d9 15576 type_flags = gdbarch_address_class_type_flags
5e2b427d 15577 (gdbarch, byte_size, addr_class);
876cecd0
TT
15578 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
15579 == 0);
8b2dbe47
KB
15580 type = make_type_with_address_space (type, type_flags);
15581 }
15582 else if (TYPE_LENGTH (type) != byte_size)
15583 {
3e43a32a
MS
15584 complaint (&symfile_complaints,
15585 _("invalid pointer size %d"), byte_size);
8b2dbe47 15586 }
6e70227d 15587 else
9a619af0
MS
15588 {
15589 /* Should we also complain about unhandled address classes? */
15590 }
c906108c 15591 }
8b2dbe47
KB
15592
15593 TYPE_LENGTH (type) = byte_size;
f792889a 15594 return set_die_type (die, type, cu);
c906108c
SS
15595}
15596
15597/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
15598 the user defined type vector. */
15599
f792889a 15600static struct type *
e7c27a73 15601read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
15602{
15603 struct type *type;
15604 struct type *to_type;
15605 struct type *domain;
15606
e7c27a73
DJ
15607 to_type = die_type (die, cu);
15608 domain = die_containing_type (die, cu);
0d5de010 15609
7e314c57
JK
15610 /* The calls above may have already set the type for this DIE. */
15611 type = get_die_type (die, cu);
15612 if (type)
15613 return type;
15614
0d5de010
DJ
15615 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
15616 type = lookup_methodptr_type (to_type);
7078baeb
TT
15617 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
15618 {
15619 struct type *new_type = alloc_type (cu->objfile);
15620
15621 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
15622 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
15623 TYPE_VARARGS (to_type));
15624 type = lookup_methodptr_type (new_type);
15625 }
0d5de010
DJ
15626 else
15627 type = lookup_memberptr_type (to_type, domain);
c906108c 15628
f792889a 15629 return set_die_type (die, type, cu);
c906108c
SS
15630}
15631
4297a3f0 15632/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
15633 the user defined type vector. */
15634
f792889a 15635static struct type *
4297a3f0
AV
15636read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
15637 enum type_code refcode)
c906108c 15638{
e7c27a73 15639 struct comp_unit_head *cu_header = &cu->header;
7e314c57 15640 struct type *type, *target_type;
c906108c
SS
15641 struct attribute *attr;
15642
4297a3f0
AV
15643 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
15644
7e314c57
JK
15645 target_type = die_type (die, cu);
15646
15647 /* The die_type call above may have already set the type for this DIE. */
15648 type = get_die_type (die, cu);
15649 if (type)
15650 return type;
15651
4297a3f0 15652 type = lookup_reference_type (target_type, refcode);
e142c38c 15653 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15654 if (attr)
15655 {
15656 TYPE_LENGTH (type) = DW_UNSND (attr);
15657 }
15658 else
15659 {
107d2387 15660 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 15661 }
f792889a 15662 return set_die_type (die, type, cu);
c906108c
SS
15663}
15664
cf363f18
MW
15665/* Add the given cv-qualifiers to the element type of the array. GCC
15666 outputs DWARF type qualifiers that apply to an array, not the
15667 element type. But GDB relies on the array element type to carry
15668 the cv-qualifiers. This mimics section 6.7.3 of the C99
15669 specification. */
15670
15671static struct type *
15672add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
15673 struct type *base_type, int cnst, int voltl)
15674{
15675 struct type *el_type, *inner_array;
15676
15677 base_type = copy_type (base_type);
15678 inner_array = base_type;
15679
15680 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
15681 {
15682 TYPE_TARGET_TYPE (inner_array) =
15683 copy_type (TYPE_TARGET_TYPE (inner_array));
15684 inner_array = TYPE_TARGET_TYPE (inner_array);
15685 }
15686
15687 el_type = TYPE_TARGET_TYPE (inner_array);
15688 cnst |= TYPE_CONST (el_type);
15689 voltl |= TYPE_VOLATILE (el_type);
15690 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
15691
15692 return set_die_type (die, base_type, cu);
15693}
15694
f792889a 15695static struct type *
e7c27a73 15696read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15697{
f792889a 15698 struct type *base_type, *cv_type;
c906108c 15699
e7c27a73 15700 base_type = die_type (die, cu);
7e314c57
JK
15701
15702 /* The die_type call above may have already set the type for this DIE. */
15703 cv_type = get_die_type (die, cu);
15704 if (cv_type)
15705 return cv_type;
15706
2f608a3a
KW
15707 /* In case the const qualifier is applied to an array type, the element type
15708 is so qualified, not the array type (section 6.7.3 of C99). */
15709 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 15710 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 15711
f792889a
DJ
15712 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
15713 return set_die_type (die, cv_type, cu);
c906108c
SS
15714}
15715
f792889a 15716static struct type *
e7c27a73 15717read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15718{
f792889a 15719 struct type *base_type, *cv_type;
c906108c 15720
e7c27a73 15721 base_type = die_type (die, cu);
7e314c57
JK
15722
15723 /* The die_type call above may have already set the type for this DIE. */
15724 cv_type = get_die_type (die, cu);
15725 if (cv_type)
15726 return cv_type;
15727
cf363f18
MW
15728 /* In case the volatile qualifier is applied to an array type, the
15729 element type is so qualified, not the array type (section 6.7.3
15730 of C99). */
15731 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
15732 return add_array_cv_type (die, cu, base_type, 0, 1);
15733
f792889a
DJ
15734 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
15735 return set_die_type (die, cv_type, cu);
c906108c
SS
15736}
15737
06d66ee9
TT
15738/* Handle DW_TAG_restrict_type. */
15739
15740static struct type *
15741read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
15742{
15743 struct type *base_type, *cv_type;
15744
15745 base_type = die_type (die, cu);
15746
15747 /* The die_type call above may have already set the type for this DIE. */
15748 cv_type = get_die_type (die, cu);
15749 if (cv_type)
15750 return cv_type;
15751
15752 cv_type = make_restrict_type (base_type);
15753 return set_die_type (die, cv_type, cu);
15754}
15755
a2c2acaf
MW
15756/* Handle DW_TAG_atomic_type. */
15757
15758static struct type *
15759read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
15760{
15761 struct type *base_type, *cv_type;
15762
15763 base_type = die_type (die, cu);
15764
15765 /* The die_type call above may have already set the type for this DIE. */
15766 cv_type = get_die_type (die, cu);
15767 if (cv_type)
15768 return cv_type;
15769
15770 cv_type = make_atomic_type (base_type);
15771 return set_die_type (die, cv_type, cu);
15772}
15773
c906108c
SS
15774/* Extract all information from a DW_TAG_string_type DIE and add to
15775 the user defined type vector. It isn't really a user defined type,
15776 but it behaves like one, with other DIE's using an AT_user_def_type
15777 attribute to reference it. */
15778
f792889a 15779static struct type *
e7c27a73 15780read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15781{
e7c27a73 15782 struct objfile *objfile = cu->objfile;
3b7538c0 15783 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15784 struct type *type, *range_type, *index_type, *char_type;
15785 struct attribute *attr;
15786 unsigned int length;
15787
e142c38c 15788 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
15789 if (attr)
15790 {
15791 length = DW_UNSND (attr);
15792 }
15793 else
15794 {
0963b4bd 15795 /* Check for the DW_AT_byte_size attribute. */
e142c38c 15796 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
15797 if (attr)
15798 {
15799 length = DW_UNSND (attr);
15800 }
15801 else
15802 {
15803 length = 1;
15804 }
c906108c 15805 }
6ccb9162 15806
46bf5051 15807 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 15808 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
15809 char_type = language_string_char_type (cu->language_defn, gdbarch);
15810 type = create_string_type (NULL, char_type, range_type);
6ccb9162 15811
f792889a 15812 return set_die_type (die, type, cu);
c906108c
SS
15813}
15814
4d804846
JB
15815/* Assuming that DIE corresponds to a function, returns nonzero
15816 if the function is prototyped. */
15817
15818static int
15819prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
15820{
15821 struct attribute *attr;
15822
15823 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
15824 if (attr && (DW_UNSND (attr) != 0))
15825 return 1;
15826
15827 /* The DWARF standard implies that the DW_AT_prototyped attribute
15828 is only meaninful for C, but the concept also extends to other
15829 languages that allow unprototyped functions (Eg: Objective C).
15830 For all other languages, assume that functions are always
15831 prototyped. */
15832 if (cu->language != language_c
15833 && cu->language != language_objc
15834 && cu->language != language_opencl)
15835 return 1;
15836
15837 /* RealView does not emit DW_AT_prototyped. We can not distinguish
15838 prototyped and unprototyped functions; default to prototyped,
15839 since that is more common in modern code (and RealView warns
15840 about unprototyped functions). */
15841 if (producer_is_realview (cu->producer))
15842 return 1;
15843
15844 return 0;
15845}
15846
c906108c
SS
15847/* Handle DIES due to C code like:
15848
15849 struct foo
c5aa993b
JM
15850 {
15851 int (*funcp)(int a, long l);
15852 int b;
15853 };
c906108c 15854
0963b4bd 15855 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 15856
f792889a 15857static struct type *
e7c27a73 15858read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15859{
bb5ed363 15860 struct objfile *objfile = cu->objfile;
0963b4bd
MS
15861 struct type *type; /* Type that this function returns. */
15862 struct type *ftype; /* Function that returns above type. */
c906108c
SS
15863 struct attribute *attr;
15864
e7c27a73 15865 type = die_type (die, cu);
7e314c57
JK
15866
15867 /* The die_type call above may have already set the type for this DIE. */
15868 ftype = get_die_type (die, cu);
15869 if (ftype)
15870 return ftype;
15871
0c8b41f1 15872 ftype = lookup_function_type (type);
c906108c 15873
4d804846 15874 if (prototyped_function_p (die, cu))
a6c727b2 15875 TYPE_PROTOTYPED (ftype) = 1;
c906108c 15876
c055b101
CV
15877 /* Store the calling convention in the type if it's available in
15878 the subroutine die. Otherwise set the calling convention to
15879 the default value DW_CC_normal. */
15880 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
15881 if (attr)
15882 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
15883 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
15884 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
15885 else
15886 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 15887
743649fd
MW
15888 /* Record whether the function returns normally to its caller or not
15889 if the DWARF producer set that information. */
15890 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
15891 if (attr && (DW_UNSND (attr) != 0))
15892 TYPE_NO_RETURN (ftype) = 1;
15893
76c10ea2
GM
15894 /* We need to add the subroutine type to the die immediately so
15895 we don't infinitely recurse when dealing with parameters
0963b4bd 15896 declared as the same subroutine type. */
76c10ea2 15897 set_die_type (die, ftype, cu);
6e70227d 15898
639d11d3 15899 if (die->child != NULL)
c906108c 15900 {
bb5ed363 15901 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 15902 struct die_info *child_die;
8072405b 15903 int nparams, iparams;
c906108c
SS
15904
15905 /* Count the number of parameters.
15906 FIXME: GDB currently ignores vararg functions, but knows about
15907 vararg member functions. */
8072405b 15908 nparams = 0;
639d11d3 15909 child_die = die->child;
c906108c
SS
15910 while (child_die && child_die->tag)
15911 {
15912 if (child_die->tag == DW_TAG_formal_parameter)
15913 nparams++;
15914 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 15915 TYPE_VARARGS (ftype) = 1;
c906108c
SS
15916 child_die = sibling_die (child_die);
15917 }
15918
15919 /* Allocate storage for parameters and fill them in. */
15920 TYPE_NFIELDS (ftype) = nparams;
15921 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 15922 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 15923
8072405b
JK
15924 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
15925 even if we error out during the parameters reading below. */
15926 for (iparams = 0; iparams < nparams; iparams++)
15927 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
15928
15929 iparams = 0;
639d11d3 15930 child_die = die->child;
c906108c
SS
15931 while (child_die && child_die->tag)
15932 {
15933 if (child_die->tag == DW_TAG_formal_parameter)
15934 {
3ce3b1ba
PA
15935 struct type *arg_type;
15936
15937 /* DWARF version 2 has no clean way to discern C++
15938 static and non-static member functions. G++ helps
15939 GDB by marking the first parameter for non-static
15940 member functions (which is the this pointer) as
15941 artificial. We pass this information to
15942 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
15943
15944 DWARF version 3 added DW_AT_object_pointer, which GCC
15945 4.5 does not yet generate. */
e142c38c 15946 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
15947 if (attr)
15948 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
15949 else
9c37b5ae 15950 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
15951 arg_type = die_type (child_die, cu);
15952
15953 /* RealView does not mark THIS as const, which the testsuite
15954 expects. GCC marks THIS as const in method definitions,
15955 but not in the class specifications (GCC PR 43053). */
15956 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
15957 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
15958 {
15959 int is_this = 0;
15960 struct dwarf2_cu *arg_cu = cu;
15961 const char *name = dwarf2_name (child_die, cu);
15962
15963 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
15964 if (attr)
15965 {
15966 /* If the compiler emits this, use it. */
15967 if (follow_die_ref (die, attr, &arg_cu) == child_die)
15968 is_this = 1;
15969 }
15970 else if (name && strcmp (name, "this") == 0)
15971 /* Function definitions will have the argument names. */
15972 is_this = 1;
15973 else if (name == NULL && iparams == 0)
15974 /* Declarations may not have the names, so like
15975 elsewhere in GDB, assume an artificial first
15976 argument is "this". */
15977 is_this = 1;
15978
15979 if (is_this)
15980 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
15981 arg_type, 0);
15982 }
15983
15984 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
15985 iparams++;
15986 }
15987 child_die = sibling_die (child_die);
15988 }
15989 }
15990
76c10ea2 15991 return ftype;
c906108c
SS
15992}
15993
f792889a 15994static struct type *
e7c27a73 15995read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15996{
e7c27a73 15997 struct objfile *objfile = cu->objfile;
0114d602 15998 const char *name = NULL;
3c8e0968 15999 struct type *this_type, *target_type;
c906108c 16000
94af9270 16001 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
16002 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16003 TYPE_TARGET_STUB (this_type) = 1;
f792889a 16004 set_die_type (die, this_type, cu);
3c8e0968
DE
16005 target_type = die_type (die, cu);
16006 if (target_type != this_type)
16007 TYPE_TARGET_TYPE (this_type) = target_type;
16008 else
16009 {
16010 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16011 spec and cause infinite loops in GDB. */
16012 complaint (&symfile_complaints,
16013 _("Self-referential DW_TAG_typedef "
16014 "- DIE at 0x%x [in module %s]"),
9c541725 16015 to_underlying (die->sect_off), objfile_name (objfile));
3c8e0968
DE
16016 TYPE_TARGET_TYPE (this_type) = NULL;
16017 }
f792889a 16018 return this_type;
c906108c
SS
16019}
16020
9b790ce7
UW
16021/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16022 (which may be different from NAME) to the architecture back-end to allow
16023 it to guess the correct format if necessary. */
16024
16025static struct type *
16026dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16027 const char *name_hint)
16028{
16029 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16030 const struct floatformat **format;
16031 struct type *type;
16032
16033 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16034 if (format)
16035 type = init_float_type (objfile, bits, name, format);
16036 else
77b7c781 16037 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
16038
16039 return type;
16040}
16041
c906108c
SS
16042/* Find a representation of a given base type and install
16043 it in the TYPE field of the die. */
16044
f792889a 16045static struct type *
e7c27a73 16046read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16047{
e7c27a73 16048 struct objfile *objfile = cu->objfile;
c906108c
SS
16049 struct type *type;
16050 struct attribute *attr;
19f392bc 16051 int encoding = 0, bits = 0;
15d034d0 16052 const char *name;
c906108c 16053
e142c38c 16054 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
16055 if (attr)
16056 {
16057 encoding = DW_UNSND (attr);
16058 }
e142c38c 16059 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16060 if (attr)
16061 {
19f392bc 16062 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 16063 }
39cbfefa 16064 name = dwarf2_name (die, cu);
6ccb9162 16065 if (!name)
c906108c 16066 {
6ccb9162
UW
16067 complaint (&symfile_complaints,
16068 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 16069 }
6ccb9162
UW
16070
16071 switch (encoding)
c906108c 16072 {
6ccb9162
UW
16073 case DW_ATE_address:
16074 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 16075 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 16076 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
16077 break;
16078 case DW_ATE_boolean:
19f392bc 16079 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
16080 break;
16081 case DW_ATE_complex_float:
9b790ce7 16082 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 16083 type = init_complex_type (objfile, name, type);
6ccb9162
UW
16084 break;
16085 case DW_ATE_decimal_float:
19f392bc 16086 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
16087 break;
16088 case DW_ATE_float:
9b790ce7 16089 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
16090 break;
16091 case DW_ATE_signed:
19f392bc 16092 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
16093 break;
16094 case DW_ATE_unsigned:
3b2b8fea
TT
16095 if (cu->language == language_fortran
16096 && name
61012eef 16097 && startswith (name, "character("))
19f392bc
UW
16098 type = init_character_type (objfile, bits, 1, name);
16099 else
16100 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
16101 break;
16102 case DW_ATE_signed_char:
6e70227d 16103 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
16104 || cu->language == language_pascal
16105 || cu->language == language_fortran)
19f392bc
UW
16106 type = init_character_type (objfile, bits, 0, name);
16107 else
16108 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
16109 break;
16110 case DW_ATE_unsigned_char:
868a0084 16111 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 16112 || cu->language == language_pascal
c44af4eb
TT
16113 || cu->language == language_fortran
16114 || cu->language == language_rust)
19f392bc
UW
16115 type = init_character_type (objfile, bits, 1, name);
16116 else
16117 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 16118 break;
75079b2b 16119 case DW_ATE_UTF:
53e710ac
PA
16120 {
16121 gdbarch *arch = get_objfile_arch (objfile);
16122
16123 if (bits == 16)
16124 type = builtin_type (arch)->builtin_char16;
16125 else if (bits == 32)
16126 type = builtin_type (arch)->builtin_char32;
16127 else
16128 {
16129 complaint (&symfile_complaints,
16130 _("unsupported DW_ATE_UTF bit size: '%d'"),
16131 bits);
16132 type = init_integer_type (objfile, bits, 1, name);
16133 }
16134 return set_die_type (die, type, cu);
16135 }
75079b2b
TT
16136 break;
16137
6ccb9162
UW
16138 default:
16139 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
16140 dwarf_type_encoding_name (encoding));
77b7c781 16141 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 16142 break;
c906108c 16143 }
6ccb9162 16144
0114d602 16145 if (name && strcmp (name, "char") == 0)
876cecd0 16146 TYPE_NOSIGN (type) = 1;
0114d602 16147
f792889a 16148 return set_die_type (die, type, cu);
c906108c
SS
16149}
16150
80180f79
SA
16151/* Parse dwarf attribute if it's a block, reference or constant and put the
16152 resulting value of the attribute into struct bound_prop.
16153 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
16154
16155static int
16156attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
16157 struct dwarf2_cu *cu, struct dynamic_prop *prop)
16158{
16159 struct dwarf2_property_baton *baton;
16160 struct obstack *obstack = &cu->objfile->objfile_obstack;
16161
16162 if (attr == NULL || prop == NULL)
16163 return 0;
16164
16165 if (attr_form_is_block (attr))
16166 {
8d749320 16167 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
16168 baton->referenced_type = NULL;
16169 baton->locexpr.per_cu = cu->per_cu;
16170 baton->locexpr.size = DW_BLOCK (attr)->size;
16171 baton->locexpr.data = DW_BLOCK (attr)->data;
16172 prop->data.baton = baton;
16173 prop->kind = PROP_LOCEXPR;
16174 gdb_assert (prop->data.baton != NULL);
16175 }
16176 else if (attr_form_is_ref (attr))
16177 {
16178 struct dwarf2_cu *target_cu = cu;
16179 struct die_info *target_die;
16180 struct attribute *target_attr;
16181
16182 target_die = follow_die_ref (die, attr, &target_cu);
16183 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
16184 if (target_attr == NULL)
16185 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
16186 target_cu);
80180f79
SA
16187 if (target_attr == NULL)
16188 return 0;
16189
df25ebbd 16190 switch (target_attr->name)
80180f79 16191 {
df25ebbd
JB
16192 case DW_AT_location:
16193 if (attr_form_is_section_offset (target_attr))
16194 {
8d749320 16195 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
16196 baton->referenced_type = die_type (target_die, target_cu);
16197 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
16198 prop->data.baton = baton;
16199 prop->kind = PROP_LOCLIST;
16200 gdb_assert (prop->data.baton != NULL);
16201 }
16202 else if (attr_form_is_block (target_attr))
16203 {
8d749320 16204 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
16205 baton->referenced_type = die_type (target_die, target_cu);
16206 baton->locexpr.per_cu = cu->per_cu;
16207 baton->locexpr.size = DW_BLOCK (target_attr)->size;
16208 baton->locexpr.data = DW_BLOCK (target_attr)->data;
16209 prop->data.baton = baton;
16210 prop->kind = PROP_LOCEXPR;
16211 gdb_assert (prop->data.baton != NULL);
16212 }
16213 else
16214 {
16215 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16216 "dynamic property");
16217 return 0;
16218 }
16219 break;
16220 case DW_AT_data_member_location:
16221 {
16222 LONGEST offset;
16223
16224 if (!handle_data_member_location (target_die, target_cu,
16225 &offset))
16226 return 0;
16227
8d749320 16228 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
16229 baton->referenced_type = read_type_die (target_die->parent,
16230 target_cu);
df25ebbd
JB
16231 baton->offset_info.offset = offset;
16232 baton->offset_info.type = die_type (target_die, target_cu);
16233 prop->data.baton = baton;
16234 prop->kind = PROP_ADDR_OFFSET;
16235 break;
16236 }
80180f79
SA
16237 }
16238 }
16239 else if (attr_form_is_constant (attr))
16240 {
16241 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
16242 prop->kind = PROP_CONST;
16243 }
16244 else
16245 {
16246 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
16247 dwarf2_name (die, cu));
16248 return 0;
16249 }
16250
16251 return 1;
16252}
16253
a02abb62
JB
16254/* Read the given DW_AT_subrange DIE. */
16255
f792889a 16256static struct type *
a02abb62
JB
16257read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
16258{
4c9ad8c2 16259 struct type *base_type, *orig_base_type;
a02abb62
JB
16260 struct type *range_type;
16261 struct attribute *attr;
729efb13 16262 struct dynamic_prop low, high;
4fae6e18 16263 int low_default_is_valid;
c451ebe5 16264 int high_bound_is_count = 0;
15d034d0 16265 const char *name;
43bbcdc2 16266 LONGEST negative_mask;
e77813c8 16267
4c9ad8c2
TT
16268 orig_base_type = die_type (die, cu);
16269 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
16270 whereas the real type might be. So, we use ORIG_BASE_TYPE when
16271 creating the range type, but we use the result of check_typedef
16272 when examining properties of the type. */
16273 base_type = check_typedef (orig_base_type);
a02abb62 16274
7e314c57
JK
16275 /* The die_type call above may have already set the type for this DIE. */
16276 range_type = get_die_type (die, cu);
16277 if (range_type)
16278 return range_type;
16279
729efb13
SA
16280 low.kind = PROP_CONST;
16281 high.kind = PROP_CONST;
16282 high.data.const_val = 0;
16283
4fae6e18
JK
16284 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
16285 omitting DW_AT_lower_bound. */
16286 switch (cu->language)
6e70227d 16287 {
4fae6e18
JK
16288 case language_c:
16289 case language_cplus:
729efb13 16290 low.data.const_val = 0;
4fae6e18
JK
16291 low_default_is_valid = 1;
16292 break;
16293 case language_fortran:
729efb13 16294 low.data.const_val = 1;
4fae6e18
JK
16295 low_default_is_valid = 1;
16296 break;
16297 case language_d:
4fae6e18 16298 case language_objc:
c44af4eb 16299 case language_rust:
729efb13 16300 low.data.const_val = 0;
4fae6e18
JK
16301 low_default_is_valid = (cu->header.version >= 4);
16302 break;
16303 case language_ada:
16304 case language_m2:
16305 case language_pascal:
729efb13 16306 low.data.const_val = 1;
4fae6e18
JK
16307 low_default_is_valid = (cu->header.version >= 4);
16308 break;
16309 default:
729efb13 16310 low.data.const_val = 0;
4fae6e18
JK
16311 low_default_is_valid = 0;
16312 break;
a02abb62
JB
16313 }
16314
e142c38c 16315 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 16316 if (attr)
11c1ba78 16317 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
16318 else if (!low_default_is_valid)
16319 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
16320 "- DIE at 0x%x [in module %s]"),
9c541725 16321 to_underlying (die->sect_off), objfile_name (cu->objfile));
a02abb62 16322
e142c38c 16323 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 16324 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
16325 {
16326 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 16327 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 16328 {
c451ebe5
SA
16329 /* If bounds are constant do the final calculation here. */
16330 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
16331 high.data.const_val = low.data.const_val + high.data.const_val - 1;
16332 else
16333 high_bound_is_count = 1;
c2ff108b 16334 }
e77813c8
PM
16335 }
16336
16337 /* Dwarf-2 specifications explicitly allows to create subrange types
16338 without specifying a base type.
16339 In that case, the base type must be set to the type of
16340 the lower bound, upper bound or count, in that order, if any of these
16341 three attributes references an object that has a type.
16342 If no base type is found, the Dwarf-2 specifications say that
16343 a signed integer type of size equal to the size of an address should
16344 be used.
16345 For the following C code: `extern char gdb_int [];'
16346 GCC produces an empty range DIE.
16347 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 16348 high bound or count are not yet handled by this code. */
e77813c8
PM
16349 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
16350 {
16351 struct objfile *objfile = cu->objfile;
16352 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16353 int addr_size = gdbarch_addr_bit (gdbarch) /8;
16354 struct type *int_type = objfile_type (objfile)->builtin_int;
16355
16356 /* Test "int", "long int", and "long long int" objfile types,
16357 and select the first one having a size above or equal to the
16358 architecture address size. */
16359 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16360 base_type = int_type;
16361 else
16362 {
16363 int_type = objfile_type (objfile)->builtin_long;
16364 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16365 base_type = int_type;
16366 else
16367 {
16368 int_type = objfile_type (objfile)->builtin_long_long;
16369 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16370 base_type = int_type;
16371 }
16372 }
16373 }
a02abb62 16374
dbb9c2b1
JB
16375 /* Normally, the DWARF producers are expected to use a signed
16376 constant form (Eg. DW_FORM_sdata) to express negative bounds.
16377 But this is unfortunately not always the case, as witnessed
16378 with GCC, for instance, where the ambiguous DW_FORM_dataN form
16379 is used instead. To work around that ambiguity, we treat
16380 the bounds as signed, and thus sign-extend their values, when
16381 the base type is signed. */
6e70227d 16382 negative_mask =
66c6502d 16383 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
16384 if (low.kind == PROP_CONST
16385 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
16386 low.data.const_val |= negative_mask;
16387 if (high.kind == PROP_CONST
16388 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
16389 high.data.const_val |= negative_mask;
43bbcdc2 16390
729efb13 16391 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 16392
c451ebe5
SA
16393 if (high_bound_is_count)
16394 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
16395
c2ff108b
JK
16396 /* Ada expects an empty array on no boundary attributes. */
16397 if (attr == NULL && cu->language != language_ada)
729efb13 16398 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 16399
39cbfefa
DJ
16400 name = dwarf2_name (die, cu);
16401 if (name)
16402 TYPE_NAME (range_type) = name;
6e70227d 16403
e142c38c 16404 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
16405 if (attr)
16406 TYPE_LENGTH (range_type) = DW_UNSND (attr);
16407
7e314c57
JK
16408 set_die_type (die, range_type, cu);
16409
16410 /* set_die_type should be already done. */
b4ba55a1
JB
16411 set_descriptive_type (range_type, die, cu);
16412
7e314c57 16413 return range_type;
a02abb62 16414}
6e70227d 16415
f792889a 16416static struct type *
81a17f79
JB
16417read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
16418{
16419 struct type *type;
81a17f79 16420
81a17f79
JB
16421 /* For now, we only support the C meaning of an unspecified type: void. */
16422
19f392bc 16423 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 16424 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 16425
f792889a 16426 return set_die_type (die, type, cu);
81a17f79 16427}
a02abb62 16428
639d11d3
DC
16429/* Read a single die and all its descendents. Set the die's sibling
16430 field to NULL; set other fields in the die correctly, and set all
16431 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
16432 location of the info_ptr after reading all of those dies. PARENT
16433 is the parent of the die in question. */
16434
16435static struct die_info *
dee91e82 16436read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
16437 const gdb_byte *info_ptr,
16438 const gdb_byte **new_info_ptr,
dee91e82 16439 struct die_info *parent)
639d11d3
DC
16440{
16441 struct die_info *die;
d521ce57 16442 const gdb_byte *cur_ptr;
639d11d3
DC
16443 int has_children;
16444
bf6af496 16445 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
16446 if (die == NULL)
16447 {
16448 *new_info_ptr = cur_ptr;
16449 return NULL;
16450 }
93311388 16451 store_in_ref_table (die, reader->cu);
639d11d3
DC
16452
16453 if (has_children)
bf6af496 16454 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
16455 else
16456 {
16457 die->child = NULL;
16458 *new_info_ptr = cur_ptr;
16459 }
16460
16461 die->sibling = NULL;
16462 die->parent = parent;
16463 return die;
16464}
16465
16466/* Read a die, all of its descendents, and all of its siblings; set
16467 all of the fields of all of the dies correctly. Arguments are as
16468 in read_die_and_children. */
16469
16470static struct die_info *
bf6af496 16471read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
16472 const gdb_byte *info_ptr,
16473 const gdb_byte **new_info_ptr,
bf6af496 16474 struct die_info *parent)
639d11d3
DC
16475{
16476 struct die_info *first_die, *last_sibling;
d521ce57 16477 const gdb_byte *cur_ptr;
639d11d3 16478
c906108c 16479 cur_ptr = info_ptr;
639d11d3
DC
16480 first_die = last_sibling = NULL;
16481
16482 while (1)
c906108c 16483 {
639d11d3 16484 struct die_info *die
dee91e82 16485 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 16486
1d325ec1 16487 if (die == NULL)
c906108c 16488 {
639d11d3
DC
16489 *new_info_ptr = cur_ptr;
16490 return first_die;
c906108c 16491 }
1d325ec1
DJ
16492
16493 if (!first_die)
16494 first_die = die;
c906108c 16495 else
1d325ec1
DJ
16496 last_sibling->sibling = die;
16497
16498 last_sibling = die;
c906108c 16499 }
c906108c
SS
16500}
16501
bf6af496
DE
16502/* Read a die, all of its descendents, and all of its siblings; set
16503 all of the fields of all of the dies correctly. Arguments are as
16504 in read_die_and_children.
16505 This the main entry point for reading a DIE and all its children. */
16506
16507static struct die_info *
16508read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
16509 const gdb_byte *info_ptr,
16510 const gdb_byte **new_info_ptr,
bf6af496
DE
16511 struct die_info *parent)
16512{
16513 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
16514 new_info_ptr, parent);
16515
b4f54984 16516 if (dwarf_die_debug)
bf6af496
DE
16517 {
16518 fprintf_unfiltered (gdb_stdlog,
16519 "Read die from %s@0x%x of %s:\n",
a32a8923 16520 get_section_name (reader->die_section),
bf6af496
DE
16521 (unsigned) (info_ptr - reader->die_section->buffer),
16522 bfd_get_filename (reader->abfd));
b4f54984 16523 dump_die (die, dwarf_die_debug);
bf6af496
DE
16524 }
16525
16526 return die;
16527}
16528
3019eac3
DE
16529/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
16530 attributes.
16531 The caller is responsible for filling in the extra attributes
16532 and updating (*DIEP)->num_attrs.
16533 Set DIEP to point to a newly allocated die with its information,
16534 except for its child, sibling, and parent fields.
16535 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 16536
d521ce57 16537static const gdb_byte *
3019eac3 16538read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 16539 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 16540 int *has_children, int num_extra_attrs)
93311388 16541{
b64f50a1 16542 unsigned int abbrev_number, bytes_read, i;
93311388
DE
16543 struct abbrev_info *abbrev;
16544 struct die_info *die;
16545 struct dwarf2_cu *cu = reader->cu;
16546 bfd *abfd = reader->abfd;
16547
9c541725 16548 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
16549 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16550 info_ptr += bytes_read;
16551 if (!abbrev_number)
16552 {
16553 *diep = NULL;
16554 *has_children = 0;
16555 return info_ptr;
16556 }
16557
433df2d4 16558 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 16559 if (!abbrev)
348e048f
DE
16560 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
16561 abbrev_number,
16562 bfd_get_filename (abfd));
16563
3019eac3 16564 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 16565 die->sect_off = sect_off;
93311388
DE
16566 die->tag = abbrev->tag;
16567 die->abbrev = abbrev_number;
16568
3019eac3
DE
16569 /* Make the result usable.
16570 The caller needs to update num_attrs after adding the extra
16571 attributes. */
93311388
DE
16572 die->num_attrs = abbrev->num_attrs;
16573
16574 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
16575 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
16576 info_ptr);
93311388
DE
16577
16578 *diep = die;
16579 *has_children = abbrev->has_children;
16580 return info_ptr;
16581}
16582
3019eac3
DE
16583/* Read a die and all its attributes.
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. */
16587
d521ce57 16588static const gdb_byte *
3019eac3 16589read_full_die (const struct die_reader_specs *reader,
d521ce57 16590 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
16591 int *has_children)
16592{
d521ce57 16593 const gdb_byte *result;
bf6af496
DE
16594
16595 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
16596
b4f54984 16597 if (dwarf_die_debug)
bf6af496
DE
16598 {
16599 fprintf_unfiltered (gdb_stdlog,
16600 "Read die from %s@0x%x of %s:\n",
a32a8923 16601 get_section_name (reader->die_section),
bf6af496
DE
16602 (unsigned) (info_ptr - reader->die_section->buffer),
16603 bfd_get_filename (reader->abfd));
b4f54984 16604 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
16605 }
16606
16607 return result;
3019eac3 16608}
433df2d4
DE
16609\f
16610/* Abbreviation tables.
3019eac3 16611
433df2d4 16612 In DWARF version 2, the description of the debugging information is
c906108c
SS
16613 stored in a separate .debug_abbrev section. Before we read any
16614 dies from a section we read in all abbreviations and install them
433df2d4
DE
16615 in a hash table. */
16616
16617/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
16618
16619static struct abbrev_info *
16620abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
16621{
16622 struct abbrev_info *abbrev;
16623
8d749320 16624 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 16625 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 16626
433df2d4
DE
16627 return abbrev;
16628}
16629
16630/* Add an abbreviation to the table. */
c906108c
SS
16631
16632static void
433df2d4
DE
16633abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
16634 unsigned int abbrev_number,
16635 struct abbrev_info *abbrev)
16636{
16637 unsigned int hash_number;
16638
16639 hash_number = abbrev_number % ABBREV_HASH_SIZE;
16640 abbrev->next = abbrev_table->abbrevs[hash_number];
16641 abbrev_table->abbrevs[hash_number] = abbrev;
16642}
dee91e82 16643
433df2d4
DE
16644/* Look up an abbrev in the table.
16645 Returns NULL if the abbrev is not found. */
16646
16647static struct abbrev_info *
16648abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
16649 unsigned int abbrev_number)
c906108c 16650{
433df2d4
DE
16651 unsigned int hash_number;
16652 struct abbrev_info *abbrev;
16653
16654 hash_number = abbrev_number % ABBREV_HASH_SIZE;
16655 abbrev = abbrev_table->abbrevs[hash_number];
16656
16657 while (abbrev)
16658 {
16659 if (abbrev->number == abbrev_number)
16660 return abbrev;
16661 abbrev = abbrev->next;
16662 }
16663 return NULL;
16664}
16665
16666/* Read in an abbrev table. */
16667
16668static struct abbrev_table *
16669abbrev_table_read_table (struct dwarf2_section_info *section,
9c541725 16670 sect_offset sect_off)
433df2d4
DE
16671{
16672 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 16673 bfd *abfd = get_section_bfd_owner (section);
433df2d4 16674 struct abbrev_table *abbrev_table;
d521ce57 16675 const gdb_byte *abbrev_ptr;
c906108c
SS
16676 struct abbrev_info *cur_abbrev;
16677 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 16678 unsigned int abbrev_form;
f3dd6933
DJ
16679 struct attr_abbrev *cur_attrs;
16680 unsigned int allocated_attrs;
c906108c 16681
70ba0933 16682 abbrev_table = XNEW (struct abbrev_table);
9c541725 16683 abbrev_table->sect_off = sect_off;
433df2d4 16684 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
16685 abbrev_table->abbrevs =
16686 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
16687 ABBREV_HASH_SIZE);
433df2d4
DE
16688 memset (abbrev_table->abbrevs, 0,
16689 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 16690
433df2d4 16691 dwarf2_read_section (objfile, section);
9c541725 16692 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
16693 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16694 abbrev_ptr += bytes_read;
16695
f3dd6933 16696 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 16697 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 16698
0963b4bd 16699 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
16700 while (abbrev_number)
16701 {
433df2d4 16702 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
16703
16704 /* read in abbrev header */
16705 cur_abbrev->number = abbrev_number;
aead7601
SM
16706 cur_abbrev->tag
16707 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
16708 abbrev_ptr += bytes_read;
16709 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
16710 abbrev_ptr += 1;
16711
16712 /* now read in declarations */
22d2f3ab 16713 for (;;)
c906108c 16714 {
43988095
JK
16715 LONGEST implicit_const;
16716
22d2f3ab
JK
16717 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16718 abbrev_ptr += bytes_read;
16719 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16720 abbrev_ptr += bytes_read;
43988095
JK
16721 if (abbrev_form == DW_FORM_implicit_const)
16722 {
16723 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
16724 &bytes_read);
16725 abbrev_ptr += bytes_read;
16726 }
16727 else
16728 {
16729 /* Initialize it due to a false compiler warning. */
16730 implicit_const = -1;
16731 }
22d2f3ab
JK
16732
16733 if (abbrev_name == 0)
16734 break;
16735
f3dd6933 16736 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 16737 {
f3dd6933
DJ
16738 allocated_attrs += ATTR_ALLOC_CHUNK;
16739 cur_attrs
224c3ddb 16740 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 16741 }
ae038cb0 16742
aead7601
SM
16743 cur_attrs[cur_abbrev->num_attrs].name
16744 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 16745 cur_attrs[cur_abbrev->num_attrs].form
aead7601 16746 = (enum dwarf_form) abbrev_form;
43988095 16747 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 16748 ++cur_abbrev->num_attrs;
c906108c
SS
16749 }
16750
8d749320
SM
16751 cur_abbrev->attrs =
16752 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
16753 cur_abbrev->num_attrs);
f3dd6933
DJ
16754 memcpy (cur_abbrev->attrs, cur_attrs,
16755 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
16756
433df2d4 16757 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
16758
16759 /* Get next abbreviation.
16760 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
16761 always properly terminated with an abbrev number of 0.
16762 Exit loop if we encounter an abbreviation which we have
16763 already read (which means we are about to read the abbreviations
16764 for the next compile unit) or if the end of the abbreviation
16765 table is reached. */
433df2d4 16766 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
16767 break;
16768 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16769 abbrev_ptr += bytes_read;
433df2d4 16770 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
16771 break;
16772 }
f3dd6933
DJ
16773
16774 xfree (cur_attrs);
433df2d4 16775 return abbrev_table;
c906108c
SS
16776}
16777
433df2d4 16778/* Free the resources held by ABBREV_TABLE. */
c906108c 16779
c906108c 16780static void
433df2d4 16781abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 16782{
433df2d4
DE
16783 obstack_free (&abbrev_table->abbrev_obstack, NULL);
16784 xfree (abbrev_table);
c906108c
SS
16785}
16786
f4dc4d17
DE
16787/* Same as abbrev_table_free but as a cleanup.
16788 We pass in a pointer to the pointer to the table so that we can
16789 set the pointer to NULL when we're done. It also simplifies
73051182 16790 build_type_psymtabs_1. */
f4dc4d17
DE
16791
16792static void
16793abbrev_table_free_cleanup (void *table_ptr)
16794{
9a3c8263 16795 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
16796
16797 if (*abbrev_table_ptr != NULL)
16798 abbrev_table_free (*abbrev_table_ptr);
16799 *abbrev_table_ptr = NULL;
16800}
16801
433df2d4
DE
16802/* Read the abbrev table for CU from ABBREV_SECTION. */
16803
16804static void
16805dwarf2_read_abbrevs (struct dwarf2_cu *cu,
16806 struct dwarf2_section_info *abbrev_section)
c906108c 16807{
433df2d4 16808 cu->abbrev_table =
9c541725 16809 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
433df2d4 16810}
c906108c 16811
433df2d4 16812/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 16813
433df2d4
DE
16814static void
16815dwarf2_free_abbrev_table (void *ptr_to_cu)
16816{
9a3c8263 16817 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 16818
a2ce51a0
DE
16819 if (cu->abbrev_table != NULL)
16820 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
16821 /* Set this to NULL so that we SEGV if we try to read it later,
16822 and also because free_comp_unit verifies this is NULL. */
16823 cu->abbrev_table = NULL;
16824}
16825\f
72bf9492
DJ
16826/* Returns nonzero if TAG represents a type that we might generate a partial
16827 symbol for. */
16828
16829static int
16830is_type_tag_for_partial (int tag)
16831{
16832 switch (tag)
16833 {
16834#if 0
16835 /* Some types that would be reasonable to generate partial symbols for,
16836 that we don't at present. */
16837 case DW_TAG_array_type:
16838 case DW_TAG_file_type:
16839 case DW_TAG_ptr_to_member_type:
16840 case DW_TAG_set_type:
16841 case DW_TAG_string_type:
16842 case DW_TAG_subroutine_type:
16843#endif
16844 case DW_TAG_base_type:
16845 case DW_TAG_class_type:
680b30c7 16846 case DW_TAG_interface_type:
72bf9492
DJ
16847 case DW_TAG_enumeration_type:
16848 case DW_TAG_structure_type:
16849 case DW_TAG_subrange_type:
16850 case DW_TAG_typedef:
16851 case DW_TAG_union_type:
16852 return 1;
16853 default:
16854 return 0;
16855 }
16856}
16857
16858/* Load all DIEs that are interesting for partial symbols into memory. */
16859
16860static struct partial_die_info *
dee91e82 16861load_partial_dies (const struct die_reader_specs *reader,
d521ce57 16862 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 16863{
dee91e82 16864 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16865 struct objfile *objfile = cu->objfile;
72bf9492
DJ
16866 struct partial_die_info *part_die;
16867 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
16868 struct abbrev_info *abbrev;
16869 unsigned int bytes_read;
5afb4e99 16870 unsigned int load_all = 0;
72bf9492
DJ
16871 int nesting_level = 1;
16872
16873 parent_die = NULL;
16874 last_die = NULL;
16875
7adf1e79
DE
16876 gdb_assert (cu->per_cu != NULL);
16877 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
16878 load_all = 1;
16879
72bf9492
DJ
16880 cu->partial_dies
16881 = htab_create_alloc_ex (cu->header.length / 12,
16882 partial_die_hash,
16883 partial_die_eq,
16884 NULL,
16885 &cu->comp_unit_obstack,
16886 hashtab_obstack_allocate,
16887 dummy_obstack_deallocate);
16888
8d749320 16889 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16890
16891 while (1)
16892 {
16893 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
16894
16895 /* A NULL abbrev means the end of a series of children. */
16896 if (abbrev == NULL)
16897 {
16898 if (--nesting_level == 0)
16899 {
16900 /* PART_DIE was probably the last thing allocated on the
16901 comp_unit_obstack, so we could call obstack_free
16902 here. We don't do that because the waste is small,
16903 and will be cleaned up when we're done with this
16904 compilation unit. This way, we're also more robust
16905 against other users of the comp_unit_obstack. */
16906 return first_die;
16907 }
16908 info_ptr += bytes_read;
16909 last_die = parent_die;
16910 parent_die = parent_die->die_parent;
16911 continue;
16912 }
16913
98bfdba5
PA
16914 /* Check for template arguments. We never save these; if
16915 they're seen, we just mark the parent, and go on our way. */
16916 if (parent_die != NULL
16917 && cu->language == language_cplus
16918 && (abbrev->tag == DW_TAG_template_type_param
16919 || abbrev->tag == DW_TAG_template_value_param))
16920 {
16921 parent_die->has_template_arguments = 1;
16922
16923 if (!load_all)
16924 {
16925 /* We don't need a partial DIE for the template argument. */
dee91e82 16926 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
16927 continue;
16928 }
16929 }
16930
0d99eb77 16931 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
16932 Skip their other children. */
16933 if (!load_all
16934 && cu->language == language_cplus
16935 && parent_die != NULL
16936 && parent_die->tag == DW_TAG_subprogram)
16937 {
dee91e82 16938 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
16939 continue;
16940 }
16941
5afb4e99
DJ
16942 /* Check whether this DIE is interesting enough to save. Normally
16943 we would not be interested in members here, but there may be
16944 later variables referencing them via DW_AT_specification (for
16945 static members). */
16946 if (!load_all
16947 && !is_type_tag_for_partial (abbrev->tag)
72929c62 16948 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
16949 && abbrev->tag != DW_TAG_enumerator
16950 && abbrev->tag != DW_TAG_subprogram
bc30ff58 16951 && abbrev->tag != DW_TAG_lexical_block
72bf9492 16952 && abbrev->tag != DW_TAG_variable
5afb4e99 16953 && abbrev->tag != DW_TAG_namespace
f55ee35c 16954 && abbrev->tag != DW_TAG_module
95554aad 16955 && abbrev->tag != DW_TAG_member
74921315
KS
16956 && abbrev->tag != DW_TAG_imported_unit
16957 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
16958 {
16959 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16960 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
16961 continue;
16962 }
16963
dee91e82
DE
16964 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
16965 info_ptr);
72bf9492
DJ
16966
16967 /* This two-pass algorithm for processing partial symbols has a
16968 high cost in cache pressure. Thus, handle some simple cases
16969 here which cover the majority of C partial symbols. DIEs
16970 which neither have specification tags in them, nor could have
16971 specification tags elsewhere pointing at them, can simply be
16972 processed and discarded.
16973
16974 This segment is also optional; scan_partial_symbols and
16975 add_partial_symbol will handle these DIEs if we chain
16976 them in normally. When compilers which do not emit large
16977 quantities of duplicate debug information are more common,
16978 this code can probably be removed. */
16979
16980 /* Any complete simple types at the top level (pretty much all
16981 of them, for a language without namespaces), can be processed
16982 directly. */
16983 if (parent_die == NULL
16984 && part_die->has_specification == 0
16985 && part_die->is_declaration == 0
d8228535 16986 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
16987 || part_die->tag == DW_TAG_base_type
16988 || part_die->tag == DW_TAG_subrange_type))
16989 {
16990 if (building_psymtab && part_die->name != NULL)
04a679b8 16991 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16992 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 16993 &objfile->static_psymbols,
1762568f 16994 0, cu->language, objfile);
dee91e82 16995 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16996 continue;
16997 }
16998
d8228535
JK
16999 /* The exception for DW_TAG_typedef with has_children above is
17000 a workaround of GCC PR debug/47510. In the case of this complaint
17001 type_name_no_tag_or_error will error on such types later.
17002
17003 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17004 it could not find the child DIEs referenced later, this is checked
17005 above. In correct DWARF DW_TAG_typedef should have no children. */
17006
17007 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
17008 complaint (&symfile_complaints,
17009 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17010 "- DIE at 0x%x [in module %s]"),
9c541725 17011 to_underlying (part_die->sect_off), objfile_name (objfile));
d8228535 17012
72bf9492
DJ
17013 /* If we're at the second level, and we're an enumerator, and
17014 our parent has no specification (meaning possibly lives in a
17015 namespace elsewhere), then we can add the partial symbol now
17016 instead of queueing it. */
17017 if (part_die->tag == DW_TAG_enumerator
17018 && parent_die != NULL
17019 && parent_die->die_parent == NULL
17020 && parent_die->tag == DW_TAG_enumeration_type
17021 && parent_die->has_specification == 0)
17022 {
17023 if (part_die->name == NULL)
3e43a32a
MS
17024 complaint (&symfile_complaints,
17025 _("malformed enumerator DIE ignored"));
72bf9492 17026 else if (building_psymtab)
04a679b8 17027 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 17028 VAR_DOMAIN, LOC_CONST,
9c37b5ae 17029 cu->language == language_cplus
bb5ed363
DE
17030 ? &objfile->global_psymbols
17031 : &objfile->static_psymbols,
1762568f 17032 0, cu->language, objfile);
72bf9492 17033
dee91e82 17034 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
17035 continue;
17036 }
17037
17038 /* We'll save this DIE so link it in. */
17039 part_die->die_parent = parent_die;
17040 part_die->die_sibling = NULL;
17041 part_die->die_child = NULL;
17042
17043 if (last_die && last_die == parent_die)
17044 last_die->die_child = part_die;
17045 else if (last_die)
17046 last_die->die_sibling = part_die;
17047
17048 last_die = part_die;
17049
17050 if (first_die == NULL)
17051 first_die = part_die;
17052
17053 /* Maybe add the DIE to the hash table. Not all DIEs that we
17054 find interesting need to be in the hash table, because we
17055 also have the parent/sibling/child chains; only those that we
17056 might refer to by offset later during partial symbol reading.
17057
17058 For now this means things that might have be the target of a
17059 DW_AT_specification, DW_AT_abstract_origin, or
17060 DW_AT_extension. DW_AT_extension will refer only to
17061 namespaces; DW_AT_abstract_origin refers to functions (and
17062 many things under the function DIE, but we do not recurse
17063 into function DIEs during partial symbol reading) and
17064 possibly variables as well; DW_AT_specification refers to
17065 declarations. Declarations ought to have the DW_AT_declaration
17066 flag. It happens that GCC forgets to put it in sometimes, but
17067 only for functions, not for types.
17068
17069 Adding more things than necessary to the hash table is harmless
17070 except for the performance cost. Adding too few will result in
5afb4e99
DJ
17071 wasted time in find_partial_die, when we reread the compilation
17072 unit with load_all_dies set. */
72bf9492 17073
5afb4e99 17074 if (load_all
72929c62 17075 || abbrev->tag == DW_TAG_constant
5afb4e99 17076 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
17077 || abbrev->tag == DW_TAG_variable
17078 || abbrev->tag == DW_TAG_namespace
17079 || part_die->is_declaration)
17080 {
17081 void **slot;
17082
17083 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
17084 to_underlying (part_die->sect_off),
17085 INSERT);
72bf9492
DJ
17086 *slot = part_die;
17087 }
17088
8d749320 17089 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
17090
17091 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 17092 we have no reason to follow the children of structures; for other
98bfdba5
PA
17093 languages we have to, so that we can get at method physnames
17094 to infer fully qualified class names, for DW_AT_specification,
17095 and for C++ template arguments. For C++, we also look one level
17096 inside functions to find template arguments (if the name of the
17097 function does not already contain the template arguments).
bc30ff58
JB
17098
17099 For Ada, we need to scan the children of subprograms and lexical
17100 blocks as well because Ada allows the definition of nested
17101 entities that could be interesting for the debugger, such as
17102 nested subprograms for instance. */
72bf9492 17103 if (last_die->has_children
5afb4e99
DJ
17104 && (load_all
17105 || last_die->tag == DW_TAG_namespace
f55ee35c 17106 || last_die->tag == DW_TAG_module
72bf9492 17107 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
17108 || (cu->language == language_cplus
17109 && last_die->tag == DW_TAG_subprogram
17110 && (last_die->name == NULL
17111 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
17112 || (cu->language != language_c
17113 && (last_die->tag == DW_TAG_class_type
680b30c7 17114 || last_die->tag == DW_TAG_interface_type
72bf9492 17115 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
17116 || last_die->tag == DW_TAG_union_type))
17117 || (cu->language == language_ada
17118 && (last_die->tag == DW_TAG_subprogram
17119 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
17120 {
17121 nesting_level++;
17122 parent_die = last_die;
17123 continue;
17124 }
17125
17126 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17127 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
17128
17129 /* Back to the top, do it again. */
17130 }
17131}
17132
c906108c
SS
17133/* Read a minimal amount of information into the minimal die structure. */
17134
d521ce57 17135static const gdb_byte *
dee91e82
DE
17136read_partial_die (const struct die_reader_specs *reader,
17137 struct partial_die_info *part_die,
17138 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 17139 const gdb_byte *info_ptr)
c906108c 17140{
dee91e82 17141 struct dwarf2_cu *cu = reader->cu;
bb5ed363 17142 struct objfile *objfile = cu->objfile;
d521ce57 17143 const gdb_byte *buffer = reader->buffer;
fa238c03 17144 unsigned int i;
c906108c 17145 struct attribute attr;
c5aa993b 17146 int has_low_pc_attr = 0;
c906108c 17147 int has_high_pc_attr = 0;
91da1414 17148 int high_pc_relative = 0;
c906108c 17149
72bf9492 17150 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 17151
9c541725 17152 part_die->sect_off = (sect_offset) (info_ptr - buffer);
72bf9492
DJ
17153
17154 info_ptr += abbrev_len;
17155
17156 if (abbrev == NULL)
17157 return info_ptr;
17158
c906108c
SS
17159 part_die->tag = abbrev->tag;
17160 part_die->has_children = abbrev->has_children;
c906108c
SS
17161
17162 for (i = 0; i < abbrev->num_attrs; ++i)
17163 {
dee91e82 17164 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
17165
17166 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 17167 partial symbol table. */
c906108c
SS
17168 switch (attr.name)
17169 {
17170 case DW_AT_name:
71c25dea
TT
17171 switch (part_die->tag)
17172 {
17173 case DW_TAG_compile_unit:
95554aad 17174 case DW_TAG_partial_unit:
348e048f 17175 case DW_TAG_type_unit:
71c25dea
TT
17176 /* Compilation units have a DW_AT_name that is a filename, not
17177 a source language identifier. */
17178 case DW_TAG_enumeration_type:
17179 case DW_TAG_enumerator:
17180 /* These tags always have simple identifiers already; no need
17181 to canonicalize them. */
17182 part_die->name = DW_STRING (&attr);
17183 break;
17184 default:
17185 part_die->name
17186 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 17187 &objfile->per_bfd->storage_obstack);
71c25dea
TT
17188 break;
17189 }
c906108c 17190 break;
31ef98ae 17191 case DW_AT_linkage_name:
c906108c 17192 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
17193 /* Note that both forms of linkage name might appear. We
17194 assume they will be the same, and we only store the last
17195 one we see. */
94af9270
KS
17196 if (cu->language == language_ada)
17197 part_die->name = DW_STRING (&attr);
abc72ce4 17198 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
17199 break;
17200 case DW_AT_low_pc:
17201 has_low_pc_attr = 1;
31aa7e4e 17202 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
17203 break;
17204 case DW_AT_high_pc:
17205 has_high_pc_attr = 1;
31aa7e4e
JB
17206 part_die->highpc = attr_value_as_address (&attr);
17207 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
17208 high_pc_relative = 1;
c906108c
SS
17209 break;
17210 case DW_AT_location:
0963b4bd 17211 /* Support the .debug_loc offsets. */
8e19ed76
PS
17212 if (attr_form_is_block (&attr))
17213 {
95554aad 17214 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 17215 }
3690dd37 17216 else if (attr_form_is_section_offset (&attr))
8e19ed76 17217 {
4d3c2250 17218 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
17219 }
17220 else
17221 {
4d3c2250
KB
17222 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17223 "partial symbol information");
8e19ed76 17224 }
c906108c 17225 break;
c906108c
SS
17226 case DW_AT_external:
17227 part_die->is_external = DW_UNSND (&attr);
17228 break;
17229 case DW_AT_declaration:
17230 part_die->is_declaration = DW_UNSND (&attr);
17231 break;
17232 case DW_AT_type:
17233 part_die->has_type = 1;
17234 break;
17235 case DW_AT_abstract_origin:
17236 case DW_AT_specification:
72bf9492
DJ
17237 case DW_AT_extension:
17238 part_die->has_specification = 1;
c764a876 17239 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
17240 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17241 || cu->per_cu->is_dwz);
c906108c
SS
17242 break;
17243 case DW_AT_sibling:
17244 /* Ignore absolute siblings, they might point outside of
17245 the current compile unit. */
17246 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
17247 complaint (&symfile_complaints,
17248 _("ignoring absolute DW_AT_sibling"));
c906108c 17249 else
b9502d3f 17250 {
9c541725
PA
17251 sect_offset off = dwarf2_get_ref_die_offset (&attr);
17252 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
17253
17254 if (sibling_ptr < info_ptr)
17255 complaint (&symfile_complaints,
17256 _("DW_AT_sibling points backwards"));
22869d73
KS
17257 else if (sibling_ptr > reader->buffer_end)
17258 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
17259 else
17260 part_die->sibling = sibling_ptr;
17261 }
c906108c 17262 break;
fa4028e9
JB
17263 case DW_AT_byte_size:
17264 part_die->has_byte_size = 1;
17265 break;
ff908ebf
AW
17266 case DW_AT_const_value:
17267 part_die->has_const_value = 1;
17268 break;
68511cec
CES
17269 case DW_AT_calling_convention:
17270 /* DWARF doesn't provide a way to identify a program's source-level
17271 entry point. DW_AT_calling_convention attributes are only meant
17272 to describe functions' calling conventions.
17273
17274 However, because it's a necessary piece of information in
0c1b455e
TT
17275 Fortran, and before DWARF 4 DW_CC_program was the only
17276 piece of debugging information whose definition refers to
17277 a 'main program' at all, several compilers marked Fortran
17278 main programs with DW_CC_program --- even when those
17279 functions use the standard calling conventions.
17280
17281 Although DWARF now specifies a way to provide this
17282 information, we support this practice for backward
17283 compatibility. */
68511cec 17284 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
17285 && cu->language == language_fortran)
17286 part_die->main_subprogram = 1;
68511cec 17287 break;
481860b3
GB
17288 case DW_AT_inline:
17289 if (DW_UNSND (&attr) == DW_INL_inlined
17290 || DW_UNSND (&attr) == DW_INL_declared_inlined)
17291 part_die->may_be_inlined = 1;
17292 break;
95554aad
TT
17293
17294 case DW_AT_import:
17295 if (part_die->tag == DW_TAG_imported_unit)
36586728 17296 {
9c541725 17297 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
36586728
TT
17298 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17299 || cu->per_cu->is_dwz);
17300 }
95554aad
TT
17301 break;
17302
0c1b455e
TT
17303 case DW_AT_main_subprogram:
17304 part_die->main_subprogram = DW_UNSND (&attr);
17305 break;
17306
c906108c
SS
17307 default:
17308 break;
17309 }
17310 }
17311
91da1414
MW
17312 if (high_pc_relative)
17313 part_die->highpc += part_die->lowpc;
17314
9373cf26
JK
17315 if (has_low_pc_attr && has_high_pc_attr)
17316 {
17317 /* When using the GNU linker, .gnu.linkonce. sections are used to
17318 eliminate duplicate copies of functions and vtables and such.
17319 The linker will arbitrarily choose one and discard the others.
17320 The AT_*_pc values for such functions refer to local labels in
17321 these sections. If the section from that file was discarded, the
17322 labels are not in the output, so the relocs get a value of 0.
17323 If this is a discarded function, mark the pc bounds as invalid,
17324 so that GDB will ignore it. */
17325 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
17326 {
bb5ed363 17327 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
17328
17329 complaint (&symfile_complaints,
17330 _("DW_AT_low_pc %s is zero "
17331 "for DIE at 0x%x [in module %s]"),
17332 paddress (gdbarch, part_die->lowpc),
9c541725 17333 to_underlying (part_die->sect_off), objfile_name (objfile));
9373cf26
JK
17334 }
17335 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
17336 else if (part_die->lowpc >= part_die->highpc)
17337 {
bb5ed363 17338 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
17339
17340 complaint (&symfile_complaints,
17341 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
17342 "for DIE at 0x%x [in module %s]"),
17343 paddress (gdbarch, part_die->lowpc),
17344 paddress (gdbarch, part_die->highpc),
9c541725
PA
17345 to_underlying (part_die->sect_off),
17346 objfile_name (objfile));
9373cf26
JK
17347 }
17348 else
17349 part_die->has_pc_info = 1;
17350 }
85cbf3d3 17351
c906108c
SS
17352 return info_ptr;
17353}
17354
72bf9492
DJ
17355/* Find a cached partial DIE at OFFSET in CU. */
17356
17357static struct partial_die_info *
9c541725 17358find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
72bf9492
DJ
17359{
17360 struct partial_die_info *lookup_die = NULL;
17361 struct partial_die_info part_die;
17362
9c541725 17363 part_die.sect_off = sect_off;
9a3c8263
SM
17364 lookup_die = ((struct partial_die_info *)
17365 htab_find_with_hash (cu->partial_dies, &part_die,
9c541725 17366 to_underlying (sect_off)));
72bf9492 17367
72bf9492
DJ
17368 return lookup_die;
17369}
17370
348e048f
DE
17371/* Find a partial DIE at OFFSET, which may or may not be in CU,
17372 except in the case of .debug_types DIEs which do not reference
17373 outside their CU (they do however referencing other types via
55f1336d 17374 DW_FORM_ref_sig8). */
72bf9492
DJ
17375
17376static struct partial_die_info *
9c541725 17377find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 17378{
bb5ed363 17379 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
17380 struct dwarf2_per_cu_data *per_cu = NULL;
17381 struct partial_die_info *pd = NULL;
72bf9492 17382
36586728 17383 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 17384 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 17385 {
9c541725 17386 pd = find_partial_die_in_comp_unit (sect_off, cu);
5afb4e99
DJ
17387 if (pd != NULL)
17388 return pd;
0d99eb77
DE
17389 /* We missed recording what we needed.
17390 Load all dies and try again. */
17391 per_cu = cu->per_cu;
5afb4e99 17392 }
0d99eb77
DE
17393 else
17394 {
17395 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 17396 if (cu->per_cu->is_debug_types)
0d99eb77 17397 {
9c541725
PA
17398 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
17399 " external reference to offset 0x%x [in module %s].\n"),
17400 to_underlying (cu->header.sect_off), to_underlying (sect_off),
0d99eb77
DE
17401 bfd_get_filename (objfile->obfd));
17402 }
9c541725 17403 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 17404 objfile);
72bf9492 17405
0d99eb77
DE
17406 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
17407 load_partial_comp_unit (per_cu);
ae038cb0 17408
0d99eb77 17409 per_cu->cu->last_used = 0;
9c541725 17410 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
0d99eb77 17411 }
5afb4e99 17412
dee91e82
DE
17413 /* If we didn't find it, and not all dies have been loaded,
17414 load them all and try again. */
17415
5afb4e99
DJ
17416 if (pd == NULL && per_cu->load_all_dies == 0)
17417 {
5afb4e99 17418 per_cu->load_all_dies = 1;
fd820528
DE
17419
17420 /* This is nasty. When we reread the DIEs, somewhere up the call chain
17421 THIS_CU->cu may already be in use. So we can't just free it and
17422 replace its DIEs with the ones we read in. Instead, we leave those
17423 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
17424 and clobber THIS_CU->cu->partial_dies with the hash table for the new
17425 set. */
dee91e82 17426 load_partial_comp_unit (per_cu);
5afb4e99 17427
9c541725 17428 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
5afb4e99
DJ
17429 }
17430
17431 if (pd == NULL)
17432 internal_error (__FILE__, __LINE__,
3e43a32a
MS
17433 _("could not find partial DIE 0x%x "
17434 "in cache [from module %s]\n"),
9c541725 17435 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 17436 return pd;
72bf9492
DJ
17437}
17438
abc72ce4
DE
17439/* See if we can figure out if the class lives in a namespace. We do
17440 this by looking for a member function; its demangled name will
17441 contain namespace info, if there is any. */
17442
17443static void
17444guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
17445 struct dwarf2_cu *cu)
17446{
17447 /* NOTE: carlton/2003-10-07: Getting the info this way changes
17448 what template types look like, because the demangler
17449 frequently doesn't give the same name as the debug info. We
17450 could fix this by only using the demangled name to get the
17451 prefix (but see comment in read_structure_type). */
17452
17453 struct partial_die_info *real_pdi;
17454 struct partial_die_info *child_pdi;
17455
17456 /* If this DIE (this DIE's specification, if any) has a parent, then
17457 we should not do this. We'll prepend the parent's fully qualified
17458 name when we create the partial symbol. */
17459
17460 real_pdi = struct_pdi;
17461 while (real_pdi->has_specification)
36586728
TT
17462 real_pdi = find_partial_die (real_pdi->spec_offset,
17463 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
17464
17465 if (real_pdi->die_parent != NULL)
17466 return;
17467
17468 for (child_pdi = struct_pdi->die_child;
17469 child_pdi != NULL;
17470 child_pdi = child_pdi->die_sibling)
17471 {
17472 if (child_pdi->tag == DW_TAG_subprogram
17473 && child_pdi->linkage_name != NULL)
17474 {
17475 char *actual_class_name
17476 = language_class_name_from_physname (cu->language_defn,
17477 child_pdi->linkage_name);
17478 if (actual_class_name != NULL)
17479 {
17480 struct_pdi->name
224c3ddb
SM
17481 = ((const char *)
17482 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
17483 actual_class_name,
17484 strlen (actual_class_name)));
abc72ce4
DE
17485 xfree (actual_class_name);
17486 }
17487 break;
17488 }
17489 }
17490}
17491
72bf9492
DJ
17492/* Adjust PART_DIE before generating a symbol for it. This function
17493 may set the is_external flag or change the DIE's name. */
17494
17495static void
17496fixup_partial_die (struct partial_die_info *part_die,
17497 struct dwarf2_cu *cu)
17498{
abc72ce4
DE
17499 /* Once we've fixed up a die, there's no point in doing so again.
17500 This also avoids a memory leak if we were to call
17501 guess_partial_die_structure_name multiple times. */
17502 if (part_die->fixup_called)
17503 return;
17504
72bf9492
DJ
17505 /* If we found a reference attribute and the DIE has no name, try
17506 to find a name in the referred to DIE. */
17507
17508 if (part_die->name == NULL && part_die->has_specification)
17509 {
17510 struct partial_die_info *spec_die;
72bf9492 17511
36586728
TT
17512 spec_die = find_partial_die (part_die->spec_offset,
17513 part_die->spec_is_dwz, cu);
72bf9492 17514
10b3939b 17515 fixup_partial_die (spec_die, cu);
72bf9492
DJ
17516
17517 if (spec_die->name)
17518 {
17519 part_die->name = spec_die->name;
17520
17521 /* Copy DW_AT_external attribute if it is set. */
17522 if (spec_die->is_external)
17523 part_die->is_external = spec_die->is_external;
17524 }
17525 }
17526
17527 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
17528
17529 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 17530 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 17531
abc72ce4
DE
17532 /* If there is no parent die to provide a namespace, and there are
17533 children, see if we can determine the namespace from their linkage
122d1940 17534 name. */
abc72ce4 17535 if (cu->language == language_cplus
8b70b953 17536 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
17537 && part_die->die_parent == NULL
17538 && part_die->has_children
17539 && (part_die->tag == DW_TAG_class_type
17540 || part_die->tag == DW_TAG_structure_type
17541 || part_die->tag == DW_TAG_union_type))
17542 guess_partial_die_structure_name (part_die, cu);
17543
53832f31
TT
17544 /* GCC might emit a nameless struct or union that has a linkage
17545 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17546 if (part_die->name == NULL
96408a79
SA
17547 && (part_die->tag == DW_TAG_class_type
17548 || part_die->tag == DW_TAG_interface_type
17549 || part_die->tag == DW_TAG_structure_type
17550 || part_die->tag == DW_TAG_union_type)
53832f31
TT
17551 && part_die->linkage_name != NULL)
17552 {
17553 char *demangled;
17554
8de20a37 17555 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
17556 if (demangled)
17557 {
96408a79
SA
17558 const char *base;
17559
17560 /* Strip any leading namespaces/classes, keep only the base name.
17561 DW_AT_name for named DIEs does not contain the prefixes. */
17562 base = strrchr (demangled, ':');
17563 if (base && base > demangled && base[-1] == ':')
17564 base++;
17565 else
17566 base = demangled;
17567
34a68019 17568 part_die->name
224c3ddb
SM
17569 = ((const char *)
17570 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
17571 base, strlen (base)));
53832f31
TT
17572 xfree (demangled);
17573 }
17574 }
17575
abc72ce4 17576 part_die->fixup_called = 1;
72bf9492
DJ
17577}
17578
a8329558 17579/* Read an attribute value described by an attribute form. */
c906108c 17580
d521ce57 17581static const gdb_byte *
dee91e82
DE
17582read_attribute_value (const struct die_reader_specs *reader,
17583 struct attribute *attr, unsigned form,
43988095 17584 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 17585{
dee91e82 17586 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
17587 struct objfile *objfile = cu->objfile;
17588 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 17589 bfd *abfd = reader->abfd;
e7c27a73 17590 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
17591 unsigned int bytes_read;
17592 struct dwarf_block *blk;
17593
aead7601 17594 attr->form = (enum dwarf_form) form;
a8329558 17595 switch (form)
c906108c 17596 {
c906108c 17597 case DW_FORM_ref_addr:
ae411497 17598 if (cu->header.version == 2)
4568ecf9 17599 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 17600 else
4568ecf9
DE
17601 DW_UNSND (attr) = read_offset (abfd, info_ptr,
17602 &cu->header, &bytes_read);
ae411497
TT
17603 info_ptr += bytes_read;
17604 break;
36586728
TT
17605 case DW_FORM_GNU_ref_alt:
17606 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
17607 info_ptr += bytes_read;
17608 break;
ae411497 17609 case DW_FORM_addr:
e7c27a73 17610 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 17611 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 17612 info_ptr += bytes_read;
c906108c
SS
17613 break;
17614 case DW_FORM_block2:
7b5a2f43 17615 blk = dwarf_alloc_block (cu);
c906108c
SS
17616 blk->size = read_2_bytes (abfd, info_ptr);
17617 info_ptr += 2;
17618 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17619 info_ptr += blk->size;
17620 DW_BLOCK (attr) = blk;
17621 break;
17622 case DW_FORM_block4:
7b5a2f43 17623 blk = dwarf_alloc_block (cu);
c906108c
SS
17624 blk->size = read_4_bytes (abfd, info_ptr);
17625 info_ptr += 4;
17626 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17627 info_ptr += blk->size;
17628 DW_BLOCK (attr) = blk;
17629 break;
17630 case DW_FORM_data2:
17631 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
17632 info_ptr += 2;
17633 break;
17634 case DW_FORM_data4:
17635 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
17636 info_ptr += 4;
17637 break;
17638 case DW_FORM_data8:
17639 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
17640 info_ptr += 8;
17641 break;
0224619f
JK
17642 case DW_FORM_data16:
17643 blk = dwarf_alloc_block (cu);
17644 blk->size = 16;
17645 blk->data = read_n_bytes (abfd, info_ptr, 16);
17646 info_ptr += 16;
17647 DW_BLOCK (attr) = blk;
17648 break;
2dc7f7b3
TT
17649 case DW_FORM_sec_offset:
17650 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
17651 info_ptr += bytes_read;
17652 break;
c906108c 17653 case DW_FORM_string:
9b1c24c8 17654 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 17655 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
17656 info_ptr += bytes_read;
17657 break;
4bdf3d34 17658 case DW_FORM_strp:
36586728
TT
17659 if (!cu->per_cu->is_dwz)
17660 {
17661 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
17662 &bytes_read);
17663 DW_STRING_IS_CANONICAL (attr) = 0;
17664 info_ptr += bytes_read;
17665 break;
17666 }
17667 /* FALLTHROUGH */
43988095
JK
17668 case DW_FORM_line_strp:
17669 if (!cu->per_cu->is_dwz)
17670 {
17671 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
17672 cu_header, &bytes_read);
17673 DW_STRING_IS_CANONICAL (attr) = 0;
17674 info_ptr += bytes_read;
17675 break;
17676 }
17677 /* FALLTHROUGH */
36586728
TT
17678 case DW_FORM_GNU_strp_alt:
17679 {
17680 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17681 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
17682 &bytes_read);
17683
17684 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
17685 DW_STRING_IS_CANONICAL (attr) = 0;
17686 info_ptr += bytes_read;
17687 }
4bdf3d34 17688 break;
2dc7f7b3 17689 case DW_FORM_exprloc:
c906108c 17690 case DW_FORM_block:
7b5a2f43 17691 blk = dwarf_alloc_block (cu);
c906108c
SS
17692 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17693 info_ptr += bytes_read;
17694 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17695 info_ptr += blk->size;
17696 DW_BLOCK (attr) = blk;
17697 break;
17698 case DW_FORM_block1:
7b5a2f43 17699 blk = dwarf_alloc_block (cu);
c906108c
SS
17700 blk->size = read_1_byte (abfd, info_ptr);
17701 info_ptr += 1;
17702 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17703 info_ptr += blk->size;
17704 DW_BLOCK (attr) = blk;
17705 break;
17706 case DW_FORM_data1:
17707 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
17708 info_ptr += 1;
17709 break;
17710 case DW_FORM_flag:
17711 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
17712 info_ptr += 1;
17713 break;
2dc7f7b3
TT
17714 case DW_FORM_flag_present:
17715 DW_UNSND (attr) = 1;
17716 break;
c906108c
SS
17717 case DW_FORM_sdata:
17718 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17719 info_ptr += bytes_read;
17720 break;
17721 case DW_FORM_udata:
17722 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17723 info_ptr += bytes_read;
17724 break;
17725 case DW_FORM_ref1:
9c541725 17726 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17727 + read_1_byte (abfd, info_ptr));
c906108c
SS
17728 info_ptr += 1;
17729 break;
17730 case DW_FORM_ref2:
9c541725 17731 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17732 + read_2_bytes (abfd, info_ptr));
c906108c
SS
17733 info_ptr += 2;
17734 break;
17735 case DW_FORM_ref4:
9c541725 17736 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17737 + read_4_bytes (abfd, info_ptr));
c906108c
SS
17738 info_ptr += 4;
17739 break;
613e1657 17740 case DW_FORM_ref8:
9c541725 17741 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17742 + read_8_bytes (abfd, info_ptr));
613e1657
KB
17743 info_ptr += 8;
17744 break;
55f1336d 17745 case DW_FORM_ref_sig8:
ac9ec31b 17746 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
17747 info_ptr += 8;
17748 break;
c906108c 17749 case DW_FORM_ref_udata:
9c541725 17750 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17751 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
17752 info_ptr += bytes_read;
17753 break;
c906108c 17754 case DW_FORM_indirect:
a8329558
KW
17755 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17756 info_ptr += bytes_read;
43988095
JK
17757 if (form == DW_FORM_implicit_const)
17758 {
17759 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17760 info_ptr += bytes_read;
17761 }
17762 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
17763 info_ptr);
17764 break;
17765 case DW_FORM_implicit_const:
17766 DW_SND (attr) = implicit_const;
a8329558 17767 break;
3019eac3
DE
17768 case DW_FORM_GNU_addr_index:
17769 if (reader->dwo_file == NULL)
17770 {
17771 /* For now flag a hard error.
17772 Later we can turn this into a complaint. */
17773 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
17774 dwarf_form_name (form),
17775 bfd_get_filename (abfd));
17776 }
17777 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
17778 info_ptr += bytes_read;
17779 break;
17780 case DW_FORM_GNU_str_index:
17781 if (reader->dwo_file == NULL)
17782 {
17783 /* For now flag a hard error.
17784 Later we can turn this into a complaint if warranted. */
17785 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
17786 dwarf_form_name (form),
17787 bfd_get_filename (abfd));
17788 }
17789 {
17790 ULONGEST str_index =
17791 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17792
342587c4 17793 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
17794 DW_STRING_IS_CANONICAL (attr) = 0;
17795 info_ptr += bytes_read;
17796 }
17797 break;
c906108c 17798 default:
8a3fe4f8 17799 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
17800 dwarf_form_name (form),
17801 bfd_get_filename (abfd));
c906108c 17802 }
28e94949 17803
36586728 17804 /* Super hack. */
7771576e 17805 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
17806 attr->form = DW_FORM_GNU_ref_alt;
17807
28e94949
JB
17808 /* We have seen instances where the compiler tried to emit a byte
17809 size attribute of -1 which ended up being encoded as an unsigned
17810 0xffffffff. Although 0xffffffff is technically a valid size value,
17811 an object of this size seems pretty unlikely so we can relatively
17812 safely treat these cases as if the size attribute was invalid and
17813 treat them as zero by default. */
17814 if (attr->name == DW_AT_byte_size
17815 && form == DW_FORM_data4
17816 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
17817 {
17818 complaint
17819 (&symfile_complaints,
43bbcdc2
PH
17820 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
17821 hex_string (DW_UNSND (attr)));
01c66ae6
JB
17822 DW_UNSND (attr) = 0;
17823 }
28e94949 17824
c906108c
SS
17825 return info_ptr;
17826}
17827
a8329558
KW
17828/* Read an attribute described by an abbreviated attribute. */
17829
d521ce57 17830static const gdb_byte *
dee91e82
DE
17831read_attribute (const struct die_reader_specs *reader,
17832 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 17833 const gdb_byte *info_ptr)
a8329558
KW
17834{
17835 attr->name = abbrev->name;
43988095
JK
17836 return read_attribute_value (reader, attr, abbrev->form,
17837 abbrev->implicit_const, info_ptr);
a8329558
KW
17838}
17839
0963b4bd 17840/* Read dwarf information from a buffer. */
c906108c
SS
17841
17842static unsigned int
a1855c1d 17843read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 17844{
fe1b8b76 17845 return bfd_get_8 (abfd, buf);
c906108c
SS
17846}
17847
17848static int
a1855c1d 17849read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 17850{
fe1b8b76 17851 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
17852}
17853
17854static unsigned int
a1855c1d 17855read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17856{
fe1b8b76 17857 return bfd_get_16 (abfd, buf);
c906108c
SS
17858}
17859
21ae7a4d 17860static int
a1855c1d 17861read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
17862{
17863 return bfd_get_signed_16 (abfd, buf);
17864}
17865
c906108c 17866static unsigned int
a1855c1d 17867read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17868{
fe1b8b76 17869 return bfd_get_32 (abfd, buf);
c906108c
SS
17870}
17871
21ae7a4d 17872static int
a1855c1d 17873read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
17874{
17875 return bfd_get_signed_32 (abfd, buf);
17876}
17877
93311388 17878static ULONGEST
a1855c1d 17879read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17880{
fe1b8b76 17881 return bfd_get_64 (abfd, buf);
c906108c
SS
17882}
17883
17884static CORE_ADDR
d521ce57 17885read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 17886 unsigned int *bytes_read)
c906108c 17887{
e7c27a73 17888 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
17889 CORE_ADDR retval = 0;
17890
107d2387 17891 if (cu_header->signed_addr_p)
c906108c 17892 {
107d2387
AC
17893 switch (cu_header->addr_size)
17894 {
17895 case 2:
fe1b8b76 17896 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
17897 break;
17898 case 4:
fe1b8b76 17899 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
17900 break;
17901 case 8:
fe1b8b76 17902 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
17903 break;
17904 default:
8e65ff28 17905 internal_error (__FILE__, __LINE__,
e2e0b3e5 17906 _("read_address: bad switch, signed [in module %s]"),
659b0389 17907 bfd_get_filename (abfd));
107d2387
AC
17908 }
17909 }
17910 else
17911 {
17912 switch (cu_header->addr_size)
17913 {
17914 case 2:
fe1b8b76 17915 retval = bfd_get_16 (abfd, buf);
107d2387
AC
17916 break;
17917 case 4:
fe1b8b76 17918 retval = bfd_get_32 (abfd, buf);
107d2387
AC
17919 break;
17920 case 8:
fe1b8b76 17921 retval = bfd_get_64 (abfd, buf);
107d2387
AC
17922 break;
17923 default:
8e65ff28 17924 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
17925 _("read_address: bad switch, "
17926 "unsigned [in module %s]"),
659b0389 17927 bfd_get_filename (abfd));
107d2387 17928 }
c906108c 17929 }
64367e0a 17930
107d2387
AC
17931 *bytes_read = cu_header->addr_size;
17932 return retval;
c906108c
SS
17933}
17934
f7ef9339 17935/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
17936 specification allows the initial length to take up either 4 bytes
17937 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
17938 bytes describe the length and all offsets will be 8 bytes in length
17939 instead of 4.
17940
f7ef9339
KB
17941 An older, non-standard 64-bit format is also handled by this
17942 function. The older format in question stores the initial length
17943 as an 8-byte quantity without an escape value. Lengths greater
17944 than 2^32 aren't very common which means that the initial 4 bytes
17945 is almost always zero. Since a length value of zero doesn't make
17946 sense for the 32-bit format, this initial zero can be considered to
17947 be an escape value which indicates the presence of the older 64-bit
17948 format. As written, the code can't detect (old format) lengths
917c78fc
MK
17949 greater than 4GB. If it becomes necessary to handle lengths
17950 somewhat larger than 4GB, we could allow other small values (such
17951 as the non-sensical values of 1, 2, and 3) to also be used as
17952 escape values indicating the presence of the old format.
f7ef9339 17953
917c78fc
MK
17954 The value returned via bytes_read should be used to increment the
17955 relevant pointer after calling read_initial_length().
c764a876 17956
613e1657
KB
17957 [ Note: read_initial_length() and read_offset() are based on the
17958 document entitled "DWARF Debugging Information Format", revision
f7ef9339 17959 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
17960 from:
17961
f7ef9339 17962 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 17963
613e1657
KB
17964 This document is only a draft and is subject to change. (So beware.)
17965
f7ef9339 17966 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
17967 determined empirically by examining 64-bit ELF files produced by
17968 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
17969
17970 - Kevin, July 16, 2002
613e1657
KB
17971 ] */
17972
17973static LONGEST
d521ce57 17974read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 17975{
fe1b8b76 17976 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 17977
dd373385 17978 if (length == 0xffffffff)
613e1657 17979 {
fe1b8b76 17980 length = bfd_get_64 (abfd, buf + 4);
613e1657 17981 *bytes_read = 12;
613e1657 17982 }
dd373385 17983 else if (length == 0)
f7ef9339 17984 {
dd373385 17985 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 17986 length = bfd_get_64 (abfd, buf);
f7ef9339 17987 *bytes_read = 8;
f7ef9339 17988 }
613e1657
KB
17989 else
17990 {
17991 *bytes_read = 4;
613e1657
KB
17992 }
17993
c764a876
DE
17994 return length;
17995}
dd373385 17996
c764a876
DE
17997/* Cover function for read_initial_length.
17998 Returns the length of the object at BUF, and stores the size of the
17999 initial length in *BYTES_READ and stores the size that offsets will be in
18000 *OFFSET_SIZE.
18001 If the initial length size is not equivalent to that specified in
18002 CU_HEADER then issue a complaint.
18003 This is useful when reading non-comp-unit headers. */
dd373385 18004
c764a876 18005static LONGEST
d521ce57 18006read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
18007 const struct comp_unit_head *cu_header,
18008 unsigned int *bytes_read,
18009 unsigned int *offset_size)
18010{
18011 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18012
18013 gdb_assert (cu_header->initial_length_size == 4
18014 || cu_header->initial_length_size == 8
18015 || cu_header->initial_length_size == 12);
18016
18017 if (cu_header->initial_length_size != *bytes_read)
18018 complaint (&symfile_complaints,
18019 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 18020
c764a876 18021 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 18022 return length;
613e1657
KB
18023}
18024
18025/* Read an offset from the data stream. The size of the offset is
917c78fc 18026 given by cu_header->offset_size. */
613e1657
KB
18027
18028static LONGEST
d521ce57
TT
18029read_offset (bfd *abfd, const gdb_byte *buf,
18030 const struct comp_unit_head *cu_header,
891d2f0b 18031 unsigned int *bytes_read)
c764a876
DE
18032{
18033 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 18034
c764a876
DE
18035 *bytes_read = cu_header->offset_size;
18036 return offset;
18037}
18038
18039/* Read an offset from the data stream. */
18040
18041static LONGEST
d521ce57 18042read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
18043{
18044 LONGEST retval = 0;
18045
c764a876 18046 switch (offset_size)
613e1657
KB
18047 {
18048 case 4:
fe1b8b76 18049 retval = bfd_get_32 (abfd, buf);
613e1657
KB
18050 break;
18051 case 8:
fe1b8b76 18052 retval = bfd_get_64 (abfd, buf);
613e1657
KB
18053 break;
18054 default:
8e65ff28 18055 internal_error (__FILE__, __LINE__,
c764a876 18056 _("read_offset_1: bad switch [in module %s]"),
659b0389 18057 bfd_get_filename (abfd));
613e1657
KB
18058 }
18059
917c78fc 18060 return retval;
613e1657
KB
18061}
18062
d521ce57
TT
18063static const gdb_byte *
18064read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
18065{
18066 /* If the size of a host char is 8 bits, we can return a pointer
18067 to the buffer, otherwise we have to copy the data to a buffer
18068 allocated on the temporary obstack. */
4bdf3d34 18069 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 18070 return buf;
c906108c
SS
18071}
18072
d521ce57
TT
18073static const char *
18074read_direct_string (bfd *abfd, const gdb_byte *buf,
18075 unsigned int *bytes_read_ptr)
c906108c
SS
18076{
18077 /* If the size of a host char is 8 bits, we can return a pointer
18078 to the string, otherwise we have to copy the string to a buffer
18079 allocated on the temporary obstack. */
4bdf3d34 18080 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
18081 if (*buf == '\0')
18082 {
18083 *bytes_read_ptr = 1;
18084 return NULL;
18085 }
d521ce57
TT
18086 *bytes_read_ptr = strlen ((const char *) buf) + 1;
18087 return (const char *) buf;
4bdf3d34
JJ
18088}
18089
43988095
JK
18090/* Return pointer to string at section SECT offset STR_OFFSET with error
18091 reporting strings FORM_NAME and SECT_NAME. */
18092
d521ce57 18093static const char *
43988095
JK
18094read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
18095 struct dwarf2_section_info *sect,
18096 const char *form_name,
18097 const char *sect_name)
18098{
18099 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
18100 if (sect->buffer == NULL)
18101 error (_("%s used without %s section [in module %s]"),
18102 form_name, sect_name, bfd_get_filename (abfd));
18103 if (str_offset >= sect->size)
18104 error (_("%s pointing outside of %s section [in module %s]"),
18105 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 18106 gdb_assert (HOST_CHAR_BIT == 8);
43988095 18107 if (sect->buffer[str_offset] == '\0')
4bdf3d34 18108 return NULL;
43988095
JK
18109 return (const char *) (sect->buffer + str_offset);
18110}
18111
18112/* Return pointer to string at .debug_str offset STR_OFFSET. */
18113
18114static const char *
18115read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
18116{
18117 return read_indirect_string_at_offset_from (abfd, str_offset,
18118 &dwarf2_per_objfile->str,
18119 "DW_FORM_strp", ".debug_str");
18120}
18121
18122/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18123
18124static const char *
18125read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
18126{
18127 return read_indirect_string_at_offset_from (abfd, str_offset,
18128 &dwarf2_per_objfile->line_str,
18129 "DW_FORM_line_strp",
18130 ".debug_line_str");
c906108c
SS
18131}
18132
36586728
TT
18133/* Read a string at offset STR_OFFSET in the .debug_str section from
18134 the .dwz file DWZ. Throw an error if the offset is too large. If
18135 the string consists of a single NUL byte, return NULL; otherwise
18136 return a pointer to the string. */
18137
d521ce57 18138static const char *
36586728
TT
18139read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
18140{
18141 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
18142
18143 if (dwz->str.buffer == NULL)
18144 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18145 "section [in module %s]"),
18146 bfd_get_filename (dwz->dwz_bfd));
18147 if (str_offset >= dwz->str.size)
18148 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18149 ".debug_str section [in module %s]"),
18150 bfd_get_filename (dwz->dwz_bfd));
18151 gdb_assert (HOST_CHAR_BIT == 8);
18152 if (dwz->str.buffer[str_offset] == '\0')
18153 return NULL;
d521ce57 18154 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
18155}
18156
43988095
JK
18157/* Return pointer to string at .debug_str offset as read from BUF.
18158 BUF is assumed to be in a compilation unit described by CU_HEADER.
18159 Return *BYTES_READ_PTR count of bytes read from BUF. */
18160
d521ce57
TT
18161static const char *
18162read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
18163 const struct comp_unit_head *cu_header,
18164 unsigned int *bytes_read_ptr)
18165{
18166 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
18167
18168 return read_indirect_string_at_offset (abfd, str_offset);
18169}
18170
43988095
JK
18171/* Return pointer to string at .debug_line_str offset as read from BUF.
18172 BUF is assumed to be in a compilation unit described by CU_HEADER.
18173 Return *BYTES_READ_PTR count of bytes read from BUF. */
18174
18175static const char *
18176read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
18177 const struct comp_unit_head *cu_header,
18178 unsigned int *bytes_read_ptr)
18179{
18180 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
18181
18182 return read_indirect_line_string_at_offset (abfd, str_offset);
18183}
18184
18185ULONGEST
d521ce57 18186read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 18187 unsigned int *bytes_read_ptr)
c906108c 18188{
12df843f 18189 ULONGEST result;
ce5d95e1 18190 unsigned int num_read;
870f88f7 18191 int shift;
c906108c
SS
18192 unsigned char byte;
18193
18194 result = 0;
18195 shift = 0;
18196 num_read = 0;
c906108c
SS
18197 while (1)
18198 {
fe1b8b76 18199 byte = bfd_get_8 (abfd, buf);
c906108c
SS
18200 buf++;
18201 num_read++;
12df843f 18202 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
18203 if ((byte & 128) == 0)
18204 {
18205 break;
18206 }
18207 shift += 7;
18208 }
18209 *bytes_read_ptr = num_read;
18210 return result;
18211}
18212
12df843f 18213static LONGEST
d521ce57
TT
18214read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
18215 unsigned int *bytes_read_ptr)
c906108c 18216{
12df843f 18217 LONGEST result;
870f88f7 18218 int shift, num_read;
c906108c
SS
18219 unsigned char byte;
18220
18221 result = 0;
18222 shift = 0;
c906108c 18223 num_read = 0;
c906108c
SS
18224 while (1)
18225 {
fe1b8b76 18226 byte = bfd_get_8 (abfd, buf);
c906108c
SS
18227 buf++;
18228 num_read++;
12df843f 18229 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
18230 shift += 7;
18231 if ((byte & 128) == 0)
18232 {
18233 break;
18234 }
18235 }
77e0b926 18236 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 18237 result |= -(((LONGEST) 1) << shift);
c906108c
SS
18238 *bytes_read_ptr = num_read;
18239 return result;
18240}
18241
3019eac3
DE
18242/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18243 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
18244 ADDR_SIZE is the size of addresses from the CU header. */
18245
18246static CORE_ADDR
18247read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
18248{
18249 struct objfile *objfile = dwarf2_per_objfile->objfile;
18250 bfd *abfd = objfile->obfd;
18251 const gdb_byte *info_ptr;
18252
18253 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
18254 if (dwarf2_per_objfile->addr.buffer == NULL)
18255 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 18256 objfile_name (objfile));
3019eac3
DE
18257 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
18258 error (_("DW_FORM_addr_index pointing outside of "
18259 ".debug_addr section [in module %s]"),
4262abfb 18260 objfile_name (objfile));
3019eac3
DE
18261 info_ptr = (dwarf2_per_objfile->addr.buffer
18262 + addr_base + addr_index * addr_size);
18263 if (addr_size == 4)
18264 return bfd_get_32 (abfd, info_ptr);
18265 else
18266 return bfd_get_64 (abfd, info_ptr);
18267}
18268
18269/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18270
18271static CORE_ADDR
18272read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18273{
18274 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
18275}
18276
18277/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18278
18279static CORE_ADDR
d521ce57 18280read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
18281 unsigned int *bytes_read)
18282{
18283 bfd *abfd = cu->objfile->obfd;
18284 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18285
18286 return read_addr_index (cu, addr_index);
18287}
18288
18289/* Data structure to pass results from dwarf2_read_addr_index_reader
18290 back to dwarf2_read_addr_index. */
18291
18292struct dwarf2_read_addr_index_data
18293{
18294 ULONGEST addr_base;
18295 int addr_size;
18296};
18297
18298/* die_reader_func for dwarf2_read_addr_index. */
18299
18300static void
18301dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 18302 const gdb_byte *info_ptr,
3019eac3
DE
18303 struct die_info *comp_unit_die,
18304 int has_children,
18305 void *data)
18306{
18307 struct dwarf2_cu *cu = reader->cu;
18308 struct dwarf2_read_addr_index_data *aidata =
18309 (struct dwarf2_read_addr_index_data *) data;
18310
18311 aidata->addr_base = cu->addr_base;
18312 aidata->addr_size = cu->header.addr_size;
18313}
18314
18315/* Given an index in .debug_addr, fetch the value.
18316 NOTE: This can be called during dwarf expression evaluation,
18317 long after the debug information has been read, and thus per_cu->cu
18318 may no longer exist. */
18319
18320CORE_ADDR
18321dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
18322 unsigned int addr_index)
18323{
18324 struct objfile *objfile = per_cu->objfile;
18325 struct dwarf2_cu *cu = per_cu->cu;
18326 ULONGEST addr_base;
18327 int addr_size;
18328
18329 /* This is intended to be called from outside this file. */
18330 dw2_setup (objfile);
18331
18332 /* We need addr_base and addr_size.
18333 If we don't have PER_CU->cu, we have to get it.
18334 Nasty, but the alternative is storing the needed info in PER_CU,
18335 which at this point doesn't seem justified: it's not clear how frequently
18336 it would get used and it would increase the size of every PER_CU.
18337 Entry points like dwarf2_per_cu_addr_size do a similar thing
18338 so we're not in uncharted territory here.
18339 Alas we need to be a bit more complicated as addr_base is contained
18340 in the DIE.
18341
18342 We don't need to read the entire CU(/TU).
18343 We just need the header and top level die.
a1b64ce1 18344
3019eac3 18345 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 18346 For now we skip this optimization. */
3019eac3
DE
18347
18348 if (cu != NULL)
18349 {
18350 addr_base = cu->addr_base;
18351 addr_size = cu->header.addr_size;
18352 }
18353 else
18354 {
18355 struct dwarf2_read_addr_index_data aidata;
18356
a1b64ce1
DE
18357 /* Note: We can't use init_cutu_and_read_dies_simple here,
18358 we need addr_base. */
18359 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
18360 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
18361 addr_base = aidata.addr_base;
18362 addr_size = aidata.addr_size;
18363 }
18364
18365 return read_addr_index_1 (addr_index, addr_base, addr_size);
18366}
18367
57d63ce2
DE
18368/* Given a DW_FORM_GNU_str_index, fetch the string.
18369 This is only used by the Fission support. */
3019eac3 18370
d521ce57 18371static const char *
342587c4 18372read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
18373{
18374 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 18375 const char *objf_name = objfile_name (objfile);
3019eac3 18376 bfd *abfd = objfile->obfd;
342587c4 18377 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
18378 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
18379 struct dwarf2_section_info *str_offsets_section =
18380 &reader->dwo_file->sections.str_offsets;
d521ce57 18381 const gdb_byte *info_ptr;
3019eac3 18382 ULONGEST str_offset;
57d63ce2 18383 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 18384
73869dc2
DE
18385 dwarf2_read_section (objfile, str_section);
18386 dwarf2_read_section (objfile, str_offsets_section);
18387 if (str_section->buffer == NULL)
57d63ce2 18388 error (_("%s used without .debug_str.dwo section"
9c541725
PA
18389 " in CU at offset 0x%x [in module %s]"),
18390 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 18391 if (str_offsets_section->buffer == NULL)
57d63ce2 18392 error (_("%s used without .debug_str_offsets.dwo section"
9c541725
PA
18393 " in CU at offset 0x%x [in module %s]"),
18394 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 18395 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 18396 error (_("%s pointing outside of .debug_str_offsets.dwo"
9c541725
PA
18397 " section in CU at offset 0x%x [in module %s]"),
18398 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 18399 info_ptr = (str_offsets_section->buffer
3019eac3
DE
18400 + str_index * cu->header.offset_size);
18401 if (cu->header.offset_size == 4)
18402 str_offset = bfd_get_32 (abfd, info_ptr);
18403 else
18404 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 18405 if (str_offset >= str_section->size)
57d63ce2 18406 error (_("Offset from %s pointing outside of"
9c541725
PA
18407 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
18408 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 18409 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
18410}
18411
3019eac3
DE
18412/* Return the length of an LEB128 number in BUF. */
18413
18414static int
18415leb128_size (const gdb_byte *buf)
18416{
18417 const gdb_byte *begin = buf;
18418 gdb_byte byte;
18419
18420 while (1)
18421 {
18422 byte = *buf++;
18423 if ((byte & 128) == 0)
18424 return buf - begin;
18425 }
18426}
18427
c906108c 18428static void
e142c38c 18429set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
18430{
18431 switch (lang)
18432 {
18433 case DW_LANG_C89:
76bee0cc 18434 case DW_LANG_C99:
0cfd832f 18435 case DW_LANG_C11:
c906108c 18436 case DW_LANG_C:
d1be3247 18437 case DW_LANG_UPC:
e142c38c 18438 cu->language = language_c;
c906108c 18439 break;
9c37b5ae 18440 case DW_LANG_Java:
c906108c 18441 case DW_LANG_C_plus_plus:
0cfd832f
MW
18442 case DW_LANG_C_plus_plus_11:
18443 case DW_LANG_C_plus_plus_14:
e142c38c 18444 cu->language = language_cplus;
c906108c 18445 break;
6aecb9c2
JB
18446 case DW_LANG_D:
18447 cu->language = language_d;
18448 break;
c906108c
SS
18449 case DW_LANG_Fortran77:
18450 case DW_LANG_Fortran90:
b21b22e0 18451 case DW_LANG_Fortran95:
f7de9aab
MW
18452 case DW_LANG_Fortran03:
18453 case DW_LANG_Fortran08:
e142c38c 18454 cu->language = language_fortran;
c906108c 18455 break;
a766d390
DE
18456 case DW_LANG_Go:
18457 cu->language = language_go;
18458 break;
c906108c 18459 case DW_LANG_Mips_Assembler:
e142c38c 18460 cu->language = language_asm;
c906108c
SS
18461 break;
18462 case DW_LANG_Ada83:
8aaf0b47 18463 case DW_LANG_Ada95:
bc5f45f8
JB
18464 cu->language = language_ada;
18465 break;
72019c9c
GM
18466 case DW_LANG_Modula2:
18467 cu->language = language_m2;
18468 break;
fe8e67fd
PM
18469 case DW_LANG_Pascal83:
18470 cu->language = language_pascal;
18471 break;
22566fbd
DJ
18472 case DW_LANG_ObjC:
18473 cu->language = language_objc;
18474 break;
c44af4eb
TT
18475 case DW_LANG_Rust:
18476 case DW_LANG_Rust_old:
18477 cu->language = language_rust;
18478 break;
c906108c
SS
18479 case DW_LANG_Cobol74:
18480 case DW_LANG_Cobol85:
c906108c 18481 default:
e142c38c 18482 cu->language = language_minimal;
c906108c
SS
18483 break;
18484 }
e142c38c 18485 cu->language_defn = language_def (cu->language);
c906108c
SS
18486}
18487
18488/* Return the named attribute or NULL if not there. */
18489
18490static struct attribute *
e142c38c 18491dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 18492{
a48e046c 18493 for (;;)
c906108c 18494 {
a48e046c
TT
18495 unsigned int i;
18496 struct attribute *spec = NULL;
18497
18498 for (i = 0; i < die->num_attrs; ++i)
18499 {
18500 if (die->attrs[i].name == name)
18501 return &die->attrs[i];
18502 if (die->attrs[i].name == DW_AT_specification
18503 || die->attrs[i].name == DW_AT_abstract_origin)
18504 spec = &die->attrs[i];
18505 }
18506
18507 if (!spec)
18508 break;
c906108c 18509
f2f0e013 18510 die = follow_die_ref (die, spec, &cu);
f2f0e013 18511 }
c5aa993b 18512
c906108c
SS
18513 return NULL;
18514}
18515
348e048f
DE
18516/* Return the named attribute or NULL if not there,
18517 but do not follow DW_AT_specification, etc.
18518 This is for use in contexts where we're reading .debug_types dies.
18519 Following DW_AT_specification, DW_AT_abstract_origin will take us
18520 back up the chain, and we want to go down. */
18521
18522static struct attribute *
45e58e77 18523dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
18524{
18525 unsigned int i;
18526
18527 for (i = 0; i < die->num_attrs; ++i)
18528 if (die->attrs[i].name == name)
18529 return &die->attrs[i];
18530
18531 return NULL;
18532}
18533
7d45c7c3
KB
18534/* Return the string associated with a string-typed attribute, or NULL if it
18535 is either not found or is of an incorrect type. */
18536
18537static const char *
18538dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18539{
18540 struct attribute *attr;
18541 const char *str = NULL;
18542
18543 attr = dwarf2_attr (die, name, cu);
18544
18545 if (attr != NULL)
18546 {
43988095 18547 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
18548 || attr->form == DW_FORM_string
18549 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 18550 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
18551 str = DW_STRING (attr);
18552 else
18553 complaint (&symfile_complaints,
18554 _("string type expected for attribute %s for "
18555 "DIE at 0x%x in module %s"),
9c541725 18556 dwarf_attr_name (name), to_underlying (die->sect_off),
7d45c7c3
KB
18557 objfile_name (cu->objfile));
18558 }
18559
18560 return str;
18561}
18562
05cf31d1
JB
18563/* Return non-zero iff the attribute NAME is defined for the given DIE,
18564 and holds a non-zero value. This function should only be used for
2dc7f7b3 18565 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
18566
18567static int
18568dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
18569{
18570 struct attribute *attr = dwarf2_attr (die, name, cu);
18571
18572 return (attr && DW_UNSND (attr));
18573}
18574
3ca72b44 18575static int
e142c38c 18576die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 18577{
05cf31d1
JB
18578 /* A DIE is a declaration if it has a DW_AT_declaration attribute
18579 which value is non-zero. However, we have to be careful with
18580 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
18581 (via dwarf2_flag_true_p) follows this attribute. So we may
18582 end up accidently finding a declaration attribute that belongs
18583 to a different DIE referenced by the specification attribute,
18584 even though the given DIE does not have a declaration attribute. */
18585 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
18586 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
18587}
18588
63d06c5c 18589/* Return the die giving the specification for DIE, if there is
f2f0e013 18590 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
18591 containing the return value on output. If there is no
18592 specification, but there is an abstract origin, that is
18593 returned. */
63d06c5c
DC
18594
18595static struct die_info *
f2f0e013 18596die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 18597{
f2f0e013
DJ
18598 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
18599 *spec_cu);
63d06c5c 18600
edb3359d
DJ
18601 if (spec_attr == NULL)
18602 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
18603
63d06c5c
DC
18604 if (spec_attr == NULL)
18605 return NULL;
18606 else
f2f0e013 18607 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 18608}
c906108c 18609
527f3840
JK
18610/* Stub for free_line_header to match void * callback types. */
18611
18612static void
18613free_line_header_voidp (void *arg)
18614{
9a3c8263 18615 struct line_header *lh = (struct line_header *) arg;
527f3840 18616
fff8551c 18617 delete lh;
527f3840
JK
18618}
18619
fff8551c
PA
18620void
18621line_header::add_include_dir (const char *include_dir)
c906108c 18622{
27e0867f 18623 if (dwarf_line_debug >= 2)
fff8551c
PA
18624 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
18625 include_dirs.size () + 1, include_dir);
27e0867f 18626
fff8551c 18627 include_dirs.push_back (include_dir);
debd256d 18628}
6e70227d 18629
fff8551c
PA
18630void
18631line_header::add_file_name (const char *name,
ecfb656c 18632 dir_index d_index,
fff8551c
PA
18633 unsigned int mod_time,
18634 unsigned int length)
debd256d 18635{
27e0867f
DE
18636 if (dwarf_line_debug >= 2)
18637 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 18638 (unsigned) file_names.size () + 1, name);
27e0867f 18639
ecfb656c 18640 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 18641}
6e70227d 18642
83769d0b 18643/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
18644
18645static struct dwarf2_section_info *
18646get_debug_line_section (struct dwarf2_cu *cu)
18647{
18648 struct dwarf2_section_info *section;
18649
18650 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
18651 DWO file. */
18652 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18653 section = &cu->dwo_unit->dwo_file->sections.line;
18654 else if (cu->per_cu->is_dwz)
18655 {
18656 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18657
18658 section = &dwz->line;
18659 }
18660 else
18661 section = &dwarf2_per_objfile->line;
18662
18663 return section;
18664}
18665
43988095
JK
18666/* Read directory or file name entry format, starting with byte of
18667 format count entries, ULEB128 pairs of entry formats, ULEB128 of
18668 entries count and the entries themselves in the described entry
18669 format. */
18670
18671static void
18672read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
18673 struct line_header *lh,
18674 const struct comp_unit_head *cu_header,
18675 void (*callback) (struct line_header *lh,
18676 const char *name,
ecfb656c 18677 dir_index d_index,
43988095
JK
18678 unsigned int mod_time,
18679 unsigned int length))
18680{
18681 gdb_byte format_count, formati;
18682 ULONGEST data_count, datai;
18683 const gdb_byte *buf = *bufp;
18684 const gdb_byte *format_header_data;
18685 int i;
18686 unsigned int bytes_read;
18687
18688 format_count = read_1_byte (abfd, buf);
18689 buf += 1;
18690 format_header_data = buf;
18691 for (formati = 0; formati < format_count; formati++)
18692 {
18693 read_unsigned_leb128 (abfd, buf, &bytes_read);
18694 buf += bytes_read;
18695 read_unsigned_leb128 (abfd, buf, &bytes_read);
18696 buf += bytes_read;
18697 }
18698
18699 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
18700 buf += bytes_read;
18701 for (datai = 0; datai < data_count; datai++)
18702 {
18703 const gdb_byte *format = format_header_data;
18704 struct file_entry fe;
18705
43988095
JK
18706 for (formati = 0; formati < format_count; formati++)
18707 {
ecfb656c 18708 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 18709 format += bytes_read;
43988095 18710
ecfb656c 18711 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 18712 format += bytes_read;
ecfb656c
PA
18713
18714 gdb::optional<const char *> string;
18715 gdb::optional<unsigned int> uint;
18716
43988095
JK
18717 switch (form)
18718 {
18719 case DW_FORM_string:
ecfb656c 18720 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
18721 buf += bytes_read;
18722 break;
18723
18724 case DW_FORM_line_strp:
ecfb656c
PA
18725 string.emplace (read_indirect_line_string (abfd, buf,
18726 cu_header,
18727 &bytes_read));
43988095
JK
18728 buf += bytes_read;
18729 break;
18730
18731 case DW_FORM_data1:
ecfb656c 18732 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
18733 buf += 1;
18734 break;
18735
18736 case DW_FORM_data2:
ecfb656c 18737 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
18738 buf += 2;
18739 break;
18740
18741 case DW_FORM_data4:
ecfb656c 18742 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
18743 buf += 4;
18744 break;
18745
18746 case DW_FORM_data8:
ecfb656c 18747 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
18748 buf += 8;
18749 break;
18750
18751 case DW_FORM_udata:
ecfb656c 18752 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
18753 buf += bytes_read;
18754 break;
18755
18756 case DW_FORM_block:
18757 /* It is valid only for DW_LNCT_timestamp which is ignored by
18758 current GDB. */
18759 break;
18760 }
ecfb656c
PA
18761
18762 switch (content_type)
18763 {
18764 case DW_LNCT_path:
18765 if (string.has_value ())
18766 fe.name = *string;
18767 break;
18768 case DW_LNCT_directory_index:
18769 if (uint.has_value ())
18770 fe.d_index = (dir_index) *uint;
18771 break;
18772 case DW_LNCT_timestamp:
18773 if (uint.has_value ())
18774 fe.mod_time = *uint;
18775 break;
18776 case DW_LNCT_size:
18777 if (uint.has_value ())
18778 fe.length = *uint;
18779 break;
18780 case DW_LNCT_MD5:
18781 break;
18782 default:
18783 complaint (&symfile_complaints,
18784 _("Unknown format content type %s"),
18785 pulongest (content_type));
18786 }
43988095
JK
18787 }
18788
ecfb656c 18789 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
18790 }
18791
18792 *bufp = buf;
18793}
18794
debd256d 18795/* Read the statement program header starting at OFFSET in
3019eac3 18796 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 18797 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
18798 Returns NULL if there is a problem reading the header, e.g., if it
18799 has a version we don't understand.
debd256d
JB
18800
18801 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
18802 the returned object point into the dwarf line section buffer,
18803 and must not be freed. */
ae2de4f8 18804
fff8551c 18805static line_header_up
9c541725 18806dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 18807{
d521ce57 18808 const gdb_byte *line_ptr;
c764a876 18809 unsigned int bytes_read, offset_size;
debd256d 18810 int i;
d521ce57 18811 const char *cur_dir, *cur_file;
3019eac3
DE
18812 struct dwarf2_section_info *section;
18813 bfd *abfd;
18814
36586728 18815 section = get_debug_line_section (cu);
3019eac3
DE
18816 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
18817 if (section->buffer == NULL)
debd256d 18818 {
3019eac3
DE
18819 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18820 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
18821 else
18822 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
18823 return 0;
18824 }
18825
fceca515
DE
18826 /* We can't do this until we know the section is non-empty.
18827 Only then do we know we have such a section. */
a32a8923 18828 abfd = get_section_bfd_owner (section);
fceca515 18829
a738430d
MK
18830 /* Make sure that at least there's room for the total_length field.
18831 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 18832 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 18833 {
4d3c2250 18834 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
18835 return 0;
18836 }
18837
fff8551c 18838 line_header_up lh (new line_header ());
debd256d 18839
9c541725 18840 lh->sect_off = sect_off;
527f3840
JK
18841 lh->offset_in_dwz = cu->per_cu->is_dwz;
18842
9c541725 18843 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 18844
a738430d 18845 /* Read in the header. */
6e70227d 18846 lh->total_length =
c764a876
DE
18847 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
18848 &bytes_read, &offset_size);
debd256d 18849 line_ptr += bytes_read;
3019eac3 18850 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 18851 {
4d3c2250 18852 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
18853 return 0;
18854 }
18855 lh->statement_program_end = line_ptr + lh->total_length;
18856 lh->version = read_2_bytes (abfd, line_ptr);
18857 line_ptr += 2;
43988095 18858 if (lh->version > 5)
cd366ee8
DE
18859 {
18860 /* This is a version we don't understand. The format could have
18861 changed in ways we don't handle properly so just punt. */
18862 complaint (&symfile_complaints,
18863 _("unsupported version in .debug_line section"));
18864 return NULL;
18865 }
43988095
JK
18866 if (lh->version >= 5)
18867 {
18868 gdb_byte segment_selector_size;
18869
18870 /* Skip address size. */
18871 read_1_byte (abfd, line_ptr);
18872 line_ptr += 1;
18873
18874 segment_selector_size = read_1_byte (abfd, line_ptr);
18875 line_ptr += 1;
18876 if (segment_selector_size != 0)
18877 {
18878 complaint (&symfile_complaints,
18879 _("unsupported segment selector size %u "
18880 "in .debug_line section"),
18881 segment_selector_size);
18882 return NULL;
18883 }
18884 }
c764a876
DE
18885 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
18886 line_ptr += offset_size;
debd256d
JB
18887 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
18888 line_ptr += 1;
2dc7f7b3
TT
18889 if (lh->version >= 4)
18890 {
18891 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
18892 line_ptr += 1;
18893 }
18894 else
18895 lh->maximum_ops_per_instruction = 1;
18896
18897 if (lh->maximum_ops_per_instruction == 0)
18898 {
18899 lh->maximum_ops_per_instruction = 1;
18900 complaint (&symfile_complaints,
3e43a32a
MS
18901 _("invalid maximum_ops_per_instruction "
18902 "in `.debug_line' section"));
2dc7f7b3
TT
18903 }
18904
debd256d
JB
18905 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
18906 line_ptr += 1;
18907 lh->line_base = read_1_signed_byte (abfd, line_ptr);
18908 line_ptr += 1;
18909 lh->line_range = read_1_byte (abfd, line_ptr);
18910 line_ptr += 1;
18911 lh->opcode_base = read_1_byte (abfd, line_ptr);
18912 line_ptr += 1;
fff8551c 18913 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
18914
18915 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
18916 for (i = 1; i < lh->opcode_base; ++i)
18917 {
18918 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
18919 line_ptr += 1;
18920 }
18921
43988095 18922 if (lh->version >= 5)
debd256d 18923 {
43988095 18924 /* Read directory table. */
fff8551c
PA
18925 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18926 [] (struct line_header *lh, const char *name,
ecfb656c 18927 dir_index d_index, unsigned int mod_time,
fff8551c
PA
18928 unsigned int length)
18929 {
18930 lh->add_include_dir (name);
18931 });
debd256d 18932
43988095 18933 /* Read file name table. */
fff8551c
PA
18934 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18935 [] (struct line_header *lh, const char *name,
ecfb656c 18936 dir_index d_index, unsigned int mod_time,
fff8551c
PA
18937 unsigned int length)
18938 {
ecfb656c 18939 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 18940 });
43988095
JK
18941 }
18942 else
debd256d 18943 {
43988095
JK
18944 /* Read directory table. */
18945 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18946 {
18947 line_ptr += bytes_read;
fff8551c 18948 lh->add_include_dir (cur_dir);
43988095 18949 }
debd256d
JB
18950 line_ptr += bytes_read;
18951
43988095
JK
18952 /* Read file name table. */
18953 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18954 {
ecfb656c
PA
18955 unsigned int mod_time, length;
18956 dir_index d_index;
43988095
JK
18957
18958 line_ptr += bytes_read;
ecfb656c 18959 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
18960 line_ptr += bytes_read;
18961 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18962 line_ptr += bytes_read;
18963 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18964 line_ptr += bytes_read;
18965
ecfb656c 18966 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
18967 }
18968 line_ptr += bytes_read;
debd256d 18969 }
6e70227d 18970 lh->statement_program_start = line_ptr;
debd256d 18971
3019eac3 18972 if (line_ptr > (section->buffer + section->size))
4d3c2250 18973 complaint (&symfile_complaints,
3e43a32a
MS
18974 _("line number info header doesn't "
18975 "fit in `.debug_line' section"));
debd256d 18976
debd256d
JB
18977 return lh;
18978}
c906108c 18979
c6da4cef
DE
18980/* Subroutine of dwarf_decode_lines to simplify it.
18981 Return the file name of the psymtab for included file FILE_INDEX
18982 in line header LH of PST.
18983 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18984 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
18985 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
18986
18987 The function creates dangling cleanup registration. */
c6da4cef 18988
d521ce57 18989static const char *
c6da4cef
DE
18990psymtab_include_file_name (const struct line_header *lh, int file_index,
18991 const struct partial_symtab *pst,
18992 const char *comp_dir)
18993{
8c43009f 18994 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
18995 const char *include_name = fe.name;
18996 const char *include_name_to_compare = include_name;
72b9f47f
TT
18997 const char *pst_filename;
18998 char *copied_name = NULL;
c6da4cef
DE
18999 int file_is_pst;
19000
8c43009f 19001 const char *dir_name = fe.include_dir (lh);
c6da4cef
DE
19002
19003 if (!IS_ABSOLUTE_PATH (include_name)
19004 && (dir_name != NULL || comp_dir != NULL))
19005 {
19006 /* Avoid creating a duplicate psymtab for PST.
19007 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19008 Before we do the comparison, however, we need to account
19009 for DIR_NAME and COMP_DIR.
19010 First prepend dir_name (if non-NULL). If we still don't
19011 have an absolute path prepend comp_dir (if non-NULL).
19012 However, the directory we record in the include-file's
19013 psymtab does not contain COMP_DIR (to match the
19014 corresponding symtab(s)).
19015
19016 Example:
19017
19018 bash$ cd /tmp
19019 bash$ gcc -g ./hello.c
19020 include_name = "hello.c"
19021 dir_name = "."
19022 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19023 DW_AT_name = "./hello.c"
19024
19025 */
c6da4cef
DE
19026
19027 if (dir_name != NULL)
19028 {
d521ce57
TT
19029 char *tem = concat (dir_name, SLASH_STRING,
19030 include_name, (char *)NULL);
19031
19032 make_cleanup (xfree, tem);
19033 include_name = tem;
c6da4cef 19034 include_name_to_compare = include_name;
c6da4cef
DE
19035 }
19036 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19037 {
d521ce57
TT
19038 char *tem = concat (comp_dir, SLASH_STRING,
19039 include_name, (char *)NULL);
19040
19041 make_cleanup (xfree, tem);
19042 include_name_to_compare = tem;
c6da4cef
DE
19043 }
19044 }
19045
19046 pst_filename = pst->filename;
19047 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19048 {
72b9f47f
TT
19049 copied_name = concat (pst->dirname, SLASH_STRING,
19050 pst_filename, (char *)NULL);
19051 pst_filename = copied_name;
c6da4cef
DE
19052 }
19053
1e3fad37 19054 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19055
72b9f47f
TT
19056 if (copied_name != NULL)
19057 xfree (copied_name);
c6da4cef
DE
19058
19059 if (file_is_pst)
19060 return NULL;
19061 return include_name;
19062}
19063
d9b3de22
DE
19064/* State machine to track the state of the line number program. */
19065
6f77053d 19066class lnp_state_machine
d9b3de22 19067{
6f77053d
PA
19068public:
19069 /* Initialize a machine state for the start of a line number
19070 program. */
19071 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
19072
8c43009f
PA
19073 file_entry *current_file ()
19074 {
19075 /* lh->file_names is 0-based, but the file name numbers in the
19076 statement program are 1-based. */
6f77053d
PA
19077 return m_line_header->file_name_at (m_file);
19078 }
19079
19080 /* Record the line in the state machine. END_SEQUENCE is true if
19081 we're processing the end of a sequence. */
19082 void record_line (bool end_sequence);
19083
19084 /* Check address and if invalid nop-out the rest of the lines in this
19085 sequence. */
19086 void check_line_address (struct dwarf2_cu *cu,
19087 const gdb_byte *line_ptr,
19088 CORE_ADDR lowpc, CORE_ADDR address);
19089
19090 void handle_set_discriminator (unsigned int discriminator)
19091 {
19092 m_discriminator = discriminator;
19093 m_line_has_non_zero_discriminator |= discriminator != 0;
19094 }
19095
19096 /* Handle DW_LNE_set_address. */
19097 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19098 {
19099 m_op_index = 0;
19100 address += baseaddr;
19101 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19102 }
19103
19104 /* Handle DW_LNS_advance_pc. */
19105 void handle_advance_pc (CORE_ADDR adjust);
19106
19107 /* Handle a special opcode. */
19108 void handle_special_opcode (unsigned char op_code);
19109
19110 /* Handle DW_LNS_advance_line. */
19111 void handle_advance_line (int line_delta)
19112 {
19113 advance_line (line_delta);
19114 }
19115
19116 /* Handle DW_LNS_set_file. */
19117 void handle_set_file (file_name_index file);
19118
19119 /* Handle DW_LNS_negate_stmt. */
19120 void handle_negate_stmt ()
19121 {
19122 m_is_stmt = !m_is_stmt;
19123 }
19124
19125 /* Handle DW_LNS_const_add_pc. */
19126 void handle_const_add_pc ();
19127
19128 /* Handle DW_LNS_fixed_advance_pc. */
19129 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19130 {
19131 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19132 m_op_index = 0;
19133 }
19134
19135 /* Handle DW_LNS_copy. */
19136 void handle_copy ()
19137 {
19138 record_line (false);
19139 m_discriminator = 0;
19140 }
19141
19142 /* Handle DW_LNE_end_sequence. */
19143 void handle_end_sequence ()
19144 {
19145 m_record_line_callback = ::record_line;
19146 }
19147
19148private:
19149 /* Advance the line by LINE_DELTA. */
19150 void advance_line (int line_delta)
19151 {
19152 m_line += line_delta;
19153
19154 if (line_delta != 0)
19155 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19156 }
19157
6f77053d
PA
19158 gdbarch *m_gdbarch;
19159
19160 /* True if we're recording lines.
19161 Otherwise we're building partial symtabs and are just interested in
19162 finding include files mentioned by the line number program. */
19163 bool m_record_lines_p;
19164
8c43009f 19165 /* The line number header. */
6f77053d 19166 line_header *m_line_header;
8c43009f 19167
6f77053d
PA
19168 /* These are part of the standard DWARF line number state machine,
19169 and initialized according to the DWARF spec. */
d9b3de22 19170
6f77053d 19171 unsigned char m_op_index = 0;
8c43009f 19172 /* The line table index (1-based) of the current file. */
6f77053d
PA
19173 file_name_index m_file = (file_name_index) 1;
19174 unsigned int m_line = 1;
19175
19176 /* These are initialized in the constructor. */
19177
19178 CORE_ADDR m_address;
19179 bool m_is_stmt;
19180 unsigned int m_discriminator;
d9b3de22
DE
19181
19182 /* Additional bits of state we need to track. */
19183
19184 /* The last file that we called dwarf2_start_subfile for.
19185 This is only used for TLLs. */
6f77053d 19186 unsigned int m_last_file = 0;
d9b3de22 19187 /* The last file a line number was recorded for. */
6f77053d 19188 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
19189
19190 /* The function to call to record a line. */
6f77053d 19191 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
19192
19193 /* The last line number that was recorded, used to coalesce
19194 consecutive entries for the same line. This can happen, for
19195 example, when discriminators are present. PR 17276. */
6f77053d
PA
19196 unsigned int m_last_line = 0;
19197 bool m_line_has_non_zero_discriminator = false;
8c43009f 19198};
d9b3de22 19199
6f77053d
PA
19200void
19201lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19202{
19203 CORE_ADDR addr_adj = (((m_op_index + adjust)
19204 / m_line_header->maximum_ops_per_instruction)
19205 * m_line_header->minimum_instruction_length);
19206 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19207 m_op_index = ((m_op_index + adjust)
19208 % m_line_header->maximum_ops_per_instruction);
19209}
d9b3de22 19210
6f77053d
PA
19211void
19212lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19213{
6f77053d
PA
19214 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19215 CORE_ADDR addr_adj = (((m_op_index
19216 + (adj_opcode / m_line_header->line_range))
19217 / m_line_header->maximum_ops_per_instruction)
19218 * m_line_header->minimum_instruction_length);
19219 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19220 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
19221 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19222
6f77053d
PA
19223 int line_delta = (m_line_header->line_base
19224 + (adj_opcode % m_line_header->line_range));
19225 advance_line (line_delta);
19226 record_line (false);
19227 m_discriminator = 0;
19228}
d9b3de22 19229
6f77053d
PA
19230void
19231lnp_state_machine::handle_set_file (file_name_index file)
19232{
19233 m_file = file;
19234
19235 const file_entry *fe = current_file ();
19236 if (fe == NULL)
19237 dwarf2_debug_line_missing_file_complaint ();
19238 else if (m_record_lines_p)
19239 {
19240 const char *dir = fe->include_dir (m_line_header);
19241
19242 m_last_subfile = current_subfile;
19243 m_line_has_non_zero_discriminator = m_discriminator != 0;
19244 dwarf2_start_subfile (fe->name, dir);
19245 }
19246}
19247
19248void
19249lnp_state_machine::handle_const_add_pc ()
19250{
19251 CORE_ADDR adjust
19252 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19253
19254 CORE_ADDR addr_adj
19255 = (((m_op_index + adjust)
19256 / m_line_header->maximum_ops_per_instruction)
19257 * m_line_header->minimum_instruction_length);
19258
19259 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19260 m_op_index = ((m_op_index + adjust)
19261 % m_line_header->maximum_ops_per_instruction);
19262}
d9b3de22 19263
c91513d8
PP
19264/* Ignore this record_line request. */
19265
19266static void
19267noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
19268{
19269 return;
19270}
19271
a05a36a5
DE
19272/* Return non-zero if we should add LINE to the line number table.
19273 LINE is the line to add, LAST_LINE is the last line that was added,
19274 LAST_SUBFILE is the subfile for LAST_LINE.
19275 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19276 had a non-zero discriminator.
19277
19278 We have to be careful in the presence of discriminators.
19279 E.g., for this line:
19280
19281 for (i = 0; i < 100000; i++);
19282
19283 clang can emit four line number entries for that one line,
19284 each with a different discriminator.
19285 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19286
19287 However, we want gdb to coalesce all four entries into one.
19288 Otherwise the user could stepi into the middle of the line and
19289 gdb would get confused about whether the pc really was in the
19290 middle of the line.
19291
19292 Things are further complicated by the fact that two consecutive
19293 line number entries for the same line is a heuristic used by gcc
19294 to denote the end of the prologue. So we can't just discard duplicate
19295 entries, we have to be selective about it. The heuristic we use is
19296 that we only collapse consecutive entries for the same line if at least
19297 one of those entries has a non-zero discriminator. PR 17276.
19298
19299 Note: Addresses in the line number state machine can never go backwards
19300 within one sequence, thus this coalescing is ok. */
19301
19302static int
19303dwarf_record_line_p (unsigned int line, unsigned int last_line,
19304 int line_has_non_zero_discriminator,
19305 struct subfile *last_subfile)
19306{
19307 if (current_subfile != last_subfile)
19308 return 1;
19309 if (line != last_line)
19310 return 1;
19311 /* Same line for the same file that we've seen already.
19312 As a last check, for pr 17276, only record the line if the line
19313 has never had a non-zero discriminator. */
19314 if (!line_has_non_zero_discriminator)
19315 return 1;
19316 return 0;
19317}
19318
252a6764
DE
19319/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
19320 in the line table of subfile SUBFILE. */
19321
19322static void
d9b3de22
DE
19323dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19324 unsigned int line, CORE_ADDR address,
19325 record_line_ftype p_record_line)
252a6764
DE
19326{
19327 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19328
27e0867f
DE
19329 if (dwarf_line_debug)
19330 {
19331 fprintf_unfiltered (gdb_stdlog,
19332 "Recording line %u, file %s, address %s\n",
19333 line, lbasename (subfile->name),
19334 paddress (gdbarch, address));
19335 }
19336
d5962de5 19337 (*p_record_line) (subfile, line, addr);
252a6764
DE
19338}
19339
19340/* Subroutine of dwarf_decode_lines_1 to simplify it.
19341 Mark the end of a set of line number records.
d9b3de22 19342 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
19343 If SUBFILE is NULL the request is ignored. */
19344
19345static void
19346dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19347 CORE_ADDR address, record_line_ftype p_record_line)
19348{
27e0867f
DE
19349 if (subfile == NULL)
19350 return;
19351
19352 if (dwarf_line_debug)
19353 {
19354 fprintf_unfiltered (gdb_stdlog,
19355 "Finishing current line, file %s, address %s\n",
19356 lbasename (subfile->name),
19357 paddress (gdbarch, address));
19358 }
19359
d9b3de22
DE
19360 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
19361}
19362
6f77053d
PA
19363void
19364lnp_state_machine::record_line (bool end_sequence)
d9b3de22 19365{
d9b3de22
DE
19366 if (dwarf_line_debug)
19367 {
19368 fprintf_unfiltered (gdb_stdlog,
19369 "Processing actual line %u: file %u,"
19370 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
19371 m_line, to_underlying (m_file),
19372 paddress (m_gdbarch, m_address),
19373 m_is_stmt, m_discriminator);
d9b3de22
DE
19374 }
19375
6f77053d 19376 file_entry *fe = current_file ();
8c43009f
PA
19377
19378 if (fe == NULL)
d9b3de22
DE
19379 dwarf2_debug_line_missing_file_complaint ();
19380 /* For now we ignore lines not starting on an instruction boundary.
19381 But not when processing end_sequence for compatibility with the
19382 previous version of the code. */
6f77053d 19383 else if (m_op_index == 0 || end_sequence)
d9b3de22 19384 {
8c43009f 19385 fe->included_p = 1;
6f77053d 19386 if (m_record_lines_p && m_is_stmt)
d9b3de22 19387 {
6f77053d 19388 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 19389 {
6f77053d
PA
19390 dwarf_finish_line (m_gdbarch, m_last_subfile,
19391 m_address, m_record_line_callback);
d9b3de22
DE
19392 }
19393
19394 if (!end_sequence)
19395 {
6f77053d
PA
19396 if (dwarf_record_line_p (m_line, m_last_line,
19397 m_line_has_non_zero_discriminator,
19398 m_last_subfile))
d9b3de22 19399 {
6f77053d
PA
19400 dwarf_record_line_1 (m_gdbarch, current_subfile,
19401 m_line, m_address,
19402 m_record_line_callback);
d9b3de22 19403 }
6f77053d
PA
19404 m_last_subfile = current_subfile;
19405 m_last_line = m_line;
d9b3de22
DE
19406 }
19407 }
19408 }
19409}
19410
6f77053d
PA
19411lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
19412 bool record_lines_p)
d9b3de22 19413{
6f77053d
PA
19414 m_gdbarch = arch;
19415 m_record_lines_p = record_lines_p;
19416 m_line_header = lh;
d9b3de22 19417
6f77053d 19418 m_record_line_callback = ::record_line;
d9b3de22 19419
d9b3de22
DE
19420 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19421 was a line entry for it so that the backend has a chance to adjust it
19422 and also record it in case it needs it. This is currently used by MIPS
19423 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
19424 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19425 m_is_stmt = lh->default_is_stmt;
19426 m_discriminator = 0;
252a6764
DE
19427}
19428
6f77053d
PA
19429void
19430lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19431 const gdb_byte *line_ptr,
19432 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
19433{
19434 /* If address < lowpc then it's not a usable value, it's outside the
19435 pc range of the CU. However, we restrict the test to only address
19436 values of zero to preserve GDB's previous behaviour which is to
19437 handle the specific case of a function being GC'd by the linker. */
19438
19439 if (address == 0 && address < lowpc)
19440 {
19441 /* This line table is for a function which has been
19442 GCd by the linker. Ignore it. PR gdb/12528 */
19443
19444 struct objfile *objfile = cu->objfile;
19445 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19446
19447 complaint (&symfile_complaints,
19448 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19449 line_offset, objfile_name (objfile));
6f77053d
PA
19450 m_record_line_callback = noop_record_line;
19451 /* Note: record_line_callback is left as noop_record_line until
19452 we see DW_LNE_end_sequence. */
924c2928
DE
19453 }
19454}
19455
f3f5162e 19456/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
19457 Process the line number information in LH.
19458 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19459 program in order to set included_p for every referenced header. */
debd256d 19460
c906108c 19461static void
43f3e411
DE
19462dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19463 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 19464{
d521ce57
TT
19465 const gdb_byte *line_ptr, *extended_end;
19466 const gdb_byte *line_end;
a8c50c1f 19467 unsigned int bytes_read, extended_len;
699ca60a 19468 unsigned char op_code, extended_op;
e142c38c
DJ
19469 CORE_ADDR baseaddr;
19470 struct objfile *objfile = cu->objfile;
f3f5162e 19471 bfd *abfd = objfile->obfd;
fbf65064 19472 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
19473 /* True if we're recording line info (as opposed to building partial
19474 symtabs and just interested in finding include files mentioned by
19475 the line number program). */
19476 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
19477
19478 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 19479
debd256d
JB
19480 line_ptr = lh->statement_program_start;
19481 line_end = lh->statement_program_end;
c906108c
SS
19482
19483 /* Read the statement sequences until there's nothing left. */
19484 while (line_ptr < line_end)
19485 {
6f77053d
PA
19486 /* The DWARF line number program state machine. Reset the state
19487 machine at the start of each sequence. */
19488 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
19489 bool end_sequence = false;
d9b3de22 19490
8c43009f 19491 if (record_lines_p)
c906108c 19492 {
8c43009f
PA
19493 /* Start a subfile for the current file of the state
19494 machine. */
19495 const file_entry *fe = state_machine.current_file ();
19496
19497 if (fe != NULL)
19498 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
19499 }
19500
a738430d 19501 /* Decode the table. */
d9b3de22 19502 while (line_ptr < line_end && !end_sequence)
c906108c
SS
19503 {
19504 op_code = read_1_byte (abfd, line_ptr);
19505 line_ptr += 1;
9aa1fe7e 19506
debd256d 19507 if (op_code >= lh->opcode_base)
6e70227d 19508 {
8e07a239 19509 /* Special opcode. */
6f77053d 19510 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
19511 }
19512 else switch (op_code)
c906108c
SS
19513 {
19514 case DW_LNS_extended_op:
3e43a32a
MS
19515 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19516 &bytes_read);
473b7be6 19517 line_ptr += bytes_read;
a8c50c1f 19518 extended_end = line_ptr + extended_len;
c906108c
SS
19519 extended_op = read_1_byte (abfd, line_ptr);
19520 line_ptr += 1;
19521 switch (extended_op)
19522 {
19523 case DW_LNE_end_sequence:
6f77053d
PA
19524 state_machine.handle_end_sequence ();
19525 end_sequence = true;
c906108c
SS
19526 break;
19527 case DW_LNE_set_address:
d9b3de22
DE
19528 {
19529 CORE_ADDR address
19530 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 19531 line_ptr += bytes_read;
6f77053d
PA
19532
19533 state_machine.check_line_address (cu, line_ptr,
19534 lowpc, address);
19535 state_machine.handle_set_address (baseaddr, address);
d9b3de22 19536 }
c906108c
SS
19537 break;
19538 case DW_LNE_define_file:
debd256d 19539 {
d521ce57 19540 const char *cur_file;
ecfb656c
PA
19541 unsigned int mod_time, length;
19542 dir_index dindex;
6e70227d 19543
3e43a32a
MS
19544 cur_file = read_direct_string (abfd, line_ptr,
19545 &bytes_read);
debd256d 19546 line_ptr += bytes_read;
ecfb656c 19547 dindex = (dir_index)
debd256d
JB
19548 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19549 line_ptr += bytes_read;
19550 mod_time =
19551 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19552 line_ptr += bytes_read;
19553 length =
19554 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19555 line_ptr += bytes_read;
ecfb656c 19556 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 19557 }
c906108c 19558 break;
d0c6ba3d 19559 case DW_LNE_set_discriminator:
6f77053d
PA
19560 {
19561 /* The discriminator is not interesting to the
19562 debugger; just ignore it. We still need to
19563 check its value though:
19564 if there are consecutive entries for the same
19565 (non-prologue) line we want to coalesce them.
19566 PR 17276. */
19567 unsigned int discr
19568 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19569 line_ptr += bytes_read;
19570
19571 state_machine.handle_set_discriminator (discr);
19572 }
d0c6ba3d 19573 break;
c906108c 19574 default:
4d3c2250 19575 complaint (&symfile_complaints,
e2e0b3e5 19576 _("mangled .debug_line section"));
debd256d 19577 return;
c906108c 19578 }
a8c50c1f
DJ
19579 /* Make sure that we parsed the extended op correctly. If e.g.
19580 we expected a different address size than the producer used,
19581 we may have read the wrong number of bytes. */
19582 if (line_ptr != extended_end)
19583 {
19584 complaint (&symfile_complaints,
19585 _("mangled .debug_line section"));
19586 return;
19587 }
c906108c
SS
19588 break;
19589 case DW_LNS_copy:
6f77053d 19590 state_machine.handle_copy ();
c906108c
SS
19591 break;
19592 case DW_LNS_advance_pc:
2dc7f7b3
TT
19593 {
19594 CORE_ADDR adjust
19595 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 19596 line_ptr += bytes_read;
6f77053d
PA
19597
19598 state_machine.handle_advance_pc (adjust);
2dc7f7b3 19599 }
c906108c
SS
19600 break;
19601 case DW_LNS_advance_line:
a05a36a5
DE
19602 {
19603 int line_delta
19604 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 19605 line_ptr += bytes_read;
6f77053d
PA
19606
19607 state_machine.handle_advance_line (line_delta);
a05a36a5 19608 }
c906108c
SS
19609 break;
19610 case DW_LNS_set_file:
d9b3de22 19611 {
6f77053d 19612 file_name_index file
ecfb656c
PA
19613 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19614 &bytes_read);
d9b3de22 19615 line_ptr += bytes_read;
8c43009f 19616
6f77053d 19617 state_machine.handle_set_file (file);
d9b3de22 19618 }
c906108c
SS
19619 break;
19620 case DW_LNS_set_column:
0ad93d4f 19621 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
19622 line_ptr += bytes_read;
19623 break;
19624 case DW_LNS_negate_stmt:
6f77053d 19625 state_machine.handle_negate_stmt ();
c906108c
SS
19626 break;
19627 case DW_LNS_set_basic_block:
c906108c 19628 break;
c2c6d25f
JM
19629 /* Add to the address register of the state machine the
19630 address increment value corresponding to special opcode
a738430d
MK
19631 255. I.e., this value is scaled by the minimum
19632 instruction length since special opcode 255 would have
b021a221 19633 scaled the increment. */
c906108c 19634 case DW_LNS_const_add_pc:
6f77053d 19635 state_machine.handle_const_add_pc ();
c906108c
SS
19636 break;
19637 case DW_LNS_fixed_advance_pc:
3e29f34a 19638 {
6f77053d 19639 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 19640 line_ptr += 2;
6f77053d
PA
19641
19642 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 19643 }
c906108c 19644 break;
9aa1fe7e 19645 default:
a738430d
MK
19646 {
19647 /* Unknown standard opcode, ignore it. */
9aa1fe7e 19648 int i;
a738430d 19649
debd256d 19650 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
19651 {
19652 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19653 line_ptr += bytes_read;
19654 }
19655 }
c906108c
SS
19656 }
19657 }
d9b3de22
DE
19658
19659 if (!end_sequence)
19660 dwarf2_debug_line_missing_end_sequence_complaint ();
19661
19662 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19663 in which case we still finish recording the last line). */
6f77053d 19664 state_machine.record_line (true);
c906108c 19665 }
f3f5162e
DE
19666}
19667
19668/* Decode the Line Number Program (LNP) for the given line_header
19669 structure and CU. The actual information extracted and the type
19670 of structures created from the LNP depends on the value of PST.
19671
19672 1. If PST is NULL, then this procedure uses the data from the program
19673 to create all necessary symbol tables, and their linetables.
19674
19675 2. If PST is not NULL, this procedure reads the program to determine
19676 the list of files included by the unit represented by PST, and
19677 builds all the associated partial symbol tables.
19678
19679 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19680 It is used for relative paths in the line table.
19681 NOTE: When processing partial symtabs (pst != NULL),
19682 comp_dir == pst->dirname.
19683
19684 NOTE: It is important that psymtabs have the same file name (via strcmp)
19685 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19686 symtab we don't use it in the name of the psymtabs we create.
19687 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
19688 A good testcase for this is mb-inline.exp.
19689
527f3840
JK
19690 LOWPC is the lowest address in CU (or 0 if not known).
19691
19692 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19693 for its PC<->lines mapping information. Otherwise only the filename
19694 table is read in. */
f3f5162e
DE
19695
19696static void
19697dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 19698 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 19699 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
19700{
19701 struct objfile *objfile = cu->objfile;
19702 const int decode_for_pst_p = (pst != NULL);
f3f5162e 19703
527f3840
JK
19704 if (decode_mapping)
19705 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
19706
19707 if (decode_for_pst_p)
19708 {
19709 int file_index;
19710
19711 /* Now that we're done scanning the Line Header Program, we can
19712 create the psymtab of each included file. */
fff8551c 19713 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
19714 if (lh->file_names[file_index].included_p == 1)
19715 {
d521ce57 19716 const char *include_name =
c6da4cef
DE
19717 psymtab_include_file_name (lh, file_index, pst, comp_dir);
19718 if (include_name != NULL)
aaa75496
JB
19719 dwarf2_create_include_psymtab (include_name, pst, objfile);
19720 }
19721 }
cb1df416
DJ
19722 else
19723 {
19724 /* Make sure a symtab is created for every file, even files
19725 which contain only variables (i.e. no code with associated
19726 line numbers). */
43f3e411 19727 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 19728 int i;
cb1df416 19729
fff8551c 19730 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 19731 {
8c43009f 19732 file_entry &fe = lh->file_names[i];
9a619af0 19733
8c43009f 19734 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 19735
cb1df416 19736 if (current_subfile->symtab == NULL)
43f3e411
DE
19737 {
19738 current_subfile->symtab
19739 = allocate_symtab (cust, current_subfile->name);
19740 }
8c43009f 19741 fe.symtab = current_subfile->symtab;
cb1df416
DJ
19742 }
19743 }
c906108c
SS
19744}
19745
19746/* Start a subfile for DWARF. FILENAME is the name of the file and
19747 DIRNAME the name of the source directory which contains FILENAME
4d663531 19748 or NULL if not known.
c906108c
SS
19749 This routine tries to keep line numbers from identical absolute and
19750 relative file names in a common subfile.
19751
19752 Using the `list' example from the GDB testsuite, which resides in
19753 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19754 of /srcdir/list0.c yields the following debugging information for list0.c:
19755
c5aa993b 19756 DW_AT_name: /srcdir/list0.c
4d663531 19757 DW_AT_comp_dir: /compdir
357e46e7 19758 files.files[0].name: list0.h
c5aa993b 19759 files.files[0].dir: /srcdir
357e46e7 19760 files.files[1].name: list0.c
c5aa993b 19761 files.files[1].dir: /srcdir
c906108c
SS
19762
19763 The line number information for list0.c has to end up in a single
4f1520fb
FR
19764 subfile, so that `break /srcdir/list0.c:1' works as expected.
19765 start_subfile will ensure that this happens provided that we pass the
19766 concatenation of files.files[1].dir and files.files[1].name as the
19767 subfile's name. */
c906108c
SS
19768
19769static void
4d663531 19770dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 19771{
d521ce57 19772 char *copy = NULL;
4f1520fb 19773
4d663531 19774 /* In order not to lose the line information directory,
4f1520fb
FR
19775 we concatenate it to the filename when it makes sense.
19776 Note that the Dwarf3 standard says (speaking of filenames in line
19777 information): ``The directory index is ignored for file names
19778 that represent full path names''. Thus ignoring dirname in the
19779 `else' branch below isn't an issue. */
c906108c 19780
d5166ae1 19781 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
19782 {
19783 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
19784 filename = copy;
19785 }
c906108c 19786
4d663531 19787 start_subfile (filename);
4f1520fb 19788
d521ce57
TT
19789 if (copy != NULL)
19790 xfree (copy);
c906108c
SS
19791}
19792
f4dc4d17
DE
19793/* Start a symtab for DWARF.
19794 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
19795
43f3e411 19796static struct compunit_symtab *
f4dc4d17 19797dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 19798 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 19799{
43f3e411 19800 struct compunit_symtab *cust
5ffa0793 19801 = start_symtab (cu->objfile, name, comp_dir, low_pc, cu->language);
43f3e411 19802
f4dc4d17
DE
19803 record_debugformat ("DWARF 2");
19804 record_producer (cu->producer);
19805
19806 /* We assume that we're processing GCC output. */
19807 processing_gcc_compilation = 2;
19808
4d4ec4e5 19809 cu->processing_has_namespace_info = 0;
43f3e411
DE
19810
19811 return cust;
f4dc4d17
DE
19812}
19813
4c2df51b
DJ
19814static void
19815var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 19816 struct dwarf2_cu *cu)
4c2df51b 19817{
e7c27a73
DJ
19818 struct objfile *objfile = cu->objfile;
19819 struct comp_unit_head *cu_header = &cu->header;
19820
4c2df51b
DJ
19821 /* NOTE drow/2003-01-30: There used to be a comment and some special
19822 code here to turn a symbol with DW_AT_external and a
19823 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19824 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19825 with some versions of binutils) where shared libraries could have
19826 relocations against symbols in their debug information - the
19827 minimal symbol would have the right address, but the debug info
19828 would not. It's no longer necessary, because we will explicitly
19829 apply relocations when we read in the debug information now. */
19830
19831 /* A DW_AT_location attribute with no contents indicates that a
19832 variable has been optimized away. */
19833 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
19834 {
f1e6e072 19835 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
19836 return;
19837 }
19838
19839 /* Handle one degenerate form of location expression specially, to
19840 preserve GDB's previous behavior when section offsets are
3019eac3
DE
19841 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
19842 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
19843
19844 if (attr_form_is_block (attr)
3019eac3
DE
19845 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19846 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19847 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19848 && (DW_BLOCK (attr)->size
19849 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 19850 {
891d2f0b 19851 unsigned int dummy;
4c2df51b 19852
3019eac3
DE
19853 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
19854 SYMBOL_VALUE_ADDRESS (sym) =
19855 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
19856 else
19857 SYMBOL_VALUE_ADDRESS (sym) =
19858 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 19859 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
19860 fixup_symbol_section (sym, objfile);
19861 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
19862 SYMBOL_SECTION (sym));
4c2df51b
DJ
19863 return;
19864 }
19865
19866 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
19867 expression evaluator, and use LOC_COMPUTED only when necessary
19868 (i.e. when the value of a register or memory location is
19869 referenced, or a thread-local block, etc.). Then again, it might
19870 not be worthwhile. I'm assuming that it isn't unless performance
19871 or memory numbers show me otherwise. */
19872
f1e6e072 19873 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 19874
f1e6e072 19875 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 19876 cu->has_loclist = 1;
4c2df51b
DJ
19877}
19878
c906108c
SS
19879/* Given a pointer to a DWARF information entry, figure out if we need
19880 to make a symbol table entry for it, and if so, create a new entry
19881 and return a pointer to it.
19882 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
19883 used the passed type.
19884 If SPACE is not NULL, use it to hold the new symbol. If it is
19885 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
19886
19887static struct symbol *
34eaf542
TT
19888new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
19889 struct symbol *space)
c906108c 19890{
e7c27a73 19891 struct objfile *objfile = cu->objfile;
3e29f34a 19892 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 19893 struct symbol *sym = NULL;
15d034d0 19894 const char *name;
c906108c
SS
19895 struct attribute *attr = NULL;
19896 struct attribute *attr2 = NULL;
e142c38c 19897 CORE_ADDR baseaddr;
e37fd15a
SW
19898 struct pending **list_to_add = NULL;
19899
edb3359d 19900 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
19901
19902 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 19903
94af9270 19904 name = dwarf2_name (die, cu);
c906108c
SS
19905 if (name)
19906 {
94af9270 19907 const char *linkagename;
34eaf542 19908 int suppress_add = 0;
94af9270 19909
34eaf542
TT
19910 if (space)
19911 sym = space;
19912 else
e623cf5d 19913 sym = allocate_symbol (objfile);
c906108c 19914 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
19915
19916 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 19917 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
19918 linkagename = dwarf2_physname (name, die, cu);
19919 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 19920
f55ee35c
JK
19921 /* Fortran does not have mangling standard and the mangling does differ
19922 between gfortran, iFort etc. */
19923 if (cu->language == language_fortran
b250c185 19924 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 19925 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 19926 dwarf2_full_name (name, die, cu),
29df156d 19927 NULL);
f55ee35c 19928
c906108c 19929 /* Default assumptions.
c5aa993b 19930 Use the passed type or decode it from the die. */
176620f1 19931 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 19932 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
19933 if (type != NULL)
19934 SYMBOL_TYPE (sym) = type;
19935 else
e7c27a73 19936 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
19937 attr = dwarf2_attr (die,
19938 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
19939 cu);
c906108c
SS
19940 if (attr)
19941 {
19942 SYMBOL_LINE (sym) = DW_UNSND (attr);
19943 }
cb1df416 19944
edb3359d
DJ
19945 attr = dwarf2_attr (die,
19946 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
19947 cu);
cb1df416
DJ
19948 if (attr)
19949 {
ecfb656c 19950 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 19951 struct file_entry *fe;
9a619af0 19952
ecfb656c
PA
19953 if (cu->line_header != NULL)
19954 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
19955 else
19956 fe = NULL;
19957
19958 if (fe == NULL)
cb1df416
DJ
19959 complaint (&symfile_complaints,
19960 _("file index out of range"));
8c43009f
PA
19961 else
19962 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
19963 }
19964
c906108c
SS
19965 switch (die->tag)
19966 {
19967 case DW_TAG_label:
e142c38c 19968 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 19969 if (attr)
3e29f34a
MR
19970 {
19971 CORE_ADDR addr;
19972
19973 addr = attr_value_as_address (attr);
19974 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
19975 SYMBOL_VALUE_ADDRESS (sym) = addr;
19976 }
0f5238ed
TT
19977 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
19978 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 19979 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 19980 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
19981 break;
19982 case DW_TAG_subprogram:
19983 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19984 finish_block. */
f1e6e072 19985 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 19986 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
19987 if ((attr2 && (DW_UNSND (attr2) != 0))
19988 || cu->language == language_ada)
c906108c 19989 {
2cfa0c8d
JB
19990 /* Subprograms marked external are stored as a global symbol.
19991 Ada subprograms, whether marked external or not, are always
19992 stored as a global symbol, because we want to be able to
19993 access them globally. For instance, we want to be able
19994 to break on a nested subprogram without having to
19995 specify the context. */
e37fd15a 19996 list_to_add = &global_symbols;
c906108c
SS
19997 }
19998 else
19999 {
e37fd15a 20000 list_to_add = cu->list_in_scope;
c906108c
SS
20001 }
20002 break;
edb3359d
DJ
20003 case DW_TAG_inlined_subroutine:
20004 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20005 finish_block. */
f1e6e072 20006 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20007 SYMBOL_INLINED (sym) = 1;
481860b3 20008 list_to_add = cu->list_in_scope;
edb3359d 20009 break;
34eaf542
TT
20010 case DW_TAG_template_value_param:
20011 suppress_add = 1;
20012 /* Fall through. */
72929c62 20013 case DW_TAG_constant:
c906108c 20014 case DW_TAG_variable:
254e6b9e 20015 case DW_TAG_member:
0963b4bd
MS
20016 /* Compilation with minimal debug info may result in
20017 variables with missing type entries. Change the
20018 misleading `void' type to something sensible. */
c906108c 20019 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 20020 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20021
e142c38c 20022 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20023 /* In the case of DW_TAG_member, we should only be called for
20024 static const members. */
20025 if (die->tag == DW_TAG_member)
20026 {
3863f96c
DE
20027 /* dwarf2_add_field uses die_is_declaration,
20028 so we do the same. */
254e6b9e
DE
20029 gdb_assert (die_is_declaration (die, cu));
20030 gdb_assert (attr);
20031 }
c906108c
SS
20032 if (attr)
20033 {
e7c27a73 20034 dwarf2_const_value (attr, sym, cu);
e142c38c 20035 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20036 if (!suppress_add)
34eaf542
TT
20037 {
20038 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 20039 list_to_add = &global_symbols;
34eaf542 20040 else
e37fd15a 20041 list_to_add = cu->list_in_scope;
34eaf542 20042 }
c906108c
SS
20043 break;
20044 }
e142c38c 20045 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
20046 if (attr)
20047 {
e7c27a73 20048 var_decode_location (attr, sym, cu);
e142c38c 20049 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20050
20051 /* Fortran explicitly imports any global symbols to the local
20052 scope by DW_TAG_common_block. */
20053 if (cu->language == language_fortran && die->parent
20054 && die->parent->tag == DW_TAG_common_block)
20055 attr2 = NULL;
20056
caac4577
JG
20057 if (SYMBOL_CLASS (sym) == LOC_STATIC
20058 && SYMBOL_VALUE_ADDRESS (sym) == 0
20059 && !dwarf2_per_objfile->has_section_at_zero)
20060 {
20061 /* When a static variable is eliminated by the linker,
20062 the corresponding debug information is not stripped
20063 out, but the variable address is set to null;
20064 do not add such variables into symbol table. */
20065 }
20066 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20067 {
f55ee35c
JK
20068 /* Workaround gfortran PR debug/40040 - it uses
20069 DW_AT_location for variables in -fPIC libraries which may
20070 get overriden by other libraries/executable and get
20071 a different address. Resolve it by the minimal symbol
20072 which may come from inferior's executable using copy
20073 relocation. Make this workaround only for gfortran as for
20074 other compilers GDB cannot guess the minimal symbol
20075 Fortran mangling kind. */
20076 if (cu->language == language_fortran && die->parent
20077 && die->parent->tag == DW_TAG_module
20078 && cu->producer
28586665 20079 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 20080 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 20081
1c809c68
TT
20082 /* A variable with DW_AT_external is never static,
20083 but it may be block-scoped. */
20084 list_to_add = (cu->list_in_scope == &file_symbols
20085 ? &global_symbols : cu->list_in_scope);
1c809c68 20086 }
c906108c 20087 else
e37fd15a 20088 list_to_add = cu->list_in_scope;
c906108c
SS
20089 }
20090 else
20091 {
20092 /* We do not know the address of this symbol.
c5aa993b
JM
20093 If it is an external symbol and we have type information
20094 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20095 The address of the variable will then be determined from
20096 the minimal symbol table whenever the variable is
20097 referenced. */
e142c38c 20098 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20099
20100 /* Fortran explicitly imports any global symbols to the local
20101 scope by DW_TAG_common_block. */
20102 if (cu->language == language_fortran && die->parent
20103 && die->parent->tag == DW_TAG_common_block)
20104 {
20105 /* SYMBOL_CLASS doesn't matter here because
20106 read_common_block is going to reset it. */
20107 if (!suppress_add)
20108 list_to_add = cu->list_in_scope;
20109 }
20110 else if (attr2 && (DW_UNSND (attr2) != 0)
20111 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20112 {
0fe7935b
DJ
20113 /* A variable with DW_AT_external is never static, but it
20114 may be block-scoped. */
20115 list_to_add = (cu->list_in_scope == &file_symbols
20116 ? &global_symbols : cu->list_in_scope);
20117
f1e6e072 20118 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20119 }
442ddf59
JK
20120 else if (!die_is_declaration (die, cu))
20121 {
20122 /* Use the default LOC_OPTIMIZED_OUT class. */
20123 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20124 if (!suppress_add)
20125 list_to_add = cu->list_in_scope;
442ddf59 20126 }
c906108c
SS
20127 }
20128 break;
20129 case DW_TAG_formal_parameter:
edb3359d
DJ
20130 /* If we are inside a function, mark this as an argument. If
20131 not, we might be looking at an argument to an inlined function
20132 when we do not have enough information to show inlined frames;
20133 pretend it's a local variable in that case so that the user can
20134 still see it. */
20135 if (context_stack_depth > 0
20136 && context_stack[context_stack_depth - 1].name != NULL)
20137 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 20138 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
20139 if (attr)
20140 {
e7c27a73 20141 var_decode_location (attr, sym, cu);
c906108c 20142 }
e142c38c 20143 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
20144 if (attr)
20145 {
e7c27a73 20146 dwarf2_const_value (attr, sym, cu);
c906108c 20147 }
f346a30d 20148
e37fd15a 20149 list_to_add = cu->list_in_scope;
c906108c
SS
20150 break;
20151 case DW_TAG_unspecified_parameters:
20152 /* From varargs functions; gdb doesn't seem to have any
20153 interest in this information, so just ignore it for now.
20154 (FIXME?) */
20155 break;
34eaf542
TT
20156 case DW_TAG_template_type_param:
20157 suppress_add = 1;
20158 /* Fall through. */
c906108c 20159 case DW_TAG_class_type:
680b30c7 20160 case DW_TAG_interface_type:
c906108c
SS
20161 case DW_TAG_structure_type:
20162 case DW_TAG_union_type:
72019c9c 20163 case DW_TAG_set_type:
c906108c 20164 case DW_TAG_enumeration_type:
f1e6e072 20165 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20166 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20167
63d06c5c 20168 {
9c37b5ae 20169 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20170 really ever be static objects: otherwise, if you try
20171 to, say, break of a class's method and you're in a file
20172 which doesn't mention that class, it won't work unless
20173 the check for all static symbols in lookup_symbol_aux
20174 saves you. See the OtherFileClass tests in
20175 gdb.c++/namespace.exp. */
20176
e37fd15a 20177 if (!suppress_add)
34eaf542 20178 {
34eaf542 20179 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 20180 && cu->language == language_cplus
34eaf542 20181 ? &global_symbols : cu->list_in_scope);
63d06c5c 20182
64382290 20183 /* The semantics of C++ state that "struct foo {
9c37b5ae 20184 ... }" also defines a typedef for "foo". */
64382290 20185 if (cu->language == language_cplus
45280282 20186 || cu->language == language_ada
c44af4eb
TT
20187 || cu->language == language_d
20188 || cu->language == language_rust)
64382290
TT
20189 {
20190 /* The symbol's name is already allocated along
20191 with this objfile, so we don't need to
20192 duplicate it for the type. */
20193 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20194 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
20195 }
63d06c5c
DC
20196 }
20197 }
c906108c
SS
20198 break;
20199 case DW_TAG_typedef:
f1e6e072 20200 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20201 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20202 list_to_add = cu->list_in_scope;
63d06c5c 20203 break;
c906108c 20204 case DW_TAG_base_type:
a02abb62 20205 case DW_TAG_subrange_type:
f1e6e072 20206 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20207 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20208 list_to_add = cu->list_in_scope;
c906108c
SS
20209 break;
20210 case DW_TAG_enumerator:
e142c38c 20211 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
20212 if (attr)
20213 {
e7c27a73 20214 dwarf2_const_value (attr, sym, cu);
c906108c 20215 }
63d06c5c
DC
20216 {
20217 /* NOTE: carlton/2003-11-10: See comment above in the
20218 DW_TAG_class_type, etc. block. */
20219
e142c38c 20220 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 20221 && cu->language == language_cplus
e142c38c 20222 ? &global_symbols : cu->list_in_scope);
63d06c5c 20223 }
c906108c 20224 break;
74921315 20225 case DW_TAG_imported_declaration:
5c4e30ca 20226 case DW_TAG_namespace:
f1e6e072 20227 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 20228 list_to_add = &global_symbols;
5c4e30ca 20229 break;
530e8392
KB
20230 case DW_TAG_module:
20231 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20232 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20233 list_to_add = &global_symbols;
20234 break;
4357ac6c 20235 case DW_TAG_common_block:
f1e6e072 20236 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
20237 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20238 add_symbol_to_list (sym, cu->list_in_scope);
20239 break;
c906108c
SS
20240 default:
20241 /* Not a tag we recognize. Hopefully we aren't processing
20242 trash data, but since we must specifically ignore things
20243 we don't recognize, there is nothing else we should do at
0963b4bd 20244 this point. */
e2e0b3e5 20245 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 20246 dwarf_tag_name (die->tag));
c906108c
SS
20247 break;
20248 }
df8a16a1 20249
e37fd15a
SW
20250 if (suppress_add)
20251 {
20252 sym->hash_next = objfile->template_symbols;
20253 objfile->template_symbols = sym;
20254 list_to_add = NULL;
20255 }
20256
20257 if (list_to_add != NULL)
20258 add_symbol_to_list (sym, list_to_add);
20259
df8a16a1
DJ
20260 /* For the benefit of old versions of GCC, check for anonymous
20261 namespaces based on the demangled name. */
4d4ec4e5 20262 if (!cu->processing_has_namespace_info
94af9270 20263 && cu->language == language_cplus)
a10964d1 20264 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
20265 }
20266 return (sym);
20267}
20268
34eaf542
TT
20269/* A wrapper for new_symbol_full that always allocates a new symbol. */
20270
20271static struct symbol *
20272new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20273{
20274 return new_symbol_full (die, type, cu, NULL);
20275}
20276
98bfdba5
PA
20277/* Given an attr with a DW_FORM_dataN value in host byte order,
20278 zero-extend it as appropriate for the symbol's type. The DWARF
20279 standard (v4) is not entirely clear about the meaning of using
20280 DW_FORM_dataN for a constant with a signed type, where the type is
20281 wider than the data. The conclusion of a discussion on the DWARF
20282 list was that this is unspecified. We choose to always zero-extend
20283 because that is the interpretation long in use by GCC. */
c906108c 20284
98bfdba5 20285static gdb_byte *
ff39bb5e 20286dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 20287 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 20288{
e7c27a73 20289 struct objfile *objfile = cu->objfile;
e17a4113
UW
20290 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20291 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
20292 LONGEST l = DW_UNSND (attr);
20293
20294 if (bits < sizeof (*value) * 8)
20295 {
20296 l &= ((LONGEST) 1 << bits) - 1;
20297 *value = l;
20298 }
20299 else if (bits == sizeof (*value) * 8)
20300 *value = l;
20301 else
20302 {
224c3ddb 20303 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
20304 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20305 return bytes;
20306 }
20307
20308 return NULL;
20309}
20310
20311/* Read a constant value from an attribute. Either set *VALUE, or if
20312 the value does not fit in *VALUE, set *BYTES - either already
20313 allocated on the objfile obstack, or newly allocated on OBSTACK,
20314 or, set *BATON, if we translated the constant to a location
20315 expression. */
20316
20317static void
ff39bb5e 20318dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
20319 const char *name, struct obstack *obstack,
20320 struct dwarf2_cu *cu,
d521ce57 20321 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
20322 struct dwarf2_locexpr_baton **baton)
20323{
20324 struct objfile *objfile = cu->objfile;
20325 struct comp_unit_head *cu_header = &cu->header;
c906108c 20326 struct dwarf_block *blk;
98bfdba5
PA
20327 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20328 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20329
20330 *value = 0;
20331 *bytes = NULL;
20332 *baton = NULL;
c906108c
SS
20333
20334 switch (attr->form)
20335 {
20336 case DW_FORM_addr:
3019eac3 20337 case DW_FORM_GNU_addr_index:
ac56253d 20338 {
ac56253d
TT
20339 gdb_byte *data;
20340
98bfdba5
PA
20341 if (TYPE_LENGTH (type) != cu_header->addr_size)
20342 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 20343 cu_header->addr_size,
98bfdba5 20344 TYPE_LENGTH (type));
ac56253d
TT
20345 /* Symbols of this form are reasonably rare, so we just
20346 piggyback on the existing location code rather than writing
20347 a new implementation of symbol_computed_ops. */
8d749320 20348 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
20349 (*baton)->per_cu = cu->per_cu;
20350 gdb_assert ((*baton)->per_cu);
ac56253d 20351
98bfdba5 20352 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 20353 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 20354 (*baton)->data = data;
ac56253d
TT
20355
20356 data[0] = DW_OP_addr;
20357 store_unsigned_integer (&data[1], cu_header->addr_size,
20358 byte_order, DW_ADDR (attr));
20359 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 20360 }
c906108c 20361 break;
4ac36638 20362 case DW_FORM_string:
93b5768b 20363 case DW_FORM_strp:
3019eac3 20364 case DW_FORM_GNU_str_index:
36586728 20365 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
20366 /* DW_STRING is already allocated on the objfile obstack, point
20367 directly to it. */
d521ce57 20368 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 20369 break;
c906108c
SS
20370 case DW_FORM_block1:
20371 case DW_FORM_block2:
20372 case DW_FORM_block4:
20373 case DW_FORM_block:
2dc7f7b3 20374 case DW_FORM_exprloc:
0224619f 20375 case DW_FORM_data16:
c906108c 20376 blk = DW_BLOCK (attr);
98bfdba5
PA
20377 if (TYPE_LENGTH (type) != blk->size)
20378 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20379 TYPE_LENGTH (type));
20380 *bytes = blk->data;
c906108c 20381 break;
2df3850c
JM
20382
20383 /* The DW_AT_const_value attributes are supposed to carry the
20384 symbol's value "represented as it would be on the target
20385 architecture." By the time we get here, it's already been
20386 converted to host endianness, so we just need to sign- or
20387 zero-extend it as appropriate. */
20388 case DW_FORM_data1:
3aef2284 20389 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 20390 break;
c906108c 20391 case DW_FORM_data2:
3aef2284 20392 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 20393 break;
c906108c 20394 case DW_FORM_data4:
3aef2284 20395 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 20396 break;
c906108c 20397 case DW_FORM_data8:
3aef2284 20398 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
20399 break;
20400
c906108c 20401 case DW_FORM_sdata:
663c44ac 20402 case DW_FORM_implicit_const:
98bfdba5 20403 *value = DW_SND (attr);
2df3850c
JM
20404 break;
20405
c906108c 20406 case DW_FORM_udata:
98bfdba5 20407 *value = DW_UNSND (attr);
c906108c 20408 break;
2df3850c 20409
c906108c 20410 default:
4d3c2250 20411 complaint (&symfile_complaints,
e2e0b3e5 20412 _("unsupported const value attribute form: '%s'"),
4d3c2250 20413 dwarf_form_name (attr->form));
98bfdba5 20414 *value = 0;
c906108c
SS
20415 break;
20416 }
20417}
20418
2df3850c 20419
98bfdba5
PA
20420/* Copy constant value from an attribute to a symbol. */
20421
2df3850c 20422static void
ff39bb5e 20423dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 20424 struct dwarf2_cu *cu)
2df3850c 20425{
98bfdba5 20426 struct objfile *objfile = cu->objfile;
12df843f 20427 LONGEST value;
d521ce57 20428 const gdb_byte *bytes;
98bfdba5 20429 struct dwarf2_locexpr_baton *baton;
2df3850c 20430
98bfdba5
PA
20431 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20432 SYMBOL_PRINT_NAME (sym),
20433 &objfile->objfile_obstack, cu,
20434 &value, &bytes, &baton);
2df3850c 20435
98bfdba5
PA
20436 if (baton != NULL)
20437 {
98bfdba5 20438 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 20439 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
20440 }
20441 else if (bytes != NULL)
20442 {
20443 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 20444 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
20445 }
20446 else
20447 {
20448 SYMBOL_VALUE (sym) = value;
f1e6e072 20449 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 20450 }
2df3850c
JM
20451}
20452
c906108c
SS
20453/* Return the type of the die in question using its DW_AT_type attribute. */
20454
20455static struct type *
e7c27a73 20456die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20457{
c906108c 20458 struct attribute *type_attr;
c906108c 20459
e142c38c 20460 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
20461 if (!type_attr)
20462 {
20463 /* A missing DW_AT_type represents a void type. */
46bf5051 20464 return objfile_type (cu->objfile)->builtin_void;
c906108c 20465 }
348e048f 20466
673bfd45 20467 return lookup_die_type (die, type_attr, cu);
c906108c
SS
20468}
20469
b4ba55a1
JB
20470/* True iff CU's producer generates GNAT Ada auxiliary information
20471 that allows to find parallel types through that information instead
20472 of having to do expensive parallel lookups by type name. */
20473
20474static int
20475need_gnat_info (struct dwarf2_cu *cu)
20476{
20477 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
20478 of GNAT produces this auxiliary information, without any indication
20479 that it is produced. Part of enhancing the FSF version of GNAT
20480 to produce that information will be to put in place an indicator
20481 that we can use in order to determine whether the descriptive type
20482 info is available or not. One suggestion that has been made is
20483 to use a new attribute, attached to the CU die. For now, assume
20484 that the descriptive type info is not available. */
20485 return 0;
20486}
20487
b4ba55a1
JB
20488/* Return the auxiliary type of the die in question using its
20489 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20490 attribute is not present. */
20491
20492static struct type *
20493die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20494{
b4ba55a1 20495 struct attribute *type_attr;
b4ba55a1
JB
20496
20497 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20498 if (!type_attr)
20499 return NULL;
20500
673bfd45 20501 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
20502}
20503
20504/* If DIE has a descriptive_type attribute, then set the TYPE's
20505 descriptive type accordingly. */
20506
20507static void
20508set_descriptive_type (struct type *type, struct die_info *die,
20509 struct dwarf2_cu *cu)
20510{
20511 struct type *descriptive_type = die_descriptive_type (die, cu);
20512
20513 if (descriptive_type)
20514 {
20515 ALLOCATE_GNAT_AUX_TYPE (type);
20516 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20517 }
20518}
20519
c906108c
SS
20520/* Return the containing type of the die in question using its
20521 DW_AT_containing_type attribute. */
20522
20523static struct type *
e7c27a73 20524die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20525{
c906108c 20526 struct attribute *type_attr;
c906108c 20527
e142c38c 20528 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
20529 if (!type_attr)
20530 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 20531 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 20532
673bfd45 20533 return lookup_die_type (die, type_attr, cu);
c906108c
SS
20534}
20535
ac9ec31b
DE
20536/* Return an error marker type to use for the ill formed type in DIE/CU. */
20537
20538static struct type *
20539build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20540{
20541 struct objfile *objfile = dwarf2_per_objfile->objfile;
20542 char *message, *saved;
20543
20544 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 20545 objfile_name (objfile),
9c541725
PA
20546 to_underlying (cu->header.sect_off),
20547 to_underlying (die->sect_off));
224c3ddb
SM
20548 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
20549 message, strlen (message));
ac9ec31b
DE
20550 xfree (message);
20551
19f392bc 20552 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
20553}
20554
673bfd45 20555/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
20556 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20557 DW_AT_containing_type.
673bfd45
DE
20558 If there is no type substitute an error marker. */
20559
c906108c 20560static struct type *
ff39bb5e 20561lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 20562 struct dwarf2_cu *cu)
c906108c 20563{
bb5ed363 20564 struct objfile *objfile = cu->objfile;
f792889a
DJ
20565 struct type *this_type;
20566
ac9ec31b
DE
20567 gdb_assert (attr->name == DW_AT_type
20568 || attr->name == DW_AT_GNAT_descriptive_type
20569 || attr->name == DW_AT_containing_type);
20570
673bfd45
DE
20571 /* First see if we have it cached. */
20572
36586728
TT
20573 if (attr->form == DW_FORM_GNU_ref_alt)
20574 {
20575 struct dwarf2_per_cu_data *per_cu;
9c541725 20576 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 20577
9c541725
PA
20578 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
20579 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 20580 }
7771576e 20581 else if (attr_form_is_ref (attr))
673bfd45 20582 {
9c541725 20583 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 20584
9c541725 20585 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 20586 }
55f1336d 20587 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 20588 {
ac9ec31b 20589 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 20590
ac9ec31b 20591 return get_signatured_type (die, signature, cu);
673bfd45
DE
20592 }
20593 else
20594 {
ac9ec31b
DE
20595 complaint (&symfile_complaints,
20596 _("Dwarf Error: Bad type attribute %s in DIE"
20597 " at 0x%x [in module %s]"),
9c541725 20598 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
4262abfb 20599 objfile_name (objfile));
ac9ec31b 20600 return build_error_marker_type (cu, die);
673bfd45
DE
20601 }
20602
20603 /* If not cached we need to read it in. */
20604
20605 if (this_type == NULL)
20606 {
ac9ec31b 20607 struct die_info *type_die = NULL;
673bfd45
DE
20608 struct dwarf2_cu *type_cu = cu;
20609
7771576e 20610 if (attr_form_is_ref (attr))
ac9ec31b
DE
20611 type_die = follow_die_ref (die, attr, &type_cu);
20612 if (type_die == NULL)
20613 return build_error_marker_type (cu, die);
20614 /* If we find the type now, it's probably because the type came
3019eac3
DE
20615 from an inter-CU reference and the type's CU got expanded before
20616 ours. */
ac9ec31b 20617 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
20618 }
20619
20620 /* If we still don't have a type use an error marker. */
20621
20622 if (this_type == NULL)
ac9ec31b 20623 return build_error_marker_type (cu, die);
673bfd45 20624
f792889a 20625 return this_type;
c906108c
SS
20626}
20627
673bfd45
DE
20628/* Return the type in DIE, CU.
20629 Returns NULL for invalid types.
20630
02142a6c 20631 This first does a lookup in die_type_hash,
673bfd45
DE
20632 and only reads the die in if necessary.
20633
20634 NOTE: This can be called when reading in partial or full symbols. */
20635
f792889a 20636static struct type *
e7c27a73 20637read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20638{
f792889a
DJ
20639 struct type *this_type;
20640
20641 this_type = get_die_type (die, cu);
20642 if (this_type)
20643 return this_type;
20644
673bfd45
DE
20645 return read_type_die_1 (die, cu);
20646}
20647
20648/* Read the type in DIE, CU.
20649 Returns NULL for invalid types. */
20650
20651static struct type *
20652read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20653{
20654 struct type *this_type = NULL;
20655
c906108c
SS
20656 switch (die->tag)
20657 {
20658 case DW_TAG_class_type:
680b30c7 20659 case DW_TAG_interface_type:
c906108c
SS
20660 case DW_TAG_structure_type:
20661 case DW_TAG_union_type:
f792889a 20662 this_type = read_structure_type (die, cu);
c906108c
SS
20663 break;
20664 case DW_TAG_enumeration_type:
f792889a 20665 this_type = read_enumeration_type (die, cu);
c906108c
SS
20666 break;
20667 case DW_TAG_subprogram:
20668 case DW_TAG_subroutine_type:
edb3359d 20669 case DW_TAG_inlined_subroutine:
f792889a 20670 this_type = read_subroutine_type (die, cu);
c906108c
SS
20671 break;
20672 case DW_TAG_array_type:
f792889a 20673 this_type = read_array_type (die, cu);
c906108c 20674 break;
72019c9c 20675 case DW_TAG_set_type:
f792889a 20676 this_type = read_set_type (die, cu);
72019c9c 20677 break;
c906108c 20678 case DW_TAG_pointer_type:
f792889a 20679 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
20680 break;
20681 case DW_TAG_ptr_to_member_type:
f792889a 20682 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
20683 break;
20684 case DW_TAG_reference_type:
4297a3f0
AV
20685 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20686 break;
20687 case DW_TAG_rvalue_reference_type:
20688 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
20689 break;
20690 case DW_TAG_const_type:
f792889a 20691 this_type = read_tag_const_type (die, cu);
c906108c
SS
20692 break;
20693 case DW_TAG_volatile_type:
f792889a 20694 this_type = read_tag_volatile_type (die, cu);
c906108c 20695 break;
06d66ee9
TT
20696 case DW_TAG_restrict_type:
20697 this_type = read_tag_restrict_type (die, cu);
20698 break;
c906108c 20699 case DW_TAG_string_type:
f792889a 20700 this_type = read_tag_string_type (die, cu);
c906108c
SS
20701 break;
20702 case DW_TAG_typedef:
f792889a 20703 this_type = read_typedef (die, cu);
c906108c 20704 break;
a02abb62 20705 case DW_TAG_subrange_type:
f792889a 20706 this_type = read_subrange_type (die, cu);
a02abb62 20707 break;
c906108c 20708 case DW_TAG_base_type:
f792889a 20709 this_type = read_base_type (die, cu);
c906108c 20710 break;
81a17f79 20711 case DW_TAG_unspecified_type:
f792889a 20712 this_type = read_unspecified_type (die, cu);
81a17f79 20713 break;
0114d602
DJ
20714 case DW_TAG_namespace:
20715 this_type = read_namespace_type (die, cu);
20716 break;
f55ee35c
JK
20717 case DW_TAG_module:
20718 this_type = read_module_type (die, cu);
20719 break;
a2c2acaf
MW
20720 case DW_TAG_atomic_type:
20721 this_type = read_tag_atomic_type (die, cu);
20722 break;
c906108c 20723 default:
3e43a32a
MS
20724 complaint (&symfile_complaints,
20725 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 20726 dwarf_tag_name (die->tag));
c906108c
SS
20727 break;
20728 }
63d06c5c 20729
f792889a 20730 return this_type;
63d06c5c
DC
20731}
20732
abc72ce4
DE
20733/* See if we can figure out if the class lives in a namespace. We do
20734 this by looking for a member function; its demangled name will
20735 contain namespace info, if there is any.
20736 Return the computed name or NULL.
20737 Space for the result is allocated on the objfile's obstack.
20738 This is the full-die version of guess_partial_die_structure_name.
20739 In this case we know DIE has no useful parent. */
20740
20741static char *
20742guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20743{
20744 struct die_info *spec_die;
20745 struct dwarf2_cu *spec_cu;
20746 struct die_info *child;
20747
20748 spec_cu = cu;
20749 spec_die = die_specification (die, &spec_cu);
20750 if (spec_die != NULL)
20751 {
20752 die = spec_die;
20753 cu = spec_cu;
20754 }
20755
20756 for (child = die->child;
20757 child != NULL;
20758 child = child->sibling)
20759 {
20760 if (child->tag == DW_TAG_subprogram)
20761 {
73b9be8b 20762 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 20763
7d45c7c3 20764 if (linkage_name != NULL)
abc72ce4
DE
20765 {
20766 char *actual_name
20767 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 20768 linkage_name);
abc72ce4
DE
20769 char *name = NULL;
20770
20771 if (actual_name != NULL)
20772 {
15d034d0 20773 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
20774
20775 if (die_name != NULL
20776 && strcmp (die_name, actual_name) != 0)
20777 {
20778 /* Strip off the class name from the full name.
20779 We want the prefix. */
20780 int die_name_len = strlen (die_name);
20781 int actual_name_len = strlen (actual_name);
20782
20783 /* Test for '::' as a sanity check. */
20784 if (actual_name_len > die_name_len + 2
3e43a32a
MS
20785 && actual_name[actual_name_len
20786 - die_name_len - 1] == ':')
224c3ddb
SM
20787 name = (char *) obstack_copy0 (
20788 &cu->objfile->per_bfd->storage_obstack,
20789 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
20790 }
20791 }
20792 xfree (actual_name);
20793 return name;
20794 }
20795 }
20796 }
20797
20798 return NULL;
20799}
20800
96408a79
SA
20801/* GCC might emit a nameless typedef that has a linkage name. Determine the
20802 prefix part in such case. See
20803 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20804
a121b7c1 20805static const char *
96408a79
SA
20806anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
20807{
20808 struct attribute *attr;
e6a959d6 20809 const char *base;
96408a79
SA
20810
20811 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
20812 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
20813 return NULL;
20814
7d45c7c3 20815 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
20816 return NULL;
20817
73b9be8b 20818 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
20819 if (attr == NULL || DW_STRING (attr) == NULL)
20820 return NULL;
20821
20822 /* dwarf2_name had to be already called. */
20823 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20824
20825 /* Strip the base name, keep any leading namespaces/classes. */
20826 base = strrchr (DW_STRING (attr), ':');
20827 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20828 return "";
20829
224c3ddb
SM
20830 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20831 DW_STRING (attr),
20832 &base[-1] - DW_STRING (attr));
96408a79
SA
20833}
20834
fdde2d81 20835/* Return the name of the namespace/class that DIE is defined within,
0114d602 20836 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 20837
0114d602
DJ
20838 For example, if we're within the method foo() in the following
20839 code:
20840
20841 namespace N {
20842 class C {
20843 void foo () {
20844 }
20845 };
20846 }
20847
20848 then determine_prefix on foo's die will return "N::C". */
fdde2d81 20849
0d5cff50 20850static const char *
e142c38c 20851determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 20852{
0114d602
DJ
20853 struct die_info *parent, *spec_die;
20854 struct dwarf2_cu *spec_cu;
20855 struct type *parent_type;
a121b7c1 20856 const char *retval;
63d06c5c 20857
9c37b5ae 20858 if (cu->language != language_cplus
c44af4eb
TT
20859 && cu->language != language_fortran && cu->language != language_d
20860 && cu->language != language_rust)
0114d602
DJ
20861 return "";
20862
96408a79
SA
20863 retval = anonymous_struct_prefix (die, cu);
20864 if (retval)
20865 return retval;
20866
0114d602
DJ
20867 /* We have to be careful in the presence of DW_AT_specification.
20868 For example, with GCC 3.4, given the code
20869
20870 namespace N {
20871 void foo() {
20872 // Definition of N::foo.
20873 }
20874 }
20875
20876 then we'll have a tree of DIEs like this:
20877
20878 1: DW_TAG_compile_unit
20879 2: DW_TAG_namespace // N
20880 3: DW_TAG_subprogram // declaration of N::foo
20881 4: DW_TAG_subprogram // definition of N::foo
20882 DW_AT_specification // refers to die #3
20883
20884 Thus, when processing die #4, we have to pretend that we're in
20885 the context of its DW_AT_specification, namely the contex of die
20886 #3. */
20887 spec_cu = cu;
20888 spec_die = die_specification (die, &spec_cu);
20889 if (spec_die == NULL)
20890 parent = die->parent;
20891 else
63d06c5c 20892 {
0114d602
DJ
20893 parent = spec_die->parent;
20894 cu = spec_cu;
63d06c5c 20895 }
0114d602
DJ
20896
20897 if (parent == NULL)
20898 return "";
98bfdba5
PA
20899 else if (parent->building_fullname)
20900 {
20901 const char *name;
20902 const char *parent_name;
20903
20904 /* It has been seen on RealView 2.2 built binaries,
20905 DW_TAG_template_type_param types actually _defined_ as
20906 children of the parent class:
20907
20908 enum E {};
20909 template class <class Enum> Class{};
20910 Class<enum E> class_e;
20911
20912 1: DW_TAG_class_type (Class)
20913 2: DW_TAG_enumeration_type (E)
20914 3: DW_TAG_enumerator (enum1:0)
20915 3: DW_TAG_enumerator (enum2:1)
20916 ...
20917 2: DW_TAG_template_type_param
20918 DW_AT_type DW_FORM_ref_udata (E)
20919
20920 Besides being broken debug info, it can put GDB into an
20921 infinite loop. Consider:
20922
20923 When we're building the full name for Class<E>, we'll start
20924 at Class, and go look over its template type parameters,
20925 finding E. We'll then try to build the full name of E, and
20926 reach here. We're now trying to build the full name of E,
20927 and look over the parent DIE for containing scope. In the
20928 broken case, if we followed the parent DIE of E, we'd again
20929 find Class, and once again go look at its template type
20930 arguments, etc., etc. Simply don't consider such parent die
20931 as source-level parent of this die (it can't be, the language
20932 doesn't allow it), and break the loop here. */
20933 name = dwarf2_name (die, cu);
20934 parent_name = dwarf2_name (parent, cu);
20935 complaint (&symfile_complaints,
20936 _("template param type '%s' defined within parent '%s'"),
20937 name ? name : "<unknown>",
20938 parent_name ? parent_name : "<unknown>");
20939 return "";
20940 }
63d06c5c 20941 else
0114d602
DJ
20942 switch (parent->tag)
20943 {
63d06c5c 20944 case DW_TAG_namespace:
0114d602 20945 parent_type = read_type_die (parent, cu);
acebe513
UW
20946 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
20947 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
20948 Work around this problem here. */
20949 if (cu->language == language_cplus
20950 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
20951 return "";
0114d602
DJ
20952 /* We give a name to even anonymous namespaces. */
20953 return TYPE_TAG_NAME (parent_type);
63d06c5c 20954 case DW_TAG_class_type:
680b30c7 20955 case DW_TAG_interface_type:
63d06c5c 20956 case DW_TAG_structure_type:
0114d602 20957 case DW_TAG_union_type:
f55ee35c 20958 case DW_TAG_module:
0114d602
DJ
20959 parent_type = read_type_die (parent, cu);
20960 if (TYPE_TAG_NAME (parent_type) != NULL)
20961 return TYPE_TAG_NAME (parent_type);
20962 else
20963 /* An anonymous structure is only allowed non-static data
20964 members; no typedefs, no member functions, et cetera.
20965 So it does not need a prefix. */
20966 return "";
abc72ce4 20967 case DW_TAG_compile_unit:
95554aad 20968 case DW_TAG_partial_unit:
abc72ce4
DE
20969 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
20970 if (cu->language == language_cplus
8b70b953 20971 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
20972 && die->child != NULL
20973 && (die->tag == DW_TAG_class_type
20974 || die->tag == DW_TAG_structure_type
20975 || die->tag == DW_TAG_union_type))
20976 {
20977 char *name = guess_full_die_structure_name (die, cu);
20978 if (name != NULL)
20979 return name;
20980 }
20981 return "";
3d567982
TT
20982 case DW_TAG_enumeration_type:
20983 parent_type = read_type_die (parent, cu);
20984 if (TYPE_DECLARED_CLASS (parent_type))
20985 {
20986 if (TYPE_TAG_NAME (parent_type) != NULL)
20987 return TYPE_TAG_NAME (parent_type);
20988 return "";
20989 }
20990 /* Fall through. */
63d06c5c 20991 default:
8176b9b8 20992 return determine_prefix (parent, cu);
63d06c5c 20993 }
63d06c5c
DC
20994}
20995
3e43a32a
MS
20996/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20997 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20998 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
20999 an obconcat, otherwise allocate storage for the result. The CU argument is
21000 used to determine the language and hence, the appropriate separator. */
987504bb 21001
f55ee35c 21002#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21003
21004static char *
f55ee35c
JK
21005typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21006 int physname, struct dwarf2_cu *cu)
63d06c5c 21007{
f55ee35c 21008 const char *lead = "";
5c315b68 21009 const char *sep;
63d06c5c 21010
3e43a32a
MS
21011 if (suffix == NULL || suffix[0] == '\0'
21012 || prefix == NULL || prefix[0] == '\0')
987504bb 21013 sep = "";
45280282
IB
21014 else if (cu->language == language_d)
21015 {
21016 /* For D, the 'main' function could be defined in any module, but it
21017 should never be prefixed. */
21018 if (strcmp (suffix, "D main") == 0)
21019 {
21020 prefix = "";
21021 sep = "";
21022 }
21023 else
21024 sep = ".";
21025 }
f55ee35c
JK
21026 else if (cu->language == language_fortran && physname)
21027 {
21028 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21029 DW_AT_MIPS_linkage_name is preferred and used instead. */
21030
21031 lead = "__";
21032 sep = "_MOD_";
21033 }
987504bb
JJ
21034 else
21035 sep = "::";
63d06c5c 21036
6dd47d34
DE
21037 if (prefix == NULL)
21038 prefix = "";
21039 if (suffix == NULL)
21040 suffix = "";
21041
987504bb
JJ
21042 if (obs == NULL)
21043 {
3e43a32a 21044 char *retval
224c3ddb
SM
21045 = ((char *)
21046 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21047
f55ee35c
JK
21048 strcpy (retval, lead);
21049 strcat (retval, prefix);
6dd47d34
DE
21050 strcat (retval, sep);
21051 strcat (retval, suffix);
63d06c5c
DC
21052 return retval;
21053 }
987504bb
JJ
21054 else
21055 {
21056 /* We have an obstack. */
f55ee35c 21057 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21058 }
63d06c5c
DC
21059}
21060
c906108c
SS
21061/* Return sibling of die, NULL if no sibling. */
21062
f9aca02d 21063static struct die_info *
fba45db2 21064sibling_die (struct die_info *die)
c906108c 21065{
639d11d3 21066 return die->sibling;
c906108c
SS
21067}
21068
71c25dea
TT
21069/* Get name of a die, return NULL if not found. */
21070
15d034d0
TT
21071static const char *
21072dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
21073 struct obstack *obstack)
21074{
21075 if (name && cu->language == language_cplus)
21076 {
2f408ecb 21077 std::string canon_name = cp_canonicalize_string (name);
71c25dea 21078
2f408ecb 21079 if (!canon_name.empty ())
71c25dea 21080 {
2f408ecb
PA
21081 if (canon_name != name)
21082 name = (const char *) obstack_copy0 (obstack,
21083 canon_name.c_str (),
21084 canon_name.length ());
71c25dea
TT
21085 }
21086 }
21087
21088 return name;
c906108c
SS
21089}
21090
96553a0c
DE
21091/* Get name of a die, return NULL if not found.
21092 Anonymous namespaces are converted to their magic string. */
9219021c 21093
15d034d0 21094static const char *
e142c38c 21095dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21096{
21097 struct attribute *attr;
21098
e142c38c 21099 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21100 if ((!attr || !DW_STRING (attr))
96553a0c 21101 && die->tag != DW_TAG_namespace
53832f31
TT
21102 && die->tag != DW_TAG_class_type
21103 && die->tag != DW_TAG_interface_type
21104 && die->tag != DW_TAG_structure_type
21105 && die->tag != DW_TAG_union_type)
71c25dea
TT
21106 return NULL;
21107
21108 switch (die->tag)
21109 {
21110 case DW_TAG_compile_unit:
95554aad 21111 case DW_TAG_partial_unit:
71c25dea
TT
21112 /* Compilation units have a DW_AT_name that is a filename, not
21113 a source language identifier. */
21114 case DW_TAG_enumeration_type:
21115 case DW_TAG_enumerator:
21116 /* These tags always have simple identifiers already; no need
21117 to canonicalize them. */
21118 return DW_STRING (attr);
907af001 21119
96553a0c
DE
21120 case DW_TAG_namespace:
21121 if (attr != NULL && DW_STRING (attr) != NULL)
21122 return DW_STRING (attr);
21123 return CP_ANONYMOUS_NAMESPACE_STR;
21124
907af001
UW
21125 case DW_TAG_class_type:
21126 case DW_TAG_interface_type:
21127 case DW_TAG_structure_type:
21128 case DW_TAG_union_type:
21129 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21130 structures or unions. These were of the form "._%d" in GCC 4.1,
21131 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21132 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21133 if (attr && DW_STRING (attr)
61012eef
GB
21134 && (startswith (DW_STRING (attr), "._")
21135 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21136 return NULL;
53832f31
TT
21137
21138 /* GCC might emit a nameless typedef that has a linkage name. See
21139 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21140 if (!attr || DW_STRING (attr) == NULL)
21141 {
df5c6c50 21142 char *demangled = NULL;
53832f31 21143
73b9be8b 21144 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21145 if (attr == NULL || DW_STRING (attr) == NULL)
21146 return NULL;
21147
df5c6c50
JK
21148 /* Avoid demangling DW_STRING (attr) the second time on a second
21149 call for the same DIE. */
21150 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 21151 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
21152
21153 if (demangled)
21154 {
e6a959d6 21155 const char *base;
96408a79 21156
53832f31 21157 /* FIXME: we already did this for the partial symbol... */
34a68019 21158 DW_STRING (attr)
224c3ddb
SM
21159 = ((const char *)
21160 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
21161 demangled, strlen (demangled)));
53832f31
TT
21162 DW_STRING_IS_CANONICAL (attr) = 1;
21163 xfree (demangled);
96408a79
SA
21164
21165 /* Strip any leading namespaces/classes, keep only the base name.
21166 DW_AT_name for named DIEs does not contain the prefixes. */
21167 base = strrchr (DW_STRING (attr), ':');
21168 if (base && base > DW_STRING (attr) && base[-1] == ':')
21169 return &base[1];
21170 else
21171 return DW_STRING (attr);
53832f31
TT
21172 }
21173 }
907af001
UW
21174 break;
21175
71c25dea 21176 default:
907af001
UW
21177 break;
21178 }
21179
21180 if (!DW_STRING_IS_CANONICAL (attr))
21181 {
21182 DW_STRING (attr)
21183 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 21184 &cu->objfile->per_bfd->storage_obstack);
907af001 21185 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21186 }
907af001 21187 return DW_STRING (attr);
9219021c
DC
21188}
21189
21190/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21191 is none. *EXT_CU is the CU containing DIE on input, and the CU
21192 containing the return value on output. */
9219021c
DC
21193
21194static struct die_info *
f2f0e013 21195dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21196{
21197 struct attribute *attr;
9219021c 21198
f2f0e013 21199 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21200 if (attr == NULL)
21201 return NULL;
21202
f2f0e013 21203 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21204}
21205
c906108c
SS
21206/* Convert a DIE tag into its string name. */
21207
f39c6ffd 21208static const char *
aa1ee363 21209dwarf_tag_name (unsigned tag)
c906108c 21210{
f39c6ffd
TT
21211 const char *name = get_DW_TAG_name (tag);
21212
21213 if (name == NULL)
21214 return "DW_TAG_<unknown>";
21215
21216 return name;
c906108c
SS
21217}
21218
21219/* Convert a DWARF attribute code into its string name. */
21220
f39c6ffd 21221static const char *
aa1ee363 21222dwarf_attr_name (unsigned attr)
c906108c 21223{
f39c6ffd
TT
21224 const char *name;
21225
c764a876 21226#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
21227 if (attr == DW_AT_MIPS_fde)
21228 return "DW_AT_MIPS_fde";
21229#else
21230 if (attr == DW_AT_HP_block_index)
21231 return "DW_AT_HP_block_index";
c764a876 21232#endif
f39c6ffd
TT
21233
21234 name = get_DW_AT_name (attr);
21235
21236 if (name == NULL)
21237 return "DW_AT_<unknown>";
21238
21239 return name;
c906108c
SS
21240}
21241
21242/* Convert a DWARF value form code into its string name. */
21243
f39c6ffd 21244static const char *
aa1ee363 21245dwarf_form_name (unsigned form)
c906108c 21246{
f39c6ffd
TT
21247 const char *name = get_DW_FORM_name (form);
21248
21249 if (name == NULL)
21250 return "DW_FORM_<unknown>";
21251
21252 return name;
c906108c
SS
21253}
21254
a121b7c1 21255static const char *
fba45db2 21256dwarf_bool_name (unsigned mybool)
c906108c
SS
21257{
21258 if (mybool)
21259 return "TRUE";
21260 else
21261 return "FALSE";
21262}
21263
21264/* Convert a DWARF type code into its string name. */
21265
f39c6ffd 21266static const char *
aa1ee363 21267dwarf_type_encoding_name (unsigned enc)
c906108c 21268{
f39c6ffd 21269 const char *name = get_DW_ATE_name (enc);
c906108c 21270
f39c6ffd
TT
21271 if (name == NULL)
21272 return "DW_ATE_<unknown>";
c906108c 21273
f39c6ffd 21274 return name;
c906108c 21275}
c906108c 21276
f9aca02d 21277static void
d97bc12b 21278dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21279{
21280 unsigned int i;
21281
d97bc12b
DE
21282 print_spaces (indent, f);
21283 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
9c541725
PA
21284 dwarf_tag_name (die->tag), die->abbrev,
21285 to_underlying (die->sect_off));
d97bc12b
DE
21286
21287 if (die->parent != NULL)
21288 {
21289 print_spaces (indent, f);
21290 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9c541725 21291 to_underlying (die->parent->sect_off));
d97bc12b
DE
21292 }
21293
21294 print_spaces (indent, f);
21295 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21296 dwarf_bool_name (die->child != NULL));
c906108c 21297
d97bc12b
DE
21298 print_spaces (indent, f);
21299 fprintf_unfiltered (f, " attributes:\n");
21300
c906108c
SS
21301 for (i = 0; i < die->num_attrs; ++i)
21302 {
d97bc12b
DE
21303 print_spaces (indent, f);
21304 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21305 dwarf_attr_name (die->attrs[i].name),
21306 dwarf_form_name (die->attrs[i].form));
d97bc12b 21307
c906108c
SS
21308 switch (die->attrs[i].form)
21309 {
c906108c 21310 case DW_FORM_addr:
3019eac3 21311 case DW_FORM_GNU_addr_index:
d97bc12b 21312 fprintf_unfiltered (f, "address: ");
5af949e3 21313 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
21314 break;
21315 case DW_FORM_block2:
21316 case DW_FORM_block4:
21317 case DW_FORM_block:
21318 case DW_FORM_block1:
56eb65bd
SP
21319 fprintf_unfiltered (f, "block: size %s",
21320 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 21321 break;
2dc7f7b3 21322 case DW_FORM_exprloc:
56eb65bd
SP
21323 fprintf_unfiltered (f, "expression: size %s",
21324 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 21325 break;
0224619f
JK
21326 case DW_FORM_data16:
21327 fprintf_unfiltered (f, "constant of 16 bytes");
21328 break;
4568ecf9
DE
21329 case DW_FORM_ref_addr:
21330 fprintf_unfiltered (f, "ref address: ");
21331 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21332 break;
36586728
TT
21333 case DW_FORM_GNU_ref_alt:
21334 fprintf_unfiltered (f, "alt ref address: ");
21335 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21336 break;
10b3939b
DJ
21337 case DW_FORM_ref1:
21338 case DW_FORM_ref2:
21339 case DW_FORM_ref4:
4568ecf9
DE
21340 case DW_FORM_ref8:
21341 case DW_FORM_ref_udata:
d97bc12b 21342 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 21343 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 21344 break;
c906108c
SS
21345 case DW_FORM_data1:
21346 case DW_FORM_data2:
21347 case DW_FORM_data4:
ce5d95e1 21348 case DW_FORM_data8:
c906108c
SS
21349 case DW_FORM_udata:
21350 case DW_FORM_sdata:
43bbcdc2
PH
21351 fprintf_unfiltered (f, "constant: %s",
21352 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 21353 break;
2dc7f7b3
TT
21354 case DW_FORM_sec_offset:
21355 fprintf_unfiltered (f, "section offset: %s",
21356 pulongest (DW_UNSND (&die->attrs[i])));
21357 break;
55f1336d 21358 case DW_FORM_ref_sig8:
ac9ec31b
DE
21359 fprintf_unfiltered (f, "signature: %s",
21360 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 21361 break;
c906108c 21362 case DW_FORM_string:
4bdf3d34 21363 case DW_FORM_strp:
43988095 21364 case DW_FORM_line_strp:
3019eac3 21365 case DW_FORM_GNU_str_index:
36586728 21366 case DW_FORM_GNU_strp_alt:
8285870a 21367 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 21368 DW_STRING (&die->attrs[i])
8285870a
JK
21369 ? DW_STRING (&die->attrs[i]) : "",
21370 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
21371 break;
21372 case DW_FORM_flag:
21373 if (DW_UNSND (&die->attrs[i]))
d97bc12b 21374 fprintf_unfiltered (f, "flag: TRUE");
c906108c 21375 else
d97bc12b 21376 fprintf_unfiltered (f, "flag: FALSE");
c906108c 21377 break;
2dc7f7b3
TT
21378 case DW_FORM_flag_present:
21379 fprintf_unfiltered (f, "flag: TRUE");
21380 break;
a8329558 21381 case DW_FORM_indirect:
0963b4bd
MS
21382 /* The reader will have reduced the indirect form to
21383 the "base form" so this form should not occur. */
3e43a32a
MS
21384 fprintf_unfiltered (f,
21385 "unexpected attribute form: DW_FORM_indirect");
a8329558 21386 break;
663c44ac
JK
21387 case DW_FORM_implicit_const:
21388 fprintf_unfiltered (f, "constant: %s",
21389 plongest (DW_SND (&die->attrs[i])));
21390 break;
c906108c 21391 default:
d97bc12b 21392 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 21393 die->attrs[i].form);
d97bc12b 21394 break;
c906108c 21395 }
d97bc12b 21396 fprintf_unfiltered (f, "\n");
c906108c
SS
21397 }
21398}
21399
f9aca02d 21400static void
d97bc12b 21401dump_die_for_error (struct die_info *die)
c906108c 21402{
d97bc12b
DE
21403 dump_die_shallow (gdb_stderr, 0, die);
21404}
21405
21406static void
21407dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21408{
21409 int indent = level * 4;
21410
21411 gdb_assert (die != NULL);
21412
21413 if (level >= max_level)
21414 return;
21415
21416 dump_die_shallow (f, indent, die);
21417
21418 if (die->child != NULL)
c906108c 21419 {
d97bc12b
DE
21420 print_spaces (indent, f);
21421 fprintf_unfiltered (f, " Children:");
21422 if (level + 1 < max_level)
21423 {
21424 fprintf_unfiltered (f, "\n");
21425 dump_die_1 (f, level + 1, max_level, die->child);
21426 }
21427 else
21428 {
3e43a32a
MS
21429 fprintf_unfiltered (f,
21430 " [not printed, max nesting level reached]\n");
d97bc12b
DE
21431 }
21432 }
21433
21434 if (die->sibling != NULL && level > 0)
21435 {
21436 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
21437 }
21438}
21439
d97bc12b
DE
21440/* This is called from the pdie macro in gdbinit.in.
21441 It's not static so gcc will keep a copy callable from gdb. */
21442
21443void
21444dump_die (struct die_info *die, int max_level)
21445{
21446 dump_die_1 (gdb_stdlog, 0, max_level, die);
21447}
21448
f9aca02d 21449static void
51545339 21450store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21451{
51545339 21452 void **slot;
c906108c 21453
9c541725
PA
21454 slot = htab_find_slot_with_hash (cu->die_hash, die,
21455 to_underlying (die->sect_off),
b64f50a1 21456 INSERT);
51545339
DJ
21457
21458 *slot = die;
c906108c
SS
21459}
21460
b64f50a1
JK
21461/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
21462 required kind. */
21463
21464static sect_offset
ff39bb5e 21465dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 21466{
7771576e 21467 if (attr_form_is_ref (attr))
9c541725 21468 return (sect_offset) DW_UNSND (attr);
93311388
DE
21469
21470 complaint (&symfile_complaints,
21471 _("unsupported die ref attribute form: '%s'"),
21472 dwarf_form_name (attr->form));
9c541725 21473 return {};
c906108c
SS
21474}
21475
43bbcdc2
PH
21476/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
21477 * the value held by the attribute is not constant. */
a02abb62 21478
43bbcdc2 21479static LONGEST
ff39bb5e 21480dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 21481{
663c44ac 21482 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
21483 return DW_SND (attr);
21484 else if (attr->form == DW_FORM_udata
21485 || attr->form == DW_FORM_data1
21486 || attr->form == DW_FORM_data2
21487 || attr->form == DW_FORM_data4
21488 || attr->form == DW_FORM_data8)
21489 return DW_UNSND (attr);
21490 else
21491 {
0224619f 21492 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
21493 complaint (&symfile_complaints,
21494 _("Attribute value is not a constant (%s)"),
a02abb62
JB
21495 dwarf_form_name (attr->form));
21496 return default_value;
21497 }
21498}
21499
348e048f
DE
21500/* Follow reference or signature attribute ATTR of SRC_DIE.
21501 On entry *REF_CU is the CU of SRC_DIE.
21502 On exit *REF_CU is the CU of the result. */
21503
21504static struct die_info *
ff39bb5e 21505follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
21506 struct dwarf2_cu **ref_cu)
21507{
21508 struct die_info *die;
21509
7771576e 21510 if (attr_form_is_ref (attr))
348e048f 21511 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 21512 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
21513 die = follow_die_sig (src_die, attr, ref_cu);
21514 else
21515 {
21516 dump_die_for_error (src_die);
21517 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 21518 objfile_name ((*ref_cu)->objfile));
348e048f
DE
21519 }
21520
21521 return die;
03dd20cc
DJ
21522}
21523
5c631832 21524/* Follow reference OFFSET.
673bfd45
DE
21525 On entry *REF_CU is the CU of the source die referencing OFFSET.
21526 On exit *REF_CU is the CU of the result.
21527 Returns NULL if OFFSET is invalid. */
f504f079 21528
f9aca02d 21529static struct die_info *
9c541725 21530follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 21531 struct dwarf2_cu **ref_cu)
c906108c 21532{
10b3939b 21533 struct die_info temp_die;
f2f0e013 21534 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 21535
348e048f
DE
21536 gdb_assert (cu->per_cu != NULL);
21537
98bfdba5
PA
21538 target_cu = cu;
21539
3019eac3 21540 if (cu->per_cu->is_debug_types)
348e048f
DE
21541 {
21542 /* .debug_types CUs cannot reference anything outside their CU.
21543 If they need to, they have to reference a signatured type via
55f1336d 21544 DW_FORM_ref_sig8. */
9c541725 21545 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 21546 return NULL;
348e048f 21547 }
36586728 21548 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 21549 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
21550 {
21551 struct dwarf2_per_cu_data *per_cu;
9a619af0 21552
9c541725 21553 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 21554 cu->objfile);
03dd20cc
DJ
21555
21556 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
21557 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21558 load_full_comp_unit (per_cu, cu->language);
03dd20cc 21559
10b3939b
DJ
21560 target_cu = per_cu->cu;
21561 }
98bfdba5
PA
21562 else if (cu->dies == NULL)
21563 {
21564 /* We're loading full DIEs during partial symbol reading. */
21565 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 21566 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 21567 }
c906108c 21568
f2f0e013 21569 *ref_cu = target_cu;
9c541725 21570 temp_die.sect_off = sect_off;
9a3c8263 21571 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
21572 &temp_die,
21573 to_underlying (sect_off));
5c631832 21574}
10b3939b 21575
5c631832
JK
21576/* Follow reference attribute ATTR of SRC_DIE.
21577 On entry *REF_CU is the CU of SRC_DIE.
21578 On exit *REF_CU is the CU of the result. */
21579
21580static struct die_info *
ff39bb5e 21581follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
21582 struct dwarf2_cu **ref_cu)
21583{
9c541725 21584 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
21585 struct dwarf2_cu *cu = *ref_cu;
21586 struct die_info *die;
21587
9c541725 21588 die = follow_die_offset (sect_off,
36586728
TT
21589 (attr->form == DW_FORM_GNU_ref_alt
21590 || cu->per_cu->is_dwz),
21591 ref_cu);
5c631832
JK
21592 if (!die)
21593 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
21594 "at 0x%x [in module %s]"),
9c541725 21595 to_underlying (sect_off), to_underlying (src_die->sect_off),
4262abfb 21596 objfile_name (cu->objfile));
348e048f 21597
5c631832
JK
21598 return die;
21599}
21600
9c541725 21601/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b
JK
21602 Returned value is intended for DW_OP_call*. Returned
21603 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
21604
21605struct dwarf2_locexpr_baton
9c541725 21606dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
21607 struct dwarf2_per_cu_data *per_cu,
21608 CORE_ADDR (*get_frame_pc) (void *baton),
21609 void *baton)
5c631832 21610{
918dd910 21611 struct dwarf2_cu *cu;
5c631832
JK
21612 struct die_info *die;
21613 struct attribute *attr;
21614 struct dwarf2_locexpr_baton retval;
21615
8cf6f0b1
TT
21616 dw2_setup (per_cu->objfile);
21617
918dd910
JK
21618 if (per_cu->cu == NULL)
21619 load_cu (per_cu);
21620 cu = per_cu->cu;
cc12ce38
DE
21621 if (cu == NULL)
21622 {
21623 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21624 Instead just throw an error, not much else we can do. */
21625 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 21626 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 21627 }
918dd910 21628
9c541725 21629 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832
JK
21630 if (!die)
21631 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 21632 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
21633
21634 attr = dwarf2_attr (die, DW_AT_location, cu);
21635 if (!attr)
21636 {
e103e986
JK
21637 /* DWARF: "If there is no such attribute, then there is no effect.".
21638 DATA is ignored if SIZE is 0. */
5c631832 21639
e103e986 21640 retval.data = NULL;
5c631832
JK
21641 retval.size = 0;
21642 }
8cf6f0b1
TT
21643 else if (attr_form_is_section_offset (attr))
21644 {
21645 struct dwarf2_loclist_baton loclist_baton;
21646 CORE_ADDR pc = (*get_frame_pc) (baton);
21647 size_t size;
21648
21649 fill_in_loclist_baton (cu, &loclist_baton, attr);
21650
21651 retval.data = dwarf2_find_location_expression (&loclist_baton,
21652 &size, pc);
21653 retval.size = size;
21654 }
5c631832
JK
21655 else
21656 {
21657 if (!attr_form_is_block (attr))
21658 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
21659 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9c541725 21660 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
21661
21662 retval.data = DW_BLOCK (attr)->data;
21663 retval.size = DW_BLOCK (attr)->size;
21664 }
21665 retval.per_cu = cu->per_cu;
918dd910 21666
918dd910
JK
21667 age_cached_comp_units ();
21668
5c631832 21669 return retval;
348e048f
DE
21670}
21671
8b9737bf
TT
21672/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
21673 offset. */
21674
21675struct dwarf2_locexpr_baton
21676dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21677 struct dwarf2_per_cu_data *per_cu,
21678 CORE_ADDR (*get_frame_pc) (void *baton),
21679 void *baton)
21680{
9c541725 21681 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 21682
9c541725 21683 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
21684}
21685
b6807d98
TT
21686/* Write a constant of a given type as target-ordered bytes into
21687 OBSTACK. */
21688
21689static const gdb_byte *
21690write_constant_as_bytes (struct obstack *obstack,
21691 enum bfd_endian byte_order,
21692 struct type *type,
21693 ULONGEST value,
21694 LONGEST *len)
21695{
21696 gdb_byte *result;
21697
21698 *len = TYPE_LENGTH (type);
224c3ddb 21699 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
21700 store_unsigned_integer (result, *len, byte_order, value);
21701
21702 return result;
21703}
21704
21705/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
21706 pointer to the constant bytes and set LEN to the length of the
21707 data. If memory is needed, allocate it on OBSTACK. If the DIE
21708 does not have a DW_AT_const_value, return NULL. */
21709
21710const gdb_byte *
9c541725 21711dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
21712 struct dwarf2_per_cu_data *per_cu,
21713 struct obstack *obstack,
21714 LONGEST *len)
21715{
21716 struct dwarf2_cu *cu;
21717 struct die_info *die;
21718 struct attribute *attr;
21719 const gdb_byte *result = NULL;
21720 struct type *type;
21721 LONGEST value;
21722 enum bfd_endian byte_order;
21723
21724 dw2_setup (per_cu->objfile);
21725
21726 if (per_cu->cu == NULL)
21727 load_cu (per_cu);
21728 cu = per_cu->cu;
cc12ce38
DE
21729 if (cu == NULL)
21730 {
21731 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21732 Instead just throw an error, not much else we can do. */
21733 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 21734 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 21735 }
b6807d98 21736
9c541725 21737 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98
TT
21738 if (!die)
21739 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 21740 to_underlying (sect_off), objfile_name (per_cu->objfile));
b6807d98
TT
21741
21742
21743 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21744 if (attr == NULL)
21745 return NULL;
21746
21747 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
21748 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21749
21750 switch (attr->form)
21751 {
21752 case DW_FORM_addr:
21753 case DW_FORM_GNU_addr_index:
21754 {
21755 gdb_byte *tem;
21756
21757 *len = cu->header.addr_size;
224c3ddb 21758 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
21759 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
21760 result = tem;
21761 }
21762 break;
21763 case DW_FORM_string:
21764 case DW_FORM_strp:
21765 case DW_FORM_GNU_str_index:
21766 case DW_FORM_GNU_strp_alt:
21767 /* DW_STRING is already allocated on the objfile obstack, point
21768 directly to it. */
21769 result = (const gdb_byte *) DW_STRING (attr);
21770 *len = strlen (DW_STRING (attr));
21771 break;
21772 case DW_FORM_block1:
21773 case DW_FORM_block2:
21774 case DW_FORM_block4:
21775 case DW_FORM_block:
21776 case DW_FORM_exprloc:
0224619f 21777 case DW_FORM_data16:
b6807d98
TT
21778 result = DW_BLOCK (attr)->data;
21779 *len = DW_BLOCK (attr)->size;
21780 break;
21781
21782 /* The DW_AT_const_value attributes are supposed to carry the
21783 symbol's value "represented as it would be on the target
21784 architecture." By the time we get here, it's already been
21785 converted to host endianness, so we just need to sign- or
21786 zero-extend it as appropriate. */
21787 case DW_FORM_data1:
21788 type = die_type (die, cu);
21789 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
21790 if (result == NULL)
21791 result = write_constant_as_bytes (obstack, byte_order,
21792 type, value, len);
21793 break;
21794 case DW_FORM_data2:
21795 type = die_type (die, cu);
21796 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
21797 if (result == NULL)
21798 result = write_constant_as_bytes (obstack, byte_order,
21799 type, value, len);
21800 break;
21801 case DW_FORM_data4:
21802 type = die_type (die, cu);
21803 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
21804 if (result == NULL)
21805 result = write_constant_as_bytes (obstack, byte_order,
21806 type, value, len);
21807 break;
21808 case DW_FORM_data8:
21809 type = die_type (die, cu);
21810 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
21811 if (result == NULL)
21812 result = write_constant_as_bytes (obstack, byte_order,
21813 type, value, len);
21814 break;
21815
21816 case DW_FORM_sdata:
663c44ac 21817 case DW_FORM_implicit_const:
b6807d98
TT
21818 type = die_type (die, cu);
21819 result = write_constant_as_bytes (obstack, byte_order,
21820 type, DW_SND (attr), len);
21821 break;
21822
21823 case DW_FORM_udata:
21824 type = die_type (die, cu);
21825 result = write_constant_as_bytes (obstack, byte_order,
21826 type, DW_UNSND (attr), len);
21827 break;
21828
21829 default:
21830 complaint (&symfile_complaints,
21831 _("unsupported const value attribute form: '%s'"),
21832 dwarf_form_name (attr->form));
21833 break;
21834 }
21835
21836 return result;
21837}
21838
7942e96e
AA
21839/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
21840 valid type for this die is found. */
21841
21842struct type *
9c541725 21843dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
21844 struct dwarf2_per_cu_data *per_cu)
21845{
21846 struct dwarf2_cu *cu;
21847 struct die_info *die;
21848
21849 dw2_setup (per_cu->objfile);
21850
21851 if (per_cu->cu == NULL)
21852 load_cu (per_cu);
21853 cu = per_cu->cu;
21854 if (!cu)
21855 return NULL;
21856
9c541725 21857 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
21858 if (!die)
21859 return NULL;
21860
21861 return die_type (die, cu);
21862}
21863
8a9b8146
TT
21864/* Return the type of the DIE at DIE_OFFSET in the CU named by
21865 PER_CU. */
21866
21867struct type *
b64f50a1 21868dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
21869 struct dwarf2_per_cu_data *per_cu)
21870{
8a9b8146 21871 dw2_setup (per_cu->objfile);
b64f50a1 21872
9c541725 21873 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 21874 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
21875}
21876
ac9ec31b 21877/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 21878 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
21879 On exit *REF_CU is the CU of the result.
21880 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
21881
21882static struct die_info *
ac9ec31b
DE
21883follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21884 struct dwarf2_cu **ref_cu)
348e048f 21885{
348e048f 21886 struct die_info temp_die;
348e048f
DE
21887 struct dwarf2_cu *sig_cu;
21888 struct die_info *die;
21889
ac9ec31b
DE
21890 /* While it might be nice to assert sig_type->type == NULL here,
21891 we can get here for DW_AT_imported_declaration where we need
21892 the DIE not the type. */
348e048f
DE
21893
21894 /* If necessary, add it to the queue and load its DIEs. */
21895
95554aad 21896 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 21897 read_signatured_type (sig_type);
348e048f 21898
348e048f 21899 sig_cu = sig_type->per_cu.cu;
69d751e3 21900 gdb_assert (sig_cu != NULL);
9c541725
PA
21901 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
21902 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 21903 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 21904 to_underlying (temp_die.sect_off));
348e048f
DE
21905 if (die)
21906 {
796a7ff8
DE
21907 /* For .gdb_index version 7 keep track of included TUs.
21908 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
21909 if (dwarf2_per_objfile->index_table != NULL
21910 && dwarf2_per_objfile->index_table->version <= 7)
21911 {
21912 VEC_safe_push (dwarf2_per_cu_ptr,
21913 (*ref_cu)->per_cu->imported_symtabs,
21914 sig_cu->per_cu);
21915 }
21916
348e048f
DE
21917 *ref_cu = sig_cu;
21918 return die;
21919 }
21920
ac9ec31b
DE
21921 return NULL;
21922}
21923
21924/* Follow signatured type referenced by ATTR in SRC_DIE.
21925 On entry *REF_CU is the CU of SRC_DIE.
21926 On exit *REF_CU is the CU of the result.
21927 The result is the DIE of the type.
21928 If the referenced type cannot be found an error is thrown. */
21929
21930static struct die_info *
ff39bb5e 21931follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
21932 struct dwarf2_cu **ref_cu)
21933{
21934 ULONGEST signature = DW_SIGNATURE (attr);
21935 struct signatured_type *sig_type;
21936 struct die_info *die;
21937
21938 gdb_assert (attr->form == DW_FORM_ref_sig8);
21939
a2ce51a0 21940 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
21941 /* sig_type will be NULL if the signatured type is missing from
21942 the debug info. */
21943 if (sig_type == NULL)
21944 {
21945 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
21946 " from DIE at 0x%x [in module %s]"),
9c541725 21947 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 21948 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
21949 }
21950
21951 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
21952 if (die == NULL)
21953 {
21954 dump_die_for_error (src_die);
21955 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
21956 " from DIE at 0x%x [in module %s]"),
9c541725 21957 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 21958 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
21959 }
21960
21961 return die;
21962}
21963
21964/* Get the type specified by SIGNATURE referenced in DIE/CU,
21965 reading in and processing the type unit if necessary. */
21966
21967static struct type *
21968get_signatured_type (struct die_info *die, ULONGEST signature,
21969 struct dwarf2_cu *cu)
21970{
21971 struct signatured_type *sig_type;
21972 struct dwarf2_cu *type_cu;
21973 struct die_info *type_die;
21974 struct type *type;
21975
a2ce51a0 21976 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
21977 /* sig_type will be NULL if the signatured type is missing from
21978 the debug info. */
21979 if (sig_type == NULL)
21980 {
21981 complaint (&symfile_complaints,
21982 _("Dwarf Error: Cannot find signatured DIE %s referenced"
21983 " from DIE at 0x%x [in module %s]"),
9c541725 21984 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21985 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21986 return build_error_marker_type (cu, die);
21987 }
21988
21989 /* If we already know the type we're done. */
21990 if (sig_type->type != NULL)
21991 return sig_type->type;
21992
21993 type_cu = cu;
21994 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
21995 if (type_die != NULL)
21996 {
21997 /* N.B. We need to call get_die_type to ensure only one type for this DIE
21998 is created. This is important, for example, because for c++ classes
21999 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22000 type = read_type_die (type_die, type_cu);
22001 if (type == NULL)
22002 {
22003 complaint (&symfile_complaints,
22004 _("Dwarf Error: Cannot build signatured type %s"
22005 " referenced from DIE at 0x%x [in module %s]"),
9c541725 22006 hex_string (signature), to_underlying (die->sect_off),
4262abfb 22007 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22008 type = build_error_marker_type (cu, die);
22009 }
22010 }
22011 else
22012 {
22013 complaint (&symfile_complaints,
22014 _("Dwarf Error: Problem reading signatured DIE %s referenced"
22015 " from DIE at 0x%x [in module %s]"),
9c541725 22016 hex_string (signature), to_underlying (die->sect_off),
4262abfb 22017 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22018 type = build_error_marker_type (cu, die);
22019 }
22020 sig_type->type = type;
22021
22022 return type;
22023}
22024
22025/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22026 reading in and processing the type unit if necessary. */
22027
22028static struct type *
ff39bb5e 22029get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22030 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22031{
22032 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 22033 if (attr_form_is_ref (attr))
ac9ec31b
DE
22034 {
22035 struct dwarf2_cu *type_cu = cu;
22036 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22037
22038 return read_type_die (type_die, type_cu);
22039 }
22040 else if (attr->form == DW_FORM_ref_sig8)
22041 {
22042 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22043 }
22044 else
22045 {
22046 complaint (&symfile_complaints,
22047 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22048 " at 0x%x [in module %s]"),
9c541725 22049 dwarf_form_name (attr->form), to_underlying (die->sect_off),
4262abfb 22050 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22051 return build_error_marker_type (cu, die);
22052 }
348e048f
DE
22053}
22054
e5fe5e75 22055/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22056
22057static void
e5fe5e75 22058load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 22059{
52dc124a 22060 struct signatured_type *sig_type;
348e048f 22061
f4dc4d17
DE
22062 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22063 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
22064
6721b2ec
DE
22065 /* We have the per_cu, but we need the signatured_type.
22066 Fortunately this is an easy translation. */
22067 gdb_assert (per_cu->is_debug_types);
22068 sig_type = (struct signatured_type *) per_cu;
348e048f 22069
6721b2ec 22070 gdb_assert (per_cu->cu == NULL);
348e048f 22071
52dc124a 22072 read_signatured_type (sig_type);
348e048f 22073
6721b2ec 22074 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22075}
22076
dee91e82
DE
22077/* die_reader_func for read_signatured_type.
22078 This is identical to load_full_comp_unit_reader,
22079 but is kept separate for now. */
348e048f
DE
22080
22081static void
dee91e82 22082read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 22083 const gdb_byte *info_ptr,
dee91e82
DE
22084 struct die_info *comp_unit_die,
22085 int has_children,
22086 void *data)
348e048f 22087{
dee91e82 22088 struct dwarf2_cu *cu = reader->cu;
348e048f 22089
dee91e82
DE
22090 gdb_assert (cu->die_hash == NULL);
22091 cu->die_hash =
22092 htab_create_alloc_ex (cu->header.length / 12,
22093 die_hash,
22094 die_eq,
22095 NULL,
22096 &cu->comp_unit_obstack,
22097 hashtab_obstack_allocate,
22098 dummy_obstack_deallocate);
348e048f 22099
dee91e82
DE
22100 if (has_children)
22101 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
22102 &info_ptr, comp_unit_die);
22103 cu->dies = comp_unit_die;
22104 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
22105
22106 /* We try not to read any attributes in this function, because not
9cdd5dbd 22107 all CUs needed for references have been loaded yet, and symbol
348e048f 22108 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
22109 or we won't be able to build types correctly.
22110 Similarly, if we do not read the producer, we can not apply
22111 producer-specific interpretation. */
95554aad 22112 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 22113}
348e048f 22114
3019eac3
DE
22115/* Read in a signatured type and build its CU and DIEs.
22116 If the type is a stub for the real type in a DWO file,
22117 read in the real type from the DWO file as well. */
dee91e82
DE
22118
22119static void
22120read_signatured_type (struct signatured_type *sig_type)
22121{
22122 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22123
3019eac3 22124 gdb_assert (per_cu->is_debug_types);
dee91e82 22125 gdb_assert (per_cu->cu == NULL);
348e048f 22126
f4dc4d17
DE
22127 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
22128 read_signatured_type_reader, NULL);
7ee85ab1 22129 sig_type->per_cu.tu_read = 1;
c906108c
SS
22130}
22131
c906108c
SS
22132/* Decode simple location descriptions.
22133 Given a pointer to a dwarf block that defines a location, compute
22134 the location and return the value.
22135
4cecd739
DJ
22136 NOTE drow/2003-11-18: This function is called in two situations
22137 now: for the address of static or global variables (partial symbols
22138 only) and for offsets into structures which are expected to be
22139 (more or less) constant. The partial symbol case should go away,
22140 and only the constant case should remain. That will let this
22141 function complain more accurately. A few special modes are allowed
22142 without complaint for global variables (for instance, global
22143 register values and thread-local values).
c906108c
SS
22144
22145 A location description containing no operations indicates that the
4cecd739 22146 object is optimized out. The return value is 0 for that case.
6b992462
DJ
22147 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22148 callers will only want a very basic result and this can become a
21ae7a4d
JK
22149 complaint.
22150
22151 Note that stack[0] is unused except as a default error return. */
c906108c
SS
22152
22153static CORE_ADDR
e7c27a73 22154decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 22155{
e7c27a73 22156 struct objfile *objfile = cu->objfile;
56eb65bd
SP
22157 size_t i;
22158 size_t size = blk->size;
d521ce57 22159 const gdb_byte *data = blk->data;
21ae7a4d
JK
22160 CORE_ADDR stack[64];
22161 int stacki;
22162 unsigned int bytes_read, unsnd;
22163 gdb_byte op;
c906108c 22164
21ae7a4d
JK
22165 i = 0;
22166 stacki = 0;
22167 stack[stacki] = 0;
22168 stack[++stacki] = 0;
22169
22170 while (i < size)
22171 {
22172 op = data[i++];
22173 switch (op)
22174 {
22175 case DW_OP_lit0:
22176 case DW_OP_lit1:
22177 case DW_OP_lit2:
22178 case DW_OP_lit3:
22179 case DW_OP_lit4:
22180 case DW_OP_lit5:
22181 case DW_OP_lit6:
22182 case DW_OP_lit7:
22183 case DW_OP_lit8:
22184 case DW_OP_lit9:
22185 case DW_OP_lit10:
22186 case DW_OP_lit11:
22187 case DW_OP_lit12:
22188 case DW_OP_lit13:
22189 case DW_OP_lit14:
22190 case DW_OP_lit15:
22191 case DW_OP_lit16:
22192 case DW_OP_lit17:
22193 case DW_OP_lit18:
22194 case DW_OP_lit19:
22195 case DW_OP_lit20:
22196 case DW_OP_lit21:
22197 case DW_OP_lit22:
22198 case DW_OP_lit23:
22199 case DW_OP_lit24:
22200 case DW_OP_lit25:
22201 case DW_OP_lit26:
22202 case DW_OP_lit27:
22203 case DW_OP_lit28:
22204 case DW_OP_lit29:
22205 case DW_OP_lit30:
22206 case DW_OP_lit31:
22207 stack[++stacki] = op - DW_OP_lit0;
22208 break;
f1bea926 22209
21ae7a4d
JK
22210 case DW_OP_reg0:
22211 case DW_OP_reg1:
22212 case DW_OP_reg2:
22213 case DW_OP_reg3:
22214 case DW_OP_reg4:
22215 case DW_OP_reg5:
22216 case DW_OP_reg6:
22217 case DW_OP_reg7:
22218 case DW_OP_reg8:
22219 case DW_OP_reg9:
22220 case DW_OP_reg10:
22221 case DW_OP_reg11:
22222 case DW_OP_reg12:
22223 case DW_OP_reg13:
22224 case DW_OP_reg14:
22225 case DW_OP_reg15:
22226 case DW_OP_reg16:
22227 case DW_OP_reg17:
22228 case DW_OP_reg18:
22229 case DW_OP_reg19:
22230 case DW_OP_reg20:
22231 case DW_OP_reg21:
22232 case DW_OP_reg22:
22233 case DW_OP_reg23:
22234 case DW_OP_reg24:
22235 case DW_OP_reg25:
22236 case DW_OP_reg26:
22237 case DW_OP_reg27:
22238 case DW_OP_reg28:
22239 case DW_OP_reg29:
22240 case DW_OP_reg30:
22241 case DW_OP_reg31:
22242 stack[++stacki] = op - DW_OP_reg0;
22243 if (i < size)
22244 dwarf2_complex_location_expr_complaint ();
22245 break;
c906108c 22246
21ae7a4d
JK
22247 case DW_OP_regx:
22248 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22249 i += bytes_read;
22250 stack[++stacki] = unsnd;
22251 if (i < size)
22252 dwarf2_complex_location_expr_complaint ();
22253 break;
c906108c 22254
21ae7a4d
JK
22255 case DW_OP_addr:
22256 stack[++stacki] = read_address (objfile->obfd, &data[i],
22257 cu, &bytes_read);
22258 i += bytes_read;
22259 break;
d53d4ac5 22260
21ae7a4d
JK
22261 case DW_OP_const1u:
22262 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22263 i += 1;
22264 break;
22265
22266 case DW_OP_const1s:
22267 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22268 i += 1;
22269 break;
22270
22271 case DW_OP_const2u:
22272 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22273 i += 2;
22274 break;
22275
22276 case DW_OP_const2s:
22277 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22278 i += 2;
22279 break;
d53d4ac5 22280
21ae7a4d
JK
22281 case DW_OP_const4u:
22282 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22283 i += 4;
22284 break;
22285
22286 case DW_OP_const4s:
22287 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22288 i += 4;
22289 break;
22290
585861ea
JK
22291 case DW_OP_const8u:
22292 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22293 i += 8;
22294 break;
22295
21ae7a4d
JK
22296 case DW_OP_constu:
22297 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22298 &bytes_read);
22299 i += bytes_read;
22300 break;
22301
22302 case DW_OP_consts:
22303 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22304 i += bytes_read;
22305 break;
22306
22307 case DW_OP_dup:
22308 stack[stacki + 1] = stack[stacki];
22309 stacki++;
22310 break;
22311
22312 case DW_OP_plus:
22313 stack[stacki - 1] += stack[stacki];
22314 stacki--;
22315 break;
22316
22317 case DW_OP_plus_uconst:
22318 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22319 &bytes_read);
22320 i += bytes_read;
22321 break;
22322
22323 case DW_OP_minus:
22324 stack[stacki - 1] -= stack[stacki];
22325 stacki--;
22326 break;
22327
22328 case DW_OP_deref:
22329 /* If we're not the last op, then we definitely can't encode
22330 this using GDB's address_class enum. This is valid for partial
22331 global symbols, although the variable's address will be bogus
22332 in the psymtab. */
22333 if (i < size)
22334 dwarf2_complex_location_expr_complaint ();
22335 break;
22336
22337 case DW_OP_GNU_push_tls_address:
4aa4e28b 22338 case DW_OP_form_tls_address:
21ae7a4d
JK
22339 /* The top of the stack has the offset from the beginning
22340 of the thread control block at which the variable is located. */
22341 /* Nothing should follow this operator, so the top of stack would
22342 be returned. */
22343 /* This is valid for partial global symbols, but the variable's
585861ea
JK
22344 address will be bogus in the psymtab. Make it always at least
22345 non-zero to not look as a variable garbage collected by linker
22346 which have DW_OP_addr 0. */
21ae7a4d
JK
22347 if (i < size)
22348 dwarf2_complex_location_expr_complaint ();
585861ea 22349 stack[stacki]++;
21ae7a4d
JK
22350 break;
22351
22352 case DW_OP_GNU_uninit:
22353 break;
22354
3019eac3 22355 case DW_OP_GNU_addr_index:
49f6c839 22356 case DW_OP_GNU_const_index:
3019eac3
DE
22357 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22358 &bytes_read);
22359 i += bytes_read;
22360 break;
22361
21ae7a4d
JK
22362 default:
22363 {
f39c6ffd 22364 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
22365
22366 if (name)
22367 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
22368 name);
22369 else
22370 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
22371 op);
22372 }
22373
22374 return (stack[stacki]);
d53d4ac5 22375 }
3c6e0cb3 22376
21ae7a4d
JK
22377 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22378 outside of the allocated space. Also enforce minimum>0. */
22379 if (stacki >= ARRAY_SIZE (stack) - 1)
22380 {
22381 complaint (&symfile_complaints,
22382 _("location description stack overflow"));
22383 return 0;
22384 }
22385
22386 if (stacki <= 0)
22387 {
22388 complaint (&symfile_complaints,
22389 _("location description stack underflow"));
22390 return 0;
22391 }
22392 }
22393 return (stack[stacki]);
c906108c
SS
22394}
22395
22396/* memory allocation interface */
22397
c906108c 22398static struct dwarf_block *
7b5a2f43 22399dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 22400{
8d749320 22401 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
22402}
22403
c906108c 22404static struct die_info *
b60c80d6 22405dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
22406{
22407 struct die_info *die;
b60c80d6
DJ
22408 size_t size = sizeof (struct die_info);
22409
22410 if (num_attrs > 1)
22411 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 22412
b60c80d6 22413 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
22414 memset (die, 0, sizeof (struct die_info));
22415 return (die);
22416}
2e276125
JB
22417
22418\f
22419/* Macro support. */
22420
233d95b5
JK
22421/* Return file name relative to the compilation directory of file number I in
22422 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 22423 responsible for freeing it. */
233d95b5 22424
2e276125 22425static char *
233d95b5 22426file_file_name (int file, struct line_header *lh)
2e276125 22427{
6a83a1e6
EZ
22428 /* Is the file number a valid index into the line header's file name
22429 table? Remember that file numbers start with one, not zero. */
fff8551c 22430 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 22431 {
8c43009f 22432 const file_entry &fe = lh->file_names[file - 1];
6e70227d 22433
8c43009f
PA
22434 if (!IS_ABSOLUTE_PATH (fe.name))
22435 {
22436 const char *dir = fe.include_dir (lh);
22437 if (dir != NULL)
22438 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
22439 }
22440 return xstrdup (fe.name);
6a83a1e6 22441 }
2e276125
JB
22442 else
22443 {
6a83a1e6
EZ
22444 /* The compiler produced a bogus file number. We can at least
22445 record the macro definitions made in the file, even if we
22446 won't be able to find the file by name. */
22447 char fake_name[80];
9a619af0 22448
8c042590
PM
22449 xsnprintf (fake_name, sizeof (fake_name),
22450 "<bad macro file number %d>", file);
2e276125 22451
6e70227d 22452 complaint (&symfile_complaints,
6a83a1e6
EZ
22453 _("bad file number in macro information (%d)"),
22454 file);
2e276125 22455
6a83a1e6 22456 return xstrdup (fake_name);
2e276125
JB
22457 }
22458}
22459
233d95b5
JK
22460/* Return the full name of file number I in *LH's file name table.
22461 Use COMP_DIR as the name of the current directory of the
22462 compilation. The result is allocated using xmalloc; the caller is
22463 responsible for freeing it. */
22464static char *
22465file_full_name (int file, struct line_header *lh, const char *comp_dir)
22466{
22467 /* Is the file number a valid index into the line header's file name
22468 table? Remember that file numbers start with one, not zero. */
fff8551c 22469 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
22470 {
22471 char *relative = file_file_name (file, lh);
22472
22473 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
22474 return relative;
b36cec19
PA
22475 return reconcat (relative, comp_dir, SLASH_STRING,
22476 relative, (char *) NULL);
233d95b5
JK
22477 }
22478 else
22479 return file_file_name (file, lh);
22480}
22481
2e276125
JB
22482
22483static struct macro_source_file *
22484macro_start_file (int file, int line,
22485 struct macro_source_file *current_file,
43f3e411 22486 struct line_header *lh)
2e276125 22487{
233d95b5
JK
22488 /* File name relative to the compilation directory of this source file. */
22489 char *file_name = file_file_name (file, lh);
2e276125 22490
2e276125 22491 if (! current_file)
abc9d0dc 22492 {
fc474241
DE
22493 /* Note: We don't create a macro table for this compilation unit
22494 at all until we actually get a filename. */
43f3e411 22495 struct macro_table *macro_table = get_macro_table ();
fc474241 22496
abc9d0dc
TT
22497 /* If we have no current file, then this must be the start_file
22498 directive for the compilation unit's main source file. */
fc474241
DE
22499 current_file = macro_set_main (macro_table, file_name);
22500 macro_define_special (macro_table);
abc9d0dc 22501 }
2e276125 22502 else
233d95b5 22503 current_file = macro_include (current_file, line, file_name);
2e276125 22504
233d95b5 22505 xfree (file_name);
6e70227d 22506
2e276125
JB
22507 return current_file;
22508}
22509
2e276125
JB
22510static const char *
22511consume_improper_spaces (const char *p, const char *body)
22512{
22513 if (*p == ' ')
22514 {
4d3c2250 22515 complaint (&symfile_complaints,
3e43a32a
MS
22516 _("macro definition contains spaces "
22517 "in formal argument list:\n`%s'"),
4d3c2250 22518 body);
2e276125
JB
22519
22520 while (*p == ' ')
22521 p++;
22522 }
22523
22524 return p;
22525}
22526
22527
22528static void
22529parse_macro_definition (struct macro_source_file *file, int line,
22530 const char *body)
22531{
22532 const char *p;
22533
22534 /* The body string takes one of two forms. For object-like macro
22535 definitions, it should be:
22536
22537 <macro name> " " <definition>
22538
22539 For function-like macro definitions, it should be:
22540
22541 <macro name> "() " <definition>
22542 or
22543 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
22544
22545 Spaces may appear only where explicitly indicated, and in the
22546 <definition>.
22547
22548 The Dwarf 2 spec says that an object-like macro's name is always
22549 followed by a space, but versions of GCC around March 2002 omit
6e70227d 22550 the space when the macro's definition is the empty string.
2e276125
JB
22551
22552 The Dwarf 2 spec says that there should be no spaces between the
22553 formal arguments in a function-like macro's formal argument list,
22554 but versions of GCC around March 2002 include spaces after the
22555 commas. */
22556
22557
22558 /* Find the extent of the macro name. The macro name is terminated
22559 by either a space or null character (for an object-like macro) or
22560 an opening paren (for a function-like macro). */
22561 for (p = body; *p; p++)
22562 if (*p == ' ' || *p == '(')
22563 break;
22564
22565 if (*p == ' ' || *p == '\0')
22566 {
22567 /* It's an object-like macro. */
22568 int name_len = p - body;
3f8a7804 22569 char *name = savestring (body, name_len);
2e276125
JB
22570 const char *replacement;
22571
22572 if (*p == ' ')
22573 replacement = body + name_len + 1;
22574 else
22575 {
4d3c2250 22576 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22577 replacement = body + name_len;
22578 }
6e70227d 22579
2e276125
JB
22580 macro_define_object (file, line, name, replacement);
22581
22582 xfree (name);
22583 }
22584 else if (*p == '(')
22585 {
22586 /* It's a function-like macro. */
3f8a7804 22587 char *name = savestring (body, p - body);
2e276125
JB
22588 int argc = 0;
22589 int argv_size = 1;
8d749320 22590 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
22591
22592 p++;
22593
22594 p = consume_improper_spaces (p, body);
22595
22596 /* Parse the formal argument list. */
22597 while (*p && *p != ')')
22598 {
22599 /* Find the extent of the current argument name. */
22600 const char *arg_start = p;
22601
22602 while (*p && *p != ',' && *p != ')' && *p != ' ')
22603 p++;
22604
22605 if (! *p || p == arg_start)
4d3c2250 22606 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22607 else
22608 {
22609 /* Make sure argv has room for the new argument. */
22610 if (argc >= argv_size)
22611 {
22612 argv_size *= 2;
224c3ddb 22613 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
22614 }
22615
3f8a7804 22616 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
22617 }
22618
22619 p = consume_improper_spaces (p, body);
22620
22621 /* Consume the comma, if present. */
22622 if (*p == ',')
22623 {
22624 p++;
22625
22626 p = consume_improper_spaces (p, body);
22627 }
22628 }
22629
22630 if (*p == ')')
22631 {
22632 p++;
22633
22634 if (*p == ' ')
22635 /* Perfectly formed definition, no complaints. */
22636 macro_define_function (file, line, name,
6e70227d 22637 argc, (const char **) argv,
2e276125
JB
22638 p + 1);
22639 else if (*p == '\0')
22640 {
22641 /* Complain, but do define it. */
4d3c2250 22642 dwarf2_macro_malformed_definition_complaint (body);
2e276125 22643 macro_define_function (file, line, name,
6e70227d 22644 argc, (const char **) argv,
2e276125
JB
22645 p);
22646 }
22647 else
22648 /* Just complain. */
4d3c2250 22649 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22650 }
22651 else
22652 /* Just complain. */
4d3c2250 22653 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22654
22655 xfree (name);
22656 {
22657 int i;
22658
22659 for (i = 0; i < argc; i++)
22660 xfree (argv[i]);
22661 }
22662 xfree (argv);
22663 }
22664 else
4d3c2250 22665 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22666}
22667
cf2c3c16
TT
22668/* Skip some bytes from BYTES according to the form given in FORM.
22669 Returns the new pointer. */
2e276125 22670
d521ce57
TT
22671static const gdb_byte *
22672skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
22673 enum dwarf_form form,
22674 unsigned int offset_size,
22675 struct dwarf2_section_info *section)
2e276125 22676{
cf2c3c16 22677 unsigned int bytes_read;
2e276125 22678
cf2c3c16 22679 switch (form)
2e276125 22680 {
cf2c3c16
TT
22681 case DW_FORM_data1:
22682 case DW_FORM_flag:
22683 ++bytes;
22684 break;
22685
22686 case DW_FORM_data2:
22687 bytes += 2;
22688 break;
22689
22690 case DW_FORM_data4:
22691 bytes += 4;
22692 break;
22693
22694 case DW_FORM_data8:
22695 bytes += 8;
22696 break;
22697
0224619f
JK
22698 case DW_FORM_data16:
22699 bytes += 16;
22700 break;
22701
cf2c3c16
TT
22702 case DW_FORM_string:
22703 read_direct_string (abfd, bytes, &bytes_read);
22704 bytes += bytes_read;
22705 break;
22706
22707 case DW_FORM_sec_offset:
22708 case DW_FORM_strp:
36586728 22709 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
22710 bytes += offset_size;
22711 break;
22712
22713 case DW_FORM_block:
22714 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
22715 bytes += bytes_read;
22716 break;
22717
22718 case DW_FORM_block1:
22719 bytes += 1 + read_1_byte (abfd, bytes);
22720 break;
22721 case DW_FORM_block2:
22722 bytes += 2 + read_2_bytes (abfd, bytes);
22723 break;
22724 case DW_FORM_block4:
22725 bytes += 4 + read_4_bytes (abfd, bytes);
22726 break;
22727
22728 case DW_FORM_sdata:
22729 case DW_FORM_udata:
3019eac3
DE
22730 case DW_FORM_GNU_addr_index:
22731 case DW_FORM_GNU_str_index:
d521ce57 22732 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
22733 if (bytes == NULL)
22734 {
22735 dwarf2_section_buffer_overflow_complaint (section);
22736 return NULL;
22737 }
cf2c3c16
TT
22738 break;
22739
663c44ac
JK
22740 case DW_FORM_implicit_const:
22741 break;
22742
cf2c3c16
TT
22743 default:
22744 {
22745 complain:
22746 complaint (&symfile_complaints,
22747 _("invalid form 0x%x in `%s'"),
a32a8923 22748 form, get_section_name (section));
cf2c3c16
TT
22749 return NULL;
22750 }
2e276125
JB
22751 }
22752
cf2c3c16
TT
22753 return bytes;
22754}
757a13d0 22755
cf2c3c16
TT
22756/* A helper for dwarf_decode_macros that handles skipping an unknown
22757 opcode. Returns an updated pointer to the macro data buffer; or,
22758 on error, issues a complaint and returns NULL. */
757a13d0 22759
d521ce57 22760static const gdb_byte *
cf2c3c16 22761skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
22762 const gdb_byte **opcode_definitions,
22763 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
22764 bfd *abfd,
22765 unsigned int offset_size,
22766 struct dwarf2_section_info *section)
22767{
22768 unsigned int bytes_read, i;
22769 unsigned long arg;
d521ce57 22770 const gdb_byte *defn;
2e276125 22771
cf2c3c16 22772 if (opcode_definitions[opcode] == NULL)
2e276125 22773 {
cf2c3c16
TT
22774 complaint (&symfile_complaints,
22775 _("unrecognized DW_MACFINO opcode 0x%x"),
22776 opcode);
22777 return NULL;
22778 }
2e276125 22779
cf2c3c16
TT
22780 defn = opcode_definitions[opcode];
22781 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
22782 defn += bytes_read;
2e276125 22783
cf2c3c16
TT
22784 for (i = 0; i < arg; ++i)
22785 {
aead7601
SM
22786 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
22787 (enum dwarf_form) defn[i], offset_size,
f664829e 22788 section);
cf2c3c16
TT
22789 if (mac_ptr == NULL)
22790 {
22791 /* skip_form_bytes already issued the complaint. */
22792 return NULL;
22793 }
22794 }
757a13d0 22795
cf2c3c16
TT
22796 return mac_ptr;
22797}
757a13d0 22798
cf2c3c16
TT
22799/* A helper function which parses the header of a macro section.
22800 If the macro section is the extended (for now called "GNU") type,
22801 then this updates *OFFSET_SIZE. Returns a pointer to just after
22802 the header, or issues a complaint and returns NULL on error. */
757a13d0 22803
d521ce57
TT
22804static const gdb_byte *
22805dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 22806 bfd *abfd,
d521ce57 22807 const gdb_byte *mac_ptr,
cf2c3c16
TT
22808 unsigned int *offset_size,
22809 int section_is_gnu)
22810{
22811 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 22812
cf2c3c16
TT
22813 if (section_is_gnu)
22814 {
22815 unsigned int version, flags;
757a13d0 22816
cf2c3c16 22817 version = read_2_bytes (abfd, mac_ptr);
0af92d60 22818 if (version != 4 && version != 5)
cf2c3c16
TT
22819 {
22820 complaint (&symfile_complaints,
22821 _("unrecognized version `%d' in .debug_macro section"),
22822 version);
22823 return NULL;
22824 }
22825 mac_ptr += 2;
757a13d0 22826
cf2c3c16
TT
22827 flags = read_1_byte (abfd, mac_ptr);
22828 ++mac_ptr;
22829 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 22830
cf2c3c16
TT
22831 if ((flags & 2) != 0)
22832 /* We don't need the line table offset. */
22833 mac_ptr += *offset_size;
757a13d0 22834
cf2c3c16
TT
22835 /* Vendor opcode descriptions. */
22836 if ((flags & 4) != 0)
22837 {
22838 unsigned int i, count;
757a13d0 22839
cf2c3c16
TT
22840 count = read_1_byte (abfd, mac_ptr);
22841 ++mac_ptr;
22842 for (i = 0; i < count; ++i)
22843 {
22844 unsigned int opcode, bytes_read;
22845 unsigned long arg;
22846
22847 opcode = read_1_byte (abfd, mac_ptr);
22848 ++mac_ptr;
22849 opcode_definitions[opcode] = mac_ptr;
22850 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22851 mac_ptr += bytes_read;
22852 mac_ptr += arg;
22853 }
757a13d0 22854 }
cf2c3c16 22855 }
757a13d0 22856
cf2c3c16
TT
22857 return mac_ptr;
22858}
757a13d0 22859
cf2c3c16 22860/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 22861 including DW_MACRO_import. */
cf2c3c16
TT
22862
22863static void
d521ce57
TT
22864dwarf_decode_macro_bytes (bfd *abfd,
22865 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 22866 struct macro_source_file *current_file,
43f3e411 22867 struct line_header *lh,
cf2c3c16 22868 struct dwarf2_section_info *section,
36586728 22869 int section_is_gnu, int section_is_dwz,
cf2c3c16 22870 unsigned int offset_size,
8fc3fc34 22871 htab_t include_hash)
cf2c3c16 22872{
4d663531 22873 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
22874 enum dwarf_macro_record_type macinfo_type;
22875 int at_commandline;
d521ce57 22876 const gdb_byte *opcode_definitions[256];
757a13d0 22877
cf2c3c16
TT
22878 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22879 &offset_size, section_is_gnu);
22880 if (mac_ptr == NULL)
22881 {
22882 /* We already issued a complaint. */
22883 return;
22884 }
757a13d0
JK
22885
22886 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
22887 GDB is still reading the definitions from command line. First
22888 DW_MACINFO_start_file will need to be ignored as it was already executed
22889 to create CURRENT_FILE for the main source holding also the command line
22890 definitions. On first met DW_MACINFO_start_file this flag is reset to
22891 normally execute all the remaining DW_MACINFO_start_file macinfos. */
22892
22893 at_commandline = 1;
22894
22895 do
22896 {
22897 /* Do we at least have room for a macinfo type byte? */
22898 if (mac_ptr >= mac_end)
22899 {
f664829e 22900 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
22901 break;
22902 }
22903
aead7601 22904 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
22905 mac_ptr++;
22906
cf2c3c16
TT
22907 /* Note that we rely on the fact that the corresponding GNU and
22908 DWARF constants are the same. */
757a13d0
JK
22909 switch (macinfo_type)
22910 {
22911 /* A zero macinfo type indicates the end of the macro
22912 information. */
22913 case 0:
22914 break;
2e276125 22915
0af92d60
JK
22916 case DW_MACRO_define:
22917 case DW_MACRO_undef:
22918 case DW_MACRO_define_strp:
22919 case DW_MACRO_undef_strp:
22920 case DW_MACRO_define_sup:
22921 case DW_MACRO_undef_sup:
2e276125 22922 {
891d2f0b 22923 unsigned int bytes_read;
2e276125 22924 int line;
d521ce57 22925 const char *body;
cf2c3c16 22926 int is_define;
2e276125 22927
cf2c3c16
TT
22928 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22929 mac_ptr += bytes_read;
22930
0af92d60
JK
22931 if (macinfo_type == DW_MACRO_define
22932 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
22933 {
22934 body = read_direct_string (abfd, mac_ptr, &bytes_read);
22935 mac_ptr += bytes_read;
22936 }
22937 else
22938 {
22939 LONGEST str_offset;
22940
22941 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
22942 mac_ptr += offset_size;
2e276125 22943
0af92d60
JK
22944 if (macinfo_type == DW_MACRO_define_sup
22945 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 22946 || section_is_dwz)
36586728
TT
22947 {
22948 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22949
22950 body = read_indirect_string_from_dwz (dwz, str_offset);
22951 }
22952 else
22953 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
22954 }
22955
0af92d60
JK
22956 is_define = (macinfo_type == DW_MACRO_define
22957 || macinfo_type == DW_MACRO_define_strp
22958 || macinfo_type == DW_MACRO_define_sup);
2e276125 22959 if (! current_file)
757a13d0
JK
22960 {
22961 /* DWARF violation as no main source is present. */
22962 complaint (&symfile_complaints,
22963 _("debug info with no main source gives macro %s "
22964 "on line %d: %s"),
cf2c3c16
TT
22965 is_define ? _("definition") : _("undefinition"),
22966 line, body);
757a13d0
JK
22967 break;
22968 }
3e43a32a
MS
22969 if ((line == 0 && !at_commandline)
22970 || (line != 0 && at_commandline))
4d3c2250 22971 complaint (&symfile_complaints,
757a13d0
JK
22972 _("debug info gives %s macro %s with %s line %d: %s"),
22973 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 22974 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
22975 line == 0 ? _("zero") : _("non-zero"), line, body);
22976
cf2c3c16 22977 if (is_define)
757a13d0 22978 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
22979 else
22980 {
0af92d60
JK
22981 gdb_assert (macinfo_type == DW_MACRO_undef
22982 || macinfo_type == DW_MACRO_undef_strp
22983 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
22984 macro_undef (current_file, line, body);
22985 }
2e276125
JB
22986 }
22987 break;
22988
0af92d60 22989 case DW_MACRO_start_file:
2e276125 22990 {
891d2f0b 22991 unsigned int bytes_read;
2e276125
JB
22992 int line, file;
22993
22994 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22995 mac_ptr += bytes_read;
22996 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22997 mac_ptr += bytes_read;
22998
3e43a32a
MS
22999 if ((line == 0 && !at_commandline)
23000 || (line != 0 && at_commandline))
757a13d0
JK
23001 complaint (&symfile_complaints,
23002 _("debug info gives source %d included "
23003 "from %s at %s line %d"),
23004 file, at_commandline ? _("command-line") : _("file"),
23005 line == 0 ? _("zero") : _("non-zero"), line);
23006
23007 if (at_commandline)
23008 {
0af92d60 23009 /* This DW_MACRO_start_file was executed in the
cf2c3c16 23010 pass one. */
757a13d0
JK
23011 at_commandline = 0;
23012 }
23013 else
43f3e411 23014 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
23015 }
23016 break;
23017
0af92d60 23018 case DW_MACRO_end_file:
2e276125 23019 if (! current_file)
4d3c2250 23020 complaint (&symfile_complaints,
3e43a32a
MS
23021 _("macro debug info has an unmatched "
23022 "`close_file' directive"));
2e276125
JB
23023 else
23024 {
23025 current_file = current_file->included_by;
23026 if (! current_file)
23027 {
cf2c3c16 23028 enum dwarf_macro_record_type next_type;
2e276125
JB
23029
23030 /* GCC circa March 2002 doesn't produce the zero
23031 type byte marking the end of the compilation
23032 unit. Complain if it's not there, but exit no
23033 matter what. */
23034
23035 /* Do we at least have room for a macinfo type byte? */
23036 if (mac_ptr >= mac_end)
23037 {
f664829e 23038 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
23039 return;
23040 }
23041
23042 /* We don't increment mac_ptr here, so this is just
23043 a look-ahead. */
aead7601
SM
23044 next_type
23045 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23046 mac_ptr);
2e276125 23047 if (next_type != 0)
4d3c2250 23048 complaint (&symfile_complaints,
3e43a32a
MS
23049 _("no terminating 0-type entry for "
23050 "macros in `.debug_macinfo' section"));
2e276125
JB
23051
23052 return;
23053 }
23054 }
23055 break;
23056
0af92d60
JK
23057 case DW_MACRO_import:
23058 case DW_MACRO_import_sup:
cf2c3c16
TT
23059 {
23060 LONGEST offset;
8fc3fc34 23061 void **slot;
a036ba48
TT
23062 bfd *include_bfd = abfd;
23063 struct dwarf2_section_info *include_section = section;
d521ce57 23064 const gdb_byte *include_mac_end = mac_end;
a036ba48 23065 int is_dwz = section_is_dwz;
d521ce57 23066 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
23067
23068 offset = read_offset_1 (abfd, mac_ptr, offset_size);
23069 mac_ptr += offset_size;
23070
0af92d60 23071 if (macinfo_type == DW_MACRO_import_sup)
a036ba48
TT
23072 {
23073 struct dwz_file *dwz = dwarf2_get_dwz_file ();
23074
4d663531 23075 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 23076
a036ba48 23077 include_section = &dwz->macro;
a32a8923 23078 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
23079 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23080 is_dwz = 1;
23081 }
23082
23083 new_mac_ptr = include_section->buffer + offset;
23084 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23085
8fc3fc34
TT
23086 if (*slot != NULL)
23087 {
23088 /* This has actually happened; see
23089 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
23090 complaint (&symfile_complaints,
0af92d60 23091 _("recursive DW_MACRO_import in "
8fc3fc34
TT
23092 ".debug_macro section"));
23093 }
23094 else
23095 {
d521ce57 23096 *slot = (void *) new_mac_ptr;
36586728 23097
a036ba48 23098 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 23099 include_mac_end, current_file, lh,
36586728 23100 section, section_is_gnu, is_dwz,
4d663531 23101 offset_size, include_hash);
8fc3fc34 23102
d521ce57 23103 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 23104 }
cf2c3c16
TT
23105 }
23106 break;
23107
2e276125 23108 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
23109 if (!section_is_gnu)
23110 {
23111 unsigned int bytes_read;
2e276125 23112
ac298888
TT
23113 /* This reads the constant, but since we don't recognize
23114 any vendor extensions, we ignore it. */
23115 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
23116 mac_ptr += bytes_read;
23117 read_direct_string (abfd, mac_ptr, &bytes_read);
23118 mac_ptr += bytes_read;
2e276125 23119
cf2c3c16
TT
23120 /* We don't recognize any vendor extensions. */
23121 break;
23122 }
23123 /* FALLTHROUGH */
23124
23125 default:
23126 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 23127 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
23128 section);
23129 if (mac_ptr == NULL)
23130 return;
23131 break;
2e276125 23132 }
757a13d0 23133 } while (macinfo_type != 0);
2e276125 23134}
8e19ed76 23135
cf2c3c16 23136static void
09262596 23137dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 23138 int section_is_gnu)
cf2c3c16 23139{
bb5ed363 23140 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
23141 struct line_header *lh = cu->line_header;
23142 bfd *abfd;
d521ce57 23143 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
23144 struct macro_source_file *current_file = 0;
23145 enum dwarf_macro_record_type macinfo_type;
23146 unsigned int offset_size = cu->header.offset_size;
d521ce57 23147 const gdb_byte *opcode_definitions[256];
8fc3fc34 23148 void **slot;
09262596
DE
23149 struct dwarf2_section_info *section;
23150 const char *section_name;
23151
23152 if (cu->dwo_unit != NULL)
23153 {
23154 if (section_is_gnu)
23155 {
23156 section = &cu->dwo_unit->dwo_file->sections.macro;
23157 section_name = ".debug_macro.dwo";
23158 }
23159 else
23160 {
23161 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23162 section_name = ".debug_macinfo.dwo";
23163 }
23164 }
23165 else
23166 {
23167 if (section_is_gnu)
23168 {
23169 section = &dwarf2_per_objfile->macro;
23170 section_name = ".debug_macro";
23171 }
23172 else
23173 {
23174 section = &dwarf2_per_objfile->macinfo;
23175 section_name = ".debug_macinfo";
23176 }
23177 }
cf2c3c16 23178
bb5ed363 23179 dwarf2_read_section (objfile, section);
cf2c3c16
TT
23180 if (section->buffer == NULL)
23181 {
fceca515 23182 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
23183 return;
23184 }
a32a8923 23185 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
23186
23187 /* First pass: Find the name of the base filename.
23188 This filename is needed in order to process all macros whose definition
23189 (or undefinition) comes from the command line. These macros are defined
23190 before the first DW_MACINFO_start_file entry, and yet still need to be
23191 associated to the base file.
23192
23193 To determine the base file name, we scan the macro definitions until we
23194 reach the first DW_MACINFO_start_file entry. We then initialize
23195 CURRENT_FILE accordingly so that any macro definition found before the
23196 first DW_MACINFO_start_file can still be associated to the base file. */
23197
23198 mac_ptr = section->buffer + offset;
23199 mac_end = section->buffer + section->size;
23200
23201 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23202 &offset_size, section_is_gnu);
23203 if (mac_ptr == NULL)
23204 {
23205 /* We already issued a complaint. */
23206 return;
23207 }
23208
23209 do
23210 {
23211 /* Do we at least have room for a macinfo type byte? */
23212 if (mac_ptr >= mac_end)
23213 {
23214 /* Complaint is printed during the second pass as GDB will probably
23215 stop the first pass earlier upon finding
23216 DW_MACINFO_start_file. */
23217 break;
23218 }
23219
aead7601 23220 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
23221 mac_ptr++;
23222
23223 /* Note that we rely on the fact that the corresponding GNU and
23224 DWARF constants are the same. */
23225 switch (macinfo_type)
23226 {
23227 /* A zero macinfo type indicates the end of the macro
23228 information. */
23229 case 0:
23230 break;
23231
0af92d60
JK
23232 case DW_MACRO_define:
23233 case DW_MACRO_undef:
cf2c3c16
TT
23234 /* Only skip the data by MAC_PTR. */
23235 {
23236 unsigned int bytes_read;
23237
23238 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23239 mac_ptr += bytes_read;
23240 read_direct_string (abfd, mac_ptr, &bytes_read);
23241 mac_ptr += bytes_read;
23242 }
23243 break;
23244
0af92d60 23245 case DW_MACRO_start_file:
cf2c3c16
TT
23246 {
23247 unsigned int bytes_read;
23248 int line, file;
23249
23250 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23251 mac_ptr += bytes_read;
23252 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23253 mac_ptr += bytes_read;
23254
43f3e411 23255 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
23256 }
23257 break;
23258
0af92d60 23259 case DW_MACRO_end_file:
cf2c3c16
TT
23260 /* No data to skip by MAC_PTR. */
23261 break;
23262
0af92d60
JK
23263 case DW_MACRO_define_strp:
23264 case DW_MACRO_undef_strp:
23265 case DW_MACRO_define_sup:
23266 case DW_MACRO_undef_sup:
cf2c3c16
TT
23267 {
23268 unsigned int bytes_read;
23269
23270 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23271 mac_ptr += bytes_read;
23272 mac_ptr += offset_size;
23273 }
23274 break;
23275
0af92d60
JK
23276 case DW_MACRO_import:
23277 case DW_MACRO_import_sup:
cf2c3c16 23278 /* Note that, according to the spec, a transparent include
0af92d60 23279 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
23280 skip this opcode. */
23281 mac_ptr += offset_size;
23282 break;
23283
23284 case DW_MACINFO_vendor_ext:
23285 /* Only skip the data by MAC_PTR. */
23286 if (!section_is_gnu)
23287 {
23288 unsigned int bytes_read;
23289
23290 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23291 mac_ptr += bytes_read;
23292 read_direct_string (abfd, mac_ptr, &bytes_read);
23293 mac_ptr += bytes_read;
23294 }
23295 /* FALLTHROUGH */
23296
23297 default:
23298 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 23299 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
23300 section);
23301 if (mac_ptr == NULL)
23302 return;
23303 break;
23304 }
23305 } while (macinfo_type != 0 && current_file == NULL);
23306
23307 /* Second pass: Process all entries.
23308
23309 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23310 command-line macro definitions/undefinitions. This flag is unset when we
23311 reach the first DW_MACINFO_start_file entry. */
23312
fc4007c9
TT
23313 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23314 htab_eq_pointer,
23315 NULL, xcalloc, xfree));
8fc3fc34 23316 mac_ptr = section->buffer + offset;
fc4007c9 23317 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 23318 *slot = (void *) mac_ptr;
8fc3fc34 23319 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 23320 current_file, lh, section,
fc4007c9
TT
23321 section_is_gnu, 0, offset_size,
23322 include_hash.get ());
cf2c3c16
TT
23323}
23324
8e19ed76 23325/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 23326 if so return true else false. */
380bca97 23327
8e19ed76 23328static int
6e5a29e1 23329attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
23330{
23331 return (attr == NULL ? 0 :
23332 attr->form == DW_FORM_block1
23333 || attr->form == DW_FORM_block2
23334 || attr->form == DW_FORM_block4
2dc7f7b3
TT
23335 || attr->form == DW_FORM_block
23336 || attr->form == DW_FORM_exprloc);
8e19ed76 23337}
4c2df51b 23338
c6a0999f
JB
23339/* Return non-zero if ATTR's value is a section offset --- classes
23340 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
23341 You may use DW_UNSND (attr) to retrieve such offsets.
23342
23343 Section 7.5.4, "Attribute Encodings", explains that no attribute
23344 may have a value that belongs to more than one of these classes; it
23345 would be ambiguous if we did, because we use the same forms for all
23346 of them. */
380bca97 23347
3690dd37 23348static int
6e5a29e1 23349attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
23350{
23351 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
23352 || attr->form == DW_FORM_data8
23353 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
23354}
23355
3690dd37
JB
23356/* Return non-zero if ATTR's value falls in the 'constant' class, or
23357 zero otherwise. When this function returns true, you can apply
23358 dwarf2_get_attr_constant_value to it.
23359
23360 However, note that for some attributes you must check
23361 attr_form_is_section_offset before using this test. DW_FORM_data4
23362 and DW_FORM_data8 are members of both the constant class, and of
23363 the classes that contain offsets into other debug sections
23364 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
23365 that, if an attribute's can be either a constant or one of the
23366 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
23367 taken as section offsets, not constants.
23368
23369 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
23370 cannot handle that. */
380bca97 23371
3690dd37 23372static int
6e5a29e1 23373attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
23374{
23375 switch (attr->form)
23376 {
23377 case DW_FORM_sdata:
23378 case DW_FORM_udata:
23379 case DW_FORM_data1:
23380 case DW_FORM_data2:
23381 case DW_FORM_data4:
23382 case DW_FORM_data8:
663c44ac 23383 case DW_FORM_implicit_const:
3690dd37
JB
23384 return 1;
23385 default:
23386 return 0;
23387 }
23388}
23389
7771576e
SA
23390
23391/* DW_ADDR is always stored already as sect_offset; despite for the forms
23392 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
23393
23394static int
6e5a29e1 23395attr_form_is_ref (const struct attribute *attr)
7771576e
SA
23396{
23397 switch (attr->form)
23398 {
23399 case DW_FORM_ref_addr:
23400 case DW_FORM_ref1:
23401 case DW_FORM_ref2:
23402 case DW_FORM_ref4:
23403 case DW_FORM_ref8:
23404 case DW_FORM_ref_udata:
23405 case DW_FORM_GNU_ref_alt:
23406 return 1;
23407 default:
23408 return 0;
23409 }
23410}
23411
3019eac3
DE
23412/* Return the .debug_loc section to use for CU.
23413 For DWO files use .debug_loc.dwo. */
23414
23415static struct dwarf2_section_info *
23416cu_debug_loc_section (struct dwarf2_cu *cu)
23417{
23418 if (cu->dwo_unit)
43988095
JK
23419 {
23420 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23421
23422 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23423 }
23424 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23425 : &dwarf2_per_objfile->loc);
3019eac3
DE
23426}
23427
8cf6f0b1
TT
23428/* A helper function that fills in a dwarf2_loclist_baton. */
23429
23430static void
23431fill_in_loclist_baton (struct dwarf2_cu *cu,
23432 struct dwarf2_loclist_baton *baton,
ff39bb5e 23433 const struct attribute *attr)
8cf6f0b1 23434{
3019eac3
DE
23435 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23436
23437 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
23438
23439 baton->per_cu = cu->per_cu;
23440 gdb_assert (baton->per_cu);
23441 /* We don't know how long the location list is, but make sure we
23442 don't run off the edge of the section. */
3019eac3
DE
23443 baton->size = section->size - DW_UNSND (attr);
23444 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 23445 baton->base_address = cu->base_address;
f664829e 23446 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23447}
23448
4c2df51b 23449static void
ff39bb5e 23450dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23451 struct dwarf2_cu *cu, int is_block)
4c2df51b 23452{
bb5ed363 23453 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23454 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23455
3690dd37 23456 if (attr_form_is_section_offset (attr)
3019eac3 23457 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23458 the section. If so, fall through to the complaint in the
23459 other branch. */
3019eac3 23460 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 23461 {
0d53c4c4 23462 struct dwarf2_loclist_baton *baton;
4c2df51b 23463
8d749320 23464 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23465
8cf6f0b1 23466 fill_in_loclist_baton (cu, baton, attr);
be391dca 23467
d00adf39 23468 if (cu->base_known == 0)
0d53c4c4 23469 complaint (&symfile_complaints,
3e43a32a
MS
23470 _("Location list used without "
23471 "specifying the CU base address."));
4c2df51b 23472
f1e6e072
TT
23473 SYMBOL_ACLASS_INDEX (sym) = (is_block
23474 ? dwarf2_loclist_block_index
23475 : dwarf2_loclist_index);
0d53c4c4
DJ
23476 SYMBOL_LOCATION_BATON (sym) = baton;
23477 }
23478 else
23479 {
23480 struct dwarf2_locexpr_baton *baton;
23481
8d749320 23482 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
23483 baton->per_cu = cu->per_cu;
23484 gdb_assert (baton->per_cu);
0d53c4c4
DJ
23485
23486 if (attr_form_is_block (attr))
23487 {
23488 /* Note that we're just copying the block's data pointer
23489 here, not the actual data. We're still pointing into the
6502dd73
DJ
23490 info_buffer for SYM's objfile; right now we never release
23491 that buffer, but when we do clean up properly this may
23492 need to change. */
0d53c4c4
DJ
23493 baton->size = DW_BLOCK (attr)->size;
23494 baton->data = DW_BLOCK (attr)->data;
23495 }
23496 else
23497 {
23498 dwarf2_invalid_attrib_class_complaint ("location description",
23499 SYMBOL_NATURAL_NAME (sym));
23500 baton->size = 0;
0d53c4c4 23501 }
6e70227d 23502
f1e6e072
TT
23503 SYMBOL_ACLASS_INDEX (sym) = (is_block
23504 ? dwarf2_locexpr_block_index
23505 : dwarf2_locexpr_index);
0d53c4c4
DJ
23506 SYMBOL_LOCATION_BATON (sym) = baton;
23507 }
4c2df51b 23508}
6502dd73 23509
9aa1f1e3
TT
23510/* Return the OBJFILE associated with the compilation unit CU. If CU
23511 came from a separate debuginfo file, then the master objfile is
23512 returned. */
ae0d2f24
UW
23513
23514struct objfile *
23515dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
23516{
9291a0cd 23517 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
23518
23519 /* Return the master objfile, so that we can report and look up the
23520 correct file containing this variable. */
23521 if (objfile->separate_debug_objfile_backlink)
23522 objfile = objfile->separate_debug_objfile_backlink;
23523
23524 return objfile;
23525}
23526
96408a79
SA
23527/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23528 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23529 CU_HEADERP first. */
23530
23531static const struct comp_unit_head *
23532per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23533 struct dwarf2_per_cu_data *per_cu)
23534{
d521ce57 23535 const gdb_byte *info_ptr;
96408a79
SA
23536
23537 if (per_cu->cu)
23538 return &per_cu->cu->header;
23539
9c541725 23540 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
23541
23542 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
23543 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23544 rcuh_kind::COMPILE);
96408a79
SA
23545
23546 return cu_headerp;
23547}
23548
ae0d2f24
UW
23549/* Return the address size given in the compilation unit header for CU. */
23550
98714339 23551int
ae0d2f24
UW
23552dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
23553{
96408a79
SA
23554 struct comp_unit_head cu_header_local;
23555 const struct comp_unit_head *cu_headerp;
c471e790 23556
96408a79
SA
23557 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23558
23559 return cu_headerp->addr_size;
ae0d2f24
UW
23560}
23561
9eae7c52
TT
23562/* Return the offset size given in the compilation unit header for CU. */
23563
23564int
23565dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
23566{
96408a79
SA
23567 struct comp_unit_head cu_header_local;
23568 const struct comp_unit_head *cu_headerp;
9c6c53f7 23569
96408a79
SA
23570 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23571
23572 return cu_headerp->offset_size;
23573}
23574
23575/* See its dwarf2loc.h declaration. */
23576
23577int
23578dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
23579{
23580 struct comp_unit_head cu_header_local;
23581 const struct comp_unit_head *cu_headerp;
23582
23583 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23584
23585 if (cu_headerp->version == 2)
23586 return cu_headerp->addr_size;
23587 else
23588 return cu_headerp->offset_size;
181cebd4
JK
23589}
23590
9aa1f1e3
TT
23591/* Return the text offset of the CU. The returned offset comes from
23592 this CU's objfile. If this objfile came from a separate debuginfo
23593 file, then the offset may be different from the corresponding
23594 offset in the parent objfile. */
23595
23596CORE_ADDR
23597dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
23598{
bb3fa9d0 23599 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
23600
23601 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23602}
23603
43988095
JK
23604/* Return DWARF version number of PER_CU. */
23605
23606short
23607dwarf2_version (struct dwarf2_per_cu_data *per_cu)
23608{
23609 return per_cu->dwarf_version;
23610}
23611
348e048f
DE
23612/* Locate the .debug_info compilation unit from CU's objfile which contains
23613 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
23614
23615static struct dwarf2_per_cu_data *
9c541725 23616dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 23617 unsigned int offset_in_dwz,
ae038cb0
DJ
23618 struct objfile *objfile)
23619{
23620 struct dwarf2_per_cu_data *this_cu;
23621 int low, high;
36586728 23622 const sect_offset *cu_off;
ae038cb0 23623
ae038cb0
DJ
23624 low = 0;
23625 high = dwarf2_per_objfile->n_comp_units - 1;
23626 while (high > low)
23627 {
36586728 23628 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23629 int mid = low + (high - low) / 2;
9a619af0 23630
36586728 23631 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 23632 cu_off = &mid_cu->sect_off;
36586728 23633 if (mid_cu->is_dwz > offset_in_dwz
9c541725 23634 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
23635 high = mid;
23636 else
23637 low = mid + 1;
23638 }
23639 gdb_assert (low == high);
36586728 23640 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
23641 cu_off = &this_cu->sect_off;
23642 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 23643 {
36586728 23644 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23645 error (_("Dwarf Error: could not find partial DIE containing "
9c541725
PA
23646 "offset 0x%x [in module %s]"),
23647 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
10b3939b 23648
9c541725
PA
23649 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23650 <= sect_off);
ae038cb0
DJ
23651 return dwarf2_per_objfile->all_comp_units[low-1];
23652 }
23653 else
23654 {
23655 this_cu = dwarf2_per_objfile->all_comp_units[low];
23656 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725
PA
23657 && sect_off >= this_cu->sect_off + this_cu->length)
23658 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
23659 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23660 return this_cu;
23661 }
23662}
23663
23745b47 23664/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 23665
9816fde3 23666static void
23745b47 23667init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 23668{
9816fde3 23669 memset (cu, 0, sizeof (*cu));
23745b47
DE
23670 per_cu->cu = cu;
23671 cu->per_cu = per_cu;
23672 cu->objfile = per_cu->objfile;
93311388 23673 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
23674}
23675
23676/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23677
23678static void
95554aad
TT
23679prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23680 enum language pretend_language)
9816fde3
JK
23681{
23682 struct attribute *attr;
23683
23684 /* Set the language we're debugging. */
23685 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23686 if (attr)
23687 set_cu_language (DW_UNSND (attr), cu);
23688 else
9cded63f 23689 {
95554aad 23690 cu->language = pretend_language;
9cded63f
TT
23691 cu->language_defn = language_def (cu->language);
23692 }
dee91e82 23693
7d45c7c3 23694 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23695}
23696
ae038cb0
DJ
23697/* Release one cached compilation unit, CU. We unlink it from the tree
23698 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
23699 the caller is responsible for that.
23700 NOTE: DATA is a void * because this function is also used as a
23701 cleanup routine. */
ae038cb0
DJ
23702
23703static void
68dc6402 23704free_heap_comp_unit (void *data)
ae038cb0 23705{
9a3c8263 23706 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 23707
23745b47
DE
23708 gdb_assert (cu->per_cu != NULL);
23709 cu->per_cu->cu = NULL;
ae038cb0
DJ
23710 cu->per_cu = NULL;
23711
23712 obstack_free (&cu->comp_unit_obstack, NULL);
23713
23714 xfree (cu);
23715}
23716
72bf9492 23717/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 23718 when we're finished with it. We can't free the pointer itself, but be
dee91e82 23719 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
23720
23721static void
23722free_stack_comp_unit (void *data)
23723{
9a3c8263 23724 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 23725
23745b47
DE
23726 gdb_assert (cu->per_cu != NULL);
23727 cu->per_cu->cu = NULL;
23728 cu->per_cu = NULL;
23729
72bf9492
DJ
23730 obstack_free (&cu->comp_unit_obstack, NULL);
23731 cu->partial_dies = NULL;
ae038cb0
DJ
23732}
23733
23734/* Free all cached compilation units. */
23735
23736static void
23737free_cached_comp_units (void *data)
23738{
330cdd98 23739 dwarf2_per_objfile->free_cached_comp_units ();
ae038cb0
DJ
23740}
23741
23742/* Increase the age counter on each cached compilation unit, and free
23743 any that are too old. */
23744
23745static void
23746age_cached_comp_units (void)
23747{
23748 struct dwarf2_per_cu_data *per_cu, **last_chain;
23749
23750 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23751 per_cu = dwarf2_per_objfile->read_in_chain;
23752 while (per_cu != NULL)
23753 {
23754 per_cu->cu->last_used ++;
b4f54984 23755 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23756 dwarf2_mark (per_cu->cu);
23757 per_cu = per_cu->cu->read_in_chain;
23758 }
23759
23760 per_cu = dwarf2_per_objfile->read_in_chain;
23761 last_chain = &dwarf2_per_objfile->read_in_chain;
23762 while (per_cu != NULL)
23763 {
23764 struct dwarf2_per_cu_data *next_cu;
23765
23766 next_cu = per_cu->cu->read_in_chain;
23767
23768 if (!per_cu->cu->mark)
23769 {
68dc6402 23770 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
23771 *last_chain = next_cu;
23772 }
23773 else
23774 last_chain = &per_cu->cu->read_in_chain;
23775
23776 per_cu = next_cu;
23777 }
23778}
23779
23780/* Remove a single compilation unit from the cache. */
23781
23782static void
dee91e82 23783free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
23784{
23785 struct dwarf2_per_cu_data *per_cu, **last_chain;
23786
23787 per_cu = dwarf2_per_objfile->read_in_chain;
23788 last_chain = &dwarf2_per_objfile->read_in_chain;
23789 while (per_cu != NULL)
23790 {
23791 struct dwarf2_per_cu_data *next_cu;
23792
23793 next_cu = per_cu->cu->read_in_chain;
23794
dee91e82 23795 if (per_cu == target_per_cu)
ae038cb0 23796 {
68dc6402 23797 free_heap_comp_unit (per_cu->cu);
dee91e82 23798 per_cu->cu = NULL;
ae038cb0
DJ
23799 *last_chain = next_cu;
23800 break;
23801 }
23802 else
23803 last_chain = &per_cu->cu->read_in_chain;
23804
23805 per_cu = next_cu;
23806 }
23807}
23808
fe3e1990
DJ
23809/* Release all extra memory associated with OBJFILE. */
23810
23811void
23812dwarf2_free_objfile (struct objfile *objfile)
23813{
9a3c8263
SM
23814 dwarf2_per_objfile
23815 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23816 dwarf2_objfile_data_key);
fe3e1990
DJ
23817
23818 if (dwarf2_per_objfile == NULL)
23819 return;
23820
330cdd98 23821 dwarf2_per_objfile->~dwarf2_per_objfile ();
fe3e1990
DJ
23822}
23823
dee91e82
DE
23824/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23825 We store these in a hash table separate from the DIEs, and preserve them
23826 when the DIEs are flushed out of cache.
23827
23828 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23829 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23830 or the type may come from a DWO file. Furthermore, while it's more logical
23831 to use per_cu->section+offset, with Fission the section with the data is in
23832 the DWO file but we don't know that section at the point we need it.
23833 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23834 because we can enter the lookup routine, get_die_type_at_offset, from
23835 outside this file, and thus won't necessarily have PER_CU->cu.
23836 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23837
dee91e82 23838struct dwarf2_per_cu_offset_and_type
1c379e20 23839{
dee91e82 23840 const struct dwarf2_per_cu_data *per_cu;
9c541725 23841 sect_offset sect_off;
1c379e20
DJ
23842 struct type *type;
23843};
23844
dee91e82 23845/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23846
23847static hashval_t
dee91e82 23848per_cu_offset_and_type_hash (const void *item)
1c379e20 23849{
9a3c8263
SM
23850 const struct dwarf2_per_cu_offset_and_type *ofs
23851 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23852
9c541725 23853 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23854}
23855
dee91e82 23856/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23857
23858static int
dee91e82 23859per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23860{
9a3c8263
SM
23861 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23862 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23863 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23864 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23865
dee91e82 23866 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23867 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23868}
23869
23870/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23871 table if necessary. For convenience, return TYPE.
23872
23873 The DIEs reading must have careful ordering to:
23874 * Not cause infite loops trying to read in DIEs as a prerequisite for
23875 reading current DIE.
23876 * Not trying to dereference contents of still incompletely read in types
23877 while reading in other DIEs.
23878 * Enable referencing still incompletely read in types just by a pointer to
23879 the type without accessing its fields.
23880
23881 Therefore caller should follow these rules:
23882 * Try to fetch any prerequisite types we may need to build this DIE type
23883 before building the type and calling set_die_type.
e71ec853 23884 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23885 possible before fetching more types to complete the current type.
23886 * Make the type as complete as possible before fetching more types. */
1c379e20 23887
f792889a 23888static struct type *
1c379e20
DJ
23889set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23890{
dee91e82 23891 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 23892 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
23893 struct attribute *attr;
23894 struct dynamic_prop prop;
1c379e20 23895
b4ba55a1
JB
23896 /* For Ada types, make sure that the gnat-specific data is always
23897 initialized (if not already set). There are a few types where
23898 we should not be doing so, because the type-specific area is
23899 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23900 where the type-specific area is used to store the floatformat).
23901 But this is not a problem, because the gnat-specific information
23902 is actually not needed for these types. */
23903 if (need_gnat_info (cu)
23904 && TYPE_CODE (type) != TYPE_CODE_FUNC
23905 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
23906 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23907 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23908 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
23909 && !HAVE_GNAT_AUX_INFO (type))
23910 INIT_GNAT_SPECIFIC (type);
23911
3f2f83dd
KB
23912 /* Read DW_AT_allocated and set in type. */
23913 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23914 if (attr_form_is_block (attr))
23915 {
23916 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23917 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
23918 }
23919 else if (attr != NULL)
23920 {
23921 complaint (&symfile_complaints,
9c541725
PA
23922 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
23923 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23924 to_underlying (die->sect_off));
3f2f83dd
KB
23925 }
23926
23927 /* Read DW_AT_associated and set in type. */
23928 attr = dwarf2_attr (die, DW_AT_associated, cu);
23929 if (attr_form_is_block (attr))
23930 {
23931 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23932 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
23933 }
23934 else if (attr != NULL)
23935 {
23936 complaint (&symfile_complaints,
9c541725
PA
23937 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
23938 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23939 to_underlying (die->sect_off));
3f2f83dd
KB
23940 }
23941
3cdcd0ce
JB
23942 /* Read DW_AT_data_location and set in type. */
23943 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23944 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 23945 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 23946
dee91e82 23947 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23948 {
dee91e82
DE
23949 dwarf2_per_objfile->die_type_hash =
23950 htab_create_alloc_ex (127,
23951 per_cu_offset_and_type_hash,
23952 per_cu_offset_and_type_eq,
23953 NULL,
23954 &objfile->objfile_obstack,
23955 hashtab_obstack_allocate,
23956 dummy_obstack_deallocate);
f792889a 23957 }
1c379e20 23958
dee91e82 23959 ofs.per_cu = cu->per_cu;
9c541725 23960 ofs.sect_off = die->sect_off;
1c379e20 23961 ofs.type = type;
dee91e82
DE
23962 slot = (struct dwarf2_per_cu_offset_and_type **)
23963 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
23964 if (*slot)
23965 complaint (&symfile_complaints,
23966 _("A problem internal to GDB: DIE 0x%x has type already set"),
9c541725 23967 to_underlying (die->sect_off));
8d749320
SM
23968 *slot = XOBNEW (&objfile->objfile_obstack,
23969 struct dwarf2_per_cu_offset_and_type);
1c379e20 23970 **slot = ofs;
f792889a 23971 return type;
1c379e20
DJ
23972}
23973
9c541725 23974/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23975 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23976
23977static struct type *
9c541725 23978get_die_type_at_offset (sect_offset sect_off,
673bfd45 23979 struct dwarf2_per_cu_data *per_cu)
1c379e20 23980{
dee91e82 23981 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23982
dee91e82 23983 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23984 return NULL;
1c379e20 23985
dee91e82 23986 ofs.per_cu = per_cu;
9c541725 23987 ofs.sect_off = sect_off;
9a3c8263
SM
23988 slot = ((struct dwarf2_per_cu_offset_and_type *)
23989 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
23990 if (slot)
23991 return slot->type;
23992 else
23993 return NULL;
23994}
23995
02142a6c 23996/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23997 or return NULL if DIE does not have a saved type. */
23998
23999static struct type *
24000get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24001{
9c541725 24002 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
24003}
24004
10b3939b
DJ
24005/* Add a dependence relationship from CU to REF_PER_CU. */
24006
24007static void
24008dwarf2_add_dependence (struct dwarf2_cu *cu,
24009 struct dwarf2_per_cu_data *ref_per_cu)
24010{
24011 void **slot;
24012
24013 if (cu->dependencies == NULL)
24014 cu->dependencies
24015 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24016 NULL, &cu->comp_unit_obstack,
24017 hashtab_obstack_allocate,
24018 dummy_obstack_deallocate);
24019
24020 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24021 if (*slot == NULL)
24022 *slot = ref_per_cu;
24023}
1c379e20 24024
f504f079
DE
24025/* Subroutine of dwarf2_mark to pass to htab_traverse.
24026 Set the mark field in every compilation unit in the
ae038cb0
DJ
24027 cache that we must keep because we are keeping CU. */
24028
10b3939b
DJ
24029static int
24030dwarf2_mark_helper (void **slot, void *data)
24031{
24032 struct dwarf2_per_cu_data *per_cu;
24033
24034 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
24035
24036 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24037 reading of the chain. As such dependencies remain valid it is not much
24038 useful to track and undo them during QUIT cleanups. */
24039 if (per_cu->cu == NULL)
24040 return 1;
24041
10b3939b
DJ
24042 if (per_cu->cu->mark)
24043 return 1;
24044 per_cu->cu->mark = 1;
24045
24046 if (per_cu->cu->dependencies != NULL)
24047 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24048
24049 return 1;
24050}
24051
f504f079
DE
24052/* Set the mark field in CU and in every other compilation unit in the
24053 cache that we must keep because we are keeping CU. */
24054
ae038cb0
DJ
24055static void
24056dwarf2_mark (struct dwarf2_cu *cu)
24057{
24058 if (cu->mark)
24059 return;
24060 cu->mark = 1;
10b3939b
DJ
24061 if (cu->dependencies != NULL)
24062 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
24063}
24064
24065static void
24066dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24067{
24068 while (per_cu)
24069 {
24070 per_cu->cu->mark = 0;
24071 per_cu = per_cu->cu->read_in_chain;
24072 }
72bf9492
DJ
24073}
24074
72bf9492
DJ
24075/* Trivial hash function for partial_die_info: the hash value of a DIE
24076 is its offset in .debug_info for this objfile. */
24077
24078static hashval_t
24079partial_die_hash (const void *item)
24080{
9a3c8263
SM
24081 const struct partial_die_info *part_die
24082 = (const struct partial_die_info *) item;
9a619af0 24083
9c541725 24084 return to_underlying (part_die->sect_off);
72bf9492
DJ
24085}
24086
24087/* Trivial comparison function for partial_die_info structures: two DIEs
24088 are equal if they have the same offset. */
24089
24090static int
24091partial_die_eq (const void *item_lhs, const void *item_rhs)
24092{
9a3c8263
SM
24093 const struct partial_die_info *part_die_lhs
24094 = (const struct partial_die_info *) item_lhs;
24095 const struct partial_die_info *part_die_rhs
24096 = (const struct partial_die_info *) item_rhs;
9a619af0 24097
9c541725 24098 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
24099}
24100
b4f54984
DE
24101static struct cmd_list_element *set_dwarf_cmdlist;
24102static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
24103
24104static void
981a3fb3 24105set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 24106{
b4f54984 24107 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 24108 gdb_stdout);
ae038cb0
DJ
24109}
24110
24111static void
981a3fb3 24112show_dwarf_cmd (const char *args, int from_tty)
6e70227d 24113{
b4f54984 24114 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
24115}
24116
4bf44c1c 24117/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
24118
24119static void
c1bd65d0 24120dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 24121{
9a3c8263 24122 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 24123 int ix;
8b70b953 24124
626f2d1c
TT
24125 /* Make sure we don't accidentally use dwarf2_per_objfile while
24126 cleaning up. */
24127 dwarf2_per_objfile = NULL;
24128
59b0c7c1
JB
24129 for (ix = 0; ix < data->n_comp_units; ++ix)
24130 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 24131
59b0c7c1 24132 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 24133 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
24134 data->all_type_units[ix]->per_cu.imported_symtabs);
24135 xfree (data->all_type_units);
95554aad 24136
8b70b953 24137 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
24138
24139 if (data->dwo_files)
24140 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
24141 if (data->dwp_file)
24142 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
24143
24144 if (data->dwz_file && data->dwz_file->dwz_bfd)
24145 gdb_bfd_unref (data->dwz_file->dwz_bfd);
3f563c84
PA
24146
24147 if (data->index_table != NULL)
24148 data->index_table->~mapped_index ();
9291a0cd
TT
24149}
24150
24151\f
ae2de4f8 24152/* The "save gdb-index" command. */
9291a0cd 24153
bc8f2430
JK
24154/* In-memory buffer to prepare data to be written later to a file. */
24155class data_buf
9291a0cd 24156{
bc8f2430 24157public:
bc8f2430
JK
24158 /* Copy DATA to the end of the buffer. */
24159 template<typename T>
24160 void append_data (const T &data)
24161 {
24162 std::copy (reinterpret_cast<const gdb_byte *> (&data),
24163 reinterpret_cast<const gdb_byte *> (&data + 1),
c2f134ac 24164 grow (sizeof (data)));
bc8f2430 24165 }
b89be57b 24166
c2f134ac
PA
24167 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
24168 terminating zero is appended too. */
bc8f2430
JK
24169 void append_cstr0 (const char *cstr)
24170 {
24171 const size_t size = strlen (cstr) + 1;
c2f134ac
PA
24172 std::copy (cstr, cstr + size, grow (size));
24173 }
24174
24175 /* Accept a host-format integer in VAL and append it to the buffer
24176 as a target-format integer which is LEN bytes long. */
24177 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
24178 {
24179 ::store_unsigned_integer (grow (len), len, byte_order, val);
bc8f2430 24180 }
9291a0cd 24181
bc8f2430
JK
24182 /* Return the size of the buffer. */
24183 size_t size () const
24184 {
24185 return m_vec.size ();
24186 }
24187
24188 /* Write the buffer to FILE. */
24189 void file_write (FILE *file) const
24190 {
a81e6d4d
PA
24191 if (::fwrite (m_vec.data (), 1, m_vec.size (), file) != m_vec.size ())
24192 error (_("couldn't write data to file"));
bc8f2430
JK
24193 }
24194
24195private:
c2f134ac
PA
24196 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
24197 the start of the new block. */
24198 gdb_byte *grow (size_t size)
24199 {
24200 m_vec.resize (m_vec.size () + size);
24201 return &*m_vec.end () - size;
24202 }
24203
d5722aa2 24204 gdb::byte_vector m_vec;
bc8f2430 24205};
9291a0cd
TT
24206
24207/* An entry in the symbol table. */
24208struct symtab_index_entry
24209{
24210 /* The name of the symbol. */
24211 const char *name;
24212 /* The offset of the name in the constant pool. */
24213 offset_type index_offset;
24214 /* A sorted vector of the indices of all the CUs that hold an object
24215 of this name. */
bc8f2430 24216 std::vector<offset_type> cu_indices;
9291a0cd
TT
24217};
24218
24219/* The symbol table. This is a power-of-2-sized hash table. */
24220struct mapped_symtab
24221{
bc8f2430
JK
24222 mapped_symtab ()
24223 {
24224 data.resize (1024);
24225 }
b89be57b 24226
bc8f2430 24227 offset_type n_elements = 0;
4b76cda9 24228 std::vector<symtab_index_entry> data;
bc8f2430 24229};
9291a0cd 24230
bc8f2430 24231/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
559a7a62
JK
24232 the slot.
24233
24234 Function is used only during write_hash_table so no index format backward
24235 compatibility is needed. */
b89be57b 24236
4b76cda9 24237static symtab_index_entry &
9291a0cd
TT
24238find_slot (struct mapped_symtab *symtab, const char *name)
24239{
559a7a62 24240 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd 24241
bc8f2430
JK
24242 index = hash & (symtab->data.size () - 1);
24243 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
9291a0cd
TT
24244
24245 for (;;)
24246 {
4b76cda9
PA
24247 if (symtab->data[index].name == NULL
24248 || strcmp (name, symtab->data[index].name) == 0)
bc8f2430
JK
24249 return symtab->data[index];
24250 index = (index + step) & (symtab->data.size () - 1);
9291a0cd
TT
24251 }
24252}
24253
24254/* Expand SYMTAB's hash table. */
b89be57b 24255
9291a0cd
TT
24256static void
24257hash_expand (struct mapped_symtab *symtab)
24258{
bc8f2430 24259 auto old_entries = std::move (symtab->data);
9291a0cd 24260
bc8f2430
JK
24261 symtab->data.clear ();
24262 symtab->data.resize (old_entries.size () * 2);
9291a0cd 24263
bc8f2430 24264 for (auto &it : old_entries)
4b76cda9 24265 if (it.name != NULL)
bc8f2430 24266 {
4b76cda9 24267 auto &ref = find_slot (symtab, it.name);
bc8f2430
JK
24268 ref = std::move (it);
24269 }
9291a0cd
TT
24270}
24271
156942c7
DE
24272/* Add an entry to SYMTAB. NAME is the name of the symbol.
24273 CU_INDEX is the index of the CU in which the symbol appears.
24274 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 24275
9291a0cd
TT
24276static void
24277add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 24278 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
24279 offset_type cu_index)
24280{
156942c7 24281 offset_type cu_index_and_attrs;
9291a0cd
TT
24282
24283 ++symtab->n_elements;
bc8f2430 24284 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
9291a0cd
TT
24285 hash_expand (symtab);
24286
4b76cda9
PA
24287 symtab_index_entry &slot = find_slot (symtab, name);
24288 if (slot.name == NULL)
9291a0cd 24289 {
4b76cda9 24290 slot.name = name;
156942c7 24291 /* index_offset is set later. */
9291a0cd 24292 }
156942c7
DE
24293
24294 cu_index_and_attrs = 0;
24295 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
24296 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
24297 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
24298
24299 /* We don't want to record an index value twice as we want to avoid the
24300 duplication.
24301 We process all global symbols and then all static symbols
24302 (which would allow us to avoid the duplication by only having to check
24303 the last entry pushed), but a symbol could have multiple kinds in one CU.
24304 To keep things simple we don't worry about the duplication here and
24305 sort and uniqufy the list after we've processed all symbols. */
4b76cda9 24306 slot.cu_indices.push_back (cu_index_and_attrs);
156942c7
DE
24307}
24308
24309/* Sort and remove duplicates of all symbols' cu_indices lists. */
24310
24311static void
24312uniquify_cu_indices (struct mapped_symtab *symtab)
24313{
4b76cda9 24314 for (auto &entry : symtab->data)
156942c7 24315 {
4b76cda9 24316 if (entry.name != NULL && !entry.cu_indices.empty ())
156942c7 24317 {
4b76cda9 24318 auto &cu_indices = entry.cu_indices;
6fd931f2
PA
24319 std::sort (cu_indices.begin (), cu_indices.end ());
24320 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
24321 cu_indices.erase (from, cu_indices.end ());
156942c7
DE
24322 }
24323 }
9291a0cd
TT
24324}
24325
bc8f2430
JK
24326/* A form of 'const char *' suitable for container keys. Only the
24327 pointer is stored. The strings themselves are compared, not the
24328 pointers. */
24329class c_str_view
9291a0cd 24330{
bc8f2430
JK
24331public:
24332 c_str_view (const char *cstr)
24333 : m_cstr (cstr)
24334 {}
9291a0cd 24335
bc8f2430
JK
24336 bool operator== (const c_str_view &other) const
24337 {
24338 return strcmp (m_cstr, other.m_cstr) == 0;
24339 }
9291a0cd 24340
bc8f2430
JK
24341private:
24342 friend class c_str_view_hasher;
24343 const char *const m_cstr;
24344};
9291a0cd 24345
bc8f2430
JK
24346/* A std::unordered_map::hasher for c_str_view that uses the right
24347 hash function for strings in a mapped index. */
24348class c_str_view_hasher
24349{
24350public:
24351 size_t operator () (const c_str_view &x) const
24352 {
24353 return mapped_index_string_hash (INT_MAX, x.m_cstr);
24354 }
24355};
b89be57b 24356
bc8f2430
JK
24357/* A std::unordered_map::hasher for std::vector<>. */
24358template<typename T>
24359class vector_hasher
9291a0cd 24360{
bc8f2430
JK
24361public:
24362 size_t operator () (const std::vector<T> &key) const
24363 {
24364 return iterative_hash (key.data (),
24365 sizeof (key.front ()) * key.size (), 0);
24366 }
24367};
9291a0cd 24368
bc8f2430
JK
24369/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
24370 constant pool entries going into the data buffer CPOOL. */
3876f04e 24371
bc8f2430
JK
24372static void
24373write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
24374{
24375 {
24376 /* Elements are sorted vectors of the indices of all the CUs that
24377 hold an object of this name. */
24378 std::unordered_map<std::vector<offset_type>, offset_type,
24379 vector_hasher<offset_type>>
24380 symbol_hash_table;
24381
24382 /* We add all the index vectors to the constant pool first, to
24383 ensure alignment is ok. */
4b76cda9 24384 for (symtab_index_entry &entry : symtab->data)
bc8f2430 24385 {
4b76cda9 24386 if (entry.name == NULL)
bc8f2430 24387 continue;
4b76cda9 24388 gdb_assert (entry.index_offset == 0);
70a1152b
PA
24389
24390 /* Finding before inserting is faster than always trying to
24391 insert, because inserting always allocates a node, does the
24392 lookup, and then destroys the new node if another node
24393 already had the same key. C++17 try_emplace will avoid
24394 this. */
24395 const auto found
4b76cda9 24396 = symbol_hash_table.find (entry.cu_indices);
70a1152b
PA
24397 if (found != symbol_hash_table.end ())
24398 {
4b76cda9 24399 entry.index_offset = found->second;
70a1152b
PA
24400 continue;
24401 }
24402
4b76cda9
PA
24403 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
24404 entry.index_offset = cpool.size ();
24405 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
24406 for (const auto index : entry.cu_indices)
24407 cpool.append_data (MAYBE_SWAP (index));
bc8f2430
JK
24408 }
24409 }
9291a0cd
TT
24410
24411 /* Now write out the hash table. */
bc8f2430 24412 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
4b76cda9 24413 for (const auto &entry : symtab->data)
9291a0cd
TT
24414 {
24415 offset_type str_off, vec_off;
24416
4b76cda9 24417 if (entry.name != NULL)
9291a0cd 24418 {
4b76cda9 24419 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
bc8f2430 24420 if (insertpair.second)
4b76cda9 24421 cpool.append_cstr0 (entry.name);
bc8f2430 24422 str_off = insertpair.first->second;
4b76cda9 24423 vec_off = entry.index_offset;
9291a0cd
TT
24424 }
24425 else
24426 {
24427 /* While 0 is a valid constant pool index, it is not valid
24428 to have 0 for both offsets. */
24429 str_off = 0;
24430 vec_off = 0;
24431 }
24432
bc8f2430
JK
24433 output.append_data (MAYBE_SWAP (str_off));
24434 output.append_data (MAYBE_SWAP (vec_off));
9291a0cd 24435 }
9291a0cd
TT
24436}
24437
bc8f2430 24438typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
0a5429f6
DE
24439
24440/* Helper struct for building the address table. */
24441struct addrmap_index_data
24442{
bc8f2430
JK
24443 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
24444 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
24445 {}
24446
0a5429f6 24447 struct objfile *objfile;
bc8f2430
JK
24448 data_buf &addr_vec;
24449 psym_index_map &cu_index_htab;
0a5429f6
DE
24450
24451 /* Non-zero if the previous_* fields are valid.
24452 We can't write an entry until we see the next entry (since it is only then
24453 that we know the end of the entry). */
24454 int previous_valid;
24455 /* Index of the CU in the table of all CUs in the index file. */
24456 unsigned int previous_cu_index;
0963b4bd 24457 /* Start address of the CU. */
0a5429f6
DE
24458 CORE_ADDR previous_cu_start;
24459};
24460
bc8f2430 24461/* Write an address entry to ADDR_VEC. */
b89be57b 24462
9291a0cd 24463static void
bc8f2430 24464add_address_entry (struct objfile *objfile, data_buf &addr_vec,
0a5429f6 24465 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 24466{
9291a0cd
TT
24467 CORE_ADDR baseaddr;
24468
24469 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24470
c2f134ac
PA
24471 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
24472 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
bc8f2430 24473 addr_vec.append_data (MAYBE_SWAP (cu_index));
0a5429f6
DE
24474}
24475
24476/* Worker function for traversing an addrmap to build the address table. */
24477
24478static int
24479add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
24480{
9a3c8263
SM
24481 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
24482 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
24483
24484 if (data->previous_valid)
bc8f2430 24485 add_address_entry (data->objfile, data->addr_vec,
0a5429f6
DE
24486 data->previous_cu_start, start_addr,
24487 data->previous_cu_index);
24488
24489 data->previous_cu_start = start_addr;
24490 if (pst != NULL)
24491 {
bc8f2430
JK
24492 const auto it = data->cu_index_htab.find (pst);
24493 gdb_assert (it != data->cu_index_htab.cend ());
24494 data->previous_cu_index = it->second;
0a5429f6
DE
24495 data->previous_valid = 1;
24496 }
24497 else
bc8f2430 24498 data->previous_valid = 0;
0a5429f6
DE
24499
24500 return 0;
24501}
24502
bc8f2430 24503/* Write OBJFILE's address map to ADDR_VEC.
0a5429f6
DE
24504 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
24505 in the index file. */
24506
24507static void
bc8f2430
JK
24508write_address_map (struct objfile *objfile, data_buf &addr_vec,
24509 psym_index_map &cu_index_htab)
0a5429f6 24510{
bc8f2430 24511 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
0a5429f6
DE
24512
24513 /* When writing the address table, we have to cope with the fact that
24514 the addrmap iterator only provides the start of a region; we have to
24515 wait until the next invocation to get the start of the next region. */
24516
24517 addrmap_index_data.objfile = objfile;
0a5429f6
DE
24518 addrmap_index_data.previous_valid = 0;
24519
24520 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
24521 &addrmap_index_data);
24522
24523 /* It's highly unlikely the last entry (end address = 0xff...ff)
24524 is valid, but we should still handle it.
24525 The end address is recorded as the start of the next region, but that
24526 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
24527 anyway. */
24528 if (addrmap_index_data.previous_valid)
bc8f2430 24529 add_address_entry (objfile, addr_vec,
0a5429f6
DE
24530 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
24531 addrmap_index_data.previous_cu_index);
9291a0cd
TT
24532}
24533
156942c7
DE
24534/* Return the symbol kind of PSYM. */
24535
24536static gdb_index_symbol_kind
24537symbol_kind (struct partial_symbol *psym)
24538{
24539 domain_enum domain = PSYMBOL_DOMAIN (psym);
24540 enum address_class aclass = PSYMBOL_CLASS (psym);
24541
24542 switch (domain)
24543 {
24544 case VAR_DOMAIN:
24545 switch (aclass)
24546 {
24547 case LOC_BLOCK:
24548 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
24549 case LOC_TYPEDEF:
24550 return GDB_INDEX_SYMBOL_KIND_TYPE;
24551 case LOC_COMPUTED:
24552 case LOC_CONST_BYTES:
24553 case LOC_OPTIMIZED_OUT:
24554 case LOC_STATIC:
24555 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
24556 case LOC_CONST:
24557 /* Note: It's currently impossible to recognize psyms as enum values
24558 short of reading the type info. For now punt. */
24559 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
24560 default:
24561 /* There are other LOC_FOO values that one might want to classify
24562 as variables, but dwarf2read.c doesn't currently use them. */
24563 return GDB_INDEX_SYMBOL_KIND_OTHER;
24564 }
24565 case STRUCT_DOMAIN:
24566 return GDB_INDEX_SYMBOL_KIND_TYPE;
24567 default:
24568 return GDB_INDEX_SYMBOL_KIND_OTHER;
24569 }
24570}
24571
9291a0cd 24572/* Add a list of partial symbols to SYMTAB. */
b89be57b 24573
9291a0cd
TT
24574static void
24575write_psymbols (struct mapped_symtab *symtab,
bc8f2430 24576 std::unordered_set<partial_symbol *> &psyms_seen,
9291a0cd
TT
24577 struct partial_symbol **psymp,
24578 int count,
987d643c
TT
24579 offset_type cu_index,
24580 int is_static)
9291a0cd
TT
24581{
24582 for (; count-- > 0; ++psymp)
24583 {
156942c7 24584 struct partial_symbol *psym = *psymp;
987d643c 24585
156942c7 24586 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 24587 error (_("Ada is not currently supported by the index"));
987d643c 24588
987d643c 24589 /* Only add a given psymbol once. */
bc8f2430 24590 if (psyms_seen.insert (psym).second)
987d643c 24591 {
156942c7
DE
24592 gdb_index_symbol_kind kind = symbol_kind (psym);
24593
156942c7
DE
24594 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
24595 is_static, kind, cu_index);
987d643c 24596 }
9291a0cd
TT
24597 }
24598}
24599
1fd400ff
TT
24600/* A helper struct used when iterating over debug_types. */
24601struct signatured_type_index_data
24602{
bc8f2430
JK
24603 signatured_type_index_data (data_buf &types_list_,
24604 std::unordered_set<partial_symbol *> &psyms_seen_)
24605 : types_list (types_list_), psyms_seen (psyms_seen_)
24606 {}
24607
1fd400ff
TT
24608 struct objfile *objfile;
24609 struct mapped_symtab *symtab;
bc8f2430
JK
24610 data_buf &types_list;
24611 std::unordered_set<partial_symbol *> &psyms_seen;
1fd400ff
TT
24612 int cu_index;
24613};
24614
24615/* A helper function that writes a single signatured_type to an
24616 obstack. */
b89be57b 24617
1fd400ff
TT
24618static int
24619write_one_signatured_type (void **slot, void *d)
24620{
9a3c8263
SM
24621 struct signatured_type_index_data *info
24622 = (struct signatured_type_index_data *) d;
1fd400ff 24623 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 24624 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
24625
24626 write_psymbols (info->symtab,
987d643c 24627 info->psyms_seen,
af5bf4ad 24628 &info->objfile->global_psymbols[psymtab->globals_offset],
987d643c
TT
24629 psymtab->n_global_syms, info->cu_index,
24630 0);
1fd400ff 24631 write_psymbols (info->symtab,
987d643c 24632 info->psyms_seen,
af5bf4ad 24633 &info->objfile->static_psymbols[psymtab->statics_offset],
987d643c
TT
24634 psymtab->n_static_syms, info->cu_index,
24635 1);
1fd400ff 24636
c2f134ac
PA
24637 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
24638 to_underlying (entry->per_cu.sect_off));
24639 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
24640 to_underlying (entry->type_offset_in_tu));
24641 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
1fd400ff
TT
24642
24643 ++info->cu_index;
24644
24645 return 1;
24646}
24647
e8f8bcb3
PA
24648/* Recurse into all "included" dependencies and count their symbols as
24649 if they appeared in this psymtab. */
24650
24651static void
24652recursively_count_psymbols (struct partial_symtab *psymtab,
24653 size_t &psyms_seen)
24654{
24655 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
24656 if (psymtab->dependencies[i]->user != NULL)
24657 recursively_count_psymbols (psymtab->dependencies[i],
24658 psyms_seen);
24659
24660 psyms_seen += psymtab->n_global_syms;
24661 psyms_seen += psymtab->n_static_syms;
24662}
24663
95554aad
TT
24664/* Recurse into all "included" dependencies and write their symbols as
24665 if they appeared in this psymtab. */
24666
24667static void
24668recursively_write_psymbols (struct objfile *objfile,
24669 struct partial_symtab *psymtab,
24670 struct mapped_symtab *symtab,
bc8f2430 24671 std::unordered_set<partial_symbol *> &psyms_seen,
95554aad
TT
24672 offset_type cu_index)
24673{
24674 int i;
24675
24676 for (i = 0; i < psymtab->number_of_dependencies; ++i)
24677 if (psymtab->dependencies[i]->user != NULL)
24678 recursively_write_psymbols (objfile, psymtab->dependencies[i],
24679 symtab, psyms_seen, cu_index);
24680
24681 write_psymbols (symtab,
24682 psyms_seen,
af5bf4ad 24683 &objfile->global_psymbols[psymtab->globals_offset],
95554aad
TT
24684 psymtab->n_global_syms, cu_index,
24685 0);
24686 write_psymbols (symtab,
24687 psyms_seen,
af5bf4ad 24688 &objfile->static_psymbols[psymtab->statics_offset],
95554aad
TT
24689 psymtab->n_static_syms, cu_index,
24690 1);
24691}
24692
9291a0cd 24693/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 24694
9291a0cd
TT
24695static void
24696write_psymtabs_to_index (struct objfile *objfile, const char *dir)
24697{
9291a0cd
TT
24698 if (dwarf2_per_objfile->using_index)
24699 error (_("Cannot use an index to create the index"));
24700
8b70b953
TT
24701 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
24702 error (_("Cannot make an index when the file has multiple .debug_types sections"));
24703
260b681b
DE
24704 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
24705 return;
24706
bc8f2430 24707 struct stat st;
4262abfb
JK
24708 if (stat (objfile_name (objfile), &st) < 0)
24709 perror_with_name (objfile_name (objfile));
9291a0cd 24710
bc8f2430
JK
24711 std::string filename (std::string (dir) + SLASH_STRING
24712 + lbasename (objfile_name (objfile)) + INDEX_SUFFIX);
9291a0cd 24713
d419f42d 24714 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
9291a0cd 24715 if (!out_file)
bc8f2430 24716 error (_("Can't open `%s' for writing"), filename.c_str ());
9291a0cd 24717
16b7a719
PA
24718 /* Order matters here; we want FILE to be closed before FILENAME is
24719 unlinked, because on MS-Windows one cannot delete a file that is
24720 still open. (Don't call anything here that might throw until
24721 file_closer is created.) */
bc8f2430 24722 gdb::unlinker unlink_file (filename.c_str ());
d419f42d 24723 gdb_file_up close_out_file (out_file);
9291a0cd 24724
bc8f2430
JK
24725 mapped_symtab symtab;
24726 data_buf cu_list;
987d643c 24727
0a5429f6
DE
24728 /* While we're scanning CU's create a table that maps a psymtab pointer
24729 (which is what addrmap records) to its index (which is what is recorded
24730 in the index file). This will later be needed to write the address
24731 table. */
bc8f2430
JK
24732 psym_index_map cu_index_htab;
24733 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
0a5429f6
DE
24734
24735 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
24736 work here. Also, the debug_types entries do not appear in
24737 all_comp_units, but only in their own hash table. */
e8f8bcb3
PA
24738
24739 /* The psyms_seen set is potentially going to be largish (~40k
24740 elements when indexing a -g3 build of GDB itself). Estimate the
24741 number of elements in order to avoid too many rehashes, which
24742 require rebuilding buckets and thus many trips to
24743 malloc/free. */
24744 size_t psyms_count = 0;
24745 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24746 {
24747 struct dwarf2_per_cu_data *per_cu
24748 = dwarf2_per_objfile->all_comp_units[i];
24749 struct partial_symtab *psymtab = per_cu->v.psymtab;
24750
24751 if (psymtab != NULL && psymtab->user == NULL)
24752 recursively_count_psymbols (psymtab, psyms_count);
24753 }
24754 /* Generating an index for gdb itself shows a ratio of
24755 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
24756 std::unordered_set<partial_symbol *> psyms_seen (psyms_count / 4);
bc8f2430 24757 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 24758 {
3e43a32a
MS
24759 struct dwarf2_per_cu_data *per_cu
24760 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 24761 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 24762
92fac807
JK
24763 /* CU of a shared file from 'dwz -m' may be unused by this main file.
24764 It may be referenced from a local scope but in such case it does not
24765 need to be present in .gdb_index. */
24766 if (psymtab == NULL)
24767 continue;
24768
95554aad 24769 if (psymtab->user == NULL)
bc8f2430
JK
24770 recursively_write_psymbols (objfile, psymtab, &symtab,
24771 psyms_seen, i);
9291a0cd 24772
bc8f2430
JK
24773 const auto insertpair = cu_index_htab.emplace (psymtab, i);
24774 gdb_assert (insertpair.second);
9291a0cd 24775
c2f134ac
PA
24776 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
24777 to_underlying (per_cu->sect_off));
24778 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
24779 }
24780
0a5429f6 24781 /* Dump the address map. */
bc8f2430
JK
24782 data_buf addr_vec;
24783 write_address_map (objfile, addr_vec, cu_index_htab);
0a5429f6 24784
1fd400ff 24785 /* Write out the .debug_type entries, if any. */
bc8f2430 24786 data_buf types_cu_list;
1fd400ff
TT
24787 if (dwarf2_per_objfile->signatured_types)
24788 {
bc8f2430
JK
24789 signatured_type_index_data sig_data (types_cu_list,
24790 psyms_seen);
1fd400ff
TT
24791
24792 sig_data.objfile = objfile;
bc8f2430 24793 sig_data.symtab = &symtab;
1fd400ff
TT
24794 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
24795 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
24796 write_one_signatured_type, &sig_data);
24797 }
24798
156942c7
DE
24799 /* Now that we've processed all symbols we can shrink their cu_indices
24800 lists. */
bc8f2430 24801 uniquify_cu_indices (&symtab);
156942c7 24802
bc8f2430
JK
24803 data_buf symtab_vec, constant_pool;
24804 write_hash_table (&symtab, symtab_vec, constant_pool);
9291a0cd 24805
bc8f2430
JK
24806 data_buf contents;
24807 const offset_type size_of_contents = 6 * sizeof (offset_type);
24808 offset_type total_len = size_of_contents;
9291a0cd
TT
24809
24810 /* The version number. */
bc8f2430 24811 contents.append_data (MAYBE_SWAP (8));
9291a0cd
TT
24812
24813 /* The offset of the CU list from the start of the file. */
bc8f2430
JK
24814 contents.append_data (MAYBE_SWAP (total_len));
24815 total_len += cu_list.size ();
9291a0cd 24816
1fd400ff 24817 /* The offset of the types CU list from the start of the file. */
bc8f2430
JK
24818 contents.append_data (MAYBE_SWAP (total_len));
24819 total_len += types_cu_list.size ();
1fd400ff 24820
9291a0cd 24821 /* The offset of the address table from the start of the file. */
bc8f2430
JK
24822 contents.append_data (MAYBE_SWAP (total_len));
24823 total_len += addr_vec.size ();
9291a0cd
TT
24824
24825 /* The offset of the symbol table from the start of the file. */
bc8f2430
JK
24826 contents.append_data (MAYBE_SWAP (total_len));
24827 total_len += symtab_vec.size ();
9291a0cd
TT
24828
24829 /* The offset of the constant pool from the start of the file. */
bc8f2430
JK
24830 contents.append_data (MAYBE_SWAP (total_len));
24831 total_len += constant_pool.size ();
9291a0cd 24832
bc8f2430 24833 gdb_assert (contents.size () == size_of_contents);
9291a0cd 24834
bc8f2430
JK
24835 contents.file_write (out_file);
24836 cu_list.file_write (out_file);
24837 types_cu_list.file_write (out_file);
24838 addr_vec.file_write (out_file);
24839 symtab_vec.file_write (out_file);
24840 constant_pool.file_write (out_file);
9291a0cd 24841
bef155c3
TT
24842 /* We want to keep the file. */
24843 unlink_file.keep ();
9291a0cd
TT
24844}
24845
90476074
TT
24846/* Implementation of the `save gdb-index' command.
24847
24848 Note that the file format used by this command is documented in the
24849 GDB manual. Any changes here must be documented there. */
11570e71 24850
9291a0cd 24851static void
8384c356 24852save_gdb_index_command (const char *arg, int from_tty)
9291a0cd
TT
24853{
24854 struct objfile *objfile;
24855
24856 if (!arg || !*arg)
96d19272 24857 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
24858
24859 ALL_OBJFILES (objfile)
24860 {
24861 struct stat st;
24862
24863 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 24864 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
24865 continue;
24866
9a3c8263
SM
24867 dwarf2_per_objfile
24868 = (struct dwarf2_per_objfile *) objfile_data (objfile,
24869 dwarf2_objfile_data_key);
9291a0cd
TT
24870 if (dwarf2_per_objfile)
24871 {
9291a0cd 24872
492d29ea 24873 TRY
9291a0cd
TT
24874 {
24875 write_psymtabs_to_index (objfile, arg);
24876 }
492d29ea
PA
24877 CATCH (except, RETURN_MASK_ERROR)
24878 {
24879 exception_fprintf (gdb_stderr, except,
24880 _("Error while writing index for `%s': "),
24881 objfile_name (objfile));
24882 }
24883 END_CATCH
9291a0cd
TT
24884 }
24885 }
dce234bc
PP
24886}
24887
9291a0cd
TT
24888\f
24889
b4f54984 24890int dwarf_always_disassemble;
9eae7c52
TT
24891
24892static void
b4f54984
DE
24893show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24894 struct cmd_list_element *c, const char *value)
9eae7c52 24895{
3e43a32a
MS
24896 fprintf_filtered (file,
24897 _("Whether to always disassemble "
24898 "DWARF expressions is %s.\n"),
9eae7c52
TT
24899 value);
24900}
24901
900e11f9
JK
24902static void
24903show_check_physname (struct ui_file *file, int from_tty,
24904 struct cmd_list_element *c, const char *value)
24905{
24906 fprintf_filtered (file,
24907 _("Whether to check \"physname\" is %s.\n"),
24908 value);
24909}
24910
6502dd73
DJ
24911void
24912_initialize_dwarf2_read (void)
24913{
96d19272
JK
24914 struct cmd_list_element *c;
24915
dce234bc 24916 dwarf2_objfile_data_key
c1bd65d0 24917 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 24918
b4f54984
DE
24919 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24920Set DWARF specific variables.\n\
24921Configure DWARF variables such as the cache size"),
24922 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
24923 0/*allow-unknown*/, &maintenance_set_cmdlist);
24924
b4f54984
DE
24925 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24926Show DWARF specific variables\n\
24927Show DWARF variables such as the cache size"),
24928 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
24929 0/*allow-unknown*/, &maintenance_show_cmdlist);
24930
24931 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
24932 &dwarf_max_cache_age, _("\
24933Set the upper bound on the age of cached DWARF compilation units."), _("\
24934Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
24935A higher limit means that cached compilation units will be stored\n\
24936in memory longer, and more total memory will be used. Zero disables\n\
24937caching, which can slow down startup."),
2c5b56ce 24938 NULL,
b4f54984
DE
24939 show_dwarf_max_cache_age,
24940 &set_dwarf_cmdlist,
24941 &show_dwarf_cmdlist);
d97bc12b 24942
9eae7c52 24943 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 24944 &dwarf_always_disassemble, _("\
9eae7c52
TT
24945Set whether `info address' always disassembles DWARF expressions."), _("\
24946Show whether `info address' always disassembles DWARF expressions."), _("\
24947When enabled, DWARF expressions are always printed in an assembly-like\n\
24948syntax. When disabled, expressions will be printed in a more\n\
24949conversational style, when possible."),
24950 NULL,
b4f54984
DE
24951 show_dwarf_always_disassemble,
24952 &set_dwarf_cmdlist,
24953 &show_dwarf_cmdlist);
24954
24955 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24956Set debugging of the DWARF reader."), _("\
24957Show debugging of the DWARF reader."), _("\
24958When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
24959reading and symtab expansion. A value of 1 (one) provides basic\n\
24960information. A value greater than 1 provides more verbose information."),
45cfd468
DE
24961 NULL,
24962 NULL,
24963 &setdebuglist, &showdebuglist);
24964
b4f54984
DE
24965 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24966Set debugging of the DWARF DIE reader."), _("\
24967Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
24968When enabled (non-zero), DIEs are dumped after they are read in.\n\
24969The value is the maximum depth to print."),
ccce17b0
YQ
24970 NULL,
24971 NULL,
24972 &setdebuglist, &showdebuglist);
9291a0cd 24973
27e0867f
DE
24974 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24975Set debugging of the dwarf line reader."), _("\
24976Show debugging of the dwarf line reader."), _("\
24977When enabled (non-zero), line number entries are dumped as they are read in.\n\
24978A value of 1 (one) provides basic information.\n\
24979A value greater than 1 provides more verbose information."),
24980 NULL,
24981 NULL,
24982 &setdebuglist, &showdebuglist);
24983
900e11f9
JK
24984 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24985Set cross-checking of \"physname\" code against demangler."), _("\
24986Show cross-checking of \"physname\" code against demangler."), _("\
24987When enabled, GDB's internal \"physname\" code is checked against\n\
24988the demangler."),
24989 NULL, show_check_physname,
24990 &setdebuglist, &showdebuglist);
24991
e615022a
DE
24992 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24993 no_class, &use_deprecated_index_sections, _("\
24994Set whether to use deprecated gdb_index sections."), _("\
24995Show whether to use deprecated gdb_index sections."), _("\
24996When enabled, deprecated .gdb_index sections are used anyway.\n\
24997Normally they are ignored either because of a missing feature or\n\
24998performance issue.\n\
24999Warning: This option must be enabled before gdb reads the file."),
25000 NULL,
25001 NULL,
25002 &setlist, &showlist);
25003
96d19272 25004 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 25005 _("\
fc1a9d6e 25006Save a gdb-index file.\n\
11570e71 25007Usage: save gdb-index DIRECTORY"),
96d19272
JK
25008 &save_cmdlist);
25009 set_cmd_completer (c, filename_completer);
f1e6e072
TT
25010
25011 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25012 &dwarf2_locexpr_funcs);
25013 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25014 &dwarf2_loclist_funcs);
25015
25016 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25017 &dwarf2_block_frame_base_locexpr_funcs);
25018 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25019 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25020
25021#if GDB_SELF_TEST
25022 selftests::register_test ("dw2_expand_symtabs_matching",
25023 selftests::dw2_expand_symtabs_matching::run_test);
25024#endif
6502dd73 25025}
This page took 4.038534 seconds and 4 git commands to generate.