Fix wrong prefix in py-breakpoint.exp
[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{
97cbe998
SDJ
2322 if (objfile->flags & OBJF_READNEVER)
2323 return 0;
2324
9a3c8263
SM
2325 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2326 objfile_data (objfile, dwarf2_objfile_data_key));
be391dca
TT
2327 if (!dwarf2_per_objfile)
2328 {
2329 /* Initialize per-objfile state. */
2330 struct dwarf2_per_objfile *data
8d749320 2331 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
9a619af0 2332
330cdd98
PA
2333 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2334 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
be391dca 2335 }
73869dc2 2336 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2337 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2338 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2339 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2340}
2341
2342/* Return the containing section of virtual section SECTION. */
2343
2344static struct dwarf2_section_info *
2345get_containing_section (const struct dwarf2_section_info *section)
2346{
2347 gdb_assert (section->is_virtual);
2348 return section->s.containing_section;
c906108c
SS
2349}
2350
a32a8923
DE
2351/* Return the bfd owner of SECTION. */
2352
2353static struct bfd *
2354get_section_bfd_owner (const struct dwarf2_section_info *section)
2355{
73869dc2
DE
2356 if (section->is_virtual)
2357 {
2358 section = get_containing_section (section);
2359 gdb_assert (!section->is_virtual);
2360 }
049412e3 2361 return section->s.section->owner;
a32a8923
DE
2362}
2363
2364/* Return the bfd section of SECTION.
2365 Returns NULL if the section is not present. */
2366
2367static asection *
2368get_section_bfd_section (const struct dwarf2_section_info *section)
2369{
73869dc2
DE
2370 if (section->is_virtual)
2371 {
2372 section = get_containing_section (section);
2373 gdb_assert (!section->is_virtual);
2374 }
049412e3 2375 return section->s.section;
a32a8923
DE
2376}
2377
2378/* Return the name of SECTION. */
2379
2380static const char *
2381get_section_name (const struct dwarf2_section_info *section)
2382{
2383 asection *sectp = get_section_bfd_section (section);
2384
2385 gdb_assert (sectp != NULL);
2386 return bfd_section_name (get_section_bfd_owner (section), sectp);
2387}
2388
2389/* Return the name of the file SECTION is in. */
2390
2391static const char *
2392get_section_file_name (const struct dwarf2_section_info *section)
2393{
2394 bfd *abfd = get_section_bfd_owner (section);
2395
2396 return bfd_get_filename (abfd);
2397}
2398
2399/* Return the id of SECTION.
2400 Returns 0 if SECTION doesn't exist. */
2401
2402static int
2403get_section_id (const struct dwarf2_section_info *section)
2404{
2405 asection *sectp = get_section_bfd_section (section);
2406
2407 if (sectp == NULL)
2408 return 0;
2409 return sectp->id;
2410}
2411
2412/* Return the flags of SECTION.
73869dc2 2413 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2414
2415static int
2416get_section_flags (const struct dwarf2_section_info *section)
2417{
2418 asection *sectp = get_section_bfd_section (section);
2419
2420 gdb_assert (sectp != NULL);
2421 return bfd_get_section_flags (sectp->owner, sectp);
2422}
2423
251d32d9
TG
2424/* When loading sections, we look either for uncompressed section or for
2425 compressed section names. */
233a11ab
CS
2426
2427static int
251d32d9
TG
2428section_is_p (const char *section_name,
2429 const struct dwarf2_section_names *names)
233a11ab 2430{
251d32d9
TG
2431 if (names->normal != NULL
2432 && strcmp (section_name, names->normal) == 0)
2433 return 1;
2434 if (names->compressed != NULL
2435 && strcmp (section_name, names->compressed) == 0)
2436 return 1;
2437 return 0;
233a11ab
CS
2438}
2439
330cdd98 2440/* See declaration. */
c906108c 2441
330cdd98
PA
2442void
2443dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2444 const dwarf2_debug_sections &names)
c906108c 2445{
dc7650b8 2446 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2447
dc7650b8
JK
2448 if ((aflag & SEC_HAS_CONTENTS) == 0)
2449 {
2450 }
330cdd98 2451 else if (section_is_p (sectp->name, &names.info))
c906108c 2452 {
330cdd98
PA
2453 this->info.s.section = sectp;
2454 this->info.size = bfd_get_section_size (sectp);
c906108c 2455 }
330cdd98 2456 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2457 {
330cdd98
PA
2458 this->abbrev.s.section = sectp;
2459 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2460 }
330cdd98 2461 else if (section_is_p (sectp->name, &names.line))
c906108c 2462 {
330cdd98
PA
2463 this->line.s.section = sectp;
2464 this->line.size = bfd_get_section_size (sectp);
c906108c 2465 }
330cdd98 2466 else if (section_is_p (sectp->name, &names.loc))
c906108c 2467 {
330cdd98
PA
2468 this->loc.s.section = sectp;
2469 this->loc.size = bfd_get_section_size (sectp);
c906108c 2470 }
330cdd98 2471 else if (section_is_p (sectp->name, &names.loclists))
43988095 2472 {
330cdd98
PA
2473 this->loclists.s.section = sectp;
2474 this->loclists.size = bfd_get_section_size (sectp);
43988095 2475 }
330cdd98 2476 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2477 {
330cdd98
PA
2478 this->macinfo.s.section = sectp;
2479 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2480 }
330cdd98 2481 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2482 {
330cdd98
PA
2483 this->macro.s.section = sectp;
2484 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2485 }
330cdd98 2486 else if (section_is_p (sectp->name, &names.str))
c906108c 2487 {
330cdd98
PA
2488 this->str.s.section = sectp;
2489 this->str.size = bfd_get_section_size (sectp);
c906108c 2490 }
330cdd98 2491 else if (section_is_p (sectp->name, &names.line_str))
43988095 2492 {
330cdd98
PA
2493 this->line_str.s.section = sectp;
2494 this->line_str.size = bfd_get_section_size (sectp);
43988095 2495 }
330cdd98 2496 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2497 {
330cdd98
PA
2498 this->addr.s.section = sectp;
2499 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2500 }
330cdd98 2501 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2502 {
330cdd98
PA
2503 this->frame.s.section = sectp;
2504 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2505 }
330cdd98 2506 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2507 {
330cdd98
PA
2508 this->eh_frame.s.section = sectp;
2509 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2510 }
330cdd98 2511 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2512 {
330cdd98
PA
2513 this->ranges.s.section = sectp;
2514 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2515 }
330cdd98 2516 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2517 {
330cdd98
PA
2518 this->rnglists.s.section = sectp;
2519 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2520 }
330cdd98 2521 else if (section_is_p (sectp->name, &names.types))
348e048f 2522 {
8b70b953
TT
2523 struct dwarf2_section_info type_section;
2524
2525 memset (&type_section, 0, sizeof (type_section));
049412e3 2526 type_section.s.section = sectp;
8b70b953
TT
2527 type_section.size = bfd_get_section_size (sectp);
2528
330cdd98 2529 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2530 &type_section);
348e048f 2531 }
330cdd98 2532 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2533 {
330cdd98
PA
2534 this->gdb_index.s.section = sectp;
2535 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2536 }
dce234bc 2537
b4e1fd61 2538 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2539 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2540 this->has_section_at_zero = true;
c906108c
SS
2541}
2542
fceca515
DE
2543/* A helper function that decides whether a section is empty,
2544 or not present. */
9e0ac564
TT
2545
2546static int
19ac8c2e 2547dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2548{
73869dc2
DE
2549 if (section->is_virtual)
2550 return section->size == 0;
049412e3 2551 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2552}
2553
3019eac3
DE
2554/* Read the contents of the section INFO.
2555 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2556 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2557 of the DWO file.
dce234bc 2558 If the section is compressed, uncompress it before returning. */
c906108c 2559
dce234bc
PP
2560static void
2561dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2562{
a32a8923 2563 asection *sectp;
3019eac3 2564 bfd *abfd;
dce234bc 2565 gdb_byte *buf, *retbuf;
c906108c 2566
be391dca
TT
2567 if (info->readin)
2568 return;
dce234bc 2569 info->buffer = NULL;
be391dca 2570 info->readin = 1;
188dd5d6 2571
9e0ac564 2572 if (dwarf2_section_empty_p (info))
dce234bc 2573 return;
c906108c 2574
a32a8923 2575 sectp = get_section_bfd_section (info);
3019eac3 2576
73869dc2
DE
2577 /* If this is a virtual section we need to read in the real one first. */
2578 if (info->is_virtual)
2579 {
2580 struct dwarf2_section_info *containing_section =
2581 get_containing_section (info);
2582
2583 gdb_assert (sectp != NULL);
2584 if ((sectp->flags & SEC_RELOC) != 0)
2585 {
2586 error (_("Dwarf Error: DWP format V2 with relocations is not"
2587 " supported in section %s [in module %s]"),
2588 get_section_name (info), get_section_file_name (info));
2589 }
2590 dwarf2_read_section (objfile, containing_section);
2591 /* Other code should have already caught virtual sections that don't
2592 fit. */
2593 gdb_assert (info->virtual_offset + info->size
2594 <= containing_section->size);
2595 /* If the real section is empty or there was a problem reading the
2596 section we shouldn't get here. */
2597 gdb_assert (containing_section->buffer != NULL);
2598 info->buffer = containing_section->buffer + info->virtual_offset;
2599 return;
2600 }
2601
4bf44c1c
TT
2602 /* If the section has relocations, we must read it ourselves.
2603 Otherwise we attach it to the BFD. */
2604 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2605 {
d521ce57 2606 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2607 return;
dce234bc 2608 }
dce234bc 2609
224c3ddb 2610 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2611 info->buffer = buf;
dce234bc
PP
2612
2613 /* When debugging .o files, we may need to apply relocations; see
2614 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2615 We never compress sections in .o files, so we only need to
2616 try this when the section is not compressed. */
ac8035ab 2617 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2618 if (retbuf != NULL)
2619 {
2620 info->buffer = retbuf;
2621 return;
2622 }
2623
a32a8923
DE
2624 abfd = get_section_bfd_owner (info);
2625 gdb_assert (abfd != NULL);
2626
dce234bc
PP
2627 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2628 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2629 {
2630 error (_("Dwarf Error: Can't read DWARF data"
2631 " in section %s [in module %s]"),
2632 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2633 }
dce234bc
PP
2634}
2635
9e0ac564
TT
2636/* A helper function that returns the size of a section in a safe way.
2637 If you are positive that the section has been read before using the
2638 size, then it is safe to refer to the dwarf2_section_info object's
2639 "size" field directly. In other cases, you must call this
2640 function, because for compressed sections the size field is not set
2641 correctly until the section has been read. */
2642
2643static bfd_size_type
2644dwarf2_section_size (struct objfile *objfile,
2645 struct dwarf2_section_info *info)
2646{
2647 if (!info->readin)
2648 dwarf2_read_section (objfile, info);
2649 return info->size;
2650}
2651
dce234bc 2652/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2653 SECTION_NAME. */
af34e669 2654
dce234bc 2655void
3017a003
TG
2656dwarf2_get_section_info (struct objfile *objfile,
2657 enum dwarf2_section_enum sect,
d521ce57 2658 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2659 bfd_size_type *sizep)
2660{
2661 struct dwarf2_per_objfile *data
9a3c8263
SM
2662 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2663 dwarf2_objfile_data_key);
dce234bc 2664 struct dwarf2_section_info *info;
a3b2a86b
TT
2665
2666 /* We may see an objfile without any DWARF, in which case we just
2667 return nothing. */
2668 if (data == NULL)
2669 {
2670 *sectp = NULL;
2671 *bufp = NULL;
2672 *sizep = 0;
2673 return;
2674 }
3017a003
TG
2675 switch (sect)
2676 {
2677 case DWARF2_DEBUG_FRAME:
2678 info = &data->frame;
2679 break;
2680 case DWARF2_EH_FRAME:
2681 info = &data->eh_frame;
2682 break;
2683 default:
2684 gdb_assert_not_reached ("unexpected section");
2685 }
dce234bc 2686
9e0ac564 2687 dwarf2_read_section (objfile, info);
dce234bc 2688
a32a8923 2689 *sectp = get_section_bfd_section (info);
dce234bc
PP
2690 *bufp = info->buffer;
2691 *sizep = info->size;
2692}
2693
36586728
TT
2694/* A helper function to find the sections for a .dwz file. */
2695
2696static void
2697locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2698{
9a3c8263 2699 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2700
2701 /* Note that we only support the standard ELF names, because .dwz
2702 is ELF-only (at the time of writing). */
2703 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2704 {
049412e3 2705 dwz_file->abbrev.s.section = sectp;
36586728
TT
2706 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2707 }
2708 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2709 {
049412e3 2710 dwz_file->info.s.section = sectp;
36586728
TT
2711 dwz_file->info.size = bfd_get_section_size (sectp);
2712 }
2713 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2714 {
049412e3 2715 dwz_file->str.s.section = sectp;
36586728
TT
2716 dwz_file->str.size = bfd_get_section_size (sectp);
2717 }
2718 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2719 {
049412e3 2720 dwz_file->line.s.section = sectp;
36586728
TT
2721 dwz_file->line.size = bfd_get_section_size (sectp);
2722 }
2723 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2724 {
049412e3 2725 dwz_file->macro.s.section = sectp;
36586728
TT
2726 dwz_file->macro.size = bfd_get_section_size (sectp);
2727 }
2ec9a5e0
TT
2728 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2729 {
049412e3 2730 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2731 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2732 }
36586728
TT
2733}
2734
4db1a1dc
TT
2735/* Open the separate '.dwz' debug file, if needed. Return NULL if
2736 there is no .gnu_debugaltlink section in the file. Error if there
2737 is such a section but the file cannot be found. */
36586728
TT
2738
2739static struct dwz_file *
2740dwarf2_get_dwz_file (void)
2741{
36586728
TT
2742 const char *filename;
2743 struct dwz_file *result;
acd13123 2744 bfd_size_type buildid_len_arg;
dc294be5
TT
2745 size_t buildid_len;
2746 bfd_byte *buildid;
36586728
TT
2747
2748 if (dwarf2_per_objfile->dwz_file != NULL)
2749 return dwarf2_per_objfile->dwz_file;
2750
4db1a1dc 2751 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2752 gdb::unique_xmalloc_ptr<char> data
2753 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2754 &buildid_len_arg, &buildid));
4db1a1dc
TT
2755 if (data == NULL)
2756 {
2757 if (bfd_get_error () == bfd_error_no_error)
2758 return NULL;
2759 error (_("could not read '.gnu_debugaltlink' section: %s"),
2760 bfd_errmsg (bfd_get_error ()));
2761 }
791afaa2
TT
2762
2763 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2764
acd13123
TT
2765 buildid_len = (size_t) buildid_len_arg;
2766
791afaa2 2767 filename = data.get ();
d721ba37
PA
2768
2769 std::string abs_storage;
36586728
TT
2770 if (!IS_ABSOLUTE_PATH (filename))
2771 {
14278e1f
TT
2772 gdb::unique_xmalloc_ptr<char> abs
2773 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2774
14278e1f 2775 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2776 filename = abs_storage.c_str ();
36586728
TT
2777 }
2778
dc294be5
TT
2779 /* First try the file name given in the section. If that doesn't
2780 work, try to use the build-id instead. */
192b62ce 2781 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2782 if (dwz_bfd != NULL)
36586728 2783 {
192b62ce
TT
2784 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2785 dwz_bfd.release ();
36586728
TT
2786 }
2787
dc294be5
TT
2788 if (dwz_bfd == NULL)
2789 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2790
2791 if (dwz_bfd == NULL)
2792 error (_("could not find '.gnu_debugaltlink' file for %s"),
2793 objfile_name (dwarf2_per_objfile->objfile));
2794
36586728
TT
2795 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2796 struct dwz_file);
192b62ce 2797 result->dwz_bfd = dwz_bfd.release ();
36586728 2798
192b62ce 2799 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728 2800
192b62ce 2801 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2802 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2803 return result;
2804}
9291a0cd 2805\f
7b9f3c50
DE
2806/* DWARF quick_symbols_functions support. */
2807
2808/* TUs can share .debug_line entries, and there can be a lot more TUs than
2809 unique line tables, so we maintain a separate table of all .debug_line
2810 derived entries to support the sharing.
2811 All the quick functions need is the list of file names. We discard the
2812 line_header when we're done and don't need to record it here. */
2813struct quick_file_names
2814{
094b34ac
DE
2815 /* The data used to construct the hash key. */
2816 struct stmt_list_hash hash;
7b9f3c50
DE
2817
2818 /* The number of entries in file_names, real_names. */
2819 unsigned int num_file_names;
2820
2821 /* The file names from the line table, after being run through
2822 file_full_name. */
2823 const char **file_names;
2824
2825 /* The file names from the line table after being run through
2826 gdb_realpath. These are computed lazily. */
2827 const char **real_names;
2828};
2829
2830/* When using the index (and thus not using psymtabs), each CU has an
2831 object of this type. This is used to hold information needed by
2832 the various "quick" methods. */
2833struct dwarf2_per_cu_quick_data
2834{
2835 /* The file table. This can be NULL if there was no file table
2836 or it's currently not read in.
2837 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2838 struct quick_file_names *file_names;
2839
2840 /* The corresponding symbol table. This is NULL if symbols for this
2841 CU have not yet been read. */
43f3e411 2842 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2843
2844 /* A temporary mark bit used when iterating over all CUs in
2845 expand_symtabs_matching. */
2846 unsigned int mark : 1;
2847
2848 /* True if we've tried to read the file table and found there isn't one.
2849 There will be no point in trying to read it again next time. */
2850 unsigned int no_file_data : 1;
2851};
2852
094b34ac
DE
2853/* Utility hash function for a stmt_list_hash. */
2854
2855static hashval_t
2856hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2857{
2858 hashval_t v = 0;
2859
2860 if (stmt_list_hash->dwo_unit != NULL)
2861 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2862 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2863 return v;
2864}
2865
2866/* Utility equality function for a stmt_list_hash. */
2867
2868static int
2869eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2870 const struct stmt_list_hash *rhs)
2871{
2872 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2873 return 0;
2874 if (lhs->dwo_unit != NULL
2875 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2876 return 0;
2877
9c541725 2878 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2879}
2880
7b9f3c50
DE
2881/* Hash function for a quick_file_names. */
2882
2883static hashval_t
2884hash_file_name_entry (const void *e)
2885{
9a3c8263
SM
2886 const struct quick_file_names *file_data
2887 = (const struct quick_file_names *) e;
7b9f3c50 2888
094b34ac 2889 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2890}
2891
2892/* Equality function for a quick_file_names. */
2893
2894static int
2895eq_file_name_entry (const void *a, const void *b)
2896{
9a3c8263
SM
2897 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2898 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2899
094b34ac 2900 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2901}
2902
2903/* Delete function for a quick_file_names. */
2904
2905static void
2906delete_file_name_entry (void *e)
2907{
9a3c8263 2908 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2909 int i;
2910
2911 for (i = 0; i < file_data->num_file_names; ++i)
2912 {
2913 xfree ((void*) file_data->file_names[i]);
2914 if (file_data->real_names)
2915 xfree ((void*) file_data->real_names[i]);
2916 }
2917
2918 /* The space for the struct itself lives on objfile_obstack,
2919 so we don't free it here. */
2920}
2921
2922/* Create a quick_file_names hash table. */
2923
2924static htab_t
2925create_quick_file_names_table (unsigned int nr_initial_entries)
2926{
2927 return htab_create_alloc (nr_initial_entries,
2928 hash_file_name_entry, eq_file_name_entry,
2929 delete_file_name_entry, xcalloc, xfree);
2930}
9291a0cd 2931
918dd910
JK
2932/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2933 have to be created afterwards. You should call age_cached_comp_units after
2934 processing PER_CU->CU. dw2_setup must have been already called. */
2935
2936static void
2937load_cu (struct dwarf2_per_cu_data *per_cu)
2938{
3019eac3 2939 if (per_cu->is_debug_types)
e5fe5e75 2940 load_full_type_unit (per_cu);
918dd910 2941 else
95554aad 2942 load_full_comp_unit (per_cu, language_minimal);
918dd910 2943
cc12ce38
DE
2944 if (per_cu->cu == NULL)
2945 return; /* Dummy CU. */
2dc860c0
DE
2946
2947 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2948}
2949
a0f42c21 2950/* Read in the symbols for PER_CU. */
2fdf6df6 2951
9291a0cd 2952static void
a0f42c21 2953dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2954{
2955 struct cleanup *back_to;
2956
f4dc4d17
DE
2957 /* Skip type_unit_groups, reading the type units they contain
2958 is handled elsewhere. */
2959 if (IS_TYPE_UNIT_GROUP (per_cu))
2960 return;
2961
9291a0cd
TT
2962 back_to = make_cleanup (dwarf2_release_queue, NULL);
2963
95554aad 2964 if (dwarf2_per_objfile->using_index
43f3e411 2965 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2966 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2967 {
2968 queue_comp_unit (per_cu, language_minimal);
2969 load_cu (per_cu);
89e63ee4
DE
2970
2971 /* If we just loaded a CU from a DWO, and we're working with an index
2972 that may badly handle TUs, load all the TUs in that DWO as well.
2973 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2974 if (!per_cu->is_debug_types
cc12ce38 2975 && per_cu->cu != NULL
89e63ee4
DE
2976 && per_cu->cu->dwo_unit != NULL
2977 && dwarf2_per_objfile->index_table != NULL
2978 && dwarf2_per_objfile->index_table->version <= 7
2979 /* DWP files aren't supported yet. */
2980 && get_dwp_file () == NULL)
2981 queue_and_load_all_dwo_tus (per_cu);
95554aad 2982 }
9291a0cd 2983
a0f42c21 2984 process_queue ();
9291a0cd
TT
2985
2986 /* Age the cache, releasing compilation units that have not
2987 been used recently. */
2988 age_cached_comp_units ();
2989
2990 do_cleanups (back_to);
2991}
2992
2993/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2994 the objfile from which this CU came. Returns the resulting symbol
2995 table. */
2fdf6df6 2996
43f3e411 2997static struct compunit_symtab *
a0f42c21 2998dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2999{
95554aad 3000 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 3001 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3002 {
3003 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
c83dd867 3004 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 3005 dw2_do_instantiate_symtab (per_cu);
95554aad 3006 process_cu_includes ();
9291a0cd
TT
3007 do_cleanups (back_to);
3008 }
f194fefb 3009
43f3e411 3010 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
3011}
3012
8832e7e3 3013/* Return the CU/TU given its index.
f4dc4d17
DE
3014
3015 This is intended for loops like:
3016
3017 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3018 + dwarf2_per_objfile->n_type_units); ++i)
3019 {
8832e7e3 3020 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
3021
3022 ...;
3023 }
3024*/
2fdf6df6 3025
1fd400ff 3026static struct dwarf2_per_cu_data *
8832e7e3 3027dw2_get_cutu (int index)
1fd400ff
TT
3028{
3029 if (index >= dwarf2_per_objfile->n_comp_units)
3030 {
f4dc4d17 3031 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
3032 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3033 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
3034 }
3035
3036 return dwarf2_per_objfile->all_comp_units[index];
3037}
3038
8832e7e3
DE
3039/* Return the CU given its index.
3040 This differs from dw2_get_cutu in that it's for when you know INDEX
3041 refers to a CU. */
f4dc4d17
DE
3042
3043static struct dwarf2_per_cu_data *
8832e7e3 3044dw2_get_cu (int index)
f4dc4d17 3045{
8832e7e3 3046 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 3047
1fd400ff
TT
3048 return dwarf2_per_objfile->all_comp_units[index];
3049}
3050
2ec9a5e0
TT
3051/* A helper for create_cus_from_index that handles a given list of
3052 CUs. */
2fdf6df6 3053
74a0d9f6 3054static void
2ec9a5e0
TT
3055create_cus_from_index_list (struct objfile *objfile,
3056 const gdb_byte *cu_list, offset_type n_elements,
3057 struct dwarf2_section_info *section,
3058 int is_dwz,
3059 int base_offset)
9291a0cd
TT
3060{
3061 offset_type i;
9291a0cd 3062
2ec9a5e0 3063 for (i = 0; i < n_elements; i += 2)
9291a0cd 3064 {
74a0d9f6 3065 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3066
3067 sect_offset sect_off
3068 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3069 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3070 cu_list += 2 * 8;
3071
9c541725
PA
3072 dwarf2_per_cu_data *the_cu
3073 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3074 struct dwarf2_per_cu_data);
3075 the_cu->sect_off = sect_off;
9291a0cd
TT
3076 the_cu->length = length;
3077 the_cu->objfile = objfile;
8a0459fd 3078 the_cu->section = section;
9291a0cd
TT
3079 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3080 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
3081 the_cu->is_dwz = is_dwz;
3082 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 3083 }
9291a0cd
TT
3084}
3085
2ec9a5e0 3086/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3087 the CU objects for this objfile. */
2ec9a5e0 3088
74a0d9f6 3089static void
2ec9a5e0
TT
3090create_cus_from_index (struct objfile *objfile,
3091 const gdb_byte *cu_list, offset_type cu_list_elements,
3092 const gdb_byte *dwz_list, offset_type dwz_elements)
3093{
3094 struct dwz_file *dwz;
3095
3096 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
3097 dwarf2_per_objfile->all_comp_units =
3098 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3099 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 3100
74a0d9f6
JK
3101 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3102 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
3103
3104 if (dwz_elements == 0)
74a0d9f6 3105 return;
2ec9a5e0
TT
3106
3107 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
3108 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3109 cu_list_elements / 2);
2ec9a5e0
TT
3110}
3111
1fd400ff 3112/* Create the signatured type hash table from the index. */
673bfd45 3113
74a0d9f6 3114static void
673bfd45 3115create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3116 struct dwarf2_section_info *section,
673bfd45
DE
3117 const gdb_byte *bytes,
3118 offset_type elements)
1fd400ff
TT
3119{
3120 offset_type i;
673bfd45 3121 htab_t sig_types_hash;
1fd400ff 3122
6aa5f3a6
DE
3123 dwarf2_per_objfile->n_type_units
3124 = dwarf2_per_objfile->n_allocated_type_units
3125 = elements / 3;
8d749320
SM
3126 dwarf2_per_objfile->all_type_units =
3127 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3128
673bfd45 3129 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3130
3131 for (i = 0; i < elements; i += 3)
3132 {
52dc124a 3133 struct signatured_type *sig_type;
9c541725 3134 ULONGEST signature;
1fd400ff 3135 void **slot;
9c541725 3136 cu_offset type_offset_in_tu;
1fd400ff 3137
74a0d9f6 3138 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3139 sect_offset sect_off
3140 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3141 type_offset_in_tu
3142 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3143 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3144 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3145 bytes += 3 * 8;
3146
52dc124a 3147 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3148 struct signatured_type);
52dc124a 3149 sig_type->signature = signature;
9c541725 3150 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3151 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3152 sig_type->per_cu.section = section;
9c541725 3153 sig_type->per_cu.sect_off = sect_off;
52dc124a
DE
3154 sig_type->per_cu.objfile = objfile;
3155 sig_type->per_cu.v.quick
1fd400ff
TT
3156 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3157 struct dwarf2_per_cu_quick_data);
3158
52dc124a
DE
3159 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3160 *slot = sig_type;
1fd400ff 3161
b4dd5633 3162 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3163 }
3164
673bfd45 3165 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3166}
3167
9291a0cd
TT
3168/* Read the address map data from the mapped index, and use it to
3169 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3170
9291a0cd
TT
3171static void
3172create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3173{
3e29f34a 3174 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3175 const gdb_byte *iter, *end;
9291a0cd 3176 struct addrmap *mutable_map;
9291a0cd
TT
3177 CORE_ADDR baseaddr;
3178
8268c778
PA
3179 auto_obstack temp_obstack;
3180
9291a0cd
TT
3181 mutable_map = addrmap_create_mutable (&temp_obstack);
3182
3183 iter = index->address_table;
3184 end = iter + index->address_table_size;
3185
3186 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3187
3188 while (iter < end)
3189 {
3190 ULONGEST hi, lo, cu_index;
3191 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3192 iter += 8;
3193 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3194 iter += 8;
3195 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3196 iter += 4;
f652bce2 3197
24a55014 3198 if (lo > hi)
f652bce2 3199 {
24a55014
DE
3200 complaint (&symfile_complaints,
3201 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3202 hex_string (lo), hex_string (hi));
24a55014 3203 continue;
f652bce2 3204 }
24a55014
DE
3205
3206 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3207 {
3208 complaint (&symfile_complaints,
3209 _(".gdb_index address table has invalid CU number %u"),
3210 (unsigned) cu_index);
24a55014 3211 continue;
f652bce2 3212 }
24a55014 3213
3e29f34a
MR
3214 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3215 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3216 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
9291a0cd
TT
3217 }
3218
3219 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3220 &objfile->objfile_obstack);
9291a0cd
TT
3221}
3222
59d7bcaf
JK
3223/* The hash function for strings in the mapped index. This is the same as
3224 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3225 implementation. This is necessary because the hash function is tied to the
3226 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3227 SYMBOL_HASH_NEXT.
3228
3229 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3230
9291a0cd 3231static hashval_t
559a7a62 3232mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3233{
3234 const unsigned char *str = (const unsigned char *) p;
3235 hashval_t r = 0;
3236 unsigned char c;
3237
3238 while ((c = *str++) != 0)
559a7a62
JK
3239 {
3240 if (index_version >= 5)
3241 c = tolower (c);
3242 r = r * 67 + c - 113;
3243 }
9291a0cd
TT
3244
3245 return r;
3246}
3247
3248/* Find a slot in the mapped index INDEX for the object named NAME.
3249 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3250 constant pool and return true. If NAME cannot be found, return
3251 false. */
2fdf6df6 3252
109483d9 3253static bool
9291a0cd
TT
3254find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3255 offset_type **vec_out)
3256{
0cf03b49 3257 offset_type hash;
9291a0cd 3258 offset_type slot, step;
559a7a62 3259 int (*cmp) (const char *, const char *);
9291a0cd 3260
791afaa2 3261 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3262 if (current_language->la_language == language_cplus
45280282
IB
3263 || current_language->la_language == language_fortran
3264 || current_language->la_language == language_d)
0cf03b49
JK
3265 {
3266 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3267 not contain any. */
a8719064 3268
72998fb3 3269 if (strchr (name, '(') != NULL)
0cf03b49 3270 {
109483d9 3271 without_params = cp_remove_params (name);
0cf03b49 3272
72998fb3 3273 if (without_params != NULL)
791afaa2 3274 name = without_params.get ();
0cf03b49
JK
3275 }
3276 }
3277
559a7a62 3278 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3279 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3280 simulate our NAME being searched is also lowercased. */
3281 hash = mapped_index_string_hash ((index->version == 4
3282 && case_sensitivity == case_sensitive_off
3283 ? 5 : index->version),
3284 name);
3285
3876f04e
DE
3286 slot = hash & (index->symbol_table_slots - 1);
3287 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 3288 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3289
3290 for (;;)
3291 {
3292 /* Convert a slot number to an offset into the table. */
3293 offset_type i = 2 * slot;
3294 const char *str;
3876f04e 3295 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
109483d9 3296 return false;
9291a0cd 3297
3876f04e 3298 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 3299 if (!cmp (name, str))
9291a0cd
TT
3300 {
3301 *vec_out = (offset_type *) (index->constant_pool
3876f04e 3302 + MAYBE_SWAP (index->symbol_table[i + 1]));
109483d9 3303 return true;
9291a0cd
TT
3304 }
3305
3876f04e 3306 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
3307 }
3308}
3309
2ec9a5e0
TT
3310/* A helper function that reads the .gdb_index from SECTION and fills
3311 in MAP. FILENAME is the name of the file containing the section;
3312 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3313 ok to use deprecated sections.
3314
3315 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3316 out parameters that are filled in with information about the CU and
3317 TU lists in the section.
3318
3319 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3320
9291a0cd 3321static int
2ec9a5e0
TT
3322read_index_from_section (struct objfile *objfile,
3323 const char *filename,
3324 int deprecated_ok,
3325 struct dwarf2_section_info *section,
3326 struct mapped_index *map,
3327 const gdb_byte **cu_list,
3328 offset_type *cu_list_elements,
3329 const gdb_byte **types_list,
3330 offset_type *types_list_elements)
9291a0cd 3331{
948f8e3d 3332 const gdb_byte *addr;
2ec9a5e0 3333 offset_type version;
b3b272e1 3334 offset_type *metadata;
1fd400ff 3335 int i;
9291a0cd 3336
2ec9a5e0 3337 if (dwarf2_section_empty_p (section))
9291a0cd 3338 return 0;
82430852
JK
3339
3340 /* Older elfutils strip versions could keep the section in the main
3341 executable while splitting it for the separate debug info file. */
a32a8923 3342 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3343 return 0;
3344
2ec9a5e0 3345 dwarf2_read_section (objfile, section);
9291a0cd 3346
2ec9a5e0 3347 addr = section->buffer;
9291a0cd 3348 /* Version check. */
1fd400ff 3349 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3350 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3351 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3352 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3353 indices. */
831adc1f 3354 if (version < 4)
481860b3
GB
3355 {
3356 static int warning_printed = 0;
3357 if (!warning_printed)
3358 {
3359 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3360 filename);
481860b3
GB
3361 warning_printed = 1;
3362 }
3363 return 0;
3364 }
3365 /* Index version 4 uses a different hash function than index version
3366 5 and later.
3367
3368 Versions earlier than 6 did not emit psymbols for inlined
3369 functions. Using these files will cause GDB not to be able to
3370 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3371 indices unless the user has done
3372 "set use-deprecated-index-sections on". */
2ec9a5e0 3373 if (version < 6 && !deprecated_ok)
481860b3
GB
3374 {
3375 static int warning_printed = 0;
3376 if (!warning_printed)
3377 {
e615022a
DE
3378 warning (_("\
3379Skipping deprecated .gdb_index section in %s.\n\
3380Do \"set use-deprecated-index-sections on\" before the file is read\n\
3381to use the section anyway."),
2ec9a5e0 3382 filename);
481860b3
GB
3383 warning_printed = 1;
3384 }
3385 return 0;
3386 }
796a7ff8 3387 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3388 of the TU (for symbols coming from TUs),
3389 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3390 Plus gold-generated indices can have duplicate entries for global symbols,
3391 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3392 These are just performance bugs, and we can't distinguish gdb-generated
3393 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3394
481860b3 3395 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3396 longer backward compatible. */
796a7ff8 3397 if (version > 8)
594e8718 3398 return 0;
9291a0cd 3399
559a7a62 3400 map->version = version;
2ec9a5e0 3401 map->total_size = section->size;
9291a0cd
TT
3402
3403 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3404
3405 i = 0;
2ec9a5e0
TT
3406 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3407 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3408 / 8);
1fd400ff
TT
3409 ++i;
3410
2ec9a5e0
TT
3411 *types_list = addr + MAYBE_SWAP (metadata[i]);
3412 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3413 - MAYBE_SWAP (metadata[i]))
3414 / 8);
987d643c 3415 ++i;
1fd400ff
TT
3416
3417 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3418 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3419 - MAYBE_SWAP (metadata[i]));
3420 ++i;
3421
3876f04e
DE
3422 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3423 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3424 - MAYBE_SWAP (metadata[i]))
3425 / (2 * sizeof (offset_type)));
1fd400ff 3426 ++i;
9291a0cd 3427
f9d83a0b 3428 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3429
2ec9a5e0
TT
3430 return 1;
3431}
3432
3433
3434/* Read the index file. If everything went ok, initialize the "quick"
3435 elements of all the CUs and return 1. Otherwise, return 0. */
3436
3437static int
3438dwarf2_read_index (struct objfile *objfile)
3439{
3440 struct mapped_index local_map, *map;
3441 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3442 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3443 struct dwz_file *dwz;
2ec9a5e0 3444
4262abfb 3445 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3446 use_deprecated_index_sections,
3447 &dwarf2_per_objfile->gdb_index, &local_map,
3448 &cu_list, &cu_list_elements,
3449 &types_list, &types_list_elements))
3450 return 0;
3451
0fefef59 3452 /* Don't use the index if it's empty. */
2ec9a5e0 3453 if (local_map.symbol_table_slots == 0)
0fefef59
DE
3454 return 0;
3455
2ec9a5e0
TT
3456 /* If there is a .dwz file, read it so we can get its CU list as
3457 well. */
4db1a1dc
TT
3458 dwz = dwarf2_get_dwz_file ();
3459 if (dwz != NULL)
2ec9a5e0 3460 {
2ec9a5e0
TT
3461 struct mapped_index dwz_map;
3462 const gdb_byte *dwz_types_ignore;
3463 offset_type dwz_types_elements_ignore;
3464
3465 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3466 1,
3467 &dwz->gdb_index, &dwz_map,
3468 &dwz_list, &dwz_list_elements,
3469 &dwz_types_ignore,
3470 &dwz_types_elements_ignore))
3471 {
3472 warning (_("could not read '.gdb_index' section from %s; skipping"),
3473 bfd_get_filename (dwz->dwz_bfd));
3474 return 0;
3475 }
3476 }
3477
74a0d9f6
JK
3478 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3479 dwz_list_elements);
1fd400ff 3480
8b70b953
TT
3481 if (types_list_elements)
3482 {
3483 struct dwarf2_section_info *section;
3484
3485 /* We can only handle a single .debug_types when we have an
3486 index. */
3487 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3488 return 0;
3489
3490 section = VEC_index (dwarf2_section_info_def,
3491 dwarf2_per_objfile->types, 0);
3492
74a0d9f6
JK
3493 create_signatured_type_table_from_index (objfile, section, types_list,
3494 types_list_elements);
8b70b953 3495 }
9291a0cd 3496
2ec9a5e0
TT
3497 create_addrmap_from_index (objfile, &local_map);
3498
8d749320 3499 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3f563c84 3500 map = new (map) mapped_index ();
2ec9a5e0 3501 *map = local_map;
9291a0cd
TT
3502
3503 dwarf2_per_objfile->index_table = map;
3504 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3505 dwarf2_per_objfile->quick_file_names_table =
3506 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3507
3508 return 1;
3509}
3510
3511/* A helper for the "quick" functions which sets the global
3512 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 3513
9291a0cd
TT
3514static void
3515dw2_setup (struct objfile *objfile)
3516{
9a3c8263
SM
3517 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3518 objfile_data (objfile, dwarf2_objfile_data_key));
9291a0cd
TT
3519 gdb_assert (dwarf2_per_objfile);
3520}
3521
dee91e82 3522/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3523
dee91e82
DE
3524static void
3525dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3526 const gdb_byte *info_ptr,
dee91e82
DE
3527 struct die_info *comp_unit_die,
3528 int has_children,
3529 void *data)
9291a0cd 3530{
dee91e82
DE
3531 struct dwarf2_cu *cu = reader->cu;
3532 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3533 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3534 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3535 struct attribute *attr;
dee91e82 3536 int i;
7b9f3c50
DE
3537 void **slot;
3538 struct quick_file_names *qfn;
9291a0cd 3539
0186c6a7
DE
3540 gdb_assert (! this_cu->is_debug_types);
3541
07261596
TT
3542 /* Our callers never want to match partial units -- instead they
3543 will match the enclosing full CU. */
3544 if (comp_unit_die->tag == DW_TAG_partial_unit)
3545 {
3546 this_cu->v.quick->no_file_data = 1;
3547 return;
3548 }
3549
0186c6a7 3550 lh_cu = this_cu;
7b9f3c50 3551 slot = NULL;
dee91e82 3552
fff8551c 3553 line_header_up lh;
9c541725 3554 sect_offset line_offset {};
fff8551c 3555
dee91e82 3556 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3557 if (attr)
3558 {
7b9f3c50
DE
3559 struct quick_file_names find_entry;
3560
9c541725 3561 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3562
3563 /* We may have already read in this line header (TU line header sharing).
3564 If we have we're done. */
094b34ac 3565 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3566 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3567 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3568 &find_entry, INSERT);
3569 if (*slot != NULL)
3570 {
9a3c8263 3571 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3572 return;
7b9f3c50
DE
3573 }
3574
3019eac3 3575 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3576 }
3577 if (lh == NULL)
3578 {
094b34ac 3579 lh_cu->v.quick->no_file_data = 1;
dee91e82 3580 return;
9291a0cd
TT
3581 }
3582
8d749320 3583 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3584 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3585 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3586 gdb_assert (slot != NULL);
3587 *slot = qfn;
9291a0cd 3588
d721ba37 3589 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3590
fff8551c 3591 qfn->num_file_names = lh->file_names.size ();
8d749320 3592 qfn->file_names =
fff8551c
PA
3593 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3594 for (i = 0; i < lh->file_names.size (); ++i)
3595 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3596 qfn->real_names = NULL;
9291a0cd 3597
094b34ac 3598 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3599}
3600
3601/* A helper for the "quick" functions which attempts to read the line
3602 table for THIS_CU. */
3603
3604static struct quick_file_names *
e4a48d9d 3605dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3606{
0186c6a7
DE
3607 /* This should never be called for TUs. */
3608 gdb_assert (! this_cu->is_debug_types);
3609 /* Nor type unit groups. */
3610 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3611
dee91e82
DE
3612 if (this_cu->v.quick->file_names != NULL)
3613 return this_cu->v.quick->file_names;
3614 /* If we know there is no line data, no point in looking again. */
3615 if (this_cu->v.quick->no_file_data)
3616 return NULL;
3617
0186c6a7 3618 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3619
3620 if (this_cu->v.quick->no_file_data)
3621 return NULL;
3622 return this_cu->v.quick->file_names;
9291a0cd
TT
3623}
3624
3625/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3626 real path for a given file name from the line table. */
2fdf6df6 3627
9291a0cd 3628static const char *
7b9f3c50
DE
3629dw2_get_real_path (struct objfile *objfile,
3630 struct quick_file_names *qfn, int index)
9291a0cd 3631{
7b9f3c50
DE
3632 if (qfn->real_names == NULL)
3633 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3634 qfn->num_file_names, const char *);
9291a0cd 3635
7b9f3c50 3636 if (qfn->real_names[index] == NULL)
14278e1f 3637 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3638
7b9f3c50 3639 return qfn->real_names[index];
9291a0cd
TT
3640}
3641
3642static struct symtab *
3643dw2_find_last_source_symtab (struct objfile *objfile)
3644{
43f3e411 3645 struct compunit_symtab *cust;
9291a0cd 3646 int index;
ae2de4f8 3647
9291a0cd
TT
3648 dw2_setup (objfile);
3649 index = dwarf2_per_objfile->n_comp_units - 1;
43f3e411
DE
3650 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3651 if (cust == NULL)
3652 return NULL;
3653 return compunit_primary_filetab (cust);
9291a0cd
TT
3654}
3655
7b9f3c50
DE
3656/* Traversal function for dw2_forget_cached_source_info. */
3657
3658static int
3659dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3660{
7b9f3c50 3661 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3662
7b9f3c50 3663 if (file_data->real_names)
9291a0cd 3664 {
7b9f3c50 3665 int i;
9291a0cd 3666
7b9f3c50 3667 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3668 {
7b9f3c50
DE
3669 xfree ((void*) file_data->real_names[i]);
3670 file_data->real_names[i] = NULL;
9291a0cd
TT
3671 }
3672 }
7b9f3c50
DE
3673
3674 return 1;
3675}
3676
3677static void
3678dw2_forget_cached_source_info (struct objfile *objfile)
3679{
3680 dw2_setup (objfile);
3681
3682 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3683 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3684}
3685
f8eba3c6
TT
3686/* Helper function for dw2_map_symtabs_matching_filename that expands
3687 the symtabs and calls the iterator. */
3688
3689static int
3690dw2_map_expand_apply (struct objfile *objfile,
3691 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3692 const char *name, const char *real_path,
14bc53a8 3693 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3694{
43f3e411 3695 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3696
3697 /* Don't visit already-expanded CUs. */
43f3e411 3698 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3699 return 0;
3700
3701 /* This may expand more than one symtab, and we want to iterate over
3702 all of them. */
a0f42c21 3703 dw2_instantiate_symtab (per_cu);
f8eba3c6 3704
14bc53a8
PA
3705 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3706 last_made, callback);
f8eba3c6
TT
3707}
3708
3709/* Implementation of the map_symtabs_matching_filename method. */
3710
14bc53a8
PA
3711static bool
3712dw2_map_symtabs_matching_filename
3713 (struct objfile *objfile, const char *name, const char *real_path,
3714 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
3715{
3716 int i;
c011a4f4 3717 const char *name_basename = lbasename (name);
9291a0cd
TT
3718
3719 dw2_setup (objfile);
ae2de4f8 3720
848e3e78
DE
3721 /* The rule is CUs specify all the files, including those used by
3722 any TU, so there's no need to scan TUs here. */
f4dc4d17 3723
848e3e78 3724 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3725 {
3726 int j;
8832e7e3 3727 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3728 struct quick_file_names *file_data;
9291a0cd 3729
3d7bb9d9 3730 /* We only need to look at symtabs not already expanded. */
43f3e411 3731 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3732 continue;
3733
e4a48d9d 3734 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3735 if (file_data == NULL)
9291a0cd
TT
3736 continue;
3737
7b9f3c50 3738 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3739 {
7b9f3c50 3740 const char *this_name = file_data->file_names[j];
da235a7c 3741 const char *this_real_name;
9291a0cd 3742
af529f8f 3743 if (compare_filenames_for_search (this_name, name))
9291a0cd 3744 {
f5b95b50 3745 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3746 callback))
3747 return true;
288e77a7 3748 continue;
4aac40c8 3749 }
9291a0cd 3750
c011a4f4
DE
3751 /* Before we invoke realpath, which can get expensive when many
3752 files are involved, do a quick comparison of the basenames. */
3753 if (! basenames_may_differ
3754 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3755 continue;
3756
da235a7c
JK
3757 this_real_name = dw2_get_real_path (objfile, file_data, j);
3758 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3759 {
da235a7c 3760 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3761 callback))
3762 return true;
288e77a7 3763 continue;
da235a7c 3764 }
9291a0cd 3765
da235a7c
JK
3766 if (real_path != NULL)
3767 {
af529f8f
JK
3768 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3769 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3770 if (this_real_name != NULL
af529f8f 3771 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3772 {
f5b95b50 3773 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3774 callback))
3775 return true;
288e77a7 3776 continue;
9291a0cd
TT
3777 }
3778 }
3779 }
3780 }
3781
14bc53a8 3782 return false;
9291a0cd
TT
3783}
3784
da51c347
DE
3785/* Struct used to manage iterating over all CUs looking for a symbol. */
3786
3787struct dw2_symtab_iterator
9291a0cd 3788{
da51c347
DE
3789 /* The internalized form of .gdb_index. */
3790 struct mapped_index *index;
3791 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3792 int want_specific_block;
3793 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3794 Unused if !WANT_SPECIFIC_BLOCK. */
3795 int block_index;
3796 /* The kind of symbol we're looking for. */
3797 domain_enum domain;
3798 /* The list of CUs from the index entry of the symbol,
3799 or NULL if not found. */
3800 offset_type *vec;
3801 /* The next element in VEC to look at. */
3802 int next;
3803 /* The number of elements in VEC, or zero if there is no match. */
3804 int length;
8943b874
DE
3805 /* Have we seen a global version of the symbol?
3806 If so we can ignore all further global instances.
3807 This is to work around gold/15646, inefficient gold-generated
3808 indices. */
3809 int global_seen;
da51c347 3810};
9291a0cd 3811
da51c347
DE
3812/* Initialize the index symtab iterator ITER.
3813 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3814 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3815
9291a0cd 3816static void
da51c347
DE
3817dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3818 struct mapped_index *index,
3819 int want_specific_block,
3820 int block_index,
3821 domain_enum domain,
3822 const char *name)
3823{
3824 iter->index = index;
3825 iter->want_specific_block = want_specific_block;
3826 iter->block_index = block_index;
3827 iter->domain = domain;
3828 iter->next = 0;
8943b874 3829 iter->global_seen = 0;
da51c347
DE
3830
3831 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3832 iter->length = MAYBE_SWAP (*iter->vec);
3833 else
3834 {
3835 iter->vec = NULL;
3836 iter->length = 0;
3837 }
3838}
3839
3840/* Return the next matching CU or NULL if there are no more. */
3841
3842static struct dwarf2_per_cu_data *
3843dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3844{
3845 for ( ; iter->next < iter->length; ++iter->next)
3846 {
3847 offset_type cu_index_and_attrs =
3848 MAYBE_SWAP (iter->vec[iter->next + 1]);
3849 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3850 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3851 int want_static = iter->block_index != GLOBAL_BLOCK;
3852 /* This value is only valid for index versions >= 7. */
3853 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3854 gdb_index_symbol_kind symbol_kind =
3855 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3856 /* Only check the symbol attributes if they're present.
3857 Indices prior to version 7 don't record them,
3858 and indices >= 7 may elide them for certain symbols
3859 (gold does this). */
3860 int attrs_valid =
3861 (iter->index->version >= 7
3862 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3863
3190f0c6
DE
3864 /* Don't crash on bad data. */
3865 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3866 + dwarf2_per_objfile->n_type_units))
3867 {
3868 complaint (&symfile_complaints,
3869 _(".gdb_index entry has bad CU index"
4262abfb
JK
3870 " [in module %s]"),
3871 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3872 continue;
3873 }
3874
8832e7e3 3875 per_cu = dw2_get_cutu (cu_index);
3190f0c6 3876
da51c347 3877 /* Skip if already read in. */
43f3e411 3878 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3879 continue;
3880
8943b874
DE
3881 /* Check static vs global. */
3882 if (attrs_valid)
3883 {
3884 if (iter->want_specific_block
3885 && want_static != is_static)
3886 continue;
3887 /* Work around gold/15646. */
3888 if (!is_static && iter->global_seen)
3889 continue;
3890 if (!is_static)
3891 iter->global_seen = 1;
3892 }
da51c347
DE
3893
3894 /* Only check the symbol's kind if it has one. */
3895 if (attrs_valid)
3896 {
3897 switch (iter->domain)
3898 {
3899 case VAR_DOMAIN:
3900 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3901 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3902 /* Some types are also in VAR_DOMAIN. */
3903 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3904 continue;
3905 break;
3906 case STRUCT_DOMAIN:
3907 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3908 continue;
3909 break;
3910 case LABEL_DOMAIN:
3911 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3912 continue;
3913 break;
3914 default:
3915 break;
3916 }
3917 }
3918
3919 ++iter->next;
3920 return per_cu;
3921 }
3922
3923 return NULL;
3924}
3925
43f3e411 3926static struct compunit_symtab *
da51c347
DE
3927dw2_lookup_symbol (struct objfile *objfile, int block_index,
3928 const char *name, domain_enum domain)
9291a0cd 3929{
43f3e411 3930 struct compunit_symtab *stab_best = NULL;
156942c7
DE
3931 struct mapped_index *index;
3932
9291a0cd
TT
3933 dw2_setup (objfile);
3934
b5ec771e
PA
3935 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3936
156942c7
DE
3937 index = dwarf2_per_objfile->index_table;
3938
da51c347 3939 /* index is NULL if OBJF_READNOW. */
156942c7 3940 if (index)
9291a0cd 3941 {
da51c347
DE
3942 struct dw2_symtab_iterator iter;
3943 struct dwarf2_per_cu_data *per_cu;
3944
3945 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3946
da51c347 3947 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3948 {
b2e2f908 3949 struct symbol *sym, *with_opaque = NULL;
43f3e411
DE
3950 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3951 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
f194fefb 3952 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3953
b2e2f908
DE
3954 sym = block_find_symbol (block, name, domain,
3955 block_find_non_opaque_type_preferred,
3956 &with_opaque);
3957
da51c347
DE
3958 /* Some caution must be observed with overloaded functions
3959 and methods, since the index will not contain any overload
3960 information (but NAME might contain it). */
da51c347 3961
b2e2f908 3962 if (sym != NULL
b5ec771e 3963 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
b2e2f908
DE
3964 return stab;
3965 if (with_opaque != NULL
b5ec771e 3966 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
b2e2f908 3967 stab_best = stab;
da51c347
DE
3968
3969 /* Keep looking through other CUs. */
9291a0cd
TT
3970 }
3971 }
9291a0cd 3972
da51c347 3973 return stab_best;
9291a0cd
TT
3974}
3975
3976static void
3977dw2_print_stats (struct objfile *objfile)
3978{
e4a48d9d 3979 int i, total, count;
9291a0cd
TT
3980
3981 dw2_setup (objfile);
e4a48d9d 3982 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3983 count = 0;
e4a48d9d 3984 for (i = 0; i < total; ++i)
9291a0cd 3985 {
8832e7e3 3986 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3987
43f3e411 3988 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3989 ++count;
3990 }
e4a48d9d 3991 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3992 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3993}
3994
779bd270
DE
3995/* This dumps minimal information about the index.
3996 It is called via "mt print objfiles".
3997 One use is to verify .gdb_index has been loaded by the
3998 gdb.dwarf2/gdb-index.exp testcase. */
3999
9291a0cd
TT
4000static void
4001dw2_dump (struct objfile *objfile)
4002{
779bd270
DE
4003 dw2_setup (objfile);
4004 gdb_assert (dwarf2_per_objfile->using_index);
4005 printf_filtered (".gdb_index:");
4006 if (dwarf2_per_objfile->index_table != NULL)
4007 {
4008 printf_filtered (" version %d\n",
4009 dwarf2_per_objfile->index_table->version);
4010 }
4011 else
4012 printf_filtered (" faked for \"readnow\"\n");
4013 printf_filtered ("\n");
9291a0cd
TT
4014}
4015
4016static void
3189cb12
DE
4017dw2_relocate (struct objfile *objfile,
4018 const struct section_offsets *new_offsets,
4019 const struct section_offsets *delta)
9291a0cd
TT
4020{
4021 /* There's nothing to relocate here. */
4022}
4023
4024static void
4025dw2_expand_symtabs_for_function (struct objfile *objfile,
4026 const char *func_name)
4027{
da51c347
DE
4028 struct mapped_index *index;
4029
4030 dw2_setup (objfile);
4031
4032 index = dwarf2_per_objfile->index_table;
4033
4034 /* index is NULL if OBJF_READNOW. */
4035 if (index)
4036 {
4037 struct dw2_symtab_iterator iter;
4038 struct dwarf2_per_cu_data *per_cu;
4039
4040 /* Note: It doesn't matter what we pass for block_index here. */
4041 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4042 func_name);
4043
4044 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4045 dw2_instantiate_symtab (per_cu);
4046 }
9291a0cd
TT
4047}
4048
4049static void
4050dw2_expand_all_symtabs (struct objfile *objfile)
4051{
4052 int i;
4053
4054 dw2_setup (objfile);
1fd400ff
TT
4055
4056 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4057 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4058 {
8832e7e3 4059 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4060
a0f42c21 4061 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4062 }
4063}
4064
4065static void
652a8996
JK
4066dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4067 const char *fullname)
9291a0cd
TT
4068{
4069 int i;
4070
4071 dw2_setup (objfile);
d4637a04
DE
4072
4073 /* We don't need to consider type units here.
4074 This is only called for examining code, e.g. expand_line_sal.
4075 There can be an order of magnitude (or more) more type units
4076 than comp units, and we avoid them if we can. */
4077
4078 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4079 {
4080 int j;
8832e7e3 4081 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7b9f3c50 4082 struct quick_file_names *file_data;
9291a0cd 4083
3d7bb9d9 4084 /* We only need to look at symtabs not already expanded. */
43f3e411 4085 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4086 continue;
4087
e4a48d9d 4088 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4089 if (file_data == NULL)
9291a0cd
TT
4090 continue;
4091
7b9f3c50 4092 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4093 {
652a8996
JK
4094 const char *this_fullname = file_data->file_names[j];
4095
4096 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4097 {
a0f42c21 4098 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4099 break;
4100 }
4101 }
4102 }
4103}
4104
9291a0cd 4105static void
ade7ed9e 4106dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4107 const char * name, domain_enum domain,
ade7ed9e 4108 int global,
40658b94
PH
4109 int (*callback) (struct block *,
4110 struct symbol *, void *),
b5ec771e 4111 void *data, symbol_name_match_type match,
2edb89d3 4112 symbol_compare_ftype *ordered_compare)
9291a0cd 4113{
40658b94 4114 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4115 current language is Ada for a non-Ada objfile using GNU index. As Ada
4116 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4117}
4118
b5ec771e
PA
4119/* Symbol name matcher for .gdb_index names.
4120
4121 Symbol names in .gdb_index have a few particularities:
4122
4123 - There's no indication of which is the language of each symbol.
4124
4125 Since each language has its own symbol name matching algorithm,
4126 and we don't know which language is the right one, we must match
3f563c84
PA
4127 each symbol against all languages. This would be a potential
4128 performance problem if it were not mitigated by the
4129 mapped_index::name_components lookup table, which significantly
4130 reduces the number of times we need to call into this matcher,
4131 making it a non-issue.
b5ec771e
PA
4132
4133 - Symbol names in the index have no overload (parameter)
4134 information. I.e., in C++, "foo(int)" and "foo(long)" both
4135 appear as "foo" in the index, for example.
4136
4137 This means that the lookup names passed to the symbol name
4138 matcher functions must have no parameter information either
4139 because (e.g.) symbol search name "foo" does not match
4140 lookup-name "foo(int)" [while swapping search name for lookup
4141 name would match].
4142*/
4143class gdb_index_symbol_name_matcher
4144{
4145public:
4146 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4147 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4148
4149 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4150 Returns true if any matcher matches. */
4151 bool matches (const char *symbol_name);
4152
4153private:
4154 /* A reference to the lookup name we're matching against. */
4155 const lookup_name_info &m_lookup_name;
4156
4157 /* A vector holding all the different symbol name matchers, for all
4158 languages. */
4159 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4160};
4161
4162gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4163 (const lookup_name_info &lookup_name)
4164 : m_lookup_name (lookup_name)
4165{
4166 /* Prepare the vector of comparison functions upfront, to avoid
4167 doing the same work for each symbol. Care is taken to avoid
4168 matching with the same matcher more than once if/when multiple
4169 languages use the same matcher function. */
4170 auto &matchers = m_symbol_name_matcher_funcs;
4171 matchers.reserve (nr_languages);
4172
4173 matchers.push_back (default_symbol_name_matcher);
4174
4175 for (int i = 0; i < nr_languages; i++)
4176 {
4177 const language_defn *lang = language_def ((enum language) i);
4178 if (lang->la_get_symbol_name_matcher != NULL)
4179 {
4180 symbol_name_matcher_ftype *name_matcher
4181 = lang->la_get_symbol_name_matcher (m_lookup_name);
4182
4183 /* Don't insert the same comparison routine more than once.
4184 Note that we do this linear walk instead of a cheaper
4185 sorted insert, or use a std::set or something like that,
4186 because relative order of function addresses is not
4187 stable. This is not a problem in practice because the
4188 number of supported languages is low, and the cost here
4189 is tiny compared to the number of searches we'll do
4190 afterwards using this object. */
4191 if (std::find (matchers.begin (), matchers.end (), name_matcher)
4192 == matchers.end ())
4193 matchers.push_back (name_matcher);
4194 }
4195 }
4196}
4197
4198bool
4199gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4200{
4201 for (auto matches_name : m_symbol_name_matcher_funcs)
4202 if (matches_name (symbol_name, m_lookup_name, NULL))
4203 return true;
4204
4205 return false;
4206}
4207
e1ef7d7a
PA
4208/* Starting from a search name, return the string that finds the upper
4209 bound of all strings that start with SEARCH_NAME in a sorted name
4210 list. Returns the empty string to indicate that the upper bound is
4211 the end of the list. */
4212
4213static std::string
4214make_sort_after_prefix_name (const char *search_name)
4215{
4216 /* When looking to complete "func", we find the upper bound of all
4217 symbols that start with "func" by looking for where we'd insert
4218 the closest string that would follow "func" in lexicographical
4219 order. Usually, that's "func"-with-last-character-incremented,
4220 i.e. "fund". Mind non-ASCII characters, though. Usually those
4221 will be UTF-8 multi-byte sequences, but we can't be certain.
4222 Especially mind the 0xff character, which is a valid character in
4223 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4224 rule out compilers allowing it in identifiers. Note that
4225 conveniently, strcmp/strcasecmp are specified to compare
4226 characters interpreted as unsigned char. So what we do is treat
4227 the whole string as a base 256 number composed of a sequence of
4228 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4229 to 0, and carries 1 to the following more-significant position.
4230 If the very first character in SEARCH_NAME ends up incremented
4231 and carries/overflows, then the upper bound is the end of the
4232 list. The string after the empty string is also the empty
4233 string.
4234
4235 Some examples of this operation:
4236
4237 SEARCH_NAME => "+1" RESULT
4238
4239 "abc" => "abd"
4240 "ab\xff" => "ac"
4241 "\xff" "a" "\xff" => "\xff" "b"
4242 "\xff" => ""
4243 "\xff\xff" => ""
4244 "" => ""
4245
4246 Then, with these symbols for example:
4247
4248 func
4249 func1
4250 fund
4251
4252 completing "func" looks for symbols between "func" and
4253 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4254 which finds "func" and "func1", but not "fund".
4255
4256 And with:
4257
4258 funcÿ (Latin1 'ÿ' [0xff])
4259 funcÿ1
4260 fund
4261
4262 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4263 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4264
4265 And with:
4266
4267 ÿÿ (Latin1 'ÿ' [0xff])
4268 ÿÿ1
4269
4270 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4271 the end of the list.
4272 */
4273 std::string after = search_name;
4274 while (!after.empty () && (unsigned char) after.back () == 0xff)
4275 after.pop_back ();
4276 if (!after.empty ())
4277 after.back () = (unsigned char) after.back () + 1;
4278 return after;
4279}
4280
5c58de74 4281/* See declaration. */
61d96d7e 4282
5c58de74
PA
4283std::pair<std::vector<name_component>::const_iterator,
4284 std::vector<name_component>::const_iterator>
4285mapped_index::find_name_components_bounds
4286 (const lookup_name_info &lookup_name_without_params) const
3f563c84 4287{
5c58de74
PA
4288 auto *name_cmp
4289 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84
PA
4290
4291 const char *cplus
c62446b1 4292 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4293
3f563c84
PA
4294 /* Comparison function object for lower_bound that matches against a
4295 given symbol name. */
4296 auto lookup_compare_lower = [&] (const name_component &elem,
4297 const char *name)
4298 {
5c58de74 4299 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4300 const char *elem_name = elem_qualified + elem.name_offset;
4301 return name_cmp (elem_name, name) < 0;
4302 };
4303
4304 /* Comparison function object for upper_bound that matches against a
4305 given symbol name. */
4306 auto lookup_compare_upper = [&] (const char *name,
4307 const name_component &elem)
4308 {
5c58de74 4309 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4310 const char *elem_name = elem_qualified + elem.name_offset;
4311 return name_cmp (name, elem_name) < 0;
4312 };
4313
5c58de74
PA
4314 auto begin = this->name_components.begin ();
4315 auto end = this->name_components.end ();
3f563c84
PA
4316
4317 /* Find the lower bound. */
4318 auto lower = [&] ()
4319 {
5c58de74 4320 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4321 return begin;
4322 else
4323 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4324 } ();
4325
4326 /* Find the upper bound. */
4327 auto upper = [&] ()
4328 {
5c58de74 4329 if (lookup_name_without_params.completion_mode ())
3f563c84 4330 {
e1ef7d7a
PA
4331 /* In completion mode, we want UPPER to point past all
4332 symbols names that have the same prefix. I.e., with
4333 these symbols, and completing "func":
4334
4335 function << lower bound
4336 function1
4337 other_function << upper bound
4338
4339 We find the upper bound by looking for the insertion
4340 point of "func"-with-last-character-incremented,
4341 i.e. "fund". */
4342 std::string after = make_sort_after_prefix_name (cplus);
4343 if (after.empty ())
3f563c84 4344 return end;
e6b2f5ef
PA
4345 return std::lower_bound (lower, end, after.c_str (),
4346 lookup_compare_lower);
3f563c84
PA
4347 }
4348 else
4349 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4350 } ();
4351
5c58de74
PA
4352 return {lower, upper};
4353}
4354
4355/* See declaration. */
4356
4357void
4358mapped_index::build_name_components ()
4359{
4360 if (!this->name_components.empty ())
4361 return;
4362
4363 this->name_components_casing = case_sensitivity;
4364 auto *name_cmp
4365 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4366
4367 /* The code below only knows how to break apart components of C++
4368 symbol names (and other languages that use '::' as
4369 namespace/module separator). If we add support for wild matching
4370 to some language that uses some other operator (E.g., Ada, Go and
4371 D use '.'), then we'll need to try splitting the symbol name
4372 according to that language too. Note that Ada does support wild
4373 matching, but doesn't currently support .gdb_index. */
4374 for (size_t iter = 0; iter < this->symbol_table_slots; ++iter)
4375 {
4376 offset_type idx = 2 * iter;
4377
4378 if (this->symbol_table[idx] == 0
4379 && this->symbol_table[idx + 1] == 0)
4380 continue;
4381
4382 const char *name = this->symbol_name_at (idx);
4383
4384 /* Add each name component to the name component table. */
4385 unsigned int previous_len = 0;
4386 for (unsigned int current_len = cp_find_first_component (name);
4387 name[current_len] != '\0';
4388 current_len += cp_find_first_component (name + current_len))
4389 {
4390 gdb_assert (name[current_len] == ':');
4391 this->name_components.push_back ({previous_len, idx});
4392 /* Skip the '::'. */
4393 current_len += 2;
4394 previous_len = current_len;
4395 }
4396 this->name_components.push_back ({previous_len, idx});
4397 }
4398
4399 /* Sort name_components elements by name. */
4400 auto name_comp_compare = [&] (const name_component &left,
4401 const name_component &right)
4402 {
4403 const char *left_qualified = this->symbol_name_at (left.idx);
4404 const char *right_qualified = this->symbol_name_at (right.idx);
4405
4406 const char *left_name = left_qualified + left.name_offset;
4407 const char *right_name = right_qualified + right.name_offset;
4408
4409 return name_cmp (left_name, right_name) < 0;
4410 };
4411
4412 std::sort (this->name_components.begin (),
4413 this->name_components.end (),
4414 name_comp_compare);
4415}
4416
4417/* Helper for dw2_expand_symtabs_matching that works with a
4418 mapped_index instead of the containing objfile. This is split to a
4419 separate function in order to be able to unit test the
4420 name_components matching using a mock mapped_index. For each
4421 symbol name that matches, calls MATCH_CALLBACK, passing it the
4422 symbol's index in the mapped_index symbol table. */
4423
4424static void
4425dw2_expand_symtabs_matching_symbol
4426 (mapped_index &index,
4427 const lookup_name_info &lookup_name_in,
4428 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4429 enum search_domain kind,
4430 gdb::function_view<void (offset_type)> match_callback)
4431{
4432 lookup_name_info lookup_name_without_params
4433 = lookup_name_in.make_ignore_params ();
4434 gdb_index_symbol_name_matcher lookup_name_matcher
4435 (lookup_name_without_params);
4436
4437 /* Build the symbol name component sorted vector, if we haven't
4438 yet. */
4439 index.build_name_components ();
4440
4441 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4442
3f563c84
PA
4443 /* Now for each symbol name in range, check to see if we have a name
4444 match, and if so, call the MATCH_CALLBACK callback. */
4445
4446 /* The same symbol may appear more than once in the range though.
4447 E.g., if we're looking for symbols that complete "w", and we have
4448 a symbol named "w1::w2", we'll find the two name components for
4449 that same symbol in the range. To be sure we only call the
4450 callback once per symbol, we first collect the symbol name
4451 indexes that matched in a temporary vector and ignore
4452 duplicates. */
4453 std::vector<offset_type> matches;
5c58de74 4454 matches.reserve (std::distance (bounds.first, bounds.second));
3f563c84 4455
5c58de74 4456 for (; bounds.first != bounds.second; ++bounds.first)
3f563c84 4457 {
5c58de74 4458 const char *qualified = index.symbol_name_at (bounds.first->idx);
3f563c84
PA
4459
4460 if (!lookup_name_matcher.matches (qualified)
4461 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4462 continue;
4463
5c58de74 4464 matches.push_back (bounds.first->idx);
3f563c84
PA
4465 }
4466
4467 std::sort (matches.begin (), matches.end ());
4468
4469 /* Finally call the callback, once per match. */
4470 ULONGEST prev = -1;
4471 for (offset_type idx : matches)
4472 {
4473 if (prev != idx)
4474 {
4475 match_callback (idx);
4476 prev = idx;
4477 }
4478 }
4479
4480 /* Above we use a type wider than idx's for 'prev', since 0 and
4481 (offset_type)-1 are both possible values. */
4482 static_assert (sizeof (prev) > sizeof (offset_type), "");
4483}
4484
c62446b1
PA
4485#if GDB_SELF_TEST
4486
4487namespace selftests { namespace dw2_expand_symtabs_matching {
4488
4489/* A wrapper around mapped_index that builds a mock mapped_index, from
4490 the symbol list passed as parameter to the constructor. */
4491class mock_mapped_index
4492{
4493public:
4494 template<size_t N>
4495 mock_mapped_index (const char *(&symbols)[N])
4496 : mock_mapped_index (symbols, N)
4497 {}
4498
4499 /* Access the built index. */
4500 mapped_index &index ()
4501 { return m_index; }
4502
4503 /* Disable copy. */
4504 mock_mapped_index(const mock_mapped_index &) = delete;
4505 void operator= (const mock_mapped_index &) = delete;
4506
4507private:
4508 mock_mapped_index (const char **symbols, size_t symbols_size)
4509 {
4510 /* No string can live at offset zero. Add a dummy entry. */
4511 obstack_grow_str0 (&m_constant_pool, "");
4512
4513 for (size_t i = 0; i < symbols_size; i++)
4514 {
4515 const char *sym = symbols[i];
4516 size_t offset = obstack_object_size (&m_constant_pool);
4517 obstack_grow_str0 (&m_constant_pool, sym);
4518 m_symbol_table.push_back (offset);
4519 m_symbol_table.push_back (0);
4520 };
4521
4522 m_index.constant_pool = (const char *) obstack_base (&m_constant_pool);
4523 m_index.symbol_table = m_symbol_table.data ();
4524 m_index.symbol_table_slots = m_symbol_table.size () / 2;
4525 }
4526
4527public:
4528 /* The built mapped_index. */
4529 mapped_index m_index{};
4530
4531 /* The storage that the built mapped_index uses for symbol and
4532 constant pool tables. */
4533 std::vector<offset_type> m_symbol_table;
4534 auto_obstack m_constant_pool;
4535};
4536
4537/* Convenience function that converts a NULL pointer to a "<null>"
4538 string, to pass to print routines. */
4539
4540static const char *
4541string_or_null (const char *str)
4542{
4543 return str != NULL ? str : "<null>";
4544}
4545
4546/* Check if a lookup_name_info built from
4547 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4548 index. EXPECTED_LIST is the list of expected matches, in expected
4549 matching order. If no match expected, then an empty list is
4550 specified. Returns true on success. On failure prints a warning
4551 indicating the file:line that failed, and returns false. */
4552
4553static bool
4554check_match (const char *file, int line,
4555 mock_mapped_index &mock_index,
4556 const char *name, symbol_name_match_type match_type,
4557 bool completion_mode,
4558 std::initializer_list<const char *> expected_list)
4559{
4560 lookup_name_info lookup_name (name, match_type, completion_mode);
4561
4562 bool matched = true;
4563
4564 auto mismatch = [&] (const char *expected_str,
4565 const char *got)
4566 {
4567 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4568 "expected=\"%s\", got=\"%s\"\n"),
4569 file, line,
4570 (match_type == symbol_name_match_type::FULL
4571 ? "FULL" : "WILD"),
4572 name, string_or_null (expected_str), string_or_null (got));
4573 matched = false;
4574 };
4575
4576 auto expected_it = expected_list.begin ();
4577 auto expected_end = expected_list.end ();
4578
4579 dw2_expand_symtabs_matching_symbol (mock_index.index (), lookup_name,
4580 NULL, ALL_DOMAIN,
4581 [&] (offset_type idx)
4582 {
4583 const char *matched_name = mock_index.index ().symbol_name_at (idx);
4584 const char *expected_str
4585 = expected_it == expected_end ? NULL : *expected_it++;
4586
4587 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4588 mismatch (expected_str, matched_name);
4589 });
4590
4591 const char *expected_str
4592 = expected_it == expected_end ? NULL : *expected_it++;
4593 if (expected_str != NULL)
4594 mismatch (expected_str, NULL);
4595
4596 return matched;
4597}
4598
4599/* The symbols added to the mock mapped_index for testing (in
4600 canonical form). */
4601static const char *test_symbols[] = {
4602 "function",
4603 "std::bar",
4604 "std::zfunction",
4605 "std::zfunction2",
4606 "w1::w2",
4607 "ns::foo<char*>",
4608 "ns::foo<int>",
4609 "ns::foo<long>",
a20714ff
PA
4610 "ns2::tmpl<int>::foo2",
4611 "(anonymous namespace)::A::B::C",
c62446b1 4612
e1ef7d7a
PA
4613 /* These are used to check that the increment-last-char in the
4614 matching algorithm for completion doesn't match "t1_fund" when
4615 completing "t1_func". */
4616 "t1_func",
4617 "t1_func1",
4618 "t1_fund",
4619 "t1_fund1",
4620
4621 /* A UTF-8 name with multi-byte sequences to make sure that
4622 cp-name-parser understands this as a single identifier ("função"
4623 is "function" in PT). */
4624 u8"u8função",
4625
4626 /* \377 (0xff) is Latin1 'ÿ'. */
4627 "yfunc\377",
4628
4629 /* \377 (0xff) is Latin1 'ÿ'. */
4630 "\377",
4631 "\377\377123",
4632
c62446b1
PA
4633 /* A name with all sorts of complications. Starts with "z" to make
4634 it easier for the completion tests below. */
4635#define Z_SYM_NAME \
4636 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4637 "::tuple<(anonymous namespace)::ui*, " \
4638 "std::default_delete<(anonymous namespace)::ui>, void>"
4639
4640 Z_SYM_NAME
4641};
4642
5c58de74
PA
4643/* Returns true if the mapped_index::find_name_component_bounds method
4644 finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME, in
4645 completion mode. */
4646
4647static bool
4648check_find_bounds_finds (mapped_index &index,
4649 const char *search_name,
4650 gdb::array_view<const char *> expected_syms)
4651{
4652 lookup_name_info lookup_name (search_name,
4653 symbol_name_match_type::FULL, true);
4654
4655 auto bounds = index.find_name_components_bounds (lookup_name);
4656
4657 size_t distance = std::distance (bounds.first, bounds.second);
4658 if (distance != expected_syms.size ())
4659 return false;
4660
4661 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4662 {
4663 auto nc_elem = bounds.first + exp_elem;
4664 const char *qualified = index.symbol_name_at (nc_elem->idx);
4665 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4666 return false;
4667 }
4668
4669 return true;
4670}
4671
4672/* Test the lower-level mapped_index::find_name_component_bounds
4673 method. */
4674
c62446b1 4675static void
5c58de74
PA
4676test_mapped_index_find_name_component_bounds ()
4677{
4678 mock_mapped_index mock_index (test_symbols);
4679
4680 mock_index.index ().build_name_components ();
4681
4682 /* Test the lower-level mapped_index::find_name_component_bounds
4683 method in completion mode. */
4684 {
4685 static const char *expected_syms[] = {
4686 "t1_func",
4687 "t1_func1",
5c58de74
PA
4688 };
4689
4690 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
4691 "t1_func", expected_syms));
4692 }
4693
4694 /* Check that the increment-last-char in the name matching algorithm
4695 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4696 {
4697 static const char *expected_syms1[] = {
4698 "\377",
4699 "\377\377123",
4700 };
4701 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
4702 "\377", expected_syms1));
4703
4704 static const char *expected_syms2[] = {
4705 "\377\377123",
4706 };
4707 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
4708 "\377\377", expected_syms2));
4709 }
4710}
4711
4712/* Test dw2_expand_symtabs_matching_symbol. */
4713
4714static void
4715test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4716{
4717 mock_mapped_index mock_index (test_symbols);
4718
4719 /* We let all tests run until the end even if some fails, for debug
4720 convenience. */
4721 bool any_mismatch = false;
4722
4723 /* Create the expected symbols list (an initializer_list). Needed
4724 because lists have commas, and we need to pass them to CHECK,
4725 which is a macro. */
4726#define EXPECT(...) { __VA_ARGS__ }
4727
4728 /* Wrapper for check_match that passes down the current
4729 __FILE__/__LINE__. */
4730#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4731 any_mismatch |= !check_match (__FILE__, __LINE__, \
4732 mock_index, \
4733 NAME, MATCH_TYPE, COMPLETION_MODE, \
4734 EXPECTED_LIST)
4735
4736 /* Identity checks. */
4737 for (const char *sym : test_symbols)
4738 {
4739 /* Should be able to match all existing symbols. */
4740 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4741 EXPECT (sym));
4742
4743 /* Should be able to match all existing symbols with
4744 parameters. */
4745 std::string with_params = std::string (sym) + "(int)";
4746 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4747 EXPECT (sym));
4748
4749 /* Should be able to match all existing symbols with
4750 parameters and qualifiers. */
4751 with_params = std::string (sym) + " ( int ) const";
4752 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4753 EXPECT (sym));
4754
4755 /* This should really find sym, but cp-name-parser.y doesn't
4756 know about lvalue/rvalue qualifiers yet. */
4757 with_params = std::string (sym) + " ( int ) &&";
4758 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4759 {});
4760 }
4761
e1ef7d7a
PA
4762 /* Check that the name matching algorithm for completion doesn't get
4763 confused with Latin1 'ÿ' / 0xff. */
4764 {
4765 static const char str[] = "\377";
4766 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4767 EXPECT ("\377", "\377\377123"));
4768 }
4769
4770 /* Check that the increment-last-char in the matching algorithm for
4771 completion doesn't match "t1_fund" when completing "t1_func". */
4772 {
4773 static const char str[] = "t1_func";
4774 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4775 EXPECT ("t1_func", "t1_func1"));
4776 }
4777
c62446b1
PA
4778 /* Check that completion mode works at each prefix of the expected
4779 symbol name. */
4780 {
4781 static const char str[] = "function(int)";
4782 size_t len = strlen (str);
4783 std::string lookup;
4784
4785 for (size_t i = 1; i < len; i++)
4786 {
4787 lookup.assign (str, i);
4788 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4789 EXPECT ("function"));
4790 }
4791 }
4792
4793 /* While "w" is a prefix of both components, the match function
4794 should still only be called once. */
4795 {
4796 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4797 EXPECT ("w1::w2"));
a20714ff
PA
4798 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4799 EXPECT ("w1::w2"));
c62446b1
PA
4800 }
4801
4802 /* Same, with a "complicated" symbol. */
4803 {
4804 static const char str[] = Z_SYM_NAME;
4805 size_t len = strlen (str);
4806 std::string lookup;
4807
4808 for (size_t i = 1; i < len; i++)
4809 {
4810 lookup.assign (str, i);
4811 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4812 EXPECT (Z_SYM_NAME));
4813 }
4814 }
4815
4816 /* In FULL mode, an incomplete symbol doesn't match. */
4817 {
4818 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4819 {});
4820 }
4821
4822 /* A complete symbol with parameters matches any overload, since the
4823 index has no overload info. */
4824 {
4825 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4826 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4827 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4828 EXPECT ("std::zfunction", "std::zfunction2"));
4829 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4830 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4831 }
4832
4833 /* Check that whitespace is ignored appropriately. A symbol with a
4834 template argument list. */
4835 {
4836 static const char expected[] = "ns::foo<int>";
4837 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4838 EXPECT (expected));
a20714ff
PA
4839 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4840 EXPECT (expected));
c62446b1
PA
4841 }
4842
4843 /* Check that whitespace is ignored appropriately. A symbol with a
4844 template argument list that includes a pointer. */
4845 {
4846 static const char expected[] = "ns::foo<char*>";
4847 /* Try both completion and non-completion modes. */
4848 static const bool completion_mode[2] = {false, true};
4849 for (size_t i = 0; i < 2; i++)
4850 {
4851 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4852 completion_mode[i], EXPECT (expected));
a20714ff
PA
4853 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4854 completion_mode[i], EXPECT (expected));
c62446b1
PA
4855
4856 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4857 completion_mode[i], EXPECT (expected));
a20714ff
PA
4858 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4859 completion_mode[i], EXPECT (expected));
c62446b1
PA
4860 }
4861 }
4862
4863 {
4864 /* Check method qualifiers are ignored. */
4865 static const char expected[] = "ns::foo<char*>";
4866 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4867 symbol_name_match_type::FULL, true, EXPECT (expected));
4868 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4869 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4870 CHECK_MATCH ("foo < char * > ( int ) const",
4871 symbol_name_match_type::WILD, true, EXPECT (expected));
4872 CHECK_MATCH ("foo < char * > ( int ) &&",
4873 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4874 }
4875
4876 /* Test lookup names that don't match anything. */
4877 {
a20714ff
PA
4878 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4879 {});
4880
c62446b1
PA
4881 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4882 {});
4883 }
4884
a20714ff
PA
4885 /* Some wild matching tests, exercising "(anonymous namespace)",
4886 which should not be confused with a parameter list. */
4887 {
4888 static const char *syms[] = {
4889 "A::B::C",
4890 "B::C",
4891 "C",
4892 "A :: B :: C ( int )",
4893 "B :: C ( int )",
4894 "C ( int )",
4895 };
4896
4897 for (const char *s : syms)
4898 {
4899 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4900 EXPECT ("(anonymous namespace)::A::B::C"));
4901 }
4902 }
4903
4904 {
4905 static const char expected[] = "ns2::tmpl<int>::foo2";
4906 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4907 EXPECT (expected));
4908 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4909 EXPECT (expected));
4910 }
4911
c62446b1
PA
4912 SELF_CHECK (!any_mismatch);
4913
4914#undef EXPECT
4915#undef CHECK_MATCH
4916}
4917
5c58de74
PA
4918static void
4919run_test ()
4920{
4921 test_mapped_index_find_name_component_bounds ();
4922 test_dw2_expand_symtabs_matching_symbol ();
4923}
4924
c62446b1
PA
4925}} // namespace selftests::dw2_expand_symtabs_matching
4926
4927#endif /* GDB_SELF_TEST */
4928
3f563c84
PA
4929/* Helper for dw2_expand_matching symtabs. Called on each symbol
4930 matched, to expand corresponding CUs that were marked. IDX is the
4931 index of the symbol name that matched. */
4932
4933static void
4934dw2_expand_marked_cus
4935 (mapped_index &index, offset_type idx,
4936 struct objfile *objfile,
4937 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4938 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4939 search_domain kind)
4940{
3f563c84
PA
4941 offset_type *vec, vec_len, vec_idx;
4942 bool global_seen = false;
4943
61920122
PA
4944 vec = (offset_type *) (index.constant_pool
4945 + MAYBE_SWAP (index.symbol_table[idx + 1]));
4946 vec_len = MAYBE_SWAP (vec[0]);
4947 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4948 {
4949 struct dwarf2_per_cu_data *per_cu;
4950 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4951 /* This value is only valid for index versions >= 7. */
4952 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4953 gdb_index_symbol_kind symbol_kind =
4954 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4955 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4956 /* Only check the symbol attributes if they're present.
4957 Indices prior to version 7 don't record them,
4958 and indices >= 7 may elide them for certain symbols
4959 (gold does this). */
4960 int attrs_valid =
4961 (index.version >= 7
4962 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4963
4964 /* Work around gold/15646. */
4965 if (attrs_valid)
9291a0cd 4966 {
61920122
PA
4967 if (!is_static && global_seen)
4968 continue;
4969 if (!is_static)
4970 global_seen = true;
4971 }
3190f0c6 4972
61920122
PA
4973 /* Only check the symbol's kind if it has one. */
4974 if (attrs_valid)
4975 {
4976 switch (kind)
8943b874 4977 {
61920122
PA
4978 case VARIABLES_DOMAIN:
4979 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4980 continue;
4981 break;
4982 case FUNCTIONS_DOMAIN:
4983 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4984 continue;
61920122
PA
4985 break;
4986 case TYPES_DOMAIN:
4987 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4988 continue;
4989 break;
4990 default:
4991 break;
8943b874 4992 }
61920122 4993 }
8943b874 4994
61920122
PA
4995 /* Don't crash on bad data. */
4996 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4997 + dwarf2_per_objfile->n_type_units))
4998 {
4999 complaint (&symfile_complaints,
5000 _(".gdb_index entry has bad CU index"
5001 " [in module %s]"), objfile_name (objfile));
5002 continue;
5003 }
5004
5005 per_cu = dw2_get_cutu (cu_index);
5006 if (file_matcher == NULL || per_cu->v.quick->mark)
5007 {
5008 int symtab_was_null =
5009 (per_cu->v.quick->compunit_symtab == NULL);
5010
5011 dw2_instantiate_symtab (per_cu);
5012
5013 if (expansion_notify != NULL
5014 && symtab_was_null
5015 && per_cu->v.quick->compunit_symtab != NULL)
5016 expansion_notify (per_cu->v.quick->compunit_symtab);
5017 }
5018 }
5019}
5020
5021static void
5022dw2_expand_symtabs_matching
5023 (struct objfile *objfile,
5024 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5025 const lookup_name_info &lookup_name,
5026 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5027 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5028 enum search_domain kind)
5029{
5030 int i;
61920122
PA
5031
5032 dw2_setup (objfile);
5033
5034 /* index_table is NULL if OBJF_READNOW. */
5035 if (!dwarf2_per_objfile->index_table)
5036 return;
5037
5038 if (file_matcher != NULL)
5039 {
5040 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5041 htab_eq_pointer,
5042 NULL, xcalloc, xfree));
5043 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5044 htab_eq_pointer,
5045 NULL, xcalloc, xfree));
5046
5047 /* The rule is CUs specify all the files, including those used by
5048 any TU, so there's no need to scan TUs here. */
5049
5050 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5051 {
5052 int j;
5053 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5054 struct quick_file_names *file_data;
5055 void **slot;
5056
5057 QUIT;
5058
5059 per_cu->v.quick->mark = 0;
156942c7 5060
61920122
PA
5061 /* We only need to look at symtabs not already expanded. */
5062 if (per_cu->v.quick->compunit_symtab)
5063 continue;
5064
5065 file_data = dw2_get_file_names (per_cu);
5066 if (file_data == NULL)
5067 continue;
5068
5069 if (htab_find (visited_not_found.get (), file_data) != NULL)
5070 continue;
5071 else if (htab_find (visited_found.get (), file_data) != NULL)
3190f0c6 5072 {
61920122 5073 per_cu->v.quick->mark = 1;
3190f0c6
DE
5074 continue;
5075 }
5076
61920122 5077 for (j = 0; j < file_data->num_file_names; ++j)
276d885b 5078 {
61920122 5079 const char *this_real_name;
276d885b 5080
61920122
PA
5081 if (file_matcher (file_data->file_names[j], false))
5082 {
5083 per_cu->v.quick->mark = 1;
5084 break;
5085 }
5086
5087 /* Before we invoke realpath, which can get expensive when many
5088 files are involved, do a quick comparison of the basenames. */
5089 if (!basenames_may_differ
5090 && !file_matcher (lbasename (file_data->file_names[j]),
5091 true))
5092 continue;
276d885b 5093
61920122
PA
5094 this_real_name = dw2_get_real_path (objfile, file_data, j);
5095 if (file_matcher (this_real_name, false))
276d885b 5096 {
61920122
PA
5097 per_cu->v.quick->mark = 1;
5098 break;
276d885b
GB
5099 }
5100 }
61920122
PA
5101
5102 slot = htab_find_slot (per_cu->v.quick->mark
5103 ? visited_found.get ()
5104 : visited_not_found.get (),
5105 file_data, INSERT);
5106 *slot = file_data;
9291a0cd 5107 }
61920122
PA
5108 }
5109
5110 mapped_index &index = *dwarf2_per_objfile->index_table;
5111
5112 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5113 symbol_matcher,
5114 kind, [&] (offset_type idx)
5115 {
5116 dw2_expand_marked_cus (index, idx, objfile, file_matcher,
5117 expansion_notify, kind);
5118 });
9291a0cd
TT
5119}
5120
43f3e411 5121/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
9703b513
TT
5122 symtab. */
5123
43f3e411
DE
5124static struct compunit_symtab *
5125recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5126 CORE_ADDR pc)
9703b513
TT
5127{
5128 int i;
5129
43f3e411
DE
5130 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5131 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5132 return cust;
9703b513 5133
43f3e411 5134 if (cust->includes == NULL)
a3ec0bb1
DE
5135 return NULL;
5136
43f3e411 5137 for (i = 0; cust->includes[i]; ++i)
9703b513 5138 {
43f3e411 5139 struct compunit_symtab *s = cust->includes[i];
9703b513 5140
43f3e411 5141 s = recursively_find_pc_sect_compunit_symtab (s, pc);
9703b513
TT
5142 if (s != NULL)
5143 return s;
5144 }
5145
5146 return NULL;
5147}
5148
43f3e411
DE
5149static struct compunit_symtab *
5150dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5151 struct bound_minimal_symbol msymbol,
5152 CORE_ADDR pc,
5153 struct obj_section *section,
5154 int warn_if_readin)
9291a0cd
TT
5155{
5156 struct dwarf2_per_cu_data *data;
43f3e411 5157 struct compunit_symtab *result;
9291a0cd
TT
5158
5159 dw2_setup (objfile);
5160
5161 if (!objfile->psymtabs_addrmap)
5162 return NULL;
5163
9a3c8263
SM
5164 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5165 pc);
9291a0cd
TT
5166 if (!data)
5167 return NULL;
5168
43f3e411 5169 if (warn_if_readin && data->v.quick->compunit_symtab)
abebb8b0 5170 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
5171 paddress (get_objfile_arch (objfile), pc));
5172
43f3e411
DE
5173 result
5174 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5175 pc);
9703b513
TT
5176 gdb_assert (result != NULL);
5177 return result;
9291a0cd
TT
5178}
5179
9291a0cd 5180static void
44b13c5a 5181dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 5182 void *data, int need_fullname)
9291a0cd 5183{
9291a0cd 5184 dw2_setup (objfile);
ae2de4f8 5185
bbf2f4df 5186 if (!dwarf2_per_objfile->filenames_cache)
24c79950 5187 {
bbf2f4df 5188 dwarf2_per_objfile->filenames_cache.emplace ();
24c79950 5189
bbf2f4df
PA
5190 htab_up visited (htab_create_alloc (10,
5191 htab_hash_pointer, htab_eq_pointer,
5192 NULL, xcalloc, xfree));
24c79950 5193
bbf2f4df
PA
5194 /* The rule is CUs specify all the files, including those used
5195 by any TU, so there's no need to scan TUs here. We can
5196 ignore file names coming from already-expanded CUs. */
24c79950 5197
bbf2f4df
PA
5198 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5199 {
5200 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 5201
bbf2f4df
PA
5202 if (per_cu->v.quick->compunit_symtab)
5203 {
5204 void **slot = htab_find_slot (visited.get (),
5205 per_cu->v.quick->file_names,
5206 INSERT);
9291a0cd 5207
bbf2f4df
PA
5208 *slot = per_cu->v.quick->file_names;
5209 }
24c79950 5210 }
24c79950 5211
bbf2f4df 5212 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 5213 {
bbf2f4df
PA
5214 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5215 struct quick_file_names *file_data;
5216 void **slot;
5217
5218 /* We only need to look at symtabs not already expanded. */
5219 if (per_cu->v.quick->compunit_symtab)
5220 continue;
74e2f255 5221
bbf2f4df
PA
5222 file_data = dw2_get_file_names (per_cu);
5223 if (file_data == NULL)
5224 continue;
5225
5226 slot = htab_find_slot (visited.get (), file_data, INSERT);
5227 if (*slot)
5228 {
5229 /* Already visited. */
5230 continue;
5231 }
5232 *slot = file_data;
5233
5234 for (int j = 0; j < file_data->num_file_names; ++j)
5235 {
5236 const char *filename = file_data->file_names[j];
5237 dwarf2_per_objfile->filenames_cache->seen (filename);
5238 }
9291a0cd
TT
5239 }
5240 }
bbf2f4df
PA
5241
5242 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5243 {
14278e1f 5244 gdb::unique_xmalloc_ptr<char> this_real_name;
bbf2f4df
PA
5245
5246 if (need_fullname)
5247 this_real_name = gdb_realpath (filename);
14278e1f 5248 (*fun) (filename, this_real_name.get (), data);
bbf2f4df 5249 });
9291a0cd
TT
5250}
5251
5252static int
5253dw2_has_symbols (struct objfile *objfile)
5254{
5255 return 1;
5256}
5257
5258const struct quick_symbol_functions dwarf2_gdb_index_functions =
5259{
5260 dw2_has_symbols,
5261 dw2_find_last_source_symtab,
5262 dw2_forget_cached_source_info,
f8eba3c6 5263 dw2_map_symtabs_matching_filename,
9291a0cd 5264 dw2_lookup_symbol,
9291a0cd
TT
5265 dw2_print_stats,
5266 dw2_dump,
5267 dw2_relocate,
5268 dw2_expand_symtabs_for_function,
5269 dw2_expand_all_symtabs,
652a8996 5270 dw2_expand_symtabs_with_fullname,
40658b94 5271 dw2_map_matching_symbols,
9291a0cd 5272 dw2_expand_symtabs_matching,
43f3e411 5273 dw2_find_pc_sect_compunit_symtab,
71a3c369 5274 NULL,
9291a0cd
TT
5275 dw2_map_symbol_filenames
5276};
5277
5278/* Initialize for reading DWARF for this objfile. Return 0 if this
5279 file will use psymtabs, or 1 if using the GNU index. */
5280
5281int
5282dwarf2_initialize_objfile (struct objfile *objfile)
5283{
5284 /* If we're about to read full symbols, don't bother with the
5285 indices. In this case we also don't care if some other debug
5286 format is making psymtabs, because they are all about to be
5287 expanded anyway. */
5288 if ((objfile->flags & OBJF_READNOW))
5289 {
5290 int i;
5291
5292 dwarf2_per_objfile->using_index = 1;
5293 create_all_comp_units (objfile);
0e50663e 5294 create_all_type_units (objfile);
7b9f3c50
DE
5295 dwarf2_per_objfile->quick_file_names_table =
5296 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 5297
1fd400ff 5298 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 5299 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 5300 {
8832e7e3 5301 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 5302
e254ef6a
DE
5303 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5304 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5305 }
5306
5307 /* Return 1 so that gdb sees the "quick" functions. However,
5308 these functions will be no-ops because we will have expanded
5309 all symtabs. */
5310 return 1;
5311 }
5312
5313 if (dwarf2_read_index (objfile))
5314 return 1;
5315
9291a0cd
TT
5316 return 0;
5317}
5318
5319\f
5320
dce234bc
PP
5321/* Build a partial symbol table. */
5322
5323void
f29dff0a 5324dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5325{
c9bf0622 5326
af5bf4ad
SM
5327 if (objfile->global_psymbols.capacity () == 0
5328 && objfile->static_psymbols.capacity () == 0)
5329 init_psymbol_list (objfile, 1024);
c906108c 5330
492d29ea 5331 TRY
c9bf0622
TT
5332 {
5333 /* This isn't really ideal: all the data we allocate on the
5334 objfile's obstack is still uselessly kept around. However,
5335 freeing it seems unsafe. */
906768f9 5336 psymtab_discarder psymtabs (objfile);
c9bf0622 5337 dwarf2_build_psymtabs_hard (objfile);
906768f9 5338 psymtabs.keep ();
c9bf0622 5339 }
492d29ea
PA
5340 CATCH (except, RETURN_MASK_ERROR)
5341 {
5342 exception_print (gdb_stderr, except);
5343 }
5344 END_CATCH
c906108c 5345}
c906108c 5346
1ce1cefd
DE
5347/* Return the total length of the CU described by HEADER. */
5348
5349static unsigned int
5350get_cu_length (const struct comp_unit_head *header)
5351{
5352 return header->initial_length_size + header->length;
5353}
5354
9c541725 5355/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 5356
9c541725
PA
5357static inline bool
5358offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 5359{
9c541725
PA
5360 sect_offset bottom = cu_header->sect_off;
5361 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 5362
9c541725 5363 return sect_off >= bottom && sect_off < top;
45452591
DE
5364}
5365
3b80fe9b
DE
5366/* Find the base address of the compilation unit for range lists and
5367 location lists. It will normally be specified by DW_AT_low_pc.
5368 In DWARF-3 draft 4, the base address could be overridden by
5369 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5370 compilation units with discontinuous ranges. */
5371
5372static void
5373dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5374{
5375 struct attribute *attr;
5376
5377 cu->base_known = 0;
5378 cu->base_address = 0;
5379
5380 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5381 if (attr)
5382 {
31aa7e4e 5383 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
5384 cu->base_known = 1;
5385 }
5386 else
5387 {
5388 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5389 if (attr)
5390 {
31aa7e4e 5391 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
5392 cu->base_known = 1;
5393 }
5394 }
5395}
5396
93311388 5397/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 5398 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
5399 NOTE: This leaves members offset, first_die_offset to be filled in
5400 by the caller. */
107d2387 5401
d521ce57 5402static const gdb_byte *
107d2387 5403read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
5404 const gdb_byte *info_ptr,
5405 struct dwarf2_section_info *section,
5406 rcuh_kind section_kind)
107d2387
AC
5407{
5408 int signed_addr;
891d2f0b 5409 unsigned int bytes_read;
43988095
JK
5410 const char *filename = get_section_file_name (section);
5411 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
5412
5413 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
5414 cu_header->initial_length_size = bytes_read;
5415 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 5416 info_ptr += bytes_read;
107d2387
AC
5417 cu_header->version = read_2_bytes (abfd, info_ptr);
5418 info_ptr += 2;
43988095
JK
5419 if (cu_header->version < 5)
5420 switch (section_kind)
5421 {
5422 case rcuh_kind::COMPILE:
5423 cu_header->unit_type = DW_UT_compile;
5424 break;
5425 case rcuh_kind::TYPE:
5426 cu_header->unit_type = DW_UT_type;
5427 break;
5428 default:
5429 internal_error (__FILE__, __LINE__,
5430 _("read_comp_unit_head: invalid section_kind"));
5431 }
5432 else
5433 {
5434 cu_header->unit_type = static_cast<enum dwarf_unit_type>
5435 (read_1_byte (abfd, info_ptr));
5436 info_ptr += 1;
5437 switch (cu_header->unit_type)
5438 {
5439 case DW_UT_compile:
5440 if (section_kind != rcuh_kind::COMPILE)
5441 error (_("Dwarf Error: wrong unit_type in compilation unit header "
5442 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
5443 filename);
5444 break;
5445 case DW_UT_type:
5446 section_kind = rcuh_kind::TYPE;
5447 break;
5448 default:
5449 error (_("Dwarf Error: wrong unit_type in compilation unit header "
5450 "(is %d, should be %d or %d) [in module %s]"),
5451 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
5452 }
5453
5454 cu_header->addr_size = read_1_byte (abfd, info_ptr);
5455 info_ptr += 1;
5456 }
9c541725
PA
5457 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
5458 cu_header,
5459 &bytes_read);
613e1657 5460 info_ptr += bytes_read;
43988095
JK
5461 if (cu_header->version < 5)
5462 {
5463 cu_header->addr_size = read_1_byte (abfd, info_ptr);
5464 info_ptr += 1;
5465 }
107d2387
AC
5466 signed_addr = bfd_get_sign_extend_vma (abfd);
5467 if (signed_addr < 0)
8e65ff28 5468 internal_error (__FILE__, __LINE__,
e2e0b3e5 5469 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 5470 cu_header->signed_addr_p = signed_addr;
c764a876 5471
43988095
JK
5472 if (section_kind == rcuh_kind::TYPE)
5473 {
5474 LONGEST type_offset;
5475
5476 cu_header->signature = read_8_bytes (abfd, info_ptr);
5477 info_ptr += 8;
5478
5479 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
5480 info_ptr += bytes_read;
9c541725
PA
5481 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
5482 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
5483 error (_("Dwarf Error: Too big type_offset in compilation unit "
5484 "header (is %s) [in module %s]"), plongest (type_offset),
5485 filename);
5486 }
5487
107d2387
AC
5488 return info_ptr;
5489}
5490
36586728
TT
5491/* Helper function that returns the proper abbrev section for
5492 THIS_CU. */
5493
5494static struct dwarf2_section_info *
5495get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5496{
5497 struct dwarf2_section_info *abbrev;
5498
5499 if (this_cu->is_dwz)
5500 abbrev = &dwarf2_get_dwz_file ()->abbrev;
5501 else
5502 abbrev = &dwarf2_per_objfile->abbrev;
5503
5504 return abbrev;
5505}
5506
9ff913ba
DE
5507/* Subroutine of read_and_check_comp_unit_head and
5508 read_and_check_type_unit_head to simplify them.
5509 Perform various error checking on the header. */
5510
5511static void
5512error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
5513 struct dwarf2_section_info *section,
5514 struct dwarf2_section_info *abbrev_section)
9ff913ba 5515{
a32a8923 5516 const char *filename = get_section_file_name (section);
9ff913ba 5517
43988095 5518 if (header->version < 2 || header->version > 5)
9ff913ba 5519 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 5520 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
5521 filename);
5522
9c541725 5523 if (to_underlying (header->abbrev_sect_off)
36586728 5524 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9c541725
PA
5525 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
5526 "(offset 0x%x + 6) [in module %s]"),
5527 to_underlying (header->abbrev_sect_off),
5528 to_underlying (header->sect_off),
9ff913ba
DE
5529 filename);
5530
9c541725 5531 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 5532 avoid potential 32-bit overflow. */
9c541725 5533 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 5534 > section->size)
9c541725
PA
5535 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
5536 "(offset 0x%x + 0) [in module %s]"),
5537 header->length, to_underlying (header->sect_off),
9ff913ba
DE
5538 filename);
5539}
5540
5541/* Read in a CU/TU header and perform some basic error checking.
5542 The contents of the header are stored in HEADER.
5543 The result is a pointer to the start of the first DIE. */
adabb602 5544
d521ce57 5545static const gdb_byte *
9ff913ba
DE
5546read_and_check_comp_unit_head (struct comp_unit_head *header,
5547 struct dwarf2_section_info *section,
4bdcc0c1 5548 struct dwarf2_section_info *abbrev_section,
d521ce57 5549 const gdb_byte *info_ptr,
43988095 5550 rcuh_kind section_kind)
72bf9492 5551{
d521ce57 5552 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 5553
9c541725 5554 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 5555
43988095 5556 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 5557
9c541725 5558 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 5559
4bdcc0c1 5560 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
5561
5562 return info_ptr;
348e048f
DE
5563}
5564
f4dc4d17
DE
5565/* Fetch the abbreviation table offset from a comp or type unit header. */
5566
5567static sect_offset
5568read_abbrev_offset (struct dwarf2_section_info *section,
9c541725 5569 sect_offset sect_off)
f4dc4d17 5570{
a32a8923 5571 bfd *abfd = get_section_bfd_owner (section);
d521ce57 5572 const gdb_byte *info_ptr;
ac298888 5573 unsigned int initial_length_size, offset_size;
43988095 5574 uint16_t version;
f4dc4d17
DE
5575
5576 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 5577 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5578 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5579 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5580 info_ptr += initial_length_size;
5581
5582 version = read_2_bytes (abfd, info_ptr);
5583 info_ptr += 2;
5584 if (version >= 5)
5585 {
5586 /* Skip unit type and address size. */
5587 info_ptr += 2;
5588 }
5589
9c541725 5590 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
5591}
5592
aaa75496
JB
5593/* Allocate a new partial symtab for file named NAME and mark this new
5594 partial symtab as being an include of PST. */
5595
5596static void
d521ce57 5597dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
5598 struct objfile *objfile)
5599{
5600 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
5601
fbd9ab74
JK
5602 if (!IS_ABSOLUTE_PATH (subpst->filename))
5603 {
5604 /* It shares objfile->objfile_obstack. */
5605 subpst->dirname = pst->dirname;
5606 }
5607
aaa75496
JB
5608 subpst->textlow = 0;
5609 subpst->texthigh = 0;
5610
8d749320
SM
5611 subpst->dependencies
5612 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
5613 subpst->dependencies[0] = pst;
5614 subpst->number_of_dependencies = 1;
5615
5616 subpst->globals_offset = 0;
5617 subpst->n_global_syms = 0;
5618 subpst->statics_offset = 0;
5619 subpst->n_static_syms = 0;
43f3e411 5620 subpst->compunit_symtab = NULL;
aaa75496
JB
5621 subpst->read_symtab = pst->read_symtab;
5622 subpst->readin = 0;
5623
5624 /* No private part is necessary for include psymtabs. This property
5625 can be used to differentiate between such include psymtabs and
10b3939b 5626 the regular ones. */
58a9656e 5627 subpst->read_symtab_private = NULL;
aaa75496
JB
5628}
5629
5630/* Read the Line Number Program data and extract the list of files
5631 included by the source file represented by PST. Build an include
d85a05f0 5632 partial symtab for each of these included files. */
aaa75496
JB
5633
5634static void
5635dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
5636 struct die_info *die,
5637 struct partial_symtab *pst)
aaa75496 5638{
fff8551c 5639 line_header_up lh;
d85a05f0 5640 struct attribute *attr;
aaa75496 5641
d85a05f0
DJ
5642 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5643 if (attr)
9c541725 5644 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
5645 if (lh == NULL)
5646 return; /* No linetable, so no includes. */
5647
c6da4cef 5648 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 5649 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
5650}
5651
348e048f 5652static hashval_t
52dc124a 5653hash_signatured_type (const void *item)
348e048f 5654{
9a3c8263
SM
5655 const struct signatured_type *sig_type
5656 = (const struct signatured_type *) item;
9a619af0 5657
348e048f 5658 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 5659 return sig_type->signature;
348e048f
DE
5660}
5661
5662static int
52dc124a 5663eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 5664{
9a3c8263
SM
5665 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5666 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 5667
348e048f
DE
5668 return lhs->signature == rhs->signature;
5669}
5670
1fd400ff
TT
5671/* Allocate a hash table for signatured types. */
5672
5673static htab_t
673bfd45 5674allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
5675{
5676 return htab_create_alloc_ex (41,
52dc124a
DE
5677 hash_signatured_type,
5678 eq_signatured_type,
1fd400ff
TT
5679 NULL,
5680 &objfile->objfile_obstack,
5681 hashtab_obstack_allocate,
5682 dummy_obstack_deallocate);
5683}
5684
d467dd73 5685/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
5686
5687static int
d467dd73 5688add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 5689{
9a3c8263
SM
5690 struct signatured_type *sigt = (struct signatured_type *) *slot;
5691 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 5692
b4dd5633 5693 **datap = sigt;
1fd400ff
TT
5694 ++*datap;
5695
5696 return 1;
5697}
5698
78d4d2c5 5699/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
5700 and fill them into TYPES_HTAB. It will process only type units,
5701 therefore DW_UT_type. */
c88ee1f0 5702
78d4d2c5
JK
5703static void
5704create_debug_type_hash_table (struct dwo_file *dwo_file,
43988095
JK
5705 dwarf2_section_info *section, htab_t &types_htab,
5706 rcuh_kind section_kind)
348e048f 5707{
3019eac3 5708 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 5709 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
5710 bfd *abfd;
5711 const gdb_byte *info_ptr, *end_ptr;
348e048f 5712
4bdcc0c1
DE
5713 abbrev_section = (dwo_file != NULL
5714 ? &dwo_file->sections.abbrev
5715 : &dwarf2_per_objfile->abbrev);
5716
b4f54984 5717 if (dwarf_read_debug)
43988095
JK
5718 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
5719 get_section_name (section),
a32a8923 5720 get_section_file_name (abbrev_section));
09406207 5721
78d4d2c5
JK
5722 dwarf2_read_section (objfile, section);
5723 info_ptr = section->buffer;
348e048f 5724
78d4d2c5
JK
5725 if (info_ptr == NULL)
5726 return;
348e048f 5727
78d4d2c5
JK
5728 /* We can't set abfd until now because the section may be empty or
5729 not present, in which case the bfd is unknown. */
5730 abfd = get_section_bfd_owner (section);
348e048f 5731
78d4d2c5
JK
5732 /* We don't use init_cutu_and_read_dies_simple, or some such, here
5733 because we don't need to read any dies: the signature is in the
5734 header. */
3019eac3 5735
78d4d2c5
JK
5736 end_ptr = info_ptr + section->size;
5737 while (info_ptr < end_ptr)
5738 {
78d4d2c5
JK
5739 struct signatured_type *sig_type;
5740 struct dwo_unit *dwo_tu;
5741 void **slot;
5742 const gdb_byte *ptr = info_ptr;
5743 struct comp_unit_head header;
5744 unsigned int length;
8b70b953 5745
9c541725 5746 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 5747
a49dd8dd
JK
5748 /* Initialize it due to a false compiler warning. */
5749 header.signature = -1;
9c541725 5750 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 5751
78d4d2c5
JK
5752 /* We need to read the type's signature in order to build the hash
5753 table, but we don't need anything else just yet. */
348e048f 5754
43988095
JK
5755 ptr = read_and_check_comp_unit_head (&header, section,
5756 abbrev_section, ptr, section_kind);
348e048f 5757
78d4d2c5 5758 length = get_cu_length (&header);
6caca83c 5759
78d4d2c5
JK
5760 /* Skip dummy type units. */
5761 if (ptr >= info_ptr + length
43988095
JK
5762 || peek_abbrev_code (abfd, ptr) == 0
5763 || header.unit_type != DW_UT_type)
78d4d2c5
JK
5764 {
5765 info_ptr += length;
5766 continue;
5767 }
dee91e82 5768
78d4d2c5
JK
5769 if (types_htab == NULL)
5770 {
5771 if (dwo_file)
5772 types_htab = allocate_dwo_unit_table (objfile);
5773 else
5774 types_htab = allocate_signatured_type_table (objfile);
5775 }
8b70b953 5776
78d4d2c5
JK
5777 if (dwo_file)
5778 {
5779 sig_type = NULL;
5780 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5781 struct dwo_unit);
5782 dwo_tu->dwo_file = dwo_file;
43988095 5783 dwo_tu->signature = header.signature;
9c541725 5784 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 5785 dwo_tu->section = section;
9c541725 5786 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
5787 dwo_tu->length = length;
5788 }
5789 else
5790 {
5791 /* N.B.: type_offset is not usable if this type uses a DWO file.
5792 The real type_offset is in the DWO file. */
5793 dwo_tu = NULL;
5794 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5795 struct signatured_type);
43988095 5796 sig_type->signature = header.signature;
9c541725 5797 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
5798 sig_type->per_cu.objfile = objfile;
5799 sig_type->per_cu.is_debug_types = 1;
5800 sig_type->per_cu.section = section;
9c541725 5801 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
5802 sig_type->per_cu.length = length;
5803 }
5804
5805 slot = htab_find_slot (types_htab,
5806 dwo_file ? (void*) dwo_tu : (void *) sig_type,
5807 INSERT);
5808 gdb_assert (slot != NULL);
5809 if (*slot != NULL)
5810 {
9c541725 5811 sect_offset dup_sect_off;
0349ea22 5812
3019eac3
DE
5813 if (dwo_file)
5814 {
78d4d2c5
JK
5815 const struct dwo_unit *dup_tu
5816 = (const struct dwo_unit *) *slot;
5817
9c541725 5818 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
5819 }
5820 else
5821 {
78d4d2c5
JK
5822 const struct signatured_type *dup_tu
5823 = (const struct signatured_type *) *slot;
5824
9c541725 5825 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 5826 }
8b70b953 5827
78d4d2c5
JK
5828 complaint (&symfile_complaints,
5829 _("debug type entry at offset 0x%x is duplicate to"
5830 " the entry at offset 0x%x, signature %s"),
9c541725 5831 to_underlying (sect_off), to_underlying (dup_sect_off),
43988095 5832 hex_string (header.signature));
78d4d2c5
JK
5833 }
5834 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 5835
78d4d2c5
JK
5836 if (dwarf_read_debug > 1)
5837 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
9c541725 5838 to_underlying (sect_off),
43988095 5839 hex_string (header.signature));
3019eac3 5840
78d4d2c5
JK
5841 info_ptr += length;
5842 }
5843}
3019eac3 5844
78d4d2c5
JK
5845/* Create the hash table of all entries in the .debug_types
5846 (or .debug_types.dwo) section(s).
5847 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
5848 otherwise it is NULL.
b3c8eb43 5849
78d4d2c5 5850 The result is a pointer to the hash table or NULL if there are no types.
348e048f 5851
78d4d2c5 5852 Note: This function processes DWO files only, not DWP files. */
348e048f 5853
78d4d2c5
JK
5854static void
5855create_debug_types_hash_table (struct dwo_file *dwo_file,
5856 VEC (dwarf2_section_info_def) *types,
5857 htab_t &types_htab)
5858{
5859 int ix;
5860 struct dwarf2_section_info *section;
5861
5862 if (VEC_empty (dwarf2_section_info_def, types))
5863 return;
348e048f 5864
78d4d2c5
JK
5865 for (ix = 0;
5866 VEC_iterate (dwarf2_section_info_def, types, ix, section);
5867 ++ix)
43988095
JK
5868 create_debug_type_hash_table (dwo_file, section, types_htab,
5869 rcuh_kind::TYPE);
3019eac3
DE
5870}
5871
5872/* Create the hash table of all entries in the .debug_types section,
5873 and initialize all_type_units.
5874 The result is zero if there is an error (e.g. missing .debug_types section),
5875 otherwise non-zero. */
5876
5877static int
5878create_all_type_units (struct objfile *objfile)
5879{
78d4d2c5 5880 htab_t types_htab = NULL;
b4dd5633 5881 struct signatured_type **iter;
3019eac3 5882
43988095
JK
5883 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
5884 rcuh_kind::COMPILE);
78d4d2c5 5885 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
3019eac3
DE
5886 if (types_htab == NULL)
5887 {
5888 dwarf2_per_objfile->signatured_types = NULL;
5889 return 0;
5890 }
5891
348e048f
DE
5892 dwarf2_per_objfile->signatured_types = types_htab;
5893
6aa5f3a6
DE
5894 dwarf2_per_objfile->n_type_units
5895 = dwarf2_per_objfile->n_allocated_type_units
5896 = htab_elements (types_htab);
8d749320
SM
5897 dwarf2_per_objfile->all_type_units =
5898 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
5899 iter = &dwarf2_per_objfile->all_type_units[0];
5900 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
5901 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
5902 == dwarf2_per_objfile->n_type_units);
1fd400ff 5903
348e048f
DE
5904 return 1;
5905}
5906
6aa5f3a6
DE
5907/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
5908 If SLOT is non-NULL, it is the entry to use in the hash table.
5909 Otherwise we find one. */
5910
5911static struct signatured_type *
5912add_type_unit (ULONGEST sig, void **slot)
5913{
5914 struct objfile *objfile = dwarf2_per_objfile->objfile;
5915 int n_type_units = dwarf2_per_objfile->n_type_units;
5916 struct signatured_type *sig_type;
5917
5918 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
5919 ++n_type_units;
5920 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
5921 {
5922 if (dwarf2_per_objfile->n_allocated_type_units == 0)
5923 dwarf2_per_objfile->n_allocated_type_units = 1;
5924 dwarf2_per_objfile->n_allocated_type_units *= 2;
5925 dwarf2_per_objfile->all_type_units
224c3ddb
SM
5926 = XRESIZEVEC (struct signatured_type *,
5927 dwarf2_per_objfile->all_type_units,
5928 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
5929 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5930 }
5931 dwarf2_per_objfile->n_type_units = n_type_units;
5932
5933 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5934 struct signatured_type);
5935 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5936 sig_type->signature = sig;
5937 sig_type->per_cu.is_debug_types = 1;
5938 if (dwarf2_per_objfile->using_index)
5939 {
5940 sig_type->per_cu.v.quick =
5941 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5942 struct dwarf2_per_cu_quick_data);
5943 }
5944
5945 if (slot == NULL)
5946 {
5947 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5948 sig_type, INSERT);
5949 }
5950 gdb_assert (*slot == NULL);
5951 *slot = sig_type;
5952 /* The rest of sig_type must be filled in by the caller. */
5953 return sig_type;
5954}
5955
a2ce51a0
DE
5956/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5957 Fill in SIG_ENTRY with DWO_ENTRY. */
5958
5959static void
5960fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5961 struct signatured_type *sig_entry,
5962 struct dwo_unit *dwo_entry)
5963{
7ee85ab1 5964 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
5965 gdb_assert (! sig_entry->per_cu.queued);
5966 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
5967 if (dwarf2_per_objfile->using_index)
5968 {
5969 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 5970 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
5971 }
5972 else
5973 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 5974 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 5975 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 5976 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
5977 gdb_assert (sig_entry->dwo_unit == NULL);
5978
5979 sig_entry->per_cu.section = dwo_entry->section;
9c541725 5980 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
5981 sig_entry->per_cu.length = dwo_entry->length;
5982 sig_entry->per_cu.reading_dwo_directly = 1;
5983 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
5984 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5985 sig_entry->dwo_unit = dwo_entry;
5986}
5987
5988/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
5989 If we haven't read the TU yet, create the signatured_type data structure
5990 for a TU to be read in directly from a DWO file, bypassing the stub.
5991 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5992 using .gdb_index, then when reading a CU we want to stay in the DWO file
5993 containing that CU. Otherwise we could end up reading several other DWO
5994 files (due to comdat folding) to process the transitive closure of all the
5995 mentioned TUs, and that can be slow. The current DWO file will have every
5996 type signature that it needs.
a2ce51a0
DE
5997 We only do this for .gdb_index because in the psymtab case we already have
5998 to read all the DWOs to build the type unit groups. */
5999
6000static struct signatured_type *
6001lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6002{
6003 struct objfile *objfile = dwarf2_per_objfile->objfile;
6004 struct dwo_file *dwo_file;
6005 struct dwo_unit find_dwo_entry, *dwo_entry;
6006 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6007 void **slot;
a2ce51a0
DE
6008
6009 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6010
6aa5f3a6
DE
6011 /* If TU skeletons have been removed then we may not have read in any
6012 TUs yet. */
6013 if (dwarf2_per_objfile->signatured_types == NULL)
6014 {
6015 dwarf2_per_objfile->signatured_types
6016 = allocate_signatured_type_table (objfile);
6017 }
a2ce51a0
DE
6018
6019 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6020 Use the global signatured_types array to do our own comdat-folding
6021 of types. If this is the first time we're reading this TU, and
6022 the TU has an entry in .gdb_index, replace the recorded data from
6023 .gdb_index with this TU. */
a2ce51a0 6024
a2ce51a0 6025 find_sig_entry.signature = sig;
6aa5f3a6
DE
6026 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6027 &find_sig_entry, INSERT);
9a3c8263 6028 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6029
6030 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6031 read. Don't reassign the global entry to point to this DWO if that's
6032 the case. Also note that if the TU is already being read, it may not
6033 have come from a DWO, the program may be a mix of Fission-compiled
6034 code and non-Fission-compiled code. */
6035
6036 /* Have we already tried to read this TU?
6037 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6038 needn't exist in the global table yet). */
6039 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6040 return sig_entry;
6041
6aa5f3a6
DE
6042 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6043 dwo_unit of the TU itself. */
6044 dwo_file = cu->dwo_unit->dwo_file;
6045
a2ce51a0
DE
6046 /* Ok, this is the first time we're reading this TU. */
6047 if (dwo_file->tus == NULL)
6048 return NULL;
6049 find_dwo_entry.signature = sig;
9a3c8263 6050 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
6051 if (dwo_entry == NULL)
6052 return NULL;
6053
6aa5f3a6
DE
6054 /* If the global table doesn't have an entry for this TU, add one. */
6055 if (sig_entry == NULL)
6056 sig_entry = add_type_unit (sig, slot);
6057
a2ce51a0 6058 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 6059 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6060 return sig_entry;
6061}
6062
a2ce51a0
DE
6063/* Subroutine of lookup_signatured_type.
6064 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6065 then try the DWP file. If the TU stub (skeleton) has been removed then
6066 it won't be in .gdb_index. */
a2ce51a0
DE
6067
6068static struct signatured_type *
6069lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6070{
6071 struct objfile *objfile = dwarf2_per_objfile->objfile;
6072 struct dwp_file *dwp_file = get_dwp_file ();
6073 struct dwo_unit *dwo_entry;
6074 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6075 void **slot;
a2ce51a0
DE
6076
6077 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6078 gdb_assert (dwp_file != NULL);
6079
6aa5f3a6
DE
6080 /* If TU skeletons have been removed then we may not have read in any
6081 TUs yet. */
6082 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 6083 {
6aa5f3a6
DE
6084 dwarf2_per_objfile->signatured_types
6085 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
6086 }
6087
6aa5f3a6
DE
6088 find_sig_entry.signature = sig;
6089 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6090 &find_sig_entry, INSERT);
9a3c8263 6091 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6092
6093 /* Have we already tried to read this TU?
6094 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6095 needn't exist in the global table yet). */
6096 if (sig_entry != NULL)
6097 return sig_entry;
6098
a2ce51a0
DE
6099 if (dwp_file->tus == NULL)
6100 return NULL;
57d63ce2
DE
6101 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
6102 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6103 if (dwo_entry == NULL)
6104 return NULL;
6105
6aa5f3a6 6106 sig_entry = add_type_unit (sig, slot);
a2ce51a0
DE
6107 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
6108
a2ce51a0
DE
6109 return sig_entry;
6110}
6111
380bca97 6112/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6113 Returns NULL if signature SIG is not present in the table.
6114 It is up to the caller to complain about this. */
348e048f
DE
6115
6116static struct signatured_type *
a2ce51a0 6117lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6118{
a2ce51a0
DE
6119 if (cu->dwo_unit
6120 && dwarf2_per_objfile->using_index)
6121 {
6122 /* We're in a DWO/DWP file, and we're using .gdb_index.
6123 These cases require special processing. */
6124 if (get_dwp_file () == NULL)
6125 return lookup_dwo_signatured_type (cu, sig);
6126 else
6127 return lookup_dwp_signatured_type (cu, sig);
6128 }
6129 else
6130 {
6131 struct signatured_type find_entry, *entry;
348e048f 6132
a2ce51a0
DE
6133 if (dwarf2_per_objfile->signatured_types == NULL)
6134 return NULL;
6135 find_entry.signature = sig;
9a3c8263
SM
6136 entry = ((struct signatured_type *)
6137 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
6138 return entry;
6139 }
348e048f 6140}
42e7ad6c
DE
6141\f
6142/* Low level DIE reading support. */
348e048f 6143
d85a05f0
DJ
6144/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6145
6146static void
6147init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6148 struct dwarf2_cu *cu,
3019eac3
DE
6149 struct dwarf2_section_info *section,
6150 struct dwo_file *dwo_file)
d85a05f0 6151{
fceca515 6152 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 6153 reader->abfd = get_section_bfd_owner (section);
d85a05f0 6154 reader->cu = cu;
3019eac3 6155 reader->dwo_file = dwo_file;
dee91e82
DE
6156 reader->die_section = section;
6157 reader->buffer = section->buffer;
f664829e 6158 reader->buffer_end = section->buffer + section->size;
a2ce51a0 6159 reader->comp_dir = NULL;
d85a05f0
DJ
6160}
6161
b0c7bfa9
DE
6162/* Subroutine of init_cutu_and_read_dies to simplify it.
6163 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6164 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
6165 already.
6166
6167 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6168 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6169 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6170 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6171 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6172 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
6173 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
6174 are filled in with the info of the DIE from the DWO file.
6175 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
6176 provided an abbrev table to use.
6177 The result is non-zero if a valid (non-dummy) DIE was found. */
6178
6179static int
6180read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6181 struct dwo_unit *dwo_unit,
6182 int abbrev_table_provided,
6183 struct die_info *stub_comp_unit_die,
a2ce51a0 6184 const char *stub_comp_dir,
b0c7bfa9 6185 struct die_reader_specs *result_reader,
d521ce57 6186 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
6187 struct die_info **result_comp_unit_die,
6188 int *result_has_children)
6189{
6190 struct objfile *objfile = dwarf2_per_objfile->objfile;
6191 struct dwarf2_cu *cu = this_cu->cu;
6192 struct dwarf2_section_info *section;
6193 bfd *abfd;
d521ce57 6194 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6195 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6196 int i,num_extra_attrs;
6197 struct dwarf2_section_info *dwo_abbrev_section;
6198 struct attribute *attr;
6199 struct die_info *comp_unit_die;
6200
b0aeadb3
DE
6201 /* At most one of these may be provided. */
6202 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6203
b0c7bfa9
DE
6204 /* These attributes aren't processed until later:
6205 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6206 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6207 referenced later. However, these attributes are found in the stub
6208 which we won't have later. In order to not impose this complication
6209 on the rest of the code, we read them here and copy them to the
6210 DWO CU/TU die. */
b0c7bfa9
DE
6211
6212 stmt_list = NULL;
6213 low_pc = NULL;
6214 high_pc = NULL;
6215 ranges = NULL;
6216 comp_dir = NULL;
6217
6218 if (stub_comp_unit_die != NULL)
6219 {
6220 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6221 DWO file. */
6222 if (! this_cu->is_debug_types)
6223 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6224 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6225 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6226 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6227 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6228
6229 /* There should be a DW_AT_addr_base attribute here (if needed).
6230 We need the value before we can process DW_FORM_GNU_addr_index. */
6231 cu->addr_base = 0;
6232 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
6233 if (attr)
6234 cu->addr_base = DW_UNSND (attr);
6235
6236 /* There should be a DW_AT_ranges_base attribute here (if needed).
6237 We need the value before we can process DW_AT_ranges. */
6238 cu->ranges_base = 0;
6239 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
6240 if (attr)
6241 cu->ranges_base = DW_UNSND (attr);
6242 }
a2ce51a0
DE
6243 else if (stub_comp_dir != NULL)
6244 {
6245 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6246 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6247 comp_dir->name = DW_AT_comp_dir;
6248 comp_dir->form = DW_FORM_string;
6249 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6250 DW_STRING (comp_dir) = stub_comp_dir;
6251 }
b0c7bfa9
DE
6252
6253 /* Set up for reading the DWO CU/TU. */
6254 cu->dwo_unit = dwo_unit;
6255 section = dwo_unit->section;
6256 dwarf2_read_section (objfile, section);
a32a8923 6257 abfd = get_section_bfd_owner (section);
9c541725
PA
6258 begin_info_ptr = info_ptr = (section->buffer
6259 + to_underlying (dwo_unit->sect_off));
b0c7bfa9
DE
6260 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6261 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
6262
6263 if (this_cu->is_debug_types)
6264 {
b0c7bfa9
DE
6265 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6266
43988095 6267 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
b0c7bfa9 6268 dwo_abbrev_section,
43988095 6269 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6270 /* This is not an assert because it can be caused by bad debug info. */
43988095 6271 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6272 {
6273 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6274 " TU at offset 0x%x [in module %s]"),
6275 hex_string (sig_type->signature),
43988095 6276 hex_string (cu->header.signature),
9c541725 6277 to_underlying (dwo_unit->sect_off),
a2ce51a0
DE
6278 bfd_get_filename (abfd));
6279 }
9c541725 6280 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6281 /* For DWOs coming from DWP files, we don't know the CU length
6282 nor the type's offset in the TU until now. */
6283 dwo_unit->length = get_cu_length (&cu->header);
9c541725 6284 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6285
6286 /* Establish the type offset that can be used to lookup the type.
6287 For DWO files, we don't know it until now. */
9c541725
PA
6288 sig_type->type_offset_in_section
6289 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6290 }
6291 else
6292 {
6293 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6294 dwo_abbrev_section,
43988095 6295 info_ptr, rcuh_kind::COMPILE);
9c541725 6296 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6297 /* For DWOs coming from DWP files, we don't know the CU length
6298 until now. */
6299 dwo_unit->length = get_cu_length (&cu->header);
6300 }
6301
02142a6c
DE
6302 /* Replace the CU's original abbrev table with the DWO's.
6303 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
6304 if (abbrev_table_provided)
6305 {
6306 /* Don't free the provided abbrev table, the caller of
6307 init_cutu_and_read_dies owns it. */
6308 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 6309 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
6310 make_cleanup (dwarf2_free_abbrev_table, cu);
6311 }
6312 else
6313 {
6314 dwarf2_free_abbrev_table (cu);
6315 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 6316 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
6317 }
6318
6319 /* Read in the die, but leave space to copy over the attributes
6320 from the stub. This has the benefit of simplifying the rest of
6321 the code - all the work to maintain the illusion of a single
6322 DW_TAG_{compile,type}_unit DIE is done here. */
6323 num_extra_attrs = ((stmt_list != NULL)
6324 + (low_pc != NULL)
6325 + (high_pc != NULL)
6326 + (ranges != NULL)
6327 + (comp_dir != NULL));
6328 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6329 result_has_children, num_extra_attrs);
6330
6331 /* Copy over the attributes from the stub to the DIE we just read in. */
6332 comp_unit_die = *result_comp_unit_die;
6333 i = comp_unit_die->num_attrs;
6334 if (stmt_list != NULL)
6335 comp_unit_die->attrs[i++] = *stmt_list;
6336 if (low_pc != NULL)
6337 comp_unit_die->attrs[i++] = *low_pc;
6338 if (high_pc != NULL)
6339 comp_unit_die->attrs[i++] = *high_pc;
6340 if (ranges != NULL)
6341 comp_unit_die->attrs[i++] = *ranges;
6342 if (comp_dir != NULL)
6343 comp_unit_die->attrs[i++] = *comp_dir;
6344 comp_unit_die->num_attrs += num_extra_attrs;
6345
b4f54984 6346 if (dwarf_die_debug)
bf6af496
DE
6347 {
6348 fprintf_unfiltered (gdb_stdlog,
6349 "Read die from %s@0x%x of %s:\n",
a32a8923 6350 get_section_name (section),
bf6af496
DE
6351 (unsigned) (begin_info_ptr - section->buffer),
6352 bfd_get_filename (abfd));
b4f54984 6353 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6354 }
6355
a2ce51a0
DE
6356 /* Save the comp_dir attribute. If there is no DWP file then we'll read
6357 TUs by skipping the stub and going directly to the entry in the DWO file.
6358 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
6359 to get it via circuitous means. Blech. */
6360 if (comp_dir != NULL)
6361 result_reader->comp_dir = DW_STRING (comp_dir);
6362
b0c7bfa9
DE
6363 /* Skip dummy compilation units. */
6364 if (info_ptr >= begin_info_ptr + dwo_unit->length
6365 || peek_abbrev_code (abfd, info_ptr) == 0)
6366 return 0;
6367
6368 *result_info_ptr = info_ptr;
6369 return 1;
6370}
6371
6372/* Subroutine of init_cutu_and_read_dies to simplify it.
6373 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6374 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6375
6376static struct dwo_unit *
6377lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6378 struct die_info *comp_unit_die)
6379{
6380 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
6381 ULONGEST signature;
6382 struct dwo_unit *dwo_unit;
6383 const char *comp_dir, *dwo_name;
6384
a2ce51a0
DE
6385 gdb_assert (cu != NULL);
6386
b0c7bfa9 6387 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
6388 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
6389 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6390
6391 if (this_cu->is_debug_types)
6392 {
6393 struct signatured_type *sig_type;
6394
6395 /* Since this_cu is the first member of struct signatured_type,
6396 we can go from a pointer to one to a pointer to the other. */
6397 sig_type = (struct signatured_type *) this_cu;
6398 signature = sig_type->signature;
6399 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6400 }
6401 else
6402 {
6403 struct attribute *attr;
6404
6405 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6406 if (! attr)
6407 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6408 " [in module %s]"),
4262abfb 6409 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
6410 signature = DW_UNSND (attr);
6411 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6412 signature);
6413 }
6414
b0c7bfa9
DE
6415 return dwo_unit;
6416}
6417
a2ce51a0 6418/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6
DE
6419 See it for a description of the parameters.
6420 Read a TU directly from a DWO file, bypassing the stub.
6421
6422 Note: This function could be a little bit simpler if we shared cleanups
6423 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
6424 to do, so we keep this function self-contained. Or we could move this
6425 into our caller, but it's complex enough already. */
a2ce51a0
DE
6426
6427static void
6aa5f3a6
DE
6428init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6429 int use_existing_cu, int keep,
a2ce51a0
DE
6430 die_reader_func_ftype *die_reader_func,
6431 void *data)
6432{
6433 struct dwarf2_cu *cu;
6434 struct signatured_type *sig_type;
6aa5f3a6 6435 struct cleanup *cleanups, *free_cu_cleanup = NULL;
a2ce51a0
DE
6436 struct die_reader_specs reader;
6437 const gdb_byte *info_ptr;
6438 struct die_info *comp_unit_die;
6439 int has_children;
6440
6441 /* Verify we can do the following downcast, and that we have the
6442 data we need. */
6443 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6444 sig_type = (struct signatured_type *) this_cu;
6445 gdb_assert (sig_type->dwo_unit != NULL);
6446
6447 cleanups = make_cleanup (null_cleanup, NULL);
6448
6aa5f3a6
DE
6449 if (use_existing_cu && this_cu->cu != NULL)
6450 {
6451 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6452 cu = this_cu->cu;
6453 /* There's no need to do the rereading_dwo_cu handling that
6454 init_cutu_and_read_dies does since we don't read the stub. */
6455 }
6456 else
6457 {
6458 /* If !use_existing_cu, this_cu->cu must be NULL. */
6459 gdb_assert (this_cu->cu == NULL);
8d749320 6460 cu = XNEW (struct dwarf2_cu);
6aa5f3a6
DE
6461 init_one_comp_unit (cu, this_cu);
6462 /* If an error occurs while loading, release our storage. */
6463 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
6464 }
6465
6466 /* A future optimization, if needed, would be to use an existing
6467 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6468 could share abbrev tables. */
a2ce51a0
DE
6469
6470 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6471 0 /* abbrev_table_provided */,
6472 NULL /* stub_comp_unit_die */,
6473 sig_type->dwo_unit->dwo_file->comp_dir,
6474 &reader, &info_ptr,
6475 &comp_unit_die, &has_children) == 0)
6476 {
6477 /* Dummy die. */
6478 do_cleanups (cleanups);
6479 return;
6480 }
6481
6482 /* All the "real" work is done here. */
6483 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6484
6aa5f3a6 6485 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
6486 but the alternative is making the latter more complex.
6487 This function is only for the special case of using DWO files directly:
6488 no point in overly complicating the general case just to handle this. */
6aa5f3a6 6489 if (free_cu_cleanup != NULL)
a2ce51a0 6490 {
6aa5f3a6
DE
6491 if (keep)
6492 {
6493 /* We've successfully allocated this compilation unit. Let our
6494 caller clean it up when finished with it. */
6495 discard_cleanups (free_cu_cleanup);
a2ce51a0 6496
6aa5f3a6
DE
6497 /* We can only discard free_cu_cleanup and all subsequent cleanups.
6498 So we have to manually free the abbrev table. */
6499 dwarf2_free_abbrev_table (cu);
a2ce51a0 6500
6aa5f3a6
DE
6501 /* Link this CU into read_in_chain. */
6502 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6503 dwarf2_per_objfile->read_in_chain = this_cu;
6504 }
6505 else
6506 do_cleanups (free_cu_cleanup);
a2ce51a0 6507 }
a2ce51a0
DE
6508
6509 do_cleanups (cleanups);
6510}
6511
fd820528 6512/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6513 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6514
f4dc4d17
DE
6515 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6516 Otherwise the table specified in the comp unit header is read in and used.
6517 This is an optimization for when we already have the abbrev table.
6518
dee91e82
DE
6519 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6520 Otherwise, a new CU is allocated with xmalloc.
6521
6522 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
6523 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
6524
6525 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 6526 linker) then DIE_READER_FUNC will not get called. */
aaa75496 6527
70221824 6528static void
fd820528 6529init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 6530 struct abbrev_table *abbrev_table,
fd820528
DE
6531 int use_existing_cu, int keep,
6532 die_reader_func_ftype *die_reader_func,
6533 void *data)
c906108c 6534{
dee91e82 6535 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6536 struct dwarf2_section_info *section = this_cu->section;
a32a8923 6537 bfd *abfd = get_section_bfd_owner (section);
dee91e82 6538 struct dwarf2_cu *cu;
d521ce57 6539 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 6540 struct die_reader_specs reader;
d85a05f0 6541 struct die_info *comp_unit_die;
dee91e82 6542 int has_children;
d85a05f0 6543 struct attribute *attr;
365156ad 6544 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 6545 struct signatured_type *sig_type = NULL;
4bdcc0c1 6546 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6547 /* Non-zero if CU currently points to a DWO file and we need to
6548 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6549 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6550 int rereading_dwo_cu = 0;
c906108c 6551
b4f54984 6552 if (dwarf_die_debug)
09406207
DE
6553 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
6554 this_cu->is_debug_types ? "type" : "comp",
9c541725 6555 to_underlying (this_cu->sect_off));
09406207 6556
dee91e82
DE
6557 if (use_existing_cu)
6558 gdb_assert (keep);
23745b47 6559
a2ce51a0
DE
6560 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6561 file (instead of going through the stub), short-circuit all of this. */
6562 if (this_cu->reading_dwo_directly)
6563 {
6564 /* Narrow down the scope of possibilities to have to understand. */
6565 gdb_assert (this_cu->is_debug_types);
6566 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
6567 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
6568 die_reader_func, data);
a2ce51a0
DE
6569 return;
6570 }
6571
dee91e82
DE
6572 cleanups = make_cleanup (null_cleanup, NULL);
6573
6574 /* This is cheap if the section is already read in. */
6575 dwarf2_read_section (objfile, section);
6576
9c541725 6577 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6578
6579 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6580
6581 if (use_existing_cu && this_cu->cu != NULL)
6582 {
6583 cu = this_cu->cu;
42e7ad6c
DE
6584 /* If this CU is from a DWO file we need to start over, we need to
6585 refetch the attributes from the skeleton CU.
6586 This could be optimized by retrieving those attributes from when we
6587 were here the first time: the previous comp_unit_die was stored in
6588 comp_unit_obstack. But there's no data yet that we need this
6589 optimization. */
6590 if (cu->dwo_unit != NULL)
6591 rereading_dwo_cu = 1;
dee91e82
DE
6592 }
6593 else
6594 {
6595 /* If !use_existing_cu, this_cu->cu must be NULL. */
6596 gdb_assert (this_cu->cu == NULL);
8d749320 6597 cu = XNEW (struct dwarf2_cu);
dee91e82 6598 init_one_comp_unit (cu, this_cu);
dee91e82 6599 /* If an error occurs while loading, release our storage. */
365156ad 6600 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 6601 }
dee91e82 6602
b0c7bfa9 6603 /* Get the header. */
9c541725 6604 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6605 {
6606 /* We already have the header, there's no need to read it in again. */
9c541725 6607 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6608 }
6609 else
6610 {
3019eac3 6611 if (this_cu->is_debug_types)
dee91e82 6612 {
43988095 6613 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4bdcc0c1 6614 abbrev_section, info_ptr,
43988095 6615 rcuh_kind::TYPE);
dee91e82 6616
42e7ad6c
DE
6617 /* Since per_cu is the first member of struct signatured_type,
6618 we can go from a pointer to one to a pointer to the other. */
6619 sig_type = (struct signatured_type *) this_cu;
43988095 6620 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6621 gdb_assert (sig_type->type_offset_in_tu
6622 == cu->header.type_cu_offset_in_tu);
6623 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6624
42e7ad6c
DE
6625 /* LENGTH has not been set yet for type units if we're
6626 using .gdb_index. */
1ce1cefd 6627 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
6628
6629 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6630 sig_type->type_offset_in_section =
6631 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6632
6633 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6634 }
6635 else
6636 {
4bdcc0c1
DE
6637 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6638 abbrev_section,
43988095
JK
6639 info_ptr,
6640 rcuh_kind::COMPILE);
dee91e82 6641
9c541725 6642 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 6643 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 6644 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6645 }
6646 }
10b3939b 6647
6caca83c 6648 /* Skip dummy compilation units. */
dee91e82 6649 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
6650 || peek_abbrev_code (abfd, info_ptr) == 0)
6651 {
dee91e82 6652 do_cleanups (cleanups);
21b2bd31 6653 return;
6caca83c
CC
6654 }
6655
433df2d4
DE
6656 /* If we don't have them yet, read the abbrevs for this compilation unit.
6657 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
6658 done. Note that it's important that if the CU had an abbrev table
6659 on entry we don't free it when we're done: Somewhere up the call stack
6660 it may be in use. */
f4dc4d17
DE
6661 if (abbrev_table != NULL)
6662 {
6663 gdb_assert (cu->abbrev_table == NULL);
9c541725 6664 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
f4dc4d17
DE
6665 cu->abbrev_table = abbrev_table;
6666 }
6667 else if (cu->abbrev_table == NULL)
dee91e82 6668 {
4bdcc0c1 6669 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
6670 make_cleanup (dwarf2_free_abbrev_table, cu);
6671 }
42e7ad6c
DE
6672 else if (rereading_dwo_cu)
6673 {
6674 dwarf2_free_abbrev_table (cu);
6675 dwarf2_read_abbrevs (cu, abbrev_section);
6676 }
af703f96 6677
dee91e82 6678 /* Read the top level CU/TU die. */
3019eac3 6679 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 6680 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 6681
b0c7bfa9
DE
6682 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6683 from the DWO file.
6684 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6685 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
6686 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
6687 if (attr)
6688 {
3019eac3 6689 struct dwo_unit *dwo_unit;
b0c7bfa9 6690 struct die_info *dwo_comp_unit_die;
3019eac3
DE
6691
6692 if (has_children)
6a506a2d
DE
6693 {
6694 complaint (&symfile_complaints,
6695 _("compilation unit with DW_AT_GNU_dwo_name"
6696 " has children (offset 0x%x) [in module %s]"),
9c541725 6697 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6a506a2d 6698 }
b0c7bfa9 6699 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 6700 if (dwo_unit != NULL)
3019eac3 6701 {
6a506a2d
DE
6702 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6703 abbrev_table != NULL,
a2ce51a0 6704 comp_unit_die, NULL,
6a506a2d
DE
6705 &reader, &info_ptr,
6706 &dwo_comp_unit_die, &has_children) == 0)
6707 {
6708 /* Dummy die. */
6709 do_cleanups (cleanups);
6710 return;
6711 }
6712 comp_unit_die = dwo_comp_unit_die;
6713 }
6714 else
6715 {
6716 /* Yikes, we couldn't find the rest of the DIE, we only have
6717 the stub. A complaint has already been logged. There's
6718 not much more we can do except pass on the stub DIE to
6719 die_reader_func. We don't want to throw an error on bad
6720 debug info. */
3019eac3
DE
6721 }
6722 }
6723
b0c7bfa9 6724 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
6725 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6726
b0c7bfa9 6727 /* Done, clean up. */
365156ad 6728 if (free_cu_cleanup != NULL)
348e048f 6729 {
365156ad
TT
6730 if (keep)
6731 {
6732 /* We've successfully allocated this compilation unit. Let our
6733 caller clean it up when finished with it. */
6734 discard_cleanups (free_cu_cleanup);
dee91e82 6735
365156ad
TT
6736 /* We can only discard free_cu_cleanup and all subsequent cleanups.
6737 So we have to manually free the abbrev table. */
6738 dwarf2_free_abbrev_table (cu);
dee91e82 6739
365156ad
TT
6740 /* Link this CU into read_in_chain. */
6741 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6742 dwarf2_per_objfile->read_in_chain = this_cu;
6743 }
6744 else
6745 do_cleanups (free_cu_cleanup);
348e048f 6746 }
365156ad
TT
6747
6748 do_cleanups (cleanups);
dee91e82
DE
6749}
6750
33e80786
DE
6751/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
6752 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
6753 to have already done the lookup to find the DWO file).
dee91e82
DE
6754
6755 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 6756 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
6757
6758 We fill in THIS_CU->length.
6759
6760 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
6761 linker) then DIE_READER_FUNC will not get called.
6762
6763 THIS_CU->cu is always freed when done.
3019eac3
DE
6764 This is done in order to not leave THIS_CU->cu in a state where we have
6765 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
6766
6767static void
6768init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 6769 struct dwo_file *dwo_file,
dee91e82
DE
6770 die_reader_func_ftype *die_reader_func,
6771 void *data)
6772{
6773 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6774 struct dwarf2_section_info *section = this_cu->section;
a32a8923 6775 bfd *abfd = get_section_bfd_owner (section);
33e80786 6776 struct dwarf2_section_info *abbrev_section;
dee91e82 6777 struct dwarf2_cu cu;
d521ce57 6778 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
6779 struct die_reader_specs reader;
6780 struct cleanup *cleanups;
6781 struct die_info *comp_unit_die;
6782 int has_children;
6783
b4f54984 6784 if (dwarf_die_debug)
09406207
DE
6785 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
6786 this_cu->is_debug_types ? "type" : "comp",
9c541725 6787 to_underlying (this_cu->sect_off));
09406207 6788
dee91e82
DE
6789 gdb_assert (this_cu->cu == NULL);
6790
33e80786
DE
6791 abbrev_section = (dwo_file != NULL
6792 ? &dwo_file->sections.abbrev
6793 : get_abbrev_section_for_cu (this_cu));
6794
dee91e82
DE
6795 /* This is cheap if the section is already read in. */
6796 dwarf2_read_section (objfile, section);
6797
6798 init_one_comp_unit (&cu, this_cu);
6799
6800 cleanups = make_cleanup (free_stack_comp_unit, &cu);
6801
9c541725 6802 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4bdcc0c1
DE
6803 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
6804 abbrev_section, info_ptr,
43988095
JK
6805 (this_cu->is_debug_types
6806 ? rcuh_kind::TYPE
6807 : rcuh_kind::COMPILE));
dee91e82 6808
1ce1cefd 6809 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
6810
6811 /* Skip dummy compilation units. */
6812 if (info_ptr >= begin_info_ptr + this_cu->length
6813 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 6814 {
dee91e82 6815 do_cleanups (cleanups);
21b2bd31 6816 return;
93311388 6817 }
72bf9492 6818
dee91e82
DE
6819 dwarf2_read_abbrevs (&cu, abbrev_section);
6820 make_cleanup (dwarf2_free_abbrev_table, &cu);
6821
3019eac3 6822 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
6823 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
6824
6825 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6826
6827 do_cleanups (cleanups);
6828}
6829
3019eac3
DE
6830/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
6831 does not lookup the specified DWO file.
6832 This cannot be used to read DWO files.
dee91e82
DE
6833
6834 THIS_CU->cu is always freed when done.
3019eac3
DE
6835 This is done in order to not leave THIS_CU->cu in a state where we have
6836 to care whether it refers to the "main" CU or the DWO CU.
6837 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
6838
6839static void
6840init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
6841 die_reader_func_ftype *die_reader_func,
6842 void *data)
6843{
33e80786 6844 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 6845}
0018ea6f
DE
6846\f
6847/* Type Unit Groups.
dee91e82 6848
0018ea6f
DE
6849 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6850 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6851 so that all types coming from the same compilation (.o file) are grouped
6852 together. A future step could be to put the types in the same symtab as
6853 the CU the types ultimately came from. */
ff013f42 6854
f4dc4d17
DE
6855static hashval_t
6856hash_type_unit_group (const void *item)
6857{
9a3c8263
SM
6858 const struct type_unit_group *tu_group
6859 = (const struct type_unit_group *) item;
f4dc4d17 6860
094b34ac 6861 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 6862}
348e048f
DE
6863
6864static int
f4dc4d17 6865eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 6866{
9a3c8263
SM
6867 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6868 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 6869
094b34ac 6870 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 6871}
348e048f 6872
f4dc4d17
DE
6873/* Allocate a hash table for type unit groups. */
6874
6875static htab_t
6876allocate_type_unit_groups_table (void)
6877{
6878 return htab_create_alloc_ex (3,
6879 hash_type_unit_group,
6880 eq_type_unit_group,
6881 NULL,
6882 &dwarf2_per_objfile->objfile->objfile_obstack,
6883 hashtab_obstack_allocate,
6884 dummy_obstack_deallocate);
6885}
dee91e82 6886
f4dc4d17
DE
6887/* Type units that don't have DW_AT_stmt_list are grouped into their own
6888 partial symtabs. We combine several TUs per psymtab to not let the size
6889 of any one psymtab grow too big. */
6890#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6891#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 6892
094b34ac 6893/* Helper routine for get_type_unit_group.
f4dc4d17
DE
6894 Create the type_unit_group object used to hold one or more TUs. */
6895
6896static struct type_unit_group *
094b34ac 6897create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
6898{
6899 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 6900 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 6901 struct type_unit_group *tu_group;
f4dc4d17
DE
6902
6903 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6904 struct type_unit_group);
094b34ac 6905 per_cu = &tu_group->per_cu;
f4dc4d17 6906 per_cu->objfile = objfile;
f4dc4d17 6907
094b34ac
DE
6908 if (dwarf2_per_objfile->using_index)
6909 {
6910 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6911 struct dwarf2_per_cu_quick_data);
094b34ac
DE
6912 }
6913 else
6914 {
9c541725 6915 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
6916 struct partial_symtab *pst;
6917 char *name;
6918
6919 /* Give the symtab a useful name for debug purposes. */
6920 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6921 name = xstrprintf ("<type_units_%d>",
6922 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6923 else
6924 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
6925
6926 pst = create_partial_symtab (per_cu, name);
6927 pst->anonymous = 1;
f4dc4d17 6928
094b34ac
DE
6929 xfree (name);
6930 }
f4dc4d17 6931
094b34ac 6932 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 6933 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
6934
6935 return tu_group;
6936}
6937
094b34ac
DE
6938/* Look up the type_unit_group for type unit CU, and create it if necessary.
6939 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
6940
6941static struct type_unit_group *
ff39bb5e 6942get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
6943{
6944 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6945 struct type_unit_group *tu_group;
6946 void **slot;
6947 unsigned int line_offset;
6948 struct type_unit_group type_unit_group_for_lookup;
6949
6950 if (dwarf2_per_objfile->type_unit_groups == NULL)
6951 {
6952 dwarf2_per_objfile->type_unit_groups =
6953 allocate_type_unit_groups_table ();
6954 }
6955
6956 /* Do we need to create a new group, or can we use an existing one? */
6957
6958 if (stmt_list)
6959 {
6960 line_offset = DW_UNSND (stmt_list);
6961 ++tu_stats->nr_symtab_sharers;
6962 }
6963 else
6964 {
6965 /* Ugh, no stmt_list. Rare, but we have to handle it.
6966 We can do various things here like create one group per TU or
6967 spread them over multiple groups to split up the expansion work.
6968 To avoid worst case scenarios (too many groups or too large groups)
6969 we, umm, group them in bunches. */
6970 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6971 | (tu_stats->nr_stmt_less_type_units
6972 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6973 ++tu_stats->nr_stmt_less_type_units;
6974 }
6975
094b34ac 6976 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 6977 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
6978 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6979 &type_unit_group_for_lookup, INSERT);
6980 if (*slot != NULL)
6981 {
9a3c8263 6982 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
6983 gdb_assert (tu_group != NULL);
6984 }
6985 else
6986 {
9c541725 6987 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 6988 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
6989 *slot = tu_group;
6990 ++tu_stats->nr_symtabs;
6991 }
6992
6993 return tu_group;
6994}
0018ea6f
DE
6995\f
6996/* Partial symbol tables. */
6997
6998/* Create a psymtab named NAME and assign it to PER_CU.
6999
7000 The caller must fill in the following details:
7001 dirname, textlow, texthigh. */
7002
7003static struct partial_symtab *
7004create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7005{
7006 struct objfile *objfile = per_cu->objfile;
7007 struct partial_symtab *pst;
7008
18a94d75 7009 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
7010 objfile->global_psymbols,
7011 objfile->static_psymbols);
0018ea6f
DE
7012
7013 pst->psymtabs_addrmap_supported = 1;
7014
7015 /* This is the glue that links PST into GDB's symbol API. */
7016 pst->read_symtab_private = per_cu;
7017 pst->read_symtab = dwarf2_read_symtab;
7018 per_cu->v.psymtab = pst;
7019
7020 return pst;
7021}
7022
b93601f3
TT
7023/* The DATA object passed to process_psymtab_comp_unit_reader has this
7024 type. */
7025
7026struct process_psymtab_comp_unit_data
7027{
7028 /* True if we are reading a DW_TAG_partial_unit. */
7029
7030 int want_partial_unit;
7031
7032 /* The "pretend" language that is used if the CU doesn't declare a
7033 language. */
7034
7035 enum language pretend_language;
7036};
7037
0018ea6f
DE
7038/* die_reader_func for process_psymtab_comp_unit. */
7039
7040static void
7041process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7042 const gdb_byte *info_ptr,
0018ea6f
DE
7043 struct die_info *comp_unit_die,
7044 int has_children,
7045 void *data)
7046{
7047 struct dwarf2_cu *cu = reader->cu;
7048 struct objfile *objfile = cu->objfile;
3e29f34a 7049 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7050 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7051 CORE_ADDR baseaddr;
7052 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7053 struct partial_symtab *pst;
3a2b436a 7054 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7055 const char *filename;
9a3c8263
SM
7056 struct process_psymtab_comp_unit_data *info
7057 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 7058
b93601f3 7059 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
7060 return;
7061
7062 gdb_assert (! per_cu->is_debug_types);
7063
b93601f3 7064 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
7065
7066 cu->list_in_scope = &file_symbols;
7067
7068 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
7069 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7070 if (filename == NULL)
0018ea6f 7071 filename = "";
0018ea6f
DE
7072
7073 pst = create_partial_symtab (per_cu, filename);
7074
7075 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7076 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
7077
7078 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7079
7080 dwarf2_find_base_address (comp_unit_die, cu);
7081
7082 /* Possibly set the default values of LOWPC and HIGHPC from
7083 `DW_AT_ranges'. */
3a2b436a
JK
7084 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7085 &best_highpc, cu, pst);
7086 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
7087 /* Store the contiguous range if it is not empty; it can be empty for
7088 CUs with no code. */
7089 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
7090 gdbarch_adjust_dwarf2_addr (gdbarch,
7091 best_lowpc + baseaddr),
7092 gdbarch_adjust_dwarf2_addr (gdbarch,
7093 best_highpc + baseaddr) - 1,
7094 pst);
0018ea6f
DE
7095
7096 /* Check if comp unit has_children.
7097 If so, read the rest of the partial symbols from this comp unit.
7098 If not, there's no more debug_info for this comp unit. */
7099 if (has_children)
7100 {
7101 struct partial_die_info *first_die;
7102 CORE_ADDR lowpc, highpc;
7103
7104 lowpc = ((CORE_ADDR) -1);
7105 highpc = ((CORE_ADDR) 0);
7106
7107 first_die = load_partial_dies (reader, info_ptr, 1);
7108
7109 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7110 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7111
7112 /* If we didn't find a lowpc, set it to highpc to avoid
7113 complaints from `maint check'. */
7114 if (lowpc == ((CORE_ADDR) -1))
7115 lowpc = highpc;
7116
7117 /* If the compilation unit didn't have an explicit address range,
7118 then use the information extracted from its child dies. */
e385593e 7119 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7120 {
7121 best_lowpc = lowpc;
7122 best_highpc = highpc;
7123 }
7124 }
3e29f34a
MR
7125 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
7126 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 7127
8763cede 7128 end_psymtab_common (objfile, pst);
0018ea6f
DE
7129
7130 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
7131 {
7132 int i;
7133 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7134 struct dwarf2_per_cu_data *iter;
7135
7136 /* Fill in 'dependencies' here; we fill in 'users' in a
7137 post-pass. */
7138 pst->number_of_dependencies = len;
8d749320
SM
7139 pst->dependencies =
7140 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
7141 for (i = 0;
7142 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7143 i, iter);
7144 ++i)
7145 pst->dependencies[i] = iter->v.psymtab;
7146
7147 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7148 }
7149
7150 /* Get the list of files included in the current compilation unit,
7151 and build a psymtab for each of them. */
7152 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7153
b4f54984 7154 if (dwarf_read_debug)
0018ea6f
DE
7155 {
7156 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7157
7158 fprintf_unfiltered (gdb_stdlog,
7159 "Psymtab for %s unit @0x%x: %s - %s"
7160 ", %d global, %d static syms\n",
7161 per_cu->is_debug_types ? "type" : "comp",
9c541725 7162 to_underlying (per_cu->sect_off),
0018ea6f
DE
7163 paddress (gdbarch, pst->textlow),
7164 paddress (gdbarch, pst->texthigh),
7165 pst->n_global_syms, pst->n_static_syms);
7166 }
7167}
7168
7169/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7170 Process compilation unit THIS_CU for a psymtab. */
7171
7172static void
7173process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
7174 int want_partial_unit,
7175 enum language pretend_language)
0018ea6f
DE
7176{
7177 /* If this compilation unit was already read in, free the
7178 cached copy in order to read it in again. This is
7179 necessary because we skipped some symbols when we first
7180 read in the compilation unit (see load_partial_dies).
7181 This problem could be avoided, but the benefit is unclear. */
7182 if (this_cu->cu != NULL)
7183 free_one_cached_comp_unit (this_cu);
7184
f1902523
JK
7185 if (this_cu->is_debug_types)
7186 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
7187 NULL);
7188 else
7189 {
7190 process_psymtab_comp_unit_data info;
7191 info.want_partial_unit = want_partial_unit;
7192 info.pretend_language = pretend_language;
7193 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
7194 process_psymtab_comp_unit_reader, &info);
7195 }
0018ea6f
DE
7196
7197 /* Age out any secondary CUs. */
7198 age_cached_comp_units ();
7199}
f4dc4d17
DE
7200
7201/* Reader function for build_type_psymtabs. */
7202
7203static void
7204build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7205 const gdb_byte *info_ptr,
f4dc4d17
DE
7206 struct die_info *type_unit_die,
7207 int has_children,
7208 void *data)
7209{
7210 struct objfile *objfile = dwarf2_per_objfile->objfile;
7211 struct dwarf2_cu *cu = reader->cu;
7212 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7213 struct signatured_type *sig_type;
f4dc4d17
DE
7214 struct type_unit_group *tu_group;
7215 struct attribute *attr;
7216 struct partial_die_info *first_die;
7217 CORE_ADDR lowpc, highpc;
7218 struct partial_symtab *pst;
7219
7220 gdb_assert (data == NULL);
0186c6a7
DE
7221 gdb_assert (per_cu->is_debug_types);
7222 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
7223
7224 if (! has_children)
7225 return;
7226
7227 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 7228 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7229
0186c6a7 7230 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
7231
7232 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7233 cu->list_in_scope = &file_symbols;
7234 pst = create_partial_symtab (per_cu, "");
7235 pst->anonymous = 1;
7236
7237 first_die = load_partial_dies (reader, info_ptr, 1);
7238
7239 lowpc = (CORE_ADDR) -1;
7240 highpc = (CORE_ADDR) 0;
7241 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7242
8763cede 7243 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7244}
7245
73051182
DE
7246/* Struct used to sort TUs by their abbreviation table offset. */
7247
7248struct tu_abbrev_offset
7249{
7250 struct signatured_type *sig_type;
7251 sect_offset abbrev_offset;
7252};
7253
7254/* Helper routine for build_type_psymtabs_1, passed to qsort. */
7255
7256static int
7257sort_tu_by_abbrev_offset (const void *ap, const void *bp)
7258{
9a3c8263
SM
7259 const struct tu_abbrev_offset * const *a
7260 = (const struct tu_abbrev_offset * const*) ap;
7261 const struct tu_abbrev_offset * const *b
7262 = (const struct tu_abbrev_offset * const*) bp;
9c541725
PA
7263 sect_offset aoff = (*a)->abbrev_offset;
7264 sect_offset boff = (*b)->abbrev_offset;
73051182
DE
7265
7266 return (aoff > boff) - (aoff < boff);
7267}
7268
7269/* Efficiently read all the type units.
7270 This does the bulk of the work for build_type_psymtabs.
7271
7272 The efficiency is because we sort TUs by the abbrev table they use and
7273 only read each abbrev table once. In one program there are 200K TUs
7274 sharing 8K abbrev tables.
7275
7276 The main purpose of this function is to support building the
7277 dwarf2_per_objfile->type_unit_groups table.
7278 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7279 can collapse the search space by grouping them by stmt_list.
7280 The savings can be significant, in the same program from above the 200K TUs
7281 share 8K stmt_list tables.
7282
7283 FUNC is expected to call get_type_unit_group, which will create the
7284 struct type_unit_group if necessary and add it to
7285 dwarf2_per_objfile->type_unit_groups. */
7286
7287static void
7288build_type_psymtabs_1 (void)
7289{
73051182
DE
7290 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7291 struct cleanup *cleanups;
7292 struct abbrev_table *abbrev_table;
7293 sect_offset abbrev_offset;
7294 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
7295 int i;
7296
7297 /* It's up to the caller to not call us multiple times. */
7298 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7299
7300 if (dwarf2_per_objfile->n_type_units == 0)
7301 return;
7302
7303 /* TUs typically share abbrev tables, and there can be way more TUs than
7304 abbrev tables. Sort by abbrev table to reduce the number of times we
7305 read each abbrev table in.
7306 Alternatives are to punt or to maintain a cache of abbrev tables.
7307 This is simpler and efficient enough for now.
7308
7309 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7310 symtab to use). Typically TUs with the same abbrev offset have the same
7311 stmt_list value too so in practice this should work well.
7312
7313 The basic algorithm here is:
7314
7315 sort TUs by abbrev table
7316 for each TU with same abbrev table:
7317 read abbrev table if first user
7318 read TU top level DIE
7319 [IWBN if DWO skeletons had DW_AT_stmt_list]
7320 call FUNC */
7321
b4f54984 7322 if (dwarf_read_debug)
73051182
DE
7323 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7324
7325 /* Sort in a separate table to maintain the order of all_type_units
7326 for .gdb_index: TU indices directly index all_type_units. */
7327 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
7328 dwarf2_per_objfile->n_type_units);
7329 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
7330 {
7331 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
7332
7333 sorted_by_abbrev[i].sig_type = sig_type;
7334 sorted_by_abbrev[i].abbrev_offset =
7335 read_abbrev_offset (sig_type->per_cu.section,
9c541725 7336 sig_type->per_cu.sect_off);
73051182
DE
7337 }
7338 cleanups = make_cleanup (xfree, sorted_by_abbrev);
7339 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
7340 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
7341
9c541725 7342 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
7343 abbrev_table = NULL;
7344 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
7345
7346 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
7347 {
7348 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
7349
7350 /* Switch to the next abbrev table if necessary. */
7351 if (abbrev_table == NULL
9c541725 7352 || tu->abbrev_offset != abbrev_offset)
73051182
DE
7353 {
7354 if (abbrev_table != NULL)
7355 {
7356 abbrev_table_free (abbrev_table);
7357 /* Reset to NULL in case abbrev_table_read_table throws
7358 an error: abbrev_table_free_cleanup will get called. */
7359 abbrev_table = NULL;
7360 }
7361 abbrev_offset = tu->abbrev_offset;
7362 abbrev_table =
7363 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
7364 abbrev_offset);
7365 ++tu_stats->nr_uniq_abbrev_tables;
7366 }
7367
7368 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
7369 build_type_psymtabs_reader, NULL);
7370 }
7371
73051182 7372 do_cleanups (cleanups);
6aa5f3a6 7373}
73051182 7374
6aa5f3a6
DE
7375/* Print collected type unit statistics. */
7376
7377static void
7378print_tu_stats (void)
7379{
7380 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7381
7382 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7383 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
7384 dwarf2_per_objfile->n_type_units);
7385 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7386 tu_stats->nr_uniq_abbrev_tables);
7387 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7388 tu_stats->nr_symtabs);
7389 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7390 tu_stats->nr_symtab_sharers);
7391 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7392 tu_stats->nr_stmt_less_type_units);
7393 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7394 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7395}
7396
f4dc4d17
DE
7397/* Traversal function for build_type_psymtabs. */
7398
7399static int
7400build_type_psymtab_dependencies (void **slot, void *info)
7401{
7402 struct objfile *objfile = dwarf2_per_objfile->objfile;
7403 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7404 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 7405 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
7406 int len = VEC_length (sig_type_ptr, tu_group->tus);
7407 struct signatured_type *iter;
f4dc4d17
DE
7408 int i;
7409
7410 gdb_assert (len > 0);
0186c6a7 7411 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
7412
7413 pst->number_of_dependencies = len;
8d749320
SM
7414 pst->dependencies =
7415 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 7416 for (i = 0;
0186c6a7 7417 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
7418 ++i)
7419 {
0186c6a7
DE
7420 gdb_assert (iter->per_cu.is_debug_types);
7421 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7422 iter->type_unit_group = tu_group;
f4dc4d17
DE
7423 }
7424
0186c6a7 7425 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
7426
7427 return 1;
7428}
7429
7430/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7431 Build partial symbol tables for the .debug_types comp-units. */
7432
7433static void
7434build_type_psymtabs (struct objfile *objfile)
7435{
0e50663e 7436 if (! create_all_type_units (objfile))
348e048f
DE
7437 return;
7438
73051182 7439 build_type_psymtabs_1 ();
6aa5f3a6 7440}
f4dc4d17 7441
6aa5f3a6
DE
7442/* Traversal function for process_skeletonless_type_unit.
7443 Read a TU in a DWO file and build partial symbols for it. */
7444
7445static int
7446process_skeletonless_type_unit (void **slot, void *info)
7447{
7448 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9a3c8263 7449 struct objfile *objfile = (struct objfile *) info;
6aa5f3a6
DE
7450 struct signatured_type find_entry, *entry;
7451
7452 /* If this TU doesn't exist in the global table, add it and read it in. */
7453
7454 if (dwarf2_per_objfile->signatured_types == NULL)
7455 {
7456 dwarf2_per_objfile->signatured_types
7457 = allocate_signatured_type_table (objfile);
7458 }
7459
7460 find_entry.signature = dwo_unit->signature;
7461 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
7462 INSERT);
7463 /* If we've already seen this type there's nothing to do. What's happening
7464 is we're doing our own version of comdat-folding here. */
7465 if (*slot != NULL)
7466 return 1;
7467
7468 /* This does the job that create_all_type_units would have done for
7469 this TU. */
7470 entry = add_type_unit (dwo_unit->signature, slot);
7471 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
7472 *slot = entry;
7473
7474 /* This does the job that build_type_psymtabs_1 would have done. */
7475 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
7476 build_type_psymtabs_reader, NULL);
7477
7478 return 1;
7479}
7480
7481/* Traversal function for process_skeletonless_type_units. */
7482
7483static int
7484process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7485{
7486 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7487
7488 if (dwo_file->tus != NULL)
7489 {
7490 htab_traverse_noresize (dwo_file->tus,
7491 process_skeletonless_type_unit, info);
7492 }
7493
7494 return 1;
7495}
7496
7497/* Scan all TUs of DWO files, verifying we've processed them.
7498 This is needed in case a TU was emitted without its skeleton.
7499 Note: This can't be done until we know what all the DWO files are. */
7500
7501static void
7502process_skeletonless_type_units (struct objfile *objfile)
7503{
7504 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7505 if (get_dwp_file () == NULL
7506 && dwarf2_per_objfile->dwo_files != NULL)
7507 {
7508 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
7509 process_dwo_file_for_skeletonless_type_units,
7510 objfile);
7511 }
348e048f
DE
7512}
7513
95554aad
TT
7514/* Compute the 'user' field for each psymtab in OBJFILE. */
7515
7516static void
7517set_partial_user (struct objfile *objfile)
7518{
7519 int i;
7520
7521 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
7522 {
8832e7e3 7523 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
95554aad
TT
7524 struct partial_symtab *pst = per_cu->v.psymtab;
7525 int j;
7526
36586728
TT
7527 if (pst == NULL)
7528 continue;
7529
95554aad
TT
7530 for (j = 0; j < pst->number_of_dependencies; ++j)
7531 {
7532 /* Set the 'user' field only if it is not already set. */
7533 if (pst->dependencies[j]->user == NULL)
7534 pst->dependencies[j]->user = pst;
7535 }
7536 }
7537}
7538
93311388
DE
7539/* Build the partial symbol table by doing a quick pass through the
7540 .debug_info and .debug_abbrev sections. */
72bf9492 7541
93311388 7542static void
c67a9c90 7543dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 7544{
791afaa2 7545 struct cleanup *back_to;
21b2bd31 7546 int i;
93311388 7547
b4f54984 7548 if (dwarf_read_debug)
45cfd468
DE
7549 {
7550 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7551 objfile_name (objfile));
45cfd468
DE
7552 }
7553
98bfdba5
PA
7554 dwarf2_per_objfile->reading_partial_symbols = 1;
7555
be391dca 7556 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 7557
93311388
DE
7558 /* Any cached compilation units will be linked by the per-objfile
7559 read_in_chain. Make sure to free them when we're done. */
7560 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 7561
348e048f
DE
7562 build_type_psymtabs (objfile);
7563
93311388 7564 create_all_comp_units (objfile);
c906108c 7565
60606b2c
TT
7566 /* Create a temporary address map on a temporary obstack. We later
7567 copy this to the final obstack. */
8268c778 7568 auto_obstack temp_obstack;
791afaa2
TT
7569
7570 scoped_restore save_psymtabs_addrmap
7571 = make_scoped_restore (&objfile->psymtabs_addrmap,
7572 addrmap_create_mutable (&temp_obstack));
72bf9492 7573
21b2bd31 7574 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 7575 {
8832e7e3 7576 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
aaa75496 7577
b93601f3 7578 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 7579 }
ff013f42 7580
6aa5f3a6
DE
7581 /* This has to wait until we read the CUs, we need the list of DWOs. */
7582 process_skeletonless_type_units (objfile);
7583
7584 /* Now that all TUs have been processed we can fill in the dependencies. */
7585 if (dwarf2_per_objfile->type_unit_groups != NULL)
7586 {
7587 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
7588 build_type_psymtab_dependencies, NULL);
7589 }
7590
b4f54984 7591 if (dwarf_read_debug)
6aa5f3a6
DE
7592 print_tu_stats ();
7593
95554aad
TT
7594 set_partial_user (objfile);
7595
ff013f42
JK
7596 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
7597 &objfile->objfile_obstack);
791afaa2
TT
7598 /* At this point we want to keep the address map. */
7599 save_psymtabs_addrmap.release ();
ff013f42 7600
ae038cb0 7601 do_cleanups (back_to);
45cfd468 7602
b4f54984 7603 if (dwarf_read_debug)
45cfd468 7604 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7605 objfile_name (objfile));
ae038cb0
DJ
7606}
7607
3019eac3 7608/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
7609
7610static void
dee91e82 7611load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7612 const gdb_byte *info_ptr,
dee91e82
DE
7613 struct die_info *comp_unit_die,
7614 int has_children,
7615 void *data)
ae038cb0 7616{
dee91e82 7617 struct dwarf2_cu *cu = reader->cu;
ae038cb0 7618
95554aad 7619 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 7620
ae038cb0
DJ
7621 /* Check if comp unit has_children.
7622 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 7623 If not, there's no more debug_info for this comp unit. */
d85a05f0 7624 if (has_children)
dee91e82
DE
7625 load_partial_dies (reader, info_ptr, 0);
7626}
98bfdba5 7627
dee91e82
DE
7628/* Load the partial DIEs for a secondary CU into memory.
7629 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7630
dee91e82
DE
7631static void
7632load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7633{
f4dc4d17
DE
7634 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7635 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
7636}
7637
ae038cb0 7638static void
36586728
TT
7639read_comp_units_from_section (struct objfile *objfile,
7640 struct dwarf2_section_info *section,
f1902523 7641 struct dwarf2_section_info *abbrev_section,
36586728
TT
7642 unsigned int is_dwz,
7643 int *n_allocated,
7644 int *n_comp_units,
7645 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 7646{
d521ce57 7647 const gdb_byte *info_ptr;
be391dca 7648
b4f54984 7649 if (dwarf_read_debug)
bf6af496 7650 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
7651 get_section_name (section),
7652 get_section_file_name (section));
bf6af496 7653
36586728 7654 dwarf2_read_section (objfile, section);
ae038cb0 7655
36586728 7656 info_ptr = section->buffer;
6e70227d 7657
36586728 7658 while (info_ptr < section->buffer + section->size)
ae038cb0 7659 {
ae038cb0 7660 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7661
9c541725 7662 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7663
f1902523
JK
7664 comp_unit_head cu_header;
7665 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
7666 info_ptr, rcuh_kind::COMPILE);
ae038cb0
DJ
7667
7668 /* Save the compilation unit for later lookup. */
f1902523
JK
7669 if (cu_header.unit_type != DW_UT_type)
7670 {
7671 this_cu = XOBNEW (&objfile->objfile_obstack,
7672 struct dwarf2_per_cu_data);
7673 memset (this_cu, 0, sizeof (*this_cu));
7674 }
7675 else
7676 {
7677 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7678 struct signatured_type);
7679 memset (sig_type, 0, sizeof (*sig_type));
7680 sig_type->signature = cu_header.signature;
7681 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7682 this_cu = &sig_type->per_cu;
7683 }
7684 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7685 this_cu->sect_off = sect_off;
f1902523 7686 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7687 this_cu->is_dwz = is_dwz;
9291a0cd 7688 this_cu->objfile = objfile;
8a0459fd 7689 this_cu->section = section;
ae038cb0 7690
36586728 7691 if (*n_comp_units == *n_allocated)
ae038cb0 7692 {
36586728 7693 *n_allocated *= 2;
224c3ddb
SM
7694 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
7695 *all_comp_units, *n_allocated);
ae038cb0 7696 }
36586728
TT
7697 (*all_comp_units)[*n_comp_units] = this_cu;
7698 ++*n_comp_units;
ae038cb0
DJ
7699
7700 info_ptr = info_ptr + this_cu->length;
7701 }
36586728
TT
7702}
7703
7704/* Create a list of all compilation units in OBJFILE.
7705 This is only done for -readnow and building partial symtabs. */
7706
7707static void
7708create_all_comp_units (struct objfile *objfile)
7709{
7710 int n_allocated;
7711 int n_comp_units;
7712 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 7713 struct dwz_file *dwz;
36586728
TT
7714
7715 n_comp_units = 0;
7716 n_allocated = 10;
8d749320 7717 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728 7718
f1902523
JK
7719 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
7720 &dwarf2_per_objfile->abbrev, 0,
36586728
TT
7721 &n_allocated, &n_comp_units, &all_comp_units);
7722
4db1a1dc
TT
7723 dwz = dwarf2_get_dwz_file ();
7724 if (dwz != NULL)
f1902523 7725 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
4db1a1dc
TT
7726 &n_allocated, &n_comp_units,
7727 &all_comp_units);
ae038cb0 7728
8d749320
SM
7729 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
7730 struct dwarf2_per_cu_data *,
7731 n_comp_units);
ae038cb0
DJ
7732 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
7733 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
7734 xfree (all_comp_units);
7735 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
7736}
7737
5734ee8b 7738/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7739 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7740 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7741 DW_AT_ranges). See the comments of add_partial_subprogram on how
7742 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7743
72bf9492
DJ
7744static void
7745scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7746 CORE_ADDR *highpc, int set_addrmap,
7747 struct dwarf2_cu *cu)
c906108c 7748{
72bf9492 7749 struct partial_die_info *pdi;
c906108c 7750
91c24f0a
DC
7751 /* Now, march along the PDI's, descending into ones which have
7752 interesting children but skipping the children of the other ones,
7753 until we reach the end of the compilation unit. */
c906108c 7754
72bf9492 7755 pdi = first_die;
91c24f0a 7756
72bf9492
DJ
7757 while (pdi != NULL)
7758 {
7759 fixup_partial_die (pdi, cu);
c906108c 7760
f55ee35c 7761 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7762 children, so we need to look at them. Ditto for anonymous
7763 enums. */
933c6fe4 7764
72bf9492 7765 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
7766 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7767 || pdi->tag == DW_TAG_imported_unit)
c906108c 7768 {
72bf9492 7769 switch (pdi->tag)
c906108c
SS
7770 {
7771 case DW_TAG_subprogram:
cdc07690 7772 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 7773 break;
72929c62 7774 case DW_TAG_constant:
c906108c
SS
7775 case DW_TAG_variable:
7776 case DW_TAG_typedef:
91c24f0a 7777 case DW_TAG_union_type:
72bf9492 7778 if (!pdi->is_declaration)
63d06c5c 7779 {
72bf9492 7780 add_partial_symbol (pdi, cu);
63d06c5c
DC
7781 }
7782 break;
c906108c 7783 case DW_TAG_class_type:
680b30c7 7784 case DW_TAG_interface_type:
c906108c 7785 case DW_TAG_structure_type:
72bf9492 7786 if (!pdi->is_declaration)
c906108c 7787 {
72bf9492 7788 add_partial_symbol (pdi, cu);
c906108c 7789 }
e98c9e7c
TT
7790 if (cu->language == language_rust && pdi->has_children)
7791 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7792 set_addrmap, cu);
c906108c 7793 break;
91c24f0a 7794 case DW_TAG_enumeration_type:
72bf9492
DJ
7795 if (!pdi->is_declaration)
7796 add_partial_enumeration (pdi, cu);
c906108c
SS
7797 break;
7798 case DW_TAG_base_type:
a02abb62 7799 case DW_TAG_subrange_type:
c906108c 7800 /* File scope base type definitions are added to the partial
c5aa993b 7801 symbol table. */
72bf9492 7802 add_partial_symbol (pdi, cu);
c906108c 7803 break;
d9fa45fe 7804 case DW_TAG_namespace:
cdc07690 7805 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7806 break;
5d7cb8df 7807 case DW_TAG_module:
cdc07690 7808 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7809 break;
95554aad
TT
7810 case DW_TAG_imported_unit:
7811 {
7812 struct dwarf2_per_cu_data *per_cu;
7813
f4dc4d17
DE
7814 /* For now we don't handle imported units in type units. */
7815 if (cu->per_cu->is_debug_types)
7816 {
7817 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7818 " supported in type units [in module %s]"),
4262abfb 7819 objfile_name (cu->objfile));
f4dc4d17
DE
7820 }
7821
9c541725 7822 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
36586728 7823 pdi->is_dwz,
95554aad
TT
7824 cu->objfile);
7825
7826 /* Go read the partial unit, if needed. */
7827 if (per_cu->v.psymtab == NULL)
b93601f3 7828 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 7829
f4dc4d17 7830 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 7831 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
7832 }
7833 break;
74921315
KS
7834 case DW_TAG_imported_declaration:
7835 add_partial_symbol (pdi, cu);
7836 break;
c906108c
SS
7837 default:
7838 break;
7839 }
7840 }
7841
72bf9492
DJ
7842 /* If the die has a sibling, skip to the sibling. */
7843
7844 pdi = pdi->die_sibling;
7845 }
7846}
7847
7848/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7849
72bf9492 7850 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7851 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7852 Enumerators are an exception; they use the scope of their parent
7853 enumeration type, i.e. the name of the enumeration type is not
7854 prepended to the enumerator.
91c24f0a 7855
72bf9492
DJ
7856 There are two complexities. One is DW_AT_specification; in this
7857 case "parent" means the parent of the target of the specification,
7858 instead of the direct parent of the DIE. The other is compilers
7859 which do not emit DW_TAG_namespace; in this case we try to guess
7860 the fully qualified name of structure types from their members'
7861 linkage names. This must be done using the DIE's children rather
7862 than the children of any DW_AT_specification target. We only need
7863 to do this for structures at the top level, i.e. if the target of
7864 any DW_AT_specification (if any; otherwise the DIE itself) does not
7865 have a parent. */
7866
7867/* Compute the scope prefix associated with PDI's parent, in
7868 compilation unit CU. The result will be allocated on CU's
7869 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7870 field. NULL is returned if no prefix is necessary. */
15d034d0 7871static const char *
72bf9492
DJ
7872partial_die_parent_scope (struct partial_die_info *pdi,
7873 struct dwarf2_cu *cu)
7874{
15d034d0 7875 const char *grandparent_scope;
72bf9492 7876 struct partial_die_info *parent, *real_pdi;
91c24f0a 7877
72bf9492
DJ
7878 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7879 then this means the parent of the specification DIE. */
7880
7881 real_pdi = pdi;
72bf9492 7882 while (real_pdi->has_specification)
36586728
TT
7883 real_pdi = find_partial_die (real_pdi->spec_offset,
7884 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
7885
7886 parent = real_pdi->die_parent;
7887 if (parent == NULL)
7888 return NULL;
7889
7890 if (parent->scope_set)
7891 return parent->scope;
7892
7893 fixup_partial_die (parent, cu);
7894
10b3939b 7895 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 7896
acebe513
UW
7897 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7898 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7899 Work around this problem here. */
7900 if (cu->language == language_cplus
6e70227d 7901 && parent->tag == DW_TAG_namespace
acebe513
UW
7902 && strcmp (parent->name, "::") == 0
7903 && grandparent_scope == NULL)
7904 {
7905 parent->scope = NULL;
7906 parent->scope_set = 1;
7907 return NULL;
7908 }
7909
9c6c53f7
SA
7910 if (pdi->tag == DW_TAG_enumerator)
7911 /* Enumerators should not get the name of the enumeration as a prefix. */
7912 parent->scope = grandparent_scope;
7913 else if (parent->tag == DW_TAG_namespace
f55ee35c 7914 || parent->tag == DW_TAG_module
72bf9492
DJ
7915 || parent->tag == DW_TAG_structure_type
7916 || parent->tag == DW_TAG_class_type
680b30c7 7917 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
7918 || parent->tag == DW_TAG_union_type
7919 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
7920 {
7921 if (grandparent_scope == NULL)
7922 parent->scope = parent->name;
7923 else
3e43a32a
MS
7924 parent->scope = typename_concat (&cu->comp_unit_obstack,
7925 grandparent_scope,
f55ee35c 7926 parent->name, 0, cu);
72bf9492 7927 }
72bf9492
DJ
7928 else
7929 {
7930 /* FIXME drow/2004-04-01: What should we be doing with
7931 function-local names? For partial symbols, we should probably be
7932 ignoring them. */
7933 complaint (&symfile_complaints,
e2e0b3e5 7934 _("unhandled containing DIE tag %d for DIE at %d"),
9c541725 7935 parent->tag, to_underlying (pdi->sect_off));
72bf9492 7936 parent->scope = grandparent_scope;
c906108c
SS
7937 }
7938
72bf9492
DJ
7939 parent->scope_set = 1;
7940 return parent->scope;
7941}
7942
7943/* Return the fully scoped name associated with PDI, from compilation unit
7944 CU. The result will be allocated with malloc. */
4568ecf9 7945
72bf9492
DJ
7946static char *
7947partial_die_full_name (struct partial_die_info *pdi,
7948 struct dwarf2_cu *cu)
7949{
15d034d0 7950 const char *parent_scope;
72bf9492 7951
98bfdba5
PA
7952 /* If this is a template instantiation, we can not work out the
7953 template arguments from partial DIEs. So, unfortunately, we have
7954 to go through the full DIEs. At least any work we do building
7955 types here will be reused if full symbols are loaded later. */
7956 if (pdi->has_template_arguments)
7957 {
7958 fixup_partial_die (pdi, cu);
7959
7960 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7961 {
7962 struct die_info *die;
7963 struct attribute attr;
7964 struct dwarf2_cu *ref_cu = cu;
7965
b64f50a1 7966 /* DW_FORM_ref_addr is using section offset. */
b4069958 7967 attr.name = (enum dwarf_attribute) 0;
98bfdba5 7968 attr.form = DW_FORM_ref_addr;
9c541725 7969 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
7970 die = follow_die_ref (NULL, &attr, &ref_cu);
7971
7972 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7973 }
7974 }
7975
72bf9492
DJ
7976 parent_scope = partial_die_parent_scope (pdi, cu);
7977 if (parent_scope == NULL)
7978 return NULL;
7979 else
f55ee35c 7980 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
7981}
7982
7983static void
72bf9492 7984add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 7985{
e7c27a73 7986 struct objfile *objfile = cu->objfile;
3e29f34a 7987 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 7988 CORE_ADDR addr = 0;
15d034d0 7989 const char *actual_name = NULL;
e142c38c 7990 CORE_ADDR baseaddr;
15d034d0 7991 char *built_actual_name;
e142c38c
DJ
7992
7993 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7994
15d034d0
TT
7995 built_actual_name = partial_die_full_name (pdi, cu);
7996 if (built_actual_name != NULL)
7997 actual_name = built_actual_name;
63d06c5c 7998
72bf9492
DJ
7999 if (actual_name == NULL)
8000 actual_name = pdi->name;
8001
c906108c
SS
8002 switch (pdi->tag)
8003 {
8004 case DW_TAG_subprogram:
3e29f34a 8005 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 8006 if (pdi->is_external || cu->language == language_ada)
c906108c 8007 {
2cfa0c8d
JB
8008 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8009 of the global scope. But in Ada, we want to be able to access
8010 nested procedures globally. So all Ada subprograms are stored
8011 in the global scope. */
f47fb265 8012 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8013 built_actual_name != NULL,
f47fb265
MS
8014 VAR_DOMAIN, LOC_BLOCK,
8015 &objfile->global_psymbols,
1762568f 8016 addr, cu->language, objfile);
c906108c
SS
8017 }
8018 else
8019 {
f47fb265 8020 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8021 built_actual_name != NULL,
f47fb265
MS
8022 VAR_DOMAIN, LOC_BLOCK,
8023 &objfile->static_psymbols,
1762568f 8024 addr, cu->language, objfile);
c906108c 8025 }
0c1b455e
TT
8026
8027 if (pdi->main_subprogram && actual_name != NULL)
8028 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8029 break;
72929c62
JB
8030 case DW_TAG_constant:
8031 {
af5bf4ad 8032 std::vector<partial_symbol *> *list;
72929c62
JB
8033
8034 if (pdi->is_external)
8035 list = &objfile->global_psymbols;
8036 else
8037 list = &objfile->static_psymbols;
f47fb265 8038 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8039 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 8040 list, 0, cu->language, objfile);
72929c62
JB
8041 }
8042 break;
c906108c 8043 case DW_TAG_variable:
95554aad
TT
8044 if (pdi->d.locdesc)
8045 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8046
95554aad 8047 if (pdi->d.locdesc
caac4577
JG
8048 && addr == 0
8049 && !dwarf2_per_objfile->has_section_at_zero)
8050 {
8051 /* A global or static variable may also have been stripped
8052 out by the linker if unused, in which case its address
8053 will be nullified; do not add such variables into partial
8054 symbol table then. */
8055 }
8056 else if (pdi->is_external)
c906108c
SS
8057 {
8058 /* Global Variable.
8059 Don't enter into the minimal symbol tables as there is
8060 a minimal symbol table entry from the ELF symbols already.
8061 Enter into partial symbol table if it has a location
8062 descriptor or a type.
8063 If the location descriptor is missing, new_symbol will create
8064 a LOC_UNRESOLVED symbol, the address of the variable will then
8065 be determined from the minimal symbol table whenever the variable
8066 is referenced.
8067 The address for the partial symbol table entry is not
8068 used by GDB, but it comes in handy for debugging partial symbol
8069 table building. */
8070
95554aad 8071 if (pdi->d.locdesc || pdi->has_type)
f47fb265 8072 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8073 built_actual_name != NULL,
f47fb265
MS
8074 VAR_DOMAIN, LOC_STATIC,
8075 &objfile->global_psymbols,
1762568f 8076 addr + baseaddr,
f47fb265 8077 cu->language, objfile);
c906108c
SS
8078 }
8079 else
8080 {
ff908ebf
AW
8081 int has_loc = pdi->d.locdesc != NULL;
8082
8083 /* Static Variable. Skip symbols whose value we cannot know (those
8084 without location descriptors or constant values). */
8085 if (!has_loc && !pdi->has_const_value)
decbce07 8086 {
15d034d0 8087 xfree (built_actual_name);
decbce07
MS
8088 return;
8089 }
ff908ebf 8090
f47fb265 8091 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8092 built_actual_name != NULL,
f47fb265
MS
8093 VAR_DOMAIN, LOC_STATIC,
8094 &objfile->static_psymbols,
ff908ebf 8095 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 8096 cu->language, objfile);
c906108c
SS
8097 }
8098 break;
8099 case DW_TAG_typedef:
8100 case DW_TAG_base_type:
a02abb62 8101 case DW_TAG_subrange_type:
38d518c9 8102 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8103 built_actual_name != NULL,
176620f1 8104 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 8105 &objfile->static_psymbols,
1762568f 8106 0, cu->language, objfile);
c906108c 8107 break;
74921315 8108 case DW_TAG_imported_declaration:
72bf9492
DJ
8109 case DW_TAG_namespace:
8110 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8111 built_actual_name != NULL,
72bf9492
DJ
8112 VAR_DOMAIN, LOC_TYPEDEF,
8113 &objfile->global_psymbols,
1762568f 8114 0, cu->language, objfile);
72bf9492 8115 break;
530e8392
KB
8116 case DW_TAG_module:
8117 add_psymbol_to_list (actual_name, strlen (actual_name),
8118 built_actual_name != NULL,
8119 MODULE_DOMAIN, LOC_TYPEDEF,
8120 &objfile->global_psymbols,
1762568f 8121 0, cu->language, objfile);
530e8392 8122 break;
c906108c 8123 case DW_TAG_class_type:
680b30c7 8124 case DW_TAG_interface_type:
c906108c
SS
8125 case DW_TAG_structure_type:
8126 case DW_TAG_union_type:
8127 case DW_TAG_enumeration_type:
fa4028e9
JB
8128 /* Skip external references. The DWARF standard says in the section
8129 about "Structure, Union, and Class Type Entries": "An incomplete
8130 structure, union or class type is represented by a structure,
8131 union or class entry that does not have a byte size attribute
8132 and that has a DW_AT_declaration attribute." */
8133 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 8134 {
15d034d0 8135 xfree (built_actual_name);
decbce07
MS
8136 return;
8137 }
fa4028e9 8138
63d06c5c
DC
8139 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8140 static vs. global. */
38d518c9 8141 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8142 built_actual_name != NULL,
176620f1 8143 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 8144 cu->language == language_cplus
63d06c5c
DC
8145 ? &objfile->global_psymbols
8146 : &objfile->static_psymbols,
1762568f 8147 0, cu->language, objfile);
c906108c 8148
c906108c
SS
8149 break;
8150 case DW_TAG_enumerator:
38d518c9 8151 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8152 built_actual_name != NULL,
176620f1 8153 VAR_DOMAIN, LOC_CONST,
9c37b5ae 8154 cu->language == language_cplus
f6fe98ef
DJ
8155 ? &objfile->global_psymbols
8156 : &objfile->static_psymbols,
1762568f 8157 0, cu->language, objfile);
c906108c
SS
8158 break;
8159 default:
8160 break;
8161 }
5c4e30ca 8162
15d034d0 8163 xfree (built_actual_name);
c906108c
SS
8164}
8165
5c4e30ca
DC
8166/* Read a partial die corresponding to a namespace; also, add a symbol
8167 corresponding to that namespace to the symbol table. NAMESPACE is
8168 the name of the enclosing namespace. */
91c24f0a 8169
72bf9492
DJ
8170static void
8171add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8172 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8173 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8174{
72bf9492 8175 /* Add a symbol for the namespace. */
e7c27a73 8176
72bf9492 8177 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8178
8179 /* Now scan partial symbols in that namespace. */
8180
91c24f0a 8181 if (pdi->has_children)
cdc07690 8182 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8183}
8184
5d7cb8df
JK
8185/* Read a partial die corresponding to a Fortran module. */
8186
8187static void
8188add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8189 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8190{
530e8392
KB
8191 /* Add a symbol for the namespace. */
8192
8193 add_partial_symbol (pdi, cu);
8194
f55ee35c 8195 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8196
8197 if (pdi->has_children)
cdc07690 8198 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8199}
8200
bc30ff58
JB
8201/* Read a partial die corresponding to a subprogram and create a partial
8202 symbol for that subprogram. When the CU language allows it, this
8203 routine also defines a partial symbol for each nested subprogram
cdc07690 8204 that this subprogram contains. If SET_ADDRMAP is true, record the
428fc5fc
YQ
8205 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
8206 and highest PC values found in PDI.
6e70227d 8207
cdc07690
YQ
8208 PDI may also be a lexical block, in which case we simply search
8209 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8210 Again, this is only performed when the CU language allows this
8211 type of definitions. */
8212
8213static void
8214add_partial_subprogram (struct partial_die_info *pdi,
8215 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8216 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58
JB
8217{
8218 if (pdi->tag == DW_TAG_subprogram)
8219 {
8220 if (pdi->has_pc_info)
8221 {
8222 if (pdi->lowpc < *lowpc)
8223 *lowpc = pdi->lowpc;
8224 if (pdi->highpc > *highpc)
8225 *highpc = pdi->highpc;
cdc07690 8226 if (set_addrmap)
5734ee8b 8227 {
5734ee8b 8228 struct objfile *objfile = cu->objfile;
3e29f34a
MR
8229 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8230 CORE_ADDR baseaddr;
8231 CORE_ADDR highpc;
8232 CORE_ADDR lowpc;
5734ee8b
DJ
8233
8234 baseaddr = ANOFFSET (objfile->section_offsets,
8235 SECT_OFF_TEXT (objfile));
3e29f34a
MR
8236 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
8237 pdi->lowpc + baseaddr);
8238 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
8239 pdi->highpc + baseaddr);
8240 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 8241 cu->per_cu->v.psymtab);
5734ee8b 8242 }
481860b3
GB
8243 }
8244
8245 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8246 {
bc30ff58 8247 if (!pdi->is_declaration)
e8d05480
JB
8248 /* Ignore subprogram DIEs that do not have a name, they are
8249 illegal. Do not emit a complaint at this point, we will
8250 do so when we convert this psymtab into a symtab. */
8251 if (pdi->name)
8252 add_partial_symbol (pdi, cu);
bc30ff58
JB
8253 }
8254 }
6e70227d 8255
bc30ff58
JB
8256 if (! pdi->has_children)
8257 return;
8258
8259 if (cu->language == language_ada)
8260 {
8261 pdi = pdi->die_child;
8262 while (pdi != NULL)
8263 {
8264 fixup_partial_die (pdi, cu);
8265 if (pdi->tag == DW_TAG_subprogram
8266 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8267 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8268 pdi = pdi->die_sibling;
8269 }
8270 }
8271}
8272
91c24f0a
DC
8273/* Read a partial die corresponding to an enumeration type. */
8274
72bf9492
DJ
8275static void
8276add_partial_enumeration (struct partial_die_info *enum_pdi,
8277 struct dwarf2_cu *cu)
91c24f0a 8278{
72bf9492 8279 struct partial_die_info *pdi;
91c24f0a
DC
8280
8281 if (enum_pdi->name != NULL)
72bf9492
DJ
8282 add_partial_symbol (enum_pdi, cu);
8283
8284 pdi = enum_pdi->die_child;
8285 while (pdi)
91c24f0a 8286 {
72bf9492 8287 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 8288 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 8289 else
72bf9492
DJ
8290 add_partial_symbol (pdi, cu);
8291 pdi = pdi->die_sibling;
91c24f0a 8292 }
91c24f0a
DC
8293}
8294
6caca83c
CC
8295/* Return the initial uleb128 in the die at INFO_PTR. */
8296
8297static unsigned int
d521ce57 8298peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8299{
8300 unsigned int bytes_read;
8301
8302 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8303}
8304
4bb7a0a7
DJ
8305/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
8306 Return the corresponding abbrev, or NULL if the number is zero (indicating
8307 an empty DIE). In either case *BYTES_READ will be set to the length of
8308 the initial number. */
8309
8310static struct abbrev_info *
d521ce57 8311peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 8312 struct dwarf2_cu *cu)
4bb7a0a7
DJ
8313{
8314 bfd *abfd = cu->objfile->obfd;
8315 unsigned int abbrev_number;
8316 struct abbrev_info *abbrev;
8317
8318 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8319
8320 if (abbrev_number == 0)
8321 return NULL;
8322
433df2d4 8323 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
8324 if (!abbrev)
8325 {
422b9917
DE
8326 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8327 " at offset 0x%x [in module %s]"),
8328 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9c541725 8329 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8330 }
8331
8332 return abbrev;
8333}
8334
93311388
DE
8335/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8336 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8337 DIE. Any children of the skipped DIEs will also be skipped. */
8338
d521ce57
TT
8339static const gdb_byte *
8340skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8341{
dee91e82 8342 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
8343 struct abbrev_info *abbrev;
8344 unsigned int bytes_read;
8345
8346 while (1)
8347 {
8348 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8349 if (abbrev == NULL)
8350 return info_ptr + bytes_read;
8351 else
dee91e82 8352 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8353 }
8354}
8355
93311388
DE
8356/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8357 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8358 abbrev corresponding to that skipped uleb128 should be passed in
8359 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8360 children. */
8361
d521ce57
TT
8362static const gdb_byte *
8363skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8364 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8365{
8366 unsigned int bytes_read;
8367 struct attribute attr;
dee91e82
DE
8368 bfd *abfd = reader->abfd;
8369 struct dwarf2_cu *cu = reader->cu;
d521ce57 8370 const gdb_byte *buffer = reader->buffer;
f664829e 8371 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8372 unsigned int form, i;
8373
8374 for (i = 0; i < abbrev->num_attrs; i++)
8375 {
8376 /* The only abbrev we care about is DW_AT_sibling. */
8377 if (abbrev->attrs[i].name == DW_AT_sibling)
8378 {
dee91e82 8379 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8380 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
8381 complaint (&symfile_complaints,
8382 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8383 else
b9502d3f 8384 {
9c541725
PA
8385 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8386 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8387
8388 if (sibling_ptr < info_ptr)
8389 complaint (&symfile_complaints,
8390 _("DW_AT_sibling points backwards"));
22869d73
KS
8391 else if (sibling_ptr > reader->buffer_end)
8392 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
8393 else
8394 return sibling_ptr;
8395 }
4bb7a0a7
DJ
8396 }
8397
8398 /* If it isn't DW_AT_sibling, skip this attribute. */
8399 form = abbrev->attrs[i].form;
8400 skip_attribute:
8401 switch (form)
8402 {
4bb7a0a7 8403 case DW_FORM_ref_addr:
ae411497
TT
8404 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8405 and later it is offset sized. */
8406 if (cu->header.version == 2)
8407 info_ptr += cu->header.addr_size;
8408 else
8409 info_ptr += cu->header.offset_size;
8410 break;
36586728
TT
8411 case DW_FORM_GNU_ref_alt:
8412 info_ptr += cu->header.offset_size;
8413 break;
ae411497 8414 case DW_FORM_addr:
4bb7a0a7
DJ
8415 info_ptr += cu->header.addr_size;
8416 break;
8417 case DW_FORM_data1:
8418 case DW_FORM_ref1:
8419 case DW_FORM_flag:
8420 info_ptr += 1;
8421 break;
2dc7f7b3 8422 case DW_FORM_flag_present:
43988095 8423 case DW_FORM_implicit_const:
2dc7f7b3 8424 break;
4bb7a0a7
DJ
8425 case DW_FORM_data2:
8426 case DW_FORM_ref2:
8427 info_ptr += 2;
8428 break;
8429 case DW_FORM_data4:
8430 case DW_FORM_ref4:
8431 info_ptr += 4;
8432 break;
8433 case DW_FORM_data8:
8434 case DW_FORM_ref8:
55f1336d 8435 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8436 info_ptr += 8;
8437 break;
0224619f
JK
8438 case DW_FORM_data16:
8439 info_ptr += 16;
8440 break;
4bb7a0a7 8441 case DW_FORM_string:
9b1c24c8 8442 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8443 info_ptr += bytes_read;
8444 break;
2dc7f7b3 8445 case DW_FORM_sec_offset:
4bb7a0a7 8446 case DW_FORM_strp:
36586728 8447 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8448 info_ptr += cu->header.offset_size;
8449 break;
2dc7f7b3 8450 case DW_FORM_exprloc:
4bb7a0a7
DJ
8451 case DW_FORM_block:
8452 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8453 info_ptr += bytes_read;
8454 break;
8455 case DW_FORM_block1:
8456 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8457 break;
8458 case DW_FORM_block2:
8459 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8460 break;
8461 case DW_FORM_block4:
8462 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8463 break;
8464 case DW_FORM_sdata:
8465 case DW_FORM_udata:
8466 case DW_FORM_ref_udata:
3019eac3
DE
8467 case DW_FORM_GNU_addr_index:
8468 case DW_FORM_GNU_str_index:
d521ce57 8469 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8470 break;
8471 case DW_FORM_indirect:
8472 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8473 info_ptr += bytes_read;
8474 /* We need to continue parsing from here, so just go back to
8475 the top. */
8476 goto skip_attribute;
8477
8478 default:
3e43a32a
MS
8479 error (_("Dwarf Error: Cannot handle %s "
8480 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8481 dwarf_form_name (form),
8482 bfd_get_filename (abfd));
8483 }
8484 }
8485
8486 if (abbrev->has_children)
dee91e82 8487 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8488 else
8489 return info_ptr;
8490}
8491
93311388 8492/* Locate ORIG_PDI's sibling.
dee91e82 8493 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8494
d521ce57 8495static const gdb_byte *
dee91e82
DE
8496locate_pdi_sibling (const struct die_reader_specs *reader,
8497 struct partial_die_info *orig_pdi,
d521ce57 8498 const gdb_byte *info_ptr)
91c24f0a
DC
8499{
8500 /* Do we know the sibling already? */
72bf9492 8501
91c24f0a
DC
8502 if (orig_pdi->sibling)
8503 return orig_pdi->sibling;
8504
8505 /* Are there any children to deal with? */
8506
8507 if (!orig_pdi->has_children)
8508 return info_ptr;
8509
4bb7a0a7 8510 /* Skip the children the long way. */
91c24f0a 8511
dee91e82 8512 return skip_children (reader, info_ptr);
91c24f0a
DC
8513}
8514
257e7a09 8515/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8516 not NULL. */
c906108c
SS
8517
8518static void
257e7a09
YQ
8519dwarf2_read_symtab (struct partial_symtab *self,
8520 struct objfile *objfile)
c906108c 8521{
257e7a09 8522 if (self->readin)
c906108c 8523 {
442e4d9c 8524 warning (_("bug: psymtab for %s is already read in."),
257e7a09 8525 self->filename);
442e4d9c
YQ
8526 }
8527 else
8528 {
8529 if (info_verbose)
c906108c 8530 {
442e4d9c 8531 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 8532 self->filename);
442e4d9c 8533 gdb_flush (gdb_stdout);
c906108c 8534 }
c906108c 8535
442e4d9c 8536 /* Restore our global data. */
9a3c8263
SM
8537 dwarf2_per_objfile
8538 = (struct dwarf2_per_objfile *) objfile_data (objfile,
8539 dwarf2_objfile_data_key);
10b3939b 8540
442e4d9c
YQ
8541 /* If this psymtab is constructed from a debug-only objfile, the
8542 has_section_at_zero flag will not necessarily be correct. We
8543 can get the correct value for this flag by looking at the data
8544 associated with the (presumably stripped) associated objfile. */
8545 if (objfile->separate_debug_objfile_backlink)
8546 {
8547 struct dwarf2_per_objfile *dpo_backlink
9a3c8263
SM
8548 = ((struct dwarf2_per_objfile *)
8549 objfile_data (objfile->separate_debug_objfile_backlink,
8550 dwarf2_objfile_data_key));
9a619af0 8551
442e4d9c
YQ
8552 dwarf2_per_objfile->has_section_at_zero
8553 = dpo_backlink->has_section_at_zero;
8554 }
b2ab525c 8555
442e4d9c 8556 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 8557
257e7a09 8558 psymtab_to_symtab_1 (self);
c906108c 8559
442e4d9c
YQ
8560 /* Finish up the debug error message. */
8561 if (info_verbose)
8562 printf_filtered (_("done.\n"));
c906108c 8563 }
95554aad
TT
8564
8565 process_cu_includes ();
c906108c 8566}
9cdd5dbd
DE
8567\f
8568/* Reading in full CUs. */
c906108c 8569
10b3939b
DJ
8570/* Add PER_CU to the queue. */
8571
8572static void
95554aad
TT
8573queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8574 enum language pretend_language)
10b3939b
DJ
8575{
8576 struct dwarf2_queue_item *item;
8577
8578 per_cu->queued = 1;
8d749320 8579 item = XNEW (struct dwarf2_queue_item);
10b3939b 8580 item->per_cu = per_cu;
95554aad 8581 item->pretend_language = pretend_language;
10b3939b
DJ
8582 item->next = NULL;
8583
8584 if (dwarf2_queue == NULL)
8585 dwarf2_queue = item;
8586 else
8587 dwarf2_queue_tail->next = item;
8588
8589 dwarf2_queue_tail = item;
8590}
8591
89e63ee4
DE
8592/* If PER_CU is not yet queued, add it to the queue.
8593 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8594 dependency.
0907af0c 8595 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8596 meaning either PER_CU is already queued or it is already loaded.
8597
8598 N.B. There is an invariant here that if a CU is queued then it is loaded.
8599 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8600
8601static int
89e63ee4 8602maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8603 struct dwarf2_per_cu_data *per_cu,
8604 enum language pretend_language)
8605{
8606 /* We may arrive here during partial symbol reading, if we need full
8607 DIEs to process an unusual case (e.g. template arguments). Do
8608 not queue PER_CU, just tell our caller to load its DIEs. */
8609 if (dwarf2_per_objfile->reading_partial_symbols)
8610 {
8611 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8612 return 1;
8613 return 0;
8614 }
8615
8616 /* Mark the dependence relation so that we don't flush PER_CU
8617 too early. */
89e63ee4
DE
8618 if (dependent_cu != NULL)
8619 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8620
8621 /* If it's already on the queue, we have nothing to do. */
8622 if (per_cu->queued)
8623 return 0;
8624
8625 /* If the compilation unit is already loaded, just mark it as
8626 used. */
8627 if (per_cu->cu != NULL)
8628 {
8629 per_cu->cu->last_used = 0;
8630 return 0;
8631 }
8632
8633 /* Add it to the queue. */
8634 queue_comp_unit (per_cu, pretend_language);
8635
8636 return 1;
8637}
8638
10b3939b
DJ
8639/* Process the queue. */
8640
8641static void
a0f42c21 8642process_queue (void)
10b3939b
DJ
8643{
8644 struct dwarf2_queue_item *item, *next_item;
8645
b4f54984 8646 if (dwarf_read_debug)
45cfd468
DE
8647 {
8648 fprintf_unfiltered (gdb_stdlog,
8649 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8650 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8651 }
8652
03dd20cc
DJ
8653 /* The queue starts out with one item, but following a DIE reference
8654 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
8655 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
8656 {
cc12ce38
DE
8657 if ((dwarf2_per_objfile->using_index
8658 ? !item->per_cu->v.quick->compunit_symtab
8659 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
8660 /* Skip dummy CUs. */
8661 && item->per_cu->cu != NULL)
f4dc4d17
DE
8662 {
8663 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 8664 unsigned int debug_print_threshold;
247f5c4f 8665 char buf[100];
f4dc4d17 8666
247f5c4f 8667 if (per_cu->is_debug_types)
f4dc4d17 8668 {
247f5c4f
DE
8669 struct signatured_type *sig_type =
8670 (struct signatured_type *) per_cu;
8671
8672 sprintf (buf, "TU %s at offset 0x%x",
73be47f5 8673 hex_string (sig_type->signature),
9c541725 8674 to_underlying (per_cu->sect_off));
73be47f5
DE
8675 /* There can be 100s of TUs.
8676 Only print them in verbose mode. */
8677 debug_print_threshold = 2;
f4dc4d17 8678 }
247f5c4f 8679 else
73be47f5 8680 {
9c541725
PA
8681 sprintf (buf, "CU at offset 0x%x",
8682 to_underlying (per_cu->sect_off));
73be47f5
DE
8683 debug_print_threshold = 1;
8684 }
247f5c4f 8685
b4f54984 8686 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8687 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
8688
8689 if (per_cu->is_debug_types)
8690 process_full_type_unit (per_cu, item->pretend_language);
8691 else
8692 process_full_comp_unit (per_cu, item->pretend_language);
8693
b4f54984 8694 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8695 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 8696 }
10b3939b
DJ
8697
8698 item->per_cu->queued = 0;
8699 next_item = item->next;
8700 xfree (item);
8701 }
8702
8703 dwarf2_queue_tail = NULL;
45cfd468 8704
b4f54984 8705 if (dwarf_read_debug)
45cfd468
DE
8706 {
8707 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 8708 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 8709 }
10b3939b
DJ
8710}
8711
8712/* Free all allocated queue entries. This function only releases anything if
8713 an error was thrown; if the queue was processed then it would have been
8714 freed as we went along. */
8715
8716static void
8717dwarf2_release_queue (void *dummy)
8718{
8719 struct dwarf2_queue_item *item, *last;
8720
8721 item = dwarf2_queue;
8722 while (item)
8723 {
8724 /* Anything still marked queued is likely to be in an
8725 inconsistent state, so discard it. */
8726 if (item->per_cu->queued)
8727 {
8728 if (item->per_cu->cu != NULL)
dee91e82 8729 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
8730 item->per_cu->queued = 0;
8731 }
8732
8733 last = item;
8734 item = item->next;
8735 xfree (last);
8736 }
8737
8738 dwarf2_queue = dwarf2_queue_tail = NULL;
8739}
8740
8741/* Read in full symbols for PST, and anything it depends on. */
8742
c906108c 8743static void
fba45db2 8744psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 8745{
10b3939b 8746 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
8747 int i;
8748
95554aad
TT
8749 if (pst->readin)
8750 return;
8751
aaa75496 8752 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
8753 if (!pst->dependencies[i]->readin
8754 && pst->dependencies[i]->user == NULL)
aaa75496
JB
8755 {
8756 /* Inform about additional files that need to be read in. */
8757 if (info_verbose)
8758 {
a3f17187 8759 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
8760 fputs_filtered (" ", gdb_stdout);
8761 wrap_here ("");
8762 fputs_filtered ("and ", gdb_stdout);
8763 wrap_here ("");
8764 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 8765 wrap_here (""); /* Flush output. */
aaa75496
JB
8766 gdb_flush (gdb_stdout);
8767 }
8768 psymtab_to_symtab_1 (pst->dependencies[i]);
8769 }
8770
9a3c8263 8771 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
8772
8773 if (per_cu == NULL)
aaa75496
JB
8774 {
8775 /* It's an include file, no symbols to read for it.
8776 Everything is in the parent symtab. */
8777 pst->readin = 1;
8778 return;
8779 }
c906108c 8780
a0f42c21 8781 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
8782}
8783
dee91e82
DE
8784/* Trivial hash function for die_info: the hash value of a DIE
8785 is its offset in .debug_info for this objfile. */
10b3939b 8786
dee91e82
DE
8787static hashval_t
8788die_hash (const void *item)
10b3939b 8789{
9a3c8263 8790 const struct die_info *die = (const struct die_info *) item;
6502dd73 8791
9c541725 8792 return to_underlying (die->sect_off);
dee91e82 8793}
63d06c5c 8794
dee91e82
DE
8795/* Trivial comparison function for die_info structures: two DIEs
8796 are equal if they have the same offset. */
98bfdba5 8797
dee91e82
DE
8798static int
8799die_eq (const void *item_lhs, const void *item_rhs)
8800{
9a3c8263
SM
8801 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8802 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8803
9c541725 8804 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8805}
c906108c 8806
dee91e82
DE
8807/* die_reader_func for load_full_comp_unit.
8808 This is identical to read_signatured_type_reader,
8809 but is kept separate for now. */
c906108c 8810
dee91e82
DE
8811static void
8812load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8813 const gdb_byte *info_ptr,
dee91e82
DE
8814 struct die_info *comp_unit_die,
8815 int has_children,
8816 void *data)
8817{
8818 struct dwarf2_cu *cu = reader->cu;
9a3c8263 8819 enum language *language_ptr = (enum language *) data;
6caca83c 8820
dee91e82
DE
8821 gdb_assert (cu->die_hash == NULL);
8822 cu->die_hash =
8823 htab_create_alloc_ex (cu->header.length / 12,
8824 die_hash,
8825 die_eq,
8826 NULL,
8827 &cu->comp_unit_obstack,
8828 hashtab_obstack_allocate,
8829 dummy_obstack_deallocate);
e142c38c 8830
dee91e82
DE
8831 if (has_children)
8832 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
8833 &info_ptr, comp_unit_die);
8834 cu->dies = comp_unit_die;
8835 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8836
8837 /* We try not to read any attributes in this function, because not
9cdd5dbd 8838 all CUs needed for references have been loaded yet, and symbol
10b3939b 8839 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8840 or we won't be able to build types correctly.
8841 Similarly, if we do not read the producer, we can not apply
8842 producer-specific interpretation. */
95554aad 8843 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 8844}
10b3939b 8845
dee91e82 8846/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 8847
dee91e82 8848static void
95554aad
TT
8849load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8850 enum language pretend_language)
dee91e82 8851{
3019eac3 8852 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 8853
f4dc4d17
DE
8854 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8855 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
8856}
8857
3da10d80
KS
8858/* Add a DIE to the delayed physname list. */
8859
8860static void
8861add_to_method_list (struct type *type, int fnfield_index, int index,
8862 const char *name, struct die_info *die,
8863 struct dwarf2_cu *cu)
8864{
8865 struct delayed_method_info mi;
8866 mi.type = type;
8867 mi.fnfield_index = fnfield_index;
8868 mi.index = index;
8869 mi.name = name;
8870 mi.die = die;
8871 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
8872}
8873
8874/* A cleanup for freeing the delayed method list. */
8875
8876static void
8877free_delayed_list (void *ptr)
8878{
8879 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
8880 if (cu->method_list != NULL)
8881 {
8882 VEC_free (delayed_method_info, cu->method_list);
8883 cu->method_list = NULL;
8884 }
8885}
8886
3693fdb3
PA
8887/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8888 "const" / "volatile". If so, decrements LEN by the length of the
8889 modifier and return true. Otherwise return false. */
8890
8891template<size_t N>
8892static bool
8893check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8894{
8895 size_t mod_len = sizeof (mod) - 1;
8896 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8897 {
8898 len -= mod_len;
8899 return true;
8900 }
8901 return false;
8902}
8903
3da10d80
KS
8904/* Compute the physnames of any methods on the CU's method list.
8905
8906 The computation of method physnames is delayed in order to avoid the
8907 (bad) condition that one of the method's formal parameters is of an as yet
8908 incomplete type. */
8909
8910static void
8911compute_delayed_physnames (struct dwarf2_cu *cu)
8912{
8913 int i;
8914 struct delayed_method_info *mi;
3693fdb3
PA
8915
8916 /* Only C++ delays computing physnames. */
8917 if (VEC_empty (delayed_method_info, cu->method_list))
8918 return;
8919 gdb_assert (cu->language == language_cplus);
8920
3da10d80
KS
8921 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
8922 {
1d06ead6 8923 const char *physname;
3da10d80
KS
8924 struct fn_fieldlist *fn_flp
8925 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 8926 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
8927 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
8928 = physname ? physname : "";
3693fdb3
PA
8929
8930 /* Since there's no tag to indicate whether a method is a
8931 const/volatile overload, extract that information out of the
8932 demangled name. */
8933 if (physname != NULL)
8934 {
8935 size_t len = strlen (physname);
8936
8937 while (1)
8938 {
8939 if (physname[len] == ')') /* shortcut */
8940 break;
8941 else if (check_modifier (physname, len, " const"))
8942 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
8943 else if (check_modifier (physname, len, " volatile"))
8944 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
8945 else
8946 break;
8947 }
8948 }
3da10d80
KS
8949 }
8950}
8951
a766d390
DE
8952/* Go objects should be embedded in a DW_TAG_module DIE,
8953 and it's not clear if/how imported objects will appear.
8954 To keep Go support simple until that's worked out,
8955 go back through what we've read and create something usable.
8956 We could do this while processing each DIE, and feels kinda cleaner,
8957 but that way is more invasive.
8958 This is to, for example, allow the user to type "p var" or "b main"
8959 without having to specify the package name, and allow lookups
8960 of module.object to work in contexts that use the expression
8961 parser. */
8962
8963static void
8964fixup_go_packaging (struct dwarf2_cu *cu)
8965{
8966 char *package_name = NULL;
8967 struct pending *list;
8968 int i;
8969
8970 for (list = global_symbols; list != NULL; list = list->next)
8971 {
8972 for (i = 0; i < list->nsyms; ++i)
8973 {
8974 struct symbol *sym = list->symbol[i];
8975
8976 if (SYMBOL_LANGUAGE (sym) == language_go
8977 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8978 {
8979 char *this_package_name = go_symbol_package_name (sym);
8980
8981 if (this_package_name == NULL)
8982 continue;
8983 if (package_name == NULL)
8984 package_name = this_package_name;
8985 else
8986 {
8987 if (strcmp (package_name, this_package_name) != 0)
8988 complaint (&symfile_complaints,
8989 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
8990 (symbol_symtab (sym) != NULL
8991 ? symtab_to_filename_for_display
8992 (symbol_symtab (sym))
4262abfb 8993 : objfile_name (cu->objfile)),
a766d390
DE
8994 this_package_name, package_name);
8995 xfree (this_package_name);
8996 }
8997 }
8998 }
8999 }
9000
9001 if (package_name != NULL)
9002 {
9003 struct objfile *objfile = cu->objfile;
34a68019 9004 const char *saved_package_name
224c3ddb
SM
9005 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9006 package_name,
9007 strlen (package_name));
19f392bc
UW
9008 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9009 saved_package_name);
a766d390
DE
9010 struct symbol *sym;
9011
9012 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9013
e623cf5d 9014 sym = allocate_symbol (objfile);
f85f34ed 9015 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
9016 SYMBOL_SET_NAMES (sym, saved_package_name,
9017 strlen (saved_package_name), 0, objfile);
a766d390
DE
9018 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9019 e.g., "main" finds the "main" module and not C's main(). */
9020 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9021 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9022 SYMBOL_TYPE (sym) = type;
9023
9024 add_symbol_to_list (sym, &global_symbols);
9025
9026 xfree (package_name);
9027 }
9028}
9029
95554aad
TT
9030/* Return the symtab for PER_CU. This works properly regardless of
9031 whether we're using the index or psymtabs. */
9032
43f3e411
DE
9033static struct compunit_symtab *
9034get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad
TT
9035{
9036 return (dwarf2_per_objfile->using_index
43f3e411
DE
9037 ? per_cu->v.quick->compunit_symtab
9038 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
9039}
9040
9041/* A helper function for computing the list of all symbol tables
9042 included by PER_CU. */
9043
9044static void
43f3e411 9045recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 9046 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9047 struct dwarf2_per_cu_data *per_cu,
43f3e411 9048 struct compunit_symtab *immediate_parent)
95554aad
TT
9049{
9050 void **slot;
9051 int ix;
43f3e411 9052 struct compunit_symtab *cust;
95554aad
TT
9053 struct dwarf2_per_cu_data *iter;
9054
9055 slot = htab_find_slot (all_children, per_cu, INSERT);
9056 if (*slot != NULL)
9057 {
9058 /* This inclusion and its children have been processed. */
9059 return;
9060 }
9061
9062 *slot = per_cu;
9063 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9064 cust = get_compunit_symtab (per_cu);
9065 if (cust != NULL)
ec94af83
DE
9066 {
9067 /* If this is a type unit only add its symbol table if we haven't
9068 seen it yet (type unit per_cu's can share symtabs). */
9069 if (per_cu->is_debug_types)
9070 {
43f3e411 9071 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9072 if (*slot == NULL)
9073 {
43f3e411
DE
9074 *slot = cust;
9075 VEC_safe_push (compunit_symtab_ptr, *result, cust);
9076 if (cust->user == NULL)
9077 cust->user = immediate_parent;
ec94af83
DE
9078 }
9079 }
9080 else
f9125b6c 9081 {
43f3e411
DE
9082 VEC_safe_push (compunit_symtab_ptr, *result, cust);
9083 if (cust->user == NULL)
9084 cust->user = immediate_parent;
f9125b6c 9085 }
ec94af83 9086 }
95554aad
TT
9087
9088 for (ix = 0;
796a7ff8 9089 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 9090 ++ix)
ec94af83
DE
9091 {
9092 recursively_compute_inclusions (result, all_children,
43f3e411 9093 all_type_symtabs, iter, cust);
ec94af83 9094 }
95554aad
TT
9095}
9096
43f3e411 9097/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9098 PER_CU. */
9099
9100static void
43f3e411 9101compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9102{
f4dc4d17
DE
9103 gdb_assert (! per_cu->is_debug_types);
9104
796a7ff8 9105 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
9106 {
9107 int ix, len;
ec94af83 9108 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
9109 struct compunit_symtab *compunit_symtab_iter;
9110 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 9111 htab_t all_children, all_type_symtabs;
43f3e411 9112 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9113
9114 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9115 if (cust == NULL)
95554aad
TT
9116 return;
9117
9118 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9119 NULL, xcalloc, xfree);
ec94af83
DE
9120 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9121 NULL, xcalloc, xfree);
95554aad
TT
9122
9123 for (ix = 0;
796a7ff8 9124 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 9125 ix, per_cu_iter);
95554aad 9126 ++ix)
ec94af83
DE
9127 {
9128 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 9129 all_type_symtabs, per_cu_iter,
43f3e411 9130 cust);
ec94af83 9131 }
95554aad 9132
ec94af83 9133 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
9134 len = VEC_length (compunit_symtab_ptr, result_symtabs);
9135 cust->includes
8d749320
SM
9136 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
9137 struct compunit_symtab *, len + 1);
95554aad 9138 for (ix = 0;
43f3e411
DE
9139 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
9140 compunit_symtab_iter);
95554aad 9141 ++ix)
43f3e411
DE
9142 cust->includes[ix] = compunit_symtab_iter;
9143 cust->includes[len] = NULL;
95554aad 9144
43f3e411 9145 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 9146 htab_delete (all_children);
ec94af83 9147 htab_delete (all_type_symtabs);
95554aad
TT
9148 }
9149}
9150
9151/* Compute the 'includes' field for the symtabs of all the CUs we just
9152 read. */
9153
9154static void
9155process_cu_includes (void)
9156{
9157 int ix;
9158 struct dwarf2_per_cu_data *iter;
9159
9160 for (ix = 0;
9161 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
9162 ix, iter);
9163 ++ix)
f4dc4d17
DE
9164 {
9165 if (! iter->is_debug_types)
43f3e411 9166 compute_compunit_symtab_includes (iter);
f4dc4d17 9167 }
95554aad
TT
9168
9169 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
9170}
9171
9cdd5dbd 9172/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9173 already been loaded into memory. */
9174
9175static void
95554aad
TT
9176process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9177 enum language pretend_language)
10b3939b 9178{
10b3939b 9179 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 9180 struct objfile *objfile = per_cu->objfile;
3e29f34a 9181 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 9182 CORE_ADDR lowpc, highpc;
43f3e411 9183 struct compunit_symtab *cust;
33c7c59d 9184 struct cleanup *delayed_list_cleanup;
10b3939b 9185 CORE_ADDR baseaddr;
4359dff1 9186 struct block *static_block;
3e29f34a 9187 CORE_ADDR addr;
10b3939b
DJ
9188
9189 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9190
10b3939b 9191 buildsym_init ();
33c7c59d 9192 scoped_free_pendings free_pending;
3da10d80 9193 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
9194
9195 cu->list_in_scope = &file_symbols;
c906108c 9196
95554aad
TT
9197 cu->language = pretend_language;
9198 cu->language_defn = language_def (cu->language);
9199
c906108c 9200 /* Do line number decoding in read_file_scope () */
10b3939b 9201 process_die (cu->dies, cu);
c906108c 9202
a766d390
DE
9203 /* For now fudge the Go package. */
9204 if (cu->language == language_go)
9205 fixup_go_packaging (cu);
9206
3da10d80
KS
9207 /* Now that we have processed all the DIEs in the CU, all the types
9208 should be complete, and it should now be safe to compute all of the
9209 physnames. */
9210 compute_delayed_physnames (cu);
9211 do_cleanups (delayed_list_cleanup);
9212
fae299cd
DC
9213 /* Some compilers don't define a DW_AT_high_pc attribute for the
9214 compilation unit. If the DW_AT_high_pc is missing, synthesize
9215 it, by scanning the DIE's below the compilation unit. */
10b3939b 9216 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9217
3e29f34a
MR
9218 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9219 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9220
9221 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9222 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9223 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9224 addrmap to help ensure it has an accurate map of pc values belonging to
9225 this comp unit. */
9226 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9227
43f3e411
DE
9228 cust = end_symtab_from_static_block (static_block,
9229 SECT_OFF_TEXT (objfile), 0);
c906108c 9230
43f3e411 9231 if (cust != NULL)
c906108c 9232 {
df15bd07 9233 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9234
8be455d7
JK
9235 /* Set symtab language to language from DW_AT_language. If the
9236 compilation is from a C file generated by language preprocessors, do
9237 not set the language if it was already deduced by start_subfile. */
43f3e411 9238 if (!(cu->language == language_c
40e3ad0e 9239 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9240 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9241
9242 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9243 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9244 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9245 there were bugs in prologue debug info, fixed later in GCC-4.5
9246 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9247
9248 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9249 needed, it would be wrong due to missing DW_AT_producer there.
9250
9251 Still one can confuse GDB by using non-standard GCC compilation
9252 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9253 */
ab260dad 9254 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9255 cust->locations_valid = 1;
e0d00bc7
JK
9256
9257 if (gcc_4_minor >= 5)
43f3e411 9258 cust->epilogue_unwind_valid = 1;
96408a79 9259
43f3e411 9260 cust->call_site_htab = cu->call_site_htab;
c906108c 9261 }
9291a0cd
TT
9262
9263 if (dwarf2_per_objfile->using_index)
43f3e411 9264 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
9265 else
9266 {
9267 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 9268 pst->compunit_symtab = cust;
9291a0cd
TT
9269 pst->readin = 1;
9270 }
c906108c 9271
95554aad
TT
9272 /* Push it for inclusion processing later. */
9273 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
f4dc4d17 9274}
45cfd468 9275
f4dc4d17
DE
9276/* Generate full symbol information for type unit PER_CU, whose DIEs have
9277 already been loaded into memory. */
9278
9279static void
9280process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9281 enum language pretend_language)
9282{
9283 struct dwarf2_cu *cu = per_cu->cu;
9284 struct objfile *objfile = per_cu->objfile;
43f3e411 9285 struct compunit_symtab *cust;
33c7c59d 9286 struct cleanup *delayed_list_cleanup;
0186c6a7
DE
9287 struct signatured_type *sig_type;
9288
9289 gdb_assert (per_cu->is_debug_types);
9290 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
9291
9292 buildsym_init ();
33c7c59d 9293 scoped_free_pendings free_pending;
f4dc4d17
DE
9294 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
9295
9296 cu->list_in_scope = &file_symbols;
9297
9298 cu->language = pretend_language;
9299 cu->language_defn = language_def (cu->language);
9300
9301 /* The symbol tables are set up in read_type_unit_scope. */
9302 process_die (cu->dies, cu);
9303
9304 /* For now fudge the Go package. */
9305 if (cu->language == language_go)
9306 fixup_go_packaging (cu);
9307
9308 /* Now that we have processed all the DIEs in the CU, all the types
9309 should be complete, and it should now be safe to compute all of the
9310 physnames. */
9311 compute_delayed_physnames (cu);
9312 do_cleanups (delayed_list_cleanup);
9313
9314 /* TUs share symbol tables.
9315 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9316 of it with end_expandable_symtab. Otherwise, complete the addition of
9317 this TU's symbols to the existing symtab. */
43f3e411 9318 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9319 {
43f3e411
DE
9320 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9321 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9322
43f3e411 9323 if (cust != NULL)
f4dc4d17
DE
9324 {
9325 /* Set symtab language to language from DW_AT_language. If the
9326 compilation is from a C file generated by language preprocessors,
9327 do not set the language if it was already deduced by
9328 start_subfile. */
43f3e411
DE
9329 if (!(cu->language == language_c
9330 && COMPUNIT_FILETABS (cust)->language != language_c))
9331 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9332 }
9333 }
9334 else
9335 {
0ab9ce85 9336 augment_type_symtab ();
43f3e411 9337 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9338 }
9339
9340 if (dwarf2_per_objfile->using_index)
43f3e411 9341 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
9342 else
9343 {
9344 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 9345 pst->compunit_symtab = cust;
f4dc4d17 9346 pst->readin = 1;
45cfd468 9347 }
c906108c
SS
9348}
9349
95554aad
TT
9350/* Process an imported unit DIE. */
9351
9352static void
9353process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9354{
9355 struct attribute *attr;
9356
f4dc4d17
DE
9357 /* For now we don't handle imported units in type units. */
9358 if (cu->per_cu->is_debug_types)
9359 {
9360 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9361 " supported in type units [in module %s]"),
4262abfb 9362 objfile_name (cu->objfile));
f4dc4d17
DE
9363 }
9364
95554aad
TT
9365 attr = dwarf2_attr (die, DW_AT_import, cu);
9366 if (attr != NULL)
9367 {
9c541725
PA
9368 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9369 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9370 dwarf2_per_cu_data *per_cu
9371 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
95554aad 9372
69d751e3 9373 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
9374 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9375 load_full_comp_unit (per_cu, cu->language);
9376
796a7ff8 9377 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
9378 per_cu);
9379 }
9380}
9381
4c8aa72d
PA
9382/* RAII object that represents a process_die scope: i.e.,
9383 starts/finishes processing a DIE. */
9384class process_die_scope
adde2bff 9385{
4c8aa72d
PA
9386public:
9387 process_die_scope (die_info *die, dwarf2_cu *cu)
9388 : m_die (die), m_cu (cu)
9389 {
9390 /* We should only be processing DIEs not already in process. */
9391 gdb_assert (!m_die->in_process);
9392 m_die->in_process = true;
9393 }
8c3cb9fa 9394
4c8aa72d
PA
9395 ~process_die_scope ()
9396 {
9397 m_die->in_process = false;
9398
9399 /* If we're done processing the DIE for the CU that owns the line
9400 header, we don't need the line header anymore. */
9401 if (m_cu->line_header_die_owner == m_die)
9402 {
9403 delete m_cu->line_header;
9404 m_cu->line_header = NULL;
9405 m_cu->line_header_die_owner = NULL;
9406 }
9407 }
9408
9409private:
9410 die_info *m_die;
9411 dwarf2_cu *m_cu;
9412};
adde2bff 9413
c906108c
SS
9414/* Process a die and its children. */
9415
9416static void
e7c27a73 9417process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9418{
4c8aa72d 9419 process_die_scope scope (die, cu);
adde2bff 9420
c906108c
SS
9421 switch (die->tag)
9422 {
9423 case DW_TAG_padding:
9424 break;
9425 case DW_TAG_compile_unit:
95554aad 9426 case DW_TAG_partial_unit:
e7c27a73 9427 read_file_scope (die, cu);
c906108c 9428 break;
348e048f
DE
9429 case DW_TAG_type_unit:
9430 read_type_unit_scope (die, cu);
9431 break;
c906108c 9432 case DW_TAG_subprogram:
c906108c 9433 case DW_TAG_inlined_subroutine:
edb3359d 9434 read_func_scope (die, cu);
c906108c
SS
9435 break;
9436 case DW_TAG_lexical_block:
14898363
L
9437 case DW_TAG_try_block:
9438 case DW_TAG_catch_block:
e7c27a73 9439 read_lexical_block_scope (die, cu);
c906108c 9440 break;
216f72a1 9441 case DW_TAG_call_site:
96408a79
SA
9442 case DW_TAG_GNU_call_site:
9443 read_call_site_scope (die, cu);
9444 break;
c906108c 9445 case DW_TAG_class_type:
680b30c7 9446 case DW_TAG_interface_type:
c906108c
SS
9447 case DW_TAG_structure_type:
9448 case DW_TAG_union_type:
134d01f1 9449 process_structure_scope (die, cu);
c906108c
SS
9450 break;
9451 case DW_TAG_enumeration_type:
134d01f1 9452 process_enumeration_scope (die, cu);
c906108c 9453 break;
134d01f1 9454
f792889a
DJ
9455 /* These dies have a type, but processing them does not create
9456 a symbol or recurse to process the children. Therefore we can
9457 read them on-demand through read_type_die. */
c906108c 9458 case DW_TAG_subroutine_type:
72019c9c 9459 case DW_TAG_set_type:
c906108c 9460 case DW_TAG_array_type:
c906108c 9461 case DW_TAG_pointer_type:
c906108c 9462 case DW_TAG_ptr_to_member_type:
c906108c 9463 case DW_TAG_reference_type:
4297a3f0 9464 case DW_TAG_rvalue_reference_type:
c906108c 9465 case DW_TAG_string_type:
c906108c 9466 break;
134d01f1 9467
c906108c 9468 case DW_TAG_base_type:
a02abb62 9469 case DW_TAG_subrange_type:
cb249c71 9470 case DW_TAG_typedef:
134d01f1
DJ
9471 /* Add a typedef symbol for the type definition, if it has a
9472 DW_AT_name. */
f792889a 9473 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9474 break;
c906108c 9475 case DW_TAG_common_block:
e7c27a73 9476 read_common_block (die, cu);
c906108c
SS
9477 break;
9478 case DW_TAG_common_inclusion:
9479 break;
d9fa45fe 9480 case DW_TAG_namespace:
4d4ec4e5 9481 cu->processing_has_namespace_info = 1;
e7c27a73 9482 read_namespace (die, cu);
d9fa45fe 9483 break;
5d7cb8df 9484 case DW_TAG_module:
4d4ec4e5 9485 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
9486 read_module (die, cu);
9487 break;
d9fa45fe 9488 case DW_TAG_imported_declaration:
74921315
KS
9489 cu->processing_has_namespace_info = 1;
9490 if (read_namespace_alias (die, cu))
9491 break;
9492 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 9493 case DW_TAG_imported_module:
4d4ec4e5 9494 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
9495 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9496 || cu->language != language_fortran))
9497 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
9498 dwarf_tag_name (die->tag));
9499 read_import_statement (die, cu);
d9fa45fe 9500 break;
95554aad
TT
9501
9502 case DW_TAG_imported_unit:
9503 process_imported_unit_die (die, cu);
9504 break;
9505
71a3c369
TT
9506 case DW_TAG_variable:
9507 read_variable (die, cu);
9508 break;
9509
c906108c 9510 default:
e7c27a73 9511 new_symbol (die, NULL, cu);
c906108c
SS
9512 break;
9513 }
9514}
ca69b9e6
DE
9515\f
9516/* DWARF name computation. */
c906108c 9517
94af9270
KS
9518/* A helper function for dwarf2_compute_name which determines whether DIE
9519 needs to have the name of the scope prepended to the name listed in the
9520 die. */
9521
9522static int
9523die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9524{
1c809c68
TT
9525 struct attribute *attr;
9526
94af9270
KS
9527 switch (die->tag)
9528 {
9529 case DW_TAG_namespace:
9530 case DW_TAG_typedef:
9531 case DW_TAG_class_type:
9532 case DW_TAG_interface_type:
9533 case DW_TAG_structure_type:
9534 case DW_TAG_union_type:
9535 case DW_TAG_enumeration_type:
9536 case DW_TAG_enumerator:
9537 case DW_TAG_subprogram:
08a76f8a 9538 case DW_TAG_inlined_subroutine:
94af9270 9539 case DW_TAG_member:
74921315 9540 case DW_TAG_imported_declaration:
94af9270
KS
9541 return 1;
9542
9543 case DW_TAG_variable:
c2b0a229 9544 case DW_TAG_constant:
94af9270
KS
9545 /* We only need to prefix "globally" visible variables. These include
9546 any variable marked with DW_AT_external or any variable that
9547 lives in a namespace. [Variables in anonymous namespaces
9548 require prefixing, but they are not DW_AT_external.] */
9549
9550 if (dwarf2_attr (die, DW_AT_specification, cu))
9551 {
9552 struct dwarf2_cu *spec_cu = cu;
9a619af0 9553
94af9270
KS
9554 return die_needs_namespace (die_specification (die, &spec_cu),
9555 spec_cu);
9556 }
9557
1c809c68 9558 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9559 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9560 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9561 return 0;
9562 /* A variable in a lexical block of some kind does not need a
9563 namespace, even though in C++ such variables may be external
9564 and have a mangled name. */
9565 if (die->parent->tag == DW_TAG_lexical_block
9566 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9567 || die->parent->tag == DW_TAG_catch_block
9568 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9569 return 0;
9570 return 1;
94af9270
KS
9571
9572 default:
9573 return 0;
9574 }
9575}
9576
73b9be8b
KS
9577/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9578 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9579 defined for the given DIE. */
9580
9581static struct attribute *
9582dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9583{
9584 struct attribute *attr;
9585
9586 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9587 if (attr == NULL)
9588 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9589
9590 return attr;
9591}
9592
9593/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9594 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9595 defined for the given DIE. */
9596
9597static const char *
9598dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9599{
9600 const char *linkage_name;
9601
9602 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9603 if (linkage_name == NULL)
9604 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9605
9606 return linkage_name;
9607}
9608
94af9270 9609/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 9610 compute the physname for the object, which include a method's:
9c37b5ae 9611 - formal parameters (C++),
a766d390 9612 - receiver type (Go),
a766d390
DE
9613
9614 The term "physname" is a bit confusing.
9615 For C++, for example, it is the demangled name.
9616 For Go, for example, it's the mangled name.
94af9270 9617
af6b7be1
JB
9618 For Ada, return the DIE's linkage name rather than the fully qualified
9619 name. PHYSNAME is ignored..
9620
94af9270
KS
9621 The result is allocated on the objfile_obstack and canonicalized. */
9622
9623static const char *
15d034d0
TT
9624dwarf2_compute_name (const char *name,
9625 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
9626 int physname)
9627{
bb5ed363
DE
9628 struct objfile *objfile = cu->objfile;
9629
94af9270
KS
9630 if (name == NULL)
9631 name = dwarf2_name (die, cu);
9632
2ee7123e
DE
9633 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9634 but otherwise compute it by typename_concat inside GDB.
9635 FIXME: Actually this is not really true, or at least not always true.
9636 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
9637 Fortran names because there is no mangling standard. So new_symbol_full
9638 will set the demangled name to the result of dwarf2_full_name, and it is
9639 the demangled name that GDB uses if it exists. */
f55ee35c
JK
9640 if (cu->language == language_ada
9641 || (cu->language == language_fortran && physname))
9642 {
9643 /* For Ada unit, we prefer the linkage name over the name, as
9644 the former contains the exported name, which the user expects
9645 to be able to reference. Ideally, we want the user to be able
9646 to reference this entity using either natural or linkage name,
9647 but we haven't started looking at this enhancement yet. */
73b9be8b 9648 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 9649
2ee7123e
DE
9650 if (linkage_name != NULL)
9651 return linkage_name;
f55ee35c
JK
9652 }
9653
94af9270
KS
9654 /* These are the only languages we know how to qualify names in. */
9655 if (name != NULL
9c37b5ae 9656 && (cu->language == language_cplus
c44af4eb
TT
9657 || cu->language == language_fortran || cu->language == language_d
9658 || cu->language == language_rust))
94af9270
KS
9659 {
9660 if (die_needs_namespace (die, cu))
9661 {
0d5cff50 9662 const char *prefix;
34a68019 9663 const char *canonical_name = NULL;
94af9270 9664
d7e74731
PA
9665 string_file buf;
9666
94af9270 9667 prefix = determine_prefix (die, cu);
94af9270
KS
9668 if (*prefix != '\0')
9669 {
f55ee35c
JK
9670 char *prefixed_name = typename_concat (NULL, prefix, name,
9671 physname, cu);
9a619af0 9672
d7e74731 9673 buf.puts (prefixed_name);
94af9270
KS
9674 xfree (prefixed_name);
9675 }
9676 else
d7e74731 9677 buf.puts (name);
94af9270 9678
98bfdba5
PA
9679 /* Template parameters may be specified in the DIE's DW_AT_name, or
9680 as children with DW_TAG_template_type_param or
9681 DW_TAG_value_type_param. If the latter, add them to the name
9682 here. If the name already has template parameters, then
9683 skip this step; some versions of GCC emit both, and
9684 it is more efficient to use the pre-computed name.
9685
9686 Something to keep in mind about this process: it is very
9687 unlikely, or in some cases downright impossible, to produce
9688 something that will match the mangled name of a function.
9689 If the definition of the function has the same debug info,
9690 we should be able to match up with it anyway. But fallbacks
9691 using the minimal symbol, for instance to find a method
9692 implemented in a stripped copy of libstdc++, will not work.
9693 If we do not have debug info for the definition, we will have to
9694 match them up some other way.
9695
9696 When we do name matching there is a related problem with function
9697 templates; two instantiated function templates are allowed to
9698 differ only by their return types, which we do not add here. */
9699
9700 if (cu->language == language_cplus && strchr (name, '<') == NULL)
9701 {
9702 struct attribute *attr;
9703 struct die_info *child;
9704 int first = 1;
9705
9706 die->building_fullname = 1;
9707
9708 for (child = die->child; child != NULL; child = child->sibling)
9709 {
9710 struct type *type;
12df843f 9711 LONGEST value;
d521ce57 9712 const gdb_byte *bytes;
98bfdba5
PA
9713 struct dwarf2_locexpr_baton *baton;
9714 struct value *v;
9715
9716 if (child->tag != DW_TAG_template_type_param
9717 && child->tag != DW_TAG_template_value_param)
9718 continue;
9719
9720 if (first)
9721 {
d7e74731 9722 buf.puts ("<");
98bfdba5
PA
9723 first = 0;
9724 }
9725 else
d7e74731 9726 buf.puts (", ");
98bfdba5
PA
9727
9728 attr = dwarf2_attr (child, DW_AT_type, cu);
9729 if (attr == NULL)
9730 {
9731 complaint (&symfile_complaints,
9732 _("template parameter missing DW_AT_type"));
d7e74731 9733 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
9734 continue;
9735 }
9736 type = die_type (child, cu);
9737
9738 if (child->tag == DW_TAG_template_type_param)
9739 {
d7e74731 9740 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
9741 continue;
9742 }
9743
9744 attr = dwarf2_attr (child, DW_AT_const_value, cu);
9745 if (attr == NULL)
9746 {
9747 complaint (&symfile_complaints,
3e43a32a
MS
9748 _("template parameter missing "
9749 "DW_AT_const_value"));
d7e74731 9750 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
9751 continue;
9752 }
9753
9754 dwarf2_const_value_attr (attr, type, name,
9755 &cu->comp_unit_obstack, cu,
9756 &value, &bytes, &baton);
9757
9758 if (TYPE_NOSIGN (type))
9759 /* GDB prints characters as NUMBER 'CHAR'. If that's
9760 changed, this can use value_print instead. */
d7e74731 9761 c_printchar (value, type, &buf);
98bfdba5
PA
9762 else
9763 {
9764 struct value_print_options opts;
9765
9766 if (baton != NULL)
9767 v = dwarf2_evaluate_loc_desc (type, NULL,
9768 baton->data,
9769 baton->size,
9770 baton->per_cu);
9771 else if (bytes != NULL)
9772 {
9773 v = allocate_value (type);
9774 memcpy (value_contents_writeable (v), bytes,
9775 TYPE_LENGTH (type));
9776 }
9777 else
9778 v = value_from_longest (type, value);
9779
3e43a32a
MS
9780 /* Specify decimal so that we do not depend on
9781 the radix. */
98bfdba5
PA
9782 get_formatted_print_options (&opts, 'd');
9783 opts.raw = 1;
d7e74731 9784 value_print (v, &buf, &opts);
98bfdba5
PA
9785 release_value (v);
9786 value_free (v);
9787 }
9788 }
9789
9790 die->building_fullname = 0;
9791
9792 if (!first)
9793 {
9794 /* Close the argument list, with a space if necessary
9795 (nested templates). */
d7e74731
PA
9796 if (!buf.empty () && buf.string ().back () == '>')
9797 buf.puts (" >");
98bfdba5 9798 else
d7e74731 9799 buf.puts (">");
98bfdba5
PA
9800 }
9801 }
9802
9c37b5ae 9803 /* For C++ methods, append formal parameter type
94af9270 9804 information, if PHYSNAME. */
6e70227d 9805
94af9270 9806 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 9807 && cu->language == language_cplus)
94af9270
KS
9808 {
9809 struct type *type = read_type_die (die, cu);
9810
d7e74731 9811 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 9812 &type_print_raw_options);
94af9270 9813
9c37b5ae 9814 if (cu->language == language_cplus)
94af9270 9815 {
60430eff
DJ
9816 /* Assume that an artificial first parameter is
9817 "this", but do not crash if it is not. RealView
9818 marks unnamed (and thus unused) parameters as
9819 artificial; there is no way to differentiate
9820 the two cases. */
94af9270
KS
9821 if (TYPE_NFIELDS (type) > 0
9822 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 9823 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
9824 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
9825 0))))
d7e74731 9826 buf.puts (" const");
94af9270
KS
9827 }
9828 }
9829
d7e74731 9830 const std::string &intermediate_name = buf.string ();
94af9270
KS
9831
9832 if (cu->language == language_cplus)
34a68019 9833 canonical_name
322a8516 9834 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
9835 &objfile->per_bfd->storage_obstack);
9836
9837 /* If we only computed INTERMEDIATE_NAME, or if
9838 INTERMEDIATE_NAME is already canonical, then we need to
9839 copy it to the appropriate obstack. */
322a8516 9840 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
9841 name = ((const char *)
9842 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
9843 intermediate_name.c_str (),
9844 intermediate_name.length ()));
34a68019
TT
9845 else
9846 name = canonical_name;
94af9270
KS
9847 }
9848 }
9849
9850 return name;
9851}
9852
0114d602
DJ
9853/* Return the fully qualified name of DIE, based on its DW_AT_name.
9854 If scope qualifiers are appropriate they will be added. The result
34a68019 9855 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
9856 not have a name. NAME may either be from a previous call to
9857 dwarf2_name or NULL.
9858
9c37b5ae 9859 The output string will be canonicalized (if C++). */
0114d602
DJ
9860
9861static const char *
15d034d0 9862dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 9863{
94af9270
KS
9864 return dwarf2_compute_name (name, die, cu, 0);
9865}
0114d602 9866
94af9270
KS
9867/* Construct a physname for the given DIE in CU. NAME may either be
9868 from a previous call to dwarf2_name or NULL. The result will be
9869 allocated on the objfile_objstack or NULL if the DIE does not have a
9870 name.
0114d602 9871
9c37b5ae 9872 The output string will be canonicalized (if C++). */
0114d602 9873
94af9270 9874static const char *
15d034d0 9875dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 9876{
bb5ed363 9877 struct objfile *objfile = cu->objfile;
900e11f9 9878 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
9879 int need_copy = 1;
9880
9881 /* In this case dwarf2_compute_name is just a shortcut not building anything
9882 on its own. */
9883 if (!die_needs_namespace (die, cu))
9884 return dwarf2_compute_name (name, die, cu, 1);
9885
73b9be8b 9886 mangled = dw2_linkage_name (die, cu);
900e11f9 9887
e98c9e7c
TT
9888 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9889 See https://github.com/rust-lang/rust/issues/32925. */
9890 if (cu->language == language_rust && mangled != NULL
9891 && strchr (mangled, '{') != NULL)
9892 mangled = NULL;
9893
900e11f9
JK
9894 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9895 has computed. */
791afaa2 9896 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 9897 if (mangled != NULL)
900e11f9 9898 {
900e11f9
JK
9899 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
9900 type. It is easier for GDB users to search for such functions as
9901 `name(params)' than `long name(params)'. In such case the minimal
9902 symbol names do not match the full symbol names but for template
9903 functions there is never a need to look up their definition from their
9904 declaration so the only disadvantage remains the minimal symbol
9905 variant `long name(params)' does not have the proper inferior type.
9906 */
9907
a766d390
DE
9908 if (cu->language == language_go)
9909 {
9910 /* This is a lie, but we already lie to the caller new_symbol_full.
9911 new_symbol_full assumes we return the mangled name.
9912 This just undoes that lie until things are cleaned up. */
a766d390
DE
9913 }
9914 else
9915 {
791afaa2
TT
9916 demangled.reset (gdb_demangle (mangled,
9917 (DMGL_PARAMS | DMGL_ANSI
9918 | DMGL_RET_DROP)));
a766d390 9919 }
900e11f9 9920 if (demangled)
791afaa2 9921 canon = demangled.get ();
900e11f9
JK
9922 else
9923 {
9924 canon = mangled;
9925 need_copy = 0;
9926 }
9927 }
9928
9929 if (canon == NULL || check_physname)
9930 {
9931 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9932
9933 if (canon != NULL && strcmp (physname, canon) != 0)
9934 {
9935 /* It may not mean a bug in GDB. The compiler could also
9936 compute DW_AT_linkage_name incorrectly. But in such case
9937 GDB would need to be bug-to-bug compatible. */
9938
9939 complaint (&symfile_complaints,
9940 _("Computed physname <%s> does not match demangled <%s> "
9941 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9c541725 9942 physname, canon, mangled, to_underlying (die->sect_off),
4262abfb 9943 objfile_name (objfile));
900e11f9
JK
9944
9945 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9946 is available here - over computed PHYSNAME. It is safer
9947 against both buggy GDB and buggy compilers. */
9948
9949 retval = canon;
9950 }
9951 else
9952 {
9953 retval = physname;
9954 need_copy = 0;
9955 }
9956 }
9957 else
9958 retval = canon;
9959
9960 if (need_copy)
224c3ddb
SM
9961 retval = ((const char *)
9962 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9963 retval, strlen (retval)));
900e11f9 9964
900e11f9 9965 return retval;
0114d602
DJ
9966}
9967
74921315
KS
9968/* Inspect DIE in CU for a namespace alias. If one exists, record
9969 a new symbol for it.
9970
9971 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9972
9973static int
9974read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
9975{
9976 struct attribute *attr;
9977
9978 /* If the die does not have a name, this is not a namespace
9979 alias. */
9980 attr = dwarf2_attr (die, DW_AT_name, cu);
9981 if (attr != NULL)
9982 {
9983 int num;
9984 struct die_info *d = die;
9985 struct dwarf2_cu *imported_cu = cu;
9986
9987 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9988 keep inspecting DIEs until we hit the underlying import. */
9989#define MAX_NESTED_IMPORTED_DECLARATIONS 100
9990 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9991 {
9992 attr = dwarf2_attr (d, DW_AT_import, cu);
9993 if (attr == NULL)
9994 break;
9995
9996 d = follow_die_ref (d, attr, &imported_cu);
9997 if (d->tag != DW_TAG_imported_declaration)
9998 break;
9999 }
10000
10001 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10002 {
10003 complaint (&symfile_complaints,
10004 _("DIE at 0x%x has too many recursively imported "
9c541725 10005 "declarations"), to_underlying (d->sect_off));
74921315
KS
10006 return 0;
10007 }
10008
10009 if (attr != NULL)
10010 {
10011 struct type *type;
9c541725 10012 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 10013
9c541725 10014 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
10015 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10016 {
10017 /* This declaration is a global namespace alias. Add
10018 a symbol for it whose type is the aliased namespace. */
10019 new_symbol (die, type, cu);
10020 return 1;
10021 }
10022 }
10023 }
10024
10025 return 0;
10026}
10027
22cee43f
PMR
10028/* Return the using directives repository (global or local?) to use in the
10029 current context for LANGUAGE.
10030
10031 For Ada, imported declarations can materialize renamings, which *may* be
10032 global. However it is impossible (for now?) in DWARF to distinguish
10033 "external" imported declarations and "static" ones. As all imported
10034 declarations seem to be static in all other languages, make them all CU-wide
10035 global only in Ada. */
10036
10037static struct using_direct **
10038using_directives (enum language language)
10039{
10040 if (language == language_ada && context_stack_depth == 0)
10041 return &global_using_directives;
10042 else
10043 return &local_using_directives;
10044}
10045
27aa8d6a
SW
10046/* Read the import statement specified by the given die and record it. */
10047
10048static void
10049read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10050{
bb5ed363 10051 struct objfile *objfile = cu->objfile;
27aa8d6a 10052 struct attribute *import_attr;
32019081 10053 struct die_info *imported_die, *child_die;
de4affc9 10054 struct dwarf2_cu *imported_cu;
27aa8d6a 10055 const char *imported_name;
794684b6 10056 const char *imported_name_prefix;
13387711
SW
10057 const char *canonical_name;
10058 const char *import_alias;
10059 const char *imported_declaration = NULL;
794684b6 10060 const char *import_prefix;
eb1e02fd 10061 std::vector<const char *> excludes;
13387711 10062
27aa8d6a
SW
10063 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10064 if (import_attr == NULL)
10065 {
10066 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
10067 dwarf_tag_name (die->tag));
10068 return;
10069 }
10070
de4affc9
CC
10071 imported_cu = cu;
10072 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10073 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10074 if (imported_name == NULL)
10075 {
10076 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10077
10078 The import in the following code:
10079 namespace A
10080 {
10081 typedef int B;
10082 }
10083
10084 int main ()
10085 {
10086 using A::B;
10087 B b;
10088 return b;
10089 }
10090
10091 ...
10092 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10093 <52> DW_AT_decl_file : 1
10094 <53> DW_AT_decl_line : 6
10095 <54> DW_AT_import : <0x75>
10096 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10097 <59> DW_AT_name : B
10098 <5b> DW_AT_decl_file : 1
10099 <5c> DW_AT_decl_line : 2
10100 <5d> DW_AT_type : <0x6e>
10101 ...
10102 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10103 <76> DW_AT_byte_size : 4
10104 <77> DW_AT_encoding : 5 (signed)
10105
10106 imports the wrong die ( 0x75 instead of 0x58 ).
10107 This case will be ignored until the gcc bug is fixed. */
10108 return;
10109 }
10110
82856980
SW
10111 /* Figure out the local name after import. */
10112 import_alias = dwarf2_name (die, cu);
27aa8d6a 10113
794684b6
SW
10114 /* Figure out where the statement is being imported to. */
10115 import_prefix = determine_prefix (die, cu);
10116
10117 /* Figure out what the scope of the imported die is and prepend it
10118 to the name of the imported die. */
de4affc9 10119 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10120
f55ee35c
JK
10121 if (imported_die->tag != DW_TAG_namespace
10122 && imported_die->tag != DW_TAG_module)
794684b6 10123 {
13387711
SW
10124 imported_declaration = imported_name;
10125 canonical_name = imported_name_prefix;
794684b6 10126 }
13387711 10127 else if (strlen (imported_name_prefix) > 0)
12aaed36 10128 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10129 imported_name_prefix,
10130 (cu->language == language_d ? "." : "::"),
10131 imported_name, (char *) NULL);
13387711
SW
10132 else
10133 canonical_name = imported_name;
794684b6 10134
32019081
JK
10135 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10136 for (child_die = die->child; child_die && child_die->tag;
10137 child_die = sibling_die (child_die))
10138 {
10139 /* DWARF-4: A Fortran use statement with a “rename list” may be
10140 represented by an imported module entry with an import attribute
10141 referring to the module and owned entries corresponding to those
10142 entities that are renamed as part of being imported. */
10143
10144 if (child_die->tag != DW_TAG_imported_declaration)
10145 {
10146 complaint (&symfile_complaints,
10147 _("child DW_TAG_imported_declaration expected "
10148 "- DIE at 0x%x [in module %s]"),
9c541725 10149 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
10150 continue;
10151 }
10152
10153 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10154 if (import_attr == NULL)
10155 {
10156 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
10157 dwarf_tag_name (child_die->tag));
10158 continue;
10159 }
10160
10161 imported_cu = cu;
10162 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10163 &imported_cu);
10164 imported_name = dwarf2_name (imported_die, imported_cu);
10165 if (imported_name == NULL)
10166 {
10167 complaint (&symfile_complaints,
10168 _("child DW_TAG_imported_declaration has unknown "
10169 "imported name - DIE at 0x%x [in module %s]"),
9c541725 10170 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
10171 continue;
10172 }
10173
eb1e02fd 10174 excludes.push_back (imported_name);
32019081
JK
10175
10176 process_die (child_die, cu);
10177 }
10178
22cee43f
PMR
10179 add_using_directive (using_directives (cu->language),
10180 import_prefix,
10181 canonical_name,
10182 import_alias,
10183 imported_declaration,
10184 excludes,
10185 0,
10186 &objfile->objfile_obstack);
27aa8d6a
SW
10187}
10188
5230b05a
WT
10189/* ICC<14 does not output the required DW_AT_declaration on incomplete
10190 types, but gives them a size of zero. Starting with version 14,
10191 ICC is compatible with GCC. */
10192
10193static int
10194producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10195{
10196 if (!cu->checked_producer)
10197 check_producer (cu);
10198
10199 return cu->producer_is_icc_lt_14;
10200}
10201
1b80a9fa
JK
10202/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10203 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10204 this, it was first present in GCC release 4.3.0. */
10205
10206static int
10207producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10208{
10209 if (!cu->checked_producer)
10210 check_producer (cu);
10211
10212 return cu->producer_is_gcc_lt_4_3;
10213}
10214
d721ba37
PA
10215static file_and_directory
10216find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10217{
d721ba37
PA
10218 file_and_directory res;
10219
9291a0cd
TT
10220 /* Find the filename. Do not use dwarf2_name here, since the filename
10221 is not a source language identifier. */
d721ba37
PA
10222 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10223 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10224
d721ba37
PA
10225 if (res.comp_dir == NULL
10226 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10227 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10228 {
d721ba37
PA
10229 res.comp_dir_storage = ldirname (res.name);
10230 if (!res.comp_dir_storage.empty ())
10231 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10232 }
d721ba37 10233 if (res.comp_dir != NULL)
9291a0cd
TT
10234 {
10235 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10236 directory, get rid of it. */
d721ba37 10237 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10238
d721ba37
PA
10239 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10240 res.comp_dir = cp + 1;
9291a0cd
TT
10241 }
10242
d721ba37
PA
10243 if (res.name == NULL)
10244 res.name = "<unknown>";
10245
10246 return res;
9291a0cd
TT
10247}
10248
f4dc4d17
DE
10249/* Handle DW_AT_stmt_list for a compilation unit.
10250 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10251 COMP_DIR is the compilation directory. LOWPC is passed to
10252 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10253
10254static void
10255handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10256 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10257{
527f3840 10258 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 10259 struct attribute *attr;
527f3840
JK
10260 struct line_header line_header_local;
10261 hashval_t line_header_local_hash;
527f3840
JK
10262 void **slot;
10263 int decode_mapping;
2ab95328 10264
f4dc4d17
DE
10265 gdb_assert (! cu->per_cu->is_debug_types);
10266
2ab95328 10267 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10268 if (attr == NULL)
10269 return;
10270
9c541725 10271 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10272
10273 /* The line header hash table is only created if needed (it exists to
10274 prevent redundant reading of the line table for partial_units).
10275 If we're given a partial_unit, we'll need it. If we're given a
10276 compile_unit, then use the line header hash table if it's already
10277 created, but don't create one just yet. */
10278
10279 if (dwarf2_per_objfile->line_header_hash == NULL
10280 && die->tag == DW_TAG_partial_unit)
2ab95328 10281 {
527f3840
JK
10282 dwarf2_per_objfile->line_header_hash
10283 = htab_create_alloc_ex (127, line_header_hash_voidp,
10284 line_header_eq_voidp,
10285 free_line_header_voidp,
10286 &objfile->objfile_obstack,
10287 hashtab_obstack_allocate,
10288 dummy_obstack_deallocate);
10289 }
2ab95328 10290
9c541725 10291 line_header_local.sect_off = line_offset;
527f3840
JK
10292 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10293 line_header_local_hash = line_header_hash (&line_header_local);
10294 if (dwarf2_per_objfile->line_header_hash != NULL)
10295 {
10296 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
10297 &line_header_local,
10298 line_header_local_hash, NO_INSERT);
10299
10300 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10301 is not present in *SLOT (since if there is something in *SLOT then
10302 it will be for a partial_unit). */
10303 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10304 {
527f3840 10305 gdb_assert (*slot != NULL);
9a3c8263 10306 cu->line_header = (struct line_header *) *slot;
527f3840 10307 return;
dee91e82 10308 }
2ab95328 10309 }
527f3840
JK
10310
10311 /* dwarf_decode_line_header does not yet provide sufficient information.
10312 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10313 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10314 if (lh == NULL)
527f3840 10315 return;
4c8aa72d
PA
10316
10317 cu->line_header = lh.release ();
10318 cu->line_header_die_owner = die;
527f3840
JK
10319
10320 if (dwarf2_per_objfile->line_header_hash == NULL)
10321 slot = NULL;
10322 else
10323 {
10324 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
10325 &line_header_local,
10326 line_header_local_hash, INSERT);
10327 gdb_assert (slot != NULL);
10328 }
10329 if (slot != NULL && *slot == NULL)
10330 {
10331 /* This newly decoded line number information unit will be owned
10332 by line_header_hash hash table. */
10333 *slot = cu->line_header;
4c8aa72d 10334 cu->line_header_die_owner = NULL;
527f3840
JK
10335 }
10336 else
10337 {
10338 /* We cannot free any current entry in (*slot) as that struct line_header
10339 may be already used by multiple CUs. Create only temporary decoded
10340 line_header for this CU - it may happen at most once for each line
10341 number information unit. And if we're not using line_header_hash
10342 then this is what we want as well. */
10343 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10344 }
10345 decode_mapping = (die->tag != DW_TAG_partial_unit);
10346 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10347 decode_mapping);
fff8551c 10348
2ab95328
TT
10349}
10350
95554aad 10351/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10352
c906108c 10353static void
e7c27a73 10354read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10355{
dee91e82 10356 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10357 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 10358 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10359 CORE_ADDR highpc = ((CORE_ADDR) 0);
10360 struct attribute *attr;
c906108c 10361 struct die_info *child_die;
e142c38c 10362 CORE_ADDR baseaddr;
6e70227d 10363
e142c38c 10364 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10365
fae299cd 10366 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10367
10368 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10369 from finish_block. */
2acceee2 10370 if (lowpc == ((CORE_ADDR) -1))
c906108c 10371 lowpc = highpc;
3e29f34a 10372 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10373
d721ba37 10374 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10375
95554aad 10376 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 10377
f4b8a18d
KW
10378 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10379 standardised yet. As a workaround for the language detection we fall
10380 back to the DW_AT_producer string. */
10381 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10382 cu->language = language_opencl;
10383
3019eac3
DE
10384 /* Similar hack for Go. */
10385 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10386 set_cu_language (DW_LANG_Go, cu);
10387
d721ba37 10388 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10389
10390 /* Decode line number information if present. We do this before
10391 processing child DIEs, so that the line header table is available
10392 for DW_AT_decl_file. */
d721ba37 10393 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10394
10395 /* Process all dies in compilation unit. */
10396 if (die->child != NULL)
10397 {
10398 child_die = die->child;
10399 while (child_die && child_die->tag)
10400 {
10401 process_die (child_die, cu);
10402 child_die = sibling_die (child_die);
10403 }
10404 }
10405
10406 /* Decode macro information, if present. Dwarf 2 macro information
10407 refers to information in the line number info statement program
10408 header, so we can only read it if we've read the header
10409 successfully. */
0af92d60
JK
10410 attr = dwarf2_attr (die, DW_AT_macros, cu);
10411 if (attr == NULL)
10412 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10413 if (attr && cu->line_header)
10414 {
10415 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10416 complaint (&symfile_complaints,
0af92d60 10417 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10418
43f3e411 10419 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10420 }
10421 else
10422 {
10423 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10424 if (attr && cu->line_header)
10425 {
10426 unsigned int macro_offset = DW_UNSND (attr);
10427
43f3e411 10428 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10429 }
10430 }
3019eac3
DE
10431}
10432
f4dc4d17
DE
10433/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
10434 Create the set of symtabs used by this TU, or if this TU is sharing
10435 symtabs with another TU and the symtabs have already been created
10436 then restore those symtabs in the line header.
10437 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
10438
10439static void
f4dc4d17 10440setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 10441{
f4dc4d17
DE
10442 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
10443 struct type_unit_group *tu_group;
10444 int first_time;
3019eac3 10445 struct attribute *attr;
9c541725 10446 unsigned int i;
0186c6a7 10447 struct signatured_type *sig_type;
3019eac3 10448
f4dc4d17 10449 gdb_assert (per_cu->is_debug_types);
0186c6a7 10450 sig_type = (struct signatured_type *) per_cu;
3019eac3 10451
f4dc4d17 10452 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 10453
f4dc4d17 10454 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10455 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
10456 if (sig_type->type_unit_group == NULL)
10457 sig_type->type_unit_group = get_type_unit_group (cu, attr);
10458 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10459
10460 /* If we've already processed this stmt_list there's no real need to
10461 do it again, we could fake it and just recreate the part we need
10462 (file name,index -> symtab mapping). If data shows this optimization
10463 is useful we can do it then. */
43f3e411 10464 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
10465
10466 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10467 debug info. */
fff8551c 10468 line_header_up lh;
f4dc4d17 10469 if (attr != NULL)
3019eac3 10470 {
9c541725 10471 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
10472 lh = dwarf_decode_line_header (line_offset, cu);
10473 }
10474 if (lh == NULL)
10475 {
10476 if (first_time)
10477 dwarf2_start_symtab (cu, "", NULL, 0);
10478 else
10479 {
10480 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 10481 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 10482 }
f4dc4d17 10483 return;
3019eac3
DE
10484 }
10485
4c8aa72d
PA
10486 cu->line_header = lh.release ();
10487 cu->line_header_die_owner = die;
3019eac3 10488
f4dc4d17
DE
10489 if (first_time)
10490 {
43f3e411 10491 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 10492
1fd60fc0
DE
10493 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10494 still initializing it, and our caller (a few levels up)
10495 process_full_type_unit still needs to know if this is the first
10496 time. */
10497
4c8aa72d
PA
10498 tu_group->num_symtabs = cu->line_header->file_names.size ();
10499 tu_group->symtabs = XNEWVEC (struct symtab *,
10500 cu->line_header->file_names.size ());
3019eac3 10501
4c8aa72d 10502 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 10503 {
4c8aa72d 10504 file_entry &fe = cu->line_header->file_names[i];
3019eac3 10505
4c8aa72d 10506 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 10507
f4dc4d17
DE
10508 if (current_subfile->symtab == NULL)
10509 {
4c8aa72d
PA
10510 /* NOTE: start_subfile will recognize when it's been
10511 passed a file it has already seen. So we can't
10512 assume there's a simple mapping from
10513 cu->line_header->file_names to subfiles, plus
10514 cu->line_header->file_names may contain dups. */
43f3e411
DE
10515 current_subfile->symtab
10516 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
10517 }
10518
8c43009f
PA
10519 fe.symtab = current_subfile->symtab;
10520 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
10521 }
10522 }
10523 else
3019eac3 10524 {
0ab9ce85 10525 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 10526
4c8aa72d 10527 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 10528 {
4c8aa72d 10529 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 10530
4c8aa72d 10531 fe.symtab = tu_group->symtabs[i];
f4dc4d17 10532 }
3019eac3
DE
10533 }
10534
f4dc4d17
DE
10535 /* The main symtab is allocated last. Type units don't have DW_AT_name
10536 so they don't have a "real" (so to speak) symtab anyway.
10537 There is later code that will assign the main symtab to all symbols
10538 that don't have one. We need to handle the case of a symbol with a
10539 missing symtab (DW_AT_decl_file) anyway. */
10540}
3019eac3 10541
f4dc4d17
DE
10542/* Process DW_TAG_type_unit.
10543 For TUs we want to skip the first top level sibling if it's not the
10544 actual type being defined by this TU. In this case the first top
10545 level sibling is there to provide context only. */
3019eac3 10546
f4dc4d17
DE
10547static void
10548read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10549{
10550 struct die_info *child_die;
3019eac3 10551
f4dc4d17
DE
10552 prepare_one_comp_unit (cu, die, language_minimal);
10553
10554 /* Initialize (or reinitialize) the machinery for building symtabs.
10555 We do this before processing child DIEs, so that the line header table
10556 is available for DW_AT_decl_file. */
10557 setup_type_unit_groups (die, cu);
10558
10559 if (die->child != NULL)
10560 {
10561 child_die = die->child;
10562 while (child_die && child_die->tag)
10563 {
10564 process_die (child_die, cu);
10565 child_die = sibling_die (child_die);
10566 }
10567 }
3019eac3
DE
10568}
10569\f
80626a55
DE
10570/* DWO/DWP files.
10571
10572 http://gcc.gnu.org/wiki/DebugFission
10573 http://gcc.gnu.org/wiki/DebugFissionDWP
10574
10575 To simplify handling of both DWO files ("object" files with the DWARF info)
10576 and DWP files (a file with the DWOs packaged up into one file), we treat
10577 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10578
10579static hashval_t
10580hash_dwo_file (const void *item)
10581{
9a3c8263 10582 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10583 hashval_t hash;
3019eac3 10584
a2ce51a0
DE
10585 hash = htab_hash_string (dwo_file->dwo_name);
10586 if (dwo_file->comp_dir != NULL)
10587 hash += htab_hash_string (dwo_file->comp_dir);
10588 return hash;
3019eac3
DE
10589}
10590
10591static int
10592eq_dwo_file (const void *item_lhs, const void *item_rhs)
10593{
9a3c8263
SM
10594 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10595 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 10596
a2ce51a0
DE
10597 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10598 return 0;
10599 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10600 return lhs->comp_dir == rhs->comp_dir;
10601 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
10602}
10603
10604/* Allocate a hash table for DWO files. */
10605
10606static htab_t
10607allocate_dwo_file_hash_table (void)
10608{
10609 struct objfile *objfile = dwarf2_per_objfile->objfile;
10610
10611 return htab_create_alloc_ex (41,
10612 hash_dwo_file,
10613 eq_dwo_file,
10614 NULL,
10615 &objfile->objfile_obstack,
10616 hashtab_obstack_allocate,
10617 dummy_obstack_deallocate);
10618}
10619
80626a55
DE
10620/* Lookup DWO file DWO_NAME. */
10621
10622static void **
0ac5b59e 10623lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
10624{
10625 struct dwo_file find_entry;
10626 void **slot;
10627
10628 if (dwarf2_per_objfile->dwo_files == NULL)
10629 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
10630
10631 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
10632 find_entry.dwo_name = dwo_name;
10633 find_entry.comp_dir = comp_dir;
80626a55
DE
10634 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
10635
10636 return slot;
10637}
10638
3019eac3
DE
10639static hashval_t
10640hash_dwo_unit (const void *item)
10641{
9a3c8263 10642 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
10643
10644 /* This drops the top 32 bits of the id, but is ok for a hash. */
10645 return dwo_unit->signature;
10646}
10647
10648static int
10649eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10650{
9a3c8263
SM
10651 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10652 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
10653
10654 /* The signature is assumed to be unique within the DWO file.
10655 So while object file CU dwo_id's always have the value zero,
10656 that's OK, assuming each object file DWO file has only one CU,
10657 and that's the rule for now. */
10658 return lhs->signature == rhs->signature;
10659}
10660
10661/* Allocate a hash table for DWO CUs,TUs.
10662 There is one of these tables for each of CUs,TUs for each DWO file. */
10663
10664static htab_t
10665allocate_dwo_unit_table (struct objfile *objfile)
10666{
10667 /* Start out with a pretty small number.
10668 Generally DWO files contain only one CU and maybe some TUs. */
10669 return htab_create_alloc_ex (3,
10670 hash_dwo_unit,
10671 eq_dwo_unit,
10672 NULL,
10673 &objfile->objfile_obstack,
10674 hashtab_obstack_allocate,
10675 dummy_obstack_deallocate);
10676}
10677
80626a55 10678/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 10679
19c3d4c9 10680struct create_dwo_cu_data
3019eac3
DE
10681{
10682 struct dwo_file *dwo_file;
19c3d4c9 10683 struct dwo_unit dwo_unit;
3019eac3
DE
10684};
10685
19c3d4c9 10686/* die_reader_func for create_dwo_cu. */
3019eac3
DE
10687
10688static void
19c3d4c9
DE
10689create_dwo_cu_reader (const struct die_reader_specs *reader,
10690 const gdb_byte *info_ptr,
10691 struct die_info *comp_unit_die,
10692 int has_children,
10693 void *datap)
3019eac3
DE
10694{
10695 struct dwarf2_cu *cu = reader->cu;
9c541725 10696 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 10697 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 10698 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 10699 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 10700 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 10701 struct attribute *attr;
3019eac3
DE
10702
10703 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
10704 if (attr == NULL)
10705 {
19c3d4c9
DE
10706 complaint (&symfile_complaints,
10707 _("Dwarf Error: debug entry at offset 0x%x is missing"
10708 " its dwo_id [in module %s]"),
9c541725 10709 to_underlying (sect_off), dwo_file->dwo_name);
3019eac3
DE
10710 return;
10711 }
10712
3019eac3
DE
10713 dwo_unit->dwo_file = dwo_file;
10714 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 10715 dwo_unit->section = section;
9c541725 10716 dwo_unit->sect_off = sect_off;
3019eac3
DE
10717 dwo_unit->length = cu->per_cu->length;
10718
b4f54984 10719 if (dwarf_read_debug)
4031ecc5 10720 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9c541725
PA
10721 to_underlying (sect_off),
10722 hex_string (dwo_unit->signature));
3019eac3
DE
10723}
10724
33c5cd75 10725/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 10726 Note: This function processes DWO files only, not DWP files. */
3019eac3 10727
33c5cd75
DB
10728static void
10729create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
10730 htab_t &cus_htab)
3019eac3
DE
10731{
10732 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 10733 const gdb_byte *info_ptr, *end_ptr;
3019eac3 10734
33c5cd75
DB
10735 dwarf2_read_section (objfile, &section);
10736 info_ptr = section.buffer;
3019eac3
DE
10737
10738 if (info_ptr == NULL)
33c5cd75 10739 return;
3019eac3 10740
b4f54984 10741 if (dwarf_read_debug)
19c3d4c9
DE
10742 {
10743 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
10744 get_section_name (&section),
10745 get_section_file_name (&section));
19c3d4c9 10746 }
3019eac3 10747
33c5cd75 10748 end_ptr = info_ptr + section.size;
3019eac3
DE
10749 while (info_ptr < end_ptr)
10750 {
10751 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
10752 struct create_dwo_cu_data create_dwo_cu_data;
10753 struct dwo_unit *dwo_unit;
10754 void **slot;
10755 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 10756
19c3d4c9
DE
10757 memset (&create_dwo_cu_data.dwo_unit, 0,
10758 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
10759 memset (&per_cu, 0, sizeof (per_cu));
10760 per_cu.objfile = objfile;
10761 per_cu.is_debug_types = 0;
33c5cd75
DB
10762 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10763 per_cu.section = &section;
c5ed0576 10764 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
10765
10766 init_cutu_and_read_dies_no_follow (
10767 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
10768 info_ptr += per_cu.length;
10769
10770 // If the unit could not be parsed, skip it.
10771 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
10772 continue;
3019eac3 10773
33c5cd75
DB
10774 if (cus_htab == NULL)
10775 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 10776
33c5cd75
DB
10777 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10778 *dwo_unit = create_dwo_cu_data.dwo_unit;
10779 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
10780 gdb_assert (slot != NULL);
10781 if (*slot != NULL)
19c3d4c9 10782 {
33c5cd75
DB
10783 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10784 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 10785
33c5cd75
DB
10786 complaint (&symfile_complaints,
10787 _("debug cu entry at offset 0x%x is duplicate to"
10788 " the entry at offset 0x%x, signature %s"),
10789 to_underlying (sect_off), to_underlying (dup_sect_off),
10790 hex_string (dwo_unit->signature));
19c3d4c9 10791 }
33c5cd75 10792 *slot = (void *)dwo_unit;
3019eac3 10793 }
3019eac3
DE
10794}
10795
80626a55
DE
10796/* DWP file .debug_{cu,tu}_index section format:
10797 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
10798
d2415c6c
DE
10799 DWP Version 1:
10800
80626a55
DE
10801 Both index sections have the same format, and serve to map a 64-bit
10802 signature to a set of section numbers. Each section begins with a header,
10803 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10804 indexes, and a pool of 32-bit section numbers. The index sections will be
10805 aligned at 8-byte boundaries in the file.
10806
d2415c6c
DE
10807 The index section header consists of:
10808
10809 V, 32 bit version number
10810 -, 32 bits unused
10811 N, 32 bit number of compilation units or type units in the index
10812 M, 32 bit number of slots in the hash table
80626a55 10813
d2415c6c 10814 Numbers are recorded using the byte order of the application binary.
80626a55 10815
d2415c6c
DE
10816 The hash table begins at offset 16 in the section, and consists of an array
10817 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10818 order of the application binary). Unused slots in the hash table are 0.
10819 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 10820
d2415c6c
DE
10821 The parallel table begins immediately after the hash table
10822 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10823 array of 32-bit indexes (using the byte order of the application binary),
10824 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10825 table contains a 32-bit index into the pool of section numbers. For unused
10826 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 10827
73869dc2
DE
10828 The pool of section numbers begins immediately following the hash table
10829 (at offset 16 + 12 * M from the beginning of the section). The pool of
10830 section numbers consists of an array of 32-bit words (using the byte order
10831 of the application binary). Each item in the array is indexed starting
10832 from 0. The hash table entry provides the index of the first section
10833 number in the set. Additional section numbers in the set follow, and the
10834 set is terminated by a 0 entry (section number 0 is not used in ELF).
10835
10836 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10837 section must be the first entry in the set, and the .debug_abbrev.dwo must
10838 be the second entry. Other members of the set may follow in any order.
10839
10840 ---
10841
10842 DWP Version 2:
10843
10844 DWP Version 2 combines all the .debug_info, etc. sections into one,
10845 and the entries in the index tables are now offsets into these sections.
10846 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10847 section.
10848
10849 Index Section Contents:
10850 Header
10851 Hash Table of Signatures dwp_hash_table.hash_table
10852 Parallel Table of Indices dwp_hash_table.unit_table
10853 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
10854 Table of Section Sizes dwp_hash_table.v2.sizes
10855
10856 The index section header consists of:
10857
10858 V, 32 bit version number
10859 L, 32 bit number of columns in the table of section offsets
10860 N, 32 bit number of compilation units or type units in the index
10861 M, 32 bit number of slots in the hash table
10862
10863 Numbers are recorded using the byte order of the application binary.
10864
10865 The hash table has the same format as version 1.
10866 The parallel table of indices has the same format as version 1,
10867 except that the entries are origin-1 indices into the table of sections
10868 offsets and the table of section sizes.
10869
10870 The table of offsets begins immediately following the parallel table
10871 (at offset 16 + 12 * M from the beginning of the section). The table is
10872 a two-dimensional array of 32-bit words (using the byte order of the
10873 application binary), with L columns and N+1 rows, in row-major order.
10874 Each row in the array is indexed starting from 0. The first row provides
10875 a key to the remaining rows: each column in this row provides an identifier
10876 for a debug section, and the offsets in the same column of subsequent rows
10877 refer to that section. The section identifiers are:
10878
10879 DW_SECT_INFO 1 .debug_info.dwo
10880 DW_SECT_TYPES 2 .debug_types.dwo
10881 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10882 DW_SECT_LINE 4 .debug_line.dwo
10883 DW_SECT_LOC 5 .debug_loc.dwo
10884 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10885 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10886 DW_SECT_MACRO 8 .debug_macro.dwo
10887
10888 The offsets provided by the CU and TU index sections are the base offsets
10889 for the contributions made by each CU or TU to the corresponding section
10890 in the package file. Each CU and TU header contains an abbrev_offset
10891 field, used to find the abbreviations table for that CU or TU within the
10892 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10893 be interpreted as relative to the base offset given in the index section.
10894 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10895 should be interpreted as relative to the base offset for .debug_line.dwo,
10896 and offsets into other debug sections obtained from DWARF attributes should
10897 also be interpreted as relative to the corresponding base offset.
10898
10899 The table of sizes begins immediately following the table of offsets.
10900 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10901 with L columns and N rows, in row-major order. Each row in the array is
10902 indexed starting from 1 (row 0 is shared by the two tables).
10903
10904 ---
10905
10906 Hash table lookup is handled the same in version 1 and 2:
10907
10908 We assume that N and M will not exceed 2^32 - 1.
10909 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10910
d2415c6c
DE
10911 Given a 64-bit compilation unit signature or a type signature S, an entry
10912 in the hash table is located as follows:
80626a55 10913
d2415c6c
DE
10914 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10915 the low-order k bits all set to 1.
80626a55 10916
d2415c6c 10917 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 10918
d2415c6c
DE
10919 3) If the hash table entry at index H matches the signature, use that
10920 entry. If the hash table entry at index H is unused (all zeroes),
10921 terminate the search: the signature is not present in the table.
80626a55 10922
d2415c6c 10923 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 10924
d2415c6c 10925 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 10926 to stop at an unused slot or find the match. */
80626a55
DE
10927
10928/* Create a hash table to map DWO IDs to their CU/TU entry in
10929 .debug_{info,types}.dwo in DWP_FILE.
10930 Returns NULL if there isn't one.
10931 Note: This function processes DWP files only, not DWO files. */
10932
10933static struct dwp_hash_table *
10934create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
10935{
10936 struct objfile *objfile = dwarf2_per_objfile->objfile;
10937 bfd *dbfd = dwp_file->dbfd;
948f8e3d 10938 const gdb_byte *index_ptr, *index_end;
80626a55 10939 struct dwarf2_section_info *index;
73869dc2 10940 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
10941 struct dwp_hash_table *htab;
10942
10943 if (is_debug_types)
10944 index = &dwp_file->sections.tu_index;
10945 else
10946 index = &dwp_file->sections.cu_index;
10947
10948 if (dwarf2_section_empty_p (index))
10949 return NULL;
10950 dwarf2_read_section (objfile, index);
10951
10952 index_ptr = index->buffer;
10953 index_end = index_ptr + index->size;
10954
10955 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
10956 index_ptr += 4;
10957 if (version == 2)
10958 nr_columns = read_4_bytes (dbfd, index_ptr);
10959 else
10960 nr_columns = 0;
10961 index_ptr += 4;
80626a55
DE
10962 nr_units = read_4_bytes (dbfd, index_ptr);
10963 index_ptr += 4;
10964 nr_slots = read_4_bytes (dbfd, index_ptr);
10965 index_ptr += 4;
10966
73869dc2 10967 if (version != 1 && version != 2)
80626a55 10968 {
21aa081e 10969 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 10970 " [in module %s]"),
21aa081e 10971 pulongest (version), dwp_file->name);
80626a55
DE
10972 }
10973 if (nr_slots != (nr_slots & -nr_slots))
10974 {
21aa081e 10975 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 10976 " is not power of 2 [in module %s]"),
21aa081e 10977 pulongest (nr_slots), dwp_file->name);
80626a55
DE
10978 }
10979
10980 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
10981 htab->version = version;
10982 htab->nr_columns = nr_columns;
80626a55
DE
10983 htab->nr_units = nr_units;
10984 htab->nr_slots = nr_slots;
10985 htab->hash_table = index_ptr;
10986 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
10987
10988 /* Exit early if the table is empty. */
10989 if (nr_slots == 0 || nr_units == 0
10990 || (version == 2 && nr_columns == 0))
10991 {
10992 /* All must be zero. */
10993 if (nr_slots != 0 || nr_units != 0
10994 || (version == 2 && nr_columns != 0))
10995 {
10996 complaint (&symfile_complaints,
10997 _("Empty DWP but nr_slots,nr_units,nr_columns not"
10998 " all zero [in modules %s]"),
10999 dwp_file->name);
11000 }
11001 return htab;
11002 }
11003
11004 if (version == 1)
11005 {
11006 htab->section_pool.v1.indices =
11007 htab->unit_table + sizeof (uint32_t) * nr_slots;
11008 /* It's harder to decide whether the section is too small in v1.
11009 V1 is deprecated anyway so we punt. */
11010 }
11011 else
11012 {
11013 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11014 int *ids = htab->section_pool.v2.section_ids;
11015 /* Reverse map for error checking. */
11016 int ids_seen[DW_SECT_MAX + 1];
11017 int i;
11018
11019 if (nr_columns < 2)
11020 {
11021 error (_("Dwarf Error: bad DWP hash table, too few columns"
11022 " in section table [in module %s]"),
11023 dwp_file->name);
11024 }
11025 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11026 {
11027 error (_("Dwarf Error: bad DWP hash table, too many columns"
11028 " in section table [in module %s]"),
11029 dwp_file->name);
11030 }
11031 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
11032 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
11033 for (i = 0; i < nr_columns; ++i)
11034 {
11035 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11036
11037 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11038 {
11039 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11040 " in section table [in module %s]"),
11041 id, dwp_file->name);
11042 }
11043 if (ids_seen[id] != -1)
11044 {
11045 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11046 " id %d in section table [in module %s]"),
11047 id, dwp_file->name);
11048 }
11049 ids_seen[id] = i;
11050 ids[i] = id;
11051 }
11052 /* Must have exactly one info or types section. */
11053 if (((ids_seen[DW_SECT_INFO] != -1)
11054 + (ids_seen[DW_SECT_TYPES] != -1))
11055 != 1)
11056 {
11057 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11058 " DWO info/types section [in module %s]"),
11059 dwp_file->name);
11060 }
11061 /* Must have an abbrev section. */
11062 if (ids_seen[DW_SECT_ABBREV] == -1)
11063 {
11064 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11065 " section [in module %s]"),
11066 dwp_file->name);
11067 }
11068 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11069 htab->section_pool.v2.sizes =
11070 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11071 * nr_units * nr_columns);
11072 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11073 * nr_units * nr_columns))
11074 > index_end)
11075 {
11076 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11077 " [in module %s]"),
11078 dwp_file->name);
11079 }
11080 }
80626a55
DE
11081
11082 return htab;
11083}
11084
11085/* Update SECTIONS with the data from SECTP.
11086
11087 This function is like the other "locate" section routines that are
11088 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11089 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11090
11091 The result is non-zero for success, or zero if an error was found. */
11092
11093static int
73869dc2
DE
11094locate_v1_virtual_dwo_sections (asection *sectp,
11095 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11096{
11097 const struct dwop_section_names *names = &dwop_section_names;
11098
11099 if (section_is_p (sectp->name, &names->abbrev_dwo))
11100 {
11101 /* There can be only one. */
049412e3 11102 if (sections->abbrev.s.section != NULL)
80626a55 11103 return 0;
049412e3 11104 sections->abbrev.s.section = sectp;
80626a55
DE
11105 sections->abbrev.size = bfd_get_section_size (sectp);
11106 }
11107 else if (section_is_p (sectp->name, &names->info_dwo)
11108 || section_is_p (sectp->name, &names->types_dwo))
11109 {
11110 /* There can be only one. */
049412e3 11111 if (sections->info_or_types.s.section != NULL)
80626a55 11112 return 0;
049412e3 11113 sections->info_or_types.s.section = sectp;
80626a55
DE
11114 sections->info_or_types.size = bfd_get_section_size (sectp);
11115 }
11116 else if (section_is_p (sectp->name, &names->line_dwo))
11117 {
11118 /* There can be only one. */
049412e3 11119 if (sections->line.s.section != NULL)
80626a55 11120 return 0;
049412e3 11121 sections->line.s.section = sectp;
80626a55
DE
11122 sections->line.size = bfd_get_section_size (sectp);
11123 }
11124 else if (section_is_p (sectp->name, &names->loc_dwo))
11125 {
11126 /* There can be only one. */
049412e3 11127 if (sections->loc.s.section != NULL)
80626a55 11128 return 0;
049412e3 11129 sections->loc.s.section = sectp;
80626a55
DE
11130 sections->loc.size = bfd_get_section_size (sectp);
11131 }
11132 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11133 {
11134 /* There can be only one. */
049412e3 11135 if (sections->macinfo.s.section != NULL)
80626a55 11136 return 0;
049412e3 11137 sections->macinfo.s.section = sectp;
80626a55
DE
11138 sections->macinfo.size = bfd_get_section_size (sectp);
11139 }
11140 else if (section_is_p (sectp->name, &names->macro_dwo))
11141 {
11142 /* There can be only one. */
049412e3 11143 if (sections->macro.s.section != NULL)
80626a55 11144 return 0;
049412e3 11145 sections->macro.s.section = sectp;
80626a55
DE
11146 sections->macro.size = bfd_get_section_size (sectp);
11147 }
11148 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11149 {
11150 /* There can be only one. */
049412e3 11151 if (sections->str_offsets.s.section != NULL)
80626a55 11152 return 0;
049412e3 11153 sections->str_offsets.s.section = sectp;
80626a55
DE
11154 sections->str_offsets.size = bfd_get_section_size (sectp);
11155 }
11156 else
11157 {
11158 /* No other kind of section is valid. */
11159 return 0;
11160 }
11161
11162 return 1;
11163}
11164
73869dc2
DE
11165/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11166 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11167 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11168 This is for DWP version 1 files. */
80626a55
DE
11169
11170static struct dwo_unit *
73869dc2
DE
11171create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
11172 uint32_t unit_index,
11173 const char *comp_dir,
11174 ULONGEST signature, int is_debug_types)
80626a55
DE
11175{
11176 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
11177 const struct dwp_hash_table *dwp_htab =
11178 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
11179 bfd *dbfd = dwp_file->dbfd;
11180 const char *kind = is_debug_types ? "TU" : "CU";
11181 struct dwo_file *dwo_file;
11182 struct dwo_unit *dwo_unit;
73869dc2 11183 struct virtual_v1_dwo_sections sections;
80626a55 11184 void **dwo_file_slot;
80626a55
DE
11185 int i;
11186
73869dc2
DE
11187 gdb_assert (dwp_file->version == 1);
11188
b4f54984 11189 if (dwarf_read_debug)
80626a55 11190 {
73869dc2 11191 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11192 kind,
73869dc2 11193 pulongest (unit_index), hex_string (signature),
80626a55
DE
11194 dwp_file->name);
11195 }
11196
19ac8c2e 11197 /* Fetch the sections of this DWO unit.
80626a55
DE
11198 Put a limit on the number of sections we look for so that bad data
11199 doesn't cause us to loop forever. */
11200
73869dc2 11201#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11202 (1 /* .debug_info or .debug_types */ \
11203 + 1 /* .debug_abbrev */ \
11204 + 1 /* .debug_line */ \
11205 + 1 /* .debug_loc */ \
11206 + 1 /* .debug_str_offsets */ \
19ac8c2e 11207 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11208 + 1 /* trailing zero */)
11209
11210 memset (&sections, 0, sizeof (sections));
80626a55 11211
73869dc2 11212 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11213 {
11214 asection *sectp;
11215 uint32_t section_nr =
11216 read_4_bytes (dbfd,
73869dc2
DE
11217 dwp_htab->section_pool.v1.indices
11218 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11219
11220 if (section_nr == 0)
11221 break;
11222 if (section_nr >= dwp_file->num_sections)
11223 {
11224 error (_("Dwarf Error: bad DWP hash table, section number too large"
11225 " [in module %s]"),
11226 dwp_file->name);
11227 }
11228
11229 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11230 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11231 {
11232 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11233 " [in module %s]"),
11234 dwp_file->name);
11235 }
11236 }
11237
11238 if (i < 2
a32a8923
DE
11239 || dwarf2_section_empty_p (&sections.info_or_types)
11240 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
11241 {
11242 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11243 " [in module %s]"),
11244 dwp_file->name);
11245 }
73869dc2 11246 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11247 {
11248 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11249 " [in module %s]"),
11250 dwp_file->name);
11251 }
11252
11253 /* It's easier for the rest of the code if we fake a struct dwo_file and
11254 have dwo_unit "live" in that. At least for now.
11255
11256 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11257 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11258 file, we can combine them back into a virtual DWO file to save space
11259 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11260 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11261
791afaa2
TT
11262 std::string virtual_dwo_name =
11263 string_printf ("virtual-dwo/%d-%d-%d-%d",
11264 get_section_id (&sections.abbrev),
11265 get_section_id (&sections.line),
11266 get_section_id (&sections.loc),
11267 get_section_id (&sections.str_offsets));
80626a55 11268 /* Can we use an existing virtual DWO file? */
791afaa2 11269 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
80626a55
DE
11270 /* Create one if necessary. */
11271 if (*dwo_file_slot == NULL)
11272 {
b4f54984 11273 if (dwarf_read_debug)
80626a55
DE
11274 {
11275 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11276 virtual_dwo_name.c_str ());
80626a55
DE
11277 }
11278 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
11279 dwo_file->dwo_name
11280 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
11281 virtual_dwo_name.c_str (),
11282 virtual_dwo_name.size ());
0ac5b59e 11283 dwo_file->comp_dir = comp_dir;
80626a55
DE
11284 dwo_file->sections.abbrev = sections.abbrev;
11285 dwo_file->sections.line = sections.line;
11286 dwo_file->sections.loc = sections.loc;
11287 dwo_file->sections.macinfo = sections.macinfo;
11288 dwo_file->sections.macro = sections.macro;
11289 dwo_file->sections.str_offsets = sections.str_offsets;
11290 /* The "str" section is global to the entire DWP file. */
11291 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11292 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11293 there's no need to record it in dwo_file.
11294 Also, we can't simply record type sections in dwo_file because
11295 we record a pointer into the vector in dwo_unit. As we collect more
11296 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11297 for it, invalidating all copies of pointers into the previous
11298 contents. */
80626a55
DE
11299 *dwo_file_slot = dwo_file;
11300 }
11301 else
11302 {
b4f54984 11303 if (dwarf_read_debug)
80626a55
DE
11304 {
11305 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11306 virtual_dwo_name.c_str ());
80626a55 11307 }
9a3c8263 11308 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11309 }
80626a55
DE
11310
11311 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11312 dwo_unit->dwo_file = dwo_file;
11313 dwo_unit->signature = signature;
8d749320
SM
11314 dwo_unit->section =
11315 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 11316 *dwo_unit->section = sections.info_or_types;
57d63ce2 11317 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11318
11319 return dwo_unit;
11320}
11321
73869dc2
DE
11322/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11323 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11324 piece within that section used by a TU/CU, return a virtual section
11325 of just that piece. */
11326
11327static struct dwarf2_section_info
11328create_dwp_v2_section (struct dwarf2_section_info *section,
11329 bfd_size_type offset, bfd_size_type size)
11330{
11331 struct dwarf2_section_info result;
11332 asection *sectp;
11333
11334 gdb_assert (section != NULL);
11335 gdb_assert (!section->is_virtual);
11336
11337 memset (&result, 0, sizeof (result));
11338 result.s.containing_section = section;
11339 result.is_virtual = 1;
11340
11341 if (size == 0)
11342 return result;
11343
11344 sectp = get_section_bfd_section (section);
11345
11346 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11347 bounds of the real section. This is a pretty-rare event, so just
11348 flag an error (easier) instead of a warning and trying to cope. */
11349 if (sectp == NULL
11350 || offset + size > bfd_get_section_size (sectp))
11351 {
73869dc2
DE
11352 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11353 " in section %s [in module %s]"),
11354 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
11355 objfile_name (dwarf2_per_objfile->objfile));
11356 }
11357
11358 result.virtual_offset = offset;
11359 result.size = size;
11360 return result;
11361}
11362
11363/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11364 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11365 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11366 This is for DWP version 2 files. */
11367
11368static struct dwo_unit *
11369create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
11370 uint32_t unit_index,
11371 const char *comp_dir,
11372 ULONGEST signature, int is_debug_types)
11373{
11374 struct objfile *objfile = dwarf2_per_objfile->objfile;
11375 const struct dwp_hash_table *dwp_htab =
11376 is_debug_types ? dwp_file->tus : dwp_file->cus;
11377 bfd *dbfd = dwp_file->dbfd;
11378 const char *kind = is_debug_types ? "TU" : "CU";
11379 struct dwo_file *dwo_file;
11380 struct dwo_unit *dwo_unit;
11381 struct virtual_v2_dwo_sections sections;
11382 void **dwo_file_slot;
73869dc2
DE
11383 int i;
11384
11385 gdb_assert (dwp_file->version == 2);
11386
b4f54984 11387 if (dwarf_read_debug)
73869dc2
DE
11388 {
11389 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11390 kind,
11391 pulongest (unit_index), hex_string (signature),
11392 dwp_file->name);
11393 }
11394
11395 /* Fetch the section offsets of this DWO unit. */
11396
11397 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11398
11399 for (i = 0; i < dwp_htab->nr_columns; ++i)
11400 {
11401 uint32_t offset = read_4_bytes (dbfd,
11402 dwp_htab->section_pool.v2.offsets
11403 + (((unit_index - 1) * dwp_htab->nr_columns
11404 + i)
11405 * sizeof (uint32_t)));
11406 uint32_t size = read_4_bytes (dbfd,
11407 dwp_htab->section_pool.v2.sizes
11408 + (((unit_index - 1) * dwp_htab->nr_columns
11409 + i)
11410 * sizeof (uint32_t)));
11411
11412 switch (dwp_htab->section_pool.v2.section_ids[i])
11413 {
11414 case DW_SECT_INFO:
11415 case DW_SECT_TYPES:
11416 sections.info_or_types_offset = offset;
11417 sections.info_or_types_size = size;
11418 break;
11419 case DW_SECT_ABBREV:
11420 sections.abbrev_offset = offset;
11421 sections.abbrev_size = size;
11422 break;
11423 case DW_SECT_LINE:
11424 sections.line_offset = offset;
11425 sections.line_size = size;
11426 break;
11427 case DW_SECT_LOC:
11428 sections.loc_offset = offset;
11429 sections.loc_size = size;
11430 break;
11431 case DW_SECT_STR_OFFSETS:
11432 sections.str_offsets_offset = offset;
11433 sections.str_offsets_size = size;
11434 break;
11435 case DW_SECT_MACINFO:
11436 sections.macinfo_offset = offset;
11437 sections.macinfo_size = size;
11438 break;
11439 case DW_SECT_MACRO:
11440 sections.macro_offset = offset;
11441 sections.macro_size = size;
11442 break;
11443 }
11444 }
11445
11446 /* It's easier for the rest of the code if we fake a struct dwo_file and
11447 have dwo_unit "live" in that. At least for now.
11448
11449 The DWP file can be made up of a random collection of CUs and TUs.
11450 However, for each CU + set of TUs that came from the same original DWO
11451 file, we can combine them back into a virtual DWO file to save space
11452 (fewer struct dwo_file objects to allocate). Remember that for really
11453 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11454
791afaa2
TT
11455 std::string virtual_dwo_name =
11456 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11457 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11458 (long) (sections.line_size ? sections.line_offset : 0),
11459 (long) (sections.loc_size ? sections.loc_offset : 0),
11460 (long) (sections.str_offsets_size
11461 ? sections.str_offsets_offset : 0));
73869dc2 11462 /* Can we use an existing virtual DWO file? */
791afaa2 11463 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
73869dc2
DE
11464 /* Create one if necessary. */
11465 if (*dwo_file_slot == NULL)
11466 {
b4f54984 11467 if (dwarf_read_debug)
73869dc2
DE
11468 {
11469 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11470 virtual_dwo_name.c_str ());
73869dc2
DE
11471 }
11472 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
11473 dwo_file->dwo_name
11474 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
11475 virtual_dwo_name.c_str (),
11476 virtual_dwo_name.size ());
73869dc2
DE
11477 dwo_file->comp_dir = comp_dir;
11478 dwo_file->sections.abbrev =
11479 create_dwp_v2_section (&dwp_file->sections.abbrev,
11480 sections.abbrev_offset, sections.abbrev_size);
11481 dwo_file->sections.line =
11482 create_dwp_v2_section (&dwp_file->sections.line,
11483 sections.line_offset, sections.line_size);
11484 dwo_file->sections.loc =
11485 create_dwp_v2_section (&dwp_file->sections.loc,
11486 sections.loc_offset, sections.loc_size);
11487 dwo_file->sections.macinfo =
11488 create_dwp_v2_section (&dwp_file->sections.macinfo,
11489 sections.macinfo_offset, sections.macinfo_size);
11490 dwo_file->sections.macro =
11491 create_dwp_v2_section (&dwp_file->sections.macro,
11492 sections.macro_offset, sections.macro_size);
11493 dwo_file->sections.str_offsets =
11494 create_dwp_v2_section (&dwp_file->sections.str_offsets,
11495 sections.str_offsets_offset,
11496 sections.str_offsets_size);
11497 /* The "str" section is global to the entire DWP file. */
11498 dwo_file->sections.str = dwp_file->sections.str;
11499 /* The info or types section is assigned below to dwo_unit,
11500 there's no need to record it in dwo_file.
11501 Also, we can't simply record type sections in dwo_file because
11502 we record a pointer into the vector in dwo_unit. As we collect more
11503 types we'll grow the vector and eventually have to reallocate space
11504 for it, invalidating all copies of pointers into the previous
11505 contents. */
11506 *dwo_file_slot = dwo_file;
11507 }
11508 else
11509 {
b4f54984 11510 if (dwarf_read_debug)
73869dc2
DE
11511 {
11512 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11513 virtual_dwo_name.c_str ());
73869dc2 11514 }
9a3c8263 11515 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11516 }
73869dc2
DE
11517
11518 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11519 dwo_unit->dwo_file = dwo_file;
11520 dwo_unit->signature = signature;
8d749320
SM
11521 dwo_unit->section =
11522 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
73869dc2
DE
11523 *dwo_unit->section = create_dwp_v2_section (is_debug_types
11524 ? &dwp_file->sections.types
11525 : &dwp_file->sections.info,
11526 sections.info_or_types_offset,
11527 sections.info_or_types_size);
11528 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11529
11530 return dwo_unit;
11531}
11532
57d63ce2
DE
11533/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11534 Returns NULL if the signature isn't found. */
80626a55
DE
11535
11536static struct dwo_unit *
57d63ce2
DE
11537lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
11538 ULONGEST signature, int is_debug_types)
80626a55 11539{
57d63ce2
DE
11540 const struct dwp_hash_table *dwp_htab =
11541 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 11542 bfd *dbfd = dwp_file->dbfd;
57d63ce2 11543 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11544 uint32_t hash = signature & mask;
11545 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11546 unsigned int i;
11547 void **slot;
870f88f7 11548 struct dwo_unit find_dwo_cu;
80626a55
DE
11549
11550 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11551 find_dwo_cu.signature = signature;
19ac8c2e
DE
11552 slot = htab_find_slot (is_debug_types
11553 ? dwp_file->loaded_tus
11554 : dwp_file->loaded_cus,
11555 &find_dwo_cu, INSERT);
80626a55
DE
11556
11557 if (*slot != NULL)
9a3c8263 11558 return (struct dwo_unit *) *slot;
80626a55
DE
11559
11560 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11561 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11562 {
11563 ULONGEST signature_in_table;
11564
11565 signature_in_table =
57d63ce2 11566 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
11567 if (signature_in_table == signature)
11568 {
57d63ce2
DE
11569 uint32_t unit_index =
11570 read_4_bytes (dbfd,
11571 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 11572
73869dc2
DE
11573 if (dwp_file->version == 1)
11574 {
11575 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
11576 comp_dir, signature,
11577 is_debug_types);
11578 }
11579 else
11580 {
11581 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
11582 comp_dir, signature,
11583 is_debug_types);
11584 }
9a3c8263 11585 return (struct dwo_unit *) *slot;
80626a55
DE
11586 }
11587 if (signature_in_table == 0)
11588 return NULL;
11589 hash = (hash + hash2) & mask;
11590 }
11591
11592 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11593 " [in module %s]"),
11594 dwp_file->name);
11595}
11596
ab5088bf 11597/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
11598 Open the file specified by FILE_NAME and hand it off to BFD for
11599 preliminary analysis. Return a newly initialized bfd *, which
11600 includes a canonicalized copy of FILE_NAME.
80626a55 11601 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
11602 SEARCH_CWD is true if the current directory is to be searched.
11603 It will be searched before debug-file-directory.
13aaf454
DE
11604 If successful, the file is added to the bfd include table of the
11605 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 11606 If unable to find/open the file, return NULL.
3019eac3
DE
11607 NOTE: This function is derived from symfile_bfd_open. */
11608
192b62ce 11609static gdb_bfd_ref_ptr
6ac97d4c 11610try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3 11611{
80626a55 11612 int desc, flags;
3019eac3 11613 char *absolute_name;
9c02c129
DE
11614 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11615 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11616 to debug_file_directory. */
11617 char *search_path;
11618 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11619
6ac97d4c
DE
11620 if (search_cwd)
11621 {
11622 if (*debug_file_directory != '\0')
11623 search_path = concat (".", dirname_separator_string,
b36cec19 11624 debug_file_directory, (char *) NULL);
6ac97d4c
DE
11625 else
11626 search_path = xstrdup (".");
11627 }
9c02c129 11628 else
6ac97d4c 11629 search_path = xstrdup (debug_file_directory);
3019eac3 11630
492c0ab7 11631 flags = OPF_RETURN_REALPATH;
80626a55
DE
11632 if (is_dwp)
11633 flags |= OPF_SEARCH_IN_PATH;
9c02c129 11634 desc = openp (search_path, flags, file_name,
3019eac3 11635 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 11636 xfree (search_path);
3019eac3
DE
11637 if (desc < 0)
11638 return NULL;
11639
192b62ce 11640 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 11641 xfree (absolute_name);
9c02c129
DE
11642 if (sym_bfd == NULL)
11643 return NULL;
192b62ce 11644 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 11645
192b62ce
TT
11646 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11647 return NULL;
3019eac3 11648
13aaf454
DE
11649 /* Success. Record the bfd as having been included by the objfile's bfd.
11650 This is important because things like demangled_names_hash lives in the
11651 objfile's per_bfd space and may have references to things like symbol
11652 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 11653 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 11654
3019eac3
DE
11655 return sym_bfd;
11656}
11657
ab5088bf 11658/* Try to open DWO file FILE_NAME.
3019eac3
DE
11659 COMP_DIR is the DW_AT_comp_dir attribute.
11660 The result is the bfd handle of the file.
11661 If there is a problem finding or opening the file, return NULL.
11662 Upon success, the canonicalized path of the file is stored in the bfd,
11663 same as symfile_bfd_open. */
11664
192b62ce 11665static gdb_bfd_ref_ptr
ab5088bf 11666open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3 11667{
80626a55 11668 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 11669 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
11670
11671 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11672
11673 if (comp_dir != NULL)
11674 {
b36cec19
PA
11675 char *path_to_try = concat (comp_dir, SLASH_STRING,
11676 file_name, (char *) NULL);
3019eac3
DE
11677
11678 /* NOTE: If comp_dir is a relative path, this will also try the
11679 search path, which seems useful. */
192b62ce
TT
11680 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
11681 1 /*search_cwd*/));
3019eac3
DE
11682 xfree (path_to_try);
11683 if (abfd != NULL)
11684 return abfd;
11685 }
11686
11687 /* That didn't work, try debug-file-directory, which, despite its name,
11688 is a list of paths. */
11689
11690 if (*debug_file_directory == '\0')
11691 return NULL;
11692
6ac97d4c 11693 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
11694}
11695
80626a55
DE
11696/* This function is mapped across the sections and remembers the offset and
11697 size of each of the DWO debugging sections we are interested in. */
11698
11699static void
11700dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
11701{
9a3c8263 11702 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
11703 const struct dwop_section_names *names = &dwop_section_names;
11704
11705 if (section_is_p (sectp->name, &names->abbrev_dwo))
11706 {
049412e3 11707 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
11708 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
11709 }
11710 else if (section_is_p (sectp->name, &names->info_dwo))
11711 {
049412e3 11712 dwo_sections->info.s.section = sectp;
80626a55
DE
11713 dwo_sections->info.size = bfd_get_section_size (sectp);
11714 }
11715 else if (section_is_p (sectp->name, &names->line_dwo))
11716 {
049412e3 11717 dwo_sections->line.s.section = sectp;
80626a55
DE
11718 dwo_sections->line.size = bfd_get_section_size (sectp);
11719 }
11720 else if (section_is_p (sectp->name, &names->loc_dwo))
11721 {
049412e3 11722 dwo_sections->loc.s.section = sectp;
80626a55
DE
11723 dwo_sections->loc.size = bfd_get_section_size (sectp);
11724 }
11725 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11726 {
049412e3 11727 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
11728 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
11729 }
11730 else if (section_is_p (sectp->name, &names->macro_dwo))
11731 {
049412e3 11732 dwo_sections->macro.s.section = sectp;
80626a55
DE
11733 dwo_sections->macro.size = bfd_get_section_size (sectp);
11734 }
11735 else if (section_is_p (sectp->name, &names->str_dwo))
11736 {
049412e3 11737 dwo_sections->str.s.section = sectp;
80626a55
DE
11738 dwo_sections->str.size = bfd_get_section_size (sectp);
11739 }
11740 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11741 {
049412e3 11742 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
11743 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
11744 }
11745 else if (section_is_p (sectp->name, &names->types_dwo))
11746 {
11747 struct dwarf2_section_info type_section;
11748
11749 memset (&type_section, 0, sizeof (type_section));
049412e3 11750 type_section.s.section = sectp;
80626a55
DE
11751 type_section.size = bfd_get_section_size (sectp);
11752 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
11753 &type_section);
11754 }
11755}
11756
ab5088bf 11757/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 11758 by PER_CU. This is for the non-DWP case.
80626a55 11759 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
11760
11761static struct dwo_file *
0ac5b59e
DE
11762open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
11763 const char *dwo_name, const char *comp_dir)
3019eac3
DE
11764{
11765 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 11766 struct dwo_file *dwo_file;
3019eac3
DE
11767 struct cleanup *cleanups;
11768
192b62ce 11769 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
80626a55
DE
11770 if (dbfd == NULL)
11771 {
b4f54984 11772 if (dwarf_read_debug)
80626a55
DE
11773 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
11774 return NULL;
11775 }
11776 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
11777 dwo_file->dwo_name = dwo_name;
11778 dwo_file->comp_dir = comp_dir;
192b62ce 11779 dwo_file->dbfd = dbfd.release ();
3019eac3
DE
11780
11781 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
11782
192b62ce
TT
11783 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
11784 &dwo_file->sections);
3019eac3 11785
33c5cd75 11786 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
3019eac3 11787
78d4d2c5
JK
11788 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
11789 dwo_file->tus);
3019eac3
DE
11790
11791 discard_cleanups (cleanups);
11792
b4f54984 11793 if (dwarf_read_debug)
80626a55
DE
11794 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
11795
3019eac3
DE
11796 return dwo_file;
11797}
11798
80626a55 11799/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
11800 size of each of the DWP debugging sections common to version 1 and 2 that
11801 we are interested in. */
3019eac3 11802
80626a55 11803static void
73869dc2
DE
11804dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
11805 void *dwp_file_ptr)
3019eac3 11806{
9a3c8263 11807 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
11808 const struct dwop_section_names *names = &dwop_section_names;
11809 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 11810
80626a55 11811 /* Record the ELF section number for later lookup: this is what the
73869dc2 11812 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
11813 gdb_assert (elf_section_nr < dwp_file->num_sections);
11814 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 11815
80626a55
DE
11816 /* Look for specific sections that we need. */
11817 if (section_is_p (sectp->name, &names->str_dwo))
11818 {
049412e3 11819 dwp_file->sections.str.s.section = sectp;
80626a55
DE
11820 dwp_file->sections.str.size = bfd_get_section_size (sectp);
11821 }
11822 else if (section_is_p (sectp->name, &names->cu_index))
11823 {
049412e3 11824 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
11825 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
11826 }
11827 else if (section_is_p (sectp->name, &names->tu_index))
11828 {
049412e3 11829 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
11830 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
11831 }
11832}
3019eac3 11833
73869dc2
DE
11834/* This function is mapped across the sections and remembers the offset and
11835 size of each of the DWP version 2 debugging sections that we are interested
11836 in. This is split into a separate function because we don't know if we
11837 have version 1 or 2 until we parse the cu_index/tu_index sections. */
11838
11839static void
11840dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
11841{
9a3c8263 11842 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
11843 const struct dwop_section_names *names = &dwop_section_names;
11844 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11845
11846 /* Record the ELF section number for later lookup: this is what the
11847 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11848 gdb_assert (elf_section_nr < dwp_file->num_sections);
11849 dwp_file->elf_sections[elf_section_nr] = sectp;
11850
11851 /* Look for specific sections that we need. */
11852 if (section_is_p (sectp->name, &names->abbrev_dwo))
11853 {
049412e3 11854 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
11855 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
11856 }
11857 else if (section_is_p (sectp->name, &names->info_dwo))
11858 {
049412e3 11859 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
11860 dwp_file->sections.info.size = bfd_get_section_size (sectp);
11861 }
11862 else if (section_is_p (sectp->name, &names->line_dwo))
11863 {
049412e3 11864 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
11865 dwp_file->sections.line.size = bfd_get_section_size (sectp);
11866 }
11867 else if (section_is_p (sectp->name, &names->loc_dwo))
11868 {
049412e3 11869 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
11870 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
11871 }
11872 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11873 {
049412e3 11874 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
11875 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
11876 }
11877 else if (section_is_p (sectp->name, &names->macro_dwo))
11878 {
049412e3 11879 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
11880 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
11881 }
11882 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11883 {
049412e3 11884 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
11885 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
11886 }
11887 else if (section_is_p (sectp->name, &names->types_dwo))
11888 {
049412e3 11889 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
11890 dwp_file->sections.types.size = bfd_get_section_size (sectp);
11891 }
11892}
11893
80626a55 11894/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 11895
80626a55
DE
11896static hashval_t
11897hash_dwp_loaded_cutus (const void *item)
11898{
9a3c8263 11899 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 11900
80626a55
DE
11901 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11902 return dwo_unit->signature;
3019eac3
DE
11903}
11904
80626a55 11905/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 11906
80626a55
DE
11907static int
11908eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 11909{
9a3c8263
SM
11910 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11911 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 11912
80626a55
DE
11913 return dua->signature == dub->signature;
11914}
3019eac3 11915
80626a55 11916/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 11917
80626a55
DE
11918static htab_t
11919allocate_dwp_loaded_cutus_table (struct objfile *objfile)
11920{
11921 return htab_create_alloc_ex (3,
11922 hash_dwp_loaded_cutus,
11923 eq_dwp_loaded_cutus,
11924 NULL,
11925 &objfile->objfile_obstack,
11926 hashtab_obstack_allocate,
11927 dummy_obstack_deallocate);
11928}
3019eac3 11929
ab5088bf
DE
11930/* Try to open DWP file FILE_NAME.
11931 The result is the bfd handle of the file.
11932 If there is a problem finding or opening the file, return NULL.
11933 Upon success, the canonicalized path of the file is stored in the bfd,
11934 same as symfile_bfd_open. */
11935
192b62ce 11936static gdb_bfd_ref_ptr
ab5088bf
DE
11937open_dwp_file (const char *file_name)
11938{
192b62ce
TT
11939 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
11940 1 /*search_cwd*/));
6ac97d4c
DE
11941 if (abfd != NULL)
11942 return abfd;
11943
11944 /* Work around upstream bug 15652.
11945 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11946 [Whether that's a "bug" is debatable, but it is getting in our way.]
11947 We have no real idea where the dwp file is, because gdb's realpath-ing
11948 of the executable's path may have discarded the needed info.
11949 [IWBN if the dwp file name was recorded in the executable, akin to
11950 .gnu_debuglink, but that doesn't exist yet.]
11951 Strip the directory from FILE_NAME and search again. */
11952 if (*debug_file_directory != '\0')
11953 {
11954 /* Don't implicitly search the current directory here.
11955 If the user wants to search "." to handle this case,
11956 it must be added to debug-file-directory. */
11957 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
11958 0 /*search_cwd*/);
11959 }
11960
11961 return NULL;
ab5088bf
DE
11962}
11963
80626a55
DE
11964/* Initialize the use of the DWP file for the current objfile.
11965 By convention the name of the DWP file is ${objfile}.dwp.
11966 The result is NULL if it can't be found. */
a766d390 11967
80626a55 11968static struct dwp_file *
ab5088bf 11969open_and_init_dwp_file (void)
80626a55
DE
11970{
11971 struct objfile *objfile = dwarf2_per_objfile->objfile;
11972 struct dwp_file *dwp_file;
80626a55 11973
82bf32bc
JK
11974 /* Try to find first .dwp for the binary file before any symbolic links
11975 resolving. */
6c447423
DE
11976
11977 /* If the objfile is a debug file, find the name of the real binary
11978 file and get the name of dwp file from there. */
d721ba37 11979 std::string dwp_name;
6c447423
DE
11980 if (objfile->separate_debug_objfile_backlink != NULL)
11981 {
11982 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11983 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 11984
d721ba37 11985 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
11986 }
11987 else
d721ba37
PA
11988 dwp_name = objfile->original_name;
11989
11990 dwp_name += ".dwp";
80626a55 11991
d721ba37 11992 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
82bf32bc
JK
11993 if (dbfd == NULL
11994 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11995 {
11996 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
11997 dwp_name = objfile_name (objfile);
11998 dwp_name += ".dwp";
11999 dbfd = open_dwp_file (dwp_name.c_str ());
82bf32bc
JK
12000 }
12001
80626a55
DE
12002 if (dbfd == NULL)
12003 {
b4f54984 12004 if (dwarf_read_debug)
d721ba37 12005 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 12006 return NULL;
3019eac3 12007 }
80626a55 12008 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
12009 dwp_file->name = bfd_get_filename (dbfd.get ());
12010 dwp_file->dbfd = dbfd.release ();
c906108c 12011
80626a55 12012 /* +1: section 0 is unused */
192b62ce 12013 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
12014 dwp_file->elf_sections =
12015 OBSTACK_CALLOC (&objfile->objfile_obstack,
12016 dwp_file->num_sections, asection *);
12017
192b62ce
TT
12018 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
12019 dwp_file);
80626a55
DE
12020
12021 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
12022
12023 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
12024
73869dc2 12025 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12026 if (dwp_file->cus && dwp_file->tus
12027 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12028 {
12029 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12030 pretty bizarre. We use pulongest here because that's the established
4d65956b 12031 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12032 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12033 " TU version %s [in DWP file %s]"),
12034 pulongest (dwp_file->cus->version),
d721ba37 12035 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12036 }
08302ed2
DE
12037
12038 if (dwp_file->cus)
12039 dwp_file->version = dwp_file->cus->version;
12040 else if (dwp_file->tus)
12041 dwp_file->version = dwp_file->tus->version;
12042 else
12043 dwp_file->version = 2;
73869dc2
DE
12044
12045 if (dwp_file->version == 2)
192b62ce
TT
12046 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
12047 dwp_file);
73869dc2 12048
19ac8c2e
DE
12049 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
12050 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 12051
b4f54984 12052 if (dwarf_read_debug)
80626a55
DE
12053 {
12054 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12055 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12056 " %s CUs, %s TUs\n",
12057 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12058 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12059 }
12060
12061 return dwp_file;
3019eac3 12062}
c906108c 12063
ab5088bf
DE
12064/* Wrapper around open_and_init_dwp_file, only open it once. */
12065
12066static struct dwp_file *
12067get_dwp_file (void)
12068{
12069 if (! dwarf2_per_objfile->dwp_checked)
12070 {
12071 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
12072 dwarf2_per_objfile->dwp_checked = 1;
12073 }
12074 return dwarf2_per_objfile->dwp_file;
12075}
12076
80626a55
DE
12077/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12078 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12079 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12080 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12081 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12082
12083 This is called, for example, when wanting to read a variable with a
12084 complex location. Therefore we don't want to do file i/o for every call.
12085 Therefore we don't want to look for a DWO file on every call.
12086 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12087 then we check if we've already seen DWO_NAME, and only THEN do we check
12088 for a DWO file.
12089
1c658ad5 12090 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12091 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12092
3019eac3 12093static struct dwo_unit *
80626a55
DE
12094lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12095 const char *dwo_name, const char *comp_dir,
12096 ULONGEST signature, int is_debug_types)
3019eac3
DE
12097{
12098 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12099 const char *kind = is_debug_types ? "TU" : "CU";
12100 void **dwo_file_slot;
3019eac3 12101 struct dwo_file *dwo_file;
80626a55 12102 struct dwp_file *dwp_file;
cb1df416 12103
6a506a2d
DE
12104 /* First see if there's a DWP file.
12105 If we have a DWP file but didn't find the DWO inside it, don't
12106 look for the original DWO file. It makes gdb behave differently
12107 depending on whether one is debugging in the build tree. */
cf2c3c16 12108
ab5088bf 12109 dwp_file = get_dwp_file ();
80626a55 12110 if (dwp_file != NULL)
cf2c3c16 12111 {
80626a55
DE
12112 const struct dwp_hash_table *dwp_htab =
12113 is_debug_types ? dwp_file->tus : dwp_file->cus;
12114
12115 if (dwp_htab != NULL)
12116 {
12117 struct dwo_unit *dwo_cutu =
57d63ce2
DE
12118 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
12119 signature, is_debug_types);
80626a55
DE
12120
12121 if (dwo_cutu != NULL)
12122 {
b4f54984 12123 if (dwarf_read_debug)
80626a55
DE
12124 {
12125 fprintf_unfiltered (gdb_stdlog,
12126 "Virtual DWO %s %s found: @%s\n",
12127 kind, hex_string (signature),
12128 host_address_to_string (dwo_cutu));
12129 }
12130 return dwo_cutu;
12131 }
12132 }
12133 }
6a506a2d 12134 else
80626a55 12135 {
6a506a2d 12136 /* No DWP file, look for the DWO file. */
80626a55 12137
6a506a2d
DE
12138 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
12139 if (*dwo_file_slot == NULL)
80626a55 12140 {
6a506a2d
DE
12141 /* Read in the file and build a table of the CUs/TUs it contains. */
12142 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12143 }
6a506a2d 12144 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12145 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12146
6a506a2d 12147 if (dwo_file != NULL)
19c3d4c9 12148 {
6a506a2d
DE
12149 struct dwo_unit *dwo_cutu = NULL;
12150
12151 if (is_debug_types && dwo_file->tus)
12152 {
12153 struct dwo_unit find_dwo_cutu;
12154
12155 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12156 find_dwo_cutu.signature = signature;
9a3c8263
SM
12157 dwo_cutu
12158 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 12159 }
33c5cd75 12160 else if (!is_debug_types && dwo_file->cus)
80626a55 12161 {
33c5cd75
DB
12162 struct dwo_unit find_dwo_cutu;
12163
12164 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12165 find_dwo_cutu.signature = signature;
12166 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
12167 &find_dwo_cutu);
6a506a2d
DE
12168 }
12169
12170 if (dwo_cutu != NULL)
12171 {
b4f54984 12172 if (dwarf_read_debug)
6a506a2d
DE
12173 {
12174 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12175 kind, dwo_name, hex_string (signature),
12176 host_address_to_string (dwo_cutu));
12177 }
12178 return dwo_cutu;
80626a55
DE
12179 }
12180 }
2e276125 12181 }
9cdd5dbd 12182
80626a55
DE
12183 /* We didn't find it. This could mean a dwo_id mismatch, or
12184 someone deleted the DWO/DWP file, or the search path isn't set up
12185 correctly to find the file. */
12186
b4f54984 12187 if (dwarf_read_debug)
80626a55
DE
12188 {
12189 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12190 kind, dwo_name, hex_string (signature));
12191 }
3019eac3 12192
6656a72d
DE
12193 /* This is a warning and not a complaint because it can be caused by
12194 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12195 {
12196 /* Print the name of the DWP file if we looked there, helps the user
12197 better diagnose the problem. */
791afaa2 12198 std::string dwp_text;
43942612
DE
12199
12200 if (dwp_file != NULL)
791afaa2
TT
12201 dwp_text = string_printf (" [in DWP file %s]",
12202 lbasename (dwp_file->name));
43942612
DE
12203
12204 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
12205 " [in module %s]"),
12206 kind, dwo_name, hex_string (signature),
791afaa2 12207 dwp_text.c_str (),
43942612 12208 this_unit->is_debug_types ? "TU" : "CU",
9c541725 12209 to_underlying (this_unit->sect_off), objfile_name (objfile));
43942612 12210 }
3019eac3 12211 return NULL;
5fb290d7
DJ
12212}
12213
80626a55
DE
12214/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12215 See lookup_dwo_cutu_unit for details. */
12216
12217static struct dwo_unit *
12218lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12219 const char *dwo_name, const char *comp_dir,
12220 ULONGEST signature)
12221{
12222 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12223}
12224
12225/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12226 See lookup_dwo_cutu_unit for details. */
12227
12228static struct dwo_unit *
12229lookup_dwo_type_unit (struct signatured_type *this_tu,
12230 const char *dwo_name, const char *comp_dir)
12231{
12232 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12233}
12234
89e63ee4
DE
12235/* Traversal function for queue_and_load_all_dwo_tus. */
12236
12237static int
12238queue_and_load_dwo_tu (void **slot, void *info)
12239{
12240 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12241 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12242 ULONGEST signature = dwo_unit->signature;
12243 struct signatured_type *sig_type =
12244 lookup_dwo_signatured_type (per_cu->cu, signature);
12245
12246 if (sig_type != NULL)
12247 {
12248 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12249
12250 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12251 a real dependency of PER_CU on SIG_TYPE. That is detected later
12252 while processing PER_CU. */
12253 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12254 load_full_type_unit (sig_cu);
12255 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
12256 }
12257
12258 return 1;
12259}
12260
12261/* Queue all TUs contained in the DWO of PER_CU to be read in.
12262 The DWO may have the only definition of the type, though it may not be
12263 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12264 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12265
12266static void
12267queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12268{
12269 struct dwo_unit *dwo_unit;
12270 struct dwo_file *dwo_file;
12271
12272 gdb_assert (!per_cu->is_debug_types);
12273 gdb_assert (get_dwp_file () == NULL);
12274 gdb_assert (per_cu->cu != NULL);
12275
12276 dwo_unit = per_cu->cu->dwo_unit;
12277 gdb_assert (dwo_unit != NULL);
12278
12279 dwo_file = dwo_unit->dwo_file;
12280 if (dwo_file->tus != NULL)
12281 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
12282}
12283
3019eac3
DE
12284/* Free all resources associated with DWO_FILE.
12285 Close the DWO file and munmap the sections.
12286 All memory should be on the objfile obstack. */
348e048f
DE
12287
12288static void
3019eac3 12289free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 12290{
348e048f 12291
5c6fa7ab 12292 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 12293 gdb_bfd_unref (dwo_file->dbfd);
348e048f 12294
3019eac3
DE
12295 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
12296}
348e048f 12297
3019eac3 12298/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 12299
3019eac3
DE
12300static void
12301free_dwo_file_cleanup (void *arg)
12302{
12303 struct dwo_file *dwo_file = (struct dwo_file *) arg;
12304 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 12305
3019eac3
DE
12306 free_dwo_file (dwo_file, objfile);
12307}
348e048f 12308
3019eac3 12309/* Traversal function for free_dwo_files. */
2ab95328 12310
3019eac3
DE
12311static int
12312free_dwo_file_from_slot (void **slot, void *info)
12313{
12314 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
12315 struct objfile *objfile = (struct objfile *) info;
348e048f 12316
3019eac3 12317 free_dwo_file (dwo_file, objfile);
348e048f 12318
3019eac3
DE
12319 return 1;
12320}
348e048f 12321
3019eac3 12322/* Free all resources associated with DWO_FILES. */
348e048f 12323
3019eac3
DE
12324static void
12325free_dwo_files (htab_t dwo_files, struct objfile *objfile)
12326{
12327 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 12328}
3019eac3
DE
12329\f
12330/* Read in various DIEs. */
348e048f 12331
d389af10 12332/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12333 Inherit only the children of the DW_AT_abstract_origin DIE not being
12334 already referenced by DW_AT_abstract_origin from the children of the
12335 current DIE. */
d389af10
JK
12336
12337static void
12338inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12339{
12340 struct die_info *child_die;
791afaa2 12341 sect_offset *offsetp;
d389af10
JK
12342 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12343 struct die_info *origin_die;
12344 /* Iterator of the ORIGIN_DIE children. */
12345 struct die_info *origin_child_die;
d389af10 12346 struct attribute *attr;
cd02d79d
PA
12347 struct dwarf2_cu *origin_cu;
12348 struct pending **origin_previous_list_in_scope;
d389af10
JK
12349
12350 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12351 if (!attr)
12352 return;
12353
cd02d79d
PA
12354 /* Note that following die references may follow to a die in a
12355 different cu. */
12356
12357 origin_cu = cu;
12358 origin_die = follow_die_ref (die, attr, &origin_cu);
12359
12360 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12361 symbols in. */
12362 origin_previous_list_in_scope = origin_cu->list_in_scope;
12363 origin_cu->list_in_scope = cu->list_in_scope;
12364
edb3359d
DJ
12365 if (die->tag != origin_die->tag
12366 && !(die->tag == DW_TAG_inlined_subroutine
12367 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
12368 complaint (&symfile_complaints,
12369 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9c541725
PA
12370 to_underlying (die->sect_off),
12371 to_underlying (origin_die->sect_off));
d389af10 12372
791afaa2 12373 std::vector<sect_offset> offsets;
d389af10 12374
3ea89b92
PMR
12375 for (child_die = die->child;
12376 child_die && child_die->tag;
12377 child_die = sibling_die (child_die))
12378 {
12379 struct die_info *child_origin_die;
12380 struct dwarf2_cu *child_origin_cu;
12381
12382 /* We are trying to process concrete instance entries:
216f72a1 12383 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12384 it's not relevant to our analysis here. i.e. detecting DIEs that are
12385 present in the abstract instance but not referenced in the concrete
12386 one. */
216f72a1
JK
12387 if (child_die->tag == DW_TAG_call_site
12388 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12389 continue;
12390
c38f313d
DJ
12391 /* For each CHILD_DIE, find the corresponding child of
12392 ORIGIN_DIE. If there is more than one layer of
12393 DW_AT_abstract_origin, follow them all; there shouldn't be,
12394 but GCC versions at least through 4.4 generate this (GCC PR
12395 40573). */
3ea89b92
PMR
12396 child_origin_die = child_die;
12397 child_origin_cu = cu;
c38f313d
DJ
12398 while (1)
12399 {
cd02d79d
PA
12400 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12401 child_origin_cu);
c38f313d
DJ
12402 if (attr == NULL)
12403 break;
cd02d79d
PA
12404 child_origin_die = follow_die_ref (child_origin_die, attr,
12405 &child_origin_cu);
c38f313d
DJ
12406 }
12407
d389af10
JK
12408 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12409 counterpart may exist. */
c38f313d 12410 if (child_origin_die != child_die)
d389af10 12411 {
edb3359d
DJ
12412 if (child_die->tag != child_origin_die->tag
12413 && !(child_die->tag == DW_TAG_inlined_subroutine
12414 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
12415 complaint (&symfile_complaints,
12416 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
12417 "different tags"),
12418 to_underlying (child_die->sect_off),
12419 to_underlying (child_origin_die->sect_off));
c38f313d
DJ
12420 if (child_origin_die->parent != origin_die)
12421 complaint (&symfile_complaints,
12422 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
12423 "different parents"),
12424 to_underlying (child_die->sect_off),
12425 to_underlying (child_origin_die->sect_off));
c38f313d 12426 else
791afaa2 12427 offsets.push_back (child_origin_die->sect_off);
d389af10 12428 }
d389af10 12429 }
791afaa2
TT
12430 std::sort (offsets.begin (), offsets.end ());
12431 sect_offset *offsets_end = offsets.data () + offsets.size ();
12432 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12433 if (offsetp[-1] == *offsetp)
3e43a32a
MS
12434 complaint (&symfile_complaints,
12435 _("Multiple children of DIE 0x%x refer "
12436 "to DIE 0x%x as their abstract origin"),
9c541725 12437 to_underlying (die->sect_off), to_underlying (*offsetp));
d389af10 12438
791afaa2 12439 offsetp = offsets.data ();
d389af10
JK
12440 origin_child_die = origin_die->child;
12441 while (origin_child_die && origin_child_die->tag)
12442 {
12443 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12444 while (offsetp < offsets_end
9c541725 12445 && *offsetp < origin_child_die->sect_off)
d389af10 12446 offsetp++;
b64f50a1 12447 if (offsetp >= offsets_end
9c541725 12448 || *offsetp > origin_child_die->sect_off)
d389af10 12449 {
adde2bff
DE
12450 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12451 Check whether we're already processing ORIGIN_CHILD_DIE.
12452 This can happen with mutually referenced abstract_origins.
12453 PR 16581. */
12454 if (!origin_child_die->in_process)
12455 process_die (origin_child_die, origin_cu);
d389af10
JK
12456 }
12457 origin_child_die = sibling_die (origin_child_die);
12458 }
cd02d79d 12459 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
12460}
12461
c906108c 12462static void
e7c27a73 12463read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12464{
e7c27a73 12465 struct objfile *objfile = cu->objfile;
3e29f34a 12466 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 12467 struct context_stack *newobj;
c906108c
SS
12468 CORE_ADDR lowpc;
12469 CORE_ADDR highpc;
12470 struct die_info *child_die;
edb3359d 12471 struct attribute *attr, *call_line, *call_file;
15d034d0 12472 const char *name;
e142c38c 12473 CORE_ADDR baseaddr;
801e3a5b 12474 struct block *block;
edb3359d 12475 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12476 std::vector<struct symbol *> template_args;
34eaf542 12477 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12478
12479 if (inlined_func)
12480 {
12481 /* If we do not have call site information, we can't show the
12482 caller of this inlined function. That's too confusing, so
12483 only use the scope for local variables. */
12484 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12485 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12486 if (call_line == NULL || call_file == NULL)
12487 {
12488 read_lexical_block_scope (die, cu);
12489 return;
12490 }
12491 }
c906108c 12492
e142c38c
DJ
12493 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12494
94af9270 12495 name = dwarf2_name (die, cu);
c906108c 12496
e8d05480
JB
12497 /* Ignore functions with missing or empty names. These are actually
12498 illegal according to the DWARF standard. */
12499 if (name == NULL)
12500 {
12501 complaint (&symfile_complaints,
b64f50a1 12502 _("missing name for subprogram DIE at %d"),
9c541725 12503 to_underlying (die->sect_off));
e8d05480
JB
12504 return;
12505 }
12506
12507 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12508 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12509 <= PC_BOUNDS_INVALID)
e8d05480 12510 {
ae4d0c03
PM
12511 attr = dwarf2_attr (die, DW_AT_external, cu);
12512 if (!attr || !DW_UNSND (attr))
12513 complaint (&symfile_complaints,
3e43a32a
MS
12514 _("cannot get low and high bounds "
12515 "for subprogram DIE at %d"),
9c541725 12516 to_underlying (die->sect_off));
e8d05480
JB
12517 return;
12518 }
c906108c 12519
3e29f34a
MR
12520 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12521 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12522
34eaf542
TT
12523 /* If we have any template arguments, then we must allocate a
12524 different sort of symbol. */
12525 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12526 {
12527 if (child_die->tag == DW_TAG_template_type_param
12528 || child_die->tag == DW_TAG_template_value_param)
12529 {
e623cf5d 12530 templ_func = allocate_template_symbol (objfile);
cf724bc9 12531 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
12532 break;
12533 }
12534 }
12535
fe978cb0
PA
12536 newobj = push_context (0, lowpc);
12537 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 12538 (struct symbol *) templ_func);
4c2df51b 12539
4cecd739
DJ
12540 /* If there is a location expression for DW_AT_frame_base, record
12541 it. */
e142c38c 12542 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 12543 if (attr)
fe978cb0 12544 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 12545
63e43d3a
PMR
12546 /* If there is a location for the static link, record it. */
12547 newobj->static_link = NULL;
12548 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12549 if (attr)
12550 {
224c3ddb
SM
12551 newobj->static_link
12552 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
12553 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
12554 }
12555
e142c38c 12556 cu->list_in_scope = &local_symbols;
c906108c 12557
639d11d3 12558 if (die->child != NULL)
c906108c 12559 {
639d11d3 12560 child_die = die->child;
c906108c
SS
12561 while (child_die && child_die->tag)
12562 {
34eaf542
TT
12563 if (child_die->tag == DW_TAG_template_type_param
12564 || child_die->tag == DW_TAG_template_value_param)
12565 {
12566 struct symbol *arg = new_symbol (child_die, NULL, cu);
12567
f1078f66 12568 if (arg != NULL)
2f4732b0 12569 template_args.push_back (arg);
34eaf542
TT
12570 }
12571 else
12572 process_die (child_die, cu);
c906108c
SS
12573 child_die = sibling_die (child_die);
12574 }
12575 }
12576
d389af10
JK
12577 inherit_abstract_dies (die, cu);
12578
4a811a97
UW
12579 /* If we have a DW_AT_specification, we might need to import using
12580 directives from the context of the specification DIE. See the
12581 comment in determine_prefix. */
12582 if (cu->language == language_cplus
12583 && dwarf2_attr (die, DW_AT_specification, cu))
12584 {
12585 struct dwarf2_cu *spec_cu = cu;
12586 struct die_info *spec_die = die_specification (die, &spec_cu);
12587
12588 while (spec_die)
12589 {
12590 child_die = spec_die->child;
12591 while (child_die && child_die->tag)
12592 {
12593 if (child_die->tag == DW_TAG_imported_module)
12594 process_die (child_die, spec_cu);
12595 child_die = sibling_die (child_die);
12596 }
12597
12598 /* In some cases, GCC generates specification DIEs that
12599 themselves contain DW_AT_specification attributes. */
12600 spec_die = die_specification (spec_die, &spec_cu);
12601 }
12602 }
12603
fe978cb0 12604 newobj = pop_context ();
c906108c 12605 /* Make a block for the local symbols within. */
fe978cb0 12606 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 12607 newobj->static_link, lowpc, highpc);
801e3a5b 12608
df8a16a1 12609 /* For C++, set the block's scope. */
45280282
IB
12610 if ((cu->language == language_cplus
12611 || cu->language == language_fortran
c44af4eb
TT
12612 || cu->language == language_d
12613 || cu->language == language_rust)
4d4ec4e5 12614 && cu->processing_has_namespace_info)
195a3f6c
TT
12615 block_set_scope (block, determine_prefix (die, cu),
12616 &objfile->objfile_obstack);
df8a16a1 12617
801e3a5b
JB
12618 /* If we have address ranges, record them. */
12619 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 12620
fe978cb0 12621 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 12622
34eaf542 12623 /* Attach template arguments to function. */
2f4732b0 12624 if (!template_args.empty ())
34eaf542
TT
12625 {
12626 gdb_assert (templ_func != NULL);
12627
2f4732b0 12628 templ_func->n_template_arguments = template_args.size ();
34eaf542 12629 templ_func->template_arguments
8d749320
SM
12630 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12631 templ_func->n_template_arguments);
34eaf542 12632 memcpy (templ_func->template_arguments,
2f4732b0 12633 template_args.data (),
34eaf542 12634 (templ_func->n_template_arguments * sizeof (struct symbol *)));
34eaf542
TT
12635 }
12636
208d8187
JB
12637 /* In C++, we can have functions nested inside functions (e.g., when
12638 a function declares a class that has methods). This means that
12639 when we finish processing a function scope, we may need to go
12640 back to building a containing block's symbol lists. */
fe978cb0 12641 local_symbols = newobj->locals;
22cee43f 12642 local_using_directives = newobj->local_using_directives;
208d8187 12643
921e78cf
JB
12644 /* If we've finished processing a top-level function, subsequent
12645 symbols go in the file symbol list. */
12646 if (outermost_context_p ())
e142c38c 12647 cu->list_in_scope = &file_symbols;
c906108c
SS
12648}
12649
12650/* Process all the DIES contained within a lexical block scope. Start
12651 a new scope, process the dies, and then close the scope. */
12652
12653static void
e7c27a73 12654read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12655{
e7c27a73 12656 struct objfile *objfile = cu->objfile;
3e29f34a 12657 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 12658 struct context_stack *newobj;
c906108c
SS
12659 CORE_ADDR lowpc, highpc;
12660 struct die_info *child_die;
e142c38c
DJ
12661 CORE_ADDR baseaddr;
12662
12663 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
12664
12665 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
12666 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12667 as multiple lexical blocks? Handling children in a sane way would
6e70227d 12668 be nasty. Might be easier to properly extend generic blocks to
af34e669 12669 describe ranges. */
e385593e
JK
12670 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
12671 {
12672 case PC_BOUNDS_NOT_PRESENT:
12673 /* DW_TAG_lexical_block has no attributes, process its children as if
12674 there was no wrapping by that DW_TAG_lexical_block.
12675 GCC does no longer produces such DWARF since GCC r224161. */
12676 for (child_die = die->child;
12677 child_die != NULL && child_die->tag;
12678 child_die = sibling_die (child_die))
12679 process_die (child_die, cu);
12680 return;
12681 case PC_BOUNDS_INVALID:
12682 return;
12683 }
3e29f34a
MR
12684 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12685 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
12686
12687 push_context (0, lowpc);
639d11d3 12688 if (die->child != NULL)
c906108c 12689 {
639d11d3 12690 child_die = die->child;
c906108c
SS
12691 while (child_die && child_die->tag)
12692 {
e7c27a73 12693 process_die (child_die, cu);
c906108c
SS
12694 child_die = sibling_die (child_die);
12695 }
12696 }
3ea89b92 12697 inherit_abstract_dies (die, cu);
fe978cb0 12698 newobj = pop_context ();
c906108c 12699
22cee43f 12700 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 12701 {
801e3a5b 12702 struct block *block
63e43d3a 12703 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 12704 newobj->start_addr, highpc);
801e3a5b
JB
12705
12706 /* Note that recording ranges after traversing children, as we
12707 do here, means that recording a parent's ranges entails
12708 walking across all its children's ranges as they appear in
12709 the address map, which is quadratic behavior.
12710
12711 It would be nicer to record the parent's ranges before
12712 traversing its children, simply overriding whatever you find
12713 there. But since we don't even decide whether to create a
12714 block until after we've traversed its children, that's hard
12715 to do. */
12716 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 12717 }
fe978cb0 12718 local_symbols = newobj->locals;
22cee43f 12719 local_using_directives = newobj->local_using_directives;
c906108c
SS
12720}
12721
216f72a1 12722/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
12723
12724static void
12725read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
12726{
12727 struct objfile *objfile = cu->objfile;
12728 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12729 CORE_ADDR pc, baseaddr;
12730 struct attribute *attr;
12731 struct call_site *call_site, call_site_local;
12732 void **slot;
12733 int nparams;
12734 struct die_info *child_die;
12735
12736 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12737
216f72a1
JK
12738 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
12739 if (attr == NULL)
12740 {
12741 /* This was a pre-DWARF-5 GNU extension alias
12742 for DW_AT_call_return_pc. */
12743 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12744 }
96408a79
SA
12745 if (!attr)
12746 {
12747 complaint (&symfile_complaints,
216f72a1 12748 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 12749 "DIE 0x%x [in module %s]"),
9c541725 12750 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12751 return;
12752 }
31aa7e4e 12753 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 12754 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
12755
12756 if (cu->call_site_htab == NULL)
12757 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
12758 NULL, &objfile->objfile_obstack,
12759 hashtab_obstack_allocate, NULL);
12760 call_site_local.pc = pc;
12761 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
12762 if (*slot != NULL)
12763 {
12764 complaint (&symfile_complaints,
216f72a1 12765 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 12766 "DIE 0x%x [in module %s]"),
9c541725 12767 paddress (gdbarch, pc), to_underlying (die->sect_off),
4262abfb 12768 objfile_name (objfile));
96408a79
SA
12769 return;
12770 }
12771
12772 /* Count parameters at the caller. */
12773
12774 nparams = 0;
12775 for (child_die = die->child; child_die && child_die->tag;
12776 child_die = sibling_die (child_die))
12777 {
216f72a1
JK
12778 if (child_die->tag != DW_TAG_call_site_parameter
12779 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
12780 {
12781 complaint (&symfile_complaints,
216f72a1
JK
12782 _("Tag %d is not DW_TAG_call_site_parameter in "
12783 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 12784 child_die->tag, to_underlying (child_die->sect_off),
4262abfb 12785 objfile_name (objfile));
96408a79
SA
12786 continue;
12787 }
12788
12789 nparams++;
12790 }
12791
224c3ddb
SM
12792 call_site
12793 = ((struct call_site *)
12794 obstack_alloc (&objfile->objfile_obstack,
12795 sizeof (*call_site)
12796 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
12797 *slot = call_site;
12798 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
12799 call_site->pc = pc;
12800
216f72a1
JK
12801 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
12802 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
12803 {
12804 struct die_info *func_die;
12805
12806 /* Skip also over DW_TAG_inlined_subroutine. */
12807 for (func_die = die->parent;
12808 func_die && func_die->tag != DW_TAG_subprogram
12809 && func_die->tag != DW_TAG_subroutine_type;
12810 func_die = func_die->parent);
12811
216f72a1
JK
12812 /* DW_AT_call_all_calls is a superset
12813 of DW_AT_call_all_tail_calls. */
96408a79 12814 if (func_die
216f72a1 12815 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 12816 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 12817 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
12818 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12819 {
12820 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12821 not complete. But keep CALL_SITE for look ups via call_site_htab,
12822 both the initial caller containing the real return address PC and
12823 the final callee containing the current PC of a chain of tail
12824 calls do not need to have the tail call list complete. But any
12825 function candidate for a virtual tail call frame searched via
12826 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12827 determined unambiguously. */
12828 }
12829 else
12830 {
12831 struct type *func_type = NULL;
12832
12833 if (func_die)
12834 func_type = get_die_type (func_die, cu);
12835 if (func_type != NULL)
12836 {
12837 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
12838
12839 /* Enlist this call site to the function. */
12840 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12841 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12842 }
12843 else
12844 complaint (&symfile_complaints,
216f72a1 12845 _("Cannot find function owning DW_TAG_call_site "
96408a79 12846 "DIE 0x%x [in module %s]"),
9c541725 12847 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12848 }
12849 }
12850
216f72a1
JK
12851 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12852 if (attr == NULL)
12853 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12854 if (attr == NULL)
12855 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 12856 if (attr == NULL)
216f72a1
JK
12857 {
12858 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12859 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12860 }
96408a79
SA
12861 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
12862 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
12863 /* Keep NULL DWARF_BLOCK. */;
12864 else if (attr_form_is_block (attr))
12865 {
12866 struct dwarf2_locexpr_baton *dlbaton;
12867
8d749320 12868 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
12869 dlbaton->data = DW_BLOCK (attr)->data;
12870 dlbaton->size = DW_BLOCK (attr)->size;
12871 dlbaton->per_cu = cu->per_cu;
12872
12873 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
12874 }
7771576e 12875 else if (attr_form_is_ref (attr))
96408a79 12876 {
96408a79
SA
12877 struct dwarf2_cu *target_cu = cu;
12878 struct die_info *target_die;
12879
ac9ec31b 12880 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
12881 gdb_assert (target_cu->objfile == objfile);
12882 if (die_is_declaration (target_die, target_cu))
12883 {
7d45c7c3 12884 const char *target_physname;
9112db09
JK
12885
12886 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 12887 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 12888 if (target_physname == NULL)
9112db09 12889 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
12890 if (target_physname == NULL)
12891 complaint (&symfile_complaints,
216f72a1 12892 _("DW_AT_call_target target DIE has invalid "
96408a79 12893 "physname, for referencing DIE 0x%x [in module %s]"),
9c541725 12894 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 12895 else
7d455152 12896 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
12897 }
12898 else
12899 {
12900 CORE_ADDR lowpc;
12901
12902 /* DW_AT_entry_pc should be preferred. */
3a2b436a 12903 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 12904 <= PC_BOUNDS_INVALID)
96408a79 12905 complaint (&symfile_complaints,
216f72a1 12906 _("DW_AT_call_target target DIE has invalid "
96408a79 12907 "low pc, for referencing DIE 0x%x [in module %s]"),
9c541725 12908 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 12909 else
3e29f34a
MR
12910 {
12911 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12912 SET_FIELD_PHYSADDR (call_site->target, lowpc);
12913 }
96408a79
SA
12914 }
12915 }
12916 else
12917 complaint (&symfile_complaints,
216f72a1 12918 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 12919 "block nor reference, for DIE 0x%x [in module %s]"),
9c541725 12920 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12921
12922 call_site->per_cu = cu->per_cu;
12923
12924 for (child_die = die->child;
12925 child_die && child_die->tag;
12926 child_die = sibling_die (child_die))
12927 {
96408a79 12928 struct call_site_parameter *parameter;
1788b2d3 12929 struct attribute *loc, *origin;
96408a79 12930
216f72a1
JK
12931 if (child_die->tag != DW_TAG_call_site_parameter
12932 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
12933 {
12934 /* Already printed the complaint above. */
12935 continue;
12936 }
12937
12938 gdb_assert (call_site->parameter_count < nparams);
12939 parameter = &call_site->parameter[call_site->parameter_count];
12940
1788b2d3
JK
12941 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12942 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 12943 register is contained in DW_AT_call_value. */
96408a79 12944
24c5c679 12945 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
12946 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12947 if (origin == NULL)
12948 {
12949 /* This was a pre-DWARF-5 GNU extension alias
12950 for DW_AT_call_parameter. */
12951 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12952 }
7771576e 12953 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 12954 {
1788b2d3 12955 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
12956
12957 sect_offset sect_off
12958 = (sect_offset) dwarf2_get_ref_die_offset (origin);
12959 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
12960 {
12961 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12962 binding can be done only inside one CU. Such referenced DIE
12963 therefore cannot be even moved to DW_TAG_partial_unit. */
12964 complaint (&symfile_complaints,
216f72a1
JK
12965 _("DW_AT_call_parameter offset is not in CU for "
12966 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12967 to_underlying (child_die->sect_off),
12968 objfile_name (objfile));
d76b7dbc
JK
12969 continue;
12970 }
9c541725
PA
12971 parameter->u.param_cu_off
12972 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
12973 }
12974 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
12975 {
12976 complaint (&symfile_complaints,
12977 _("No DW_FORM_block* DW_AT_location for "
216f72a1 12978 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 12979 to_underlying (child_die->sect_off), objfile_name (objfile));
96408a79
SA
12980 continue;
12981 }
24c5c679 12982 else
96408a79 12983 {
24c5c679
JK
12984 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12985 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12986 if (parameter->u.dwarf_reg != -1)
12987 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12988 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12989 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12990 &parameter->u.fb_offset))
12991 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12992 else
12993 {
12994 complaint (&symfile_complaints,
12995 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12996 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 12997 "DW_TAG_call_site child DIE 0x%x "
24c5c679 12998 "[in module %s]"),
9c541725
PA
12999 to_underlying (child_die->sect_off),
13000 objfile_name (objfile));
24c5c679
JK
13001 continue;
13002 }
96408a79
SA
13003 }
13004
216f72a1
JK
13005 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13006 if (attr == NULL)
13007 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
13008 if (!attr_form_is_block (attr))
13009 {
13010 complaint (&symfile_complaints,
216f72a1
JK
13011 _("No DW_FORM_block* DW_AT_call_value for "
13012 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
13013 to_underlying (child_die->sect_off),
13014 objfile_name (objfile));
96408a79
SA
13015 continue;
13016 }
13017 parameter->value = DW_BLOCK (attr)->data;
13018 parameter->value_size = DW_BLOCK (attr)->size;
13019
13020 /* Parameters are not pre-cleared by memset above. */
13021 parameter->data_value = NULL;
13022 parameter->data_value_size = 0;
13023 call_site->parameter_count++;
13024
216f72a1
JK
13025 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13026 if (attr == NULL)
13027 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
13028 if (attr)
13029 {
13030 if (!attr_form_is_block (attr))
13031 complaint (&symfile_complaints,
216f72a1
JK
13032 _("No DW_FORM_block* DW_AT_call_data_value for "
13033 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
13034 to_underlying (child_die->sect_off),
13035 objfile_name (objfile));
96408a79
SA
13036 else
13037 {
13038 parameter->data_value = DW_BLOCK (attr)->data;
13039 parameter->data_value_size = DW_BLOCK (attr)->size;
13040 }
13041 }
13042 }
13043}
13044
71a3c369
TT
13045/* Helper function for read_variable. If DIE represents a virtual
13046 table, then return the type of the concrete object that is
13047 associated with the virtual table. Otherwise, return NULL. */
13048
13049static struct type *
13050rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13051{
13052 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13053 if (attr == NULL)
13054 return NULL;
13055
13056 /* Find the type DIE. */
13057 struct die_info *type_die = NULL;
13058 struct dwarf2_cu *type_cu = cu;
13059
13060 if (attr_form_is_ref (attr))
13061 type_die = follow_die_ref (die, attr, &type_cu);
13062 if (type_die == NULL)
13063 return NULL;
13064
13065 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13066 return NULL;
13067 return die_containing_type (type_die, type_cu);
13068}
13069
13070/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13071
13072static void
13073read_variable (struct die_info *die, struct dwarf2_cu *cu)
13074{
13075 struct rust_vtable_symbol *storage = NULL;
13076
13077 if (cu->language == language_rust)
13078 {
13079 struct type *containing_type = rust_containing_type (die, cu);
13080
13081 if (containing_type != NULL)
13082 {
13083 struct objfile *objfile = cu->objfile;
13084
13085 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
13086 struct rust_vtable_symbol);
13087 initialize_objfile_symbol (storage);
13088 storage->concrete_type = containing_type;
cf724bc9 13089 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13090 }
13091 }
13092
13093 new_symbol_full (die, NULL, cu, storage);
13094}
13095
43988095
JK
13096/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13097 reading .debug_rnglists.
13098 Callback's type should be:
13099 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13100 Return true if the attributes are present and valid, otherwise,
13101 return false. */
13102
13103template <typename Callback>
13104static bool
13105dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13106 Callback &&callback)
13107{
13108 struct objfile *objfile = cu->objfile;
43988095 13109 bfd *obfd = objfile->obfd;
43988095
JK
13110 /* Base address selection entry. */
13111 CORE_ADDR base;
13112 int found_base;
43988095 13113 const gdb_byte *buffer;
43988095
JK
13114 CORE_ADDR baseaddr;
13115 bool overflow = false;
13116
13117 found_base = cu->base_known;
13118 base = cu->base_address;
13119
13120 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
13121 if (offset >= dwarf2_per_objfile->rnglists.size)
13122 {
13123 complaint (&symfile_complaints,
13124 _("Offset %d out of bounds for DW_AT_ranges attribute"),
13125 offset);
13126 return false;
13127 }
13128 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13129
13130 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13131
13132 while (1)
13133 {
7814882a
JK
13134 /* Initialize it due to a false compiler warning. */
13135 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13136 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13137 + dwarf2_per_objfile->rnglists.size);
13138 unsigned int bytes_read;
13139
13140 if (buffer == buf_end)
13141 {
13142 overflow = true;
13143 break;
13144 }
13145 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13146 switch (rlet)
13147 {
13148 case DW_RLE_end_of_list:
13149 break;
13150 case DW_RLE_base_address:
13151 if (buffer + cu->header.addr_size > buf_end)
13152 {
13153 overflow = true;
13154 break;
13155 }
13156 base = read_address (obfd, buffer, cu, &bytes_read);
13157 found_base = 1;
13158 buffer += bytes_read;
13159 break;
13160 case DW_RLE_start_length:
13161 if (buffer + cu->header.addr_size > buf_end)
13162 {
13163 overflow = true;
13164 break;
13165 }
13166 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13167 buffer += bytes_read;
13168 range_end = (range_beginning
13169 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13170 buffer += bytes_read;
13171 if (buffer > buf_end)
13172 {
13173 overflow = true;
13174 break;
13175 }
13176 break;
13177 case DW_RLE_offset_pair:
13178 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13179 buffer += bytes_read;
13180 if (buffer > buf_end)
13181 {
13182 overflow = true;
13183 break;
13184 }
13185 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13186 buffer += bytes_read;
13187 if (buffer > buf_end)
13188 {
13189 overflow = true;
13190 break;
13191 }
13192 break;
13193 case DW_RLE_start_end:
13194 if (buffer + 2 * cu->header.addr_size > buf_end)
13195 {
13196 overflow = true;
13197 break;
13198 }
13199 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13200 buffer += bytes_read;
13201 range_end = read_address (obfd, buffer, cu, &bytes_read);
13202 buffer += bytes_read;
13203 break;
13204 default:
13205 complaint (&symfile_complaints,
13206 _("Invalid .debug_rnglists data (no base address)"));
13207 return false;
13208 }
13209 if (rlet == DW_RLE_end_of_list || overflow)
13210 break;
13211 if (rlet == DW_RLE_base_address)
13212 continue;
13213
13214 if (!found_base)
13215 {
13216 /* We have no valid base address for the ranges
13217 data. */
13218 complaint (&symfile_complaints,
13219 _("Invalid .debug_rnglists data (no base address)"));
13220 return false;
13221 }
13222
13223 if (range_beginning > range_end)
13224 {
13225 /* Inverted range entries are invalid. */
13226 complaint (&symfile_complaints,
13227 _("Invalid .debug_rnglists data (inverted range)"));
13228 return false;
13229 }
13230
13231 /* Empty range entries have no effect. */
13232 if (range_beginning == range_end)
13233 continue;
13234
13235 range_beginning += base;
13236 range_end += base;
13237
13238 /* A not-uncommon case of bad debug info.
13239 Don't pollute the addrmap with bad data. */
13240 if (range_beginning + baseaddr == 0
13241 && !dwarf2_per_objfile->has_section_at_zero)
13242 {
13243 complaint (&symfile_complaints,
13244 _(".debug_rnglists entry has start address of zero"
13245 " [in module %s]"), objfile_name (objfile));
13246 continue;
13247 }
13248
13249 callback (range_beginning, range_end);
13250 }
13251
13252 if (overflow)
13253 {
13254 complaint (&symfile_complaints,
13255 _("Offset %d is not terminated "
13256 "for DW_AT_ranges attribute"),
13257 offset);
13258 return false;
13259 }
13260
13261 return true;
13262}
13263
13264/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13265 Callback's type should be:
13266 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13267 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13268
43988095 13269template <typename Callback>
43039443 13270static int
5f46c5a5 13271dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13272 Callback &&callback)
43039443
JK
13273{
13274 struct objfile *objfile = cu->objfile;
13275 struct comp_unit_head *cu_header = &cu->header;
13276 bfd *obfd = objfile->obfd;
13277 unsigned int addr_size = cu_header->addr_size;
13278 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13279 /* Base address selection entry. */
13280 CORE_ADDR base;
13281 int found_base;
13282 unsigned int dummy;
d521ce57 13283 const gdb_byte *buffer;
ff013f42 13284 CORE_ADDR baseaddr;
43039443 13285
43988095
JK
13286 if (cu_header->version >= 5)
13287 return dwarf2_rnglists_process (offset, cu, callback);
13288
d00adf39
DE
13289 found_base = cu->base_known;
13290 base = cu->base_address;
43039443 13291
be391dca 13292 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 13293 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
13294 {
13295 complaint (&symfile_complaints,
13296 _("Offset %d out of bounds for DW_AT_ranges attribute"),
13297 offset);
13298 return 0;
13299 }
dce234bc 13300 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 13301
e7030f15 13302 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 13303
43039443
JK
13304 while (1)
13305 {
13306 CORE_ADDR range_beginning, range_end;
13307
13308 range_beginning = read_address (obfd, buffer, cu, &dummy);
13309 buffer += addr_size;
13310 range_end = read_address (obfd, buffer, cu, &dummy);
13311 buffer += addr_size;
13312 offset += 2 * addr_size;
13313
13314 /* An end of list marker is a pair of zero addresses. */
13315 if (range_beginning == 0 && range_end == 0)
13316 /* Found the end of list entry. */
13317 break;
13318
13319 /* Each base address selection entry is a pair of 2 values.
13320 The first is the largest possible address, the second is
13321 the base address. Check for a base address here. */
13322 if ((range_beginning & mask) == mask)
13323 {
28d2bfb9
AB
13324 /* If we found the largest possible address, then we already
13325 have the base address in range_end. */
13326 base = range_end;
43039443
JK
13327 found_base = 1;
13328 continue;
13329 }
13330
13331 if (!found_base)
13332 {
13333 /* We have no valid base address for the ranges
13334 data. */
13335 complaint (&symfile_complaints,
13336 _("Invalid .debug_ranges data (no base address)"));
13337 return 0;
13338 }
13339
9277c30c
UW
13340 if (range_beginning > range_end)
13341 {
13342 /* Inverted range entries are invalid. */
13343 complaint (&symfile_complaints,
13344 _("Invalid .debug_ranges data (inverted range)"));
13345 return 0;
13346 }
13347
13348 /* Empty range entries have no effect. */
13349 if (range_beginning == range_end)
13350 continue;
13351
43039443
JK
13352 range_beginning += base;
13353 range_end += base;
13354
01093045
DE
13355 /* A not-uncommon case of bad debug info.
13356 Don't pollute the addrmap with bad data. */
13357 if (range_beginning + baseaddr == 0
13358 && !dwarf2_per_objfile->has_section_at_zero)
13359 {
13360 complaint (&symfile_complaints,
13361 _(".debug_ranges entry has start address of zero"
4262abfb 13362 " [in module %s]"), objfile_name (objfile));
01093045
DE
13363 continue;
13364 }
13365
5f46c5a5
JK
13366 callback (range_beginning, range_end);
13367 }
13368
13369 return 1;
13370}
13371
13372/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13373 Return 1 if the attributes are present and valid, otherwise, return 0.
13374 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13375
13376static int
13377dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13378 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13379 struct partial_symtab *ranges_pst)
13380{
13381 struct objfile *objfile = cu->objfile;
13382 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13383 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
13384 SECT_OFF_TEXT (objfile));
13385 int low_set = 0;
13386 CORE_ADDR low = 0;
13387 CORE_ADDR high = 0;
13388 int retval;
13389
13390 retval = dwarf2_ranges_process (offset, cu,
13391 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13392 {
9277c30c 13393 if (ranges_pst != NULL)
3e29f34a
MR
13394 {
13395 CORE_ADDR lowpc;
13396 CORE_ADDR highpc;
13397
13398 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
13399 range_beginning + baseaddr);
13400 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
13401 range_end + baseaddr);
13402 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
13403 ranges_pst);
13404 }
ff013f42 13405
43039443
JK
13406 /* FIXME: This is recording everything as a low-high
13407 segment of consecutive addresses. We should have a
13408 data structure for discontiguous block ranges
13409 instead. */
13410 if (! low_set)
13411 {
13412 low = range_beginning;
13413 high = range_end;
13414 low_set = 1;
13415 }
13416 else
13417 {
13418 if (range_beginning < low)
13419 low = range_beginning;
13420 if (range_end > high)
13421 high = range_end;
13422 }
5f46c5a5
JK
13423 });
13424 if (!retval)
13425 return 0;
43039443
JK
13426
13427 if (! low_set)
13428 /* If the first entry is an end-of-list marker, the range
13429 describes an empty scope, i.e. no instructions. */
13430 return 0;
13431
13432 if (low_return)
13433 *low_return = low;
13434 if (high_return)
13435 *high_return = high;
13436 return 1;
13437}
13438
3a2b436a
JK
13439/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13440 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13441 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13442
3a2b436a 13443static enum pc_bounds_kind
af34e669 13444dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
13445 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13446 struct partial_symtab *pst)
c906108c
SS
13447{
13448 struct attribute *attr;
91da1414 13449 struct attribute *attr_high;
af34e669
DJ
13450 CORE_ADDR low = 0;
13451 CORE_ADDR high = 0;
e385593e 13452 enum pc_bounds_kind ret;
c906108c 13453
91da1414
MW
13454 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13455 if (attr_high)
af34e669 13456 {
e142c38c 13457 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 13458 if (attr)
91da1414 13459 {
31aa7e4e
JB
13460 low = attr_value_as_address (attr);
13461 high = attr_value_as_address (attr_high);
13462 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
13463 high += low;
91da1414 13464 }
af34e669
DJ
13465 else
13466 /* Found high w/o low attribute. */
e385593e 13467 return PC_BOUNDS_INVALID;
af34e669
DJ
13468
13469 /* Found consecutive range of addresses. */
3a2b436a 13470 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13471 }
c906108c 13472 else
af34e669 13473 {
e142c38c 13474 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
13475 if (attr != NULL)
13476 {
ab435259
DE
13477 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
13478 We take advantage of the fact that DW_AT_ranges does not appear
13479 in DW_TAG_compile_unit of DWO files. */
13480 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13481 unsigned int ranges_offset = (DW_UNSND (attr)
13482 + (need_ranges_base
13483 ? cu->ranges_base
13484 : 0));
2e3cf129 13485
af34e669 13486 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 13487 .debug_ranges section. */
2e3cf129 13488 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 13489 return PC_BOUNDS_INVALID;
43039443 13490 /* Found discontinuous range of addresses. */
3a2b436a 13491 ret = PC_BOUNDS_RANGES;
af34e669 13492 }
e385593e
JK
13493 else
13494 return PC_BOUNDS_NOT_PRESENT;
af34e669 13495 }
c906108c 13496
9373cf26
JK
13497 /* read_partial_die has also the strict LOW < HIGH requirement. */
13498 if (high <= low)
e385593e 13499 return PC_BOUNDS_INVALID;
c906108c
SS
13500
13501 /* When using the GNU linker, .gnu.linkonce. sections are used to
13502 eliminate duplicate copies of functions and vtables and such.
13503 The linker will arbitrarily choose one and discard the others.
13504 The AT_*_pc values for such functions refer to local labels in
13505 these sections. If the section from that file was discarded, the
13506 labels are not in the output, so the relocs get a value of 0.
13507 If this is a discarded function, mark the pc bounds as invalid,
13508 so that GDB will ignore it. */
72dca2f5 13509 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 13510 return PC_BOUNDS_INVALID;
c906108c
SS
13511
13512 *lowpc = low;
96408a79
SA
13513 if (highpc)
13514 *highpc = high;
af34e669 13515 return ret;
c906108c
SS
13516}
13517
b084d499
JB
13518/* Assuming that DIE represents a subprogram DIE or a lexical block, get
13519 its low and high PC addresses. Do nothing if these addresses could not
13520 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13521 and HIGHPC to the high address if greater than HIGHPC. */
13522
13523static void
13524dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13525 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13526 struct dwarf2_cu *cu)
13527{
13528 CORE_ADDR low, high;
13529 struct die_info *child = die->child;
13530
e385593e 13531 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 13532 {
325fac50
PA
13533 *lowpc = std::min (*lowpc, low);
13534 *highpc = std::max (*highpc, high);
b084d499
JB
13535 }
13536
13537 /* If the language does not allow nested subprograms (either inside
13538 subprograms or lexical blocks), we're done. */
13539 if (cu->language != language_ada)
13540 return;
6e70227d 13541
b084d499
JB
13542 /* Check all the children of the given DIE. If it contains nested
13543 subprograms, then check their pc bounds. Likewise, we need to
13544 check lexical blocks as well, as they may also contain subprogram
13545 definitions. */
13546 while (child && child->tag)
13547 {
13548 if (child->tag == DW_TAG_subprogram
13549 || child->tag == DW_TAG_lexical_block)
13550 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13551 child = sibling_die (child);
13552 }
13553}
13554
fae299cd
DC
13555/* Get the low and high pc's represented by the scope DIE, and store
13556 them in *LOWPC and *HIGHPC. If the correct values can't be
13557 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13558
13559static void
13560get_scope_pc_bounds (struct die_info *die,
13561 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13562 struct dwarf2_cu *cu)
13563{
13564 CORE_ADDR best_low = (CORE_ADDR) -1;
13565 CORE_ADDR best_high = (CORE_ADDR) 0;
13566 CORE_ADDR current_low, current_high;
13567
3a2b436a 13568 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 13569 >= PC_BOUNDS_RANGES)
fae299cd
DC
13570 {
13571 best_low = current_low;
13572 best_high = current_high;
13573 }
13574 else
13575 {
13576 struct die_info *child = die->child;
13577
13578 while (child && child->tag)
13579 {
13580 switch (child->tag) {
13581 case DW_TAG_subprogram:
b084d499 13582 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
13583 break;
13584 case DW_TAG_namespace:
f55ee35c 13585 case DW_TAG_module:
fae299cd
DC
13586 /* FIXME: carlton/2004-01-16: Should we do this for
13587 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13588 that current GCC's always emit the DIEs corresponding
13589 to definitions of methods of classes as children of a
13590 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13591 the DIEs giving the declarations, which could be
13592 anywhere). But I don't see any reason why the
13593 standards says that they have to be there. */
13594 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13595
13596 if (current_low != ((CORE_ADDR) -1))
13597 {
325fac50
PA
13598 best_low = std::min (best_low, current_low);
13599 best_high = std::max (best_high, current_high);
fae299cd
DC
13600 }
13601 break;
13602 default:
0963b4bd 13603 /* Ignore. */
fae299cd
DC
13604 break;
13605 }
13606
13607 child = sibling_die (child);
13608 }
13609 }
13610
13611 *lowpc = best_low;
13612 *highpc = best_high;
13613}
13614
801e3a5b
JB
13615/* Record the address ranges for BLOCK, offset by BASEADDR, as given
13616 in DIE. */
380bca97 13617
801e3a5b
JB
13618static void
13619dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13620 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13621{
bb5ed363 13622 struct objfile *objfile = cu->objfile;
3e29f34a 13623 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 13624 struct attribute *attr;
91da1414 13625 struct attribute *attr_high;
801e3a5b 13626
91da1414
MW
13627 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13628 if (attr_high)
801e3a5b 13629 {
801e3a5b
JB
13630 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13631 if (attr)
13632 {
31aa7e4e
JB
13633 CORE_ADDR low = attr_value_as_address (attr);
13634 CORE_ADDR high = attr_value_as_address (attr_high);
13635
13636 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
13637 high += low;
9a619af0 13638
3e29f34a
MR
13639 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13640 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13641 record_block_range (block, low, high - 1);
801e3a5b
JB
13642 }
13643 }
13644
13645 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13646 if (attr)
13647 {
ab435259
DE
13648 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
13649 We take advantage of the fact that DW_AT_ranges does not appear
13650 in DW_TAG_compile_unit of DWO files. */
13651 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
13652
13653 /* The value of the DW_AT_ranges attribute is the offset of the
13654 address range list in the .debug_ranges section. */
ab435259
DE
13655 unsigned long offset = (DW_UNSND (attr)
13656 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 13657 const gdb_byte *buffer;
801e3a5b
JB
13658
13659 /* For some target architectures, but not others, the
13660 read_address function sign-extends the addresses it returns.
13661 To recognize base address selection entries, we need a
13662 mask. */
13663 unsigned int addr_size = cu->header.addr_size;
13664 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13665
13666 /* The base address, to which the next pair is relative. Note
13667 that this 'base' is a DWARF concept: most entries in a range
13668 list are relative, to reduce the number of relocs against the
13669 debugging information. This is separate from this function's
13670 'baseaddr' argument, which GDB uses to relocate debugging
13671 information from a shared library based on the address at
13672 which the library was loaded. */
d00adf39
DE
13673 CORE_ADDR base = cu->base_address;
13674 int base_known = cu->base_known;
801e3a5b 13675
5f46c5a5
JK
13676 dwarf2_ranges_process (offset, cu,
13677 [&] (CORE_ADDR start, CORE_ADDR end)
13678 {
58fdfd2c
JK
13679 start += baseaddr;
13680 end += baseaddr;
5f46c5a5
JK
13681 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13682 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13683 record_block_range (block, start, end - 1);
13684 });
801e3a5b
JB
13685 }
13686}
13687
685b1105
JK
13688/* Check whether the producer field indicates either of GCC < 4.6, or the
13689 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 13690
685b1105
JK
13691static void
13692check_producer (struct dwarf2_cu *cu)
60d5a603 13693{
38360086 13694 int major, minor;
60d5a603
JK
13695
13696 if (cu->producer == NULL)
13697 {
13698 /* For unknown compilers expect their behavior is DWARF version
13699 compliant.
13700
13701 GCC started to support .debug_types sections by -gdwarf-4 since
13702 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13703 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13704 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13705 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 13706 }
b1ffba5a 13707 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 13708 {
38360086
MW
13709 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13710 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 13711 }
5230b05a
WT
13712 else if (producer_is_icc (cu->producer, &major, &minor))
13713 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
13714 else
13715 {
13716 /* For other non-GCC compilers, expect their behavior is DWARF version
13717 compliant. */
60d5a603
JK
13718 }
13719
ba919b58 13720 cu->checked_producer = 1;
685b1105 13721}
ba919b58 13722
685b1105
JK
13723/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13724 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13725 during 4.6.0 experimental. */
13726
13727static int
13728producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
13729{
13730 if (!cu->checked_producer)
13731 check_producer (cu);
13732
13733 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
13734}
13735
13736/* Return the default accessibility type if it is not overriden by
13737 DW_AT_accessibility. */
13738
13739static enum dwarf_access_attribute
13740dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
13741{
13742 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
13743 {
13744 /* The default DWARF 2 accessibility for members is public, the default
13745 accessibility for inheritance is private. */
13746
13747 if (die->tag != DW_TAG_inheritance)
13748 return DW_ACCESS_public;
13749 else
13750 return DW_ACCESS_private;
13751 }
13752 else
13753 {
13754 /* DWARF 3+ defines the default accessibility a different way. The same
13755 rules apply now for DW_TAG_inheritance as for the members and it only
13756 depends on the container kind. */
13757
13758 if (die->parent->tag == DW_TAG_class_type)
13759 return DW_ACCESS_private;
13760 else
13761 return DW_ACCESS_public;
13762 }
13763}
13764
74ac6d43
TT
13765/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
13766 offset. If the attribute was not found return 0, otherwise return
13767 1. If it was found but could not properly be handled, set *OFFSET
13768 to 0. */
13769
13770static int
13771handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
13772 LONGEST *offset)
13773{
13774 struct attribute *attr;
13775
13776 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13777 if (attr != NULL)
13778 {
13779 *offset = 0;
13780
13781 /* Note that we do not check for a section offset first here.
13782 This is because DW_AT_data_member_location is new in DWARF 4,
13783 so if we see it, we can assume that a constant form is really
13784 a constant and not a section offset. */
13785 if (attr_form_is_constant (attr))
13786 *offset = dwarf2_get_attr_constant_value (attr, 0);
13787 else if (attr_form_is_section_offset (attr))
13788 dwarf2_complex_location_expr_complaint ();
13789 else if (attr_form_is_block (attr))
13790 *offset = decode_locdesc (DW_BLOCK (attr), cu);
13791 else
13792 dwarf2_complex_location_expr_complaint ();
13793
13794 return 1;
13795 }
13796
13797 return 0;
13798}
13799
c906108c
SS
13800/* Add an aggregate field to the field list. */
13801
13802static void
107d2387 13803dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 13804 struct dwarf2_cu *cu)
6e70227d 13805{
e7c27a73 13806 struct objfile *objfile = cu->objfile;
5e2b427d 13807 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13808 struct nextfield *new_field;
13809 struct attribute *attr;
13810 struct field *fp;
15d034d0 13811 const char *fieldname = "";
c906108c
SS
13812
13813 /* Allocate a new field list entry and link it in. */
8d749320 13814 new_field = XNEW (struct nextfield);
b8c9b27d 13815 make_cleanup (xfree, new_field);
c906108c 13816 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
13817
13818 if (die->tag == DW_TAG_inheritance)
13819 {
13820 new_field->next = fip->baseclasses;
13821 fip->baseclasses = new_field;
13822 }
13823 else
13824 {
13825 new_field->next = fip->fields;
13826 fip->fields = new_field;
13827 }
c906108c
SS
13828 fip->nfields++;
13829
e142c38c 13830 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
13831 if (attr)
13832 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
13833 else
13834 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
13835 if (new_field->accessibility != DW_ACCESS_public)
13836 fip->non_public_fields = 1;
60d5a603 13837
e142c38c 13838 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
13839 if (attr)
13840 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
13841 else
13842 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
13843
13844 fp = &new_field->field;
a9a9bd0f 13845
e142c38c 13846 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 13847 {
74ac6d43
TT
13848 LONGEST offset;
13849
a9a9bd0f 13850 /* Data member other than a C++ static data member. */
6e70227d 13851
c906108c 13852 /* Get type of field. */
e7c27a73 13853 fp->type = die_type (die, cu);
c906108c 13854
d6a843b5 13855 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 13856
c906108c 13857 /* Get bit size of field (zero if none). */
e142c38c 13858 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
13859 if (attr)
13860 {
13861 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
13862 }
13863 else
13864 {
13865 FIELD_BITSIZE (*fp) = 0;
13866 }
13867
13868 /* Get bit offset of field. */
74ac6d43
TT
13869 if (handle_data_member_location (die, cu, &offset))
13870 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 13871 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
13872 if (attr)
13873 {
5e2b427d 13874 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
13875 {
13876 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
13877 additional bit offset from the MSB of the containing
13878 anonymous object to the MSB of the field. We don't
13879 have to do anything special since we don't need to
13880 know the size of the anonymous object. */
f41f5e61 13881 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
13882 }
13883 else
13884 {
13885 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
13886 MSB of the anonymous object, subtract off the number of
13887 bits from the MSB of the field to the MSB of the
13888 object, and then subtract off the number of bits of
13889 the field itself. The result is the bit offset of
13890 the LSB of the field. */
c906108c
SS
13891 int anonymous_size;
13892 int bit_offset = DW_UNSND (attr);
13893
e142c38c 13894 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13895 if (attr)
13896 {
13897 /* The size of the anonymous object containing
13898 the bit field is explicit, so use the
13899 indicated size (in bytes). */
13900 anonymous_size = DW_UNSND (attr);
13901 }
13902 else
13903 {
13904 /* The size of the anonymous object containing
13905 the bit field must be inferred from the type
13906 attribute of the data member containing the
13907 bit field. */
13908 anonymous_size = TYPE_LENGTH (fp->type);
13909 }
f41f5e61
PA
13910 SET_FIELD_BITPOS (*fp,
13911 (FIELD_BITPOS (*fp)
13912 + anonymous_size * bits_per_byte
13913 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
13914 }
13915 }
da5b30da
AA
13916 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13917 if (attr != NULL)
13918 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
13919 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
13920
13921 /* Get name of field. */
39cbfefa
DJ
13922 fieldname = dwarf2_name (die, cu);
13923 if (fieldname == NULL)
13924 fieldname = "";
d8151005
DJ
13925
13926 /* The name is already allocated along with this objfile, so we don't
13927 need to duplicate it for the type. */
13928 fp->name = fieldname;
c906108c
SS
13929
13930 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 13931 pointer or virtual base class pointer) to private. */
e142c38c 13932 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 13933 {
d48cc9dd 13934 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
13935 new_field->accessibility = DW_ACCESS_private;
13936 fip->non_public_fields = 1;
13937 }
13938 }
a9a9bd0f 13939 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 13940 {
a9a9bd0f
DC
13941 /* C++ static member. */
13942
13943 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13944 is a declaration, but all versions of G++ as of this writing
13945 (so through at least 3.2.1) incorrectly generate
13946 DW_TAG_variable tags. */
6e70227d 13947
ff355380 13948 const char *physname;
c906108c 13949
a9a9bd0f 13950 /* Get name of field. */
39cbfefa
DJ
13951 fieldname = dwarf2_name (die, cu);
13952 if (fieldname == NULL)
c906108c
SS
13953 return;
13954
254e6b9e 13955 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
13956 if (attr
13957 /* Only create a symbol if this is an external value.
13958 new_symbol checks this and puts the value in the global symbol
13959 table, which we want. If it is not external, new_symbol
13960 will try to put the value in cu->list_in_scope which is wrong. */
13961 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
13962 {
13963 /* A static const member, not much different than an enum as far as
13964 we're concerned, except that we can support more types. */
13965 new_symbol (die, NULL, cu);
13966 }
13967
2df3850c 13968 /* Get physical name. */
ff355380 13969 physname = dwarf2_physname (fieldname, die, cu);
c906108c 13970
d8151005
DJ
13971 /* The name is already allocated along with this objfile, so we don't
13972 need to duplicate it for the type. */
13973 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 13974 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 13975 FIELD_NAME (*fp) = fieldname;
c906108c
SS
13976 }
13977 else if (die->tag == DW_TAG_inheritance)
13978 {
74ac6d43 13979 LONGEST offset;
d4b96c9a 13980
74ac6d43
TT
13981 /* C++ base class field. */
13982 if (handle_data_member_location (die, cu, &offset))
13983 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 13984 FIELD_BITSIZE (*fp) = 0;
e7c27a73 13985 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
13986 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
13987 fip->nbaseclasses++;
13988 }
13989}
13990
98751a41
JK
13991/* Add a typedef defined in the scope of the FIP's class. */
13992
13993static void
13994dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
13995 struct dwarf2_cu *cu)
6e70227d 13996{
98751a41 13997 struct typedef_field_list *new_field;
98751a41 13998 struct typedef_field *fp;
98751a41
JK
13999
14000 /* Allocate a new field list entry and link it in. */
8d749320 14001 new_field = XCNEW (struct typedef_field_list);
98751a41
JK
14002 make_cleanup (xfree, new_field);
14003
14004 gdb_assert (die->tag == DW_TAG_typedef);
14005
14006 fp = &new_field->field;
14007
14008 /* Get name of field. */
14009 fp->name = dwarf2_name (die, cu);
14010 if (fp->name == NULL)
14011 return;
14012
14013 fp->type = read_type_die (die, cu);
14014
c191a687
KS
14015 /* Save accessibility. */
14016 enum dwarf_access_attribute accessibility;
14017 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14018 if (attr != NULL)
14019 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14020 else
14021 accessibility = dwarf2_default_access_attribute (die, cu);
14022 switch (accessibility)
14023 {
14024 case DW_ACCESS_public:
14025 /* The assumed value if neither private nor protected. */
14026 break;
14027 case DW_ACCESS_private:
14028 fp->is_private = 1;
14029 break;
14030 case DW_ACCESS_protected:
14031 fp->is_protected = 1;
14032 break;
14033 default:
37534686
KS
14034 complaint (&symfile_complaints,
14035 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14036 }
14037
98751a41
JK
14038 new_field->next = fip->typedef_field_list;
14039 fip->typedef_field_list = new_field;
14040 fip->typedef_field_list_count++;
14041}
14042
c906108c
SS
14043/* Create the vector of fields, and attach it to the type. */
14044
14045static void
fba45db2 14046dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14047 struct dwarf2_cu *cu)
c906108c
SS
14048{
14049 int nfields = fip->nfields;
14050
14051 /* Record the field count, allocate space for the array of fields,
14052 and create blank accessibility bitfields if necessary. */
14053 TYPE_NFIELDS (type) = nfields;
14054 TYPE_FIELDS (type) = (struct field *)
14055 TYPE_ALLOC (type, sizeof (struct field) * nfields);
14056 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
14057
b4ba55a1 14058 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14059 {
14060 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14061
14062 TYPE_FIELD_PRIVATE_BITS (type) =
14063 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14064 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14065
14066 TYPE_FIELD_PROTECTED_BITS (type) =
14067 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14068 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14069
774b6a14
TT
14070 TYPE_FIELD_IGNORE_BITS (type) =
14071 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14072 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14073 }
14074
14075 /* If the type has baseclasses, allocate and clear a bit vector for
14076 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 14077 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
14078 {
14079 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 14080 unsigned char *pointer;
c906108c
SS
14081
14082 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14083 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14084 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
14085 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
14086 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
14087 }
14088
3e43a32a
MS
14089 /* Copy the saved-up fields into the field vector. Start from the head of
14090 the list, adding to the tail of the field array, so that they end up in
14091 the same order in the array in which they were added to the list. */
c906108c
SS
14092 while (nfields-- > 0)
14093 {
7d0ccb61
DJ
14094 struct nextfield *fieldp;
14095
14096 if (fip->fields)
14097 {
14098 fieldp = fip->fields;
14099 fip->fields = fieldp->next;
14100 }
14101 else
14102 {
14103 fieldp = fip->baseclasses;
14104 fip->baseclasses = fieldp->next;
14105 }
14106
14107 TYPE_FIELD (type, nfields) = fieldp->field;
14108 switch (fieldp->accessibility)
c906108c 14109 {
c5aa993b 14110 case DW_ACCESS_private:
b4ba55a1
JB
14111 if (cu->language != language_ada)
14112 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 14113 break;
c906108c 14114
c5aa993b 14115 case DW_ACCESS_protected:
b4ba55a1
JB
14116 if (cu->language != language_ada)
14117 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 14118 break;
c906108c 14119
c5aa993b
JM
14120 case DW_ACCESS_public:
14121 break;
c906108c 14122
c5aa993b
JM
14123 default:
14124 /* Unknown accessibility. Complain and treat it as public. */
14125 {
e2e0b3e5 14126 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 14127 fieldp->accessibility);
c5aa993b
JM
14128 }
14129 break;
c906108c
SS
14130 }
14131 if (nfields < fip->nbaseclasses)
14132 {
7d0ccb61 14133 switch (fieldp->virtuality)
c906108c 14134 {
c5aa993b
JM
14135 case DW_VIRTUALITY_virtual:
14136 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14137 if (cu->language == language_ada)
a73c6dcd 14138 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
14139 SET_TYPE_FIELD_VIRTUAL (type, nfields);
14140 break;
c906108c
SS
14141 }
14142 }
c906108c
SS
14143 }
14144}
14145
7d27a96d
TT
14146/* Return true if this member function is a constructor, false
14147 otherwise. */
14148
14149static int
14150dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14151{
14152 const char *fieldname;
fe978cb0 14153 const char *type_name;
7d27a96d
TT
14154 int len;
14155
14156 if (die->parent == NULL)
14157 return 0;
14158
14159 if (die->parent->tag != DW_TAG_structure_type
14160 && die->parent->tag != DW_TAG_union_type
14161 && die->parent->tag != DW_TAG_class_type)
14162 return 0;
14163
14164 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14165 type_name = dwarf2_name (die->parent, cu);
14166 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14167 return 0;
14168
14169 len = strlen (fieldname);
fe978cb0
PA
14170 return (strncmp (fieldname, type_name, len) == 0
14171 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14172}
14173
c906108c
SS
14174/* Add a member function to the proper fieldlist. */
14175
14176static void
107d2387 14177dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14178 struct type *type, struct dwarf2_cu *cu)
c906108c 14179{
e7c27a73 14180 struct objfile *objfile = cu->objfile;
c906108c
SS
14181 struct attribute *attr;
14182 struct fnfieldlist *flp;
14183 int i;
14184 struct fn_field *fnp;
15d034d0 14185 const char *fieldname;
c906108c 14186 struct nextfnfield *new_fnfield;
f792889a 14187 struct type *this_type;
60d5a603 14188 enum dwarf_access_attribute accessibility;
c906108c 14189
b4ba55a1 14190 if (cu->language == language_ada)
a73c6dcd 14191 error (_("unexpected member function in Ada type"));
b4ba55a1 14192
2df3850c 14193 /* Get name of member function. */
39cbfefa
DJ
14194 fieldname = dwarf2_name (die, cu);
14195 if (fieldname == NULL)
2df3850c 14196 return;
c906108c 14197
c906108c
SS
14198 /* Look up member function name in fieldlist. */
14199 for (i = 0; i < fip->nfnfields; i++)
14200 {
27bfe10e 14201 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
14202 break;
14203 }
14204
14205 /* Create new list element if necessary. */
14206 if (i < fip->nfnfields)
14207 flp = &fip->fnfieldlists[i];
14208 else
14209 {
14210 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
14211 {
14212 fip->fnfieldlists = (struct fnfieldlist *)
14213 xrealloc (fip->fnfieldlists,
14214 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 14215 * sizeof (struct fnfieldlist));
c906108c 14216 if (fip->nfnfields == 0)
c13c43fd 14217 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
14218 }
14219 flp = &fip->fnfieldlists[fip->nfnfields];
14220 flp->name = fieldname;
14221 flp->length = 0;
14222 flp->head = NULL;
3da10d80 14223 i = fip->nfnfields++;
c906108c
SS
14224 }
14225
14226 /* Create a new member function field and chain it to the field list
0963b4bd 14227 entry. */
8d749320 14228 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 14229 make_cleanup (xfree, new_fnfield);
c906108c
SS
14230 memset (new_fnfield, 0, sizeof (struct nextfnfield));
14231 new_fnfield->next = flp->head;
14232 flp->head = new_fnfield;
14233 flp->length++;
14234
14235 /* Fill in the member function field info. */
14236 fnp = &new_fnfield->fnfield;
3da10d80
KS
14237
14238 /* Delay processing of the physname until later. */
9c37b5ae 14239 if (cu->language == language_cplus)
3da10d80
KS
14240 {
14241 add_to_method_list (type, i, flp->length - 1, fieldname,
14242 die, cu);
14243 }
14244 else
14245 {
1d06ead6 14246 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14247 fnp->physname = physname ? physname : "";
14248 }
14249
c906108c 14250 fnp->type = alloc_type (objfile);
f792889a
DJ
14251 this_type = read_type_die (die, cu);
14252 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 14253 {
f792889a 14254 int nparams = TYPE_NFIELDS (this_type);
c906108c 14255
f792889a 14256 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14257 of the method itself (TYPE_CODE_METHOD). */
14258 smash_to_method_type (fnp->type, type,
f792889a
DJ
14259 TYPE_TARGET_TYPE (this_type),
14260 TYPE_FIELDS (this_type),
14261 TYPE_NFIELDS (this_type),
14262 TYPE_VARARGS (this_type));
c906108c
SS
14263
14264 /* Handle static member functions.
c5aa993b 14265 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
14266 member functions. G++ helps GDB by marking the first
14267 parameter for non-static member functions (which is the this
14268 pointer) as artificial. We obtain this information from
14269 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 14270 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
14271 fnp->voffset = VOFFSET_STATIC;
14272 }
14273 else
e2e0b3e5 14274 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 14275 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
14276
14277 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 14278 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 14279 fnp->fcontext = die_containing_type (die, cu);
c906108c 14280
3e43a32a
MS
14281 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14282 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
14283
14284 /* Get accessibility. */
e142c38c 14285 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 14286 if (attr)
aead7601 14287 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
14288 else
14289 accessibility = dwarf2_default_access_attribute (die, cu);
14290 switch (accessibility)
c906108c 14291 {
60d5a603
JK
14292 case DW_ACCESS_private:
14293 fnp->is_private = 1;
14294 break;
14295 case DW_ACCESS_protected:
14296 fnp->is_protected = 1;
14297 break;
c906108c
SS
14298 }
14299
b02dede2 14300 /* Check for artificial methods. */
e142c38c 14301 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
14302 if (attr && DW_UNSND (attr) != 0)
14303 fnp->is_artificial = 1;
14304
7d27a96d
TT
14305 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14306
0d564a31 14307 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
14308 function. For older versions of GCC, this is an offset in the
14309 appropriate virtual table, as specified by DW_AT_containing_type.
14310 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
14311 to the object address. */
14312
e142c38c 14313 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 14314 if (attr)
8e19ed76 14315 {
aec5aa8b 14316 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 14317 {
aec5aa8b
TT
14318 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14319 {
14320 /* Old-style GCC. */
14321 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14322 }
14323 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14324 || (DW_BLOCK (attr)->size > 1
14325 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14326 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14327 {
aec5aa8b
TT
14328 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14329 if ((fnp->voffset % cu->header.addr_size) != 0)
14330 dwarf2_complex_location_expr_complaint ();
14331 else
14332 fnp->voffset /= cu->header.addr_size;
14333 fnp->voffset += 2;
14334 }
14335 else
14336 dwarf2_complex_location_expr_complaint ();
14337
14338 if (!fnp->fcontext)
7e993ebf
KS
14339 {
14340 /* If there is no `this' field and no DW_AT_containing_type,
14341 we cannot actually find a base class context for the
14342 vtable! */
14343 if (TYPE_NFIELDS (this_type) == 0
14344 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14345 {
14346 complaint (&symfile_complaints,
14347 _("cannot determine context for virtual member "
14348 "function \"%s\" (offset %d)"),
9c541725 14349 fieldname, to_underlying (die->sect_off));
7e993ebf
KS
14350 }
14351 else
14352 {
14353 fnp->fcontext
14354 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14355 }
14356 }
aec5aa8b 14357 }
3690dd37 14358 else if (attr_form_is_section_offset (attr))
8e19ed76 14359 {
4d3c2250 14360 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14361 }
14362 else
14363 {
4d3c2250
KB
14364 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14365 fieldname);
8e19ed76 14366 }
0d564a31 14367 }
d48cc9dd
DJ
14368 else
14369 {
14370 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14371 if (attr && DW_UNSND (attr))
14372 {
14373 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14374 complaint (&symfile_complaints,
3e43a32a
MS
14375 _("Member function \"%s\" (offset %d) is virtual "
14376 "but the vtable offset is not specified"),
9c541725 14377 fieldname, to_underlying (die->sect_off));
9655fd1a 14378 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
14379 TYPE_CPLUS_DYNAMIC (type) = 1;
14380 }
14381 }
c906108c
SS
14382}
14383
14384/* Create the vector of member function fields, and attach it to the type. */
14385
14386static void
fba45db2 14387dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14388 struct dwarf2_cu *cu)
c906108c
SS
14389{
14390 struct fnfieldlist *flp;
c906108c
SS
14391 int i;
14392
b4ba55a1 14393 if (cu->language == language_ada)
a73c6dcd 14394 error (_("unexpected member functions in Ada type"));
b4ba55a1 14395
c906108c
SS
14396 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14397 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14398 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
14399
14400 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
14401 {
14402 struct nextfnfield *nfp = flp->head;
14403 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14404 int k;
14405
14406 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
14407 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
14408 fn_flp->fn_fields = (struct fn_field *)
14409 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
14410 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 14411 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
14412 }
14413
14414 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
14415}
14416
1168df01
JB
14417/* Returns non-zero if NAME is the name of a vtable member in CU's
14418 language, zero otherwise. */
14419static int
14420is_vtable_name (const char *name, struct dwarf2_cu *cu)
14421{
14422 static const char vptr[] = "_vptr";
14423
9c37b5ae
TT
14424 /* Look for the C++ form of the vtable. */
14425 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
14426 return 1;
14427
14428 return 0;
14429}
14430
c0dd20ea 14431/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
14432 functions, with the ABI-specified layout. If TYPE describes
14433 such a structure, smash it into a member function type.
61049d3b
DJ
14434
14435 GCC shouldn't do this; it should just output pointer to member DIEs.
14436 This is GCC PR debug/28767. */
c0dd20ea 14437
0b92b5bb
TT
14438static void
14439quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 14440{
09e2d7c7 14441 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
14442
14443 /* Check for a structure with no name and two children. */
0b92b5bb
TT
14444 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14445 return;
c0dd20ea
DJ
14446
14447 /* Check for __pfn and __delta members. */
0b92b5bb
TT
14448 if (TYPE_FIELD_NAME (type, 0) == NULL
14449 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14450 || TYPE_FIELD_NAME (type, 1) == NULL
14451 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14452 return;
c0dd20ea
DJ
14453
14454 /* Find the type of the method. */
0b92b5bb 14455 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
14456 if (pfn_type == NULL
14457 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14458 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 14459 return;
c0dd20ea
DJ
14460
14461 /* Look for the "this" argument. */
14462 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14463 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 14464 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 14465 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 14466 return;
c0dd20ea 14467
09e2d7c7 14468 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 14469 new_type = alloc_type (objfile);
09e2d7c7 14470 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
14471 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14472 TYPE_VARARGS (pfn_type));
0b92b5bb 14473 smash_to_methodptr_type (type, new_type);
c0dd20ea 14474}
1168df01 14475
685b1105 14476
c906108c 14477/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
14478 (definition) to create a type for the structure or union. Fill in
14479 the type's name and general properties; the members will not be
83655187
DE
14480 processed until process_structure_scope. A symbol table entry for
14481 the type will also not be done until process_structure_scope (assuming
14482 the type has a name).
c906108c 14483
c767944b
DJ
14484 NOTE: we need to call these functions regardless of whether or not the
14485 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 14486 structure or union. This gets the type entered into our set of
83655187 14487 user defined types. */
c906108c 14488
f792889a 14489static struct type *
134d01f1 14490read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14491{
e7c27a73 14492 struct objfile *objfile = cu->objfile;
c906108c
SS
14493 struct type *type;
14494 struct attribute *attr;
15d034d0 14495 const char *name;
c906108c 14496
348e048f
DE
14497 /* If the definition of this type lives in .debug_types, read that type.
14498 Don't follow DW_AT_specification though, that will take us back up
14499 the chain and we want to go down. */
45e58e77 14500 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
14501 if (attr)
14502 {
ac9ec31b 14503 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 14504
ac9ec31b 14505 /* The type's CU may not be the same as CU.
02142a6c 14506 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
14507 return set_die_type (die, type, cu);
14508 }
14509
c0dd20ea 14510 type = alloc_type (objfile);
c906108c 14511 INIT_CPLUS_SPECIFIC (type);
93311388 14512
39cbfefa
DJ
14513 name = dwarf2_name (die, cu);
14514 if (name != NULL)
c906108c 14515 {
987504bb 14516 if (cu->language == language_cplus
c44af4eb
TT
14517 || cu->language == language_d
14518 || cu->language == language_rust)
63d06c5c 14519 {
15d034d0 14520 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
14521
14522 /* dwarf2_full_name might have already finished building the DIE's
14523 type. If so, there is no need to continue. */
14524 if (get_die_type (die, cu) != NULL)
14525 return get_die_type (die, cu);
14526
14527 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
14528 if (die->tag == DW_TAG_structure_type
14529 || die->tag == DW_TAG_class_type)
14530 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
14531 }
14532 else
14533 {
d8151005
DJ
14534 /* The name is already allocated along with this objfile, so
14535 we don't need to duplicate it for the type. */
7d455152 14536 TYPE_TAG_NAME (type) = name;
94af9270
KS
14537 if (die->tag == DW_TAG_class_type)
14538 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 14539 }
c906108c
SS
14540 }
14541
14542 if (die->tag == DW_TAG_structure_type)
14543 {
14544 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14545 }
14546 else if (die->tag == DW_TAG_union_type)
14547 {
14548 TYPE_CODE (type) = TYPE_CODE_UNION;
14549 }
14550 else
14551 {
4753d33b 14552 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
14553 }
14554
0cc2414c
TT
14555 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
14556 TYPE_DECLARED_CLASS (type) = 1;
14557
e142c38c 14558 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14559 if (attr)
14560 {
155bfbd3
JB
14561 if (attr_form_is_constant (attr))
14562 TYPE_LENGTH (type) = DW_UNSND (attr);
14563 else
14564 {
14565 /* For the moment, dynamic type sizes are not supported
14566 by GDB's struct type. The actual size is determined
14567 on-demand when resolving the type of a given object,
14568 so set the type's length to zero for now. Otherwise,
14569 we record an expression as the length, and that expression
14570 could lead to a very large value, which could eventually
14571 lead to us trying to allocate that much memory when creating
14572 a value of that type. */
14573 TYPE_LENGTH (type) = 0;
14574 }
c906108c
SS
14575 }
14576 else
14577 {
14578 TYPE_LENGTH (type) = 0;
14579 }
14580
5230b05a 14581 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 14582 {
5230b05a
WT
14583 /* ICC<14 does not output the required DW_AT_declaration on
14584 incomplete types, but gives them a size of zero. */
422b1cb0 14585 TYPE_STUB (type) = 1;
685b1105
JK
14586 }
14587 else
14588 TYPE_STUB_SUPPORTED (type) = 1;
14589
dc718098 14590 if (die_is_declaration (die, cu))
876cecd0 14591 TYPE_STUB (type) = 1;
a6c727b2
DJ
14592 else if (attr == NULL && die->child == NULL
14593 && producer_is_realview (cu->producer))
14594 /* RealView does not output the required DW_AT_declaration
14595 on incomplete types. */
14596 TYPE_STUB (type) = 1;
dc718098 14597
c906108c
SS
14598 /* We need to add the type field to the die immediately so we don't
14599 infinitely recurse when dealing with pointers to the structure
0963b4bd 14600 type within the structure itself. */
1c379e20 14601 set_die_type (die, type, cu);
c906108c 14602
7e314c57
JK
14603 /* set_die_type should be already done. */
14604 set_descriptive_type (type, die, cu);
14605
c767944b
DJ
14606 return type;
14607}
14608
14609/* Finish creating a structure or union type, including filling in
14610 its members and creating a symbol for it. */
14611
14612static void
14613process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
14614{
14615 struct objfile *objfile = cu->objfile;
ca040673 14616 struct die_info *child_die;
c767944b
DJ
14617 struct type *type;
14618
14619 type = get_die_type (die, cu);
14620 if (type == NULL)
14621 type = read_structure_type (die, cu);
14622
e142c38c 14623 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
14624 {
14625 struct field_info fi;
2f4732b0 14626 std::vector<struct symbol *> template_args;
c767944b 14627 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
14628
14629 memset (&fi, 0, sizeof (struct field_info));
14630
639d11d3 14631 child_die = die->child;
c906108c
SS
14632
14633 while (child_die && child_die->tag)
14634 {
a9a9bd0f
DC
14635 if (child_die->tag == DW_TAG_member
14636 || child_die->tag == DW_TAG_variable)
c906108c 14637 {
a9a9bd0f
DC
14638 /* NOTE: carlton/2002-11-05: A C++ static data member
14639 should be a DW_TAG_member that is a declaration, but
14640 all versions of G++ as of this writing (so through at
14641 least 3.2.1) incorrectly generate DW_TAG_variable
14642 tags for them instead. */
e7c27a73 14643 dwarf2_add_field (&fi, child_die, cu);
c906108c 14644 }
8713b1b1 14645 else if (child_die->tag == DW_TAG_subprogram)
c906108c 14646 {
e98c9e7c
TT
14647 /* Rust doesn't have member functions in the C++ sense.
14648 However, it does emit ordinary functions as children
14649 of a struct DIE. */
14650 if (cu->language == language_rust)
14651 read_func_scope (child_die, cu);
14652 else
14653 {
14654 /* C++ member function. */
14655 dwarf2_add_member_fn (&fi, child_die, type, cu);
14656 }
c906108c
SS
14657 }
14658 else if (child_die->tag == DW_TAG_inheritance)
14659 {
14660 /* C++ base class field. */
e7c27a73 14661 dwarf2_add_field (&fi, child_die, cu);
c906108c 14662 }
98751a41
JK
14663 else if (child_die->tag == DW_TAG_typedef)
14664 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
14665 else if (child_die->tag == DW_TAG_template_type_param
14666 || child_die->tag == DW_TAG_template_value_param)
14667 {
14668 struct symbol *arg = new_symbol (child_die, NULL, cu);
14669
f1078f66 14670 if (arg != NULL)
2f4732b0 14671 template_args.push_back (arg);
34eaf542
TT
14672 }
14673
c906108c
SS
14674 child_die = sibling_die (child_die);
14675 }
14676
34eaf542 14677 /* Attach template arguments to type. */
2f4732b0 14678 if (!template_args.empty ())
34eaf542
TT
14679 {
14680 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 14681 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 14682 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
14683 = XOBNEWVEC (&objfile->objfile_obstack,
14684 struct symbol *,
14685 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 14686 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 14687 template_args.data (),
34eaf542
TT
14688 (TYPE_N_TEMPLATE_ARGUMENTS (type)
14689 * sizeof (struct symbol *)));
34eaf542
TT
14690 }
14691
c906108c
SS
14692 /* Attach fields and member functions to the type. */
14693 if (fi.nfields)
e7c27a73 14694 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
14695 if (fi.nfnfields)
14696 {
e7c27a73 14697 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 14698
c5aa993b 14699 /* Get the type which refers to the base class (possibly this
c906108c 14700 class itself) which contains the vtable pointer for the current
0d564a31
DJ
14701 class from the DW_AT_containing_type attribute. This use of
14702 DW_AT_containing_type is a GNU extension. */
c906108c 14703
e142c38c 14704 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 14705 {
e7c27a73 14706 struct type *t = die_containing_type (die, cu);
c906108c 14707
ae6ae975 14708 set_type_vptr_basetype (type, t);
c906108c
SS
14709 if (type == t)
14710 {
c906108c
SS
14711 int i;
14712
14713 /* Our own class provides vtbl ptr. */
14714 for (i = TYPE_NFIELDS (t) - 1;
14715 i >= TYPE_N_BASECLASSES (t);
14716 --i)
14717 {
0d5cff50 14718 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 14719
1168df01 14720 if (is_vtable_name (fieldname, cu))
c906108c 14721 {
ae6ae975 14722 set_type_vptr_fieldno (type, i);
c906108c
SS
14723 break;
14724 }
14725 }
14726
14727 /* Complain if virtual function table field not found. */
14728 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 14729 complaint (&symfile_complaints,
3e43a32a
MS
14730 _("virtual function table pointer "
14731 "not found when defining class '%s'"),
4d3c2250
KB
14732 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
14733 "");
c906108c
SS
14734 }
14735 else
14736 {
ae6ae975 14737 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
14738 }
14739 }
f6235d4c 14740 else if (cu->producer
61012eef 14741 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
14742 {
14743 /* The IBM XLC compiler does not provide direct indication
14744 of the containing type, but the vtable pointer is
14745 always named __vfp. */
14746
14747 int i;
14748
14749 for (i = TYPE_NFIELDS (type) - 1;
14750 i >= TYPE_N_BASECLASSES (type);
14751 --i)
14752 {
14753 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
14754 {
ae6ae975
DE
14755 set_type_vptr_fieldno (type, i);
14756 set_type_vptr_basetype (type, type);
f6235d4c
EZ
14757 break;
14758 }
14759 }
14760 }
c906108c 14761 }
98751a41
JK
14762
14763 /* Copy fi.typedef_field_list linked list elements content into the
14764 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
14765 if (fi.typedef_field_list)
14766 {
14767 int i = fi.typedef_field_list_count;
14768
a0d7a4ff 14769 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 14770 TYPE_TYPEDEF_FIELD_ARRAY (type)
224c3ddb
SM
14771 = ((struct typedef_field *)
14772 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
14773 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
14774
14775 /* Reverse the list order to keep the debug info elements order. */
14776 while (--i >= 0)
14777 {
14778 struct typedef_field *dest, *src;
6e70227d 14779
98751a41
JK
14780 dest = &TYPE_TYPEDEF_FIELD (type, i);
14781 src = &fi.typedef_field_list->field;
14782 fi.typedef_field_list = fi.typedef_field_list->next;
14783 *dest = *src;
14784 }
14785 }
c767944b
DJ
14786
14787 do_cleanups (back_to);
c906108c 14788 }
63d06c5c 14789
bb5ed363 14790 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 14791
90aeadfc
DC
14792 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
14793 snapshots) has been known to create a die giving a declaration
14794 for a class that has, as a child, a die giving a definition for a
14795 nested class. So we have to process our children even if the
14796 current die is a declaration. Normally, of course, a declaration
14797 won't have any children at all. */
134d01f1 14798
ca040673
DE
14799 child_die = die->child;
14800
90aeadfc
DC
14801 while (child_die != NULL && child_die->tag)
14802 {
14803 if (child_die->tag == DW_TAG_member
14804 || child_die->tag == DW_TAG_variable
34eaf542
TT
14805 || child_die->tag == DW_TAG_inheritance
14806 || child_die->tag == DW_TAG_template_value_param
14807 || child_die->tag == DW_TAG_template_type_param)
134d01f1 14808 {
90aeadfc 14809 /* Do nothing. */
134d01f1 14810 }
90aeadfc
DC
14811 else
14812 process_die (child_die, cu);
134d01f1 14813
90aeadfc 14814 child_die = sibling_die (child_die);
134d01f1
DJ
14815 }
14816
fa4028e9
JB
14817 /* Do not consider external references. According to the DWARF standard,
14818 these DIEs are identified by the fact that they have no byte_size
14819 attribute, and a declaration attribute. */
14820 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
14821 || !die_is_declaration (die, cu))
c767944b 14822 new_symbol (die, type, cu);
134d01f1
DJ
14823}
14824
55426c9d
JB
14825/* Assuming DIE is an enumeration type, and TYPE is its associated type,
14826 update TYPE using some information only available in DIE's children. */
14827
14828static void
14829update_enumeration_type_from_children (struct die_info *die,
14830 struct type *type,
14831 struct dwarf2_cu *cu)
14832{
60f7655a 14833 struct die_info *child_die;
55426c9d
JB
14834 int unsigned_enum = 1;
14835 int flag_enum = 1;
14836 ULONGEST mask = 0;
55426c9d 14837
8268c778 14838 auto_obstack obstack;
55426c9d 14839
60f7655a
DE
14840 for (child_die = die->child;
14841 child_die != NULL && child_die->tag;
14842 child_die = sibling_die (child_die))
55426c9d
JB
14843 {
14844 struct attribute *attr;
14845 LONGEST value;
14846 const gdb_byte *bytes;
14847 struct dwarf2_locexpr_baton *baton;
14848 const char *name;
60f7655a 14849
55426c9d
JB
14850 if (child_die->tag != DW_TAG_enumerator)
14851 continue;
14852
14853 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
14854 if (attr == NULL)
14855 continue;
14856
14857 name = dwarf2_name (child_die, cu);
14858 if (name == NULL)
14859 name = "<anonymous enumerator>";
14860
14861 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
14862 &value, &bytes, &baton);
14863 if (value < 0)
14864 {
14865 unsigned_enum = 0;
14866 flag_enum = 0;
14867 }
14868 else if ((mask & value) != 0)
14869 flag_enum = 0;
14870 else
14871 mask |= value;
14872
14873 /* If we already know that the enum type is neither unsigned, nor
14874 a flag type, no need to look at the rest of the enumerates. */
14875 if (!unsigned_enum && !flag_enum)
14876 break;
55426c9d
JB
14877 }
14878
14879 if (unsigned_enum)
14880 TYPE_UNSIGNED (type) = 1;
14881 if (flag_enum)
14882 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
14883}
14884
134d01f1
DJ
14885/* Given a DW_AT_enumeration_type die, set its type. We do not
14886 complete the type's fields yet, or create any symbols. */
c906108c 14887
f792889a 14888static struct type *
134d01f1 14889read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14890{
e7c27a73 14891 struct objfile *objfile = cu->objfile;
c906108c 14892 struct type *type;
c906108c 14893 struct attribute *attr;
0114d602 14894 const char *name;
134d01f1 14895
348e048f
DE
14896 /* If the definition of this type lives in .debug_types, read that type.
14897 Don't follow DW_AT_specification though, that will take us back up
14898 the chain and we want to go down. */
45e58e77 14899 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
14900 if (attr)
14901 {
ac9ec31b 14902 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 14903
ac9ec31b 14904 /* The type's CU may not be the same as CU.
02142a6c 14905 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
14906 return set_die_type (die, type, cu);
14907 }
14908
c906108c
SS
14909 type = alloc_type (objfile);
14910
14911 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 14912 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 14913 if (name != NULL)
7d455152 14914 TYPE_TAG_NAME (type) = name;
c906108c 14915
0626fc76
TT
14916 attr = dwarf2_attr (die, DW_AT_type, cu);
14917 if (attr != NULL)
14918 {
14919 struct type *underlying_type = die_type (die, cu);
14920
14921 TYPE_TARGET_TYPE (type) = underlying_type;
14922 }
14923
e142c38c 14924 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14925 if (attr)
14926 {
14927 TYPE_LENGTH (type) = DW_UNSND (attr);
14928 }
14929 else
14930 {
14931 TYPE_LENGTH (type) = 0;
14932 }
14933
137033e9
JB
14934 /* The enumeration DIE can be incomplete. In Ada, any type can be
14935 declared as private in the package spec, and then defined only
14936 inside the package body. Such types are known as Taft Amendment
14937 Types. When another package uses such a type, an incomplete DIE
14938 may be generated by the compiler. */
02eb380e 14939 if (die_is_declaration (die, cu))
876cecd0 14940 TYPE_STUB (type) = 1;
02eb380e 14941
0626fc76
TT
14942 /* Finish the creation of this type by using the enum's children.
14943 We must call this even when the underlying type has been provided
14944 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
14945 update_enumeration_type_from_children (die, type, cu);
14946
0626fc76
TT
14947 /* If this type has an underlying type that is not a stub, then we
14948 may use its attributes. We always use the "unsigned" attribute
14949 in this situation, because ordinarily we guess whether the type
14950 is unsigned -- but the guess can be wrong and the underlying type
14951 can tell us the reality. However, we defer to a local size
14952 attribute if one exists, because this lets the compiler override
14953 the underlying type if needed. */
14954 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
14955 {
14956 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
14957 if (TYPE_LENGTH (type) == 0)
14958 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
14959 }
14960
3d567982
TT
14961 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
14962
f792889a 14963 return set_die_type (die, type, cu);
134d01f1
DJ
14964}
14965
14966/* Given a pointer to a die which begins an enumeration, process all
14967 the dies that define the members of the enumeration, and create the
14968 symbol for the enumeration type.
14969
14970 NOTE: We reverse the order of the element list. */
14971
14972static void
14973process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
14974{
f792889a 14975 struct type *this_type;
134d01f1 14976
f792889a
DJ
14977 this_type = get_die_type (die, cu);
14978 if (this_type == NULL)
14979 this_type = read_enumeration_type (die, cu);
9dc481d3 14980
639d11d3 14981 if (die->child != NULL)
c906108c 14982 {
9dc481d3
DE
14983 struct die_info *child_die;
14984 struct symbol *sym;
14985 struct field *fields = NULL;
14986 int num_fields = 0;
15d034d0 14987 const char *name;
9dc481d3 14988
639d11d3 14989 child_die = die->child;
c906108c
SS
14990 while (child_die && child_die->tag)
14991 {
14992 if (child_die->tag != DW_TAG_enumerator)
14993 {
e7c27a73 14994 process_die (child_die, cu);
c906108c
SS
14995 }
14996 else
14997 {
39cbfefa
DJ
14998 name = dwarf2_name (child_die, cu);
14999 if (name)
c906108c 15000 {
f792889a 15001 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
15002
15003 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
15004 {
15005 fields = (struct field *)
15006 xrealloc (fields,
15007 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 15008 * sizeof (struct field));
c906108c
SS
15009 }
15010
3567439c 15011 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 15012 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 15013 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
15014 FIELD_BITSIZE (fields[num_fields]) = 0;
15015
15016 num_fields++;
15017 }
15018 }
15019
15020 child_die = sibling_die (child_die);
15021 }
15022
15023 if (num_fields)
15024 {
f792889a
DJ
15025 TYPE_NFIELDS (this_type) = num_fields;
15026 TYPE_FIELDS (this_type) = (struct field *)
15027 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
15028 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 15029 sizeof (struct field) * num_fields);
b8c9b27d 15030 xfree (fields);
c906108c 15031 }
c906108c 15032 }
134d01f1 15033
6c83ed52
TT
15034 /* If we are reading an enum from a .debug_types unit, and the enum
15035 is a declaration, and the enum is not the signatured type in the
15036 unit, then we do not want to add a symbol for it. Adding a
15037 symbol would in some cases obscure the true definition of the
15038 enum, giving users an incomplete type when the definition is
15039 actually available. Note that we do not want to do this for all
15040 enums which are just declarations, because C++0x allows forward
15041 enum declarations. */
3019eac3 15042 if (cu->per_cu->is_debug_types
6c83ed52
TT
15043 && die_is_declaration (die, cu))
15044 {
52dc124a 15045 struct signatured_type *sig_type;
6c83ed52 15046
c0f78cd4 15047 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
15048 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15049 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
15050 return;
15051 }
15052
f792889a 15053 new_symbol (die, this_type, cu);
c906108c
SS
15054}
15055
15056/* Extract all information from a DW_TAG_array_type DIE and put it in
15057 the DIE's type field. For now, this only handles one dimensional
15058 arrays. */
15059
f792889a 15060static struct type *
e7c27a73 15061read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15062{
e7c27a73 15063 struct objfile *objfile = cu->objfile;
c906108c 15064 struct die_info *child_die;
7e314c57 15065 struct type *type;
c906108c 15066 struct type *element_type, *range_type, *index_type;
c906108c 15067 struct attribute *attr;
15d034d0 15068 const char *name;
dc53a7ad 15069 unsigned int bit_stride = 0;
c906108c 15070
e7c27a73 15071 element_type = die_type (die, cu);
c906108c 15072
7e314c57
JK
15073 /* The die_type call above may have already set the type for this DIE. */
15074 type = get_die_type (die, cu);
15075 if (type)
15076 return type;
15077
dc53a7ad
JB
15078 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15079 if (attr != NULL)
15080 bit_stride = DW_UNSND (attr) * 8;
15081
15082 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15083 if (attr != NULL)
15084 bit_stride = DW_UNSND (attr);
15085
c906108c
SS
15086 /* Irix 6.2 native cc creates array types without children for
15087 arrays with unspecified length. */
639d11d3 15088 if (die->child == NULL)
c906108c 15089 {
46bf5051 15090 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 15091 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
15092 type = create_array_type_with_stride (NULL, element_type, range_type,
15093 bit_stride);
f792889a 15094 return set_die_type (die, type, cu);
c906108c
SS
15095 }
15096
791afaa2 15097 std::vector<struct type *> range_types;
639d11d3 15098 child_die = die->child;
c906108c
SS
15099 while (child_die && child_die->tag)
15100 {
15101 if (child_die->tag == DW_TAG_subrange_type)
15102 {
f792889a 15103 struct type *child_type = read_type_die (child_die, cu);
9a619af0 15104
f792889a 15105 if (child_type != NULL)
a02abb62 15106 {
0963b4bd
MS
15107 /* The range type was succesfully read. Save it for the
15108 array type creation. */
791afaa2 15109 range_types.push_back (child_type);
a02abb62 15110 }
c906108c
SS
15111 }
15112 child_die = sibling_die (child_die);
15113 }
15114
15115 /* Dwarf2 dimensions are output from left to right, create the
15116 necessary array types in backwards order. */
7ca2d3a3 15117
c906108c 15118 type = element_type;
7ca2d3a3
DL
15119
15120 if (read_array_order (die, cu) == DW_ORD_col_major)
15121 {
15122 int i = 0;
9a619af0 15123
791afaa2 15124 while (i < range_types.size ())
dc53a7ad
JB
15125 type = create_array_type_with_stride (NULL, type, range_types[i++],
15126 bit_stride);
7ca2d3a3
DL
15127 }
15128 else
15129 {
791afaa2 15130 size_t ndim = range_types.size ();
7ca2d3a3 15131 while (ndim-- > 0)
dc53a7ad
JB
15132 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15133 bit_stride);
7ca2d3a3 15134 }
c906108c 15135
f5f8a009
EZ
15136 /* Understand Dwarf2 support for vector types (like they occur on
15137 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15138 array type. This is not part of the Dwarf2/3 standard yet, but a
15139 custom vendor extension. The main difference between a regular
15140 array and the vector variant is that vectors are passed by value
15141 to functions. */
e142c38c 15142 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 15143 if (attr)
ea37ba09 15144 make_vector_type (type);
f5f8a009 15145
dbc98a8b
KW
15146 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15147 implementation may choose to implement triple vectors using this
15148 attribute. */
15149 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15150 if (attr)
15151 {
15152 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15153 TYPE_LENGTH (type) = DW_UNSND (attr);
15154 else
3e43a32a
MS
15155 complaint (&symfile_complaints,
15156 _("DW_AT_byte_size for array type smaller "
15157 "than the total size of elements"));
dbc98a8b
KW
15158 }
15159
39cbfefa
DJ
15160 name = dwarf2_name (die, cu);
15161 if (name)
15162 TYPE_NAME (type) = name;
6e70227d 15163
0963b4bd 15164 /* Install the type in the die. */
7e314c57
JK
15165 set_die_type (die, type, cu);
15166
15167 /* set_die_type should be already done. */
b4ba55a1
JB
15168 set_descriptive_type (type, die, cu);
15169
7e314c57 15170 return type;
c906108c
SS
15171}
15172
7ca2d3a3 15173static enum dwarf_array_dim_ordering
6e70227d 15174read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
15175{
15176 struct attribute *attr;
15177
15178 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15179
aead7601
SM
15180 if (attr)
15181 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 15182
0963b4bd
MS
15183 /* GNU F77 is a special case, as at 08/2004 array type info is the
15184 opposite order to the dwarf2 specification, but data is still
15185 laid out as per normal fortran.
7ca2d3a3 15186
0963b4bd
MS
15187 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15188 version checking. */
7ca2d3a3 15189
905e0470
PM
15190 if (cu->language == language_fortran
15191 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
15192 {
15193 return DW_ORD_row_major;
15194 }
15195
6e70227d 15196 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
15197 {
15198 case array_column_major:
15199 return DW_ORD_col_major;
15200 case array_row_major:
15201 default:
15202 return DW_ORD_row_major;
15203 };
15204}
15205
72019c9c 15206/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 15207 the DIE's type field. */
72019c9c 15208
f792889a 15209static struct type *
72019c9c
GM
15210read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15211{
7e314c57
JK
15212 struct type *domain_type, *set_type;
15213 struct attribute *attr;
f792889a 15214
7e314c57
JK
15215 domain_type = die_type (die, cu);
15216
15217 /* The die_type call above may have already set the type for this DIE. */
15218 set_type = get_die_type (die, cu);
15219 if (set_type)
15220 return set_type;
15221
15222 set_type = create_set_type (NULL, domain_type);
15223
15224 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
15225 if (attr)
15226 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 15227
f792889a 15228 return set_die_type (die, set_type, cu);
72019c9c 15229}
7ca2d3a3 15230
0971de02
TT
15231/* A helper for read_common_block that creates a locexpr baton.
15232 SYM is the symbol which we are marking as computed.
15233 COMMON_DIE is the DIE for the common block.
15234 COMMON_LOC is the location expression attribute for the common
15235 block itself.
15236 MEMBER_LOC is the location expression attribute for the particular
15237 member of the common block that we are processing.
15238 CU is the CU from which the above come. */
15239
15240static void
15241mark_common_block_symbol_computed (struct symbol *sym,
15242 struct die_info *common_die,
15243 struct attribute *common_loc,
15244 struct attribute *member_loc,
15245 struct dwarf2_cu *cu)
15246{
15247 struct objfile *objfile = dwarf2_per_objfile->objfile;
15248 struct dwarf2_locexpr_baton *baton;
15249 gdb_byte *ptr;
15250 unsigned int cu_off;
15251 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15252 LONGEST offset = 0;
15253
15254 gdb_assert (common_loc && member_loc);
15255 gdb_assert (attr_form_is_block (common_loc));
15256 gdb_assert (attr_form_is_block (member_loc)
15257 || attr_form_is_constant (member_loc));
15258
8d749320 15259 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
15260 baton->per_cu = cu->per_cu;
15261 gdb_assert (baton->per_cu);
15262
15263 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15264
15265 if (attr_form_is_constant (member_loc))
15266 {
15267 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15268 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15269 }
15270 else
15271 baton->size += DW_BLOCK (member_loc)->size;
15272
224c3ddb 15273 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
15274 baton->data = ptr;
15275
15276 *ptr++ = DW_OP_call4;
9c541725 15277 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
15278 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15279 ptr += 4;
15280
15281 if (attr_form_is_constant (member_loc))
15282 {
15283 *ptr++ = DW_OP_addr;
15284 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15285 ptr += cu->header.addr_size;
15286 }
15287 else
15288 {
15289 /* We have to copy the data here, because DW_OP_call4 will only
15290 use a DW_AT_location attribute. */
15291 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15292 ptr += DW_BLOCK (member_loc)->size;
15293 }
15294
15295 *ptr++ = DW_OP_plus;
15296 gdb_assert (ptr - baton->data == baton->size);
15297
0971de02 15298 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 15299 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
15300}
15301
4357ac6c
TT
15302/* Create appropriate locally-scoped variables for all the
15303 DW_TAG_common_block entries. Also create a struct common_block
15304 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15305 is used to sepate the common blocks name namespace from regular
15306 variable names. */
c906108c
SS
15307
15308static void
e7c27a73 15309read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15310{
0971de02
TT
15311 struct attribute *attr;
15312
15313 attr = dwarf2_attr (die, DW_AT_location, cu);
15314 if (attr)
15315 {
15316 /* Support the .debug_loc offsets. */
15317 if (attr_form_is_block (attr))
15318 {
15319 /* Ok. */
15320 }
15321 else if (attr_form_is_section_offset (attr))
15322 {
15323 dwarf2_complex_location_expr_complaint ();
15324 attr = NULL;
15325 }
15326 else
15327 {
15328 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15329 "common block member");
15330 attr = NULL;
15331 }
15332 }
15333
639d11d3 15334 if (die->child != NULL)
c906108c 15335 {
4357ac6c
TT
15336 struct objfile *objfile = cu->objfile;
15337 struct die_info *child_die;
15338 size_t n_entries = 0, size;
15339 struct common_block *common_block;
15340 struct symbol *sym;
74ac6d43 15341
4357ac6c
TT
15342 for (child_die = die->child;
15343 child_die && child_die->tag;
15344 child_die = sibling_die (child_die))
15345 ++n_entries;
15346
15347 size = (sizeof (struct common_block)
15348 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
15349 common_block
15350 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15351 size);
4357ac6c
TT
15352 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15353 common_block->n_entries = 0;
15354
15355 for (child_die = die->child;
15356 child_die && child_die->tag;
15357 child_die = sibling_die (child_die))
15358 {
15359 /* Create the symbol in the DW_TAG_common_block block in the current
15360 symbol scope. */
e7c27a73 15361 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
15362 if (sym != NULL)
15363 {
15364 struct attribute *member_loc;
15365
15366 common_block->contents[common_block->n_entries++] = sym;
15367
15368 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15369 cu);
15370 if (member_loc)
15371 {
15372 /* GDB has handled this for a long time, but it is
15373 not specified by DWARF. It seems to have been
15374 emitted by gfortran at least as recently as:
15375 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15376 complaint (&symfile_complaints,
15377 _("Variable in common block has "
15378 "DW_AT_data_member_location "
15379 "- DIE at 0x%x [in module %s]"),
9c541725 15380 to_underlying (child_die->sect_off),
4262abfb 15381 objfile_name (cu->objfile));
0971de02
TT
15382
15383 if (attr_form_is_section_offset (member_loc))
15384 dwarf2_complex_location_expr_complaint ();
15385 else if (attr_form_is_constant (member_loc)
15386 || attr_form_is_block (member_loc))
15387 {
15388 if (attr)
15389 mark_common_block_symbol_computed (sym, die, attr,
15390 member_loc, cu);
15391 }
15392 else
15393 dwarf2_complex_location_expr_complaint ();
15394 }
15395 }
c906108c 15396 }
4357ac6c
TT
15397
15398 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
15399 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
15400 }
15401}
15402
0114d602 15403/* Create a type for a C++ namespace. */
d9fa45fe 15404
0114d602
DJ
15405static struct type *
15406read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 15407{
e7c27a73 15408 struct objfile *objfile = cu->objfile;
0114d602 15409 const char *previous_prefix, *name;
9219021c 15410 int is_anonymous;
0114d602
DJ
15411 struct type *type;
15412
15413 /* For extensions, reuse the type of the original namespace. */
15414 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
15415 {
15416 struct die_info *ext_die;
15417 struct dwarf2_cu *ext_cu = cu;
9a619af0 15418
0114d602
DJ
15419 ext_die = dwarf2_extension (die, &ext_cu);
15420 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
15421
15422 /* EXT_CU may not be the same as CU.
02142a6c 15423 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
15424 return set_die_type (die, type, cu);
15425 }
9219021c 15426
e142c38c 15427 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
15428
15429 /* Now build the name of the current namespace. */
15430
0114d602
DJ
15431 previous_prefix = determine_prefix (die, cu);
15432 if (previous_prefix[0] != '\0')
15433 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 15434 previous_prefix, name, 0, cu);
0114d602
DJ
15435
15436 /* Create the type. */
19f392bc 15437 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
15438 TYPE_TAG_NAME (type) = TYPE_NAME (type);
15439
60531b24 15440 return set_die_type (die, type, cu);
0114d602
DJ
15441}
15442
22cee43f 15443/* Read a namespace scope. */
0114d602
DJ
15444
15445static void
15446read_namespace (struct die_info *die, struct dwarf2_cu *cu)
15447{
15448 struct objfile *objfile = cu->objfile;
0114d602 15449 int is_anonymous;
9219021c 15450
5c4e30ca
DC
15451 /* Add a symbol associated to this if we haven't seen the namespace
15452 before. Also, add a using directive if it's an anonymous
15453 namespace. */
9219021c 15454
f2f0e013 15455 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
15456 {
15457 struct type *type;
15458
0114d602 15459 type = read_type_die (die, cu);
e7c27a73 15460 new_symbol (die, type, cu);
5c4e30ca 15461
e8e80198 15462 namespace_name (die, &is_anonymous, cu);
5c4e30ca 15463 if (is_anonymous)
0114d602
DJ
15464 {
15465 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 15466
eb1e02fd 15467 std::vector<const char *> excludes;
22cee43f
PMR
15468 add_using_directive (using_directives (cu->language),
15469 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 15470 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 15471 }
5c4e30ca 15472 }
9219021c 15473
639d11d3 15474 if (die->child != NULL)
d9fa45fe 15475 {
639d11d3 15476 struct die_info *child_die = die->child;
6e70227d 15477
d9fa45fe
DC
15478 while (child_die && child_die->tag)
15479 {
e7c27a73 15480 process_die (child_die, cu);
d9fa45fe
DC
15481 child_die = sibling_die (child_die);
15482 }
15483 }
38d518c9
EZ
15484}
15485
f55ee35c
JK
15486/* Read a Fortran module as type. This DIE can be only a declaration used for
15487 imported module. Still we need that type as local Fortran "use ... only"
15488 declaration imports depend on the created type in determine_prefix. */
15489
15490static struct type *
15491read_module_type (struct die_info *die, struct dwarf2_cu *cu)
15492{
15493 struct objfile *objfile = cu->objfile;
15d034d0 15494 const char *module_name;
f55ee35c
JK
15495 struct type *type;
15496
15497 module_name = dwarf2_name (die, cu);
15498 if (!module_name)
3e43a32a
MS
15499 complaint (&symfile_complaints,
15500 _("DW_TAG_module has no name, offset 0x%x"),
9c541725 15501 to_underlying (die->sect_off));
19f392bc 15502 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
15503
15504 /* determine_prefix uses TYPE_TAG_NAME. */
15505 TYPE_TAG_NAME (type) = TYPE_NAME (type);
15506
15507 return set_die_type (die, type, cu);
15508}
15509
5d7cb8df
JK
15510/* Read a Fortran module. */
15511
15512static void
15513read_module (struct die_info *die, struct dwarf2_cu *cu)
15514{
15515 struct die_info *child_die = die->child;
530e8392
KB
15516 struct type *type;
15517
15518 type = read_type_die (die, cu);
15519 new_symbol (die, type, cu);
5d7cb8df 15520
5d7cb8df
JK
15521 while (child_die && child_die->tag)
15522 {
15523 process_die (child_die, cu);
15524 child_die = sibling_die (child_die);
15525 }
15526}
15527
38d518c9
EZ
15528/* Return the name of the namespace represented by DIE. Set
15529 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
15530 namespace. */
15531
15532static const char *
e142c38c 15533namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
15534{
15535 struct die_info *current_die;
15536 const char *name = NULL;
15537
15538 /* Loop through the extensions until we find a name. */
15539
15540 for (current_die = die;
15541 current_die != NULL;
f2f0e013 15542 current_die = dwarf2_extension (die, &cu))
38d518c9 15543 {
96553a0c
DE
15544 /* We don't use dwarf2_name here so that we can detect the absence
15545 of a name -> anonymous namespace. */
7d45c7c3 15546 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 15547
38d518c9
EZ
15548 if (name != NULL)
15549 break;
15550 }
15551
15552 /* Is it an anonymous namespace? */
15553
15554 *is_anonymous = (name == NULL);
15555 if (*is_anonymous)
2b1dbab0 15556 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
15557
15558 return name;
d9fa45fe
DC
15559}
15560
c906108c
SS
15561/* Extract all information from a DW_TAG_pointer_type DIE and add to
15562 the user defined type vector. */
15563
f792889a 15564static struct type *
e7c27a73 15565read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15566{
5e2b427d 15567 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 15568 struct comp_unit_head *cu_header = &cu->header;
c906108c 15569 struct type *type;
8b2dbe47
KB
15570 struct attribute *attr_byte_size;
15571 struct attribute *attr_address_class;
15572 int byte_size, addr_class;
7e314c57
JK
15573 struct type *target_type;
15574
15575 target_type = die_type (die, cu);
c906108c 15576
7e314c57
JK
15577 /* The die_type call above may have already set the type for this DIE. */
15578 type = get_die_type (die, cu);
15579 if (type)
15580 return type;
15581
15582 type = lookup_pointer_type (target_type);
8b2dbe47 15583
e142c38c 15584 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
15585 if (attr_byte_size)
15586 byte_size = DW_UNSND (attr_byte_size);
c906108c 15587 else
8b2dbe47
KB
15588 byte_size = cu_header->addr_size;
15589
e142c38c 15590 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
15591 if (attr_address_class)
15592 addr_class = DW_UNSND (attr_address_class);
15593 else
15594 addr_class = DW_ADDR_none;
15595
15596 /* If the pointer size or address class is different than the
15597 default, create a type variant marked as such and set the
15598 length accordingly. */
15599 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 15600 {
5e2b427d 15601 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
15602 {
15603 int type_flags;
15604
849957d9 15605 type_flags = gdbarch_address_class_type_flags
5e2b427d 15606 (gdbarch, byte_size, addr_class);
876cecd0
TT
15607 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
15608 == 0);
8b2dbe47
KB
15609 type = make_type_with_address_space (type, type_flags);
15610 }
15611 else if (TYPE_LENGTH (type) != byte_size)
15612 {
3e43a32a
MS
15613 complaint (&symfile_complaints,
15614 _("invalid pointer size %d"), byte_size);
8b2dbe47 15615 }
6e70227d 15616 else
9a619af0
MS
15617 {
15618 /* Should we also complain about unhandled address classes? */
15619 }
c906108c 15620 }
8b2dbe47
KB
15621
15622 TYPE_LENGTH (type) = byte_size;
f792889a 15623 return set_die_type (die, type, cu);
c906108c
SS
15624}
15625
15626/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
15627 the user defined type vector. */
15628
f792889a 15629static struct type *
e7c27a73 15630read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
15631{
15632 struct type *type;
15633 struct type *to_type;
15634 struct type *domain;
15635
e7c27a73
DJ
15636 to_type = die_type (die, cu);
15637 domain = die_containing_type (die, cu);
0d5de010 15638
7e314c57
JK
15639 /* The calls above may have already set the type for this DIE. */
15640 type = get_die_type (die, cu);
15641 if (type)
15642 return type;
15643
0d5de010
DJ
15644 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
15645 type = lookup_methodptr_type (to_type);
7078baeb
TT
15646 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
15647 {
15648 struct type *new_type = alloc_type (cu->objfile);
15649
15650 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
15651 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
15652 TYPE_VARARGS (to_type));
15653 type = lookup_methodptr_type (new_type);
15654 }
0d5de010
DJ
15655 else
15656 type = lookup_memberptr_type (to_type, domain);
c906108c 15657
f792889a 15658 return set_die_type (die, type, cu);
c906108c
SS
15659}
15660
4297a3f0 15661/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
15662 the user defined type vector. */
15663
f792889a 15664static struct type *
4297a3f0
AV
15665read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
15666 enum type_code refcode)
c906108c 15667{
e7c27a73 15668 struct comp_unit_head *cu_header = &cu->header;
7e314c57 15669 struct type *type, *target_type;
c906108c
SS
15670 struct attribute *attr;
15671
4297a3f0
AV
15672 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
15673
7e314c57
JK
15674 target_type = die_type (die, cu);
15675
15676 /* The die_type call above may have already set the type for this DIE. */
15677 type = get_die_type (die, cu);
15678 if (type)
15679 return type;
15680
4297a3f0 15681 type = lookup_reference_type (target_type, refcode);
e142c38c 15682 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15683 if (attr)
15684 {
15685 TYPE_LENGTH (type) = DW_UNSND (attr);
15686 }
15687 else
15688 {
107d2387 15689 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 15690 }
f792889a 15691 return set_die_type (die, type, cu);
c906108c
SS
15692}
15693
cf363f18
MW
15694/* Add the given cv-qualifiers to the element type of the array. GCC
15695 outputs DWARF type qualifiers that apply to an array, not the
15696 element type. But GDB relies on the array element type to carry
15697 the cv-qualifiers. This mimics section 6.7.3 of the C99
15698 specification. */
15699
15700static struct type *
15701add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
15702 struct type *base_type, int cnst, int voltl)
15703{
15704 struct type *el_type, *inner_array;
15705
15706 base_type = copy_type (base_type);
15707 inner_array = base_type;
15708
15709 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
15710 {
15711 TYPE_TARGET_TYPE (inner_array) =
15712 copy_type (TYPE_TARGET_TYPE (inner_array));
15713 inner_array = TYPE_TARGET_TYPE (inner_array);
15714 }
15715
15716 el_type = TYPE_TARGET_TYPE (inner_array);
15717 cnst |= TYPE_CONST (el_type);
15718 voltl |= TYPE_VOLATILE (el_type);
15719 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
15720
15721 return set_die_type (die, base_type, cu);
15722}
15723
f792889a 15724static struct type *
e7c27a73 15725read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15726{
f792889a 15727 struct type *base_type, *cv_type;
c906108c 15728
e7c27a73 15729 base_type = die_type (die, cu);
7e314c57
JK
15730
15731 /* The die_type call above may have already set the type for this DIE. */
15732 cv_type = get_die_type (die, cu);
15733 if (cv_type)
15734 return cv_type;
15735
2f608a3a
KW
15736 /* In case the const qualifier is applied to an array type, the element type
15737 is so qualified, not the array type (section 6.7.3 of C99). */
15738 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 15739 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 15740
f792889a
DJ
15741 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
15742 return set_die_type (die, cv_type, cu);
c906108c
SS
15743}
15744
f792889a 15745static struct type *
e7c27a73 15746read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15747{
f792889a 15748 struct type *base_type, *cv_type;
c906108c 15749
e7c27a73 15750 base_type = die_type (die, cu);
7e314c57
JK
15751
15752 /* The die_type call above may have already set the type for this DIE. */
15753 cv_type = get_die_type (die, cu);
15754 if (cv_type)
15755 return cv_type;
15756
cf363f18
MW
15757 /* In case the volatile qualifier is applied to an array type, the
15758 element type is so qualified, not the array type (section 6.7.3
15759 of C99). */
15760 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
15761 return add_array_cv_type (die, cu, base_type, 0, 1);
15762
f792889a
DJ
15763 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
15764 return set_die_type (die, cv_type, cu);
c906108c
SS
15765}
15766
06d66ee9
TT
15767/* Handle DW_TAG_restrict_type. */
15768
15769static struct type *
15770read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
15771{
15772 struct type *base_type, *cv_type;
15773
15774 base_type = die_type (die, cu);
15775
15776 /* The die_type call above may have already set the type for this DIE. */
15777 cv_type = get_die_type (die, cu);
15778 if (cv_type)
15779 return cv_type;
15780
15781 cv_type = make_restrict_type (base_type);
15782 return set_die_type (die, cv_type, cu);
15783}
15784
a2c2acaf
MW
15785/* Handle DW_TAG_atomic_type. */
15786
15787static struct type *
15788read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
15789{
15790 struct type *base_type, *cv_type;
15791
15792 base_type = die_type (die, cu);
15793
15794 /* The die_type call above may have already set the type for this DIE. */
15795 cv_type = get_die_type (die, cu);
15796 if (cv_type)
15797 return cv_type;
15798
15799 cv_type = make_atomic_type (base_type);
15800 return set_die_type (die, cv_type, cu);
15801}
15802
c906108c
SS
15803/* Extract all information from a DW_TAG_string_type DIE and add to
15804 the user defined type vector. It isn't really a user defined type,
15805 but it behaves like one, with other DIE's using an AT_user_def_type
15806 attribute to reference it. */
15807
f792889a 15808static struct type *
e7c27a73 15809read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15810{
e7c27a73 15811 struct objfile *objfile = cu->objfile;
3b7538c0 15812 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15813 struct type *type, *range_type, *index_type, *char_type;
15814 struct attribute *attr;
15815 unsigned int length;
15816
e142c38c 15817 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
15818 if (attr)
15819 {
15820 length = DW_UNSND (attr);
15821 }
15822 else
15823 {
0963b4bd 15824 /* Check for the DW_AT_byte_size attribute. */
e142c38c 15825 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
15826 if (attr)
15827 {
15828 length = DW_UNSND (attr);
15829 }
15830 else
15831 {
15832 length = 1;
15833 }
c906108c 15834 }
6ccb9162 15835
46bf5051 15836 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 15837 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
15838 char_type = language_string_char_type (cu->language_defn, gdbarch);
15839 type = create_string_type (NULL, char_type, range_type);
6ccb9162 15840
f792889a 15841 return set_die_type (die, type, cu);
c906108c
SS
15842}
15843
4d804846
JB
15844/* Assuming that DIE corresponds to a function, returns nonzero
15845 if the function is prototyped. */
15846
15847static int
15848prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
15849{
15850 struct attribute *attr;
15851
15852 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
15853 if (attr && (DW_UNSND (attr) != 0))
15854 return 1;
15855
15856 /* The DWARF standard implies that the DW_AT_prototyped attribute
15857 is only meaninful for C, but the concept also extends to other
15858 languages that allow unprototyped functions (Eg: Objective C).
15859 For all other languages, assume that functions are always
15860 prototyped. */
15861 if (cu->language != language_c
15862 && cu->language != language_objc
15863 && cu->language != language_opencl)
15864 return 1;
15865
15866 /* RealView does not emit DW_AT_prototyped. We can not distinguish
15867 prototyped and unprototyped functions; default to prototyped,
15868 since that is more common in modern code (and RealView warns
15869 about unprototyped functions). */
15870 if (producer_is_realview (cu->producer))
15871 return 1;
15872
15873 return 0;
15874}
15875
c906108c
SS
15876/* Handle DIES due to C code like:
15877
15878 struct foo
c5aa993b
JM
15879 {
15880 int (*funcp)(int a, long l);
15881 int b;
15882 };
c906108c 15883
0963b4bd 15884 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 15885
f792889a 15886static struct type *
e7c27a73 15887read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15888{
bb5ed363 15889 struct objfile *objfile = cu->objfile;
0963b4bd
MS
15890 struct type *type; /* Type that this function returns. */
15891 struct type *ftype; /* Function that returns above type. */
c906108c
SS
15892 struct attribute *attr;
15893
e7c27a73 15894 type = die_type (die, cu);
7e314c57
JK
15895
15896 /* The die_type call above may have already set the type for this DIE. */
15897 ftype = get_die_type (die, cu);
15898 if (ftype)
15899 return ftype;
15900
0c8b41f1 15901 ftype = lookup_function_type (type);
c906108c 15902
4d804846 15903 if (prototyped_function_p (die, cu))
a6c727b2 15904 TYPE_PROTOTYPED (ftype) = 1;
c906108c 15905
c055b101
CV
15906 /* Store the calling convention in the type if it's available in
15907 the subroutine die. Otherwise set the calling convention to
15908 the default value DW_CC_normal. */
15909 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
15910 if (attr)
15911 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
15912 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
15913 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
15914 else
15915 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 15916
743649fd
MW
15917 /* Record whether the function returns normally to its caller or not
15918 if the DWARF producer set that information. */
15919 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
15920 if (attr && (DW_UNSND (attr) != 0))
15921 TYPE_NO_RETURN (ftype) = 1;
15922
76c10ea2
GM
15923 /* We need to add the subroutine type to the die immediately so
15924 we don't infinitely recurse when dealing with parameters
0963b4bd 15925 declared as the same subroutine type. */
76c10ea2 15926 set_die_type (die, ftype, cu);
6e70227d 15927
639d11d3 15928 if (die->child != NULL)
c906108c 15929 {
bb5ed363 15930 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 15931 struct die_info *child_die;
8072405b 15932 int nparams, iparams;
c906108c
SS
15933
15934 /* Count the number of parameters.
15935 FIXME: GDB currently ignores vararg functions, but knows about
15936 vararg member functions. */
8072405b 15937 nparams = 0;
639d11d3 15938 child_die = die->child;
c906108c
SS
15939 while (child_die && child_die->tag)
15940 {
15941 if (child_die->tag == DW_TAG_formal_parameter)
15942 nparams++;
15943 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 15944 TYPE_VARARGS (ftype) = 1;
c906108c
SS
15945 child_die = sibling_die (child_die);
15946 }
15947
15948 /* Allocate storage for parameters and fill them in. */
15949 TYPE_NFIELDS (ftype) = nparams;
15950 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 15951 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 15952
8072405b
JK
15953 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
15954 even if we error out during the parameters reading below. */
15955 for (iparams = 0; iparams < nparams; iparams++)
15956 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
15957
15958 iparams = 0;
639d11d3 15959 child_die = die->child;
c906108c
SS
15960 while (child_die && child_die->tag)
15961 {
15962 if (child_die->tag == DW_TAG_formal_parameter)
15963 {
3ce3b1ba
PA
15964 struct type *arg_type;
15965
15966 /* DWARF version 2 has no clean way to discern C++
15967 static and non-static member functions. G++ helps
15968 GDB by marking the first parameter for non-static
15969 member functions (which is the this pointer) as
15970 artificial. We pass this information to
15971 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
15972
15973 DWARF version 3 added DW_AT_object_pointer, which GCC
15974 4.5 does not yet generate. */
e142c38c 15975 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
15976 if (attr)
15977 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
15978 else
9c37b5ae 15979 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
15980 arg_type = die_type (child_die, cu);
15981
15982 /* RealView does not mark THIS as const, which the testsuite
15983 expects. GCC marks THIS as const in method definitions,
15984 but not in the class specifications (GCC PR 43053). */
15985 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
15986 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
15987 {
15988 int is_this = 0;
15989 struct dwarf2_cu *arg_cu = cu;
15990 const char *name = dwarf2_name (child_die, cu);
15991
15992 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
15993 if (attr)
15994 {
15995 /* If the compiler emits this, use it. */
15996 if (follow_die_ref (die, attr, &arg_cu) == child_die)
15997 is_this = 1;
15998 }
15999 else if (name && strcmp (name, "this") == 0)
16000 /* Function definitions will have the argument names. */
16001 is_this = 1;
16002 else if (name == NULL && iparams == 0)
16003 /* Declarations may not have the names, so like
16004 elsewhere in GDB, assume an artificial first
16005 argument is "this". */
16006 is_this = 1;
16007
16008 if (is_this)
16009 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16010 arg_type, 0);
16011 }
16012
16013 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
16014 iparams++;
16015 }
16016 child_die = sibling_die (child_die);
16017 }
16018 }
16019
76c10ea2 16020 return ftype;
c906108c
SS
16021}
16022
f792889a 16023static struct type *
e7c27a73 16024read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16025{
e7c27a73 16026 struct objfile *objfile = cu->objfile;
0114d602 16027 const char *name = NULL;
3c8e0968 16028 struct type *this_type, *target_type;
c906108c 16029
94af9270 16030 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
16031 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16032 TYPE_TARGET_STUB (this_type) = 1;
f792889a 16033 set_die_type (die, this_type, cu);
3c8e0968
DE
16034 target_type = die_type (die, cu);
16035 if (target_type != this_type)
16036 TYPE_TARGET_TYPE (this_type) = target_type;
16037 else
16038 {
16039 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16040 spec and cause infinite loops in GDB. */
16041 complaint (&symfile_complaints,
16042 _("Self-referential DW_TAG_typedef "
16043 "- DIE at 0x%x [in module %s]"),
9c541725 16044 to_underlying (die->sect_off), objfile_name (objfile));
3c8e0968
DE
16045 TYPE_TARGET_TYPE (this_type) = NULL;
16046 }
f792889a 16047 return this_type;
c906108c
SS
16048}
16049
9b790ce7
UW
16050/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16051 (which may be different from NAME) to the architecture back-end to allow
16052 it to guess the correct format if necessary. */
16053
16054static struct type *
16055dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16056 const char *name_hint)
16057{
16058 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16059 const struct floatformat **format;
16060 struct type *type;
16061
16062 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16063 if (format)
16064 type = init_float_type (objfile, bits, name, format);
16065 else
77b7c781 16066 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
16067
16068 return type;
16069}
16070
c906108c
SS
16071/* Find a representation of a given base type and install
16072 it in the TYPE field of the die. */
16073
f792889a 16074static struct type *
e7c27a73 16075read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16076{
e7c27a73 16077 struct objfile *objfile = cu->objfile;
c906108c
SS
16078 struct type *type;
16079 struct attribute *attr;
19f392bc 16080 int encoding = 0, bits = 0;
15d034d0 16081 const char *name;
c906108c 16082
e142c38c 16083 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
16084 if (attr)
16085 {
16086 encoding = DW_UNSND (attr);
16087 }
e142c38c 16088 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16089 if (attr)
16090 {
19f392bc 16091 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 16092 }
39cbfefa 16093 name = dwarf2_name (die, cu);
6ccb9162 16094 if (!name)
c906108c 16095 {
6ccb9162
UW
16096 complaint (&symfile_complaints,
16097 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 16098 }
6ccb9162
UW
16099
16100 switch (encoding)
c906108c 16101 {
6ccb9162
UW
16102 case DW_ATE_address:
16103 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 16104 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 16105 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
16106 break;
16107 case DW_ATE_boolean:
19f392bc 16108 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
16109 break;
16110 case DW_ATE_complex_float:
9b790ce7 16111 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 16112 type = init_complex_type (objfile, name, type);
6ccb9162
UW
16113 break;
16114 case DW_ATE_decimal_float:
19f392bc 16115 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
16116 break;
16117 case DW_ATE_float:
9b790ce7 16118 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
16119 break;
16120 case DW_ATE_signed:
19f392bc 16121 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
16122 break;
16123 case DW_ATE_unsigned:
3b2b8fea
TT
16124 if (cu->language == language_fortran
16125 && name
61012eef 16126 && startswith (name, "character("))
19f392bc
UW
16127 type = init_character_type (objfile, bits, 1, name);
16128 else
16129 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
16130 break;
16131 case DW_ATE_signed_char:
6e70227d 16132 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
16133 || cu->language == language_pascal
16134 || cu->language == language_fortran)
19f392bc
UW
16135 type = init_character_type (objfile, bits, 0, name);
16136 else
16137 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
16138 break;
16139 case DW_ATE_unsigned_char:
868a0084 16140 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 16141 || cu->language == language_pascal
c44af4eb
TT
16142 || cu->language == language_fortran
16143 || cu->language == language_rust)
19f392bc
UW
16144 type = init_character_type (objfile, bits, 1, name);
16145 else
16146 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 16147 break;
75079b2b 16148 case DW_ATE_UTF:
53e710ac
PA
16149 {
16150 gdbarch *arch = get_objfile_arch (objfile);
16151
16152 if (bits == 16)
16153 type = builtin_type (arch)->builtin_char16;
16154 else if (bits == 32)
16155 type = builtin_type (arch)->builtin_char32;
16156 else
16157 {
16158 complaint (&symfile_complaints,
16159 _("unsupported DW_ATE_UTF bit size: '%d'"),
16160 bits);
16161 type = init_integer_type (objfile, bits, 1, name);
16162 }
16163 return set_die_type (die, type, cu);
16164 }
75079b2b
TT
16165 break;
16166
6ccb9162
UW
16167 default:
16168 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
16169 dwarf_type_encoding_name (encoding));
77b7c781 16170 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 16171 break;
c906108c 16172 }
6ccb9162 16173
0114d602 16174 if (name && strcmp (name, "char") == 0)
876cecd0 16175 TYPE_NOSIGN (type) = 1;
0114d602 16176
f792889a 16177 return set_die_type (die, type, cu);
c906108c
SS
16178}
16179
80180f79
SA
16180/* Parse dwarf attribute if it's a block, reference or constant and put the
16181 resulting value of the attribute into struct bound_prop.
16182 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
16183
16184static int
16185attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
16186 struct dwarf2_cu *cu, struct dynamic_prop *prop)
16187{
16188 struct dwarf2_property_baton *baton;
16189 struct obstack *obstack = &cu->objfile->objfile_obstack;
16190
16191 if (attr == NULL || prop == NULL)
16192 return 0;
16193
16194 if (attr_form_is_block (attr))
16195 {
8d749320 16196 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
16197 baton->referenced_type = NULL;
16198 baton->locexpr.per_cu = cu->per_cu;
16199 baton->locexpr.size = DW_BLOCK (attr)->size;
16200 baton->locexpr.data = DW_BLOCK (attr)->data;
16201 prop->data.baton = baton;
16202 prop->kind = PROP_LOCEXPR;
16203 gdb_assert (prop->data.baton != NULL);
16204 }
16205 else if (attr_form_is_ref (attr))
16206 {
16207 struct dwarf2_cu *target_cu = cu;
16208 struct die_info *target_die;
16209 struct attribute *target_attr;
16210
16211 target_die = follow_die_ref (die, attr, &target_cu);
16212 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
16213 if (target_attr == NULL)
16214 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
16215 target_cu);
80180f79
SA
16216 if (target_attr == NULL)
16217 return 0;
16218
df25ebbd 16219 switch (target_attr->name)
80180f79 16220 {
df25ebbd
JB
16221 case DW_AT_location:
16222 if (attr_form_is_section_offset (target_attr))
16223 {
8d749320 16224 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
16225 baton->referenced_type = die_type (target_die, target_cu);
16226 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
16227 prop->data.baton = baton;
16228 prop->kind = PROP_LOCLIST;
16229 gdb_assert (prop->data.baton != NULL);
16230 }
16231 else if (attr_form_is_block (target_attr))
16232 {
8d749320 16233 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
16234 baton->referenced_type = die_type (target_die, target_cu);
16235 baton->locexpr.per_cu = cu->per_cu;
16236 baton->locexpr.size = DW_BLOCK (target_attr)->size;
16237 baton->locexpr.data = DW_BLOCK (target_attr)->data;
16238 prop->data.baton = baton;
16239 prop->kind = PROP_LOCEXPR;
16240 gdb_assert (prop->data.baton != NULL);
16241 }
16242 else
16243 {
16244 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16245 "dynamic property");
16246 return 0;
16247 }
16248 break;
16249 case DW_AT_data_member_location:
16250 {
16251 LONGEST offset;
16252
16253 if (!handle_data_member_location (target_die, target_cu,
16254 &offset))
16255 return 0;
16256
8d749320 16257 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
16258 baton->referenced_type = read_type_die (target_die->parent,
16259 target_cu);
df25ebbd
JB
16260 baton->offset_info.offset = offset;
16261 baton->offset_info.type = die_type (target_die, target_cu);
16262 prop->data.baton = baton;
16263 prop->kind = PROP_ADDR_OFFSET;
16264 break;
16265 }
80180f79
SA
16266 }
16267 }
16268 else if (attr_form_is_constant (attr))
16269 {
16270 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
16271 prop->kind = PROP_CONST;
16272 }
16273 else
16274 {
16275 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
16276 dwarf2_name (die, cu));
16277 return 0;
16278 }
16279
16280 return 1;
16281}
16282
a02abb62
JB
16283/* Read the given DW_AT_subrange DIE. */
16284
f792889a 16285static struct type *
a02abb62
JB
16286read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
16287{
4c9ad8c2 16288 struct type *base_type, *orig_base_type;
a02abb62
JB
16289 struct type *range_type;
16290 struct attribute *attr;
729efb13 16291 struct dynamic_prop low, high;
4fae6e18 16292 int low_default_is_valid;
c451ebe5 16293 int high_bound_is_count = 0;
15d034d0 16294 const char *name;
43bbcdc2 16295 LONGEST negative_mask;
e77813c8 16296
4c9ad8c2
TT
16297 orig_base_type = die_type (die, cu);
16298 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
16299 whereas the real type might be. So, we use ORIG_BASE_TYPE when
16300 creating the range type, but we use the result of check_typedef
16301 when examining properties of the type. */
16302 base_type = check_typedef (orig_base_type);
a02abb62 16303
7e314c57
JK
16304 /* The die_type call above may have already set the type for this DIE. */
16305 range_type = get_die_type (die, cu);
16306 if (range_type)
16307 return range_type;
16308
729efb13
SA
16309 low.kind = PROP_CONST;
16310 high.kind = PROP_CONST;
16311 high.data.const_val = 0;
16312
4fae6e18
JK
16313 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
16314 omitting DW_AT_lower_bound. */
16315 switch (cu->language)
6e70227d 16316 {
4fae6e18
JK
16317 case language_c:
16318 case language_cplus:
729efb13 16319 low.data.const_val = 0;
4fae6e18
JK
16320 low_default_is_valid = 1;
16321 break;
16322 case language_fortran:
729efb13 16323 low.data.const_val = 1;
4fae6e18
JK
16324 low_default_is_valid = 1;
16325 break;
16326 case language_d:
4fae6e18 16327 case language_objc:
c44af4eb 16328 case language_rust:
729efb13 16329 low.data.const_val = 0;
4fae6e18
JK
16330 low_default_is_valid = (cu->header.version >= 4);
16331 break;
16332 case language_ada:
16333 case language_m2:
16334 case language_pascal:
729efb13 16335 low.data.const_val = 1;
4fae6e18
JK
16336 low_default_is_valid = (cu->header.version >= 4);
16337 break;
16338 default:
729efb13 16339 low.data.const_val = 0;
4fae6e18
JK
16340 low_default_is_valid = 0;
16341 break;
a02abb62
JB
16342 }
16343
e142c38c 16344 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 16345 if (attr)
11c1ba78 16346 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
16347 else if (!low_default_is_valid)
16348 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
16349 "- DIE at 0x%x [in module %s]"),
9c541725 16350 to_underlying (die->sect_off), objfile_name (cu->objfile));
a02abb62 16351
e142c38c 16352 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 16353 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
16354 {
16355 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 16356 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 16357 {
c451ebe5
SA
16358 /* If bounds are constant do the final calculation here. */
16359 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
16360 high.data.const_val = low.data.const_val + high.data.const_val - 1;
16361 else
16362 high_bound_is_count = 1;
c2ff108b 16363 }
e77813c8
PM
16364 }
16365
16366 /* Dwarf-2 specifications explicitly allows to create subrange types
16367 without specifying a base type.
16368 In that case, the base type must be set to the type of
16369 the lower bound, upper bound or count, in that order, if any of these
16370 three attributes references an object that has a type.
16371 If no base type is found, the Dwarf-2 specifications say that
16372 a signed integer type of size equal to the size of an address should
16373 be used.
16374 For the following C code: `extern char gdb_int [];'
16375 GCC produces an empty range DIE.
16376 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 16377 high bound or count are not yet handled by this code. */
e77813c8
PM
16378 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
16379 {
16380 struct objfile *objfile = cu->objfile;
16381 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16382 int addr_size = gdbarch_addr_bit (gdbarch) /8;
16383 struct type *int_type = objfile_type (objfile)->builtin_int;
16384
16385 /* Test "int", "long int", and "long long int" objfile types,
16386 and select the first one having a size above or equal to the
16387 architecture address size. */
16388 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16389 base_type = int_type;
16390 else
16391 {
16392 int_type = objfile_type (objfile)->builtin_long;
16393 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16394 base_type = int_type;
16395 else
16396 {
16397 int_type = objfile_type (objfile)->builtin_long_long;
16398 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16399 base_type = int_type;
16400 }
16401 }
16402 }
a02abb62 16403
dbb9c2b1
JB
16404 /* Normally, the DWARF producers are expected to use a signed
16405 constant form (Eg. DW_FORM_sdata) to express negative bounds.
16406 But this is unfortunately not always the case, as witnessed
16407 with GCC, for instance, where the ambiguous DW_FORM_dataN form
16408 is used instead. To work around that ambiguity, we treat
16409 the bounds as signed, and thus sign-extend their values, when
16410 the base type is signed. */
6e70227d 16411 negative_mask =
66c6502d 16412 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
16413 if (low.kind == PROP_CONST
16414 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
16415 low.data.const_val |= negative_mask;
16416 if (high.kind == PROP_CONST
16417 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
16418 high.data.const_val |= negative_mask;
43bbcdc2 16419
729efb13 16420 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 16421
c451ebe5
SA
16422 if (high_bound_is_count)
16423 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
16424
c2ff108b
JK
16425 /* Ada expects an empty array on no boundary attributes. */
16426 if (attr == NULL && cu->language != language_ada)
729efb13 16427 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 16428
39cbfefa
DJ
16429 name = dwarf2_name (die, cu);
16430 if (name)
16431 TYPE_NAME (range_type) = name;
6e70227d 16432
e142c38c 16433 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
16434 if (attr)
16435 TYPE_LENGTH (range_type) = DW_UNSND (attr);
16436
7e314c57
JK
16437 set_die_type (die, range_type, cu);
16438
16439 /* set_die_type should be already done. */
b4ba55a1
JB
16440 set_descriptive_type (range_type, die, cu);
16441
7e314c57 16442 return range_type;
a02abb62 16443}
6e70227d 16444
f792889a 16445static struct type *
81a17f79
JB
16446read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
16447{
16448 struct type *type;
81a17f79 16449
81a17f79
JB
16450 /* For now, we only support the C meaning of an unspecified type: void. */
16451
19f392bc 16452 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 16453 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 16454
f792889a 16455 return set_die_type (die, type, cu);
81a17f79 16456}
a02abb62 16457
639d11d3
DC
16458/* Read a single die and all its descendents. Set the die's sibling
16459 field to NULL; set other fields in the die correctly, and set all
16460 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
16461 location of the info_ptr after reading all of those dies. PARENT
16462 is the parent of the die in question. */
16463
16464static struct die_info *
dee91e82 16465read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
16466 const gdb_byte *info_ptr,
16467 const gdb_byte **new_info_ptr,
dee91e82 16468 struct die_info *parent)
639d11d3
DC
16469{
16470 struct die_info *die;
d521ce57 16471 const gdb_byte *cur_ptr;
639d11d3
DC
16472 int has_children;
16473
bf6af496 16474 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
16475 if (die == NULL)
16476 {
16477 *new_info_ptr = cur_ptr;
16478 return NULL;
16479 }
93311388 16480 store_in_ref_table (die, reader->cu);
639d11d3
DC
16481
16482 if (has_children)
bf6af496 16483 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
16484 else
16485 {
16486 die->child = NULL;
16487 *new_info_ptr = cur_ptr;
16488 }
16489
16490 die->sibling = NULL;
16491 die->parent = parent;
16492 return die;
16493}
16494
16495/* Read a die, all of its descendents, and all of its siblings; set
16496 all of the fields of all of the dies correctly. Arguments are as
16497 in read_die_and_children. */
16498
16499static struct die_info *
bf6af496 16500read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
16501 const gdb_byte *info_ptr,
16502 const gdb_byte **new_info_ptr,
bf6af496 16503 struct die_info *parent)
639d11d3
DC
16504{
16505 struct die_info *first_die, *last_sibling;
d521ce57 16506 const gdb_byte *cur_ptr;
639d11d3 16507
c906108c 16508 cur_ptr = info_ptr;
639d11d3
DC
16509 first_die = last_sibling = NULL;
16510
16511 while (1)
c906108c 16512 {
639d11d3 16513 struct die_info *die
dee91e82 16514 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 16515
1d325ec1 16516 if (die == NULL)
c906108c 16517 {
639d11d3
DC
16518 *new_info_ptr = cur_ptr;
16519 return first_die;
c906108c 16520 }
1d325ec1
DJ
16521
16522 if (!first_die)
16523 first_die = die;
c906108c 16524 else
1d325ec1
DJ
16525 last_sibling->sibling = die;
16526
16527 last_sibling = die;
c906108c 16528 }
c906108c
SS
16529}
16530
bf6af496
DE
16531/* Read a die, all of its descendents, and all of its siblings; set
16532 all of the fields of all of the dies correctly. Arguments are as
16533 in read_die_and_children.
16534 This the main entry point for reading a DIE and all its children. */
16535
16536static struct die_info *
16537read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
16538 const gdb_byte *info_ptr,
16539 const gdb_byte **new_info_ptr,
bf6af496
DE
16540 struct die_info *parent)
16541{
16542 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
16543 new_info_ptr, parent);
16544
b4f54984 16545 if (dwarf_die_debug)
bf6af496
DE
16546 {
16547 fprintf_unfiltered (gdb_stdlog,
16548 "Read die from %s@0x%x of %s:\n",
a32a8923 16549 get_section_name (reader->die_section),
bf6af496
DE
16550 (unsigned) (info_ptr - reader->die_section->buffer),
16551 bfd_get_filename (reader->abfd));
b4f54984 16552 dump_die (die, dwarf_die_debug);
bf6af496
DE
16553 }
16554
16555 return die;
16556}
16557
3019eac3
DE
16558/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
16559 attributes.
16560 The caller is responsible for filling in the extra attributes
16561 and updating (*DIEP)->num_attrs.
16562 Set DIEP to point to a newly allocated die with its information,
16563 except for its child, sibling, and parent fields.
16564 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 16565
d521ce57 16566static const gdb_byte *
3019eac3 16567read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 16568 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 16569 int *has_children, int num_extra_attrs)
93311388 16570{
b64f50a1 16571 unsigned int abbrev_number, bytes_read, i;
93311388
DE
16572 struct abbrev_info *abbrev;
16573 struct die_info *die;
16574 struct dwarf2_cu *cu = reader->cu;
16575 bfd *abfd = reader->abfd;
16576
9c541725 16577 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
16578 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16579 info_ptr += bytes_read;
16580 if (!abbrev_number)
16581 {
16582 *diep = NULL;
16583 *has_children = 0;
16584 return info_ptr;
16585 }
16586
433df2d4 16587 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 16588 if (!abbrev)
348e048f
DE
16589 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
16590 abbrev_number,
16591 bfd_get_filename (abfd));
16592
3019eac3 16593 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 16594 die->sect_off = sect_off;
93311388
DE
16595 die->tag = abbrev->tag;
16596 die->abbrev = abbrev_number;
16597
3019eac3
DE
16598 /* Make the result usable.
16599 The caller needs to update num_attrs after adding the extra
16600 attributes. */
93311388
DE
16601 die->num_attrs = abbrev->num_attrs;
16602
16603 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
16604 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
16605 info_ptr);
93311388
DE
16606
16607 *diep = die;
16608 *has_children = abbrev->has_children;
16609 return info_ptr;
16610}
16611
3019eac3
DE
16612/* Read a die and all its attributes.
16613 Set DIEP to point to a newly allocated die with its information,
16614 except for its child, sibling, and parent fields.
16615 Set HAS_CHILDREN to tell whether the die has children or not. */
16616
d521ce57 16617static const gdb_byte *
3019eac3 16618read_full_die (const struct die_reader_specs *reader,
d521ce57 16619 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
16620 int *has_children)
16621{
d521ce57 16622 const gdb_byte *result;
bf6af496
DE
16623
16624 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
16625
b4f54984 16626 if (dwarf_die_debug)
bf6af496
DE
16627 {
16628 fprintf_unfiltered (gdb_stdlog,
16629 "Read die from %s@0x%x of %s:\n",
a32a8923 16630 get_section_name (reader->die_section),
bf6af496
DE
16631 (unsigned) (info_ptr - reader->die_section->buffer),
16632 bfd_get_filename (reader->abfd));
b4f54984 16633 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
16634 }
16635
16636 return result;
3019eac3 16637}
433df2d4
DE
16638\f
16639/* Abbreviation tables.
3019eac3 16640
433df2d4 16641 In DWARF version 2, the description of the debugging information is
c906108c
SS
16642 stored in a separate .debug_abbrev section. Before we read any
16643 dies from a section we read in all abbreviations and install them
433df2d4
DE
16644 in a hash table. */
16645
16646/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
16647
16648static struct abbrev_info *
16649abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
16650{
16651 struct abbrev_info *abbrev;
16652
8d749320 16653 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 16654 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 16655
433df2d4
DE
16656 return abbrev;
16657}
16658
16659/* Add an abbreviation to the table. */
c906108c
SS
16660
16661static void
433df2d4
DE
16662abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
16663 unsigned int abbrev_number,
16664 struct abbrev_info *abbrev)
16665{
16666 unsigned int hash_number;
16667
16668 hash_number = abbrev_number % ABBREV_HASH_SIZE;
16669 abbrev->next = abbrev_table->abbrevs[hash_number];
16670 abbrev_table->abbrevs[hash_number] = abbrev;
16671}
dee91e82 16672
433df2d4
DE
16673/* Look up an abbrev in the table.
16674 Returns NULL if the abbrev is not found. */
16675
16676static struct abbrev_info *
16677abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
16678 unsigned int abbrev_number)
c906108c 16679{
433df2d4
DE
16680 unsigned int hash_number;
16681 struct abbrev_info *abbrev;
16682
16683 hash_number = abbrev_number % ABBREV_HASH_SIZE;
16684 abbrev = abbrev_table->abbrevs[hash_number];
16685
16686 while (abbrev)
16687 {
16688 if (abbrev->number == abbrev_number)
16689 return abbrev;
16690 abbrev = abbrev->next;
16691 }
16692 return NULL;
16693}
16694
16695/* Read in an abbrev table. */
16696
16697static struct abbrev_table *
16698abbrev_table_read_table (struct dwarf2_section_info *section,
9c541725 16699 sect_offset sect_off)
433df2d4
DE
16700{
16701 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 16702 bfd *abfd = get_section_bfd_owner (section);
433df2d4 16703 struct abbrev_table *abbrev_table;
d521ce57 16704 const gdb_byte *abbrev_ptr;
c906108c
SS
16705 struct abbrev_info *cur_abbrev;
16706 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 16707 unsigned int abbrev_form;
f3dd6933
DJ
16708 struct attr_abbrev *cur_attrs;
16709 unsigned int allocated_attrs;
c906108c 16710
70ba0933 16711 abbrev_table = XNEW (struct abbrev_table);
9c541725 16712 abbrev_table->sect_off = sect_off;
433df2d4 16713 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
16714 abbrev_table->abbrevs =
16715 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
16716 ABBREV_HASH_SIZE);
433df2d4
DE
16717 memset (abbrev_table->abbrevs, 0,
16718 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 16719
433df2d4 16720 dwarf2_read_section (objfile, section);
9c541725 16721 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
16722 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16723 abbrev_ptr += bytes_read;
16724
f3dd6933 16725 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 16726 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 16727
0963b4bd 16728 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
16729 while (abbrev_number)
16730 {
433df2d4 16731 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
16732
16733 /* read in abbrev header */
16734 cur_abbrev->number = abbrev_number;
aead7601
SM
16735 cur_abbrev->tag
16736 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
16737 abbrev_ptr += bytes_read;
16738 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
16739 abbrev_ptr += 1;
16740
16741 /* now read in declarations */
22d2f3ab 16742 for (;;)
c906108c 16743 {
43988095
JK
16744 LONGEST implicit_const;
16745
22d2f3ab
JK
16746 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16747 abbrev_ptr += bytes_read;
16748 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16749 abbrev_ptr += bytes_read;
43988095
JK
16750 if (abbrev_form == DW_FORM_implicit_const)
16751 {
16752 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
16753 &bytes_read);
16754 abbrev_ptr += bytes_read;
16755 }
16756 else
16757 {
16758 /* Initialize it due to a false compiler warning. */
16759 implicit_const = -1;
16760 }
22d2f3ab
JK
16761
16762 if (abbrev_name == 0)
16763 break;
16764
f3dd6933 16765 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 16766 {
f3dd6933
DJ
16767 allocated_attrs += ATTR_ALLOC_CHUNK;
16768 cur_attrs
224c3ddb 16769 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 16770 }
ae038cb0 16771
aead7601
SM
16772 cur_attrs[cur_abbrev->num_attrs].name
16773 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 16774 cur_attrs[cur_abbrev->num_attrs].form
aead7601 16775 = (enum dwarf_form) abbrev_form;
43988095 16776 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 16777 ++cur_abbrev->num_attrs;
c906108c
SS
16778 }
16779
8d749320
SM
16780 cur_abbrev->attrs =
16781 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
16782 cur_abbrev->num_attrs);
f3dd6933
DJ
16783 memcpy (cur_abbrev->attrs, cur_attrs,
16784 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
16785
433df2d4 16786 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
16787
16788 /* Get next abbreviation.
16789 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
16790 always properly terminated with an abbrev number of 0.
16791 Exit loop if we encounter an abbreviation which we have
16792 already read (which means we are about to read the abbreviations
16793 for the next compile unit) or if the end of the abbreviation
16794 table is reached. */
433df2d4 16795 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
16796 break;
16797 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16798 abbrev_ptr += bytes_read;
433df2d4 16799 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
16800 break;
16801 }
f3dd6933
DJ
16802
16803 xfree (cur_attrs);
433df2d4 16804 return abbrev_table;
c906108c
SS
16805}
16806
433df2d4 16807/* Free the resources held by ABBREV_TABLE. */
c906108c 16808
c906108c 16809static void
433df2d4 16810abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 16811{
433df2d4
DE
16812 obstack_free (&abbrev_table->abbrev_obstack, NULL);
16813 xfree (abbrev_table);
c906108c
SS
16814}
16815
f4dc4d17
DE
16816/* Same as abbrev_table_free but as a cleanup.
16817 We pass in a pointer to the pointer to the table so that we can
16818 set the pointer to NULL when we're done. It also simplifies
73051182 16819 build_type_psymtabs_1. */
f4dc4d17
DE
16820
16821static void
16822abbrev_table_free_cleanup (void *table_ptr)
16823{
9a3c8263 16824 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
16825
16826 if (*abbrev_table_ptr != NULL)
16827 abbrev_table_free (*abbrev_table_ptr);
16828 *abbrev_table_ptr = NULL;
16829}
16830
433df2d4
DE
16831/* Read the abbrev table for CU from ABBREV_SECTION. */
16832
16833static void
16834dwarf2_read_abbrevs (struct dwarf2_cu *cu,
16835 struct dwarf2_section_info *abbrev_section)
c906108c 16836{
433df2d4 16837 cu->abbrev_table =
9c541725 16838 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
433df2d4 16839}
c906108c 16840
433df2d4 16841/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 16842
433df2d4
DE
16843static void
16844dwarf2_free_abbrev_table (void *ptr_to_cu)
16845{
9a3c8263 16846 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 16847
a2ce51a0
DE
16848 if (cu->abbrev_table != NULL)
16849 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
16850 /* Set this to NULL so that we SEGV if we try to read it later,
16851 and also because free_comp_unit verifies this is NULL. */
16852 cu->abbrev_table = NULL;
16853}
16854\f
72bf9492
DJ
16855/* Returns nonzero if TAG represents a type that we might generate a partial
16856 symbol for. */
16857
16858static int
16859is_type_tag_for_partial (int tag)
16860{
16861 switch (tag)
16862 {
16863#if 0
16864 /* Some types that would be reasonable to generate partial symbols for,
16865 that we don't at present. */
16866 case DW_TAG_array_type:
16867 case DW_TAG_file_type:
16868 case DW_TAG_ptr_to_member_type:
16869 case DW_TAG_set_type:
16870 case DW_TAG_string_type:
16871 case DW_TAG_subroutine_type:
16872#endif
16873 case DW_TAG_base_type:
16874 case DW_TAG_class_type:
680b30c7 16875 case DW_TAG_interface_type:
72bf9492
DJ
16876 case DW_TAG_enumeration_type:
16877 case DW_TAG_structure_type:
16878 case DW_TAG_subrange_type:
16879 case DW_TAG_typedef:
16880 case DW_TAG_union_type:
16881 return 1;
16882 default:
16883 return 0;
16884 }
16885}
16886
16887/* Load all DIEs that are interesting for partial symbols into memory. */
16888
16889static struct partial_die_info *
dee91e82 16890load_partial_dies (const struct die_reader_specs *reader,
d521ce57 16891 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 16892{
dee91e82 16893 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16894 struct objfile *objfile = cu->objfile;
72bf9492
DJ
16895 struct partial_die_info *part_die;
16896 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
16897 struct abbrev_info *abbrev;
16898 unsigned int bytes_read;
5afb4e99 16899 unsigned int load_all = 0;
72bf9492
DJ
16900 int nesting_level = 1;
16901
16902 parent_die = NULL;
16903 last_die = NULL;
16904
7adf1e79
DE
16905 gdb_assert (cu->per_cu != NULL);
16906 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
16907 load_all = 1;
16908
72bf9492
DJ
16909 cu->partial_dies
16910 = htab_create_alloc_ex (cu->header.length / 12,
16911 partial_die_hash,
16912 partial_die_eq,
16913 NULL,
16914 &cu->comp_unit_obstack,
16915 hashtab_obstack_allocate,
16916 dummy_obstack_deallocate);
16917
8d749320 16918 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16919
16920 while (1)
16921 {
16922 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
16923
16924 /* A NULL abbrev means the end of a series of children. */
16925 if (abbrev == NULL)
16926 {
16927 if (--nesting_level == 0)
16928 {
16929 /* PART_DIE was probably the last thing allocated on the
16930 comp_unit_obstack, so we could call obstack_free
16931 here. We don't do that because the waste is small,
16932 and will be cleaned up when we're done with this
16933 compilation unit. This way, we're also more robust
16934 against other users of the comp_unit_obstack. */
16935 return first_die;
16936 }
16937 info_ptr += bytes_read;
16938 last_die = parent_die;
16939 parent_die = parent_die->die_parent;
16940 continue;
16941 }
16942
98bfdba5
PA
16943 /* Check for template arguments. We never save these; if
16944 they're seen, we just mark the parent, and go on our way. */
16945 if (parent_die != NULL
16946 && cu->language == language_cplus
16947 && (abbrev->tag == DW_TAG_template_type_param
16948 || abbrev->tag == DW_TAG_template_value_param))
16949 {
16950 parent_die->has_template_arguments = 1;
16951
16952 if (!load_all)
16953 {
16954 /* We don't need a partial DIE for the template argument. */
dee91e82 16955 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
16956 continue;
16957 }
16958 }
16959
0d99eb77 16960 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
16961 Skip their other children. */
16962 if (!load_all
16963 && cu->language == language_cplus
16964 && parent_die != NULL
16965 && parent_die->tag == DW_TAG_subprogram)
16966 {
dee91e82 16967 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
16968 continue;
16969 }
16970
5afb4e99
DJ
16971 /* Check whether this DIE is interesting enough to save. Normally
16972 we would not be interested in members here, but there may be
16973 later variables referencing them via DW_AT_specification (for
16974 static members). */
16975 if (!load_all
16976 && !is_type_tag_for_partial (abbrev->tag)
72929c62 16977 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
16978 && abbrev->tag != DW_TAG_enumerator
16979 && abbrev->tag != DW_TAG_subprogram
bc30ff58 16980 && abbrev->tag != DW_TAG_lexical_block
72bf9492 16981 && abbrev->tag != DW_TAG_variable
5afb4e99 16982 && abbrev->tag != DW_TAG_namespace
f55ee35c 16983 && abbrev->tag != DW_TAG_module
95554aad 16984 && abbrev->tag != DW_TAG_member
74921315
KS
16985 && abbrev->tag != DW_TAG_imported_unit
16986 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
16987 {
16988 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16989 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
16990 continue;
16991 }
16992
dee91e82
DE
16993 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
16994 info_ptr);
72bf9492
DJ
16995
16996 /* This two-pass algorithm for processing partial symbols has a
16997 high cost in cache pressure. Thus, handle some simple cases
16998 here which cover the majority of C partial symbols. DIEs
16999 which neither have specification tags in them, nor could have
17000 specification tags elsewhere pointing at them, can simply be
17001 processed and discarded.
17002
17003 This segment is also optional; scan_partial_symbols and
17004 add_partial_symbol will handle these DIEs if we chain
17005 them in normally. When compilers which do not emit large
17006 quantities of duplicate debug information are more common,
17007 this code can probably be removed. */
17008
17009 /* Any complete simple types at the top level (pretty much all
17010 of them, for a language without namespaces), can be processed
17011 directly. */
17012 if (parent_die == NULL
17013 && part_die->has_specification == 0
17014 && part_die->is_declaration == 0
d8228535 17015 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
17016 || part_die->tag == DW_TAG_base_type
17017 || part_die->tag == DW_TAG_subrange_type))
17018 {
17019 if (building_psymtab && part_die->name != NULL)
04a679b8 17020 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 17021 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 17022 &objfile->static_psymbols,
1762568f 17023 0, cu->language, objfile);
dee91e82 17024 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
17025 continue;
17026 }
17027
d8228535
JK
17028 /* The exception for DW_TAG_typedef with has_children above is
17029 a workaround of GCC PR debug/47510. In the case of this complaint
17030 type_name_no_tag_or_error will error on such types later.
17031
17032 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17033 it could not find the child DIEs referenced later, this is checked
17034 above. In correct DWARF DW_TAG_typedef should have no children. */
17035
17036 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
17037 complaint (&symfile_complaints,
17038 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17039 "- DIE at 0x%x [in module %s]"),
9c541725 17040 to_underlying (part_die->sect_off), objfile_name (objfile));
d8228535 17041
72bf9492
DJ
17042 /* If we're at the second level, and we're an enumerator, and
17043 our parent has no specification (meaning possibly lives in a
17044 namespace elsewhere), then we can add the partial symbol now
17045 instead of queueing it. */
17046 if (part_die->tag == DW_TAG_enumerator
17047 && parent_die != NULL
17048 && parent_die->die_parent == NULL
17049 && parent_die->tag == DW_TAG_enumeration_type
17050 && parent_die->has_specification == 0)
17051 {
17052 if (part_die->name == NULL)
3e43a32a
MS
17053 complaint (&symfile_complaints,
17054 _("malformed enumerator DIE ignored"));
72bf9492 17055 else if (building_psymtab)
04a679b8 17056 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 17057 VAR_DOMAIN, LOC_CONST,
9c37b5ae 17058 cu->language == language_cplus
bb5ed363
DE
17059 ? &objfile->global_psymbols
17060 : &objfile->static_psymbols,
1762568f 17061 0, cu->language, objfile);
72bf9492 17062
dee91e82 17063 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
17064 continue;
17065 }
17066
17067 /* We'll save this DIE so link it in. */
17068 part_die->die_parent = parent_die;
17069 part_die->die_sibling = NULL;
17070 part_die->die_child = NULL;
17071
17072 if (last_die && last_die == parent_die)
17073 last_die->die_child = part_die;
17074 else if (last_die)
17075 last_die->die_sibling = part_die;
17076
17077 last_die = part_die;
17078
17079 if (first_die == NULL)
17080 first_die = part_die;
17081
17082 /* Maybe add the DIE to the hash table. Not all DIEs that we
17083 find interesting need to be in the hash table, because we
17084 also have the parent/sibling/child chains; only those that we
17085 might refer to by offset later during partial symbol reading.
17086
17087 For now this means things that might have be the target of a
17088 DW_AT_specification, DW_AT_abstract_origin, or
17089 DW_AT_extension. DW_AT_extension will refer only to
17090 namespaces; DW_AT_abstract_origin refers to functions (and
17091 many things under the function DIE, but we do not recurse
17092 into function DIEs during partial symbol reading) and
17093 possibly variables as well; DW_AT_specification refers to
17094 declarations. Declarations ought to have the DW_AT_declaration
17095 flag. It happens that GCC forgets to put it in sometimes, but
17096 only for functions, not for types.
17097
17098 Adding more things than necessary to the hash table is harmless
17099 except for the performance cost. Adding too few will result in
5afb4e99
DJ
17100 wasted time in find_partial_die, when we reread the compilation
17101 unit with load_all_dies set. */
72bf9492 17102
5afb4e99 17103 if (load_all
72929c62 17104 || abbrev->tag == DW_TAG_constant
5afb4e99 17105 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
17106 || abbrev->tag == DW_TAG_variable
17107 || abbrev->tag == DW_TAG_namespace
17108 || part_die->is_declaration)
17109 {
17110 void **slot;
17111
17112 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
17113 to_underlying (part_die->sect_off),
17114 INSERT);
72bf9492
DJ
17115 *slot = part_die;
17116 }
17117
8d749320 17118 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
17119
17120 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 17121 we have no reason to follow the children of structures; for other
98bfdba5
PA
17122 languages we have to, so that we can get at method physnames
17123 to infer fully qualified class names, for DW_AT_specification,
17124 and for C++ template arguments. For C++, we also look one level
17125 inside functions to find template arguments (if the name of the
17126 function does not already contain the template arguments).
bc30ff58
JB
17127
17128 For Ada, we need to scan the children of subprograms and lexical
17129 blocks as well because Ada allows the definition of nested
17130 entities that could be interesting for the debugger, such as
17131 nested subprograms for instance. */
72bf9492 17132 if (last_die->has_children
5afb4e99
DJ
17133 && (load_all
17134 || last_die->tag == DW_TAG_namespace
f55ee35c 17135 || last_die->tag == DW_TAG_module
72bf9492 17136 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
17137 || (cu->language == language_cplus
17138 && last_die->tag == DW_TAG_subprogram
17139 && (last_die->name == NULL
17140 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
17141 || (cu->language != language_c
17142 && (last_die->tag == DW_TAG_class_type
680b30c7 17143 || last_die->tag == DW_TAG_interface_type
72bf9492 17144 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
17145 || last_die->tag == DW_TAG_union_type))
17146 || (cu->language == language_ada
17147 && (last_die->tag == DW_TAG_subprogram
17148 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
17149 {
17150 nesting_level++;
17151 parent_die = last_die;
17152 continue;
17153 }
17154
17155 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17156 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
17157
17158 /* Back to the top, do it again. */
17159 }
17160}
17161
c906108c
SS
17162/* Read a minimal amount of information into the minimal die structure. */
17163
d521ce57 17164static const gdb_byte *
dee91e82
DE
17165read_partial_die (const struct die_reader_specs *reader,
17166 struct partial_die_info *part_die,
17167 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 17168 const gdb_byte *info_ptr)
c906108c 17169{
dee91e82 17170 struct dwarf2_cu *cu = reader->cu;
bb5ed363 17171 struct objfile *objfile = cu->objfile;
d521ce57 17172 const gdb_byte *buffer = reader->buffer;
fa238c03 17173 unsigned int i;
c906108c 17174 struct attribute attr;
c5aa993b 17175 int has_low_pc_attr = 0;
c906108c 17176 int has_high_pc_attr = 0;
91da1414 17177 int high_pc_relative = 0;
c906108c 17178
72bf9492 17179 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 17180
9c541725 17181 part_die->sect_off = (sect_offset) (info_ptr - buffer);
72bf9492
DJ
17182
17183 info_ptr += abbrev_len;
17184
17185 if (abbrev == NULL)
17186 return info_ptr;
17187
c906108c
SS
17188 part_die->tag = abbrev->tag;
17189 part_die->has_children = abbrev->has_children;
c906108c
SS
17190
17191 for (i = 0; i < abbrev->num_attrs; ++i)
17192 {
dee91e82 17193 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
17194
17195 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 17196 partial symbol table. */
c906108c
SS
17197 switch (attr.name)
17198 {
17199 case DW_AT_name:
71c25dea
TT
17200 switch (part_die->tag)
17201 {
17202 case DW_TAG_compile_unit:
95554aad 17203 case DW_TAG_partial_unit:
348e048f 17204 case DW_TAG_type_unit:
71c25dea
TT
17205 /* Compilation units have a DW_AT_name that is a filename, not
17206 a source language identifier. */
17207 case DW_TAG_enumeration_type:
17208 case DW_TAG_enumerator:
17209 /* These tags always have simple identifiers already; no need
17210 to canonicalize them. */
17211 part_die->name = DW_STRING (&attr);
17212 break;
17213 default:
17214 part_die->name
17215 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 17216 &objfile->per_bfd->storage_obstack);
71c25dea
TT
17217 break;
17218 }
c906108c 17219 break;
31ef98ae 17220 case DW_AT_linkage_name:
c906108c 17221 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
17222 /* Note that both forms of linkage name might appear. We
17223 assume they will be the same, and we only store the last
17224 one we see. */
94af9270
KS
17225 if (cu->language == language_ada)
17226 part_die->name = DW_STRING (&attr);
abc72ce4 17227 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
17228 break;
17229 case DW_AT_low_pc:
17230 has_low_pc_attr = 1;
31aa7e4e 17231 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
17232 break;
17233 case DW_AT_high_pc:
17234 has_high_pc_attr = 1;
31aa7e4e
JB
17235 part_die->highpc = attr_value_as_address (&attr);
17236 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
17237 high_pc_relative = 1;
c906108c
SS
17238 break;
17239 case DW_AT_location:
0963b4bd 17240 /* Support the .debug_loc offsets. */
8e19ed76
PS
17241 if (attr_form_is_block (&attr))
17242 {
95554aad 17243 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 17244 }
3690dd37 17245 else if (attr_form_is_section_offset (&attr))
8e19ed76 17246 {
4d3c2250 17247 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
17248 }
17249 else
17250 {
4d3c2250
KB
17251 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17252 "partial symbol information");
8e19ed76 17253 }
c906108c 17254 break;
c906108c
SS
17255 case DW_AT_external:
17256 part_die->is_external = DW_UNSND (&attr);
17257 break;
17258 case DW_AT_declaration:
17259 part_die->is_declaration = DW_UNSND (&attr);
17260 break;
17261 case DW_AT_type:
17262 part_die->has_type = 1;
17263 break;
17264 case DW_AT_abstract_origin:
17265 case DW_AT_specification:
72bf9492
DJ
17266 case DW_AT_extension:
17267 part_die->has_specification = 1;
c764a876 17268 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
17269 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17270 || cu->per_cu->is_dwz);
c906108c
SS
17271 break;
17272 case DW_AT_sibling:
17273 /* Ignore absolute siblings, they might point outside of
17274 the current compile unit. */
17275 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
17276 complaint (&symfile_complaints,
17277 _("ignoring absolute DW_AT_sibling"));
c906108c 17278 else
b9502d3f 17279 {
9c541725
PA
17280 sect_offset off = dwarf2_get_ref_die_offset (&attr);
17281 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
17282
17283 if (sibling_ptr < info_ptr)
17284 complaint (&symfile_complaints,
17285 _("DW_AT_sibling points backwards"));
22869d73
KS
17286 else if (sibling_ptr > reader->buffer_end)
17287 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
17288 else
17289 part_die->sibling = sibling_ptr;
17290 }
c906108c 17291 break;
fa4028e9
JB
17292 case DW_AT_byte_size:
17293 part_die->has_byte_size = 1;
17294 break;
ff908ebf
AW
17295 case DW_AT_const_value:
17296 part_die->has_const_value = 1;
17297 break;
68511cec
CES
17298 case DW_AT_calling_convention:
17299 /* DWARF doesn't provide a way to identify a program's source-level
17300 entry point. DW_AT_calling_convention attributes are only meant
17301 to describe functions' calling conventions.
17302
17303 However, because it's a necessary piece of information in
0c1b455e
TT
17304 Fortran, and before DWARF 4 DW_CC_program was the only
17305 piece of debugging information whose definition refers to
17306 a 'main program' at all, several compilers marked Fortran
17307 main programs with DW_CC_program --- even when those
17308 functions use the standard calling conventions.
17309
17310 Although DWARF now specifies a way to provide this
17311 information, we support this practice for backward
17312 compatibility. */
68511cec 17313 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
17314 && cu->language == language_fortran)
17315 part_die->main_subprogram = 1;
68511cec 17316 break;
481860b3
GB
17317 case DW_AT_inline:
17318 if (DW_UNSND (&attr) == DW_INL_inlined
17319 || DW_UNSND (&attr) == DW_INL_declared_inlined)
17320 part_die->may_be_inlined = 1;
17321 break;
95554aad
TT
17322
17323 case DW_AT_import:
17324 if (part_die->tag == DW_TAG_imported_unit)
36586728 17325 {
9c541725 17326 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
36586728
TT
17327 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17328 || cu->per_cu->is_dwz);
17329 }
95554aad
TT
17330 break;
17331
0c1b455e
TT
17332 case DW_AT_main_subprogram:
17333 part_die->main_subprogram = DW_UNSND (&attr);
17334 break;
17335
c906108c
SS
17336 default:
17337 break;
17338 }
17339 }
17340
91da1414
MW
17341 if (high_pc_relative)
17342 part_die->highpc += part_die->lowpc;
17343
9373cf26
JK
17344 if (has_low_pc_attr && has_high_pc_attr)
17345 {
17346 /* When using the GNU linker, .gnu.linkonce. sections are used to
17347 eliminate duplicate copies of functions and vtables and such.
17348 The linker will arbitrarily choose one and discard the others.
17349 The AT_*_pc values for such functions refer to local labels in
17350 these sections. If the section from that file was discarded, the
17351 labels are not in the output, so the relocs get a value of 0.
17352 If this is a discarded function, mark the pc bounds as invalid,
17353 so that GDB will ignore it. */
17354 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
17355 {
bb5ed363 17356 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
17357
17358 complaint (&symfile_complaints,
17359 _("DW_AT_low_pc %s is zero "
17360 "for DIE at 0x%x [in module %s]"),
17361 paddress (gdbarch, part_die->lowpc),
9c541725 17362 to_underlying (part_die->sect_off), objfile_name (objfile));
9373cf26
JK
17363 }
17364 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
17365 else if (part_die->lowpc >= part_die->highpc)
17366 {
bb5ed363 17367 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
17368
17369 complaint (&symfile_complaints,
17370 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
17371 "for DIE at 0x%x [in module %s]"),
17372 paddress (gdbarch, part_die->lowpc),
17373 paddress (gdbarch, part_die->highpc),
9c541725
PA
17374 to_underlying (part_die->sect_off),
17375 objfile_name (objfile));
9373cf26
JK
17376 }
17377 else
17378 part_die->has_pc_info = 1;
17379 }
85cbf3d3 17380
c906108c
SS
17381 return info_ptr;
17382}
17383
72bf9492
DJ
17384/* Find a cached partial DIE at OFFSET in CU. */
17385
17386static struct partial_die_info *
9c541725 17387find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
72bf9492
DJ
17388{
17389 struct partial_die_info *lookup_die = NULL;
17390 struct partial_die_info part_die;
17391
9c541725 17392 part_die.sect_off = sect_off;
9a3c8263
SM
17393 lookup_die = ((struct partial_die_info *)
17394 htab_find_with_hash (cu->partial_dies, &part_die,
9c541725 17395 to_underlying (sect_off)));
72bf9492 17396
72bf9492
DJ
17397 return lookup_die;
17398}
17399
348e048f
DE
17400/* Find a partial DIE at OFFSET, which may or may not be in CU,
17401 except in the case of .debug_types DIEs which do not reference
17402 outside their CU (they do however referencing other types via
55f1336d 17403 DW_FORM_ref_sig8). */
72bf9492
DJ
17404
17405static struct partial_die_info *
9c541725 17406find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 17407{
bb5ed363 17408 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
17409 struct dwarf2_per_cu_data *per_cu = NULL;
17410 struct partial_die_info *pd = NULL;
72bf9492 17411
36586728 17412 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 17413 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 17414 {
9c541725 17415 pd = find_partial_die_in_comp_unit (sect_off, cu);
5afb4e99
DJ
17416 if (pd != NULL)
17417 return pd;
0d99eb77
DE
17418 /* We missed recording what we needed.
17419 Load all dies and try again. */
17420 per_cu = cu->per_cu;
5afb4e99 17421 }
0d99eb77
DE
17422 else
17423 {
17424 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 17425 if (cu->per_cu->is_debug_types)
0d99eb77 17426 {
9c541725
PA
17427 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
17428 " external reference to offset 0x%x [in module %s].\n"),
17429 to_underlying (cu->header.sect_off), to_underlying (sect_off),
0d99eb77
DE
17430 bfd_get_filename (objfile->obfd));
17431 }
9c541725 17432 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 17433 objfile);
72bf9492 17434
0d99eb77
DE
17435 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
17436 load_partial_comp_unit (per_cu);
ae038cb0 17437
0d99eb77 17438 per_cu->cu->last_used = 0;
9c541725 17439 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
0d99eb77 17440 }
5afb4e99 17441
dee91e82
DE
17442 /* If we didn't find it, and not all dies have been loaded,
17443 load them all and try again. */
17444
5afb4e99
DJ
17445 if (pd == NULL && per_cu->load_all_dies == 0)
17446 {
5afb4e99 17447 per_cu->load_all_dies = 1;
fd820528
DE
17448
17449 /* This is nasty. When we reread the DIEs, somewhere up the call chain
17450 THIS_CU->cu may already be in use. So we can't just free it and
17451 replace its DIEs with the ones we read in. Instead, we leave those
17452 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
17453 and clobber THIS_CU->cu->partial_dies with the hash table for the new
17454 set. */
dee91e82 17455 load_partial_comp_unit (per_cu);
5afb4e99 17456
9c541725 17457 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
5afb4e99
DJ
17458 }
17459
17460 if (pd == NULL)
17461 internal_error (__FILE__, __LINE__,
3e43a32a
MS
17462 _("could not find partial DIE 0x%x "
17463 "in cache [from module %s]\n"),
9c541725 17464 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 17465 return pd;
72bf9492
DJ
17466}
17467
abc72ce4
DE
17468/* See if we can figure out if the class lives in a namespace. We do
17469 this by looking for a member function; its demangled name will
17470 contain namespace info, if there is any. */
17471
17472static void
17473guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
17474 struct dwarf2_cu *cu)
17475{
17476 /* NOTE: carlton/2003-10-07: Getting the info this way changes
17477 what template types look like, because the demangler
17478 frequently doesn't give the same name as the debug info. We
17479 could fix this by only using the demangled name to get the
17480 prefix (but see comment in read_structure_type). */
17481
17482 struct partial_die_info *real_pdi;
17483 struct partial_die_info *child_pdi;
17484
17485 /* If this DIE (this DIE's specification, if any) has a parent, then
17486 we should not do this. We'll prepend the parent's fully qualified
17487 name when we create the partial symbol. */
17488
17489 real_pdi = struct_pdi;
17490 while (real_pdi->has_specification)
36586728
TT
17491 real_pdi = find_partial_die (real_pdi->spec_offset,
17492 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
17493
17494 if (real_pdi->die_parent != NULL)
17495 return;
17496
17497 for (child_pdi = struct_pdi->die_child;
17498 child_pdi != NULL;
17499 child_pdi = child_pdi->die_sibling)
17500 {
17501 if (child_pdi->tag == DW_TAG_subprogram
17502 && child_pdi->linkage_name != NULL)
17503 {
17504 char *actual_class_name
17505 = language_class_name_from_physname (cu->language_defn,
17506 child_pdi->linkage_name);
17507 if (actual_class_name != NULL)
17508 {
17509 struct_pdi->name
224c3ddb
SM
17510 = ((const char *)
17511 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
17512 actual_class_name,
17513 strlen (actual_class_name)));
abc72ce4
DE
17514 xfree (actual_class_name);
17515 }
17516 break;
17517 }
17518 }
17519}
17520
72bf9492
DJ
17521/* Adjust PART_DIE before generating a symbol for it. This function
17522 may set the is_external flag or change the DIE's name. */
17523
17524static void
17525fixup_partial_die (struct partial_die_info *part_die,
17526 struct dwarf2_cu *cu)
17527{
abc72ce4
DE
17528 /* Once we've fixed up a die, there's no point in doing so again.
17529 This also avoids a memory leak if we were to call
17530 guess_partial_die_structure_name multiple times. */
17531 if (part_die->fixup_called)
17532 return;
17533
72bf9492
DJ
17534 /* If we found a reference attribute and the DIE has no name, try
17535 to find a name in the referred to DIE. */
17536
17537 if (part_die->name == NULL && part_die->has_specification)
17538 {
17539 struct partial_die_info *spec_die;
72bf9492 17540
36586728
TT
17541 spec_die = find_partial_die (part_die->spec_offset,
17542 part_die->spec_is_dwz, cu);
72bf9492 17543
10b3939b 17544 fixup_partial_die (spec_die, cu);
72bf9492
DJ
17545
17546 if (spec_die->name)
17547 {
17548 part_die->name = spec_die->name;
17549
17550 /* Copy DW_AT_external attribute if it is set. */
17551 if (spec_die->is_external)
17552 part_die->is_external = spec_die->is_external;
17553 }
17554 }
17555
17556 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
17557
17558 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 17559 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 17560
abc72ce4
DE
17561 /* If there is no parent die to provide a namespace, and there are
17562 children, see if we can determine the namespace from their linkage
122d1940 17563 name. */
abc72ce4 17564 if (cu->language == language_cplus
8b70b953 17565 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
17566 && part_die->die_parent == NULL
17567 && part_die->has_children
17568 && (part_die->tag == DW_TAG_class_type
17569 || part_die->tag == DW_TAG_structure_type
17570 || part_die->tag == DW_TAG_union_type))
17571 guess_partial_die_structure_name (part_die, cu);
17572
53832f31
TT
17573 /* GCC might emit a nameless struct or union that has a linkage
17574 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17575 if (part_die->name == NULL
96408a79
SA
17576 && (part_die->tag == DW_TAG_class_type
17577 || part_die->tag == DW_TAG_interface_type
17578 || part_die->tag == DW_TAG_structure_type
17579 || part_die->tag == DW_TAG_union_type)
53832f31
TT
17580 && part_die->linkage_name != NULL)
17581 {
17582 char *demangled;
17583
8de20a37 17584 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
17585 if (demangled)
17586 {
96408a79
SA
17587 const char *base;
17588
17589 /* Strip any leading namespaces/classes, keep only the base name.
17590 DW_AT_name for named DIEs does not contain the prefixes. */
17591 base = strrchr (demangled, ':');
17592 if (base && base > demangled && base[-1] == ':')
17593 base++;
17594 else
17595 base = demangled;
17596
34a68019 17597 part_die->name
224c3ddb
SM
17598 = ((const char *)
17599 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
17600 base, strlen (base)));
53832f31
TT
17601 xfree (demangled);
17602 }
17603 }
17604
abc72ce4 17605 part_die->fixup_called = 1;
72bf9492
DJ
17606}
17607
a8329558 17608/* Read an attribute value described by an attribute form. */
c906108c 17609
d521ce57 17610static const gdb_byte *
dee91e82
DE
17611read_attribute_value (const struct die_reader_specs *reader,
17612 struct attribute *attr, unsigned form,
43988095 17613 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 17614{
dee91e82 17615 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
17616 struct objfile *objfile = cu->objfile;
17617 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 17618 bfd *abfd = reader->abfd;
e7c27a73 17619 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
17620 unsigned int bytes_read;
17621 struct dwarf_block *blk;
17622
aead7601 17623 attr->form = (enum dwarf_form) form;
a8329558 17624 switch (form)
c906108c 17625 {
c906108c 17626 case DW_FORM_ref_addr:
ae411497 17627 if (cu->header.version == 2)
4568ecf9 17628 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 17629 else
4568ecf9
DE
17630 DW_UNSND (attr) = read_offset (abfd, info_ptr,
17631 &cu->header, &bytes_read);
ae411497
TT
17632 info_ptr += bytes_read;
17633 break;
36586728
TT
17634 case DW_FORM_GNU_ref_alt:
17635 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
17636 info_ptr += bytes_read;
17637 break;
ae411497 17638 case DW_FORM_addr:
e7c27a73 17639 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 17640 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 17641 info_ptr += bytes_read;
c906108c
SS
17642 break;
17643 case DW_FORM_block2:
7b5a2f43 17644 blk = dwarf_alloc_block (cu);
c906108c
SS
17645 blk->size = read_2_bytes (abfd, info_ptr);
17646 info_ptr += 2;
17647 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17648 info_ptr += blk->size;
17649 DW_BLOCK (attr) = blk;
17650 break;
17651 case DW_FORM_block4:
7b5a2f43 17652 blk = dwarf_alloc_block (cu);
c906108c
SS
17653 blk->size = read_4_bytes (abfd, info_ptr);
17654 info_ptr += 4;
17655 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17656 info_ptr += blk->size;
17657 DW_BLOCK (attr) = blk;
17658 break;
17659 case DW_FORM_data2:
17660 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
17661 info_ptr += 2;
17662 break;
17663 case DW_FORM_data4:
17664 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
17665 info_ptr += 4;
17666 break;
17667 case DW_FORM_data8:
17668 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
17669 info_ptr += 8;
17670 break;
0224619f
JK
17671 case DW_FORM_data16:
17672 blk = dwarf_alloc_block (cu);
17673 blk->size = 16;
17674 blk->data = read_n_bytes (abfd, info_ptr, 16);
17675 info_ptr += 16;
17676 DW_BLOCK (attr) = blk;
17677 break;
2dc7f7b3
TT
17678 case DW_FORM_sec_offset:
17679 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
17680 info_ptr += bytes_read;
17681 break;
c906108c 17682 case DW_FORM_string:
9b1c24c8 17683 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 17684 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
17685 info_ptr += bytes_read;
17686 break;
4bdf3d34 17687 case DW_FORM_strp:
36586728
TT
17688 if (!cu->per_cu->is_dwz)
17689 {
17690 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
17691 &bytes_read);
17692 DW_STRING_IS_CANONICAL (attr) = 0;
17693 info_ptr += bytes_read;
17694 break;
17695 }
17696 /* FALLTHROUGH */
43988095
JK
17697 case DW_FORM_line_strp:
17698 if (!cu->per_cu->is_dwz)
17699 {
17700 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
17701 cu_header, &bytes_read);
17702 DW_STRING_IS_CANONICAL (attr) = 0;
17703 info_ptr += bytes_read;
17704 break;
17705 }
17706 /* FALLTHROUGH */
36586728
TT
17707 case DW_FORM_GNU_strp_alt:
17708 {
17709 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17710 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
17711 &bytes_read);
17712
17713 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
17714 DW_STRING_IS_CANONICAL (attr) = 0;
17715 info_ptr += bytes_read;
17716 }
4bdf3d34 17717 break;
2dc7f7b3 17718 case DW_FORM_exprloc:
c906108c 17719 case DW_FORM_block:
7b5a2f43 17720 blk = dwarf_alloc_block (cu);
c906108c
SS
17721 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17722 info_ptr += bytes_read;
17723 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17724 info_ptr += blk->size;
17725 DW_BLOCK (attr) = blk;
17726 break;
17727 case DW_FORM_block1:
7b5a2f43 17728 blk = dwarf_alloc_block (cu);
c906108c
SS
17729 blk->size = read_1_byte (abfd, info_ptr);
17730 info_ptr += 1;
17731 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17732 info_ptr += blk->size;
17733 DW_BLOCK (attr) = blk;
17734 break;
17735 case DW_FORM_data1:
17736 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
17737 info_ptr += 1;
17738 break;
17739 case DW_FORM_flag:
17740 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
17741 info_ptr += 1;
17742 break;
2dc7f7b3
TT
17743 case DW_FORM_flag_present:
17744 DW_UNSND (attr) = 1;
17745 break;
c906108c
SS
17746 case DW_FORM_sdata:
17747 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17748 info_ptr += bytes_read;
17749 break;
17750 case DW_FORM_udata:
17751 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17752 info_ptr += bytes_read;
17753 break;
17754 case DW_FORM_ref1:
9c541725 17755 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17756 + read_1_byte (abfd, info_ptr));
c906108c
SS
17757 info_ptr += 1;
17758 break;
17759 case DW_FORM_ref2:
9c541725 17760 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17761 + read_2_bytes (abfd, info_ptr));
c906108c
SS
17762 info_ptr += 2;
17763 break;
17764 case DW_FORM_ref4:
9c541725 17765 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17766 + read_4_bytes (abfd, info_ptr));
c906108c
SS
17767 info_ptr += 4;
17768 break;
613e1657 17769 case DW_FORM_ref8:
9c541725 17770 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17771 + read_8_bytes (abfd, info_ptr));
613e1657
KB
17772 info_ptr += 8;
17773 break;
55f1336d 17774 case DW_FORM_ref_sig8:
ac9ec31b 17775 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
17776 info_ptr += 8;
17777 break;
c906108c 17778 case DW_FORM_ref_udata:
9c541725 17779 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17780 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
17781 info_ptr += bytes_read;
17782 break;
c906108c 17783 case DW_FORM_indirect:
a8329558
KW
17784 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17785 info_ptr += bytes_read;
43988095
JK
17786 if (form == DW_FORM_implicit_const)
17787 {
17788 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17789 info_ptr += bytes_read;
17790 }
17791 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
17792 info_ptr);
17793 break;
17794 case DW_FORM_implicit_const:
17795 DW_SND (attr) = implicit_const;
a8329558 17796 break;
3019eac3
DE
17797 case DW_FORM_GNU_addr_index:
17798 if (reader->dwo_file == NULL)
17799 {
17800 /* For now flag a hard error.
17801 Later we can turn this into a complaint. */
17802 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
17803 dwarf_form_name (form),
17804 bfd_get_filename (abfd));
17805 }
17806 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
17807 info_ptr += bytes_read;
17808 break;
17809 case DW_FORM_GNU_str_index:
17810 if (reader->dwo_file == NULL)
17811 {
17812 /* For now flag a hard error.
17813 Later we can turn this into a complaint if warranted. */
17814 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
17815 dwarf_form_name (form),
17816 bfd_get_filename (abfd));
17817 }
17818 {
17819 ULONGEST str_index =
17820 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17821
342587c4 17822 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
17823 DW_STRING_IS_CANONICAL (attr) = 0;
17824 info_ptr += bytes_read;
17825 }
17826 break;
c906108c 17827 default:
8a3fe4f8 17828 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
17829 dwarf_form_name (form),
17830 bfd_get_filename (abfd));
c906108c 17831 }
28e94949 17832
36586728 17833 /* Super hack. */
7771576e 17834 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
17835 attr->form = DW_FORM_GNU_ref_alt;
17836
28e94949
JB
17837 /* We have seen instances where the compiler tried to emit a byte
17838 size attribute of -1 which ended up being encoded as an unsigned
17839 0xffffffff. Although 0xffffffff is technically a valid size value,
17840 an object of this size seems pretty unlikely so we can relatively
17841 safely treat these cases as if the size attribute was invalid and
17842 treat them as zero by default. */
17843 if (attr->name == DW_AT_byte_size
17844 && form == DW_FORM_data4
17845 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
17846 {
17847 complaint
17848 (&symfile_complaints,
43bbcdc2
PH
17849 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
17850 hex_string (DW_UNSND (attr)));
01c66ae6
JB
17851 DW_UNSND (attr) = 0;
17852 }
28e94949 17853
c906108c
SS
17854 return info_ptr;
17855}
17856
a8329558
KW
17857/* Read an attribute described by an abbreviated attribute. */
17858
d521ce57 17859static const gdb_byte *
dee91e82
DE
17860read_attribute (const struct die_reader_specs *reader,
17861 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 17862 const gdb_byte *info_ptr)
a8329558
KW
17863{
17864 attr->name = abbrev->name;
43988095
JK
17865 return read_attribute_value (reader, attr, abbrev->form,
17866 abbrev->implicit_const, info_ptr);
a8329558
KW
17867}
17868
0963b4bd 17869/* Read dwarf information from a buffer. */
c906108c
SS
17870
17871static unsigned int
a1855c1d 17872read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 17873{
fe1b8b76 17874 return bfd_get_8 (abfd, buf);
c906108c
SS
17875}
17876
17877static int
a1855c1d 17878read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 17879{
fe1b8b76 17880 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
17881}
17882
17883static unsigned int
a1855c1d 17884read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17885{
fe1b8b76 17886 return bfd_get_16 (abfd, buf);
c906108c
SS
17887}
17888
21ae7a4d 17889static int
a1855c1d 17890read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
17891{
17892 return bfd_get_signed_16 (abfd, buf);
17893}
17894
c906108c 17895static unsigned int
a1855c1d 17896read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17897{
fe1b8b76 17898 return bfd_get_32 (abfd, buf);
c906108c
SS
17899}
17900
21ae7a4d 17901static int
a1855c1d 17902read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
17903{
17904 return bfd_get_signed_32 (abfd, buf);
17905}
17906
93311388 17907static ULONGEST
a1855c1d 17908read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17909{
fe1b8b76 17910 return bfd_get_64 (abfd, buf);
c906108c
SS
17911}
17912
17913static CORE_ADDR
d521ce57 17914read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 17915 unsigned int *bytes_read)
c906108c 17916{
e7c27a73 17917 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
17918 CORE_ADDR retval = 0;
17919
107d2387 17920 if (cu_header->signed_addr_p)
c906108c 17921 {
107d2387
AC
17922 switch (cu_header->addr_size)
17923 {
17924 case 2:
fe1b8b76 17925 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
17926 break;
17927 case 4:
fe1b8b76 17928 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
17929 break;
17930 case 8:
fe1b8b76 17931 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
17932 break;
17933 default:
8e65ff28 17934 internal_error (__FILE__, __LINE__,
e2e0b3e5 17935 _("read_address: bad switch, signed [in module %s]"),
659b0389 17936 bfd_get_filename (abfd));
107d2387
AC
17937 }
17938 }
17939 else
17940 {
17941 switch (cu_header->addr_size)
17942 {
17943 case 2:
fe1b8b76 17944 retval = bfd_get_16 (abfd, buf);
107d2387
AC
17945 break;
17946 case 4:
fe1b8b76 17947 retval = bfd_get_32 (abfd, buf);
107d2387
AC
17948 break;
17949 case 8:
fe1b8b76 17950 retval = bfd_get_64 (abfd, buf);
107d2387
AC
17951 break;
17952 default:
8e65ff28 17953 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
17954 _("read_address: bad switch, "
17955 "unsigned [in module %s]"),
659b0389 17956 bfd_get_filename (abfd));
107d2387 17957 }
c906108c 17958 }
64367e0a 17959
107d2387
AC
17960 *bytes_read = cu_header->addr_size;
17961 return retval;
c906108c
SS
17962}
17963
f7ef9339 17964/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
17965 specification allows the initial length to take up either 4 bytes
17966 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
17967 bytes describe the length and all offsets will be 8 bytes in length
17968 instead of 4.
17969
f7ef9339
KB
17970 An older, non-standard 64-bit format is also handled by this
17971 function. The older format in question stores the initial length
17972 as an 8-byte quantity without an escape value. Lengths greater
17973 than 2^32 aren't very common which means that the initial 4 bytes
17974 is almost always zero. Since a length value of zero doesn't make
17975 sense for the 32-bit format, this initial zero can be considered to
17976 be an escape value which indicates the presence of the older 64-bit
17977 format. As written, the code can't detect (old format) lengths
917c78fc
MK
17978 greater than 4GB. If it becomes necessary to handle lengths
17979 somewhat larger than 4GB, we could allow other small values (such
17980 as the non-sensical values of 1, 2, and 3) to also be used as
17981 escape values indicating the presence of the old format.
f7ef9339 17982
917c78fc
MK
17983 The value returned via bytes_read should be used to increment the
17984 relevant pointer after calling read_initial_length().
c764a876 17985
613e1657
KB
17986 [ Note: read_initial_length() and read_offset() are based on the
17987 document entitled "DWARF Debugging Information Format", revision
f7ef9339 17988 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
17989 from:
17990
f7ef9339 17991 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 17992
613e1657
KB
17993 This document is only a draft and is subject to change. (So beware.)
17994
f7ef9339 17995 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
17996 determined empirically by examining 64-bit ELF files produced by
17997 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
17998
17999 - Kevin, July 16, 2002
613e1657
KB
18000 ] */
18001
18002static LONGEST
d521ce57 18003read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 18004{
fe1b8b76 18005 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 18006
dd373385 18007 if (length == 0xffffffff)
613e1657 18008 {
fe1b8b76 18009 length = bfd_get_64 (abfd, buf + 4);
613e1657 18010 *bytes_read = 12;
613e1657 18011 }
dd373385 18012 else if (length == 0)
f7ef9339 18013 {
dd373385 18014 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 18015 length = bfd_get_64 (abfd, buf);
f7ef9339 18016 *bytes_read = 8;
f7ef9339 18017 }
613e1657
KB
18018 else
18019 {
18020 *bytes_read = 4;
613e1657
KB
18021 }
18022
c764a876
DE
18023 return length;
18024}
dd373385 18025
c764a876
DE
18026/* Cover function for read_initial_length.
18027 Returns the length of the object at BUF, and stores the size of the
18028 initial length in *BYTES_READ and stores the size that offsets will be in
18029 *OFFSET_SIZE.
18030 If the initial length size is not equivalent to that specified in
18031 CU_HEADER then issue a complaint.
18032 This is useful when reading non-comp-unit headers. */
dd373385 18033
c764a876 18034static LONGEST
d521ce57 18035read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
18036 const struct comp_unit_head *cu_header,
18037 unsigned int *bytes_read,
18038 unsigned int *offset_size)
18039{
18040 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18041
18042 gdb_assert (cu_header->initial_length_size == 4
18043 || cu_header->initial_length_size == 8
18044 || cu_header->initial_length_size == 12);
18045
18046 if (cu_header->initial_length_size != *bytes_read)
18047 complaint (&symfile_complaints,
18048 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 18049
c764a876 18050 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 18051 return length;
613e1657
KB
18052}
18053
18054/* Read an offset from the data stream. The size of the offset is
917c78fc 18055 given by cu_header->offset_size. */
613e1657
KB
18056
18057static LONGEST
d521ce57
TT
18058read_offset (bfd *abfd, const gdb_byte *buf,
18059 const struct comp_unit_head *cu_header,
891d2f0b 18060 unsigned int *bytes_read)
c764a876
DE
18061{
18062 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 18063
c764a876
DE
18064 *bytes_read = cu_header->offset_size;
18065 return offset;
18066}
18067
18068/* Read an offset from the data stream. */
18069
18070static LONGEST
d521ce57 18071read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
18072{
18073 LONGEST retval = 0;
18074
c764a876 18075 switch (offset_size)
613e1657
KB
18076 {
18077 case 4:
fe1b8b76 18078 retval = bfd_get_32 (abfd, buf);
613e1657
KB
18079 break;
18080 case 8:
fe1b8b76 18081 retval = bfd_get_64 (abfd, buf);
613e1657
KB
18082 break;
18083 default:
8e65ff28 18084 internal_error (__FILE__, __LINE__,
c764a876 18085 _("read_offset_1: bad switch [in module %s]"),
659b0389 18086 bfd_get_filename (abfd));
613e1657
KB
18087 }
18088
917c78fc 18089 return retval;
613e1657
KB
18090}
18091
d521ce57
TT
18092static const gdb_byte *
18093read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
18094{
18095 /* If the size of a host char is 8 bits, we can return a pointer
18096 to the buffer, otherwise we have to copy the data to a buffer
18097 allocated on the temporary obstack. */
4bdf3d34 18098 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 18099 return buf;
c906108c
SS
18100}
18101
d521ce57
TT
18102static const char *
18103read_direct_string (bfd *abfd, const gdb_byte *buf,
18104 unsigned int *bytes_read_ptr)
c906108c
SS
18105{
18106 /* If the size of a host char is 8 bits, we can return a pointer
18107 to the string, otherwise we have to copy the string to a buffer
18108 allocated on the temporary obstack. */
4bdf3d34 18109 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
18110 if (*buf == '\0')
18111 {
18112 *bytes_read_ptr = 1;
18113 return NULL;
18114 }
d521ce57
TT
18115 *bytes_read_ptr = strlen ((const char *) buf) + 1;
18116 return (const char *) buf;
4bdf3d34
JJ
18117}
18118
43988095
JK
18119/* Return pointer to string at section SECT offset STR_OFFSET with error
18120 reporting strings FORM_NAME and SECT_NAME. */
18121
d521ce57 18122static const char *
43988095
JK
18123read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
18124 struct dwarf2_section_info *sect,
18125 const char *form_name,
18126 const char *sect_name)
18127{
18128 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
18129 if (sect->buffer == NULL)
18130 error (_("%s used without %s section [in module %s]"),
18131 form_name, sect_name, bfd_get_filename (abfd));
18132 if (str_offset >= sect->size)
18133 error (_("%s pointing outside of %s section [in module %s]"),
18134 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 18135 gdb_assert (HOST_CHAR_BIT == 8);
43988095 18136 if (sect->buffer[str_offset] == '\0')
4bdf3d34 18137 return NULL;
43988095
JK
18138 return (const char *) (sect->buffer + str_offset);
18139}
18140
18141/* Return pointer to string at .debug_str offset STR_OFFSET. */
18142
18143static const char *
18144read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
18145{
18146 return read_indirect_string_at_offset_from (abfd, str_offset,
18147 &dwarf2_per_objfile->str,
18148 "DW_FORM_strp", ".debug_str");
18149}
18150
18151/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18152
18153static const char *
18154read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
18155{
18156 return read_indirect_string_at_offset_from (abfd, str_offset,
18157 &dwarf2_per_objfile->line_str,
18158 "DW_FORM_line_strp",
18159 ".debug_line_str");
c906108c
SS
18160}
18161
36586728
TT
18162/* Read a string at offset STR_OFFSET in the .debug_str section from
18163 the .dwz file DWZ. Throw an error if the offset is too large. If
18164 the string consists of a single NUL byte, return NULL; otherwise
18165 return a pointer to the string. */
18166
d521ce57 18167static const char *
36586728
TT
18168read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
18169{
18170 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
18171
18172 if (dwz->str.buffer == NULL)
18173 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18174 "section [in module %s]"),
18175 bfd_get_filename (dwz->dwz_bfd));
18176 if (str_offset >= dwz->str.size)
18177 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18178 ".debug_str section [in module %s]"),
18179 bfd_get_filename (dwz->dwz_bfd));
18180 gdb_assert (HOST_CHAR_BIT == 8);
18181 if (dwz->str.buffer[str_offset] == '\0')
18182 return NULL;
d521ce57 18183 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
18184}
18185
43988095
JK
18186/* Return pointer to string at .debug_str offset as read from BUF.
18187 BUF is assumed to be in a compilation unit described by CU_HEADER.
18188 Return *BYTES_READ_PTR count of bytes read from BUF. */
18189
d521ce57
TT
18190static const char *
18191read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
18192 const struct comp_unit_head *cu_header,
18193 unsigned int *bytes_read_ptr)
18194{
18195 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
18196
18197 return read_indirect_string_at_offset (abfd, str_offset);
18198}
18199
43988095
JK
18200/* Return pointer to string at .debug_line_str offset as read from BUF.
18201 BUF is assumed to be in a compilation unit described by CU_HEADER.
18202 Return *BYTES_READ_PTR count of bytes read from BUF. */
18203
18204static const char *
18205read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
18206 const struct comp_unit_head *cu_header,
18207 unsigned int *bytes_read_ptr)
18208{
18209 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
18210
18211 return read_indirect_line_string_at_offset (abfd, str_offset);
18212}
18213
18214ULONGEST
d521ce57 18215read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 18216 unsigned int *bytes_read_ptr)
c906108c 18217{
12df843f 18218 ULONGEST result;
ce5d95e1 18219 unsigned int num_read;
870f88f7 18220 int shift;
c906108c
SS
18221 unsigned char byte;
18222
18223 result = 0;
18224 shift = 0;
18225 num_read = 0;
c906108c
SS
18226 while (1)
18227 {
fe1b8b76 18228 byte = bfd_get_8 (abfd, buf);
c906108c
SS
18229 buf++;
18230 num_read++;
12df843f 18231 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
18232 if ((byte & 128) == 0)
18233 {
18234 break;
18235 }
18236 shift += 7;
18237 }
18238 *bytes_read_ptr = num_read;
18239 return result;
18240}
18241
12df843f 18242static LONGEST
d521ce57
TT
18243read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
18244 unsigned int *bytes_read_ptr)
c906108c 18245{
12df843f 18246 LONGEST result;
870f88f7 18247 int shift, num_read;
c906108c
SS
18248 unsigned char byte;
18249
18250 result = 0;
18251 shift = 0;
c906108c 18252 num_read = 0;
c906108c
SS
18253 while (1)
18254 {
fe1b8b76 18255 byte = bfd_get_8 (abfd, buf);
c906108c
SS
18256 buf++;
18257 num_read++;
12df843f 18258 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
18259 shift += 7;
18260 if ((byte & 128) == 0)
18261 {
18262 break;
18263 }
18264 }
77e0b926 18265 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 18266 result |= -(((LONGEST) 1) << shift);
c906108c
SS
18267 *bytes_read_ptr = num_read;
18268 return result;
18269}
18270
3019eac3
DE
18271/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18272 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
18273 ADDR_SIZE is the size of addresses from the CU header. */
18274
18275static CORE_ADDR
18276read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
18277{
18278 struct objfile *objfile = dwarf2_per_objfile->objfile;
18279 bfd *abfd = objfile->obfd;
18280 const gdb_byte *info_ptr;
18281
18282 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
18283 if (dwarf2_per_objfile->addr.buffer == NULL)
18284 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 18285 objfile_name (objfile));
3019eac3
DE
18286 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
18287 error (_("DW_FORM_addr_index pointing outside of "
18288 ".debug_addr section [in module %s]"),
4262abfb 18289 objfile_name (objfile));
3019eac3
DE
18290 info_ptr = (dwarf2_per_objfile->addr.buffer
18291 + addr_base + addr_index * addr_size);
18292 if (addr_size == 4)
18293 return bfd_get_32 (abfd, info_ptr);
18294 else
18295 return bfd_get_64 (abfd, info_ptr);
18296}
18297
18298/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18299
18300static CORE_ADDR
18301read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18302{
18303 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
18304}
18305
18306/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18307
18308static CORE_ADDR
d521ce57 18309read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
18310 unsigned int *bytes_read)
18311{
18312 bfd *abfd = cu->objfile->obfd;
18313 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18314
18315 return read_addr_index (cu, addr_index);
18316}
18317
18318/* Data structure to pass results from dwarf2_read_addr_index_reader
18319 back to dwarf2_read_addr_index. */
18320
18321struct dwarf2_read_addr_index_data
18322{
18323 ULONGEST addr_base;
18324 int addr_size;
18325};
18326
18327/* die_reader_func for dwarf2_read_addr_index. */
18328
18329static void
18330dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 18331 const gdb_byte *info_ptr,
3019eac3
DE
18332 struct die_info *comp_unit_die,
18333 int has_children,
18334 void *data)
18335{
18336 struct dwarf2_cu *cu = reader->cu;
18337 struct dwarf2_read_addr_index_data *aidata =
18338 (struct dwarf2_read_addr_index_data *) data;
18339
18340 aidata->addr_base = cu->addr_base;
18341 aidata->addr_size = cu->header.addr_size;
18342}
18343
18344/* Given an index in .debug_addr, fetch the value.
18345 NOTE: This can be called during dwarf expression evaluation,
18346 long after the debug information has been read, and thus per_cu->cu
18347 may no longer exist. */
18348
18349CORE_ADDR
18350dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
18351 unsigned int addr_index)
18352{
18353 struct objfile *objfile = per_cu->objfile;
18354 struct dwarf2_cu *cu = per_cu->cu;
18355 ULONGEST addr_base;
18356 int addr_size;
18357
18358 /* This is intended to be called from outside this file. */
18359 dw2_setup (objfile);
18360
18361 /* We need addr_base and addr_size.
18362 If we don't have PER_CU->cu, we have to get it.
18363 Nasty, but the alternative is storing the needed info in PER_CU,
18364 which at this point doesn't seem justified: it's not clear how frequently
18365 it would get used and it would increase the size of every PER_CU.
18366 Entry points like dwarf2_per_cu_addr_size do a similar thing
18367 so we're not in uncharted territory here.
18368 Alas we need to be a bit more complicated as addr_base is contained
18369 in the DIE.
18370
18371 We don't need to read the entire CU(/TU).
18372 We just need the header and top level die.
a1b64ce1 18373
3019eac3 18374 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 18375 For now we skip this optimization. */
3019eac3
DE
18376
18377 if (cu != NULL)
18378 {
18379 addr_base = cu->addr_base;
18380 addr_size = cu->header.addr_size;
18381 }
18382 else
18383 {
18384 struct dwarf2_read_addr_index_data aidata;
18385
a1b64ce1
DE
18386 /* Note: We can't use init_cutu_and_read_dies_simple here,
18387 we need addr_base. */
18388 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
18389 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
18390 addr_base = aidata.addr_base;
18391 addr_size = aidata.addr_size;
18392 }
18393
18394 return read_addr_index_1 (addr_index, addr_base, addr_size);
18395}
18396
57d63ce2
DE
18397/* Given a DW_FORM_GNU_str_index, fetch the string.
18398 This is only used by the Fission support. */
3019eac3 18399
d521ce57 18400static const char *
342587c4 18401read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
18402{
18403 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 18404 const char *objf_name = objfile_name (objfile);
3019eac3 18405 bfd *abfd = objfile->obfd;
342587c4 18406 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
18407 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
18408 struct dwarf2_section_info *str_offsets_section =
18409 &reader->dwo_file->sections.str_offsets;
d521ce57 18410 const gdb_byte *info_ptr;
3019eac3 18411 ULONGEST str_offset;
57d63ce2 18412 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 18413
73869dc2
DE
18414 dwarf2_read_section (objfile, str_section);
18415 dwarf2_read_section (objfile, str_offsets_section);
18416 if (str_section->buffer == NULL)
57d63ce2 18417 error (_("%s used without .debug_str.dwo section"
9c541725
PA
18418 " in CU at offset 0x%x [in module %s]"),
18419 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 18420 if (str_offsets_section->buffer == NULL)
57d63ce2 18421 error (_("%s used without .debug_str_offsets.dwo section"
9c541725
PA
18422 " in CU at offset 0x%x [in module %s]"),
18423 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 18424 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 18425 error (_("%s pointing outside of .debug_str_offsets.dwo"
9c541725
PA
18426 " section in CU at offset 0x%x [in module %s]"),
18427 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 18428 info_ptr = (str_offsets_section->buffer
3019eac3
DE
18429 + str_index * cu->header.offset_size);
18430 if (cu->header.offset_size == 4)
18431 str_offset = bfd_get_32 (abfd, info_ptr);
18432 else
18433 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 18434 if (str_offset >= str_section->size)
57d63ce2 18435 error (_("Offset from %s pointing outside of"
9c541725
PA
18436 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
18437 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 18438 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
18439}
18440
3019eac3
DE
18441/* Return the length of an LEB128 number in BUF. */
18442
18443static int
18444leb128_size (const gdb_byte *buf)
18445{
18446 const gdb_byte *begin = buf;
18447 gdb_byte byte;
18448
18449 while (1)
18450 {
18451 byte = *buf++;
18452 if ((byte & 128) == 0)
18453 return buf - begin;
18454 }
18455}
18456
c906108c 18457static void
e142c38c 18458set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
18459{
18460 switch (lang)
18461 {
18462 case DW_LANG_C89:
76bee0cc 18463 case DW_LANG_C99:
0cfd832f 18464 case DW_LANG_C11:
c906108c 18465 case DW_LANG_C:
d1be3247 18466 case DW_LANG_UPC:
e142c38c 18467 cu->language = language_c;
c906108c 18468 break;
9c37b5ae 18469 case DW_LANG_Java:
c906108c 18470 case DW_LANG_C_plus_plus:
0cfd832f
MW
18471 case DW_LANG_C_plus_plus_11:
18472 case DW_LANG_C_plus_plus_14:
e142c38c 18473 cu->language = language_cplus;
c906108c 18474 break;
6aecb9c2
JB
18475 case DW_LANG_D:
18476 cu->language = language_d;
18477 break;
c906108c
SS
18478 case DW_LANG_Fortran77:
18479 case DW_LANG_Fortran90:
b21b22e0 18480 case DW_LANG_Fortran95:
f7de9aab
MW
18481 case DW_LANG_Fortran03:
18482 case DW_LANG_Fortran08:
e142c38c 18483 cu->language = language_fortran;
c906108c 18484 break;
a766d390
DE
18485 case DW_LANG_Go:
18486 cu->language = language_go;
18487 break;
c906108c 18488 case DW_LANG_Mips_Assembler:
e142c38c 18489 cu->language = language_asm;
c906108c
SS
18490 break;
18491 case DW_LANG_Ada83:
8aaf0b47 18492 case DW_LANG_Ada95:
bc5f45f8
JB
18493 cu->language = language_ada;
18494 break;
72019c9c
GM
18495 case DW_LANG_Modula2:
18496 cu->language = language_m2;
18497 break;
fe8e67fd
PM
18498 case DW_LANG_Pascal83:
18499 cu->language = language_pascal;
18500 break;
22566fbd
DJ
18501 case DW_LANG_ObjC:
18502 cu->language = language_objc;
18503 break;
c44af4eb
TT
18504 case DW_LANG_Rust:
18505 case DW_LANG_Rust_old:
18506 cu->language = language_rust;
18507 break;
c906108c
SS
18508 case DW_LANG_Cobol74:
18509 case DW_LANG_Cobol85:
c906108c 18510 default:
e142c38c 18511 cu->language = language_minimal;
c906108c
SS
18512 break;
18513 }
e142c38c 18514 cu->language_defn = language_def (cu->language);
c906108c
SS
18515}
18516
18517/* Return the named attribute or NULL if not there. */
18518
18519static struct attribute *
e142c38c 18520dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 18521{
a48e046c 18522 for (;;)
c906108c 18523 {
a48e046c
TT
18524 unsigned int i;
18525 struct attribute *spec = NULL;
18526
18527 for (i = 0; i < die->num_attrs; ++i)
18528 {
18529 if (die->attrs[i].name == name)
18530 return &die->attrs[i];
18531 if (die->attrs[i].name == DW_AT_specification
18532 || die->attrs[i].name == DW_AT_abstract_origin)
18533 spec = &die->attrs[i];
18534 }
18535
18536 if (!spec)
18537 break;
c906108c 18538
f2f0e013 18539 die = follow_die_ref (die, spec, &cu);
f2f0e013 18540 }
c5aa993b 18541
c906108c
SS
18542 return NULL;
18543}
18544
348e048f
DE
18545/* Return the named attribute or NULL if not there,
18546 but do not follow DW_AT_specification, etc.
18547 This is for use in contexts where we're reading .debug_types dies.
18548 Following DW_AT_specification, DW_AT_abstract_origin will take us
18549 back up the chain, and we want to go down. */
18550
18551static struct attribute *
45e58e77 18552dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
18553{
18554 unsigned int i;
18555
18556 for (i = 0; i < die->num_attrs; ++i)
18557 if (die->attrs[i].name == name)
18558 return &die->attrs[i];
18559
18560 return NULL;
18561}
18562
7d45c7c3
KB
18563/* Return the string associated with a string-typed attribute, or NULL if it
18564 is either not found or is of an incorrect type. */
18565
18566static const char *
18567dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18568{
18569 struct attribute *attr;
18570 const char *str = NULL;
18571
18572 attr = dwarf2_attr (die, name, cu);
18573
18574 if (attr != NULL)
18575 {
43988095 18576 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
18577 || attr->form == DW_FORM_string
18578 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 18579 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
18580 str = DW_STRING (attr);
18581 else
18582 complaint (&symfile_complaints,
18583 _("string type expected for attribute %s for "
18584 "DIE at 0x%x in module %s"),
9c541725 18585 dwarf_attr_name (name), to_underlying (die->sect_off),
7d45c7c3
KB
18586 objfile_name (cu->objfile));
18587 }
18588
18589 return str;
18590}
18591
05cf31d1
JB
18592/* Return non-zero iff the attribute NAME is defined for the given DIE,
18593 and holds a non-zero value. This function should only be used for
2dc7f7b3 18594 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
18595
18596static int
18597dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
18598{
18599 struct attribute *attr = dwarf2_attr (die, name, cu);
18600
18601 return (attr && DW_UNSND (attr));
18602}
18603
3ca72b44 18604static int
e142c38c 18605die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 18606{
05cf31d1
JB
18607 /* A DIE is a declaration if it has a DW_AT_declaration attribute
18608 which value is non-zero. However, we have to be careful with
18609 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
18610 (via dwarf2_flag_true_p) follows this attribute. So we may
18611 end up accidently finding a declaration attribute that belongs
18612 to a different DIE referenced by the specification attribute,
18613 even though the given DIE does not have a declaration attribute. */
18614 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
18615 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
18616}
18617
63d06c5c 18618/* Return the die giving the specification for DIE, if there is
f2f0e013 18619 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
18620 containing the return value on output. If there is no
18621 specification, but there is an abstract origin, that is
18622 returned. */
63d06c5c
DC
18623
18624static struct die_info *
f2f0e013 18625die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 18626{
f2f0e013
DJ
18627 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
18628 *spec_cu);
63d06c5c 18629
edb3359d
DJ
18630 if (spec_attr == NULL)
18631 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
18632
63d06c5c
DC
18633 if (spec_attr == NULL)
18634 return NULL;
18635 else
f2f0e013 18636 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 18637}
c906108c 18638
527f3840
JK
18639/* Stub for free_line_header to match void * callback types. */
18640
18641static void
18642free_line_header_voidp (void *arg)
18643{
9a3c8263 18644 struct line_header *lh = (struct line_header *) arg;
527f3840 18645
fff8551c 18646 delete lh;
527f3840
JK
18647}
18648
fff8551c
PA
18649void
18650line_header::add_include_dir (const char *include_dir)
c906108c 18651{
27e0867f 18652 if (dwarf_line_debug >= 2)
fff8551c
PA
18653 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
18654 include_dirs.size () + 1, include_dir);
27e0867f 18655
fff8551c 18656 include_dirs.push_back (include_dir);
debd256d 18657}
6e70227d 18658
fff8551c
PA
18659void
18660line_header::add_file_name (const char *name,
ecfb656c 18661 dir_index d_index,
fff8551c
PA
18662 unsigned int mod_time,
18663 unsigned int length)
debd256d 18664{
27e0867f
DE
18665 if (dwarf_line_debug >= 2)
18666 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 18667 (unsigned) file_names.size () + 1, name);
27e0867f 18668
ecfb656c 18669 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 18670}
6e70227d 18671
83769d0b 18672/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
18673
18674static struct dwarf2_section_info *
18675get_debug_line_section (struct dwarf2_cu *cu)
18676{
18677 struct dwarf2_section_info *section;
18678
18679 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
18680 DWO file. */
18681 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18682 section = &cu->dwo_unit->dwo_file->sections.line;
18683 else if (cu->per_cu->is_dwz)
18684 {
18685 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18686
18687 section = &dwz->line;
18688 }
18689 else
18690 section = &dwarf2_per_objfile->line;
18691
18692 return section;
18693}
18694
43988095
JK
18695/* Read directory or file name entry format, starting with byte of
18696 format count entries, ULEB128 pairs of entry formats, ULEB128 of
18697 entries count and the entries themselves in the described entry
18698 format. */
18699
18700static void
18701read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
18702 struct line_header *lh,
18703 const struct comp_unit_head *cu_header,
18704 void (*callback) (struct line_header *lh,
18705 const char *name,
ecfb656c 18706 dir_index d_index,
43988095
JK
18707 unsigned int mod_time,
18708 unsigned int length))
18709{
18710 gdb_byte format_count, formati;
18711 ULONGEST data_count, datai;
18712 const gdb_byte *buf = *bufp;
18713 const gdb_byte *format_header_data;
43988095
JK
18714 unsigned int bytes_read;
18715
18716 format_count = read_1_byte (abfd, buf);
18717 buf += 1;
18718 format_header_data = buf;
18719 for (formati = 0; formati < format_count; formati++)
18720 {
18721 read_unsigned_leb128 (abfd, buf, &bytes_read);
18722 buf += bytes_read;
18723 read_unsigned_leb128 (abfd, buf, &bytes_read);
18724 buf += bytes_read;
18725 }
18726
18727 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
18728 buf += bytes_read;
18729 for (datai = 0; datai < data_count; datai++)
18730 {
18731 const gdb_byte *format = format_header_data;
18732 struct file_entry fe;
18733
43988095
JK
18734 for (formati = 0; formati < format_count; formati++)
18735 {
ecfb656c 18736 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 18737 format += bytes_read;
43988095 18738
ecfb656c 18739 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 18740 format += bytes_read;
ecfb656c
PA
18741
18742 gdb::optional<const char *> string;
18743 gdb::optional<unsigned int> uint;
18744
43988095
JK
18745 switch (form)
18746 {
18747 case DW_FORM_string:
ecfb656c 18748 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
18749 buf += bytes_read;
18750 break;
18751
18752 case DW_FORM_line_strp:
ecfb656c
PA
18753 string.emplace (read_indirect_line_string (abfd, buf,
18754 cu_header,
18755 &bytes_read));
43988095
JK
18756 buf += bytes_read;
18757 break;
18758
18759 case DW_FORM_data1:
ecfb656c 18760 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
18761 buf += 1;
18762 break;
18763
18764 case DW_FORM_data2:
ecfb656c 18765 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
18766 buf += 2;
18767 break;
18768
18769 case DW_FORM_data4:
ecfb656c 18770 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
18771 buf += 4;
18772 break;
18773
18774 case DW_FORM_data8:
ecfb656c 18775 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
18776 buf += 8;
18777 break;
18778
18779 case DW_FORM_udata:
ecfb656c 18780 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
18781 buf += bytes_read;
18782 break;
18783
18784 case DW_FORM_block:
18785 /* It is valid only for DW_LNCT_timestamp which is ignored by
18786 current GDB. */
18787 break;
18788 }
ecfb656c
PA
18789
18790 switch (content_type)
18791 {
18792 case DW_LNCT_path:
18793 if (string.has_value ())
18794 fe.name = *string;
18795 break;
18796 case DW_LNCT_directory_index:
18797 if (uint.has_value ())
18798 fe.d_index = (dir_index) *uint;
18799 break;
18800 case DW_LNCT_timestamp:
18801 if (uint.has_value ())
18802 fe.mod_time = *uint;
18803 break;
18804 case DW_LNCT_size:
18805 if (uint.has_value ())
18806 fe.length = *uint;
18807 break;
18808 case DW_LNCT_MD5:
18809 break;
18810 default:
18811 complaint (&symfile_complaints,
18812 _("Unknown format content type %s"),
18813 pulongest (content_type));
18814 }
43988095
JK
18815 }
18816
ecfb656c 18817 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
18818 }
18819
18820 *bufp = buf;
18821}
18822
debd256d 18823/* Read the statement program header starting at OFFSET in
3019eac3 18824 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 18825 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
18826 Returns NULL if there is a problem reading the header, e.g., if it
18827 has a version we don't understand.
debd256d
JB
18828
18829 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
18830 the returned object point into the dwarf line section buffer,
18831 and must not be freed. */
ae2de4f8 18832
fff8551c 18833static line_header_up
9c541725 18834dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 18835{
d521ce57 18836 const gdb_byte *line_ptr;
c764a876 18837 unsigned int bytes_read, offset_size;
debd256d 18838 int i;
d521ce57 18839 const char *cur_dir, *cur_file;
3019eac3
DE
18840 struct dwarf2_section_info *section;
18841 bfd *abfd;
18842
36586728 18843 section = get_debug_line_section (cu);
3019eac3
DE
18844 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
18845 if (section->buffer == NULL)
debd256d 18846 {
3019eac3
DE
18847 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18848 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
18849 else
18850 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
18851 return 0;
18852 }
18853
fceca515
DE
18854 /* We can't do this until we know the section is non-empty.
18855 Only then do we know we have such a section. */
a32a8923 18856 abfd = get_section_bfd_owner (section);
fceca515 18857
a738430d
MK
18858 /* Make sure that at least there's room for the total_length field.
18859 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 18860 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 18861 {
4d3c2250 18862 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
18863 return 0;
18864 }
18865
fff8551c 18866 line_header_up lh (new line_header ());
debd256d 18867
9c541725 18868 lh->sect_off = sect_off;
527f3840
JK
18869 lh->offset_in_dwz = cu->per_cu->is_dwz;
18870
9c541725 18871 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 18872
a738430d 18873 /* Read in the header. */
6e70227d 18874 lh->total_length =
c764a876
DE
18875 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
18876 &bytes_read, &offset_size);
debd256d 18877 line_ptr += bytes_read;
3019eac3 18878 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 18879 {
4d3c2250 18880 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
18881 return 0;
18882 }
18883 lh->statement_program_end = line_ptr + lh->total_length;
18884 lh->version = read_2_bytes (abfd, line_ptr);
18885 line_ptr += 2;
43988095 18886 if (lh->version > 5)
cd366ee8
DE
18887 {
18888 /* This is a version we don't understand. The format could have
18889 changed in ways we don't handle properly so just punt. */
18890 complaint (&symfile_complaints,
18891 _("unsupported version in .debug_line section"));
18892 return NULL;
18893 }
43988095
JK
18894 if (lh->version >= 5)
18895 {
18896 gdb_byte segment_selector_size;
18897
18898 /* Skip address size. */
18899 read_1_byte (abfd, line_ptr);
18900 line_ptr += 1;
18901
18902 segment_selector_size = read_1_byte (abfd, line_ptr);
18903 line_ptr += 1;
18904 if (segment_selector_size != 0)
18905 {
18906 complaint (&symfile_complaints,
18907 _("unsupported segment selector size %u "
18908 "in .debug_line section"),
18909 segment_selector_size);
18910 return NULL;
18911 }
18912 }
c764a876
DE
18913 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
18914 line_ptr += offset_size;
debd256d
JB
18915 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
18916 line_ptr += 1;
2dc7f7b3
TT
18917 if (lh->version >= 4)
18918 {
18919 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
18920 line_ptr += 1;
18921 }
18922 else
18923 lh->maximum_ops_per_instruction = 1;
18924
18925 if (lh->maximum_ops_per_instruction == 0)
18926 {
18927 lh->maximum_ops_per_instruction = 1;
18928 complaint (&symfile_complaints,
3e43a32a
MS
18929 _("invalid maximum_ops_per_instruction "
18930 "in `.debug_line' section"));
2dc7f7b3
TT
18931 }
18932
debd256d
JB
18933 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
18934 line_ptr += 1;
18935 lh->line_base = read_1_signed_byte (abfd, line_ptr);
18936 line_ptr += 1;
18937 lh->line_range = read_1_byte (abfd, line_ptr);
18938 line_ptr += 1;
18939 lh->opcode_base = read_1_byte (abfd, line_ptr);
18940 line_ptr += 1;
fff8551c 18941 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
18942
18943 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
18944 for (i = 1; i < lh->opcode_base; ++i)
18945 {
18946 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
18947 line_ptr += 1;
18948 }
18949
43988095 18950 if (lh->version >= 5)
debd256d 18951 {
43988095 18952 /* Read directory table. */
fff8551c
PA
18953 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18954 [] (struct line_header *lh, const char *name,
ecfb656c 18955 dir_index d_index, unsigned int mod_time,
fff8551c
PA
18956 unsigned int length)
18957 {
18958 lh->add_include_dir (name);
18959 });
debd256d 18960
43988095 18961 /* Read file name table. */
fff8551c
PA
18962 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18963 [] (struct line_header *lh, const char *name,
ecfb656c 18964 dir_index d_index, unsigned int mod_time,
fff8551c
PA
18965 unsigned int length)
18966 {
ecfb656c 18967 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 18968 });
43988095
JK
18969 }
18970 else
debd256d 18971 {
43988095
JK
18972 /* Read directory table. */
18973 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18974 {
18975 line_ptr += bytes_read;
fff8551c 18976 lh->add_include_dir (cur_dir);
43988095 18977 }
debd256d
JB
18978 line_ptr += bytes_read;
18979
43988095
JK
18980 /* Read file name table. */
18981 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18982 {
ecfb656c
PA
18983 unsigned int mod_time, length;
18984 dir_index d_index;
43988095
JK
18985
18986 line_ptr += bytes_read;
ecfb656c 18987 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
18988 line_ptr += bytes_read;
18989 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18990 line_ptr += bytes_read;
18991 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18992 line_ptr += bytes_read;
18993
ecfb656c 18994 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
18995 }
18996 line_ptr += bytes_read;
debd256d 18997 }
6e70227d 18998 lh->statement_program_start = line_ptr;
debd256d 18999
3019eac3 19000 if (line_ptr > (section->buffer + section->size))
4d3c2250 19001 complaint (&symfile_complaints,
3e43a32a
MS
19002 _("line number info header doesn't "
19003 "fit in `.debug_line' section"));
debd256d 19004
debd256d
JB
19005 return lh;
19006}
c906108c 19007
c6da4cef
DE
19008/* Subroutine of dwarf_decode_lines to simplify it.
19009 Return the file name of the psymtab for included file FILE_INDEX
19010 in line header LH of PST.
19011 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19012 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
19013 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
19014
19015 The function creates dangling cleanup registration. */
c6da4cef 19016
d521ce57 19017static const char *
c6da4cef
DE
19018psymtab_include_file_name (const struct line_header *lh, int file_index,
19019 const struct partial_symtab *pst,
19020 const char *comp_dir)
19021{
8c43009f 19022 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
19023 const char *include_name = fe.name;
19024 const char *include_name_to_compare = include_name;
72b9f47f
TT
19025 const char *pst_filename;
19026 char *copied_name = NULL;
c6da4cef
DE
19027 int file_is_pst;
19028
8c43009f 19029 const char *dir_name = fe.include_dir (lh);
c6da4cef
DE
19030
19031 if (!IS_ABSOLUTE_PATH (include_name)
19032 && (dir_name != NULL || comp_dir != NULL))
19033 {
19034 /* Avoid creating a duplicate psymtab for PST.
19035 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19036 Before we do the comparison, however, we need to account
19037 for DIR_NAME and COMP_DIR.
19038 First prepend dir_name (if non-NULL). If we still don't
19039 have an absolute path prepend comp_dir (if non-NULL).
19040 However, the directory we record in the include-file's
19041 psymtab does not contain COMP_DIR (to match the
19042 corresponding symtab(s)).
19043
19044 Example:
19045
19046 bash$ cd /tmp
19047 bash$ gcc -g ./hello.c
19048 include_name = "hello.c"
19049 dir_name = "."
19050 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19051 DW_AT_name = "./hello.c"
19052
19053 */
c6da4cef
DE
19054
19055 if (dir_name != NULL)
19056 {
d521ce57
TT
19057 char *tem = concat (dir_name, SLASH_STRING,
19058 include_name, (char *)NULL);
19059
19060 make_cleanup (xfree, tem);
19061 include_name = tem;
c6da4cef 19062 include_name_to_compare = include_name;
c6da4cef
DE
19063 }
19064 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19065 {
d521ce57
TT
19066 char *tem = concat (comp_dir, SLASH_STRING,
19067 include_name, (char *)NULL);
19068
19069 make_cleanup (xfree, tem);
19070 include_name_to_compare = tem;
c6da4cef
DE
19071 }
19072 }
19073
19074 pst_filename = pst->filename;
19075 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19076 {
72b9f47f
TT
19077 copied_name = concat (pst->dirname, SLASH_STRING,
19078 pst_filename, (char *)NULL);
19079 pst_filename = copied_name;
c6da4cef
DE
19080 }
19081
1e3fad37 19082 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19083
72b9f47f
TT
19084 if (copied_name != NULL)
19085 xfree (copied_name);
c6da4cef
DE
19086
19087 if (file_is_pst)
19088 return NULL;
19089 return include_name;
19090}
19091
d9b3de22
DE
19092/* State machine to track the state of the line number program. */
19093
6f77053d 19094class lnp_state_machine
d9b3de22 19095{
6f77053d
PA
19096public:
19097 /* Initialize a machine state for the start of a line number
19098 program. */
19099 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
19100
8c43009f
PA
19101 file_entry *current_file ()
19102 {
19103 /* lh->file_names is 0-based, but the file name numbers in the
19104 statement program are 1-based. */
6f77053d
PA
19105 return m_line_header->file_name_at (m_file);
19106 }
19107
19108 /* Record the line in the state machine. END_SEQUENCE is true if
19109 we're processing the end of a sequence. */
19110 void record_line (bool end_sequence);
19111
19112 /* Check address and if invalid nop-out the rest of the lines in this
19113 sequence. */
19114 void check_line_address (struct dwarf2_cu *cu,
19115 const gdb_byte *line_ptr,
19116 CORE_ADDR lowpc, CORE_ADDR address);
19117
19118 void handle_set_discriminator (unsigned int discriminator)
19119 {
19120 m_discriminator = discriminator;
19121 m_line_has_non_zero_discriminator |= discriminator != 0;
19122 }
19123
19124 /* Handle DW_LNE_set_address. */
19125 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19126 {
19127 m_op_index = 0;
19128 address += baseaddr;
19129 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19130 }
19131
19132 /* Handle DW_LNS_advance_pc. */
19133 void handle_advance_pc (CORE_ADDR adjust);
19134
19135 /* Handle a special opcode. */
19136 void handle_special_opcode (unsigned char op_code);
19137
19138 /* Handle DW_LNS_advance_line. */
19139 void handle_advance_line (int line_delta)
19140 {
19141 advance_line (line_delta);
19142 }
19143
19144 /* Handle DW_LNS_set_file. */
19145 void handle_set_file (file_name_index file);
19146
19147 /* Handle DW_LNS_negate_stmt. */
19148 void handle_negate_stmt ()
19149 {
19150 m_is_stmt = !m_is_stmt;
19151 }
19152
19153 /* Handle DW_LNS_const_add_pc. */
19154 void handle_const_add_pc ();
19155
19156 /* Handle DW_LNS_fixed_advance_pc. */
19157 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19158 {
19159 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19160 m_op_index = 0;
19161 }
19162
19163 /* Handle DW_LNS_copy. */
19164 void handle_copy ()
19165 {
19166 record_line (false);
19167 m_discriminator = 0;
19168 }
19169
19170 /* Handle DW_LNE_end_sequence. */
19171 void handle_end_sequence ()
19172 {
19173 m_record_line_callback = ::record_line;
19174 }
19175
19176private:
19177 /* Advance the line by LINE_DELTA. */
19178 void advance_line (int line_delta)
19179 {
19180 m_line += line_delta;
19181
19182 if (line_delta != 0)
19183 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19184 }
19185
6f77053d
PA
19186 gdbarch *m_gdbarch;
19187
19188 /* True if we're recording lines.
19189 Otherwise we're building partial symtabs and are just interested in
19190 finding include files mentioned by the line number program. */
19191 bool m_record_lines_p;
19192
8c43009f 19193 /* The line number header. */
6f77053d 19194 line_header *m_line_header;
8c43009f 19195
6f77053d
PA
19196 /* These are part of the standard DWARF line number state machine,
19197 and initialized according to the DWARF spec. */
d9b3de22 19198
6f77053d 19199 unsigned char m_op_index = 0;
8c43009f 19200 /* The line table index (1-based) of the current file. */
6f77053d
PA
19201 file_name_index m_file = (file_name_index) 1;
19202 unsigned int m_line = 1;
19203
19204 /* These are initialized in the constructor. */
19205
19206 CORE_ADDR m_address;
19207 bool m_is_stmt;
19208 unsigned int m_discriminator;
d9b3de22
DE
19209
19210 /* Additional bits of state we need to track. */
19211
19212 /* The last file that we called dwarf2_start_subfile for.
19213 This is only used for TLLs. */
6f77053d 19214 unsigned int m_last_file = 0;
d9b3de22 19215 /* The last file a line number was recorded for. */
6f77053d 19216 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
19217
19218 /* The function to call to record a line. */
6f77053d 19219 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
19220
19221 /* The last line number that was recorded, used to coalesce
19222 consecutive entries for the same line. This can happen, for
19223 example, when discriminators are present. PR 17276. */
6f77053d
PA
19224 unsigned int m_last_line = 0;
19225 bool m_line_has_non_zero_discriminator = false;
8c43009f 19226};
d9b3de22 19227
6f77053d
PA
19228void
19229lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19230{
19231 CORE_ADDR addr_adj = (((m_op_index + adjust)
19232 / m_line_header->maximum_ops_per_instruction)
19233 * m_line_header->minimum_instruction_length);
19234 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19235 m_op_index = ((m_op_index + adjust)
19236 % m_line_header->maximum_ops_per_instruction);
19237}
d9b3de22 19238
6f77053d
PA
19239void
19240lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19241{
6f77053d
PA
19242 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19243 CORE_ADDR addr_adj = (((m_op_index
19244 + (adj_opcode / m_line_header->line_range))
19245 / m_line_header->maximum_ops_per_instruction)
19246 * m_line_header->minimum_instruction_length);
19247 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19248 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
19249 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19250
6f77053d
PA
19251 int line_delta = (m_line_header->line_base
19252 + (adj_opcode % m_line_header->line_range));
19253 advance_line (line_delta);
19254 record_line (false);
19255 m_discriminator = 0;
19256}
d9b3de22 19257
6f77053d
PA
19258void
19259lnp_state_machine::handle_set_file (file_name_index file)
19260{
19261 m_file = file;
19262
19263 const file_entry *fe = current_file ();
19264 if (fe == NULL)
19265 dwarf2_debug_line_missing_file_complaint ();
19266 else if (m_record_lines_p)
19267 {
19268 const char *dir = fe->include_dir (m_line_header);
19269
19270 m_last_subfile = current_subfile;
19271 m_line_has_non_zero_discriminator = m_discriminator != 0;
19272 dwarf2_start_subfile (fe->name, dir);
19273 }
19274}
19275
19276void
19277lnp_state_machine::handle_const_add_pc ()
19278{
19279 CORE_ADDR adjust
19280 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19281
19282 CORE_ADDR addr_adj
19283 = (((m_op_index + adjust)
19284 / m_line_header->maximum_ops_per_instruction)
19285 * m_line_header->minimum_instruction_length);
19286
19287 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19288 m_op_index = ((m_op_index + adjust)
19289 % m_line_header->maximum_ops_per_instruction);
19290}
d9b3de22 19291
c91513d8
PP
19292/* Ignore this record_line request. */
19293
19294static void
19295noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
19296{
19297 return;
19298}
19299
a05a36a5
DE
19300/* Return non-zero if we should add LINE to the line number table.
19301 LINE is the line to add, LAST_LINE is the last line that was added,
19302 LAST_SUBFILE is the subfile for LAST_LINE.
19303 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19304 had a non-zero discriminator.
19305
19306 We have to be careful in the presence of discriminators.
19307 E.g., for this line:
19308
19309 for (i = 0; i < 100000; i++);
19310
19311 clang can emit four line number entries for that one line,
19312 each with a different discriminator.
19313 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19314
19315 However, we want gdb to coalesce all four entries into one.
19316 Otherwise the user could stepi into the middle of the line and
19317 gdb would get confused about whether the pc really was in the
19318 middle of the line.
19319
19320 Things are further complicated by the fact that two consecutive
19321 line number entries for the same line is a heuristic used by gcc
19322 to denote the end of the prologue. So we can't just discard duplicate
19323 entries, we have to be selective about it. The heuristic we use is
19324 that we only collapse consecutive entries for the same line if at least
19325 one of those entries has a non-zero discriminator. PR 17276.
19326
19327 Note: Addresses in the line number state machine can never go backwards
19328 within one sequence, thus this coalescing is ok. */
19329
19330static int
19331dwarf_record_line_p (unsigned int line, unsigned int last_line,
19332 int line_has_non_zero_discriminator,
19333 struct subfile *last_subfile)
19334{
19335 if (current_subfile != last_subfile)
19336 return 1;
19337 if (line != last_line)
19338 return 1;
19339 /* Same line for the same file that we've seen already.
19340 As a last check, for pr 17276, only record the line if the line
19341 has never had a non-zero discriminator. */
19342 if (!line_has_non_zero_discriminator)
19343 return 1;
19344 return 0;
19345}
19346
252a6764
DE
19347/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
19348 in the line table of subfile SUBFILE. */
19349
19350static void
d9b3de22
DE
19351dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19352 unsigned int line, CORE_ADDR address,
19353 record_line_ftype p_record_line)
252a6764
DE
19354{
19355 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19356
27e0867f
DE
19357 if (dwarf_line_debug)
19358 {
19359 fprintf_unfiltered (gdb_stdlog,
19360 "Recording line %u, file %s, address %s\n",
19361 line, lbasename (subfile->name),
19362 paddress (gdbarch, address));
19363 }
19364
d5962de5 19365 (*p_record_line) (subfile, line, addr);
252a6764
DE
19366}
19367
19368/* Subroutine of dwarf_decode_lines_1 to simplify it.
19369 Mark the end of a set of line number records.
d9b3de22 19370 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
19371 If SUBFILE is NULL the request is ignored. */
19372
19373static void
19374dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19375 CORE_ADDR address, record_line_ftype p_record_line)
19376{
27e0867f
DE
19377 if (subfile == NULL)
19378 return;
19379
19380 if (dwarf_line_debug)
19381 {
19382 fprintf_unfiltered (gdb_stdlog,
19383 "Finishing current line, file %s, address %s\n",
19384 lbasename (subfile->name),
19385 paddress (gdbarch, address));
19386 }
19387
d9b3de22
DE
19388 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
19389}
19390
6f77053d
PA
19391void
19392lnp_state_machine::record_line (bool end_sequence)
d9b3de22 19393{
d9b3de22
DE
19394 if (dwarf_line_debug)
19395 {
19396 fprintf_unfiltered (gdb_stdlog,
19397 "Processing actual line %u: file %u,"
19398 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
19399 m_line, to_underlying (m_file),
19400 paddress (m_gdbarch, m_address),
19401 m_is_stmt, m_discriminator);
d9b3de22
DE
19402 }
19403
6f77053d 19404 file_entry *fe = current_file ();
8c43009f
PA
19405
19406 if (fe == NULL)
d9b3de22
DE
19407 dwarf2_debug_line_missing_file_complaint ();
19408 /* For now we ignore lines not starting on an instruction boundary.
19409 But not when processing end_sequence for compatibility with the
19410 previous version of the code. */
6f77053d 19411 else if (m_op_index == 0 || end_sequence)
d9b3de22 19412 {
8c43009f 19413 fe->included_p = 1;
6f77053d 19414 if (m_record_lines_p && m_is_stmt)
d9b3de22 19415 {
6f77053d 19416 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 19417 {
6f77053d
PA
19418 dwarf_finish_line (m_gdbarch, m_last_subfile,
19419 m_address, m_record_line_callback);
d9b3de22
DE
19420 }
19421
19422 if (!end_sequence)
19423 {
6f77053d
PA
19424 if (dwarf_record_line_p (m_line, m_last_line,
19425 m_line_has_non_zero_discriminator,
19426 m_last_subfile))
d9b3de22 19427 {
6f77053d
PA
19428 dwarf_record_line_1 (m_gdbarch, current_subfile,
19429 m_line, m_address,
19430 m_record_line_callback);
d9b3de22 19431 }
6f77053d
PA
19432 m_last_subfile = current_subfile;
19433 m_last_line = m_line;
d9b3de22
DE
19434 }
19435 }
19436 }
19437}
19438
6f77053d
PA
19439lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
19440 bool record_lines_p)
d9b3de22 19441{
6f77053d
PA
19442 m_gdbarch = arch;
19443 m_record_lines_p = record_lines_p;
19444 m_line_header = lh;
d9b3de22 19445
6f77053d 19446 m_record_line_callback = ::record_line;
d9b3de22 19447
d9b3de22
DE
19448 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19449 was a line entry for it so that the backend has a chance to adjust it
19450 and also record it in case it needs it. This is currently used by MIPS
19451 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
19452 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19453 m_is_stmt = lh->default_is_stmt;
19454 m_discriminator = 0;
252a6764
DE
19455}
19456
6f77053d
PA
19457void
19458lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19459 const gdb_byte *line_ptr,
19460 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
19461{
19462 /* If address < lowpc then it's not a usable value, it's outside the
19463 pc range of the CU. However, we restrict the test to only address
19464 values of zero to preserve GDB's previous behaviour which is to
19465 handle the specific case of a function being GC'd by the linker. */
19466
19467 if (address == 0 && address < lowpc)
19468 {
19469 /* This line table is for a function which has been
19470 GCd by the linker. Ignore it. PR gdb/12528 */
19471
19472 struct objfile *objfile = cu->objfile;
19473 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19474
19475 complaint (&symfile_complaints,
19476 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19477 line_offset, objfile_name (objfile));
6f77053d
PA
19478 m_record_line_callback = noop_record_line;
19479 /* Note: record_line_callback is left as noop_record_line until
19480 we see DW_LNE_end_sequence. */
924c2928
DE
19481 }
19482}
19483
f3f5162e 19484/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
19485 Process the line number information in LH.
19486 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19487 program in order to set included_p for every referenced header. */
debd256d 19488
c906108c 19489static void
43f3e411
DE
19490dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19491 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 19492{
d521ce57
TT
19493 const gdb_byte *line_ptr, *extended_end;
19494 const gdb_byte *line_end;
a8c50c1f 19495 unsigned int bytes_read, extended_len;
699ca60a 19496 unsigned char op_code, extended_op;
e142c38c
DJ
19497 CORE_ADDR baseaddr;
19498 struct objfile *objfile = cu->objfile;
f3f5162e 19499 bfd *abfd = objfile->obfd;
fbf65064 19500 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
19501 /* True if we're recording line info (as opposed to building partial
19502 symtabs and just interested in finding include files mentioned by
19503 the line number program). */
19504 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
19505
19506 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 19507
debd256d
JB
19508 line_ptr = lh->statement_program_start;
19509 line_end = lh->statement_program_end;
c906108c
SS
19510
19511 /* Read the statement sequences until there's nothing left. */
19512 while (line_ptr < line_end)
19513 {
6f77053d
PA
19514 /* The DWARF line number program state machine. Reset the state
19515 machine at the start of each sequence. */
19516 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
19517 bool end_sequence = false;
d9b3de22 19518
8c43009f 19519 if (record_lines_p)
c906108c 19520 {
8c43009f
PA
19521 /* Start a subfile for the current file of the state
19522 machine. */
19523 const file_entry *fe = state_machine.current_file ();
19524
19525 if (fe != NULL)
19526 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
19527 }
19528
a738430d 19529 /* Decode the table. */
d9b3de22 19530 while (line_ptr < line_end && !end_sequence)
c906108c
SS
19531 {
19532 op_code = read_1_byte (abfd, line_ptr);
19533 line_ptr += 1;
9aa1fe7e 19534
debd256d 19535 if (op_code >= lh->opcode_base)
6e70227d 19536 {
8e07a239 19537 /* Special opcode. */
6f77053d 19538 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
19539 }
19540 else switch (op_code)
c906108c
SS
19541 {
19542 case DW_LNS_extended_op:
3e43a32a
MS
19543 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19544 &bytes_read);
473b7be6 19545 line_ptr += bytes_read;
a8c50c1f 19546 extended_end = line_ptr + extended_len;
c906108c
SS
19547 extended_op = read_1_byte (abfd, line_ptr);
19548 line_ptr += 1;
19549 switch (extended_op)
19550 {
19551 case DW_LNE_end_sequence:
6f77053d
PA
19552 state_machine.handle_end_sequence ();
19553 end_sequence = true;
c906108c
SS
19554 break;
19555 case DW_LNE_set_address:
d9b3de22
DE
19556 {
19557 CORE_ADDR address
19558 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 19559 line_ptr += bytes_read;
6f77053d
PA
19560
19561 state_machine.check_line_address (cu, line_ptr,
19562 lowpc, address);
19563 state_machine.handle_set_address (baseaddr, address);
d9b3de22 19564 }
c906108c
SS
19565 break;
19566 case DW_LNE_define_file:
debd256d 19567 {
d521ce57 19568 const char *cur_file;
ecfb656c
PA
19569 unsigned int mod_time, length;
19570 dir_index dindex;
6e70227d 19571
3e43a32a
MS
19572 cur_file = read_direct_string (abfd, line_ptr,
19573 &bytes_read);
debd256d 19574 line_ptr += bytes_read;
ecfb656c 19575 dindex = (dir_index)
debd256d
JB
19576 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19577 line_ptr += bytes_read;
19578 mod_time =
19579 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19580 line_ptr += bytes_read;
19581 length =
19582 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19583 line_ptr += bytes_read;
ecfb656c 19584 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 19585 }
c906108c 19586 break;
d0c6ba3d 19587 case DW_LNE_set_discriminator:
6f77053d
PA
19588 {
19589 /* The discriminator is not interesting to the
19590 debugger; just ignore it. We still need to
19591 check its value though:
19592 if there are consecutive entries for the same
19593 (non-prologue) line we want to coalesce them.
19594 PR 17276. */
19595 unsigned int discr
19596 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19597 line_ptr += bytes_read;
19598
19599 state_machine.handle_set_discriminator (discr);
19600 }
d0c6ba3d 19601 break;
c906108c 19602 default:
4d3c2250 19603 complaint (&symfile_complaints,
e2e0b3e5 19604 _("mangled .debug_line section"));
debd256d 19605 return;
c906108c 19606 }
a8c50c1f
DJ
19607 /* Make sure that we parsed the extended op correctly. If e.g.
19608 we expected a different address size than the producer used,
19609 we may have read the wrong number of bytes. */
19610 if (line_ptr != extended_end)
19611 {
19612 complaint (&symfile_complaints,
19613 _("mangled .debug_line section"));
19614 return;
19615 }
c906108c
SS
19616 break;
19617 case DW_LNS_copy:
6f77053d 19618 state_machine.handle_copy ();
c906108c
SS
19619 break;
19620 case DW_LNS_advance_pc:
2dc7f7b3
TT
19621 {
19622 CORE_ADDR adjust
19623 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 19624 line_ptr += bytes_read;
6f77053d
PA
19625
19626 state_machine.handle_advance_pc (adjust);
2dc7f7b3 19627 }
c906108c
SS
19628 break;
19629 case DW_LNS_advance_line:
a05a36a5
DE
19630 {
19631 int line_delta
19632 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 19633 line_ptr += bytes_read;
6f77053d
PA
19634
19635 state_machine.handle_advance_line (line_delta);
a05a36a5 19636 }
c906108c
SS
19637 break;
19638 case DW_LNS_set_file:
d9b3de22 19639 {
6f77053d 19640 file_name_index file
ecfb656c
PA
19641 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19642 &bytes_read);
d9b3de22 19643 line_ptr += bytes_read;
8c43009f 19644
6f77053d 19645 state_machine.handle_set_file (file);
d9b3de22 19646 }
c906108c
SS
19647 break;
19648 case DW_LNS_set_column:
0ad93d4f 19649 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
19650 line_ptr += bytes_read;
19651 break;
19652 case DW_LNS_negate_stmt:
6f77053d 19653 state_machine.handle_negate_stmt ();
c906108c
SS
19654 break;
19655 case DW_LNS_set_basic_block:
c906108c 19656 break;
c2c6d25f
JM
19657 /* Add to the address register of the state machine the
19658 address increment value corresponding to special opcode
a738430d
MK
19659 255. I.e., this value is scaled by the minimum
19660 instruction length since special opcode 255 would have
b021a221 19661 scaled the increment. */
c906108c 19662 case DW_LNS_const_add_pc:
6f77053d 19663 state_machine.handle_const_add_pc ();
c906108c
SS
19664 break;
19665 case DW_LNS_fixed_advance_pc:
3e29f34a 19666 {
6f77053d 19667 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 19668 line_ptr += 2;
6f77053d
PA
19669
19670 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 19671 }
c906108c 19672 break;
9aa1fe7e 19673 default:
a738430d
MK
19674 {
19675 /* Unknown standard opcode, ignore it. */
9aa1fe7e 19676 int i;
a738430d 19677
debd256d 19678 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
19679 {
19680 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19681 line_ptr += bytes_read;
19682 }
19683 }
c906108c
SS
19684 }
19685 }
d9b3de22
DE
19686
19687 if (!end_sequence)
19688 dwarf2_debug_line_missing_end_sequence_complaint ();
19689
19690 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19691 in which case we still finish recording the last line). */
6f77053d 19692 state_machine.record_line (true);
c906108c 19693 }
f3f5162e
DE
19694}
19695
19696/* Decode the Line Number Program (LNP) for the given line_header
19697 structure and CU. The actual information extracted and the type
19698 of structures created from the LNP depends on the value of PST.
19699
19700 1. If PST is NULL, then this procedure uses the data from the program
19701 to create all necessary symbol tables, and their linetables.
19702
19703 2. If PST is not NULL, this procedure reads the program to determine
19704 the list of files included by the unit represented by PST, and
19705 builds all the associated partial symbol tables.
19706
19707 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19708 It is used for relative paths in the line table.
19709 NOTE: When processing partial symtabs (pst != NULL),
19710 comp_dir == pst->dirname.
19711
19712 NOTE: It is important that psymtabs have the same file name (via strcmp)
19713 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19714 symtab we don't use it in the name of the psymtabs we create.
19715 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
19716 A good testcase for this is mb-inline.exp.
19717
527f3840
JK
19718 LOWPC is the lowest address in CU (or 0 if not known).
19719
19720 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19721 for its PC<->lines mapping information. Otherwise only the filename
19722 table is read in. */
f3f5162e
DE
19723
19724static void
19725dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 19726 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 19727 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
19728{
19729 struct objfile *objfile = cu->objfile;
19730 const int decode_for_pst_p = (pst != NULL);
f3f5162e 19731
527f3840
JK
19732 if (decode_mapping)
19733 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
19734
19735 if (decode_for_pst_p)
19736 {
19737 int file_index;
19738
19739 /* Now that we're done scanning the Line Header Program, we can
19740 create the psymtab of each included file. */
fff8551c 19741 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
19742 if (lh->file_names[file_index].included_p == 1)
19743 {
d521ce57 19744 const char *include_name =
c6da4cef
DE
19745 psymtab_include_file_name (lh, file_index, pst, comp_dir);
19746 if (include_name != NULL)
aaa75496
JB
19747 dwarf2_create_include_psymtab (include_name, pst, objfile);
19748 }
19749 }
cb1df416
DJ
19750 else
19751 {
19752 /* Make sure a symtab is created for every file, even files
19753 which contain only variables (i.e. no code with associated
19754 line numbers). */
43f3e411 19755 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 19756 int i;
cb1df416 19757
fff8551c 19758 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 19759 {
8c43009f 19760 file_entry &fe = lh->file_names[i];
9a619af0 19761
8c43009f 19762 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 19763
cb1df416 19764 if (current_subfile->symtab == NULL)
43f3e411
DE
19765 {
19766 current_subfile->symtab
19767 = allocate_symtab (cust, current_subfile->name);
19768 }
8c43009f 19769 fe.symtab = current_subfile->symtab;
cb1df416
DJ
19770 }
19771 }
c906108c
SS
19772}
19773
19774/* Start a subfile for DWARF. FILENAME is the name of the file and
19775 DIRNAME the name of the source directory which contains FILENAME
4d663531 19776 or NULL if not known.
c906108c
SS
19777 This routine tries to keep line numbers from identical absolute and
19778 relative file names in a common subfile.
19779
19780 Using the `list' example from the GDB testsuite, which resides in
19781 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19782 of /srcdir/list0.c yields the following debugging information for list0.c:
19783
c5aa993b 19784 DW_AT_name: /srcdir/list0.c
4d663531 19785 DW_AT_comp_dir: /compdir
357e46e7 19786 files.files[0].name: list0.h
c5aa993b 19787 files.files[0].dir: /srcdir
357e46e7 19788 files.files[1].name: list0.c
c5aa993b 19789 files.files[1].dir: /srcdir
c906108c
SS
19790
19791 The line number information for list0.c has to end up in a single
4f1520fb
FR
19792 subfile, so that `break /srcdir/list0.c:1' works as expected.
19793 start_subfile will ensure that this happens provided that we pass the
19794 concatenation of files.files[1].dir and files.files[1].name as the
19795 subfile's name. */
c906108c
SS
19796
19797static void
4d663531 19798dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 19799{
d521ce57 19800 char *copy = NULL;
4f1520fb 19801
4d663531 19802 /* In order not to lose the line information directory,
4f1520fb
FR
19803 we concatenate it to the filename when it makes sense.
19804 Note that the Dwarf3 standard says (speaking of filenames in line
19805 information): ``The directory index is ignored for file names
19806 that represent full path names''. Thus ignoring dirname in the
19807 `else' branch below isn't an issue. */
c906108c 19808
d5166ae1 19809 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
19810 {
19811 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
19812 filename = copy;
19813 }
c906108c 19814
4d663531 19815 start_subfile (filename);
4f1520fb 19816
d521ce57
TT
19817 if (copy != NULL)
19818 xfree (copy);
c906108c
SS
19819}
19820
f4dc4d17
DE
19821/* Start a symtab for DWARF.
19822 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
19823
43f3e411 19824static struct compunit_symtab *
f4dc4d17 19825dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 19826 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 19827{
43f3e411 19828 struct compunit_symtab *cust
5ffa0793 19829 = start_symtab (cu->objfile, name, comp_dir, low_pc, cu->language);
43f3e411 19830
f4dc4d17
DE
19831 record_debugformat ("DWARF 2");
19832 record_producer (cu->producer);
19833
19834 /* We assume that we're processing GCC output. */
19835 processing_gcc_compilation = 2;
19836
4d4ec4e5 19837 cu->processing_has_namespace_info = 0;
43f3e411
DE
19838
19839 return cust;
f4dc4d17
DE
19840}
19841
4c2df51b
DJ
19842static void
19843var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 19844 struct dwarf2_cu *cu)
4c2df51b 19845{
e7c27a73
DJ
19846 struct objfile *objfile = cu->objfile;
19847 struct comp_unit_head *cu_header = &cu->header;
19848
4c2df51b
DJ
19849 /* NOTE drow/2003-01-30: There used to be a comment and some special
19850 code here to turn a symbol with DW_AT_external and a
19851 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19852 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19853 with some versions of binutils) where shared libraries could have
19854 relocations against symbols in their debug information - the
19855 minimal symbol would have the right address, but the debug info
19856 would not. It's no longer necessary, because we will explicitly
19857 apply relocations when we read in the debug information now. */
19858
19859 /* A DW_AT_location attribute with no contents indicates that a
19860 variable has been optimized away. */
19861 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
19862 {
f1e6e072 19863 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
19864 return;
19865 }
19866
19867 /* Handle one degenerate form of location expression specially, to
19868 preserve GDB's previous behavior when section offsets are
3019eac3
DE
19869 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
19870 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
19871
19872 if (attr_form_is_block (attr)
3019eac3
DE
19873 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19874 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19875 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19876 && (DW_BLOCK (attr)->size
19877 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 19878 {
891d2f0b 19879 unsigned int dummy;
4c2df51b 19880
3019eac3
DE
19881 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
19882 SYMBOL_VALUE_ADDRESS (sym) =
19883 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
19884 else
19885 SYMBOL_VALUE_ADDRESS (sym) =
19886 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 19887 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
19888 fixup_symbol_section (sym, objfile);
19889 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
19890 SYMBOL_SECTION (sym));
4c2df51b
DJ
19891 return;
19892 }
19893
19894 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
19895 expression evaluator, and use LOC_COMPUTED only when necessary
19896 (i.e. when the value of a register or memory location is
19897 referenced, or a thread-local block, etc.). Then again, it might
19898 not be worthwhile. I'm assuming that it isn't unless performance
19899 or memory numbers show me otherwise. */
19900
f1e6e072 19901 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 19902
f1e6e072 19903 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 19904 cu->has_loclist = 1;
4c2df51b
DJ
19905}
19906
c906108c
SS
19907/* Given a pointer to a DWARF information entry, figure out if we need
19908 to make a symbol table entry for it, and if so, create a new entry
19909 and return a pointer to it.
19910 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
19911 used the passed type.
19912 If SPACE is not NULL, use it to hold the new symbol. If it is
19913 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
19914
19915static struct symbol *
34eaf542
TT
19916new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
19917 struct symbol *space)
c906108c 19918{
e7c27a73 19919 struct objfile *objfile = cu->objfile;
3e29f34a 19920 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 19921 struct symbol *sym = NULL;
15d034d0 19922 const char *name;
c906108c
SS
19923 struct attribute *attr = NULL;
19924 struct attribute *attr2 = NULL;
e142c38c 19925 CORE_ADDR baseaddr;
e37fd15a
SW
19926 struct pending **list_to_add = NULL;
19927
edb3359d 19928 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
19929
19930 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 19931
94af9270 19932 name = dwarf2_name (die, cu);
c906108c
SS
19933 if (name)
19934 {
94af9270 19935 const char *linkagename;
34eaf542 19936 int suppress_add = 0;
94af9270 19937
34eaf542
TT
19938 if (space)
19939 sym = space;
19940 else
e623cf5d 19941 sym = allocate_symbol (objfile);
c906108c 19942 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
19943
19944 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 19945 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
19946 linkagename = dwarf2_physname (name, die, cu);
19947 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 19948
f55ee35c
JK
19949 /* Fortran does not have mangling standard and the mangling does differ
19950 between gfortran, iFort etc. */
19951 if (cu->language == language_fortran
b250c185 19952 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 19953 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 19954 dwarf2_full_name (name, die, cu),
29df156d 19955 NULL);
f55ee35c 19956
c906108c 19957 /* Default assumptions.
c5aa993b 19958 Use the passed type or decode it from the die. */
176620f1 19959 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 19960 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
19961 if (type != NULL)
19962 SYMBOL_TYPE (sym) = type;
19963 else
e7c27a73 19964 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
19965 attr = dwarf2_attr (die,
19966 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
19967 cu);
c906108c
SS
19968 if (attr)
19969 {
19970 SYMBOL_LINE (sym) = DW_UNSND (attr);
19971 }
cb1df416 19972
edb3359d
DJ
19973 attr = dwarf2_attr (die,
19974 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
19975 cu);
cb1df416
DJ
19976 if (attr)
19977 {
ecfb656c 19978 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 19979 struct file_entry *fe;
9a619af0 19980
ecfb656c
PA
19981 if (cu->line_header != NULL)
19982 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
19983 else
19984 fe = NULL;
19985
19986 if (fe == NULL)
cb1df416
DJ
19987 complaint (&symfile_complaints,
19988 _("file index out of range"));
8c43009f
PA
19989 else
19990 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
19991 }
19992
c906108c
SS
19993 switch (die->tag)
19994 {
19995 case DW_TAG_label:
e142c38c 19996 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 19997 if (attr)
3e29f34a
MR
19998 {
19999 CORE_ADDR addr;
20000
20001 addr = attr_value_as_address (attr);
20002 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20003 SYMBOL_VALUE_ADDRESS (sym) = addr;
20004 }
0f5238ed
TT
20005 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20006 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20007 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 20008 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20009 break;
20010 case DW_TAG_subprogram:
20011 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20012 finish_block. */
f1e6e072 20013 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20014 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
20015 if ((attr2 && (DW_UNSND (attr2) != 0))
20016 || cu->language == language_ada)
c906108c 20017 {
2cfa0c8d
JB
20018 /* Subprograms marked external are stored as a global symbol.
20019 Ada subprograms, whether marked external or not, are always
20020 stored as a global symbol, because we want to be able to
20021 access them globally. For instance, we want to be able
20022 to break on a nested subprogram without having to
20023 specify the context. */
e37fd15a 20024 list_to_add = &global_symbols;
c906108c
SS
20025 }
20026 else
20027 {
e37fd15a 20028 list_to_add = cu->list_in_scope;
c906108c
SS
20029 }
20030 break;
edb3359d
DJ
20031 case DW_TAG_inlined_subroutine:
20032 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20033 finish_block. */
f1e6e072 20034 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20035 SYMBOL_INLINED (sym) = 1;
481860b3 20036 list_to_add = cu->list_in_scope;
edb3359d 20037 break;
34eaf542
TT
20038 case DW_TAG_template_value_param:
20039 suppress_add = 1;
20040 /* Fall through. */
72929c62 20041 case DW_TAG_constant:
c906108c 20042 case DW_TAG_variable:
254e6b9e 20043 case DW_TAG_member:
0963b4bd
MS
20044 /* Compilation with minimal debug info may result in
20045 variables with missing type entries. Change the
20046 misleading `void' type to something sensible. */
c906108c 20047 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 20048 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20049
e142c38c 20050 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20051 /* In the case of DW_TAG_member, we should only be called for
20052 static const members. */
20053 if (die->tag == DW_TAG_member)
20054 {
3863f96c
DE
20055 /* dwarf2_add_field uses die_is_declaration,
20056 so we do the same. */
254e6b9e
DE
20057 gdb_assert (die_is_declaration (die, cu));
20058 gdb_assert (attr);
20059 }
c906108c
SS
20060 if (attr)
20061 {
e7c27a73 20062 dwarf2_const_value (attr, sym, cu);
e142c38c 20063 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20064 if (!suppress_add)
34eaf542
TT
20065 {
20066 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 20067 list_to_add = &global_symbols;
34eaf542 20068 else
e37fd15a 20069 list_to_add = cu->list_in_scope;
34eaf542 20070 }
c906108c
SS
20071 break;
20072 }
e142c38c 20073 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
20074 if (attr)
20075 {
e7c27a73 20076 var_decode_location (attr, sym, cu);
e142c38c 20077 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20078
20079 /* Fortran explicitly imports any global symbols to the local
20080 scope by DW_TAG_common_block. */
20081 if (cu->language == language_fortran && die->parent
20082 && die->parent->tag == DW_TAG_common_block)
20083 attr2 = NULL;
20084
caac4577
JG
20085 if (SYMBOL_CLASS (sym) == LOC_STATIC
20086 && SYMBOL_VALUE_ADDRESS (sym) == 0
20087 && !dwarf2_per_objfile->has_section_at_zero)
20088 {
20089 /* When a static variable is eliminated by the linker,
20090 the corresponding debug information is not stripped
20091 out, but the variable address is set to null;
20092 do not add such variables into symbol table. */
20093 }
20094 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20095 {
f55ee35c
JK
20096 /* Workaround gfortran PR debug/40040 - it uses
20097 DW_AT_location for variables in -fPIC libraries which may
20098 get overriden by other libraries/executable and get
20099 a different address. Resolve it by the minimal symbol
20100 which may come from inferior's executable using copy
20101 relocation. Make this workaround only for gfortran as for
20102 other compilers GDB cannot guess the minimal symbol
20103 Fortran mangling kind. */
20104 if (cu->language == language_fortran && die->parent
20105 && die->parent->tag == DW_TAG_module
20106 && cu->producer
28586665 20107 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 20108 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 20109
1c809c68
TT
20110 /* A variable with DW_AT_external is never static,
20111 but it may be block-scoped. */
20112 list_to_add = (cu->list_in_scope == &file_symbols
20113 ? &global_symbols : cu->list_in_scope);
1c809c68 20114 }
c906108c 20115 else
e37fd15a 20116 list_to_add = cu->list_in_scope;
c906108c
SS
20117 }
20118 else
20119 {
20120 /* We do not know the address of this symbol.
c5aa993b
JM
20121 If it is an external symbol and we have type information
20122 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20123 The address of the variable will then be determined from
20124 the minimal symbol table whenever the variable is
20125 referenced. */
e142c38c 20126 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20127
20128 /* Fortran explicitly imports any global symbols to the local
20129 scope by DW_TAG_common_block. */
20130 if (cu->language == language_fortran && die->parent
20131 && die->parent->tag == DW_TAG_common_block)
20132 {
20133 /* SYMBOL_CLASS doesn't matter here because
20134 read_common_block is going to reset it. */
20135 if (!suppress_add)
20136 list_to_add = cu->list_in_scope;
20137 }
20138 else if (attr2 && (DW_UNSND (attr2) != 0)
20139 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20140 {
0fe7935b
DJ
20141 /* A variable with DW_AT_external is never static, but it
20142 may be block-scoped. */
20143 list_to_add = (cu->list_in_scope == &file_symbols
20144 ? &global_symbols : cu->list_in_scope);
20145
f1e6e072 20146 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20147 }
442ddf59
JK
20148 else if (!die_is_declaration (die, cu))
20149 {
20150 /* Use the default LOC_OPTIMIZED_OUT class. */
20151 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20152 if (!suppress_add)
20153 list_to_add = cu->list_in_scope;
442ddf59 20154 }
c906108c
SS
20155 }
20156 break;
20157 case DW_TAG_formal_parameter:
edb3359d
DJ
20158 /* If we are inside a function, mark this as an argument. If
20159 not, we might be looking at an argument to an inlined function
20160 when we do not have enough information to show inlined frames;
20161 pretend it's a local variable in that case so that the user can
20162 still see it. */
20163 if (context_stack_depth > 0
20164 && context_stack[context_stack_depth - 1].name != NULL)
20165 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 20166 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
20167 if (attr)
20168 {
e7c27a73 20169 var_decode_location (attr, sym, cu);
c906108c 20170 }
e142c38c 20171 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
20172 if (attr)
20173 {
e7c27a73 20174 dwarf2_const_value (attr, sym, cu);
c906108c 20175 }
f346a30d 20176
e37fd15a 20177 list_to_add = cu->list_in_scope;
c906108c
SS
20178 break;
20179 case DW_TAG_unspecified_parameters:
20180 /* From varargs functions; gdb doesn't seem to have any
20181 interest in this information, so just ignore it for now.
20182 (FIXME?) */
20183 break;
34eaf542
TT
20184 case DW_TAG_template_type_param:
20185 suppress_add = 1;
20186 /* Fall through. */
c906108c 20187 case DW_TAG_class_type:
680b30c7 20188 case DW_TAG_interface_type:
c906108c
SS
20189 case DW_TAG_structure_type:
20190 case DW_TAG_union_type:
72019c9c 20191 case DW_TAG_set_type:
c906108c 20192 case DW_TAG_enumeration_type:
f1e6e072 20193 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20194 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20195
63d06c5c 20196 {
9c37b5ae 20197 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20198 really ever be static objects: otherwise, if you try
20199 to, say, break of a class's method and you're in a file
20200 which doesn't mention that class, it won't work unless
20201 the check for all static symbols in lookup_symbol_aux
20202 saves you. See the OtherFileClass tests in
20203 gdb.c++/namespace.exp. */
20204
e37fd15a 20205 if (!suppress_add)
34eaf542 20206 {
34eaf542 20207 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 20208 && cu->language == language_cplus
34eaf542 20209 ? &global_symbols : cu->list_in_scope);
63d06c5c 20210
64382290 20211 /* The semantics of C++ state that "struct foo {
9c37b5ae 20212 ... }" also defines a typedef for "foo". */
64382290 20213 if (cu->language == language_cplus
45280282 20214 || cu->language == language_ada
c44af4eb
TT
20215 || cu->language == language_d
20216 || cu->language == language_rust)
64382290
TT
20217 {
20218 /* The symbol's name is already allocated along
20219 with this objfile, so we don't need to
20220 duplicate it for the type. */
20221 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20222 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
20223 }
63d06c5c
DC
20224 }
20225 }
c906108c
SS
20226 break;
20227 case DW_TAG_typedef:
f1e6e072 20228 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20229 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20230 list_to_add = cu->list_in_scope;
63d06c5c 20231 break;
c906108c 20232 case DW_TAG_base_type:
a02abb62 20233 case DW_TAG_subrange_type:
f1e6e072 20234 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20235 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20236 list_to_add = cu->list_in_scope;
c906108c
SS
20237 break;
20238 case DW_TAG_enumerator:
e142c38c 20239 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
20240 if (attr)
20241 {
e7c27a73 20242 dwarf2_const_value (attr, sym, cu);
c906108c 20243 }
63d06c5c
DC
20244 {
20245 /* NOTE: carlton/2003-11-10: See comment above in the
20246 DW_TAG_class_type, etc. block. */
20247
e142c38c 20248 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 20249 && cu->language == language_cplus
e142c38c 20250 ? &global_symbols : cu->list_in_scope);
63d06c5c 20251 }
c906108c 20252 break;
74921315 20253 case DW_TAG_imported_declaration:
5c4e30ca 20254 case DW_TAG_namespace:
f1e6e072 20255 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 20256 list_to_add = &global_symbols;
5c4e30ca 20257 break;
530e8392
KB
20258 case DW_TAG_module:
20259 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20260 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20261 list_to_add = &global_symbols;
20262 break;
4357ac6c 20263 case DW_TAG_common_block:
f1e6e072 20264 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
20265 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20266 add_symbol_to_list (sym, cu->list_in_scope);
20267 break;
c906108c
SS
20268 default:
20269 /* Not a tag we recognize. Hopefully we aren't processing
20270 trash data, but since we must specifically ignore things
20271 we don't recognize, there is nothing else we should do at
0963b4bd 20272 this point. */
e2e0b3e5 20273 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 20274 dwarf_tag_name (die->tag));
c906108c
SS
20275 break;
20276 }
df8a16a1 20277
e37fd15a
SW
20278 if (suppress_add)
20279 {
20280 sym->hash_next = objfile->template_symbols;
20281 objfile->template_symbols = sym;
20282 list_to_add = NULL;
20283 }
20284
20285 if (list_to_add != NULL)
20286 add_symbol_to_list (sym, list_to_add);
20287
df8a16a1
DJ
20288 /* For the benefit of old versions of GCC, check for anonymous
20289 namespaces based on the demangled name. */
4d4ec4e5 20290 if (!cu->processing_has_namespace_info
94af9270 20291 && cu->language == language_cplus)
a10964d1 20292 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
20293 }
20294 return (sym);
20295}
20296
34eaf542
TT
20297/* A wrapper for new_symbol_full that always allocates a new symbol. */
20298
20299static struct symbol *
20300new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20301{
20302 return new_symbol_full (die, type, cu, NULL);
20303}
20304
98bfdba5
PA
20305/* Given an attr with a DW_FORM_dataN value in host byte order,
20306 zero-extend it as appropriate for the symbol's type. The DWARF
20307 standard (v4) is not entirely clear about the meaning of using
20308 DW_FORM_dataN for a constant with a signed type, where the type is
20309 wider than the data. The conclusion of a discussion on the DWARF
20310 list was that this is unspecified. We choose to always zero-extend
20311 because that is the interpretation long in use by GCC. */
c906108c 20312
98bfdba5 20313static gdb_byte *
ff39bb5e 20314dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 20315 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 20316{
e7c27a73 20317 struct objfile *objfile = cu->objfile;
e17a4113
UW
20318 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20319 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
20320 LONGEST l = DW_UNSND (attr);
20321
20322 if (bits < sizeof (*value) * 8)
20323 {
20324 l &= ((LONGEST) 1 << bits) - 1;
20325 *value = l;
20326 }
20327 else if (bits == sizeof (*value) * 8)
20328 *value = l;
20329 else
20330 {
224c3ddb 20331 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
20332 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20333 return bytes;
20334 }
20335
20336 return NULL;
20337}
20338
20339/* Read a constant value from an attribute. Either set *VALUE, or if
20340 the value does not fit in *VALUE, set *BYTES - either already
20341 allocated on the objfile obstack, or newly allocated on OBSTACK,
20342 or, set *BATON, if we translated the constant to a location
20343 expression. */
20344
20345static void
ff39bb5e 20346dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
20347 const char *name, struct obstack *obstack,
20348 struct dwarf2_cu *cu,
d521ce57 20349 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
20350 struct dwarf2_locexpr_baton **baton)
20351{
20352 struct objfile *objfile = cu->objfile;
20353 struct comp_unit_head *cu_header = &cu->header;
c906108c 20354 struct dwarf_block *blk;
98bfdba5
PA
20355 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20356 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20357
20358 *value = 0;
20359 *bytes = NULL;
20360 *baton = NULL;
c906108c
SS
20361
20362 switch (attr->form)
20363 {
20364 case DW_FORM_addr:
3019eac3 20365 case DW_FORM_GNU_addr_index:
ac56253d 20366 {
ac56253d
TT
20367 gdb_byte *data;
20368
98bfdba5
PA
20369 if (TYPE_LENGTH (type) != cu_header->addr_size)
20370 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 20371 cu_header->addr_size,
98bfdba5 20372 TYPE_LENGTH (type));
ac56253d
TT
20373 /* Symbols of this form are reasonably rare, so we just
20374 piggyback on the existing location code rather than writing
20375 a new implementation of symbol_computed_ops. */
8d749320 20376 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
20377 (*baton)->per_cu = cu->per_cu;
20378 gdb_assert ((*baton)->per_cu);
ac56253d 20379
98bfdba5 20380 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 20381 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 20382 (*baton)->data = data;
ac56253d
TT
20383
20384 data[0] = DW_OP_addr;
20385 store_unsigned_integer (&data[1], cu_header->addr_size,
20386 byte_order, DW_ADDR (attr));
20387 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 20388 }
c906108c 20389 break;
4ac36638 20390 case DW_FORM_string:
93b5768b 20391 case DW_FORM_strp:
3019eac3 20392 case DW_FORM_GNU_str_index:
36586728 20393 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
20394 /* DW_STRING is already allocated on the objfile obstack, point
20395 directly to it. */
d521ce57 20396 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 20397 break;
c906108c
SS
20398 case DW_FORM_block1:
20399 case DW_FORM_block2:
20400 case DW_FORM_block4:
20401 case DW_FORM_block:
2dc7f7b3 20402 case DW_FORM_exprloc:
0224619f 20403 case DW_FORM_data16:
c906108c 20404 blk = DW_BLOCK (attr);
98bfdba5
PA
20405 if (TYPE_LENGTH (type) != blk->size)
20406 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20407 TYPE_LENGTH (type));
20408 *bytes = blk->data;
c906108c 20409 break;
2df3850c
JM
20410
20411 /* The DW_AT_const_value attributes are supposed to carry the
20412 symbol's value "represented as it would be on the target
20413 architecture." By the time we get here, it's already been
20414 converted to host endianness, so we just need to sign- or
20415 zero-extend it as appropriate. */
20416 case DW_FORM_data1:
3aef2284 20417 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 20418 break;
c906108c 20419 case DW_FORM_data2:
3aef2284 20420 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 20421 break;
c906108c 20422 case DW_FORM_data4:
3aef2284 20423 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 20424 break;
c906108c 20425 case DW_FORM_data8:
3aef2284 20426 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
20427 break;
20428
c906108c 20429 case DW_FORM_sdata:
663c44ac 20430 case DW_FORM_implicit_const:
98bfdba5 20431 *value = DW_SND (attr);
2df3850c
JM
20432 break;
20433
c906108c 20434 case DW_FORM_udata:
98bfdba5 20435 *value = DW_UNSND (attr);
c906108c 20436 break;
2df3850c 20437
c906108c 20438 default:
4d3c2250 20439 complaint (&symfile_complaints,
e2e0b3e5 20440 _("unsupported const value attribute form: '%s'"),
4d3c2250 20441 dwarf_form_name (attr->form));
98bfdba5 20442 *value = 0;
c906108c
SS
20443 break;
20444 }
20445}
20446
2df3850c 20447
98bfdba5
PA
20448/* Copy constant value from an attribute to a symbol. */
20449
2df3850c 20450static void
ff39bb5e 20451dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 20452 struct dwarf2_cu *cu)
2df3850c 20453{
98bfdba5 20454 struct objfile *objfile = cu->objfile;
12df843f 20455 LONGEST value;
d521ce57 20456 const gdb_byte *bytes;
98bfdba5 20457 struct dwarf2_locexpr_baton *baton;
2df3850c 20458
98bfdba5
PA
20459 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20460 SYMBOL_PRINT_NAME (sym),
20461 &objfile->objfile_obstack, cu,
20462 &value, &bytes, &baton);
2df3850c 20463
98bfdba5
PA
20464 if (baton != NULL)
20465 {
98bfdba5 20466 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 20467 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
20468 }
20469 else if (bytes != NULL)
20470 {
20471 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 20472 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
20473 }
20474 else
20475 {
20476 SYMBOL_VALUE (sym) = value;
f1e6e072 20477 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 20478 }
2df3850c
JM
20479}
20480
c906108c
SS
20481/* Return the type of the die in question using its DW_AT_type attribute. */
20482
20483static struct type *
e7c27a73 20484die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20485{
c906108c 20486 struct attribute *type_attr;
c906108c 20487
e142c38c 20488 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
20489 if (!type_attr)
20490 {
20491 /* A missing DW_AT_type represents a void type. */
46bf5051 20492 return objfile_type (cu->objfile)->builtin_void;
c906108c 20493 }
348e048f 20494
673bfd45 20495 return lookup_die_type (die, type_attr, cu);
c906108c
SS
20496}
20497
b4ba55a1
JB
20498/* True iff CU's producer generates GNAT Ada auxiliary information
20499 that allows to find parallel types through that information instead
20500 of having to do expensive parallel lookups by type name. */
20501
20502static int
20503need_gnat_info (struct dwarf2_cu *cu)
20504{
20505 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
20506 of GNAT produces this auxiliary information, without any indication
20507 that it is produced. Part of enhancing the FSF version of GNAT
20508 to produce that information will be to put in place an indicator
20509 that we can use in order to determine whether the descriptive type
20510 info is available or not. One suggestion that has been made is
20511 to use a new attribute, attached to the CU die. For now, assume
20512 that the descriptive type info is not available. */
20513 return 0;
20514}
20515
b4ba55a1
JB
20516/* Return the auxiliary type of the die in question using its
20517 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20518 attribute is not present. */
20519
20520static struct type *
20521die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20522{
b4ba55a1 20523 struct attribute *type_attr;
b4ba55a1
JB
20524
20525 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20526 if (!type_attr)
20527 return NULL;
20528
673bfd45 20529 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
20530}
20531
20532/* If DIE has a descriptive_type attribute, then set the TYPE's
20533 descriptive type accordingly. */
20534
20535static void
20536set_descriptive_type (struct type *type, struct die_info *die,
20537 struct dwarf2_cu *cu)
20538{
20539 struct type *descriptive_type = die_descriptive_type (die, cu);
20540
20541 if (descriptive_type)
20542 {
20543 ALLOCATE_GNAT_AUX_TYPE (type);
20544 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20545 }
20546}
20547
c906108c
SS
20548/* Return the containing type of the die in question using its
20549 DW_AT_containing_type attribute. */
20550
20551static struct type *
e7c27a73 20552die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20553{
c906108c 20554 struct attribute *type_attr;
c906108c 20555
e142c38c 20556 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
20557 if (!type_attr)
20558 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 20559 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 20560
673bfd45 20561 return lookup_die_type (die, type_attr, cu);
c906108c
SS
20562}
20563
ac9ec31b
DE
20564/* Return an error marker type to use for the ill formed type in DIE/CU. */
20565
20566static struct type *
20567build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20568{
20569 struct objfile *objfile = dwarf2_per_objfile->objfile;
20570 char *message, *saved;
20571
20572 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 20573 objfile_name (objfile),
9c541725
PA
20574 to_underlying (cu->header.sect_off),
20575 to_underlying (die->sect_off));
224c3ddb
SM
20576 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
20577 message, strlen (message));
ac9ec31b
DE
20578 xfree (message);
20579
19f392bc 20580 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
20581}
20582
673bfd45 20583/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
20584 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20585 DW_AT_containing_type.
673bfd45
DE
20586 If there is no type substitute an error marker. */
20587
c906108c 20588static struct type *
ff39bb5e 20589lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 20590 struct dwarf2_cu *cu)
c906108c 20591{
bb5ed363 20592 struct objfile *objfile = cu->objfile;
f792889a
DJ
20593 struct type *this_type;
20594
ac9ec31b
DE
20595 gdb_assert (attr->name == DW_AT_type
20596 || attr->name == DW_AT_GNAT_descriptive_type
20597 || attr->name == DW_AT_containing_type);
20598
673bfd45
DE
20599 /* First see if we have it cached. */
20600
36586728
TT
20601 if (attr->form == DW_FORM_GNU_ref_alt)
20602 {
20603 struct dwarf2_per_cu_data *per_cu;
9c541725 20604 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 20605
9c541725
PA
20606 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
20607 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 20608 }
7771576e 20609 else if (attr_form_is_ref (attr))
673bfd45 20610 {
9c541725 20611 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 20612
9c541725 20613 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 20614 }
55f1336d 20615 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 20616 {
ac9ec31b 20617 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 20618
ac9ec31b 20619 return get_signatured_type (die, signature, cu);
673bfd45
DE
20620 }
20621 else
20622 {
ac9ec31b
DE
20623 complaint (&symfile_complaints,
20624 _("Dwarf Error: Bad type attribute %s in DIE"
20625 " at 0x%x [in module %s]"),
9c541725 20626 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
4262abfb 20627 objfile_name (objfile));
ac9ec31b 20628 return build_error_marker_type (cu, die);
673bfd45
DE
20629 }
20630
20631 /* If not cached we need to read it in. */
20632
20633 if (this_type == NULL)
20634 {
ac9ec31b 20635 struct die_info *type_die = NULL;
673bfd45
DE
20636 struct dwarf2_cu *type_cu = cu;
20637
7771576e 20638 if (attr_form_is_ref (attr))
ac9ec31b
DE
20639 type_die = follow_die_ref (die, attr, &type_cu);
20640 if (type_die == NULL)
20641 return build_error_marker_type (cu, die);
20642 /* If we find the type now, it's probably because the type came
3019eac3
DE
20643 from an inter-CU reference and the type's CU got expanded before
20644 ours. */
ac9ec31b 20645 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
20646 }
20647
20648 /* If we still don't have a type use an error marker. */
20649
20650 if (this_type == NULL)
ac9ec31b 20651 return build_error_marker_type (cu, die);
673bfd45 20652
f792889a 20653 return this_type;
c906108c
SS
20654}
20655
673bfd45
DE
20656/* Return the type in DIE, CU.
20657 Returns NULL for invalid types.
20658
02142a6c 20659 This first does a lookup in die_type_hash,
673bfd45
DE
20660 and only reads the die in if necessary.
20661
20662 NOTE: This can be called when reading in partial or full symbols. */
20663
f792889a 20664static struct type *
e7c27a73 20665read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20666{
f792889a
DJ
20667 struct type *this_type;
20668
20669 this_type = get_die_type (die, cu);
20670 if (this_type)
20671 return this_type;
20672
673bfd45
DE
20673 return read_type_die_1 (die, cu);
20674}
20675
20676/* Read the type in DIE, CU.
20677 Returns NULL for invalid types. */
20678
20679static struct type *
20680read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20681{
20682 struct type *this_type = NULL;
20683
c906108c
SS
20684 switch (die->tag)
20685 {
20686 case DW_TAG_class_type:
680b30c7 20687 case DW_TAG_interface_type:
c906108c
SS
20688 case DW_TAG_structure_type:
20689 case DW_TAG_union_type:
f792889a 20690 this_type = read_structure_type (die, cu);
c906108c
SS
20691 break;
20692 case DW_TAG_enumeration_type:
f792889a 20693 this_type = read_enumeration_type (die, cu);
c906108c
SS
20694 break;
20695 case DW_TAG_subprogram:
20696 case DW_TAG_subroutine_type:
edb3359d 20697 case DW_TAG_inlined_subroutine:
f792889a 20698 this_type = read_subroutine_type (die, cu);
c906108c
SS
20699 break;
20700 case DW_TAG_array_type:
f792889a 20701 this_type = read_array_type (die, cu);
c906108c 20702 break;
72019c9c 20703 case DW_TAG_set_type:
f792889a 20704 this_type = read_set_type (die, cu);
72019c9c 20705 break;
c906108c 20706 case DW_TAG_pointer_type:
f792889a 20707 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
20708 break;
20709 case DW_TAG_ptr_to_member_type:
f792889a 20710 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
20711 break;
20712 case DW_TAG_reference_type:
4297a3f0
AV
20713 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20714 break;
20715 case DW_TAG_rvalue_reference_type:
20716 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
20717 break;
20718 case DW_TAG_const_type:
f792889a 20719 this_type = read_tag_const_type (die, cu);
c906108c
SS
20720 break;
20721 case DW_TAG_volatile_type:
f792889a 20722 this_type = read_tag_volatile_type (die, cu);
c906108c 20723 break;
06d66ee9
TT
20724 case DW_TAG_restrict_type:
20725 this_type = read_tag_restrict_type (die, cu);
20726 break;
c906108c 20727 case DW_TAG_string_type:
f792889a 20728 this_type = read_tag_string_type (die, cu);
c906108c
SS
20729 break;
20730 case DW_TAG_typedef:
f792889a 20731 this_type = read_typedef (die, cu);
c906108c 20732 break;
a02abb62 20733 case DW_TAG_subrange_type:
f792889a 20734 this_type = read_subrange_type (die, cu);
a02abb62 20735 break;
c906108c 20736 case DW_TAG_base_type:
f792889a 20737 this_type = read_base_type (die, cu);
c906108c 20738 break;
81a17f79 20739 case DW_TAG_unspecified_type:
f792889a 20740 this_type = read_unspecified_type (die, cu);
81a17f79 20741 break;
0114d602
DJ
20742 case DW_TAG_namespace:
20743 this_type = read_namespace_type (die, cu);
20744 break;
f55ee35c
JK
20745 case DW_TAG_module:
20746 this_type = read_module_type (die, cu);
20747 break;
a2c2acaf
MW
20748 case DW_TAG_atomic_type:
20749 this_type = read_tag_atomic_type (die, cu);
20750 break;
c906108c 20751 default:
3e43a32a
MS
20752 complaint (&symfile_complaints,
20753 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 20754 dwarf_tag_name (die->tag));
c906108c
SS
20755 break;
20756 }
63d06c5c 20757
f792889a 20758 return this_type;
63d06c5c
DC
20759}
20760
abc72ce4
DE
20761/* See if we can figure out if the class lives in a namespace. We do
20762 this by looking for a member function; its demangled name will
20763 contain namespace info, if there is any.
20764 Return the computed name or NULL.
20765 Space for the result is allocated on the objfile's obstack.
20766 This is the full-die version of guess_partial_die_structure_name.
20767 In this case we know DIE has no useful parent. */
20768
20769static char *
20770guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20771{
20772 struct die_info *spec_die;
20773 struct dwarf2_cu *spec_cu;
20774 struct die_info *child;
20775
20776 spec_cu = cu;
20777 spec_die = die_specification (die, &spec_cu);
20778 if (spec_die != NULL)
20779 {
20780 die = spec_die;
20781 cu = spec_cu;
20782 }
20783
20784 for (child = die->child;
20785 child != NULL;
20786 child = child->sibling)
20787 {
20788 if (child->tag == DW_TAG_subprogram)
20789 {
73b9be8b 20790 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 20791
7d45c7c3 20792 if (linkage_name != NULL)
abc72ce4
DE
20793 {
20794 char *actual_name
20795 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 20796 linkage_name);
abc72ce4
DE
20797 char *name = NULL;
20798
20799 if (actual_name != NULL)
20800 {
15d034d0 20801 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
20802
20803 if (die_name != NULL
20804 && strcmp (die_name, actual_name) != 0)
20805 {
20806 /* Strip off the class name from the full name.
20807 We want the prefix. */
20808 int die_name_len = strlen (die_name);
20809 int actual_name_len = strlen (actual_name);
20810
20811 /* Test for '::' as a sanity check. */
20812 if (actual_name_len > die_name_len + 2
3e43a32a
MS
20813 && actual_name[actual_name_len
20814 - die_name_len - 1] == ':')
224c3ddb
SM
20815 name = (char *) obstack_copy0 (
20816 &cu->objfile->per_bfd->storage_obstack,
20817 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
20818 }
20819 }
20820 xfree (actual_name);
20821 return name;
20822 }
20823 }
20824 }
20825
20826 return NULL;
20827}
20828
96408a79
SA
20829/* GCC might emit a nameless typedef that has a linkage name. Determine the
20830 prefix part in such case. See
20831 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20832
a121b7c1 20833static const char *
96408a79
SA
20834anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
20835{
20836 struct attribute *attr;
e6a959d6 20837 const char *base;
96408a79
SA
20838
20839 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
20840 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
20841 return NULL;
20842
7d45c7c3 20843 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
20844 return NULL;
20845
73b9be8b 20846 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
20847 if (attr == NULL || DW_STRING (attr) == NULL)
20848 return NULL;
20849
20850 /* dwarf2_name had to be already called. */
20851 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20852
20853 /* Strip the base name, keep any leading namespaces/classes. */
20854 base = strrchr (DW_STRING (attr), ':');
20855 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20856 return "";
20857
224c3ddb
SM
20858 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20859 DW_STRING (attr),
20860 &base[-1] - DW_STRING (attr));
96408a79
SA
20861}
20862
fdde2d81 20863/* Return the name of the namespace/class that DIE is defined within,
0114d602 20864 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 20865
0114d602
DJ
20866 For example, if we're within the method foo() in the following
20867 code:
20868
20869 namespace N {
20870 class C {
20871 void foo () {
20872 }
20873 };
20874 }
20875
20876 then determine_prefix on foo's die will return "N::C". */
fdde2d81 20877
0d5cff50 20878static const char *
e142c38c 20879determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 20880{
0114d602
DJ
20881 struct die_info *parent, *spec_die;
20882 struct dwarf2_cu *spec_cu;
20883 struct type *parent_type;
a121b7c1 20884 const char *retval;
63d06c5c 20885
9c37b5ae 20886 if (cu->language != language_cplus
c44af4eb
TT
20887 && cu->language != language_fortran && cu->language != language_d
20888 && cu->language != language_rust)
0114d602
DJ
20889 return "";
20890
96408a79
SA
20891 retval = anonymous_struct_prefix (die, cu);
20892 if (retval)
20893 return retval;
20894
0114d602
DJ
20895 /* We have to be careful in the presence of DW_AT_specification.
20896 For example, with GCC 3.4, given the code
20897
20898 namespace N {
20899 void foo() {
20900 // Definition of N::foo.
20901 }
20902 }
20903
20904 then we'll have a tree of DIEs like this:
20905
20906 1: DW_TAG_compile_unit
20907 2: DW_TAG_namespace // N
20908 3: DW_TAG_subprogram // declaration of N::foo
20909 4: DW_TAG_subprogram // definition of N::foo
20910 DW_AT_specification // refers to die #3
20911
20912 Thus, when processing die #4, we have to pretend that we're in
20913 the context of its DW_AT_specification, namely the contex of die
20914 #3. */
20915 spec_cu = cu;
20916 spec_die = die_specification (die, &spec_cu);
20917 if (spec_die == NULL)
20918 parent = die->parent;
20919 else
63d06c5c 20920 {
0114d602
DJ
20921 parent = spec_die->parent;
20922 cu = spec_cu;
63d06c5c 20923 }
0114d602
DJ
20924
20925 if (parent == NULL)
20926 return "";
98bfdba5
PA
20927 else if (parent->building_fullname)
20928 {
20929 const char *name;
20930 const char *parent_name;
20931
20932 /* It has been seen on RealView 2.2 built binaries,
20933 DW_TAG_template_type_param types actually _defined_ as
20934 children of the parent class:
20935
20936 enum E {};
20937 template class <class Enum> Class{};
20938 Class<enum E> class_e;
20939
20940 1: DW_TAG_class_type (Class)
20941 2: DW_TAG_enumeration_type (E)
20942 3: DW_TAG_enumerator (enum1:0)
20943 3: DW_TAG_enumerator (enum2:1)
20944 ...
20945 2: DW_TAG_template_type_param
20946 DW_AT_type DW_FORM_ref_udata (E)
20947
20948 Besides being broken debug info, it can put GDB into an
20949 infinite loop. Consider:
20950
20951 When we're building the full name for Class<E>, we'll start
20952 at Class, and go look over its template type parameters,
20953 finding E. We'll then try to build the full name of E, and
20954 reach here. We're now trying to build the full name of E,
20955 and look over the parent DIE for containing scope. In the
20956 broken case, if we followed the parent DIE of E, we'd again
20957 find Class, and once again go look at its template type
20958 arguments, etc., etc. Simply don't consider such parent die
20959 as source-level parent of this die (it can't be, the language
20960 doesn't allow it), and break the loop here. */
20961 name = dwarf2_name (die, cu);
20962 parent_name = dwarf2_name (parent, cu);
20963 complaint (&symfile_complaints,
20964 _("template param type '%s' defined within parent '%s'"),
20965 name ? name : "<unknown>",
20966 parent_name ? parent_name : "<unknown>");
20967 return "";
20968 }
63d06c5c 20969 else
0114d602
DJ
20970 switch (parent->tag)
20971 {
63d06c5c 20972 case DW_TAG_namespace:
0114d602 20973 parent_type = read_type_die (parent, cu);
acebe513
UW
20974 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
20975 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
20976 Work around this problem here. */
20977 if (cu->language == language_cplus
20978 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
20979 return "";
0114d602
DJ
20980 /* We give a name to even anonymous namespaces. */
20981 return TYPE_TAG_NAME (parent_type);
63d06c5c 20982 case DW_TAG_class_type:
680b30c7 20983 case DW_TAG_interface_type:
63d06c5c 20984 case DW_TAG_structure_type:
0114d602 20985 case DW_TAG_union_type:
f55ee35c 20986 case DW_TAG_module:
0114d602
DJ
20987 parent_type = read_type_die (parent, cu);
20988 if (TYPE_TAG_NAME (parent_type) != NULL)
20989 return TYPE_TAG_NAME (parent_type);
20990 else
20991 /* An anonymous structure is only allowed non-static data
20992 members; no typedefs, no member functions, et cetera.
20993 So it does not need a prefix. */
20994 return "";
abc72ce4 20995 case DW_TAG_compile_unit:
95554aad 20996 case DW_TAG_partial_unit:
abc72ce4
DE
20997 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
20998 if (cu->language == language_cplus
8b70b953 20999 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
21000 && die->child != NULL
21001 && (die->tag == DW_TAG_class_type
21002 || die->tag == DW_TAG_structure_type
21003 || die->tag == DW_TAG_union_type))
21004 {
21005 char *name = guess_full_die_structure_name (die, cu);
21006 if (name != NULL)
21007 return name;
21008 }
21009 return "";
3d567982
TT
21010 case DW_TAG_enumeration_type:
21011 parent_type = read_type_die (parent, cu);
21012 if (TYPE_DECLARED_CLASS (parent_type))
21013 {
21014 if (TYPE_TAG_NAME (parent_type) != NULL)
21015 return TYPE_TAG_NAME (parent_type);
21016 return "";
21017 }
21018 /* Fall through. */
63d06c5c 21019 default:
8176b9b8 21020 return determine_prefix (parent, cu);
63d06c5c 21021 }
63d06c5c
DC
21022}
21023
3e43a32a
MS
21024/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21025 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21026 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21027 an obconcat, otherwise allocate storage for the result. The CU argument is
21028 used to determine the language and hence, the appropriate separator. */
987504bb 21029
f55ee35c 21030#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21031
21032static char *
f55ee35c
JK
21033typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21034 int physname, struct dwarf2_cu *cu)
63d06c5c 21035{
f55ee35c 21036 const char *lead = "";
5c315b68 21037 const char *sep;
63d06c5c 21038
3e43a32a
MS
21039 if (suffix == NULL || suffix[0] == '\0'
21040 || prefix == NULL || prefix[0] == '\0')
987504bb 21041 sep = "";
45280282
IB
21042 else if (cu->language == language_d)
21043 {
21044 /* For D, the 'main' function could be defined in any module, but it
21045 should never be prefixed. */
21046 if (strcmp (suffix, "D main") == 0)
21047 {
21048 prefix = "";
21049 sep = "";
21050 }
21051 else
21052 sep = ".";
21053 }
f55ee35c
JK
21054 else if (cu->language == language_fortran && physname)
21055 {
21056 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21057 DW_AT_MIPS_linkage_name is preferred and used instead. */
21058
21059 lead = "__";
21060 sep = "_MOD_";
21061 }
987504bb
JJ
21062 else
21063 sep = "::";
63d06c5c 21064
6dd47d34
DE
21065 if (prefix == NULL)
21066 prefix = "";
21067 if (suffix == NULL)
21068 suffix = "";
21069
987504bb
JJ
21070 if (obs == NULL)
21071 {
3e43a32a 21072 char *retval
224c3ddb
SM
21073 = ((char *)
21074 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21075
f55ee35c
JK
21076 strcpy (retval, lead);
21077 strcat (retval, prefix);
6dd47d34
DE
21078 strcat (retval, sep);
21079 strcat (retval, suffix);
63d06c5c
DC
21080 return retval;
21081 }
987504bb
JJ
21082 else
21083 {
21084 /* We have an obstack. */
f55ee35c 21085 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21086 }
63d06c5c
DC
21087}
21088
c906108c
SS
21089/* Return sibling of die, NULL if no sibling. */
21090
f9aca02d 21091static struct die_info *
fba45db2 21092sibling_die (struct die_info *die)
c906108c 21093{
639d11d3 21094 return die->sibling;
c906108c
SS
21095}
21096
71c25dea
TT
21097/* Get name of a die, return NULL if not found. */
21098
15d034d0
TT
21099static const char *
21100dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
21101 struct obstack *obstack)
21102{
21103 if (name && cu->language == language_cplus)
21104 {
2f408ecb 21105 std::string canon_name = cp_canonicalize_string (name);
71c25dea 21106
2f408ecb 21107 if (!canon_name.empty ())
71c25dea 21108 {
2f408ecb
PA
21109 if (canon_name != name)
21110 name = (const char *) obstack_copy0 (obstack,
21111 canon_name.c_str (),
21112 canon_name.length ());
71c25dea
TT
21113 }
21114 }
21115
21116 return name;
c906108c
SS
21117}
21118
96553a0c
DE
21119/* Get name of a die, return NULL if not found.
21120 Anonymous namespaces are converted to their magic string. */
9219021c 21121
15d034d0 21122static const char *
e142c38c 21123dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21124{
21125 struct attribute *attr;
21126
e142c38c 21127 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21128 if ((!attr || !DW_STRING (attr))
96553a0c 21129 && die->tag != DW_TAG_namespace
53832f31
TT
21130 && die->tag != DW_TAG_class_type
21131 && die->tag != DW_TAG_interface_type
21132 && die->tag != DW_TAG_structure_type
21133 && die->tag != DW_TAG_union_type)
71c25dea
TT
21134 return NULL;
21135
21136 switch (die->tag)
21137 {
21138 case DW_TAG_compile_unit:
95554aad 21139 case DW_TAG_partial_unit:
71c25dea
TT
21140 /* Compilation units have a DW_AT_name that is a filename, not
21141 a source language identifier. */
21142 case DW_TAG_enumeration_type:
21143 case DW_TAG_enumerator:
21144 /* These tags always have simple identifiers already; no need
21145 to canonicalize them. */
21146 return DW_STRING (attr);
907af001 21147
96553a0c
DE
21148 case DW_TAG_namespace:
21149 if (attr != NULL && DW_STRING (attr) != NULL)
21150 return DW_STRING (attr);
21151 return CP_ANONYMOUS_NAMESPACE_STR;
21152
907af001
UW
21153 case DW_TAG_class_type:
21154 case DW_TAG_interface_type:
21155 case DW_TAG_structure_type:
21156 case DW_TAG_union_type:
21157 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21158 structures or unions. These were of the form "._%d" in GCC 4.1,
21159 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21160 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21161 if (attr && DW_STRING (attr)
61012eef
GB
21162 && (startswith (DW_STRING (attr), "._")
21163 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21164 return NULL;
53832f31
TT
21165
21166 /* GCC might emit a nameless typedef that has a linkage name. See
21167 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21168 if (!attr || DW_STRING (attr) == NULL)
21169 {
df5c6c50 21170 char *demangled = NULL;
53832f31 21171
73b9be8b 21172 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21173 if (attr == NULL || DW_STRING (attr) == NULL)
21174 return NULL;
21175
df5c6c50
JK
21176 /* Avoid demangling DW_STRING (attr) the second time on a second
21177 call for the same DIE. */
21178 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 21179 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
21180
21181 if (demangled)
21182 {
e6a959d6 21183 const char *base;
96408a79 21184
53832f31 21185 /* FIXME: we already did this for the partial symbol... */
34a68019 21186 DW_STRING (attr)
224c3ddb
SM
21187 = ((const char *)
21188 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
21189 demangled, strlen (demangled)));
53832f31
TT
21190 DW_STRING_IS_CANONICAL (attr) = 1;
21191 xfree (demangled);
96408a79
SA
21192
21193 /* Strip any leading namespaces/classes, keep only the base name.
21194 DW_AT_name for named DIEs does not contain the prefixes. */
21195 base = strrchr (DW_STRING (attr), ':');
21196 if (base && base > DW_STRING (attr) && base[-1] == ':')
21197 return &base[1];
21198 else
21199 return DW_STRING (attr);
53832f31
TT
21200 }
21201 }
907af001
UW
21202 break;
21203
71c25dea 21204 default:
907af001
UW
21205 break;
21206 }
21207
21208 if (!DW_STRING_IS_CANONICAL (attr))
21209 {
21210 DW_STRING (attr)
21211 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 21212 &cu->objfile->per_bfd->storage_obstack);
907af001 21213 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21214 }
907af001 21215 return DW_STRING (attr);
9219021c
DC
21216}
21217
21218/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21219 is none. *EXT_CU is the CU containing DIE on input, and the CU
21220 containing the return value on output. */
9219021c
DC
21221
21222static struct die_info *
f2f0e013 21223dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21224{
21225 struct attribute *attr;
9219021c 21226
f2f0e013 21227 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21228 if (attr == NULL)
21229 return NULL;
21230
f2f0e013 21231 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21232}
21233
c906108c
SS
21234/* Convert a DIE tag into its string name. */
21235
f39c6ffd 21236static const char *
aa1ee363 21237dwarf_tag_name (unsigned tag)
c906108c 21238{
f39c6ffd
TT
21239 const char *name = get_DW_TAG_name (tag);
21240
21241 if (name == NULL)
21242 return "DW_TAG_<unknown>";
21243
21244 return name;
c906108c
SS
21245}
21246
21247/* Convert a DWARF attribute code into its string name. */
21248
f39c6ffd 21249static const char *
aa1ee363 21250dwarf_attr_name (unsigned attr)
c906108c 21251{
f39c6ffd
TT
21252 const char *name;
21253
c764a876 21254#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
21255 if (attr == DW_AT_MIPS_fde)
21256 return "DW_AT_MIPS_fde";
21257#else
21258 if (attr == DW_AT_HP_block_index)
21259 return "DW_AT_HP_block_index";
c764a876 21260#endif
f39c6ffd
TT
21261
21262 name = get_DW_AT_name (attr);
21263
21264 if (name == NULL)
21265 return "DW_AT_<unknown>";
21266
21267 return name;
c906108c
SS
21268}
21269
21270/* Convert a DWARF value form code into its string name. */
21271
f39c6ffd 21272static const char *
aa1ee363 21273dwarf_form_name (unsigned form)
c906108c 21274{
f39c6ffd
TT
21275 const char *name = get_DW_FORM_name (form);
21276
21277 if (name == NULL)
21278 return "DW_FORM_<unknown>";
21279
21280 return name;
c906108c
SS
21281}
21282
a121b7c1 21283static const char *
fba45db2 21284dwarf_bool_name (unsigned mybool)
c906108c
SS
21285{
21286 if (mybool)
21287 return "TRUE";
21288 else
21289 return "FALSE";
21290}
21291
21292/* Convert a DWARF type code into its string name. */
21293
f39c6ffd 21294static const char *
aa1ee363 21295dwarf_type_encoding_name (unsigned enc)
c906108c 21296{
f39c6ffd 21297 const char *name = get_DW_ATE_name (enc);
c906108c 21298
f39c6ffd
TT
21299 if (name == NULL)
21300 return "DW_ATE_<unknown>";
c906108c 21301
f39c6ffd 21302 return name;
c906108c 21303}
c906108c 21304
f9aca02d 21305static void
d97bc12b 21306dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21307{
21308 unsigned int i;
21309
d97bc12b
DE
21310 print_spaces (indent, f);
21311 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
9c541725
PA
21312 dwarf_tag_name (die->tag), die->abbrev,
21313 to_underlying (die->sect_off));
d97bc12b
DE
21314
21315 if (die->parent != NULL)
21316 {
21317 print_spaces (indent, f);
21318 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9c541725 21319 to_underlying (die->parent->sect_off));
d97bc12b
DE
21320 }
21321
21322 print_spaces (indent, f);
21323 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21324 dwarf_bool_name (die->child != NULL));
c906108c 21325
d97bc12b
DE
21326 print_spaces (indent, f);
21327 fprintf_unfiltered (f, " attributes:\n");
21328
c906108c
SS
21329 for (i = 0; i < die->num_attrs; ++i)
21330 {
d97bc12b
DE
21331 print_spaces (indent, f);
21332 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21333 dwarf_attr_name (die->attrs[i].name),
21334 dwarf_form_name (die->attrs[i].form));
d97bc12b 21335
c906108c
SS
21336 switch (die->attrs[i].form)
21337 {
c906108c 21338 case DW_FORM_addr:
3019eac3 21339 case DW_FORM_GNU_addr_index:
d97bc12b 21340 fprintf_unfiltered (f, "address: ");
5af949e3 21341 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
21342 break;
21343 case DW_FORM_block2:
21344 case DW_FORM_block4:
21345 case DW_FORM_block:
21346 case DW_FORM_block1:
56eb65bd
SP
21347 fprintf_unfiltered (f, "block: size %s",
21348 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 21349 break;
2dc7f7b3 21350 case DW_FORM_exprloc:
56eb65bd
SP
21351 fprintf_unfiltered (f, "expression: size %s",
21352 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 21353 break;
0224619f
JK
21354 case DW_FORM_data16:
21355 fprintf_unfiltered (f, "constant of 16 bytes");
21356 break;
4568ecf9
DE
21357 case DW_FORM_ref_addr:
21358 fprintf_unfiltered (f, "ref address: ");
21359 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21360 break;
36586728
TT
21361 case DW_FORM_GNU_ref_alt:
21362 fprintf_unfiltered (f, "alt ref address: ");
21363 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21364 break;
10b3939b
DJ
21365 case DW_FORM_ref1:
21366 case DW_FORM_ref2:
21367 case DW_FORM_ref4:
4568ecf9
DE
21368 case DW_FORM_ref8:
21369 case DW_FORM_ref_udata:
d97bc12b 21370 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 21371 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 21372 break;
c906108c
SS
21373 case DW_FORM_data1:
21374 case DW_FORM_data2:
21375 case DW_FORM_data4:
ce5d95e1 21376 case DW_FORM_data8:
c906108c
SS
21377 case DW_FORM_udata:
21378 case DW_FORM_sdata:
43bbcdc2
PH
21379 fprintf_unfiltered (f, "constant: %s",
21380 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 21381 break;
2dc7f7b3
TT
21382 case DW_FORM_sec_offset:
21383 fprintf_unfiltered (f, "section offset: %s",
21384 pulongest (DW_UNSND (&die->attrs[i])));
21385 break;
55f1336d 21386 case DW_FORM_ref_sig8:
ac9ec31b
DE
21387 fprintf_unfiltered (f, "signature: %s",
21388 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 21389 break;
c906108c 21390 case DW_FORM_string:
4bdf3d34 21391 case DW_FORM_strp:
43988095 21392 case DW_FORM_line_strp:
3019eac3 21393 case DW_FORM_GNU_str_index:
36586728 21394 case DW_FORM_GNU_strp_alt:
8285870a 21395 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 21396 DW_STRING (&die->attrs[i])
8285870a
JK
21397 ? DW_STRING (&die->attrs[i]) : "",
21398 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
21399 break;
21400 case DW_FORM_flag:
21401 if (DW_UNSND (&die->attrs[i]))
d97bc12b 21402 fprintf_unfiltered (f, "flag: TRUE");
c906108c 21403 else
d97bc12b 21404 fprintf_unfiltered (f, "flag: FALSE");
c906108c 21405 break;
2dc7f7b3
TT
21406 case DW_FORM_flag_present:
21407 fprintf_unfiltered (f, "flag: TRUE");
21408 break;
a8329558 21409 case DW_FORM_indirect:
0963b4bd
MS
21410 /* The reader will have reduced the indirect form to
21411 the "base form" so this form should not occur. */
3e43a32a
MS
21412 fprintf_unfiltered (f,
21413 "unexpected attribute form: DW_FORM_indirect");
a8329558 21414 break;
663c44ac
JK
21415 case DW_FORM_implicit_const:
21416 fprintf_unfiltered (f, "constant: %s",
21417 plongest (DW_SND (&die->attrs[i])));
21418 break;
c906108c 21419 default:
d97bc12b 21420 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 21421 die->attrs[i].form);
d97bc12b 21422 break;
c906108c 21423 }
d97bc12b 21424 fprintf_unfiltered (f, "\n");
c906108c
SS
21425 }
21426}
21427
f9aca02d 21428static void
d97bc12b 21429dump_die_for_error (struct die_info *die)
c906108c 21430{
d97bc12b
DE
21431 dump_die_shallow (gdb_stderr, 0, die);
21432}
21433
21434static void
21435dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21436{
21437 int indent = level * 4;
21438
21439 gdb_assert (die != NULL);
21440
21441 if (level >= max_level)
21442 return;
21443
21444 dump_die_shallow (f, indent, die);
21445
21446 if (die->child != NULL)
c906108c 21447 {
d97bc12b
DE
21448 print_spaces (indent, f);
21449 fprintf_unfiltered (f, " Children:");
21450 if (level + 1 < max_level)
21451 {
21452 fprintf_unfiltered (f, "\n");
21453 dump_die_1 (f, level + 1, max_level, die->child);
21454 }
21455 else
21456 {
3e43a32a
MS
21457 fprintf_unfiltered (f,
21458 " [not printed, max nesting level reached]\n");
d97bc12b
DE
21459 }
21460 }
21461
21462 if (die->sibling != NULL && level > 0)
21463 {
21464 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
21465 }
21466}
21467
d97bc12b
DE
21468/* This is called from the pdie macro in gdbinit.in.
21469 It's not static so gcc will keep a copy callable from gdb. */
21470
21471void
21472dump_die (struct die_info *die, int max_level)
21473{
21474 dump_die_1 (gdb_stdlog, 0, max_level, die);
21475}
21476
f9aca02d 21477static void
51545339 21478store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21479{
51545339 21480 void **slot;
c906108c 21481
9c541725
PA
21482 slot = htab_find_slot_with_hash (cu->die_hash, die,
21483 to_underlying (die->sect_off),
b64f50a1 21484 INSERT);
51545339
DJ
21485
21486 *slot = die;
c906108c
SS
21487}
21488
b64f50a1
JK
21489/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
21490 required kind. */
21491
21492static sect_offset
ff39bb5e 21493dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 21494{
7771576e 21495 if (attr_form_is_ref (attr))
9c541725 21496 return (sect_offset) DW_UNSND (attr);
93311388
DE
21497
21498 complaint (&symfile_complaints,
21499 _("unsupported die ref attribute form: '%s'"),
21500 dwarf_form_name (attr->form));
9c541725 21501 return {};
c906108c
SS
21502}
21503
43bbcdc2
PH
21504/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
21505 * the value held by the attribute is not constant. */
a02abb62 21506
43bbcdc2 21507static LONGEST
ff39bb5e 21508dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 21509{
663c44ac 21510 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
21511 return DW_SND (attr);
21512 else if (attr->form == DW_FORM_udata
21513 || attr->form == DW_FORM_data1
21514 || attr->form == DW_FORM_data2
21515 || attr->form == DW_FORM_data4
21516 || attr->form == DW_FORM_data8)
21517 return DW_UNSND (attr);
21518 else
21519 {
0224619f 21520 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
21521 complaint (&symfile_complaints,
21522 _("Attribute value is not a constant (%s)"),
a02abb62
JB
21523 dwarf_form_name (attr->form));
21524 return default_value;
21525 }
21526}
21527
348e048f
DE
21528/* Follow reference or signature attribute ATTR of SRC_DIE.
21529 On entry *REF_CU is the CU of SRC_DIE.
21530 On exit *REF_CU is the CU of the result. */
21531
21532static struct die_info *
ff39bb5e 21533follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
21534 struct dwarf2_cu **ref_cu)
21535{
21536 struct die_info *die;
21537
7771576e 21538 if (attr_form_is_ref (attr))
348e048f 21539 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 21540 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
21541 die = follow_die_sig (src_die, attr, ref_cu);
21542 else
21543 {
21544 dump_die_for_error (src_die);
21545 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 21546 objfile_name ((*ref_cu)->objfile));
348e048f
DE
21547 }
21548
21549 return die;
03dd20cc
DJ
21550}
21551
5c631832 21552/* Follow reference OFFSET.
673bfd45
DE
21553 On entry *REF_CU is the CU of the source die referencing OFFSET.
21554 On exit *REF_CU is the CU of the result.
21555 Returns NULL if OFFSET is invalid. */
f504f079 21556
f9aca02d 21557static struct die_info *
9c541725 21558follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 21559 struct dwarf2_cu **ref_cu)
c906108c 21560{
10b3939b 21561 struct die_info temp_die;
f2f0e013 21562 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 21563
348e048f
DE
21564 gdb_assert (cu->per_cu != NULL);
21565
98bfdba5
PA
21566 target_cu = cu;
21567
3019eac3 21568 if (cu->per_cu->is_debug_types)
348e048f
DE
21569 {
21570 /* .debug_types CUs cannot reference anything outside their CU.
21571 If they need to, they have to reference a signatured type via
55f1336d 21572 DW_FORM_ref_sig8. */
9c541725 21573 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 21574 return NULL;
348e048f 21575 }
36586728 21576 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 21577 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
21578 {
21579 struct dwarf2_per_cu_data *per_cu;
9a619af0 21580
9c541725 21581 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 21582 cu->objfile);
03dd20cc
DJ
21583
21584 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
21585 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21586 load_full_comp_unit (per_cu, cu->language);
03dd20cc 21587
10b3939b
DJ
21588 target_cu = per_cu->cu;
21589 }
98bfdba5
PA
21590 else if (cu->dies == NULL)
21591 {
21592 /* We're loading full DIEs during partial symbol reading. */
21593 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 21594 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 21595 }
c906108c 21596
f2f0e013 21597 *ref_cu = target_cu;
9c541725 21598 temp_die.sect_off = sect_off;
9a3c8263 21599 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
21600 &temp_die,
21601 to_underlying (sect_off));
5c631832 21602}
10b3939b 21603
5c631832
JK
21604/* Follow reference attribute ATTR of SRC_DIE.
21605 On entry *REF_CU is the CU of SRC_DIE.
21606 On exit *REF_CU is the CU of the result. */
21607
21608static struct die_info *
ff39bb5e 21609follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
21610 struct dwarf2_cu **ref_cu)
21611{
9c541725 21612 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
21613 struct dwarf2_cu *cu = *ref_cu;
21614 struct die_info *die;
21615
9c541725 21616 die = follow_die_offset (sect_off,
36586728
TT
21617 (attr->form == DW_FORM_GNU_ref_alt
21618 || cu->per_cu->is_dwz),
21619 ref_cu);
5c631832
JK
21620 if (!die)
21621 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
21622 "at 0x%x [in module %s]"),
9c541725 21623 to_underlying (sect_off), to_underlying (src_die->sect_off),
4262abfb 21624 objfile_name (cu->objfile));
348e048f 21625
5c631832
JK
21626 return die;
21627}
21628
9c541725 21629/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b
JK
21630 Returned value is intended for DW_OP_call*. Returned
21631 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
21632
21633struct dwarf2_locexpr_baton
9c541725 21634dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
21635 struct dwarf2_per_cu_data *per_cu,
21636 CORE_ADDR (*get_frame_pc) (void *baton),
21637 void *baton)
5c631832 21638{
918dd910 21639 struct dwarf2_cu *cu;
5c631832
JK
21640 struct die_info *die;
21641 struct attribute *attr;
21642 struct dwarf2_locexpr_baton retval;
21643
8cf6f0b1
TT
21644 dw2_setup (per_cu->objfile);
21645
918dd910
JK
21646 if (per_cu->cu == NULL)
21647 load_cu (per_cu);
21648 cu = per_cu->cu;
cc12ce38
DE
21649 if (cu == NULL)
21650 {
21651 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21652 Instead just throw an error, not much else we can do. */
21653 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 21654 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 21655 }
918dd910 21656
9c541725 21657 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832
JK
21658 if (!die)
21659 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 21660 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
21661
21662 attr = dwarf2_attr (die, DW_AT_location, cu);
21663 if (!attr)
21664 {
e103e986
JK
21665 /* DWARF: "If there is no such attribute, then there is no effect.".
21666 DATA is ignored if SIZE is 0. */
5c631832 21667
e103e986 21668 retval.data = NULL;
5c631832
JK
21669 retval.size = 0;
21670 }
8cf6f0b1
TT
21671 else if (attr_form_is_section_offset (attr))
21672 {
21673 struct dwarf2_loclist_baton loclist_baton;
21674 CORE_ADDR pc = (*get_frame_pc) (baton);
21675 size_t size;
21676
21677 fill_in_loclist_baton (cu, &loclist_baton, attr);
21678
21679 retval.data = dwarf2_find_location_expression (&loclist_baton,
21680 &size, pc);
21681 retval.size = size;
21682 }
5c631832
JK
21683 else
21684 {
21685 if (!attr_form_is_block (attr))
21686 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
21687 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9c541725 21688 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
21689
21690 retval.data = DW_BLOCK (attr)->data;
21691 retval.size = DW_BLOCK (attr)->size;
21692 }
21693 retval.per_cu = cu->per_cu;
918dd910 21694
918dd910
JK
21695 age_cached_comp_units ();
21696
5c631832 21697 return retval;
348e048f
DE
21698}
21699
8b9737bf
TT
21700/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
21701 offset. */
21702
21703struct dwarf2_locexpr_baton
21704dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21705 struct dwarf2_per_cu_data *per_cu,
21706 CORE_ADDR (*get_frame_pc) (void *baton),
21707 void *baton)
21708{
9c541725 21709 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 21710
9c541725 21711 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
21712}
21713
b6807d98
TT
21714/* Write a constant of a given type as target-ordered bytes into
21715 OBSTACK. */
21716
21717static const gdb_byte *
21718write_constant_as_bytes (struct obstack *obstack,
21719 enum bfd_endian byte_order,
21720 struct type *type,
21721 ULONGEST value,
21722 LONGEST *len)
21723{
21724 gdb_byte *result;
21725
21726 *len = TYPE_LENGTH (type);
224c3ddb 21727 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
21728 store_unsigned_integer (result, *len, byte_order, value);
21729
21730 return result;
21731}
21732
21733/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
21734 pointer to the constant bytes and set LEN to the length of the
21735 data. If memory is needed, allocate it on OBSTACK. If the DIE
21736 does not have a DW_AT_const_value, return NULL. */
21737
21738const gdb_byte *
9c541725 21739dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
21740 struct dwarf2_per_cu_data *per_cu,
21741 struct obstack *obstack,
21742 LONGEST *len)
21743{
21744 struct dwarf2_cu *cu;
21745 struct die_info *die;
21746 struct attribute *attr;
21747 const gdb_byte *result = NULL;
21748 struct type *type;
21749 LONGEST value;
21750 enum bfd_endian byte_order;
21751
21752 dw2_setup (per_cu->objfile);
21753
21754 if (per_cu->cu == NULL)
21755 load_cu (per_cu);
21756 cu = per_cu->cu;
cc12ce38
DE
21757 if (cu == NULL)
21758 {
21759 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21760 Instead just throw an error, not much else we can do. */
21761 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 21762 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 21763 }
b6807d98 21764
9c541725 21765 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98
TT
21766 if (!die)
21767 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 21768 to_underlying (sect_off), objfile_name (per_cu->objfile));
b6807d98
TT
21769
21770
21771 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21772 if (attr == NULL)
21773 return NULL;
21774
21775 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
21776 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21777
21778 switch (attr->form)
21779 {
21780 case DW_FORM_addr:
21781 case DW_FORM_GNU_addr_index:
21782 {
21783 gdb_byte *tem;
21784
21785 *len = cu->header.addr_size;
224c3ddb 21786 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
21787 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
21788 result = tem;
21789 }
21790 break;
21791 case DW_FORM_string:
21792 case DW_FORM_strp:
21793 case DW_FORM_GNU_str_index:
21794 case DW_FORM_GNU_strp_alt:
21795 /* DW_STRING is already allocated on the objfile obstack, point
21796 directly to it. */
21797 result = (const gdb_byte *) DW_STRING (attr);
21798 *len = strlen (DW_STRING (attr));
21799 break;
21800 case DW_FORM_block1:
21801 case DW_FORM_block2:
21802 case DW_FORM_block4:
21803 case DW_FORM_block:
21804 case DW_FORM_exprloc:
0224619f 21805 case DW_FORM_data16:
b6807d98
TT
21806 result = DW_BLOCK (attr)->data;
21807 *len = DW_BLOCK (attr)->size;
21808 break;
21809
21810 /* The DW_AT_const_value attributes are supposed to carry the
21811 symbol's value "represented as it would be on the target
21812 architecture." By the time we get here, it's already been
21813 converted to host endianness, so we just need to sign- or
21814 zero-extend it as appropriate. */
21815 case DW_FORM_data1:
21816 type = die_type (die, cu);
21817 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
21818 if (result == NULL)
21819 result = write_constant_as_bytes (obstack, byte_order,
21820 type, value, len);
21821 break;
21822 case DW_FORM_data2:
21823 type = die_type (die, cu);
21824 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
21825 if (result == NULL)
21826 result = write_constant_as_bytes (obstack, byte_order,
21827 type, value, len);
21828 break;
21829 case DW_FORM_data4:
21830 type = die_type (die, cu);
21831 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
21832 if (result == NULL)
21833 result = write_constant_as_bytes (obstack, byte_order,
21834 type, value, len);
21835 break;
21836 case DW_FORM_data8:
21837 type = die_type (die, cu);
21838 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
21839 if (result == NULL)
21840 result = write_constant_as_bytes (obstack, byte_order,
21841 type, value, len);
21842 break;
21843
21844 case DW_FORM_sdata:
663c44ac 21845 case DW_FORM_implicit_const:
b6807d98
TT
21846 type = die_type (die, cu);
21847 result = write_constant_as_bytes (obstack, byte_order,
21848 type, DW_SND (attr), len);
21849 break;
21850
21851 case DW_FORM_udata:
21852 type = die_type (die, cu);
21853 result = write_constant_as_bytes (obstack, byte_order,
21854 type, DW_UNSND (attr), len);
21855 break;
21856
21857 default:
21858 complaint (&symfile_complaints,
21859 _("unsupported const value attribute form: '%s'"),
21860 dwarf_form_name (attr->form));
21861 break;
21862 }
21863
21864 return result;
21865}
21866
7942e96e
AA
21867/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
21868 valid type for this die is found. */
21869
21870struct type *
9c541725 21871dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
21872 struct dwarf2_per_cu_data *per_cu)
21873{
21874 struct dwarf2_cu *cu;
21875 struct die_info *die;
21876
21877 dw2_setup (per_cu->objfile);
21878
21879 if (per_cu->cu == NULL)
21880 load_cu (per_cu);
21881 cu = per_cu->cu;
21882 if (!cu)
21883 return NULL;
21884
9c541725 21885 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
21886 if (!die)
21887 return NULL;
21888
21889 return die_type (die, cu);
21890}
21891
8a9b8146
TT
21892/* Return the type of the DIE at DIE_OFFSET in the CU named by
21893 PER_CU. */
21894
21895struct type *
b64f50a1 21896dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
21897 struct dwarf2_per_cu_data *per_cu)
21898{
8a9b8146 21899 dw2_setup (per_cu->objfile);
b64f50a1 21900
9c541725 21901 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 21902 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
21903}
21904
ac9ec31b 21905/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 21906 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
21907 On exit *REF_CU is the CU of the result.
21908 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
21909
21910static struct die_info *
ac9ec31b
DE
21911follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21912 struct dwarf2_cu **ref_cu)
348e048f 21913{
348e048f 21914 struct die_info temp_die;
348e048f
DE
21915 struct dwarf2_cu *sig_cu;
21916 struct die_info *die;
21917
ac9ec31b
DE
21918 /* While it might be nice to assert sig_type->type == NULL here,
21919 we can get here for DW_AT_imported_declaration where we need
21920 the DIE not the type. */
348e048f
DE
21921
21922 /* If necessary, add it to the queue and load its DIEs. */
21923
95554aad 21924 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 21925 read_signatured_type (sig_type);
348e048f 21926
348e048f 21927 sig_cu = sig_type->per_cu.cu;
69d751e3 21928 gdb_assert (sig_cu != NULL);
9c541725
PA
21929 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
21930 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 21931 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 21932 to_underlying (temp_die.sect_off));
348e048f
DE
21933 if (die)
21934 {
796a7ff8
DE
21935 /* For .gdb_index version 7 keep track of included TUs.
21936 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
21937 if (dwarf2_per_objfile->index_table != NULL
21938 && dwarf2_per_objfile->index_table->version <= 7)
21939 {
21940 VEC_safe_push (dwarf2_per_cu_ptr,
21941 (*ref_cu)->per_cu->imported_symtabs,
21942 sig_cu->per_cu);
21943 }
21944
348e048f
DE
21945 *ref_cu = sig_cu;
21946 return die;
21947 }
21948
ac9ec31b
DE
21949 return NULL;
21950}
21951
21952/* Follow signatured type referenced by ATTR in SRC_DIE.
21953 On entry *REF_CU is the CU of SRC_DIE.
21954 On exit *REF_CU is the CU of the result.
21955 The result is the DIE of the type.
21956 If the referenced type cannot be found an error is thrown. */
21957
21958static struct die_info *
ff39bb5e 21959follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
21960 struct dwarf2_cu **ref_cu)
21961{
21962 ULONGEST signature = DW_SIGNATURE (attr);
21963 struct signatured_type *sig_type;
21964 struct die_info *die;
21965
21966 gdb_assert (attr->form == DW_FORM_ref_sig8);
21967
a2ce51a0 21968 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
21969 /* sig_type will be NULL if the signatured type is missing from
21970 the debug info. */
21971 if (sig_type == NULL)
21972 {
21973 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
21974 " from DIE at 0x%x [in module %s]"),
9c541725 21975 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 21976 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
21977 }
21978
21979 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
21980 if (die == NULL)
21981 {
21982 dump_die_for_error (src_die);
21983 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
21984 " from DIE at 0x%x [in module %s]"),
9c541725 21985 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 21986 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
21987 }
21988
21989 return die;
21990}
21991
21992/* Get the type specified by SIGNATURE referenced in DIE/CU,
21993 reading in and processing the type unit if necessary. */
21994
21995static struct type *
21996get_signatured_type (struct die_info *die, ULONGEST signature,
21997 struct dwarf2_cu *cu)
21998{
21999 struct signatured_type *sig_type;
22000 struct dwarf2_cu *type_cu;
22001 struct die_info *type_die;
22002 struct type *type;
22003
a2ce51a0 22004 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22005 /* sig_type will be NULL if the signatured type is missing from
22006 the debug info. */
22007 if (sig_type == NULL)
22008 {
22009 complaint (&symfile_complaints,
22010 _("Dwarf Error: Cannot find signatured DIE %s referenced"
22011 " from DIE at 0x%x [in module %s]"),
9c541725 22012 hex_string (signature), to_underlying (die->sect_off),
4262abfb 22013 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22014 return build_error_marker_type (cu, die);
22015 }
22016
22017 /* If we already know the type we're done. */
22018 if (sig_type->type != NULL)
22019 return sig_type->type;
22020
22021 type_cu = cu;
22022 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22023 if (type_die != NULL)
22024 {
22025 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22026 is created. This is important, for example, because for c++ classes
22027 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22028 type = read_type_die (type_die, type_cu);
22029 if (type == NULL)
22030 {
22031 complaint (&symfile_complaints,
22032 _("Dwarf Error: Cannot build signatured type %s"
22033 " referenced from DIE at 0x%x [in module %s]"),
9c541725 22034 hex_string (signature), to_underlying (die->sect_off),
4262abfb 22035 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22036 type = build_error_marker_type (cu, die);
22037 }
22038 }
22039 else
22040 {
22041 complaint (&symfile_complaints,
22042 _("Dwarf Error: Problem reading signatured DIE %s referenced"
22043 " from DIE at 0x%x [in module %s]"),
9c541725 22044 hex_string (signature), to_underlying (die->sect_off),
4262abfb 22045 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22046 type = build_error_marker_type (cu, die);
22047 }
22048 sig_type->type = type;
22049
22050 return type;
22051}
22052
22053/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22054 reading in and processing the type unit if necessary. */
22055
22056static struct type *
ff39bb5e 22057get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22058 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22059{
22060 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 22061 if (attr_form_is_ref (attr))
ac9ec31b
DE
22062 {
22063 struct dwarf2_cu *type_cu = cu;
22064 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22065
22066 return read_type_die (type_die, type_cu);
22067 }
22068 else if (attr->form == DW_FORM_ref_sig8)
22069 {
22070 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22071 }
22072 else
22073 {
22074 complaint (&symfile_complaints,
22075 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22076 " at 0x%x [in module %s]"),
9c541725 22077 dwarf_form_name (attr->form), to_underlying (die->sect_off),
4262abfb 22078 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22079 return build_error_marker_type (cu, die);
22080 }
348e048f
DE
22081}
22082
e5fe5e75 22083/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22084
22085static void
e5fe5e75 22086load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 22087{
52dc124a 22088 struct signatured_type *sig_type;
348e048f 22089
f4dc4d17
DE
22090 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22091 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
22092
6721b2ec
DE
22093 /* We have the per_cu, but we need the signatured_type.
22094 Fortunately this is an easy translation. */
22095 gdb_assert (per_cu->is_debug_types);
22096 sig_type = (struct signatured_type *) per_cu;
348e048f 22097
6721b2ec 22098 gdb_assert (per_cu->cu == NULL);
348e048f 22099
52dc124a 22100 read_signatured_type (sig_type);
348e048f 22101
6721b2ec 22102 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22103}
22104
dee91e82
DE
22105/* die_reader_func for read_signatured_type.
22106 This is identical to load_full_comp_unit_reader,
22107 but is kept separate for now. */
348e048f
DE
22108
22109static void
dee91e82 22110read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 22111 const gdb_byte *info_ptr,
dee91e82
DE
22112 struct die_info *comp_unit_die,
22113 int has_children,
22114 void *data)
348e048f 22115{
dee91e82 22116 struct dwarf2_cu *cu = reader->cu;
348e048f 22117
dee91e82
DE
22118 gdb_assert (cu->die_hash == NULL);
22119 cu->die_hash =
22120 htab_create_alloc_ex (cu->header.length / 12,
22121 die_hash,
22122 die_eq,
22123 NULL,
22124 &cu->comp_unit_obstack,
22125 hashtab_obstack_allocate,
22126 dummy_obstack_deallocate);
348e048f 22127
dee91e82
DE
22128 if (has_children)
22129 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
22130 &info_ptr, comp_unit_die);
22131 cu->dies = comp_unit_die;
22132 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
22133
22134 /* We try not to read any attributes in this function, because not
9cdd5dbd 22135 all CUs needed for references have been loaded yet, and symbol
348e048f 22136 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
22137 or we won't be able to build types correctly.
22138 Similarly, if we do not read the producer, we can not apply
22139 producer-specific interpretation. */
95554aad 22140 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 22141}
348e048f 22142
3019eac3
DE
22143/* Read in a signatured type and build its CU and DIEs.
22144 If the type is a stub for the real type in a DWO file,
22145 read in the real type from the DWO file as well. */
dee91e82
DE
22146
22147static void
22148read_signatured_type (struct signatured_type *sig_type)
22149{
22150 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22151
3019eac3 22152 gdb_assert (per_cu->is_debug_types);
dee91e82 22153 gdb_assert (per_cu->cu == NULL);
348e048f 22154
f4dc4d17
DE
22155 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
22156 read_signatured_type_reader, NULL);
7ee85ab1 22157 sig_type->per_cu.tu_read = 1;
c906108c
SS
22158}
22159
c906108c
SS
22160/* Decode simple location descriptions.
22161 Given a pointer to a dwarf block that defines a location, compute
22162 the location and return the value.
22163
4cecd739
DJ
22164 NOTE drow/2003-11-18: This function is called in two situations
22165 now: for the address of static or global variables (partial symbols
22166 only) and for offsets into structures which are expected to be
22167 (more or less) constant. The partial symbol case should go away,
22168 and only the constant case should remain. That will let this
22169 function complain more accurately. A few special modes are allowed
22170 without complaint for global variables (for instance, global
22171 register values and thread-local values).
c906108c
SS
22172
22173 A location description containing no operations indicates that the
4cecd739 22174 object is optimized out. The return value is 0 for that case.
6b992462
DJ
22175 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22176 callers will only want a very basic result and this can become a
21ae7a4d
JK
22177 complaint.
22178
22179 Note that stack[0] is unused except as a default error return. */
c906108c
SS
22180
22181static CORE_ADDR
e7c27a73 22182decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 22183{
e7c27a73 22184 struct objfile *objfile = cu->objfile;
56eb65bd
SP
22185 size_t i;
22186 size_t size = blk->size;
d521ce57 22187 const gdb_byte *data = blk->data;
21ae7a4d
JK
22188 CORE_ADDR stack[64];
22189 int stacki;
22190 unsigned int bytes_read, unsnd;
22191 gdb_byte op;
c906108c 22192
21ae7a4d
JK
22193 i = 0;
22194 stacki = 0;
22195 stack[stacki] = 0;
22196 stack[++stacki] = 0;
22197
22198 while (i < size)
22199 {
22200 op = data[i++];
22201 switch (op)
22202 {
22203 case DW_OP_lit0:
22204 case DW_OP_lit1:
22205 case DW_OP_lit2:
22206 case DW_OP_lit3:
22207 case DW_OP_lit4:
22208 case DW_OP_lit5:
22209 case DW_OP_lit6:
22210 case DW_OP_lit7:
22211 case DW_OP_lit8:
22212 case DW_OP_lit9:
22213 case DW_OP_lit10:
22214 case DW_OP_lit11:
22215 case DW_OP_lit12:
22216 case DW_OP_lit13:
22217 case DW_OP_lit14:
22218 case DW_OP_lit15:
22219 case DW_OP_lit16:
22220 case DW_OP_lit17:
22221 case DW_OP_lit18:
22222 case DW_OP_lit19:
22223 case DW_OP_lit20:
22224 case DW_OP_lit21:
22225 case DW_OP_lit22:
22226 case DW_OP_lit23:
22227 case DW_OP_lit24:
22228 case DW_OP_lit25:
22229 case DW_OP_lit26:
22230 case DW_OP_lit27:
22231 case DW_OP_lit28:
22232 case DW_OP_lit29:
22233 case DW_OP_lit30:
22234 case DW_OP_lit31:
22235 stack[++stacki] = op - DW_OP_lit0;
22236 break;
f1bea926 22237
21ae7a4d
JK
22238 case DW_OP_reg0:
22239 case DW_OP_reg1:
22240 case DW_OP_reg2:
22241 case DW_OP_reg3:
22242 case DW_OP_reg4:
22243 case DW_OP_reg5:
22244 case DW_OP_reg6:
22245 case DW_OP_reg7:
22246 case DW_OP_reg8:
22247 case DW_OP_reg9:
22248 case DW_OP_reg10:
22249 case DW_OP_reg11:
22250 case DW_OP_reg12:
22251 case DW_OP_reg13:
22252 case DW_OP_reg14:
22253 case DW_OP_reg15:
22254 case DW_OP_reg16:
22255 case DW_OP_reg17:
22256 case DW_OP_reg18:
22257 case DW_OP_reg19:
22258 case DW_OP_reg20:
22259 case DW_OP_reg21:
22260 case DW_OP_reg22:
22261 case DW_OP_reg23:
22262 case DW_OP_reg24:
22263 case DW_OP_reg25:
22264 case DW_OP_reg26:
22265 case DW_OP_reg27:
22266 case DW_OP_reg28:
22267 case DW_OP_reg29:
22268 case DW_OP_reg30:
22269 case DW_OP_reg31:
22270 stack[++stacki] = op - DW_OP_reg0;
22271 if (i < size)
22272 dwarf2_complex_location_expr_complaint ();
22273 break;
c906108c 22274
21ae7a4d
JK
22275 case DW_OP_regx:
22276 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22277 i += bytes_read;
22278 stack[++stacki] = unsnd;
22279 if (i < size)
22280 dwarf2_complex_location_expr_complaint ();
22281 break;
c906108c 22282
21ae7a4d
JK
22283 case DW_OP_addr:
22284 stack[++stacki] = read_address (objfile->obfd, &data[i],
22285 cu, &bytes_read);
22286 i += bytes_read;
22287 break;
d53d4ac5 22288
21ae7a4d
JK
22289 case DW_OP_const1u:
22290 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22291 i += 1;
22292 break;
22293
22294 case DW_OP_const1s:
22295 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22296 i += 1;
22297 break;
22298
22299 case DW_OP_const2u:
22300 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22301 i += 2;
22302 break;
22303
22304 case DW_OP_const2s:
22305 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22306 i += 2;
22307 break;
d53d4ac5 22308
21ae7a4d
JK
22309 case DW_OP_const4u:
22310 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22311 i += 4;
22312 break;
22313
22314 case DW_OP_const4s:
22315 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22316 i += 4;
22317 break;
22318
585861ea
JK
22319 case DW_OP_const8u:
22320 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22321 i += 8;
22322 break;
22323
21ae7a4d
JK
22324 case DW_OP_constu:
22325 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22326 &bytes_read);
22327 i += bytes_read;
22328 break;
22329
22330 case DW_OP_consts:
22331 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22332 i += bytes_read;
22333 break;
22334
22335 case DW_OP_dup:
22336 stack[stacki + 1] = stack[stacki];
22337 stacki++;
22338 break;
22339
22340 case DW_OP_plus:
22341 stack[stacki - 1] += stack[stacki];
22342 stacki--;
22343 break;
22344
22345 case DW_OP_plus_uconst:
22346 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22347 &bytes_read);
22348 i += bytes_read;
22349 break;
22350
22351 case DW_OP_minus:
22352 stack[stacki - 1] -= stack[stacki];
22353 stacki--;
22354 break;
22355
22356 case DW_OP_deref:
22357 /* If we're not the last op, then we definitely can't encode
22358 this using GDB's address_class enum. This is valid for partial
22359 global symbols, although the variable's address will be bogus
22360 in the psymtab. */
22361 if (i < size)
22362 dwarf2_complex_location_expr_complaint ();
22363 break;
22364
22365 case DW_OP_GNU_push_tls_address:
4aa4e28b 22366 case DW_OP_form_tls_address:
21ae7a4d
JK
22367 /* The top of the stack has the offset from the beginning
22368 of the thread control block at which the variable is located. */
22369 /* Nothing should follow this operator, so the top of stack would
22370 be returned. */
22371 /* This is valid for partial global symbols, but the variable's
585861ea
JK
22372 address will be bogus in the psymtab. Make it always at least
22373 non-zero to not look as a variable garbage collected by linker
22374 which have DW_OP_addr 0. */
21ae7a4d
JK
22375 if (i < size)
22376 dwarf2_complex_location_expr_complaint ();
585861ea 22377 stack[stacki]++;
21ae7a4d
JK
22378 break;
22379
22380 case DW_OP_GNU_uninit:
22381 break;
22382
3019eac3 22383 case DW_OP_GNU_addr_index:
49f6c839 22384 case DW_OP_GNU_const_index:
3019eac3
DE
22385 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22386 &bytes_read);
22387 i += bytes_read;
22388 break;
22389
21ae7a4d
JK
22390 default:
22391 {
f39c6ffd 22392 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
22393
22394 if (name)
22395 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
22396 name);
22397 else
22398 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
22399 op);
22400 }
22401
22402 return (stack[stacki]);
d53d4ac5 22403 }
3c6e0cb3 22404
21ae7a4d
JK
22405 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22406 outside of the allocated space. Also enforce minimum>0. */
22407 if (stacki >= ARRAY_SIZE (stack) - 1)
22408 {
22409 complaint (&symfile_complaints,
22410 _("location description stack overflow"));
22411 return 0;
22412 }
22413
22414 if (stacki <= 0)
22415 {
22416 complaint (&symfile_complaints,
22417 _("location description stack underflow"));
22418 return 0;
22419 }
22420 }
22421 return (stack[stacki]);
c906108c
SS
22422}
22423
22424/* memory allocation interface */
22425
c906108c 22426static struct dwarf_block *
7b5a2f43 22427dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 22428{
8d749320 22429 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
22430}
22431
c906108c 22432static struct die_info *
b60c80d6 22433dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
22434{
22435 struct die_info *die;
b60c80d6
DJ
22436 size_t size = sizeof (struct die_info);
22437
22438 if (num_attrs > 1)
22439 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 22440
b60c80d6 22441 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
22442 memset (die, 0, sizeof (struct die_info));
22443 return (die);
22444}
2e276125
JB
22445
22446\f
22447/* Macro support. */
22448
233d95b5
JK
22449/* Return file name relative to the compilation directory of file number I in
22450 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 22451 responsible for freeing it. */
233d95b5 22452
2e276125 22453static char *
233d95b5 22454file_file_name (int file, struct line_header *lh)
2e276125 22455{
6a83a1e6
EZ
22456 /* Is the file number a valid index into the line header's file name
22457 table? Remember that file numbers start with one, not zero. */
fff8551c 22458 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 22459 {
8c43009f 22460 const file_entry &fe = lh->file_names[file - 1];
6e70227d 22461
8c43009f
PA
22462 if (!IS_ABSOLUTE_PATH (fe.name))
22463 {
22464 const char *dir = fe.include_dir (lh);
22465 if (dir != NULL)
22466 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
22467 }
22468 return xstrdup (fe.name);
6a83a1e6 22469 }
2e276125
JB
22470 else
22471 {
6a83a1e6
EZ
22472 /* The compiler produced a bogus file number. We can at least
22473 record the macro definitions made in the file, even if we
22474 won't be able to find the file by name. */
22475 char fake_name[80];
9a619af0 22476
8c042590
PM
22477 xsnprintf (fake_name, sizeof (fake_name),
22478 "<bad macro file number %d>", file);
2e276125 22479
6e70227d 22480 complaint (&symfile_complaints,
6a83a1e6
EZ
22481 _("bad file number in macro information (%d)"),
22482 file);
2e276125 22483
6a83a1e6 22484 return xstrdup (fake_name);
2e276125
JB
22485 }
22486}
22487
233d95b5
JK
22488/* Return the full name of file number I in *LH's file name table.
22489 Use COMP_DIR as the name of the current directory of the
22490 compilation. The result is allocated using xmalloc; the caller is
22491 responsible for freeing it. */
22492static char *
22493file_full_name (int file, struct line_header *lh, const char *comp_dir)
22494{
22495 /* Is the file number a valid index into the line header's file name
22496 table? Remember that file numbers start with one, not zero. */
fff8551c 22497 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
22498 {
22499 char *relative = file_file_name (file, lh);
22500
22501 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
22502 return relative;
b36cec19
PA
22503 return reconcat (relative, comp_dir, SLASH_STRING,
22504 relative, (char *) NULL);
233d95b5
JK
22505 }
22506 else
22507 return file_file_name (file, lh);
22508}
22509
2e276125
JB
22510
22511static struct macro_source_file *
22512macro_start_file (int file, int line,
22513 struct macro_source_file *current_file,
43f3e411 22514 struct line_header *lh)
2e276125 22515{
233d95b5
JK
22516 /* File name relative to the compilation directory of this source file. */
22517 char *file_name = file_file_name (file, lh);
2e276125 22518
2e276125 22519 if (! current_file)
abc9d0dc 22520 {
fc474241
DE
22521 /* Note: We don't create a macro table for this compilation unit
22522 at all until we actually get a filename. */
43f3e411 22523 struct macro_table *macro_table = get_macro_table ();
fc474241 22524
abc9d0dc
TT
22525 /* If we have no current file, then this must be the start_file
22526 directive for the compilation unit's main source file. */
fc474241
DE
22527 current_file = macro_set_main (macro_table, file_name);
22528 macro_define_special (macro_table);
abc9d0dc 22529 }
2e276125 22530 else
233d95b5 22531 current_file = macro_include (current_file, line, file_name);
2e276125 22532
233d95b5 22533 xfree (file_name);
6e70227d 22534
2e276125
JB
22535 return current_file;
22536}
22537
2e276125
JB
22538static const char *
22539consume_improper_spaces (const char *p, const char *body)
22540{
22541 if (*p == ' ')
22542 {
4d3c2250 22543 complaint (&symfile_complaints,
3e43a32a
MS
22544 _("macro definition contains spaces "
22545 "in formal argument list:\n`%s'"),
4d3c2250 22546 body);
2e276125
JB
22547
22548 while (*p == ' ')
22549 p++;
22550 }
22551
22552 return p;
22553}
22554
22555
22556static void
22557parse_macro_definition (struct macro_source_file *file, int line,
22558 const char *body)
22559{
22560 const char *p;
22561
22562 /* The body string takes one of two forms. For object-like macro
22563 definitions, it should be:
22564
22565 <macro name> " " <definition>
22566
22567 For function-like macro definitions, it should be:
22568
22569 <macro name> "() " <definition>
22570 or
22571 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
22572
22573 Spaces may appear only where explicitly indicated, and in the
22574 <definition>.
22575
22576 The Dwarf 2 spec says that an object-like macro's name is always
22577 followed by a space, but versions of GCC around March 2002 omit
6e70227d 22578 the space when the macro's definition is the empty string.
2e276125
JB
22579
22580 The Dwarf 2 spec says that there should be no spaces between the
22581 formal arguments in a function-like macro's formal argument list,
22582 but versions of GCC around March 2002 include spaces after the
22583 commas. */
22584
22585
22586 /* Find the extent of the macro name. The macro name is terminated
22587 by either a space or null character (for an object-like macro) or
22588 an opening paren (for a function-like macro). */
22589 for (p = body; *p; p++)
22590 if (*p == ' ' || *p == '(')
22591 break;
22592
22593 if (*p == ' ' || *p == '\0')
22594 {
22595 /* It's an object-like macro. */
22596 int name_len = p - body;
3f8a7804 22597 char *name = savestring (body, name_len);
2e276125
JB
22598 const char *replacement;
22599
22600 if (*p == ' ')
22601 replacement = body + name_len + 1;
22602 else
22603 {
4d3c2250 22604 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22605 replacement = body + name_len;
22606 }
6e70227d 22607
2e276125
JB
22608 macro_define_object (file, line, name, replacement);
22609
22610 xfree (name);
22611 }
22612 else if (*p == '(')
22613 {
22614 /* It's a function-like macro. */
3f8a7804 22615 char *name = savestring (body, p - body);
2e276125
JB
22616 int argc = 0;
22617 int argv_size = 1;
8d749320 22618 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
22619
22620 p++;
22621
22622 p = consume_improper_spaces (p, body);
22623
22624 /* Parse the formal argument list. */
22625 while (*p && *p != ')')
22626 {
22627 /* Find the extent of the current argument name. */
22628 const char *arg_start = p;
22629
22630 while (*p && *p != ',' && *p != ')' && *p != ' ')
22631 p++;
22632
22633 if (! *p || p == arg_start)
4d3c2250 22634 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22635 else
22636 {
22637 /* Make sure argv has room for the new argument. */
22638 if (argc >= argv_size)
22639 {
22640 argv_size *= 2;
224c3ddb 22641 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
22642 }
22643
3f8a7804 22644 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
22645 }
22646
22647 p = consume_improper_spaces (p, body);
22648
22649 /* Consume the comma, if present. */
22650 if (*p == ',')
22651 {
22652 p++;
22653
22654 p = consume_improper_spaces (p, body);
22655 }
22656 }
22657
22658 if (*p == ')')
22659 {
22660 p++;
22661
22662 if (*p == ' ')
22663 /* Perfectly formed definition, no complaints. */
22664 macro_define_function (file, line, name,
6e70227d 22665 argc, (const char **) argv,
2e276125
JB
22666 p + 1);
22667 else if (*p == '\0')
22668 {
22669 /* Complain, but do define it. */
4d3c2250 22670 dwarf2_macro_malformed_definition_complaint (body);
2e276125 22671 macro_define_function (file, line, name,
6e70227d 22672 argc, (const char **) argv,
2e276125
JB
22673 p);
22674 }
22675 else
22676 /* Just complain. */
4d3c2250 22677 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22678 }
22679 else
22680 /* Just complain. */
4d3c2250 22681 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22682
22683 xfree (name);
22684 {
22685 int i;
22686
22687 for (i = 0; i < argc; i++)
22688 xfree (argv[i]);
22689 }
22690 xfree (argv);
22691 }
22692 else
4d3c2250 22693 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22694}
22695
cf2c3c16
TT
22696/* Skip some bytes from BYTES according to the form given in FORM.
22697 Returns the new pointer. */
2e276125 22698
d521ce57
TT
22699static const gdb_byte *
22700skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
22701 enum dwarf_form form,
22702 unsigned int offset_size,
22703 struct dwarf2_section_info *section)
2e276125 22704{
cf2c3c16 22705 unsigned int bytes_read;
2e276125 22706
cf2c3c16 22707 switch (form)
2e276125 22708 {
cf2c3c16
TT
22709 case DW_FORM_data1:
22710 case DW_FORM_flag:
22711 ++bytes;
22712 break;
22713
22714 case DW_FORM_data2:
22715 bytes += 2;
22716 break;
22717
22718 case DW_FORM_data4:
22719 bytes += 4;
22720 break;
22721
22722 case DW_FORM_data8:
22723 bytes += 8;
22724 break;
22725
0224619f
JK
22726 case DW_FORM_data16:
22727 bytes += 16;
22728 break;
22729
cf2c3c16
TT
22730 case DW_FORM_string:
22731 read_direct_string (abfd, bytes, &bytes_read);
22732 bytes += bytes_read;
22733 break;
22734
22735 case DW_FORM_sec_offset:
22736 case DW_FORM_strp:
36586728 22737 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
22738 bytes += offset_size;
22739 break;
22740
22741 case DW_FORM_block:
22742 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
22743 bytes += bytes_read;
22744 break;
22745
22746 case DW_FORM_block1:
22747 bytes += 1 + read_1_byte (abfd, bytes);
22748 break;
22749 case DW_FORM_block2:
22750 bytes += 2 + read_2_bytes (abfd, bytes);
22751 break;
22752 case DW_FORM_block4:
22753 bytes += 4 + read_4_bytes (abfd, bytes);
22754 break;
22755
22756 case DW_FORM_sdata:
22757 case DW_FORM_udata:
3019eac3
DE
22758 case DW_FORM_GNU_addr_index:
22759 case DW_FORM_GNU_str_index:
d521ce57 22760 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
22761 if (bytes == NULL)
22762 {
22763 dwarf2_section_buffer_overflow_complaint (section);
22764 return NULL;
22765 }
cf2c3c16
TT
22766 break;
22767
663c44ac
JK
22768 case DW_FORM_implicit_const:
22769 break;
22770
cf2c3c16
TT
22771 default:
22772 {
cf2c3c16
TT
22773 complaint (&symfile_complaints,
22774 _("invalid form 0x%x in `%s'"),
a32a8923 22775 form, get_section_name (section));
cf2c3c16
TT
22776 return NULL;
22777 }
2e276125
JB
22778 }
22779
cf2c3c16
TT
22780 return bytes;
22781}
757a13d0 22782
cf2c3c16
TT
22783/* A helper for dwarf_decode_macros that handles skipping an unknown
22784 opcode. Returns an updated pointer to the macro data buffer; or,
22785 on error, issues a complaint and returns NULL. */
757a13d0 22786
d521ce57 22787static const gdb_byte *
cf2c3c16 22788skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
22789 const gdb_byte **opcode_definitions,
22790 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
22791 bfd *abfd,
22792 unsigned int offset_size,
22793 struct dwarf2_section_info *section)
22794{
22795 unsigned int bytes_read, i;
22796 unsigned long arg;
d521ce57 22797 const gdb_byte *defn;
2e276125 22798
cf2c3c16 22799 if (opcode_definitions[opcode] == NULL)
2e276125 22800 {
cf2c3c16
TT
22801 complaint (&symfile_complaints,
22802 _("unrecognized DW_MACFINO opcode 0x%x"),
22803 opcode);
22804 return NULL;
22805 }
2e276125 22806
cf2c3c16
TT
22807 defn = opcode_definitions[opcode];
22808 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
22809 defn += bytes_read;
2e276125 22810
cf2c3c16
TT
22811 for (i = 0; i < arg; ++i)
22812 {
aead7601
SM
22813 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
22814 (enum dwarf_form) defn[i], offset_size,
f664829e 22815 section);
cf2c3c16
TT
22816 if (mac_ptr == NULL)
22817 {
22818 /* skip_form_bytes already issued the complaint. */
22819 return NULL;
22820 }
22821 }
757a13d0 22822
cf2c3c16
TT
22823 return mac_ptr;
22824}
757a13d0 22825
cf2c3c16
TT
22826/* A helper function which parses the header of a macro section.
22827 If the macro section is the extended (for now called "GNU") type,
22828 then this updates *OFFSET_SIZE. Returns a pointer to just after
22829 the header, or issues a complaint and returns NULL on error. */
757a13d0 22830
d521ce57
TT
22831static const gdb_byte *
22832dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 22833 bfd *abfd,
d521ce57 22834 const gdb_byte *mac_ptr,
cf2c3c16
TT
22835 unsigned int *offset_size,
22836 int section_is_gnu)
22837{
22838 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 22839
cf2c3c16
TT
22840 if (section_is_gnu)
22841 {
22842 unsigned int version, flags;
757a13d0 22843
cf2c3c16 22844 version = read_2_bytes (abfd, mac_ptr);
0af92d60 22845 if (version != 4 && version != 5)
cf2c3c16
TT
22846 {
22847 complaint (&symfile_complaints,
22848 _("unrecognized version `%d' in .debug_macro section"),
22849 version);
22850 return NULL;
22851 }
22852 mac_ptr += 2;
757a13d0 22853
cf2c3c16
TT
22854 flags = read_1_byte (abfd, mac_ptr);
22855 ++mac_ptr;
22856 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 22857
cf2c3c16
TT
22858 if ((flags & 2) != 0)
22859 /* We don't need the line table offset. */
22860 mac_ptr += *offset_size;
757a13d0 22861
cf2c3c16
TT
22862 /* Vendor opcode descriptions. */
22863 if ((flags & 4) != 0)
22864 {
22865 unsigned int i, count;
757a13d0 22866
cf2c3c16
TT
22867 count = read_1_byte (abfd, mac_ptr);
22868 ++mac_ptr;
22869 for (i = 0; i < count; ++i)
22870 {
22871 unsigned int opcode, bytes_read;
22872 unsigned long arg;
22873
22874 opcode = read_1_byte (abfd, mac_ptr);
22875 ++mac_ptr;
22876 opcode_definitions[opcode] = mac_ptr;
22877 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22878 mac_ptr += bytes_read;
22879 mac_ptr += arg;
22880 }
757a13d0 22881 }
cf2c3c16 22882 }
757a13d0 22883
cf2c3c16
TT
22884 return mac_ptr;
22885}
757a13d0 22886
cf2c3c16 22887/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 22888 including DW_MACRO_import. */
cf2c3c16
TT
22889
22890static void
d521ce57
TT
22891dwarf_decode_macro_bytes (bfd *abfd,
22892 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 22893 struct macro_source_file *current_file,
43f3e411 22894 struct line_header *lh,
cf2c3c16 22895 struct dwarf2_section_info *section,
36586728 22896 int section_is_gnu, int section_is_dwz,
cf2c3c16 22897 unsigned int offset_size,
8fc3fc34 22898 htab_t include_hash)
cf2c3c16 22899{
4d663531 22900 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
22901 enum dwarf_macro_record_type macinfo_type;
22902 int at_commandline;
d521ce57 22903 const gdb_byte *opcode_definitions[256];
757a13d0 22904
cf2c3c16
TT
22905 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22906 &offset_size, section_is_gnu);
22907 if (mac_ptr == NULL)
22908 {
22909 /* We already issued a complaint. */
22910 return;
22911 }
757a13d0
JK
22912
22913 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
22914 GDB is still reading the definitions from command line. First
22915 DW_MACINFO_start_file will need to be ignored as it was already executed
22916 to create CURRENT_FILE for the main source holding also the command line
22917 definitions. On first met DW_MACINFO_start_file this flag is reset to
22918 normally execute all the remaining DW_MACINFO_start_file macinfos. */
22919
22920 at_commandline = 1;
22921
22922 do
22923 {
22924 /* Do we at least have room for a macinfo type byte? */
22925 if (mac_ptr >= mac_end)
22926 {
f664829e 22927 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
22928 break;
22929 }
22930
aead7601 22931 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
22932 mac_ptr++;
22933
cf2c3c16
TT
22934 /* Note that we rely on the fact that the corresponding GNU and
22935 DWARF constants are the same. */
757a13d0
JK
22936 switch (macinfo_type)
22937 {
22938 /* A zero macinfo type indicates the end of the macro
22939 information. */
22940 case 0:
22941 break;
2e276125 22942
0af92d60
JK
22943 case DW_MACRO_define:
22944 case DW_MACRO_undef:
22945 case DW_MACRO_define_strp:
22946 case DW_MACRO_undef_strp:
22947 case DW_MACRO_define_sup:
22948 case DW_MACRO_undef_sup:
2e276125 22949 {
891d2f0b 22950 unsigned int bytes_read;
2e276125 22951 int line;
d521ce57 22952 const char *body;
cf2c3c16 22953 int is_define;
2e276125 22954
cf2c3c16
TT
22955 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22956 mac_ptr += bytes_read;
22957
0af92d60
JK
22958 if (macinfo_type == DW_MACRO_define
22959 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
22960 {
22961 body = read_direct_string (abfd, mac_ptr, &bytes_read);
22962 mac_ptr += bytes_read;
22963 }
22964 else
22965 {
22966 LONGEST str_offset;
22967
22968 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
22969 mac_ptr += offset_size;
2e276125 22970
0af92d60
JK
22971 if (macinfo_type == DW_MACRO_define_sup
22972 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 22973 || section_is_dwz)
36586728
TT
22974 {
22975 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22976
22977 body = read_indirect_string_from_dwz (dwz, str_offset);
22978 }
22979 else
22980 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
22981 }
22982
0af92d60
JK
22983 is_define = (macinfo_type == DW_MACRO_define
22984 || macinfo_type == DW_MACRO_define_strp
22985 || macinfo_type == DW_MACRO_define_sup);
2e276125 22986 if (! current_file)
757a13d0
JK
22987 {
22988 /* DWARF violation as no main source is present. */
22989 complaint (&symfile_complaints,
22990 _("debug info with no main source gives macro %s "
22991 "on line %d: %s"),
cf2c3c16
TT
22992 is_define ? _("definition") : _("undefinition"),
22993 line, body);
757a13d0
JK
22994 break;
22995 }
3e43a32a
MS
22996 if ((line == 0 && !at_commandline)
22997 || (line != 0 && at_commandline))
4d3c2250 22998 complaint (&symfile_complaints,
757a13d0
JK
22999 _("debug info gives %s macro %s with %s line %d: %s"),
23000 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 23001 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
23002 line == 0 ? _("zero") : _("non-zero"), line, body);
23003
cf2c3c16 23004 if (is_define)
757a13d0 23005 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
23006 else
23007 {
0af92d60
JK
23008 gdb_assert (macinfo_type == DW_MACRO_undef
23009 || macinfo_type == DW_MACRO_undef_strp
23010 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
23011 macro_undef (current_file, line, body);
23012 }
2e276125
JB
23013 }
23014 break;
23015
0af92d60 23016 case DW_MACRO_start_file:
2e276125 23017 {
891d2f0b 23018 unsigned int bytes_read;
2e276125
JB
23019 int line, file;
23020
23021 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23022 mac_ptr += bytes_read;
23023 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23024 mac_ptr += bytes_read;
23025
3e43a32a
MS
23026 if ((line == 0 && !at_commandline)
23027 || (line != 0 && at_commandline))
757a13d0
JK
23028 complaint (&symfile_complaints,
23029 _("debug info gives source %d included "
23030 "from %s at %s line %d"),
23031 file, at_commandline ? _("command-line") : _("file"),
23032 line == 0 ? _("zero") : _("non-zero"), line);
23033
23034 if (at_commandline)
23035 {
0af92d60 23036 /* This DW_MACRO_start_file was executed in the
cf2c3c16 23037 pass one. */
757a13d0
JK
23038 at_commandline = 0;
23039 }
23040 else
43f3e411 23041 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
23042 }
23043 break;
23044
0af92d60 23045 case DW_MACRO_end_file:
2e276125 23046 if (! current_file)
4d3c2250 23047 complaint (&symfile_complaints,
3e43a32a
MS
23048 _("macro debug info has an unmatched "
23049 "`close_file' directive"));
2e276125
JB
23050 else
23051 {
23052 current_file = current_file->included_by;
23053 if (! current_file)
23054 {
cf2c3c16 23055 enum dwarf_macro_record_type next_type;
2e276125
JB
23056
23057 /* GCC circa March 2002 doesn't produce the zero
23058 type byte marking the end of the compilation
23059 unit. Complain if it's not there, but exit no
23060 matter what. */
23061
23062 /* Do we at least have room for a macinfo type byte? */
23063 if (mac_ptr >= mac_end)
23064 {
f664829e 23065 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
23066 return;
23067 }
23068
23069 /* We don't increment mac_ptr here, so this is just
23070 a look-ahead. */
aead7601
SM
23071 next_type
23072 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23073 mac_ptr);
2e276125 23074 if (next_type != 0)
4d3c2250 23075 complaint (&symfile_complaints,
3e43a32a
MS
23076 _("no terminating 0-type entry for "
23077 "macros in `.debug_macinfo' section"));
2e276125
JB
23078
23079 return;
23080 }
23081 }
23082 break;
23083
0af92d60
JK
23084 case DW_MACRO_import:
23085 case DW_MACRO_import_sup:
cf2c3c16
TT
23086 {
23087 LONGEST offset;
8fc3fc34 23088 void **slot;
a036ba48
TT
23089 bfd *include_bfd = abfd;
23090 struct dwarf2_section_info *include_section = section;
d521ce57 23091 const gdb_byte *include_mac_end = mac_end;
a036ba48 23092 int is_dwz = section_is_dwz;
d521ce57 23093 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
23094
23095 offset = read_offset_1 (abfd, mac_ptr, offset_size);
23096 mac_ptr += offset_size;
23097
0af92d60 23098 if (macinfo_type == DW_MACRO_import_sup)
a036ba48
TT
23099 {
23100 struct dwz_file *dwz = dwarf2_get_dwz_file ();
23101
4d663531 23102 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 23103
a036ba48 23104 include_section = &dwz->macro;
a32a8923 23105 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
23106 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23107 is_dwz = 1;
23108 }
23109
23110 new_mac_ptr = include_section->buffer + offset;
23111 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23112
8fc3fc34
TT
23113 if (*slot != NULL)
23114 {
23115 /* This has actually happened; see
23116 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
23117 complaint (&symfile_complaints,
0af92d60 23118 _("recursive DW_MACRO_import in "
8fc3fc34
TT
23119 ".debug_macro section"));
23120 }
23121 else
23122 {
d521ce57 23123 *slot = (void *) new_mac_ptr;
36586728 23124
a036ba48 23125 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 23126 include_mac_end, current_file, lh,
36586728 23127 section, section_is_gnu, is_dwz,
4d663531 23128 offset_size, include_hash);
8fc3fc34 23129
d521ce57 23130 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 23131 }
cf2c3c16
TT
23132 }
23133 break;
23134
2e276125 23135 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
23136 if (!section_is_gnu)
23137 {
23138 unsigned int bytes_read;
2e276125 23139
ac298888
TT
23140 /* This reads the constant, but since we don't recognize
23141 any vendor extensions, we ignore it. */
23142 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
23143 mac_ptr += bytes_read;
23144 read_direct_string (abfd, mac_ptr, &bytes_read);
23145 mac_ptr += bytes_read;
2e276125 23146
cf2c3c16
TT
23147 /* We don't recognize any vendor extensions. */
23148 break;
23149 }
23150 /* FALLTHROUGH */
23151
23152 default:
23153 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 23154 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
23155 section);
23156 if (mac_ptr == NULL)
23157 return;
23158 break;
2e276125 23159 }
757a13d0 23160 } while (macinfo_type != 0);
2e276125 23161}
8e19ed76 23162
cf2c3c16 23163static void
09262596 23164dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 23165 int section_is_gnu)
cf2c3c16 23166{
bb5ed363 23167 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
23168 struct line_header *lh = cu->line_header;
23169 bfd *abfd;
d521ce57 23170 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
23171 struct macro_source_file *current_file = 0;
23172 enum dwarf_macro_record_type macinfo_type;
23173 unsigned int offset_size = cu->header.offset_size;
d521ce57 23174 const gdb_byte *opcode_definitions[256];
8fc3fc34 23175 void **slot;
09262596
DE
23176 struct dwarf2_section_info *section;
23177 const char *section_name;
23178
23179 if (cu->dwo_unit != NULL)
23180 {
23181 if (section_is_gnu)
23182 {
23183 section = &cu->dwo_unit->dwo_file->sections.macro;
23184 section_name = ".debug_macro.dwo";
23185 }
23186 else
23187 {
23188 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23189 section_name = ".debug_macinfo.dwo";
23190 }
23191 }
23192 else
23193 {
23194 if (section_is_gnu)
23195 {
23196 section = &dwarf2_per_objfile->macro;
23197 section_name = ".debug_macro";
23198 }
23199 else
23200 {
23201 section = &dwarf2_per_objfile->macinfo;
23202 section_name = ".debug_macinfo";
23203 }
23204 }
cf2c3c16 23205
bb5ed363 23206 dwarf2_read_section (objfile, section);
cf2c3c16
TT
23207 if (section->buffer == NULL)
23208 {
fceca515 23209 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
23210 return;
23211 }
a32a8923 23212 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
23213
23214 /* First pass: Find the name of the base filename.
23215 This filename is needed in order to process all macros whose definition
23216 (or undefinition) comes from the command line. These macros are defined
23217 before the first DW_MACINFO_start_file entry, and yet still need to be
23218 associated to the base file.
23219
23220 To determine the base file name, we scan the macro definitions until we
23221 reach the first DW_MACINFO_start_file entry. We then initialize
23222 CURRENT_FILE accordingly so that any macro definition found before the
23223 first DW_MACINFO_start_file can still be associated to the base file. */
23224
23225 mac_ptr = section->buffer + offset;
23226 mac_end = section->buffer + section->size;
23227
23228 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23229 &offset_size, section_is_gnu);
23230 if (mac_ptr == NULL)
23231 {
23232 /* We already issued a complaint. */
23233 return;
23234 }
23235
23236 do
23237 {
23238 /* Do we at least have room for a macinfo type byte? */
23239 if (mac_ptr >= mac_end)
23240 {
23241 /* Complaint is printed during the second pass as GDB will probably
23242 stop the first pass earlier upon finding
23243 DW_MACINFO_start_file. */
23244 break;
23245 }
23246
aead7601 23247 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
23248 mac_ptr++;
23249
23250 /* Note that we rely on the fact that the corresponding GNU and
23251 DWARF constants are the same. */
23252 switch (macinfo_type)
23253 {
23254 /* A zero macinfo type indicates the end of the macro
23255 information. */
23256 case 0:
23257 break;
23258
0af92d60
JK
23259 case DW_MACRO_define:
23260 case DW_MACRO_undef:
cf2c3c16
TT
23261 /* Only skip the data by MAC_PTR. */
23262 {
23263 unsigned int bytes_read;
23264
23265 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23266 mac_ptr += bytes_read;
23267 read_direct_string (abfd, mac_ptr, &bytes_read);
23268 mac_ptr += bytes_read;
23269 }
23270 break;
23271
0af92d60 23272 case DW_MACRO_start_file:
cf2c3c16
TT
23273 {
23274 unsigned int bytes_read;
23275 int line, file;
23276
23277 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23278 mac_ptr += bytes_read;
23279 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23280 mac_ptr += bytes_read;
23281
43f3e411 23282 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
23283 }
23284 break;
23285
0af92d60 23286 case DW_MACRO_end_file:
cf2c3c16
TT
23287 /* No data to skip by MAC_PTR. */
23288 break;
23289
0af92d60
JK
23290 case DW_MACRO_define_strp:
23291 case DW_MACRO_undef_strp:
23292 case DW_MACRO_define_sup:
23293 case DW_MACRO_undef_sup:
cf2c3c16
TT
23294 {
23295 unsigned int bytes_read;
23296
23297 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23298 mac_ptr += bytes_read;
23299 mac_ptr += offset_size;
23300 }
23301 break;
23302
0af92d60
JK
23303 case DW_MACRO_import:
23304 case DW_MACRO_import_sup:
cf2c3c16 23305 /* Note that, according to the spec, a transparent include
0af92d60 23306 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
23307 skip this opcode. */
23308 mac_ptr += offset_size;
23309 break;
23310
23311 case DW_MACINFO_vendor_ext:
23312 /* Only skip the data by MAC_PTR. */
23313 if (!section_is_gnu)
23314 {
23315 unsigned int bytes_read;
23316
23317 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23318 mac_ptr += bytes_read;
23319 read_direct_string (abfd, mac_ptr, &bytes_read);
23320 mac_ptr += bytes_read;
23321 }
23322 /* FALLTHROUGH */
23323
23324 default:
23325 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 23326 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
23327 section);
23328 if (mac_ptr == NULL)
23329 return;
23330 break;
23331 }
23332 } while (macinfo_type != 0 && current_file == NULL);
23333
23334 /* Second pass: Process all entries.
23335
23336 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23337 command-line macro definitions/undefinitions. This flag is unset when we
23338 reach the first DW_MACINFO_start_file entry. */
23339
fc4007c9
TT
23340 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23341 htab_eq_pointer,
23342 NULL, xcalloc, xfree));
8fc3fc34 23343 mac_ptr = section->buffer + offset;
fc4007c9 23344 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 23345 *slot = (void *) mac_ptr;
8fc3fc34 23346 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 23347 current_file, lh, section,
fc4007c9
TT
23348 section_is_gnu, 0, offset_size,
23349 include_hash.get ());
cf2c3c16
TT
23350}
23351
8e19ed76 23352/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 23353 if so return true else false. */
380bca97 23354
8e19ed76 23355static int
6e5a29e1 23356attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
23357{
23358 return (attr == NULL ? 0 :
23359 attr->form == DW_FORM_block1
23360 || attr->form == DW_FORM_block2
23361 || attr->form == DW_FORM_block4
2dc7f7b3
TT
23362 || attr->form == DW_FORM_block
23363 || attr->form == DW_FORM_exprloc);
8e19ed76 23364}
4c2df51b 23365
c6a0999f
JB
23366/* Return non-zero if ATTR's value is a section offset --- classes
23367 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
23368 You may use DW_UNSND (attr) to retrieve such offsets.
23369
23370 Section 7.5.4, "Attribute Encodings", explains that no attribute
23371 may have a value that belongs to more than one of these classes; it
23372 would be ambiguous if we did, because we use the same forms for all
23373 of them. */
380bca97 23374
3690dd37 23375static int
6e5a29e1 23376attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
23377{
23378 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
23379 || attr->form == DW_FORM_data8
23380 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
23381}
23382
3690dd37
JB
23383/* Return non-zero if ATTR's value falls in the 'constant' class, or
23384 zero otherwise. When this function returns true, you can apply
23385 dwarf2_get_attr_constant_value to it.
23386
23387 However, note that for some attributes you must check
23388 attr_form_is_section_offset before using this test. DW_FORM_data4
23389 and DW_FORM_data8 are members of both the constant class, and of
23390 the classes that contain offsets into other debug sections
23391 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
23392 that, if an attribute's can be either a constant or one of the
23393 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
23394 taken as section offsets, not constants.
23395
23396 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
23397 cannot handle that. */
380bca97 23398
3690dd37 23399static int
6e5a29e1 23400attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
23401{
23402 switch (attr->form)
23403 {
23404 case DW_FORM_sdata:
23405 case DW_FORM_udata:
23406 case DW_FORM_data1:
23407 case DW_FORM_data2:
23408 case DW_FORM_data4:
23409 case DW_FORM_data8:
663c44ac 23410 case DW_FORM_implicit_const:
3690dd37
JB
23411 return 1;
23412 default:
23413 return 0;
23414 }
23415}
23416
7771576e
SA
23417
23418/* DW_ADDR is always stored already as sect_offset; despite for the forms
23419 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
23420
23421static int
6e5a29e1 23422attr_form_is_ref (const struct attribute *attr)
7771576e
SA
23423{
23424 switch (attr->form)
23425 {
23426 case DW_FORM_ref_addr:
23427 case DW_FORM_ref1:
23428 case DW_FORM_ref2:
23429 case DW_FORM_ref4:
23430 case DW_FORM_ref8:
23431 case DW_FORM_ref_udata:
23432 case DW_FORM_GNU_ref_alt:
23433 return 1;
23434 default:
23435 return 0;
23436 }
23437}
23438
3019eac3
DE
23439/* Return the .debug_loc section to use for CU.
23440 For DWO files use .debug_loc.dwo. */
23441
23442static struct dwarf2_section_info *
23443cu_debug_loc_section (struct dwarf2_cu *cu)
23444{
23445 if (cu->dwo_unit)
43988095
JK
23446 {
23447 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23448
23449 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23450 }
23451 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23452 : &dwarf2_per_objfile->loc);
3019eac3
DE
23453}
23454
8cf6f0b1
TT
23455/* A helper function that fills in a dwarf2_loclist_baton. */
23456
23457static void
23458fill_in_loclist_baton (struct dwarf2_cu *cu,
23459 struct dwarf2_loclist_baton *baton,
ff39bb5e 23460 const struct attribute *attr)
8cf6f0b1 23461{
3019eac3
DE
23462 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23463
23464 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
23465
23466 baton->per_cu = cu->per_cu;
23467 gdb_assert (baton->per_cu);
23468 /* We don't know how long the location list is, but make sure we
23469 don't run off the edge of the section. */
3019eac3
DE
23470 baton->size = section->size - DW_UNSND (attr);
23471 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 23472 baton->base_address = cu->base_address;
f664829e 23473 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23474}
23475
4c2df51b 23476static void
ff39bb5e 23477dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23478 struct dwarf2_cu *cu, int is_block)
4c2df51b 23479{
bb5ed363 23480 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23481 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23482
3690dd37 23483 if (attr_form_is_section_offset (attr)
3019eac3 23484 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23485 the section. If so, fall through to the complaint in the
23486 other branch. */
3019eac3 23487 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 23488 {
0d53c4c4 23489 struct dwarf2_loclist_baton *baton;
4c2df51b 23490
8d749320 23491 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23492
8cf6f0b1 23493 fill_in_loclist_baton (cu, baton, attr);
be391dca 23494
d00adf39 23495 if (cu->base_known == 0)
0d53c4c4 23496 complaint (&symfile_complaints,
3e43a32a
MS
23497 _("Location list used without "
23498 "specifying the CU base address."));
4c2df51b 23499
f1e6e072
TT
23500 SYMBOL_ACLASS_INDEX (sym) = (is_block
23501 ? dwarf2_loclist_block_index
23502 : dwarf2_loclist_index);
0d53c4c4
DJ
23503 SYMBOL_LOCATION_BATON (sym) = baton;
23504 }
23505 else
23506 {
23507 struct dwarf2_locexpr_baton *baton;
23508
8d749320 23509 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
23510 baton->per_cu = cu->per_cu;
23511 gdb_assert (baton->per_cu);
0d53c4c4
DJ
23512
23513 if (attr_form_is_block (attr))
23514 {
23515 /* Note that we're just copying the block's data pointer
23516 here, not the actual data. We're still pointing into the
6502dd73
DJ
23517 info_buffer for SYM's objfile; right now we never release
23518 that buffer, but when we do clean up properly this may
23519 need to change. */
0d53c4c4
DJ
23520 baton->size = DW_BLOCK (attr)->size;
23521 baton->data = DW_BLOCK (attr)->data;
23522 }
23523 else
23524 {
23525 dwarf2_invalid_attrib_class_complaint ("location description",
23526 SYMBOL_NATURAL_NAME (sym));
23527 baton->size = 0;
0d53c4c4 23528 }
6e70227d 23529
f1e6e072
TT
23530 SYMBOL_ACLASS_INDEX (sym) = (is_block
23531 ? dwarf2_locexpr_block_index
23532 : dwarf2_locexpr_index);
0d53c4c4
DJ
23533 SYMBOL_LOCATION_BATON (sym) = baton;
23534 }
4c2df51b 23535}
6502dd73 23536
9aa1f1e3
TT
23537/* Return the OBJFILE associated with the compilation unit CU. If CU
23538 came from a separate debuginfo file, then the master objfile is
23539 returned. */
ae0d2f24
UW
23540
23541struct objfile *
23542dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
23543{
9291a0cd 23544 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
23545
23546 /* Return the master objfile, so that we can report and look up the
23547 correct file containing this variable. */
23548 if (objfile->separate_debug_objfile_backlink)
23549 objfile = objfile->separate_debug_objfile_backlink;
23550
23551 return objfile;
23552}
23553
96408a79
SA
23554/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23555 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23556 CU_HEADERP first. */
23557
23558static const struct comp_unit_head *
23559per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23560 struct dwarf2_per_cu_data *per_cu)
23561{
d521ce57 23562 const gdb_byte *info_ptr;
96408a79
SA
23563
23564 if (per_cu->cu)
23565 return &per_cu->cu->header;
23566
9c541725 23567 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
23568
23569 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
23570 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23571 rcuh_kind::COMPILE);
96408a79
SA
23572
23573 return cu_headerp;
23574}
23575
ae0d2f24
UW
23576/* Return the address size given in the compilation unit header for CU. */
23577
98714339 23578int
ae0d2f24
UW
23579dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
23580{
96408a79
SA
23581 struct comp_unit_head cu_header_local;
23582 const struct comp_unit_head *cu_headerp;
c471e790 23583
96408a79
SA
23584 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23585
23586 return cu_headerp->addr_size;
ae0d2f24
UW
23587}
23588
9eae7c52
TT
23589/* Return the offset size given in the compilation unit header for CU. */
23590
23591int
23592dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
23593{
96408a79
SA
23594 struct comp_unit_head cu_header_local;
23595 const struct comp_unit_head *cu_headerp;
9c6c53f7 23596
96408a79
SA
23597 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23598
23599 return cu_headerp->offset_size;
23600}
23601
23602/* See its dwarf2loc.h declaration. */
23603
23604int
23605dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
23606{
23607 struct comp_unit_head cu_header_local;
23608 const struct comp_unit_head *cu_headerp;
23609
23610 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23611
23612 if (cu_headerp->version == 2)
23613 return cu_headerp->addr_size;
23614 else
23615 return cu_headerp->offset_size;
181cebd4
JK
23616}
23617
9aa1f1e3
TT
23618/* Return the text offset of the CU. The returned offset comes from
23619 this CU's objfile. If this objfile came from a separate debuginfo
23620 file, then the offset may be different from the corresponding
23621 offset in the parent objfile. */
23622
23623CORE_ADDR
23624dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
23625{
bb3fa9d0 23626 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
23627
23628 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23629}
23630
43988095
JK
23631/* Return DWARF version number of PER_CU. */
23632
23633short
23634dwarf2_version (struct dwarf2_per_cu_data *per_cu)
23635{
23636 return per_cu->dwarf_version;
23637}
23638
348e048f
DE
23639/* Locate the .debug_info compilation unit from CU's objfile which contains
23640 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
23641
23642static struct dwarf2_per_cu_data *
9c541725 23643dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 23644 unsigned int offset_in_dwz,
ae038cb0
DJ
23645 struct objfile *objfile)
23646{
23647 struct dwarf2_per_cu_data *this_cu;
23648 int low, high;
36586728 23649 const sect_offset *cu_off;
ae038cb0 23650
ae038cb0
DJ
23651 low = 0;
23652 high = dwarf2_per_objfile->n_comp_units - 1;
23653 while (high > low)
23654 {
36586728 23655 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23656 int mid = low + (high - low) / 2;
9a619af0 23657
36586728 23658 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 23659 cu_off = &mid_cu->sect_off;
36586728 23660 if (mid_cu->is_dwz > offset_in_dwz
9c541725 23661 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
23662 high = mid;
23663 else
23664 low = mid + 1;
23665 }
23666 gdb_assert (low == high);
36586728 23667 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
23668 cu_off = &this_cu->sect_off;
23669 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 23670 {
36586728 23671 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23672 error (_("Dwarf Error: could not find partial DIE containing "
9c541725
PA
23673 "offset 0x%x [in module %s]"),
23674 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
10b3939b 23675
9c541725
PA
23676 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23677 <= sect_off);
ae038cb0
DJ
23678 return dwarf2_per_objfile->all_comp_units[low-1];
23679 }
23680 else
23681 {
23682 this_cu = dwarf2_per_objfile->all_comp_units[low];
23683 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725
PA
23684 && sect_off >= this_cu->sect_off + this_cu->length)
23685 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
23686 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23687 return this_cu;
23688 }
23689}
23690
23745b47 23691/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 23692
9816fde3 23693static void
23745b47 23694init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 23695{
9816fde3 23696 memset (cu, 0, sizeof (*cu));
23745b47
DE
23697 per_cu->cu = cu;
23698 cu->per_cu = per_cu;
23699 cu->objfile = per_cu->objfile;
93311388 23700 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
23701}
23702
23703/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23704
23705static void
95554aad
TT
23706prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23707 enum language pretend_language)
9816fde3
JK
23708{
23709 struct attribute *attr;
23710
23711 /* Set the language we're debugging. */
23712 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23713 if (attr)
23714 set_cu_language (DW_UNSND (attr), cu);
23715 else
9cded63f 23716 {
95554aad 23717 cu->language = pretend_language;
9cded63f
TT
23718 cu->language_defn = language_def (cu->language);
23719 }
dee91e82 23720
7d45c7c3 23721 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23722}
23723
ae038cb0
DJ
23724/* Release one cached compilation unit, CU. We unlink it from the tree
23725 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
23726 the caller is responsible for that.
23727 NOTE: DATA is a void * because this function is also used as a
23728 cleanup routine. */
ae038cb0
DJ
23729
23730static void
68dc6402 23731free_heap_comp_unit (void *data)
ae038cb0 23732{
9a3c8263 23733 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 23734
23745b47
DE
23735 gdb_assert (cu->per_cu != NULL);
23736 cu->per_cu->cu = NULL;
ae038cb0
DJ
23737 cu->per_cu = NULL;
23738
23739 obstack_free (&cu->comp_unit_obstack, NULL);
23740
23741 xfree (cu);
23742}
23743
72bf9492 23744/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 23745 when we're finished with it. We can't free the pointer itself, but be
dee91e82 23746 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
23747
23748static void
23749free_stack_comp_unit (void *data)
23750{
9a3c8263 23751 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 23752
23745b47
DE
23753 gdb_assert (cu->per_cu != NULL);
23754 cu->per_cu->cu = NULL;
23755 cu->per_cu = NULL;
23756
72bf9492
DJ
23757 obstack_free (&cu->comp_unit_obstack, NULL);
23758 cu->partial_dies = NULL;
ae038cb0
DJ
23759}
23760
23761/* Free all cached compilation units. */
23762
23763static void
23764free_cached_comp_units (void *data)
23765{
330cdd98 23766 dwarf2_per_objfile->free_cached_comp_units ();
ae038cb0
DJ
23767}
23768
23769/* Increase the age counter on each cached compilation unit, and free
23770 any that are too old. */
23771
23772static void
23773age_cached_comp_units (void)
23774{
23775 struct dwarf2_per_cu_data *per_cu, **last_chain;
23776
23777 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23778 per_cu = dwarf2_per_objfile->read_in_chain;
23779 while (per_cu != NULL)
23780 {
23781 per_cu->cu->last_used ++;
b4f54984 23782 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23783 dwarf2_mark (per_cu->cu);
23784 per_cu = per_cu->cu->read_in_chain;
23785 }
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
23795 if (!per_cu->cu->mark)
23796 {
68dc6402 23797 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
23798 *last_chain = next_cu;
23799 }
23800 else
23801 last_chain = &per_cu->cu->read_in_chain;
23802
23803 per_cu = next_cu;
23804 }
23805}
23806
23807/* Remove a single compilation unit from the cache. */
23808
23809static void
dee91e82 23810free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
23811{
23812 struct dwarf2_per_cu_data *per_cu, **last_chain;
23813
23814 per_cu = dwarf2_per_objfile->read_in_chain;
23815 last_chain = &dwarf2_per_objfile->read_in_chain;
23816 while (per_cu != NULL)
23817 {
23818 struct dwarf2_per_cu_data *next_cu;
23819
23820 next_cu = per_cu->cu->read_in_chain;
23821
dee91e82 23822 if (per_cu == target_per_cu)
ae038cb0 23823 {
68dc6402 23824 free_heap_comp_unit (per_cu->cu);
dee91e82 23825 per_cu->cu = NULL;
ae038cb0
DJ
23826 *last_chain = next_cu;
23827 break;
23828 }
23829 else
23830 last_chain = &per_cu->cu->read_in_chain;
23831
23832 per_cu = next_cu;
23833 }
23834}
23835
fe3e1990
DJ
23836/* Release all extra memory associated with OBJFILE. */
23837
23838void
23839dwarf2_free_objfile (struct objfile *objfile)
23840{
9a3c8263
SM
23841 dwarf2_per_objfile
23842 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23843 dwarf2_objfile_data_key);
fe3e1990
DJ
23844
23845 if (dwarf2_per_objfile == NULL)
23846 return;
23847
330cdd98 23848 dwarf2_per_objfile->~dwarf2_per_objfile ();
fe3e1990
DJ
23849}
23850
dee91e82
DE
23851/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23852 We store these in a hash table separate from the DIEs, and preserve them
23853 when the DIEs are flushed out of cache.
23854
23855 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23856 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23857 or the type may come from a DWO file. Furthermore, while it's more logical
23858 to use per_cu->section+offset, with Fission the section with the data is in
23859 the DWO file but we don't know that section at the point we need it.
23860 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23861 because we can enter the lookup routine, get_die_type_at_offset, from
23862 outside this file, and thus won't necessarily have PER_CU->cu.
23863 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23864
dee91e82 23865struct dwarf2_per_cu_offset_and_type
1c379e20 23866{
dee91e82 23867 const struct dwarf2_per_cu_data *per_cu;
9c541725 23868 sect_offset sect_off;
1c379e20
DJ
23869 struct type *type;
23870};
23871
dee91e82 23872/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23873
23874static hashval_t
dee91e82 23875per_cu_offset_and_type_hash (const void *item)
1c379e20 23876{
9a3c8263
SM
23877 const struct dwarf2_per_cu_offset_and_type *ofs
23878 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23879
9c541725 23880 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23881}
23882
dee91e82 23883/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23884
23885static int
dee91e82 23886per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23887{
9a3c8263
SM
23888 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23889 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23890 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23891 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23892
dee91e82 23893 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23894 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23895}
23896
23897/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23898 table if necessary. For convenience, return TYPE.
23899
23900 The DIEs reading must have careful ordering to:
23901 * Not cause infite loops trying to read in DIEs as a prerequisite for
23902 reading current DIE.
23903 * Not trying to dereference contents of still incompletely read in types
23904 while reading in other DIEs.
23905 * Enable referencing still incompletely read in types just by a pointer to
23906 the type without accessing its fields.
23907
23908 Therefore caller should follow these rules:
23909 * Try to fetch any prerequisite types we may need to build this DIE type
23910 before building the type and calling set_die_type.
e71ec853 23911 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23912 possible before fetching more types to complete the current type.
23913 * Make the type as complete as possible before fetching more types. */
1c379e20 23914
f792889a 23915static struct type *
1c379e20
DJ
23916set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23917{
dee91e82 23918 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 23919 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
23920 struct attribute *attr;
23921 struct dynamic_prop prop;
1c379e20 23922
b4ba55a1
JB
23923 /* For Ada types, make sure that the gnat-specific data is always
23924 initialized (if not already set). There are a few types where
23925 we should not be doing so, because the type-specific area is
23926 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23927 where the type-specific area is used to store the floatformat).
23928 But this is not a problem, because the gnat-specific information
23929 is actually not needed for these types. */
23930 if (need_gnat_info (cu)
23931 && TYPE_CODE (type) != TYPE_CODE_FUNC
23932 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
23933 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23934 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23935 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
23936 && !HAVE_GNAT_AUX_INFO (type))
23937 INIT_GNAT_SPECIFIC (type);
23938
3f2f83dd
KB
23939 /* Read DW_AT_allocated and set in type. */
23940 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23941 if (attr_form_is_block (attr))
23942 {
23943 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23944 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
23945 }
23946 else if (attr != NULL)
23947 {
23948 complaint (&symfile_complaints,
9c541725
PA
23949 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
23950 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23951 to_underlying (die->sect_off));
3f2f83dd
KB
23952 }
23953
23954 /* Read DW_AT_associated and set in type. */
23955 attr = dwarf2_attr (die, DW_AT_associated, cu);
23956 if (attr_form_is_block (attr))
23957 {
23958 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23959 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
23960 }
23961 else if (attr != NULL)
23962 {
23963 complaint (&symfile_complaints,
9c541725
PA
23964 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
23965 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23966 to_underlying (die->sect_off));
3f2f83dd
KB
23967 }
23968
3cdcd0ce
JB
23969 /* Read DW_AT_data_location and set in type. */
23970 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23971 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 23972 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 23973
dee91e82 23974 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23975 {
dee91e82
DE
23976 dwarf2_per_objfile->die_type_hash =
23977 htab_create_alloc_ex (127,
23978 per_cu_offset_and_type_hash,
23979 per_cu_offset_and_type_eq,
23980 NULL,
23981 &objfile->objfile_obstack,
23982 hashtab_obstack_allocate,
23983 dummy_obstack_deallocate);
f792889a 23984 }
1c379e20 23985
dee91e82 23986 ofs.per_cu = cu->per_cu;
9c541725 23987 ofs.sect_off = die->sect_off;
1c379e20 23988 ofs.type = type;
dee91e82
DE
23989 slot = (struct dwarf2_per_cu_offset_and_type **)
23990 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
23991 if (*slot)
23992 complaint (&symfile_complaints,
23993 _("A problem internal to GDB: DIE 0x%x has type already set"),
9c541725 23994 to_underlying (die->sect_off));
8d749320
SM
23995 *slot = XOBNEW (&objfile->objfile_obstack,
23996 struct dwarf2_per_cu_offset_and_type);
1c379e20 23997 **slot = ofs;
f792889a 23998 return type;
1c379e20
DJ
23999}
24000
9c541725 24001/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 24002 or return NULL if the die does not have a saved type. */
1c379e20
DJ
24003
24004static struct type *
9c541725 24005get_die_type_at_offset (sect_offset sect_off,
673bfd45 24006 struct dwarf2_per_cu_data *per_cu)
1c379e20 24007{
dee91e82 24008 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 24009
dee91e82 24010 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 24011 return NULL;
1c379e20 24012
dee91e82 24013 ofs.per_cu = per_cu;
9c541725 24014 ofs.sect_off = sect_off;
9a3c8263
SM
24015 slot = ((struct dwarf2_per_cu_offset_and_type *)
24016 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
24017 if (slot)
24018 return slot->type;
24019 else
24020 return NULL;
24021}
24022
02142a6c 24023/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
24024 or return NULL if DIE does not have a saved type. */
24025
24026static struct type *
24027get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24028{
9c541725 24029 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
24030}
24031
10b3939b
DJ
24032/* Add a dependence relationship from CU to REF_PER_CU. */
24033
24034static void
24035dwarf2_add_dependence (struct dwarf2_cu *cu,
24036 struct dwarf2_per_cu_data *ref_per_cu)
24037{
24038 void **slot;
24039
24040 if (cu->dependencies == NULL)
24041 cu->dependencies
24042 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24043 NULL, &cu->comp_unit_obstack,
24044 hashtab_obstack_allocate,
24045 dummy_obstack_deallocate);
24046
24047 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24048 if (*slot == NULL)
24049 *slot = ref_per_cu;
24050}
1c379e20 24051
f504f079
DE
24052/* Subroutine of dwarf2_mark to pass to htab_traverse.
24053 Set the mark field in every compilation unit in the
ae038cb0
DJ
24054 cache that we must keep because we are keeping CU. */
24055
10b3939b
DJ
24056static int
24057dwarf2_mark_helper (void **slot, void *data)
24058{
24059 struct dwarf2_per_cu_data *per_cu;
24060
24061 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
24062
24063 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24064 reading of the chain. As such dependencies remain valid it is not much
24065 useful to track and undo them during QUIT cleanups. */
24066 if (per_cu->cu == NULL)
24067 return 1;
24068
10b3939b
DJ
24069 if (per_cu->cu->mark)
24070 return 1;
24071 per_cu->cu->mark = 1;
24072
24073 if (per_cu->cu->dependencies != NULL)
24074 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24075
24076 return 1;
24077}
24078
f504f079
DE
24079/* Set the mark field in CU and in every other compilation unit in the
24080 cache that we must keep because we are keeping CU. */
24081
ae038cb0
DJ
24082static void
24083dwarf2_mark (struct dwarf2_cu *cu)
24084{
24085 if (cu->mark)
24086 return;
24087 cu->mark = 1;
10b3939b
DJ
24088 if (cu->dependencies != NULL)
24089 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
24090}
24091
24092static void
24093dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24094{
24095 while (per_cu)
24096 {
24097 per_cu->cu->mark = 0;
24098 per_cu = per_cu->cu->read_in_chain;
24099 }
72bf9492
DJ
24100}
24101
72bf9492
DJ
24102/* Trivial hash function for partial_die_info: the hash value of a DIE
24103 is its offset in .debug_info for this objfile. */
24104
24105static hashval_t
24106partial_die_hash (const void *item)
24107{
9a3c8263
SM
24108 const struct partial_die_info *part_die
24109 = (const struct partial_die_info *) item;
9a619af0 24110
9c541725 24111 return to_underlying (part_die->sect_off);
72bf9492
DJ
24112}
24113
24114/* Trivial comparison function for partial_die_info structures: two DIEs
24115 are equal if they have the same offset. */
24116
24117static int
24118partial_die_eq (const void *item_lhs, const void *item_rhs)
24119{
9a3c8263
SM
24120 const struct partial_die_info *part_die_lhs
24121 = (const struct partial_die_info *) item_lhs;
24122 const struct partial_die_info *part_die_rhs
24123 = (const struct partial_die_info *) item_rhs;
9a619af0 24124
9c541725 24125 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
24126}
24127
b4f54984
DE
24128static struct cmd_list_element *set_dwarf_cmdlist;
24129static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
24130
24131static void
981a3fb3 24132set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 24133{
b4f54984 24134 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 24135 gdb_stdout);
ae038cb0
DJ
24136}
24137
24138static void
981a3fb3 24139show_dwarf_cmd (const char *args, int from_tty)
6e70227d 24140{
b4f54984 24141 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
24142}
24143
4bf44c1c 24144/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
24145
24146static void
c1bd65d0 24147dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 24148{
9a3c8263 24149 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 24150 int ix;
8b70b953 24151
626f2d1c
TT
24152 /* Make sure we don't accidentally use dwarf2_per_objfile while
24153 cleaning up. */
24154 dwarf2_per_objfile = NULL;
24155
59b0c7c1
JB
24156 for (ix = 0; ix < data->n_comp_units; ++ix)
24157 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 24158
59b0c7c1 24159 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 24160 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
24161 data->all_type_units[ix]->per_cu.imported_symtabs);
24162 xfree (data->all_type_units);
95554aad 24163
8b70b953 24164 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
24165
24166 if (data->dwo_files)
24167 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
24168 if (data->dwp_file)
24169 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
24170
24171 if (data->dwz_file && data->dwz_file->dwz_bfd)
24172 gdb_bfd_unref (data->dwz_file->dwz_bfd);
3f563c84
PA
24173
24174 if (data->index_table != NULL)
24175 data->index_table->~mapped_index ();
9291a0cd
TT
24176}
24177
24178\f
ae2de4f8 24179/* The "save gdb-index" command. */
9291a0cd 24180
bc8f2430
JK
24181/* In-memory buffer to prepare data to be written later to a file. */
24182class data_buf
9291a0cd 24183{
bc8f2430 24184public:
bc8f2430
JK
24185 /* Copy DATA to the end of the buffer. */
24186 template<typename T>
24187 void append_data (const T &data)
24188 {
24189 std::copy (reinterpret_cast<const gdb_byte *> (&data),
24190 reinterpret_cast<const gdb_byte *> (&data + 1),
c2f134ac 24191 grow (sizeof (data)));
bc8f2430 24192 }
b89be57b 24193
c2f134ac
PA
24194 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
24195 terminating zero is appended too. */
bc8f2430
JK
24196 void append_cstr0 (const char *cstr)
24197 {
24198 const size_t size = strlen (cstr) + 1;
c2f134ac
PA
24199 std::copy (cstr, cstr + size, grow (size));
24200 }
24201
24202 /* Accept a host-format integer in VAL and append it to the buffer
24203 as a target-format integer which is LEN bytes long. */
24204 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
24205 {
24206 ::store_unsigned_integer (grow (len), len, byte_order, val);
bc8f2430 24207 }
9291a0cd 24208
bc8f2430
JK
24209 /* Return the size of the buffer. */
24210 size_t size () const
24211 {
24212 return m_vec.size ();
24213 }
24214
24215 /* Write the buffer to FILE. */
24216 void file_write (FILE *file) const
24217 {
a81e6d4d
PA
24218 if (::fwrite (m_vec.data (), 1, m_vec.size (), file) != m_vec.size ())
24219 error (_("couldn't write data to file"));
bc8f2430
JK
24220 }
24221
24222private:
c2f134ac
PA
24223 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
24224 the start of the new block. */
24225 gdb_byte *grow (size_t size)
24226 {
24227 m_vec.resize (m_vec.size () + size);
24228 return &*m_vec.end () - size;
24229 }
24230
d5722aa2 24231 gdb::byte_vector m_vec;
bc8f2430 24232};
9291a0cd
TT
24233
24234/* An entry in the symbol table. */
24235struct symtab_index_entry
24236{
24237 /* The name of the symbol. */
24238 const char *name;
24239 /* The offset of the name in the constant pool. */
24240 offset_type index_offset;
24241 /* A sorted vector of the indices of all the CUs that hold an object
24242 of this name. */
bc8f2430 24243 std::vector<offset_type> cu_indices;
9291a0cd
TT
24244};
24245
24246/* The symbol table. This is a power-of-2-sized hash table. */
24247struct mapped_symtab
24248{
bc8f2430
JK
24249 mapped_symtab ()
24250 {
24251 data.resize (1024);
24252 }
b89be57b 24253
bc8f2430 24254 offset_type n_elements = 0;
4b76cda9 24255 std::vector<symtab_index_entry> data;
bc8f2430 24256};
9291a0cd 24257
bc8f2430 24258/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
559a7a62
JK
24259 the slot.
24260
24261 Function is used only during write_hash_table so no index format backward
24262 compatibility is needed. */
b89be57b 24263
4b76cda9 24264static symtab_index_entry &
9291a0cd
TT
24265find_slot (struct mapped_symtab *symtab, const char *name)
24266{
559a7a62 24267 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd 24268
bc8f2430
JK
24269 index = hash & (symtab->data.size () - 1);
24270 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
9291a0cd
TT
24271
24272 for (;;)
24273 {
4b76cda9
PA
24274 if (symtab->data[index].name == NULL
24275 || strcmp (name, symtab->data[index].name) == 0)
bc8f2430
JK
24276 return symtab->data[index];
24277 index = (index + step) & (symtab->data.size () - 1);
9291a0cd
TT
24278 }
24279}
24280
24281/* Expand SYMTAB's hash table. */
b89be57b 24282
9291a0cd
TT
24283static void
24284hash_expand (struct mapped_symtab *symtab)
24285{
bc8f2430 24286 auto old_entries = std::move (symtab->data);
9291a0cd 24287
bc8f2430
JK
24288 symtab->data.clear ();
24289 symtab->data.resize (old_entries.size () * 2);
9291a0cd 24290
bc8f2430 24291 for (auto &it : old_entries)
4b76cda9 24292 if (it.name != NULL)
bc8f2430 24293 {
4b76cda9 24294 auto &ref = find_slot (symtab, it.name);
bc8f2430
JK
24295 ref = std::move (it);
24296 }
9291a0cd
TT
24297}
24298
156942c7
DE
24299/* Add an entry to SYMTAB. NAME is the name of the symbol.
24300 CU_INDEX is the index of the CU in which the symbol appears.
24301 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 24302
9291a0cd
TT
24303static void
24304add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 24305 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
24306 offset_type cu_index)
24307{
156942c7 24308 offset_type cu_index_and_attrs;
9291a0cd
TT
24309
24310 ++symtab->n_elements;
bc8f2430 24311 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
9291a0cd
TT
24312 hash_expand (symtab);
24313
4b76cda9
PA
24314 symtab_index_entry &slot = find_slot (symtab, name);
24315 if (slot.name == NULL)
9291a0cd 24316 {
4b76cda9 24317 slot.name = name;
156942c7 24318 /* index_offset is set later. */
9291a0cd 24319 }
156942c7
DE
24320
24321 cu_index_and_attrs = 0;
24322 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
24323 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
24324 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
24325
24326 /* We don't want to record an index value twice as we want to avoid the
24327 duplication.
24328 We process all global symbols and then all static symbols
24329 (which would allow us to avoid the duplication by only having to check
24330 the last entry pushed), but a symbol could have multiple kinds in one CU.
24331 To keep things simple we don't worry about the duplication here and
24332 sort and uniqufy the list after we've processed all symbols. */
4b76cda9 24333 slot.cu_indices.push_back (cu_index_and_attrs);
156942c7
DE
24334}
24335
24336/* Sort and remove duplicates of all symbols' cu_indices lists. */
24337
24338static void
24339uniquify_cu_indices (struct mapped_symtab *symtab)
24340{
4b76cda9 24341 for (auto &entry : symtab->data)
156942c7 24342 {
4b76cda9 24343 if (entry.name != NULL && !entry.cu_indices.empty ())
156942c7 24344 {
4b76cda9 24345 auto &cu_indices = entry.cu_indices;
6fd931f2
PA
24346 std::sort (cu_indices.begin (), cu_indices.end ());
24347 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
24348 cu_indices.erase (from, cu_indices.end ());
156942c7
DE
24349 }
24350 }
9291a0cd
TT
24351}
24352
bc8f2430
JK
24353/* A form of 'const char *' suitable for container keys. Only the
24354 pointer is stored. The strings themselves are compared, not the
24355 pointers. */
24356class c_str_view
9291a0cd 24357{
bc8f2430
JK
24358public:
24359 c_str_view (const char *cstr)
24360 : m_cstr (cstr)
24361 {}
9291a0cd 24362
bc8f2430
JK
24363 bool operator== (const c_str_view &other) const
24364 {
24365 return strcmp (m_cstr, other.m_cstr) == 0;
24366 }
9291a0cd 24367
bc8f2430
JK
24368private:
24369 friend class c_str_view_hasher;
24370 const char *const m_cstr;
24371};
9291a0cd 24372
bc8f2430
JK
24373/* A std::unordered_map::hasher for c_str_view that uses the right
24374 hash function for strings in a mapped index. */
24375class c_str_view_hasher
24376{
24377public:
24378 size_t operator () (const c_str_view &x) const
24379 {
24380 return mapped_index_string_hash (INT_MAX, x.m_cstr);
24381 }
24382};
b89be57b 24383
bc8f2430
JK
24384/* A std::unordered_map::hasher for std::vector<>. */
24385template<typename T>
24386class vector_hasher
9291a0cd 24387{
bc8f2430
JK
24388public:
24389 size_t operator () (const std::vector<T> &key) const
24390 {
24391 return iterative_hash (key.data (),
24392 sizeof (key.front ()) * key.size (), 0);
24393 }
24394};
9291a0cd 24395
bc8f2430
JK
24396/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
24397 constant pool entries going into the data buffer CPOOL. */
3876f04e 24398
bc8f2430
JK
24399static void
24400write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
24401{
24402 {
24403 /* Elements are sorted vectors of the indices of all the CUs that
24404 hold an object of this name. */
24405 std::unordered_map<std::vector<offset_type>, offset_type,
24406 vector_hasher<offset_type>>
24407 symbol_hash_table;
24408
24409 /* We add all the index vectors to the constant pool first, to
24410 ensure alignment is ok. */
4b76cda9 24411 for (symtab_index_entry &entry : symtab->data)
bc8f2430 24412 {
4b76cda9 24413 if (entry.name == NULL)
bc8f2430 24414 continue;
4b76cda9 24415 gdb_assert (entry.index_offset == 0);
70a1152b
PA
24416
24417 /* Finding before inserting is faster than always trying to
24418 insert, because inserting always allocates a node, does the
24419 lookup, and then destroys the new node if another node
24420 already had the same key. C++17 try_emplace will avoid
24421 this. */
24422 const auto found
4b76cda9 24423 = symbol_hash_table.find (entry.cu_indices);
70a1152b
PA
24424 if (found != symbol_hash_table.end ())
24425 {
4b76cda9 24426 entry.index_offset = found->second;
70a1152b
PA
24427 continue;
24428 }
24429
4b76cda9
PA
24430 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
24431 entry.index_offset = cpool.size ();
24432 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
24433 for (const auto index : entry.cu_indices)
24434 cpool.append_data (MAYBE_SWAP (index));
bc8f2430
JK
24435 }
24436 }
9291a0cd
TT
24437
24438 /* Now write out the hash table. */
bc8f2430 24439 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
4b76cda9 24440 for (const auto &entry : symtab->data)
9291a0cd
TT
24441 {
24442 offset_type str_off, vec_off;
24443
4b76cda9 24444 if (entry.name != NULL)
9291a0cd 24445 {
4b76cda9 24446 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
bc8f2430 24447 if (insertpair.second)
4b76cda9 24448 cpool.append_cstr0 (entry.name);
bc8f2430 24449 str_off = insertpair.first->second;
4b76cda9 24450 vec_off = entry.index_offset;
9291a0cd
TT
24451 }
24452 else
24453 {
24454 /* While 0 is a valid constant pool index, it is not valid
24455 to have 0 for both offsets. */
24456 str_off = 0;
24457 vec_off = 0;
24458 }
24459
bc8f2430
JK
24460 output.append_data (MAYBE_SWAP (str_off));
24461 output.append_data (MAYBE_SWAP (vec_off));
9291a0cd 24462 }
9291a0cd
TT
24463}
24464
bc8f2430 24465typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
0a5429f6
DE
24466
24467/* Helper struct for building the address table. */
24468struct addrmap_index_data
24469{
bc8f2430
JK
24470 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
24471 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
24472 {}
24473
0a5429f6 24474 struct objfile *objfile;
bc8f2430
JK
24475 data_buf &addr_vec;
24476 psym_index_map &cu_index_htab;
0a5429f6
DE
24477
24478 /* Non-zero if the previous_* fields are valid.
24479 We can't write an entry until we see the next entry (since it is only then
24480 that we know the end of the entry). */
24481 int previous_valid;
24482 /* Index of the CU in the table of all CUs in the index file. */
24483 unsigned int previous_cu_index;
0963b4bd 24484 /* Start address of the CU. */
0a5429f6
DE
24485 CORE_ADDR previous_cu_start;
24486};
24487
bc8f2430 24488/* Write an address entry to ADDR_VEC. */
b89be57b 24489
9291a0cd 24490static void
bc8f2430 24491add_address_entry (struct objfile *objfile, data_buf &addr_vec,
0a5429f6 24492 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 24493{
9291a0cd
TT
24494 CORE_ADDR baseaddr;
24495
24496 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24497
c2f134ac
PA
24498 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
24499 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
bc8f2430 24500 addr_vec.append_data (MAYBE_SWAP (cu_index));
0a5429f6
DE
24501}
24502
24503/* Worker function for traversing an addrmap to build the address table. */
24504
24505static int
24506add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
24507{
9a3c8263
SM
24508 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
24509 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
24510
24511 if (data->previous_valid)
bc8f2430 24512 add_address_entry (data->objfile, data->addr_vec,
0a5429f6
DE
24513 data->previous_cu_start, start_addr,
24514 data->previous_cu_index);
24515
24516 data->previous_cu_start = start_addr;
24517 if (pst != NULL)
24518 {
bc8f2430
JK
24519 const auto it = data->cu_index_htab.find (pst);
24520 gdb_assert (it != data->cu_index_htab.cend ());
24521 data->previous_cu_index = it->second;
0a5429f6
DE
24522 data->previous_valid = 1;
24523 }
24524 else
bc8f2430 24525 data->previous_valid = 0;
0a5429f6
DE
24526
24527 return 0;
24528}
24529
bc8f2430 24530/* Write OBJFILE's address map to ADDR_VEC.
0a5429f6
DE
24531 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
24532 in the index file. */
24533
24534static void
bc8f2430
JK
24535write_address_map (struct objfile *objfile, data_buf &addr_vec,
24536 psym_index_map &cu_index_htab)
0a5429f6 24537{
bc8f2430 24538 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
0a5429f6
DE
24539
24540 /* When writing the address table, we have to cope with the fact that
24541 the addrmap iterator only provides the start of a region; we have to
24542 wait until the next invocation to get the start of the next region. */
24543
24544 addrmap_index_data.objfile = objfile;
0a5429f6
DE
24545 addrmap_index_data.previous_valid = 0;
24546
24547 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
24548 &addrmap_index_data);
24549
24550 /* It's highly unlikely the last entry (end address = 0xff...ff)
24551 is valid, but we should still handle it.
24552 The end address is recorded as the start of the next region, but that
24553 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
24554 anyway. */
24555 if (addrmap_index_data.previous_valid)
bc8f2430 24556 add_address_entry (objfile, addr_vec,
0a5429f6
DE
24557 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
24558 addrmap_index_data.previous_cu_index);
9291a0cd
TT
24559}
24560
156942c7
DE
24561/* Return the symbol kind of PSYM. */
24562
24563static gdb_index_symbol_kind
24564symbol_kind (struct partial_symbol *psym)
24565{
24566 domain_enum domain = PSYMBOL_DOMAIN (psym);
24567 enum address_class aclass = PSYMBOL_CLASS (psym);
24568
24569 switch (domain)
24570 {
24571 case VAR_DOMAIN:
24572 switch (aclass)
24573 {
24574 case LOC_BLOCK:
24575 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
24576 case LOC_TYPEDEF:
24577 return GDB_INDEX_SYMBOL_KIND_TYPE;
24578 case LOC_COMPUTED:
24579 case LOC_CONST_BYTES:
24580 case LOC_OPTIMIZED_OUT:
24581 case LOC_STATIC:
24582 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
24583 case LOC_CONST:
24584 /* Note: It's currently impossible to recognize psyms as enum values
24585 short of reading the type info. For now punt. */
24586 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
24587 default:
24588 /* There are other LOC_FOO values that one might want to classify
24589 as variables, but dwarf2read.c doesn't currently use them. */
24590 return GDB_INDEX_SYMBOL_KIND_OTHER;
24591 }
24592 case STRUCT_DOMAIN:
24593 return GDB_INDEX_SYMBOL_KIND_TYPE;
24594 default:
24595 return GDB_INDEX_SYMBOL_KIND_OTHER;
24596 }
24597}
24598
9291a0cd 24599/* Add a list of partial symbols to SYMTAB. */
b89be57b 24600
9291a0cd
TT
24601static void
24602write_psymbols (struct mapped_symtab *symtab,
bc8f2430 24603 std::unordered_set<partial_symbol *> &psyms_seen,
9291a0cd
TT
24604 struct partial_symbol **psymp,
24605 int count,
987d643c
TT
24606 offset_type cu_index,
24607 int is_static)
9291a0cd
TT
24608{
24609 for (; count-- > 0; ++psymp)
24610 {
156942c7 24611 struct partial_symbol *psym = *psymp;
987d643c 24612
156942c7 24613 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 24614 error (_("Ada is not currently supported by the index"));
987d643c 24615
987d643c 24616 /* Only add a given psymbol once. */
bc8f2430 24617 if (psyms_seen.insert (psym).second)
987d643c 24618 {
156942c7
DE
24619 gdb_index_symbol_kind kind = symbol_kind (psym);
24620
156942c7
DE
24621 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
24622 is_static, kind, cu_index);
987d643c 24623 }
9291a0cd
TT
24624 }
24625}
24626
1fd400ff
TT
24627/* A helper struct used when iterating over debug_types. */
24628struct signatured_type_index_data
24629{
bc8f2430
JK
24630 signatured_type_index_data (data_buf &types_list_,
24631 std::unordered_set<partial_symbol *> &psyms_seen_)
24632 : types_list (types_list_), psyms_seen (psyms_seen_)
24633 {}
24634
1fd400ff
TT
24635 struct objfile *objfile;
24636 struct mapped_symtab *symtab;
bc8f2430
JK
24637 data_buf &types_list;
24638 std::unordered_set<partial_symbol *> &psyms_seen;
1fd400ff
TT
24639 int cu_index;
24640};
24641
24642/* A helper function that writes a single signatured_type to an
24643 obstack. */
b89be57b 24644
1fd400ff
TT
24645static int
24646write_one_signatured_type (void **slot, void *d)
24647{
9a3c8263
SM
24648 struct signatured_type_index_data *info
24649 = (struct signatured_type_index_data *) d;
1fd400ff 24650 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 24651 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
24652
24653 write_psymbols (info->symtab,
987d643c 24654 info->psyms_seen,
af5bf4ad 24655 &info->objfile->global_psymbols[psymtab->globals_offset],
987d643c
TT
24656 psymtab->n_global_syms, info->cu_index,
24657 0);
1fd400ff 24658 write_psymbols (info->symtab,
987d643c 24659 info->psyms_seen,
af5bf4ad 24660 &info->objfile->static_psymbols[psymtab->statics_offset],
987d643c
TT
24661 psymtab->n_static_syms, info->cu_index,
24662 1);
1fd400ff 24663
c2f134ac
PA
24664 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
24665 to_underlying (entry->per_cu.sect_off));
24666 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
24667 to_underlying (entry->type_offset_in_tu));
24668 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
1fd400ff
TT
24669
24670 ++info->cu_index;
24671
24672 return 1;
24673}
24674
e8f8bcb3
PA
24675/* Recurse into all "included" dependencies and count their symbols as
24676 if they appeared in this psymtab. */
24677
24678static void
24679recursively_count_psymbols (struct partial_symtab *psymtab,
24680 size_t &psyms_seen)
24681{
24682 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
24683 if (psymtab->dependencies[i]->user != NULL)
24684 recursively_count_psymbols (psymtab->dependencies[i],
24685 psyms_seen);
24686
24687 psyms_seen += psymtab->n_global_syms;
24688 psyms_seen += psymtab->n_static_syms;
24689}
24690
95554aad
TT
24691/* Recurse into all "included" dependencies and write their symbols as
24692 if they appeared in this psymtab. */
24693
24694static void
24695recursively_write_psymbols (struct objfile *objfile,
24696 struct partial_symtab *psymtab,
24697 struct mapped_symtab *symtab,
bc8f2430 24698 std::unordered_set<partial_symbol *> &psyms_seen,
95554aad
TT
24699 offset_type cu_index)
24700{
24701 int i;
24702
24703 for (i = 0; i < psymtab->number_of_dependencies; ++i)
24704 if (psymtab->dependencies[i]->user != NULL)
24705 recursively_write_psymbols (objfile, psymtab->dependencies[i],
24706 symtab, psyms_seen, cu_index);
24707
24708 write_psymbols (symtab,
24709 psyms_seen,
af5bf4ad 24710 &objfile->global_psymbols[psymtab->globals_offset],
95554aad
TT
24711 psymtab->n_global_syms, cu_index,
24712 0);
24713 write_psymbols (symtab,
24714 psyms_seen,
af5bf4ad 24715 &objfile->static_psymbols[psymtab->statics_offset],
95554aad
TT
24716 psymtab->n_static_syms, cu_index,
24717 1);
24718}
24719
9291a0cd 24720/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 24721
9291a0cd
TT
24722static void
24723write_psymtabs_to_index (struct objfile *objfile, const char *dir)
24724{
9291a0cd
TT
24725 if (dwarf2_per_objfile->using_index)
24726 error (_("Cannot use an index to create the index"));
24727
8b70b953
TT
24728 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
24729 error (_("Cannot make an index when the file has multiple .debug_types sections"));
24730
260b681b
DE
24731 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
24732 return;
24733
bc8f2430 24734 struct stat st;
4262abfb
JK
24735 if (stat (objfile_name (objfile), &st) < 0)
24736 perror_with_name (objfile_name (objfile));
9291a0cd 24737
bc8f2430
JK
24738 std::string filename (std::string (dir) + SLASH_STRING
24739 + lbasename (objfile_name (objfile)) + INDEX_SUFFIX);
9291a0cd 24740
d419f42d 24741 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
9291a0cd 24742 if (!out_file)
bc8f2430 24743 error (_("Can't open `%s' for writing"), filename.c_str ());
9291a0cd 24744
16b7a719
PA
24745 /* Order matters here; we want FILE to be closed before FILENAME is
24746 unlinked, because on MS-Windows one cannot delete a file that is
24747 still open. (Don't call anything here that might throw until
24748 file_closer is created.) */
bc8f2430 24749 gdb::unlinker unlink_file (filename.c_str ());
d419f42d 24750 gdb_file_up close_out_file (out_file);
9291a0cd 24751
bc8f2430
JK
24752 mapped_symtab symtab;
24753 data_buf cu_list;
987d643c 24754
0a5429f6
DE
24755 /* While we're scanning CU's create a table that maps a psymtab pointer
24756 (which is what addrmap records) to its index (which is what is recorded
24757 in the index file). This will later be needed to write the address
24758 table. */
bc8f2430
JK
24759 psym_index_map cu_index_htab;
24760 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
0a5429f6
DE
24761
24762 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
24763 work here. Also, the debug_types entries do not appear in
24764 all_comp_units, but only in their own hash table. */
e8f8bcb3
PA
24765
24766 /* The psyms_seen set is potentially going to be largish (~40k
24767 elements when indexing a -g3 build of GDB itself). Estimate the
24768 number of elements in order to avoid too many rehashes, which
24769 require rebuilding buckets and thus many trips to
24770 malloc/free. */
24771 size_t psyms_count = 0;
24772 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24773 {
24774 struct dwarf2_per_cu_data *per_cu
24775 = dwarf2_per_objfile->all_comp_units[i];
24776 struct partial_symtab *psymtab = per_cu->v.psymtab;
24777
24778 if (psymtab != NULL && psymtab->user == NULL)
24779 recursively_count_psymbols (psymtab, psyms_count);
24780 }
24781 /* Generating an index for gdb itself shows a ratio of
24782 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
24783 std::unordered_set<partial_symbol *> psyms_seen (psyms_count / 4);
bc8f2430 24784 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 24785 {
3e43a32a
MS
24786 struct dwarf2_per_cu_data *per_cu
24787 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 24788 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 24789
92fac807
JK
24790 /* CU of a shared file from 'dwz -m' may be unused by this main file.
24791 It may be referenced from a local scope but in such case it does not
24792 need to be present in .gdb_index. */
24793 if (psymtab == NULL)
24794 continue;
24795
95554aad 24796 if (psymtab->user == NULL)
bc8f2430
JK
24797 recursively_write_psymbols (objfile, psymtab, &symtab,
24798 psyms_seen, i);
9291a0cd 24799
bc8f2430
JK
24800 const auto insertpair = cu_index_htab.emplace (psymtab, i);
24801 gdb_assert (insertpair.second);
9291a0cd 24802
c2f134ac
PA
24803 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
24804 to_underlying (per_cu->sect_off));
24805 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
24806 }
24807
0a5429f6 24808 /* Dump the address map. */
bc8f2430
JK
24809 data_buf addr_vec;
24810 write_address_map (objfile, addr_vec, cu_index_htab);
0a5429f6 24811
1fd400ff 24812 /* Write out the .debug_type entries, if any. */
bc8f2430 24813 data_buf types_cu_list;
1fd400ff
TT
24814 if (dwarf2_per_objfile->signatured_types)
24815 {
bc8f2430
JK
24816 signatured_type_index_data sig_data (types_cu_list,
24817 psyms_seen);
1fd400ff
TT
24818
24819 sig_data.objfile = objfile;
bc8f2430 24820 sig_data.symtab = &symtab;
1fd400ff
TT
24821 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
24822 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
24823 write_one_signatured_type, &sig_data);
24824 }
24825
156942c7
DE
24826 /* Now that we've processed all symbols we can shrink their cu_indices
24827 lists. */
bc8f2430 24828 uniquify_cu_indices (&symtab);
156942c7 24829
bc8f2430
JK
24830 data_buf symtab_vec, constant_pool;
24831 write_hash_table (&symtab, symtab_vec, constant_pool);
9291a0cd 24832
bc8f2430
JK
24833 data_buf contents;
24834 const offset_type size_of_contents = 6 * sizeof (offset_type);
24835 offset_type total_len = size_of_contents;
9291a0cd
TT
24836
24837 /* The version number. */
bc8f2430 24838 contents.append_data (MAYBE_SWAP (8));
9291a0cd
TT
24839
24840 /* The offset of the CU list from the start of the file. */
bc8f2430
JK
24841 contents.append_data (MAYBE_SWAP (total_len));
24842 total_len += cu_list.size ();
9291a0cd 24843
1fd400ff 24844 /* The offset of the types CU list from the start of the file. */
bc8f2430
JK
24845 contents.append_data (MAYBE_SWAP (total_len));
24846 total_len += types_cu_list.size ();
1fd400ff 24847
9291a0cd 24848 /* The offset of the address table from the start of the file. */
bc8f2430
JK
24849 contents.append_data (MAYBE_SWAP (total_len));
24850 total_len += addr_vec.size ();
9291a0cd
TT
24851
24852 /* The offset of the symbol table from the start of the file. */
bc8f2430
JK
24853 contents.append_data (MAYBE_SWAP (total_len));
24854 total_len += symtab_vec.size ();
9291a0cd
TT
24855
24856 /* The offset of the constant pool from the start of the file. */
bc8f2430
JK
24857 contents.append_data (MAYBE_SWAP (total_len));
24858 total_len += constant_pool.size ();
9291a0cd 24859
bc8f2430 24860 gdb_assert (contents.size () == size_of_contents);
9291a0cd 24861
bc8f2430
JK
24862 contents.file_write (out_file);
24863 cu_list.file_write (out_file);
24864 types_cu_list.file_write (out_file);
24865 addr_vec.file_write (out_file);
24866 symtab_vec.file_write (out_file);
24867 constant_pool.file_write (out_file);
9291a0cd 24868
bef155c3
TT
24869 /* We want to keep the file. */
24870 unlink_file.keep ();
9291a0cd
TT
24871}
24872
90476074
TT
24873/* Implementation of the `save gdb-index' command.
24874
24875 Note that the file format used by this command is documented in the
24876 GDB manual. Any changes here must be documented there. */
11570e71 24877
9291a0cd 24878static void
8384c356 24879save_gdb_index_command (const char *arg, int from_tty)
9291a0cd
TT
24880{
24881 struct objfile *objfile;
24882
24883 if (!arg || !*arg)
96d19272 24884 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
24885
24886 ALL_OBJFILES (objfile)
24887 {
24888 struct stat st;
24889
24890 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 24891 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
24892 continue;
24893
9a3c8263
SM
24894 dwarf2_per_objfile
24895 = (struct dwarf2_per_objfile *) objfile_data (objfile,
24896 dwarf2_objfile_data_key);
9291a0cd
TT
24897 if (dwarf2_per_objfile)
24898 {
9291a0cd 24899
492d29ea 24900 TRY
9291a0cd
TT
24901 {
24902 write_psymtabs_to_index (objfile, arg);
24903 }
492d29ea
PA
24904 CATCH (except, RETURN_MASK_ERROR)
24905 {
24906 exception_fprintf (gdb_stderr, except,
24907 _("Error while writing index for `%s': "),
24908 objfile_name (objfile));
24909 }
24910 END_CATCH
9291a0cd
TT
24911 }
24912 }
dce234bc
PP
24913}
24914
9291a0cd
TT
24915\f
24916
b4f54984 24917int dwarf_always_disassemble;
9eae7c52
TT
24918
24919static void
b4f54984
DE
24920show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24921 struct cmd_list_element *c, const char *value)
9eae7c52 24922{
3e43a32a
MS
24923 fprintf_filtered (file,
24924 _("Whether to always disassemble "
24925 "DWARF expressions is %s.\n"),
9eae7c52
TT
24926 value);
24927}
24928
900e11f9
JK
24929static void
24930show_check_physname (struct ui_file *file, int from_tty,
24931 struct cmd_list_element *c, const char *value)
24932{
24933 fprintf_filtered (file,
24934 _("Whether to check \"physname\" is %s.\n"),
24935 value);
24936}
24937
6502dd73
DJ
24938void
24939_initialize_dwarf2_read (void)
24940{
96d19272
JK
24941 struct cmd_list_element *c;
24942
dce234bc 24943 dwarf2_objfile_data_key
c1bd65d0 24944 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 24945
b4f54984
DE
24946 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24947Set DWARF specific variables.\n\
24948Configure DWARF variables such as the cache size"),
24949 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
24950 0/*allow-unknown*/, &maintenance_set_cmdlist);
24951
b4f54984
DE
24952 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24953Show DWARF specific variables\n\
24954Show DWARF variables such as the cache size"),
24955 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
24956 0/*allow-unknown*/, &maintenance_show_cmdlist);
24957
24958 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
24959 &dwarf_max_cache_age, _("\
24960Set the upper bound on the age of cached DWARF compilation units."), _("\
24961Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
24962A higher limit means that cached compilation units will be stored\n\
24963in memory longer, and more total memory will be used. Zero disables\n\
24964caching, which can slow down startup."),
2c5b56ce 24965 NULL,
b4f54984
DE
24966 show_dwarf_max_cache_age,
24967 &set_dwarf_cmdlist,
24968 &show_dwarf_cmdlist);
d97bc12b 24969
9eae7c52 24970 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 24971 &dwarf_always_disassemble, _("\
9eae7c52
TT
24972Set whether `info address' always disassembles DWARF expressions."), _("\
24973Show whether `info address' always disassembles DWARF expressions."), _("\
24974When enabled, DWARF expressions are always printed in an assembly-like\n\
24975syntax. When disabled, expressions will be printed in a more\n\
24976conversational style, when possible."),
24977 NULL,
b4f54984
DE
24978 show_dwarf_always_disassemble,
24979 &set_dwarf_cmdlist,
24980 &show_dwarf_cmdlist);
24981
24982 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24983Set debugging of the DWARF reader."), _("\
24984Show debugging of the DWARF reader."), _("\
24985When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
24986reading and symtab expansion. A value of 1 (one) provides basic\n\
24987information. A value greater than 1 provides more verbose information."),
45cfd468
DE
24988 NULL,
24989 NULL,
24990 &setdebuglist, &showdebuglist);
24991
b4f54984
DE
24992 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24993Set debugging of the DWARF DIE reader."), _("\
24994Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
24995When enabled (non-zero), DIEs are dumped after they are read in.\n\
24996The value is the maximum depth to print."),
ccce17b0
YQ
24997 NULL,
24998 NULL,
24999 &setdebuglist, &showdebuglist);
9291a0cd 25000
27e0867f
DE
25001 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25002Set debugging of the dwarf line reader."), _("\
25003Show debugging of the dwarf line reader."), _("\
25004When enabled (non-zero), line number entries are dumped as they are read in.\n\
25005A value of 1 (one) provides basic information.\n\
25006A value greater than 1 provides more verbose information."),
25007 NULL,
25008 NULL,
25009 &setdebuglist, &showdebuglist);
25010
900e11f9
JK
25011 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25012Set cross-checking of \"physname\" code against demangler."), _("\
25013Show cross-checking of \"physname\" code against demangler."), _("\
25014When enabled, GDB's internal \"physname\" code is checked against\n\
25015the demangler."),
25016 NULL, show_check_physname,
25017 &setdebuglist, &showdebuglist);
25018
e615022a
DE
25019 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25020 no_class, &use_deprecated_index_sections, _("\
25021Set whether to use deprecated gdb_index sections."), _("\
25022Show whether to use deprecated gdb_index sections."), _("\
25023When enabled, deprecated .gdb_index sections are used anyway.\n\
25024Normally they are ignored either because of a missing feature or\n\
25025performance issue.\n\
25026Warning: This option must be enabled before gdb reads the file."),
25027 NULL,
25028 NULL,
25029 &setlist, &showlist);
25030
96d19272 25031 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 25032 _("\
fc1a9d6e 25033Save a gdb-index file.\n\
11570e71 25034Usage: save gdb-index DIRECTORY"),
96d19272
JK
25035 &save_cmdlist);
25036 set_cmd_completer (c, filename_completer);
f1e6e072
TT
25037
25038 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25039 &dwarf2_locexpr_funcs);
25040 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25041 &dwarf2_loclist_funcs);
25042
25043 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25044 &dwarf2_block_frame_base_locexpr_funcs);
25045 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25046 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25047
25048#if GDB_SELF_TEST
25049 selftests::register_test ("dw2_expand_symtabs_matching",
25050 selftests::dw2_expand_symtabs_matching::run_test);
25051#endif
6502dd73 25052}
This page took 4.19915 seconds and 4 git commands to generate.