Remove usage of find_inferior when calling kill_one_lwp_callback
[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
259 /* Convenience method to get at the name of the symbol at IDX in the
260 symbol table. */
261 const char *symbol_name_at (offset_type idx) const
262 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx]); }
9291a0cd
TT
263};
264
95554aad
TT
265typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
266DEF_VEC_P (dwarf2_per_cu_ptr);
267
52059ffd
TT
268struct tu_stats
269{
270 int nr_uniq_abbrev_tables;
271 int nr_symtabs;
272 int nr_symtab_sharers;
273 int nr_stmt_less_type_units;
274 int nr_all_type_units_reallocs;
275};
276
9cdd5dbd
DE
277/* Collection of data recorded per objfile.
278 This hangs off of dwarf2_objfile_data_key. */
279
6502dd73
DJ
280struct dwarf2_per_objfile
281{
330cdd98
PA
282 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
283 dwarf2 section names, or is NULL if the standard ELF names are
284 used. */
285 dwarf2_per_objfile (struct objfile *objfile,
286 const dwarf2_debug_sections *names);
ae038cb0 287
330cdd98
PA
288 ~dwarf2_per_objfile ();
289
d6541620 290 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
330cdd98
PA
291
292 /* Free all cached compilation units. */
293 void free_cached_comp_units ();
294private:
295 /* This function is mapped across the sections and remembers the
296 offset and size of each of the debugging sections we are
297 interested in. */
298 void locate_sections (bfd *abfd, asection *sectp,
299 const dwarf2_debug_sections &names);
300
301public:
302 dwarf2_section_info info {};
303 dwarf2_section_info abbrev {};
304 dwarf2_section_info line {};
305 dwarf2_section_info loc {};
306 dwarf2_section_info loclists {};
307 dwarf2_section_info macinfo {};
308 dwarf2_section_info macro {};
309 dwarf2_section_info str {};
310 dwarf2_section_info line_str {};
311 dwarf2_section_info ranges {};
312 dwarf2_section_info rnglists {};
313 dwarf2_section_info addr {};
314 dwarf2_section_info frame {};
315 dwarf2_section_info eh_frame {};
316 dwarf2_section_info gdb_index {};
317
318 VEC (dwarf2_section_info_def) *types = NULL;
8b70b953 319
be391dca 320 /* Back link. */
330cdd98 321 struct objfile *objfile = NULL;
be391dca 322
d467dd73 323 /* Table of all the compilation units. This is used to locate
10b3939b 324 the target compilation unit of a particular reference. */
330cdd98 325 struct dwarf2_per_cu_data **all_comp_units = NULL;
ae038cb0
DJ
326
327 /* The number of compilation units in ALL_COMP_UNITS. */
330cdd98 328 int n_comp_units = 0;
ae038cb0 329
1fd400ff 330 /* The number of .debug_types-related CUs. */
330cdd98 331 int n_type_units = 0;
1fd400ff 332
6aa5f3a6
DE
333 /* The number of elements allocated in all_type_units.
334 If there are skeleton-less TUs, we add them to all_type_units lazily. */
330cdd98 335 int n_allocated_type_units = 0;
6aa5f3a6 336
a2ce51a0
DE
337 /* The .debug_types-related CUs (TUs).
338 This is stored in malloc space because we may realloc it. */
330cdd98 339 struct signatured_type **all_type_units = NULL;
1fd400ff 340
f4dc4d17
DE
341 /* Table of struct type_unit_group objects.
342 The hash key is the DW_AT_stmt_list value. */
330cdd98 343 htab_t type_unit_groups {};
72dca2f5 344
348e048f
DE
345 /* A table mapping .debug_types signatures to its signatured_type entry.
346 This is NULL if the .debug_types section hasn't been read in yet. */
330cdd98 347 htab_t signatured_types {};
348e048f 348
f4dc4d17
DE
349 /* Type unit statistics, to see how well the scaling improvements
350 are doing. */
330cdd98 351 struct tu_stats tu_stats {};
f4dc4d17
DE
352
353 /* A chain of compilation units that are currently read in, so that
354 they can be freed later. */
330cdd98 355 dwarf2_per_cu_data *read_in_chain = NULL;
f4dc4d17 356
3019eac3
DE
357 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
358 This is NULL if the table hasn't been allocated yet. */
330cdd98 359 htab_t dwo_files {};
3019eac3 360
330cdd98
PA
361 /* True if we've checked for whether there is a DWP file. */
362 bool dwp_checked = false;
80626a55
DE
363
364 /* The DWP file if there is one, or NULL. */
330cdd98 365 struct dwp_file *dwp_file = NULL;
80626a55 366
36586728
TT
367 /* The shared '.dwz' file, if one exists. This is used when the
368 original data was compressed using 'dwz -m'. */
330cdd98 369 struct dwz_file *dwz_file = NULL;
36586728 370
330cdd98 371 /* A flag indicating whether this objfile has a section loaded at a
72dca2f5 372 VMA of 0. */
330cdd98 373 bool has_section_at_zero = false;
9291a0cd 374
ae2de4f8
DE
375 /* True if we are using the mapped index,
376 or we are faking it for OBJF_READNOW's sake. */
330cdd98 377 bool using_index = false;
9291a0cd 378
ae2de4f8 379 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
330cdd98 380 mapped_index *index_table = NULL;
98bfdba5 381
7b9f3c50 382 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
383 TUs typically share line table entries with a CU, so we maintain a
384 separate table of all line table entries to support the sharing.
385 Note that while there can be way more TUs than CUs, we've already
386 sorted all the TUs into "type unit groups", grouped by their
387 DW_AT_stmt_list value. Therefore the only sharing done here is with a
388 CU and its associated TU group if there is one. */
330cdd98 389 htab_t quick_file_names_table {};
7b9f3c50 390
98bfdba5
PA
391 /* Set during partial symbol reading, to prevent queueing of full
392 symbols. */
330cdd98 393 bool reading_partial_symbols = false;
673bfd45 394
dee91e82 395 /* Table mapping type DIEs to their struct type *.
673bfd45 396 This is NULL if not allocated yet.
02142a6c 397 The mapping is done via (CU/TU + DIE offset) -> type. */
330cdd98 398 htab_t die_type_hash {};
95554aad
TT
399
400 /* The CUs we recently read. */
330cdd98 401 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
527f3840
JK
402
403 /* Table containing line_header indexed by offset and offset_in_dwz. */
330cdd98 404 htab_t line_header_hash {};
bbf2f4df
PA
405
406 /* Table containing all filenames. This is an optional because the
407 table is lazily constructed on first access. */
408 gdb::optional<filename_seen_cache> filenames_cache;
6502dd73
DJ
409};
410
411static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 412
251d32d9 413/* Default names of the debugging sections. */
c906108c 414
233a11ab
CS
415/* Note that if the debugging section has been compressed, it might
416 have a name like .zdebug_info. */
417
9cdd5dbd
DE
418static const struct dwarf2_debug_sections dwarf2_elf_names =
419{
251d32d9
TG
420 { ".debug_info", ".zdebug_info" },
421 { ".debug_abbrev", ".zdebug_abbrev" },
422 { ".debug_line", ".zdebug_line" },
423 { ".debug_loc", ".zdebug_loc" },
43988095 424 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 425 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 426 { ".debug_macro", ".zdebug_macro" },
251d32d9 427 { ".debug_str", ".zdebug_str" },
43988095 428 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 429 { ".debug_ranges", ".zdebug_ranges" },
43988095 430 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 431 { ".debug_types", ".zdebug_types" },
3019eac3 432 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
433 { ".debug_frame", ".zdebug_frame" },
434 { ".eh_frame", NULL },
24d3216f
TT
435 { ".gdb_index", ".zgdb_index" },
436 23
251d32d9 437};
c906108c 438
80626a55 439/* List of DWO/DWP sections. */
3019eac3 440
80626a55 441static const struct dwop_section_names
3019eac3
DE
442{
443 struct dwarf2_section_names abbrev_dwo;
444 struct dwarf2_section_names info_dwo;
445 struct dwarf2_section_names line_dwo;
446 struct dwarf2_section_names loc_dwo;
43988095 447 struct dwarf2_section_names loclists_dwo;
09262596
DE
448 struct dwarf2_section_names macinfo_dwo;
449 struct dwarf2_section_names macro_dwo;
3019eac3
DE
450 struct dwarf2_section_names str_dwo;
451 struct dwarf2_section_names str_offsets_dwo;
452 struct dwarf2_section_names types_dwo;
80626a55
DE
453 struct dwarf2_section_names cu_index;
454 struct dwarf2_section_names tu_index;
3019eac3 455}
80626a55 456dwop_section_names =
3019eac3
DE
457{
458 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
459 { ".debug_info.dwo", ".zdebug_info.dwo" },
460 { ".debug_line.dwo", ".zdebug_line.dwo" },
461 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 462 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
463 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
464 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
465 { ".debug_str.dwo", ".zdebug_str.dwo" },
466 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
467 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
468 { ".debug_cu_index", ".zdebug_cu_index" },
469 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
470};
471
c906108c
SS
472/* local data types */
473
107d2387
AC
474/* The data in a compilation unit header, after target2host
475 translation, looks like this. */
c906108c 476struct comp_unit_head
a738430d 477{
c764a876 478 unsigned int length;
a738430d 479 short version;
a738430d
MK
480 unsigned char addr_size;
481 unsigned char signed_addr_p;
9c541725 482 sect_offset abbrev_sect_off;
57349743 483
a738430d
MK
484 /* Size of file offsets; either 4 or 8. */
485 unsigned int offset_size;
57349743 486
a738430d
MK
487 /* Size of the length field; either 4 or 12. */
488 unsigned int initial_length_size;
57349743 489
43988095
JK
490 enum dwarf_unit_type unit_type;
491
a738430d
MK
492 /* Offset to the first byte of this compilation unit header in the
493 .debug_info section, for resolving relative reference dies. */
9c541725 494 sect_offset sect_off;
57349743 495
d00adf39
DE
496 /* Offset to first die in this cu from the start of the cu.
497 This will be the first byte following the compilation unit header. */
9c541725 498 cu_offset first_die_cu_offset;
43988095
JK
499
500 /* 64-bit signature of this type unit - it is valid only for
501 UNIT_TYPE DW_UT_type. */
502 ULONGEST signature;
503
504 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 505 cu_offset type_cu_offset_in_tu;
a738430d 506};
c906108c 507
3da10d80
KS
508/* Type used for delaying computation of method physnames.
509 See comments for compute_delayed_physnames. */
510struct delayed_method_info
511{
512 /* The type to which the method is attached, i.e., its parent class. */
513 struct type *type;
514
515 /* The index of the method in the type's function fieldlists. */
516 int fnfield_index;
517
518 /* The index of the method in the fieldlist. */
519 int index;
520
521 /* The name of the DIE. */
522 const char *name;
523
524 /* The DIE associated with this method. */
525 struct die_info *die;
526};
527
528typedef struct delayed_method_info delayed_method_info;
529DEF_VEC_O (delayed_method_info);
530
e7c27a73
DJ
531/* Internal state when decoding a particular compilation unit. */
532struct dwarf2_cu
533{
534 /* The objfile containing this compilation unit. */
535 struct objfile *objfile;
536
d00adf39 537 /* The header of the compilation unit. */
e7c27a73 538 struct comp_unit_head header;
e142c38c 539
d00adf39
DE
540 /* Base address of this compilation unit. */
541 CORE_ADDR base_address;
542
543 /* Non-zero if base_address has been set. */
544 int base_known;
545
e142c38c
DJ
546 /* The language we are debugging. */
547 enum language language;
548 const struct language_defn *language_defn;
549
b0f35d58
DL
550 const char *producer;
551
e142c38c
DJ
552 /* The generic symbol table building routines have separate lists for
553 file scope symbols and all all other scopes (local scopes). So
554 we need to select the right one to pass to add_symbol_to_list().
555 We do it by keeping a pointer to the correct list in list_in_scope.
556
557 FIXME: The original dwarf code just treated the file scope as the
558 first local scope, and all other local scopes as nested local
559 scopes, and worked fine. Check to see if we really need to
560 distinguish these in buildsym.c. */
561 struct pending **list_in_scope;
562
433df2d4
DE
563 /* The abbrev table for this CU.
564 Normally this points to the abbrev table in the objfile.
565 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
566 struct abbrev_table *abbrev_table;
72bf9492 567
b64f50a1
JK
568 /* Hash table holding all the loaded partial DIEs
569 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
570 htab_t partial_dies;
571
572 /* Storage for things with the same lifetime as this read-in compilation
573 unit, including partial DIEs. */
574 struct obstack comp_unit_obstack;
575
ae038cb0
DJ
576 /* When multiple dwarf2_cu structures are living in memory, this field
577 chains them all together, so that they can be released efficiently.
578 We will probably also want a generation counter so that most-recently-used
579 compilation units are cached... */
580 struct dwarf2_per_cu_data *read_in_chain;
581
69d751e3 582 /* Backlink to our per_cu entry. */
ae038cb0
DJ
583 struct dwarf2_per_cu_data *per_cu;
584
585 /* How many compilation units ago was this CU last referenced? */
586 int last_used;
587
b64f50a1
JK
588 /* A hash table of DIE cu_offset for following references with
589 die_info->offset.sect_off as hash. */
51545339 590 htab_t die_hash;
10b3939b
DJ
591
592 /* Full DIEs if read in. */
593 struct die_info *dies;
594
595 /* A set of pointers to dwarf2_per_cu_data objects for compilation
596 units referenced by this one. Only set during full symbol processing;
597 partial symbol tables do not have dependencies. */
598 htab_t dependencies;
599
cb1df416
DJ
600 /* Header data from the line table, during full symbol processing. */
601 struct line_header *line_header;
4c8aa72d
PA
602 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
603 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
604 this is the DW_TAG_compile_unit die for this CU. We'll hold on
605 to the line header as long as this DIE is being processed. See
606 process_die_scope. */
607 die_info *line_header_die_owner;
cb1df416 608
3da10d80
KS
609 /* A list of methods which need to have physnames computed
610 after all type information has been read. */
611 VEC (delayed_method_info) *method_list;
612
96408a79
SA
613 /* To be copied to symtab->call_site_htab. */
614 htab_t call_site_htab;
615
034e5797
DE
616 /* Non-NULL if this CU came from a DWO file.
617 There is an invariant here that is important to remember:
618 Except for attributes copied from the top level DIE in the "main"
619 (or "stub") file in preparation for reading the DWO file
620 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
621 Either there isn't a DWO file (in which case this is NULL and the point
622 is moot), or there is and either we're not going to read it (in which
623 case this is NULL) or there is and we are reading it (in which case this
624 is non-NULL). */
3019eac3
DE
625 struct dwo_unit *dwo_unit;
626
627 /* The DW_AT_addr_base attribute if present, zero otherwise
628 (zero is a valid value though).
1dbab08b 629 Note this value comes from the Fission stub CU/TU's DIE. */
3019eac3
DE
630 ULONGEST addr_base;
631
2e3cf129
DE
632 /* The DW_AT_ranges_base attribute if present, zero otherwise
633 (zero is a valid value though).
1dbab08b 634 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 635 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
636 be used without needing to know whether DWO files are in use or not.
637 N.B. This does not apply to DW_AT_ranges appearing in
638 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
639 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
640 DW_AT_ranges_base *would* have to be applied, and we'd have to care
641 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
642 ULONGEST ranges_base;
643
ae038cb0
DJ
644 /* Mark used when releasing cached dies. */
645 unsigned int mark : 1;
646
8be455d7
JK
647 /* This CU references .debug_loc. See the symtab->locations_valid field.
648 This test is imperfect as there may exist optimized debug code not using
649 any location list and still facing inlining issues if handled as
650 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 651 unsigned int has_loclist : 1;
ba919b58 652
1b80a9fa
JK
653 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
654 if all the producer_is_* fields are valid. This information is cached
655 because profiling CU expansion showed excessive time spent in
656 producer_is_gxx_lt_4_6. */
ba919b58
TT
657 unsigned int checked_producer : 1;
658 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 659 unsigned int producer_is_gcc_lt_4_3 : 1;
5230b05a 660 unsigned int producer_is_icc_lt_14 : 1;
4d4ec4e5
TT
661
662 /* When set, the file that we're processing is known to have
663 debugging info for C++ namespaces. GCC 3.3.x did not produce
664 this information, but later versions do. */
665
666 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
667};
668
10b3939b
DJ
669/* Persistent data held for a compilation unit, even when not
670 processing it. We put a pointer to this structure in the
28dee7f5 671 read_symtab_private field of the psymtab. */
10b3939b 672
ae038cb0
DJ
673struct dwarf2_per_cu_data
674{
36586728 675 /* The start offset and length of this compilation unit.
45452591 676 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
677 initial_length_size.
678 If the DIE refers to a DWO file, this is always of the original die,
679 not the DWO file. */
9c541725 680 sect_offset sect_off;
36586728 681 unsigned int length;
ae038cb0 682
43988095
JK
683 /* DWARF standard version this data has been read from (such as 4 or 5). */
684 short dwarf_version;
685
ae038cb0
DJ
686 /* Flag indicating this compilation unit will be read in before
687 any of the current compilation units are processed. */
c764a876 688 unsigned int queued : 1;
ae038cb0 689
0d99eb77
DE
690 /* This flag will be set when reading partial DIEs if we need to load
691 absolutely all DIEs for this compilation unit, instead of just the ones
692 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
693 hash table and don't find it. */
694 unsigned int load_all_dies : 1;
695
0186c6a7
DE
696 /* Non-zero if this CU is from .debug_types.
697 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
698 this is non-zero. */
3019eac3
DE
699 unsigned int is_debug_types : 1;
700
36586728
TT
701 /* Non-zero if this CU is from the .dwz file. */
702 unsigned int is_dwz : 1;
703
a2ce51a0
DE
704 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
705 This flag is only valid if is_debug_types is true.
706 We can't read a CU directly from a DWO file: There are required
707 attributes in the stub. */
708 unsigned int reading_dwo_directly : 1;
709
7ee85ab1
DE
710 /* Non-zero if the TU has been read.
711 This is used to assist the "Stay in DWO Optimization" for Fission:
712 When reading a DWO, it's faster to read TUs from the DWO instead of
713 fetching them from random other DWOs (due to comdat folding).
714 If the TU has already been read, the optimization is unnecessary
715 (and unwise - we don't want to change where gdb thinks the TU lives
716 "midflight").
717 This flag is only valid if is_debug_types is true. */
718 unsigned int tu_read : 1;
719
3019eac3
DE
720 /* The section this CU/TU lives in.
721 If the DIE refers to a DWO file, this is always the original die,
722 not the DWO file. */
8a0459fd 723 struct dwarf2_section_info *section;
348e048f 724
17ea53c3 725 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
726 of the CU cache it gets reset to NULL again. This is left as NULL for
727 dummy CUs (a CU header, but nothing else). */
ae038cb0 728 struct dwarf2_cu *cu;
1c379e20 729
9cdd5dbd
DE
730 /* The corresponding objfile.
731 Normally we can get the objfile from dwarf2_per_objfile.
732 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
733 struct objfile *objfile;
734
fffbe6a8
YQ
735 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
736 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
737 union
738 {
739 /* The partial symbol table associated with this compilation unit,
95554aad 740 or NULL for unread partial units. */
9291a0cd
TT
741 struct partial_symtab *psymtab;
742
743 /* Data needed by the "quick" functions. */
744 struct dwarf2_per_cu_quick_data *quick;
745 } v;
95554aad 746
796a7ff8
DE
747 /* The CUs we import using DW_TAG_imported_unit. This is filled in
748 while reading psymtabs, used to compute the psymtab dependencies,
749 and then cleared. Then it is filled in again while reading full
750 symbols, and only deleted when the objfile is destroyed.
751
752 This is also used to work around a difference between the way gold
753 generates .gdb_index version <=7 and the way gdb does. Arguably this
754 is a gold bug. For symbols coming from TUs, gold records in the index
755 the CU that includes the TU instead of the TU itself. This breaks
756 dw2_lookup_symbol: It assumes that if the index says symbol X lives
757 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
758 will find X. Alas TUs live in their own symtab, so after expanding CU Y
759 we need to look in TU Z to find X. Fortunately, this is akin to
760 DW_TAG_imported_unit, so we just use the same mechanism: For
761 .gdb_index version <=7 this also records the TUs that the CU referred
762 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
763 indices so we only pay a price for gold generated indices.
764 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 765 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
766};
767
348e048f
DE
768/* Entry in the signatured_types hash table. */
769
770struct signatured_type
771{
42e7ad6c 772 /* The "per_cu" object of this type.
ac9ec31b 773 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
774 N.B.: This is the first member so that it's easy to convert pointers
775 between them. */
776 struct dwarf2_per_cu_data per_cu;
777
3019eac3 778 /* The type's signature. */
348e048f
DE
779 ULONGEST signature;
780
3019eac3 781 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
782 If this TU is a DWO stub and the definition lives in a DWO file
783 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
784 cu_offset type_offset_in_tu;
785
786 /* Offset in the section of the type's DIE.
787 If the definition lives in a DWO file, this is the offset in the
788 .debug_types.dwo section.
789 The value is zero until the actual value is known.
790 Zero is otherwise not a valid section offset. */
791 sect_offset type_offset_in_section;
0186c6a7
DE
792
793 /* Type units are grouped by their DW_AT_stmt_list entry so that they
794 can share them. This points to the containing symtab. */
795 struct type_unit_group *type_unit_group;
ac9ec31b
DE
796
797 /* The type.
798 The first time we encounter this type we fully read it in and install it
799 in the symbol tables. Subsequent times we only need the type. */
800 struct type *type;
a2ce51a0
DE
801
802 /* Containing DWO unit.
803 This field is valid iff per_cu.reading_dwo_directly. */
804 struct dwo_unit *dwo_unit;
348e048f
DE
805};
806
0186c6a7
DE
807typedef struct signatured_type *sig_type_ptr;
808DEF_VEC_P (sig_type_ptr);
809
094b34ac
DE
810/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
811 This includes type_unit_group and quick_file_names. */
812
813struct stmt_list_hash
814{
815 /* The DWO unit this table is from or NULL if there is none. */
816 struct dwo_unit *dwo_unit;
817
818 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 819 sect_offset line_sect_off;
094b34ac
DE
820};
821
f4dc4d17
DE
822/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
823 an object of this type. */
824
825struct type_unit_group
826{
0186c6a7 827 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
828 To simplify things we create an artificial CU that "includes" all the
829 type units using this stmt_list so that the rest of the code still has
830 a "per_cu" handle on the symtab.
831 This PER_CU is recognized by having no section. */
8a0459fd 832#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
833 struct dwarf2_per_cu_data per_cu;
834
0186c6a7
DE
835 /* The TUs that share this DW_AT_stmt_list entry.
836 This is added to while parsing type units to build partial symtabs,
837 and is deleted afterwards and not used again. */
838 VEC (sig_type_ptr) *tus;
f4dc4d17 839
43f3e411 840 /* The compunit symtab.
094b34ac 841 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
842 so we create an essentially anonymous symtab as the compunit symtab. */
843 struct compunit_symtab *compunit_symtab;
f4dc4d17 844
094b34ac
DE
845 /* The data used to construct the hash key. */
846 struct stmt_list_hash hash;
f4dc4d17
DE
847
848 /* The number of symtabs from the line header.
849 The value here must match line_header.num_file_names. */
850 unsigned int num_symtabs;
851
852 /* The symbol tables for this TU (obtained from the files listed in
853 DW_AT_stmt_list).
854 WARNING: The order of entries here must match the order of entries
855 in the line header. After the first TU using this type_unit_group, the
856 line header for the subsequent TUs is recreated from this. This is done
857 because we need to use the same symtabs for each TU using the same
858 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
859 there's no guarantee the line header doesn't have duplicate entries. */
860 struct symtab **symtabs;
861};
862
73869dc2 863/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
864
865struct dwo_sections
866{
867 struct dwarf2_section_info abbrev;
3019eac3
DE
868 struct dwarf2_section_info line;
869 struct dwarf2_section_info loc;
43988095 870 struct dwarf2_section_info loclists;
09262596
DE
871 struct dwarf2_section_info macinfo;
872 struct dwarf2_section_info macro;
3019eac3
DE
873 struct dwarf2_section_info str;
874 struct dwarf2_section_info str_offsets;
80626a55
DE
875 /* In the case of a virtual DWO file, these two are unused. */
876 struct dwarf2_section_info info;
3019eac3
DE
877 VEC (dwarf2_section_info_def) *types;
878};
879
c88ee1f0 880/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
881
882struct dwo_unit
883{
884 /* Backlink to the containing struct dwo_file. */
885 struct dwo_file *dwo_file;
886
887 /* The "id" that distinguishes this CU/TU.
888 .debug_info calls this "dwo_id", .debug_types calls this "signature".
889 Since signatures came first, we stick with it for consistency. */
890 ULONGEST signature;
891
892 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 893 struct dwarf2_section_info *section;
3019eac3 894
9c541725
PA
895 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
896 sect_offset sect_off;
3019eac3
DE
897 unsigned int length;
898
899 /* For types, offset in the type's DIE of the type defined by this TU. */
900 cu_offset type_offset_in_tu;
901};
902
73869dc2
DE
903/* include/dwarf2.h defines the DWP section codes.
904 It defines a max value but it doesn't define a min value, which we
905 use for error checking, so provide one. */
906
907enum dwp_v2_section_ids
908{
909 DW_SECT_MIN = 1
910};
911
80626a55 912/* Data for one DWO file.
57d63ce2
DE
913
914 This includes virtual DWO files (a virtual DWO file is a DWO file as it
915 appears in a DWP file). DWP files don't really have DWO files per se -
916 comdat folding of types "loses" the DWO file they came from, and from
917 a high level view DWP files appear to contain a mass of random types.
918 However, to maintain consistency with the non-DWP case we pretend DWP
919 files contain virtual DWO files, and we assign each TU with one virtual
920 DWO file (generally based on the line and abbrev section offsets -
921 a heuristic that seems to work in practice). */
3019eac3
DE
922
923struct dwo_file
924{
0ac5b59e 925 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
926 For virtual DWO files the name is constructed from the section offsets
927 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
928 from related CU+TUs. */
0ac5b59e
DE
929 const char *dwo_name;
930
931 /* The DW_AT_comp_dir attribute. */
932 const char *comp_dir;
3019eac3 933
80626a55
DE
934 /* The bfd, when the file is open. Otherwise this is NULL.
935 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
936 bfd *dbfd;
3019eac3 937
73869dc2
DE
938 /* The sections that make up this DWO file.
939 Remember that for virtual DWO files in DWP V2, these are virtual
940 sections (for lack of a better name). */
3019eac3
DE
941 struct dwo_sections sections;
942
33c5cd75
DB
943 /* The CUs in the file.
944 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
945 an extension to handle LLVM's Link Time Optimization output (where
946 multiple source files may be compiled into a single object/dwo pair). */
947 htab_t cus;
3019eac3
DE
948
949 /* Table of TUs in the file.
950 Each element is a struct dwo_unit. */
951 htab_t tus;
952};
953
80626a55
DE
954/* These sections are what may appear in a DWP file. */
955
956struct dwp_sections
957{
73869dc2 958 /* These are used by both DWP version 1 and 2. */
80626a55
DE
959 struct dwarf2_section_info str;
960 struct dwarf2_section_info cu_index;
961 struct dwarf2_section_info tu_index;
73869dc2
DE
962
963 /* These are only used by DWP version 2 files.
964 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
965 sections are referenced by section number, and are not recorded here.
966 In DWP version 2 there is at most one copy of all these sections, each
967 section being (effectively) comprised of the concatenation of all of the
968 individual sections that exist in the version 1 format.
969 To keep the code simple we treat each of these concatenated pieces as a
970 section itself (a virtual section?). */
971 struct dwarf2_section_info abbrev;
972 struct dwarf2_section_info info;
973 struct dwarf2_section_info line;
974 struct dwarf2_section_info loc;
975 struct dwarf2_section_info macinfo;
976 struct dwarf2_section_info macro;
977 struct dwarf2_section_info str_offsets;
978 struct dwarf2_section_info types;
80626a55
DE
979};
980
73869dc2
DE
981/* These sections are what may appear in a virtual DWO file in DWP version 1.
982 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 983
73869dc2 984struct virtual_v1_dwo_sections
80626a55
DE
985{
986 struct dwarf2_section_info abbrev;
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 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 993 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
994 struct dwarf2_section_info info_or_types;
995};
996
73869dc2
DE
997/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
998 In version 2, the sections of the DWO files are concatenated together
999 and stored in one section of that name. Thus each ELF section contains
1000 several "virtual" sections. */
1001
1002struct virtual_v2_dwo_sections
1003{
1004 bfd_size_type abbrev_offset;
1005 bfd_size_type abbrev_size;
1006
1007 bfd_size_type line_offset;
1008 bfd_size_type line_size;
1009
1010 bfd_size_type loc_offset;
1011 bfd_size_type loc_size;
1012
1013 bfd_size_type macinfo_offset;
1014 bfd_size_type macinfo_size;
1015
1016 bfd_size_type macro_offset;
1017 bfd_size_type macro_size;
1018
1019 bfd_size_type str_offsets_offset;
1020 bfd_size_type str_offsets_size;
1021
1022 /* Each DWP hash table entry records one CU or one TU.
1023 That is recorded here, and copied to dwo_unit.section. */
1024 bfd_size_type info_or_types_offset;
1025 bfd_size_type info_or_types_size;
1026};
1027
80626a55
DE
1028/* Contents of DWP hash tables. */
1029
1030struct dwp_hash_table
1031{
73869dc2 1032 uint32_t version, nr_columns;
80626a55 1033 uint32_t nr_units, nr_slots;
73869dc2
DE
1034 const gdb_byte *hash_table, *unit_table;
1035 union
1036 {
1037 struct
1038 {
1039 const gdb_byte *indices;
1040 } v1;
1041 struct
1042 {
1043 /* This is indexed by column number and gives the id of the section
1044 in that column. */
1045#define MAX_NR_V2_DWO_SECTIONS \
1046 (1 /* .debug_info or .debug_types */ \
1047 + 1 /* .debug_abbrev */ \
1048 + 1 /* .debug_line */ \
1049 + 1 /* .debug_loc */ \
1050 + 1 /* .debug_str_offsets */ \
1051 + 1 /* .debug_macro or .debug_macinfo */)
1052 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1053 const gdb_byte *offsets;
1054 const gdb_byte *sizes;
1055 } v2;
1056 } section_pool;
80626a55
DE
1057};
1058
1059/* Data for one DWP file. */
1060
1061struct dwp_file
1062{
1063 /* Name of the file. */
1064 const char *name;
1065
73869dc2
DE
1066 /* File format version. */
1067 int version;
1068
93417882 1069 /* The bfd. */
80626a55
DE
1070 bfd *dbfd;
1071
1072 /* Section info for this file. */
1073 struct dwp_sections sections;
1074
57d63ce2 1075 /* Table of CUs in the file. */
80626a55
DE
1076 const struct dwp_hash_table *cus;
1077
1078 /* Table of TUs in the file. */
1079 const struct dwp_hash_table *tus;
1080
19ac8c2e
DE
1081 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1082 htab_t loaded_cus;
1083 htab_t loaded_tus;
80626a55 1084
73869dc2
DE
1085 /* Table to map ELF section numbers to their sections.
1086 This is only needed for the DWP V1 file format. */
80626a55
DE
1087 unsigned int num_sections;
1088 asection **elf_sections;
1089};
1090
36586728
TT
1091/* This represents a '.dwz' file. */
1092
1093struct dwz_file
1094{
1095 /* A dwz file can only contain a few sections. */
1096 struct dwarf2_section_info abbrev;
1097 struct dwarf2_section_info info;
1098 struct dwarf2_section_info str;
1099 struct dwarf2_section_info line;
1100 struct dwarf2_section_info macro;
2ec9a5e0 1101 struct dwarf2_section_info gdb_index;
36586728
TT
1102
1103 /* The dwz's BFD. */
1104 bfd *dwz_bfd;
1105};
1106
0963b4bd
MS
1107/* Struct used to pass misc. parameters to read_die_and_children, et
1108 al. which are used for both .debug_info and .debug_types dies.
1109 All parameters here are unchanging for the life of the call. This
dee91e82 1110 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1111
1112struct die_reader_specs
1113{
a32a8923 1114 /* The bfd of die_section. */
93311388
DE
1115 bfd* abfd;
1116
1117 /* The CU of the DIE we are parsing. */
1118 struct dwarf2_cu *cu;
1119
80626a55 1120 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1121 struct dwo_file *dwo_file;
1122
dee91e82 1123 /* The section the die comes from.
3019eac3 1124 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1125 struct dwarf2_section_info *die_section;
1126
1127 /* die_section->buffer. */
d521ce57 1128 const gdb_byte *buffer;
f664829e
DE
1129
1130 /* The end of the buffer. */
1131 const gdb_byte *buffer_end;
a2ce51a0
DE
1132
1133 /* The value of the DW_AT_comp_dir attribute. */
1134 const char *comp_dir;
93311388
DE
1135};
1136
fd820528 1137/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1138typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1139 const gdb_byte *info_ptr,
dee91e82
DE
1140 struct die_info *comp_unit_die,
1141 int has_children,
1142 void *data);
1143
ecfb656c
PA
1144/* A 1-based directory index. This is a strong typedef to prevent
1145 accidentally using a directory index as a 0-based index into an
1146 array/vector. */
1147enum class dir_index : unsigned int {};
1148
1149/* Likewise, a 1-based file name index. */
1150enum class file_name_index : unsigned int {};
1151
52059ffd
TT
1152struct file_entry
1153{
fff8551c
PA
1154 file_entry () = default;
1155
ecfb656c 1156 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
1157 unsigned int mod_time_, unsigned int length_)
1158 : name (name_),
ecfb656c 1159 d_index (d_index_),
fff8551c
PA
1160 mod_time (mod_time_),
1161 length (length_)
1162 {}
1163
ecfb656c
PA
1164 /* Return the include directory at D_INDEX stored in LH. Returns
1165 NULL if D_INDEX is out of bounds. */
8c43009f
PA
1166 const char *include_dir (const line_header *lh) const;
1167
fff8551c
PA
1168 /* The file name. Note this is an observing pointer. The memory is
1169 owned by debug_line_buffer. */
1170 const char *name {};
1171
8c43009f 1172 /* The directory index (1-based). */
ecfb656c 1173 dir_index d_index {};
fff8551c
PA
1174
1175 unsigned int mod_time {};
1176
1177 unsigned int length {};
1178
1179 /* True if referenced by the Line Number Program. */
1180 bool included_p {};
1181
83769d0b 1182 /* The associated symbol table, if any. */
fff8551c 1183 struct symtab *symtab {};
52059ffd
TT
1184};
1185
debd256d
JB
1186/* The line number information for a compilation unit (found in the
1187 .debug_line section) begins with a "statement program header",
1188 which contains the following information. */
1189struct line_header
1190{
fff8551c
PA
1191 line_header ()
1192 : offset_in_dwz {}
1193 {}
1194
1195 /* Add an entry to the include directory table. */
1196 void add_include_dir (const char *include_dir);
1197
1198 /* Add an entry to the file name table. */
ecfb656c 1199 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1200 unsigned int mod_time, unsigned int length);
1201
ecfb656c 1202 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 1203 is out of bounds. */
ecfb656c 1204 const char *include_dir_at (dir_index index) const
8c43009f 1205 {
ecfb656c
PA
1206 /* Convert directory index number (1-based) to vector index
1207 (0-based). */
1208 size_t vec_index = to_underlying (index) - 1;
1209
1210 if (vec_index >= include_dirs.size ())
8c43009f 1211 return NULL;
ecfb656c 1212 return include_dirs[vec_index];
8c43009f
PA
1213 }
1214
ecfb656c 1215 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 1216 is out of bounds. */
ecfb656c 1217 file_entry *file_name_at (file_name_index index)
8c43009f 1218 {
ecfb656c
PA
1219 /* Convert file name index number (1-based) to vector index
1220 (0-based). */
1221 size_t vec_index = to_underlying (index) - 1;
1222
1223 if (vec_index >= file_names.size ())
fff8551c 1224 return NULL;
ecfb656c 1225 return &file_names[vec_index];
fff8551c
PA
1226 }
1227
1228 /* Const version of the above. */
1229 const file_entry *file_name_at (unsigned int index) const
1230 {
1231 if (index >= file_names.size ())
8c43009f
PA
1232 return NULL;
1233 return &file_names[index];
1234 }
1235
527f3840 1236 /* Offset of line number information in .debug_line section. */
9c541725 1237 sect_offset sect_off {};
527f3840
JK
1238
1239 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1240 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1241
1242 unsigned int total_length {};
1243 unsigned short version {};
1244 unsigned int header_length {};
1245 unsigned char minimum_instruction_length {};
1246 unsigned char maximum_ops_per_instruction {};
1247 unsigned char default_is_stmt {};
1248 int line_base {};
1249 unsigned char line_range {};
1250 unsigned char opcode_base {};
debd256d
JB
1251
1252 /* standard_opcode_lengths[i] is the number of operands for the
1253 standard opcode whose value is i. This means that
1254 standard_opcode_lengths[0] is unused, and the last meaningful
1255 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1256 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1257
fff8551c
PA
1258 /* The include_directories table. Note these are observing
1259 pointers. The memory is owned by debug_line_buffer. */
1260 std::vector<const char *> include_dirs;
debd256d 1261
fff8551c
PA
1262 /* The file_names table. */
1263 std::vector<file_entry> file_names;
debd256d
JB
1264
1265 /* The start and end of the statement program following this
6502dd73 1266 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1267 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1268};
c906108c 1269
fff8551c
PA
1270typedef std::unique_ptr<line_header> line_header_up;
1271
8c43009f
PA
1272const char *
1273file_entry::include_dir (const line_header *lh) const
1274{
ecfb656c 1275 return lh->include_dir_at (d_index);
8c43009f
PA
1276}
1277
c906108c 1278/* When we construct a partial symbol table entry we only
0963b4bd 1279 need this much information. */
c906108c
SS
1280struct partial_die_info
1281 {
72bf9492 1282 /* Offset of this DIE. */
9c541725 1283 sect_offset sect_off;
72bf9492
DJ
1284
1285 /* DWARF-2 tag for this DIE. */
1286 ENUM_BITFIELD(dwarf_tag) tag : 16;
1287
72bf9492
DJ
1288 /* Assorted flags describing the data found in this DIE. */
1289 unsigned int has_children : 1;
1290 unsigned int is_external : 1;
1291 unsigned int is_declaration : 1;
1292 unsigned int has_type : 1;
1293 unsigned int has_specification : 1;
1294 unsigned int has_pc_info : 1;
481860b3 1295 unsigned int may_be_inlined : 1;
72bf9492 1296
0c1b455e
TT
1297 /* This DIE has been marked DW_AT_main_subprogram. */
1298 unsigned int main_subprogram : 1;
1299
72bf9492
DJ
1300 /* Flag set if the SCOPE field of this structure has been
1301 computed. */
1302 unsigned int scope_set : 1;
1303
fa4028e9
JB
1304 /* Flag set if the DIE has a byte_size attribute. */
1305 unsigned int has_byte_size : 1;
1306
ff908ebf
AW
1307 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1308 unsigned int has_const_value : 1;
1309
98bfdba5
PA
1310 /* Flag set if any of the DIE's children are template arguments. */
1311 unsigned int has_template_arguments : 1;
1312
abc72ce4
DE
1313 /* Flag set if fixup_partial_die has been called on this die. */
1314 unsigned int fixup_called : 1;
1315
36586728
TT
1316 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1317 unsigned int is_dwz : 1;
1318
1319 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1320 unsigned int spec_is_dwz : 1;
1321
72bf9492 1322 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1323 sometimes a default name for unnamed DIEs. */
15d034d0 1324 const char *name;
72bf9492 1325
abc72ce4
DE
1326 /* The linkage name, if present. */
1327 const char *linkage_name;
1328
72bf9492
DJ
1329 /* The scope to prepend to our children. This is generally
1330 allocated on the comp_unit_obstack, so will disappear
1331 when this compilation unit leaves the cache. */
15d034d0 1332 const char *scope;
72bf9492 1333
95554aad
TT
1334 /* Some data associated with the partial DIE. The tag determines
1335 which field is live. */
1336 union
1337 {
1338 /* The location description associated with this DIE, if any. */
1339 struct dwarf_block *locdesc;
1340 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1341 sect_offset sect_off;
95554aad 1342 } d;
72bf9492
DJ
1343
1344 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1345 CORE_ADDR lowpc;
1346 CORE_ADDR highpc;
72bf9492 1347
93311388 1348 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1349 DW_AT_sibling, if any. */
abc72ce4
DE
1350 /* NOTE: This member isn't strictly necessary, read_partial_die could
1351 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1352 const gdb_byte *sibling;
72bf9492
DJ
1353
1354 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1355 DW_AT_specification (or DW_AT_abstract_origin or
1356 DW_AT_extension). */
b64f50a1 1357 sect_offset spec_offset;
72bf9492
DJ
1358
1359 /* Pointers to this DIE's parent, first child, and next sibling,
1360 if any. */
1361 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1362 };
1363
0963b4bd 1364/* This data structure holds the information of an abbrev. */
c906108c
SS
1365struct abbrev_info
1366 {
1367 unsigned int number; /* number identifying abbrev */
1368 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1369 unsigned short has_children; /* boolean */
1370 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1371 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1372 struct abbrev_info *next; /* next in chain */
1373 };
1374
1375struct attr_abbrev
1376 {
9d25dd43
DE
1377 ENUM_BITFIELD(dwarf_attribute) name : 16;
1378 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1379
1380 /* It is valid only if FORM is DW_FORM_implicit_const. */
1381 LONGEST implicit_const;
c906108c
SS
1382 };
1383
433df2d4
DE
1384/* Size of abbrev_table.abbrev_hash_table. */
1385#define ABBREV_HASH_SIZE 121
1386
1387/* Top level data structure to contain an abbreviation table. */
1388
1389struct abbrev_table
1390{
f4dc4d17
DE
1391 /* Where the abbrev table came from.
1392 This is used as a sanity check when the table is used. */
9c541725 1393 sect_offset sect_off;
433df2d4
DE
1394
1395 /* Storage for the abbrev table. */
1396 struct obstack abbrev_obstack;
1397
1398 /* Hash table of abbrevs.
1399 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1400 It could be statically allocated, but the previous code didn't so we
1401 don't either. */
1402 struct abbrev_info **abbrevs;
1403};
1404
0963b4bd 1405/* Attributes have a name and a value. */
b60c80d6
DJ
1406struct attribute
1407 {
9d25dd43 1408 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1409 ENUM_BITFIELD(dwarf_form) form : 15;
1410
1411 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1412 field should be in u.str (existing only for DW_STRING) but it is kept
1413 here for better struct attribute alignment. */
1414 unsigned int string_is_canonical : 1;
1415
b60c80d6
DJ
1416 union
1417 {
15d034d0 1418 const char *str;
b60c80d6 1419 struct dwarf_block *blk;
43bbcdc2
PH
1420 ULONGEST unsnd;
1421 LONGEST snd;
b60c80d6 1422 CORE_ADDR addr;
ac9ec31b 1423 ULONGEST signature;
b60c80d6
DJ
1424 }
1425 u;
1426 };
1427
0963b4bd 1428/* This data structure holds a complete die structure. */
c906108c
SS
1429struct die_info
1430 {
76815b17
DE
1431 /* DWARF-2 tag for this DIE. */
1432 ENUM_BITFIELD(dwarf_tag) tag : 16;
1433
1434 /* Number of attributes */
98bfdba5
PA
1435 unsigned char num_attrs;
1436
1437 /* True if we're presently building the full type name for the
1438 type derived from this DIE. */
1439 unsigned char building_fullname : 1;
76815b17 1440
adde2bff
DE
1441 /* True if this die is in process. PR 16581. */
1442 unsigned char in_process : 1;
1443
76815b17
DE
1444 /* Abbrev number */
1445 unsigned int abbrev;
1446
93311388 1447 /* Offset in .debug_info or .debug_types section. */
9c541725 1448 sect_offset sect_off;
78ba4af6
JB
1449
1450 /* The dies in a compilation unit form an n-ary tree. PARENT
1451 points to this die's parent; CHILD points to the first child of
1452 this node; and all the children of a given node are chained
4950bc1c 1453 together via their SIBLING fields. */
639d11d3
DC
1454 struct die_info *child; /* Its first child, if any. */
1455 struct die_info *sibling; /* Its next sibling, if any. */
1456 struct die_info *parent; /* Its parent, if any. */
c906108c 1457
b60c80d6
DJ
1458 /* An array of attributes, with NUM_ATTRS elements. There may be
1459 zero, but it's not common and zero-sized arrays are not
1460 sufficiently portable C. */
1461 struct attribute attrs[1];
c906108c
SS
1462 };
1463
0963b4bd 1464/* Get at parts of an attribute structure. */
c906108c
SS
1465
1466#define DW_STRING(attr) ((attr)->u.str)
8285870a 1467#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1468#define DW_UNSND(attr) ((attr)->u.unsnd)
1469#define DW_BLOCK(attr) ((attr)->u.blk)
1470#define DW_SND(attr) ((attr)->u.snd)
1471#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1472#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1473
0963b4bd 1474/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1475struct dwarf_block
1476 {
56eb65bd 1477 size_t size;
1d6edc3c
JK
1478
1479 /* Valid only if SIZE is not zero. */
d521ce57 1480 const gdb_byte *data;
c906108c
SS
1481 };
1482
c906108c
SS
1483#ifndef ATTR_ALLOC_CHUNK
1484#define ATTR_ALLOC_CHUNK 4
1485#endif
1486
c906108c
SS
1487/* Allocate fields for structs, unions and enums in this size. */
1488#ifndef DW_FIELD_ALLOC_CHUNK
1489#define DW_FIELD_ALLOC_CHUNK 4
1490#endif
1491
c906108c
SS
1492/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1493 but this would require a corresponding change in unpack_field_as_long
1494 and friends. */
1495static int bits_per_byte = 8;
1496
52059ffd
TT
1497struct nextfield
1498{
1499 struct nextfield *next;
1500 int accessibility;
1501 int virtuality;
1502 struct field field;
1503};
1504
1505struct nextfnfield
1506{
1507 struct nextfnfield *next;
1508 struct fn_field fnfield;
1509};
1510
1511struct fnfieldlist
1512{
1513 const char *name;
1514 int length;
1515 struct nextfnfield *head;
1516};
1517
1518struct typedef_field_list
1519{
1520 struct typedef_field field;
1521 struct typedef_field_list *next;
1522};
1523
c906108c
SS
1524/* The routines that read and process dies for a C struct or C++ class
1525 pass lists of data member fields and lists of member function fields
1526 in an instance of a field_info structure, as defined below. */
1527struct field_info
c5aa993b 1528 {
0963b4bd 1529 /* List of data member and baseclasses fields. */
52059ffd 1530 struct nextfield *fields, *baseclasses;
c906108c 1531
7d0ccb61 1532 /* Number of fields (including baseclasses). */
c5aa993b 1533 int nfields;
c906108c 1534
c5aa993b
JM
1535 /* Number of baseclasses. */
1536 int nbaseclasses;
c906108c 1537
c5aa993b
JM
1538 /* Set if the accesibility of one of the fields is not public. */
1539 int non_public_fields;
c906108c 1540
c5aa993b
JM
1541 /* Member function fieldlist array, contains name of possibly overloaded
1542 member function, number of overloaded member functions and a pointer
1543 to the head of the member function field chain. */
52059ffd 1544 struct fnfieldlist *fnfieldlists;
c906108c 1545
c5aa993b
JM
1546 /* Number of entries in the fnfieldlists array. */
1547 int nfnfields;
98751a41
JK
1548
1549 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1550 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
52059ffd 1551 struct typedef_field_list *typedef_field_list;
98751a41 1552 unsigned typedef_field_list_count;
c5aa993b 1553 };
c906108c 1554
10b3939b
DJ
1555/* One item on the queue of compilation units to read in full symbols
1556 for. */
1557struct dwarf2_queue_item
1558{
1559 struct dwarf2_per_cu_data *per_cu;
95554aad 1560 enum language pretend_language;
10b3939b
DJ
1561 struct dwarf2_queue_item *next;
1562};
1563
1564/* The current queue. */
1565static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1566
ae038cb0
DJ
1567/* Loaded secondary compilation units are kept in memory until they
1568 have not been referenced for the processing of this many
1569 compilation units. Set this to zero to disable caching. Cache
1570 sizes of up to at least twenty will improve startup time for
1571 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1572static int dwarf_max_cache_age = 5;
920d2a44 1573static void
b4f54984
DE
1574show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1575 struct cmd_list_element *c, const char *value)
920d2a44 1576{
3e43a32a 1577 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1578 "DWARF compilation units is %s.\n"),
920d2a44
AC
1579 value);
1580}
4390d890 1581\f
c906108c
SS
1582/* local function prototypes */
1583
a32a8923
DE
1584static const char *get_section_name (const struct dwarf2_section_info *);
1585
1586static const char *get_section_file_name (const struct dwarf2_section_info *);
1587
918dd910
JK
1588static void dwarf2_find_base_address (struct die_info *die,
1589 struct dwarf2_cu *cu);
1590
0018ea6f
DE
1591static struct partial_symtab *create_partial_symtab
1592 (struct dwarf2_per_cu_data *per_cu, const char *name);
1593
f1902523
JK
1594static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1595 const gdb_byte *info_ptr,
1596 struct die_info *type_unit_die,
1597 int has_children, void *data);
1598
c67a9c90 1599static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1600
72bf9492
DJ
1601static void scan_partial_symbols (struct partial_die_info *,
1602 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1603 int, struct dwarf2_cu *);
c906108c 1604
72bf9492
DJ
1605static void add_partial_symbol (struct partial_die_info *,
1606 struct dwarf2_cu *);
63d06c5c 1607
72bf9492
DJ
1608static void add_partial_namespace (struct partial_die_info *pdi,
1609 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1610 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1611
5d7cb8df 1612static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1613 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1614 struct dwarf2_cu *cu);
1615
72bf9492
DJ
1616static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1617 struct dwarf2_cu *cu);
91c24f0a 1618
bc30ff58
JB
1619static void add_partial_subprogram (struct partial_die_info *pdi,
1620 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1621 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1622
257e7a09
YQ
1623static void dwarf2_read_symtab (struct partial_symtab *,
1624 struct objfile *);
c906108c 1625
a14ed312 1626static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1627
433df2d4
DE
1628static struct abbrev_info *abbrev_table_lookup_abbrev
1629 (const struct abbrev_table *, unsigned int);
1630
1631static struct abbrev_table *abbrev_table_read_table
1632 (struct dwarf2_section_info *, sect_offset);
1633
1634static void abbrev_table_free (struct abbrev_table *);
1635
f4dc4d17
DE
1636static void abbrev_table_free_cleanup (void *);
1637
dee91e82
DE
1638static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1639 struct dwarf2_section_info *);
c906108c 1640
f3dd6933 1641static void dwarf2_free_abbrev_table (void *);
c906108c 1642
d521ce57 1643static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1644
dee91e82 1645static struct partial_die_info *load_partial_dies
d521ce57 1646 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1647
d521ce57
TT
1648static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1649 struct partial_die_info *,
1650 struct abbrev_info *,
1651 unsigned int,
1652 const gdb_byte *);
c906108c 1653
36586728 1654static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1655 struct dwarf2_cu *);
72bf9492
DJ
1656
1657static void fixup_partial_die (struct partial_die_info *,
1658 struct dwarf2_cu *);
1659
d521ce57
TT
1660static const gdb_byte *read_attribute (const struct die_reader_specs *,
1661 struct attribute *, struct attr_abbrev *,
1662 const gdb_byte *);
a8329558 1663
a1855c1d 1664static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1665
a1855c1d 1666static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1667
a1855c1d 1668static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1669
a1855c1d 1670static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1671
a1855c1d 1672static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1673
d521ce57 1674static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1675 unsigned int *);
c906108c 1676
d521ce57 1677static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1678
1679static LONGEST read_checked_initial_length_and_offset
d521ce57 1680 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1681 unsigned int *, unsigned int *);
613e1657 1682
d521ce57
TT
1683static LONGEST read_offset (bfd *, const gdb_byte *,
1684 const struct comp_unit_head *,
c764a876
DE
1685 unsigned int *);
1686
d521ce57 1687static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1688
f4dc4d17
DE
1689static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1690 sect_offset);
1691
d521ce57 1692static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1693
d521ce57 1694static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1695
d521ce57
TT
1696static const char *read_indirect_string (bfd *, const gdb_byte *,
1697 const struct comp_unit_head *,
1698 unsigned int *);
4bdf3d34 1699
43988095
JK
1700static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1701 const struct comp_unit_head *,
1702 unsigned int *);
36586728 1703
43988095 1704static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
c906108c 1705
d521ce57 1706static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1707
d521ce57
TT
1708static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1709 const gdb_byte *,
3019eac3
DE
1710 unsigned int *);
1711
d521ce57 1712static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1713 ULONGEST str_index);
3019eac3 1714
e142c38c 1715static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1716
e142c38c
DJ
1717static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1718 struct dwarf2_cu *);
c906108c 1719
348e048f 1720static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1721 unsigned int);
348e048f 1722
7d45c7c3
KB
1723static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1724 struct dwarf2_cu *cu);
1725
05cf31d1
JB
1726static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1727 struct dwarf2_cu *cu);
1728
e142c38c 1729static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1730
e142c38c 1731static struct die_info *die_specification (struct die_info *die,
f2f0e013 1732 struct dwarf2_cu **);
63d06c5c 1733
9c541725 1734static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1735 struct dwarf2_cu *cu);
debd256d 1736
f3f5162e 1737static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1738 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1739 CORE_ADDR, int decode_mapping);
c906108c 1740
4d663531 1741static void dwarf2_start_subfile (const char *, const char *);
c906108c 1742
43f3e411
DE
1743static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1744 const char *, const char *,
1745 CORE_ADDR);
f4dc4d17 1746
a14ed312 1747static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1748 struct dwarf2_cu *);
c906108c 1749
34eaf542
TT
1750static struct symbol *new_symbol_full (struct die_info *, struct type *,
1751 struct dwarf2_cu *, struct symbol *);
1752
ff39bb5e 1753static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1754 struct dwarf2_cu *);
c906108c 1755
ff39bb5e 1756static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1757 struct type *type,
1758 const char *name,
1759 struct obstack *obstack,
12df843f 1760 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1761 const gdb_byte **bytes,
98bfdba5 1762 struct dwarf2_locexpr_baton **baton);
2df3850c 1763
e7c27a73 1764static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1765
b4ba55a1
JB
1766static int need_gnat_info (struct dwarf2_cu *);
1767
3e43a32a
MS
1768static struct type *die_descriptive_type (struct die_info *,
1769 struct dwarf2_cu *);
b4ba55a1
JB
1770
1771static void set_descriptive_type (struct type *, struct die_info *,
1772 struct dwarf2_cu *);
1773
e7c27a73
DJ
1774static struct type *die_containing_type (struct die_info *,
1775 struct dwarf2_cu *);
c906108c 1776
ff39bb5e 1777static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1778 struct dwarf2_cu *);
c906108c 1779
f792889a 1780static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1781
673bfd45
DE
1782static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1783
0d5cff50 1784static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1785
6e70227d 1786static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1787 const char *suffix, int physname,
1788 struct dwarf2_cu *cu);
63d06c5c 1789
e7c27a73 1790static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1791
348e048f
DE
1792static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1793
e7c27a73 1794static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1795
e7c27a73 1796static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1797
96408a79
SA
1798static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1799
71a3c369
TT
1800static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1801
ff013f42
JK
1802static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1803 struct dwarf2_cu *, struct partial_symtab *);
1804
3a2b436a 1805/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1806 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1807enum pc_bounds_kind
1808{
e385593e 1809 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1810 PC_BOUNDS_NOT_PRESENT,
1811
e385593e
JK
1812 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1813 were present but they do not form a valid range of PC addresses. */
1814 PC_BOUNDS_INVALID,
1815
3a2b436a
JK
1816 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1817 PC_BOUNDS_RANGES,
1818
1819 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1820 PC_BOUNDS_HIGH_LOW,
1821};
1822
1823static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1824 CORE_ADDR *, CORE_ADDR *,
1825 struct dwarf2_cu *,
1826 struct partial_symtab *);
c906108c 1827
fae299cd
DC
1828static void get_scope_pc_bounds (struct die_info *,
1829 CORE_ADDR *, CORE_ADDR *,
1830 struct dwarf2_cu *);
1831
801e3a5b
JB
1832static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1833 CORE_ADDR, struct dwarf2_cu *);
1834
a14ed312 1835static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1836 struct dwarf2_cu *);
c906108c 1837
a14ed312 1838static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1839 struct type *, struct dwarf2_cu *);
c906108c 1840
a14ed312 1841static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1842 struct die_info *, struct type *,
e7c27a73 1843 struct dwarf2_cu *);
c906108c 1844
a14ed312 1845static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1846 struct type *,
1847 struct dwarf2_cu *);
c906108c 1848
134d01f1 1849static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1850
e7c27a73 1851static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1852
e7c27a73 1853static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1854
5d7cb8df
JK
1855static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1856
22cee43f
PMR
1857static struct using_direct **using_directives (enum language);
1858
27aa8d6a
SW
1859static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1860
74921315
KS
1861static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1862
f55ee35c
JK
1863static struct type *read_module_type (struct die_info *die,
1864 struct dwarf2_cu *cu);
1865
38d518c9 1866static const char *namespace_name (struct die_info *die,
e142c38c 1867 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1868
134d01f1 1869static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1870
e7c27a73 1871static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1872
6e70227d 1873static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1874 struct dwarf2_cu *);
1875
bf6af496 1876static struct die_info *read_die_and_siblings_1
d521ce57 1877 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1878 struct die_info *);
639d11d3 1879
dee91e82 1880static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1881 const gdb_byte *info_ptr,
1882 const gdb_byte **new_info_ptr,
639d11d3
DC
1883 struct die_info *parent);
1884
d521ce57
TT
1885static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1886 struct die_info **, const gdb_byte *,
1887 int *, int);
3019eac3 1888
d521ce57
TT
1889static const gdb_byte *read_full_die (const struct die_reader_specs *,
1890 struct die_info **, const gdb_byte *,
1891 int *);
93311388 1892
e7c27a73 1893static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1894
15d034d0
TT
1895static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1896 struct obstack *);
71c25dea 1897
15d034d0 1898static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1899
15d034d0 1900static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1901 struct die_info *die,
1902 struct dwarf2_cu *cu);
1903
ca69b9e6
DE
1904static const char *dwarf2_physname (const char *name, struct die_info *die,
1905 struct dwarf2_cu *cu);
1906
e142c38c 1907static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1908 struct dwarf2_cu **);
9219021c 1909
f39c6ffd 1910static const char *dwarf_tag_name (unsigned int);
c906108c 1911
f39c6ffd 1912static const char *dwarf_attr_name (unsigned int);
c906108c 1913
f39c6ffd 1914static const char *dwarf_form_name (unsigned int);
c906108c 1915
a121b7c1 1916static const char *dwarf_bool_name (unsigned int);
c906108c 1917
f39c6ffd 1918static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1919
f9aca02d 1920static struct die_info *sibling_die (struct die_info *);
c906108c 1921
d97bc12b
DE
1922static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1923
1924static void dump_die_for_error (struct die_info *);
1925
1926static void dump_die_1 (struct ui_file *, int level, int max_level,
1927 struct die_info *);
c906108c 1928
d97bc12b 1929/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1930
51545339 1931static void store_in_ref_table (struct die_info *,
10b3939b 1932 struct dwarf2_cu *);
c906108c 1933
ff39bb5e 1934static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1935
ff39bb5e 1936static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1937
348e048f 1938static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1939 const struct attribute *,
348e048f
DE
1940 struct dwarf2_cu **);
1941
10b3939b 1942static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1943 const struct attribute *,
f2f0e013 1944 struct dwarf2_cu **);
c906108c 1945
348e048f 1946static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1947 const struct attribute *,
348e048f
DE
1948 struct dwarf2_cu **);
1949
ac9ec31b
DE
1950static struct type *get_signatured_type (struct die_info *, ULONGEST,
1951 struct dwarf2_cu *);
1952
1953static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1954 const struct attribute *,
ac9ec31b
DE
1955 struct dwarf2_cu *);
1956
e5fe5e75 1957static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1958
52dc124a 1959static void read_signatured_type (struct signatured_type *);
348e048f 1960
63e43d3a
PMR
1961static int attr_to_dynamic_prop (const struct attribute *attr,
1962 struct die_info *die, struct dwarf2_cu *cu,
1963 struct dynamic_prop *prop);
1964
c906108c
SS
1965/* memory allocation interface */
1966
7b5a2f43 1967static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1968
b60c80d6 1969static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1970
43f3e411 1971static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1972
6e5a29e1 1973static int attr_form_is_block (const struct attribute *);
8e19ed76 1974
6e5a29e1 1975static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1976
6e5a29e1 1977static int attr_form_is_constant (const struct attribute *);
3690dd37 1978
6e5a29e1 1979static int attr_form_is_ref (const struct attribute *);
7771576e 1980
8cf6f0b1
TT
1981static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1982 struct dwarf2_loclist_baton *baton,
ff39bb5e 1983 const struct attribute *attr);
8cf6f0b1 1984
ff39bb5e 1985static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1986 struct symbol *sym,
f1e6e072
TT
1987 struct dwarf2_cu *cu,
1988 int is_block);
4c2df51b 1989
d521ce57
TT
1990static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1991 const gdb_byte *info_ptr,
1992 struct abbrev_info *abbrev);
4bb7a0a7 1993
72bf9492
DJ
1994static void free_stack_comp_unit (void *);
1995
72bf9492
DJ
1996static hashval_t partial_die_hash (const void *item);
1997
1998static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1999
ae038cb0 2000static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
9c541725 2001 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 2002
9816fde3 2003static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 2004 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
2005
2006static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
2007 struct die_info *comp_unit_die,
2008 enum language pretend_language);
93311388 2009
68dc6402 2010static void free_heap_comp_unit (void *);
ae038cb0
DJ
2011
2012static void free_cached_comp_units (void *);
2013
2014static void age_cached_comp_units (void);
2015
dee91e82 2016static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 2017
f792889a
DJ
2018static struct type *set_die_type (struct die_info *, struct type *,
2019 struct dwarf2_cu *);
1c379e20 2020
ae038cb0
DJ
2021static void create_all_comp_units (struct objfile *);
2022
0e50663e 2023static int create_all_type_units (struct objfile *);
1fd400ff 2024
95554aad
TT
2025static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2026 enum language);
10b3939b 2027
95554aad
TT
2028static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2029 enum language);
10b3939b 2030
f4dc4d17
DE
2031static void process_full_type_unit (struct dwarf2_per_cu_data *,
2032 enum language);
2033
10b3939b
DJ
2034static void dwarf2_add_dependence (struct dwarf2_cu *,
2035 struct dwarf2_per_cu_data *);
2036
ae038cb0
DJ
2037static void dwarf2_mark (struct dwarf2_cu *);
2038
2039static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2040
b64f50a1 2041static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 2042 struct dwarf2_per_cu_data *);
673bfd45 2043
f792889a 2044static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 2045
9291a0cd
TT
2046static void dwarf2_release_queue (void *dummy);
2047
95554aad
TT
2048static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2049 enum language pretend_language);
2050
a0f42c21 2051static void process_queue (void);
9291a0cd 2052
d721ba37
PA
2053/* The return type of find_file_and_directory. Note, the enclosed
2054 string pointers are only valid while this object is valid. */
2055
2056struct file_and_directory
2057{
2058 /* The filename. This is never NULL. */
2059 const char *name;
2060
2061 /* The compilation directory. NULL if not known. If we needed to
2062 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2063 points directly to the DW_AT_comp_dir string attribute owned by
2064 the obstack that owns the DIE. */
2065 const char *comp_dir;
2066
2067 /* If we needed to build a new string for comp_dir, this is what
2068 owns the storage. */
2069 std::string comp_dir_storage;
2070};
2071
2072static file_and_directory find_file_and_directory (struct die_info *die,
2073 struct dwarf2_cu *cu);
9291a0cd
TT
2074
2075static char *file_full_name (int file, struct line_header *lh,
2076 const char *comp_dir);
2077
43988095
JK
2078/* Expected enum dwarf_unit_type for read_comp_unit_head. */
2079enum class rcuh_kind { COMPILE, TYPE };
2080
d521ce57 2081static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
2082 (struct comp_unit_head *header,
2083 struct dwarf2_section_info *section,
d521ce57 2084 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 2085 rcuh_kind section_kind);
36586728 2086
fd820528 2087static void init_cutu_and_read_dies
f4dc4d17
DE
2088 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2089 int use_existing_cu, int keep,
3019eac3
DE
2090 die_reader_func_ftype *die_reader_func, void *data);
2091
dee91e82
DE
2092static void init_cutu_and_read_dies_simple
2093 (struct dwarf2_per_cu_data *this_cu,
2094 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 2095
673bfd45 2096static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2097
3019eac3
DE
2098static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2099
57d63ce2
DE
2100static struct dwo_unit *lookup_dwo_unit_in_dwp
2101 (struct dwp_file *dwp_file, const char *comp_dir,
2102 ULONGEST signature, int is_debug_types);
a2ce51a0
DE
2103
2104static struct dwp_file *get_dwp_file (void);
2105
3019eac3 2106static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2107 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2108
2109static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2110 (struct signatured_type *, const char *, const char *);
3019eac3 2111
89e63ee4
DE
2112static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2113
3019eac3
DE
2114static void free_dwo_file_cleanup (void *);
2115
95554aad
TT
2116static void process_cu_includes (void);
2117
1b80a9fa 2118static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2119
2120static void free_line_header_voidp (void *arg);
4390d890
DE
2121\f
2122/* Various complaints about symbol reading that don't abort the process. */
2123
2124static void
2125dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2126{
2127 complaint (&symfile_complaints,
2128 _("statement list doesn't fit in .debug_line section"));
2129}
2130
2131static void
2132dwarf2_debug_line_missing_file_complaint (void)
2133{
2134 complaint (&symfile_complaints,
2135 _(".debug_line section has line data without a file"));
2136}
2137
2138static void
2139dwarf2_debug_line_missing_end_sequence_complaint (void)
2140{
2141 complaint (&symfile_complaints,
2142 _(".debug_line section has line "
2143 "program sequence without an end"));
2144}
2145
2146static void
2147dwarf2_complex_location_expr_complaint (void)
2148{
2149 complaint (&symfile_complaints, _("location expression too complex"));
2150}
2151
2152static void
2153dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2154 int arg3)
2155{
2156 complaint (&symfile_complaints,
2157 _("const value length mismatch for '%s', got %d, expected %d"),
2158 arg1, arg2, arg3);
2159}
2160
2161static void
2162dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2163{
2164 complaint (&symfile_complaints,
2165 _("debug info runs off end of %s section"
2166 " [in module %s]"),
a32a8923
DE
2167 get_section_name (section),
2168 get_section_file_name (section));
4390d890 2169}
1b80a9fa 2170
4390d890
DE
2171static void
2172dwarf2_macro_malformed_definition_complaint (const char *arg1)
2173{
2174 complaint (&symfile_complaints,
2175 _("macro debug info contains a "
2176 "malformed macro definition:\n`%s'"),
2177 arg1);
2178}
2179
2180static void
2181dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2182{
2183 complaint (&symfile_complaints,
2184 _("invalid attribute class or form for '%s' in '%s'"),
2185 arg1, arg2);
2186}
527f3840
JK
2187
2188/* Hash function for line_header_hash. */
2189
2190static hashval_t
2191line_header_hash (const struct line_header *ofs)
2192{
9c541725 2193 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2194}
2195
2196/* Hash function for htab_create_alloc_ex for line_header_hash. */
2197
2198static hashval_t
2199line_header_hash_voidp (const void *item)
2200{
9a3c8263 2201 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2202
2203 return line_header_hash (ofs);
2204}
2205
2206/* Equality function for line_header_hash. */
2207
2208static int
2209line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2210{
9a3c8263
SM
2211 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2212 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2213
9c541725 2214 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2215 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2216}
2217
4390d890 2218\f
9291a0cd 2219
31aa7e4e
JB
2220/* Read the given attribute value as an address, taking the attribute's
2221 form into account. */
2222
2223static CORE_ADDR
2224attr_value_as_address (struct attribute *attr)
2225{
2226 CORE_ADDR addr;
2227
2228 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2229 {
2230 /* Aside from a few clearly defined exceptions, attributes that
2231 contain an address must always be in DW_FORM_addr form.
2232 Unfortunately, some compilers happen to be violating this
2233 requirement by encoding addresses using other forms, such
2234 as DW_FORM_data4 for example. For those broken compilers,
2235 we try to do our best, without any guarantee of success,
2236 to interpret the address correctly. It would also be nice
2237 to generate a complaint, but that would require us to maintain
2238 a list of legitimate cases where a non-address form is allowed,
2239 as well as update callers to pass in at least the CU's DWARF
2240 version. This is more overhead than what we're willing to
2241 expand for a pretty rare case. */
2242 addr = DW_UNSND (attr);
2243 }
2244 else
2245 addr = DW_ADDR (attr);
2246
2247 return addr;
2248}
2249
9291a0cd
TT
2250/* The suffix for an index file. */
2251#define INDEX_SUFFIX ".gdb-index"
2252
330cdd98
PA
2253/* See declaration. */
2254
2255dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2256 const dwarf2_debug_sections *names)
2257 : objfile (objfile_)
2258{
2259 if (names == NULL)
2260 names = &dwarf2_elf_names;
2261
2262 bfd *obfd = objfile->obfd;
2263
2264 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2265 locate_sections (obfd, sec, *names);
2266}
2267
2268dwarf2_per_objfile::~dwarf2_per_objfile ()
2269{
2270 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2271 free_cached_comp_units ();
2272
2273 if (quick_file_names_table)
2274 htab_delete (quick_file_names_table);
2275
2276 if (line_header_hash)
2277 htab_delete (line_header_hash);
2278
2279 /* Everything else should be on the objfile obstack. */
2280}
2281
2282/* See declaration. */
2283
2284void
2285dwarf2_per_objfile::free_cached_comp_units ()
2286{
2287 dwarf2_per_cu_data *per_cu = read_in_chain;
2288 dwarf2_per_cu_data **last_chain = &read_in_chain;
2289 while (per_cu != NULL)
2290 {
2291 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2292
2293 free_heap_comp_unit (per_cu->cu);
2294 *last_chain = next_cu;
2295 per_cu = next_cu;
2296 }
2297}
2298
c906108c 2299/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2300 information and return true if we have enough to do something.
2301 NAMES points to the dwarf2 section names, or is NULL if the standard
2302 ELF names are used. */
c906108c
SS
2303
2304int
251d32d9
TG
2305dwarf2_has_info (struct objfile *objfile,
2306 const struct dwarf2_debug_sections *names)
c906108c 2307{
9a3c8263
SM
2308 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2309 objfile_data (objfile, dwarf2_objfile_data_key));
be391dca
TT
2310 if (!dwarf2_per_objfile)
2311 {
2312 /* Initialize per-objfile state. */
2313 struct dwarf2_per_objfile *data
8d749320 2314 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
9a619af0 2315
330cdd98
PA
2316 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2317 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
be391dca 2318 }
73869dc2 2319 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2320 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2321 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2322 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2323}
2324
2325/* Return the containing section of virtual section SECTION. */
2326
2327static struct dwarf2_section_info *
2328get_containing_section (const struct dwarf2_section_info *section)
2329{
2330 gdb_assert (section->is_virtual);
2331 return section->s.containing_section;
c906108c
SS
2332}
2333
a32a8923
DE
2334/* Return the bfd owner of SECTION. */
2335
2336static struct bfd *
2337get_section_bfd_owner (const struct dwarf2_section_info *section)
2338{
73869dc2
DE
2339 if (section->is_virtual)
2340 {
2341 section = get_containing_section (section);
2342 gdb_assert (!section->is_virtual);
2343 }
049412e3 2344 return section->s.section->owner;
a32a8923
DE
2345}
2346
2347/* Return the bfd section of SECTION.
2348 Returns NULL if the section is not present. */
2349
2350static asection *
2351get_section_bfd_section (const struct dwarf2_section_info *section)
2352{
73869dc2
DE
2353 if (section->is_virtual)
2354 {
2355 section = get_containing_section (section);
2356 gdb_assert (!section->is_virtual);
2357 }
049412e3 2358 return section->s.section;
a32a8923
DE
2359}
2360
2361/* Return the name of SECTION. */
2362
2363static const char *
2364get_section_name (const struct dwarf2_section_info *section)
2365{
2366 asection *sectp = get_section_bfd_section (section);
2367
2368 gdb_assert (sectp != NULL);
2369 return bfd_section_name (get_section_bfd_owner (section), sectp);
2370}
2371
2372/* Return the name of the file SECTION is in. */
2373
2374static const char *
2375get_section_file_name (const struct dwarf2_section_info *section)
2376{
2377 bfd *abfd = get_section_bfd_owner (section);
2378
2379 return bfd_get_filename (abfd);
2380}
2381
2382/* Return the id of SECTION.
2383 Returns 0 if SECTION doesn't exist. */
2384
2385static int
2386get_section_id (const struct dwarf2_section_info *section)
2387{
2388 asection *sectp = get_section_bfd_section (section);
2389
2390 if (sectp == NULL)
2391 return 0;
2392 return sectp->id;
2393}
2394
2395/* Return the flags of SECTION.
73869dc2 2396 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2397
2398static int
2399get_section_flags (const struct dwarf2_section_info *section)
2400{
2401 asection *sectp = get_section_bfd_section (section);
2402
2403 gdb_assert (sectp != NULL);
2404 return bfd_get_section_flags (sectp->owner, sectp);
2405}
2406
251d32d9
TG
2407/* When loading sections, we look either for uncompressed section or for
2408 compressed section names. */
233a11ab
CS
2409
2410static int
251d32d9
TG
2411section_is_p (const char *section_name,
2412 const struct dwarf2_section_names *names)
233a11ab 2413{
251d32d9
TG
2414 if (names->normal != NULL
2415 && strcmp (section_name, names->normal) == 0)
2416 return 1;
2417 if (names->compressed != NULL
2418 && strcmp (section_name, names->compressed) == 0)
2419 return 1;
2420 return 0;
233a11ab
CS
2421}
2422
330cdd98 2423/* See declaration. */
c906108c 2424
330cdd98
PA
2425void
2426dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2427 const dwarf2_debug_sections &names)
c906108c 2428{
dc7650b8 2429 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2430
dc7650b8
JK
2431 if ((aflag & SEC_HAS_CONTENTS) == 0)
2432 {
2433 }
330cdd98 2434 else if (section_is_p (sectp->name, &names.info))
c906108c 2435 {
330cdd98
PA
2436 this->info.s.section = sectp;
2437 this->info.size = bfd_get_section_size (sectp);
c906108c 2438 }
330cdd98 2439 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2440 {
330cdd98
PA
2441 this->abbrev.s.section = sectp;
2442 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2443 }
330cdd98 2444 else if (section_is_p (sectp->name, &names.line))
c906108c 2445 {
330cdd98
PA
2446 this->line.s.section = sectp;
2447 this->line.size = bfd_get_section_size (sectp);
c906108c 2448 }
330cdd98 2449 else if (section_is_p (sectp->name, &names.loc))
c906108c 2450 {
330cdd98
PA
2451 this->loc.s.section = sectp;
2452 this->loc.size = bfd_get_section_size (sectp);
c906108c 2453 }
330cdd98 2454 else if (section_is_p (sectp->name, &names.loclists))
43988095 2455 {
330cdd98
PA
2456 this->loclists.s.section = sectp;
2457 this->loclists.size = bfd_get_section_size (sectp);
43988095 2458 }
330cdd98 2459 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2460 {
330cdd98
PA
2461 this->macinfo.s.section = sectp;
2462 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2463 }
330cdd98 2464 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2465 {
330cdd98
PA
2466 this->macro.s.section = sectp;
2467 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2468 }
330cdd98 2469 else if (section_is_p (sectp->name, &names.str))
c906108c 2470 {
330cdd98
PA
2471 this->str.s.section = sectp;
2472 this->str.size = bfd_get_section_size (sectp);
c906108c 2473 }
330cdd98 2474 else if (section_is_p (sectp->name, &names.line_str))
43988095 2475 {
330cdd98
PA
2476 this->line_str.s.section = sectp;
2477 this->line_str.size = bfd_get_section_size (sectp);
43988095 2478 }
330cdd98 2479 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2480 {
330cdd98
PA
2481 this->addr.s.section = sectp;
2482 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2483 }
330cdd98 2484 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2485 {
330cdd98
PA
2486 this->frame.s.section = sectp;
2487 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2488 }
330cdd98 2489 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2490 {
330cdd98
PA
2491 this->eh_frame.s.section = sectp;
2492 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2493 }
330cdd98 2494 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2495 {
330cdd98
PA
2496 this->ranges.s.section = sectp;
2497 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2498 }
330cdd98 2499 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2500 {
330cdd98
PA
2501 this->rnglists.s.section = sectp;
2502 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2503 }
330cdd98 2504 else if (section_is_p (sectp->name, &names.types))
348e048f 2505 {
8b70b953
TT
2506 struct dwarf2_section_info type_section;
2507
2508 memset (&type_section, 0, sizeof (type_section));
049412e3 2509 type_section.s.section = sectp;
8b70b953
TT
2510 type_section.size = bfd_get_section_size (sectp);
2511
330cdd98 2512 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2513 &type_section);
348e048f 2514 }
330cdd98 2515 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2516 {
330cdd98
PA
2517 this->gdb_index.s.section = sectp;
2518 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2519 }
dce234bc 2520
b4e1fd61 2521 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2522 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2523 this->has_section_at_zero = true;
c906108c
SS
2524}
2525
fceca515
DE
2526/* A helper function that decides whether a section is empty,
2527 or not present. */
9e0ac564
TT
2528
2529static int
19ac8c2e 2530dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2531{
73869dc2
DE
2532 if (section->is_virtual)
2533 return section->size == 0;
049412e3 2534 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2535}
2536
3019eac3
DE
2537/* Read the contents of the section INFO.
2538 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2539 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2540 of the DWO file.
dce234bc 2541 If the section is compressed, uncompress it before returning. */
c906108c 2542
dce234bc
PP
2543static void
2544dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2545{
a32a8923 2546 asection *sectp;
3019eac3 2547 bfd *abfd;
dce234bc 2548 gdb_byte *buf, *retbuf;
c906108c 2549
be391dca
TT
2550 if (info->readin)
2551 return;
dce234bc 2552 info->buffer = NULL;
be391dca 2553 info->readin = 1;
188dd5d6 2554
9e0ac564 2555 if (dwarf2_section_empty_p (info))
dce234bc 2556 return;
c906108c 2557
a32a8923 2558 sectp = get_section_bfd_section (info);
3019eac3 2559
73869dc2
DE
2560 /* If this is a virtual section we need to read in the real one first. */
2561 if (info->is_virtual)
2562 {
2563 struct dwarf2_section_info *containing_section =
2564 get_containing_section (info);
2565
2566 gdb_assert (sectp != NULL);
2567 if ((sectp->flags & SEC_RELOC) != 0)
2568 {
2569 error (_("Dwarf Error: DWP format V2 with relocations is not"
2570 " supported in section %s [in module %s]"),
2571 get_section_name (info), get_section_file_name (info));
2572 }
2573 dwarf2_read_section (objfile, containing_section);
2574 /* Other code should have already caught virtual sections that don't
2575 fit. */
2576 gdb_assert (info->virtual_offset + info->size
2577 <= containing_section->size);
2578 /* If the real section is empty or there was a problem reading the
2579 section we shouldn't get here. */
2580 gdb_assert (containing_section->buffer != NULL);
2581 info->buffer = containing_section->buffer + info->virtual_offset;
2582 return;
2583 }
2584
4bf44c1c
TT
2585 /* If the section has relocations, we must read it ourselves.
2586 Otherwise we attach it to the BFD. */
2587 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2588 {
d521ce57 2589 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2590 return;
dce234bc 2591 }
dce234bc 2592
224c3ddb 2593 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2594 info->buffer = buf;
dce234bc
PP
2595
2596 /* When debugging .o files, we may need to apply relocations; see
2597 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2598 We never compress sections in .o files, so we only need to
2599 try this when the section is not compressed. */
ac8035ab 2600 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2601 if (retbuf != NULL)
2602 {
2603 info->buffer = retbuf;
2604 return;
2605 }
2606
a32a8923
DE
2607 abfd = get_section_bfd_owner (info);
2608 gdb_assert (abfd != NULL);
2609
dce234bc
PP
2610 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2611 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2612 {
2613 error (_("Dwarf Error: Can't read DWARF data"
2614 " in section %s [in module %s]"),
2615 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2616 }
dce234bc
PP
2617}
2618
9e0ac564
TT
2619/* A helper function that returns the size of a section in a safe way.
2620 If you are positive that the section has been read before using the
2621 size, then it is safe to refer to the dwarf2_section_info object's
2622 "size" field directly. In other cases, you must call this
2623 function, because for compressed sections the size field is not set
2624 correctly until the section has been read. */
2625
2626static bfd_size_type
2627dwarf2_section_size (struct objfile *objfile,
2628 struct dwarf2_section_info *info)
2629{
2630 if (!info->readin)
2631 dwarf2_read_section (objfile, info);
2632 return info->size;
2633}
2634
dce234bc 2635/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2636 SECTION_NAME. */
af34e669 2637
dce234bc 2638void
3017a003
TG
2639dwarf2_get_section_info (struct objfile *objfile,
2640 enum dwarf2_section_enum sect,
d521ce57 2641 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2642 bfd_size_type *sizep)
2643{
2644 struct dwarf2_per_objfile *data
9a3c8263
SM
2645 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2646 dwarf2_objfile_data_key);
dce234bc 2647 struct dwarf2_section_info *info;
a3b2a86b
TT
2648
2649 /* We may see an objfile without any DWARF, in which case we just
2650 return nothing. */
2651 if (data == NULL)
2652 {
2653 *sectp = NULL;
2654 *bufp = NULL;
2655 *sizep = 0;
2656 return;
2657 }
3017a003
TG
2658 switch (sect)
2659 {
2660 case DWARF2_DEBUG_FRAME:
2661 info = &data->frame;
2662 break;
2663 case DWARF2_EH_FRAME:
2664 info = &data->eh_frame;
2665 break;
2666 default:
2667 gdb_assert_not_reached ("unexpected section");
2668 }
dce234bc 2669
9e0ac564 2670 dwarf2_read_section (objfile, info);
dce234bc 2671
a32a8923 2672 *sectp = get_section_bfd_section (info);
dce234bc
PP
2673 *bufp = info->buffer;
2674 *sizep = info->size;
2675}
2676
36586728
TT
2677/* A helper function to find the sections for a .dwz file. */
2678
2679static void
2680locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2681{
9a3c8263 2682 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2683
2684 /* Note that we only support the standard ELF names, because .dwz
2685 is ELF-only (at the time of writing). */
2686 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2687 {
049412e3 2688 dwz_file->abbrev.s.section = sectp;
36586728
TT
2689 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2690 }
2691 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2692 {
049412e3 2693 dwz_file->info.s.section = sectp;
36586728
TT
2694 dwz_file->info.size = bfd_get_section_size (sectp);
2695 }
2696 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2697 {
049412e3 2698 dwz_file->str.s.section = sectp;
36586728
TT
2699 dwz_file->str.size = bfd_get_section_size (sectp);
2700 }
2701 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2702 {
049412e3 2703 dwz_file->line.s.section = sectp;
36586728
TT
2704 dwz_file->line.size = bfd_get_section_size (sectp);
2705 }
2706 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2707 {
049412e3 2708 dwz_file->macro.s.section = sectp;
36586728
TT
2709 dwz_file->macro.size = bfd_get_section_size (sectp);
2710 }
2ec9a5e0
TT
2711 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2712 {
049412e3 2713 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2714 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2715 }
36586728
TT
2716}
2717
4db1a1dc
TT
2718/* Open the separate '.dwz' debug file, if needed. Return NULL if
2719 there is no .gnu_debugaltlink section in the file. Error if there
2720 is such a section but the file cannot be found. */
36586728
TT
2721
2722static struct dwz_file *
2723dwarf2_get_dwz_file (void)
2724{
36586728
TT
2725 const char *filename;
2726 struct dwz_file *result;
acd13123 2727 bfd_size_type buildid_len_arg;
dc294be5
TT
2728 size_t buildid_len;
2729 bfd_byte *buildid;
36586728
TT
2730
2731 if (dwarf2_per_objfile->dwz_file != NULL)
2732 return dwarf2_per_objfile->dwz_file;
2733
4db1a1dc 2734 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2735 gdb::unique_xmalloc_ptr<char> data
2736 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2737 &buildid_len_arg, &buildid));
4db1a1dc
TT
2738 if (data == NULL)
2739 {
2740 if (bfd_get_error () == bfd_error_no_error)
2741 return NULL;
2742 error (_("could not read '.gnu_debugaltlink' section: %s"),
2743 bfd_errmsg (bfd_get_error ()));
2744 }
791afaa2
TT
2745
2746 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2747
acd13123
TT
2748 buildid_len = (size_t) buildid_len_arg;
2749
791afaa2 2750 filename = data.get ();
d721ba37
PA
2751
2752 std::string abs_storage;
36586728
TT
2753 if (!IS_ABSOLUTE_PATH (filename))
2754 {
14278e1f
TT
2755 gdb::unique_xmalloc_ptr<char> abs
2756 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2757
14278e1f 2758 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2759 filename = abs_storage.c_str ();
36586728
TT
2760 }
2761
dc294be5
TT
2762 /* First try the file name given in the section. If that doesn't
2763 work, try to use the build-id instead. */
192b62ce 2764 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2765 if (dwz_bfd != NULL)
36586728 2766 {
192b62ce
TT
2767 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2768 dwz_bfd.release ();
36586728
TT
2769 }
2770
dc294be5
TT
2771 if (dwz_bfd == NULL)
2772 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2773
2774 if (dwz_bfd == NULL)
2775 error (_("could not find '.gnu_debugaltlink' file for %s"),
2776 objfile_name (dwarf2_per_objfile->objfile));
2777
36586728
TT
2778 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2779 struct dwz_file);
192b62ce 2780 result->dwz_bfd = dwz_bfd.release ();
36586728 2781
192b62ce 2782 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728 2783
192b62ce 2784 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2785 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2786 return result;
2787}
9291a0cd 2788\f
7b9f3c50
DE
2789/* DWARF quick_symbols_functions support. */
2790
2791/* TUs can share .debug_line entries, and there can be a lot more TUs than
2792 unique line tables, so we maintain a separate table of all .debug_line
2793 derived entries to support the sharing.
2794 All the quick functions need is the list of file names. We discard the
2795 line_header when we're done and don't need to record it here. */
2796struct quick_file_names
2797{
094b34ac
DE
2798 /* The data used to construct the hash key. */
2799 struct stmt_list_hash hash;
7b9f3c50
DE
2800
2801 /* The number of entries in file_names, real_names. */
2802 unsigned int num_file_names;
2803
2804 /* The file names from the line table, after being run through
2805 file_full_name. */
2806 const char **file_names;
2807
2808 /* The file names from the line table after being run through
2809 gdb_realpath. These are computed lazily. */
2810 const char **real_names;
2811};
2812
2813/* When using the index (and thus not using psymtabs), each CU has an
2814 object of this type. This is used to hold information needed by
2815 the various "quick" methods. */
2816struct dwarf2_per_cu_quick_data
2817{
2818 /* The file table. This can be NULL if there was no file table
2819 or it's currently not read in.
2820 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2821 struct quick_file_names *file_names;
2822
2823 /* The corresponding symbol table. This is NULL if symbols for this
2824 CU have not yet been read. */
43f3e411 2825 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2826
2827 /* A temporary mark bit used when iterating over all CUs in
2828 expand_symtabs_matching. */
2829 unsigned int mark : 1;
2830
2831 /* True if we've tried to read the file table and found there isn't one.
2832 There will be no point in trying to read it again next time. */
2833 unsigned int no_file_data : 1;
2834};
2835
094b34ac
DE
2836/* Utility hash function for a stmt_list_hash. */
2837
2838static hashval_t
2839hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2840{
2841 hashval_t v = 0;
2842
2843 if (stmt_list_hash->dwo_unit != NULL)
2844 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2845 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2846 return v;
2847}
2848
2849/* Utility equality function for a stmt_list_hash. */
2850
2851static int
2852eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2853 const struct stmt_list_hash *rhs)
2854{
2855 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2856 return 0;
2857 if (lhs->dwo_unit != NULL
2858 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2859 return 0;
2860
9c541725 2861 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2862}
2863
7b9f3c50
DE
2864/* Hash function for a quick_file_names. */
2865
2866static hashval_t
2867hash_file_name_entry (const void *e)
2868{
9a3c8263
SM
2869 const struct quick_file_names *file_data
2870 = (const struct quick_file_names *) e;
7b9f3c50 2871
094b34ac 2872 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2873}
2874
2875/* Equality function for a quick_file_names. */
2876
2877static int
2878eq_file_name_entry (const void *a, const void *b)
2879{
9a3c8263
SM
2880 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2881 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2882
094b34ac 2883 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2884}
2885
2886/* Delete function for a quick_file_names. */
2887
2888static void
2889delete_file_name_entry (void *e)
2890{
9a3c8263 2891 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2892 int i;
2893
2894 for (i = 0; i < file_data->num_file_names; ++i)
2895 {
2896 xfree ((void*) file_data->file_names[i]);
2897 if (file_data->real_names)
2898 xfree ((void*) file_data->real_names[i]);
2899 }
2900
2901 /* The space for the struct itself lives on objfile_obstack,
2902 so we don't free it here. */
2903}
2904
2905/* Create a quick_file_names hash table. */
2906
2907static htab_t
2908create_quick_file_names_table (unsigned int nr_initial_entries)
2909{
2910 return htab_create_alloc (nr_initial_entries,
2911 hash_file_name_entry, eq_file_name_entry,
2912 delete_file_name_entry, xcalloc, xfree);
2913}
9291a0cd 2914
918dd910
JK
2915/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2916 have to be created afterwards. You should call age_cached_comp_units after
2917 processing PER_CU->CU. dw2_setup must have been already called. */
2918
2919static void
2920load_cu (struct dwarf2_per_cu_data *per_cu)
2921{
3019eac3 2922 if (per_cu->is_debug_types)
e5fe5e75 2923 load_full_type_unit (per_cu);
918dd910 2924 else
95554aad 2925 load_full_comp_unit (per_cu, language_minimal);
918dd910 2926
cc12ce38
DE
2927 if (per_cu->cu == NULL)
2928 return; /* Dummy CU. */
2dc860c0
DE
2929
2930 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2931}
2932
a0f42c21 2933/* Read in the symbols for PER_CU. */
2fdf6df6 2934
9291a0cd 2935static void
a0f42c21 2936dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2937{
2938 struct cleanup *back_to;
2939
f4dc4d17
DE
2940 /* Skip type_unit_groups, reading the type units they contain
2941 is handled elsewhere. */
2942 if (IS_TYPE_UNIT_GROUP (per_cu))
2943 return;
2944
9291a0cd
TT
2945 back_to = make_cleanup (dwarf2_release_queue, NULL);
2946
95554aad 2947 if (dwarf2_per_objfile->using_index
43f3e411 2948 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2949 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2950 {
2951 queue_comp_unit (per_cu, language_minimal);
2952 load_cu (per_cu);
89e63ee4
DE
2953
2954 /* If we just loaded a CU from a DWO, and we're working with an index
2955 that may badly handle TUs, load all the TUs in that DWO as well.
2956 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2957 if (!per_cu->is_debug_types
cc12ce38 2958 && per_cu->cu != NULL
89e63ee4
DE
2959 && per_cu->cu->dwo_unit != NULL
2960 && dwarf2_per_objfile->index_table != NULL
2961 && dwarf2_per_objfile->index_table->version <= 7
2962 /* DWP files aren't supported yet. */
2963 && get_dwp_file () == NULL)
2964 queue_and_load_all_dwo_tus (per_cu);
95554aad 2965 }
9291a0cd 2966
a0f42c21 2967 process_queue ();
9291a0cd
TT
2968
2969 /* Age the cache, releasing compilation units that have not
2970 been used recently. */
2971 age_cached_comp_units ();
2972
2973 do_cleanups (back_to);
2974}
2975
2976/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2977 the objfile from which this CU came. Returns the resulting symbol
2978 table. */
2fdf6df6 2979
43f3e411 2980static struct compunit_symtab *
a0f42c21 2981dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2982{
95554aad 2983 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2984 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
2985 {
2986 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
c83dd867 2987 scoped_restore decrementer = increment_reading_symtab ();
a0f42c21 2988 dw2_do_instantiate_symtab (per_cu);
95554aad 2989 process_cu_includes ();
9291a0cd
TT
2990 do_cleanups (back_to);
2991 }
f194fefb 2992
43f3e411 2993 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2994}
2995
8832e7e3 2996/* Return the CU/TU given its index.
f4dc4d17
DE
2997
2998 This is intended for loops like:
2999
3000 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3001 + dwarf2_per_objfile->n_type_units); ++i)
3002 {
8832e7e3 3003 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
3004
3005 ...;
3006 }
3007*/
2fdf6df6 3008
1fd400ff 3009static struct dwarf2_per_cu_data *
8832e7e3 3010dw2_get_cutu (int index)
1fd400ff
TT
3011{
3012 if (index >= dwarf2_per_objfile->n_comp_units)
3013 {
f4dc4d17 3014 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
3015 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3016 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
3017 }
3018
3019 return dwarf2_per_objfile->all_comp_units[index];
3020}
3021
8832e7e3
DE
3022/* Return the CU given its index.
3023 This differs from dw2_get_cutu in that it's for when you know INDEX
3024 refers to a CU. */
f4dc4d17
DE
3025
3026static struct dwarf2_per_cu_data *
8832e7e3 3027dw2_get_cu (int index)
f4dc4d17 3028{
8832e7e3 3029 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 3030
1fd400ff
TT
3031 return dwarf2_per_objfile->all_comp_units[index];
3032}
3033
2ec9a5e0
TT
3034/* A helper for create_cus_from_index that handles a given list of
3035 CUs. */
2fdf6df6 3036
74a0d9f6 3037static void
2ec9a5e0
TT
3038create_cus_from_index_list (struct objfile *objfile,
3039 const gdb_byte *cu_list, offset_type n_elements,
3040 struct dwarf2_section_info *section,
3041 int is_dwz,
3042 int base_offset)
9291a0cd
TT
3043{
3044 offset_type i;
9291a0cd 3045
2ec9a5e0 3046 for (i = 0; i < n_elements; i += 2)
9291a0cd 3047 {
74a0d9f6 3048 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3049
3050 sect_offset sect_off
3051 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3052 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3053 cu_list += 2 * 8;
3054
9c541725
PA
3055 dwarf2_per_cu_data *the_cu
3056 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3057 struct dwarf2_per_cu_data);
3058 the_cu->sect_off = sect_off;
9291a0cd
TT
3059 the_cu->length = length;
3060 the_cu->objfile = objfile;
8a0459fd 3061 the_cu->section = section;
9291a0cd
TT
3062 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3063 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
3064 the_cu->is_dwz = is_dwz;
3065 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 3066 }
9291a0cd
TT
3067}
3068
2ec9a5e0 3069/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3070 the CU objects for this objfile. */
2ec9a5e0 3071
74a0d9f6 3072static void
2ec9a5e0
TT
3073create_cus_from_index (struct objfile *objfile,
3074 const gdb_byte *cu_list, offset_type cu_list_elements,
3075 const gdb_byte *dwz_list, offset_type dwz_elements)
3076{
3077 struct dwz_file *dwz;
3078
3079 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
3080 dwarf2_per_objfile->all_comp_units =
3081 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3082 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 3083
74a0d9f6
JK
3084 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3085 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
3086
3087 if (dwz_elements == 0)
74a0d9f6 3088 return;
2ec9a5e0
TT
3089
3090 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
3091 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3092 cu_list_elements / 2);
2ec9a5e0
TT
3093}
3094
1fd400ff 3095/* Create the signatured type hash table from the index. */
673bfd45 3096
74a0d9f6 3097static void
673bfd45 3098create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 3099 struct dwarf2_section_info *section,
673bfd45
DE
3100 const gdb_byte *bytes,
3101 offset_type elements)
1fd400ff
TT
3102{
3103 offset_type i;
673bfd45 3104 htab_t sig_types_hash;
1fd400ff 3105
6aa5f3a6
DE
3106 dwarf2_per_objfile->n_type_units
3107 = dwarf2_per_objfile->n_allocated_type_units
3108 = elements / 3;
8d749320
SM
3109 dwarf2_per_objfile->all_type_units =
3110 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 3111
673bfd45 3112 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
3113
3114 for (i = 0; i < elements; i += 3)
3115 {
52dc124a 3116 struct signatured_type *sig_type;
9c541725 3117 ULONGEST signature;
1fd400ff 3118 void **slot;
9c541725 3119 cu_offset type_offset_in_tu;
1fd400ff 3120
74a0d9f6 3121 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3122 sect_offset sect_off
3123 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3124 type_offset_in_tu
3125 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3126 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3127 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3128 bytes += 3 * 8;
3129
52dc124a 3130 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3131 struct signatured_type);
52dc124a 3132 sig_type->signature = signature;
9c541725 3133 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3134 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3135 sig_type->per_cu.section = section;
9c541725 3136 sig_type->per_cu.sect_off = sect_off;
52dc124a
DE
3137 sig_type->per_cu.objfile = objfile;
3138 sig_type->per_cu.v.quick
1fd400ff
TT
3139 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3140 struct dwarf2_per_cu_quick_data);
3141
52dc124a
DE
3142 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3143 *slot = sig_type;
1fd400ff 3144
b4dd5633 3145 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
3146 }
3147
673bfd45 3148 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3149}
3150
9291a0cd
TT
3151/* Read the address map data from the mapped index, and use it to
3152 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3153
9291a0cd
TT
3154static void
3155create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3156{
3e29f34a 3157 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3158 const gdb_byte *iter, *end;
9291a0cd 3159 struct addrmap *mutable_map;
9291a0cd
TT
3160 CORE_ADDR baseaddr;
3161
8268c778
PA
3162 auto_obstack temp_obstack;
3163
9291a0cd
TT
3164 mutable_map = addrmap_create_mutable (&temp_obstack);
3165
3166 iter = index->address_table;
3167 end = iter + index->address_table_size;
3168
3169 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3170
3171 while (iter < end)
3172 {
3173 ULONGEST hi, lo, cu_index;
3174 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3175 iter += 8;
3176 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3177 iter += 8;
3178 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3179 iter += 4;
f652bce2 3180
24a55014 3181 if (lo > hi)
f652bce2 3182 {
24a55014
DE
3183 complaint (&symfile_complaints,
3184 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3185 hex_string (lo), hex_string (hi));
24a55014 3186 continue;
f652bce2 3187 }
24a55014
DE
3188
3189 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3190 {
3191 complaint (&symfile_complaints,
3192 _(".gdb_index address table has invalid CU number %u"),
3193 (unsigned) cu_index);
24a55014 3194 continue;
f652bce2 3195 }
24a55014 3196
3e29f34a
MR
3197 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3198 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3199 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
9291a0cd
TT
3200 }
3201
3202 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3203 &objfile->objfile_obstack);
9291a0cd
TT
3204}
3205
59d7bcaf
JK
3206/* The hash function for strings in the mapped index. This is the same as
3207 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3208 implementation. This is necessary because the hash function is tied to the
3209 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3210 SYMBOL_HASH_NEXT.
3211
3212 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3213
9291a0cd 3214static hashval_t
559a7a62 3215mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3216{
3217 const unsigned char *str = (const unsigned char *) p;
3218 hashval_t r = 0;
3219 unsigned char c;
3220
3221 while ((c = *str++) != 0)
559a7a62
JK
3222 {
3223 if (index_version >= 5)
3224 c = tolower (c);
3225 r = r * 67 + c - 113;
3226 }
9291a0cd
TT
3227
3228 return r;
3229}
3230
3231/* Find a slot in the mapped index INDEX for the object named NAME.
3232 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3233 constant pool and return true. If NAME cannot be found, return
3234 false. */
2fdf6df6 3235
109483d9 3236static bool
9291a0cd
TT
3237find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3238 offset_type **vec_out)
3239{
0cf03b49 3240 offset_type hash;
9291a0cd 3241 offset_type slot, step;
559a7a62 3242 int (*cmp) (const char *, const char *);
9291a0cd 3243
791afaa2 3244 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3245 if (current_language->la_language == language_cplus
45280282
IB
3246 || current_language->la_language == language_fortran
3247 || current_language->la_language == language_d)
0cf03b49
JK
3248 {
3249 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3250 not contain any. */
a8719064 3251
72998fb3 3252 if (strchr (name, '(') != NULL)
0cf03b49 3253 {
109483d9 3254 without_params = cp_remove_params (name);
0cf03b49 3255
72998fb3 3256 if (without_params != NULL)
791afaa2 3257 name = without_params.get ();
0cf03b49
JK
3258 }
3259 }
3260
559a7a62 3261 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3262 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3263 simulate our NAME being searched is also lowercased. */
3264 hash = mapped_index_string_hash ((index->version == 4
3265 && case_sensitivity == case_sensitive_off
3266 ? 5 : index->version),
3267 name);
3268
3876f04e
DE
3269 slot = hash & (index->symbol_table_slots - 1);
3270 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 3271 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3272
3273 for (;;)
3274 {
3275 /* Convert a slot number to an offset into the table. */
3276 offset_type i = 2 * slot;
3277 const char *str;
3876f04e 3278 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
109483d9 3279 return false;
9291a0cd 3280
3876f04e 3281 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 3282 if (!cmp (name, str))
9291a0cd
TT
3283 {
3284 *vec_out = (offset_type *) (index->constant_pool
3876f04e 3285 + MAYBE_SWAP (index->symbol_table[i + 1]));
109483d9 3286 return true;
9291a0cd
TT
3287 }
3288
3876f04e 3289 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
3290 }
3291}
3292
2ec9a5e0
TT
3293/* A helper function that reads the .gdb_index from SECTION and fills
3294 in MAP. FILENAME is the name of the file containing the section;
3295 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3296 ok to use deprecated sections.
3297
3298 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3299 out parameters that are filled in with information about the CU and
3300 TU lists in the section.
3301
3302 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3303
9291a0cd 3304static int
2ec9a5e0
TT
3305read_index_from_section (struct objfile *objfile,
3306 const char *filename,
3307 int deprecated_ok,
3308 struct dwarf2_section_info *section,
3309 struct mapped_index *map,
3310 const gdb_byte **cu_list,
3311 offset_type *cu_list_elements,
3312 const gdb_byte **types_list,
3313 offset_type *types_list_elements)
9291a0cd 3314{
948f8e3d 3315 const gdb_byte *addr;
2ec9a5e0 3316 offset_type version;
b3b272e1 3317 offset_type *metadata;
1fd400ff 3318 int i;
9291a0cd 3319
2ec9a5e0 3320 if (dwarf2_section_empty_p (section))
9291a0cd 3321 return 0;
82430852
JK
3322
3323 /* Older elfutils strip versions could keep the section in the main
3324 executable while splitting it for the separate debug info file. */
a32a8923 3325 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3326 return 0;
3327
2ec9a5e0 3328 dwarf2_read_section (objfile, section);
9291a0cd 3329
2ec9a5e0 3330 addr = section->buffer;
9291a0cd 3331 /* Version check. */
1fd400ff 3332 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3333 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3334 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3335 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3336 indices. */
831adc1f 3337 if (version < 4)
481860b3
GB
3338 {
3339 static int warning_printed = 0;
3340 if (!warning_printed)
3341 {
3342 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3343 filename);
481860b3
GB
3344 warning_printed = 1;
3345 }
3346 return 0;
3347 }
3348 /* Index version 4 uses a different hash function than index version
3349 5 and later.
3350
3351 Versions earlier than 6 did not emit psymbols for inlined
3352 functions. Using these files will cause GDB not to be able to
3353 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3354 indices unless the user has done
3355 "set use-deprecated-index-sections on". */
2ec9a5e0 3356 if (version < 6 && !deprecated_ok)
481860b3
GB
3357 {
3358 static int warning_printed = 0;
3359 if (!warning_printed)
3360 {
e615022a
DE
3361 warning (_("\
3362Skipping deprecated .gdb_index section in %s.\n\
3363Do \"set use-deprecated-index-sections on\" before the file is read\n\
3364to use the section anyway."),
2ec9a5e0 3365 filename);
481860b3
GB
3366 warning_printed = 1;
3367 }
3368 return 0;
3369 }
796a7ff8 3370 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3371 of the TU (for symbols coming from TUs),
3372 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3373 Plus gold-generated indices can have duplicate entries for global symbols,
3374 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3375 These are just performance bugs, and we can't distinguish gdb-generated
3376 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3377
481860b3 3378 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3379 longer backward compatible. */
796a7ff8 3380 if (version > 8)
594e8718 3381 return 0;
9291a0cd 3382
559a7a62 3383 map->version = version;
2ec9a5e0 3384 map->total_size = section->size;
9291a0cd
TT
3385
3386 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3387
3388 i = 0;
2ec9a5e0
TT
3389 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3390 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3391 / 8);
1fd400ff
TT
3392 ++i;
3393
2ec9a5e0
TT
3394 *types_list = addr + MAYBE_SWAP (metadata[i]);
3395 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3396 - MAYBE_SWAP (metadata[i]))
3397 / 8);
987d643c 3398 ++i;
1fd400ff
TT
3399
3400 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3401 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3402 - MAYBE_SWAP (metadata[i]));
3403 ++i;
3404
3876f04e
DE
3405 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3406 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3407 - MAYBE_SWAP (metadata[i]))
3408 / (2 * sizeof (offset_type)));
1fd400ff 3409 ++i;
9291a0cd 3410
f9d83a0b 3411 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3412
2ec9a5e0
TT
3413 return 1;
3414}
3415
3416
3417/* Read the index file. If everything went ok, initialize the "quick"
3418 elements of all the CUs and return 1. Otherwise, return 0. */
3419
3420static int
3421dwarf2_read_index (struct objfile *objfile)
3422{
3423 struct mapped_index local_map, *map;
3424 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3425 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3426 struct dwz_file *dwz;
2ec9a5e0 3427
4262abfb 3428 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3429 use_deprecated_index_sections,
3430 &dwarf2_per_objfile->gdb_index, &local_map,
3431 &cu_list, &cu_list_elements,
3432 &types_list, &types_list_elements))
3433 return 0;
3434
0fefef59 3435 /* Don't use the index if it's empty. */
2ec9a5e0 3436 if (local_map.symbol_table_slots == 0)
0fefef59
DE
3437 return 0;
3438
2ec9a5e0
TT
3439 /* If there is a .dwz file, read it so we can get its CU list as
3440 well. */
4db1a1dc
TT
3441 dwz = dwarf2_get_dwz_file ();
3442 if (dwz != NULL)
2ec9a5e0 3443 {
2ec9a5e0
TT
3444 struct mapped_index dwz_map;
3445 const gdb_byte *dwz_types_ignore;
3446 offset_type dwz_types_elements_ignore;
3447
3448 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3449 1,
3450 &dwz->gdb_index, &dwz_map,
3451 &dwz_list, &dwz_list_elements,
3452 &dwz_types_ignore,
3453 &dwz_types_elements_ignore))
3454 {
3455 warning (_("could not read '.gdb_index' section from %s; skipping"),
3456 bfd_get_filename (dwz->dwz_bfd));
3457 return 0;
3458 }
3459 }
3460
74a0d9f6
JK
3461 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3462 dwz_list_elements);
1fd400ff 3463
8b70b953
TT
3464 if (types_list_elements)
3465 {
3466 struct dwarf2_section_info *section;
3467
3468 /* We can only handle a single .debug_types when we have an
3469 index. */
3470 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3471 return 0;
3472
3473 section = VEC_index (dwarf2_section_info_def,
3474 dwarf2_per_objfile->types, 0);
3475
74a0d9f6
JK
3476 create_signatured_type_table_from_index (objfile, section, types_list,
3477 types_list_elements);
8b70b953 3478 }
9291a0cd 3479
2ec9a5e0
TT
3480 create_addrmap_from_index (objfile, &local_map);
3481
8d749320 3482 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3f563c84 3483 map = new (map) mapped_index ();
2ec9a5e0 3484 *map = local_map;
9291a0cd
TT
3485
3486 dwarf2_per_objfile->index_table = map;
3487 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3488 dwarf2_per_objfile->quick_file_names_table =
3489 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3490
3491 return 1;
3492}
3493
3494/* A helper for the "quick" functions which sets the global
3495 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 3496
9291a0cd
TT
3497static void
3498dw2_setup (struct objfile *objfile)
3499{
9a3c8263
SM
3500 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3501 objfile_data (objfile, dwarf2_objfile_data_key));
9291a0cd
TT
3502 gdb_assert (dwarf2_per_objfile);
3503}
3504
dee91e82 3505/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3506
dee91e82
DE
3507static void
3508dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3509 const gdb_byte *info_ptr,
dee91e82
DE
3510 struct die_info *comp_unit_die,
3511 int has_children,
3512 void *data)
9291a0cd 3513{
dee91e82
DE
3514 struct dwarf2_cu *cu = reader->cu;
3515 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3516 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3517 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3518 struct attribute *attr;
dee91e82 3519 int i;
7b9f3c50
DE
3520 void **slot;
3521 struct quick_file_names *qfn;
9291a0cd 3522
0186c6a7
DE
3523 gdb_assert (! this_cu->is_debug_types);
3524
07261596
TT
3525 /* Our callers never want to match partial units -- instead they
3526 will match the enclosing full CU. */
3527 if (comp_unit_die->tag == DW_TAG_partial_unit)
3528 {
3529 this_cu->v.quick->no_file_data = 1;
3530 return;
3531 }
3532
0186c6a7 3533 lh_cu = this_cu;
7b9f3c50 3534 slot = NULL;
dee91e82 3535
fff8551c 3536 line_header_up lh;
9c541725 3537 sect_offset line_offset {};
fff8551c 3538
dee91e82 3539 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3540 if (attr)
3541 {
7b9f3c50
DE
3542 struct quick_file_names find_entry;
3543
9c541725 3544 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3545
3546 /* We may have already read in this line header (TU line header sharing).
3547 If we have we're done. */
094b34ac 3548 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3549 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3550 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3551 &find_entry, INSERT);
3552 if (*slot != NULL)
3553 {
9a3c8263 3554 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3555 return;
7b9f3c50
DE
3556 }
3557
3019eac3 3558 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3559 }
3560 if (lh == NULL)
3561 {
094b34ac 3562 lh_cu->v.quick->no_file_data = 1;
dee91e82 3563 return;
9291a0cd
TT
3564 }
3565
8d749320 3566 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3567 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3568 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3569 gdb_assert (slot != NULL);
3570 *slot = qfn;
9291a0cd 3571
d721ba37 3572 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3573
fff8551c 3574 qfn->num_file_names = lh->file_names.size ();
8d749320 3575 qfn->file_names =
fff8551c
PA
3576 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3577 for (i = 0; i < lh->file_names.size (); ++i)
3578 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3579 qfn->real_names = NULL;
9291a0cd 3580
094b34ac 3581 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3582}
3583
3584/* A helper for the "quick" functions which attempts to read the line
3585 table for THIS_CU. */
3586
3587static struct quick_file_names *
e4a48d9d 3588dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3589{
0186c6a7
DE
3590 /* This should never be called for TUs. */
3591 gdb_assert (! this_cu->is_debug_types);
3592 /* Nor type unit groups. */
3593 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3594
dee91e82
DE
3595 if (this_cu->v.quick->file_names != NULL)
3596 return this_cu->v.quick->file_names;
3597 /* If we know there is no line data, no point in looking again. */
3598 if (this_cu->v.quick->no_file_data)
3599 return NULL;
3600
0186c6a7 3601 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3602
3603 if (this_cu->v.quick->no_file_data)
3604 return NULL;
3605 return this_cu->v.quick->file_names;
9291a0cd
TT
3606}
3607
3608/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3609 real path for a given file name from the line table. */
2fdf6df6 3610
9291a0cd 3611static const char *
7b9f3c50
DE
3612dw2_get_real_path (struct objfile *objfile,
3613 struct quick_file_names *qfn, int index)
9291a0cd 3614{
7b9f3c50
DE
3615 if (qfn->real_names == NULL)
3616 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3617 qfn->num_file_names, const char *);
9291a0cd 3618
7b9f3c50 3619 if (qfn->real_names[index] == NULL)
14278e1f 3620 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3621
7b9f3c50 3622 return qfn->real_names[index];
9291a0cd
TT
3623}
3624
3625static struct symtab *
3626dw2_find_last_source_symtab (struct objfile *objfile)
3627{
43f3e411 3628 struct compunit_symtab *cust;
9291a0cd 3629 int index;
ae2de4f8 3630
9291a0cd
TT
3631 dw2_setup (objfile);
3632 index = dwarf2_per_objfile->n_comp_units - 1;
43f3e411
DE
3633 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3634 if (cust == NULL)
3635 return NULL;
3636 return compunit_primary_filetab (cust);
9291a0cd
TT
3637}
3638
7b9f3c50
DE
3639/* Traversal function for dw2_forget_cached_source_info. */
3640
3641static int
3642dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3643{
7b9f3c50 3644 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3645
7b9f3c50 3646 if (file_data->real_names)
9291a0cd 3647 {
7b9f3c50 3648 int i;
9291a0cd 3649
7b9f3c50 3650 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3651 {
7b9f3c50
DE
3652 xfree ((void*) file_data->real_names[i]);
3653 file_data->real_names[i] = NULL;
9291a0cd
TT
3654 }
3655 }
7b9f3c50
DE
3656
3657 return 1;
3658}
3659
3660static void
3661dw2_forget_cached_source_info (struct objfile *objfile)
3662{
3663 dw2_setup (objfile);
3664
3665 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3666 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3667}
3668
f8eba3c6
TT
3669/* Helper function for dw2_map_symtabs_matching_filename that expands
3670 the symtabs and calls the iterator. */
3671
3672static int
3673dw2_map_expand_apply (struct objfile *objfile,
3674 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3675 const char *name, const char *real_path,
14bc53a8 3676 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3677{
43f3e411 3678 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3679
3680 /* Don't visit already-expanded CUs. */
43f3e411 3681 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3682 return 0;
3683
3684 /* This may expand more than one symtab, and we want to iterate over
3685 all of them. */
a0f42c21 3686 dw2_instantiate_symtab (per_cu);
f8eba3c6 3687
14bc53a8
PA
3688 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3689 last_made, callback);
f8eba3c6
TT
3690}
3691
3692/* Implementation of the map_symtabs_matching_filename method. */
3693
14bc53a8
PA
3694static bool
3695dw2_map_symtabs_matching_filename
3696 (struct objfile *objfile, const char *name, const char *real_path,
3697 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
3698{
3699 int i;
c011a4f4 3700 const char *name_basename = lbasename (name);
9291a0cd
TT
3701
3702 dw2_setup (objfile);
ae2de4f8 3703
848e3e78
DE
3704 /* The rule is CUs specify all the files, including those used by
3705 any TU, so there's no need to scan TUs here. */
f4dc4d17 3706
848e3e78 3707 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3708 {
3709 int j;
8832e7e3 3710 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3711 struct quick_file_names *file_data;
9291a0cd 3712
3d7bb9d9 3713 /* We only need to look at symtabs not already expanded. */
43f3e411 3714 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3715 continue;
3716
e4a48d9d 3717 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3718 if (file_data == NULL)
9291a0cd
TT
3719 continue;
3720
7b9f3c50 3721 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3722 {
7b9f3c50 3723 const char *this_name = file_data->file_names[j];
da235a7c 3724 const char *this_real_name;
9291a0cd 3725
af529f8f 3726 if (compare_filenames_for_search (this_name, name))
9291a0cd 3727 {
f5b95b50 3728 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3729 callback))
3730 return true;
288e77a7 3731 continue;
4aac40c8 3732 }
9291a0cd 3733
c011a4f4
DE
3734 /* Before we invoke realpath, which can get expensive when many
3735 files are involved, do a quick comparison of the basenames. */
3736 if (! basenames_may_differ
3737 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3738 continue;
3739
da235a7c
JK
3740 this_real_name = dw2_get_real_path (objfile, file_data, j);
3741 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3742 {
da235a7c 3743 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3744 callback))
3745 return true;
288e77a7 3746 continue;
da235a7c 3747 }
9291a0cd 3748
da235a7c
JK
3749 if (real_path != NULL)
3750 {
af529f8f
JK
3751 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3752 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3753 if (this_real_name != NULL
af529f8f 3754 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3755 {
f5b95b50 3756 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3757 callback))
3758 return true;
288e77a7 3759 continue;
9291a0cd
TT
3760 }
3761 }
3762 }
3763 }
3764
14bc53a8 3765 return false;
9291a0cd
TT
3766}
3767
da51c347
DE
3768/* Struct used to manage iterating over all CUs looking for a symbol. */
3769
3770struct dw2_symtab_iterator
9291a0cd 3771{
da51c347
DE
3772 /* The internalized form of .gdb_index. */
3773 struct mapped_index *index;
3774 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3775 int want_specific_block;
3776 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3777 Unused if !WANT_SPECIFIC_BLOCK. */
3778 int block_index;
3779 /* The kind of symbol we're looking for. */
3780 domain_enum domain;
3781 /* The list of CUs from the index entry of the symbol,
3782 or NULL if not found. */
3783 offset_type *vec;
3784 /* The next element in VEC to look at. */
3785 int next;
3786 /* The number of elements in VEC, or zero if there is no match. */
3787 int length;
8943b874
DE
3788 /* Have we seen a global version of the symbol?
3789 If so we can ignore all further global instances.
3790 This is to work around gold/15646, inefficient gold-generated
3791 indices. */
3792 int global_seen;
da51c347 3793};
9291a0cd 3794
da51c347
DE
3795/* Initialize the index symtab iterator ITER.
3796 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3797 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3798
9291a0cd 3799static void
da51c347
DE
3800dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3801 struct mapped_index *index,
3802 int want_specific_block,
3803 int block_index,
3804 domain_enum domain,
3805 const char *name)
3806{
3807 iter->index = index;
3808 iter->want_specific_block = want_specific_block;
3809 iter->block_index = block_index;
3810 iter->domain = domain;
3811 iter->next = 0;
8943b874 3812 iter->global_seen = 0;
da51c347
DE
3813
3814 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3815 iter->length = MAYBE_SWAP (*iter->vec);
3816 else
3817 {
3818 iter->vec = NULL;
3819 iter->length = 0;
3820 }
3821}
3822
3823/* Return the next matching CU or NULL if there are no more. */
3824
3825static struct dwarf2_per_cu_data *
3826dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3827{
3828 for ( ; iter->next < iter->length; ++iter->next)
3829 {
3830 offset_type cu_index_and_attrs =
3831 MAYBE_SWAP (iter->vec[iter->next + 1]);
3832 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3833 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3834 int want_static = iter->block_index != GLOBAL_BLOCK;
3835 /* This value is only valid for index versions >= 7. */
3836 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3837 gdb_index_symbol_kind symbol_kind =
3838 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3839 /* Only check the symbol attributes if they're present.
3840 Indices prior to version 7 don't record them,
3841 and indices >= 7 may elide them for certain symbols
3842 (gold does this). */
3843 int attrs_valid =
3844 (iter->index->version >= 7
3845 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3846
3190f0c6
DE
3847 /* Don't crash on bad data. */
3848 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3849 + dwarf2_per_objfile->n_type_units))
3850 {
3851 complaint (&symfile_complaints,
3852 _(".gdb_index entry has bad CU index"
4262abfb
JK
3853 " [in module %s]"),
3854 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3855 continue;
3856 }
3857
8832e7e3 3858 per_cu = dw2_get_cutu (cu_index);
3190f0c6 3859
da51c347 3860 /* Skip if already read in. */
43f3e411 3861 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3862 continue;
3863
8943b874
DE
3864 /* Check static vs global. */
3865 if (attrs_valid)
3866 {
3867 if (iter->want_specific_block
3868 && want_static != is_static)
3869 continue;
3870 /* Work around gold/15646. */
3871 if (!is_static && iter->global_seen)
3872 continue;
3873 if (!is_static)
3874 iter->global_seen = 1;
3875 }
da51c347
DE
3876
3877 /* Only check the symbol's kind if it has one. */
3878 if (attrs_valid)
3879 {
3880 switch (iter->domain)
3881 {
3882 case VAR_DOMAIN:
3883 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3884 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3885 /* Some types are also in VAR_DOMAIN. */
3886 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3887 continue;
3888 break;
3889 case STRUCT_DOMAIN:
3890 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3891 continue;
3892 break;
3893 case LABEL_DOMAIN:
3894 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3895 continue;
3896 break;
3897 default:
3898 break;
3899 }
3900 }
3901
3902 ++iter->next;
3903 return per_cu;
3904 }
3905
3906 return NULL;
3907}
3908
43f3e411 3909static struct compunit_symtab *
da51c347
DE
3910dw2_lookup_symbol (struct objfile *objfile, int block_index,
3911 const char *name, domain_enum domain)
9291a0cd 3912{
43f3e411 3913 struct compunit_symtab *stab_best = NULL;
156942c7
DE
3914 struct mapped_index *index;
3915
9291a0cd
TT
3916 dw2_setup (objfile);
3917
b5ec771e
PA
3918 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3919
156942c7
DE
3920 index = dwarf2_per_objfile->index_table;
3921
da51c347 3922 /* index is NULL if OBJF_READNOW. */
156942c7 3923 if (index)
9291a0cd 3924 {
da51c347
DE
3925 struct dw2_symtab_iterator iter;
3926 struct dwarf2_per_cu_data *per_cu;
3927
3928 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3929
da51c347 3930 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3931 {
b2e2f908 3932 struct symbol *sym, *with_opaque = NULL;
43f3e411
DE
3933 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3934 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
f194fefb 3935 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3936
b2e2f908
DE
3937 sym = block_find_symbol (block, name, domain,
3938 block_find_non_opaque_type_preferred,
3939 &with_opaque);
3940
da51c347
DE
3941 /* Some caution must be observed with overloaded functions
3942 and methods, since the index will not contain any overload
3943 information (but NAME might contain it). */
da51c347 3944
b2e2f908 3945 if (sym != NULL
b5ec771e 3946 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
b2e2f908
DE
3947 return stab;
3948 if (with_opaque != NULL
b5ec771e 3949 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
b2e2f908 3950 stab_best = stab;
da51c347
DE
3951
3952 /* Keep looking through other CUs. */
9291a0cd
TT
3953 }
3954 }
9291a0cd 3955
da51c347 3956 return stab_best;
9291a0cd
TT
3957}
3958
3959static void
3960dw2_print_stats (struct objfile *objfile)
3961{
e4a48d9d 3962 int i, total, count;
9291a0cd
TT
3963
3964 dw2_setup (objfile);
e4a48d9d 3965 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3966 count = 0;
e4a48d9d 3967 for (i = 0; i < total; ++i)
9291a0cd 3968 {
8832e7e3 3969 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3970
43f3e411 3971 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3972 ++count;
3973 }
e4a48d9d 3974 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3975 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3976}
3977
779bd270
DE
3978/* This dumps minimal information about the index.
3979 It is called via "mt print objfiles".
3980 One use is to verify .gdb_index has been loaded by the
3981 gdb.dwarf2/gdb-index.exp testcase. */
3982
9291a0cd
TT
3983static void
3984dw2_dump (struct objfile *objfile)
3985{
779bd270
DE
3986 dw2_setup (objfile);
3987 gdb_assert (dwarf2_per_objfile->using_index);
3988 printf_filtered (".gdb_index:");
3989 if (dwarf2_per_objfile->index_table != NULL)
3990 {
3991 printf_filtered (" version %d\n",
3992 dwarf2_per_objfile->index_table->version);
3993 }
3994 else
3995 printf_filtered (" faked for \"readnow\"\n");
3996 printf_filtered ("\n");
9291a0cd
TT
3997}
3998
3999static void
3189cb12
DE
4000dw2_relocate (struct objfile *objfile,
4001 const struct section_offsets *new_offsets,
4002 const struct section_offsets *delta)
9291a0cd
TT
4003{
4004 /* There's nothing to relocate here. */
4005}
4006
4007static void
4008dw2_expand_symtabs_for_function (struct objfile *objfile,
4009 const char *func_name)
4010{
da51c347
DE
4011 struct mapped_index *index;
4012
4013 dw2_setup (objfile);
4014
4015 index = dwarf2_per_objfile->index_table;
4016
4017 /* index is NULL if OBJF_READNOW. */
4018 if (index)
4019 {
4020 struct dw2_symtab_iterator iter;
4021 struct dwarf2_per_cu_data *per_cu;
4022
4023 /* Note: It doesn't matter what we pass for block_index here. */
4024 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4025 func_name);
4026
4027 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4028 dw2_instantiate_symtab (per_cu);
4029 }
9291a0cd
TT
4030}
4031
4032static void
4033dw2_expand_all_symtabs (struct objfile *objfile)
4034{
4035 int i;
4036
4037 dw2_setup (objfile);
1fd400ff
TT
4038
4039 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4040 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4041 {
8832e7e3 4042 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4043
a0f42c21 4044 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4045 }
4046}
4047
4048static void
652a8996
JK
4049dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4050 const char *fullname)
9291a0cd
TT
4051{
4052 int i;
4053
4054 dw2_setup (objfile);
d4637a04
DE
4055
4056 /* We don't need to consider type units here.
4057 This is only called for examining code, e.g. expand_line_sal.
4058 There can be an order of magnitude (or more) more type units
4059 than comp units, and we avoid them if we can. */
4060
4061 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4062 {
4063 int j;
8832e7e3 4064 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7b9f3c50 4065 struct quick_file_names *file_data;
9291a0cd 4066
3d7bb9d9 4067 /* We only need to look at symtabs not already expanded. */
43f3e411 4068 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4069 continue;
4070
e4a48d9d 4071 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4072 if (file_data == NULL)
9291a0cd
TT
4073 continue;
4074
7b9f3c50 4075 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4076 {
652a8996
JK
4077 const char *this_fullname = file_data->file_names[j];
4078
4079 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4080 {
a0f42c21 4081 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
4082 break;
4083 }
4084 }
4085 }
4086}
4087
9291a0cd 4088static void
ade7ed9e 4089dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4090 const char * name, domain_enum domain,
ade7ed9e 4091 int global,
40658b94
PH
4092 int (*callback) (struct block *,
4093 struct symbol *, void *),
b5ec771e 4094 void *data, symbol_name_match_type match,
2edb89d3 4095 symbol_compare_ftype *ordered_compare)
9291a0cd 4096{
40658b94 4097 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4098 current language is Ada for a non-Ada objfile using GNU index. As Ada
4099 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4100}
4101
b5ec771e
PA
4102/* Symbol name matcher for .gdb_index names.
4103
4104 Symbol names in .gdb_index have a few particularities:
4105
4106 - There's no indication of which is the language of each symbol.
4107
4108 Since each language has its own symbol name matching algorithm,
4109 and we don't know which language is the right one, we must match
3f563c84
PA
4110 each symbol against all languages. This would be a potential
4111 performance problem if it were not mitigated by the
4112 mapped_index::name_components lookup table, which significantly
4113 reduces the number of times we need to call into this matcher,
4114 making it a non-issue.
b5ec771e
PA
4115
4116 - Symbol names in the index have no overload (parameter)
4117 information. I.e., in C++, "foo(int)" and "foo(long)" both
4118 appear as "foo" in the index, for example.
4119
4120 This means that the lookup names passed to the symbol name
4121 matcher functions must have no parameter information either
4122 because (e.g.) symbol search name "foo" does not match
4123 lookup-name "foo(int)" [while swapping search name for lookup
4124 name would match].
4125*/
4126class gdb_index_symbol_name_matcher
4127{
4128public:
4129 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4130 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4131
4132 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4133 Returns true if any matcher matches. */
4134 bool matches (const char *symbol_name);
4135
4136private:
4137 /* A reference to the lookup name we're matching against. */
4138 const lookup_name_info &m_lookup_name;
4139
4140 /* A vector holding all the different symbol name matchers, for all
4141 languages. */
4142 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4143};
4144
4145gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4146 (const lookup_name_info &lookup_name)
4147 : m_lookup_name (lookup_name)
4148{
4149 /* Prepare the vector of comparison functions upfront, to avoid
4150 doing the same work for each symbol. Care is taken to avoid
4151 matching with the same matcher more than once if/when multiple
4152 languages use the same matcher function. */
4153 auto &matchers = m_symbol_name_matcher_funcs;
4154 matchers.reserve (nr_languages);
4155
4156 matchers.push_back (default_symbol_name_matcher);
4157
4158 for (int i = 0; i < nr_languages; i++)
4159 {
4160 const language_defn *lang = language_def ((enum language) i);
4161 if (lang->la_get_symbol_name_matcher != NULL)
4162 {
4163 symbol_name_matcher_ftype *name_matcher
4164 = lang->la_get_symbol_name_matcher (m_lookup_name);
4165
4166 /* Don't insert the same comparison routine more than once.
4167 Note that we do this linear walk instead of a cheaper
4168 sorted insert, or use a std::set or something like that,
4169 because relative order of function addresses is not
4170 stable. This is not a problem in practice because the
4171 number of supported languages is low, and the cost here
4172 is tiny compared to the number of searches we'll do
4173 afterwards using this object. */
4174 if (std::find (matchers.begin (), matchers.end (), name_matcher)
4175 == matchers.end ())
4176 matchers.push_back (name_matcher);
4177 }
4178 }
4179}
4180
4181bool
4182gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4183{
4184 for (auto matches_name : m_symbol_name_matcher_funcs)
4185 if (matches_name (symbol_name, m_lookup_name, NULL))
4186 return true;
4187
4188 return false;
4189}
4190
3f563c84
PA
4191/* Helper for dw2_expand_symtabs_matching that works with a
4192 mapped_index instead of the containing objfile. This is split to a
4193 separate function in order to be able to unit test the
4194 name_components matching using a mock mapped_index. For each
4195 symbol name that matches, calls MATCH_CALLBACK, passing it the
4196 symbol's index in the mapped_index symbol table. */
61d96d7e 4197
3f563c84
PA
4198static void
4199dw2_expand_symtabs_matching_symbol
4200 (mapped_index &index,
c62446b1 4201 const lookup_name_info &lookup_name_in,
3f563c84
PA
4202 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4203 enum search_domain kind,
4204 gdb::function_view<void (offset_type)> match_callback)
4205{
c62446b1
PA
4206 lookup_name_info lookup_name_without_params
4207 = lookup_name_in.make_ignore_params ();
3f563c84 4208 gdb_index_symbol_name_matcher lookup_name_matcher
c62446b1 4209 (lookup_name_without_params);
3f563c84
PA
4210
4211 auto *name_cmp = case_sensitivity == case_sensitive_on ? strcmp : strcasecmp;
4212
4213 /* Build the symbol name component sorted vector, if we haven't yet.
4214 The code below only knows how to break apart components of C++
4215 symbol names (and other languages that use '::' as
4216 namespace/module separator). If we add support for wild matching
4217 to some language that uses some other operator (E.g., Ada, Go and
4218 D use '.'), then we'll need to try splitting the symbol name
4219 according to that language too. Note that Ada does support wild
4220 matching, but doesn't currently support .gdb_index. */
4221 if (index.name_components.empty ())
4222 {
4223 for (size_t iter = 0; iter < index.symbol_table_slots; ++iter)
4224 {
4225 offset_type idx = 2 * iter;
4226
4227 if (index.symbol_table[idx] == 0
4228 && index.symbol_table[idx + 1] == 0)
4229 continue;
4230
4231 const char *name = index.symbol_name_at (idx);
4232
4233 /* Add each name component to the name component table. */
4234 unsigned int previous_len = 0;
4235 for (unsigned int current_len = cp_find_first_component (name);
4236 name[current_len] != '\0';
4237 current_len += cp_find_first_component (name + current_len))
4238 {
4239 gdb_assert (name[current_len] == ':');
4240 index.name_components.push_back ({previous_len, idx});
4241 /* Skip the '::'. */
4242 current_len += 2;
4243 previous_len = current_len;
4244 }
4245 index.name_components.push_back ({previous_len, idx});
4246 }
9291a0cd 4247
3f563c84
PA
4248 /* Sort name_components elements by name. */
4249 auto name_comp_compare = [&] (const name_component &left,
4250 const name_component &right)
4251 {
4252 const char *left_qualified = index.symbol_name_at (left.idx);
4253 const char *right_qualified = index.symbol_name_at (right.idx);
4254
4255 const char *left_name = left_qualified + left.name_offset;
4256 const char *right_name = right_qualified + right.name_offset;
4257
4258 return name_cmp (left_name, right_name) < 0;
4259 };
4260
4261 std::sort (index.name_components.begin (),
4262 index.name_components.end (),
4263 name_comp_compare);
4264 }
4265
4266 const char *cplus
c62446b1 4267 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4268
3f563c84
PA
4269 /* Comparison function object for lower_bound that matches against a
4270 given symbol name. */
4271 auto lookup_compare_lower = [&] (const name_component &elem,
4272 const char *name)
4273 {
4274 const char *elem_qualified = index.symbol_name_at (elem.idx);
4275 const char *elem_name = elem_qualified + elem.name_offset;
4276 return name_cmp (elem_name, name) < 0;
4277 };
4278
4279 /* Comparison function object for upper_bound that matches against a
4280 given symbol name. */
4281 auto lookup_compare_upper = [&] (const char *name,
4282 const name_component &elem)
4283 {
4284 const char *elem_qualified = index.symbol_name_at (elem.idx);
4285 const char *elem_name = elem_qualified + elem.name_offset;
4286 return name_cmp (name, elem_name) < 0;
4287 };
4288
4289 auto begin = index.name_components.begin ();
4290 auto end = index.name_components.end ();
4291
4292 /* Find the lower bound. */
4293 auto lower = [&] ()
4294 {
c62446b1 4295 if (lookup_name_in.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4296 return begin;
4297 else
4298 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4299 } ();
4300
4301 /* Find the upper bound. */
4302 auto upper = [&] ()
4303 {
c62446b1 4304 if (lookup_name_in.completion_mode ())
3f563c84
PA
4305 {
4306 /* The string frobbing below won't work if the string is
4307 empty. We don't need it then, anyway -- if we're
4308 completing an empty string, then we want to iterate over
4309 the whole range. */
4310 if (cplus[0] == '\0')
4311 return end;
4312
4313 /* In completion mode, increment the last character because
4314 we want UPPER to point past all symbols names that have
4315 the same prefix. */
4316 std::string after = cplus;
4317
4318 gdb_assert (after.back () != 0xff);
4319 after.back ()++;
4320
4321 return std::upper_bound (lower, end, after.c_str (),
4322 lookup_compare_upper);
4323 }
4324 else
4325 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4326 } ();
4327
4328 /* Now for each symbol name in range, check to see if we have a name
4329 match, and if so, call the MATCH_CALLBACK callback. */
4330
4331 /* The same symbol may appear more than once in the range though.
4332 E.g., if we're looking for symbols that complete "w", and we have
4333 a symbol named "w1::w2", we'll find the two name components for
4334 that same symbol in the range. To be sure we only call the
4335 callback once per symbol, we first collect the symbol name
4336 indexes that matched in a temporary vector and ignore
4337 duplicates. */
4338 std::vector<offset_type> matches;
4339 matches.reserve (std::distance (lower, upper));
4340
4341 for (;lower != upper; ++lower)
4342 {
4343 const char *qualified = index.symbol_name_at (lower->idx);
4344
4345 if (!lookup_name_matcher.matches (qualified)
4346 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4347 continue;
4348
3f563c84
PA
4349 matches.push_back (lower->idx);
4350 }
4351
4352 std::sort (matches.begin (), matches.end ());
4353
4354 /* Finally call the callback, once per match. */
4355 ULONGEST prev = -1;
4356 for (offset_type idx : matches)
4357 {
4358 if (prev != idx)
4359 {
4360 match_callback (idx);
4361 prev = idx;
4362 }
4363 }
4364
4365 /* Above we use a type wider than idx's for 'prev', since 0 and
4366 (offset_type)-1 are both possible values. */
4367 static_assert (sizeof (prev) > sizeof (offset_type), "");
4368}
4369
c62446b1
PA
4370#if GDB_SELF_TEST
4371
4372namespace selftests { namespace dw2_expand_symtabs_matching {
4373
4374/* A wrapper around mapped_index that builds a mock mapped_index, from
4375 the symbol list passed as parameter to the constructor. */
4376class mock_mapped_index
4377{
4378public:
4379 template<size_t N>
4380 mock_mapped_index (const char *(&symbols)[N])
4381 : mock_mapped_index (symbols, N)
4382 {}
4383
4384 /* Access the built index. */
4385 mapped_index &index ()
4386 { return m_index; }
4387
4388 /* Disable copy. */
4389 mock_mapped_index(const mock_mapped_index &) = delete;
4390 void operator= (const mock_mapped_index &) = delete;
4391
4392private:
4393 mock_mapped_index (const char **symbols, size_t symbols_size)
4394 {
4395 /* No string can live at offset zero. Add a dummy entry. */
4396 obstack_grow_str0 (&m_constant_pool, "");
4397
4398 for (size_t i = 0; i < symbols_size; i++)
4399 {
4400 const char *sym = symbols[i];
4401 size_t offset = obstack_object_size (&m_constant_pool);
4402 obstack_grow_str0 (&m_constant_pool, sym);
4403 m_symbol_table.push_back (offset);
4404 m_symbol_table.push_back (0);
4405 };
4406
4407 m_index.constant_pool = (const char *) obstack_base (&m_constant_pool);
4408 m_index.symbol_table = m_symbol_table.data ();
4409 m_index.symbol_table_slots = m_symbol_table.size () / 2;
4410 }
4411
4412public:
4413 /* The built mapped_index. */
4414 mapped_index m_index{};
4415
4416 /* The storage that the built mapped_index uses for symbol and
4417 constant pool tables. */
4418 std::vector<offset_type> m_symbol_table;
4419 auto_obstack m_constant_pool;
4420};
4421
4422/* Convenience function that converts a NULL pointer to a "<null>"
4423 string, to pass to print routines. */
4424
4425static const char *
4426string_or_null (const char *str)
4427{
4428 return str != NULL ? str : "<null>";
4429}
4430
4431/* Check if a lookup_name_info built from
4432 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4433 index. EXPECTED_LIST is the list of expected matches, in expected
4434 matching order. If no match expected, then an empty list is
4435 specified. Returns true on success. On failure prints a warning
4436 indicating the file:line that failed, and returns false. */
4437
4438static bool
4439check_match (const char *file, int line,
4440 mock_mapped_index &mock_index,
4441 const char *name, symbol_name_match_type match_type,
4442 bool completion_mode,
4443 std::initializer_list<const char *> expected_list)
4444{
4445 lookup_name_info lookup_name (name, match_type, completion_mode);
4446
4447 bool matched = true;
4448
4449 auto mismatch = [&] (const char *expected_str,
4450 const char *got)
4451 {
4452 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4453 "expected=\"%s\", got=\"%s\"\n"),
4454 file, line,
4455 (match_type == symbol_name_match_type::FULL
4456 ? "FULL" : "WILD"),
4457 name, string_or_null (expected_str), string_or_null (got));
4458 matched = false;
4459 };
4460
4461 auto expected_it = expected_list.begin ();
4462 auto expected_end = expected_list.end ();
4463
4464 dw2_expand_symtabs_matching_symbol (mock_index.index (), lookup_name,
4465 NULL, ALL_DOMAIN,
4466 [&] (offset_type idx)
4467 {
4468 const char *matched_name = mock_index.index ().symbol_name_at (idx);
4469 const char *expected_str
4470 = expected_it == expected_end ? NULL : *expected_it++;
4471
4472 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4473 mismatch (expected_str, matched_name);
4474 });
4475
4476 const char *expected_str
4477 = expected_it == expected_end ? NULL : *expected_it++;
4478 if (expected_str != NULL)
4479 mismatch (expected_str, NULL);
4480
4481 return matched;
4482}
4483
4484/* The symbols added to the mock mapped_index for testing (in
4485 canonical form). */
4486static const char *test_symbols[] = {
4487 "function",
4488 "std::bar",
4489 "std::zfunction",
4490 "std::zfunction2",
4491 "w1::w2",
4492 "ns::foo<char*>",
4493 "ns::foo<int>",
4494 "ns::foo<long>",
4495
4496 /* A name with all sorts of complications. Starts with "z" to make
4497 it easier for the completion tests below. */
4498#define Z_SYM_NAME \
4499 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4500 "::tuple<(anonymous namespace)::ui*, " \
4501 "std::default_delete<(anonymous namespace)::ui>, void>"
4502
4503 Z_SYM_NAME
4504};
4505
4506static void
4507run_test ()
4508{
4509 mock_mapped_index mock_index (test_symbols);
4510
4511 /* We let all tests run until the end even if some fails, for debug
4512 convenience. */
4513 bool any_mismatch = false;
4514
4515 /* Create the expected symbols list (an initializer_list). Needed
4516 because lists have commas, and we need to pass them to CHECK,
4517 which is a macro. */
4518#define EXPECT(...) { __VA_ARGS__ }
4519
4520 /* Wrapper for check_match that passes down the current
4521 __FILE__/__LINE__. */
4522#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4523 any_mismatch |= !check_match (__FILE__, __LINE__, \
4524 mock_index, \
4525 NAME, MATCH_TYPE, COMPLETION_MODE, \
4526 EXPECTED_LIST)
4527
4528 /* Identity checks. */
4529 for (const char *sym : test_symbols)
4530 {
4531 /* Should be able to match all existing symbols. */
4532 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4533 EXPECT (sym));
4534
4535 /* Should be able to match all existing symbols with
4536 parameters. */
4537 std::string with_params = std::string (sym) + "(int)";
4538 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4539 EXPECT (sym));
4540
4541 /* Should be able to match all existing symbols with
4542 parameters and qualifiers. */
4543 with_params = std::string (sym) + " ( int ) const";
4544 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4545 EXPECT (sym));
4546
4547 /* This should really find sym, but cp-name-parser.y doesn't
4548 know about lvalue/rvalue qualifiers yet. */
4549 with_params = std::string (sym) + " ( int ) &&";
4550 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4551 {});
4552 }
4553
4554 /* Check that completion mode works at each prefix of the expected
4555 symbol name. */
4556 {
4557 static const char str[] = "function(int)";
4558 size_t len = strlen (str);
4559 std::string lookup;
4560
4561 for (size_t i = 1; i < len; i++)
4562 {
4563 lookup.assign (str, i);
4564 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4565 EXPECT ("function"));
4566 }
4567 }
4568
4569 /* While "w" is a prefix of both components, the match function
4570 should still only be called once. */
4571 {
4572 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4573 EXPECT ("w1::w2"));
4574 }
4575
4576 /* Same, with a "complicated" symbol. */
4577 {
4578 static const char str[] = Z_SYM_NAME;
4579 size_t len = strlen (str);
4580 std::string lookup;
4581
4582 for (size_t i = 1; i < len; i++)
4583 {
4584 lookup.assign (str, i);
4585 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4586 EXPECT (Z_SYM_NAME));
4587 }
4588 }
4589
4590 /* In FULL mode, an incomplete symbol doesn't match. */
4591 {
4592 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4593 {});
4594 }
4595
4596 /* A complete symbol with parameters matches any overload, since the
4597 index has no overload info. */
4598 {
4599 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4600 EXPECT ("std::zfunction", "std::zfunction2"));
4601 }
4602
4603 /* Check that whitespace is ignored appropriately. A symbol with a
4604 template argument list. */
4605 {
4606 static const char expected[] = "ns::foo<int>";
4607 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4608 EXPECT (expected));
4609 }
4610
4611 /* Check that whitespace is ignored appropriately. A symbol with a
4612 template argument list that includes a pointer. */
4613 {
4614 static const char expected[] = "ns::foo<char*>";
4615 /* Try both completion and non-completion modes. */
4616 static const bool completion_mode[2] = {false, true};
4617 for (size_t i = 0; i < 2; i++)
4618 {
4619 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4620 completion_mode[i], EXPECT (expected));
4621
4622 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4623 completion_mode[i], EXPECT (expected));
4624 }
4625 }
4626
4627 {
4628 /* Check method qualifiers are ignored. */
4629 static const char expected[] = "ns::foo<char*>";
4630 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4631 symbol_name_match_type::FULL, true, EXPECT (expected));
4632 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4633 symbol_name_match_type::FULL, true, EXPECT (expected));
4634 }
4635
4636 /* Test lookup names that don't match anything. */
4637 {
4638 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4639 {});
4640 }
4641
4642 SELF_CHECK (!any_mismatch);
4643
4644#undef EXPECT
4645#undef CHECK_MATCH
4646}
4647
4648}} // namespace selftests::dw2_expand_symtabs_matching
4649
4650#endif /* GDB_SELF_TEST */
4651
3f563c84
PA
4652/* Helper for dw2_expand_matching symtabs. Called on each symbol
4653 matched, to expand corresponding CUs that were marked. IDX is the
4654 index of the symbol name that matched. */
4655
4656static void
4657dw2_expand_marked_cus
4658 (mapped_index &index, offset_type idx,
4659 struct objfile *objfile,
4660 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4661 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4662 search_domain kind)
4663{
4664 const char *name;
4665 offset_type *vec, vec_len, vec_idx;
4666 bool global_seen = false;
4667
61920122
PA
4668 vec = (offset_type *) (index.constant_pool
4669 + MAYBE_SWAP (index.symbol_table[idx + 1]));
4670 vec_len = MAYBE_SWAP (vec[0]);
4671 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4672 {
4673 struct dwarf2_per_cu_data *per_cu;
4674 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4675 /* This value is only valid for index versions >= 7. */
4676 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4677 gdb_index_symbol_kind symbol_kind =
4678 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4679 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4680 /* Only check the symbol attributes if they're present.
4681 Indices prior to version 7 don't record them,
4682 and indices >= 7 may elide them for certain symbols
4683 (gold does this). */
4684 int attrs_valid =
4685 (index.version >= 7
4686 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4687
4688 /* Work around gold/15646. */
4689 if (attrs_valid)
9291a0cd 4690 {
61920122
PA
4691 if (!is_static && global_seen)
4692 continue;
4693 if (!is_static)
4694 global_seen = true;
4695 }
3190f0c6 4696
61920122
PA
4697 /* Only check the symbol's kind if it has one. */
4698 if (attrs_valid)
4699 {
4700 switch (kind)
8943b874 4701 {
61920122
PA
4702 case VARIABLES_DOMAIN:
4703 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4704 continue;
4705 break;
4706 case FUNCTIONS_DOMAIN:
4707 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4708 continue;
61920122
PA
4709 break;
4710 case TYPES_DOMAIN:
4711 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4712 continue;
4713 break;
4714 default:
4715 break;
8943b874 4716 }
61920122 4717 }
8943b874 4718
61920122
PA
4719 /* Don't crash on bad data. */
4720 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4721 + dwarf2_per_objfile->n_type_units))
4722 {
4723 complaint (&symfile_complaints,
4724 _(".gdb_index entry has bad CU index"
4725 " [in module %s]"), objfile_name (objfile));
4726 continue;
4727 }
4728
4729 per_cu = dw2_get_cutu (cu_index);
4730 if (file_matcher == NULL || per_cu->v.quick->mark)
4731 {
4732 int symtab_was_null =
4733 (per_cu->v.quick->compunit_symtab == NULL);
4734
4735 dw2_instantiate_symtab (per_cu);
4736
4737 if (expansion_notify != NULL
4738 && symtab_was_null
4739 && per_cu->v.quick->compunit_symtab != NULL)
4740 expansion_notify (per_cu->v.quick->compunit_symtab);
4741 }
4742 }
4743}
4744
4745static void
4746dw2_expand_symtabs_matching
4747 (struct objfile *objfile,
4748 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4749 const lookup_name_info &lookup_name,
4750 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4751 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4752 enum search_domain kind)
4753{
4754 int i;
4755 offset_type iter;
4756
4757 dw2_setup (objfile);
4758
4759 /* index_table is NULL if OBJF_READNOW. */
4760 if (!dwarf2_per_objfile->index_table)
4761 return;
4762
4763 if (file_matcher != NULL)
4764 {
4765 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4766 htab_eq_pointer,
4767 NULL, xcalloc, xfree));
4768 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4769 htab_eq_pointer,
4770 NULL, xcalloc, xfree));
4771
4772 /* The rule is CUs specify all the files, including those used by
4773 any TU, so there's no need to scan TUs here. */
4774
4775 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4776 {
4777 int j;
4778 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4779 struct quick_file_names *file_data;
4780 void **slot;
4781
4782 QUIT;
4783
4784 per_cu->v.quick->mark = 0;
156942c7 4785
61920122
PA
4786 /* We only need to look at symtabs not already expanded. */
4787 if (per_cu->v.quick->compunit_symtab)
4788 continue;
4789
4790 file_data = dw2_get_file_names (per_cu);
4791 if (file_data == NULL)
4792 continue;
4793
4794 if (htab_find (visited_not_found.get (), file_data) != NULL)
4795 continue;
4796 else if (htab_find (visited_found.get (), file_data) != NULL)
3190f0c6 4797 {
61920122 4798 per_cu->v.quick->mark = 1;
3190f0c6
DE
4799 continue;
4800 }
4801
61920122 4802 for (j = 0; j < file_data->num_file_names; ++j)
276d885b 4803 {
61920122 4804 const char *this_real_name;
276d885b 4805
61920122
PA
4806 if (file_matcher (file_data->file_names[j], false))
4807 {
4808 per_cu->v.quick->mark = 1;
4809 break;
4810 }
4811
4812 /* Before we invoke realpath, which can get expensive when many
4813 files are involved, do a quick comparison of the basenames. */
4814 if (!basenames_may_differ
4815 && !file_matcher (lbasename (file_data->file_names[j]),
4816 true))
4817 continue;
276d885b 4818
61920122
PA
4819 this_real_name = dw2_get_real_path (objfile, file_data, j);
4820 if (file_matcher (this_real_name, false))
276d885b 4821 {
61920122
PA
4822 per_cu->v.quick->mark = 1;
4823 break;
276d885b
GB
4824 }
4825 }
61920122
PA
4826
4827 slot = htab_find_slot (per_cu->v.quick->mark
4828 ? visited_found.get ()
4829 : visited_not_found.get (),
4830 file_data, INSERT);
4831 *slot = file_data;
9291a0cd 4832 }
61920122
PA
4833 }
4834
4835 mapped_index &index = *dwarf2_per_objfile->index_table;
4836
4837 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4838 symbol_matcher,
4839 kind, [&] (offset_type idx)
4840 {
4841 dw2_expand_marked_cus (index, idx, objfile, file_matcher,
4842 expansion_notify, kind);
4843 });
9291a0cd
TT
4844}
4845
43f3e411 4846/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
9703b513
TT
4847 symtab. */
4848
43f3e411
DE
4849static struct compunit_symtab *
4850recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4851 CORE_ADDR pc)
9703b513
TT
4852{
4853 int i;
4854
43f3e411
DE
4855 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4856 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4857 return cust;
9703b513 4858
43f3e411 4859 if (cust->includes == NULL)
a3ec0bb1
DE
4860 return NULL;
4861
43f3e411 4862 for (i = 0; cust->includes[i]; ++i)
9703b513 4863 {
43f3e411 4864 struct compunit_symtab *s = cust->includes[i];
9703b513 4865
43f3e411 4866 s = recursively_find_pc_sect_compunit_symtab (s, pc);
9703b513
TT
4867 if (s != NULL)
4868 return s;
4869 }
4870
4871 return NULL;
4872}
4873
43f3e411
DE
4874static struct compunit_symtab *
4875dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4876 struct bound_minimal_symbol msymbol,
4877 CORE_ADDR pc,
4878 struct obj_section *section,
4879 int warn_if_readin)
9291a0cd
TT
4880{
4881 struct dwarf2_per_cu_data *data;
43f3e411 4882 struct compunit_symtab *result;
9291a0cd
TT
4883
4884 dw2_setup (objfile);
4885
4886 if (!objfile->psymtabs_addrmap)
4887 return NULL;
4888
9a3c8263
SM
4889 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4890 pc);
9291a0cd
TT
4891 if (!data)
4892 return NULL;
4893
43f3e411 4894 if (warn_if_readin && data->v.quick->compunit_symtab)
abebb8b0 4895 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
4896 paddress (get_objfile_arch (objfile), pc));
4897
43f3e411
DE
4898 result
4899 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4900 pc);
9703b513
TT
4901 gdb_assert (result != NULL);
4902 return result;
9291a0cd
TT
4903}
4904
9291a0cd 4905static void
44b13c5a 4906dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 4907 void *data, int need_fullname)
9291a0cd 4908{
9291a0cd 4909 dw2_setup (objfile);
ae2de4f8 4910
bbf2f4df 4911 if (!dwarf2_per_objfile->filenames_cache)
24c79950 4912 {
bbf2f4df 4913 dwarf2_per_objfile->filenames_cache.emplace ();
24c79950 4914
bbf2f4df
PA
4915 htab_up visited (htab_create_alloc (10,
4916 htab_hash_pointer, htab_eq_pointer,
4917 NULL, xcalloc, xfree));
24c79950 4918
bbf2f4df
PA
4919 /* The rule is CUs specify all the files, including those used
4920 by any TU, so there's no need to scan TUs here. We can
4921 ignore file names coming from already-expanded CUs. */
24c79950 4922
bbf2f4df
PA
4923 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4924 {
4925 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4926
bbf2f4df
PA
4927 if (per_cu->v.quick->compunit_symtab)
4928 {
4929 void **slot = htab_find_slot (visited.get (),
4930 per_cu->v.quick->file_names,
4931 INSERT);
9291a0cd 4932
bbf2f4df
PA
4933 *slot = per_cu->v.quick->file_names;
4934 }
24c79950 4935 }
24c79950 4936
bbf2f4df 4937 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 4938 {
bbf2f4df
PA
4939 int j;
4940 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4941 struct quick_file_names *file_data;
4942 void **slot;
4943
4944 /* We only need to look at symtabs not already expanded. */
4945 if (per_cu->v.quick->compunit_symtab)
4946 continue;
74e2f255 4947
bbf2f4df
PA
4948 file_data = dw2_get_file_names (per_cu);
4949 if (file_data == NULL)
4950 continue;
4951
4952 slot = htab_find_slot (visited.get (), file_data, INSERT);
4953 if (*slot)
4954 {
4955 /* Already visited. */
4956 continue;
4957 }
4958 *slot = file_data;
4959
4960 for (int j = 0; j < file_data->num_file_names; ++j)
4961 {
4962 const char *filename = file_data->file_names[j];
4963 dwarf2_per_objfile->filenames_cache->seen (filename);
4964 }
9291a0cd
TT
4965 }
4966 }
bbf2f4df
PA
4967
4968 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4969 {
14278e1f 4970 gdb::unique_xmalloc_ptr<char> this_real_name;
bbf2f4df
PA
4971
4972 if (need_fullname)
4973 this_real_name = gdb_realpath (filename);
14278e1f 4974 (*fun) (filename, this_real_name.get (), data);
bbf2f4df 4975 });
9291a0cd
TT
4976}
4977
4978static int
4979dw2_has_symbols (struct objfile *objfile)
4980{
4981 return 1;
4982}
4983
4984const struct quick_symbol_functions dwarf2_gdb_index_functions =
4985{
4986 dw2_has_symbols,
4987 dw2_find_last_source_symtab,
4988 dw2_forget_cached_source_info,
f8eba3c6 4989 dw2_map_symtabs_matching_filename,
9291a0cd 4990 dw2_lookup_symbol,
9291a0cd
TT
4991 dw2_print_stats,
4992 dw2_dump,
4993 dw2_relocate,
4994 dw2_expand_symtabs_for_function,
4995 dw2_expand_all_symtabs,
652a8996 4996 dw2_expand_symtabs_with_fullname,
40658b94 4997 dw2_map_matching_symbols,
9291a0cd 4998 dw2_expand_symtabs_matching,
43f3e411 4999 dw2_find_pc_sect_compunit_symtab,
71a3c369 5000 NULL,
9291a0cd
TT
5001 dw2_map_symbol_filenames
5002};
5003
5004/* Initialize for reading DWARF for this objfile. Return 0 if this
5005 file will use psymtabs, or 1 if using the GNU index. */
5006
5007int
5008dwarf2_initialize_objfile (struct objfile *objfile)
5009{
5010 /* If we're about to read full symbols, don't bother with the
5011 indices. In this case we also don't care if some other debug
5012 format is making psymtabs, because they are all about to be
5013 expanded anyway. */
5014 if ((objfile->flags & OBJF_READNOW))
5015 {
5016 int i;
5017
5018 dwarf2_per_objfile->using_index = 1;
5019 create_all_comp_units (objfile);
0e50663e 5020 create_all_type_units (objfile);
7b9f3c50
DE
5021 dwarf2_per_objfile->quick_file_names_table =
5022 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 5023
1fd400ff 5024 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 5025 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 5026 {
8832e7e3 5027 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 5028
e254ef6a
DE
5029 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5030 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5031 }
5032
5033 /* Return 1 so that gdb sees the "quick" functions. However,
5034 these functions will be no-ops because we will have expanded
5035 all symtabs. */
5036 return 1;
5037 }
5038
5039 if (dwarf2_read_index (objfile))
5040 return 1;
5041
9291a0cd
TT
5042 return 0;
5043}
5044
5045\f
5046
dce234bc
PP
5047/* Build a partial symbol table. */
5048
5049void
f29dff0a 5050dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5051{
c9bf0622 5052
af5bf4ad
SM
5053 if (objfile->global_psymbols.capacity () == 0
5054 && objfile->static_psymbols.capacity () == 0)
5055 init_psymbol_list (objfile, 1024);
c906108c 5056
492d29ea 5057 TRY
c9bf0622
TT
5058 {
5059 /* This isn't really ideal: all the data we allocate on the
5060 objfile's obstack is still uselessly kept around. However,
5061 freeing it seems unsafe. */
906768f9 5062 psymtab_discarder psymtabs (objfile);
c9bf0622 5063 dwarf2_build_psymtabs_hard (objfile);
906768f9 5064 psymtabs.keep ();
c9bf0622 5065 }
492d29ea
PA
5066 CATCH (except, RETURN_MASK_ERROR)
5067 {
5068 exception_print (gdb_stderr, except);
5069 }
5070 END_CATCH
c906108c 5071}
c906108c 5072
1ce1cefd
DE
5073/* Return the total length of the CU described by HEADER. */
5074
5075static unsigned int
5076get_cu_length (const struct comp_unit_head *header)
5077{
5078 return header->initial_length_size + header->length;
5079}
5080
9c541725 5081/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 5082
9c541725
PA
5083static inline bool
5084offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 5085{
9c541725
PA
5086 sect_offset bottom = cu_header->sect_off;
5087 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 5088
9c541725 5089 return sect_off >= bottom && sect_off < top;
45452591
DE
5090}
5091
3b80fe9b
DE
5092/* Find the base address of the compilation unit for range lists and
5093 location lists. It will normally be specified by DW_AT_low_pc.
5094 In DWARF-3 draft 4, the base address could be overridden by
5095 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5096 compilation units with discontinuous ranges. */
5097
5098static void
5099dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5100{
5101 struct attribute *attr;
5102
5103 cu->base_known = 0;
5104 cu->base_address = 0;
5105
5106 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5107 if (attr)
5108 {
31aa7e4e 5109 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
5110 cu->base_known = 1;
5111 }
5112 else
5113 {
5114 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5115 if (attr)
5116 {
31aa7e4e 5117 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
5118 cu->base_known = 1;
5119 }
5120 }
5121}
5122
93311388 5123/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 5124 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
5125 NOTE: This leaves members offset, first_die_offset to be filled in
5126 by the caller. */
107d2387 5127
d521ce57 5128static const gdb_byte *
107d2387 5129read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
5130 const gdb_byte *info_ptr,
5131 struct dwarf2_section_info *section,
5132 rcuh_kind section_kind)
107d2387
AC
5133{
5134 int signed_addr;
891d2f0b 5135 unsigned int bytes_read;
43988095
JK
5136 const char *filename = get_section_file_name (section);
5137 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
5138
5139 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
5140 cu_header->initial_length_size = bytes_read;
5141 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 5142 info_ptr += bytes_read;
107d2387
AC
5143 cu_header->version = read_2_bytes (abfd, info_ptr);
5144 info_ptr += 2;
43988095
JK
5145 if (cu_header->version < 5)
5146 switch (section_kind)
5147 {
5148 case rcuh_kind::COMPILE:
5149 cu_header->unit_type = DW_UT_compile;
5150 break;
5151 case rcuh_kind::TYPE:
5152 cu_header->unit_type = DW_UT_type;
5153 break;
5154 default:
5155 internal_error (__FILE__, __LINE__,
5156 _("read_comp_unit_head: invalid section_kind"));
5157 }
5158 else
5159 {
5160 cu_header->unit_type = static_cast<enum dwarf_unit_type>
5161 (read_1_byte (abfd, info_ptr));
5162 info_ptr += 1;
5163 switch (cu_header->unit_type)
5164 {
5165 case DW_UT_compile:
5166 if (section_kind != rcuh_kind::COMPILE)
5167 error (_("Dwarf Error: wrong unit_type in compilation unit header "
5168 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
5169 filename);
5170 break;
5171 case DW_UT_type:
5172 section_kind = rcuh_kind::TYPE;
5173 break;
5174 default:
5175 error (_("Dwarf Error: wrong unit_type in compilation unit header "
5176 "(is %d, should be %d or %d) [in module %s]"),
5177 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
5178 }
5179
5180 cu_header->addr_size = read_1_byte (abfd, info_ptr);
5181 info_ptr += 1;
5182 }
9c541725
PA
5183 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
5184 cu_header,
5185 &bytes_read);
613e1657 5186 info_ptr += bytes_read;
43988095
JK
5187 if (cu_header->version < 5)
5188 {
5189 cu_header->addr_size = read_1_byte (abfd, info_ptr);
5190 info_ptr += 1;
5191 }
107d2387
AC
5192 signed_addr = bfd_get_sign_extend_vma (abfd);
5193 if (signed_addr < 0)
8e65ff28 5194 internal_error (__FILE__, __LINE__,
e2e0b3e5 5195 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 5196 cu_header->signed_addr_p = signed_addr;
c764a876 5197
43988095
JK
5198 if (section_kind == rcuh_kind::TYPE)
5199 {
5200 LONGEST type_offset;
5201
5202 cu_header->signature = read_8_bytes (abfd, info_ptr);
5203 info_ptr += 8;
5204
5205 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
5206 info_ptr += bytes_read;
9c541725
PA
5207 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
5208 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
5209 error (_("Dwarf Error: Too big type_offset in compilation unit "
5210 "header (is %s) [in module %s]"), plongest (type_offset),
5211 filename);
5212 }
5213
107d2387
AC
5214 return info_ptr;
5215}
5216
36586728
TT
5217/* Helper function that returns the proper abbrev section for
5218 THIS_CU. */
5219
5220static struct dwarf2_section_info *
5221get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5222{
5223 struct dwarf2_section_info *abbrev;
5224
5225 if (this_cu->is_dwz)
5226 abbrev = &dwarf2_get_dwz_file ()->abbrev;
5227 else
5228 abbrev = &dwarf2_per_objfile->abbrev;
5229
5230 return abbrev;
5231}
5232
9ff913ba
DE
5233/* Subroutine of read_and_check_comp_unit_head and
5234 read_and_check_type_unit_head to simplify them.
5235 Perform various error checking on the header. */
5236
5237static void
5238error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
5239 struct dwarf2_section_info *section,
5240 struct dwarf2_section_info *abbrev_section)
9ff913ba 5241{
a32a8923 5242 const char *filename = get_section_file_name (section);
9ff913ba 5243
43988095 5244 if (header->version < 2 || header->version > 5)
9ff913ba 5245 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 5246 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
5247 filename);
5248
9c541725 5249 if (to_underlying (header->abbrev_sect_off)
36586728 5250 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9c541725
PA
5251 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
5252 "(offset 0x%x + 6) [in module %s]"),
5253 to_underlying (header->abbrev_sect_off),
5254 to_underlying (header->sect_off),
9ff913ba
DE
5255 filename);
5256
9c541725 5257 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 5258 avoid potential 32-bit overflow. */
9c541725 5259 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 5260 > section->size)
9c541725
PA
5261 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
5262 "(offset 0x%x + 0) [in module %s]"),
5263 header->length, to_underlying (header->sect_off),
9ff913ba
DE
5264 filename);
5265}
5266
5267/* Read in a CU/TU header and perform some basic error checking.
5268 The contents of the header are stored in HEADER.
5269 The result is a pointer to the start of the first DIE. */
adabb602 5270
d521ce57 5271static const gdb_byte *
9ff913ba
DE
5272read_and_check_comp_unit_head (struct comp_unit_head *header,
5273 struct dwarf2_section_info *section,
4bdcc0c1 5274 struct dwarf2_section_info *abbrev_section,
d521ce57 5275 const gdb_byte *info_ptr,
43988095 5276 rcuh_kind section_kind)
72bf9492 5277{
d521ce57 5278 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 5279 bfd *abfd = get_section_bfd_owner (section);
72bf9492 5280
9c541725 5281 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 5282
43988095 5283 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 5284
9c541725 5285 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 5286
4bdcc0c1 5287 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
5288
5289 return info_ptr;
348e048f
DE
5290}
5291
f4dc4d17
DE
5292/* Fetch the abbreviation table offset from a comp or type unit header. */
5293
5294static sect_offset
5295read_abbrev_offset (struct dwarf2_section_info *section,
9c541725 5296 sect_offset sect_off)
f4dc4d17 5297{
a32a8923 5298 bfd *abfd = get_section_bfd_owner (section);
d521ce57 5299 const gdb_byte *info_ptr;
ac298888 5300 unsigned int initial_length_size, offset_size;
43988095 5301 uint16_t version;
f4dc4d17
DE
5302
5303 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 5304 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5305 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5306 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5307 info_ptr += initial_length_size;
5308
5309 version = read_2_bytes (abfd, info_ptr);
5310 info_ptr += 2;
5311 if (version >= 5)
5312 {
5313 /* Skip unit type and address size. */
5314 info_ptr += 2;
5315 }
5316
9c541725 5317 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
5318}
5319
aaa75496
JB
5320/* Allocate a new partial symtab for file named NAME and mark this new
5321 partial symtab as being an include of PST. */
5322
5323static void
d521ce57 5324dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
5325 struct objfile *objfile)
5326{
5327 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
5328
fbd9ab74
JK
5329 if (!IS_ABSOLUTE_PATH (subpst->filename))
5330 {
5331 /* It shares objfile->objfile_obstack. */
5332 subpst->dirname = pst->dirname;
5333 }
5334
aaa75496
JB
5335 subpst->textlow = 0;
5336 subpst->texthigh = 0;
5337
8d749320
SM
5338 subpst->dependencies
5339 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
5340 subpst->dependencies[0] = pst;
5341 subpst->number_of_dependencies = 1;
5342
5343 subpst->globals_offset = 0;
5344 subpst->n_global_syms = 0;
5345 subpst->statics_offset = 0;
5346 subpst->n_static_syms = 0;
43f3e411 5347 subpst->compunit_symtab = NULL;
aaa75496
JB
5348 subpst->read_symtab = pst->read_symtab;
5349 subpst->readin = 0;
5350
5351 /* No private part is necessary for include psymtabs. This property
5352 can be used to differentiate between such include psymtabs and
10b3939b 5353 the regular ones. */
58a9656e 5354 subpst->read_symtab_private = NULL;
aaa75496
JB
5355}
5356
5357/* Read the Line Number Program data and extract the list of files
5358 included by the source file represented by PST. Build an include
d85a05f0 5359 partial symtab for each of these included files. */
aaa75496
JB
5360
5361static void
5362dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
5363 struct die_info *die,
5364 struct partial_symtab *pst)
aaa75496 5365{
fff8551c 5366 line_header_up lh;
d85a05f0 5367 struct attribute *attr;
aaa75496 5368
d85a05f0
DJ
5369 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5370 if (attr)
9c541725 5371 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
5372 if (lh == NULL)
5373 return; /* No linetable, so no includes. */
5374
c6da4cef 5375 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 5376 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
5377}
5378
348e048f 5379static hashval_t
52dc124a 5380hash_signatured_type (const void *item)
348e048f 5381{
9a3c8263
SM
5382 const struct signatured_type *sig_type
5383 = (const struct signatured_type *) item;
9a619af0 5384
348e048f 5385 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 5386 return sig_type->signature;
348e048f
DE
5387}
5388
5389static int
52dc124a 5390eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 5391{
9a3c8263
SM
5392 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5393 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 5394
348e048f
DE
5395 return lhs->signature == rhs->signature;
5396}
5397
1fd400ff
TT
5398/* Allocate a hash table for signatured types. */
5399
5400static htab_t
673bfd45 5401allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
5402{
5403 return htab_create_alloc_ex (41,
52dc124a
DE
5404 hash_signatured_type,
5405 eq_signatured_type,
1fd400ff
TT
5406 NULL,
5407 &objfile->objfile_obstack,
5408 hashtab_obstack_allocate,
5409 dummy_obstack_deallocate);
5410}
5411
d467dd73 5412/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
5413
5414static int
d467dd73 5415add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 5416{
9a3c8263
SM
5417 struct signatured_type *sigt = (struct signatured_type *) *slot;
5418 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 5419
b4dd5633 5420 **datap = sigt;
1fd400ff
TT
5421 ++*datap;
5422
5423 return 1;
5424}
5425
78d4d2c5 5426/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
5427 and fill them into TYPES_HTAB. It will process only type units,
5428 therefore DW_UT_type. */
c88ee1f0 5429
78d4d2c5
JK
5430static void
5431create_debug_type_hash_table (struct dwo_file *dwo_file,
43988095
JK
5432 dwarf2_section_info *section, htab_t &types_htab,
5433 rcuh_kind section_kind)
348e048f 5434{
3019eac3 5435 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 5436 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
5437 bfd *abfd;
5438 const gdb_byte *info_ptr, *end_ptr;
348e048f 5439
4bdcc0c1
DE
5440 abbrev_section = (dwo_file != NULL
5441 ? &dwo_file->sections.abbrev
5442 : &dwarf2_per_objfile->abbrev);
5443
b4f54984 5444 if (dwarf_read_debug)
43988095
JK
5445 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
5446 get_section_name (section),
a32a8923 5447 get_section_file_name (abbrev_section));
09406207 5448
78d4d2c5
JK
5449 dwarf2_read_section (objfile, section);
5450 info_ptr = section->buffer;
348e048f 5451
78d4d2c5
JK
5452 if (info_ptr == NULL)
5453 return;
348e048f 5454
78d4d2c5
JK
5455 /* We can't set abfd until now because the section may be empty or
5456 not present, in which case the bfd is unknown. */
5457 abfd = get_section_bfd_owner (section);
348e048f 5458
78d4d2c5
JK
5459 /* We don't use init_cutu_and_read_dies_simple, or some such, here
5460 because we don't need to read any dies: the signature is in the
5461 header. */
3019eac3 5462
78d4d2c5
JK
5463 end_ptr = info_ptr + section->size;
5464 while (info_ptr < end_ptr)
5465 {
78d4d2c5
JK
5466 struct signatured_type *sig_type;
5467 struct dwo_unit *dwo_tu;
5468 void **slot;
5469 const gdb_byte *ptr = info_ptr;
5470 struct comp_unit_head header;
5471 unsigned int length;
8b70b953 5472
9c541725 5473 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 5474
a49dd8dd
JK
5475 /* Initialize it due to a false compiler warning. */
5476 header.signature = -1;
9c541725 5477 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 5478
78d4d2c5
JK
5479 /* We need to read the type's signature in order to build the hash
5480 table, but we don't need anything else just yet. */
348e048f 5481
43988095
JK
5482 ptr = read_and_check_comp_unit_head (&header, section,
5483 abbrev_section, ptr, section_kind);
348e048f 5484
78d4d2c5 5485 length = get_cu_length (&header);
6caca83c 5486
78d4d2c5
JK
5487 /* Skip dummy type units. */
5488 if (ptr >= info_ptr + length
43988095
JK
5489 || peek_abbrev_code (abfd, ptr) == 0
5490 || header.unit_type != DW_UT_type)
78d4d2c5
JK
5491 {
5492 info_ptr += length;
5493 continue;
5494 }
dee91e82 5495
78d4d2c5
JK
5496 if (types_htab == NULL)
5497 {
5498 if (dwo_file)
5499 types_htab = allocate_dwo_unit_table (objfile);
5500 else
5501 types_htab = allocate_signatured_type_table (objfile);
5502 }
8b70b953 5503
78d4d2c5
JK
5504 if (dwo_file)
5505 {
5506 sig_type = NULL;
5507 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5508 struct dwo_unit);
5509 dwo_tu->dwo_file = dwo_file;
43988095 5510 dwo_tu->signature = header.signature;
9c541725 5511 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 5512 dwo_tu->section = section;
9c541725 5513 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
5514 dwo_tu->length = length;
5515 }
5516 else
5517 {
5518 /* N.B.: type_offset is not usable if this type uses a DWO file.
5519 The real type_offset is in the DWO file. */
5520 dwo_tu = NULL;
5521 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5522 struct signatured_type);
43988095 5523 sig_type->signature = header.signature;
9c541725 5524 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
5525 sig_type->per_cu.objfile = objfile;
5526 sig_type->per_cu.is_debug_types = 1;
5527 sig_type->per_cu.section = section;
9c541725 5528 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
5529 sig_type->per_cu.length = length;
5530 }
5531
5532 slot = htab_find_slot (types_htab,
5533 dwo_file ? (void*) dwo_tu : (void *) sig_type,
5534 INSERT);
5535 gdb_assert (slot != NULL);
5536 if (*slot != NULL)
5537 {
9c541725 5538 sect_offset dup_sect_off;
0349ea22 5539
3019eac3
DE
5540 if (dwo_file)
5541 {
78d4d2c5
JK
5542 const struct dwo_unit *dup_tu
5543 = (const struct dwo_unit *) *slot;
5544
9c541725 5545 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
5546 }
5547 else
5548 {
78d4d2c5
JK
5549 const struct signatured_type *dup_tu
5550 = (const struct signatured_type *) *slot;
5551
9c541725 5552 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 5553 }
8b70b953 5554
78d4d2c5
JK
5555 complaint (&symfile_complaints,
5556 _("debug type entry at offset 0x%x is duplicate to"
5557 " the entry at offset 0x%x, signature %s"),
9c541725 5558 to_underlying (sect_off), to_underlying (dup_sect_off),
43988095 5559 hex_string (header.signature));
78d4d2c5
JK
5560 }
5561 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 5562
78d4d2c5
JK
5563 if (dwarf_read_debug > 1)
5564 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
9c541725 5565 to_underlying (sect_off),
43988095 5566 hex_string (header.signature));
3019eac3 5567
78d4d2c5
JK
5568 info_ptr += length;
5569 }
5570}
3019eac3 5571
78d4d2c5
JK
5572/* Create the hash table of all entries in the .debug_types
5573 (or .debug_types.dwo) section(s).
5574 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
5575 otherwise it is NULL.
b3c8eb43 5576
78d4d2c5 5577 The result is a pointer to the hash table or NULL if there are no types.
348e048f 5578
78d4d2c5 5579 Note: This function processes DWO files only, not DWP files. */
348e048f 5580
78d4d2c5
JK
5581static void
5582create_debug_types_hash_table (struct dwo_file *dwo_file,
5583 VEC (dwarf2_section_info_def) *types,
5584 htab_t &types_htab)
5585{
5586 int ix;
5587 struct dwarf2_section_info *section;
5588
5589 if (VEC_empty (dwarf2_section_info_def, types))
5590 return;
348e048f 5591
78d4d2c5
JK
5592 for (ix = 0;
5593 VEC_iterate (dwarf2_section_info_def, types, ix, section);
5594 ++ix)
43988095
JK
5595 create_debug_type_hash_table (dwo_file, section, types_htab,
5596 rcuh_kind::TYPE);
3019eac3
DE
5597}
5598
5599/* Create the hash table of all entries in the .debug_types section,
5600 and initialize all_type_units.
5601 The result is zero if there is an error (e.g. missing .debug_types section),
5602 otherwise non-zero. */
5603
5604static int
5605create_all_type_units (struct objfile *objfile)
5606{
78d4d2c5 5607 htab_t types_htab = NULL;
b4dd5633 5608 struct signatured_type **iter;
3019eac3 5609
43988095
JK
5610 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
5611 rcuh_kind::COMPILE);
78d4d2c5 5612 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
3019eac3
DE
5613 if (types_htab == NULL)
5614 {
5615 dwarf2_per_objfile->signatured_types = NULL;
5616 return 0;
5617 }
5618
348e048f
DE
5619 dwarf2_per_objfile->signatured_types = types_htab;
5620
6aa5f3a6
DE
5621 dwarf2_per_objfile->n_type_units
5622 = dwarf2_per_objfile->n_allocated_type_units
5623 = htab_elements (types_htab);
8d749320
SM
5624 dwarf2_per_objfile->all_type_units =
5625 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
5626 iter = &dwarf2_per_objfile->all_type_units[0];
5627 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
5628 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
5629 == dwarf2_per_objfile->n_type_units);
1fd400ff 5630
348e048f
DE
5631 return 1;
5632}
5633
6aa5f3a6
DE
5634/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
5635 If SLOT is non-NULL, it is the entry to use in the hash table.
5636 Otherwise we find one. */
5637
5638static struct signatured_type *
5639add_type_unit (ULONGEST sig, void **slot)
5640{
5641 struct objfile *objfile = dwarf2_per_objfile->objfile;
5642 int n_type_units = dwarf2_per_objfile->n_type_units;
5643 struct signatured_type *sig_type;
5644
5645 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
5646 ++n_type_units;
5647 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
5648 {
5649 if (dwarf2_per_objfile->n_allocated_type_units == 0)
5650 dwarf2_per_objfile->n_allocated_type_units = 1;
5651 dwarf2_per_objfile->n_allocated_type_units *= 2;
5652 dwarf2_per_objfile->all_type_units
224c3ddb
SM
5653 = XRESIZEVEC (struct signatured_type *,
5654 dwarf2_per_objfile->all_type_units,
5655 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
5656 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5657 }
5658 dwarf2_per_objfile->n_type_units = n_type_units;
5659
5660 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5661 struct signatured_type);
5662 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5663 sig_type->signature = sig;
5664 sig_type->per_cu.is_debug_types = 1;
5665 if (dwarf2_per_objfile->using_index)
5666 {
5667 sig_type->per_cu.v.quick =
5668 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5669 struct dwarf2_per_cu_quick_data);
5670 }
5671
5672 if (slot == NULL)
5673 {
5674 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5675 sig_type, INSERT);
5676 }
5677 gdb_assert (*slot == NULL);
5678 *slot = sig_type;
5679 /* The rest of sig_type must be filled in by the caller. */
5680 return sig_type;
5681}
5682
a2ce51a0
DE
5683/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5684 Fill in SIG_ENTRY with DWO_ENTRY. */
5685
5686static void
5687fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5688 struct signatured_type *sig_entry,
5689 struct dwo_unit *dwo_entry)
5690{
7ee85ab1 5691 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
5692 gdb_assert (! sig_entry->per_cu.queued);
5693 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
5694 if (dwarf2_per_objfile->using_index)
5695 {
5696 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 5697 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
5698 }
5699 else
5700 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 5701 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 5702 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 5703 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
5704 gdb_assert (sig_entry->dwo_unit == NULL);
5705
5706 sig_entry->per_cu.section = dwo_entry->section;
9c541725 5707 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
5708 sig_entry->per_cu.length = dwo_entry->length;
5709 sig_entry->per_cu.reading_dwo_directly = 1;
5710 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
5711 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5712 sig_entry->dwo_unit = dwo_entry;
5713}
5714
5715/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
5716 If we haven't read the TU yet, create the signatured_type data structure
5717 for a TU to be read in directly from a DWO file, bypassing the stub.
5718 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5719 using .gdb_index, then when reading a CU we want to stay in the DWO file
5720 containing that CU. Otherwise we could end up reading several other DWO
5721 files (due to comdat folding) to process the transitive closure of all the
5722 mentioned TUs, and that can be slow. The current DWO file will have every
5723 type signature that it needs.
a2ce51a0
DE
5724 We only do this for .gdb_index because in the psymtab case we already have
5725 to read all the DWOs to build the type unit groups. */
5726
5727static struct signatured_type *
5728lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5729{
5730 struct objfile *objfile = dwarf2_per_objfile->objfile;
5731 struct dwo_file *dwo_file;
5732 struct dwo_unit find_dwo_entry, *dwo_entry;
5733 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5734 void **slot;
a2ce51a0
DE
5735
5736 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5737
6aa5f3a6
DE
5738 /* If TU skeletons have been removed then we may not have read in any
5739 TUs yet. */
5740 if (dwarf2_per_objfile->signatured_types == NULL)
5741 {
5742 dwarf2_per_objfile->signatured_types
5743 = allocate_signatured_type_table (objfile);
5744 }
a2ce51a0
DE
5745
5746 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
5747 Use the global signatured_types array to do our own comdat-folding
5748 of types. If this is the first time we're reading this TU, and
5749 the TU has an entry in .gdb_index, replace the recorded data from
5750 .gdb_index with this TU. */
a2ce51a0 5751
a2ce51a0 5752 find_sig_entry.signature = sig;
6aa5f3a6
DE
5753 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5754 &find_sig_entry, INSERT);
9a3c8263 5755 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
5756
5757 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
5758 read. Don't reassign the global entry to point to this DWO if that's
5759 the case. Also note that if the TU is already being read, it may not
5760 have come from a DWO, the program may be a mix of Fission-compiled
5761 code and non-Fission-compiled code. */
5762
5763 /* Have we already tried to read this TU?
5764 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5765 needn't exist in the global table yet). */
5766 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
5767 return sig_entry;
5768
6aa5f3a6
DE
5769 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5770 dwo_unit of the TU itself. */
5771 dwo_file = cu->dwo_unit->dwo_file;
5772
a2ce51a0
DE
5773 /* Ok, this is the first time we're reading this TU. */
5774 if (dwo_file->tus == NULL)
5775 return NULL;
5776 find_dwo_entry.signature = sig;
9a3c8263 5777 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
5778 if (dwo_entry == NULL)
5779 return NULL;
5780
6aa5f3a6
DE
5781 /* If the global table doesn't have an entry for this TU, add one. */
5782 if (sig_entry == NULL)
5783 sig_entry = add_type_unit (sig, slot);
5784
a2ce51a0 5785 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 5786 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
5787 return sig_entry;
5788}
5789
a2ce51a0
DE
5790/* Subroutine of lookup_signatured_type.
5791 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
5792 then try the DWP file. If the TU stub (skeleton) has been removed then
5793 it won't be in .gdb_index. */
a2ce51a0
DE
5794
5795static struct signatured_type *
5796lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5797{
5798 struct objfile *objfile = dwarf2_per_objfile->objfile;
5799 struct dwp_file *dwp_file = get_dwp_file ();
5800 struct dwo_unit *dwo_entry;
5801 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5802 void **slot;
a2ce51a0
DE
5803
5804 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5805 gdb_assert (dwp_file != NULL);
5806
6aa5f3a6
DE
5807 /* If TU skeletons have been removed then we may not have read in any
5808 TUs yet. */
5809 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 5810 {
6aa5f3a6
DE
5811 dwarf2_per_objfile->signatured_types
5812 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
5813 }
5814
6aa5f3a6
DE
5815 find_sig_entry.signature = sig;
5816 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5817 &find_sig_entry, INSERT);
9a3c8263 5818 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
5819
5820 /* Have we already tried to read this TU?
5821 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5822 needn't exist in the global table yet). */
5823 if (sig_entry != NULL)
5824 return sig_entry;
5825
a2ce51a0
DE
5826 if (dwp_file->tus == NULL)
5827 return NULL;
57d63ce2
DE
5828 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5829 sig, 1 /* is_debug_types */);
a2ce51a0
DE
5830 if (dwo_entry == NULL)
5831 return NULL;
5832
6aa5f3a6 5833 sig_entry = add_type_unit (sig, slot);
a2ce51a0
DE
5834 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5835
a2ce51a0
DE
5836 return sig_entry;
5837}
5838
380bca97 5839/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
5840 Returns NULL if signature SIG is not present in the table.
5841 It is up to the caller to complain about this. */
348e048f
DE
5842
5843static struct signatured_type *
a2ce51a0 5844lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 5845{
a2ce51a0
DE
5846 if (cu->dwo_unit
5847 && dwarf2_per_objfile->using_index)
5848 {
5849 /* We're in a DWO/DWP file, and we're using .gdb_index.
5850 These cases require special processing. */
5851 if (get_dwp_file () == NULL)
5852 return lookup_dwo_signatured_type (cu, sig);
5853 else
5854 return lookup_dwp_signatured_type (cu, sig);
5855 }
5856 else
5857 {
5858 struct signatured_type find_entry, *entry;
348e048f 5859
a2ce51a0
DE
5860 if (dwarf2_per_objfile->signatured_types == NULL)
5861 return NULL;
5862 find_entry.signature = sig;
9a3c8263
SM
5863 entry = ((struct signatured_type *)
5864 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
5865 return entry;
5866 }
348e048f 5867}
42e7ad6c
DE
5868\f
5869/* Low level DIE reading support. */
348e048f 5870
d85a05f0
DJ
5871/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5872
5873static void
5874init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 5875 struct dwarf2_cu *cu,
3019eac3
DE
5876 struct dwarf2_section_info *section,
5877 struct dwo_file *dwo_file)
d85a05f0 5878{
fceca515 5879 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 5880 reader->abfd = get_section_bfd_owner (section);
d85a05f0 5881 reader->cu = cu;
3019eac3 5882 reader->dwo_file = dwo_file;
dee91e82
DE
5883 reader->die_section = section;
5884 reader->buffer = section->buffer;
f664829e 5885 reader->buffer_end = section->buffer + section->size;
a2ce51a0 5886 reader->comp_dir = NULL;
d85a05f0
DJ
5887}
5888
b0c7bfa9
DE
5889/* Subroutine of init_cutu_and_read_dies to simplify it.
5890 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5891 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5892 already.
5893
5894 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5895 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
5896 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5897 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
5898 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5899 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
5900 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5901 are filled in with the info of the DIE from the DWO file.
5902 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5903 provided an abbrev table to use.
5904 The result is non-zero if a valid (non-dummy) DIE was found. */
5905
5906static int
5907read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5908 struct dwo_unit *dwo_unit,
5909 int abbrev_table_provided,
5910 struct die_info *stub_comp_unit_die,
a2ce51a0 5911 const char *stub_comp_dir,
b0c7bfa9 5912 struct die_reader_specs *result_reader,
d521ce57 5913 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
5914 struct die_info **result_comp_unit_die,
5915 int *result_has_children)
5916{
5917 struct objfile *objfile = dwarf2_per_objfile->objfile;
5918 struct dwarf2_cu *cu = this_cu->cu;
5919 struct dwarf2_section_info *section;
5920 bfd *abfd;
d521ce57 5921 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
5922 ULONGEST signature; /* Or dwo_id. */
5923 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5924 int i,num_extra_attrs;
5925 struct dwarf2_section_info *dwo_abbrev_section;
5926 struct attribute *attr;
5927 struct die_info *comp_unit_die;
5928
b0aeadb3
DE
5929 /* At most one of these may be provided. */
5930 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 5931
b0c7bfa9
DE
5932 /* These attributes aren't processed until later:
5933 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
5934 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5935 referenced later. However, these attributes are found in the stub
5936 which we won't have later. In order to not impose this complication
5937 on the rest of the code, we read them here and copy them to the
5938 DWO CU/TU die. */
b0c7bfa9
DE
5939
5940 stmt_list = NULL;
5941 low_pc = NULL;
5942 high_pc = NULL;
5943 ranges = NULL;
5944 comp_dir = NULL;
5945
5946 if (stub_comp_unit_die != NULL)
5947 {
5948 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5949 DWO file. */
5950 if (! this_cu->is_debug_types)
5951 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5952 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5953 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5954 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5955 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5956
5957 /* There should be a DW_AT_addr_base attribute here (if needed).
5958 We need the value before we can process DW_FORM_GNU_addr_index. */
5959 cu->addr_base = 0;
5960 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5961 if (attr)
5962 cu->addr_base = DW_UNSND (attr);
5963
5964 /* There should be a DW_AT_ranges_base attribute here (if needed).
5965 We need the value before we can process DW_AT_ranges. */
5966 cu->ranges_base = 0;
5967 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5968 if (attr)
5969 cu->ranges_base = DW_UNSND (attr);
5970 }
a2ce51a0
DE
5971 else if (stub_comp_dir != NULL)
5972 {
5973 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 5974 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
5975 comp_dir->name = DW_AT_comp_dir;
5976 comp_dir->form = DW_FORM_string;
5977 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5978 DW_STRING (comp_dir) = stub_comp_dir;
5979 }
b0c7bfa9
DE
5980
5981 /* Set up for reading the DWO CU/TU. */
5982 cu->dwo_unit = dwo_unit;
5983 section = dwo_unit->section;
5984 dwarf2_read_section (objfile, section);
a32a8923 5985 abfd = get_section_bfd_owner (section);
9c541725
PA
5986 begin_info_ptr = info_ptr = (section->buffer
5987 + to_underlying (dwo_unit->sect_off));
b0c7bfa9
DE
5988 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5989 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5990
5991 if (this_cu->is_debug_types)
5992 {
b0c7bfa9
DE
5993 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5994
43988095 5995 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
b0c7bfa9 5996 dwo_abbrev_section,
43988095 5997 info_ptr, rcuh_kind::TYPE);
a2ce51a0 5998 /* This is not an assert because it can be caused by bad debug info. */
43988095 5999 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6000 {
6001 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6002 " TU at offset 0x%x [in module %s]"),
6003 hex_string (sig_type->signature),
43988095 6004 hex_string (cu->header.signature),
9c541725 6005 to_underlying (dwo_unit->sect_off),
a2ce51a0
DE
6006 bfd_get_filename (abfd));
6007 }
9c541725 6008 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6009 /* For DWOs coming from DWP files, we don't know the CU length
6010 nor the type's offset in the TU until now. */
6011 dwo_unit->length = get_cu_length (&cu->header);
9c541725 6012 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6013
6014 /* Establish the type offset that can be used to lookup the type.
6015 For DWO files, we don't know it until now. */
9c541725
PA
6016 sig_type->type_offset_in_section
6017 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6018 }
6019 else
6020 {
6021 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6022 dwo_abbrev_section,
43988095 6023 info_ptr, rcuh_kind::COMPILE);
9c541725 6024 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6025 /* For DWOs coming from DWP files, we don't know the CU length
6026 until now. */
6027 dwo_unit->length = get_cu_length (&cu->header);
6028 }
6029
02142a6c
DE
6030 /* Replace the CU's original abbrev table with the DWO's.
6031 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
6032 if (abbrev_table_provided)
6033 {
6034 /* Don't free the provided abbrev table, the caller of
6035 init_cutu_and_read_dies owns it. */
6036 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 6037 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
6038 make_cleanup (dwarf2_free_abbrev_table, cu);
6039 }
6040 else
6041 {
6042 dwarf2_free_abbrev_table (cu);
6043 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 6044 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
6045 }
6046
6047 /* Read in the die, but leave space to copy over the attributes
6048 from the stub. This has the benefit of simplifying the rest of
6049 the code - all the work to maintain the illusion of a single
6050 DW_TAG_{compile,type}_unit DIE is done here. */
6051 num_extra_attrs = ((stmt_list != NULL)
6052 + (low_pc != NULL)
6053 + (high_pc != NULL)
6054 + (ranges != NULL)
6055 + (comp_dir != NULL));
6056 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6057 result_has_children, num_extra_attrs);
6058
6059 /* Copy over the attributes from the stub to the DIE we just read in. */
6060 comp_unit_die = *result_comp_unit_die;
6061 i = comp_unit_die->num_attrs;
6062 if (stmt_list != NULL)
6063 comp_unit_die->attrs[i++] = *stmt_list;
6064 if (low_pc != NULL)
6065 comp_unit_die->attrs[i++] = *low_pc;
6066 if (high_pc != NULL)
6067 comp_unit_die->attrs[i++] = *high_pc;
6068 if (ranges != NULL)
6069 comp_unit_die->attrs[i++] = *ranges;
6070 if (comp_dir != NULL)
6071 comp_unit_die->attrs[i++] = *comp_dir;
6072 comp_unit_die->num_attrs += num_extra_attrs;
6073
b4f54984 6074 if (dwarf_die_debug)
bf6af496
DE
6075 {
6076 fprintf_unfiltered (gdb_stdlog,
6077 "Read die from %s@0x%x of %s:\n",
a32a8923 6078 get_section_name (section),
bf6af496
DE
6079 (unsigned) (begin_info_ptr - section->buffer),
6080 bfd_get_filename (abfd));
b4f54984 6081 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6082 }
6083
a2ce51a0
DE
6084 /* Save the comp_dir attribute. If there is no DWP file then we'll read
6085 TUs by skipping the stub and going directly to the entry in the DWO file.
6086 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
6087 to get it via circuitous means. Blech. */
6088 if (comp_dir != NULL)
6089 result_reader->comp_dir = DW_STRING (comp_dir);
6090
b0c7bfa9
DE
6091 /* Skip dummy compilation units. */
6092 if (info_ptr >= begin_info_ptr + dwo_unit->length
6093 || peek_abbrev_code (abfd, info_ptr) == 0)
6094 return 0;
6095
6096 *result_info_ptr = info_ptr;
6097 return 1;
6098}
6099
6100/* Subroutine of init_cutu_and_read_dies to simplify it.
6101 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6102 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6103
6104static struct dwo_unit *
6105lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6106 struct die_info *comp_unit_die)
6107{
6108 struct dwarf2_cu *cu = this_cu->cu;
6109 struct attribute *attr;
6110 ULONGEST signature;
6111 struct dwo_unit *dwo_unit;
6112 const char *comp_dir, *dwo_name;
6113
a2ce51a0
DE
6114 gdb_assert (cu != NULL);
6115
b0c7bfa9 6116 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
6117 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
6118 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6119
6120 if (this_cu->is_debug_types)
6121 {
6122 struct signatured_type *sig_type;
6123
6124 /* Since this_cu is the first member of struct signatured_type,
6125 we can go from a pointer to one to a pointer to the other. */
6126 sig_type = (struct signatured_type *) this_cu;
6127 signature = sig_type->signature;
6128 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6129 }
6130 else
6131 {
6132 struct attribute *attr;
6133
6134 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6135 if (! attr)
6136 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6137 " [in module %s]"),
4262abfb 6138 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
6139 signature = DW_UNSND (attr);
6140 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6141 signature);
6142 }
6143
b0c7bfa9
DE
6144 return dwo_unit;
6145}
6146
a2ce51a0 6147/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6
DE
6148 See it for a description of the parameters.
6149 Read a TU directly from a DWO file, bypassing the stub.
6150
6151 Note: This function could be a little bit simpler if we shared cleanups
6152 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
6153 to do, so we keep this function self-contained. Or we could move this
6154 into our caller, but it's complex enough already. */
a2ce51a0
DE
6155
6156static void
6aa5f3a6
DE
6157init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6158 int use_existing_cu, int keep,
a2ce51a0
DE
6159 die_reader_func_ftype *die_reader_func,
6160 void *data)
6161{
6162 struct dwarf2_cu *cu;
6163 struct signatured_type *sig_type;
6aa5f3a6 6164 struct cleanup *cleanups, *free_cu_cleanup = NULL;
a2ce51a0
DE
6165 struct die_reader_specs reader;
6166 const gdb_byte *info_ptr;
6167 struct die_info *comp_unit_die;
6168 int has_children;
6169
6170 /* Verify we can do the following downcast, and that we have the
6171 data we need. */
6172 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6173 sig_type = (struct signatured_type *) this_cu;
6174 gdb_assert (sig_type->dwo_unit != NULL);
6175
6176 cleanups = make_cleanup (null_cleanup, NULL);
6177
6aa5f3a6
DE
6178 if (use_existing_cu && this_cu->cu != NULL)
6179 {
6180 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6181 cu = this_cu->cu;
6182 /* There's no need to do the rereading_dwo_cu handling that
6183 init_cutu_and_read_dies does since we don't read the stub. */
6184 }
6185 else
6186 {
6187 /* If !use_existing_cu, this_cu->cu must be NULL. */
6188 gdb_assert (this_cu->cu == NULL);
8d749320 6189 cu = XNEW (struct dwarf2_cu);
6aa5f3a6
DE
6190 init_one_comp_unit (cu, this_cu);
6191 /* If an error occurs while loading, release our storage. */
6192 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
6193 }
6194
6195 /* A future optimization, if needed, would be to use an existing
6196 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6197 could share abbrev tables. */
a2ce51a0
DE
6198
6199 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6200 0 /* abbrev_table_provided */,
6201 NULL /* stub_comp_unit_die */,
6202 sig_type->dwo_unit->dwo_file->comp_dir,
6203 &reader, &info_ptr,
6204 &comp_unit_die, &has_children) == 0)
6205 {
6206 /* Dummy die. */
6207 do_cleanups (cleanups);
6208 return;
6209 }
6210
6211 /* All the "real" work is done here. */
6212 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6213
6aa5f3a6 6214 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
6215 but the alternative is making the latter more complex.
6216 This function is only for the special case of using DWO files directly:
6217 no point in overly complicating the general case just to handle this. */
6aa5f3a6 6218 if (free_cu_cleanup != NULL)
a2ce51a0 6219 {
6aa5f3a6
DE
6220 if (keep)
6221 {
6222 /* We've successfully allocated this compilation unit. Let our
6223 caller clean it up when finished with it. */
6224 discard_cleanups (free_cu_cleanup);
a2ce51a0 6225
6aa5f3a6
DE
6226 /* We can only discard free_cu_cleanup and all subsequent cleanups.
6227 So we have to manually free the abbrev table. */
6228 dwarf2_free_abbrev_table (cu);
a2ce51a0 6229
6aa5f3a6
DE
6230 /* Link this CU into read_in_chain. */
6231 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6232 dwarf2_per_objfile->read_in_chain = this_cu;
6233 }
6234 else
6235 do_cleanups (free_cu_cleanup);
a2ce51a0 6236 }
a2ce51a0
DE
6237
6238 do_cleanups (cleanups);
6239}
6240
fd820528 6241/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6242 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6243
f4dc4d17
DE
6244 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6245 Otherwise the table specified in the comp unit header is read in and used.
6246 This is an optimization for when we already have the abbrev table.
6247
dee91e82
DE
6248 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6249 Otherwise, a new CU is allocated with xmalloc.
6250
6251 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
6252 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
6253
6254 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 6255 linker) then DIE_READER_FUNC will not get called. */
aaa75496 6256
70221824 6257static void
fd820528 6258init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 6259 struct abbrev_table *abbrev_table,
fd820528
DE
6260 int use_existing_cu, int keep,
6261 die_reader_func_ftype *die_reader_func,
6262 void *data)
c906108c 6263{
dee91e82 6264 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6265 struct dwarf2_section_info *section = this_cu->section;
a32a8923 6266 bfd *abfd = get_section_bfd_owner (section);
dee91e82 6267 struct dwarf2_cu *cu;
d521ce57 6268 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 6269 struct die_reader_specs reader;
d85a05f0 6270 struct die_info *comp_unit_die;
dee91e82 6271 int has_children;
d85a05f0 6272 struct attribute *attr;
365156ad 6273 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 6274 struct signatured_type *sig_type = NULL;
4bdcc0c1 6275 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6276 /* Non-zero if CU currently points to a DWO file and we need to
6277 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6278 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6279 int rereading_dwo_cu = 0;
c906108c 6280
b4f54984 6281 if (dwarf_die_debug)
09406207
DE
6282 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
6283 this_cu->is_debug_types ? "type" : "comp",
9c541725 6284 to_underlying (this_cu->sect_off));
09406207 6285
dee91e82
DE
6286 if (use_existing_cu)
6287 gdb_assert (keep);
23745b47 6288
a2ce51a0
DE
6289 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6290 file (instead of going through the stub), short-circuit all of this. */
6291 if (this_cu->reading_dwo_directly)
6292 {
6293 /* Narrow down the scope of possibilities to have to understand. */
6294 gdb_assert (this_cu->is_debug_types);
6295 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
6296 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
6297 die_reader_func, data);
a2ce51a0
DE
6298 return;
6299 }
6300
dee91e82
DE
6301 cleanups = make_cleanup (null_cleanup, NULL);
6302
6303 /* This is cheap if the section is already read in. */
6304 dwarf2_read_section (objfile, section);
6305
9c541725 6306 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6307
6308 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6309
6310 if (use_existing_cu && this_cu->cu != NULL)
6311 {
6312 cu = this_cu->cu;
42e7ad6c
DE
6313 /* If this CU is from a DWO file we need to start over, we need to
6314 refetch the attributes from the skeleton CU.
6315 This could be optimized by retrieving those attributes from when we
6316 were here the first time: the previous comp_unit_die was stored in
6317 comp_unit_obstack. But there's no data yet that we need this
6318 optimization. */
6319 if (cu->dwo_unit != NULL)
6320 rereading_dwo_cu = 1;
dee91e82
DE
6321 }
6322 else
6323 {
6324 /* If !use_existing_cu, this_cu->cu must be NULL. */
6325 gdb_assert (this_cu->cu == NULL);
8d749320 6326 cu = XNEW (struct dwarf2_cu);
dee91e82 6327 init_one_comp_unit (cu, this_cu);
dee91e82 6328 /* If an error occurs while loading, release our storage. */
365156ad 6329 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 6330 }
dee91e82 6331
b0c7bfa9 6332 /* Get the header. */
9c541725 6333 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6334 {
6335 /* We already have the header, there's no need to read it in again. */
9c541725 6336 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6337 }
6338 else
6339 {
3019eac3 6340 if (this_cu->is_debug_types)
dee91e82 6341 {
43988095 6342 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4bdcc0c1 6343 abbrev_section, info_ptr,
43988095 6344 rcuh_kind::TYPE);
dee91e82 6345
42e7ad6c
DE
6346 /* Since per_cu is the first member of struct signatured_type,
6347 we can go from a pointer to one to a pointer to the other. */
6348 sig_type = (struct signatured_type *) this_cu;
43988095 6349 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6350 gdb_assert (sig_type->type_offset_in_tu
6351 == cu->header.type_cu_offset_in_tu);
6352 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6353
42e7ad6c
DE
6354 /* LENGTH has not been set yet for type units if we're
6355 using .gdb_index. */
1ce1cefd 6356 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
6357
6358 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6359 sig_type->type_offset_in_section =
6360 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6361
6362 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6363 }
6364 else
6365 {
4bdcc0c1
DE
6366 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6367 abbrev_section,
43988095
JK
6368 info_ptr,
6369 rcuh_kind::COMPILE);
dee91e82 6370
9c541725 6371 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 6372 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 6373 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6374 }
6375 }
10b3939b 6376
6caca83c 6377 /* Skip dummy compilation units. */
dee91e82 6378 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
6379 || peek_abbrev_code (abfd, info_ptr) == 0)
6380 {
dee91e82 6381 do_cleanups (cleanups);
21b2bd31 6382 return;
6caca83c
CC
6383 }
6384
433df2d4
DE
6385 /* If we don't have them yet, read the abbrevs for this compilation unit.
6386 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
6387 done. Note that it's important that if the CU had an abbrev table
6388 on entry we don't free it when we're done: Somewhere up the call stack
6389 it may be in use. */
f4dc4d17
DE
6390 if (abbrev_table != NULL)
6391 {
6392 gdb_assert (cu->abbrev_table == NULL);
9c541725 6393 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
f4dc4d17
DE
6394 cu->abbrev_table = abbrev_table;
6395 }
6396 else if (cu->abbrev_table == NULL)
dee91e82 6397 {
4bdcc0c1 6398 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
6399 make_cleanup (dwarf2_free_abbrev_table, cu);
6400 }
42e7ad6c
DE
6401 else if (rereading_dwo_cu)
6402 {
6403 dwarf2_free_abbrev_table (cu);
6404 dwarf2_read_abbrevs (cu, abbrev_section);
6405 }
af703f96 6406
dee91e82 6407 /* Read the top level CU/TU die. */
3019eac3 6408 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 6409 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 6410
b0c7bfa9
DE
6411 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6412 from the DWO file.
6413 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6414 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
6415 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
6416 if (attr)
6417 {
3019eac3 6418 struct dwo_unit *dwo_unit;
b0c7bfa9 6419 struct die_info *dwo_comp_unit_die;
3019eac3
DE
6420
6421 if (has_children)
6a506a2d
DE
6422 {
6423 complaint (&symfile_complaints,
6424 _("compilation unit with DW_AT_GNU_dwo_name"
6425 " has children (offset 0x%x) [in module %s]"),
9c541725 6426 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6a506a2d 6427 }
b0c7bfa9 6428 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 6429 if (dwo_unit != NULL)
3019eac3 6430 {
6a506a2d
DE
6431 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6432 abbrev_table != NULL,
a2ce51a0 6433 comp_unit_die, NULL,
6a506a2d
DE
6434 &reader, &info_ptr,
6435 &dwo_comp_unit_die, &has_children) == 0)
6436 {
6437 /* Dummy die. */
6438 do_cleanups (cleanups);
6439 return;
6440 }
6441 comp_unit_die = dwo_comp_unit_die;
6442 }
6443 else
6444 {
6445 /* Yikes, we couldn't find the rest of the DIE, we only have
6446 the stub. A complaint has already been logged. There's
6447 not much more we can do except pass on the stub DIE to
6448 die_reader_func. We don't want to throw an error on bad
6449 debug info. */
3019eac3
DE
6450 }
6451 }
6452
b0c7bfa9 6453 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
6454 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6455
b0c7bfa9 6456 /* Done, clean up. */
365156ad 6457 if (free_cu_cleanup != NULL)
348e048f 6458 {
365156ad
TT
6459 if (keep)
6460 {
6461 /* We've successfully allocated this compilation unit. Let our
6462 caller clean it up when finished with it. */
6463 discard_cleanups (free_cu_cleanup);
dee91e82 6464
365156ad
TT
6465 /* We can only discard free_cu_cleanup and all subsequent cleanups.
6466 So we have to manually free the abbrev table. */
6467 dwarf2_free_abbrev_table (cu);
dee91e82 6468
365156ad
TT
6469 /* Link this CU into read_in_chain. */
6470 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6471 dwarf2_per_objfile->read_in_chain = this_cu;
6472 }
6473 else
6474 do_cleanups (free_cu_cleanup);
348e048f 6475 }
365156ad
TT
6476
6477 do_cleanups (cleanups);
dee91e82
DE
6478}
6479
33e80786
DE
6480/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
6481 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
6482 to have already done the lookup to find the DWO file).
dee91e82
DE
6483
6484 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 6485 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
6486
6487 We fill in THIS_CU->length.
6488
6489 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
6490 linker) then DIE_READER_FUNC will not get called.
6491
6492 THIS_CU->cu is always freed when done.
3019eac3
DE
6493 This is done in order to not leave THIS_CU->cu in a state where we have
6494 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
6495
6496static void
6497init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 6498 struct dwo_file *dwo_file,
dee91e82
DE
6499 die_reader_func_ftype *die_reader_func,
6500 void *data)
6501{
6502 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6503 struct dwarf2_section_info *section = this_cu->section;
a32a8923 6504 bfd *abfd = get_section_bfd_owner (section);
33e80786 6505 struct dwarf2_section_info *abbrev_section;
dee91e82 6506 struct dwarf2_cu cu;
d521ce57 6507 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
6508 struct die_reader_specs reader;
6509 struct cleanup *cleanups;
6510 struct die_info *comp_unit_die;
6511 int has_children;
6512
b4f54984 6513 if (dwarf_die_debug)
09406207
DE
6514 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
6515 this_cu->is_debug_types ? "type" : "comp",
9c541725 6516 to_underlying (this_cu->sect_off));
09406207 6517
dee91e82
DE
6518 gdb_assert (this_cu->cu == NULL);
6519
33e80786
DE
6520 abbrev_section = (dwo_file != NULL
6521 ? &dwo_file->sections.abbrev
6522 : get_abbrev_section_for_cu (this_cu));
6523
dee91e82
DE
6524 /* This is cheap if the section is already read in. */
6525 dwarf2_read_section (objfile, section);
6526
6527 init_one_comp_unit (&cu, this_cu);
6528
6529 cleanups = make_cleanup (free_stack_comp_unit, &cu);
6530
9c541725 6531 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4bdcc0c1
DE
6532 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
6533 abbrev_section, info_ptr,
43988095
JK
6534 (this_cu->is_debug_types
6535 ? rcuh_kind::TYPE
6536 : rcuh_kind::COMPILE));
dee91e82 6537
1ce1cefd 6538 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
6539
6540 /* Skip dummy compilation units. */
6541 if (info_ptr >= begin_info_ptr + this_cu->length
6542 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 6543 {
dee91e82 6544 do_cleanups (cleanups);
21b2bd31 6545 return;
93311388 6546 }
72bf9492 6547
dee91e82
DE
6548 dwarf2_read_abbrevs (&cu, abbrev_section);
6549 make_cleanup (dwarf2_free_abbrev_table, &cu);
6550
3019eac3 6551 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
6552 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
6553
6554 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6555
6556 do_cleanups (cleanups);
6557}
6558
3019eac3
DE
6559/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
6560 does not lookup the specified DWO file.
6561 This cannot be used to read DWO files.
dee91e82
DE
6562
6563 THIS_CU->cu is always freed when done.
3019eac3
DE
6564 This is done in order to not leave THIS_CU->cu in a state where we have
6565 to care whether it refers to the "main" CU or the DWO CU.
6566 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
6567
6568static void
6569init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
6570 die_reader_func_ftype *die_reader_func,
6571 void *data)
6572{
33e80786 6573 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 6574}
0018ea6f
DE
6575\f
6576/* Type Unit Groups.
dee91e82 6577
0018ea6f
DE
6578 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6579 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6580 so that all types coming from the same compilation (.o file) are grouped
6581 together. A future step could be to put the types in the same symtab as
6582 the CU the types ultimately came from. */
ff013f42 6583
f4dc4d17
DE
6584static hashval_t
6585hash_type_unit_group (const void *item)
6586{
9a3c8263
SM
6587 const struct type_unit_group *tu_group
6588 = (const struct type_unit_group *) item;
f4dc4d17 6589
094b34ac 6590 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 6591}
348e048f
DE
6592
6593static int
f4dc4d17 6594eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 6595{
9a3c8263
SM
6596 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6597 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 6598
094b34ac 6599 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 6600}
348e048f 6601
f4dc4d17
DE
6602/* Allocate a hash table for type unit groups. */
6603
6604static htab_t
6605allocate_type_unit_groups_table (void)
6606{
6607 return htab_create_alloc_ex (3,
6608 hash_type_unit_group,
6609 eq_type_unit_group,
6610 NULL,
6611 &dwarf2_per_objfile->objfile->objfile_obstack,
6612 hashtab_obstack_allocate,
6613 dummy_obstack_deallocate);
6614}
dee91e82 6615
f4dc4d17
DE
6616/* Type units that don't have DW_AT_stmt_list are grouped into their own
6617 partial symtabs. We combine several TUs per psymtab to not let the size
6618 of any one psymtab grow too big. */
6619#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6620#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 6621
094b34ac 6622/* Helper routine for get_type_unit_group.
f4dc4d17
DE
6623 Create the type_unit_group object used to hold one or more TUs. */
6624
6625static struct type_unit_group *
094b34ac 6626create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
6627{
6628 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 6629 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 6630 struct type_unit_group *tu_group;
f4dc4d17
DE
6631
6632 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6633 struct type_unit_group);
094b34ac 6634 per_cu = &tu_group->per_cu;
f4dc4d17 6635 per_cu->objfile = objfile;
f4dc4d17 6636
094b34ac
DE
6637 if (dwarf2_per_objfile->using_index)
6638 {
6639 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6640 struct dwarf2_per_cu_quick_data);
094b34ac
DE
6641 }
6642 else
6643 {
9c541725 6644 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
6645 struct partial_symtab *pst;
6646 char *name;
6647
6648 /* Give the symtab a useful name for debug purposes. */
6649 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6650 name = xstrprintf ("<type_units_%d>",
6651 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6652 else
6653 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
6654
6655 pst = create_partial_symtab (per_cu, name);
6656 pst->anonymous = 1;
f4dc4d17 6657
094b34ac
DE
6658 xfree (name);
6659 }
f4dc4d17 6660
094b34ac 6661 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 6662 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
6663
6664 return tu_group;
6665}
6666
094b34ac
DE
6667/* Look up the type_unit_group for type unit CU, and create it if necessary.
6668 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
6669
6670static struct type_unit_group *
ff39bb5e 6671get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
6672{
6673 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6674 struct type_unit_group *tu_group;
6675 void **slot;
6676 unsigned int line_offset;
6677 struct type_unit_group type_unit_group_for_lookup;
6678
6679 if (dwarf2_per_objfile->type_unit_groups == NULL)
6680 {
6681 dwarf2_per_objfile->type_unit_groups =
6682 allocate_type_unit_groups_table ();
6683 }
6684
6685 /* Do we need to create a new group, or can we use an existing one? */
6686
6687 if (stmt_list)
6688 {
6689 line_offset = DW_UNSND (stmt_list);
6690 ++tu_stats->nr_symtab_sharers;
6691 }
6692 else
6693 {
6694 /* Ugh, no stmt_list. Rare, but we have to handle it.
6695 We can do various things here like create one group per TU or
6696 spread them over multiple groups to split up the expansion work.
6697 To avoid worst case scenarios (too many groups or too large groups)
6698 we, umm, group them in bunches. */
6699 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6700 | (tu_stats->nr_stmt_less_type_units
6701 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6702 ++tu_stats->nr_stmt_less_type_units;
6703 }
6704
094b34ac 6705 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 6706 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
6707 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6708 &type_unit_group_for_lookup, INSERT);
6709 if (*slot != NULL)
6710 {
9a3c8263 6711 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
6712 gdb_assert (tu_group != NULL);
6713 }
6714 else
6715 {
9c541725 6716 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 6717 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
6718 *slot = tu_group;
6719 ++tu_stats->nr_symtabs;
6720 }
6721
6722 return tu_group;
6723}
0018ea6f
DE
6724\f
6725/* Partial symbol tables. */
6726
6727/* Create a psymtab named NAME and assign it to PER_CU.
6728
6729 The caller must fill in the following details:
6730 dirname, textlow, texthigh. */
6731
6732static struct partial_symtab *
6733create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
6734{
6735 struct objfile *objfile = per_cu->objfile;
6736 struct partial_symtab *pst;
6737
18a94d75 6738 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
6739 objfile->global_psymbols,
6740 objfile->static_psymbols);
0018ea6f
DE
6741
6742 pst->psymtabs_addrmap_supported = 1;
6743
6744 /* This is the glue that links PST into GDB's symbol API. */
6745 pst->read_symtab_private = per_cu;
6746 pst->read_symtab = dwarf2_read_symtab;
6747 per_cu->v.psymtab = pst;
6748
6749 return pst;
6750}
6751
b93601f3
TT
6752/* The DATA object passed to process_psymtab_comp_unit_reader has this
6753 type. */
6754
6755struct process_psymtab_comp_unit_data
6756{
6757 /* True if we are reading a DW_TAG_partial_unit. */
6758
6759 int want_partial_unit;
6760
6761 /* The "pretend" language that is used if the CU doesn't declare a
6762 language. */
6763
6764 enum language pretend_language;
6765};
6766
0018ea6f
DE
6767/* die_reader_func for process_psymtab_comp_unit. */
6768
6769static void
6770process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6771 const gdb_byte *info_ptr,
0018ea6f
DE
6772 struct die_info *comp_unit_die,
6773 int has_children,
6774 void *data)
6775{
6776 struct dwarf2_cu *cu = reader->cu;
6777 struct objfile *objfile = cu->objfile;
3e29f34a 6778 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 6779 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
6780 CORE_ADDR baseaddr;
6781 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6782 struct partial_symtab *pst;
3a2b436a 6783 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 6784 const char *filename;
9a3c8263
SM
6785 struct process_psymtab_comp_unit_data *info
6786 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 6787
b93601f3 6788 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
6789 return;
6790
6791 gdb_assert (! per_cu->is_debug_types);
6792
b93601f3 6793 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
6794
6795 cu->list_in_scope = &file_symbols;
6796
6797 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
6798 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6799 if (filename == NULL)
0018ea6f 6800 filename = "";
0018ea6f
DE
6801
6802 pst = create_partial_symtab (per_cu, filename);
6803
6804 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 6805 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
6806
6807 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6808
6809 dwarf2_find_base_address (comp_unit_die, cu);
6810
6811 /* Possibly set the default values of LOWPC and HIGHPC from
6812 `DW_AT_ranges'. */
3a2b436a
JK
6813 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6814 &best_highpc, cu, pst);
6815 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
6816 /* Store the contiguous range if it is not empty; it can be empty for
6817 CUs with no code. */
6818 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
6819 gdbarch_adjust_dwarf2_addr (gdbarch,
6820 best_lowpc + baseaddr),
6821 gdbarch_adjust_dwarf2_addr (gdbarch,
6822 best_highpc + baseaddr) - 1,
6823 pst);
0018ea6f
DE
6824
6825 /* Check if comp unit has_children.
6826 If so, read the rest of the partial symbols from this comp unit.
6827 If not, there's no more debug_info for this comp unit. */
6828 if (has_children)
6829 {
6830 struct partial_die_info *first_die;
6831 CORE_ADDR lowpc, highpc;
6832
6833 lowpc = ((CORE_ADDR) -1);
6834 highpc = ((CORE_ADDR) 0);
6835
6836 first_die = load_partial_dies (reader, info_ptr, 1);
6837
6838 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 6839 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
6840
6841 /* If we didn't find a lowpc, set it to highpc to avoid
6842 complaints from `maint check'. */
6843 if (lowpc == ((CORE_ADDR) -1))
6844 lowpc = highpc;
6845
6846 /* If the compilation unit didn't have an explicit address range,
6847 then use the information extracted from its child dies. */
e385593e 6848 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
6849 {
6850 best_lowpc = lowpc;
6851 best_highpc = highpc;
6852 }
6853 }
3e29f34a
MR
6854 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6855 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 6856
8763cede 6857 end_psymtab_common (objfile, pst);
0018ea6f
DE
6858
6859 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6860 {
6861 int i;
6862 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6863 struct dwarf2_per_cu_data *iter;
6864
6865 /* Fill in 'dependencies' here; we fill in 'users' in a
6866 post-pass. */
6867 pst->number_of_dependencies = len;
8d749320
SM
6868 pst->dependencies =
6869 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
6870 for (i = 0;
6871 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6872 i, iter);
6873 ++i)
6874 pst->dependencies[i] = iter->v.psymtab;
6875
6876 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6877 }
6878
6879 /* Get the list of files included in the current compilation unit,
6880 and build a psymtab for each of them. */
6881 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6882
b4f54984 6883 if (dwarf_read_debug)
0018ea6f
DE
6884 {
6885 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6886
6887 fprintf_unfiltered (gdb_stdlog,
6888 "Psymtab for %s unit @0x%x: %s - %s"
6889 ", %d global, %d static syms\n",
6890 per_cu->is_debug_types ? "type" : "comp",
9c541725 6891 to_underlying (per_cu->sect_off),
0018ea6f
DE
6892 paddress (gdbarch, pst->textlow),
6893 paddress (gdbarch, pst->texthigh),
6894 pst->n_global_syms, pst->n_static_syms);
6895 }
6896}
6897
6898/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6899 Process compilation unit THIS_CU for a psymtab. */
6900
6901static void
6902process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
6903 int want_partial_unit,
6904 enum language pretend_language)
0018ea6f
DE
6905{
6906 /* If this compilation unit was already read in, free the
6907 cached copy in order to read it in again. This is
6908 necessary because we skipped some symbols when we first
6909 read in the compilation unit (see load_partial_dies).
6910 This problem could be avoided, but the benefit is unclear. */
6911 if (this_cu->cu != NULL)
6912 free_one_cached_comp_unit (this_cu);
6913
f1902523
JK
6914 if (this_cu->is_debug_types)
6915 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
6916 NULL);
6917 else
6918 {
6919 process_psymtab_comp_unit_data info;
6920 info.want_partial_unit = want_partial_unit;
6921 info.pretend_language = pretend_language;
6922 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6923 process_psymtab_comp_unit_reader, &info);
6924 }
0018ea6f
DE
6925
6926 /* Age out any secondary CUs. */
6927 age_cached_comp_units ();
6928}
f4dc4d17
DE
6929
6930/* Reader function for build_type_psymtabs. */
6931
6932static void
6933build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 6934 const gdb_byte *info_ptr,
f4dc4d17
DE
6935 struct die_info *type_unit_die,
6936 int has_children,
6937 void *data)
6938{
6939 struct objfile *objfile = dwarf2_per_objfile->objfile;
6940 struct dwarf2_cu *cu = reader->cu;
6941 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 6942 struct signatured_type *sig_type;
f4dc4d17
DE
6943 struct type_unit_group *tu_group;
6944 struct attribute *attr;
6945 struct partial_die_info *first_die;
6946 CORE_ADDR lowpc, highpc;
6947 struct partial_symtab *pst;
6948
6949 gdb_assert (data == NULL);
0186c6a7
DE
6950 gdb_assert (per_cu->is_debug_types);
6951 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
6952
6953 if (! has_children)
6954 return;
6955
6956 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 6957 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 6958
0186c6a7 6959 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
6960
6961 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6962 cu->list_in_scope = &file_symbols;
6963 pst = create_partial_symtab (per_cu, "");
6964 pst->anonymous = 1;
6965
6966 first_die = load_partial_dies (reader, info_ptr, 1);
6967
6968 lowpc = (CORE_ADDR) -1;
6969 highpc = (CORE_ADDR) 0;
6970 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6971
8763cede 6972 end_psymtab_common (objfile, pst);
f4dc4d17
DE
6973}
6974
73051182
DE
6975/* Struct used to sort TUs by their abbreviation table offset. */
6976
6977struct tu_abbrev_offset
6978{
6979 struct signatured_type *sig_type;
6980 sect_offset abbrev_offset;
6981};
6982
6983/* Helper routine for build_type_psymtabs_1, passed to qsort. */
6984
6985static int
6986sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6987{
9a3c8263
SM
6988 const struct tu_abbrev_offset * const *a
6989 = (const struct tu_abbrev_offset * const*) ap;
6990 const struct tu_abbrev_offset * const *b
6991 = (const struct tu_abbrev_offset * const*) bp;
9c541725
PA
6992 sect_offset aoff = (*a)->abbrev_offset;
6993 sect_offset boff = (*b)->abbrev_offset;
73051182
DE
6994
6995 return (aoff > boff) - (aoff < boff);
6996}
6997
6998/* Efficiently read all the type units.
6999 This does the bulk of the work for build_type_psymtabs.
7000
7001 The efficiency is because we sort TUs by the abbrev table they use and
7002 only read each abbrev table once. In one program there are 200K TUs
7003 sharing 8K abbrev tables.
7004
7005 The main purpose of this function is to support building the
7006 dwarf2_per_objfile->type_unit_groups table.
7007 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7008 can collapse the search space by grouping them by stmt_list.
7009 The savings can be significant, in the same program from above the 200K TUs
7010 share 8K stmt_list tables.
7011
7012 FUNC is expected to call get_type_unit_group, which will create the
7013 struct type_unit_group if necessary and add it to
7014 dwarf2_per_objfile->type_unit_groups. */
7015
7016static void
7017build_type_psymtabs_1 (void)
7018{
73051182
DE
7019 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7020 struct cleanup *cleanups;
7021 struct abbrev_table *abbrev_table;
7022 sect_offset abbrev_offset;
7023 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
7024 int i;
7025
7026 /* It's up to the caller to not call us multiple times. */
7027 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7028
7029 if (dwarf2_per_objfile->n_type_units == 0)
7030 return;
7031
7032 /* TUs typically share abbrev tables, and there can be way more TUs than
7033 abbrev tables. Sort by abbrev table to reduce the number of times we
7034 read each abbrev table in.
7035 Alternatives are to punt or to maintain a cache of abbrev tables.
7036 This is simpler and efficient enough for now.
7037
7038 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7039 symtab to use). Typically TUs with the same abbrev offset have the same
7040 stmt_list value too so in practice this should work well.
7041
7042 The basic algorithm here is:
7043
7044 sort TUs by abbrev table
7045 for each TU with same abbrev table:
7046 read abbrev table if first user
7047 read TU top level DIE
7048 [IWBN if DWO skeletons had DW_AT_stmt_list]
7049 call FUNC */
7050
b4f54984 7051 if (dwarf_read_debug)
73051182
DE
7052 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7053
7054 /* Sort in a separate table to maintain the order of all_type_units
7055 for .gdb_index: TU indices directly index all_type_units. */
7056 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
7057 dwarf2_per_objfile->n_type_units);
7058 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
7059 {
7060 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
7061
7062 sorted_by_abbrev[i].sig_type = sig_type;
7063 sorted_by_abbrev[i].abbrev_offset =
7064 read_abbrev_offset (sig_type->per_cu.section,
9c541725 7065 sig_type->per_cu.sect_off);
73051182
DE
7066 }
7067 cleanups = make_cleanup (xfree, sorted_by_abbrev);
7068 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
7069 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
7070
9c541725 7071 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
7072 abbrev_table = NULL;
7073 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
7074
7075 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
7076 {
7077 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
7078
7079 /* Switch to the next abbrev table if necessary. */
7080 if (abbrev_table == NULL
9c541725 7081 || tu->abbrev_offset != abbrev_offset)
73051182
DE
7082 {
7083 if (abbrev_table != NULL)
7084 {
7085 abbrev_table_free (abbrev_table);
7086 /* Reset to NULL in case abbrev_table_read_table throws
7087 an error: abbrev_table_free_cleanup will get called. */
7088 abbrev_table = NULL;
7089 }
7090 abbrev_offset = tu->abbrev_offset;
7091 abbrev_table =
7092 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
7093 abbrev_offset);
7094 ++tu_stats->nr_uniq_abbrev_tables;
7095 }
7096
7097 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
7098 build_type_psymtabs_reader, NULL);
7099 }
7100
73051182 7101 do_cleanups (cleanups);
6aa5f3a6 7102}
73051182 7103
6aa5f3a6
DE
7104/* Print collected type unit statistics. */
7105
7106static void
7107print_tu_stats (void)
7108{
7109 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7110
7111 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7112 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
7113 dwarf2_per_objfile->n_type_units);
7114 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7115 tu_stats->nr_uniq_abbrev_tables);
7116 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7117 tu_stats->nr_symtabs);
7118 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7119 tu_stats->nr_symtab_sharers);
7120 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7121 tu_stats->nr_stmt_less_type_units);
7122 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7123 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7124}
7125
f4dc4d17
DE
7126/* Traversal function for build_type_psymtabs. */
7127
7128static int
7129build_type_psymtab_dependencies (void **slot, void *info)
7130{
7131 struct objfile *objfile = dwarf2_per_objfile->objfile;
7132 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7133 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 7134 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
7135 int len = VEC_length (sig_type_ptr, tu_group->tus);
7136 struct signatured_type *iter;
f4dc4d17
DE
7137 int i;
7138
7139 gdb_assert (len > 0);
0186c6a7 7140 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
7141
7142 pst->number_of_dependencies = len;
8d749320
SM
7143 pst->dependencies =
7144 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 7145 for (i = 0;
0186c6a7 7146 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
7147 ++i)
7148 {
0186c6a7
DE
7149 gdb_assert (iter->per_cu.is_debug_types);
7150 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7151 iter->type_unit_group = tu_group;
f4dc4d17
DE
7152 }
7153
0186c6a7 7154 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
7155
7156 return 1;
7157}
7158
7159/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7160 Build partial symbol tables for the .debug_types comp-units. */
7161
7162static void
7163build_type_psymtabs (struct objfile *objfile)
7164{
0e50663e 7165 if (! create_all_type_units (objfile))
348e048f
DE
7166 return;
7167
73051182 7168 build_type_psymtabs_1 ();
6aa5f3a6 7169}
f4dc4d17 7170
6aa5f3a6
DE
7171/* Traversal function for process_skeletonless_type_unit.
7172 Read a TU in a DWO file and build partial symbols for it. */
7173
7174static int
7175process_skeletonless_type_unit (void **slot, void *info)
7176{
7177 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9a3c8263 7178 struct objfile *objfile = (struct objfile *) info;
6aa5f3a6
DE
7179 struct signatured_type find_entry, *entry;
7180
7181 /* If this TU doesn't exist in the global table, add it and read it in. */
7182
7183 if (dwarf2_per_objfile->signatured_types == NULL)
7184 {
7185 dwarf2_per_objfile->signatured_types
7186 = allocate_signatured_type_table (objfile);
7187 }
7188
7189 find_entry.signature = dwo_unit->signature;
7190 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
7191 INSERT);
7192 /* If we've already seen this type there's nothing to do. What's happening
7193 is we're doing our own version of comdat-folding here. */
7194 if (*slot != NULL)
7195 return 1;
7196
7197 /* This does the job that create_all_type_units would have done for
7198 this TU. */
7199 entry = add_type_unit (dwo_unit->signature, slot);
7200 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
7201 *slot = entry;
7202
7203 /* This does the job that build_type_psymtabs_1 would have done. */
7204 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
7205 build_type_psymtabs_reader, NULL);
7206
7207 return 1;
7208}
7209
7210/* Traversal function for process_skeletonless_type_units. */
7211
7212static int
7213process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7214{
7215 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7216
7217 if (dwo_file->tus != NULL)
7218 {
7219 htab_traverse_noresize (dwo_file->tus,
7220 process_skeletonless_type_unit, info);
7221 }
7222
7223 return 1;
7224}
7225
7226/* Scan all TUs of DWO files, verifying we've processed them.
7227 This is needed in case a TU was emitted without its skeleton.
7228 Note: This can't be done until we know what all the DWO files are. */
7229
7230static void
7231process_skeletonless_type_units (struct objfile *objfile)
7232{
7233 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7234 if (get_dwp_file () == NULL
7235 && dwarf2_per_objfile->dwo_files != NULL)
7236 {
7237 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
7238 process_dwo_file_for_skeletonless_type_units,
7239 objfile);
7240 }
348e048f
DE
7241}
7242
95554aad
TT
7243/* Compute the 'user' field for each psymtab in OBJFILE. */
7244
7245static void
7246set_partial_user (struct objfile *objfile)
7247{
7248 int i;
7249
7250 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
7251 {
8832e7e3 7252 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
95554aad
TT
7253 struct partial_symtab *pst = per_cu->v.psymtab;
7254 int j;
7255
36586728
TT
7256 if (pst == NULL)
7257 continue;
7258
95554aad
TT
7259 for (j = 0; j < pst->number_of_dependencies; ++j)
7260 {
7261 /* Set the 'user' field only if it is not already set. */
7262 if (pst->dependencies[j]->user == NULL)
7263 pst->dependencies[j]->user = pst;
7264 }
7265 }
7266}
7267
93311388
DE
7268/* Build the partial symbol table by doing a quick pass through the
7269 .debug_info and .debug_abbrev sections. */
72bf9492 7270
93311388 7271static void
c67a9c90 7272dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 7273{
791afaa2 7274 struct cleanup *back_to;
21b2bd31 7275 int i;
93311388 7276
b4f54984 7277 if (dwarf_read_debug)
45cfd468
DE
7278 {
7279 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7280 objfile_name (objfile));
45cfd468
DE
7281 }
7282
98bfdba5
PA
7283 dwarf2_per_objfile->reading_partial_symbols = 1;
7284
be391dca 7285 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 7286
93311388
DE
7287 /* Any cached compilation units will be linked by the per-objfile
7288 read_in_chain. Make sure to free them when we're done. */
7289 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 7290
348e048f
DE
7291 build_type_psymtabs (objfile);
7292
93311388 7293 create_all_comp_units (objfile);
c906108c 7294
60606b2c
TT
7295 /* Create a temporary address map on a temporary obstack. We later
7296 copy this to the final obstack. */
8268c778 7297 auto_obstack temp_obstack;
791afaa2
TT
7298
7299 scoped_restore save_psymtabs_addrmap
7300 = make_scoped_restore (&objfile->psymtabs_addrmap,
7301 addrmap_create_mutable (&temp_obstack));
72bf9492 7302
21b2bd31 7303 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 7304 {
8832e7e3 7305 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
aaa75496 7306
b93601f3 7307 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 7308 }
ff013f42 7309
6aa5f3a6
DE
7310 /* This has to wait until we read the CUs, we need the list of DWOs. */
7311 process_skeletonless_type_units (objfile);
7312
7313 /* Now that all TUs have been processed we can fill in the dependencies. */
7314 if (dwarf2_per_objfile->type_unit_groups != NULL)
7315 {
7316 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
7317 build_type_psymtab_dependencies, NULL);
7318 }
7319
b4f54984 7320 if (dwarf_read_debug)
6aa5f3a6
DE
7321 print_tu_stats ();
7322
95554aad
TT
7323 set_partial_user (objfile);
7324
ff013f42
JK
7325 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
7326 &objfile->objfile_obstack);
791afaa2
TT
7327 /* At this point we want to keep the address map. */
7328 save_psymtabs_addrmap.release ();
ff013f42 7329
ae038cb0 7330 do_cleanups (back_to);
45cfd468 7331
b4f54984 7332 if (dwarf_read_debug)
45cfd468 7333 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7334 objfile_name (objfile));
ae038cb0
DJ
7335}
7336
3019eac3 7337/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
7338
7339static void
dee91e82 7340load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7341 const gdb_byte *info_ptr,
dee91e82
DE
7342 struct die_info *comp_unit_die,
7343 int has_children,
7344 void *data)
ae038cb0 7345{
dee91e82 7346 struct dwarf2_cu *cu = reader->cu;
ae038cb0 7347
95554aad 7348 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 7349
ae038cb0
DJ
7350 /* Check if comp unit has_children.
7351 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 7352 If not, there's no more debug_info for this comp unit. */
d85a05f0 7353 if (has_children)
dee91e82
DE
7354 load_partial_dies (reader, info_ptr, 0);
7355}
98bfdba5 7356
dee91e82
DE
7357/* Load the partial DIEs for a secondary CU into memory.
7358 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7359
dee91e82
DE
7360static void
7361load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7362{
f4dc4d17
DE
7363 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7364 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
7365}
7366
ae038cb0 7367static void
36586728
TT
7368read_comp_units_from_section (struct objfile *objfile,
7369 struct dwarf2_section_info *section,
f1902523 7370 struct dwarf2_section_info *abbrev_section,
36586728
TT
7371 unsigned int is_dwz,
7372 int *n_allocated,
7373 int *n_comp_units,
7374 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 7375{
d521ce57 7376 const gdb_byte *info_ptr;
a32a8923 7377 bfd *abfd = get_section_bfd_owner (section);
be391dca 7378
b4f54984 7379 if (dwarf_read_debug)
bf6af496 7380 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
7381 get_section_name (section),
7382 get_section_file_name (section));
bf6af496 7383
36586728 7384 dwarf2_read_section (objfile, section);
ae038cb0 7385
36586728 7386 info_ptr = section->buffer;
6e70227d 7387
36586728 7388 while (info_ptr < section->buffer + section->size)
ae038cb0 7389 {
ae038cb0 7390 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7391
9c541725 7392 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7393
f1902523
JK
7394 comp_unit_head cu_header;
7395 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
7396 info_ptr, rcuh_kind::COMPILE);
ae038cb0
DJ
7397
7398 /* Save the compilation unit for later lookup. */
f1902523
JK
7399 if (cu_header.unit_type != DW_UT_type)
7400 {
7401 this_cu = XOBNEW (&objfile->objfile_obstack,
7402 struct dwarf2_per_cu_data);
7403 memset (this_cu, 0, sizeof (*this_cu));
7404 }
7405 else
7406 {
7407 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7408 struct signatured_type);
7409 memset (sig_type, 0, sizeof (*sig_type));
7410 sig_type->signature = cu_header.signature;
7411 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7412 this_cu = &sig_type->per_cu;
7413 }
7414 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7415 this_cu->sect_off = sect_off;
f1902523 7416 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7417 this_cu->is_dwz = is_dwz;
9291a0cd 7418 this_cu->objfile = objfile;
8a0459fd 7419 this_cu->section = section;
ae038cb0 7420
36586728 7421 if (*n_comp_units == *n_allocated)
ae038cb0 7422 {
36586728 7423 *n_allocated *= 2;
224c3ddb
SM
7424 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
7425 *all_comp_units, *n_allocated);
ae038cb0 7426 }
36586728
TT
7427 (*all_comp_units)[*n_comp_units] = this_cu;
7428 ++*n_comp_units;
ae038cb0
DJ
7429
7430 info_ptr = info_ptr + this_cu->length;
7431 }
36586728
TT
7432}
7433
7434/* Create a list of all compilation units in OBJFILE.
7435 This is only done for -readnow and building partial symtabs. */
7436
7437static void
7438create_all_comp_units (struct objfile *objfile)
7439{
7440 int n_allocated;
7441 int n_comp_units;
7442 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 7443 struct dwz_file *dwz;
36586728
TT
7444
7445 n_comp_units = 0;
7446 n_allocated = 10;
8d749320 7447 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728 7448
f1902523
JK
7449 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
7450 &dwarf2_per_objfile->abbrev, 0,
36586728
TT
7451 &n_allocated, &n_comp_units, &all_comp_units);
7452
4db1a1dc
TT
7453 dwz = dwarf2_get_dwz_file ();
7454 if (dwz != NULL)
f1902523 7455 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
4db1a1dc
TT
7456 &n_allocated, &n_comp_units,
7457 &all_comp_units);
ae038cb0 7458
8d749320
SM
7459 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
7460 struct dwarf2_per_cu_data *,
7461 n_comp_units);
ae038cb0
DJ
7462 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
7463 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
7464 xfree (all_comp_units);
7465 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
7466}
7467
5734ee8b 7468/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7469 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7470 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7471 DW_AT_ranges). See the comments of add_partial_subprogram on how
7472 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7473
72bf9492
DJ
7474static void
7475scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7476 CORE_ADDR *highpc, int set_addrmap,
7477 struct dwarf2_cu *cu)
c906108c 7478{
72bf9492 7479 struct partial_die_info *pdi;
c906108c 7480
91c24f0a
DC
7481 /* Now, march along the PDI's, descending into ones which have
7482 interesting children but skipping the children of the other ones,
7483 until we reach the end of the compilation unit. */
c906108c 7484
72bf9492 7485 pdi = first_die;
91c24f0a 7486
72bf9492
DJ
7487 while (pdi != NULL)
7488 {
7489 fixup_partial_die (pdi, cu);
c906108c 7490
f55ee35c 7491 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7492 children, so we need to look at them. Ditto for anonymous
7493 enums. */
933c6fe4 7494
72bf9492 7495 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
7496 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7497 || pdi->tag == DW_TAG_imported_unit)
c906108c 7498 {
72bf9492 7499 switch (pdi->tag)
c906108c
SS
7500 {
7501 case DW_TAG_subprogram:
cdc07690 7502 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 7503 break;
72929c62 7504 case DW_TAG_constant:
c906108c
SS
7505 case DW_TAG_variable:
7506 case DW_TAG_typedef:
91c24f0a 7507 case DW_TAG_union_type:
72bf9492 7508 if (!pdi->is_declaration)
63d06c5c 7509 {
72bf9492 7510 add_partial_symbol (pdi, cu);
63d06c5c
DC
7511 }
7512 break;
c906108c 7513 case DW_TAG_class_type:
680b30c7 7514 case DW_TAG_interface_type:
c906108c 7515 case DW_TAG_structure_type:
72bf9492 7516 if (!pdi->is_declaration)
c906108c 7517 {
72bf9492 7518 add_partial_symbol (pdi, cu);
c906108c 7519 }
e98c9e7c
TT
7520 if (cu->language == language_rust && pdi->has_children)
7521 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7522 set_addrmap, cu);
c906108c 7523 break;
91c24f0a 7524 case DW_TAG_enumeration_type:
72bf9492
DJ
7525 if (!pdi->is_declaration)
7526 add_partial_enumeration (pdi, cu);
c906108c
SS
7527 break;
7528 case DW_TAG_base_type:
a02abb62 7529 case DW_TAG_subrange_type:
c906108c 7530 /* File scope base type definitions are added to the partial
c5aa993b 7531 symbol table. */
72bf9492 7532 add_partial_symbol (pdi, cu);
c906108c 7533 break;
d9fa45fe 7534 case DW_TAG_namespace:
cdc07690 7535 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7536 break;
5d7cb8df 7537 case DW_TAG_module:
cdc07690 7538 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7539 break;
95554aad
TT
7540 case DW_TAG_imported_unit:
7541 {
7542 struct dwarf2_per_cu_data *per_cu;
7543
f4dc4d17
DE
7544 /* For now we don't handle imported units in type units. */
7545 if (cu->per_cu->is_debug_types)
7546 {
7547 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7548 " supported in type units [in module %s]"),
4262abfb 7549 objfile_name (cu->objfile));
f4dc4d17
DE
7550 }
7551
9c541725 7552 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
36586728 7553 pdi->is_dwz,
95554aad
TT
7554 cu->objfile);
7555
7556 /* Go read the partial unit, if needed. */
7557 if (per_cu->v.psymtab == NULL)
b93601f3 7558 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 7559
f4dc4d17 7560 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 7561 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
7562 }
7563 break;
74921315
KS
7564 case DW_TAG_imported_declaration:
7565 add_partial_symbol (pdi, cu);
7566 break;
c906108c
SS
7567 default:
7568 break;
7569 }
7570 }
7571
72bf9492
DJ
7572 /* If the die has a sibling, skip to the sibling. */
7573
7574 pdi = pdi->die_sibling;
7575 }
7576}
7577
7578/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7579
72bf9492 7580 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7581 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7582 Enumerators are an exception; they use the scope of their parent
7583 enumeration type, i.e. the name of the enumeration type is not
7584 prepended to the enumerator.
91c24f0a 7585
72bf9492
DJ
7586 There are two complexities. One is DW_AT_specification; in this
7587 case "parent" means the parent of the target of the specification,
7588 instead of the direct parent of the DIE. The other is compilers
7589 which do not emit DW_TAG_namespace; in this case we try to guess
7590 the fully qualified name of structure types from their members'
7591 linkage names. This must be done using the DIE's children rather
7592 than the children of any DW_AT_specification target. We only need
7593 to do this for structures at the top level, i.e. if the target of
7594 any DW_AT_specification (if any; otherwise the DIE itself) does not
7595 have a parent. */
7596
7597/* Compute the scope prefix associated with PDI's parent, in
7598 compilation unit CU. The result will be allocated on CU's
7599 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7600 field. NULL is returned if no prefix is necessary. */
15d034d0 7601static const char *
72bf9492
DJ
7602partial_die_parent_scope (struct partial_die_info *pdi,
7603 struct dwarf2_cu *cu)
7604{
15d034d0 7605 const char *grandparent_scope;
72bf9492 7606 struct partial_die_info *parent, *real_pdi;
91c24f0a 7607
72bf9492
DJ
7608 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7609 then this means the parent of the specification DIE. */
7610
7611 real_pdi = pdi;
72bf9492 7612 while (real_pdi->has_specification)
36586728
TT
7613 real_pdi = find_partial_die (real_pdi->spec_offset,
7614 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
7615
7616 parent = real_pdi->die_parent;
7617 if (parent == NULL)
7618 return NULL;
7619
7620 if (parent->scope_set)
7621 return parent->scope;
7622
7623 fixup_partial_die (parent, cu);
7624
10b3939b 7625 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 7626
acebe513
UW
7627 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7628 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7629 Work around this problem here. */
7630 if (cu->language == language_cplus
6e70227d 7631 && parent->tag == DW_TAG_namespace
acebe513
UW
7632 && strcmp (parent->name, "::") == 0
7633 && grandparent_scope == NULL)
7634 {
7635 parent->scope = NULL;
7636 parent->scope_set = 1;
7637 return NULL;
7638 }
7639
9c6c53f7
SA
7640 if (pdi->tag == DW_TAG_enumerator)
7641 /* Enumerators should not get the name of the enumeration as a prefix. */
7642 parent->scope = grandparent_scope;
7643 else if (parent->tag == DW_TAG_namespace
f55ee35c 7644 || parent->tag == DW_TAG_module
72bf9492
DJ
7645 || parent->tag == DW_TAG_structure_type
7646 || parent->tag == DW_TAG_class_type
680b30c7 7647 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
7648 || parent->tag == DW_TAG_union_type
7649 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
7650 {
7651 if (grandparent_scope == NULL)
7652 parent->scope = parent->name;
7653 else
3e43a32a
MS
7654 parent->scope = typename_concat (&cu->comp_unit_obstack,
7655 grandparent_scope,
f55ee35c 7656 parent->name, 0, cu);
72bf9492 7657 }
72bf9492
DJ
7658 else
7659 {
7660 /* FIXME drow/2004-04-01: What should we be doing with
7661 function-local names? For partial symbols, we should probably be
7662 ignoring them. */
7663 complaint (&symfile_complaints,
e2e0b3e5 7664 _("unhandled containing DIE tag %d for DIE at %d"),
9c541725 7665 parent->tag, to_underlying (pdi->sect_off));
72bf9492 7666 parent->scope = grandparent_scope;
c906108c
SS
7667 }
7668
72bf9492
DJ
7669 parent->scope_set = 1;
7670 return parent->scope;
7671}
7672
7673/* Return the fully scoped name associated with PDI, from compilation unit
7674 CU. The result will be allocated with malloc. */
4568ecf9 7675
72bf9492
DJ
7676static char *
7677partial_die_full_name (struct partial_die_info *pdi,
7678 struct dwarf2_cu *cu)
7679{
15d034d0 7680 const char *parent_scope;
72bf9492 7681
98bfdba5
PA
7682 /* If this is a template instantiation, we can not work out the
7683 template arguments from partial DIEs. So, unfortunately, we have
7684 to go through the full DIEs. At least any work we do building
7685 types here will be reused if full symbols are loaded later. */
7686 if (pdi->has_template_arguments)
7687 {
7688 fixup_partial_die (pdi, cu);
7689
7690 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7691 {
7692 struct die_info *die;
7693 struct attribute attr;
7694 struct dwarf2_cu *ref_cu = cu;
7695
b64f50a1 7696 /* DW_FORM_ref_addr is using section offset. */
b4069958 7697 attr.name = (enum dwarf_attribute) 0;
98bfdba5 7698 attr.form = DW_FORM_ref_addr;
9c541725 7699 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
7700 die = follow_die_ref (NULL, &attr, &ref_cu);
7701
7702 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7703 }
7704 }
7705
72bf9492
DJ
7706 parent_scope = partial_die_parent_scope (pdi, cu);
7707 if (parent_scope == NULL)
7708 return NULL;
7709 else
f55ee35c 7710 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
7711}
7712
7713static void
72bf9492 7714add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 7715{
e7c27a73 7716 struct objfile *objfile = cu->objfile;
3e29f34a 7717 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 7718 CORE_ADDR addr = 0;
15d034d0 7719 const char *actual_name = NULL;
e142c38c 7720 CORE_ADDR baseaddr;
15d034d0 7721 char *built_actual_name;
e142c38c
DJ
7722
7723 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7724
15d034d0
TT
7725 built_actual_name = partial_die_full_name (pdi, cu);
7726 if (built_actual_name != NULL)
7727 actual_name = built_actual_name;
63d06c5c 7728
72bf9492
DJ
7729 if (actual_name == NULL)
7730 actual_name = pdi->name;
7731
c906108c
SS
7732 switch (pdi->tag)
7733 {
7734 case DW_TAG_subprogram:
3e29f34a 7735 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 7736 if (pdi->is_external || cu->language == language_ada)
c906108c 7737 {
2cfa0c8d
JB
7738 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
7739 of the global scope. But in Ada, we want to be able to access
7740 nested procedures globally. So all Ada subprograms are stored
7741 in the global scope. */
f47fb265 7742 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7743 built_actual_name != NULL,
f47fb265
MS
7744 VAR_DOMAIN, LOC_BLOCK,
7745 &objfile->global_psymbols,
1762568f 7746 addr, cu->language, objfile);
c906108c
SS
7747 }
7748 else
7749 {
f47fb265 7750 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7751 built_actual_name != NULL,
f47fb265
MS
7752 VAR_DOMAIN, LOC_BLOCK,
7753 &objfile->static_psymbols,
1762568f 7754 addr, cu->language, objfile);
c906108c 7755 }
0c1b455e
TT
7756
7757 if (pdi->main_subprogram && actual_name != NULL)
7758 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 7759 break;
72929c62
JB
7760 case DW_TAG_constant:
7761 {
af5bf4ad 7762 std::vector<partial_symbol *> *list;
72929c62
JB
7763
7764 if (pdi->is_external)
7765 list = &objfile->global_psymbols;
7766 else
7767 list = &objfile->static_psymbols;
f47fb265 7768 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7769 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 7770 list, 0, cu->language, objfile);
72929c62
JB
7771 }
7772 break;
c906108c 7773 case DW_TAG_variable:
95554aad
TT
7774 if (pdi->d.locdesc)
7775 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 7776
95554aad 7777 if (pdi->d.locdesc
caac4577
JG
7778 && addr == 0
7779 && !dwarf2_per_objfile->has_section_at_zero)
7780 {
7781 /* A global or static variable may also have been stripped
7782 out by the linker if unused, in which case its address
7783 will be nullified; do not add such variables into partial
7784 symbol table then. */
7785 }
7786 else if (pdi->is_external)
c906108c
SS
7787 {
7788 /* Global Variable.
7789 Don't enter into the minimal symbol tables as there is
7790 a minimal symbol table entry from the ELF symbols already.
7791 Enter into partial symbol table if it has a location
7792 descriptor or a type.
7793 If the location descriptor is missing, new_symbol will create
7794 a LOC_UNRESOLVED symbol, the address of the variable will then
7795 be determined from the minimal symbol table whenever the variable
7796 is referenced.
7797 The address for the partial symbol table entry is not
7798 used by GDB, but it comes in handy for debugging partial symbol
7799 table building. */
7800
95554aad 7801 if (pdi->d.locdesc || pdi->has_type)
f47fb265 7802 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7803 built_actual_name != NULL,
f47fb265
MS
7804 VAR_DOMAIN, LOC_STATIC,
7805 &objfile->global_psymbols,
1762568f 7806 addr + baseaddr,
f47fb265 7807 cu->language, objfile);
c906108c
SS
7808 }
7809 else
7810 {
ff908ebf
AW
7811 int has_loc = pdi->d.locdesc != NULL;
7812
7813 /* Static Variable. Skip symbols whose value we cannot know (those
7814 without location descriptors or constant values). */
7815 if (!has_loc && !pdi->has_const_value)
decbce07 7816 {
15d034d0 7817 xfree (built_actual_name);
decbce07
MS
7818 return;
7819 }
ff908ebf 7820
f47fb265 7821 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7822 built_actual_name != NULL,
f47fb265
MS
7823 VAR_DOMAIN, LOC_STATIC,
7824 &objfile->static_psymbols,
ff908ebf 7825 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 7826 cu->language, objfile);
c906108c
SS
7827 }
7828 break;
7829 case DW_TAG_typedef:
7830 case DW_TAG_base_type:
a02abb62 7831 case DW_TAG_subrange_type:
38d518c9 7832 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7833 built_actual_name != NULL,
176620f1 7834 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 7835 &objfile->static_psymbols,
1762568f 7836 0, cu->language, objfile);
c906108c 7837 break;
74921315 7838 case DW_TAG_imported_declaration:
72bf9492
DJ
7839 case DW_TAG_namespace:
7840 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7841 built_actual_name != NULL,
72bf9492
DJ
7842 VAR_DOMAIN, LOC_TYPEDEF,
7843 &objfile->global_psymbols,
1762568f 7844 0, cu->language, objfile);
72bf9492 7845 break;
530e8392
KB
7846 case DW_TAG_module:
7847 add_psymbol_to_list (actual_name, strlen (actual_name),
7848 built_actual_name != NULL,
7849 MODULE_DOMAIN, LOC_TYPEDEF,
7850 &objfile->global_psymbols,
1762568f 7851 0, cu->language, objfile);
530e8392 7852 break;
c906108c 7853 case DW_TAG_class_type:
680b30c7 7854 case DW_TAG_interface_type:
c906108c
SS
7855 case DW_TAG_structure_type:
7856 case DW_TAG_union_type:
7857 case DW_TAG_enumeration_type:
fa4028e9
JB
7858 /* Skip external references. The DWARF standard says in the section
7859 about "Structure, Union, and Class Type Entries": "An incomplete
7860 structure, union or class type is represented by a structure,
7861 union or class entry that does not have a byte size attribute
7862 and that has a DW_AT_declaration attribute." */
7863 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 7864 {
15d034d0 7865 xfree (built_actual_name);
decbce07
MS
7866 return;
7867 }
fa4028e9 7868
63d06c5c
DC
7869 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7870 static vs. global. */
38d518c9 7871 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7872 built_actual_name != NULL,
176620f1 7873 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 7874 cu->language == language_cplus
63d06c5c
DC
7875 ? &objfile->global_psymbols
7876 : &objfile->static_psymbols,
1762568f 7877 0, cu->language, objfile);
c906108c 7878
c906108c
SS
7879 break;
7880 case DW_TAG_enumerator:
38d518c9 7881 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7882 built_actual_name != NULL,
176620f1 7883 VAR_DOMAIN, LOC_CONST,
9c37b5ae 7884 cu->language == language_cplus
f6fe98ef
DJ
7885 ? &objfile->global_psymbols
7886 : &objfile->static_psymbols,
1762568f 7887 0, cu->language, objfile);
c906108c
SS
7888 break;
7889 default:
7890 break;
7891 }
5c4e30ca 7892
15d034d0 7893 xfree (built_actual_name);
c906108c
SS
7894}
7895
5c4e30ca
DC
7896/* Read a partial die corresponding to a namespace; also, add a symbol
7897 corresponding to that namespace to the symbol table. NAMESPACE is
7898 the name of the enclosing namespace. */
91c24f0a 7899
72bf9492
DJ
7900static void
7901add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 7902 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7903 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 7904{
72bf9492 7905 /* Add a symbol for the namespace. */
e7c27a73 7906
72bf9492 7907 add_partial_symbol (pdi, cu);
5c4e30ca
DC
7908
7909 /* Now scan partial symbols in that namespace. */
7910
91c24f0a 7911 if (pdi->has_children)
cdc07690 7912 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
7913}
7914
5d7cb8df
JK
7915/* Read a partial die corresponding to a Fortran module. */
7916
7917static void
7918add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 7919 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 7920{
530e8392
KB
7921 /* Add a symbol for the namespace. */
7922
7923 add_partial_symbol (pdi, cu);
7924
f55ee35c 7925 /* Now scan partial symbols in that module. */
5d7cb8df
JK
7926
7927 if (pdi->has_children)
cdc07690 7928 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
7929}
7930
bc30ff58
JB
7931/* Read a partial die corresponding to a subprogram and create a partial
7932 symbol for that subprogram. When the CU language allows it, this
7933 routine also defines a partial symbol for each nested subprogram
cdc07690 7934 that this subprogram contains. If SET_ADDRMAP is true, record the
428fc5fc
YQ
7935 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7936 and highest PC values found in PDI.
6e70227d 7937
cdc07690
YQ
7938 PDI may also be a lexical block, in which case we simply search
7939 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
7940 Again, this is only performed when the CU language allows this
7941 type of definitions. */
7942
7943static void
7944add_partial_subprogram (struct partial_die_info *pdi,
7945 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7946 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58
JB
7947{
7948 if (pdi->tag == DW_TAG_subprogram)
7949 {
7950 if (pdi->has_pc_info)
7951 {
7952 if (pdi->lowpc < *lowpc)
7953 *lowpc = pdi->lowpc;
7954 if (pdi->highpc > *highpc)
7955 *highpc = pdi->highpc;
cdc07690 7956 if (set_addrmap)
5734ee8b 7957 {
5734ee8b 7958 struct objfile *objfile = cu->objfile;
3e29f34a
MR
7959 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7960 CORE_ADDR baseaddr;
7961 CORE_ADDR highpc;
7962 CORE_ADDR lowpc;
5734ee8b
DJ
7963
7964 baseaddr = ANOFFSET (objfile->section_offsets,
7965 SECT_OFF_TEXT (objfile));
3e29f34a
MR
7966 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7967 pdi->lowpc + baseaddr);
7968 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7969 pdi->highpc + baseaddr);
7970 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 7971 cu->per_cu->v.psymtab);
5734ee8b 7972 }
481860b3
GB
7973 }
7974
7975 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7976 {
bc30ff58 7977 if (!pdi->is_declaration)
e8d05480
JB
7978 /* Ignore subprogram DIEs that do not have a name, they are
7979 illegal. Do not emit a complaint at this point, we will
7980 do so when we convert this psymtab into a symtab. */
7981 if (pdi->name)
7982 add_partial_symbol (pdi, cu);
bc30ff58
JB
7983 }
7984 }
6e70227d 7985
bc30ff58
JB
7986 if (! pdi->has_children)
7987 return;
7988
7989 if (cu->language == language_ada)
7990 {
7991 pdi = pdi->die_child;
7992 while (pdi != NULL)
7993 {
7994 fixup_partial_die (pdi, cu);
7995 if (pdi->tag == DW_TAG_subprogram
7996 || pdi->tag == DW_TAG_lexical_block)
cdc07690 7997 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
7998 pdi = pdi->die_sibling;
7999 }
8000 }
8001}
8002
91c24f0a
DC
8003/* Read a partial die corresponding to an enumeration type. */
8004
72bf9492
DJ
8005static void
8006add_partial_enumeration (struct partial_die_info *enum_pdi,
8007 struct dwarf2_cu *cu)
91c24f0a 8008{
72bf9492 8009 struct partial_die_info *pdi;
91c24f0a
DC
8010
8011 if (enum_pdi->name != NULL)
72bf9492
DJ
8012 add_partial_symbol (enum_pdi, cu);
8013
8014 pdi = enum_pdi->die_child;
8015 while (pdi)
91c24f0a 8016 {
72bf9492 8017 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 8018 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 8019 else
72bf9492
DJ
8020 add_partial_symbol (pdi, cu);
8021 pdi = pdi->die_sibling;
91c24f0a 8022 }
91c24f0a
DC
8023}
8024
6caca83c
CC
8025/* Return the initial uleb128 in the die at INFO_PTR. */
8026
8027static unsigned int
d521ce57 8028peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8029{
8030 unsigned int bytes_read;
8031
8032 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8033}
8034
4bb7a0a7
DJ
8035/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
8036 Return the corresponding abbrev, or NULL if the number is zero (indicating
8037 an empty DIE). In either case *BYTES_READ will be set to the length of
8038 the initial number. */
8039
8040static struct abbrev_info *
d521ce57 8041peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 8042 struct dwarf2_cu *cu)
4bb7a0a7
DJ
8043{
8044 bfd *abfd = cu->objfile->obfd;
8045 unsigned int abbrev_number;
8046 struct abbrev_info *abbrev;
8047
8048 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8049
8050 if (abbrev_number == 0)
8051 return NULL;
8052
433df2d4 8053 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
8054 if (!abbrev)
8055 {
422b9917
DE
8056 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8057 " at offset 0x%x [in module %s]"),
8058 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9c541725 8059 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8060 }
8061
8062 return abbrev;
8063}
8064
93311388
DE
8065/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8066 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8067 DIE. Any children of the skipped DIEs will also be skipped. */
8068
d521ce57
TT
8069static const gdb_byte *
8070skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8071{
dee91e82 8072 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
8073 struct abbrev_info *abbrev;
8074 unsigned int bytes_read;
8075
8076 while (1)
8077 {
8078 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8079 if (abbrev == NULL)
8080 return info_ptr + bytes_read;
8081 else
dee91e82 8082 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8083 }
8084}
8085
93311388
DE
8086/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8087 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8088 abbrev corresponding to that skipped uleb128 should be passed in
8089 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8090 children. */
8091
d521ce57
TT
8092static const gdb_byte *
8093skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8094 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8095{
8096 unsigned int bytes_read;
8097 struct attribute attr;
dee91e82
DE
8098 bfd *abfd = reader->abfd;
8099 struct dwarf2_cu *cu = reader->cu;
d521ce57 8100 const gdb_byte *buffer = reader->buffer;
f664829e 8101 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8102 unsigned int form, i;
8103
8104 for (i = 0; i < abbrev->num_attrs; i++)
8105 {
8106 /* The only abbrev we care about is DW_AT_sibling. */
8107 if (abbrev->attrs[i].name == DW_AT_sibling)
8108 {
dee91e82 8109 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8110 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
8111 complaint (&symfile_complaints,
8112 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8113 else
b9502d3f 8114 {
9c541725
PA
8115 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8116 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8117
8118 if (sibling_ptr < info_ptr)
8119 complaint (&symfile_complaints,
8120 _("DW_AT_sibling points backwards"));
22869d73
KS
8121 else if (sibling_ptr > reader->buffer_end)
8122 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
8123 else
8124 return sibling_ptr;
8125 }
4bb7a0a7
DJ
8126 }
8127
8128 /* If it isn't DW_AT_sibling, skip this attribute. */
8129 form = abbrev->attrs[i].form;
8130 skip_attribute:
8131 switch (form)
8132 {
4bb7a0a7 8133 case DW_FORM_ref_addr:
ae411497
TT
8134 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8135 and later it is offset sized. */
8136 if (cu->header.version == 2)
8137 info_ptr += cu->header.addr_size;
8138 else
8139 info_ptr += cu->header.offset_size;
8140 break;
36586728
TT
8141 case DW_FORM_GNU_ref_alt:
8142 info_ptr += cu->header.offset_size;
8143 break;
ae411497 8144 case DW_FORM_addr:
4bb7a0a7
DJ
8145 info_ptr += cu->header.addr_size;
8146 break;
8147 case DW_FORM_data1:
8148 case DW_FORM_ref1:
8149 case DW_FORM_flag:
8150 info_ptr += 1;
8151 break;
2dc7f7b3 8152 case DW_FORM_flag_present:
43988095 8153 case DW_FORM_implicit_const:
2dc7f7b3 8154 break;
4bb7a0a7
DJ
8155 case DW_FORM_data2:
8156 case DW_FORM_ref2:
8157 info_ptr += 2;
8158 break;
8159 case DW_FORM_data4:
8160 case DW_FORM_ref4:
8161 info_ptr += 4;
8162 break;
8163 case DW_FORM_data8:
8164 case DW_FORM_ref8:
55f1336d 8165 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8166 info_ptr += 8;
8167 break;
0224619f
JK
8168 case DW_FORM_data16:
8169 info_ptr += 16;
8170 break;
4bb7a0a7 8171 case DW_FORM_string:
9b1c24c8 8172 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8173 info_ptr += bytes_read;
8174 break;
2dc7f7b3 8175 case DW_FORM_sec_offset:
4bb7a0a7 8176 case DW_FORM_strp:
36586728 8177 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8178 info_ptr += cu->header.offset_size;
8179 break;
2dc7f7b3 8180 case DW_FORM_exprloc:
4bb7a0a7
DJ
8181 case DW_FORM_block:
8182 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8183 info_ptr += bytes_read;
8184 break;
8185 case DW_FORM_block1:
8186 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8187 break;
8188 case DW_FORM_block2:
8189 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8190 break;
8191 case DW_FORM_block4:
8192 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8193 break;
8194 case DW_FORM_sdata:
8195 case DW_FORM_udata:
8196 case DW_FORM_ref_udata:
3019eac3
DE
8197 case DW_FORM_GNU_addr_index:
8198 case DW_FORM_GNU_str_index:
d521ce57 8199 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8200 break;
8201 case DW_FORM_indirect:
8202 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8203 info_ptr += bytes_read;
8204 /* We need to continue parsing from here, so just go back to
8205 the top. */
8206 goto skip_attribute;
8207
8208 default:
3e43a32a
MS
8209 error (_("Dwarf Error: Cannot handle %s "
8210 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8211 dwarf_form_name (form),
8212 bfd_get_filename (abfd));
8213 }
8214 }
8215
8216 if (abbrev->has_children)
dee91e82 8217 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8218 else
8219 return info_ptr;
8220}
8221
93311388 8222/* Locate ORIG_PDI's sibling.
dee91e82 8223 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8224
d521ce57 8225static const gdb_byte *
dee91e82
DE
8226locate_pdi_sibling (const struct die_reader_specs *reader,
8227 struct partial_die_info *orig_pdi,
d521ce57 8228 const gdb_byte *info_ptr)
91c24f0a
DC
8229{
8230 /* Do we know the sibling already? */
72bf9492 8231
91c24f0a
DC
8232 if (orig_pdi->sibling)
8233 return orig_pdi->sibling;
8234
8235 /* Are there any children to deal with? */
8236
8237 if (!orig_pdi->has_children)
8238 return info_ptr;
8239
4bb7a0a7 8240 /* Skip the children the long way. */
91c24f0a 8241
dee91e82 8242 return skip_children (reader, info_ptr);
91c24f0a
DC
8243}
8244
257e7a09 8245/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8246 not NULL. */
c906108c
SS
8247
8248static void
257e7a09
YQ
8249dwarf2_read_symtab (struct partial_symtab *self,
8250 struct objfile *objfile)
c906108c 8251{
257e7a09 8252 if (self->readin)
c906108c 8253 {
442e4d9c 8254 warning (_("bug: psymtab for %s is already read in."),
257e7a09 8255 self->filename);
442e4d9c
YQ
8256 }
8257 else
8258 {
8259 if (info_verbose)
c906108c 8260 {
442e4d9c 8261 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 8262 self->filename);
442e4d9c 8263 gdb_flush (gdb_stdout);
c906108c 8264 }
c906108c 8265
442e4d9c 8266 /* Restore our global data. */
9a3c8263
SM
8267 dwarf2_per_objfile
8268 = (struct dwarf2_per_objfile *) objfile_data (objfile,
8269 dwarf2_objfile_data_key);
10b3939b 8270
442e4d9c
YQ
8271 /* If this psymtab is constructed from a debug-only objfile, the
8272 has_section_at_zero flag will not necessarily be correct. We
8273 can get the correct value for this flag by looking at the data
8274 associated with the (presumably stripped) associated objfile. */
8275 if (objfile->separate_debug_objfile_backlink)
8276 {
8277 struct dwarf2_per_objfile *dpo_backlink
9a3c8263
SM
8278 = ((struct dwarf2_per_objfile *)
8279 objfile_data (objfile->separate_debug_objfile_backlink,
8280 dwarf2_objfile_data_key));
9a619af0 8281
442e4d9c
YQ
8282 dwarf2_per_objfile->has_section_at_zero
8283 = dpo_backlink->has_section_at_zero;
8284 }
b2ab525c 8285
442e4d9c 8286 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 8287
257e7a09 8288 psymtab_to_symtab_1 (self);
c906108c 8289
442e4d9c
YQ
8290 /* Finish up the debug error message. */
8291 if (info_verbose)
8292 printf_filtered (_("done.\n"));
c906108c 8293 }
95554aad
TT
8294
8295 process_cu_includes ();
c906108c 8296}
9cdd5dbd
DE
8297\f
8298/* Reading in full CUs. */
c906108c 8299
10b3939b
DJ
8300/* Add PER_CU to the queue. */
8301
8302static void
95554aad
TT
8303queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8304 enum language pretend_language)
10b3939b
DJ
8305{
8306 struct dwarf2_queue_item *item;
8307
8308 per_cu->queued = 1;
8d749320 8309 item = XNEW (struct dwarf2_queue_item);
10b3939b 8310 item->per_cu = per_cu;
95554aad 8311 item->pretend_language = pretend_language;
10b3939b
DJ
8312 item->next = NULL;
8313
8314 if (dwarf2_queue == NULL)
8315 dwarf2_queue = item;
8316 else
8317 dwarf2_queue_tail->next = item;
8318
8319 dwarf2_queue_tail = item;
8320}
8321
89e63ee4
DE
8322/* If PER_CU is not yet queued, add it to the queue.
8323 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8324 dependency.
0907af0c 8325 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8326 meaning either PER_CU is already queued or it is already loaded.
8327
8328 N.B. There is an invariant here that if a CU is queued then it is loaded.
8329 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8330
8331static int
89e63ee4 8332maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8333 struct dwarf2_per_cu_data *per_cu,
8334 enum language pretend_language)
8335{
8336 /* We may arrive here during partial symbol reading, if we need full
8337 DIEs to process an unusual case (e.g. template arguments). Do
8338 not queue PER_CU, just tell our caller to load its DIEs. */
8339 if (dwarf2_per_objfile->reading_partial_symbols)
8340 {
8341 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8342 return 1;
8343 return 0;
8344 }
8345
8346 /* Mark the dependence relation so that we don't flush PER_CU
8347 too early. */
89e63ee4
DE
8348 if (dependent_cu != NULL)
8349 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8350
8351 /* If it's already on the queue, we have nothing to do. */
8352 if (per_cu->queued)
8353 return 0;
8354
8355 /* If the compilation unit is already loaded, just mark it as
8356 used. */
8357 if (per_cu->cu != NULL)
8358 {
8359 per_cu->cu->last_used = 0;
8360 return 0;
8361 }
8362
8363 /* Add it to the queue. */
8364 queue_comp_unit (per_cu, pretend_language);
8365
8366 return 1;
8367}
8368
10b3939b
DJ
8369/* Process the queue. */
8370
8371static void
a0f42c21 8372process_queue (void)
10b3939b
DJ
8373{
8374 struct dwarf2_queue_item *item, *next_item;
8375
b4f54984 8376 if (dwarf_read_debug)
45cfd468
DE
8377 {
8378 fprintf_unfiltered (gdb_stdlog,
8379 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8380 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8381 }
8382
03dd20cc
DJ
8383 /* The queue starts out with one item, but following a DIE reference
8384 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
8385 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
8386 {
cc12ce38
DE
8387 if ((dwarf2_per_objfile->using_index
8388 ? !item->per_cu->v.quick->compunit_symtab
8389 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
8390 /* Skip dummy CUs. */
8391 && item->per_cu->cu != NULL)
f4dc4d17
DE
8392 {
8393 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 8394 unsigned int debug_print_threshold;
247f5c4f 8395 char buf[100];
f4dc4d17 8396
247f5c4f 8397 if (per_cu->is_debug_types)
f4dc4d17 8398 {
247f5c4f
DE
8399 struct signatured_type *sig_type =
8400 (struct signatured_type *) per_cu;
8401
8402 sprintf (buf, "TU %s at offset 0x%x",
73be47f5 8403 hex_string (sig_type->signature),
9c541725 8404 to_underlying (per_cu->sect_off));
73be47f5
DE
8405 /* There can be 100s of TUs.
8406 Only print them in verbose mode. */
8407 debug_print_threshold = 2;
f4dc4d17 8408 }
247f5c4f 8409 else
73be47f5 8410 {
9c541725
PA
8411 sprintf (buf, "CU at offset 0x%x",
8412 to_underlying (per_cu->sect_off));
73be47f5
DE
8413 debug_print_threshold = 1;
8414 }
247f5c4f 8415
b4f54984 8416 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8417 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
8418
8419 if (per_cu->is_debug_types)
8420 process_full_type_unit (per_cu, item->pretend_language);
8421 else
8422 process_full_comp_unit (per_cu, item->pretend_language);
8423
b4f54984 8424 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8425 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 8426 }
10b3939b
DJ
8427
8428 item->per_cu->queued = 0;
8429 next_item = item->next;
8430 xfree (item);
8431 }
8432
8433 dwarf2_queue_tail = NULL;
45cfd468 8434
b4f54984 8435 if (dwarf_read_debug)
45cfd468
DE
8436 {
8437 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 8438 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 8439 }
10b3939b
DJ
8440}
8441
8442/* Free all allocated queue entries. This function only releases anything if
8443 an error was thrown; if the queue was processed then it would have been
8444 freed as we went along. */
8445
8446static void
8447dwarf2_release_queue (void *dummy)
8448{
8449 struct dwarf2_queue_item *item, *last;
8450
8451 item = dwarf2_queue;
8452 while (item)
8453 {
8454 /* Anything still marked queued is likely to be in an
8455 inconsistent state, so discard it. */
8456 if (item->per_cu->queued)
8457 {
8458 if (item->per_cu->cu != NULL)
dee91e82 8459 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
8460 item->per_cu->queued = 0;
8461 }
8462
8463 last = item;
8464 item = item->next;
8465 xfree (last);
8466 }
8467
8468 dwarf2_queue = dwarf2_queue_tail = NULL;
8469}
8470
8471/* Read in full symbols for PST, and anything it depends on. */
8472
c906108c 8473static void
fba45db2 8474psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 8475{
10b3939b 8476 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
8477 int i;
8478
95554aad
TT
8479 if (pst->readin)
8480 return;
8481
aaa75496 8482 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
8483 if (!pst->dependencies[i]->readin
8484 && pst->dependencies[i]->user == NULL)
aaa75496
JB
8485 {
8486 /* Inform about additional files that need to be read in. */
8487 if (info_verbose)
8488 {
a3f17187 8489 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
8490 fputs_filtered (" ", gdb_stdout);
8491 wrap_here ("");
8492 fputs_filtered ("and ", gdb_stdout);
8493 wrap_here ("");
8494 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 8495 wrap_here (""); /* Flush output. */
aaa75496
JB
8496 gdb_flush (gdb_stdout);
8497 }
8498 psymtab_to_symtab_1 (pst->dependencies[i]);
8499 }
8500
9a3c8263 8501 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
8502
8503 if (per_cu == NULL)
aaa75496
JB
8504 {
8505 /* It's an include file, no symbols to read for it.
8506 Everything is in the parent symtab. */
8507 pst->readin = 1;
8508 return;
8509 }
c906108c 8510
a0f42c21 8511 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
8512}
8513
dee91e82
DE
8514/* Trivial hash function for die_info: the hash value of a DIE
8515 is its offset in .debug_info for this objfile. */
10b3939b 8516
dee91e82
DE
8517static hashval_t
8518die_hash (const void *item)
10b3939b 8519{
9a3c8263 8520 const struct die_info *die = (const struct die_info *) item;
6502dd73 8521
9c541725 8522 return to_underlying (die->sect_off);
dee91e82 8523}
63d06c5c 8524
dee91e82
DE
8525/* Trivial comparison function for die_info structures: two DIEs
8526 are equal if they have the same offset. */
98bfdba5 8527
dee91e82
DE
8528static int
8529die_eq (const void *item_lhs, const void *item_rhs)
8530{
9a3c8263
SM
8531 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8532 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8533
9c541725 8534 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8535}
c906108c 8536
dee91e82
DE
8537/* die_reader_func for load_full_comp_unit.
8538 This is identical to read_signatured_type_reader,
8539 but is kept separate for now. */
c906108c 8540
dee91e82
DE
8541static void
8542load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8543 const gdb_byte *info_ptr,
dee91e82
DE
8544 struct die_info *comp_unit_die,
8545 int has_children,
8546 void *data)
8547{
8548 struct dwarf2_cu *cu = reader->cu;
9a3c8263 8549 enum language *language_ptr = (enum language *) data;
6caca83c 8550
dee91e82
DE
8551 gdb_assert (cu->die_hash == NULL);
8552 cu->die_hash =
8553 htab_create_alloc_ex (cu->header.length / 12,
8554 die_hash,
8555 die_eq,
8556 NULL,
8557 &cu->comp_unit_obstack,
8558 hashtab_obstack_allocate,
8559 dummy_obstack_deallocate);
e142c38c 8560
dee91e82
DE
8561 if (has_children)
8562 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
8563 &info_ptr, comp_unit_die);
8564 cu->dies = comp_unit_die;
8565 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8566
8567 /* We try not to read any attributes in this function, because not
9cdd5dbd 8568 all CUs needed for references have been loaded yet, and symbol
10b3939b 8569 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8570 or we won't be able to build types correctly.
8571 Similarly, if we do not read the producer, we can not apply
8572 producer-specific interpretation. */
95554aad 8573 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 8574}
10b3939b 8575
dee91e82 8576/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 8577
dee91e82 8578static void
95554aad
TT
8579load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8580 enum language pretend_language)
dee91e82 8581{
3019eac3 8582 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 8583
f4dc4d17
DE
8584 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8585 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
8586}
8587
3da10d80
KS
8588/* Add a DIE to the delayed physname list. */
8589
8590static void
8591add_to_method_list (struct type *type, int fnfield_index, int index,
8592 const char *name, struct die_info *die,
8593 struct dwarf2_cu *cu)
8594{
8595 struct delayed_method_info mi;
8596 mi.type = type;
8597 mi.fnfield_index = fnfield_index;
8598 mi.index = index;
8599 mi.name = name;
8600 mi.die = die;
8601 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
8602}
8603
8604/* A cleanup for freeing the delayed method list. */
8605
8606static void
8607free_delayed_list (void *ptr)
8608{
8609 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
8610 if (cu->method_list != NULL)
8611 {
8612 VEC_free (delayed_method_info, cu->method_list);
8613 cu->method_list = NULL;
8614 }
8615}
8616
3693fdb3
PA
8617/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8618 "const" / "volatile". If so, decrements LEN by the length of the
8619 modifier and return true. Otherwise return false. */
8620
8621template<size_t N>
8622static bool
8623check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8624{
8625 size_t mod_len = sizeof (mod) - 1;
8626 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8627 {
8628 len -= mod_len;
8629 return true;
8630 }
8631 return false;
8632}
8633
3da10d80
KS
8634/* Compute the physnames of any methods on the CU's method list.
8635
8636 The computation of method physnames is delayed in order to avoid the
8637 (bad) condition that one of the method's formal parameters is of an as yet
8638 incomplete type. */
8639
8640static void
8641compute_delayed_physnames (struct dwarf2_cu *cu)
8642{
8643 int i;
8644 struct delayed_method_info *mi;
3693fdb3
PA
8645
8646 /* Only C++ delays computing physnames. */
8647 if (VEC_empty (delayed_method_info, cu->method_list))
8648 return;
8649 gdb_assert (cu->language == language_cplus);
8650
3da10d80
KS
8651 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
8652 {
1d06ead6 8653 const char *physname;
3da10d80
KS
8654 struct fn_fieldlist *fn_flp
8655 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 8656 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
8657 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
8658 = physname ? physname : "";
3693fdb3
PA
8659
8660 /* Since there's no tag to indicate whether a method is a
8661 const/volatile overload, extract that information out of the
8662 demangled name. */
8663 if (physname != NULL)
8664 {
8665 size_t len = strlen (physname);
8666
8667 while (1)
8668 {
8669 if (physname[len] == ')') /* shortcut */
8670 break;
8671 else if (check_modifier (physname, len, " const"))
8672 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
8673 else if (check_modifier (physname, len, " volatile"))
8674 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
8675 else
8676 break;
8677 }
8678 }
3da10d80
KS
8679 }
8680}
8681
a766d390
DE
8682/* Go objects should be embedded in a DW_TAG_module DIE,
8683 and it's not clear if/how imported objects will appear.
8684 To keep Go support simple until that's worked out,
8685 go back through what we've read and create something usable.
8686 We could do this while processing each DIE, and feels kinda cleaner,
8687 but that way is more invasive.
8688 This is to, for example, allow the user to type "p var" or "b main"
8689 without having to specify the package name, and allow lookups
8690 of module.object to work in contexts that use the expression
8691 parser. */
8692
8693static void
8694fixup_go_packaging (struct dwarf2_cu *cu)
8695{
8696 char *package_name = NULL;
8697 struct pending *list;
8698 int i;
8699
8700 for (list = global_symbols; list != NULL; list = list->next)
8701 {
8702 for (i = 0; i < list->nsyms; ++i)
8703 {
8704 struct symbol *sym = list->symbol[i];
8705
8706 if (SYMBOL_LANGUAGE (sym) == language_go
8707 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8708 {
8709 char *this_package_name = go_symbol_package_name (sym);
8710
8711 if (this_package_name == NULL)
8712 continue;
8713 if (package_name == NULL)
8714 package_name = this_package_name;
8715 else
8716 {
8717 if (strcmp (package_name, this_package_name) != 0)
8718 complaint (&symfile_complaints,
8719 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
8720 (symbol_symtab (sym) != NULL
8721 ? symtab_to_filename_for_display
8722 (symbol_symtab (sym))
4262abfb 8723 : objfile_name (cu->objfile)),
a766d390
DE
8724 this_package_name, package_name);
8725 xfree (this_package_name);
8726 }
8727 }
8728 }
8729 }
8730
8731 if (package_name != NULL)
8732 {
8733 struct objfile *objfile = cu->objfile;
34a68019 8734 const char *saved_package_name
224c3ddb
SM
8735 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
8736 package_name,
8737 strlen (package_name));
19f392bc
UW
8738 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8739 saved_package_name);
a766d390
DE
8740 struct symbol *sym;
8741
8742 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8743
e623cf5d 8744 sym = allocate_symbol (objfile);
f85f34ed 8745 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
8746 SYMBOL_SET_NAMES (sym, saved_package_name,
8747 strlen (saved_package_name), 0, objfile);
a766d390
DE
8748 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8749 e.g., "main" finds the "main" module and not C's main(). */
8750 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 8751 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
8752 SYMBOL_TYPE (sym) = type;
8753
8754 add_symbol_to_list (sym, &global_symbols);
8755
8756 xfree (package_name);
8757 }
8758}
8759
95554aad
TT
8760/* Return the symtab for PER_CU. This works properly regardless of
8761 whether we're using the index or psymtabs. */
8762
43f3e411
DE
8763static struct compunit_symtab *
8764get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad
TT
8765{
8766 return (dwarf2_per_objfile->using_index
43f3e411
DE
8767 ? per_cu->v.quick->compunit_symtab
8768 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
8769}
8770
8771/* A helper function for computing the list of all symbol tables
8772 included by PER_CU. */
8773
8774static void
43f3e411 8775recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 8776 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 8777 struct dwarf2_per_cu_data *per_cu,
43f3e411 8778 struct compunit_symtab *immediate_parent)
95554aad
TT
8779{
8780 void **slot;
8781 int ix;
43f3e411 8782 struct compunit_symtab *cust;
95554aad
TT
8783 struct dwarf2_per_cu_data *iter;
8784
8785 slot = htab_find_slot (all_children, per_cu, INSERT);
8786 if (*slot != NULL)
8787 {
8788 /* This inclusion and its children have been processed. */
8789 return;
8790 }
8791
8792 *slot = per_cu;
8793 /* Only add a CU if it has a symbol table. */
43f3e411
DE
8794 cust = get_compunit_symtab (per_cu);
8795 if (cust != NULL)
ec94af83
DE
8796 {
8797 /* If this is a type unit only add its symbol table if we haven't
8798 seen it yet (type unit per_cu's can share symtabs). */
8799 if (per_cu->is_debug_types)
8800 {
43f3e411 8801 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
8802 if (*slot == NULL)
8803 {
43f3e411
DE
8804 *slot = cust;
8805 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8806 if (cust->user == NULL)
8807 cust->user = immediate_parent;
ec94af83
DE
8808 }
8809 }
8810 else
f9125b6c 8811 {
43f3e411
DE
8812 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8813 if (cust->user == NULL)
8814 cust->user = immediate_parent;
f9125b6c 8815 }
ec94af83 8816 }
95554aad
TT
8817
8818 for (ix = 0;
796a7ff8 8819 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 8820 ++ix)
ec94af83
DE
8821 {
8822 recursively_compute_inclusions (result, all_children,
43f3e411 8823 all_type_symtabs, iter, cust);
ec94af83 8824 }
95554aad
TT
8825}
8826
43f3e411 8827/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
8828 PER_CU. */
8829
8830static void
43f3e411 8831compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 8832{
f4dc4d17
DE
8833 gdb_assert (! per_cu->is_debug_types);
8834
796a7ff8 8835 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
8836 {
8837 int ix, len;
ec94af83 8838 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
8839 struct compunit_symtab *compunit_symtab_iter;
8840 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 8841 htab_t all_children, all_type_symtabs;
43f3e411 8842 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
8843
8844 /* If we don't have a symtab, we can just skip this case. */
43f3e411 8845 if (cust == NULL)
95554aad
TT
8846 return;
8847
8848 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8849 NULL, xcalloc, xfree);
ec94af83
DE
8850 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8851 NULL, xcalloc, xfree);
95554aad
TT
8852
8853 for (ix = 0;
796a7ff8 8854 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 8855 ix, per_cu_iter);
95554aad 8856 ++ix)
ec94af83
DE
8857 {
8858 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 8859 all_type_symtabs, per_cu_iter,
43f3e411 8860 cust);
ec94af83 8861 }
95554aad 8862
ec94af83 8863 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
8864 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8865 cust->includes
8d749320
SM
8866 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8867 struct compunit_symtab *, len + 1);
95554aad 8868 for (ix = 0;
43f3e411
DE
8869 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8870 compunit_symtab_iter);
95554aad 8871 ++ix)
43f3e411
DE
8872 cust->includes[ix] = compunit_symtab_iter;
8873 cust->includes[len] = NULL;
95554aad 8874
43f3e411 8875 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 8876 htab_delete (all_children);
ec94af83 8877 htab_delete (all_type_symtabs);
95554aad
TT
8878 }
8879}
8880
8881/* Compute the 'includes' field for the symtabs of all the CUs we just
8882 read. */
8883
8884static void
8885process_cu_includes (void)
8886{
8887 int ix;
8888 struct dwarf2_per_cu_data *iter;
8889
8890 for (ix = 0;
8891 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8892 ix, iter);
8893 ++ix)
f4dc4d17
DE
8894 {
8895 if (! iter->is_debug_types)
43f3e411 8896 compute_compunit_symtab_includes (iter);
f4dc4d17 8897 }
95554aad
TT
8898
8899 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8900}
8901
9cdd5dbd 8902/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
8903 already been loaded into memory. */
8904
8905static void
95554aad
TT
8906process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8907 enum language pretend_language)
10b3939b 8908{
10b3939b 8909 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 8910 struct objfile *objfile = per_cu->objfile;
3e29f34a 8911 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 8912 CORE_ADDR lowpc, highpc;
43f3e411 8913 struct compunit_symtab *cust;
33c7c59d 8914 struct cleanup *delayed_list_cleanup;
10b3939b 8915 CORE_ADDR baseaddr;
4359dff1 8916 struct block *static_block;
3e29f34a 8917 CORE_ADDR addr;
10b3939b
DJ
8918
8919 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8920
10b3939b 8921 buildsym_init ();
33c7c59d 8922 scoped_free_pendings free_pending;
3da10d80 8923 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
8924
8925 cu->list_in_scope = &file_symbols;
c906108c 8926
95554aad
TT
8927 cu->language = pretend_language;
8928 cu->language_defn = language_def (cu->language);
8929
c906108c 8930 /* Do line number decoding in read_file_scope () */
10b3939b 8931 process_die (cu->dies, cu);
c906108c 8932
a766d390
DE
8933 /* For now fudge the Go package. */
8934 if (cu->language == language_go)
8935 fixup_go_packaging (cu);
8936
3da10d80
KS
8937 /* Now that we have processed all the DIEs in the CU, all the types
8938 should be complete, and it should now be safe to compute all of the
8939 physnames. */
8940 compute_delayed_physnames (cu);
8941 do_cleanups (delayed_list_cleanup);
8942
fae299cd
DC
8943 /* Some compilers don't define a DW_AT_high_pc attribute for the
8944 compilation unit. If the DW_AT_high_pc is missing, synthesize
8945 it, by scanning the DIE's below the compilation unit. */
10b3939b 8946 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 8947
3e29f34a
MR
8948 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8949 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
8950
8951 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8952 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8953 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8954 addrmap to help ensure it has an accurate map of pc values belonging to
8955 this comp unit. */
8956 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8957
43f3e411
DE
8958 cust = end_symtab_from_static_block (static_block,
8959 SECT_OFF_TEXT (objfile), 0);
c906108c 8960
43f3e411 8961 if (cust != NULL)
c906108c 8962 {
df15bd07 8963 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 8964
8be455d7
JK
8965 /* Set symtab language to language from DW_AT_language. If the
8966 compilation is from a C file generated by language preprocessors, do
8967 not set the language if it was already deduced by start_subfile. */
43f3e411 8968 if (!(cu->language == language_c
40e3ad0e 8969 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 8970 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
8971
8972 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8973 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
8974 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8975 there were bugs in prologue debug info, fixed later in GCC-4.5
8976 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
8977
8978 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8979 needed, it would be wrong due to missing DW_AT_producer there.
8980
8981 Still one can confuse GDB by using non-standard GCC compilation
8982 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8983 */
ab260dad 8984 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 8985 cust->locations_valid = 1;
e0d00bc7
JK
8986
8987 if (gcc_4_minor >= 5)
43f3e411 8988 cust->epilogue_unwind_valid = 1;
96408a79 8989
43f3e411 8990 cust->call_site_htab = cu->call_site_htab;
c906108c 8991 }
9291a0cd
TT
8992
8993 if (dwarf2_per_objfile->using_index)
43f3e411 8994 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
8995 else
8996 {
8997 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8998 pst->compunit_symtab = cust;
9291a0cd
TT
8999 pst->readin = 1;
9000 }
c906108c 9001
95554aad
TT
9002 /* Push it for inclusion processing later. */
9003 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
f4dc4d17 9004}
45cfd468 9005
f4dc4d17
DE
9006/* Generate full symbol information for type unit PER_CU, whose DIEs have
9007 already been loaded into memory. */
9008
9009static void
9010process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9011 enum language pretend_language)
9012{
9013 struct dwarf2_cu *cu = per_cu->cu;
9014 struct objfile *objfile = per_cu->objfile;
43f3e411 9015 struct compunit_symtab *cust;
33c7c59d 9016 struct cleanup *delayed_list_cleanup;
0186c6a7
DE
9017 struct signatured_type *sig_type;
9018
9019 gdb_assert (per_cu->is_debug_types);
9020 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
9021
9022 buildsym_init ();
33c7c59d 9023 scoped_free_pendings free_pending;
f4dc4d17
DE
9024 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
9025
9026 cu->list_in_scope = &file_symbols;
9027
9028 cu->language = pretend_language;
9029 cu->language_defn = language_def (cu->language);
9030
9031 /* The symbol tables are set up in read_type_unit_scope. */
9032 process_die (cu->dies, cu);
9033
9034 /* For now fudge the Go package. */
9035 if (cu->language == language_go)
9036 fixup_go_packaging (cu);
9037
9038 /* Now that we have processed all the DIEs in the CU, all the types
9039 should be complete, and it should now be safe to compute all of the
9040 physnames. */
9041 compute_delayed_physnames (cu);
9042 do_cleanups (delayed_list_cleanup);
9043
9044 /* TUs share symbol tables.
9045 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9046 of it with end_expandable_symtab. Otherwise, complete the addition of
9047 this TU's symbols to the existing symtab. */
43f3e411 9048 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9049 {
43f3e411
DE
9050 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9051 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9052
43f3e411 9053 if (cust != NULL)
f4dc4d17
DE
9054 {
9055 /* Set symtab language to language from DW_AT_language. If the
9056 compilation is from a C file generated by language preprocessors,
9057 do not set the language if it was already deduced by
9058 start_subfile. */
43f3e411
DE
9059 if (!(cu->language == language_c
9060 && COMPUNIT_FILETABS (cust)->language != language_c))
9061 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9062 }
9063 }
9064 else
9065 {
0ab9ce85 9066 augment_type_symtab ();
43f3e411 9067 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9068 }
9069
9070 if (dwarf2_per_objfile->using_index)
43f3e411 9071 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
9072 else
9073 {
9074 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 9075 pst->compunit_symtab = cust;
f4dc4d17 9076 pst->readin = 1;
45cfd468 9077 }
c906108c
SS
9078}
9079
95554aad
TT
9080/* Process an imported unit DIE. */
9081
9082static void
9083process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9084{
9085 struct attribute *attr;
9086
f4dc4d17
DE
9087 /* For now we don't handle imported units in type units. */
9088 if (cu->per_cu->is_debug_types)
9089 {
9090 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9091 " supported in type units [in module %s]"),
4262abfb 9092 objfile_name (cu->objfile));
f4dc4d17
DE
9093 }
9094
95554aad
TT
9095 attr = dwarf2_attr (die, DW_AT_import, cu);
9096 if (attr != NULL)
9097 {
9c541725
PA
9098 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9099 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9100 dwarf2_per_cu_data *per_cu
9101 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
95554aad 9102
69d751e3 9103 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
9104 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9105 load_full_comp_unit (per_cu, cu->language);
9106
796a7ff8 9107 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
9108 per_cu);
9109 }
9110}
9111
4c8aa72d
PA
9112/* RAII object that represents a process_die scope: i.e.,
9113 starts/finishes processing a DIE. */
9114class process_die_scope
adde2bff 9115{
4c8aa72d
PA
9116public:
9117 process_die_scope (die_info *die, dwarf2_cu *cu)
9118 : m_die (die), m_cu (cu)
9119 {
9120 /* We should only be processing DIEs not already in process. */
9121 gdb_assert (!m_die->in_process);
9122 m_die->in_process = true;
9123 }
8c3cb9fa 9124
4c8aa72d
PA
9125 ~process_die_scope ()
9126 {
9127 m_die->in_process = false;
9128
9129 /* If we're done processing the DIE for the CU that owns the line
9130 header, we don't need the line header anymore. */
9131 if (m_cu->line_header_die_owner == m_die)
9132 {
9133 delete m_cu->line_header;
9134 m_cu->line_header = NULL;
9135 m_cu->line_header_die_owner = NULL;
9136 }
9137 }
9138
9139private:
9140 die_info *m_die;
9141 dwarf2_cu *m_cu;
9142};
adde2bff 9143
c906108c
SS
9144/* Process a die and its children. */
9145
9146static void
e7c27a73 9147process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9148{
4c8aa72d 9149 process_die_scope scope (die, cu);
adde2bff 9150
c906108c
SS
9151 switch (die->tag)
9152 {
9153 case DW_TAG_padding:
9154 break;
9155 case DW_TAG_compile_unit:
95554aad 9156 case DW_TAG_partial_unit:
e7c27a73 9157 read_file_scope (die, cu);
c906108c 9158 break;
348e048f
DE
9159 case DW_TAG_type_unit:
9160 read_type_unit_scope (die, cu);
9161 break;
c906108c 9162 case DW_TAG_subprogram:
c906108c 9163 case DW_TAG_inlined_subroutine:
edb3359d 9164 read_func_scope (die, cu);
c906108c
SS
9165 break;
9166 case DW_TAG_lexical_block:
14898363
L
9167 case DW_TAG_try_block:
9168 case DW_TAG_catch_block:
e7c27a73 9169 read_lexical_block_scope (die, cu);
c906108c 9170 break;
216f72a1 9171 case DW_TAG_call_site:
96408a79
SA
9172 case DW_TAG_GNU_call_site:
9173 read_call_site_scope (die, cu);
9174 break;
c906108c 9175 case DW_TAG_class_type:
680b30c7 9176 case DW_TAG_interface_type:
c906108c
SS
9177 case DW_TAG_structure_type:
9178 case DW_TAG_union_type:
134d01f1 9179 process_structure_scope (die, cu);
c906108c
SS
9180 break;
9181 case DW_TAG_enumeration_type:
134d01f1 9182 process_enumeration_scope (die, cu);
c906108c 9183 break;
134d01f1 9184
f792889a
DJ
9185 /* These dies have a type, but processing them does not create
9186 a symbol or recurse to process the children. Therefore we can
9187 read them on-demand through read_type_die. */
c906108c 9188 case DW_TAG_subroutine_type:
72019c9c 9189 case DW_TAG_set_type:
c906108c 9190 case DW_TAG_array_type:
c906108c 9191 case DW_TAG_pointer_type:
c906108c 9192 case DW_TAG_ptr_to_member_type:
c906108c 9193 case DW_TAG_reference_type:
4297a3f0 9194 case DW_TAG_rvalue_reference_type:
c906108c 9195 case DW_TAG_string_type:
c906108c 9196 break;
134d01f1 9197
c906108c 9198 case DW_TAG_base_type:
a02abb62 9199 case DW_TAG_subrange_type:
cb249c71 9200 case DW_TAG_typedef:
134d01f1
DJ
9201 /* Add a typedef symbol for the type definition, if it has a
9202 DW_AT_name. */
f792889a 9203 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9204 break;
c906108c 9205 case DW_TAG_common_block:
e7c27a73 9206 read_common_block (die, cu);
c906108c
SS
9207 break;
9208 case DW_TAG_common_inclusion:
9209 break;
d9fa45fe 9210 case DW_TAG_namespace:
4d4ec4e5 9211 cu->processing_has_namespace_info = 1;
e7c27a73 9212 read_namespace (die, cu);
d9fa45fe 9213 break;
5d7cb8df 9214 case DW_TAG_module:
4d4ec4e5 9215 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
9216 read_module (die, cu);
9217 break;
d9fa45fe 9218 case DW_TAG_imported_declaration:
74921315
KS
9219 cu->processing_has_namespace_info = 1;
9220 if (read_namespace_alias (die, cu))
9221 break;
9222 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 9223 case DW_TAG_imported_module:
4d4ec4e5 9224 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
9225 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9226 || cu->language != language_fortran))
9227 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
9228 dwarf_tag_name (die->tag));
9229 read_import_statement (die, cu);
d9fa45fe 9230 break;
95554aad
TT
9231
9232 case DW_TAG_imported_unit:
9233 process_imported_unit_die (die, cu);
9234 break;
9235
71a3c369
TT
9236 case DW_TAG_variable:
9237 read_variable (die, cu);
9238 break;
9239
c906108c 9240 default:
e7c27a73 9241 new_symbol (die, NULL, cu);
c906108c
SS
9242 break;
9243 }
9244}
ca69b9e6
DE
9245\f
9246/* DWARF name computation. */
c906108c 9247
94af9270
KS
9248/* A helper function for dwarf2_compute_name which determines whether DIE
9249 needs to have the name of the scope prepended to the name listed in the
9250 die. */
9251
9252static int
9253die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9254{
1c809c68
TT
9255 struct attribute *attr;
9256
94af9270
KS
9257 switch (die->tag)
9258 {
9259 case DW_TAG_namespace:
9260 case DW_TAG_typedef:
9261 case DW_TAG_class_type:
9262 case DW_TAG_interface_type:
9263 case DW_TAG_structure_type:
9264 case DW_TAG_union_type:
9265 case DW_TAG_enumeration_type:
9266 case DW_TAG_enumerator:
9267 case DW_TAG_subprogram:
08a76f8a 9268 case DW_TAG_inlined_subroutine:
94af9270 9269 case DW_TAG_member:
74921315 9270 case DW_TAG_imported_declaration:
94af9270
KS
9271 return 1;
9272
9273 case DW_TAG_variable:
c2b0a229 9274 case DW_TAG_constant:
94af9270
KS
9275 /* We only need to prefix "globally" visible variables. These include
9276 any variable marked with DW_AT_external or any variable that
9277 lives in a namespace. [Variables in anonymous namespaces
9278 require prefixing, but they are not DW_AT_external.] */
9279
9280 if (dwarf2_attr (die, DW_AT_specification, cu))
9281 {
9282 struct dwarf2_cu *spec_cu = cu;
9a619af0 9283
94af9270
KS
9284 return die_needs_namespace (die_specification (die, &spec_cu),
9285 spec_cu);
9286 }
9287
1c809c68 9288 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9289 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9290 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9291 return 0;
9292 /* A variable in a lexical block of some kind does not need a
9293 namespace, even though in C++ such variables may be external
9294 and have a mangled name. */
9295 if (die->parent->tag == DW_TAG_lexical_block
9296 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9297 || die->parent->tag == DW_TAG_catch_block
9298 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9299 return 0;
9300 return 1;
94af9270
KS
9301
9302 default:
9303 return 0;
9304 }
9305}
9306
73b9be8b
KS
9307/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9308 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9309 defined for the given DIE. */
9310
9311static struct attribute *
9312dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9313{
9314 struct attribute *attr;
9315
9316 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9317 if (attr == NULL)
9318 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9319
9320 return attr;
9321}
9322
9323/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9324 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9325 defined for the given DIE. */
9326
9327static const char *
9328dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9329{
9330 const char *linkage_name;
9331
9332 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9333 if (linkage_name == NULL)
9334 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9335
9336 return linkage_name;
9337}
9338
94af9270 9339/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 9340 compute the physname for the object, which include a method's:
9c37b5ae 9341 - formal parameters (C++),
a766d390 9342 - receiver type (Go),
a766d390
DE
9343
9344 The term "physname" is a bit confusing.
9345 For C++, for example, it is the demangled name.
9346 For Go, for example, it's the mangled name.
94af9270 9347
af6b7be1
JB
9348 For Ada, return the DIE's linkage name rather than the fully qualified
9349 name. PHYSNAME is ignored..
9350
94af9270
KS
9351 The result is allocated on the objfile_obstack and canonicalized. */
9352
9353static const char *
15d034d0
TT
9354dwarf2_compute_name (const char *name,
9355 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
9356 int physname)
9357{
bb5ed363
DE
9358 struct objfile *objfile = cu->objfile;
9359
94af9270
KS
9360 if (name == NULL)
9361 name = dwarf2_name (die, cu);
9362
2ee7123e
DE
9363 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9364 but otherwise compute it by typename_concat inside GDB.
9365 FIXME: Actually this is not really true, or at least not always true.
9366 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
9367 Fortran names because there is no mangling standard. So new_symbol_full
9368 will set the demangled name to the result of dwarf2_full_name, and it is
9369 the demangled name that GDB uses if it exists. */
f55ee35c
JK
9370 if (cu->language == language_ada
9371 || (cu->language == language_fortran && physname))
9372 {
9373 /* For Ada unit, we prefer the linkage name over the name, as
9374 the former contains the exported name, which the user expects
9375 to be able to reference. Ideally, we want the user to be able
9376 to reference this entity using either natural or linkage name,
9377 but we haven't started looking at this enhancement yet. */
73b9be8b 9378 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 9379
2ee7123e
DE
9380 if (linkage_name != NULL)
9381 return linkage_name;
f55ee35c
JK
9382 }
9383
94af9270
KS
9384 /* These are the only languages we know how to qualify names in. */
9385 if (name != NULL
9c37b5ae 9386 && (cu->language == language_cplus
c44af4eb
TT
9387 || cu->language == language_fortran || cu->language == language_d
9388 || cu->language == language_rust))
94af9270
KS
9389 {
9390 if (die_needs_namespace (die, cu))
9391 {
9392 long length;
0d5cff50 9393 const char *prefix;
34a68019 9394 const char *canonical_name = NULL;
94af9270 9395
d7e74731
PA
9396 string_file buf;
9397
94af9270 9398 prefix = determine_prefix (die, cu);
94af9270
KS
9399 if (*prefix != '\0')
9400 {
f55ee35c
JK
9401 char *prefixed_name = typename_concat (NULL, prefix, name,
9402 physname, cu);
9a619af0 9403
d7e74731 9404 buf.puts (prefixed_name);
94af9270
KS
9405 xfree (prefixed_name);
9406 }
9407 else
d7e74731 9408 buf.puts (name);
94af9270 9409
98bfdba5
PA
9410 /* Template parameters may be specified in the DIE's DW_AT_name, or
9411 as children with DW_TAG_template_type_param or
9412 DW_TAG_value_type_param. If the latter, add them to the name
9413 here. If the name already has template parameters, then
9414 skip this step; some versions of GCC emit both, and
9415 it is more efficient to use the pre-computed name.
9416
9417 Something to keep in mind about this process: it is very
9418 unlikely, or in some cases downright impossible, to produce
9419 something that will match the mangled name of a function.
9420 If the definition of the function has the same debug info,
9421 we should be able to match up with it anyway. But fallbacks
9422 using the minimal symbol, for instance to find a method
9423 implemented in a stripped copy of libstdc++, will not work.
9424 If we do not have debug info for the definition, we will have to
9425 match them up some other way.
9426
9427 When we do name matching there is a related problem with function
9428 templates; two instantiated function templates are allowed to
9429 differ only by their return types, which we do not add here. */
9430
9431 if (cu->language == language_cplus && strchr (name, '<') == NULL)
9432 {
9433 struct attribute *attr;
9434 struct die_info *child;
9435 int first = 1;
9436
9437 die->building_fullname = 1;
9438
9439 for (child = die->child; child != NULL; child = child->sibling)
9440 {
9441 struct type *type;
12df843f 9442 LONGEST value;
d521ce57 9443 const gdb_byte *bytes;
98bfdba5
PA
9444 struct dwarf2_locexpr_baton *baton;
9445 struct value *v;
9446
9447 if (child->tag != DW_TAG_template_type_param
9448 && child->tag != DW_TAG_template_value_param)
9449 continue;
9450
9451 if (first)
9452 {
d7e74731 9453 buf.puts ("<");
98bfdba5
PA
9454 first = 0;
9455 }
9456 else
d7e74731 9457 buf.puts (", ");
98bfdba5
PA
9458
9459 attr = dwarf2_attr (child, DW_AT_type, cu);
9460 if (attr == NULL)
9461 {
9462 complaint (&symfile_complaints,
9463 _("template parameter missing DW_AT_type"));
d7e74731 9464 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
9465 continue;
9466 }
9467 type = die_type (child, cu);
9468
9469 if (child->tag == DW_TAG_template_type_param)
9470 {
d7e74731 9471 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
9472 continue;
9473 }
9474
9475 attr = dwarf2_attr (child, DW_AT_const_value, cu);
9476 if (attr == NULL)
9477 {
9478 complaint (&symfile_complaints,
3e43a32a
MS
9479 _("template parameter missing "
9480 "DW_AT_const_value"));
d7e74731 9481 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
9482 continue;
9483 }
9484
9485 dwarf2_const_value_attr (attr, type, name,
9486 &cu->comp_unit_obstack, cu,
9487 &value, &bytes, &baton);
9488
9489 if (TYPE_NOSIGN (type))
9490 /* GDB prints characters as NUMBER 'CHAR'. If that's
9491 changed, this can use value_print instead. */
d7e74731 9492 c_printchar (value, type, &buf);
98bfdba5
PA
9493 else
9494 {
9495 struct value_print_options opts;
9496
9497 if (baton != NULL)
9498 v = dwarf2_evaluate_loc_desc (type, NULL,
9499 baton->data,
9500 baton->size,
9501 baton->per_cu);
9502 else if (bytes != NULL)
9503 {
9504 v = allocate_value (type);
9505 memcpy (value_contents_writeable (v), bytes,
9506 TYPE_LENGTH (type));
9507 }
9508 else
9509 v = value_from_longest (type, value);
9510
3e43a32a
MS
9511 /* Specify decimal so that we do not depend on
9512 the radix. */
98bfdba5
PA
9513 get_formatted_print_options (&opts, 'd');
9514 opts.raw = 1;
d7e74731 9515 value_print (v, &buf, &opts);
98bfdba5
PA
9516 release_value (v);
9517 value_free (v);
9518 }
9519 }
9520
9521 die->building_fullname = 0;
9522
9523 if (!first)
9524 {
9525 /* Close the argument list, with a space if necessary
9526 (nested templates). */
d7e74731
PA
9527 if (!buf.empty () && buf.string ().back () == '>')
9528 buf.puts (" >");
98bfdba5 9529 else
d7e74731 9530 buf.puts (">");
98bfdba5
PA
9531 }
9532 }
9533
9c37b5ae 9534 /* For C++ methods, append formal parameter type
94af9270 9535 information, if PHYSNAME. */
6e70227d 9536
94af9270 9537 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 9538 && cu->language == language_cplus)
94af9270
KS
9539 {
9540 struct type *type = read_type_die (die, cu);
9541
d7e74731 9542 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 9543 &type_print_raw_options);
94af9270 9544
9c37b5ae 9545 if (cu->language == language_cplus)
94af9270 9546 {
60430eff
DJ
9547 /* Assume that an artificial first parameter is
9548 "this", but do not crash if it is not. RealView
9549 marks unnamed (and thus unused) parameters as
9550 artificial; there is no way to differentiate
9551 the two cases. */
94af9270
KS
9552 if (TYPE_NFIELDS (type) > 0
9553 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 9554 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
9555 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
9556 0))))
d7e74731 9557 buf.puts (" const");
94af9270
KS
9558 }
9559 }
9560
d7e74731 9561 const std::string &intermediate_name = buf.string ();
94af9270
KS
9562
9563 if (cu->language == language_cplus)
34a68019 9564 canonical_name
322a8516 9565 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
9566 &objfile->per_bfd->storage_obstack);
9567
9568 /* If we only computed INTERMEDIATE_NAME, or if
9569 INTERMEDIATE_NAME is already canonical, then we need to
9570 copy it to the appropriate obstack. */
322a8516 9571 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
9572 name = ((const char *)
9573 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
9574 intermediate_name.c_str (),
9575 intermediate_name.length ()));
34a68019
TT
9576 else
9577 name = canonical_name;
94af9270
KS
9578 }
9579 }
9580
9581 return name;
9582}
9583
0114d602
DJ
9584/* Return the fully qualified name of DIE, based on its DW_AT_name.
9585 If scope qualifiers are appropriate they will be added. The result
34a68019 9586 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
9587 not have a name. NAME may either be from a previous call to
9588 dwarf2_name or NULL.
9589
9c37b5ae 9590 The output string will be canonicalized (if C++). */
0114d602
DJ
9591
9592static const char *
15d034d0 9593dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 9594{
94af9270
KS
9595 return dwarf2_compute_name (name, die, cu, 0);
9596}
0114d602 9597
94af9270
KS
9598/* Construct a physname for the given DIE in CU. NAME may either be
9599 from a previous call to dwarf2_name or NULL. The result will be
9600 allocated on the objfile_objstack or NULL if the DIE does not have a
9601 name.
0114d602 9602
9c37b5ae 9603 The output string will be canonicalized (if C++). */
0114d602 9604
94af9270 9605static const char *
15d034d0 9606dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 9607{
bb5ed363 9608 struct objfile *objfile = cu->objfile;
900e11f9 9609 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
9610 int need_copy = 1;
9611
9612 /* In this case dwarf2_compute_name is just a shortcut not building anything
9613 on its own. */
9614 if (!die_needs_namespace (die, cu))
9615 return dwarf2_compute_name (name, die, cu, 1);
9616
73b9be8b 9617 mangled = dw2_linkage_name (die, cu);
900e11f9 9618
e98c9e7c
TT
9619 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9620 See https://github.com/rust-lang/rust/issues/32925. */
9621 if (cu->language == language_rust && mangled != NULL
9622 && strchr (mangled, '{') != NULL)
9623 mangled = NULL;
9624
900e11f9
JK
9625 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9626 has computed. */
791afaa2 9627 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 9628 if (mangled != NULL)
900e11f9 9629 {
900e11f9
JK
9630 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
9631 type. It is easier for GDB users to search for such functions as
9632 `name(params)' than `long name(params)'. In such case the minimal
9633 symbol names do not match the full symbol names but for template
9634 functions there is never a need to look up their definition from their
9635 declaration so the only disadvantage remains the minimal symbol
9636 variant `long name(params)' does not have the proper inferior type.
9637 */
9638
a766d390
DE
9639 if (cu->language == language_go)
9640 {
9641 /* This is a lie, but we already lie to the caller new_symbol_full.
9642 new_symbol_full assumes we return the mangled name.
9643 This just undoes that lie until things are cleaned up. */
a766d390
DE
9644 }
9645 else
9646 {
791afaa2
TT
9647 demangled.reset (gdb_demangle (mangled,
9648 (DMGL_PARAMS | DMGL_ANSI
9649 | DMGL_RET_DROP)));
a766d390 9650 }
900e11f9 9651 if (demangled)
791afaa2 9652 canon = demangled.get ();
900e11f9
JK
9653 else
9654 {
9655 canon = mangled;
9656 need_copy = 0;
9657 }
9658 }
9659
9660 if (canon == NULL || check_physname)
9661 {
9662 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9663
9664 if (canon != NULL && strcmp (physname, canon) != 0)
9665 {
9666 /* It may not mean a bug in GDB. The compiler could also
9667 compute DW_AT_linkage_name incorrectly. But in such case
9668 GDB would need to be bug-to-bug compatible. */
9669
9670 complaint (&symfile_complaints,
9671 _("Computed physname <%s> does not match demangled <%s> "
9672 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9c541725 9673 physname, canon, mangled, to_underlying (die->sect_off),
4262abfb 9674 objfile_name (objfile));
900e11f9
JK
9675
9676 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9677 is available here - over computed PHYSNAME. It is safer
9678 against both buggy GDB and buggy compilers. */
9679
9680 retval = canon;
9681 }
9682 else
9683 {
9684 retval = physname;
9685 need_copy = 0;
9686 }
9687 }
9688 else
9689 retval = canon;
9690
9691 if (need_copy)
224c3ddb
SM
9692 retval = ((const char *)
9693 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9694 retval, strlen (retval)));
900e11f9 9695
900e11f9 9696 return retval;
0114d602
DJ
9697}
9698
74921315
KS
9699/* Inspect DIE in CU for a namespace alias. If one exists, record
9700 a new symbol for it.
9701
9702 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9703
9704static int
9705read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
9706{
9707 struct attribute *attr;
9708
9709 /* If the die does not have a name, this is not a namespace
9710 alias. */
9711 attr = dwarf2_attr (die, DW_AT_name, cu);
9712 if (attr != NULL)
9713 {
9714 int num;
9715 struct die_info *d = die;
9716 struct dwarf2_cu *imported_cu = cu;
9717
9718 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9719 keep inspecting DIEs until we hit the underlying import. */
9720#define MAX_NESTED_IMPORTED_DECLARATIONS 100
9721 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9722 {
9723 attr = dwarf2_attr (d, DW_AT_import, cu);
9724 if (attr == NULL)
9725 break;
9726
9727 d = follow_die_ref (d, attr, &imported_cu);
9728 if (d->tag != DW_TAG_imported_declaration)
9729 break;
9730 }
9731
9732 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
9733 {
9734 complaint (&symfile_complaints,
9735 _("DIE at 0x%x has too many recursively imported "
9c541725 9736 "declarations"), to_underlying (d->sect_off));
74921315
KS
9737 return 0;
9738 }
9739
9740 if (attr != NULL)
9741 {
9742 struct type *type;
9c541725 9743 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 9744
9c541725 9745 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
9746 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
9747 {
9748 /* This declaration is a global namespace alias. Add
9749 a symbol for it whose type is the aliased namespace. */
9750 new_symbol (die, type, cu);
9751 return 1;
9752 }
9753 }
9754 }
9755
9756 return 0;
9757}
9758
22cee43f
PMR
9759/* Return the using directives repository (global or local?) to use in the
9760 current context for LANGUAGE.
9761
9762 For Ada, imported declarations can materialize renamings, which *may* be
9763 global. However it is impossible (for now?) in DWARF to distinguish
9764 "external" imported declarations and "static" ones. As all imported
9765 declarations seem to be static in all other languages, make them all CU-wide
9766 global only in Ada. */
9767
9768static struct using_direct **
9769using_directives (enum language language)
9770{
9771 if (language == language_ada && context_stack_depth == 0)
9772 return &global_using_directives;
9773 else
9774 return &local_using_directives;
9775}
9776
27aa8d6a
SW
9777/* Read the import statement specified by the given die and record it. */
9778
9779static void
9780read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9781{
bb5ed363 9782 struct objfile *objfile = cu->objfile;
27aa8d6a 9783 struct attribute *import_attr;
32019081 9784 struct die_info *imported_die, *child_die;
de4affc9 9785 struct dwarf2_cu *imported_cu;
27aa8d6a 9786 const char *imported_name;
794684b6 9787 const char *imported_name_prefix;
13387711
SW
9788 const char *canonical_name;
9789 const char *import_alias;
9790 const char *imported_declaration = NULL;
794684b6 9791 const char *import_prefix;
eb1e02fd 9792 std::vector<const char *> excludes;
13387711 9793
27aa8d6a
SW
9794 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9795 if (import_attr == NULL)
9796 {
9797 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9798 dwarf_tag_name (die->tag));
9799 return;
9800 }
9801
de4affc9
CC
9802 imported_cu = cu;
9803 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9804 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
9805 if (imported_name == NULL)
9806 {
9807 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9808
9809 The import in the following code:
9810 namespace A
9811 {
9812 typedef int B;
9813 }
9814
9815 int main ()
9816 {
9817 using A::B;
9818 B b;
9819 return b;
9820 }
9821
9822 ...
9823 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9824 <52> DW_AT_decl_file : 1
9825 <53> DW_AT_decl_line : 6
9826 <54> DW_AT_import : <0x75>
9827 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9828 <59> DW_AT_name : B
9829 <5b> DW_AT_decl_file : 1
9830 <5c> DW_AT_decl_line : 2
9831 <5d> DW_AT_type : <0x6e>
9832 ...
9833 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9834 <76> DW_AT_byte_size : 4
9835 <77> DW_AT_encoding : 5 (signed)
9836
9837 imports the wrong die ( 0x75 instead of 0x58 ).
9838 This case will be ignored until the gcc bug is fixed. */
9839 return;
9840 }
9841
82856980
SW
9842 /* Figure out the local name after import. */
9843 import_alias = dwarf2_name (die, cu);
27aa8d6a 9844
794684b6
SW
9845 /* Figure out where the statement is being imported to. */
9846 import_prefix = determine_prefix (die, cu);
9847
9848 /* Figure out what the scope of the imported die is and prepend it
9849 to the name of the imported die. */
de4affc9 9850 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 9851
f55ee35c
JK
9852 if (imported_die->tag != DW_TAG_namespace
9853 && imported_die->tag != DW_TAG_module)
794684b6 9854 {
13387711
SW
9855 imported_declaration = imported_name;
9856 canonical_name = imported_name_prefix;
794684b6 9857 }
13387711 9858 else if (strlen (imported_name_prefix) > 0)
12aaed36 9859 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
9860 imported_name_prefix,
9861 (cu->language == language_d ? "." : "::"),
9862 imported_name, (char *) NULL);
13387711
SW
9863 else
9864 canonical_name = imported_name;
794684b6 9865
32019081
JK
9866 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9867 for (child_die = die->child; child_die && child_die->tag;
9868 child_die = sibling_die (child_die))
9869 {
9870 /* DWARF-4: A Fortran use statement with a “rename list” may be
9871 represented by an imported module entry with an import attribute
9872 referring to the module and owned entries corresponding to those
9873 entities that are renamed as part of being imported. */
9874
9875 if (child_die->tag != DW_TAG_imported_declaration)
9876 {
9877 complaint (&symfile_complaints,
9878 _("child DW_TAG_imported_declaration expected "
9879 "- DIE at 0x%x [in module %s]"),
9c541725 9880 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
9881 continue;
9882 }
9883
9884 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9885 if (import_attr == NULL)
9886 {
9887 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9888 dwarf_tag_name (child_die->tag));
9889 continue;
9890 }
9891
9892 imported_cu = cu;
9893 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9894 &imported_cu);
9895 imported_name = dwarf2_name (imported_die, imported_cu);
9896 if (imported_name == NULL)
9897 {
9898 complaint (&symfile_complaints,
9899 _("child DW_TAG_imported_declaration has unknown "
9900 "imported name - DIE at 0x%x [in module %s]"),
9c541725 9901 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
9902 continue;
9903 }
9904
eb1e02fd 9905 excludes.push_back (imported_name);
32019081
JK
9906
9907 process_die (child_die, cu);
9908 }
9909
22cee43f
PMR
9910 add_using_directive (using_directives (cu->language),
9911 import_prefix,
9912 canonical_name,
9913 import_alias,
9914 imported_declaration,
9915 excludes,
9916 0,
9917 &objfile->objfile_obstack);
27aa8d6a
SW
9918}
9919
5230b05a
WT
9920/* ICC<14 does not output the required DW_AT_declaration on incomplete
9921 types, but gives them a size of zero. Starting with version 14,
9922 ICC is compatible with GCC. */
9923
9924static int
9925producer_is_icc_lt_14 (struct dwarf2_cu *cu)
9926{
9927 if (!cu->checked_producer)
9928 check_producer (cu);
9929
9930 return cu->producer_is_icc_lt_14;
9931}
9932
1b80a9fa
JK
9933/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9934 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9935 this, it was first present in GCC release 4.3.0. */
9936
9937static int
9938producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9939{
9940 if (!cu->checked_producer)
9941 check_producer (cu);
9942
9943 return cu->producer_is_gcc_lt_4_3;
9944}
9945
d721ba37
PA
9946static file_and_directory
9947find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 9948{
d721ba37
PA
9949 file_and_directory res;
9950
9291a0cd
TT
9951 /* Find the filename. Do not use dwarf2_name here, since the filename
9952 is not a source language identifier. */
d721ba37
PA
9953 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
9954 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 9955
d721ba37
PA
9956 if (res.comp_dir == NULL
9957 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
9958 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 9959 {
d721ba37
PA
9960 res.comp_dir_storage = ldirname (res.name);
9961 if (!res.comp_dir_storage.empty ())
9962 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 9963 }
d721ba37 9964 if (res.comp_dir != NULL)
9291a0cd
TT
9965 {
9966 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9967 directory, get rid of it. */
d721ba37 9968 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 9969
d721ba37
PA
9970 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
9971 res.comp_dir = cp + 1;
9291a0cd
TT
9972 }
9973
d721ba37
PA
9974 if (res.name == NULL)
9975 res.name = "<unknown>";
9976
9977 return res;
9291a0cd
TT
9978}
9979
f4dc4d17
DE
9980/* Handle DW_AT_stmt_list for a compilation unit.
9981 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
9982 COMP_DIR is the compilation directory. LOWPC is passed to
9983 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
9984
9985static void
9986handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 9987 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 9988{
527f3840 9989 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 9990 struct attribute *attr;
527f3840
JK
9991 struct line_header line_header_local;
9992 hashval_t line_header_local_hash;
9993 unsigned u;
9994 void **slot;
9995 int decode_mapping;
2ab95328 9996
f4dc4d17
DE
9997 gdb_assert (! cu->per_cu->is_debug_types);
9998
2ab95328 9999 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10000 if (attr == NULL)
10001 return;
10002
9c541725 10003 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10004
10005 /* The line header hash table is only created if needed (it exists to
10006 prevent redundant reading of the line table for partial_units).
10007 If we're given a partial_unit, we'll need it. If we're given a
10008 compile_unit, then use the line header hash table if it's already
10009 created, but don't create one just yet. */
10010
10011 if (dwarf2_per_objfile->line_header_hash == NULL
10012 && die->tag == DW_TAG_partial_unit)
2ab95328 10013 {
527f3840
JK
10014 dwarf2_per_objfile->line_header_hash
10015 = htab_create_alloc_ex (127, line_header_hash_voidp,
10016 line_header_eq_voidp,
10017 free_line_header_voidp,
10018 &objfile->objfile_obstack,
10019 hashtab_obstack_allocate,
10020 dummy_obstack_deallocate);
10021 }
2ab95328 10022
9c541725 10023 line_header_local.sect_off = line_offset;
527f3840
JK
10024 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10025 line_header_local_hash = line_header_hash (&line_header_local);
10026 if (dwarf2_per_objfile->line_header_hash != NULL)
10027 {
10028 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
10029 &line_header_local,
10030 line_header_local_hash, NO_INSERT);
10031
10032 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10033 is not present in *SLOT (since if there is something in *SLOT then
10034 it will be for a partial_unit). */
10035 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10036 {
527f3840 10037 gdb_assert (*slot != NULL);
9a3c8263 10038 cu->line_header = (struct line_header *) *slot;
527f3840 10039 return;
dee91e82 10040 }
2ab95328 10041 }
527f3840
JK
10042
10043 /* dwarf_decode_line_header does not yet provide sufficient information.
10044 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10045 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10046 if (lh == NULL)
527f3840 10047 return;
4c8aa72d
PA
10048
10049 cu->line_header = lh.release ();
10050 cu->line_header_die_owner = die;
527f3840
JK
10051
10052 if (dwarf2_per_objfile->line_header_hash == NULL)
10053 slot = NULL;
10054 else
10055 {
10056 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
10057 &line_header_local,
10058 line_header_local_hash, INSERT);
10059 gdb_assert (slot != NULL);
10060 }
10061 if (slot != NULL && *slot == NULL)
10062 {
10063 /* This newly decoded line number information unit will be owned
10064 by line_header_hash hash table. */
10065 *slot = cu->line_header;
4c8aa72d 10066 cu->line_header_die_owner = NULL;
527f3840
JK
10067 }
10068 else
10069 {
10070 /* We cannot free any current entry in (*slot) as that struct line_header
10071 may be already used by multiple CUs. Create only temporary decoded
10072 line_header for this CU - it may happen at most once for each line
10073 number information unit. And if we're not using line_header_hash
10074 then this is what we want as well. */
10075 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10076 }
10077 decode_mapping = (die->tag != DW_TAG_partial_unit);
10078 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10079 decode_mapping);
fff8551c 10080
2ab95328
TT
10081}
10082
95554aad 10083/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10084
c906108c 10085static void
e7c27a73 10086read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10087{
dee91e82 10088 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10089 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 10090 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10091 CORE_ADDR highpc = ((CORE_ADDR) 0);
10092 struct attribute *attr;
c906108c 10093 struct die_info *child_die;
e142c38c 10094 CORE_ADDR baseaddr;
6e70227d 10095
e142c38c 10096 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10097
fae299cd 10098 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10099
10100 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10101 from finish_block. */
2acceee2 10102 if (lowpc == ((CORE_ADDR) -1))
c906108c 10103 lowpc = highpc;
3e29f34a 10104 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10105
d721ba37 10106 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10107
95554aad 10108 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 10109
f4b8a18d
KW
10110 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10111 standardised yet. As a workaround for the language detection we fall
10112 back to the DW_AT_producer string. */
10113 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10114 cu->language = language_opencl;
10115
3019eac3
DE
10116 /* Similar hack for Go. */
10117 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10118 set_cu_language (DW_LANG_Go, cu);
10119
d721ba37 10120 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10121
10122 /* Decode line number information if present. We do this before
10123 processing child DIEs, so that the line header table is available
10124 for DW_AT_decl_file. */
d721ba37 10125 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10126
10127 /* Process all dies in compilation unit. */
10128 if (die->child != NULL)
10129 {
10130 child_die = die->child;
10131 while (child_die && child_die->tag)
10132 {
10133 process_die (child_die, cu);
10134 child_die = sibling_die (child_die);
10135 }
10136 }
10137
10138 /* Decode macro information, if present. Dwarf 2 macro information
10139 refers to information in the line number info statement program
10140 header, so we can only read it if we've read the header
10141 successfully. */
0af92d60
JK
10142 attr = dwarf2_attr (die, DW_AT_macros, cu);
10143 if (attr == NULL)
10144 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10145 if (attr && cu->line_header)
10146 {
10147 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10148 complaint (&symfile_complaints,
0af92d60 10149 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10150
43f3e411 10151 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10152 }
10153 else
10154 {
10155 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10156 if (attr && cu->line_header)
10157 {
10158 unsigned int macro_offset = DW_UNSND (attr);
10159
43f3e411 10160 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10161 }
10162 }
3019eac3
DE
10163}
10164
f4dc4d17
DE
10165/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
10166 Create the set of symtabs used by this TU, or if this TU is sharing
10167 symtabs with another TU and the symtabs have already been created
10168 then restore those symtabs in the line header.
10169 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
10170
10171static void
f4dc4d17 10172setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 10173{
f4dc4d17
DE
10174 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
10175 struct type_unit_group *tu_group;
10176 int first_time;
3019eac3 10177 struct attribute *attr;
9c541725 10178 unsigned int i;
0186c6a7 10179 struct signatured_type *sig_type;
3019eac3 10180
f4dc4d17 10181 gdb_assert (per_cu->is_debug_types);
0186c6a7 10182 sig_type = (struct signatured_type *) per_cu;
3019eac3 10183
f4dc4d17 10184 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 10185
f4dc4d17 10186 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10187 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
10188 if (sig_type->type_unit_group == NULL)
10189 sig_type->type_unit_group = get_type_unit_group (cu, attr);
10190 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10191
10192 /* If we've already processed this stmt_list there's no real need to
10193 do it again, we could fake it and just recreate the part we need
10194 (file name,index -> symtab mapping). If data shows this optimization
10195 is useful we can do it then. */
43f3e411 10196 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
10197
10198 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10199 debug info. */
fff8551c 10200 line_header_up lh;
f4dc4d17 10201 if (attr != NULL)
3019eac3 10202 {
9c541725 10203 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
10204 lh = dwarf_decode_line_header (line_offset, cu);
10205 }
10206 if (lh == NULL)
10207 {
10208 if (first_time)
10209 dwarf2_start_symtab (cu, "", NULL, 0);
10210 else
10211 {
10212 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 10213 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 10214 }
f4dc4d17 10215 return;
3019eac3
DE
10216 }
10217
4c8aa72d
PA
10218 cu->line_header = lh.release ();
10219 cu->line_header_die_owner = die;
3019eac3 10220
f4dc4d17
DE
10221 if (first_time)
10222 {
43f3e411 10223 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 10224
1fd60fc0
DE
10225 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10226 still initializing it, and our caller (a few levels up)
10227 process_full_type_unit still needs to know if this is the first
10228 time. */
10229
4c8aa72d
PA
10230 tu_group->num_symtabs = cu->line_header->file_names.size ();
10231 tu_group->symtabs = XNEWVEC (struct symtab *,
10232 cu->line_header->file_names.size ());
3019eac3 10233
4c8aa72d 10234 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 10235 {
4c8aa72d 10236 file_entry &fe = cu->line_header->file_names[i];
3019eac3 10237
4c8aa72d 10238 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 10239
f4dc4d17
DE
10240 if (current_subfile->symtab == NULL)
10241 {
4c8aa72d
PA
10242 /* NOTE: start_subfile will recognize when it's been
10243 passed a file it has already seen. So we can't
10244 assume there's a simple mapping from
10245 cu->line_header->file_names to subfiles, plus
10246 cu->line_header->file_names may contain dups. */
43f3e411
DE
10247 current_subfile->symtab
10248 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
10249 }
10250
8c43009f
PA
10251 fe.symtab = current_subfile->symtab;
10252 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
10253 }
10254 }
10255 else
3019eac3 10256 {
0ab9ce85 10257 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 10258
4c8aa72d 10259 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 10260 {
4c8aa72d 10261 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 10262
4c8aa72d 10263 fe.symtab = tu_group->symtabs[i];
f4dc4d17 10264 }
3019eac3
DE
10265 }
10266
f4dc4d17
DE
10267 /* The main symtab is allocated last. Type units don't have DW_AT_name
10268 so they don't have a "real" (so to speak) symtab anyway.
10269 There is later code that will assign the main symtab to all symbols
10270 that don't have one. We need to handle the case of a symbol with a
10271 missing symtab (DW_AT_decl_file) anyway. */
10272}
3019eac3 10273
f4dc4d17
DE
10274/* Process DW_TAG_type_unit.
10275 For TUs we want to skip the first top level sibling if it's not the
10276 actual type being defined by this TU. In this case the first top
10277 level sibling is there to provide context only. */
3019eac3 10278
f4dc4d17
DE
10279static void
10280read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10281{
10282 struct die_info *child_die;
3019eac3 10283
f4dc4d17
DE
10284 prepare_one_comp_unit (cu, die, language_minimal);
10285
10286 /* Initialize (or reinitialize) the machinery for building symtabs.
10287 We do this before processing child DIEs, so that the line header table
10288 is available for DW_AT_decl_file. */
10289 setup_type_unit_groups (die, cu);
10290
10291 if (die->child != NULL)
10292 {
10293 child_die = die->child;
10294 while (child_die && child_die->tag)
10295 {
10296 process_die (child_die, cu);
10297 child_die = sibling_die (child_die);
10298 }
10299 }
3019eac3
DE
10300}
10301\f
80626a55
DE
10302/* DWO/DWP files.
10303
10304 http://gcc.gnu.org/wiki/DebugFission
10305 http://gcc.gnu.org/wiki/DebugFissionDWP
10306
10307 To simplify handling of both DWO files ("object" files with the DWARF info)
10308 and DWP files (a file with the DWOs packaged up into one file), we treat
10309 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10310
10311static hashval_t
10312hash_dwo_file (const void *item)
10313{
9a3c8263 10314 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10315 hashval_t hash;
3019eac3 10316
a2ce51a0
DE
10317 hash = htab_hash_string (dwo_file->dwo_name);
10318 if (dwo_file->comp_dir != NULL)
10319 hash += htab_hash_string (dwo_file->comp_dir);
10320 return hash;
3019eac3
DE
10321}
10322
10323static int
10324eq_dwo_file (const void *item_lhs, const void *item_rhs)
10325{
9a3c8263
SM
10326 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10327 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 10328
a2ce51a0
DE
10329 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10330 return 0;
10331 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10332 return lhs->comp_dir == rhs->comp_dir;
10333 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
10334}
10335
10336/* Allocate a hash table for DWO files. */
10337
10338static htab_t
10339allocate_dwo_file_hash_table (void)
10340{
10341 struct objfile *objfile = dwarf2_per_objfile->objfile;
10342
10343 return htab_create_alloc_ex (41,
10344 hash_dwo_file,
10345 eq_dwo_file,
10346 NULL,
10347 &objfile->objfile_obstack,
10348 hashtab_obstack_allocate,
10349 dummy_obstack_deallocate);
10350}
10351
80626a55
DE
10352/* Lookup DWO file DWO_NAME. */
10353
10354static void **
0ac5b59e 10355lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
10356{
10357 struct dwo_file find_entry;
10358 void **slot;
10359
10360 if (dwarf2_per_objfile->dwo_files == NULL)
10361 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
10362
10363 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
10364 find_entry.dwo_name = dwo_name;
10365 find_entry.comp_dir = comp_dir;
80626a55
DE
10366 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
10367
10368 return slot;
10369}
10370
3019eac3
DE
10371static hashval_t
10372hash_dwo_unit (const void *item)
10373{
9a3c8263 10374 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
10375
10376 /* This drops the top 32 bits of the id, but is ok for a hash. */
10377 return dwo_unit->signature;
10378}
10379
10380static int
10381eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10382{
9a3c8263
SM
10383 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10384 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
10385
10386 /* The signature is assumed to be unique within the DWO file.
10387 So while object file CU dwo_id's always have the value zero,
10388 that's OK, assuming each object file DWO file has only one CU,
10389 and that's the rule for now. */
10390 return lhs->signature == rhs->signature;
10391}
10392
10393/* Allocate a hash table for DWO CUs,TUs.
10394 There is one of these tables for each of CUs,TUs for each DWO file. */
10395
10396static htab_t
10397allocate_dwo_unit_table (struct objfile *objfile)
10398{
10399 /* Start out with a pretty small number.
10400 Generally DWO files contain only one CU and maybe some TUs. */
10401 return htab_create_alloc_ex (3,
10402 hash_dwo_unit,
10403 eq_dwo_unit,
10404 NULL,
10405 &objfile->objfile_obstack,
10406 hashtab_obstack_allocate,
10407 dummy_obstack_deallocate);
10408}
10409
80626a55 10410/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 10411
19c3d4c9 10412struct create_dwo_cu_data
3019eac3
DE
10413{
10414 struct dwo_file *dwo_file;
19c3d4c9 10415 struct dwo_unit dwo_unit;
3019eac3
DE
10416};
10417
19c3d4c9 10418/* die_reader_func for create_dwo_cu. */
3019eac3
DE
10419
10420static void
19c3d4c9
DE
10421create_dwo_cu_reader (const struct die_reader_specs *reader,
10422 const gdb_byte *info_ptr,
10423 struct die_info *comp_unit_die,
10424 int has_children,
10425 void *datap)
3019eac3
DE
10426{
10427 struct dwarf2_cu *cu = reader->cu;
9c541725 10428 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 10429 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 10430 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 10431 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 10432 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 10433 struct attribute *attr;
3019eac3
DE
10434
10435 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
10436 if (attr == NULL)
10437 {
19c3d4c9
DE
10438 complaint (&symfile_complaints,
10439 _("Dwarf Error: debug entry at offset 0x%x is missing"
10440 " its dwo_id [in module %s]"),
9c541725 10441 to_underlying (sect_off), dwo_file->dwo_name);
3019eac3
DE
10442 return;
10443 }
10444
3019eac3
DE
10445 dwo_unit->dwo_file = dwo_file;
10446 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 10447 dwo_unit->section = section;
9c541725 10448 dwo_unit->sect_off = sect_off;
3019eac3
DE
10449 dwo_unit->length = cu->per_cu->length;
10450
b4f54984 10451 if (dwarf_read_debug)
4031ecc5 10452 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9c541725
PA
10453 to_underlying (sect_off),
10454 hex_string (dwo_unit->signature));
3019eac3
DE
10455}
10456
33c5cd75 10457/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 10458 Note: This function processes DWO files only, not DWP files. */
3019eac3 10459
33c5cd75
DB
10460static void
10461create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
10462 htab_t &cus_htab)
3019eac3
DE
10463{
10464 struct objfile *objfile = dwarf2_per_objfile->objfile;
33c5cd75 10465 const struct dwarf2_section_info *abbrev_section = &dwo_file.sections.abbrev;
d521ce57 10466 const gdb_byte *info_ptr, *end_ptr;
3019eac3 10467
33c5cd75
DB
10468 dwarf2_read_section (objfile, &section);
10469 info_ptr = section.buffer;
3019eac3
DE
10470
10471 if (info_ptr == NULL)
33c5cd75 10472 return;
3019eac3 10473
b4f54984 10474 if (dwarf_read_debug)
19c3d4c9
DE
10475 {
10476 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
10477 get_section_name (&section),
10478 get_section_file_name (&section));
19c3d4c9 10479 }
3019eac3 10480
33c5cd75 10481 end_ptr = info_ptr + section.size;
3019eac3
DE
10482 while (info_ptr < end_ptr)
10483 {
10484 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
10485 struct create_dwo_cu_data create_dwo_cu_data;
10486 struct dwo_unit *dwo_unit;
10487 void **slot;
10488 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 10489
19c3d4c9
DE
10490 memset (&create_dwo_cu_data.dwo_unit, 0,
10491 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
10492 memset (&per_cu, 0, sizeof (per_cu));
10493 per_cu.objfile = objfile;
10494 per_cu.is_debug_types = 0;
33c5cd75
DB
10495 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10496 per_cu.section = &section;
c5ed0576 10497 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
10498
10499 init_cutu_and_read_dies_no_follow (
10500 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
10501 info_ptr += per_cu.length;
10502
10503 // If the unit could not be parsed, skip it.
10504 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
10505 continue;
3019eac3 10506
33c5cd75
DB
10507 if (cus_htab == NULL)
10508 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 10509
33c5cd75
DB
10510 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10511 *dwo_unit = create_dwo_cu_data.dwo_unit;
10512 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
10513 gdb_assert (slot != NULL);
10514 if (*slot != NULL)
19c3d4c9 10515 {
33c5cd75
DB
10516 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10517 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 10518
33c5cd75
DB
10519 complaint (&symfile_complaints,
10520 _("debug cu entry at offset 0x%x is duplicate to"
10521 " the entry at offset 0x%x, signature %s"),
10522 to_underlying (sect_off), to_underlying (dup_sect_off),
10523 hex_string (dwo_unit->signature));
19c3d4c9 10524 }
33c5cd75 10525 *slot = (void *)dwo_unit;
3019eac3 10526 }
3019eac3
DE
10527}
10528
80626a55
DE
10529/* DWP file .debug_{cu,tu}_index section format:
10530 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
10531
d2415c6c
DE
10532 DWP Version 1:
10533
80626a55
DE
10534 Both index sections have the same format, and serve to map a 64-bit
10535 signature to a set of section numbers. Each section begins with a header,
10536 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10537 indexes, and a pool of 32-bit section numbers. The index sections will be
10538 aligned at 8-byte boundaries in the file.
10539
d2415c6c
DE
10540 The index section header consists of:
10541
10542 V, 32 bit version number
10543 -, 32 bits unused
10544 N, 32 bit number of compilation units or type units in the index
10545 M, 32 bit number of slots in the hash table
80626a55 10546
d2415c6c 10547 Numbers are recorded using the byte order of the application binary.
80626a55 10548
d2415c6c
DE
10549 The hash table begins at offset 16 in the section, and consists of an array
10550 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10551 order of the application binary). Unused slots in the hash table are 0.
10552 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 10553
d2415c6c
DE
10554 The parallel table begins immediately after the hash table
10555 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10556 array of 32-bit indexes (using the byte order of the application binary),
10557 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10558 table contains a 32-bit index into the pool of section numbers. For unused
10559 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 10560
73869dc2
DE
10561 The pool of section numbers begins immediately following the hash table
10562 (at offset 16 + 12 * M from the beginning of the section). The pool of
10563 section numbers consists of an array of 32-bit words (using the byte order
10564 of the application binary). Each item in the array is indexed starting
10565 from 0. The hash table entry provides the index of the first section
10566 number in the set. Additional section numbers in the set follow, and the
10567 set is terminated by a 0 entry (section number 0 is not used in ELF).
10568
10569 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10570 section must be the first entry in the set, and the .debug_abbrev.dwo must
10571 be the second entry. Other members of the set may follow in any order.
10572
10573 ---
10574
10575 DWP Version 2:
10576
10577 DWP Version 2 combines all the .debug_info, etc. sections into one,
10578 and the entries in the index tables are now offsets into these sections.
10579 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10580 section.
10581
10582 Index Section Contents:
10583 Header
10584 Hash Table of Signatures dwp_hash_table.hash_table
10585 Parallel Table of Indices dwp_hash_table.unit_table
10586 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
10587 Table of Section Sizes dwp_hash_table.v2.sizes
10588
10589 The index section header consists of:
10590
10591 V, 32 bit version number
10592 L, 32 bit number of columns in the table of section offsets
10593 N, 32 bit number of compilation units or type units in the index
10594 M, 32 bit number of slots in the hash table
10595
10596 Numbers are recorded using the byte order of the application binary.
10597
10598 The hash table has the same format as version 1.
10599 The parallel table of indices has the same format as version 1,
10600 except that the entries are origin-1 indices into the table of sections
10601 offsets and the table of section sizes.
10602
10603 The table of offsets begins immediately following the parallel table
10604 (at offset 16 + 12 * M from the beginning of the section). The table is
10605 a two-dimensional array of 32-bit words (using the byte order of the
10606 application binary), with L columns and N+1 rows, in row-major order.
10607 Each row in the array is indexed starting from 0. The first row provides
10608 a key to the remaining rows: each column in this row provides an identifier
10609 for a debug section, and the offsets in the same column of subsequent rows
10610 refer to that section. The section identifiers are:
10611
10612 DW_SECT_INFO 1 .debug_info.dwo
10613 DW_SECT_TYPES 2 .debug_types.dwo
10614 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10615 DW_SECT_LINE 4 .debug_line.dwo
10616 DW_SECT_LOC 5 .debug_loc.dwo
10617 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10618 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10619 DW_SECT_MACRO 8 .debug_macro.dwo
10620
10621 The offsets provided by the CU and TU index sections are the base offsets
10622 for the contributions made by each CU or TU to the corresponding section
10623 in the package file. Each CU and TU header contains an abbrev_offset
10624 field, used to find the abbreviations table for that CU or TU within the
10625 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10626 be interpreted as relative to the base offset given in the index section.
10627 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10628 should be interpreted as relative to the base offset for .debug_line.dwo,
10629 and offsets into other debug sections obtained from DWARF attributes should
10630 also be interpreted as relative to the corresponding base offset.
10631
10632 The table of sizes begins immediately following the table of offsets.
10633 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10634 with L columns and N rows, in row-major order. Each row in the array is
10635 indexed starting from 1 (row 0 is shared by the two tables).
10636
10637 ---
10638
10639 Hash table lookup is handled the same in version 1 and 2:
10640
10641 We assume that N and M will not exceed 2^32 - 1.
10642 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10643
d2415c6c
DE
10644 Given a 64-bit compilation unit signature or a type signature S, an entry
10645 in the hash table is located as follows:
80626a55 10646
d2415c6c
DE
10647 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10648 the low-order k bits all set to 1.
80626a55 10649
d2415c6c 10650 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 10651
d2415c6c
DE
10652 3) If the hash table entry at index H matches the signature, use that
10653 entry. If the hash table entry at index H is unused (all zeroes),
10654 terminate the search: the signature is not present in the table.
80626a55 10655
d2415c6c 10656 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 10657
d2415c6c 10658 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 10659 to stop at an unused slot or find the match. */
80626a55
DE
10660
10661/* Create a hash table to map DWO IDs to their CU/TU entry in
10662 .debug_{info,types}.dwo in DWP_FILE.
10663 Returns NULL if there isn't one.
10664 Note: This function processes DWP files only, not DWO files. */
10665
10666static struct dwp_hash_table *
10667create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
10668{
10669 struct objfile *objfile = dwarf2_per_objfile->objfile;
10670 bfd *dbfd = dwp_file->dbfd;
948f8e3d 10671 const gdb_byte *index_ptr, *index_end;
80626a55 10672 struct dwarf2_section_info *index;
73869dc2 10673 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
10674 struct dwp_hash_table *htab;
10675
10676 if (is_debug_types)
10677 index = &dwp_file->sections.tu_index;
10678 else
10679 index = &dwp_file->sections.cu_index;
10680
10681 if (dwarf2_section_empty_p (index))
10682 return NULL;
10683 dwarf2_read_section (objfile, index);
10684
10685 index_ptr = index->buffer;
10686 index_end = index_ptr + index->size;
10687
10688 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
10689 index_ptr += 4;
10690 if (version == 2)
10691 nr_columns = read_4_bytes (dbfd, index_ptr);
10692 else
10693 nr_columns = 0;
10694 index_ptr += 4;
80626a55
DE
10695 nr_units = read_4_bytes (dbfd, index_ptr);
10696 index_ptr += 4;
10697 nr_slots = read_4_bytes (dbfd, index_ptr);
10698 index_ptr += 4;
10699
73869dc2 10700 if (version != 1 && version != 2)
80626a55 10701 {
21aa081e 10702 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 10703 " [in module %s]"),
21aa081e 10704 pulongest (version), dwp_file->name);
80626a55
DE
10705 }
10706 if (nr_slots != (nr_slots & -nr_slots))
10707 {
21aa081e 10708 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 10709 " is not power of 2 [in module %s]"),
21aa081e 10710 pulongest (nr_slots), dwp_file->name);
80626a55
DE
10711 }
10712
10713 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
10714 htab->version = version;
10715 htab->nr_columns = nr_columns;
80626a55
DE
10716 htab->nr_units = nr_units;
10717 htab->nr_slots = nr_slots;
10718 htab->hash_table = index_ptr;
10719 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
10720
10721 /* Exit early if the table is empty. */
10722 if (nr_slots == 0 || nr_units == 0
10723 || (version == 2 && nr_columns == 0))
10724 {
10725 /* All must be zero. */
10726 if (nr_slots != 0 || nr_units != 0
10727 || (version == 2 && nr_columns != 0))
10728 {
10729 complaint (&symfile_complaints,
10730 _("Empty DWP but nr_slots,nr_units,nr_columns not"
10731 " all zero [in modules %s]"),
10732 dwp_file->name);
10733 }
10734 return htab;
10735 }
10736
10737 if (version == 1)
10738 {
10739 htab->section_pool.v1.indices =
10740 htab->unit_table + sizeof (uint32_t) * nr_slots;
10741 /* It's harder to decide whether the section is too small in v1.
10742 V1 is deprecated anyway so we punt. */
10743 }
10744 else
10745 {
10746 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10747 int *ids = htab->section_pool.v2.section_ids;
10748 /* Reverse map for error checking. */
10749 int ids_seen[DW_SECT_MAX + 1];
10750 int i;
10751
10752 if (nr_columns < 2)
10753 {
10754 error (_("Dwarf Error: bad DWP hash table, too few columns"
10755 " in section table [in module %s]"),
10756 dwp_file->name);
10757 }
10758 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10759 {
10760 error (_("Dwarf Error: bad DWP hash table, too many columns"
10761 " in section table [in module %s]"),
10762 dwp_file->name);
10763 }
10764 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10765 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10766 for (i = 0; i < nr_columns; ++i)
10767 {
10768 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10769
10770 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10771 {
10772 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10773 " in section table [in module %s]"),
10774 id, dwp_file->name);
10775 }
10776 if (ids_seen[id] != -1)
10777 {
10778 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10779 " id %d in section table [in module %s]"),
10780 id, dwp_file->name);
10781 }
10782 ids_seen[id] = i;
10783 ids[i] = id;
10784 }
10785 /* Must have exactly one info or types section. */
10786 if (((ids_seen[DW_SECT_INFO] != -1)
10787 + (ids_seen[DW_SECT_TYPES] != -1))
10788 != 1)
10789 {
10790 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10791 " DWO info/types section [in module %s]"),
10792 dwp_file->name);
10793 }
10794 /* Must have an abbrev section. */
10795 if (ids_seen[DW_SECT_ABBREV] == -1)
10796 {
10797 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10798 " section [in module %s]"),
10799 dwp_file->name);
10800 }
10801 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10802 htab->section_pool.v2.sizes =
10803 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10804 * nr_units * nr_columns);
10805 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10806 * nr_units * nr_columns))
10807 > index_end)
10808 {
10809 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10810 " [in module %s]"),
10811 dwp_file->name);
10812 }
10813 }
80626a55
DE
10814
10815 return htab;
10816}
10817
10818/* Update SECTIONS with the data from SECTP.
10819
10820 This function is like the other "locate" section routines that are
10821 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 10822 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
10823
10824 The result is non-zero for success, or zero if an error was found. */
10825
10826static int
73869dc2
DE
10827locate_v1_virtual_dwo_sections (asection *sectp,
10828 struct virtual_v1_dwo_sections *sections)
80626a55
DE
10829{
10830 const struct dwop_section_names *names = &dwop_section_names;
10831
10832 if (section_is_p (sectp->name, &names->abbrev_dwo))
10833 {
10834 /* There can be only one. */
049412e3 10835 if (sections->abbrev.s.section != NULL)
80626a55 10836 return 0;
049412e3 10837 sections->abbrev.s.section = sectp;
80626a55
DE
10838 sections->abbrev.size = bfd_get_section_size (sectp);
10839 }
10840 else if (section_is_p (sectp->name, &names->info_dwo)
10841 || section_is_p (sectp->name, &names->types_dwo))
10842 {
10843 /* There can be only one. */
049412e3 10844 if (sections->info_or_types.s.section != NULL)
80626a55 10845 return 0;
049412e3 10846 sections->info_or_types.s.section = sectp;
80626a55
DE
10847 sections->info_or_types.size = bfd_get_section_size (sectp);
10848 }
10849 else if (section_is_p (sectp->name, &names->line_dwo))
10850 {
10851 /* There can be only one. */
049412e3 10852 if (sections->line.s.section != NULL)
80626a55 10853 return 0;
049412e3 10854 sections->line.s.section = sectp;
80626a55
DE
10855 sections->line.size = bfd_get_section_size (sectp);
10856 }
10857 else if (section_is_p (sectp->name, &names->loc_dwo))
10858 {
10859 /* There can be only one. */
049412e3 10860 if (sections->loc.s.section != NULL)
80626a55 10861 return 0;
049412e3 10862 sections->loc.s.section = sectp;
80626a55
DE
10863 sections->loc.size = bfd_get_section_size (sectp);
10864 }
10865 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10866 {
10867 /* There can be only one. */
049412e3 10868 if (sections->macinfo.s.section != NULL)
80626a55 10869 return 0;
049412e3 10870 sections->macinfo.s.section = sectp;
80626a55
DE
10871 sections->macinfo.size = bfd_get_section_size (sectp);
10872 }
10873 else if (section_is_p (sectp->name, &names->macro_dwo))
10874 {
10875 /* There can be only one. */
049412e3 10876 if (sections->macro.s.section != NULL)
80626a55 10877 return 0;
049412e3 10878 sections->macro.s.section = sectp;
80626a55
DE
10879 sections->macro.size = bfd_get_section_size (sectp);
10880 }
10881 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10882 {
10883 /* There can be only one. */
049412e3 10884 if (sections->str_offsets.s.section != NULL)
80626a55 10885 return 0;
049412e3 10886 sections->str_offsets.s.section = sectp;
80626a55
DE
10887 sections->str_offsets.size = bfd_get_section_size (sectp);
10888 }
10889 else
10890 {
10891 /* No other kind of section is valid. */
10892 return 0;
10893 }
10894
10895 return 1;
10896}
10897
73869dc2
DE
10898/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10899 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10900 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10901 This is for DWP version 1 files. */
80626a55
DE
10902
10903static struct dwo_unit *
73869dc2
DE
10904create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10905 uint32_t unit_index,
10906 const char *comp_dir,
10907 ULONGEST signature, int is_debug_types)
80626a55
DE
10908{
10909 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
10910 const struct dwp_hash_table *dwp_htab =
10911 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
10912 bfd *dbfd = dwp_file->dbfd;
10913 const char *kind = is_debug_types ? "TU" : "CU";
10914 struct dwo_file *dwo_file;
10915 struct dwo_unit *dwo_unit;
73869dc2 10916 struct virtual_v1_dwo_sections sections;
80626a55 10917 void **dwo_file_slot;
80626a55
DE
10918 int i;
10919
73869dc2
DE
10920 gdb_assert (dwp_file->version == 1);
10921
b4f54984 10922 if (dwarf_read_debug)
80626a55 10923 {
73869dc2 10924 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 10925 kind,
73869dc2 10926 pulongest (unit_index), hex_string (signature),
80626a55
DE
10927 dwp_file->name);
10928 }
10929
19ac8c2e 10930 /* Fetch the sections of this DWO unit.
80626a55
DE
10931 Put a limit on the number of sections we look for so that bad data
10932 doesn't cause us to loop forever. */
10933
73869dc2 10934#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
10935 (1 /* .debug_info or .debug_types */ \
10936 + 1 /* .debug_abbrev */ \
10937 + 1 /* .debug_line */ \
10938 + 1 /* .debug_loc */ \
10939 + 1 /* .debug_str_offsets */ \
19ac8c2e 10940 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
10941 + 1 /* trailing zero */)
10942
10943 memset (&sections, 0, sizeof (sections));
80626a55 10944
73869dc2 10945 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
10946 {
10947 asection *sectp;
10948 uint32_t section_nr =
10949 read_4_bytes (dbfd,
73869dc2
DE
10950 dwp_htab->section_pool.v1.indices
10951 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
10952
10953 if (section_nr == 0)
10954 break;
10955 if (section_nr >= dwp_file->num_sections)
10956 {
10957 error (_("Dwarf Error: bad DWP hash table, section number too large"
10958 " [in module %s]"),
10959 dwp_file->name);
10960 }
10961
10962 sectp = dwp_file->elf_sections[section_nr];
73869dc2 10963 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
10964 {
10965 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10966 " [in module %s]"),
10967 dwp_file->name);
10968 }
10969 }
10970
10971 if (i < 2
a32a8923
DE
10972 || dwarf2_section_empty_p (&sections.info_or_types)
10973 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
10974 {
10975 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10976 " [in module %s]"),
10977 dwp_file->name);
10978 }
73869dc2 10979 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
10980 {
10981 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10982 " [in module %s]"),
10983 dwp_file->name);
10984 }
10985
10986 /* It's easier for the rest of the code if we fake a struct dwo_file and
10987 have dwo_unit "live" in that. At least for now.
10988
10989 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 10990 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
10991 file, we can combine them back into a virtual DWO file to save space
10992 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
10993 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10994
791afaa2
TT
10995 std::string virtual_dwo_name =
10996 string_printf ("virtual-dwo/%d-%d-%d-%d",
10997 get_section_id (&sections.abbrev),
10998 get_section_id (&sections.line),
10999 get_section_id (&sections.loc),
11000 get_section_id (&sections.str_offsets));
80626a55 11001 /* Can we use an existing virtual DWO file? */
791afaa2 11002 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
80626a55
DE
11003 /* Create one if necessary. */
11004 if (*dwo_file_slot == NULL)
11005 {
b4f54984 11006 if (dwarf_read_debug)
80626a55
DE
11007 {
11008 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11009 virtual_dwo_name.c_str ());
80626a55
DE
11010 }
11011 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
11012 dwo_file->dwo_name
11013 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
11014 virtual_dwo_name.c_str (),
11015 virtual_dwo_name.size ());
0ac5b59e 11016 dwo_file->comp_dir = comp_dir;
80626a55
DE
11017 dwo_file->sections.abbrev = sections.abbrev;
11018 dwo_file->sections.line = sections.line;
11019 dwo_file->sections.loc = sections.loc;
11020 dwo_file->sections.macinfo = sections.macinfo;
11021 dwo_file->sections.macro = sections.macro;
11022 dwo_file->sections.str_offsets = sections.str_offsets;
11023 /* The "str" section is global to the entire DWP file. */
11024 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11025 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11026 there's no need to record it in dwo_file.
11027 Also, we can't simply record type sections in dwo_file because
11028 we record a pointer into the vector in dwo_unit. As we collect more
11029 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11030 for it, invalidating all copies of pointers into the previous
11031 contents. */
80626a55
DE
11032 *dwo_file_slot = dwo_file;
11033 }
11034 else
11035 {
b4f54984 11036 if (dwarf_read_debug)
80626a55
DE
11037 {
11038 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11039 virtual_dwo_name.c_str ());
80626a55 11040 }
9a3c8263 11041 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11042 }
80626a55
DE
11043
11044 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11045 dwo_unit->dwo_file = dwo_file;
11046 dwo_unit->signature = signature;
8d749320
SM
11047 dwo_unit->section =
11048 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 11049 *dwo_unit->section = sections.info_or_types;
57d63ce2 11050 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11051
11052 return dwo_unit;
11053}
11054
73869dc2
DE
11055/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11056 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11057 piece within that section used by a TU/CU, return a virtual section
11058 of just that piece. */
11059
11060static struct dwarf2_section_info
11061create_dwp_v2_section (struct dwarf2_section_info *section,
11062 bfd_size_type offset, bfd_size_type size)
11063{
11064 struct dwarf2_section_info result;
11065 asection *sectp;
11066
11067 gdb_assert (section != NULL);
11068 gdb_assert (!section->is_virtual);
11069
11070 memset (&result, 0, sizeof (result));
11071 result.s.containing_section = section;
11072 result.is_virtual = 1;
11073
11074 if (size == 0)
11075 return result;
11076
11077 sectp = get_section_bfd_section (section);
11078
11079 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11080 bounds of the real section. This is a pretty-rare event, so just
11081 flag an error (easier) instead of a warning and trying to cope. */
11082 if (sectp == NULL
11083 || offset + size > bfd_get_section_size (sectp))
11084 {
11085 bfd *abfd = sectp->owner;
11086
11087 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11088 " in section %s [in module %s]"),
11089 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
11090 objfile_name (dwarf2_per_objfile->objfile));
11091 }
11092
11093 result.virtual_offset = offset;
11094 result.size = size;
11095 return result;
11096}
11097
11098/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11099 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11100 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11101 This is for DWP version 2 files. */
11102
11103static struct dwo_unit *
11104create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
11105 uint32_t unit_index,
11106 const char *comp_dir,
11107 ULONGEST signature, int is_debug_types)
11108{
11109 struct objfile *objfile = dwarf2_per_objfile->objfile;
11110 const struct dwp_hash_table *dwp_htab =
11111 is_debug_types ? dwp_file->tus : dwp_file->cus;
11112 bfd *dbfd = dwp_file->dbfd;
11113 const char *kind = is_debug_types ? "TU" : "CU";
11114 struct dwo_file *dwo_file;
11115 struct dwo_unit *dwo_unit;
11116 struct virtual_v2_dwo_sections sections;
11117 void **dwo_file_slot;
73869dc2
DE
11118 int i;
11119
11120 gdb_assert (dwp_file->version == 2);
11121
b4f54984 11122 if (dwarf_read_debug)
73869dc2
DE
11123 {
11124 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11125 kind,
11126 pulongest (unit_index), hex_string (signature),
11127 dwp_file->name);
11128 }
11129
11130 /* Fetch the section offsets of this DWO unit. */
11131
11132 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11133
11134 for (i = 0; i < dwp_htab->nr_columns; ++i)
11135 {
11136 uint32_t offset = read_4_bytes (dbfd,
11137 dwp_htab->section_pool.v2.offsets
11138 + (((unit_index - 1) * dwp_htab->nr_columns
11139 + i)
11140 * sizeof (uint32_t)));
11141 uint32_t size = read_4_bytes (dbfd,
11142 dwp_htab->section_pool.v2.sizes
11143 + (((unit_index - 1) * dwp_htab->nr_columns
11144 + i)
11145 * sizeof (uint32_t)));
11146
11147 switch (dwp_htab->section_pool.v2.section_ids[i])
11148 {
11149 case DW_SECT_INFO:
11150 case DW_SECT_TYPES:
11151 sections.info_or_types_offset = offset;
11152 sections.info_or_types_size = size;
11153 break;
11154 case DW_SECT_ABBREV:
11155 sections.abbrev_offset = offset;
11156 sections.abbrev_size = size;
11157 break;
11158 case DW_SECT_LINE:
11159 sections.line_offset = offset;
11160 sections.line_size = size;
11161 break;
11162 case DW_SECT_LOC:
11163 sections.loc_offset = offset;
11164 sections.loc_size = size;
11165 break;
11166 case DW_SECT_STR_OFFSETS:
11167 sections.str_offsets_offset = offset;
11168 sections.str_offsets_size = size;
11169 break;
11170 case DW_SECT_MACINFO:
11171 sections.macinfo_offset = offset;
11172 sections.macinfo_size = size;
11173 break;
11174 case DW_SECT_MACRO:
11175 sections.macro_offset = offset;
11176 sections.macro_size = size;
11177 break;
11178 }
11179 }
11180
11181 /* It's easier for the rest of the code if we fake a struct dwo_file and
11182 have dwo_unit "live" in that. At least for now.
11183
11184 The DWP file can be made up of a random collection of CUs and TUs.
11185 However, for each CU + set of TUs that came from the same original DWO
11186 file, we can combine them back into a virtual DWO file to save space
11187 (fewer struct dwo_file objects to allocate). Remember that for really
11188 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11189
791afaa2
TT
11190 std::string virtual_dwo_name =
11191 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11192 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11193 (long) (sections.line_size ? sections.line_offset : 0),
11194 (long) (sections.loc_size ? sections.loc_offset : 0),
11195 (long) (sections.str_offsets_size
11196 ? sections.str_offsets_offset : 0));
73869dc2 11197 /* Can we use an existing virtual DWO file? */
791afaa2 11198 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
73869dc2
DE
11199 /* Create one if necessary. */
11200 if (*dwo_file_slot == NULL)
11201 {
b4f54984 11202 if (dwarf_read_debug)
73869dc2
DE
11203 {
11204 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11205 virtual_dwo_name.c_str ());
73869dc2
DE
11206 }
11207 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
11208 dwo_file->dwo_name
11209 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
11210 virtual_dwo_name.c_str (),
11211 virtual_dwo_name.size ());
73869dc2
DE
11212 dwo_file->comp_dir = comp_dir;
11213 dwo_file->sections.abbrev =
11214 create_dwp_v2_section (&dwp_file->sections.abbrev,
11215 sections.abbrev_offset, sections.abbrev_size);
11216 dwo_file->sections.line =
11217 create_dwp_v2_section (&dwp_file->sections.line,
11218 sections.line_offset, sections.line_size);
11219 dwo_file->sections.loc =
11220 create_dwp_v2_section (&dwp_file->sections.loc,
11221 sections.loc_offset, sections.loc_size);
11222 dwo_file->sections.macinfo =
11223 create_dwp_v2_section (&dwp_file->sections.macinfo,
11224 sections.macinfo_offset, sections.macinfo_size);
11225 dwo_file->sections.macro =
11226 create_dwp_v2_section (&dwp_file->sections.macro,
11227 sections.macro_offset, sections.macro_size);
11228 dwo_file->sections.str_offsets =
11229 create_dwp_v2_section (&dwp_file->sections.str_offsets,
11230 sections.str_offsets_offset,
11231 sections.str_offsets_size);
11232 /* The "str" section is global to the entire DWP file. */
11233 dwo_file->sections.str = dwp_file->sections.str;
11234 /* The info or types section is assigned below to dwo_unit,
11235 there's no need to record it in dwo_file.
11236 Also, we can't simply record type sections in dwo_file because
11237 we record a pointer into the vector in dwo_unit. As we collect more
11238 types we'll grow the vector and eventually have to reallocate space
11239 for it, invalidating all copies of pointers into the previous
11240 contents. */
11241 *dwo_file_slot = dwo_file;
11242 }
11243 else
11244 {
b4f54984 11245 if (dwarf_read_debug)
73869dc2
DE
11246 {
11247 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11248 virtual_dwo_name.c_str ());
73869dc2 11249 }
9a3c8263 11250 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11251 }
73869dc2
DE
11252
11253 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11254 dwo_unit->dwo_file = dwo_file;
11255 dwo_unit->signature = signature;
8d749320
SM
11256 dwo_unit->section =
11257 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
73869dc2
DE
11258 *dwo_unit->section = create_dwp_v2_section (is_debug_types
11259 ? &dwp_file->sections.types
11260 : &dwp_file->sections.info,
11261 sections.info_or_types_offset,
11262 sections.info_or_types_size);
11263 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11264
11265 return dwo_unit;
11266}
11267
57d63ce2
DE
11268/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11269 Returns NULL if the signature isn't found. */
80626a55
DE
11270
11271static struct dwo_unit *
57d63ce2
DE
11272lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
11273 ULONGEST signature, int is_debug_types)
80626a55 11274{
57d63ce2
DE
11275 const struct dwp_hash_table *dwp_htab =
11276 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 11277 bfd *dbfd = dwp_file->dbfd;
57d63ce2 11278 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11279 uint32_t hash = signature & mask;
11280 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11281 unsigned int i;
11282 void **slot;
870f88f7 11283 struct dwo_unit find_dwo_cu;
80626a55
DE
11284
11285 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11286 find_dwo_cu.signature = signature;
19ac8c2e
DE
11287 slot = htab_find_slot (is_debug_types
11288 ? dwp_file->loaded_tus
11289 : dwp_file->loaded_cus,
11290 &find_dwo_cu, INSERT);
80626a55
DE
11291
11292 if (*slot != NULL)
9a3c8263 11293 return (struct dwo_unit *) *slot;
80626a55
DE
11294
11295 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11296 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11297 {
11298 ULONGEST signature_in_table;
11299
11300 signature_in_table =
57d63ce2 11301 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
11302 if (signature_in_table == signature)
11303 {
57d63ce2
DE
11304 uint32_t unit_index =
11305 read_4_bytes (dbfd,
11306 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 11307
73869dc2
DE
11308 if (dwp_file->version == 1)
11309 {
11310 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
11311 comp_dir, signature,
11312 is_debug_types);
11313 }
11314 else
11315 {
11316 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
11317 comp_dir, signature,
11318 is_debug_types);
11319 }
9a3c8263 11320 return (struct dwo_unit *) *slot;
80626a55
DE
11321 }
11322 if (signature_in_table == 0)
11323 return NULL;
11324 hash = (hash + hash2) & mask;
11325 }
11326
11327 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11328 " [in module %s]"),
11329 dwp_file->name);
11330}
11331
ab5088bf 11332/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
11333 Open the file specified by FILE_NAME and hand it off to BFD for
11334 preliminary analysis. Return a newly initialized bfd *, which
11335 includes a canonicalized copy of FILE_NAME.
80626a55 11336 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
11337 SEARCH_CWD is true if the current directory is to be searched.
11338 It will be searched before debug-file-directory.
13aaf454
DE
11339 If successful, the file is added to the bfd include table of the
11340 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 11341 If unable to find/open the file, return NULL.
3019eac3
DE
11342 NOTE: This function is derived from symfile_bfd_open. */
11343
192b62ce 11344static gdb_bfd_ref_ptr
6ac97d4c 11345try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3 11346{
80626a55 11347 int desc, flags;
3019eac3 11348 char *absolute_name;
9c02c129
DE
11349 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11350 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11351 to debug_file_directory. */
11352 char *search_path;
11353 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11354
6ac97d4c
DE
11355 if (search_cwd)
11356 {
11357 if (*debug_file_directory != '\0')
11358 search_path = concat (".", dirname_separator_string,
b36cec19 11359 debug_file_directory, (char *) NULL);
6ac97d4c
DE
11360 else
11361 search_path = xstrdup (".");
11362 }
9c02c129 11363 else
6ac97d4c 11364 search_path = xstrdup (debug_file_directory);
3019eac3 11365
492c0ab7 11366 flags = OPF_RETURN_REALPATH;
80626a55
DE
11367 if (is_dwp)
11368 flags |= OPF_SEARCH_IN_PATH;
9c02c129 11369 desc = openp (search_path, flags, file_name,
3019eac3 11370 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 11371 xfree (search_path);
3019eac3
DE
11372 if (desc < 0)
11373 return NULL;
11374
192b62ce 11375 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 11376 xfree (absolute_name);
9c02c129
DE
11377 if (sym_bfd == NULL)
11378 return NULL;
192b62ce 11379 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 11380
192b62ce
TT
11381 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11382 return NULL;
3019eac3 11383
13aaf454
DE
11384 /* Success. Record the bfd as having been included by the objfile's bfd.
11385 This is important because things like demangled_names_hash lives in the
11386 objfile's per_bfd space and may have references to things like symbol
11387 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 11388 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 11389
3019eac3
DE
11390 return sym_bfd;
11391}
11392
ab5088bf 11393/* Try to open DWO file FILE_NAME.
3019eac3
DE
11394 COMP_DIR is the DW_AT_comp_dir attribute.
11395 The result is the bfd handle of the file.
11396 If there is a problem finding or opening the file, return NULL.
11397 Upon success, the canonicalized path of the file is stored in the bfd,
11398 same as symfile_bfd_open. */
11399
192b62ce 11400static gdb_bfd_ref_ptr
ab5088bf 11401open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3 11402{
80626a55 11403 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 11404 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
11405
11406 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11407
11408 if (comp_dir != NULL)
11409 {
b36cec19
PA
11410 char *path_to_try = concat (comp_dir, SLASH_STRING,
11411 file_name, (char *) NULL);
3019eac3
DE
11412
11413 /* NOTE: If comp_dir is a relative path, this will also try the
11414 search path, which seems useful. */
192b62ce
TT
11415 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
11416 1 /*search_cwd*/));
3019eac3
DE
11417 xfree (path_to_try);
11418 if (abfd != NULL)
11419 return abfd;
11420 }
11421
11422 /* That didn't work, try debug-file-directory, which, despite its name,
11423 is a list of paths. */
11424
11425 if (*debug_file_directory == '\0')
11426 return NULL;
11427
6ac97d4c 11428 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
11429}
11430
80626a55
DE
11431/* This function is mapped across the sections and remembers the offset and
11432 size of each of the DWO debugging sections we are interested in. */
11433
11434static void
11435dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
11436{
9a3c8263 11437 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
11438 const struct dwop_section_names *names = &dwop_section_names;
11439
11440 if (section_is_p (sectp->name, &names->abbrev_dwo))
11441 {
049412e3 11442 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
11443 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
11444 }
11445 else if (section_is_p (sectp->name, &names->info_dwo))
11446 {
049412e3 11447 dwo_sections->info.s.section = sectp;
80626a55
DE
11448 dwo_sections->info.size = bfd_get_section_size (sectp);
11449 }
11450 else if (section_is_p (sectp->name, &names->line_dwo))
11451 {
049412e3 11452 dwo_sections->line.s.section = sectp;
80626a55
DE
11453 dwo_sections->line.size = bfd_get_section_size (sectp);
11454 }
11455 else if (section_is_p (sectp->name, &names->loc_dwo))
11456 {
049412e3 11457 dwo_sections->loc.s.section = sectp;
80626a55
DE
11458 dwo_sections->loc.size = bfd_get_section_size (sectp);
11459 }
11460 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11461 {
049412e3 11462 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
11463 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
11464 }
11465 else if (section_is_p (sectp->name, &names->macro_dwo))
11466 {
049412e3 11467 dwo_sections->macro.s.section = sectp;
80626a55
DE
11468 dwo_sections->macro.size = bfd_get_section_size (sectp);
11469 }
11470 else if (section_is_p (sectp->name, &names->str_dwo))
11471 {
049412e3 11472 dwo_sections->str.s.section = sectp;
80626a55
DE
11473 dwo_sections->str.size = bfd_get_section_size (sectp);
11474 }
11475 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11476 {
049412e3 11477 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
11478 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
11479 }
11480 else if (section_is_p (sectp->name, &names->types_dwo))
11481 {
11482 struct dwarf2_section_info type_section;
11483
11484 memset (&type_section, 0, sizeof (type_section));
049412e3 11485 type_section.s.section = sectp;
80626a55
DE
11486 type_section.size = bfd_get_section_size (sectp);
11487 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
11488 &type_section);
11489 }
11490}
11491
ab5088bf 11492/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 11493 by PER_CU. This is for the non-DWP case.
80626a55 11494 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
11495
11496static struct dwo_file *
0ac5b59e
DE
11497open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
11498 const char *dwo_name, const char *comp_dir)
3019eac3
DE
11499{
11500 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 11501 struct dwo_file *dwo_file;
3019eac3
DE
11502 struct cleanup *cleanups;
11503
192b62ce 11504 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
80626a55
DE
11505 if (dbfd == NULL)
11506 {
b4f54984 11507 if (dwarf_read_debug)
80626a55
DE
11508 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
11509 return NULL;
11510 }
11511 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
11512 dwo_file->dwo_name = dwo_name;
11513 dwo_file->comp_dir = comp_dir;
192b62ce 11514 dwo_file->dbfd = dbfd.release ();
3019eac3
DE
11515
11516 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
11517
192b62ce
TT
11518 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
11519 &dwo_file->sections);
3019eac3 11520
33c5cd75 11521 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
3019eac3 11522
78d4d2c5
JK
11523 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
11524 dwo_file->tus);
3019eac3
DE
11525
11526 discard_cleanups (cleanups);
11527
b4f54984 11528 if (dwarf_read_debug)
80626a55
DE
11529 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
11530
3019eac3
DE
11531 return dwo_file;
11532}
11533
80626a55 11534/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
11535 size of each of the DWP debugging sections common to version 1 and 2 that
11536 we are interested in. */
3019eac3 11537
80626a55 11538static void
73869dc2
DE
11539dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
11540 void *dwp_file_ptr)
3019eac3 11541{
9a3c8263 11542 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
11543 const struct dwop_section_names *names = &dwop_section_names;
11544 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 11545
80626a55 11546 /* Record the ELF section number for later lookup: this is what the
73869dc2 11547 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
11548 gdb_assert (elf_section_nr < dwp_file->num_sections);
11549 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 11550
80626a55
DE
11551 /* Look for specific sections that we need. */
11552 if (section_is_p (sectp->name, &names->str_dwo))
11553 {
049412e3 11554 dwp_file->sections.str.s.section = sectp;
80626a55
DE
11555 dwp_file->sections.str.size = bfd_get_section_size (sectp);
11556 }
11557 else if (section_is_p (sectp->name, &names->cu_index))
11558 {
049412e3 11559 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
11560 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
11561 }
11562 else if (section_is_p (sectp->name, &names->tu_index))
11563 {
049412e3 11564 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
11565 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
11566 }
11567}
3019eac3 11568
73869dc2
DE
11569/* This function is mapped across the sections and remembers the offset and
11570 size of each of the DWP version 2 debugging sections that we are interested
11571 in. This is split into a separate function because we don't know if we
11572 have version 1 or 2 until we parse the cu_index/tu_index sections. */
11573
11574static void
11575dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
11576{
9a3c8263 11577 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
11578 const struct dwop_section_names *names = &dwop_section_names;
11579 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11580
11581 /* Record the ELF section number for later lookup: this is what the
11582 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11583 gdb_assert (elf_section_nr < dwp_file->num_sections);
11584 dwp_file->elf_sections[elf_section_nr] = sectp;
11585
11586 /* Look for specific sections that we need. */
11587 if (section_is_p (sectp->name, &names->abbrev_dwo))
11588 {
049412e3 11589 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
11590 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
11591 }
11592 else if (section_is_p (sectp->name, &names->info_dwo))
11593 {
049412e3 11594 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
11595 dwp_file->sections.info.size = bfd_get_section_size (sectp);
11596 }
11597 else if (section_is_p (sectp->name, &names->line_dwo))
11598 {
049412e3 11599 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
11600 dwp_file->sections.line.size = bfd_get_section_size (sectp);
11601 }
11602 else if (section_is_p (sectp->name, &names->loc_dwo))
11603 {
049412e3 11604 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
11605 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
11606 }
11607 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11608 {
049412e3 11609 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
11610 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
11611 }
11612 else if (section_is_p (sectp->name, &names->macro_dwo))
11613 {
049412e3 11614 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
11615 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
11616 }
11617 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11618 {
049412e3 11619 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
11620 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
11621 }
11622 else if (section_is_p (sectp->name, &names->types_dwo))
11623 {
049412e3 11624 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
11625 dwp_file->sections.types.size = bfd_get_section_size (sectp);
11626 }
11627}
11628
80626a55 11629/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 11630
80626a55
DE
11631static hashval_t
11632hash_dwp_loaded_cutus (const void *item)
11633{
9a3c8263 11634 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 11635
80626a55
DE
11636 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11637 return dwo_unit->signature;
3019eac3
DE
11638}
11639
80626a55 11640/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 11641
80626a55
DE
11642static int
11643eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 11644{
9a3c8263
SM
11645 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11646 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 11647
80626a55
DE
11648 return dua->signature == dub->signature;
11649}
3019eac3 11650
80626a55 11651/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 11652
80626a55
DE
11653static htab_t
11654allocate_dwp_loaded_cutus_table (struct objfile *objfile)
11655{
11656 return htab_create_alloc_ex (3,
11657 hash_dwp_loaded_cutus,
11658 eq_dwp_loaded_cutus,
11659 NULL,
11660 &objfile->objfile_obstack,
11661 hashtab_obstack_allocate,
11662 dummy_obstack_deallocate);
11663}
3019eac3 11664
ab5088bf
DE
11665/* Try to open DWP file FILE_NAME.
11666 The result is the bfd handle of the file.
11667 If there is a problem finding or opening the file, return NULL.
11668 Upon success, the canonicalized path of the file is stored in the bfd,
11669 same as symfile_bfd_open. */
11670
192b62ce 11671static gdb_bfd_ref_ptr
ab5088bf
DE
11672open_dwp_file (const char *file_name)
11673{
192b62ce
TT
11674 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
11675 1 /*search_cwd*/));
6ac97d4c
DE
11676 if (abfd != NULL)
11677 return abfd;
11678
11679 /* Work around upstream bug 15652.
11680 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11681 [Whether that's a "bug" is debatable, but it is getting in our way.]
11682 We have no real idea where the dwp file is, because gdb's realpath-ing
11683 of the executable's path may have discarded the needed info.
11684 [IWBN if the dwp file name was recorded in the executable, akin to
11685 .gnu_debuglink, but that doesn't exist yet.]
11686 Strip the directory from FILE_NAME and search again. */
11687 if (*debug_file_directory != '\0')
11688 {
11689 /* Don't implicitly search the current directory here.
11690 If the user wants to search "." to handle this case,
11691 it must be added to debug-file-directory. */
11692 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
11693 0 /*search_cwd*/);
11694 }
11695
11696 return NULL;
ab5088bf
DE
11697}
11698
80626a55
DE
11699/* Initialize the use of the DWP file for the current objfile.
11700 By convention the name of the DWP file is ${objfile}.dwp.
11701 The result is NULL if it can't be found. */
a766d390 11702
80626a55 11703static struct dwp_file *
ab5088bf 11704open_and_init_dwp_file (void)
80626a55
DE
11705{
11706 struct objfile *objfile = dwarf2_per_objfile->objfile;
11707 struct dwp_file *dwp_file;
80626a55 11708
82bf32bc
JK
11709 /* Try to find first .dwp for the binary file before any symbolic links
11710 resolving. */
6c447423
DE
11711
11712 /* If the objfile is a debug file, find the name of the real binary
11713 file and get the name of dwp file from there. */
d721ba37 11714 std::string dwp_name;
6c447423
DE
11715 if (objfile->separate_debug_objfile_backlink != NULL)
11716 {
11717 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11718 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 11719
d721ba37 11720 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
11721 }
11722 else
d721ba37
PA
11723 dwp_name = objfile->original_name;
11724
11725 dwp_name += ".dwp";
80626a55 11726
d721ba37 11727 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
82bf32bc
JK
11728 if (dbfd == NULL
11729 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11730 {
11731 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
11732 dwp_name = objfile_name (objfile);
11733 dwp_name += ".dwp";
11734 dbfd = open_dwp_file (dwp_name.c_str ());
82bf32bc
JK
11735 }
11736
80626a55
DE
11737 if (dbfd == NULL)
11738 {
b4f54984 11739 if (dwarf_read_debug)
d721ba37 11740 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 11741 return NULL;
3019eac3 11742 }
80626a55 11743 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
11744 dwp_file->name = bfd_get_filename (dbfd.get ());
11745 dwp_file->dbfd = dbfd.release ();
c906108c 11746
80626a55 11747 /* +1: section 0 is unused */
192b62ce 11748 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
11749 dwp_file->elf_sections =
11750 OBSTACK_CALLOC (&objfile->objfile_obstack,
11751 dwp_file->num_sections, asection *);
11752
192b62ce
TT
11753 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
11754 dwp_file);
80626a55
DE
11755
11756 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
11757
11758 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
11759
73869dc2 11760 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
11761 if (dwp_file->cus && dwp_file->tus
11762 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
11763 {
11764 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 11765 pretty bizarre. We use pulongest here because that's the established
4d65956b 11766 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
11767 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11768 " TU version %s [in DWP file %s]"),
11769 pulongest (dwp_file->cus->version),
d721ba37 11770 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 11771 }
08302ed2
DE
11772
11773 if (dwp_file->cus)
11774 dwp_file->version = dwp_file->cus->version;
11775 else if (dwp_file->tus)
11776 dwp_file->version = dwp_file->tus->version;
11777 else
11778 dwp_file->version = 2;
73869dc2
DE
11779
11780 if (dwp_file->version == 2)
192b62ce
TT
11781 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
11782 dwp_file);
73869dc2 11783
19ac8c2e
DE
11784 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
11785 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 11786
b4f54984 11787 if (dwarf_read_debug)
80626a55
DE
11788 {
11789 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
11790 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
11791 " %s CUs, %s TUs\n",
11792 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11793 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
11794 }
11795
11796 return dwp_file;
3019eac3 11797}
c906108c 11798
ab5088bf
DE
11799/* Wrapper around open_and_init_dwp_file, only open it once. */
11800
11801static struct dwp_file *
11802get_dwp_file (void)
11803{
11804 if (! dwarf2_per_objfile->dwp_checked)
11805 {
11806 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
11807 dwarf2_per_objfile->dwp_checked = 1;
11808 }
11809 return dwarf2_per_objfile->dwp_file;
11810}
11811
80626a55
DE
11812/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11813 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11814 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 11815 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
11816 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11817
11818 This is called, for example, when wanting to read a variable with a
11819 complex location. Therefore we don't want to do file i/o for every call.
11820 Therefore we don't want to look for a DWO file on every call.
11821 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11822 then we check if we've already seen DWO_NAME, and only THEN do we check
11823 for a DWO file.
11824
1c658ad5 11825 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 11826 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 11827
3019eac3 11828static struct dwo_unit *
80626a55
DE
11829lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11830 const char *dwo_name, const char *comp_dir,
11831 ULONGEST signature, int is_debug_types)
3019eac3
DE
11832{
11833 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
11834 const char *kind = is_debug_types ? "TU" : "CU";
11835 void **dwo_file_slot;
3019eac3 11836 struct dwo_file *dwo_file;
80626a55 11837 struct dwp_file *dwp_file;
cb1df416 11838
6a506a2d
DE
11839 /* First see if there's a DWP file.
11840 If we have a DWP file but didn't find the DWO inside it, don't
11841 look for the original DWO file. It makes gdb behave differently
11842 depending on whether one is debugging in the build tree. */
cf2c3c16 11843
ab5088bf 11844 dwp_file = get_dwp_file ();
80626a55 11845 if (dwp_file != NULL)
cf2c3c16 11846 {
80626a55
DE
11847 const struct dwp_hash_table *dwp_htab =
11848 is_debug_types ? dwp_file->tus : dwp_file->cus;
11849
11850 if (dwp_htab != NULL)
11851 {
11852 struct dwo_unit *dwo_cutu =
57d63ce2
DE
11853 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11854 signature, is_debug_types);
80626a55
DE
11855
11856 if (dwo_cutu != NULL)
11857 {
b4f54984 11858 if (dwarf_read_debug)
80626a55
DE
11859 {
11860 fprintf_unfiltered (gdb_stdlog,
11861 "Virtual DWO %s %s found: @%s\n",
11862 kind, hex_string (signature),
11863 host_address_to_string (dwo_cutu));
11864 }
11865 return dwo_cutu;
11866 }
11867 }
11868 }
6a506a2d 11869 else
80626a55 11870 {
6a506a2d 11871 /* No DWP file, look for the DWO file. */
80626a55 11872
6a506a2d
DE
11873 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11874 if (*dwo_file_slot == NULL)
80626a55 11875 {
6a506a2d
DE
11876 /* Read in the file and build a table of the CUs/TUs it contains. */
11877 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 11878 }
6a506a2d 11879 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 11880 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 11881
6a506a2d 11882 if (dwo_file != NULL)
19c3d4c9 11883 {
6a506a2d
DE
11884 struct dwo_unit *dwo_cutu = NULL;
11885
11886 if (is_debug_types && dwo_file->tus)
11887 {
11888 struct dwo_unit find_dwo_cutu;
11889
11890 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11891 find_dwo_cutu.signature = signature;
9a3c8263
SM
11892 dwo_cutu
11893 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 11894 }
33c5cd75 11895 else if (!is_debug_types && dwo_file->cus)
80626a55 11896 {
33c5cd75
DB
11897 struct dwo_unit find_dwo_cutu;
11898
11899 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11900 find_dwo_cutu.signature = signature;
11901 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
11902 &find_dwo_cutu);
6a506a2d
DE
11903 }
11904
11905 if (dwo_cutu != NULL)
11906 {
b4f54984 11907 if (dwarf_read_debug)
6a506a2d
DE
11908 {
11909 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11910 kind, dwo_name, hex_string (signature),
11911 host_address_to_string (dwo_cutu));
11912 }
11913 return dwo_cutu;
80626a55
DE
11914 }
11915 }
2e276125 11916 }
9cdd5dbd 11917
80626a55
DE
11918 /* We didn't find it. This could mean a dwo_id mismatch, or
11919 someone deleted the DWO/DWP file, or the search path isn't set up
11920 correctly to find the file. */
11921
b4f54984 11922 if (dwarf_read_debug)
80626a55
DE
11923 {
11924 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11925 kind, dwo_name, hex_string (signature));
11926 }
3019eac3 11927
6656a72d
DE
11928 /* This is a warning and not a complaint because it can be caused by
11929 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
11930 {
11931 /* Print the name of the DWP file if we looked there, helps the user
11932 better diagnose the problem. */
791afaa2 11933 std::string dwp_text;
43942612
DE
11934
11935 if (dwp_file != NULL)
791afaa2
TT
11936 dwp_text = string_printf (" [in DWP file %s]",
11937 lbasename (dwp_file->name));
43942612
DE
11938
11939 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11940 " [in module %s]"),
11941 kind, dwo_name, hex_string (signature),
791afaa2 11942 dwp_text.c_str (),
43942612 11943 this_unit->is_debug_types ? "TU" : "CU",
9c541725 11944 to_underlying (this_unit->sect_off), objfile_name (objfile));
43942612 11945 }
3019eac3 11946 return NULL;
5fb290d7
DJ
11947}
11948
80626a55
DE
11949/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11950 See lookup_dwo_cutu_unit for details. */
11951
11952static struct dwo_unit *
11953lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11954 const char *dwo_name, const char *comp_dir,
11955 ULONGEST signature)
11956{
11957 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11958}
11959
11960/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11961 See lookup_dwo_cutu_unit for details. */
11962
11963static struct dwo_unit *
11964lookup_dwo_type_unit (struct signatured_type *this_tu,
11965 const char *dwo_name, const char *comp_dir)
11966{
11967 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11968}
11969
89e63ee4
DE
11970/* Traversal function for queue_and_load_all_dwo_tus. */
11971
11972static int
11973queue_and_load_dwo_tu (void **slot, void *info)
11974{
11975 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11976 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11977 ULONGEST signature = dwo_unit->signature;
11978 struct signatured_type *sig_type =
11979 lookup_dwo_signatured_type (per_cu->cu, signature);
11980
11981 if (sig_type != NULL)
11982 {
11983 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11984
11985 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11986 a real dependency of PER_CU on SIG_TYPE. That is detected later
11987 while processing PER_CU. */
11988 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11989 load_full_type_unit (sig_cu);
11990 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11991 }
11992
11993 return 1;
11994}
11995
11996/* Queue all TUs contained in the DWO of PER_CU to be read in.
11997 The DWO may have the only definition of the type, though it may not be
11998 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11999 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12000
12001static void
12002queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12003{
12004 struct dwo_unit *dwo_unit;
12005 struct dwo_file *dwo_file;
12006
12007 gdb_assert (!per_cu->is_debug_types);
12008 gdb_assert (get_dwp_file () == NULL);
12009 gdb_assert (per_cu->cu != NULL);
12010
12011 dwo_unit = per_cu->cu->dwo_unit;
12012 gdb_assert (dwo_unit != NULL);
12013
12014 dwo_file = dwo_unit->dwo_file;
12015 if (dwo_file->tus != NULL)
12016 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
12017}
12018
3019eac3
DE
12019/* Free all resources associated with DWO_FILE.
12020 Close the DWO file and munmap the sections.
12021 All memory should be on the objfile obstack. */
348e048f
DE
12022
12023static void
3019eac3 12024free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 12025{
348e048f 12026
5c6fa7ab 12027 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 12028 gdb_bfd_unref (dwo_file->dbfd);
348e048f 12029
3019eac3
DE
12030 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
12031}
348e048f 12032
3019eac3 12033/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 12034
3019eac3
DE
12035static void
12036free_dwo_file_cleanup (void *arg)
12037{
12038 struct dwo_file *dwo_file = (struct dwo_file *) arg;
12039 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 12040
3019eac3
DE
12041 free_dwo_file (dwo_file, objfile);
12042}
348e048f 12043
3019eac3 12044/* Traversal function for free_dwo_files. */
2ab95328 12045
3019eac3
DE
12046static int
12047free_dwo_file_from_slot (void **slot, void *info)
12048{
12049 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
12050 struct objfile *objfile = (struct objfile *) info;
348e048f 12051
3019eac3 12052 free_dwo_file (dwo_file, objfile);
348e048f 12053
3019eac3
DE
12054 return 1;
12055}
348e048f 12056
3019eac3 12057/* Free all resources associated with DWO_FILES. */
348e048f 12058
3019eac3
DE
12059static void
12060free_dwo_files (htab_t dwo_files, struct objfile *objfile)
12061{
12062 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 12063}
3019eac3
DE
12064\f
12065/* Read in various DIEs. */
348e048f 12066
d389af10 12067/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12068 Inherit only the children of the DW_AT_abstract_origin DIE not being
12069 already referenced by DW_AT_abstract_origin from the children of the
12070 current DIE. */
d389af10
JK
12071
12072static void
12073inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12074{
12075 struct die_info *child_die;
791afaa2 12076 sect_offset *offsetp;
d389af10
JK
12077 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12078 struct die_info *origin_die;
12079 /* Iterator of the ORIGIN_DIE children. */
12080 struct die_info *origin_child_die;
d389af10 12081 struct attribute *attr;
cd02d79d
PA
12082 struct dwarf2_cu *origin_cu;
12083 struct pending **origin_previous_list_in_scope;
d389af10
JK
12084
12085 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12086 if (!attr)
12087 return;
12088
cd02d79d
PA
12089 /* Note that following die references may follow to a die in a
12090 different cu. */
12091
12092 origin_cu = cu;
12093 origin_die = follow_die_ref (die, attr, &origin_cu);
12094
12095 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12096 symbols in. */
12097 origin_previous_list_in_scope = origin_cu->list_in_scope;
12098 origin_cu->list_in_scope = cu->list_in_scope;
12099
edb3359d
DJ
12100 if (die->tag != origin_die->tag
12101 && !(die->tag == DW_TAG_inlined_subroutine
12102 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
12103 complaint (&symfile_complaints,
12104 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9c541725
PA
12105 to_underlying (die->sect_off),
12106 to_underlying (origin_die->sect_off));
d389af10 12107
791afaa2 12108 std::vector<sect_offset> offsets;
d389af10 12109
3ea89b92
PMR
12110 for (child_die = die->child;
12111 child_die && child_die->tag;
12112 child_die = sibling_die (child_die))
12113 {
12114 struct die_info *child_origin_die;
12115 struct dwarf2_cu *child_origin_cu;
12116
12117 /* We are trying to process concrete instance entries:
216f72a1 12118 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12119 it's not relevant to our analysis here. i.e. detecting DIEs that are
12120 present in the abstract instance but not referenced in the concrete
12121 one. */
216f72a1
JK
12122 if (child_die->tag == DW_TAG_call_site
12123 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12124 continue;
12125
c38f313d
DJ
12126 /* For each CHILD_DIE, find the corresponding child of
12127 ORIGIN_DIE. If there is more than one layer of
12128 DW_AT_abstract_origin, follow them all; there shouldn't be,
12129 but GCC versions at least through 4.4 generate this (GCC PR
12130 40573). */
3ea89b92
PMR
12131 child_origin_die = child_die;
12132 child_origin_cu = cu;
c38f313d
DJ
12133 while (1)
12134 {
cd02d79d
PA
12135 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12136 child_origin_cu);
c38f313d
DJ
12137 if (attr == NULL)
12138 break;
cd02d79d
PA
12139 child_origin_die = follow_die_ref (child_origin_die, attr,
12140 &child_origin_cu);
c38f313d
DJ
12141 }
12142
d389af10
JK
12143 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12144 counterpart may exist. */
c38f313d 12145 if (child_origin_die != child_die)
d389af10 12146 {
edb3359d
DJ
12147 if (child_die->tag != child_origin_die->tag
12148 && !(child_die->tag == DW_TAG_inlined_subroutine
12149 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
12150 complaint (&symfile_complaints,
12151 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
12152 "different tags"),
12153 to_underlying (child_die->sect_off),
12154 to_underlying (child_origin_die->sect_off));
c38f313d
DJ
12155 if (child_origin_die->parent != origin_die)
12156 complaint (&symfile_complaints,
12157 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
12158 "different parents"),
12159 to_underlying (child_die->sect_off),
12160 to_underlying (child_origin_die->sect_off));
c38f313d 12161 else
791afaa2 12162 offsets.push_back (child_origin_die->sect_off);
d389af10 12163 }
d389af10 12164 }
791afaa2
TT
12165 std::sort (offsets.begin (), offsets.end ());
12166 sect_offset *offsets_end = offsets.data () + offsets.size ();
12167 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12168 if (offsetp[-1] == *offsetp)
3e43a32a
MS
12169 complaint (&symfile_complaints,
12170 _("Multiple children of DIE 0x%x refer "
12171 "to DIE 0x%x as their abstract origin"),
9c541725 12172 to_underlying (die->sect_off), to_underlying (*offsetp));
d389af10 12173
791afaa2 12174 offsetp = offsets.data ();
d389af10
JK
12175 origin_child_die = origin_die->child;
12176 while (origin_child_die && origin_child_die->tag)
12177 {
12178 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12179 while (offsetp < offsets_end
9c541725 12180 && *offsetp < origin_child_die->sect_off)
d389af10 12181 offsetp++;
b64f50a1 12182 if (offsetp >= offsets_end
9c541725 12183 || *offsetp > origin_child_die->sect_off)
d389af10 12184 {
adde2bff
DE
12185 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12186 Check whether we're already processing ORIGIN_CHILD_DIE.
12187 This can happen with mutually referenced abstract_origins.
12188 PR 16581. */
12189 if (!origin_child_die->in_process)
12190 process_die (origin_child_die, origin_cu);
d389af10
JK
12191 }
12192 origin_child_die = sibling_die (origin_child_die);
12193 }
cd02d79d 12194 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
12195}
12196
c906108c 12197static void
e7c27a73 12198read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12199{
e7c27a73 12200 struct objfile *objfile = cu->objfile;
3e29f34a 12201 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 12202 struct context_stack *newobj;
c906108c
SS
12203 CORE_ADDR lowpc;
12204 CORE_ADDR highpc;
12205 struct die_info *child_die;
edb3359d 12206 struct attribute *attr, *call_line, *call_file;
15d034d0 12207 const char *name;
e142c38c 12208 CORE_ADDR baseaddr;
801e3a5b 12209 struct block *block;
edb3359d 12210 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12211 std::vector<struct symbol *> template_args;
34eaf542 12212 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12213
12214 if (inlined_func)
12215 {
12216 /* If we do not have call site information, we can't show the
12217 caller of this inlined function. That's too confusing, so
12218 only use the scope for local variables. */
12219 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12220 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12221 if (call_line == NULL || call_file == NULL)
12222 {
12223 read_lexical_block_scope (die, cu);
12224 return;
12225 }
12226 }
c906108c 12227
e142c38c
DJ
12228 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12229
94af9270 12230 name = dwarf2_name (die, cu);
c906108c 12231
e8d05480
JB
12232 /* Ignore functions with missing or empty names. These are actually
12233 illegal according to the DWARF standard. */
12234 if (name == NULL)
12235 {
12236 complaint (&symfile_complaints,
b64f50a1 12237 _("missing name for subprogram DIE at %d"),
9c541725 12238 to_underlying (die->sect_off));
e8d05480
JB
12239 return;
12240 }
12241
12242 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12243 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12244 <= PC_BOUNDS_INVALID)
e8d05480 12245 {
ae4d0c03
PM
12246 attr = dwarf2_attr (die, DW_AT_external, cu);
12247 if (!attr || !DW_UNSND (attr))
12248 complaint (&symfile_complaints,
3e43a32a
MS
12249 _("cannot get low and high bounds "
12250 "for subprogram DIE at %d"),
9c541725 12251 to_underlying (die->sect_off));
e8d05480
JB
12252 return;
12253 }
c906108c 12254
3e29f34a
MR
12255 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12256 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12257
34eaf542
TT
12258 /* If we have any template arguments, then we must allocate a
12259 different sort of symbol. */
12260 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12261 {
12262 if (child_die->tag == DW_TAG_template_type_param
12263 || child_die->tag == DW_TAG_template_value_param)
12264 {
e623cf5d 12265 templ_func = allocate_template_symbol (objfile);
cf724bc9 12266 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
12267 break;
12268 }
12269 }
12270
fe978cb0
PA
12271 newobj = push_context (0, lowpc);
12272 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 12273 (struct symbol *) templ_func);
4c2df51b 12274
4cecd739
DJ
12275 /* If there is a location expression for DW_AT_frame_base, record
12276 it. */
e142c38c 12277 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 12278 if (attr)
fe978cb0 12279 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 12280
63e43d3a
PMR
12281 /* If there is a location for the static link, record it. */
12282 newobj->static_link = NULL;
12283 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12284 if (attr)
12285 {
224c3ddb
SM
12286 newobj->static_link
12287 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
12288 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
12289 }
12290
e142c38c 12291 cu->list_in_scope = &local_symbols;
c906108c 12292
639d11d3 12293 if (die->child != NULL)
c906108c 12294 {
639d11d3 12295 child_die = die->child;
c906108c
SS
12296 while (child_die && child_die->tag)
12297 {
34eaf542
TT
12298 if (child_die->tag == DW_TAG_template_type_param
12299 || child_die->tag == DW_TAG_template_value_param)
12300 {
12301 struct symbol *arg = new_symbol (child_die, NULL, cu);
12302
f1078f66 12303 if (arg != NULL)
2f4732b0 12304 template_args.push_back (arg);
34eaf542
TT
12305 }
12306 else
12307 process_die (child_die, cu);
c906108c
SS
12308 child_die = sibling_die (child_die);
12309 }
12310 }
12311
d389af10
JK
12312 inherit_abstract_dies (die, cu);
12313
4a811a97
UW
12314 /* If we have a DW_AT_specification, we might need to import using
12315 directives from the context of the specification DIE. See the
12316 comment in determine_prefix. */
12317 if (cu->language == language_cplus
12318 && dwarf2_attr (die, DW_AT_specification, cu))
12319 {
12320 struct dwarf2_cu *spec_cu = cu;
12321 struct die_info *spec_die = die_specification (die, &spec_cu);
12322
12323 while (spec_die)
12324 {
12325 child_die = spec_die->child;
12326 while (child_die && child_die->tag)
12327 {
12328 if (child_die->tag == DW_TAG_imported_module)
12329 process_die (child_die, spec_cu);
12330 child_die = sibling_die (child_die);
12331 }
12332
12333 /* In some cases, GCC generates specification DIEs that
12334 themselves contain DW_AT_specification attributes. */
12335 spec_die = die_specification (spec_die, &spec_cu);
12336 }
12337 }
12338
fe978cb0 12339 newobj = pop_context ();
c906108c 12340 /* Make a block for the local symbols within. */
fe978cb0 12341 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 12342 newobj->static_link, lowpc, highpc);
801e3a5b 12343
df8a16a1 12344 /* For C++, set the block's scope. */
45280282
IB
12345 if ((cu->language == language_cplus
12346 || cu->language == language_fortran
c44af4eb
TT
12347 || cu->language == language_d
12348 || cu->language == language_rust)
4d4ec4e5 12349 && cu->processing_has_namespace_info)
195a3f6c
TT
12350 block_set_scope (block, determine_prefix (die, cu),
12351 &objfile->objfile_obstack);
df8a16a1 12352
801e3a5b
JB
12353 /* If we have address ranges, record them. */
12354 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 12355
fe978cb0 12356 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 12357
34eaf542 12358 /* Attach template arguments to function. */
2f4732b0 12359 if (!template_args.empty ())
34eaf542
TT
12360 {
12361 gdb_assert (templ_func != NULL);
12362
2f4732b0 12363 templ_func->n_template_arguments = template_args.size ();
34eaf542 12364 templ_func->template_arguments
8d749320
SM
12365 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12366 templ_func->n_template_arguments);
34eaf542 12367 memcpy (templ_func->template_arguments,
2f4732b0 12368 template_args.data (),
34eaf542 12369 (templ_func->n_template_arguments * sizeof (struct symbol *)));
34eaf542
TT
12370 }
12371
208d8187
JB
12372 /* In C++, we can have functions nested inside functions (e.g., when
12373 a function declares a class that has methods). This means that
12374 when we finish processing a function scope, we may need to go
12375 back to building a containing block's symbol lists. */
fe978cb0 12376 local_symbols = newobj->locals;
22cee43f 12377 local_using_directives = newobj->local_using_directives;
208d8187 12378
921e78cf
JB
12379 /* If we've finished processing a top-level function, subsequent
12380 symbols go in the file symbol list. */
12381 if (outermost_context_p ())
e142c38c 12382 cu->list_in_scope = &file_symbols;
c906108c
SS
12383}
12384
12385/* Process all the DIES contained within a lexical block scope. Start
12386 a new scope, process the dies, and then close the scope. */
12387
12388static void
e7c27a73 12389read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12390{
e7c27a73 12391 struct objfile *objfile = cu->objfile;
3e29f34a 12392 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 12393 struct context_stack *newobj;
c906108c
SS
12394 CORE_ADDR lowpc, highpc;
12395 struct die_info *child_die;
e142c38c
DJ
12396 CORE_ADDR baseaddr;
12397
12398 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
12399
12400 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
12401 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12402 as multiple lexical blocks? Handling children in a sane way would
6e70227d 12403 be nasty. Might be easier to properly extend generic blocks to
af34e669 12404 describe ranges. */
e385593e
JK
12405 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
12406 {
12407 case PC_BOUNDS_NOT_PRESENT:
12408 /* DW_TAG_lexical_block has no attributes, process its children as if
12409 there was no wrapping by that DW_TAG_lexical_block.
12410 GCC does no longer produces such DWARF since GCC r224161. */
12411 for (child_die = die->child;
12412 child_die != NULL && child_die->tag;
12413 child_die = sibling_die (child_die))
12414 process_die (child_die, cu);
12415 return;
12416 case PC_BOUNDS_INVALID:
12417 return;
12418 }
3e29f34a
MR
12419 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12420 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
12421
12422 push_context (0, lowpc);
639d11d3 12423 if (die->child != NULL)
c906108c 12424 {
639d11d3 12425 child_die = die->child;
c906108c
SS
12426 while (child_die && child_die->tag)
12427 {
e7c27a73 12428 process_die (child_die, cu);
c906108c
SS
12429 child_die = sibling_die (child_die);
12430 }
12431 }
3ea89b92 12432 inherit_abstract_dies (die, cu);
fe978cb0 12433 newobj = pop_context ();
c906108c 12434
22cee43f 12435 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 12436 {
801e3a5b 12437 struct block *block
63e43d3a 12438 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 12439 newobj->start_addr, highpc);
801e3a5b
JB
12440
12441 /* Note that recording ranges after traversing children, as we
12442 do here, means that recording a parent's ranges entails
12443 walking across all its children's ranges as they appear in
12444 the address map, which is quadratic behavior.
12445
12446 It would be nicer to record the parent's ranges before
12447 traversing its children, simply overriding whatever you find
12448 there. But since we don't even decide whether to create a
12449 block until after we've traversed its children, that's hard
12450 to do. */
12451 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 12452 }
fe978cb0 12453 local_symbols = newobj->locals;
22cee43f 12454 local_using_directives = newobj->local_using_directives;
c906108c
SS
12455}
12456
216f72a1 12457/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
12458
12459static void
12460read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
12461{
12462 struct objfile *objfile = cu->objfile;
12463 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12464 CORE_ADDR pc, baseaddr;
12465 struct attribute *attr;
12466 struct call_site *call_site, call_site_local;
12467 void **slot;
12468 int nparams;
12469 struct die_info *child_die;
12470
12471 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12472
216f72a1
JK
12473 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
12474 if (attr == NULL)
12475 {
12476 /* This was a pre-DWARF-5 GNU extension alias
12477 for DW_AT_call_return_pc. */
12478 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12479 }
96408a79
SA
12480 if (!attr)
12481 {
12482 complaint (&symfile_complaints,
216f72a1 12483 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 12484 "DIE 0x%x [in module %s]"),
9c541725 12485 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12486 return;
12487 }
31aa7e4e 12488 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 12489 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
12490
12491 if (cu->call_site_htab == NULL)
12492 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
12493 NULL, &objfile->objfile_obstack,
12494 hashtab_obstack_allocate, NULL);
12495 call_site_local.pc = pc;
12496 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
12497 if (*slot != NULL)
12498 {
12499 complaint (&symfile_complaints,
216f72a1 12500 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 12501 "DIE 0x%x [in module %s]"),
9c541725 12502 paddress (gdbarch, pc), to_underlying (die->sect_off),
4262abfb 12503 objfile_name (objfile));
96408a79
SA
12504 return;
12505 }
12506
12507 /* Count parameters at the caller. */
12508
12509 nparams = 0;
12510 for (child_die = die->child; child_die && child_die->tag;
12511 child_die = sibling_die (child_die))
12512 {
216f72a1
JK
12513 if (child_die->tag != DW_TAG_call_site_parameter
12514 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
12515 {
12516 complaint (&symfile_complaints,
216f72a1
JK
12517 _("Tag %d is not DW_TAG_call_site_parameter in "
12518 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 12519 child_die->tag, to_underlying (child_die->sect_off),
4262abfb 12520 objfile_name (objfile));
96408a79
SA
12521 continue;
12522 }
12523
12524 nparams++;
12525 }
12526
224c3ddb
SM
12527 call_site
12528 = ((struct call_site *)
12529 obstack_alloc (&objfile->objfile_obstack,
12530 sizeof (*call_site)
12531 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
12532 *slot = call_site;
12533 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
12534 call_site->pc = pc;
12535
216f72a1
JK
12536 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
12537 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
12538 {
12539 struct die_info *func_die;
12540
12541 /* Skip also over DW_TAG_inlined_subroutine. */
12542 for (func_die = die->parent;
12543 func_die && func_die->tag != DW_TAG_subprogram
12544 && func_die->tag != DW_TAG_subroutine_type;
12545 func_die = func_die->parent);
12546
216f72a1
JK
12547 /* DW_AT_call_all_calls is a superset
12548 of DW_AT_call_all_tail_calls. */
96408a79 12549 if (func_die
216f72a1 12550 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 12551 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 12552 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
12553 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12554 {
12555 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12556 not complete. But keep CALL_SITE for look ups via call_site_htab,
12557 both the initial caller containing the real return address PC and
12558 the final callee containing the current PC of a chain of tail
12559 calls do not need to have the tail call list complete. But any
12560 function candidate for a virtual tail call frame searched via
12561 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12562 determined unambiguously. */
12563 }
12564 else
12565 {
12566 struct type *func_type = NULL;
12567
12568 if (func_die)
12569 func_type = get_die_type (func_die, cu);
12570 if (func_type != NULL)
12571 {
12572 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
12573
12574 /* Enlist this call site to the function. */
12575 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12576 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12577 }
12578 else
12579 complaint (&symfile_complaints,
216f72a1 12580 _("Cannot find function owning DW_TAG_call_site "
96408a79 12581 "DIE 0x%x [in module %s]"),
9c541725 12582 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12583 }
12584 }
12585
216f72a1
JK
12586 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12587 if (attr == NULL)
12588 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12589 if (attr == NULL)
12590 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 12591 if (attr == NULL)
216f72a1
JK
12592 {
12593 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12594 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12595 }
96408a79
SA
12596 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
12597 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
12598 /* Keep NULL DWARF_BLOCK. */;
12599 else if (attr_form_is_block (attr))
12600 {
12601 struct dwarf2_locexpr_baton *dlbaton;
12602
8d749320 12603 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
12604 dlbaton->data = DW_BLOCK (attr)->data;
12605 dlbaton->size = DW_BLOCK (attr)->size;
12606 dlbaton->per_cu = cu->per_cu;
12607
12608 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
12609 }
7771576e 12610 else if (attr_form_is_ref (attr))
96408a79 12611 {
96408a79
SA
12612 struct dwarf2_cu *target_cu = cu;
12613 struct die_info *target_die;
12614
ac9ec31b 12615 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
12616 gdb_assert (target_cu->objfile == objfile);
12617 if (die_is_declaration (target_die, target_cu))
12618 {
7d45c7c3 12619 const char *target_physname;
9112db09
JK
12620
12621 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 12622 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 12623 if (target_physname == NULL)
9112db09 12624 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
12625 if (target_physname == NULL)
12626 complaint (&symfile_complaints,
216f72a1 12627 _("DW_AT_call_target target DIE has invalid "
96408a79 12628 "physname, for referencing DIE 0x%x [in module %s]"),
9c541725 12629 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 12630 else
7d455152 12631 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
12632 }
12633 else
12634 {
12635 CORE_ADDR lowpc;
12636
12637 /* DW_AT_entry_pc should be preferred. */
3a2b436a 12638 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 12639 <= PC_BOUNDS_INVALID)
96408a79 12640 complaint (&symfile_complaints,
216f72a1 12641 _("DW_AT_call_target target DIE has invalid "
96408a79 12642 "low pc, for referencing DIE 0x%x [in module %s]"),
9c541725 12643 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 12644 else
3e29f34a
MR
12645 {
12646 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12647 SET_FIELD_PHYSADDR (call_site->target, lowpc);
12648 }
96408a79
SA
12649 }
12650 }
12651 else
12652 complaint (&symfile_complaints,
216f72a1 12653 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 12654 "block nor reference, for DIE 0x%x [in module %s]"),
9c541725 12655 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
12656
12657 call_site->per_cu = cu->per_cu;
12658
12659 for (child_die = die->child;
12660 child_die && child_die->tag;
12661 child_die = sibling_die (child_die))
12662 {
96408a79 12663 struct call_site_parameter *parameter;
1788b2d3 12664 struct attribute *loc, *origin;
96408a79 12665
216f72a1
JK
12666 if (child_die->tag != DW_TAG_call_site_parameter
12667 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
12668 {
12669 /* Already printed the complaint above. */
12670 continue;
12671 }
12672
12673 gdb_assert (call_site->parameter_count < nparams);
12674 parameter = &call_site->parameter[call_site->parameter_count];
12675
1788b2d3
JK
12676 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12677 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 12678 register is contained in DW_AT_call_value. */
96408a79 12679
24c5c679 12680 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
12681 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12682 if (origin == NULL)
12683 {
12684 /* This was a pre-DWARF-5 GNU extension alias
12685 for DW_AT_call_parameter. */
12686 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12687 }
7771576e 12688 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 12689 {
1788b2d3 12690 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
12691
12692 sect_offset sect_off
12693 = (sect_offset) dwarf2_get_ref_die_offset (origin);
12694 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
12695 {
12696 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12697 binding can be done only inside one CU. Such referenced DIE
12698 therefore cannot be even moved to DW_TAG_partial_unit. */
12699 complaint (&symfile_complaints,
216f72a1
JK
12700 _("DW_AT_call_parameter offset is not in CU for "
12701 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12702 to_underlying (child_die->sect_off),
12703 objfile_name (objfile));
d76b7dbc
JK
12704 continue;
12705 }
9c541725
PA
12706 parameter->u.param_cu_off
12707 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
12708 }
12709 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
12710 {
12711 complaint (&symfile_complaints,
12712 _("No DW_FORM_block* DW_AT_location for "
216f72a1 12713 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 12714 to_underlying (child_die->sect_off), objfile_name (objfile));
96408a79
SA
12715 continue;
12716 }
24c5c679 12717 else
96408a79 12718 {
24c5c679
JK
12719 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12720 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12721 if (parameter->u.dwarf_reg != -1)
12722 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12723 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12724 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12725 &parameter->u.fb_offset))
12726 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12727 else
12728 {
12729 complaint (&symfile_complaints,
12730 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12731 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 12732 "DW_TAG_call_site child DIE 0x%x "
24c5c679 12733 "[in module %s]"),
9c541725
PA
12734 to_underlying (child_die->sect_off),
12735 objfile_name (objfile));
24c5c679
JK
12736 continue;
12737 }
96408a79
SA
12738 }
12739
216f72a1
JK
12740 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12741 if (attr == NULL)
12742 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
12743 if (!attr_form_is_block (attr))
12744 {
12745 complaint (&symfile_complaints,
216f72a1
JK
12746 _("No DW_FORM_block* DW_AT_call_value for "
12747 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12748 to_underlying (child_die->sect_off),
12749 objfile_name (objfile));
96408a79
SA
12750 continue;
12751 }
12752 parameter->value = DW_BLOCK (attr)->data;
12753 parameter->value_size = DW_BLOCK (attr)->size;
12754
12755 /* Parameters are not pre-cleared by memset above. */
12756 parameter->data_value = NULL;
12757 parameter->data_value_size = 0;
12758 call_site->parameter_count++;
12759
216f72a1
JK
12760 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12761 if (attr == NULL)
12762 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
12763 if (attr)
12764 {
12765 if (!attr_form_is_block (attr))
12766 complaint (&symfile_complaints,
216f72a1
JK
12767 _("No DW_FORM_block* DW_AT_call_data_value for "
12768 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12769 to_underlying (child_die->sect_off),
12770 objfile_name (objfile));
96408a79
SA
12771 else
12772 {
12773 parameter->data_value = DW_BLOCK (attr)->data;
12774 parameter->data_value_size = DW_BLOCK (attr)->size;
12775 }
12776 }
12777 }
12778}
12779
71a3c369
TT
12780/* Helper function for read_variable. If DIE represents a virtual
12781 table, then return the type of the concrete object that is
12782 associated with the virtual table. Otherwise, return NULL. */
12783
12784static struct type *
12785rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12786{
12787 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
12788 if (attr == NULL)
12789 return NULL;
12790
12791 /* Find the type DIE. */
12792 struct die_info *type_die = NULL;
12793 struct dwarf2_cu *type_cu = cu;
12794
12795 if (attr_form_is_ref (attr))
12796 type_die = follow_die_ref (die, attr, &type_cu);
12797 if (type_die == NULL)
12798 return NULL;
12799
12800 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
12801 return NULL;
12802 return die_containing_type (type_die, type_cu);
12803}
12804
12805/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
12806
12807static void
12808read_variable (struct die_info *die, struct dwarf2_cu *cu)
12809{
12810 struct rust_vtable_symbol *storage = NULL;
12811
12812 if (cu->language == language_rust)
12813 {
12814 struct type *containing_type = rust_containing_type (die, cu);
12815
12816 if (containing_type != NULL)
12817 {
12818 struct objfile *objfile = cu->objfile;
12819
12820 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
12821 struct rust_vtable_symbol);
12822 initialize_objfile_symbol (storage);
12823 storage->concrete_type = containing_type;
cf724bc9 12824 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
12825 }
12826 }
12827
12828 new_symbol_full (die, NULL, cu, storage);
12829}
12830
43988095
JK
12831/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12832 reading .debug_rnglists.
12833 Callback's type should be:
12834 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12835 Return true if the attributes are present and valid, otherwise,
12836 return false. */
12837
12838template <typename Callback>
12839static bool
12840dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12841 Callback &&callback)
12842{
12843 struct objfile *objfile = cu->objfile;
12844 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12845 struct comp_unit_head *cu_header = &cu->header;
12846 bfd *obfd = objfile->obfd;
12847 unsigned int addr_size = cu_header->addr_size;
12848 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12849 /* Base address selection entry. */
12850 CORE_ADDR base;
12851 int found_base;
12852 unsigned int dummy;
12853 const gdb_byte *buffer;
12854 CORE_ADDR low = 0;
12855 CORE_ADDR high = 0;
12856 CORE_ADDR baseaddr;
12857 bool overflow = false;
12858
12859 found_base = cu->base_known;
12860 base = cu->base_address;
12861
12862 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12863 if (offset >= dwarf2_per_objfile->rnglists.size)
12864 {
12865 complaint (&symfile_complaints,
12866 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12867 offset);
12868 return false;
12869 }
12870 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12871
12872 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12873
12874 while (1)
12875 {
7814882a
JK
12876 /* Initialize it due to a false compiler warning. */
12877 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
12878 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12879 + dwarf2_per_objfile->rnglists.size);
12880 unsigned int bytes_read;
12881
12882 if (buffer == buf_end)
12883 {
12884 overflow = true;
12885 break;
12886 }
12887 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12888 switch (rlet)
12889 {
12890 case DW_RLE_end_of_list:
12891 break;
12892 case DW_RLE_base_address:
12893 if (buffer + cu->header.addr_size > buf_end)
12894 {
12895 overflow = true;
12896 break;
12897 }
12898 base = read_address (obfd, buffer, cu, &bytes_read);
12899 found_base = 1;
12900 buffer += bytes_read;
12901 break;
12902 case DW_RLE_start_length:
12903 if (buffer + cu->header.addr_size > buf_end)
12904 {
12905 overflow = true;
12906 break;
12907 }
12908 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12909 buffer += bytes_read;
12910 range_end = (range_beginning
12911 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12912 buffer += bytes_read;
12913 if (buffer > buf_end)
12914 {
12915 overflow = true;
12916 break;
12917 }
12918 break;
12919 case DW_RLE_offset_pair:
12920 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12921 buffer += bytes_read;
12922 if (buffer > buf_end)
12923 {
12924 overflow = true;
12925 break;
12926 }
12927 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12928 buffer += bytes_read;
12929 if (buffer > buf_end)
12930 {
12931 overflow = true;
12932 break;
12933 }
12934 break;
12935 case DW_RLE_start_end:
12936 if (buffer + 2 * cu->header.addr_size > buf_end)
12937 {
12938 overflow = true;
12939 break;
12940 }
12941 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12942 buffer += bytes_read;
12943 range_end = read_address (obfd, buffer, cu, &bytes_read);
12944 buffer += bytes_read;
12945 break;
12946 default:
12947 complaint (&symfile_complaints,
12948 _("Invalid .debug_rnglists data (no base address)"));
12949 return false;
12950 }
12951 if (rlet == DW_RLE_end_of_list || overflow)
12952 break;
12953 if (rlet == DW_RLE_base_address)
12954 continue;
12955
12956 if (!found_base)
12957 {
12958 /* We have no valid base address for the ranges
12959 data. */
12960 complaint (&symfile_complaints,
12961 _("Invalid .debug_rnglists data (no base address)"));
12962 return false;
12963 }
12964
12965 if (range_beginning > range_end)
12966 {
12967 /* Inverted range entries are invalid. */
12968 complaint (&symfile_complaints,
12969 _("Invalid .debug_rnglists data (inverted range)"));
12970 return false;
12971 }
12972
12973 /* Empty range entries have no effect. */
12974 if (range_beginning == range_end)
12975 continue;
12976
12977 range_beginning += base;
12978 range_end += base;
12979
12980 /* A not-uncommon case of bad debug info.
12981 Don't pollute the addrmap with bad data. */
12982 if (range_beginning + baseaddr == 0
12983 && !dwarf2_per_objfile->has_section_at_zero)
12984 {
12985 complaint (&symfile_complaints,
12986 _(".debug_rnglists entry has start address of zero"
12987 " [in module %s]"), objfile_name (objfile));
12988 continue;
12989 }
12990
12991 callback (range_beginning, range_end);
12992 }
12993
12994 if (overflow)
12995 {
12996 complaint (&symfile_complaints,
12997 _("Offset %d is not terminated "
12998 "for DW_AT_ranges attribute"),
12999 offset);
13000 return false;
13001 }
13002
13003 return true;
13004}
13005
13006/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13007 Callback's type should be:
13008 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13009 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13010
43988095 13011template <typename Callback>
43039443 13012static int
5f46c5a5 13013dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13014 Callback &&callback)
43039443
JK
13015{
13016 struct objfile *objfile = cu->objfile;
3e29f34a 13017 struct gdbarch *gdbarch = get_objfile_arch (objfile);
43039443
JK
13018 struct comp_unit_head *cu_header = &cu->header;
13019 bfd *obfd = objfile->obfd;
13020 unsigned int addr_size = cu_header->addr_size;
13021 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13022 /* Base address selection entry. */
13023 CORE_ADDR base;
13024 int found_base;
13025 unsigned int dummy;
d521ce57 13026 const gdb_byte *buffer;
ff013f42 13027 CORE_ADDR baseaddr;
43039443 13028
43988095
JK
13029 if (cu_header->version >= 5)
13030 return dwarf2_rnglists_process (offset, cu, callback);
13031
d00adf39
DE
13032 found_base = cu->base_known;
13033 base = cu->base_address;
43039443 13034
be391dca 13035 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 13036 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
13037 {
13038 complaint (&symfile_complaints,
13039 _("Offset %d out of bounds for DW_AT_ranges attribute"),
13040 offset);
13041 return 0;
13042 }
dce234bc 13043 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 13044
e7030f15 13045 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 13046
43039443
JK
13047 while (1)
13048 {
13049 CORE_ADDR range_beginning, range_end;
13050
13051 range_beginning = read_address (obfd, buffer, cu, &dummy);
13052 buffer += addr_size;
13053 range_end = read_address (obfd, buffer, cu, &dummy);
13054 buffer += addr_size;
13055 offset += 2 * addr_size;
13056
13057 /* An end of list marker is a pair of zero addresses. */
13058 if (range_beginning == 0 && range_end == 0)
13059 /* Found the end of list entry. */
13060 break;
13061
13062 /* Each base address selection entry is a pair of 2 values.
13063 The first is the largest possible address, the second is
13064 the base address. Check for a base address here. */
13065 if ((range_beginning & mask) == mask)
13066 {
28d2bfb9
AB
13067 /* If we found the largest possible address, then we already
13068 have the base address in range_end. */
13069 base = range_end;
43039443
JK
13070 found_base = 1;
13071 continue;
13072 }
13073
13074 if (!found_base)
13075 {
13076 /* We have no valid base address for the ranges
13077 data. */
13078 complaint (&symfile_complaints,
13079 _("Invalid .debug_ranges data (no base address)"));
13080 return 0;
13081 }
13082
9277c30c
UW
13083 if (range_beginning > range_end)
13084 {
13085 /* Inverted range entries are invalid. */
13086 complaint (&symfile_complaints,
13087 _("Invalid .debug_ranges data (inverted range)"));
13088 return 0;
13089 }
13090
13091 /* Empty range entries have no effect. */
13092 if (range_beginning == range_end)
13093 continue;
13094
43039443
JK
13095 range_beginning += base;
13096 range_end += base;
13097
01093045
DE
13098 /* A not-uncommon case of bad debug info.
13099 Don't pollute the addrmap with bad data. */
13100 if (range_beginning + baseaddr == 0
13101 && !dwarf2_per_objfile->has_section_at_zero)
13102 {
13103 complaint (&symfile_complaints,
13104 _(".debug_ranges entry has start address of zero"
4262abfb 13105 " [in module %s]"), objfile_name (objfile));
01093045
DE
13106 continue;
13107 }
13108
5f46c5a5
JK
13109 callback (range_beginning, range_end);
13110 }
13111
13112 return 1;
13113}
13114
13115/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13116 Return 1 if the attributes are present and valid, otherwise, return 0.
13117 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13118
13119static int
13120dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13121 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13122 struct partial_symtab *ranges_pst)
13123{
13124 struct objfile *objfile = cu->objfile;
13125 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13126 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
13127 SECT_OFF_TEXT (objfile));
13128 int low_set = 0;
13129 CORE_ADDR low = 0;
13130 CORE_ADDR high = 0;
13131 int retval;
13132
13133 retval = dwarf2_ranges_process (offset, cu,
13134 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13135 {
9277c30c 13136 if (ranges_pst != NULL)
3e29f34a
MR
13137 {
13138 CORE_ADDR lowpc;
13139 CORE_ADDR highpc;
13140
13141 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
13142 range_beginning + baseaddr);
13143 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
13144 range_end + baseaddr);
13145 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
13146 ranges_pst);
13147 }
ff013f42 13148
43039443
JK
13149 /* FIXME: This is recording everything as a low-high
13150 segment of consecutive addresses. We should have a
13151 data structure for discontiguous block ranges
13152 instead. */
13153 if (! low_set)
13154 {
13155 low = range_beginning;
13156 high = range_end;
13157 low_set = 1;
13158 }
13159 else
13160 {
13161 if (range_beginning < low)
13162 low = range_beginning;
13163 if (range_end > high)
13164 high = range_end;
13165 }
5f46c5a5
JK
13166 });
13167 if (!retval)
13168 return 0;
43039443
JK
13169
13170 if (! low_set)
13171 /* If the first entry is an end-of-list marker, the range
13172 describes an empty scope, i.e. no instructions. */
13173 return 0;
13174
13175 if (low_return)
13176 *low_return = low;
13177 if (high_return)
13178 *high_return = high;
13179 return 1;
13180}
13181
3a2b436a
JK
13182/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13183 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13184 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13185
3a2b436a 13186static enum pc_bounds_kind
af34e669 13187dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
13188 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13189 struct partial_symtab *pst)
c906108c
SS
13190{
13191 struct attribute *attr;
91da1414 13192 struct attribute *attr_high;
af34e669
DJ
13193 CORE_ADDR low = 0;
13194 CORE_ADDR high = 0;
e385593e 13195 enum pc_bounds_kind ret;
c906108c 13196
91da1414
MW
13197 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13198 if (attr_high)
af34e669 13199 {
e142c38c 13200 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 13201 if (attr)
91da1414 13202 {
31aa7e4e
JB
13203 low = attr_value_as_address (attr);
13204 high = attr_value_as_address (attr_high);
13205 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
13206 high += low;
91da1414 13207 }
af34e669
DJ
13208 else
13209 /* Found high w/o low attribute. */
e385593e 13210 return PC_BOUNDS_INVALID;
af34e669
DJ
13211
13212 /* Found consecutive range of addresses. */
3a2b436a 13213 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13214 }
c906108c 13215 else
af34e669 13216 {
e142c38c 13217 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
13218 if (attr != NULL)
13219 {
ab435259
DE
13220 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
13221 We take advantage of the fact that DW_AT_ranges does not appear
13222 in DW_TAG_compile_unit of DWO files. */
13223 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13224 unsigned int ranges_offset = (DW_UNSND (attr)
13225 + (need_ranges_base
13226 ? cu->ranges_base
13227 : 0));
2e3cf129 13228
af34e669 13229 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 13230 .debug_ranges section. */
2e3cf129 13231 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 13232 return PC_BOUNDS_INVALID;
43039443 13233 /* Found discontinuous range of addresses. */
3a2b436a 13234 ret = PC_BOUNDS_RANGES;
af34e669 13235 }
e385593e
JK
13236 else
13237 return PC_BOUNDS_NOT_PRESENT;
af34e669 13238 }
c906108c 13239
9373cf26
JK
13240 /* read_partial_die has also the strict LOW < HIGH requirement. */
13241 if (high <= low)
e385593e 13242 return PC_BOUNDS_INVALID;
c906108c
SS
13243
13244 /* When using the GNU linker, .gnu.linkonce. sections are used to
13245 eliminate duplicate copies of functions and vtables and such.
13246 The linker will arbitrarily choose one and discard the others.
13247 The AT_*_pc values for such functions refer to local labels in
13248 these sections. If the section from that file was discarded, the
13249 labels are not in the output, so the relocs get a value of 0.
13250 If this is a discarded function, mark the pc bounds as invalid,
13251 so that GDB will ignore it. */
72dca2f5 13252 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 13253 return PC_BOUNDS_INVALID;
c906108c
SS
13254
13255 *lowpc = low;
96408a79
SA
13256 if (highpc)
13257 *highpc = high;
af34e669 13258 return ret;
c906108c
SS
13259}
13260
b084d499
JB
13261/* Assuming that DIE represents a subprogram DIE or a lexical block, get
13262 its low and high PC addresses. Do nothing if these addresses could not
13263 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13264 and HIGHPC to the high address if greater than HIGHPC. */
13265
13266static void
13267dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13268 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13269 struct dwarf2_cu *cu)
13270{
13271 CORE_ADDR low, high;
13272 struct die_info *child = die->child;
13273
e385593e 13274 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 13275 {
325fac50
PA
13276 *lowpc = std::min (*lowpc, low);
13277 *highpc = std::max (*highpc, high);
b084d499
JB
13278 }
13279
13280 /* If the language does not allow nested subprograms (either inside
13281 subprograms or lexical blocks), we're done. */
13282 if (cu->language != language_ada)
13283 return;
6e70227d 13284
b084d499
JB
13285 /* Check all the children of the given DIE. If it contains nested
13286 subprograms, then check their pc bounds. Likewise, we need to
13287 check lexical blocks as well, as they may also contain subprogram
13288 definitions. */
13289 while (child && child->tag)
13290 {
13291 if (child->tag == DW_TAG_subprogram
13292 || child->tag == DW_TAG_lexical_block)
13293 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13294 child = sibling_die (child);
13295 }
13296}
13297
fae299cd
DC
13298/* Get the low and high pc's represented by the scope DIE, and store
13299 them in *LOWPC and *HIGHPC. If the correct values can't be
13300 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13301
13302static void
13303get_scope_pc_bounds (struct die_info *die,
13304 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13305 struct dwarf2_cu *cu)
13306{
13307 CORE_ADDR best_low = (CORE_ADDR) -1;
13308 CORE_ADDR best_high = (CORE_ADDR) 0;
13309 CORE_ADDR current_low, current_high;
13310
3a2b436a 13311 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 13312 >= PC_BOUNDS_RANGES)
fae299cd
DC
13313 {
13314 best_low = current_low;
13315 best_high = current_high;
13316 }
13317 else
13318 {
13319 struct die_info *child = die->child;
13320
13321 while (child && child->tag)
13322 {
13323 switch (child->tag) {
13324 case DW_TAG_subprogram:
b084d499 13325 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
13326 break;
13327 case DW_TAG_namespace:
f55ee35c 13328 case DW_TAG_module:
fae299cd
DC
13329 /* FIXME: carlton/2004-01-16: Should we do this for
13330 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13331 that current GCC's always emit the DIEs corresponding
13332 to definitions of methods of classes as children of a
13333 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13334 the DIEs giving the declarations, which could be
13335 anywhere). But I don't see any reason why the
13336 standards says that they have to be there. */
13337 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13338
13339 if (current_low != ((CORE_ADDR) -1))
13340 {
325fac50
PA
13341 best_low = std::min (best_low, current_low);
13342 best_high = std::max (best_high, current_high);
fae299cd
DC
13343 }
13344 break;
13345 default:
0963b4bd 13346 /* Ignore. */
fae299cd
DC
13347 break;
13348 }
13349
13350 child = sibling_die (child);
13351 }
13352 }
13353
13354 *lowpc = best_low;
13355 *highpc = best_high;
13356}
13357
801e3a5b
JB
13358/* Record the address ranges for BLOCK, offset by BASEADDR, as given
13359 in DIE. */
380bca97 13360
801e3a5b
JB
13361static void
13362dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13363 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13364{
bb5ed363 13365 struct objfile *objfile = cu->objfile;
3e29f34a 13366 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 13367 struct attribute *attr;
91da1414 13368 struct attribute *attr_high;
801e3a5b 13369
91da1414
MW
13370 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13371 if (attr_high)
801e3a5b 13372 {
801e3a5b
JB
13373 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13374 if (attr)
13375 {
31aa7e4e
JB
13376 CORE_ADDR low = attr_value_as_address (attr);
13377 CORE_ADDR high = attr_value_as_address (attr_high);
13378
13379 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
13380 high += low;
9a619af0 13381
3e29f34a
MR
13382 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13383 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13384 record_block_range (block, low, high - 1);
801e3a5b
JB
13385 }
13386 }
13387
13388 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13389 if (attr)
13390 {
bb5ed363 13391 bfd *obfd = objfile->obfd;
ab435259
DE
13392 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
13393 We take advantage of the fact that DW_AT_ranges does not appear
13394 in DW_TAG_compile_unit of DWO files. */
13395 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
13396
13397 /* The value of the DW_AT_ranges attribute is the offset of the
13398 address range list in the .debug_ranges section. */
ab435259
DE
13399 unsigned long offset = (DW_UNSND (attr)
13400 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 13401 const gdb_byte *buffer;
801e3a5b
JB
13402
13403 /* For some target architectures, but not others, the
13404 read_address function sign-extends the addresses it returns.
13405 To recognize base address selection entries, we need a
13406 mask. */
13407 unsigned int addr_size = cu->header.addr_size;
13408 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13409
13410 /* The base address, to which the next pair is relative. Note
13411 that this 'base' is a DWARF concept: most entries in a range
13412 list are relative, to reduce the number of relocs against the
13413 debugging information. This is separate from this function's
13414 'baseaddr' argument, which GDB uses to relocate debugging
13415 information from a shared library based on the address at
13416 which the library was loaded. */
d00adf39
DE
13417 CORE_ADDR base = cu->base_address;
13418 int base_known = cu->base_known;
801e3a5b 13419
5f46c5a5
JK
13420 dwarf2_ranges_process (offset, cu,
13421 [&] (CORE_ADDR start, CORE_ADDR end)
13422 {
58fdfd2c
JK
13423 start += baseaddr;
13424 end += baseaddr;
5f46c5a5
JK
13425 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13426 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13427 record_block_range (block, start, end - 1);
13428 });
801e3a5b
JB
13429 }
13430}
13431
685b1105
JK
13432/* Check whether the producer field indicates either of GCC < 4.6, or the
13433 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 13434
685b1105
JK
13435static void
13436check_producer (struct dwarf2_cu *cu)
60d5a603 13437{
38360086 13438 int major, minor;
60d5a603
JK
13439
13440 if (cu->producer == NULL)
13441 {
13442 /* For unknown compilers expect their behavior is DWARF version
13443 compliant.
13444
13445 GCC started to support .debug_types sections by -gdwarf-4 since
13446 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13447 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13448 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13449 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 13450 }
b1ffba5a 13451 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 13452 {
38360086
MW
13453 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13454 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 13455 }
5230b05a
WT
13456 else if (producer_is_icc (cu->producer, &major, &minor))
13457 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
13458 else
13459 {
13460 /* For other non-GCC compilers, expect their behavior is DWARF version
13461 compliant. */
60d5a603
JK
13462 }
13463
ba919b58 13464 cu->checked_producer = 1;
685b1105 13465}
ba919b58 13466
685b1105
JK
13467/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13468 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13469 during 4.6.0 experimental. */
13470
13471static int
13472producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
13473{
13474 if (!cu->checked_producer)
13475 check_producer (cu);
13476
13477 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
13478}
13479
13480/* Return the default accessibility type if it is not overriden by
13481 DW_AT_accessibility. */
13482
13483static enum dwarf_access_attribute
13484dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
13485{
13486 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
13487 {
13488 /* The default DWARF 2 accessibility for members is public, the default
13489 accessibility for inheritance is private. */
13490
13491 if (die->tag != DW_TAG_inheritance)
13492 return DW_ACCESS_public;
13493 else
13494 return DW_ACCESS_private;
13495 }
13496 else
13497 {
13498 /* DWARF 3+ defines the default accessibility a different way. The same
13499 rules apply now for DW_TAG_inheritance as for the members and it only
13500 depends on the container kind. */
13501
13502 if (die->parent->tag == DW_TAG_class_type)
13503 return DW_ACCESS_private;
13504 else
13505 return DW_ACCESS_public;
13506 }
13507}
13508
74ac6d43
TT
13509/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
13510 offset. If the attribute was not found return 0, otherwise return
13511 1. If it was found but could not properly be handled, set *OFFSET
13512 to 0. */
13513
13514static int
13515handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
13516 LONGEST *offset)
13517{
13518 struct attribute *attr;
13519
13520 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13521 if (attr != NULL)
13522 {
13523 *offset = 0;
13524
13525 /* Note that we do not check for a section offset first here.
13526 This is because DW_AT_data_member_location is new in DWARF 4,
13527 so if we see it, we can assume that a constant form is really
13528 a constant and not a section offset. */
13529 if (attr_form_is_constant (attr))
13530 *offset = dwarf2_get_attr_constant_value (attr, 0);
13531 else if (attr_form_is_section_offset (attr))
13532 dwarf2_complex_location_expr_complaint ();
13533 else if (attr_form_is_block (attr))
13534 *offset = decode_locdesc (DW_BLOCK (attr), cu);
13535 else
13536 dwarf2_complex_location_expr_complaint ();
13537
13538 return 1;
13539 }
13540
13541 return 0;
13542}
13543
c906108c
SS
13544/* Add an aggregate field to the field list. */
13545
13546static void
107d2387 13547dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 13548 struct dwarf2_cu *cu)
6e70227d 13549{
e7c27a73 13550 struct objfile *objfile = cu->objfile;
5e2b427d 13551 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13552 struct nextfield *new_field;
13553 struct attribute *attr;
13554 struct field *fp;
15d034d0 13555 const char *fieldname = "";
c906108c
SS
13556
13557 /* Allocate a new field list entry and link it in. */
8d749320 13558 new_field = XNEW (struct nextfield);
b8c9b27d 13559 make_cleanup (xfree, new_field);
c906108c 13560 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
13561
13562 if (die->tag == DW_TAG_inheritance)
13563 {
13564 new_field->next = fip->baseclasses;
13565 fip->baseclasses = new_field;
13566 }
13567 else
13568 {
13569 new_field->next = fip->fields;
13570 fip->fields = new_field;
13571 }
c906108c
SS
13572 fip->nfields++;
13573
e142c38c 13574 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
13575 if (attr)
13576 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
13577 else
13578 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
13579 if (new_field->accessibility != DW_ACCESS_public)
13580 fip->non_public_fields = 1;
60d5a603 13581
e142c38c 13582 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
13583 if (attr)
13584 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
13585 else
13586 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
13587
13588 fp = &new_field->field;
a9a9bd0f 13589
e142c38c 13590 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 13591 {
74ac6d43
TT
13592 LONGEST offset;
13593
a9a9bd0f 13594 /* Data member other than a C++ static data member. */
6e70227d 13595
c906108c 13596 /* Get type of field. */
e7c27a73 13597 fp->type = die_type (die, cu);
c906108c 13598
d6a843b5 13599 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 13600
c906108c 13601 /* Get bit size of field (zero if none). */
e142c38c 13602 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
13603 if (attr)
13604 {
13605 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
13606 }
13607 else
13608 {
13609 FIELD_BITSIZE (*fp) = 0;
13610 }
13611
13612 /* Get bit offset of field. */
74ac6d43
TT
13613 if (handle_data_member_location (die, cu, &offset))
13614 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 13615 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
13616 if (attr)
13617 {
5e2b427d 13618 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
13619 {
13620 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
13621 additional bit offset from the MSB of the containing
13622 anonymous object to the MSB of the field. We don't
13623 have to do anything special since we don't need to
13624 know the size of the anonymous object. */
f41f5e61 13625 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
13626 }
13627 else
13628 {
13629 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
13630 MSB of the anonymous object, subtract off the number of
13631 bits from the MSB of the field to the MSB of the
13632 object, and then subtract off the number of bits of
13633 the field itself. The result is the bit offset of
13634 the LSB of the field. */
c906108c
SS
13635 int anonymous_size;
13636 int bit_offset = DW_UNSND (attr);
13637
e142c38c 13638 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13639 if (attr)
13640 {
13641 /* The size of the anonymous object containing
13642 the bit field is explicit, so use the
13643 indicated size (in bytes). */
13644 anonymous_size = DW_UNSND (attr);
13645 }
13646 else
13647 {
13648 /* The size of the anonymous object containing
13649 the bit field must be inferred from the type
13650 attribute of the data member containing the
13651 bit field. */
13652 anonymous_size = TYPE_LENGTH (fp->type);
13653 }
f41f5e61
PA
13654 SET_FIELD_BITPOS (*fp,
13655 (FIELD_BITPOS (*fp)
13656 + anonymous_size * bits_per_byte
13657 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
13658 }
13659 }
da5b30da
AA
13660 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13661 if (attr != NULL)
13662 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
13663 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
13664
13665 /* Get name of field. */
39cbfefa
DJ
13666 fieldname = dwarf2_name (die, cu);
13667 if (fieldname == NULL)
13668 fieldname = "";
d8151005
DJ
13669
13670 /* The name is already allocated along with this objfile, so we don't
13671 need to duplicate it for the type. */
13672 fp->name = fieldname;
c906108c
SS
13673
13674 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 13675 pointer or virtual base class pointer) to private. */
e142c38c 13676 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 13677 {
d48cc9dd 13678 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
13679 new_field->accessibility = DW_ACCESS_private;
13680 fip->non_public_fields = 1;
13681 }
13682 }
a9a9bd0f 13683 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 13684 {
a9a9bd0f
DC
13685 /* C++ static member. */
13686
13687 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13688 is a declaration, but all versions of G++ as of this writing
13689 (so through at least 3.2.1) incorrectly generate
13690 DW_TAG_variable tags. */
6e70227d 13691
ff355380 13692 const char *physname;
c906108c 13693
a9a9bd0f 13694 /* Get name of field. */
39cbfefa
DJ
13695 fieldname = dwarf2_name (die, cu);
13696 if (fieldname == NULL)
c906108c
SS
13697 return;
13698
254e6b9e 13699 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
13700 if (attr
13701 /* Only create a symbol if this is an external value.
13702 new_symbol checks this and puts the value in the global symbol
13703 table, which we want. If it is not external, new_symbol
13704 will try to put the value in cu->list_in_scope which is wrong. */
13705 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
13706 {
13707 /* A static const member, not much different than an enum as far as
13708 we're concerned, except that we can support more types. */
13709 new_symbol (die, NULL, cu);
13710 }
13711
2df3850c 13712 /* Get physical name. */
ff355380 13713 physname = dwarf2_physname (fieldname, die, cu);
c906108c 13714
d8151005
DJ
13715 /* The name is already allocated along with this objfile, so we don't
13716 need to duplicate it for the type. */
13717 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 13718 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 13719 FIELD_NAME (*fp) = fieldname;
c906108c
SS
13720 }
13721 else if (die->tag == DW_TAG_inheritance)
13722 {
74ac6d43 13723 LONGEST offset;
d4b96c9a 13724
74ac6d43
TT
13725 /* C++ base class field. */
13726 if (handle_data_member_location (die, cu, &offset))
13727 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 13728 FIELD_BITSIZE (*fp) = 0;
e7c27a73 13729 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
13730 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
13731 fip->nbaseclasses++;
13732 }
13733}
13734
98751a41
JK
13735/* Add a typedef defined in the scope of the FIP's class. */
13736
13737static void
13738dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
13739 struct dwarf2_cu *cu)
6e70227d 13740{
98751a41 13741 struct typedef_field_list *new_field;
98751a41 13742 struct typedef_field *fp;
98751a41
JK
13743
13744 /* Allocate a new field list entry and link it in. */
8d749320 13745 new_field = XCNEW (struct typedef_field_list);
98751a41
JK
13746 make_cleanup (xfree, new_field);
13747
13748 gdb_assert (die->tag == DW_TAG_typedef);
13749
13750 fp = &new_field->field;
13751
13752 /* Get name of field. */
13753 fp->name = dwarf2_name (die, cu);
13754 if (fp->name == NULL)
13755 return;
13756
13757 fp->type = read_type_die (die, cu);
13758
c191a687
KS
13759 /* Save accessibility. */
13760 enum dwarf_access_attribute accessibility;
13761 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13762 if (attr != NULL)
13763 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
13764 else
13765 accessibility = dwarf2_default_access_attribute (die, cu);
13766 switch (accessibility)
13767 {
13768 case DW_ACCESS_public:
13769 /* The assumed value if neither private nor protected. */
13770 break;
13771 case DW_ACCESS_private:
13772 fp->is_private = 1;
13773 break;
13774 case DW_ACCESS_protected:
13775 fp->is_protected = 1;
13776 break;
13777 default:
37534686
KS
13778 complaint (&symfile_complaints,
13779 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
13780 }
13781
98751a41
JK
13782 new_field->next = fip->typedef_field_list;
13783 fip->typedef_field_list = new_field;
13784 fip->typedef_field_list_count++;
13785}
13786
c906108c
SS
13787/* Create the vector of fields, and attach it to the type. */
13788
13789static void
fba45db2 13790dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13791 struct dwarf2_cu *cu)
c906108c
SS
13792{
13793 int nfields = fip->nfields;
13794
13795 /* Record the field count, allocate space for the array of fields,
13796 and create blank accessibility bitfields if necessary. */
13797 TYPE_NFIELDS (type) = nfields;
13798 TYPE_FIELDS (type) = (struct field *)
13799 TYPE_ALLOC (type, sizeof (struct field) * nfields);
13800 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
13801
b4ba55a1 13802 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
13803 {
13804 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13805
13806 TYPE_FIELD_PRIVATE_BITS (type) =
13807 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13808 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13809
13810 TYPE_FIELD_PROTECTED_BITS (type) =
13811 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13812 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13813
774b6a14
TT
13814 TYPE_FIELD_IGNORE_BITS (type) =
13815 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13816 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
13817 }
13818
13819 /* If the type has baseclasses, allocate and clear a bit vector for
13820 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 13821 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
13822 {
13823 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 13824 unsigned char *pointer;
c906108c
SS
13825
13826 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 13827 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 13828 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
13829 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
13830 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
13831 }
13832
3e43a32a
MS
13833 /* Copy the saved-up fields into the field vector. Start from the head of
13834 the list, adding to the tail of the field array, so that they end up in
13835 the same order in the array in which they were added to the list. */
c906108c
SS
13836 while (nfields-- > 0)
13837 {
7d0ccb61
DJ
13838 struct nextfield *fieldp;
13839
13840 if (fip->fields)
13841 {
13842 fieldp = fip->fields;
13843 fip->fields = fieldp->next;
13844 }
13845 else
13846 {
13847 fieldp = fip->baseclasses;
13848 fip->baseclasses = fieldp->next;
13849 }
13850
13851 TYPE_FIELD (type, nfields) = fieldp->field;
13852 switch (fieldp->accessibility)
c906108c 13853 {
c5aa993b 13854 case DW_ACCESS_private:
b4ba55a1
JB
13855 if (cu->language != language_ada)
13856 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 13857 break;
c906108c 13858
c5aa993b 13859 case DW_ACCESS_protected:
b4ba55a1
JB
13860 if (cu->language != language_ada)
13861 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 13862 break;
c906108c 13863
c5aa993b
JM
13864 case DW_ACCESS_public:
13865 break;
c906108c 13866
c5aa993b
JM
13867 default:
13868 /* Unknown accessibility. Complain and treat it as public. */
13869 {
e2e0b3e5 13870 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 13871 fieldp->accessibility);
c5aa993b
JM
13872 }
13873 break;
c906108c
SS
13874 }
13875 if (nfields < fip->nbaseclasses)
13876 {
7d0ccb61 13877 switch (fieldp->virtuality)
c906108c 13878 {
c5aa993b
JM
13879 case DW_VIRTUALITY_virtual:
13880 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 13881 if (cu->language == language_ada)
a73c6dcd 13882 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
13883 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13884 break;
c906108c
SS
13885 }
13886 }
c906108c
SS
13887 }
13888}
13889
7d27a96d
TT
13890/* Return true if this member function is a constructor, false
13891 otherwise. */
13892
13893static int
13894dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13895{
13896 const char *fieldname;
fe978cb0 13897 const char *type_name;
7d27a96d
TT
13898 int len;
13899
13900 if (die->parent == NULL)
13901 return 0;
13902
13903 if (die->parent->tag != DW_TAG_structure_type
13904 && die->parent->tag != DW_TAG_union_type
13905 && die->parent->tag != DW_TAG_class_type)
13906 return 0;
13907
13908 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
13909 type_name = dwarf2_name (die->parent, cu);
13910 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
13911 return 0;
13912
13913 len = strlen (fieldname);
fe978cb0
PA
13914 return (strncmp (fieldname, type_name, len) == 0
13915 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
13916}
13917
c906108c
SS
13918/* Add a member function to the proper fieldlist. */
13919
13920static void
107d2387 13921dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 13922 struct type *type, struct dwarf2_cu *cu)
c906108c 13923{
e7c27a73 13924 struct objfile *objfile = cu->objfile;
c906108c
SS
13925 struct attribute *attr;
13926 struct fnfieldlist *flp;
13927 int i;
13928 struct fn_field *fnp;
15d034d0 13929 const char *fieldname;
c906108c 13930 struct nextfnfield *new_fnfield;
f792889a 13931 struct type *this_type;
60d5a603 13932 enum dwarf_access_attribute accessibility;
c906108c 13933
b4ba55a1 13934 if (cu->language == language_ada)
a73c6dcd 13935 error (_("unexpected member function in Ada type"));
b4ba55a1 13936
2df3850c 13937 /* Get name of member function. */
39cbfefa
DJ
13938 fieldname = dwarf2_name (die, cu);
13939 if (fieldname == NULL)
2df3850c 13940 return;
c906108c 13941
c906108c
SS
13942 /* Look up member function name in fieldlist. */
13943 for (i = 0; i < fip->nfnfields; i++)
13944 {
27bfe10e 13945 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
13946 break;
13947 }
13948
13949 /* Create new list element if necessary. */
13950 if (i < fip->nfnfields)
13951 flp = &fip->fnfieldlists[i];
13952 else
13953 {
13954 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13955 {
13956 fip->fnfieldlists = (struct fnfieldlist *)
13957 xrealloc (fip->fnfieldlists,
13958 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13959 * sizeof (struct fnfieldlist));
c906108c 13960 if (fip->nfnfields == 0)
c13c43fd 13961 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
13962 }
13963 flp = &fip->fnfieldlists[fip->nfnfields];
13964 flp->name = fieldname;
13965 flp->length = 0;
13966 flp->head = NULL;
3da10d80 13967 i = fip->nfnfields++;
c906108c
SS
13968 }
13969
13970 /* Create a new member function field and chain it to the field list
0963b4bd 13971 entry. */
8d749320 13972 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 13973 make_cleanup (xfree, new_fnfield);
c906108c
SS
13974 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13975 new_fnfield->next = flp->head;
13976 flp->head = new_fnfield;
13977 flp->length++;
13978
13979 /* Fill in the member function field info. */
13980 fnp = &new_fnfield->fnfield;
3da10d80
KS
13981
13982 /* Delay processing of the physname until later. */
9c37b5ae 13983 if (cu->language == language_cplus)
3da10d80
KS
13984 {
13985 add_to_method_list (type, i, flp->length - 1, fieldname,
13986 die, cu);
13987 }
13988 else
13989 {
1d06ead6 13990 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
13991 fnp->physname = physname ? physname : "";
13992 }
13993
c906108c 13994 fnp->type = alloc_type (objfile);
f792889a
DJ
13995 this_type = read_type_die (die, cu);
13996 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 13997 {
f792889a 13998 int nparams = TYPE_NFIELDS (this_type);
c906108c 13999
f792889a 14000 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14001 of the method itself (TYPE_CODE_METHOD). */
14002 smash_to_method_type (fnp->type, type,
f792889a
DJ
14003 TYPE_TARGET_TYPE (this_type),
14004 TYPE_FIELDS (this_type),
14005 TYPE_NFIELDS (this_type),
14006 TYPE_VARARGS (this_type));
c906108c
SS
14007
14008 /* Handle static member functions.
c5aa993b 14009 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
14010 member functions. G++ helps GDB by marking the first
14011 parameter for non-static member functions (which is the this
14012 pointer) as artificial. We obtain this information from
14013 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 14014 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
14015 fnp->voffset = VOFFSET_STATIC;
14016 }
14017 else
e2e0b3e5 14018 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 14019 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
14020
14021 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 14022 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 14023 fnp->fcontext = die_containing_type (die, cu);
c906108c 14024
3e43a32a
MS
14025 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14026 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
14027
14028 /* Get accessibility. */
e142c38c 14029 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 14030 if (attr)
aead7601 14031 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
14032 else
14033 accessibility = dwarf2_default_access_attribute (die, cu);
14034 switch (accessibility)
c906108c 14035 {
60d5a603
JK
14036 case DW_ACCESS_private:
14037 fnp->is_private = 1;
14038 break;
14039 case DW_ACCESS_protected:
14040 fnp->is_protected = 1;
14041 break;
c906108c
SS
14042 }
14043
b02dede2 14044 /* Check for artificial methods. */
e142c38c 14045 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
14046 if (attr && DW_UNSND (attr) != 0)
14047 fnp->is_artificial = 1;
14048
7d27a96d
TT
14049 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14050
0d564a31 14051 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
14052 function. For older versions of GCC, this is an offset in the
14053 appropriate virtual table, as specified by DW_AT_containing_type.
14054 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
14055 to the object address. */
14056
e142c38c 14057 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 14058 if (attr)
8e19ed76 14059 {
aec5aa8b 14060 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 14061 {
aec5aa8b
TT
14062 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14063 {
14064 /* Old-style GCC. */
14065 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14066 }
14067 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14068 || (DW_BLOCK (attr)->size > 1
14069 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14070 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14071 {
aec5aa8b
TT
14072 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14073 if ((fnp->voffset % cu->header.addr_size) != 0)
14074 dwarf2_complex_location_expr_complaint ();
14075 else
14076 fnp->voffset /= cu->header.addr_size;
14077 fnp->voffset += 2;
14078 }
14079 else
14080 dwarf2_complex_location_expr_complaint ();
14081
14082 if (!fnp->fcontext)
7e993ebf
KS
14083 {
14084 /* If there is no `this' field and no DW_AT_containing_type,
14085 we cannot actually find a base class context for the
14086 vtable! */
14087 if (TYPE_NFIELDS (this_type) == 0
14088 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14089 {
14090 complaint (&symfile_complaints,
14091 _("cannot determine context for virtual member "
14092 "function \"%s\" (offset %d)"),
9c541725 14093 fieldname, to_underlying (die->sect_off));
7e993ebf
KS
14094 }
14095 else
14096 {
14097 fnp->fcontext
14098 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14099 }
14100 }
aec5aa8b 14101 }
3690dd37 14102 else if (attr_form_is_section_offset (attr))
8e19ed76 14103 {
4d3c2250 14104 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14105 }
14106 else
14107 {
4d3c2250
KB
14108 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14109 fieldname);
8e19ed76 14110 }
0d564a31 14111 }
d48cc9dd
DJ
14112 else
14113 {
14114 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14115 if (attr && DW_UNSND (attr))
14116 {
14117 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14118 complaint (&symfile_complaints,
3e43a32a
MS
14119 _("Member function \"%s\" (offset %d) is virtual "
14120 "but the vtable offset is not specified"),
9c541725 14121 fieldname, to_underlying (die->sect_off));
9655fd1a 14122 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
14123 TYPE_CPLUS_DYNAMIC (type) = 1;
14124 }
14125 }
c906108c
SS
14126}
14127
14128/* Create the vector of member function fields, and attach it to the type. */
14129
14130static void
fba45db2 14131dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14132 struct dwarf2_cu *cu)
c906108c
SS
14133{
14134 struct fnfieldlist *flp;
c906108c
SS
14135 int i;
14136
b4ba55a1 14137 if (cu->language == language_ada)
a73c6dcd 14138 error (_("unexpected member functions in Ada type"));
b4ba55a1 14139
c906108c
SS
14140 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14141 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14142 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
14143
14144 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
14145 {
14146 struct nextfnfield *nfp = flp->head;
14147 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14148 int k;
14149
14150 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
14151 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
14152 fn_flp->fn_fields = (struct fn_field *)
14153 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
14154 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 14155 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
14156 }
14157
14158 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
14159}
14160
1168df01
JB
14161/* Returns non-zero if NAME is the name of a vtable member in CU's
14162 language, zero otherwise. */
14163static int
14164is_vtable_name (const char *name, struct dwarf2_cu *cu)
14165{
14166 static const char vptr[] = "_vptr";
987504bb 14167 static const char vtable[] = "vtable";
1168df01 14168
9c37b5ae
TT
14169 /* Look for the C++ form of the vtable. */
14170 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
14171 return 1;
14172
14173 return 0;
14174}
14175
c0dd20ea 14176/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
14177 functions, with the ABI-specified layout. If TYPE describes
14178 such a structure, smash it into a member function type.
61049d3b
DJ
14179
14180 GCC shouldn't do this; it should just output pointer to member DIEs.
14181 This is GCC PR debug/28767. */
c0dd20ea 14182
0b92b5bb
TT
14183static void
14184quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 14185{
09e2d7c7 14186 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
14187
14188 /* Check for a structure with no name and two children. */
0b92b5bb
TT
14189 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14190 return;
c0dd20ea
DJ
14191
14192 /* Check for __pfn and __delta members. */
0b92b5bb
TT
14193 if (TYPE_FIELD_NAME (type, 0) == NULL
14194 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14195 || TYPE_FIELD_NAME (type, 1) == NULL
14196 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14197 return;
c0dd20ea
DJ
14198
14199 /* Find the type of the method. */
0b92b5bb 14200 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
14201 if (pfn_type == NULL
14202 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14203 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 14204 return;
c0dd20ea
DJ
14205
14206 /* Look for the "this" argument. */
14207 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14208 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 14209 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 14210 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 14211 return;
c0dd20ea 14212
09e2d7c7 14213 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 14214 new_type = alloc_type (objfile);
09e2d7c7 14215 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
14216 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14217 TYPE_VARARGS (pfn_type));
0b92b5bb 14218 smash_to_methodptr_type (type, new_type);
c0dd20ea 14219}
1168df01 14220
685b1105 14221
c906108c 14222/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
14223 (definition) to create a type for the structure or union. Fill in
14224 the type's name and general properties; the members will not be
83655187
DE
14225 processed until process_structure_scope. A symbol table entry for
14226 the type will also not be done until process_structure_scope (assuming
14227 the type has a name).
c906108c 14228
c767944b
DJ
14229 NOTE: we need to call these functions regardless of whether or not the
14230 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 14231 structure or union. This gets the type entered into our set of
83655187 14232 user defined types. */
c906108c 14233
f792889a 14234static struct type *
134d01f1 14235read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14236{
e7c27a73 14237 struct objfile *objfile = cu->objfile;
c906108c
SS
14238 struct type *type;
14239 struct attribute *attr;
15d034d0 14240 const char *name;
c906108c 14241
348e048f
DE
14242 /* If the definition of this type lives in .debug_types, read that type.
14243 Don't follow DW_AT_specification though, that will take us back up
14244 the chain and we want to go down. */
45e58e77 14245 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
14246 if (attr)
14247 {
ac9ec31b 14248 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 14249
ac9ec31b 14250 /* The type's CU may not be the same as CU.
02142a6c 14251 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
14252 return set_die_type (die, type, cu);
14253 }
14254
c0dd20ea 14255 type = alloc_type (objfile);
c906108c 14256 INIT_CPLUS_SPECIFIC (type);
93311388 14257
39cbfefa
DJ
14258 name = dwarf2_name (die, cu);
14259 if (name != NULL)
c906108c 14260 {
987504bb 14261 if (cu->language == language_cplus
c44af4eb
TT
14262 || cu->language == language_d
14263 || cu->language == language_rust)
63d06c5c 14264 {
15d034d0 14265 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
14266
14267 /* dwarf2_full_name might have already finished building the DIE's
14268 type. If so, there is no need to continue. */
14269 if (get_die_type (die, cu) != NULL)
14270 return get_die_type (die, cu);
14271
14272 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
14273 if (die->tag == DW_TAG_structure_type
14274 || die->tag == DW_TAG_class_type)
14275 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
14276 }
14277 else
14278 {
d8151005
DJ
14279 /* The name is already allocated along with this objfile, so
14280 we don't need to duplicate it for the type. */
7d455152 14281 TYPE_TAG_NAME (type) = name;
94af9270
KS
14282 if (die->tag == DW_TAG_class_type)
14283 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 14284 }
c906108c
SS
14285 }
14286
14287 if (die->tag == DW_TAG_structure_type)
14288 {
14289 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14290 }
14291 else if (die->tag == DW_TAG_union_type)
14292 {
14293 TYPE_CODE (type) = TYPE_CODE_UNION;
14294 }
14295 else
14296 {
4753d33b 14297 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
14298 }
14299
0cc2414c
TT
14300 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
14301 TYPE_DECLARED_CLASS (type) = 1;
14302
e142c38c 14303 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14304 if (attr)
14305 {
155bfbd3
JB
14306 if (attr_form_is_constant (attr))
14307 TYPE_LENGTH (type) = DW_UNSND (attr);
14308 else
14309 {
14310 /* For the moment, dynamic type sizes are not supported
14311 by GDB's struct type. The actual size is determined
14312 on-demand when resolving the type of a given object,
14313 so set the type's length to zero for now. Otherwise,
14314 we record an expression as the length, and that expression
14315 could lead to a very large value, which could eventually
14316 lead to us trying to allocate that much memory when creating
14317 a value of that type. */
14318 TYPE_LENGTH (type) = 0;
14319 }
c906108c
SS
14320 }
14321 else
14322 {
14323 TYPE_LENGTH (type) = 0;
14324 }
14325
5230b05a 14326 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 14327 {
5230b05a
WT
14328 /* ICC<14 does not output the required DW_AT_declaration on
14329 incomplete types, but gives them a size of zero. */
422b1cb0 14330 TYPE_STUB (type) = 1;
685b1105
JK
14331 }
14332 else
14333 TYPE_STUB_SUPPORTED (type) = 1;
14334
dc718098 14335 if (die_is_declaration (die, cu))
876cecd0 14336 TYPE_STUB (type) = 1;
a6c727b2
DJ
14337 else if (attr == NULL && die->child == NULL
14338 && producer_is_realview (cu->producer))
14339 /* RealView does not output the required DW_AT_declaration
14340 on incomplete types. */
14341 TYPE_STUB (type) = 1;
dc718098 14342
c906108c
SS
14343 /* We need to add the type field to the die immediately so we don't
14344 infinitely recurse when dealing with pointers to the structure
0963b4bd 14345 type within the structure itself. */
1c379e20 14346 set_die_type (die, type, cu);
c906108c 14347
7e314c57
JK
14348 /* set_die_type should be already done. */
14349 set_descriptive_type (type, die, cu);
14350
c767944b
DJ
14351 return type;
14352}
14353
14354/* Finish creating a structure or union type, including filling in
14355 its members and creating a symbol for it. */
14356
14357static void
14358process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
14359{
14360 struct objfile *objfile = cu->objfile;
ca040673 14361 struct die_info *child_die;
c767944b
DJ
14362 struct type *type;
14363
14364 type = get_die_type (die, cu);
14365 if (type == NULL)
14366 type = read_structure_type (die, cu);
14367
e142c38c 14368 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
14369 {
14370 struct field_info fi;
2f4732b0 14371 std::vector<struct symbol *> template_args;
c767944b 14372 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
14373
14374 memset (&fi, 0, sizeof (struct field_info));
14375
639d11d3 14376 child_die = die->child;
c906108c
SS
14377
14378 while (child_die && child_die->tag)
14379 {
a9a9bd0f
DC
14380 if (child_die->tag == DW_TAG_member
14381 || child_die->tag == DW_TAG_variable)
c906108c 14382 {
a9a9bd0f
DC
14383 /* NOTE: carlton/2002-11-05: A C++ static data member
14384 should be a DW_TAG_member that is a declaration, but
14385 all versions of G++ as of this writing (so through at
14386 least 3.2.1) incorrectly generate DW_TAG_variable
14387 tags for them instead. */
e7c27a73 14388 dwarf2_add_field (&fi, child_die, cu);
c906108c 14389 }
8713b1b1 14390 else if (child_die->tag == DW_TAG_subprogram)
c906108c 14391 {
e98c9e7c
TT
14392 /* Rust doesn't have member functions in the C++ sense.
14393 However, it does emit ordinary functions as children
14394 of a struct DIE. */
14395 if (cu->language == language_rust)
14396 read_func_scope (child_die, cu);
14397 else
14398 {
14399 /* C++ member function. */
14400 dwarf2_add_member_fn (&fi, child_die, type, cu);
14401 }
c906108c
SS
14402 }
14403 else if (child_die->tag == DW_TAG_inheritance)
14404 {
14405 /* C++ base class field. */
e7c27a73 14406 dwarf2_add_field (&fi, child_die, cu);
c906108c 14407 }
98751a41
JK
14408 else if (child_die->tag == DW_TAG_typedef)
14409 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
14410 else if (child_die->tag == DW_TAG_template_type_param
14411 || child_die->tag == DW_TAG_template_value_param)
14412 {
14413 struct symbol *arg = new_symbol (child_die, NULL, cu);
14414
f1078f66 14415 if (arg != NULL)
2f4732b0 14416 template_args.push_back (arg);
34eaf542
TT
14417 }
14418
c906108c
SS
14419 child_die = sibling_die (child_die);
14420 }
14421
34eaf542 14422 /* Attach template arguments to type. */
2f4732b0 14423 if (!template_args.empty ())
34eaf542
TT
14424 {
14425 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 14426 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 14427 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
14428 = XOBNEWVEC (&objfile->objfile_obstack,
14429 struct symbol *,
14430 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 14431 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 14432 template_args.data (),
34eaf542
TT
14433 (TYPE_N_TEMPLATE_ARGUMENTS (type)
14434 * sizeof (struct symbol *)));
34eaf542
TT
14435 }
14436
c906108c
SS
14437 /* Attach fields and member functions to the type. */
14438 if (fi.nfields)
e7c27a73 14439 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
14440 if (fi.nfnfields)
14441 {
e7c27a73 14442 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 14443
c5aa993b 14444 /* Get the type which refers to the base class (possibly this
c906108c 14445 class itself) which contains the vtable pointer for the current
0d564a31
DJ
14446 class from the DW_AT_containing_type attribute. This use of
14447 DW_AT_containing_type is a GNU extension. */
c906108c 14448
e142c38c 14449 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 14450 {
e7c27a73 14451 struct type *t = die_containing_type (die, cu);
c906108c 14452
ae6ae975 14453 set_type_vptr_basetype (type, t);
c906108c
SS
14454 if (type == t)
14455 {
c906108c
SS
14456 int i;
14457
14458 /* Our own class provides vtbl ptr. */
14459 for (i = TYPE_NFIELDS (t) - 1;
14460 i >= TYPE_N_BASECLASSES (t);
14461 --i)
14462 {
0d5cff50 14463 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 14464
1168df01 14465 if (is_vtable_name (fieldname, cu))
c906108c 14466 {
ae6ae975 14467 set_type_vptr_fieldno (type, i);
c906108c
SS
14468 break;
14469 }
14470 }
14471
14472 /* Complain if virtual function table field not found. */
14473 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 14474 complaint (&symfile_complaints,
3e43a32a
MS
14475 _("virtual function table pointer "
14476 "not found when defining class '%s'"),
4d3c2250
KB
14477 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
14478 "");
c906108c
SS
14479 }
14480 else
14481 {
ae6ae975 14482 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
14483 }
14484 }
f6235d4c 14485 else if (cu->producer
61012eef 14486 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
14487 {
14488 /* The IBM XLC compiler does not provide direct indication
14489 of the containing type, but the vtable pointer is
14490 always named __vfp. */
14491
14492 int i;
14493
14494 for (i = TYPE_NFIELDS (type) - 1;
14495 i >= TYPE_N_BASECLASSES (type);
14496 --i)
14497 {
14498 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
14499 {
ae6ae975
DE
14500 set_type_vptr_fieldno (type, i);
14501 set_type_vptr_basetype (type, type);
f6235d4c
EZ
14502 break;
14503 }
14504 }
14505 }
c906108c 14506 }
98751a41
JK
14507
14508 /* Copy fi.typedef_field_list linked list elements content into the
14509 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
14510 if (fi.typedef_field_list)
14511 {
14512 int i = fi.typedef_field_list_count;
14513
a0d7a4ff 14514 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 14515 TYPE_TYPEDEF_FIELD_ARRAY (type)
224c3ddb
SM
14516 = ((struct typedef_field *)
14517 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
14518 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
14519
14520 /* Reverse the list order to keep the debug info elements order. */
14521 while (--i >= 0)
14522 {
14523 struct typedef_field *dest, *src;
6e70227d 14524
98751a41
JK
14525 dest = &TYPE_TYPEDEF_FIELD (type, i);
14526 src = &fi.typedef_field_list->field;
14527 fi.typedef_field_list = fi.typedef_field_list->next;
14528 *dest = *src;
14529 }
14530 }
c767944b
DJ
14531
14532 do_cleanups (back_to);
c906108c 14533 }
63d06c5c 14534
bb5ed363 14535 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 14536
90aeadfc
DC
14537 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
14538 snapshots) has been known to create a die giving a declaration
14539 for a class that has, as a child, a die giving a definition for a
14540 nested class. So we have to process our children even if the
14541 current die is a declaration. Normally, of course, a declaration
14542 won't have any children at all. */
134d01f1 14543
ca040673
DE
14544 child_die = die->child;
14545
90aeadfc
DC
14546 while (child_die != NULL && child_die->tag)
14547 {
14548 if (child_die->tag == DW_TAG_member
14549 || child_die->tag == DW_TAG_variable
34eaf542
TT
14550 || child_die->tag == DW_TAG_inheritance
14551 || child_die->tag == DW_TAG_template_value_param
14552 || child_die->tag == DW_TAG_template_type_param)
134d01f1 14553 {
90aeadfc 14554 /* Do nothing. */
134d01f1 14555 }
90aeadfc
DC
14556 else
14557 process_die (child_die, cu);
134d01f1 14558
90aeadfc 14559 child_die = sibling_die (child_die);
134d01f1
DJ
14560 }
14561
fa4028e9
JB
14562 /* Do not consider external references. According to the DWARF standard,
14563 these DIEs are identified by the fact that they have no byte_size
14564 attribute, and a declaration attribute. */
14565 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
14566 || !die_is_declaration (die, cu))
c767944b 14567 new_symbol (die, type, cu);
134d01f1
DJ
14568}
14569
55426c9d
JB
14570/* Assuming DIE is an enumeration type, and TYPE is its associated type,
14571 update TYPE using some information only available in DIE's children. */
14572
14573static void
14574update_enumeration_type_from_children (struct die_info *die,
14575 struct type *type,
14576 struct dwarf2_cu *cu)
14577{
60f7655a 14578 struct die_info *child_die;
55426c9d
JB
14579 int unsigned_enum = 1;
14580 int flag_enum = 1;
14581 ULONGEST mask = 0;
55426c9d 14582
8268c778 14583 auto_obstack obstack;
55426c9d 14584
60f7655a
DE
14585 for (child_die = die->child;
14586 child_die != NULL && child_die->tag;
14587 child_die = sibling_die (child_die))
55426c9d
JB
14588 {
14589 struct attribute *attr;
14590 LONGEST value;
14591 const gdb_byte *bytes;
14592 struct dwarf2_locexpr_baton *baton;
14593 const char *name;
60f7655a 14594
55426c9d
JB
14595 if (child_die->tag != DW_TAG_enumerator)
14596 continue;
14597
14598 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
14599 if (attr == NULL)
14600 continue;
14601
14602 name = dwarf2_name (child_die, cu);
14603 if (name == NULL)
14604 name = "<anonymous enumerator>";
14605
14606 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
14607 &value, &bytes, &baton);
14608 if (value < 0)
14609 {
14610 unsigned_enum = 0;
14611 flag_enum = 0;
14612 }
14613 else if ((mask & value) != 0)
14614 flag_enum = 0;
14615 else
14616 mask |= value;
14617
14618 /* If we already know that the enum type is neither unsigned, nor
14619 a flag type, no need to look at the rest of the enumerates. */
14620 if (!unsigned_enum && !flag_enum)
14621 break;
55426c9d
JB
14622 }
14623
14624 if (unsigned_enum)
14625 TYPE_UNSIGNED (type) = 1;
14626 if (flag_enum)
14627 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
14628}
14629
134d01f1
DJ
14630/* Given a DW_AT_enumeration_type die, set its type. We do not
14631 complete the type's fields yet, or create any symbols. */
c906108c 14632
f792889a 14633static struct type *
134d01f1 14634read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14635{
e7c27a73 14636 struct objfile *objfile = cu->objfile;
c906108c 14637 struct type *type;
c906108c 14638 struct attribute *attr;
0114d602 14639 const char *name;
134d01f1 14640
348e048f
DE
14641 /* If the definition of this type lives in .debug_types, read that type.
14642 Don't follow DW_AT_specification though, that will take us back up
14643 the chain and we want to go down. */
45e58e77 14644 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
14645 if (attr)
14646 {
ac9ec31b 14647 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 14648
ac9ec31b 14649 /* The type's CU may not be the same as CU.
02142a6c 14650 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
14651 return set_die_type (die, type, cu);
14652 }
14653
c906108c
SS
14654 type = alloc_type (objfile);
14655
14656 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 14657 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 14658 if (name != NULL)
7d455152 14659 TYPE_TAG_NAME (type) = name;
c906108c 14660
0626fc76
TT
14661 attr = dwarf2_attr (die, DW_AT_type, cu);
14662 if (attr != NULL)
14663 {
14664 struct type *underlying_type = die_type (die, cu);
14665
14666 TYPE_TARGET_TYPE (type) = underlying_type;
14667 }
14668
e142c38c 14669 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14670 if (attr)
14671 {
14672 TYPE_LENGTH (type) = DW_UNSND (attr);
14673 }
14674 else
14675 {
14676 TYPE_LENGTH (type) = 0;
14677 }
14678
137033e9
JB
14679 /* The enumeration DIE can be incomplete. In Ada, any type can be
14680 declared as private in the package spec, and then defined only
14681 inside the package body. Such types are known as Taft Amendment
14682 Types. When another package uses such a type, an incomplete DIE
14683 may be generated by the compiler. */
02eb380e 14684 if (die_is_declaration (die, cu))
876cecd0 14685 TYPE_STUB (type) = 1;
02eb380e 14686
0626fc76
TT
14687 /* Finish the creation of this type by using the enum's children.
14688 We must call this even when the underlying type has been provided
14689 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
14690 update_enumeration_type_from_children (die, type, cu);
14691
0626fc76
TT
14692 /* If this type has an underlying type that is not a stub, then we
14693 may use its attributes. We always use the "unsigned" attribute
14694 in this situation, because ordinarily we guess whether the type
14695 is unsigned -- but the guess can be wrong and the underlying type
14696 can tell us the reality. However, we defer to a local size
14697 attribute if one exists, because this lets the compiler override
14698 the underlying type if needed. */
14699 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
14700 {
14701 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
14702 if (TYPE_LENGTH (type) == 0)
14703 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
14704 }
14705
3d567982
TT
14706 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
14707
f792889a 14708 return set_die_type (die, type, cu);
134d01f1
DJ
14709}
14710
14711/* Given a pointer to a die which begins an enumeration, process all
14712 the dies that define the members of the enumeration, and create the
14713 symbol for the enumeration type.
14714
14715 NOTE: We reverse the order of the element list. */
14716
14717static void
14718process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
14719{
f792889a 14720 struct type *this_type;
134d01f1 14721
f792889a
DJ
14722 this_type = get_die_type (die, cu);
14723 if (this_type == NULL)
14724 this_type = read_enumeration_type (die, cu);
9dc481d3 14725
639d11d3 14726 if (die->child != NULL)
c906108c 14727 {
9dc481d3
DE
14728 struct die_info *child_die;
14729 struct symbol *sym;
14730 struct field *fields = NULL;
14731 int num_fields = 0;
15d034d0 14732 const char *name;
9dc481d3 14733
639d11d3 14734 child_die = die->child;
c906108c
SS
14735 while (child_die && child_die->tag)
14736 {
14737 if (child_die->tag != DW_TAG_enumerator)
14738 {
e7c27a73 14739 process_die (child_die, cu);
c906108c
SS
14740 }
14741 else
14742 {
39cbfefa
DJ
14743 name = dwarf2_name (child_die, cu);
14744 if (name)
c906108c 14745 {
f792889a 14746 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
14747
14748 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
14749 {
14750 fields = (struct field *)
14751 xrealloc (fields,
14752 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 14753 * sizeof (struct field));
c906108c
SS
14754 }
14755
3567439c 14756 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 14757 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 14758 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
14759 FIELD_BITSIZE (fields[num_fields]) = 0;
14760
14761 num_fields++;
14762 }
14763 }
14764
14765 child_die = sibling_die (child_die);
14766 }
14767
14768 if (num_fields)
14769 {
f792889a
DJ
14770 TYPE_NFIELDS (this_type) = num_fields;
14771 TYPE_FIELDS (this_type) = (struct field *)
14772 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
14773 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 14774 sizeof (struct field) * num_fields);
b8c9b27d 14775 xfree (fields);
c906108c 14776 }
c906108c 14777 }
134d01f1 14778
6c83ed52
TT
14779 /* If we are reading an enum from a .debug_types unit, and the enum
14780 is a declaration, and the enum is not the signatured type in the
14781 unit, then we do not want to add a symbol for it. Adding a
14782 symbol would in some cases obscure the true definition of the
14783 enum, giving users an incomplete type when the definition is
14784 actually available. Note that we do not want to do this for all
14785 enums which are just declarations, because C++0x allows forward
14786 enum declarations. */
3019eac3 14787 if (cu->per_cu->is_debug_types
6c83ed52
TT
14788 && die_is_declaration (die, cu))
14789 {
52dc124a 14790 struct signatured_type *sig_type;
6c83ed52 14791
c0f78cd4 14792 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
14793 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
14794 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
14795 return;
14796 }
14797
f792889a 14798 new_symbol (die, this_type, cu);
c906108c
SS
14799}
14800
14801/* Extract all information from a DW_TAG_array_type DIE and put it in
14802 the DIE's type field. For now, this only handles one dimensional
14803 arrays. */
14804
f792889a 14805static struct type *
e7c27a73 14806read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14807{
e7c27a73 14808 struct objfile *objfile = cu->objfile;
c906108c 14809 struct die_info *child_die;
7e314c57 14810 struct type *type;
c906108c 14811 struct type *element_type, *range_type, *index_type;
c906108c 14812 struct attribute *attr;
15d034d0 14813 const char *name;
dc53a7ad 14814 unsigned int bit_stride = 0;
c906108c 14815
e7c27a73 14816 element_type = die_type (die, cu);
c906108c 14817
7e314c57
JK
14818 /* The die_type call above may have already set the type for this DIE. */
14819 type = get_die_type (die, cu);
14820 if (type)
14821 return type;
14822
dc53a7ad
JB
14823 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
14824 if (attr != NULL)
14825 bit_stride = DW_UNSND (attr) * 8;
14826
14827 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
14828 if (attr != NULL)
14829 bit_stride = DW_UNSND (attr);
14830
c906108c
SS
14831 /* Irix 6.2 native cc creates array types without children for
14832 arrays with unspecified length. */
639d11d3 14833 if (die->child == NULL)
c906108c 14834 {
46bf5051 14835 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14836 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
14837 type = create_array_type_with_stride (NULL, element_type, range_type,
14838 bit_stride);
f792889a 14839 return set_die_type (die, type, cu);
c906108c
SS
14840 }
14841
791afaa2 14842 std::vector<struct type *> range_types;
639d11d3 14843 child_die = die->child;
c906108c
SS
14844 while (child_die && child_die->tag)
14845 {
14846 if (child_die->tag == DW_TAG_subrange_type)
14847 {
f792889a 14848 struct type *child_type = read_type_die (child_die, cu);
9a619af0 14849
f792889a 14850 if (child_type != NULL)
a02abb62 14851 {
0963b4bd
MS
14852 /* The range type was succesfully read. Save it for the
14853 array type creation. */
791afaa2 14854 range_types.push_back (child_type);
a02abb62 14855 }
c906108c
SS
14856 }
14857 child_die = sibling_die (child_die);
14858 }
14859
14860 /* Dwarf2 dimensions are output from left to right, create the
14861 necessary array types in backwards order. */
7ca2d3a3 14862
c906108c 14863 type = element_type;
7ca2d3a3
DL
14864
14865 if (read_array_order (die, cu) == DW_ORD_col_major)
14866 {
14867 int i = 0;
9a619af0 14868
791afaa2 14869 while (i < range_types.size ())
dc53a7ad
JB
14870 type = create_array_type_with_stride (NULL, type, range_types[i++],
14871 bit_stride);
7ca2d3a3
DL
14872 }
14873 else
14874 {
791afaa2 14875 size_t ndim = range_types.size ();
7ca2d3a3 14876 while (ndim-- > 0)
dc53a7ad
JB
14877 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14878 bit_stride);
7ca2d3a3 14879 }
c906108c 14880
f5f8a009
EZ
14881 /* Understand Dwarf2 support for vector types (like they occur on
14882 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14883 array type. This is not part of the Dwarf2/3 standard yet, but a
14884 custom vendor extension. The main difference between a regular
14885 array and the vector variant is that vectors are passed by value
14886 to functions. */
e142c38c 14887 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 14888 if (attr)
ea37ba09 14889 make_vector_type (type);
f5f8a009 14890
dbc98a8b
KW
14891 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14892 implementation may choose to implement triple vectors using this
14893 attribute. */
14894 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14895 if (attr)
14896 {
14897 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14898 TYPE_LENGTH (type) = DW_UNSND (attr);
14899 else
3e43a32a
MS
14900 complaint (&symfile_complaints,
14901 _("DW_AT_byte_size for array type smaller "
14902 "than the total size of elements"));
dbc98a8b
KW
14903 }
14904
39cbfefa
DJ
14905 name = dwarf2_name (die, cu);
14906 if (name)
14907 TYPE_NAME (type) = name;
6e70227d 14908
0963b4bd 14909 /* Install the type in the die. */
7e314c57
JK
14910 set_die_type (die, type, cu);
14911
14912 /* set_die_type should be already done. */
b4ba55a1
JB
14913 set_descriptive_type (type, die, cu);
14914
7e314c57 14915 return type;
c906108c
SS
14916}
14917
7ca2d3a3 14918static enum dwarf_array_dim_ordering
6e70227d 14919read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
14920{
14921 struct attribute *attr;
14922
14923 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14924
aead7601
SM
14925 if (attr)
14926 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 14927
0963b4bd
MS
14928 /* GNU F77 is a special case, as at 08/2004 array type info is the
14929 opposite order to the dwarf2 specification, but data is still
14930 laid out as per normal fortran.
7ca2d3a3 14931
0963b4bd
MS
14932 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14933 version checking. */
7ca2d3a3 14934
905e0470
PM
14935 if (cu->language == language_fortran
14936 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
14937 {
14938 return DW_ORD_row_major;
14939 }
14940
6e70227d 14941 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
14942 {
14943 case array_column_major:
14944 return DW_ORD_col_major;
14945 case array_row_major:
14946 default:
14947 return DW_ORD_row_major;
14948 };
14949}
14950
72019c9c 14951/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 14952 the DIE's type field. */
72019c9c 14953
f792889a 14954static struct type *
72019c9c
GM
14955read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14956{
7e314c57
JK
14957 struct type *domain_type, *set_type;
14958 struct attribute *attr;
f792889a 14959
7e314c57
JK
14960 domain_type = die_type (die, cu);
14961
14962 /* The die_type call above may have already set the type for this DIE. */
14963 set_type = get_die_type (die, cu);
14964 if (set_type)
14965 return set_type;
14966
14967 set_type = create_set_type (NULL, domain_type);
14968
14969 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
14970 if (attr)
14971 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 14972
f792889a 14973 return set_die_type (die, set_type, cu);
72019c9c 14974}
7ca2d3a3 14975
0971de02
TT
14976/* A helper for read_common_block that creates a locexpr baton.
14977 SYM is the symbol which we are marking as computed.
14978 COMMON_DIE is the DIE for the common block.
14979 COMMON_LOC is the location expression attribute for the common
14980 block itself.
14981 MEMBER_LOC is the location expression attribute for the particular
14982 member of the common block that we are processing.
14983 CU is the CU from which the above come. */
14984
14985static void
14986mark_common_block_symbol_computed (struct symbol *sym,
14987 struct die_info *common_die,
14988 struct attribute *common_loc,
14989 struct attribute *member_loc,
14990 struct dwarf2_cu *cu)
14991{
14992 struct objfile *objfile = dwarf2_per_objfile->objfile;
14993 struct dwarf2_locexpr_baton *baton;
14994 gdb_byte *ptr;
14995 unsigned int cu_off;
14996 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14997 LONGEST offset = 0;
14998
14999 gdb_assert (common_loc && member_loc);
15000 gdb_assert (attr_form_is_block (common_loc));
15001 gdb_assert (attr_form_is_block (member_loc)
15002 || attr_form_is_constant (member_loc));
15003
8d749320 15004 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
15005 baton->per_cu = cu->per_cu;
15006 gdb_assert (baton->per_cu);
15007
15008 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15009
15010 if (attr_form_is_constant (member_loc))
15011 {
15012 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15013 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15014 }
15015 else
15016 baton->size += DW_BLOCK (member_loc)->size;
15017
224c3ddb 15018 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
15019 baton->data = ptr;
15020
15021 *ptr++ = DW_OP_call4;
9c541725 15022 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
15023 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15024 ptr += 4;
15025
15026 if (attr_form_is_constant (member_loc))
15027 {
15028 *ptr++ = DW_OP_addr;
15029 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15030 ptr += cu->header.addr_size;
15031 }
15032 else
15033 {
15034 /* We have to copy the data here, because DW_OP_call4 will only
15035 use a DW_AT_location attribute. */
15036 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15037 ptr += DW_BLOCK (member_loc)->size;
15038 }
15039
15040 *ptr++ = DW_OP_plus;
15041 gdb_assert (ptr - baton->data == baton->size);
15042
0971de02 15043 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 15044 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
15045}
15046
4357ac6c
TT
15047/* Create appropriate locally-scoped variables for all the
15048 DW_TAG_common_block entries. Also create a struct common_block
15049 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15050 is used to sepate the common blocks name namespace from regular
15051 variable names. */
c906108c
SS
15052
15053static void
e7c27a73 15054read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15055{
0971de02
TT
15056 struct attribute *attr;
15057
15058 attr = dwarf2_attr (die, DW_AT_location, cu);
15059 if (attr)
15060 {
15061 /* Support the .debug_loc offsets. */
15062 if (attr_form_is_block (attr))
15063 {
15064 /* Ok. */
15065 }
15066 else if (attr_form_is_section_offset (attr))
15067 {
15068 dwarf2_complex_location_expr_complaint ();
15069 attr = NULL;
15070 }
15071 else
15072 {
15073 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15074 "common block member");
15075 attr = NULL;
15076 }
15077 }
15078
639d11d3 15079 if (die->child != NULL)
c906108c 15080 {
4357ac6c
TT
15081 struct objfile *objfile = cu->objfile;
15082 struct die_info *child_die;
15083 size_t n_entries = 0, size;
15084 struct common_block *common_block;
15085 struct symbol *sym;
74ac6d43 15086
4357ac6c
TT
15087 for (child_die = die->child;
15088 child_die && child_die->tag;
15089 child_die = sibling_die (child_die))
15090 ++n_entries;
15091
15092 size = (sizeof (struct common_block)
15093 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
15094 common_block
15095 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15096 size);
4357ac6c
TT
15097 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15098 common_block->n_entries = 0;
15099
15100 for (child_die = die->child;
15101 child_die && child_die->tag;
15102 child_die = sibling_die (child_die))
15103 {
15104 /* Create the symbol in the DW_TAG_common_block block in the current
15105 symbol scope. */
e7c27a73 15106 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
15107 if (sym != NULL)
15108 {
15109 struct attribute *member_loc;
15110
15111 common_block->contents[common_block->n_entries++] = sym;
15112
15113 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15114 cu);
15115 if (member_loc)
15116 {
15117 /* GDB has handled this for a long time, but it is
15118 not specified by DWARF. It seems to have been
15119 emitted by gfortran at least as recently as:
15120 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15121 complaint (&symfile_complaints,
15122 _("Variable in common block has "
15123 "DW_AT_data_member_location "
15124 "- DIE at 0x%x [in module %s]"),
9c541725 15125 to_underlying (child_die->sect_off),
4262abfb 15126 objfile_name (cu->objfile));
0971de02
TT
15127
15128 if (attr_form_is_section_offset (member_loc))
15129 dwarf2_complex_location_expr_complaint ();
15130 else if (attr_form_is_constant (member_loc)
15131 || attr_form_is_block (member_loc))
15132 {
15133 if (attr)
15134 mark_common_block_symbol_computed (sym, die, attr,
15135 member_loc, cu);
15136 }
15137 else
15138 dwarf2_complex_location_expr_complaint ();
15139 }
15140 }
c906108c 15141 }
4357ac6c
TT
15142
15143 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
15144 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
15145 }
15146}
15147
0114d602 15148/* Create a type for a C++ namespace. */
d9fa45fe 15149
0114d602
DJ
15150static struct type *
15151read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 15152{
e7c27a73 15153 struct objfile *objfile = cu->objfile;
0114d602 15154 const char *previous_prefix, *name;
9219021c 15155 int is_anonymous;
0114d602
DJ
15156 struct type *type;
15157
15158 /* For extensions, reuse the type of the original namespace. */
15159 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
15160 {
15161 struct die_info *ext_die;
15162 struct dwarf2_cu *ext_cu = cu;
9a619af0 15163
0114d602
DJ
15164 ext_die = dwarf2_extension (die, &ext_cu);
15165 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
15166
15167 /* EXT_CU may not be the same as CU.
02142a6c 15168 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
15169 return set_die_type (die, type, cu);
15170 }
9219021c 15171
e142c38c 15172 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
15173
15174 /* Now build the name of the current namespace. */
15175
0114d602
DJ
15176 previous_prefix = determine_prefix (die, cu);
15177 if (previous_prefix[0] != '\0')
15178 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 15179 previous_prefix, name, 0, cu);
0114d602
DJ
15180
15181 /* Create the type. */
19f392bc 15182 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
15183 TYPE_TAG_NAME (type) = TYPE_NAME (type);
15184
60531b24 15185 return set_die_type (die, type, cu);
0114d602
DJ
15186}
15187
22cee43f 15188/* Read a namespace scope. */
0114d602
DJ
15189
15190static void
15191read_namespace (struct die_info *die, struct dwarf2_cu *cu)
15192{
15193 struct objfile *objfile = cu->objfile;
0114d602 15194 int is_anonymous;
9219021c 15195
5c4e30ca
DC
15196 /* Add a symbol associated to this if we haven't seen the namespace
15197 before. Also, add a using directive if it's an anonymous
15198 namespace. */
9219021c 15199
f2f0e013 15200 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
15201 {
15202 struct type *type;
15203
0114d602 15204 type = read_type_die (die, cu);
e7c27a73 15205 new_symbol (die, type, cu);
5c4e30ca 15206
e8e80198 15207 namespace_name (die, &is_anonymous, cu);
5c4e30ca 15208 if (is_anonymous)
0114d602
DJ
15209 {
15210 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 15211
eb1e02fd 15212 std::vector<const char *> excludes;
22cee43f
PMR
15213 add_using_directive (using_directives (cu->language),
15214 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 15215 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 15216 }
5c4e30ca 15217 }
9219021c 15218
639d11d3 15219 if (die->child != NULL)
d9fa45fe 15220 {
639d11d3 15221 struct die_info *child_die = die->child;
6e70227d 15222
d9fa45fe
DC
15223 while (child_die && child_die->tag)
15224 {
e7c27a73 15225 process_die (child_die, cu);
d9fa45fe
DC
15226 child_die = sibling_die (child_die);
15227 }
15228 }
38d518c9
EZ
15229}
15230
f55ee35c
JK
15231/* Read a Fortran module as type. This DIE can be only a declaration used for
15232 imported module. Still we need that type as local Fortran "use ... only"
15233 declaration imports depend on the created type in determine_prefix. */
15234
15235static struct type *
15236read_module_type (struct die_info *die, struct dwarf2_cu *cu)
15237{
15238 struct objfile *objfile = cu->objfile;
15d034d0 15239 const char *module_name;
f55ee35c
JK
15240 struct type *type;
15241
15242 module_name = dwarf2_name (die, cu);
15243 if (!module_name)
3e43a32a
MS
15244 complaint (&symfile_complaints,
15245 _("DW_TAG_module has no name, offset 0x%x"),
9c541725 15246 to_underlying (die->sect_off));
19f392bc 15247 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
15248
15249 /* determine_prefix uses TYPE_TAG_NAME. */
15250 TYPE_TAG_NAME (type) = TYPE_NAME (type);
15251
15252 return set_die_type (die, type, cu);
15253}
15254
5d7cb8df
JK
15255/* Read a Fortran module. */
15256
15257static void
15258read_module (struct die_info *die, struct dwarf2_cu *cu)
15259{
15260 struct die_info *child_die = die->child;
530e8392
KB
15261 struct type *type;
15262
15263 type = read_type_die (die, cu);
15264 new_symbol (die, type, cu);
5d7cb8df 15265
5d7cb8df
JK
15266 while (child_die && child_die->tag)
15267 {
15268 process_die (child_die, cu);
15269 child_die = sibling_die (child_die);
15270 }
15271}
15272
38d518c9
EZ
15273/* Return the name of the namespace represented by DIE. Set
15274 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
15275 namespace. */
15276
15277static const char *
e142c38c 15278namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
15279{
15280 struct die_info *current_die;
15281 const char *name = NULL;
15282
15283 /* Loop through the extensions until we find a name. */
15284
15285 for (current_die = die;
15286 current_die != NULL;
f2f0e013 15287 current_die = dwarf2_extension (die, &cu))
38d518c9 15288 {
96553a0c
DE
15289 /* We don't use dwarf2_name here so that we can detect the absence
15290 of a name -> anonymous namespace. */
7d45c7c3 15291 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 15292
38d518c9
EZ
15293 if (name != NULL)
15294 break;
15295 }
15296
15297 /* Is it an anonymous namespace? */
15298
15299 *is_anonymous = (name == NULL);
15300 if (*is_anonymous)
2b1dbab0 15301 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
15302
15303 return name;
d9fa45fe
DC
15304}
15305
c906108c
SS
15306/* Extract all information from a DW_TAG_pointer_type DIE and add to
15307 the user defined type vector. */
15308
f792889a 15309static struct type *
e7c27a73 15310read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15311{
5e2b427d 15312 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 15313 struct comp_unit_head *cu_header = &cu->header;
c906108c 15314 struct type *type;
8b2dbe47
KB
15315 struct attribute *attr_byte_size;
15316 struct attribute *attr_address_class;
15317 int byte_size, addr_class;
7e314c57
JK
15318 struct type *target_type;
15319
15320 target_type = die_type (die, cu);
c906108c 15321
7e314c57
JK
15322 /* The die_type call above may have already set the type for this DIE. */
15323 type = get_die_type (die, cu);
15324 if (type)
15325 return type;
15326
15327 type = lookup_pointer_type (target_type);
8b2dbe47 15328
e142c38c 15329 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
15330 if (attr_byte_size)
15331 byte_size = DW_UNSND (attr_byte_size);
c906108c 15332 else
8b2dbe47
KB
15333 byte_size = cu_header->addr_size;
15334
e142c38c 15335 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
15336 if (attr_address_class)
15337 addr_class = DW_UNSND (attr_address_class);
15338 else
15339 addr_class = DW_ADDR_none;
15340
15341 /* If the pointer size or address class is different than the
15342 default, create a type variant marked as such and set the
15343 length accordingly. */
15344 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 15345 {
5e2b427d 15346 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
15347 {
15348 int type_flags;
15349
849957d9 15350 type_flags = gdbarch_address_class_type_flags
5e2b427d 15351 (gdbarch, byte_size, addr_class);
876cecd0
TT
15352 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
15353 == 0);
8b2dbe47
KB
15354 type = make_type_with_address_space (type, type_flags);
15355 }
15356 else if (TYPE_LENGTH (type) != byte_size)
15357 {
3e43a32a
MS
15358 complaint (&symfile_complaints,
15359 _("invalid pointer size %d"), byte_size);
8b2dbe47 15360 }
6e70227d 15361 else
9a619af0
MS
15362 {
15363 /* Should we also complain about unhandled address classes? */
15364 }
c906108c 15365 }
8b2dbe47
KB
15366
15367 TYPE_LENGTH (type) = byte_size;
f792889a 15368 return set_die_type (die, type, cu);
c906108c
SS
15369}
15370
15371/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
15372 the user defined type vector. */
15373
f792889a 15374static struct type *
e7c27a73 15375read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
15376{
15377 struct type *type;
15378 struct type *to_type;
15379 struct type *domain;
15380
e7c27a73
DJ
15381 to_type = die_type (die, cu);
15382 domain = die_containing_type (die, cu);
0d5de010 15383
7e314c57
JK
15384 /* The calls above may have already set the type for this DIE. */
15385 type = get_die_type (die, cu);
15386 if (type)
15387 return type;
15388
0d5de010
DJ
15389 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
15390 type = lookup_methodptr_type (to_type);
7078baeb
TT
15391 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
15392 {
15393 struct type *new_type = alloc_type (cu->objfile);
15394
15395 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
15396 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
15397 TYPE_VARARGS (to_type));
15398 type = lookup_methodptr_type (new_type);
15399 }
0d5de010
DJ
15400 else
15401 type = lookup_memberptr_type (to_type, domain);
c906108c 15402
f792889a 15403 return set_die_type (die, type, cu);
c906108c
SS
15404}
15405
4297a3f0 15406/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
15407 the user defined type vector. */
15408
f792889a 15409static struct type *
4297a3f0
AV
15410read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
15411 enum type_code refcode)
c906108c 15412{
e7c27a73 15413 struct comp_unit_head *cu_header = &cu->header;
7e314c57 15414 struct type *type, *target_type;
c906108c
SS
15415 struct attribute *attr;
15416
4297a3f0
AV
15417 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
15418
7e314c57
JK
15419 target_type = die_type (die, cu);
15420
15421 /* The die_type call above may have already set the type for this DIE. */
15422 type = get_die_type (die, cu);
15423 if (type)
15424 return type;
15425
4297a3f0 15426 type = lookup_reference_type (target_type, refcode);
e142c38c 15427 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15428 if (attr)
15429 {
15430 TYPE_LENGTH (type) = DW_UNSND (attr);
15431 }
15432 else
15433 {
107d2387 15434 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 15435 }
f792889a 15436 return set_die_type (die, type, cu);
c906108c
SS
15437}
15438
cf363f18
MW
15439/* Add the given cv-qualifiers to the element type of the array. GCC
15440 outputs DWARF type qualifiers that apply to an array, not the
15441 element type. But GDB relies on the array element type to carry
15442 the cv-qualifiers. This mimics section 6.7.3 of the C99
15443 specification. */
15444
15445static struct type *
15446add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
15447 struct type *base_type, int cnst, int voltl)
15448{
15449 struct type *el_type, *inner_array;
15450
15451 base_type = copy_type (base_type);
15452 inner_array = base_type;
15453
15454 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
15455 {
15456 TYPE_TARGET_TYPE (inner_array) =
15457 copy_type (TYPE_TARGET_TYPE (inner_array));
15458 inner_array = TYPE_TARGET_TYPE (inner_array);
15459 }
15460
15461 el_type = TYPE_TARGET_TYPE (inner_array);
15462 cnst |= TYPE_CONST (el_type);
15463 voltl |= TYPE_VOLATILE (el_type);
15464 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
15465
15466 return set_die_type (die, base_type, cu);
15467}
15468
f792889a 15469static struct type *
e7c27a73 15470read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15471{
f792889a 15472 struct type *base_type, *cv_type;
c906108c 15473
e7c27a73 15474 base_type = die_type (die, cu);
7e314c57
JK
15475
15476 /* The die_type call above may have already set the type for this DIE. */
15477 cv_type = get_die_type (die, cu);
15478 if (cv_type)
15479 return cv_type;
15480
2f608a3a
KW
15481 /* In case the const qualifier is applied to an array type, the element type
15482 is so qualified, not the array type (section 6.7.3 of C99). */
15483 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 15484 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 15485
f792889a
DJ
15486 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
15487 return set_die_type (die, cv_type, cu);
c906108c
SS
15488}
15489
f792889a 15490static struct type *
e7c27a73 15491read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15492{
f792889a 15493 struct type *base_type, *cv_type;
c906108c 15494
e7c27a73 15495 base_type = die_type (die, cu);
7e314c57
JK
15496
15497 /* The die_type call above may have already set the type for this DIE. */
15498 cv_type = get_die_type (die, cu);
15499 if (cv_type)
15500 return cv_type;
15501
cf363f18
MW
15502 /* In case the volatile qualifier is applied to an array type, the
15503 element type is so qualified, not the array type (section 6.7.3
15504 of C99). */
15505 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
15506 return add_array_cv_type (die, cu, base_type, 0, 1);
15507
f792889a
DJ
15508 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
15509 return set_die_type (die, cv_type, cu);
c906108c
SS
15510}
15511
06d66ee9
TT
15512/* Handle DW_TAG_restrict_type. */
15513
15514static struct type *
15515read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
15516{
15517 struct type *base_type, *cv_type;
15518
15519 base_type = die_type (die, cu);
15520
15521 /* The die_type call above may have already set the type for this DIE. */
15522 cv_type = get_die_type (die, cu);
15523 if (cv_type)
15524 return cv_type;
15525
15526 cv_type = make_restrict_type (base_type);
15527 return set_die_type (die, cv_type, cu);
15528}
15529
a2c2acaf
MW
15530/* Handle DW_TAG_atomic_type. */
15531
15532static struct type *
15533read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
15534{
15535 struct type *base_type, *cv_type;
15536
15537 base_type = die_type (die, cu);
15538
15539 /* The die_type call above may have already set the type for this DIE. */
15540 cv_type = get_die_type (die, cu);
15541 if (cv_type)
15542 return cv_type;
15543
15544 cv_type = make_atomic_type (base_type);
15545 return set_die_type (die, cv_type, cu);
15546}
15547
c906108c
SS
15548/* Extract all information from a DW_TAG_string_type DIE and add to
15549 the user defined type vector. It isn't really a user defined type,
15550 but it behaves like one, with other DIE's using an AT_user_def_type
15551 attribute to reference it. */
15552
f792889a 15553static struct type *
e7c27a73 15554read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15555{
e7c27a73 15556 struct objfile *objfile = cu->objfile;
3b7538c0 15557 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15558 struct type *type, *range_type, *index_type, *char_type;
15559 struct attribute *attr;
15560 unsigned int length;
15561
e142c38c 15562 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
15563 if (attr)
15564 {
15565 length = DW_UNSND (attr);
15566 }
15567 else
15568 {
0963b4bd 15569 /* Check for the DW_AT_byte_size attribute. */
e142c38c 15570 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
15571 if (attr)
15572 {
15573 length = DW_UNSND (attr);
15574 }
15575 else
15576 {
15577 length = 1;
15578 }
c906108c 15579 }
6ccb9162 15580
46bf5051 15581 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 15582 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
15583 char_type = language_string_char_type (cu->language_defn, gdbarch);
15584 type = create_string_type (NULL, char_type, range_type);
6ccb9162 15585
f792889a 15586 return set_die_type (die, type, cu);
c906108c
SS
15587}
15588
4d804846
JB
15589/* Assuming that DIE corresponds to a function, returns nonzero
15590 if the function is prototyped. */
15591
15592static int
15593prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
15594{
15595 struct attribute *attr;
15596
15597 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
15598 if (attr && (DW_UNSND (attr) != 0))
15599 return 1;
15600
15601 /* The DWARF standard implies that the DW_AT_prototyped attribute
15602 is only meaninful for C, but the concept also extends to other
15603 languages that allow unprototyped functions (Eg: Objective C).
15604 For all other languages, assume that functions are always
15605 prototyped. */
15606 if (cu->language != language_c
15607 && cu->language != language_objc
15608 && cu->language != language_opencl)
15609 return 1;
15610
15611 /* RealView does not emit DW_AT_prototyped. We can not distinguish
15612 prototyped and unprototyped functions; default to prototyped,
15613 since that is more common in modern code (and RealView warns
15614 about unprototyped functions). */
15615 if (producer_is_realview (cu->producer))
15616 return 1;
15617
15618 return 0;
15619}
15620
c906108c
SS
15621/* Handle DIES due to C code like:
15622
15623 struct foo
c5aa993b
JM
15624 {
15625 int (*funcp)(int a, long l);
15626 int b;
15627 };
c906108c 15628
0963b4bd 15629 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 15630
f792889a 15631static struct type *
e7c27a73 15632read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15633{
bb5ed363 15634 struct objfile *objfile = cu->objfile;
0963b4bd
MS
15635 struct type *type; /* Type that this function returns. */
15636 struct type *ftype; /* Function that returns above type. */
c906108c
SS
15637 struct attribute *attr;
15638
e7c27a73 15639 type = die_type (die, cu);
7e314c57
JK
15640
15641 /* The die_type call above may have already set the type for this DIE. */
15642 ftype = get_die_type (die, cu);
15643 if (ftype)
15644 return ftype;
15645
0c8b41f1 15646 ftype = lookup_function_type (type);
c906108c 15647
4d804846 15648 if (prototyped_function_p (die, cu))
a6c727b2 15649 TYPE_PROTOTYPED (ftype) = 1;
c906108c 15650
c055b101
CV
15651 /* Store the calling convention in the type if it's available in
15652 the subroutine die. Otherwise set the calling convention to
15653 the default value DW_CC_normal. */
15654 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
15655 if (attr)
15656 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
15657 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
15658 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
15659 else
15660 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 15661
743649fd
MW
15662 /* Record whether the function returns normally to its caller or not
15663 if the DWARF producer set that information. */
15664 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
15665 if (attr && (DW_UNSND (attr) != 0))
15666 TYPE_NO_RETURN (ftype) = 1;
15667
76c10ea2
GM
15668 /* We need to add the subroutine type to the die immediately so
15669 we don't infinitely recurse when dealing with parameters
0963b4bd 15670 declared as the same subroutine type. */
76c10ea2 15671 set_die_type (die, ftype, cu);
6e70227d 15672
639d11d3 15673 if (die->child != NULL)
c906108c 15674 {
bb5ed363 15675 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 15676 struct die_info *child_die;
8072405b 15677 int nparams, iparams;
c906108c
SS
15678
15679 /* Count the number of parameters.
15680 FIXME: GDB currently ignores vararg functions, but knows about
15681 vararg member functions. */
8072405b 15682 nparams = 0;
639d11d3 15683 child_die = die->child;
c906108c
SS
15684 while (child_die && child_die->tag)
15685 {
15686 if (child_die->tag == DW_TAG_formal_parameter)
15687 nparams++;
15688 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 15689 TYPE_VARARGS (ftype) = 1;
c906108c
SS
15690 child_die = sibling_die (child_die);
15691 }
15692
15693 /* Allocate storage for parameters and fill them in. */
15694 TYPE_NFIELDS (ftype) = nparams;
15695 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 15696 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 15697
8072405b
JK
15698 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
15699 even if we error out during the parameters reading below. */
15700 for (iparams = 0; iparams < nparams; iparams++)
15701 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
15702
15703 iparams = 0;
639d11d3 15704 child_die = die->child;
c906108c
SS
15705 while (child_die && child_die->tag)
15706 {
15707 if (child_die->tag == DW_TAG_formal_parameter)
15708 {
3ce3b1ba
PA
15709 struct type *arg_type;
15710
15711 /* DWARF version 2 has no clean way to discern C++
15712 static and non-static member functions. G++ helps
15713 GDB by marking the first parameter for non-static
15714 member functions (which is the this pointer) as
15715 artificial. We pass this information to
15716 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
15717
15718 DWARF version 3 added DW_AT_object_pointer, which GCC
15719 4.5 does not yet generate. */
e142c38c 15720 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
15721 if (attr)
15722 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
15723 else
9c37b5ae 15724 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
15725 arg_type = die_type (child_die, cu);
15726
15727 /* RealView does not mark THIS as const, which the testsuite
15728 expects. GCC marks THIS as const in method definitions,
15729 but not in the class specifications (GCC PR 43053). */
15730 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
15731 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
15732 {
15733 int is_this = 0;
15734 struct dwarf2_cu *arg_cu = cu;
15735 const char *name = dwarf2_name (child_die, cu);
15736
15737 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
15738 if (attr)
15739 {
15740 /* If the compiler emits this, use it. */
15741 if (follow_die_ref (die, attr, &arg_cu) == child_die)
15742 is_this = 1;
15743 }
15744 else if (name && strcmp (name, "this") == 0)
15745 /* Function definitions will have the argument names. */
15746 is_this = 1;
15747 else if (name == NULL && iparams == 0)
15748 /* Declarations may not have the names, so like
15749 elsewhere in GDB, assume an artificial first
15750 argument is "this". */
15751 is_this = 1;
15752
15753 if (is_this)
15754 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
15755 arg_type, 0);
15756 }
15757
15758 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
15759 iparams++;
15760 }
15761 child_die = sibling_die (child_die);
15762 }
15763 }
15764
76c10ea2 15765 return ftype;
c906108c
SS
15766}
15767
f792889a 15768static struct type *
e7c27a73 15769read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15770{
e7c27a73 15771 struct objfile *objfile = cu->objfile;
0114d602 15772 const char *name = NULL;
3c8e0968 15773 struct type *this_type, *target_type;
c906108c 15774
94af9270 15775 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
15776 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
15777 TYPE_TARGET_STUB (this_type) = 1;
f792889a 15778 set_die_type (die, this_type, cu);
3c8e0968
DE
15779 target_type = die_type (die, cu);
15780 if (target_type != this_type)
15781 TYPE_TARGET_TYPE (this_type) = target_type;
15782 else
15783 {
15784 /* Self-referential typedefs are, it seems, not allowed by the DWARF
15785 spec and cause infinite loops in GDB. */
15786 complaint (&symfile_complaints,
15787 _("Self-referential DW_TAG_typedef "
15788 "- DIE at 0x%x [in module %s]"),
9c541725 15789 to_underlying (die->sect_off), objfile_name (objfile));
3c8e0968
DE
15790 TYPE_TARGET_TYPE (this_type) = NULL;
15791 }
f792889a 15792 return this_type;
c906108c
SS
15793}
15794
9b790ce7
UW
15795/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15796 (which may be different from NAME) to the architecture back-end to allow
15797 it to guess the correct format if necessary. */
15798
15799static struct type *
15800dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15801 const char *name_hint)
15802{
15803 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15804 const struct floatformat **format;
15805 struct type *type;
15806
15807 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15808 if (format)
15809 type = init_float_type (objfile, bits, name, format);
15810 else
77b7c781 15811 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
15812
15813 return type;
15814}
15815
c906108c
SS
15816/* Find a representation of a given base type and install
15817 it in the TYPE field of the die. */
15818
f792889a 15819static struct type *
e7c27a73 15820read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15821{
e7c27a73 15822 struct objfile *objfile = cu->objfile;
c906108c
SS
15823 struct type *type;
15824 struct attribute *attr;
19f392bc 15825 int encoding = 0, bits = 0;
15d034d0 15826 const char *name;
c906108c 15827
e142c38c 15828 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
15829 if (attr)
15830 {
15831 encoding = DW_UNSND (attr);
15832 }
e142c38c 15833 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15834 if (attr)
15835 {
19f392bc 15836 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 15837 }
39cbfefa 15838 name = dwarf2_name (die, cu);
6ccb9162 15839 if (!name)
c906108c 15840 {
6ccb9162
UW
15841 complaint (&symfile_complaints,
15842 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 15843 }
6ccb9162
UW
15844
15845 switch (encoding)
c906108c 15846 {
6ccb9162
UW
15847 case DW_ATE_address:
15848 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 15849 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 15850 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
15851 break;
15852 case DW_ATE_boolean:
19f392bc 15853 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
15854 break;
15855 case DW_ATE_complex_float:
9b790ce7 15856 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 15857 type = init_complex_type (objfile, name, type);
6ccb9162
UW
15858 break;
15859 case DW_ATE_decimal_float:
19f392bc 15860 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
15861 break;
15862 case DW_ATE_float:
9b790ce7 15863 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
15864 break;
15865 case DW_ATE_signed:
19f392bc 15866 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15867 break;
15868 case DW_ATE_unsigned:
3b2b8fea
TT
15869 if (cu->language == language_fortran
15870 && name
61012eef 15871 && startswith (name, "character("))
19f392bc
UW
15872 type = init_character_type (objfile, bits, 1, name);
15873 else
15874 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
15875 break;
15876 case DW_ATE_signed_char:
6e70227d 15877 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
15878 || cu->language == language_pascal
15879 || cu->language == language_fortran)
19f392bc
UW
15880 type = init_character_type (objfile, bits, 0, name);
15881 else
15882 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15883 break;
15884 case DW_ATE_unsigned_char:
868a0084 15885 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 15886 || cu->language == language_pascal
c44af4eb
TT
15887 || cu->language == language_fortran
15888 || cu->language == language_rust)
19f392bc
UW
15889 type = init_character_type (objfile, bits, 1, name);
15890 else
15891 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 15892 break;
75079b2b 15893 case DW_ATE_UTF:
53e710ac
PA
15894 {
15895 gdbarch *arch = get_objfile_arch (objfile);
15896
15897 if (bits == 16)
15898 type = builtin_type (arch)->builtin_char16;
15899 else if (bits == 32)
15900 type = builtin_type (arch)->builtin_char32;
15901 else
15902 {
15903 complaint (&symfile_complaints,
15904 _("unsupported DW_ATE_UTF bit size: '%d'"),
15905 bits);
15906 type = init_integer_type (objfile, bits, 1, name);
15907 }
15908 return set_die_type (die, type, cu);
15909 }
75079b2b
TT
15910 break;
15911
6ccb9162
UW
15912 default:
15913 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15914 dwarf_type_encoding_name (encoding));
77b7c781 15915 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 15916 break;
c906108c 15917 }
6ccb9162 15918
0114d602 15919 if (name && strcmp (name, "char") == 0)
876cecd0 15920 TYPE_NOSIGN (type) = 1;
0114d602 15921
f792889a 15922 return set_die_type (die, type, cu);
c906108c
SS
15923}
15924
80180f79
SA
15925/* Parse dwarf attribute if it's a block, reference or constant and put the
15926 resulting value of the attribute into struct bound_prop.
15927 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15928
15929static int
15930attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15931 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15932{
15933 struct dwarf2_property_baton *baton;
15934 struct obstack *obstack = &cu->objfile->objfile_obstack;
15935
15936 if (attr == NULL || prop == NULL)
15937 return 0;
15938
15939 if (attr_form_is_block (attr))
15940 {
8d749320 15941 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
15942 baton->referenced_type = NULL;
15943 baton->locexpr.per_cu = cu->per_cu;
15944 baton->locexpr.size = DW_BLOCK (attr)->size;
15945 baton->locexpr.data = DW_BLOCK (attr)->data;
15946 prop->data.baton = baton;
15947 prop->kind = PROP_LOCEXPR;
15948 gdb_assert (prop->data.baton != NULL);
15949 }
15950 else if (attr_form_is_ref (attr))
15951 {
15952 struct dwarf2_cu *target_cu = cu;
15953 struct die_info *target_die;
15954 struct attribute *target_attr;
15955
15956 target_die = follow_die_ref (die, attr, &target_cu);
15957 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
15958 if (target_attr == NULL)
15959 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15960 target_cu);
80180f79
SA
15961 if (target_attr == NULL)
15962 return 0;
15963
df25ebbd 15964 switch (target_attr->name)
80180f79 15965 {
df25ebbd
JB
15966 case DW_AT_location:
15967 if (attr_form_is_section_offset (target_attr))
15968 {
8d749320 15969 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15970 baton->referenced_type = die_type (target_die, target_cu);
15971 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15972 prop->data.baton = baton;
15973 prop->kind = PROP_LOCLIST;
15974 gdb_assert (prop->data.baton != NULL);
15975 }
15976 else if (attr_form_is_block (target_attr))
15977 {
8d749320 15978 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15979 baton->referenced_type = die_type (target_die, target_cu);
15980 baton->locexpr.per_cu = cu->per_cu;
15981 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15982 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15983 prop->data.baton = baton;
15984 prop->kind = PROP_LOCEXPR;
15985 gdb_assert (prop->data.baton != NULL);
15986 }
15987 else
15988 {
15989 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15990 "dynamic property");
15991 return 0;
15992 }
15993 break;
15994 case DW_AT_data_member_location:
15995 {
15996 LONGEST offset;
15997
15998 if (!handle_data_member_location (target_die, target_cu,
15999 &offset))
16000 return 0;
16001
8d749320 16002 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
16003 baton->referenced_type = read_type_die (target_die->parent,
16004 target_cu);
df25ebbd
JB
16005 baton->offset_info.offset = offset;
16006 baton->offset_info.type = die_type (target_die, target_cu);
16007 prop->data.baton = baton;
16008 prop->kind = PROP_ADDR_OFFSET;
16009 break;
16010 }
80180f79
SA
16011 }
16012 }
16013 else if (attr_form_is_constant (attr))
16014 {
16015 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
16016 prop->kind = PROP_CONST;
16017 }
16018 else
16019 {
16020 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
16021 dwarf2_name (die, cu));
16022 return 0;
16023 }
16024
16025 return 1;
16026}
16027
a02abb62
JB
16028/* Read the given DW_AT_subrange DIE. */
16029
f792889a 16030static struct type *
a02abb62
JB
16031read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
16032{
4c9ad8c2 16033 struct type *base_type, *orig_base_type;
a02abb62
JB
16034 struct type *range_type;
16035 struct attribute *attr;
729efb13 16036 struct dynamic_prop low, high;
4fae6e18 16037 int low_default_is_valid;
c451ebe5 16038 int high_bound_is_count = 0;
15d034d0 16039 const char *name;
43bbcdc2 16040 LONGEST negative_mask;
e77813c8 16041
4c9ad8c2
TT
16042 orig_base_type = die_type (die, cu);
16043 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
16044 whereas the real type might be. So, we use ORIG_BASE_TYPE when
16045 creating the range type, but we use the result of check_typedef
16046 when examining properties of the type. */
16047 base_type = check_typedef (orig_base_type);
a02abb62 16048
7e314c57
JK
16049 /* The die_type call above may have already set the type for this DIE. */
16050 range_type = get_die_type (die, cu);
16051 if (range_type)
16052 return range_type;
16053
729efb13
SA
16054 low.kind = PROP_CONST;
16055 high.kind = PROP_CONST;
16056 high.data.const_val = 0;
16057
4fae6e18
JK
16058 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
16059 omitting DW_AT_lower_bound. */
16060 switch (cu->language)
6e70227d 16061 {
4fae6e18
JK
16062 case language_c:
16063 case language_cplus:
729efb13 16064 low.data.const_val = 0;
4fae6e18
JK
16065 low_default_is_valid = 1;
16066 break;
16067 case language_fortran:
729efb13 16068 low.data.const_val = 1;
4fae6e18
JK
16069 low_default_is_valid = 1;
16070 break;
16071 case language_d:
4fae6e18 16072 case language_objc:
c44af4eb 16073 case language_rust:
729efb13 16074 low.data.const_val = 0;
4fae6e18
JK
16075 low_default_is_valid = (cu->header.version >= 4);
16076 break;
16077 case language_ada:
16078 case language_m2:
16079 case language_pascal:
729efb13 16080 low.data.const_val = 1;
4fae6e18
JK
16081 low_default_is_valid = (cu->header.version >= 4);
16082 break;
16083 default:
729efb13 16084 low.data.const_val = 0;
4fae6e18
JK
16085 low_default_is_valid = 0;
16086 break;
a02abb62
JB
16087 }
16088
e142c38c 16089 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 16090 if (attr)
11c1ba78 16091 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
16092 else if (!low_default_is_valid)
16093 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
16094 "- DIE at 0x%x [in module %s]"),
9c541725 16095 to_underlying (die->sect_off), objfile_name (cu->objfile));
a02abb62 16096
e142c38c 16097 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 16098 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
16099 {
16100 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 16101 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 16102 {
c451ebe5
SA
16103 /* If bounds are constant do the final calculation here. */
16104 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
16105 high.data.const_val = low.data.const_val + high.data.const_val - 1;
16106 else
16107 high_bound_is_count = 1;
c2ff108b 16108 }
e77813c8
PM
16109 }
16110
16111 /* Dwarf-2 specifications explicitly allows to create subrange types
16112 without specifying a base type.
16113 In that case, the base type must be set to the type of
16114 the lower bound, upper bound or count, in that order, if any of these
16115 three attributes references an object that has a type.
16116 If no base type is found, the Dwarf-2 specifications say that
16117 a signed integer type of size equal to the size of an address should
16118 be used.
16119 For the following C code: `extern char gdb_int [];'
16120 GCC produces an empty range DIE.
16121 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 16122 high bound or count are not yet handled by this code. */
e77813c8
PM
16123 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
16124 {
16125 struct objfile *objfile = cu->objfile;
16126 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16127 int addr_size = gdbarch_addr_bit (gdbarch) /8;
16128 struct type *int_type = objfile_type (objfile)->builtin_int;
16129
16130 /* Test "int", "long int", and "long long int" objfile types,
16131 and select the first one having a size above or equal to the
16132 architecture address size. */
16133 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16134 base_type = int_type;
16135 else
16136 {
16137 int_type = objfile_type (objfile)->builtin_long;
16138 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16139 base_type = int_type;
16140 else
16141 {
16142 int_type = objfile_type (objfile)->builtin_long_long;
16143 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16144 base_type = int_type;
16145 }
16146 }
16147 }
a02abb62 16148
dbb9c2b1
JB
16149 /* Normally, the DWARF producers are expected to use a signed
16150 constant form (Eg. DW_FORM_sdata) to express negative bounds.
16151 But this is unfortunately not always the case, as witnessed
16152 with GCC, for instance, where the ambiguous DW_FORM_dataN form
16153 is used instead. To work around that ambiguity, we treat
16154 the bounds as signed, and thus sign-extend their values, when
16155 the base type is signed. */
6e70227d 16156 negative_mask =
66c6502d 16157 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
16158 if (low.kind == PROP_CONST
16159 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
16160 low.data.const_val |= negative_mask;
16161 if (high.kind == PROP_CONST
16162 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
16163 high.data.const_val |= negative_mask;
43bbcdc2 16164
729efb13 16165 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 16166
c451ebe5
SA
16167 if (high_bound_is_count)
16168 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
16169
c2ff108b
JK
16170 /* Ada expects an empty array on no boundary attributes. */
16171 if (attr == NULL && cu->language != language_ada)
729efb13 16172 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 16173
39cbfefa
DJ
16174 name = dwarf2_name (die, cu);
16175 if (name)
16176 TYPE_NAME (range_type) = name;
6e70227d 16177
e142c38c 16178 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
16179 if (attr)
16180 TYPE_LENGTH (range_type) = DW_UNSND (attr);
16181
7e314c57
JK
16182 set_die_type (die, range_type, cu);
16183
16184 /* set_die_type should be already done. */
b4ba55a1
JB
16185 set_descriptive_type (range_type, die, cu);
16186
7e314c57 16187 return range_type;
a02abb62 16188}
6e70227d 16189
f792889a 16190static struct type *
81a17f79
JB
16191read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
16192{
16193 struct type *type;
81a17f79 16194
81a17f79
JB
16195 /* For now, we only support the C meaning of an unspecified type: void. */
16196
19f392bc 16197 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 16198 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 16199
f792889a 16200 return set_die_type (die, type, cu);
81a17f79 16201}
a02abb62 16202
639d11d3
DC
16203/* Read a single die and all its descendents. Set the die's sibling
16204 field to NULL; set other fields in the die correctly, and set all
16205 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
16206 location of the info_ptr after reading all of those dies. PARENT
16207 is the parent of the die in question. */
16208
16209static struct die_info *
dee91e82 16210read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
16211 const gdb_byte *info_ptr,
16212 const gdb_byte **new_info_ptr,
dee91e82 16213 struct die_info *parent)
639d11d3
DC
16214{
16215 struct die_info *die;
d521ce57 16216 const gdb_byte *cur_ptr;
639d11d3
DC
16217 int has_children;
16218
bf6af496 16219 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
16220 if (die == NULL)
16221 {
16222 *new_info_ptr = cur_ptr;
16223 return NULL;
16224 }
93311388 16225 store_in_ref_table (die, reader->cu);
639d11d3
DC
16226
16227 if (has_children)
bf6af496 16228 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
16229 else
16230 {
16231 die->child = NULL;
16232 *new_info_ptr = cur_ptr;
16233 }
16234
16235 die->sibling = NULL;
16236 die->parent = parent;
16237 return die;
16238}
16239
16240/* Read a die, all of its descendents, and all of its siblings; set
16241 all of the fields of all of the dies correctly. Arguments are as
16242 in read_die_and_children. */
16243
16244static struct die_info *
bf6af496 16245read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
16246 const gdb_byte *info_ptr,
16247 const gdb_byte **new_info_ptr,
bf6af496 16248 struct die_info *parent)
639d11d3
DC
16249{
16250 struct die_info *first_die, *last_sibling;
d521ce57 16251 const gdb_byte *cur_ptr;
639d11d3 16252
c906108c 16253 cur_ptr = info_ptr;
639d11d3
DC
16254 first_die = last_sibling = NULL;
16255
16256 while (1)
c906108c 16257 {
639d11d3 16258 struct die_info *die
dee91e82 16259 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 16260
1d325ec1 16261 if (die == NULL)
c906108c 16262 {
639d11d3
DC
16263 *new_info_ptr = cur_ptr;
16264 return first_die;
c906108c 16265 }
1d325ec1
DJ
16266
16267 if (!first_die)
16268 first_die = die;
c906108c 16269 else
1d325ec1
DJ
16270 last_sibling->sibling = die;
16271
16272 last_sibling = die;
c906108c 16273 }
c906108c
SS
16274}
16275
bf6af496
DE
16276/* Read a die, all of its descendents, and all of its siblings; set
16277 all of the fields of all of the dies correctly. Arguments are as
16278 in read_die_and_children.
16279 This the main entry point for reading a DIE and all its children. */
16280
16281static struct die_info *
16282read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
16283 const gdb_byte *info_ptr,
16284 const gdb_byte **new_info_ptr,
bf6af496
DE
16285 struct die_info *parent)
16286{
16287 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
16288 new_info_ptr, parent);
16289
b4f54984 16290 if (dwarf_die_debug)
bf6af496
DE
16291 {
16292 fprintf_unfiltered (gdb_stdlog,
16293 "Read die from %s@0x%x of %s:\n",
a32a8923 16294 get_section_name (reader->die_section),
bf6af496
DE
16295 (unsigned) (info_ptr - reader->die_section->buffer),
16296 bfd_get_filename (reader->abfd));
b4f54984 16297 dump_die (die, dwarf_die_debug);
bf6af496
DE
16298 }
16299
16300 return die;
16301}
16302
3019eac3
DE
16303/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
16304 attributes.
16305 The caller is responsible for filling in the extra attributes
16306 and updating (*DIEP)->num_attrs.
16307 Set DIEP to point to a newly allocated die with its information,
16308 except for its child, sibling, and parent fields.
16309 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 16310
d521ce57 16311static const gdb_byte *
3019eac3 16312read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 16313 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 16314 int *has_children, int num_extra_attrs)
93311388 16315{
b64f50a1 16316 unsigned int abbrev_number, bytes_read, i;
93311388
DE
16317 struct abbrev_info *abbrev;
16318 struct die_info *die;
16319 struct dwarf2_cu *cu = reader->cu;
16320 bfd *abfd = reader->abfd;
16321
9c541725 16322 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
16323 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16324 info_ptr += bytes_read;
16325 if (!abbrev_number)
16326 {
16327 *diep = NULL;
16328 *has_children = 0;
16329 return info_ptr;
16330 }
16331
433df2d4 16332 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 16333 if (!abbrev)
348e048f
DE
16334 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
16335 abbrev_number,
16336 bfd_get_filename (abfd));
16337
3019eac3 16338 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 16339 die->sect_off = sect_off;
93311388
DE
16340 die->tag = abbrev->tag;
16341 die->abbrev = abbrev_number;
16342
3019eac3
DE
16343 /* Make the result usable.
16344 The caller needs to update num_attrs after adding the extra
16345 attributes. */
93311388
DE
16346 die->num_attrs = abbrev->num_attrs;
16347
16348 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
16349 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
16350 info_ptr);
93311388
DE
16351
16352 *diep = die;
16353 *has_children = abbrev->has_children;
16354 return info_ptr;
16355}
16356
3019eac3
DE
16357/* Read a die and all its attributes.
16358 Set DIEP to point to a newly allocated die with its information,
16359 except for its child, sibling, and parent fields.
16360 Set HAS_CHILDREN to tell whether the die has children or not. */
16361
d521ce57 16362static const gdb_byte *
3019eac3 16363read_full_die (const struct die_reader_specs *reader,
d521ce57 16364 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
16365 int *has_children)
16366{
d521ce57 16367 const gdb_byte *result;
bf6af496
DE
16368
16369 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
16370
b4f54984 16371 if (dwarf_die_debug)
bf6af496
DE
16372 {
16373 fprintf_unfiltered (gdb_stdlog,
16374 "Read die from %s@0x%x of %s:\n",
a32a8923 16375 get_section_name (reader->die_section),
bf6af496
DE
16376 (unsigned) (info_ptr - reader->die_section->buffer),
16377 bfd_get_filename (reader->abfd));
b4f54984 16378 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
16379 }
16380
16381 return result;
3019eac3 16382}
433df2d4
DE
16383\f
16384/* Abbreviation tables.
3019eac3 16385
433df2d4 16386 In DWARF version 2, the description of the debugging information is
c906108c
SS
16387 stored in a separate .debug_abbrev section. Before we read any
16388 dies from a section we read in all abbreviations and install them
433df2d4
DE
16389 in a hash table. */
16390
16391/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
16392
16393static struct abbrev_info *
16394abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
16395{
16396 struct abbrev_info *abbrev;
16397
8d749320 16398 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 16399 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 16400
433df2d4
DE
16401 return abbrev;
16402}
16403
16404/* Add an abbreviation to the table. */
c906108c
SS
16405
16406static void
433df2d4
DE
16407abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
16408 unsigned int abbrev_number,
16409 struct abbrev_info *abbrev)
16410{
16411 unsigned int hash_number;
16412
16413 hash_number = abbrev_number % ABBREV_HASH_SIZE;
16414 abbrev->next = abbrev_table->abbrevs[hash_number];
16415 abbrev_table->abbrevs[hash_number] = abbrev;
16416}
dee91e82 16417
433df2d4
DE
16418/* Look up an abbrev in the table.
16419 Returns NULL if the abbrev is not found. */
16420
16421static struct abbrev_info *
16422abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
16423 unsigned int abbrev_number)
c906108c 16424{
433df2d4
DE
16425 unsigned int hash_number;
16426 struct abbrev_info *abbrev;
16427
16428 hash_number = abbrev_number % ABBREV_HASH_SIZE;
16429 abbrev = abbrev_table->abbrevs[hash_number];
16430
16431 while (abbrev)
16432 {
16433 if (abbrev->number == abbrev_number)
16434 return abbrev;
16435 abbrev = abbrev->next;
16436 }
16437 return NULL;
16438}
16439
16440/* Read in an abbrev table. */
16441
16442static struct abbrev_table *
16443abbrev_table_read_table (struct dwarf2_section_info *section,
9c541725 16444 sect_offset sect_off)
433df2d4
DE
16445{
16446 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 16447 bfd *abfd = get_section_bfd_owner (section);
433df2d4 16448 struct abbrev_table *abbrev_table;
d521ce57 16449 const gdb_byte *abbrev_ptr;
c906108c
SS
16450 struct abbrev_info *cur_abbrev;
16451 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 16452 unsigned int abbrev_form;
f3dd6933
DJ
16453 struct attr_abbrev *cur_attrs;
16454 unsigned int allocated_attrs;
c906108c 16455
70ba0933 16456 abbrev_table = XNEW (struct abbrev_table);
9c541725 16457 abbrev_table->sect_off = sect_off;
433df2d4 16458 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
16459 abbrev_table->abbrevs =
16460 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
16461 ABBREV_HASH_SIZE);
433df2d4
DE
16462 memset (abbrev_table->abbrevs, 0,
16463 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 16464
433df2d4 16465 dwarf2_read_section (objfile, section);
9c541725 16466 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
16467 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16468 abbrev_ptr += bytes_read;
16469
f3dd6933 16470 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 16471 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 16472
0963b4bd 16473 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
16474 while (abbrev_number)
16475 {
433df2d4 16476 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
16477
16478 /* read in abbrev header */
16479 cur_abbrev->number = abbrev_number;
aead7601
SM
16480 cur_abbrev->tag
16481 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
16482 abbrev_ptr += bytes_read;
16483 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
16484 abbrev_ptr += 1;
16485
16486 /* now read in declarations */
22d2f3ab 16487 for (;;)
c906108c 16488 {
43988095
JK
16489 LONGEST implicit_const;
16490
22d2f3ab
JK
16491 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16492 abbrev_ptr += bytes_read;
16493 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16494 abbrev_ptr += bytes_read;
43988095
JK
16495 if (abbrev_form == DW_FORM_implicit_const)
16496 {
16497 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
16498 &bytes_read);
16499 abbrev_ptr += bytes_read;
16500 }
16501 else
16502 {
16503 /* Initialize it due to a false compiler warning. */
16504 implicit_const = -1;
16505 }
22d2f3ab
JK
16506
16507 if (abbrev_name == 0)
16508 break;
16509
f3dd6933 16510 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 16511 {
f3dd6933
DJ
16512 allocated_attrs += ATTR_ALLOC_CHUNK;
16513 cur_attrs
224c3ddb 16514 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 16515 }
ae038cb0 16516
aead7601
SM
16517 cur_attrs[cur_abbrev->num_attrs].name
16518 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 16519 cur_attrs[cur_abbrev->num_attrs].form
aead7601 16520 = (enum dwarf_form) abbrev_form;
43988095 16521 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 16522 ++cur_abbrev->num_attrs;
c906108c
SS
16523 }
16524
8d749320
SM
16525 cur_abbrev->attrs =
16526 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
16527 cur_abbrev->num_attrs);
f3dd6933
DJ
16528 memcpy (cur_abbrev->attrs, cur_attrs,
16529 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
16530
433df2d4 16531 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
16532
16533 /* Get next abbreviation.
16534 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
16535 always properly terminated with an abbrev number of 0.
16536 Exit loop if we encounter an abbreviation which we have
16537 already read (which means we are about to read the abbreviations
16538 for the next compile unit) or if the end of the abbreviation
16539 table is reached. */
433df2d4 16540 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
16541 break;
16542 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16543 abbrev_ptr += bytes_read;
433df2d4 16544 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
16545 break;
16546 }
f3dd6933
DJ
16547
16548 xfree (cur_attrs);
433df2d4 16549 return abbrev_table;
c906108c
SS
16550}
16551
433df2d4 16552/* Free the resources held by ABBREV_TABLE. */
c906108c 16553
c906108c 16554static void
433df2d4 16555abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 16556{
433df2d4
DE
16557 obstack_free (&abbrev_table->abbrev_obstack, NULL);
16558 xfree (abbrev_table);
c906108c
SS
16559}
16560
f4dc4d17
DE
16561/* Same as abbrev_table_free but as a cleanup.
16562 We pass in a pointer to the pointer to the table so that we can
16563 set the pointer to NULL when we're done. It also simplifies
73051182 16564 build_type_psymtabs_1. */
f4dc4d17
DE
16565
16566static void
16567abbrev_table_free_cleanup (void *table_ptr)
16568{
9a3c8263 16569 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
16570
16571 if (*abbrev_table_ptr != NULL)
16572 abbrev_table_free (*abbrev_table_ptr);
16573 *abbrev_table_ptr = NULL;
16574}
16575
433df2d4
DE
16576/* Read the abbrev table for CU from ABBREV_SECTION. */
16577
16578static void
16579dwarf2_read_abbrevs (struct dwarf2_cu *cu,
16580 struct dwarf2_section_info *abbrev_section)
c906108c 16581{
433df2d4 16582 cu->abbrev_table =
9c541725 16583 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
433df2d4 16584}
c906108c 16585
433df2d4 16586/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 16587
433df2d4
DE
16588static void
16589dwarf2_free_abbrev_table (void *ptr_to_cu)
16590{
9a3c8263 16591 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 16592
a2ce51a0
DE
16593 if (cu->abbrev_table != NULL)
16594 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
16595 /* Set this to NULL so that we SEGV if we try to read it later,
16596 and also because free_comp_unit verifies this is NULL. */
16597 cu->abbrev_table = NULL;
16598}
16599\f
72bf9492
DJ
16600/* Returns nonzero if TAG represents a type that we might generate a partial
16601 symbol for. */
16602
16603static int
16604is_type_tag_for_partial (int tag)
16605{
16606 switch (tag)
16607 {
16608#if 0
16609 /* Some types that would be reasonable to generate partial symbols for,
16610 that we don't at present. */
16611 case DW_TAG_array_type:
16612 case DW_TAG_file_type:
16613 case DW_TAG_ptr_to_member_type:
16614 case DW_TAG_set_type:
16615 case DW_TAG_string_type:
16616 case DW_TAG_subroutine_type:
16617#endif
16618 case DW_TAG_base_type:
16619 case DW_TAG_class_type:
680b30c7 16620 case DW_TAG_interface_type:
72bf9492
DJ
16621 case DW_TAG_enumeration_type:
16622 case DW_TAG_structure_type:
16623 case DW_TAG_subrange_type:
16624 case DW_TAG_typedef:
16625 case DW_TAG_union_type:
16626 return 1;
16627 default:
16628 return 0;
16629 }
16630}
16631
16632/* Load all DIEs that are interesting for partial symbols into memory. */
16633
16634static struct partial_die_info *
dee91e82 16635load_partial_dies (const struct die_reader_specs *reader,
d521ce57 16636 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 16637{
dee91e82 16638 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16639 struct objfile *objfile = cu->objfile;
72bf9492
DJ
16640 struct partial_die_info *part_die;
16641 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
16642 struct abbrev_info *abbrev;
16643 unsigned int bytes_read;
5afb4e99 16644 unsigned int load_all = 0;
72bf9492
DJ
16645 int nesting_level = 1;
16646
16647 parent_die = NULL;
16648 last_die = NULL;
16649
7adf1e79
DE
16650 gdb_assert (cu->per_cu != NULL);
16651 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
16652 load_all = 1;
16653
72bf9492
DJ
16654 cu->partial_dies
16655 = htab_create_alloc_ex (cu->header.length / 12,
16656 partial_die_hash,
16657 partial_die_eq,
16658 NULL,
16659 &cu->comp_unit_obstack,
16660 hashtab_obstack_allocate,
16661 dummy_obstack_deallocate);
16662
8d749320 16663 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16664
16665 while (1)
16666 {
16667 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
16668
16669 /* A NULL abbrev means the end of a series of children. */
16670 if (abbrev == NULL)
16671 {
16672 if (--nesting_level == 0)
16673 {
16674 /* PART_DIE was probably the last thing allocated on the
16675 comp_unit_obstack, so we could call obstack_free
16676 here. We don't do that because the waste is small,
16677 and will be cleaned up when we're done with this
16678 compilation unit. This way, we're also more robust
16679 against other users of the comp_unit_obstack. */
16680 return first_die;
16681 }
16682 info_ptr += bytes_read;
16683 last_die = parent_die;
16684 parent_die = parent_die->die_parent;
16685 continue;
16686 }
16687
98bfdba5
PA
16688 /* Check for template arguments. We never save these; if
16689 they're seen, we just mark the parent, and go on our way. */
16690 if (parent_die != NULL
16691 && cu->language == language_cplus
16692 && (abbrev->tag == DW_TAG_template_type_param
16693 || abbrev->tag == DW_TAG_template_value_param))
16694 {
16695 parent_die->has_template_arguments = 1;
16696
16697 if (!load_all)
16698 {
16699 /* We don't need a partial DIE for the template argument. */
dee91e82 16700 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
16701 continue;
16702 }
16703 }
16704
0d99eb77 16705 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
16706 Skip their other children. */
16707 if (!load_all
16708 && cu->language == language_cplus
16709 && parent_die != NULL
16710 && parent_die->tag == DW_TAG_subprogram)
16711 {
dee91e82 16712 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
16713 continue;
16714 }
16715
5afb4e99
DJ
16716 /* Check whether this DIE is interesting enough to save. Normally
16717 we would not be interested in members here, but there may be
16718 later variables referencing them via DW_AT_specification (for
16719 static members). */
16720 if (!load_all
16721 && !is_type_tag_for_partial (abbrev->tag)
72929c62 16722 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
16723 && abbrev->tag != DW_TAG_enumerator
16724 && abbrev->tag != DW_TAG_subprogram
bc30ff58 16725 && abbrev->tag != DW_TAG_lexical_block
72bf9492 16726 && abbrev->tag != DW_TAG_variable
5afb4e99 16727 && abbrev->tag != DW_TAG_namespace
f55ee35c 16728 && abbrev->tag != DW_TAG_module
95554aad 16729 && abbrev->tag != DW_TAG_member
74921315
KS
16730 && abbrev->tag != DW_TAG_imported_unit
16731 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
16732 {
16733 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16734 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
16735 continue;
16736 }
16737
dee91e82
DE
16738 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
16739 info_ptr);
72bf9492
DJ
16740
16741 /* This two-pass algorithm for processing partial symbols has a
16742 high cost in cache pressure. Thus, handle some simple cases
16743 here which cover the majority of C partial symbols. DIEs
16744 which neither have specification tags in them, nor could have
16745 specification tags elsewhere pointing at them, can simply be
16746 processed and discarded.
16747
16748 This segment is also optional; scan_partial_symbols and
16749 add_partial_symbol will handle these DIEs if we chain
16750 them in normally. When compilers which do not emit large
16751 quantities of duplicate debug information are more common,
16752 this code can probably be removed. */
16753
16754 /* Any complete simple types at the top level (pretty much all
16755 of them, for a language without namespaces), can be processed
16756 directly. */
16757 if (parent_die == NULL
16758 && part_die->has_specification == 0
16759 && part_die->is_declaration == 0
d8228535 16760 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
16761 || part_die->tag == DW_TAG_base_type
16762 || part_die->tag == DW_TAG_subrange_type))
16763 {
16764 if (building_psymtab && part_die->name != NULL)
04a679b8 16765 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16766 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 16767 &objfile->static_psymbols,
1762568f 16768 0, cu->language, objfile);
dee91e82 16769 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16770 continue;
16771 }
16772
d8228535
JK
16773 /* The exception for DW_TAG_typedef with has_children above is
16774 a workaround of GCC PR debug/47510. In the case of this complaint
16775 type_name_no_tag_or_error will error on such types later.
16776
16777 GDB skipped children of DW_TAG_typedef by the shortcut above and then
16778 it could not find the child DIEs referenced later, this is checked
16779 above. In correct DWARF DW_TAG_typedef should have no children. */
16780
16781 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
16782 complaint (&symfile_complaints,
16783 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
16784 "- DIE at 0x%x [in module %s]"),
9c541725 16785 to_underlying (part_die->sect_off), objfile_name (objfile));
d8228535 16786
72bf9492
DJ
16787 /* If we're at the second level, and we're an enumerator, and
16788 our parent has no specification (meaning possibly lives in a
16789 namespace elsewhere), then we can add the partial symbol now
16790 instead of queueing it. */
16791 if (part_die->tag == DW_TAG_enumerator
16792 && parent_die != NULL
16793 && parent_die->die_parent == NULL
16794 && parent_die->tag == DW_TAG_enumeration_type
16795 && parent_die->has_specification == 0)
16796 {
16797 if (part_die->name == NULL)
3e43a32a
MS
16798 complaint (&symfile_complaints,
16799 _("malformed enumerator DIE ignored"));
72bf9492 16800 else if (building_psymtab)
04a679b8 16801 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16802 VAR_DOMAIN, LOC_CONST,
9c37b5ae 16803 cu->language == language_cplus
bb5ed363
DE
16804 ? &objfile->global_psymbols
16805 : &objfile->static_psymbols,
1762568f 16806 0, cu->language, objfile);
72bf9492 16807
dee91e82 16808 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16809 continue;
16810 }
16811
16812 /* We'll save this DIE so link it in. */
16813 part_die->die_parent = parent_die;
16814 part_die->die_sibling = NULL;
16815 part_die->die_child = NULL;
16816
16817 if (last_die && last_die == parent_die)
16818 last_die->die_child = part_die;
16819 else if (last_die)
16820 last_die->die_sibling = part_die;
16821
16822 last_die = part_die;
16823
16824 if (first_die == NULL)
16825 first_die = part_die;
16826
16827 /* Maybe add the DIE to the hash table. Not all DIEs that we
16828 find interesting need to be in the hash table, because we
16829 also have the parent/sibling/child chains; only those that we
16830 might refer to by offset later during partial symbol reading.
16831
16832 For now this means things that might have be the target of a
16833 DW_AT_specification, DW_AT_abstract_origin, or
16834 DW_AT_extension. DW_AT_extension will refer only to
16835 namespaces; DW_AT_abstract_origin refers to functions (and
16836 many things under the function DIE, but we do not recurse
16837 into function DIEs during partial symbol reading) and
16838 possibly variables as well; DW_AT_specification refers to
16839 declarations. Declarations ought to have the DW_AT_declaration
16840 flag. It happens that GCC forgets to put it in sometimes, but
16841 only for functions, not for types.
16842
16843 Adding more things than necessary to the hash table is harmless
16844 except for the performance cost. Adding too few will result in
5afb4e99
DJ
16845 wasted time in find_partial_die, when we reread the compilation
16846 unit with load_all_dies set. */
72bf9492 16847
5afb4e99 16848 if (load_all
72929c62 16849 || abbrev->tag == DW_TAG_constant
5afb4e99 16850 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
16851 || abbrev->tag == DW_TAG_variable
16852 || abbrev->tag == DW_TAG_namespace
16853 || part_die->is_declaration)
16854 {
16855 void **slot;
16856
16857 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
16858 to_underlying (part_die->sect_off),
16859 INSERT);
72bf9492
DJ
16860 *slot = part_die;
16861 }
16862
8d749320 16863 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16864
16865 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 16866 we have no reason to follow the children of structures; for other
98bfdba5
PA
16867 languages we have to, so that we can get at method physnames
16868 to infer fully qualified class names, for DW_AT_specification,
16869 and for C++ template arguments. For C++, we also look one level
16870 inside functions to find template arguments (if the name of the
16871 function does not already contain the template arguments).
bc30ff58
JB
16872
16873 For Ada, we need to scan the children of subprograms and lexical
16874 blocks as well because Ada allows the definition of nested
16875 entities that could be interesting for the debugger, such as
16876 nested subprograms for instance. */
72bf9492 16877 if (last_die->has_children
5afb4e99
DJ
16878 && (load_all
16879 || last_die->tag == DW_TAG_namespace
f55ee35c 16880 || last_die->tag == DW_TAG_module
72bf9492 16881 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
16882 || (cu->language == language_cplus
16883 && last_die->tag == DW_TAG_subprogram
16884 && (last_die->name == NULL
16885 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
16886 || (cu->language != language_c
16887 && (last_die->tag == DW_TAG_class_type
680b30c7 16888 || last_die->tag == DW_TAG_interface_type
72bf9492 16889 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
16890 || last_die->tag == DW_TAG_union_type))
16891 || (cu->language == language_ada
16892 && (last_die->tag == DW_TAG_subprogram
16893 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
16894 {
16895 nesting_level++;
16896 parent_die = last_die;
16897 continue;
16898 }
16899
16900 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16901 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
16902
16903 /* Back to the top, do it again. */
16904 }
16905}
16906
c906108c
SS
16907/* Read a minimal amount of information into the minimal die structure. */
16908
d521ce57 16909static const gdb_byte *
dee91e82
DE
16910read_partial_die (const struct die_reader_specs *reader,
16911 struct partial_die_info *part_die,
16912 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 16913 const gdb_byte *info_ptr)
c906108c 16914{
dee91e82 16915 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16916 struct objfile *objfile = cu->objfile;
d521ce57 16917 const gdb_byte *buffer = reader->buffer;
fa238c03 16918 unsigned int i;
c906108c 16919 struct attribute attr;
c5aa993b 16920 int has_low_pc_attr = 0;
c906108c 16921 int has_high_pc_attr = 0;
91da1414 16922 int high_pc_relative = 0;
c906108c 16923
72bf9492 16924 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 16925
9c541725 16926 part_die->sect_off = (sect_offset) (info_ptr - buffer);
72bf9492
DJ
16927
16928 info_ptr += abbrev_len;
16929
16930 if (abbrev == NULL)
16931 return info_ptr;
16932
c906108c
SS
16933 part_die->tag = abbrev->tag;
16934 part_die->has_children = abbrev->has_children;
c906108c
SS
16935
16936 for (i = 0; i < abbrev->num_attrs; ++i)
16937 {
dee91e82 16938 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
16939
16940 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 16941 partial symbol table. */
c906108c
SS
16942 switch (attr.name)
16943 {
16944 case DW_AT_name:
71c25dea
TT
16945 switch (part_die->tag)
16946 {
16947 case DW_TAG_compile_unit:
95554aad 16948 case DW_TAG_partial_unit:
348e048f 16949 case DW_TAG_type_unit:
71c25dea
TT
16950 /* Compilation units have a DW_AT_name that is a filename, not
16951 a source language identifier. */
16952 case DW_TAG_enumeration_type:
16953 case DW_TAG_enumerator:
16954 /* These tags always have simple identifiers already; no need
16955 to canonicalize them. */
16956 part_die->name = DW_STRING (&attr);
16957 break;
16958 default:
16959 part_die->name
16960 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 16961 &objfile->per_bfd->storage_obstack);
71c25dea
TT
16962 break;
16963 }
c906108c 16964 break;
31ef98ae 16965 case DW_AT_linkage_name:
c906108c 16966 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
16967 /* Note that both forms of linkage name might appear. We
16968 assume they will be the same, and we only store the last
16969 one we see. */
94af9270
KS
16970 if (cu->language == language_ada)
16971 part_die->name = DW_STRING (&attr);
abc72ce4 16972 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
16973 break;
16974 case DW_AT_low_pc:
16975 has_low_pc_attr = 1;
31aa7e4e 16976 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
16977 break;
16978 case DW_AT_high_pc:
16979 has_high_pc_attr = 1;
31aa7e4e
JB
16980 part_die->highpc = attr_value_as_address (&attr);
16981 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16982 high_pc_relative = 1;
c906108c
SS
16983 break;
16984 case DW_AT_location:
0963b4bd 16985 /* Support the .debug_loc offsets. */
8e19ed76
PS
16986 if (attr_form_is_block (&attr))
16987 {
95554aad 16988 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 16989 }
3690dd37 16990 else if (attr_form_is_section_offset (&attr))
8e19ed76 16991 {
4d3c2250 16992 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
16993 }
16994 else
16995 {
4d3c2250
KB
16996 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16997 "partial symbol information");
8e19ed76 16998 }
c906108c 16999 break;
c906108c
SS
17000 case DW_AT_external:
17001 part_die->is_external = DW_UNSND (&attr);
17002 break;
17003 case DW_AT_declaration:
17004 part_die->is_declaration = DW_UNSND (&attr);
17005 break;
17006 case DW_AT_type:
17007 part_die->has_type = 1;
17008 break;
17009 case DW_AT_abstract_origin:
17010 case DW_AT_specification:
72bf9492
DJ
17011 case DW_AT_extension:
17012 part_die->has_specification = 1;
c764a876 17013 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
17014 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17015 || cu->per_cu->is_dwz);
c906108c
SS
17016 break;
17017 case DW_AT_sibling:
17018 /* Ignore absolute siblings, they might point outside of
17019 the current compile unit. */
17020 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
17021 complaint (&symfile_complaints,
17022 _("ignoring absolute DW_AT_sibling"));
c906108c 17023 else
b9502d3f 17024 {
9c541725
PA
17025 sect_offset off = dwarf2_get_ref_die_offset (&attr);
17026 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
17027
17028 if (sibling_ptr < info_ptr)
17029 complaint (&symfile_complaints,
17030 _("DW_AT_sibling points backwards"));
22869d73
KS
17031 else if (sibling_ptr > reader->buffer_end)
17032 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
17033 else
17034 part_die->sibling = sibling_ptr;
17035 }
c906108c 17036 break;
fa4028e9
JB
17037 case DW_AT_byte_size:
17038 part_die->has_byte_size = 1;
17039 break;
ff908ebf
AW
17040 case DW_AT_const_value:
17041 part_die->has_const_value = 1;
17042 break;
68511cec
CES
17043 case DW_AT_calling_convention:
17044 /* DWARF doesn't provide a way to identify a program's source-level
17045 entry point. DW_AT_calling_convention attributes are only meant
17046 to describe functions' calling conventions.
17047
17048 However, because it's a necessary piece of information in
0c1b455e
TT
17049 Fortran, and before DWARF 4 DW_CC_program was the only
17050 piece of debugging information whose definition refers to
17051 a 'main program' at all, several compilers marked Fortran
17052 main programs with DW_CC_program --- even when those
17053 functions use the standard calling conventions.
17054
17055 Although DWARF now specifies a way to provide this
17056 information, we support this practice for backward
17057 compatibility. */
68511cec 17058 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
17059 && cu->language == language_fortran)
17060 part_die->main_subprogram = 1;
68511cec 17061 break;
481860b3
GB
17062 case DW_AT_inline:
17063 if (DW_UNSND (&attr) == DW_INL_inlined
17064 || DW_UNSND (&attr) == DW_INL_declared_inlined)
17065 part_die->may_be_inlined = 1;
17066 break;
95554aad
TT
17067
17068 case DW_AT_import:
17069 if (part_die->tag == DW_TAG_imported_unit)
36586728 17070 {
9c541725 17071 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
36586728
TT
17072 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17073 || cu->per_cu->is_dwz);
17074 }
95554aad
TT
17075 break;
17076
0c1b455e
TT
17077 case DW_AT_main_subprogram:
17078 part_die->main_subprogram = DW_UNSND (&attr);
17079 break;
17080
c906108c
SS
17081 default:
17082 break;
17083 }
17084 }
17085
91da1414
MW
17086 if (high_pc_relative)
17087 part_die->highpc += part_die->lowpc;
17088
9373cf26
JK
17089 if (has_low_pc_attr && has_high_pc_attr)
17090 {
17091 /* When using the GNU linker, .gnu.linkonce. sections are used to
17092 eliminate duplicate copies of functions and vtables and such.
17093 The linker will arbitrarily choose one and discard the others.
17094 The AT_*_pc values for such functions refer to local labels in
17095 these sections. If the section from that file was discarded, the
17096 labels are not in the output, so the relocs get a value of 0.
17097 If this is a discarded function, mark the pc bounds as invalid,
17098 so that GDB will ignore it. */
17099 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
17100 {
bb5ed363 17101 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
17102
17103 complaint (&symfile_complaints,
17104 _("DW_AT_low_pc %s is zero "
17105 "for DIE at 0x%x [in module %s]"),
17106 paddress (gdbarch, part_die->lowpc),
9c541725 17107 to_underlying (part_die->sect_off), objfile_name (objfile));
9373cf26
JK
17108 }
17109 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
17110 else if (part_die->lowpc >= part_die->highpc)
17111 {
bb5ed363 17112 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
17113
17114 complaint (&symfile_complaints,
17115 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
17116 "for DIE at 0x%x [in module %s]"),
17117 paddress (gdbarch, part_die->lowpc),
17118 paddress (gdbarch, part_die->highpc),
9c541725
PA
17119 to_underlying (part_die->sect_off),
17120 objfile_name (objfile));
9373cf26
JK
17121 }
17122 else
17123 part_die->has_pc_info = 1;
17124 }
85cbf3d3 17125
c906108c
SS
17126 return info_ptr;
17127}
17128
72bf9492
DJ
17129/* Find a cached partial DIE at OFFSET in CU. */
17130
17131static struct partial_die_info *
9c541725 17132find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
72bf9492
DJ
17133{
17134 struct partial_die_info *lookup_die = NULL;
17135 struct partial_die_info part_die;
17136
9c541725 17137 part_die.sect_off = sect_off;
9a3c8263
SM
17138 lookup_die = ((struct partial_die_info *)
17139 htab_find_with_hash (cu->partial_dies, &part_die,
9c541725 17140 to_underlying (sect_off)));
72bf9492 17141
72bf9492
DJ
17142 return lookup_die;
17143}
17144
348e048f
DE
17145/* Find a partial DIE at OFFSET, which may or may not be in CU,
17146 except in the case of .debug_types DIEs which do not reference
17147 outside their CU (they do however referencing other types via
55f1336d 17148 DW_FORM_ref_sig8). */
72bf9492
DJ
17149
17150static struct partial_die_info *
9c541725 17151find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 17152{
bb5ed363 17153 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
17154 struct dwarf2_per_cu_data *per_cu = NULL;
17155 struct partial_die_info *pd = NULL;
72bf9492 17156
36586728 17157 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 17158 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 17159 {
9c541725 17160 pd = find_partial_die_in_comp_unit (sect_off, cu);
5afb4e99
DJ
17161 if (pd != NULL)
17162 return pd;
0d99eb77
DE
17163 /* We missed recording what we needed.
17164 Load all dies and try again. */
17165 per_cu = cu->per_cu;
5afb4e99 17166 }
0d99eb77
DE
17167 else
17168 {
17169 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 17170 if (cu->per_cu->is_debug_types)
0d99eb77 17171 {
9c541725
PA
17172 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
17173 " external reference to offset 0x%x [in module %s].\n"),
17174 to_underlying (cu->header.sect_off), to_underlying (sect_off),
0d99eb77
DE
17175 bfd_get_filename (objfile->obfd));
17176 }
9c541725 17177 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 17178 objfile);
72bf9492 17179
0d99eb77
DE
17180 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
17181 load_partial_comp_unit (per_cu);
ae038cb0 17182
0d99eb77 17183 per_cu->cu->last_used = 0;
9c541725 17184 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
0d99eb77 17185 }
5afb4e99 17186
dee91e82
DE
17187 /* If we didn't find it, and not all dies have been loaded,
17188 load them all and try again. */
17189
5afb4e99
DJ
17190 if (pd == NULL && per_cu->load_all_dies == 0)
17191 {
5afb4e99 17192 per_cu->load_all_dies = 1;
fd820528
DE
17193
17194 /* This is nasty. When we reread the DIEs, somewhere up the call chain
17195 THIS_CU->cu may already be in use. So we can't just free it and
17196 replace its DIEs with the ones we read in. Instead, we leave those
17197 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
17198 and clobber THIS_CU->cu->partial_dies with the hash table for the new
17199 set. */
dee91e82 17200 load_partial_comp_unit (per_cu);
5afb4e99 17201
9c541725 17202 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
5afb4e99
DJ
17203 }
17204
17205 if (pd == NULL)
17206 internal_error (__FILE__, __LINE__,
3e43a32a
MS
17207 _("could not find partial DIE 0x%x "
17208 "in cache [from module %s]\n"),
9c541725 17209 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 17210 return pd;
72bf9492
DJ
17211}
17212
abc72ce4
DE
17213/* See if we can figure out if the class lives in a namespace. We do
17214 this by looking for a member function; its demangled name will
17215 contain namespace info, if there is any. */
17216
17217static void
17218guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
17219 struct dwarf2_cu *cu)
17220{
17221 /* NOTE: carlton/2003-10-07: Getting the info this way changes
17222 what template types look like, because the demangler
17223 frequently doesn't give the same name as the debug info. We
17224 could fix this by only using the demangled name to get the
17225 prefix (but see comment in read_structure_type). */
17226
17227 struct partial_die_info *real_pdi;
17228 struct partial_die_info *child_pdi;
17229
17230 /* If this DIE (this DIE's specification, if any) has a parent, then
17231 we should not do this. We'll prepend the parent's fully qualified
17232 name when we create the partial symbol. */
17233
17234 real_pdi = struct_pdi;
17235 while (real_pdi->has_specification)
36586728
TT
17236 real_pdi = find_partial_die (real_pdi->spec_offset,
17237 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
17238
17239 if (real_pdi->die_parent != NULL)
17240 return;
17241
17242 for (child_pdi = struct_pdi->die_child;
17243 child_pdi != NULL;
17244 child_pdi = child_pdi->die_sibling)
17245 {
17246 if (child_pdi->tag == DW_TAG_subprogram
17247 && child_pdi->linkage_name != NULL)
17248 {
17249 char *actual_class_name
17250 = language_class_name_from_physname (cu->language_defn,
17251 child_pdi->linkage_name);
17252 if (actual_class_name != NULL)
17253 {
17254 struct_pdi->name
224c3ddb
SM
17255 = ((const char *)
17256 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
17257 actual_class_name,
17258 strlen (actual_class_name)));
abc72ce4
DE
17259 xfree (actual_class_name);
17260 }
17261 break;
17262 }
17263 }
17264}
17265
72bf9492
DJ
17266/* Adjust PART_DIE before generating a symbol for it. This function
17267 may set the is_external flag or change the DIE's name. */
17268
17269static void
17270fixup_partial_die (struct partial_die_info *part_die,
17271 struct dwarf2_cu *cu)
17272{
abc72ce4
DE
17273 /* Once we've fixed up a die, there's no point in doing so again.
17274 This also avoids a memory leak if we were to call
17275 guess_partial_die_structure_name multiple times. */
17276 if (part_die->fixup_called)
17277 return;
17278
72bf9492
DJ
17279 /* If we found a reference attribute and the DIE has no name, try
17280 to find a name in the referred to DIE. */
17281
17282 if (part_die->name == NULL && part_die->has_specification)
17283 {
17284 struct partial_die_info *spec_die;
72bf9492 17285
36586728
TT
17286 spec_die = find_partial_die (part_die->spec_offset,
17287 part_die->spec_is_dwz, cu);
72bf9492 17288
10b3939b 17289 fixup_partial_die (spec_die, cu);
72bf9492
DJ
17290
17291 if (spec_die->name)
17292 {
17293 part_die->name = spec_die->name;
17294
17295 /* Copy DW_AT_external attribute if it is set. */
17296 if (spec_die->is_external)
17297 part_die->is_external = spec_die->is_external;
17298 }
17299 }
17300
17301 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
17302
17303 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 17304 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 17305
abc72ce4
DE
17306 /* If there is no parent die to provide a namespace, and there are
17307 children, see if we can determine the namespace from their linkage
122d1940 17308 name. */
abc72ce4 17309 if (cu->language == language_cplus
8b70b953 17310 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
17311 && part_die->die_parent == NULL
17312 && part_die->has_children
17313 && (part_die->tag == DW_TAG_class_type
17314 || part_die->tag == DW_TAG_structure_type
17315 || part_die->tag == DW_TAG_union_type))
17316 guess_partial_die_structure_name (part_die, cu);
17317
53832f31
TT
17318 /* GCC might emit a nameless struct or union that has a linkage
17319 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17320 if (part_die->name == NULL
96408a79
SA
17321 && (part_die->tag == DW_TAG_class_type
17322 || part_die->tag == DW_TAG_interface_type
17323 || part_die->tag == DW_TAG_structure_type
17324 || part_die->tag == DW_TAG_union_type)
53832f31
TT
17325 && part_die->linkage_name != NULL)
17326 {
17327 char *demangled;
17328
8de20a37 17329 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
17330 if (demangled)
17331 {
96408a79
SA
17332 const char *base;
17333
17334 /* Strip any leading namespaces/classes, keep only the base name.
17335 DW_AT_name for named DIEs does not contain the prefixes. */
17336 base = strrchr (demangled, ':');
17337 if (base && base > demangled && base[-1] == ':')
17338 base++;
17339 else
17340 base = demangled;
17341
34a68019 17342 part_die->name
224c3ddb
SM
17343 = ((const char *)
17344 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
17345 base, strlen (base)));
53832f31
TT
17346 xfree (demangled);
17347 }
17348 }
17349
abc72ce4 17350 part_die->fixup_called = 1;
72bf9492
DJ
17351}
17352
a8329558 17353/* Read an attribute value described by an attribute form. */
c906108c 17354
d521ce57 17355static const gdb_byte *
dee91e82
DE
17356read_attribute_value (const struct die_reader_specs *reader,
17357 struct attribute *attr, unsigned form,
43988095 17358 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 17359{
dee91e82 17360 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
17361 struct objfile *objfile = cu->objfile;
17362 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 17363 bfd *abfd = reader->abfd;
e7c27a73 17364 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
17365 unsigned int bytes_read;
17366 struct dwarf_block *blk;
17367
aead7601 17368 attr->form = (enum dwarf_form) form;
a8329558 17369 switch (form)
c906108c 17370 {
c906108c 17371 case DW_FORM_ref_addr:
ae411497 17372 if (cu->header.version == 2)
4568ecf9 17373 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 17374 else
4568ecf9
DE
17375 DW_UNSND (attr) = read_offset (abfd, info_ptr,
17376 &cu->header, &bytes_read);
ae411497
TT
17377 info_ptr += bytes_read;
17378 break;
36586728
TT
17379 case DW_FORM_GNU_ref_alt:
17380 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
17381 info_ptr += bytes_read;
17382 break;
ae411497 17383 case DW_FORM_addr:
e7c27a73 17384 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 17385 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 17386 info_ptr += bytes_read;
c906108c
SS
17387 break;
17388 case DW_FORM_block2:
7b5a2f43 17389 blk = dwarf_alloc_block (cu);
c906108c
SS
17390 blk->size = read_2_bytes (abfd, info_ptr);
17391 info_ptr += 2;
17392 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17393 info_ptr += blk->size;
17394 DW_BLOCK (attr) = blk;
17395 break;
17396 case DW_FORM_block4:
7b5a2f43 17397 blk = dwarf_alloc_block (cu);
c906108c
SS
17398 blk->size = read_4_bytes (abfd, info_ptr);
17399 info_ptr += 4;
17400 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17401 info_ptr += blk->size;
17402 DW_BLOCK (attr) = blk;
17403 break;
17404 case DW_FORM_data2:
17405 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
17406 info_ptr += 2;
17407 break;
17408 case DW_FORM_data4:
17409 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
17410 info_ptr += 4;
17411 break;
17412 case DW_FORM_data8:
17413 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
17414 info_ptr += 8;
17415 break;
0224619f
JK
17416 case DW_FORM_data16:
17417 blk = dwarf_alloc_block (cu);
17418 blk->size = 16;
17419 blk->data = read_n_bytes (abfd, info_ptr, 16);
17420 info_ptr += 16;
17421 DW_BLOCK (attr) = blk;
17422 break;
2dc7f7b3
TT
17423 case DW_FORM_sec_offset:
17424 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
17425 info_ptr += bytes_read;
17426 break;
c906108c 17427 case DW_FORM_string:
9b1c24c8 17428 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 17429 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
17430 info_ptr += bytes_read;
17431 break;
4bdf3d34 17432 case DW_FORM_strp:
36586728
TT
17433 if (!cu->per_cu->is_dwz)
17434 {
17435 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
17436 &bytes_read);
17437 DW_STRING_IS_CANONICAL (attr) = 0;
17438 info_ptr += bytes_read;
17439 break;
17440 }
17441 /* FALLTHROUGH */
43988095
JK
17442 case DW_FORM_line_strp:
17443 if (!cu->per_cu->is_dwz)
17444 {
17445 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
17446 cu_header, &bytes_read);
17447 DW_STRING_IS_CANONICAL (attr) = 0;
17448 info_ptr += bytes_read;
17449 break;
17450 }
17451 /* FALLTHROUGH */
36586728
TT
17452 case DW_FORM_GNU_strp_alt:
17453 {
17454 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17455 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
17456 &bytes_read);
17457
17458 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
17459 DW_STRING_IS_CANONICAL (attr) = 0;
17460 info_ptr += bytes_read;
17461 }
4bdf3d34 17462 break;
2dc7f7b3 17463 case DW_FORM_exprloc:
c906108c 17464 case DW_FORM_block:
7b5a2f43 17465 blk = dwarf_alloc_block (cu);
c906108c
SS
17466 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17467 info_ptr += bytes_read;
17468 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17469 info_ptr += blk->size;
17470 DW_BLOCK (attr) = blk;
17471 break;
17472 case DW_FORM_block1:
7b5a2f43 17473 blk = dwarf_alloc_block (cu);
c906108c
SS
17474 blk->size = read_1_byte (abfd, info_ptr);
17475 info_ptr += 1;
17476 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17477 info_ptr += blk->size;
17478 DW_BLOCK (attr) = blk;
17479 break;
17480 case DW_FORM_data1:
17481 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
17482 info_ptr += 1;
17483 break;
17484 case DW_FORM_flag:
17485 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
17486 info_ptr += 1;
17487 break;
2dc7f7b3
TT
17488 case DW_FORM_flag_present:
17489 DW_UNSND (attr) = 1;
17490 break;
c906108c
SS
17491 case DW_FORM_sdata:
17492 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17493 info_ptr += bytes_read;
17494 break;
17495 case DW_FORM_udata:
17496 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17497 info_ptr += bytes_read;
17498 break;
17499 case DW_FORM_ref1:
9c541725 17500 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17501 + read_1_byte (abfd, info_ptr));
c906108c
SS
17502 info_ptr += 1;
17503 break;
17504 case DW_FORM_ref2:
9c541725 17505 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17506 + read_2_bytes (abfd, info_ptr));
c906108c
SS
17507 info_ptr += 2;
17508 break;
17509 case DW_FORM_ref4:
9c541725 17510 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17511 + read_4_bytes (abfd, info_ptr));
c906108c
SS
17512 info_ptr += 4;
17513 break;
613e1657 17514 case DW_FORM_ref8:
9c541725 17515 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17516 + read_8_bytes (abfd, info_ptr));
613e1657
KB
17517 info_ptr += 8;
17518 break;
55f1336d 17519 case DW_FORM_ref_sig8:
ac9ec31b 17520 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
17521 info_ptr += 8;
17522 break;
c906108c 17523 case DW_FORM_ref_udata:
9c541725 17524 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 17525 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
17526 info_ptr += bytes_read;
17527 break;
c906108c 17528 case DW_FORM_indirect:
a8329558
KW
17529 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17530 info_ptr += bytes_read;
43988095
JK
17531 if (form == DW_FORM_implicit_const)
17532 {
17533 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17534 info_ptr += bytes_read;
17535 }
17536 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
17537 info_ptr);
17538 break;
17539 case DW_FORM_implicit_const:
17540 DW_SND (attr) = implicit_const;
a8329558 17541 break;
3019eac3
DE
17542 case DW_FORM_GNU_addr_index:
17543 if (reader->dwo_file == NULL)
17544 {
17545 /* For now flag a hard error.
17546 Later we can turn this into a complaint. */
17547 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
17548 dwarf_form_name (form),
17549 bfd_get_filename (abfd));
17550 }
17551 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
17552 info_ptr += bytes_read;
17553 break;
17554 case DW_FORM_GNU_str_index:
17555 if (reader->dwo_file == NULL)
17556 {
17557 /* For now flag a hard error.
17558 Later we can turn this into a complaint if warranted. */
17559 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
17560 dwarf_form_name (form),
17561 bfd_get_filename (abfd));
17562 }
17563 {
17564 ULONGEST str_index =
17565 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17566
342587c4 17567 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
17568 DW_STRING_IS_CANONICAL (attr) = 0;
17569 info_ptr += bytes_read;
17570 }
17571 break;
c906108c 17572 default:
8a3fe4f8 17573 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
17574 dwarf_form_name (form),
17575 bfd_get_filename (abfd));
c906108c 17576 }
28e94949 17577
36586728 17578 /* Super hack. */
7771576e 17579 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
17580 attr->form = DW_FORM_GNU_ref_alt;
17581
28e94949
JB
17582 /* We have seen instances where the compiler tried to emit a byte
17583 size attribute of -1 which ended up being encoded as an unsigned
17584 0xffffffff. Although 0xffffffff is technically a valid size value,
17585 an object of this size seems pretty unlikely so we can relatively
17586 safely treat these cases as if the size attribute was invalid and
17587 treat them as zero by default. */
17588 if (attr->name == DW_AT_byte_size
17589 && form == DW_FORM_data4
17590 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
17591 {
17592 complaint
17593 (&symfile_complaints,
43bbcdc2
PH
17594 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
17595 hex_string (DW_UNSND (attr)));
01c66ae6
JB
17596 DW_UNSND (attr) = 0;
17597 }
28e94949 17598
c906108c
SS
17599 return info_ptr;
17600}
17601
a8329558
KW
17602/* Read an attribute described by an abbreviated attribute. */
17603
d521ce57 17604static const gdb_byte *
dee91e82
DE
17605read_attribute (const struct die_reader_specs *reader,
17606 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 17607 const gdb_byte *info_ptr)
a8329558
KW
17608{
17609 attr->name = abbrev->name;
43988095
JK
17610 return read_attribute_value (reader, attr, abbrev->form,
17611 abbrev->implicit_const, info_ptr);
a8329558
KW
17612}
17613
0963b4bd 17614/* Read dwarf information from a buffer. */
c906108c
SS
17615
17616static unsigned int
a1855c1d 17617read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 17618{
fe1b8b76 17619 return bfd_get_8 (abfd, buf);
c906108c
SS
17620}
17621
17622static int
a1855c1d 17623read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 17624{
fe1b8b76 17625 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
17626}
17627
17628static unsigned int
a1855c1d 17629read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17630{
fe1b8b76 17631 return bfd_get_16 (abfd, buf);
c906108c
SS
17632}
17633
21ae7a4d 17634static int
a1855c1d 17635read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
17636{
17637 return bfd_get_signed_16 (abfd, buf);
17638}
17639
c906108c 17640static unsigned int
a1855c1d 17641read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17642{
fe1b8b76 17643 return bfd_get_32 (abfd, buf);
c906108c
SS
17644}
17645
21ae7a4d 17646static int
a1855c1d 17647read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
17648{
17649 return bfd_get_signed_32 (abfd, buf);
17650}
17651
93311388 17652static ULONGEST
a1855c1d 17653read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 17654{
fe1b8b76 17655 return bfd_get_64 (abfd, buf);
c906108c
SS
17656}
17657
17658static CORE_ADDR
d521ce57 17659read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 17660 unsigned int *bytes_read)
c906108c 17661{
e7c27a73 17662 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
17663 CORE_ADDR retval = 0;
17664
107d2387 17665 if (cu_header->signed_addr_p)
c906108c 17666 {
107d2387
AC
17667 switch (cu_header->addr_size)
17668 {
17669 case 2:
fe1b8b76 17670 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
17671 break;
17672 case 4:
fe1b8b76 17673 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
17674 break;
17675 case 8:
fe1b8b76 17676 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
17677 break;
17678 default:
8e65ff28 17679 internal_error (__FILE__, __LINE__,
e2e0b3e5 17680 _("read_address: bad switch, signed [in module %s]"),
659b0389 17681 bfd_get_filename (abfd));
107d2387
AC
17682 }
17683 }
17684 else
17685 {
17686 switch (cu_header->addr_size)
17687 {
17688 case 2:
fe1b8b76 17689 retval = bfd_get_16 (abfd, buf);
107d2387
AC
17690 break;
17691 case 4:
fe1b8b76 17692 retval = bfd_get_32 (abfd, buf);
107d2387
AC
17693 break;
17694 case 8:
fe1b8b76 17695 retval = bfd_get_64 (abfd, buf);
107d2387
AC
17696 break;
17697 default:
8e65ff28 17698 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
17699 _("read_address: bad switch, "
17700 "unsigned [in module %s]"),
659b0389 17701 bfd_get_filename (abfd));
107d2387 17702 }
c906108c 17703 }
64367e0a 17704
107d2387
AC
17705 *bytes_read = cu_header->addr_size;
17706 return retval;
c906108c
SS
17707}
17708
f7ef9339 17709/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
17710 specification allows the initial length to take up either 4 bytes
17711 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
17712 bytes describe the length and all offsets will be 8 bytes in length
17713 instead of 4.
17714
f7ef9339
KB
17715 An older, non-standard 64-bit format is also handled by this
17716 function. The older format in question stores the initial length
17717 as an 8-byte quantity without an escape value. Lengths greater
17718 than 2^32 aren't very common which means that the initial 4 bytes
17719 is almost always zero. Since a length value of zero doesn't make
17720 sense for the 32-bit format, this initial zero can be considered to
17721 be an escape value which indicates the presence of the older 64-bit
17722 format. As written, the code can't detect (old format) lengths
917c78fc
MK
17723 greater than 4GB. If it becomes necessary to handle lengths
17724 somewhat larger than 4GB, we could allow other small values (such
17725 as the non-sensical values of 1, 2, and 3) to also be used as
17726 escape values indicating the presence of the old format.
f7ef9339 17727
917c78fc
MK
17728 The value returned via bytes_read should be used to increment the
17729 relevant pointer after calling read_initial_length().
c764a876 17730
613e1657
KB
17731 [ Note: read_initial_length() and read_offset() are based on the
17732 document entitled "DWARF Debugging Information Format", revision
f7ef9339 17733 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
17734 from:
17735
f7ef9339 17736 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 17737
613e1657
KB
17738 This document is only a draft and is subject to change. (So beware.)
17739
f7ef9339 17740 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
17741 determined empirically by examining 64-bit ELF files produced by
17742 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
17743
17744 - Kevin, July 16, 2002
613e1657
KB
17745 ] */
17746
17747static LONGEST
d521ce57 17748read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 17749{
fe1b8b76 17750 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 17751
dd373385 17752 if (length == 0xffffffff)
613e1657 17753 {
fe1b8b76 17754 length = bfd_get_64 (abfd, buf + 4);
613e1657 17755 *bytes_read = 12;
613e1657 17756 }
dd373385 17757 else if (length == 0)
f7ef9339 17758 {
dd373385 17759 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 17760 length = bfd_get_64 (abfd, buf);
f7ef9339 17761 *bytes_read = 8;
f7ef9339 17762 }
613e1657
KB
17763 else
17764 {
17765 *bytes_read = 4;
613e1657
KB
17766 }
17767
c764a876
DE
17768 return length;
17769}
dd373385 17770
c764a876
DE
17771/* Cover function for read_initial_length.
17772 Returns the length of the object at BUF, and stores the size of the
17773 initial length in *BYTES_READ and stores the size that offsets will be in
17774 *OFFSET_SIZE.
17775 If the initial length size is not equivalent to that specified in
17776 CU_HEADER then issue a complaint.
17777 This is useful when reading non-comp-unit headers. */
dd373385 17778
c764a876 17779static LONGEST
d521ce57 17780read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
17781 const struct comp_unit_head *cu_header,
17782 unsigned int *bytes_read,
17783 unsigned int *offset_size)
17784{
17785 LONGEST length = read_initial_length (abfd, buf, bytes_read);
17786
17787 gdb_assert (cu_header->initial_length_size == 4
17788 || cu_header->initial_length_size == 8
17789 || cu_header->initial_length_size == 12);
17790
17791 if (cu_header->initial_length_size != *bytes_read)
17792 complaint (&symfile_complaints,
17793 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 17794
c764a876 17795 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 17796 return length;
613e1657
KB
17797}
17798
17799/* Read an offset from the data stream. The size of the offset is
917c78fc 17800 given by cu_header->offset_size. */
613e1657
KB
17801
17802static LONGEST
d521ce57
TT
17803read_offset (bfd *abfd, const gdb_byte *buf,
17804 const struct comp_unit_head *cu_header,
891d2f0b 17805 unsigned int *bytes_read)
c764a876
DE
17806{
17807 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 17808
c764a876
DE
17809 *bytes_read = cu_header->offset_size;
17810 return offset;
17811}
17812
17813/* Read an offset from the data stream. */
17814
17815static LONGEST
d521ce57 17816read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
17817{
17818 LONGEST retval = 0;
17819
c764a876 17820 switch (offset_size)
613e1657
KB
17821 {
17822 case 4:
fe1b8b76 17823 retval = bfd_get_32 (abfd, buf);
613e1657
KB
17824 break;
17825 case 8:
fe1b8b76 17826 retval = bfd_get_64 (abfd, buf);
613e1657
KB
17827 break;
17828 default:
8e65ff28 17829 internal_error (__FILE__, __LINE__,
c764a876 17830 _("read_offset_1: bad switch [in module %s]"),
659b0389 17831 bfd_get_filename (abfd));
613e1657
KB
17832 }
17833
917c78fc 17834 return retval;
613e1657
KB
17835}
17836
d521ce57
TT
17837static const gdb_byte *
17838read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
17839{
17840 /* If the size of a host char is 8 bits, we can return a pointer
17841 to the buffer, otherwise we have to copy the data to a buffer
17842 allocated on the temporary obstack. */
4bdf3d34 17843 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 17844 return buf;
c906108c
SS
17845}
17846
d521ce57
TT
17847static const char *
17848read_direct_string (bfd *abfd, const gdb_byte *buf,
17849 unsigned int *bytes_read_ptr)
c906108c
SS
17850{
17851 /* If the size of a host char is 8 bits, we can return a pointer
17852 to the string, otherwise we have to copy the string to a buffer
17853 allocated on the temporary obstack. */
4bdf3d34 17854 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
17855 if (*buf == '\0')
17856 {
17857 *bytes_read_ptr = 1;
17858 return NULL;
17859 }
d521ce57
TT
17860 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17861 return (const char *) buf;
4bdf3d34
JJ
17862}
17863
43988095
JK
17864/* Return pointer to string at section SECT offset STR_OFFSET with error
17865 reporting strings FORM_NAME and SECT_NAME. */
17866
d521ce57 17867static const char *
43988095
JK
17868read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17869 struct dwarf2_section_info *sect,
17870 const char *form_name,
17871 const char *sect_name)
17872{
17873 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17874 if (sect->buffer == NULL)
17875 error (_("%s used without %s section [in module %s]"),
17876 form_name, sect_name, bfd_get_filename (abfd));
17877 if (str_offset >= sect->size)
17878 error (_("%s pointing outside of %s section [in module %s]"),
17879 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 17880 gdb_assert (HOST_CHAR_BIT == 8);
43988095 17881 if (sect->buffer[str_offset] == '\0')
4bdf3d34 17882 return NULL;
43988095
JK
17883 return (const char *) (sect->buffer + str_offset);
17884}
17885
17886/* Return pointer to string at .debug_str offset STR_OFFSET. */
17887
17888static const char *
17889read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17890{
17891 return read_indirect_string_at_offset_from (abfd, str_offset,
17892 &dwarf2_per_objfile->str,
17893 "DW_FORM_strp", ".debug_str");
17894}
17895
17896/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17897
17898static const char *
17899read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17900{
17901 return read_indirect_string_at_offset_from (abfd, str_offset,
17902 &dwarf2_per_objfile->line_str,
17903 "DW_FORM_line_strp",
17904 ".debug_line_str");
c906108c
SS
17905}
17906
36586728
TT
17907/* Read a string at offset STR_OFFSET in the .debug_str section from
17908 the .dwz file DWZ. Throw an error if the offset is too large. If
17909 the string consists of a single NUL byte, return NULL; otherwise
17910 return a pointer to the string. */
17911
d521ce57 17912static const char *
36586728
TT
17913read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17914{
17915 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17916
17917 if (dwz->str.buffer == NULL)
17918 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17919 "section [in module %s]"),
17920 bfd_get_filename (dwz->dwz_bfd));
17921 if (str_offset >= dwz->str.size)
17922 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17923 ".debug_str section [in module %s]"),
17924 bfd_get_filename (dwz->dwz_bfd));
17925 gdb_assert (HOST_CHAR_BIT == 8);
17926 if (dwz->str.buffer[str_offset] == '\0')
17927 return NULL;
d521ce57 17928 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
17929}
17930
43988095
JK
17931/* Return pointer to string at .debug_str offset as read from BUF.
17932 BUF is assumed to be in a compilation unit described by CU_HEADER.
17933 Return *BYTES_READ_PTR count of bytes read from BUF. */
17934
d521ce57
TT
17935static const char *
17936read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
17937 const struct comp_unit_head *cu_header,
17938 unsigned int *bytes_read_ptr)
17939{
17940 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17941
17942 return read_indirect_string_at_offset (abfd, str_offset);
17943}
17944
43988095
JK
17945/* Return pointer to string at .debug_line_str offset as read from BUF.
17946 BUF is assumed to be in a compilation unit described by CU_HEADER.
17947 Return *BYTES_READ_PTR count of bytes read from BUF. */
17948
17949static const char *
17950read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17951 const struct comp_unit_head *cu_header,
17952 unsigned int *bytes_read_ptr)
17953{
17954 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17955
17956 return read_indirect_line_string_at_offset (abfd, str_offset);
17957}
17958
17959ULONGEST
d521ce57 17960read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 17961 unsigned int *bytes_read_ptr)
c906108c 17962{
12df843f 17963 ULONGEST result;
ce5d95e1 17964 unsigned int num_read;
870f88f7 17965 int shift;
c906108c
SS
17966 unsigned char byte;
17967
17968 result = 0;
17969 shift = 0;
17970 num_read = 0;
c906108c
SS
17971 while (1)
17972 {
fe1b8b76 17973 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17974 buf++;
17975 num_read++;
12df843f 17976 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
17977 if ((byte & 128) == 0)
17978 {
17979 break;
17980 }
17981 shift += 7;
17982 }
17983 *bytes_read_ptr = num_read;
17984 return result;
17985}
17986
12df843f 17987static LONGEST
d521ce57
TT
17988read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17989 unsigned int *bytes_read_ptr)
c906108c 17990{
12df843f 17991 LONGEST result;
870f88f7 17992 int shift, num_read;
c906108c
SS
17993 unsigned char byte;
17994
17995 result = 0;
17996 shift = 0;
c906108c 17997 num_read = 0;
c906108c
SS
17998 while (1)
17999 {
fe1b8b76 18000 byte = bfd_get_8 (abfd, buf);
c906108c
SS
18001 buf++;
18002 num_read++;
12df843f 18003 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
18004 shift += 7;
18005 if ((byte & 128) == 0)
18006 {
18007 break;
18008 }
18009 }
77e0b926 18010 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 18011 result |= -(((LONGEST) 1) << shift);
c906108c
SS
18012 *bytes_read_ptr = num_read;
18013 return result;
18014}
18015
3019eac3
DE
18016/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18017 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
18018 ADDR_SIZE is the size of addresses from the CU header. */
18019
18020static CORE_ADDR
18021read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
18022{
18023 struct objfile *objfile = dwarf2_per_objfile->objfile;
18024 bfd *abfd = objfile->obfd;
18025 const gdb_byte *info_ptr;
18026
18027 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
18028 if (dwarf2_per_objfile->addr.buffer == NULL)
18029 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 18030 objfile_name (objfile));
3019eac3
DE
18031 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
18032 error (_("DW_FORM_addr_index pointing outside of "
18033 ".debug_addr section [in module %s]"),
4262abfb 18034 objfile_name (objfile));
3019eac3
DE
18035 info_ptr = (dwarf2_per_objfile->addr.buffer
18036 + addr_base + addr_index * addr_size);
18037 if (addr_size == 4)
18038 return bfd_get_32 (abfd, info_ptr);
18039 else
18040 return bfd_get_64 (abfd, info_ptr);
18041}
18042
18043/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18044
18045static CORE_ADDR
18046read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18047{
18048 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
18049}
18050
18051/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18052
18053static CORE_ADDR
d521ce57 18054read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
18055 unsigned int *bytes_read)
18056{
18057 bfd *abfd = cu->objfile->obfd;
18058 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18059
18060 return read_addr_index (cu, addr_index);
18061}
18062
18063/* Data structure to pass results from dwarf2_read_addr_index_reader
18064 back to dwarf2_read_addr_index. */
18065
18066struct dwarf2_read_addr_index_data
18067{
18068 ULONGEST addr_base;
18069 int addr_size;
18070};
18071
18072/* die_reader_func for dwarf2_read_addr_index. */
18073
18074static void
18075dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 18076 const gdb_byte *info_ptr,
3019eac3
DE
18077 struct die_info *comp_unit_die,
18078 int has_children,
18079 void *data)
18080{
18081 struct dwarf2_cu *cu = reader->cu;
18082 struct dwarf2_read_addr_index_data *aidata =
18083 (struct dwarf2_read_addr_index_data *) data;
18084
18085 aidata->addr_base = cu->addr_base;
18086 aidata->addr_size = cu->header.addr_size;
18087}
18088
18089/* Given an index in .debug_addr, fetch the value.
18090 NOTE: This can be called during dwarf expression evaluation,
18091 long after the debug information has been read, and thus per_cu->cu
18092 may no longer exist. */
18093
18094CORE_ADDR
18095dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
18096 unsigned int addr_index)
18097{
18098 struct objfile *objfile = per_cu->objfile;
18099 struct dwarf2_cu *cu = per_cu->cu;
18100 ULONGEST addr_base;
18101 int addr_size;
18102
18103 /* This is intended to be called from outside this file. */
18104 dw2_setup (objfile);
18105
18106 /* We need addr_base and addr_size.
18107 If we don't have PER_CU->cu, we have to get it.
18108 Nasty, but the alternative is storing the needed info in PER_CU,
18109 which at this point doesn't seem justified: it's not clear how frequently
18110 it would get used and it would increase the size of every PER_CU.
18111 Entry points like dwarf2_per_cu_addr_size do a similar thing
18112 so we're not in uncharted territory here.
18113 Alas we need to be a bit more complicated as addr_base is contained
18114 in the DIE.
18115
18116 We don't need to read the entire CU(/TU).
18117 We just need the header and top level die.
a1b64ce1 18118
3019eac3 18119 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 18120 For now we skip this optimization. */
3019eac3
DE
18121
18122 if (cu != NULL)
18123 {
18124 addr_base = cu->addr_base;
18125 addr_size = cu->header.addr_size;
18126 }
18127 else
18128 {
18129 struct dwarf2_read_addr_index_data aidata;
18130
a1b64ce1
DE
18131 /* Note: We can't use init_cutu_and_read_dies_simple here,
18132 we need addr_base. */
18133 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
18134 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
18135 addr_base = aidata.addr_base;
18136 addr_size = aidata.addr_size;
18137 }
18138
18139 return read_addr_index_1 (addr_index, addr_base, addr_size);
18140}
18141
57d63ce2
DE
18142/* Given a DW_FORM_GNU_str_index, fetch the string.
18143 This is only used by the Fission support. */
3019eac3 18144
d521ce57 18145static const char *
342587c4 18146read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
18147{
18148 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 18149 const char *objf_name = objfile_name (objfile);
3019eac3 18150 bfd *abfd = objfile->obfd;
342587c4 18151 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
18152 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
18153 struct dwarf2_section_info *str_offsets_section =
18154 &reader->dwo_file->sections.str_offsets;
d521ce57 18155 const gdb_byte *info_ptr;
3019eac3 18156 ULONGEST str_offset;
57d63ce2 18157 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 18158
73869dc2
DE
18159 dwarf2_read_section (objfile, str_section);
18160 dwarf2_read_section (objfile, str_offsets_section);
18161 if (str_section->buffer == NULL)
57d63ce2 18162 error (_("%s used without .debug_str.dwo section"
9c541725
PA
18163 " in CU at offset 0x%x [in module %s]"),
18164 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 18165 if (str_offsets_section->buffer == NULL)
57d63ce2 18166 error (_("%s used without .debug_str_offsets.dwo section"
9c541725
PA
18167 " in CU at offset 0x%x [in module %s]"),
18168 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 18169 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 18170 error (_("%s pointing outside of .debug_str_offsets.dwo"
9c541725
PA
18171 " section in CU at offset 0x%x [in module %s]"),
18172 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 18173 info_ptr = (str_offsets_section->buffer
3019eac3
DE
18174 + str_index * cu->header.offset_size);
18175 if (cu->header.offset_size == 4)
18176 str_offset = bfd_get_32 (abfd, info_ptr);
18177 else
18178 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 18179 if (str_offset >= str_section->size)
57d63ce2 18180 error (_("Offset from %s pointing outside of"
9c541725
PA
18181 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
18182 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 18183 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
18184}
18185
3019eac3
DE
18186/* Return the length of an LEB128 number in BUF. */
18187
18188static int
18189leb128_size (const gdb_byte *buf)
18190{
18191 const gdb_byte *begin = buf;
18192 gdb_byte byte;
18193
18194 while (1)
18195 {
18196 byte = *buf++;
18197 if ((byte & 128) == 0)
18198 return buf - begin;
18199 }
18200}
18201
c906108c 18202static void
e142c38c 18203set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
18204{
18205 switch (lang)
18206 {
18207 case DW_LANG_C89:
76bee0cc 18208 case DW_LANG_C99:
0cfd832f 18209 case DW_LANG_C11:
c906108c 18210 case DW_LANG_C:
d1be3247 18211 case DW_LANG_UPC:
e142c38c 18212 cu->language = language_c;
c906108c 18213 break;
9c37b5ae 18214 case DW_LANG_Java:
c906108c 18215 case DW_LANG_C_plus_plus:
0cfd832f
MW
18216 case DW_LANG_C_plus_plus_11:
18217 case DW_LANG_C_plus_plus_14:
e142c38c 18218 cu->language = language_cplus;
c906108c 18219 break;
6aecb9c2
JB
18220 case DW_LANG_D:
18221 cu->language = language_d;
18222 break;
c906108c
SS
18223 case DW_LANG_Fortran77:
18224 case DW_LANG_Fortran90:
b21b22e0 18225 case DW_LANG_Fortran95:
f7de9aab
MW
18226 case DW_LANG_Fortran03:
18227 case DW_LANG_Fortran08:
e142c38c 18228 cu->language = language_fortran;
c906108c 18229 break;
a766d390
DE
18230 case DW_LANG_Go:
18231 cu->language = language_go;
18232 break;
c906108c 18233 case DW_LANG_Mips_Assembler:
e142c38c 18234 cu->language = language_asm;
c906108c
SS
18235 break;
18236 case DW_LANG_Ada83:
8aaf0b47 18237 case DW_LANG_Ada95:
bc5f45f8
JB
18238 cu->language = language_ada;
18239 break;
72019c9c
GM
18240 case DW_LANG_Modula2:
18241 cu->language = language_m2;
18242 break;
fe8e67fd
PM
18243 case DW_LANG_Pascal83:
18244 cu->language = language_pascal;
18245 break;
22566fbd
DJ
18246 case DW_LANG_ObjC:
18247 cu->language = language_objc;
18248 break;
c44af4eb
TT
18249 case DW_LANG_Rust:
18250 case DW_LANG_Rust_old:
18251 cu->language = language_rust;
18252 break;
c906108c
SS
18253 case DW_LANG_Cobol74:
18254 case DW_LANG_Cobol85:
c906108c 18255 default:
e142c38c 18256 cu->language = language_minimal;
c906108c
SS
18257 break;
18258 }
e142c38c 18259 cu->language_defn = language_def (cu->language);
c906108c
SS
18260}
18261
18262/* Return the named attribute or NULL if not there. */
18263
18264static struct attribute *
e142c38c 18265dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 18266{
a48e046c 18267 for (;;)
c906108c 18268 {
a48e046c
TT
18269 unsigned int i;
18270 struct attribute *spec = NULL;
18271
18272 for (i = 0; i < die->num_attrs; ++i)
18273 {
18274 if (die->attrs[i].name == name)
18275 return &die->attrs[i];
18276 if (die->attrs[i].name == DW_AT_specification
18277 || die->attrs[i].name == DW_AT_abstract_origin)
18278 spec = &die->attrs[i];
18279 }
18280
18281 if (!spec)
18282 break;
c906108c 18283
f2f0e013 18284 die = follow_die_ref (die, spec, &cu);
f2f0e013 18285 }
c5aa993b 18286
c906108c
SS
18287 return NULL;
18288}
18289
348e048f
DE
18290/* Return the named attribute or NULL if not there,
18291 but do not follow DW_AT_specification, etc.
18292 This is for use in contexts where we're reading .debug_types dies.
18293 Following DW_AT_specification, DW_AT_abstract_origin will take us
18294 back up the chain, and we want to go down. */
18295
18296static struct attribute *
45e58e77 18297dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
18298{
18299 unsigned int i;
18300
18301 for (i = 0; i < die->num_attrs; ++i)
18302 if (die->attrs[i].name == name)
18303 return &die->attrs[i];
18304
18305 return NULL;
18306}
18307
7d45c7c3
KB
18308/* Return the string associated with a string-typed attribute, or NULL if it
18309 is either not found or is of an incorrect type. */
18310
18311static const char *
18312dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18313{
18314 struct attribute *attr;
18315 const char *str = NULL;
18316
18317 attr = dwarf2_attr (die, name, cu);
18318
18319 if (attr != NULL)
18320 {
43988095 18321 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
18322 || attr->form == DW_FORM_string
18323 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 18324 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
18325 str = DW_STRING (attr);
18326 else
18327 complaint (&symfile_complaints,
18328 _("string type expected for attribute %s for "
18329 "DIE at 0x%x in module %s"),
9c541725 18330 dwarf_attr_name (name), to_underlying (die->sect_off),
7d45c7c3
KB
18331 objfile_name (cu->objfile));
18332 }
18333
18334 return str;
18335}
18336
05cf31d1
JB
18337/* Return non-zero iff the attribute NAME is defined for the given DIE,
18338 and holds a non-zero value. This function should only be used for
2dc7f7b3 18339 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
18340
18341static int
18342dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
18343{
18344 struct attribute *attr = dwarf2_attr (die, name, cu);
18345
18346 return (attr && DW_UNSND (attr));
18347}
18348
3ca72b44 18349static int
e142c38c 18350die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 18351{
05cf31d1
JB
18352 /* A DIE is a declaration if it has a DW_AT_declaration attribute
18353 which value is non-zero. However, we have to be careful with
18354 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
18355 (via dwarf2_flag_true_p) follows this attribute. So we may
18356 end up accidently finding a declaration attribute that belongs
18357 to a different DIE referenced by the specification attribute,
18358 even though the given DIE does not have a declaration attribute. */
18359 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
18360 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
18361}
18362
63d06c5c 18363/* Return the die giving the specification for DIE, if there is
f2f0e013 18364 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
18365 containing the return value on output. If there is no
18366 specification, but there is an abstract origin, that is
18367 returned. */
63d06c5c
DC
18368
18369static struct die_info *
f2f0e013 18370die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 18371{
f2f0e013
DJ
18372 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
18373 *spec_cu);
63d06c5c 18374
edb3359d
DJ
18375 if (spec_attr == NULL)
18376 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
18377
63d06c5c
DC
18378 if (spec_attr == NULL)
18379 return NULL;
18380 else
f2f0e013 18381 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 18382}
c906108c 18383
527f3840
JK
18384/* Stub for free_line_header to match void * callback types. */
18385
18386static void
18387free_line_header_voidp (void *arg)
18388{
9a3c8263 18389 struct line_header *lh = (struct line_header *) arg;
527f3840 18390
fff8551c 18391 delete lh;
527f3840
JK
18392}
18393
fff8551c
PA
18394void
18395line_header::add_include_dir (const char *include_dir)
c906108c 18396{
27e0867f 18397 if (dwarf_line_debug >= 2)
fff8551c
PA
18398 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
18399 include_dirs.size () + 1, include_dir);
27e0867f 18400
fff8551c 18401 include_dirs.push_back (include_dir);
debd256d 18402}
6e70227d 18403
fff8551c
PA
18404void
18405line_header::add_file_name (const char *name,
ecfb656c 18406 dir_index d_index,
fff8551c
PA
18407 unsigned int mod_time,
18408 unsigned int length)
debd256d 18409{
27e0867f
DE
18410 if (dwarf_line_debug >= 2)
18411 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 18412 (unsigned) file_names.size () + 1, name);
27e0867f 18413
ecfb656c 18414 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 18415}
6e70227d 18416
83769d0b 18417/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
18418
18419static struct dwarf2_section_info *
18420get_debug_line_section (struct dwarf2_cu *cu)
18421{
18422 struct dwarf2_section_info *section;
18423
18424 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
18425 DWO file. */
18426 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18427 section = &cu->dwo_unit->dwo_file->sections.line;
18428 else if (cu->per_cu->is_dwz)
18429 {
18430 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18431
18432 section = &dwz->line;
18433 }
18434 else
18435 section = &dwarf2_per_objfile->line;
18436
18437 return section;
18438}
18439
43988095
JK
18440/* Read directory or file name entry format, starting with byte of
18441 format count entries, ULEB128 pairs of entry formats, ULEB128 of
18442 entries count and the entries themselves in the described entry
18443 format. */
18444
18445static void
18446read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
18447 struct line_header *lh,
18448 const struct comp_unit_head *cu_header,
18449 void (*callback) (struct line_header *lh,
18450 const char *name,
ecfb656c 18451 dir_index d_index,
43988095
JK
18452 unsigned int mod_time,
18453 unsigned int length))
18454{
18455 gdb_byte format_count, formati;
18456 ULONGEST data_count, datai;
18457 const gdb_byte *buf = *bufp;
18458 const gdb_byte *format_header_data;
18459 int i;
18460 unsigned int bytes_read;
18461
18462 format_count = read_1_byte (abfd, buf);
18463 buf += 1;
18464 format_header_data = buf;
18465 for (formati = 0; formati < format_count; formati++)
18466 {
18467 read_unsigned_leb128 (abfd, buf, &bytes_read);
18468 buf += bytes_read;
18469 read_unsigned_leb128 (abfd, buf, &bytes_read);
18470 buf += bytes_read;
18471 }
18472
18473 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
18474 buf += bytes_read;
18475 for (datai = 0; datai < data_count; datai++)
18476 {
18477 const gdb_byte *format = format_header_data;
18478 struct file_entry fe;
18479
43988095
JK
18480 for (formati = 0; formati < format_count; formati++)
18481 {
ecfb656c 18482 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 18483 format += bytes_read;
43988095 18484
ecfb656c 18485 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 18486 format += bytes_read;
ecfb656c
PA
18487
18488 gdb::optional<const char *> string;
18489 gdb::optional<unsigned int> uint;
18490
43988095
JK
18491 switch (form)
18492 {
18493 case DW_FORM_string:
ecfb656c 18494 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
18495 buf += bytes_read;
18496 break;
18497
18498 case DW_FORM_line_strp:
ecfb656c
PA
18499 string.emplace (read_indirect_line_string (abfd, buf,
18500 cu_header,
18501 &bytes_read));
43988095
JK
18502 buf += bytes_read;
18503 break;
18504
18505 case DW_FORM_data1:
ecfb656c 18506 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
18507 buf += 1;
18508 break;
18509
18510 case DW_FORM_data2:
ecfb656c 18511 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
18512 buf += 2;
18513 break;
18514
18515 case DW_FORM_data4:
ecfb656c 18516 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
18517 buf += 4;
18518 break;
18519
18520 case DW_FORM_data8:
ecfb656c 18521 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
18522 buf += 8;
18523 break;
18524
18525 case DW_FORM_udata:
ecfb656c 18526 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
18527 buf += bytes_read;
18528 break;
18529
18530 case DW_FORM_block:
18531 /* It is valid only for DW_LNCT_timestamp which is ignored by
18532 current GDB. */
18533 break;
18534 }
ecfb656c
PA
18535
18536 switch (content_type)
18537 {
18538 case DW_LNCT_path:
18539 if (string.has_value ())
18540 fe.name = *string;
18541 break;
18542 case DW_LNCT_directory_index:
18543 if (uint.has_value ())
18544 fe.d_index = (dir_index) *uint;
18545 break;
18546 case DW_LNCT_timestamp:
18547 if (uint.has_value ())
18548 fe.mod_time = *uint;
18549 break;
18550 case DW_LNCT_size:
18551 if (uint.has_value ())
18552 fe.length = *uint;
18553 break;
18554 case DW_LNCT_MD5:
18555 break;
18556 default:
18557 complaint (&symfile_complaints,
18558 _("Unknown format content type %s"),
18559 pulongest (content_type));
18560 }
43988095
JK
18561 }
18562
ecfb656c 18563 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
18564 }
18565
18566 *bufp = buf;
18567}
18568
debd256d 18569/* Read the statement program header starting at OFFSET in
3019eac3 18570 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 18571 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
18572 Returns NULL if there is a problem reading the header, e.g., if it
18573 has a version we don't understand.
debd256d
JB
18574
18575 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
18576 the returned object point into the dwarf line section buffer,
18577 and must not be freed. */
ae2de4f8 18578
fff8551c 18579static line_header_up
9c541725 18580dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 18581{
d521ce57 18582 const gdb_byte *line_ptr;
c764a876 18583 unsigned int bytes_read, offset_size;
debd256d 18584 int i;
d521ce57 18585 const char *cur_dir, *cur_file;
3019eac3
DE
18586 struct dwarf2_section_info *section;
18587 bfd *abfd;
18588
36586728 18589 section = get_debug_line_section (cu);
3019eac3
DE
18590 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
18591 if (section->buffer == NULL)
debd256d 18592 {
3019eac3
DE
18593 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18594 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
18595 else
18596 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
18597 return 0;
18598 }
18599
fceca515
DE
18600 /* We can't do this until we know the section is non-empty.
18601 Only then do we know we have such a section. */
a32a8923 18602 abfd = get_section_bfd_owner (section);
fceca515 18603
a738430d
MK
18604 /* Make sure that at least there's room for the total_length field.
18605 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 18606 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 18607 {
4d3c2250 18608 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
18609 return 0;
18610 }
18611
fff8551c 18612 line_header_up lh (new line_header ());
debd256d 18613
9c541725 18614 lh->sect_off = sect_off;
527f3840
JK
18615 lh->offset_in_dwz = cu->per_cu->is_dwz;
18616
9c541725 18617 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 18618
a738430d 18619 /* Read in the header. */
6e70227d 18620 lh->total_length =
c764a876
DE
18621 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
18622 &bytes_read, &offset_size);
debd256d 18623 line_ptr += bytes_read;
3019eac3 18624 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 18625 {
4d3c2250 18626 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
18627 return 0;
18628 }
18629 lh->statement_program_end = line_ptr + lh->total_length;
18630 lh->version = read_2_bytes (abfd, line_ptr);
18631 line_ptr += 2;
43988095 18632 if (lh->version > 5)
cd366ee8
DE
18633 {
18634 /* This is a version we don't understand. The format could have
18635 changed in ways we don't handle properly so just punt. */
18636 complaint (&symfile_complaints,
18637 _("unsupported version in .debug_line section"));
18638 return NULL;
18639 }
43988095
JK
18640 if (lh->version >= 5)
18641 {
18642 gdb_byte segment_selector_size;
18643
18644 /* Skip address size. */
18645 read_1_byte (abfd, line_ptr);
18646 line_ptr += 1;
18647
18648 segment_selector_size = read_1_byte (abfd, line_ptr);
18649 line_ptr += 1;
18650 if (segment_selector_size != 0)
18651 {
18652 complaint (&symfile_complaints,
18653 _("unsupported segment selector size %u "
18654 "in .debug_line section"),
18655 segment_selector_size);
18656 return NULL;
18657 }
18658 }
c764a876
DE
18659 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
18660 line_ptr += offset_size;
debd256d
JB
18661 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
18662 line_ptr += 1;
2dc7f7b3
TT
18663 if (lh->version >= 4)
18664 {
18665 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
18666 line_ptr += 1;
18667 }
18668 else
18669 lh->maximum_ops_per_instruction = 1;
18670
18671 if (lh->maximum_ops_per_instruction == 0)
18672 {
18673 lh->maximum_ops_per_instruction = 1;
18674 complaint (&symfile_complaints,
3e43a32a
MS
18675 _("invalid maximum_ops_per_instruction "
18676 "in `.debug_line' section"));
2dc7f7b3
TT
18677 }
18678
debd256d
JB
18679 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
18680 line_ptr += 1;
18681 lh->line_base = read_1_signed_byte (abfd, line_ptr);
18682 line_ptr += 1;
18683 lh->line_range = read_1_byte (abfd, line_ptr);
18684 line_ptr += 1;
18685 lh->opcode_base = read_1_byte (abfd, line_ptr);
18686 line_ptr += 1;
fff8551c 18687 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
18688
18689 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
18690 for (i = 1; i < lh->opcode_base; ++i)
18691 {
18692 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
18693 line_ptr += 1;
18694 }
18695
43988095 18696 if (lh->version >= 5)
debd256d 18697 {
43988095 18698 /* Read directory table. */
fff8551c
PA
18699 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18700 [] (struct line_header *lh, const char *name,
ecfb656c 18701 dir_index d_index, unsigned int mod_time,
fff8551c
PA
18702 unsigned int length)
18703 {
18704 lh->add_include_dir (name);
18705 });
debd256d 18706
43988095 18707 /* Read file name table. */
fff8551c
PA
18708 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18709 [] (struct line_header *lh, const char *name,
ecfb656c 18710 dir_index d_index, unsigned int mod_time,
fff8551c
PA
18711 unsigned int length)
18712 {
ecfb656c 18713 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 18714 });
43988095
JK
18715 }
18716 else
debd256d 18717 {
43988095
JK
18718 /* Read directory table. */
18719 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18720 {
18721 line_ptr += bytes_read;
fff8551c 18722 lh->add_include_dir (cur_dir);
43988095 18723 }
debd256d
JB
18724 line_ptr += bytes_read;
18725
43988095
JK
18726 /* Read file name table. */
18727 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18728 {
ecfb656c
PA
18729 unsigned int mod_time, length;
18730 dir_index d_index;
43988095
JK
18731
18732 line_ptr += bytes_read;
ecfb656c 18733 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
18734 line_ptr += bytes_read;
18735 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18736 line_ptr += bytes_read;
18737 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18738 line_ptr += bytes_read;
18739
ecfb656c 18740 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
18741 }
18742 line_ptr += bytes_read;
debd256d 18743 }
6e70227d 18744 lh->statement_program_start = line_ptr;
debd256d 18745
3019eac3 18746 if (line_ptr > (section->buffer + section->size))
4d3c2250 18747 complaint (&symfile_complaints,
3e43a32a
MS
18748 _("line number info header doesn't "
18749 "fit in `.debug_line' section"));
debd256d 18750
debd256d
JB
18751 return lh;
18752}
c906108c 18753
c6da4cef
DE
18754/* Subroutine of dwarf_decode_lines to simplify it.
18755 Return the file name of the psymtab for included file FILE_INDEX
18756 in line header LH of PST.
18757 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18758 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
18759 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
18760
18761 The function creates dangling cleanup registration. */
c6da4cef 18762
d521ce57 18763static const char *
c6da4cef
DE
18764psymtab_include_file_name (const struct line_header *lh, int file_index,
18765 const struct partial_symtab *pst,
18766 const char *comp_dir)
18767{
8c43009f 18768 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
18769 const char *include_name = fe.name;
18770 const char *include_name_to_compare = include_name;
72b9f47f
TT
18771 const char *pst_filename;
18772 char *copied_name = NULL;
c6da4cef
DE
18773 int file_is_pst;
18774
8c43009f 18775 const char *dir_name = fe.include_dir (lh);
c6da4cef
DE
18776
18777 if (!IS_ABSOLUTE_PATH (include_name)
18778 && (dir_name != NULL || comp_dir != NULL))
18779 {
18780 /* Avoid creating a duplicate psymtab for PST.
18781 We do this by comparing INCLUDE_NAME and PST_FILENAME.
18782 Before we do the comparison, however, we need to account
18783 for DIR_NAME and COMP_DIR.
18784 First prepend dir_name (if non-NULL). If we still don't
18785 have an absolute path prepend comp_dir (if non-NULL).
18786 However, the directory we record in the include-file's
18787 psymtab does not contain COMP_DIR (to match the
18788 corresponding symtab(s)).
18789
18790 Example:
18791
18792 bash$ cd /tmp
18793 bash$ gcc -g ./hello.c
18794 include_name = "hello.c"
18795 dir_name = "."
18796 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
18797 DW_AT_name = "./hello.c"
18798
18799 */
c6da4cef
DE
18800
18801 if (dir_name != NULL)
18802 {
d521ce57
TT
18803 char *tem = concat (dir_name, SLASH_STRING,
18804 include_name, (char *)NULL);
18805
18806 make_cleanup (xfree, tem);
18807 include_name = tem;
c6da4cef 18808 include_name_to_compare = include_name;
c6da4cef
DE
18809 }
18810 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18811 {
d521ce57
TT
18812 char *tem = concat (comp_dir, SLASH_STRING,
18813 include_name, (char *)NULL);
18814
18815 make_cleanup (xfree, tem);
18816 include_name_to_compare = tem;
c6da4cef
DE
18817 }
18818 }
18819
18820 pst_filename = pst->filename;
18821 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18822 {
72b9f47f
TT
18823 copied_name = concat (pst->dirname, SLASH_STRING,
18824 pst_filename, (char *)NULL);
18825 pst_filename = copied_name;
c6da4cef
DE
18826 }
18827
1e3fad37 18828 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 18829
72b9f47f
TT
18830 if (copied_name != NULL)
18831 xfree (copied_name);
c6da4cef
DE
18832
18833 if (file_is_pst)
18834 return NULL;
18835 return include_name;
18836}
18837
d9b3de22
DE
18838/* State machine to track the state of the line number program. */
18839
6f77053d 18840class lnp_state_machine
d9b3de22 18841{
6f77053d
PA
18842public:
18843 /* Initialize a machine state for the start of a line number
18844 program. */
18845 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
18846
8c43009f
PA
18847 file_entry *current_file ()
18848 {
18849 /* lh->file_names is 0-based, but the file name numbers in the
18850 statement program are 1-based. */
6f77053d
PA
18851 return m_line_header->file_name_at (m_file);
18852 }
18853
18854 /* Record the line in the state machine. END_SEQUENCE is true if
18855 we're processing the end of a sequence. */
18856 void record_line (bool end_sequence);
18857
18858 /* Check address and if invalid nop-out the rest of the lines in this
18859 sequence. */
18860 void check_line_address (struct dwarf2_cu *cu,
18861 const gdb_byte *line_ptr,
18862 CORE_ADDR lowpc, CORE_ADDR address);
18863
18864 void handle_set_discriminator (unsigned int discriminator)
18865 {
18866 m_discriminator = discriminator;
18867 m_line_has_non_zero_discriminator |= discriminator != 0;
18868 }
18869
18870 /* Handle DW_LNE_set_address. */
18871 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
18872 {
18873 m_op_index = 0;
18874 address += baseaddr;
18875 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
18876 }
18877
18878 /* Handle DW_LNS_advance_pc. */
18879 void handle_advance_pc (CORE_ADDR adjust);
18880
18881 /* Handle a special opcode. */
18882 void handle_special_opcode (unsigned char op_code);
18883
18884 /* Handle DW_LNS_advance_line. */
18885 void handle_advance_line (int line_delta)
18886 {
18887 advance_line (line_delta);
18888 }
18889
18890 /* Handle DW_LNS_set_file. */
18891 void handle_set_file (file_name_index file);
18892
18893 /* Handle DW_LNS_negate_stmt. */
18894 void handle_negate_stmt ()
18895 {
18896 m_is_stmt = !m_is_stmt;
18897 }
18898
18899 /* Handle DW_LNS_const_add_pc. */
18900 void handle_const_add_pc ();
18901
18902 /* Handle DW_LNS_fixed_advance_pc. */
18903 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18904 {
18905 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18906 m_op_index = 0;
18907 }
18908
18909 /* Handle DW_LNS_copy. */
18910 void handle_copy ()
18911 {
18912 record_line (false);
18913 m_discriminator = 0;
18914 }
18915
18916 /* Handle DW_LNE_end_sequence. */
18917 void handle_end_sequence ()
18918 {
18919 m_record_line_callback = ::record_line;
18920 }
18921
18922private:
18923 /* Advance the line by LINE_DELTA. */
18924 void advance_line (int line_delta)
18925 {
18926 m_line += line_delta;
18927
18928 if (line_delta != 0)
18929 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
18930 }
18931
6f77053d
PA
18932 gdbarch *m_gdbarch;
18933
18934 /* True if we're recording lines.
18935 Otherwise we're building partial symtabs and are just interested in
18936 finding include files mentioned by the line number program. */
18937 bool m_record_lines_p;
18938
8c43009f 18939 /* The line number header. */
6f77053d 18940 line_header *m_line_header;
8c43009f 18941
6f77053d
PA
18942 /* These are part of the standard DWARF line number state machine,
18943 and initialized according to the DWARF spec. */
d9b3de22 18944
6f77053d 18945 unsigned char m_op_index = 0;
8c43009f 18946 /* The line table index (1-based) of the current file. */
6f77053d
PA
18947 file_name_index m_file = (file_name_index) 1;
18948 unsigned int m_line = 1;
18949
18950 /* These are initialized in the constructor. */
18951
18952 CORE_ADDR m_address;
18953 bool m_is_stmt;
18954 unsigned int m_discriminator;
d9b3de22
DE
18955
18956 /* Additional bits of state we need to track. */
18957
18958 /* The last file that we called dwarf2_start_subfile for.
18959 This is only used for TLLs. */
6f77053d 18960 unsigned int m_last_file = 0;
d9b3de22 18961 /* The last file a line number was recorded for. */
6f77053d 18962 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
18963
18964 /* The function to call to record a line. */
6f77053d 18965 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
18966
18967 /* The last line number that was recorded, used to coalesce
18968 consecutive entries for the same line. This can happen, for
18969 example, when discriminators are present. PR 17276. */
6f77053d
PA
18970 unsigned int m_last_line = 0;
18971 bool m_line_has_non_zero_discriminator = false;
8c43009f 18972};
d9b3de22 18973
6f77053d
PA
18974void
18975lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18976{
18977 CORE_ADDR addr_adj = (((m_op_index + adjust)
18978 / m_line_header->maximum_ops_per_instruction)
18979 * m_line_header->minimum_instruction_length);
18980 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18981 m_op_index = ((m_op_index + adjust)
18982 % m_line_header->maximum_ops_per_instruction);
18983}
d9b3de22 18984
6f77053d
PA
18985void
18986lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 18987{
6f77053d
PA
18988 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18989 CORE_ADDR addr_adj = (((m_op_index
18990 + (adj_opcode / m_line_header->line_range))
18991 / m_line_header->maximum_ops_per_instruction)
18992 * m_line_header->minimum_instruction_length);
18993 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18994 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
18995 % m_line_header->maximum_ops_per_instruction);
d9b3de22 18996
6f77053d
PA
18997 int line_delta = (m_line_header->line_base
18998 + (adj_opcode % m_line_header->line_range));
18999 advance_line (line_delta);
19000 record_line (false);
19001 m_discriminator = 0;
19002}
d9b3de22 19003
6f77053d
PA
19004void
19005lnp_state_machine::handle_set_file (file_name_index file)
19006{
19007 m_file = file;
19008
19009 const file_entry *fe = current_file ();
19010 if (fe == NULL)
19011 dwarf2_debug_line_missing_file_complaint ();
19012 else if (m_record_lines_p)
19013 {
19014 const char *dir = fe->include_dir (m_line_header);
19015
19016 m_last_subfile = current_subfile;
19017 m_line_has_non_zero_discriminator = m_discriminator != 0;
19018 dwarf2_start_subfile (fe->name, dir);
19019 }
19020}
19021
19022void
19023lnp_state_machine::handle_const_add_pc ()
19024{
19025 CORE_ADDR adjust
19026 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19027
19028 CORE_ADDR addr_adj
19029 = (((m_op_index + adjust)
19030 / m_line_header->maximum_ops_per_instruction)
19031 * m_line_header->minimum_instruction_length);
19032
19033 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19034 m_op_index = ((m_op_index + adjust)
19035 % m_line_header->maximum_ops_per_instruction);
19036}
d9b3de22 19037
c91513d8
PP
19038/* Ignore this record_line request. */
19039
19040static void
19041noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
19042{
19043 return;
19044}
19045
a05a36a5
DE
19046/* Return non-zero if we should add LINE to the line number table.
19047 LINE is the line to add, LAST_LINE is the last line that was added,
19048 LAST_SUBFILE is the subfile for LAST_LINE.
19049 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19050 had a non-zero discriminator.
19051
19052 We have to be careful in the presence of discriminators.
19053 E.g., for this line:
19054
19055 for (i = 0; i < 100000; i++);
19056
19057 clang can emit four line number entries for that one line,
19058 each with a different discriminator.
19059 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19060
19061 However, we want gdb to coalesce all four entries into one.
19062 Otherwise the user could stepi into the middle of the line and
19063 gdb would get confused about whether the pc really was in the
19064 middle of the line.
19065
19066 Things are further complicated by the fact that two consecutive
19067 line number entries for the same line is a heuristic used by gcc
19068 to denote the end of the prologue. So we can't just discard duplicate
19069 entries, we have to be selective about it. The heuristic we use is
19070 that we only collapse consecutive entries for the same line if at least
19071 one of those entries has a non-zero discriminator. PR 17276.
19072
19073 Note: Addresses in the line number state machine can never go backwards
19074 within one sequence, thus this coalescing is ok. */
19075
19076static int
19077dwarf_record_line_p (unsigned int line, unsigned int last_line,
19078 int line_has_non_zero_discriminator,
19079 struct subfile *last_subfile)
19080{
19081 if (current_subfile != last_subfile)
19082 return 1;
19083 if (line != last_line)
19084 return 1;
19085 /* Same line for the same file that we've seen already.
19086 As a last check, for pr 17276, only record the line if the line
19087 has never had a non-zero discriminator. */
19088 if (!line_has_non_zero_discriminator)
19089 return 1;
19090 return 0;
19091}
19092
252a6764
DE
19093/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
19094 in the line table of subfile SUBFILE. */
19095
19096static void
d9b3de22
DE
19097dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19098 unsigned int line, CORE_ADDR address,
19099 record_line_ftype p_record_line)
252a6764
DE
19100{
19101 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19102
27e0867f
DE
19103 if (dwarf_line_debug)
19104 {
19105 fprintf_unfiltered (gdb_stdlog,
19106 "Recording line %u, file %s, address %s\n",
19107 line, lbasename (subfile->name),
19108 paddress (gdbarch, address));
19109 }
19110
d5962de5 19111 (*p_record_line) (subfile, line, addr);
252a6764
DE
19112}
19113
19114/* Subroutine of dwarf_decode_lines_1 to simplify it.
19115 Mark the end of a set of line number records.
d9b3de22 19116 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
19117 If SUBFILE is NULL the request is ignored. */
19118
19119static void
19120dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19121 CORE_ADDR address, record_line_ftype p_record_line)
19122{
27e0867f
DE
19123 if (subfile == NULL)
19124 return;
19125
19126 if (dwarf_line_debug)
19127 {
19128 fprintf_unfiltered (gdb_stdlog,
19129 "Finishing current line, file %s, address %s\n",
19130 lbasename (subfile->name),
19131 paddress (gdbarch, address));
19132 }
19133
d9b3de22
DE
19134 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
19135}
19136
6f77053d
PA
19137void
19138lnp_state_machine::record_line (bool end_sequence)
d9b3de22 19139{
d9b3de22
DE
19140 if (dwarf_line_debug)
19141 {
19142 fprintf_unfiltered (gdb_stdlog,
19143 "Processing actual line %u: file %u,"
19144 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
19145 m_line, to_underlying (m_file),
19146 paddress (m_gdbarch, m_address),
19147 m_is_stmt, m_discriminator);
d9b3de22
DE
19148 }
19149
6f77053d 19150 file_entry *fe = current_file ();
8c43009f
PA
19151
19152 if (fe == NULL)
d9b3de22
DE
19153 dwarf2_debug_line_missing_file_complaint ();
19154 /* For now we ignore lines not starting on an instruction boundary.
19155 But not when processing end_sequence for compatibility with the
19156 previous version of the code. */
6f77053d 19157 else if (m_op_index == 0 || end_sequence)
d9b3de22 19158 {
8c43009f 19159 fe->included_p = 1;
6f77053d 19160 if (m_record_lines_p && m_is_stmt)
d9b3de22 19161 {
6f77053d 19162 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 19163 {
6f77053d
PA
19164 dwarf_finish_line (m_gdbarch, m_last_subfile,
19165 m_address, m_record_line_callback);
d9b3de22
DE
19166 }
19167
19168 if (!end_sequence)
19169 {
6f77053d
PA
19170 if (dwarf_record_line_p (m_line, m_last_line,
19171 m_line_has_non_zero_discriminator,
19172 m_last_subfile))
d9b3de22 19173 {
6f77053d
PA
19174 dwarf_record_line_1 (m_gdbarch, current_subfile,
19175 m_line, m_address,
19176 m_record_line_callback);
d9b3de22 19177 }
6f77053d
PA
19178 m_last_subfile = current_subfile;
19179 m_last_line = m_line;
d9b3de22
DE
19180 }
19181 }
19182 }
19183}
19184
6f77053d
PA
19185lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
19186 bool record_lines_p)
d9b3de22 19187{
6f77053d
PA
19188 m_gdbarch = arch;
19189 m_record_lines_p = record_lines_p;
19190 m_line_header = lh;
d9b3de22 19191
6f77053d 19192 m_record_line_callback = ::record_line;
d9b3de22 19193
d9b3de22
DE
19194 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19195 was a line entry for it so that the backend has a chance to adjust it
19196 and also record it in case it needs it. This is currently used by MIPS
19197 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
19198 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19199 m_is_stmt = lh->default_is_stmt;
19200 m_discriminator = 0;
252a6764
DE
19201}
19202
6f77053d
PA
19203void
19204lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19205 const gdb_byte *line_ptr,
19206 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
19207{
19208 /* If address < lowpc then it's not a usable value, it's outside the
19209 pc range of the CU. However, we restrict the test to only address
19210 values of zero to preserve GDB's previous behaviour which is to
19211 handle the specific case of a function being GC'd by the linker. */
19212
19213 if (address == 0 && address < lowpc)
19214 {
19215 /* This line table is for a function which has been
19216 GCd by the linker. Ignore it. PR gdb/12528 */
19217
19218 struct objfile *objfile = cu->objfile;
19219 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19220
19221 complaint (&symfile_complaints,
19222 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19223 line_offset, objfile_name (objfile));
6f77053d
PA
19224 m_record_line_callback = noop_record_line;
19225 /* Note: record_line_callback is left as noop_record_line until
19226 we see DW_LNE_end_sequence. */
924c2928
DE
19227 }
19228}
19229
f3f5162e 19230/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
19231 Process the line number information in LH.
19232 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19233 program in order to set included_p for every referenced header. */
debd256d 19234
c906108c 19235static void
43f3e411
DE
19236dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19237 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 19238{
d521ce57
TT
19239 const gdb_byte *line_ptr, *extended_end;
19240 const gdb_byte *line_end;
a8c50c1f 19241 unsigned int bytes_read, extended_len;
699ca60a 19242 unsigned char op_code, extended_op;
e142c38c
DJ
19243 CORE_ADDR baseaddr;
19244 struct objfile *objfile = cu->objfile;
f3f5162e 19245 bfd *abfd = objfile->obfd;
fbf65064 19246 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
19247 /* True if we're recording line info (as opposed to building partial
19248 symtabs and just interested in finding include files mentioned by
19249 the line number program). */
19250 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
19251
19252 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 19253
debd256d
JB
19254 line_ptr = lh->statement_program_start;
19255 line_end = lh->statement_program_end;
c906108c
SS
19256
19257 /* Read the statement sequences until there's nothing left. */
19258 while (line_ptr < line_end)
19259 {
6f77053d
PA
19260 /* The DWARF line number program state machine. Reset the state
19261 machine at the start of each sequence. */
19262 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
19263 bool end_sequence = false;
d9b3de22 19264
8c43009f 19265 if (record_lines_p)
c906108c 19266 {
8c43009f
PA
19267 /* Start a subfile for the current file of the state
19268 machine. */
19269 const file_entry *fe = state_machine.current_file ();
19270
19271 if (fe != NULL)
19272 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
19273 }
19274
a738430d 19275 /* Decode the table. */
d9b3de22 19276 while (line_ptr < line_end && !end_sequence)
c906108c
SS
19277 {
19278 op_code = read_1_byte (abfd, line_ptr);
19279 line_ptr += 1;
9aa1fe7e 19280
debd256d 19281 if (op_code >= lh->opcode_base)
6e70227d 19282 {
8e07a239 19283 /* Special opcode. */
6f77053d 19284 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
19285 }
19286 else switch (op_code)
c906108c
SS
19287 {
19288 case DW_LNS_extended_op:
3e43a32a
MS
19289 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19290 &bytes_read);
473b7be6 19291 line_ptr += bytes_read;
a8c50c1f 19292 extended_end = line_ptr + extended_len;
c906108c
SS
19293 extended_op = read_1_byte (abfd, line_ptr);
19294 line_ptr += 1;
19295 switch (extended_op)
19296 {
19297 case DW_LNE_end_sequence:
6f77053d
PA
19298 state_machine.handle_end_sequence ();
19299 end_sequence = true;
c906108c
SS
19300 break;
19301 case DW_LNE_set_address:
d9b3de22
DE
19302 {
19303 CORE_ADDR address
19304 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 19305 line_ptr += bytes_read;
6f77053d
PA
19306
19307 state_machine.check_line_address (cu, line_ptr,
19308 lowpc, address);
19309 state_machine.handle_set_address (baseaddr, address);
d9b3de22 19310 }
c906108c
SS
19311 break;
19312 case DW_LNE_define_file:
debd256d 19313 {
d521ce57 19314 const char *cur_file;
ecfb656c
PA
19315 unsigned int mod_time, length;
19316 dir_index dindex;
6e70227d 19317
3e43a32a
MS
19318 cur_file = read_direct_string (abfd, line_ptr,
19319 &bytes_read);
debd256d 19320 line_ptr += bytes_read;
ecfb656c 19321 dindex = (dir_index)
debd256d
JB
19322 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19323 line_ptr += bytes_read;
19324 mod_time =
19325 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19326 line_ptr += bytes_read;
19327 length =
19328 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19329 line_ptr += bytes_read;
ecfb656c 19330 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 19331 }
c906108c 19332 break;
d0c6ba3d 19333 case DW_LNE_set_discriminator:
6f77053d
PA
19334 {
19335 /* The discriminator is not interesting to the
19336 debugger; just ignore it. We still need to
19337 check its value though:
19338 if there are consecutive entries for the same
19339 (non-prologue) line we want to coalesce them.
19340 PR 17276. */
19341 unsigned int discr
19342 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19343 line_ptr += bytes_read;
19344
19345 state_machine.handle_set_discriminator (discr);
19346 }
d0c6ba3d 19347 break;
c906108c 19348 default:
4d3c2250 19349 complaint (&symfile_complaints,
e2e0b3e5 19350 _("mangled .debug_line section"));
debd256d 19351 return;
c906108c 19352 }
a8c50c1f
DJ
19353 /* Make sure that we parsed the extended op correctly. If e.g.
19354 we expected a different address size than the producer used,
19355 we may have read the wrong number of bytes. */
19356 if (line_ptr != extended_end)
19357 {
19358 complaint (&symfile_complaints,
19359 _("mangled .debug_line section"));
19360 return;
19361 }
c906108c
SS
19362 break;
19363 case DW_LNS_copy:
6f77053d 19364 state_machine.handle_copy ();
c906108c
SS
19365 break;
19366 case DW_LNS_advance_pc:
2dc7f7b3
TT
19367 {
19368 CORE_ADDR adjust
19369 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 19370 line_ptr += bytes_read;
6f77053d
PA
19371
19372 state_machine.handle_advance_pc (adjust);
2dc7f7b3 19373 }
c906108c
SS
19374 break;
19375 case DW_LNS_advance_line:
a05a36a5
DE
19376 {
19377 int line_delta
19378 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 19379 line_ptr += bytes_read;
6f77053d
PA
19380
19381 state_machine.handle_advance_line (line_delta);
a05a36a5 19382 }
c906108c
SS
19383 break;
19384 case DW_LNS_set_file:
d9b3de22 19385 {
6f77053d 19386 file_name_index file
ecfb656c
PA
19387 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19388 &bytes_read);
d9b3de22 19389 line_ptr += bytes_read;
8c43009f 19390
6f77053d 19391 state_machine.handle_set_file (file);
d9b3de22 19392 }
c906108c
SS
19393 break;
19394 case DW_LNS_set_column:
0ad93d4f 19395 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
19396 line_ptr += bytes_read;
19397 break;
19398 case DW_LNS_negate_stmt:
6f77053d 19399 state_machine.handle_negate_stmt ();
c906108c
SS
19400 break;
19401 case DW_LNS_set_basic_block:
c906108c 19402 break;
c2c6d25f
JM
19403 /* Add to the address register of the state machine the
19404 address increment value corresponding to special opcode
a738430d
MK
19405 255. I.e., this value is scaled by the minimum
19406 instruction length since special opcode 255 would have
b021a221 19407 scaled the increment. */
c906108c 19408 case DW_LNS_const_add_pc:
6f77053d 19409 state_machine.handle_const_add_pc ();
c906108c
SS
19410 break;
19411 case DW_LNS_fixed_advance_pc:
3e29f34a 19412 {
6f77053d 19413 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 19414 line_ptr += 2;
6f77053d
PA
19415
19416 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 19417 }
c906108c 19418 break;
9aa1fe7e 19419 default:
a738430d
MK
19420 {
19421 /* Unknown standard opcode, ignore it. */
9aa1fe7e 19422 int i;
a738430d 19423
debd256d 19424 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
19425 {
19426 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19427 line_ptr += bytes_read;
19428 }
19429 }
c906108c
SS
19430 }
19431 }
d9b3de22
DE
19432
19433 if (!end_sequence)
19434 dwarf2_debug_line_missing_end_sequence_complaint ();
19435
19436 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19437 in which case we still finish recording the last line). */
6f77053d 19438 state_machine.record_line (true);
c906108c 19439 }
f3f5162e
DE
19440}
19441
19442/* Decode the Line Number Program (LNP) for the given line_header
19443 structure and CU. The actual information extracted and the type
19444 of structures created from the LNP depends on the value of PST.
19445
19446 1. If PST is NULL, then this procedure uses the data from the program
19447 to create all necessary symbol tables, and their linetables.
19448
19449 2. If PST is not NULL, this procedure reads the program to determine
19450 the list of files included by the unit represented by PST, and
19451 builds all the associated partial symbol tables.
19452
19453 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19454 It is used for relative paths in the line table.
19455 NOTE: When processing partial symtabs (pst != NULL),
19456 comp_dir == pst->dirname.
19457
19458 NOTE: It is important that psymtabs have the same file name (via strcmp)
19459 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19460 symtab we don't use it in the name of the psymtabs we create.
19461 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
19462 A good testcase for this is mb-inline.exp.
19463
527f3840
JK
19464 LOWPC is the lowest address in CU (or 0 if not known).
19465
19466 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19467 for its PC<->lines mapping information. Otherwise only the filename
19468 table is read in. */
f3f5162e
DE
19469
19470static void
19471dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 19472 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 19473 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
19474{
19475 struct objfile *objfile = cu->objfile;
19476 const int decode_for_pst_p = (pst != NULL);
f3f5162e 19477
527f3840
JK
19478 if (decode_mapping)
19479 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
19480
19481 if (decode_for_pst_p)
19482 {
19483 int file_index;
19484
19485 /* Now that we're done scanning the Line Header Program, we can
19486 create the psymtab of each included file. */
fff8551c 19487 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
19488 if (lh->file_names[file_index].included_p == 1)
19489 {
d521ce57 19490 const char *include_name =
c6da4cef
DE
19491 psymtab_include_file_name (lh, file_index, pst, comp_dir);
19492 if (include_name != NULL)
aaa75496
JB
19493 dwarf2_create_include_psymtab (include_name, pst, objfile);
19494 }
19495 }
cb1df416
DJ
19496 else
19497 {
19498 /* Make sure a symtab is created for every file, even files
19499 which contain only variables (i.e. no code with associated
19500 line numbers). */
43f3e411 19501 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 19502 int i;
cb1df416 19503
fff8551c 19504 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 19505 {
8c43009f 19506 file_entry &fe = lh->file_names[i];
9a619af0 19507
8c43009f 19508 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 19509
cb1df416 19510 if (current_subfile->symtab == NULL)
43f3e411
DE
19511 {
19512 current_subfile->symtab
19513 = allocate_symtab (cust, current_subfile->name);
19514 }
8c43009f 19515 fe.symtab = current_subfile->symtab;
cb1df416
DJ
19516 }
19517 }
c906108c
SS
19518}
19519
19520/* Start a subfile for DWARF. FILENAME is the name of the file and
19521 DIRNAME the name of the source directory which contains FILENAME
4d663531 19522 or NULL if not known.
c906108c
SS
19523 This routine tries to keep line numbers from identical absolute and
19524 relative file names in a common subfile.
19525
19526 Using the `list' example from the GDB testsuite, which resides in
19527 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19528 of /srcdir/list0.c yields the following debugging information for list0.c:
19529
c5aa993b 19530 DW_AT_name: /srcdir/list0.c
4d663531 19531 DW_AT_comp_dir: /compdir
357e46e7 19532 files.files[0].name: list0.h
c5aa993b 19533 files.files[0].dir: /srcdir
357e46e7 19534 files.files[1].name: list0.c
c5aa993b 19535 files.files[1].dir: /srcdir
c906108c
SS
19536
19537 The line number information for list0.c has to end up in a single
4f1520fb
FR
19538 subfile, so that `break /srcdir/list0.c:1' works as expected.
19539 start_subfile will ensure that this happens provided that we pass the
19540 concatenation of files.files[1].dir and files.files[1].name as the
19541 subfile's name. */
c906108c
SS
19542
19543static void
4d663531 19544dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 19545{
d521ce57 19546 char *copy = NULL;
4f1520fb 19547
4d663531 19548 /* In order not to lose the line information directory,
4f1520fb
FR
19549 we concatenate it to the filename when it makes sense.
19550 Note that the Dwarf3 standard says (speaking of filenames in line
19551 information): ``The directory index is ignored for file names
19552 that represent full path names''. Thus ignoring dirname in the
19553 `else' branch below isn't an issue. */
c906108c 19554
d5166ae1 19555 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
19556 {
19557 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
19558 filename = copy;
19559 }
c906108c 19560
4d663531 19561 start_subfile (filename);
4f1520fb 19562
d521ce57
TT
19563 if (copy != NULL)
19564 xfree (copy);
c906108c
SS
19565}
19566
f4dc4d17
DE
19567/* Start a symtab for DWARF.
19568 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
19569
43f3e411 19570static struct compunit_symtab *
f4dc4d17 19571dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 19572 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 19573{
43f3e411 19574 struct compunit_symtab *cust
5ffa0793 19575 = start_symtab (cu->objfile, name, comp_dir, low_pc, cu->language);
43f3e411 19576
f4dc4d17
DE
19577 record_debugformat ("DWARF 2");
19578 record_producer (cu->producer);
19579
19580 /* We assume that we're processing GCC output. */
19581 processing_gcc_compilation = 2;
19582
4d4ec4e5 19583 cu->processing_has_namespace_info = 0;
43f3e411
DE
19584
19585 return cust;
f4dc4d17
DE
19586}
19587
4c2df51b
DJ
19588static void
19589var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 19590 struct dwarf2_cu *cu)
4c2df51b 19591{
e7c27a73
DJ
19592 struct objfile *objfile = cu->objfile;
19593 struct comp_unit_head *cu_header = &cu->header;
19594
4c2df51b
DJ
19595 /* NOTE drow/2003-01-30: There used to be a comment and some special
19596 code here to turn a symbol with DW_AT_external and a
19597 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19598 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19599 with some versions of binutils) where shared libraries could have
19600 relocations against symbols in their debug information - the
19601 minimal symbol would have the right address, but the debug info
19602 would not. It's no longer necessary, because we will explicitly
19603 apply relocations when we read in the debug information now. */
19604
19605 /* A DW_AT_location attribute with no contents indicates that a
19606 variable has been optimized away. */
19607 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
19608 {
f1e6e072 19609 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
19610 return;
19611 }
19612
19613 /* Handle one degenerate form of location expression specially, to
19614 preserve GDB's previous behavior when section offsets are
3019eac3
DE
19615 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
19616 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
19617
19618 if (attr_form_is_block (attr)
3019eac3
DE
19619 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19620 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19621 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19622 && (DW_BLOCK (attr)->size
19623 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 19624 {
891d2f0b 19625 unsigned int dummy;
4c2df51b 19626
3019eac3
DE
19627 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
19628 SYMBOL_VALUE_ADDRESS (sym) =
19629 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
19630 else
19631 SYMBOL_VALUE_ADDRESS (sym) =
19632 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 19633 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
19634 fixup_symbol_section (sym, objfile);
19635 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
19636 SYMBOL_SECTION (sym));
4c2df51b
DJ
19637 return;
19638 }
19639
19640 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
19641 expression evaluator, and use LOC_COMPUTED only when necessary
19642 (i.e. when the value of a register or memory location is
19643 referenced, or a thread-local block, etc.). Then again, it might
19644 not be worthwhile. I'm assuming that it isn't unless performance
19645 or memory numbers show me otherwise. */
19646
f1e6e072 19647 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 19648
f1e6e072 19649 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 19650 cu->has_loclist = 1;
4c2df51b
DJ
19651}
19652
c906108c
SS
19653/* Given a pointer to a DWARF information entry, figure out if we need
19654 to make a symbol table entry for it, and if so, create a new entry
19655 and return a pointer to it.
19656 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
19657 used the passed type.
19658 If SPACE is not NULL, use it to hold the new symbol. If it is
19659 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
19660
19661static struct symbol *
34eaf542
TT
19662new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
19663 struct symbol *space)
c906108c 19664{
e7c27a73 19665 struct objfile *objfile = cu->objfile;
3e29f34a 19666 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 19667 struct symbol *sym = NULL;
15d034d0 19668 const char *name;
c906108c
SS
19669 struct attribute *attr = NULL;
19670 struct attribute *attr2 = NULL;
e142c38c 19671 CORE_ADDR baseaddr;
e37fd15a
SW
19672 struct pending **list_to_add = NULL;
19673
edb3359d 19674 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
19675
19676 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 19677
94af9270 19678 name = dwarf2_name (die, cu);
c906108c
SS
19679 if (name)
19680 {
94af9270 19681 const char *linkagename;
34eaf542 19682 int suppress_add = 0;
94af9270 19683
34eaf542
TT
19684 if (space)
19685 sym = space;
19686 else
e623cf5d 19687 sym = allocate_symbol (objfile);
c906108c 19688 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
19689
19690 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 19691 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
19692 linkagename = dwarf2_physname (name, die, cu);
19693 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 19694
f55ee35c
JK
19695 /* Fortran does not have mangling standard and the mangling does differ
19696 between gfortran, iFort etc. */
19697 if (cu->language == language_fortran
b250c185 19698 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 19699 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 19700 dwarf2_full_name (name, die, cu),
29df156d 19701 NULL);
f55ee35c 19702
c906108c 19703 /* Default assumptions.
c5aa993b 19704 Use the passed type or decode it from the die. */
176620f1 19705 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 19706 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
19707 if (type != NULL)
19708 SYMBOL_TYPE (sym) = type;
19709 else
e7c27a73 19710 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
19711 attr = dwarf2_attr (die,
19712 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
19713 cu);
c906108c
SS
19714 if (attr)
19715 {
19716 SYMBOL_LINE (sym) = DW_UNSND (attr);
19717 }
cb1df416 19718
edb3359d
DJ
19719 attr = dwarf2_attr (die,
19720 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
19721 cu);
cb1df416
DJ
19722 if (attr)
19723 {
ecfb656c 19724 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 19725 struct file_entry *fe;
9a619af0 19726
ecfb656c
PA
19727 if (cu->line_header != NULL)
19728 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
19729 else
19730 fe = NULL;
19731
19732 if (fe == NULL)
cb1df416
DJ
19733 complaint (&symfile_complaints,
19734 _("file index out of range"));
8c43009f
PA
19735 else
19736 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
19737 }
19738
c906108c
SS
19739 switch (die->tag)
19740 {
19741 case DW_TAG_label:
e142c38c 19742 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 19743 if (attr)
3e29f34a
MR
19744 {
19745 CORE_ADDR addr;
19746
19747 addr = attr_value_as_address (attr);
19748 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
19749 SYMBOL_VALUE_ADDRESS (sym) = addr;
19750 }
0f5238ed
TT
19751 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
19752 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 19753 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 19754 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
19755 break;
19756 case DW_TAG_subprogram:
19757 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19758 finish_block. */
f1e6e072 19759 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 19760 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
19761 if ((attr2 && (DW_UNSND (attr2) != 0))
19762 || cu->language == language_ada)
c906108c 19763 {
2cfa0c8d
JB
19764 /* Subprograms marked external are stored as a global symbol.
19765 Ada subprograms, whether marked external or not, are always
19766 stored as a global symbol, because we want to be able to
19767 access them globally. For instance, we want to be able
19768 to break on a nested subprogram without having to
19769 specify the context. */
e37fd15a 19770 list_to_add = &global_symbols;
c906108c
SS
19771 }
19772 else
19773 {
e37fd15a 19774 list_to_add = cu->list_in_scope;
c906108c
SS
19775 }
19776 break;
edb3359d
DJ
19777 case DW_TAG_inlined_subroutine:
19778 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19779 finish_block. */
f1e6e072 19780 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 19781 SYMBOL_INLINED (sym) = 1;
481860b3 19782 list_to_add = cu->list_in_scope;
edb3359d 19783 break;
34eaf542
TT
19784 case DW_TAG_template_value_param:
19785 suppress_add = 1;
19786 /* Fall through. */
72929c62 19787 case DW_TAG_constant:
c906108c 19788 case DW_TAG_variable:
254e6b9e 19789 case DW_TAG_member:
0963b4bd
MS
19790 /* Compilation with minimal debug info may result in
19791 variables with missing type entries. Change the
19792 misleading `void' type to something sensible. */
c906108c 19793 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 19794 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 19795
e142c38c 19796 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
19797 /* In the case of DW_TAG_member, we should only be called for
19798 static const members. */
19799 if (die->tag == DW_TAG_member)
19800 {
3863f96c
DE
19801 /* dwarf2_add_field uses die_is_declaration,
19802 so we do the same. */
254e6b9e
DE
19803 gdb_assert (die_is_declaration (die, cu));
19804 gdb_assert (attr);
19805 }
c906108c
SS
19806 if (attr)
19807 {
e7c27a73 19808 dwarf2_const_value (attr, sym, cu);
e142c38c 19809 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 19810 if (!suppress_add)
34eaf542
TT
19811 {
19812 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 19813 list_to_add = &global_symbols;
34eaf542 19814 else
e37fd15a 19815 list_to_add = cu->list_in_scope;
34eaf542 19816 }
c906108c
SS
19817 break;
19818 }
e142c38c 19819 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19820 if (attr)
19821 {
e7c27a73 19822 var_decode_location (attr, sym, cu);
e142c38c 19823 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
19824
19825 /* Fortran explicitly imports any global symbols to the local
19826 scope by DW_TAG_common_block. */
19827 if (cu->language == language_fortran && die->parent
19828 && die->parent->tag == DW_TAG_common_block)
19829 attr2 = NULL;
19830
caac4577
JG
19831 if (SYMBOL_CLASS (sym) == LOC_STATIC
19832 && SYMBOL_VALUE_ADDRESS (sym) == 0
19833 && !dwarf2_per_objfile->has_section_at_zero)
19834 {
19835 /* When a static variable is eliminated by the linker,
19836 the corresponding debug information is not stripped
19837 out, but the variable address is set to null;
19838 do not add such variables into symbol table. */
19839 }
19840 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 19841 {
f55ee35c
JK
19842 /* Workaround gfortran PR debug/40040 - it uses
19843 DW_AT_location for variables in -fPIC libraries which may
19844 get overriden by other libraries/executable and get
19845 a different address. Resolve it by the minimal symbol
19846 which may come from inferior's executable using copy
19847 relocation. Make this workaround only for gfortran as for
19848 other compilers GDB cannot guess the minimal symbol
19849 Fortran mangling kind. */
19850 if (cu->language == language_fortran && die->parent
19851 && die->parent->tag == DW_TAG_module
19852 && cu->producer
28586665 19853 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 19854 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 19855
1c809c68
TT
19856 /* A variable with DW_AT_external is never static,
19857 but it may be block-scoped. */
19858 list_to_add = (cu->list_in_scope == &file_symbols
19859 ? &global_symbols : cu->list_in_scope);
1c809c68 19860 }
c906108c 19861 else
e37fd15a 19862 list_to_add = cu->list_in_scope;
c906108c
SS
19863 }
19864 else
19865 {
19866 /* We do not know the address of this symbol.
c5aa993b
JM
19867 If it is an external symbol and we have type information
19868 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19869 The address of the variable will then be determined from
19870 the minimal symbol table whenever the variable is
19871 referenced. */
e142c38c 19872 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
19873
19874 /* Fortran explicitly imports any global symbols to the local
19875 scope by DW_TAG_common_block. */
19876 if (cu->language == language_fortran && die->parent
19877 && die->parent->tag == DW_TAG_common_block)
19878 {
19879 /* SYMBOL_CLASS doesn't matter here because
19880 read_common_block is going to reset it. */
19881 if (!suppress_add)
19882 list_to_add = cu->list_in_scope;
19883 }
19884 else if (attr2 && (DW_UNSND (attr2) != 0)
19885 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 19886 {
0fe7935b
DJ
19887 /* A variable with DW_AT_external is never static, but it
19888 may be block-scoped. */
19889 list_to_add = (cu->list_in_scope == &file_symbols
19890 ? &global_symbols : cu->list_in_scope);
19891
f1e6e072 19892 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 19893 }
442ddf59
JK
19894 else if (!die_is_declaration (die, cu))
19895 {
19896 /* Use the default LOC_OPTIMIZED_OUT class. */
19897 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
19898 if (!suppress_add)
19899 list_to_add = cu->list_in_scope;
442ddf59 19900 }
c906108c
SS
19901 }
19902 break;
19903 case DW_TAG_formal_parameter:
edb3359d
DJ
19904 /* If we are inside a function, mark this as an argument. If
19905 not, we might be looking at an argument to an inlined function
19906 when we do not have enough information to show inlined frames;
19907 pretend it's a local variable in that case so that the user can
19908 still see it. */
19909 if (context_stack_depth > 0
19910 && context_stack[context_stack_depth - 1].name != NULL)
19911 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 19912 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19913 if (attr)
19914 {
e7c27a73 19915 var_decode_location (attr, sym, cu);
c906108c 19916 }
e142c38c 19917 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19918 if (attr)
19919 {
e7c27a73 19920 dwarf2_const_value (attr, sym, cu);
c906108c 19921 }
f346a30d 19922
e37fd15a 19923 list_to_add = cu->list_in_scope;
c906108c
SS
19924 break;
19925 case DW_TAG_unspecified_parameters:
19926 /* From varargs functions; gdb doesn't seem to have any
19927 interest in this information, so just ignore it for now.
19928 (FIXME?) */
19929 break;
34eaf542
TT
19930 case DW_TAG_template_type_param:
19931 suppress_add = 1;
19932 /* Fall through. */
c906108c 19933 case DW_TAG_class_type:
680b30c7 19934 case DW_TAG_interface_type:
c906108c
SS
19935 case DW_TAG_structure_type:
19936 case DW_TAG_union_type:
72019c9c 19937 case DW_TAG_set_type:
c906108c 19938 case DW_TAG_enumeration_type:
f1e6e072 19939 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19940 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 19941
63d06c5c 19942 {
9c37b5ae 19943 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
19944 really ever be static objects: otherwise, if you try
19945 to, say, break of a class's method and you're in a file
19946 which doesn't mention that class, it won't work unless
19947 the check for all static symbols in lookup_symbol_aux
19948 saves you. See the OtherFileClass tests in
19949 gdb.c++/namespace.exp. */
19950
e37fd15a 19951 if (!suppress_add)
34eaf542 19952 {
34eaf542 19953 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19954 && cu->language == language_cplus
34eaf542 19955 ? &global_symbols : cu->list_in_scope);
63d06c5c 19956
64382290 19957 /* The semantics of C++ state that "struct foo {
9c37b5ae 19958 ... }" also defines a typedef for "foo". */
64382290 19959 if (cu->language == language_cplus
45280282 19960 || cu->language == language_ada
c44af4eb
TT
19961 || cu->language == language_d
19962 || cu->language == language_rust)
64382290
TT
19963 {
19964 /* The symbol's name is already allocated along
19965 with this objfile, so we don't need to
19966 duplicate it for the type. */
19967 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19968 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19969 }
63d06c5c
DC
19970 }
19971 }
c906108c
SS
19972 break;
19973 case DW_TAG_typedef:
f1e6e072 19974 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 19975 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19976 list_to_add = cu->list_in_scope;
63d06c5c 19977 break;
c906108c 19978 case DW_TAG_base_type:
a02abb62 19979 case DW_TAG_subrange_type:
f1e6e072 19980 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19981 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19982 list_to_add = cu->list_in_scope;
c906108c
SS
19983 break;
19984 case DW_TAG_enumerator:
e142c38c 19985 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19986 if (attr)
19987 {
e7c27a73 19988 dwarf2_const_value (attr, sym, cu);
c906108c 19989 }
63d06c5c
DC
19990 {
19991 /* NOTE: carlton/2003-11-10: See comment above in the
19992 DW_TAG_class_type, etc. block. */
19993
e142c38c 19994 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19995 && cu->language == language_cplus
e142c38c 19996 ? &global_symbols : cu->list_in_scope);
63d06c5c 19997 }
c906108c 19998 break;
74921315 19999 case DW_TAG_imported_declaration:
5c4e30ca 20000 case DW_TAG_namespace:
f1e6e072 20001 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 20002 list_to_add = &global_symbols;
5c4e30ca 20003 break;
530e8392
KB
20004 case DW_TAG_module:
20005 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20006 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20007 list_to_add = &global_symbols;
20008 break;
4357ac6c 20009 case DW_TAG_common_block:
f1e6e072 20010 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
20011 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20012 add_symbol_to_list (sym, cu->list_in_scope);
20013 break;
c906108c
SS
20014 default:
20015 /* Not a tag we recognize. Hopefully we aren't processing
20016 trash data, but since we must specifically ignore things
20017 we don't recognize, there is nothing else we should do at
0963b4bd 20018 this point. */
e2e0b3e5 20019 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 20020 dwarf_tag_name (die->tag));
c906108c
SS
20021 break;
20022 }
df8a16a1 20023
e37fd15a
SW
20024 if (suppress_add)
20025 {
20026 sym->hash_next = objfile->template_symbols;
20027 objfile->template_symbols = sym;
20028 list_to_add = NULL;
20029 }
20030
20031 if (list_to_add != NULL)
20032 add_symbol_to_list (sym, list_to_add);
20033
df8a16a1
DJ
20034 /* For the benefit of old versions of GCC, check for anonymous
20035 namespaces based on the demangled name. */
4d4ec4e5 20036 if (!cu->processing_has_namespace_info
94af9270 20037 && cu->language == language_cplus)
a10964d1 20038 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
20039 }
20040 return (sym);
20041}
20042
34eaf542
TT
20043/* A wrapper for new_symbol_full that always allocates a new symbol. */
20044
20045static struct symbol *
20046new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20047{
20048 return new_symbol_full (die, type, cu, NULL);
20049}
20050
98bfdba5
PA
20051/* Given an attr with a DW_FORM_dataN value in host byte order,
20052 zero-extend it as appropriate for the symbol's type. The DWARF
20053 standard (v4) is not entirely clear about the meaning of using
20054 DW_FORM_dataN for a constant with a signed type, where the type is
20055 wider than the data. The conclusion of a discussion on the DWARF
20056 list was that this is unspecified. We choose to always zero-extend
20057 because that is the interpretation long in use by GCC. */
c906108c 20058
98bfdba5 20059static gdb_byte *
ff39bb5e 20060dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 20061 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 20062{
e7c27a73 20063 struct objfile *objfile = cu->objfile;
e17a4113
UW
20064 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20065 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
20066 LONGEST l = DW_UNSND (attr);
20067
20068 if (bits < sizeof (*value) * 8)
20069 {
20070 l &= ((LONGEST) 1 << bits) - 1;
20071 *value = l;
20072 }
20073 else if (bits == sizeof (*value) * 8)
20074 *value = l;
20075 else
20076 {
224c3ddb 20077 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
20078 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20079 return bytes;
20080 }
20081
20082 return NULL;
20083}
20084
20085/* Read a constant value from an attribute. Either set *VALUE, or if
20086 the value does not fit in *VALUE, set *BYTES - either already
20087 allocated on the objfile obstack, or newly allocated on OBSTACK,
20088 or, set *BATON, if we translated the constant to a location
20089 expression. */
20090
20091static void
ff39bb5e 20092dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
20093 const char *name, struct obstack *obstack,
20094 struct dwarf2_cu *cu,
d521ce57 20095 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
20096 struct dwarf2_locexpr_baton **baton)
20097{
20098 struct objfile *objfile = cu->objfile;
20099 struct comp_unit_head *cu_header = &cu->header;
c906108c 20100 struct dwarf_block *blk;
98bfdba5
PA
20101 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20102 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20103
20104 *value = 0;
20105 *bytes = NULL;
20106 *baton = NULL;
c906108c
SS
20107
20108 switch (attr->form)
20109 {
20110 case DW_FORM_addr:
3019eac3 20111 case DW_FORM_GNU_addr_index:
ac56253d 20112 {
ac56253d
TT
20113 gdb_byte *data;
20114
98bfdba5
PA
20115 if (TYPE_LENGTH (type) != cu_header->addr_size)
20116 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 20117 cu_header->addr_size,
98bfdba5 20118 TYPE_LENGTH (type));
ac56253d
TT
20119 /* Symbols of this form are reasonably rare, so we just
20120 piggyback on the existing location code rather than writing
20121 a new implementation of symbol_computed_ops. */
8d749320 20122 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
20123 (*baton)->per_cu = cu->per_cu;
20124 gdb_assert ((*baton)->per_cu);
ac56253d 20125
98bfdba5 20126 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 20127 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 20128 (*baton)->data = data;
ac56253d
TT
20129
20130 data[0] = DW_OP_addr;
20131 store_unsigned_integer (&data[1], cu_header->addr_size,
20132 byte_order, DW_ADDR (attr));
20133 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 20134 }
c906108c 20135 break;
4ac36638 20136 case DW_FORM_string:
93b5768b 20137 case DW_FORM_strp:
3019eac3 20138 case DW_FORM_GNU_str_index:
36586728 20139 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
20140 /* DW_STRING is already allocated on the objfile obstack, point
20141 directly to it. */
d521ce57 20142 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 20143 break;
c906108c
SS
20144 case DW_FORM_block1:
20145 case DW_FORM_block2:
20146 case DW_FORM_block4:
20147 case DW_FORM_block:
2dc7f7b3 20148 case DW_FORM_exprloc:
0224619f 20149 case DW_FORM_data16:
c906108c 20150 blk = DW_BLOCK (attr);
98bfdba5
PA
20151 if (TYPE_LENGTH (type) != blk->size)
20152 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20153 TYPE_LENGTH (type));
20154 *bytes = blk->data;
c906108c 20155 break;
2df3850c
JM
20156
20157 /* The DW_AT_const_value attributes are supposed to carry the
20158 symbol's value "represented as it would be on the target
20159 architecture." By the time we get here, it's already been
20160 converted to host endianness, so we just need to sign- or
20161 zero-extend it as appropriate. */
20162 case DW_FORM_data1:
3aef2284 20163 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 20164 break;
c906108c 20165 case DW_FORM_data2:
3aef2284 20166 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 20167 break;
c906108c 20168 case DW_FORM_data4:
3aef2284 20169 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 20170 break;
c906108c 20171 case DW_FORM_data8:
3aef2284 20172 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
20173 break;
20174
c906108c 20175 case DW_FORM_sdata:
663c44ac 20176 case DW_FORM_implicit_const:
98bfdba5 20177 *value = DW_SND (attr);
2df3850c
JM
20178 break;
20179
c906108c 20180 case DW_FORM_udata:
98bfdba5 20181 *value = DW_UNSND (attr);
c906108c 20182 break;
2df3850c 20183
c906108c 20184 default:
4d3c2250 20185 complaint (&symfile_complaints,
e2e0b3e5 20186 _("unsupported const value attribute form: '%s'"),
4d3c2250 20187 dwarf_form_name (attr->form));
98bfdba5 20188 *value = 0;
c906108c
SS
20189 break;
20190 }
20191}
20192
2df3850c 20193
98bfdba5
PA
20194/* Copy constant value from an attribute to a symbol. */
20195
2df3850c 20196static void
ff39bb5e 20197dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 20198 struct dwarf2_cu *cu)
2df3850c 20199{
98bfdba5 20200 struct objfile *objfile = cu->objfile;
12df843f 20201 LONGEST value;
d521ce57 20202 const gdb_byte *bytes;
98bfdba5 20203 struct dwarf2_locexpr_baton *baton;
2df3850c 20204
98bfdba5
PA
20205 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20206 SYMBOL_PRINT_NAME (sym),
20207 &objfile->objfile_obstack, cu,
20208 &value, &bytes, &baton);
2df3850c 20209
98bfdba5
PA
20210 if (baton != NULL)
20211 {
98bfdba5 20212 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 20213 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
20214 }
20215 else if (bytes != NULL)
20216 {
20217 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 20218 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
20219 }
20220 else
20221 {
20222 SYMBOL_VALUE (sym) = value;
f1e6e072 20223 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 20224 }
2df3850c
JM
20225}
20226
c906108c
SS
20227/* Return the type of the die in question using its DW_AT_type attribute. */
20228
20229static struct type *
e7c27a73 20230die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20231{
c906108c 20232 struct attribute *type_attr;
c906108c 20233
e142c38c 20234 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
20235 if (!type_attr)
20236 {
20237 /* A missing DW_AT_type represents a void type. */
46bf5051 20238 return objfile_type (cu->objfile)->builtin_void;
c906108c 20239 }
348e048f 20240
673bfd45 20241 return lookup_die_type (die, type_attr, cu);
c906108c
SS
20242}
20243
b4ba55a1
JB
20244/* True iff CU's producer generates GNAT Ada auxiliary information
20245 that allows to find parallel types through that information instead
20246 of having to do expensive parallel lookups by type name. */
20247
20248static int
20249need_gnat_info (struct dwarf2_cu *cu)
20250{
20251 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
20252 of GNAT produces this auxiliary information, without any indication
20253 that it is produced. Part of enhancing the FSF version of GNAT
20254 to produce that information will be to put in place an indicator
20255 that we can use in order to determine whether the descriptive type
20256 info is available or not. One suggestion that has been made is
20257 to use a new attribute, attached to the CU die. For now, assume
20258 that the descriptive type info is not available. */
20259 return 0;
20260}
20261
b4ba55a1
JB
20262/* Return the auxiliary type of the die in question using its
20263 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20264 attribute is not present. */
20265
20266static struct type *
20267die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20268{
b4ba55a1 20269 struct attribute *type_attr;
b4ba55a1
JB
20270
20271 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20272 if (!type_attr)
20273 return NULL;
20274
673bfd45 20275 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
20276}
20277
20278/* If DIE has a descriptive_type attribute, then set the TYPE's
20279 descriptive type accordingly. */
20280
20281static void
20282set_descriptive_type (struct type *type, struct die_info *die,
20283 struct dwarf2_cu *cu)
20284{
20285 struct type *descriptive_type = die_descriptive_type (die, cu);
20286
20287 if (descriptive_type)
20288 {
20289 ALLOCATE_GNAT_AUX_TYPE (type);
20290 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20291 }
20292}
20293
c906108c
SS
20294/* Return the containing type of the die in question using its
20295 DW_AT_containing_type attribute. */
20296
20297static struct type *
e7c27a73 20298die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20299{
c906108c 20300 struct attribute *type_attr;
c906108c 20301
e142c38c 20302 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
20303 if (!type_attr)
20304 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 20305 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 20306
673bfd45 20307 return lookup_die_type (die, type_attr, cu);
c906108c
SS
20308}
20309
ac9ec31b
DE
20310/* Return an error marker type to use for the ill formed type in DIE/CU. */
20311
20312static struct type *
20313build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20314{
20315 struct objfile *objfile = dwarf2_per_objfile->objfile;
20316 char *message, *saved;
20317
20318 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 20319 objfile_name (objfile),
9c541725
PA
20320 to_underlying (cu->header.sect_off),
20321 to_underlying (die->sect_off));
224c3ddb
SM
20322 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
20323 message, strlen (message));
ac9ec31b
DE
20324 xfree (message);
20325
19f392bc 20326 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
20327}
20328
673bfd45 20329/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
20330 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20331 DW_AT_containing_type.
673bfd45
DE
20332 If there is no type substitute an error marker. */
20333
c906108c 20334static struct type *
ff39bb5e 20335lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 20336 struct dwarf2_cu *cu)
c906108c 20337{
bb5ed363 20338 struct objfile *objfile = cu->objfile;
f792889a
DJ
20339 struct type *this_type;
20340
ac9ec31b
DE
20341 gdb_assert (attr->name == DW_AT_type
20342 || attr->name == DW_AT_GNAT_descriptive_type
20343 || attr->name == DW_AT_containing_type);
20344
673bfd45
DE
20345 /* First see if we have it cached. */
20346
36586728
TT
20347 if (attr->form == DW_FORM_GNU_ref_alt)
20348 {
20349 struct dwarf2_per_cu_data *per_cu;
9c541725 20350 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 20351
9c541725
PA
20352 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
20353 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 20354 }
7771576e 20355 else if (attr_form_is_ref (attr))
673bfd45 20356 {
9c541725 20357 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 20358
9c541725 20359 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 20360 }
55f1336d 20361 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 20362 {
ac9ec31b 20363 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 20364
ac9ec31b 20365 return get_signatured_type (die, signature, cu);
673bfd45
DE
20366 }
20367 else
20368 {
ac9ec31b
DE
20369 complaint (&symfile_complaints,
20370 _("Dwarf Error: Bad type attribute %s in DIE"
20371 " at 0x%x [in module %s]"),
9c541725 20372 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
4262abfb 20373 objfile_name (objfile));
ac9ec31b 20374 return build_error_marker_type (cu, die);
673bfd45
DE
20375 }
20376
20377 /* If not cached we need to read it in. */
20378
20379 if (this_type == NULL)
20380 {
ac9ec31b 20381 struct die_info *type_die = NULL;
673bfd45
DE
20382 struct dwarf2_cu *type_cu = cu;
20383
7771576e 20384 if (attr_form_is_ref (attr))
ac9ec31b
DE
20385 type_die = follow_die_ref (die, attr, &type_cu);
20386 if (type_die == NULL)
20387 return build_error_marker_type (cu, die);
20388 /* If we find the type now, it's probably because the type came
3019eac3
DE
20389 from an inter-CU reference and the type's CU got expanded before
20390 ours. */
ac9ec31b 20391 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
20392 }
20393
20394 /* If we still don't have a type use an error marker. */
20395
20396 if (this_type == NULL)
ac9ec31b 20397 return build_error_marker_type (cu, die);
673bfd45 20398
f792889a 20399 return this_type;
c906108c
SS
20400}
20401
673bfd45
DE
20402/* Return the type in DIE, CU.
20403 Returns NULL for invalid types.
20404
02142a6c 20405 This first does a lookup in die_type_hash,
673bfd45
DE
20406 and only reads the die in if necessary.
20407
20408 NOTE: This can be called when reading in partial or full symbols. */
20409
f792889a 20410static struct type *
e7c27a73 20411read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20412{
f792889a
DJ
20413 struct type *this_type;
20414
20415 this_type = get_die_type (die, cu);
20416 if (this_type)
20417 return this_type;
20418
673bfd45
DE
20419 return read_type_die_1 (die, cu);
20420}
20421
20422/* Read the type in DIE, CU.
20423 Returns NULL for invalid types. */
20424
20425static struct type *
20426read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20427{
20428 struct type *this_type = NULL;
20429
c906108c
SS
20430 switch (die->tag)
20431 {
20432 case DW_TAG_class_type:
680b30c7 20433 case DW_TAG_interface_type:
c906108c
SS
20434 case DW_TAG_structure_type:
20435 case DW_TAG_union_type:
f792889a 20436 this_type = read_structure_type (die, cu);
c906108c
SS
20437 break;
20438 case DW_TAG_enumeration_type:
f792889a 20439 this_type = read_enumeration_type (die, cu);
c906108c
SS
20440 break;
20441 case DW_TAG_subprogram:
20442 case DW_TAG_subroutine_type:
edb3359d 20443 case DW_TAG_inlined_subroutine:
f792889a 20444 this_type = read_subroutine_type (die, cu);
c906108c
SS
20445 break;
20446 case DW_TAG_array_type:
f792889a 20447 this_type = read_array_type (die, cu);
c906108c 20448 break;
72019c9c 20449 case DW_TAG_set_type:
f792889a 20450 this_type = read_set_type (die, cu);
72019c9c 20451 break;
c906108c 20452 case DW_TAG_pointer_type:
f792889a 20453 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
20454 break;
20455 case DW_TAG_ptr_to_member_type:
f792889a 20456 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
20457 break;
20458 case DW_TAG_reference_type:
4297a3f0
AV
20459 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20460 break;
20461 case DW_TAG_rvalue_reference_type:
20462 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
20463 break;
20464 case DW_TAG_const_type:
f792889a 20465 this_type = read_tag_const_type (die, cu);
c906108c
SS
20466 break;
20467 case DW_TAG_volatile_type:
f792889a 20468 this_type = read_tag_volatile_type (die, cu);
c906108c 20469 break;
06d66ee9
TT
20470 case DW_TAG_restrict_type:
20471 this_type = read_tag_restrict_type (die, cu);
20472 break;
c906108c 20473 case DW_TAG_string_type:
f792889a 20474 this_type = read_tag_string_type (die, cu);
c906108c
SS
20475 break;
20476 case DW_TAG_typedef:
f792889a 20477 this_type = read_typedef (die, cu);
c906108c 20478 break;
a02abb62 20479 case DW_TAG_subrange_type:
f792889a 20480 this_type = read_subrange_type (die, cu);
a02abb62 20481 break;
c906108c 20482 case DW_TAG_base_type:
f792889a 20483 this_type = read_base_type (die, cu);
c906108c 20484 break;
81a17f79 20485 case DW_TAG_unspecified_type:
f792889a 20486 this_type = read_unspecified_type (die, cu);
81a17f79 20487 break;
0114d602
DJ
20488 case DW_TAG_namespace:
20489 this_type = read_namespace_type (die, cu);
20490 break;
f55ee35c
JK
20491 case DW_TAG_module:
20492 this_type = read_module_type (die, cu);
20493 break;
a2c2acaf
MW
20494 case DW_TAG_atomic_type:
20495 this_type = read_tag_atomic_type (die, cu);
20496 break;
c906108c 20497 default:
3e43a32a
MS
20498 complaint (&symfile_complaints,
20499 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 20500 dwarf_tag_name (die->tag));
c906108c
SS
20501 break;
20502 }
63d06c5c 20503
f792889a 20504 return this_type;
63d06c5c
DC
20505}
20506
abc72ce4
DE
20507/* See if we can figure out if the class lives in a namespace. We do
20508 this by looking for a member function; its demangled name will
20509 contain namespace info, if there is any.
20510 Return the computed name or NULL.
20511 Space for the result is allocated on the objfile's obstack.
20512 This is the full-die version of guess_partial_die_structure_name.
20513 In this case we know DIE has no useful parent. */
20514
20515static char *
20516guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20517{
20518 struct die_info *spec_die;
20519 struct dwarf2_cu *spec_cu;
20520 struct die_info *child;
20521
20522 spec_cu = cu;
20523 spec_die = die_specification (die, &spec_cu);
20524 if (spec_die != NULL)
20525 {
20526 die = spec_die;
20527 cu = spec_cu;
20528 }
20529
20530 for (child = die->child;
20531 child != NULL;
20532 child = child->sibling)
20533 {
20534 if (child->tag == DW_TAG_subprogram)
20535 {
73b9be8b 20536 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 20537
7d45c7c3 20538 if (linkage_name != NULL)
abc72ce4
DE
20539 {
20540 char *actual_name
20541 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 20542 linkage_name);
abc72ce4
DE
20543 char *name = NULL;
20544
20545 if (actual_name != NULL)
20546 {
15d034d0 20547 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
20548
20549 if (die_name != NULL
20550 && strcmp (die_name, actual_name) != 0)
20551 {
20552 /* Strip off the class name from the full name.
20553 We want the prefix. */
20554 int die_name_len = strlen (die_name);
20555 int actual_name_len = strlen (actual_name);
20556
20557 /* Test for '::' as a sanity check. */
20558 if (actual_name_len > die_name_len + 2
3e43a32a
MS
20559 && actual_name[actual_name_len
20560 - die_name_len - 1] == ':')
224c3ddb
SM
20561 name = (char *) obstack_copy0 (
20562 &cu->objfile->per_bfd->storage_obstack,
20563 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
20564 }
20565 }
20566 xfree (actual_name);
20567 return name;
20568 }
20569 }
20570 }
20571
20572 return NULL;
20573}
20574
96408a79
SA
20575/* GCC might emit a nameless typedef that has a linkage name. Determine the
20576 prefix part in such case. See
20577 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20578
a121b7c1 20579static const char *
96408a79
SA
20580anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
20581{
20582 struct attribute *attr;
e6a959d6 20583 const char *base;
96408a79
SA
20584
20585 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
20586 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
20587 return NULL;
20588
7d45c7c3 20589 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
20590 return NULL;
20591
73b9be8b 20592 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
20593 if (attr == NULL || DW_STRING (attr) == NULL)
20594 return NULL;
20595
20596 /* dwarf2_name had to be already called. */
20597 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20598
20599 /* Strip the base name, keep any leading namespaces/classes. */
20600 base = strrchr (DW_STRING (attr), ':');
20601 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20602 return "";
20603
224c3ddb
SM
20604 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20605 DW_STRING (attr),
20606 &base[-1] - DW_STRING (attr));
96408a79
SA
20607}
20608
fdde2d81 20609/* Return the name of the namespace/class that DIE is defined within,
0114d602 20610 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 20611
0114d602
DJ
20612 For example, if we're within the method foo() in the following
20613 code:
20614
20615 namespace N {
20616 class C {
20617 void foo () {
20618 }
20619 };
20620 }
20621
20622 then determine_prefix on foo's die will return "N::C". */
fdde2d81 20623
0d5cff50 20624static const char *
e142c38c 20625determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 20626{
0114d602
DJ
20627 struct die_info *parent, *spec_die;
20628 struct dwarf2_cu *spec_cu;
20629 struct type *parent_type;
a121b7c1 20630 const char *retval;
63d06c5c 20631
9c37b5ae 20632 if (cu->language != language_cplus
c44af4eb
TT
20633 && cu->language != language_fortran && cu->language != language_d
20634 && cu->language != language_rust)
0114d602
DJ
20635 return "";
20636
96408a79
SA
20637 retval = anonymous_struct_prefix (die, cu);
20638 if (retval)
20639 return retval;
20640
0114d602
DJ
20641 /* We have to be careful in the presence of DW_AT_specification.
20642 For example, with GCC 3.4, given the code
20643
20644 namespace N {
20645 void foo() {
20646 // Definition of N::foo.
20647 }
20648 }
20649
20650 then we'll have a tree of DIEs like this:
20651
20652 1: DW_TAG_compile_unit
20653 2: DW_TAG_namespace // N
20654 3: DW_TAG_subprogram // declaration of N::foo
20655 4: DW_TAG_subprogram // definition of N::foo
20656 DW_AT_specification // refers to die #3
20657
20658 Thus, when processing die #4, we have to pretend that we're in
20659 the context of its DW_AT_specification, namely the contex of die
20660 #3. */
20661 spec_cu = cu;
20662 spec_die = die_specification (die, &spec_cu);
20663 if (spec_die == NULL)
20664 parent = die->parent;
20665 else
63d06c5c 20666 {
0114d602
DJ
20667 parent = spec_die->parent;
20668 cu = spec_cu;
63d06c5c 20669 }
0114d602
DJ
20670
20671 if (parent == NULL)
20672 return "";
98bfdba5
PA
20673 else if (parent->building_fullname)
20674 {
20675 const char *name;
20676 const char *parent_name;
20677
20678 /* It has been seen on RealView 2.2 built binaries,
20679 DW_TAG_template_type_param types actually _defined_ as
20680 children of the parent class:
20681
20682 enum E {};
20683 template class <class Enum> Class{};
20684 Class<enum E> class_e;
20685
20686 1: DW_TAG_class_type (Class)
20687 2: DW_TAG_enumeration_type (E)
20688 3: DW_TAG_enumerator (enum1:0)
20689 3: DW_TAG_enumerator (enum2:1)
20690 ...
20691 2: DW_TAG_template_type_param
20692 DW_AT_type DW_FORM_ref_udata (E)
20693
20694 Besides being broken debug info, it can put GDB into an
20695 infinite loop. Consider:
20696
20697 When we're building the full name for Class<E>, we'll start
20698 at Class, and go look over its template type parameters,
20699 finding E. We'll then try to build the full name of E, and
20700 reach here. We're now trying to build the full name of E,
20701 and look over the parent DIE for containing scope. In the
20702 broken case, if we followed the parent DIE of E, we'd again
20703 find Class, and once again go look at its template type
20704 arguments, etc., etc. Simply don't consider such parent die
20705 as source-level parent of this die (it can't be, the language
20706 doesn't allow it), and break the loop here. */
20707 name = dwarf2_name (die, cu);
20708 parent_name = dwarf2_name (parent, cu);
20709 complaint (&symfile_complaints,
20710 _("template param type '%s' defined within parent '%s'"),
20711 name ? name : "<unknown>",
20712 parent_name ? parent_name : "<unknown>");
20713 return "";
20714 }
63d06c5c 20715 else
0114d602
DJ
20716 switch (parent->tag)
20717 {
63d06c5c 20718 case DW_TAG_namespace:
0114d602 20719 parent_type = read_type_die (parent, cu);
acebe513
UW
20720 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
20721 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
20722 Work around this problem here. */
20723 if (cu->language == language_cplus
20724 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
20725 return "";
0114d602
DJ
20726 /* We give a name to even anonymous namespaces. */
20727 return TYPE_TAG_NAME (parent_type);
63d06c5c 20728 case DW_TAG_class_type:
680b30c7 20729 case DW_TAG_interface_type:
63d06c5c 20730 case DW_TAG_structure_type:
0114d602 20731 case DW_TAG_union_type:
f55ee35c 20732 case DW_TAG_module:
0114d602
DJ
20733 parent_type = read_type_die (parent, cu);
20734 if (TYPE_TAG_NAME (parent_type) != NULL)
20735 return TYPE_TAG_NAME (parent_type);
20736 else
20737 /* An anonymous structure is only allowed non-static data
20738 members; no typedefs, no member functions, et cetera.
20739 So it does not need a prefix. */
20740 return "";
abc72ce4 20741 case DW_TAG_compile_unit:
95554aad 20742 case DW_TAG_partial_unit:
abc72ce4
DE
20743 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
20744 if (cu->language == language_cplus
8b70b953 20745 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
20746 && die->child != NULL
20747 && (die->tag == DW_TAG_class_type
20748 || die->tag == DW_TAG_structure_type
20749 || die->tag == DW_TAG_union_type))
20750 {
20751 char *name = guess_full_die_structure_name (die, cu);
20752 if (name != NULL)
20753 return name;
20754 }
20755 return "";
3d567982
TT
20756 case DW_TAG_enumeration_type:
20757 parent_type = read_type_die (parent, cu);
20758 if (TYPE_DECLARED_CLASS (parent_type))
20759 {
20760 if (TYPE_TAG_NAME (parent_type) != NULL)
20761 return TYPE_TAG_NAME (parent_type);
20762 return "";
20763 }
20764 /* Fall through. */
63d06c5c 20765 default:
8176b9b8 20766 return determine_prefix (parent, cu);
63d06c5c 20767 }
63d06c5c
DC
20768}
20769
3e43a32a
MS
20770/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20771 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20772 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
20773 an obconcat, otherwise allocate storage for the result. The CU argument is
20774 used to determine the language and hence, the appropriate separator. */
987504bb 20775
f55ee35c 20776#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
20777
20778static char *
f55ee35c
JK
20779typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
20780 int physname, struct dwarf2_cu *cu)
63d06c5c 20781{
f55ee35c 20782 const char *lead = "";
5c315b68 20783 const char *sep;
63d06c5c 20784
3e43a32a
MS
20785 if (suffix == NULL || suffix[0] == '\0'
20786 || prefix == NULL || prefix[0] == '\0')
987504bb 20787 sep = "";
45280282
IB
20788 else if (cu->language == language_d)
20789 {
20790 /* For D, the 'main' function could be defined in any module, but it
20791 should never be prefixed. */
20792 if (strcmp (suffix, "D main") == 0)
20793 {
20794 prefix = "";
20795 sep = "";
20796 }
20797 else
20798 sep = ".";
20799 }
f55ee35c
JK
20800 else if (cu->language == language_fortran && physname)
20801 {
20802 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20803 DW_AT_MIPS_linkage_name is preferred and used instead. */
20804
20805 lead = "__";
20806 sep = "_MOD_";
20807 }
987504bb
JJ
20808 else
20809 sep = "::";
63d06c5c 20810
6dd47d34
DE
20811 if (prefix == NULL)
20812 prefix = "";
20813 if (suffix == NULL)
20814 suffix = "";
20815
987504bb
JJ
20816 if (obs == NULL)
20817 {
3e43a32a 20818 char *retval
224c3ddb
SM
20819 = ((char *)
20820 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 20821
f55ee35c
JK
20822 strcpy (retval, lead);
20823 strcat (retval, prefix);
6dd47d34
DE
20824 strcat (retval, sep);
20825 strcat (retval, suffix);
63d06c5c
DC
20826 return retval;
20827 }
987504bb
JJ
20828 else
20829 {
20830 /* We have an obstack. */
f55ee35c 20831 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 20832 }
63d06c5c
DC
20833}
20834
c906108c
SS
20835/* Return sibling of die, NULL if no sibling. */
20836
f9aca02d 20837static struct die_info *
fba45db2 20838sibling_die (struct die_info *die)
c906108c 20839{
639d11d3 20840 return die->sibling;
c906108c
SS
20841}
20842
71c25dea
TT
20843/* Get name of a die, return NULL if not found. */
20844
15d034d0
TT
20845static const char *
20846dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
20847 struct obstack *obstack)
20848{
20849 if (name && cu->language == language_cplus)
20850 {
2f408ecb 20851 std::string canon_name = cp_canonicalize_string (name);
71c25dea 20852
2f408ecb 20853 if (!canon_name.empty ())
71c25dea 20854 {
2f408ecb
PA
20855 if (canon_name != name)
20856 name = (const char *) obstack_copy0 (obstack,
20857 canon_name.c_str (),
20858 canon_name.length ());
71c25dea
TT
20859 }
20860 }
20861
20862 return name;
c906108c
SS
20863}
20864
96553a0c
DE
20865/* Get name of a die, return NULL if not found.
20866 Anonymous namespaces are converted to their magic string. */
9219021c 20867
15d034d0 20868static const char *
e142c38c 20869dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
20870{
20871 struct attribute *attr;
20872
e142c38c 20873 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 20874 if ((!attr || !DW_STRING (attr))
96553a0c 20875 && die->tag != DW_TAG_namespace
53832f31
TT
20876 && die->tag != DW_TAG_class_type
20877 && die->tag != DW_TAG_interface_type
20878 && die->tag != DW_TAG_structure_type
20879 && die->tag != DW_TAG_union_type)
71c25dea
TT
20880 return NULL;
20881
20882 switch (die->tag)
20883 {
20884 case DW_TAG_compile_unit:
95554aad 20885 case DW_TAG_partial_unit:
71c25dea
TT
20886 /* Compilation units have a DW_AT_name that is a filename, not
20887 a source language identifier. */
20888 case DW_TAG_enumeration_type:
20889 case DW_TAG_enumerator:
20890 /* These tags always have simple identifiers already; no need
20891 to canonicalize them. */
20892 return DW_STRING (attr);
907af001 20893
96553a0c
DE
20894 case DW_TAG_namespace:
20895 if (attr != NULL && DW_STRING (attr) != NULL)
20896 return DW_STRING (attr);
20897 return CP_ANONYMOUS_NAMESPACE_STR;
20898
907af001
UW
20899 case DW_TAG_class_type:
20900 case DW_TAG_interface_type:
20901 case DW_TAG_structure_type:
20902 case DW_TAG_union_type:
20903 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20904 structures or unions. These were of the form "._%d" in GCC 4.1,
20905 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20906 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 20907 if (attr && DW_STRING (attr)
61012eef
GB
20908 && (startswith (DW_STRING (attr), "._")
20909 || startswith (DW_STRING (attr), "<anonymous")))
907af001 20910 return NULL;
53832f31
TT
20911
20912 /* GCC might emit a nameless typedef that has a linkage name. See
20913 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20914 if (!attr || DW_STRING (attr) == NULL)
20915 {
df5c6c50 20916 char *demangled = NULL;
53832f31 20917
73b9be8b 20918 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
20919 if (attr == NULL || DW_STRING (attr) == NULL)
20920 return NULL;
20921
df5c6c50
JK
20922 /* Avoid demangling DW_STRING (attr) the second time on a second
20923 call for the same DIE. */
20924 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 20925 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
20926
20927 if (demangled)
20928 {
e6a959d6 20929 const char *base;
96408a79 20930
53832f31 20931 /* FIXME: we already did this for the partial symbol... */
34a68019 20932 DW_STRING (attr)
224c3ddb
SM
20933 = ((const char *)
20934 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20935 demangled, strlen (demangled)));
53832f31
TT
20936 DW_STRING_IS_CANONICAL (attr) = 1;
20937 xfree (demangled);
96408a79
SA
20938
20939 /* Strip any leading namespaces/classes, keep only the base name.
20940 DW_AT_name for named DIEs does not contain the prefixes. */
20941 base = strrchr (DW_STRING (attr), ':');
20942 if (base && base > DW_STRING (attr) && base[-1] == ':')
20943 return &base[1];
20944 else
20945 return DW_STRING (attr);
53832f31
TT
20946 }
20947 }
907af001
UW
20948 break;
20949
71c25dea 20950 default:
907af001
UW
20951 break;
20952 }
20953
20954 if (!DW_STRING_IS_CANONICAL (attr))
20955 {
20956 DW_STRING (attr)
20957 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 20958 &cu->objfile->per_bfd->storage_obstack);
907af001 20959 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 20960 }
907af001 20961 return DW_STRING (attr);
9219021c
DC
20962}
20963
20964/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
20965 is none. *EXT_CU is the CU containing DIE on input, and the CU
20966 containing the return value on output. */
9219021c
DC
20967
20968static struct die_info *
f2f0e013 20969dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
20970{
20971 struct attribute *attr;
9219021c 20972
f2f0e013 20973 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
20974 if (attr == NULL)
20975 return NULL;
20976
f2f0e013 20977 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
20978}
20979
c906108c
SS
20980/* Convert a DIE tag into its string name. */
20981
f39c6ffd 20982static const char *
aa1ee363 20983dwarf_tag_name (unsigned tag)
c906108c 20984{
f39c6ffd
TT
20985 const char *name = get_DW_TAG_name (tag);
20986
20987 if (name == NULL)
20988 return "DW_TAG_<unknown>";
20989
20990 return name;
c906108c
SS
20991}
20992
20993/* Convert a DWARF attribute code into its string name. */
20994
f39c6ffd 20995static const char *
aa1ee363 20996dwarf_attr_name (unsigned attr)
c906108c 20997{
f39c6ffd
TT
20998 const char *name;
20999
c764a876 21000#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
21001 if (attr == DW_AT_MIPS_fde)
21002 return "DW_AT_MIPS_fde";
21003#else
21004 if (attr == DW_AT_HP_block_index)
21005 return "DW_AT_HP_block_index";
c764a876 21006#endif
f39c6ffd
TT
21007
21008 name = get_DW_AT_name (attr);
21009
21010 if (name == NULL)
21011 return "DW_AT_<unknown>";
21012
21013 return name;
c906108c
SS
21014}
21015
21016/* Convert a DWARF value form code into its string name. */
21017
f39c6ffd 21018static const char *
aa1ee363 21019dwarf_form_name (unsigned form)
c906108c 21020{
f39c6ffd
TT
21021 const char *name = get_DW_FORM_name (form);
21022
21023 if (name == NULL)
21024 return "DW_FORM_<unknown>";
21025
21026 return name;
c906108c
SS
21027}
21028
a121b7c1 21029static const char *
fba45db2 21030dwarf_bool_name (unsigned mybool)
c906108c
SS
21031{
21032 if (mybool)
21033 return "TRUE";
21034 else
21035 return "FALSE";
21036}
21037
21038/* Convert a DWARF type code into its string name. */
21039
f39c6ffd 21040static const char *
aa1ee363 21041dwarf_type_encoding_name (unsigned enc)
c906108c 21042{
f39c6ffd 21043 const char *name = get_DW_ATE_name (enc);
c906108c 21044
f39c6ffd
TT
21045 if (name == NULL)
21046 return "DW_ATE_<unknown>";
c906108c 21047
f39c6ffd 21048 return name;
c906108c 21049}
c906108c 21050
f9aca02d 21051static void
d97bc12b 21052dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21053{
21054 unsigned int i;
21055
d97bc12b
DE
21056 print_spaces (indent, f);
21057 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
9c541725
PA
21058 dwarf_tag_name (die->tag), die->abbrev,
21059 to_underlying (die->sect_off));
d97bc12b
DE
21060
21061 if (die->parent != NULL)
21062 {
21063 print_spaces (indent, f);
21064 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9c541725 21065 to_underlying (die->parent->sect_off));
d97bc12b
DE
21066 }
21067
21068 print_spaces (indent, f);
21069 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21070 dwarf_bool_name (die->child != NULL));
c906108c 21071
d97bc12b
DE
21072 print_spaces (indent, f);
21073 fprintf_unfiltered (f, " attributes:\n");
21074
c906108c
SS
21075 for (i = 0; i < die->num_attrs; ++i)
21076 {
d97bc12b
DE
21077 print_spaces (indent, f);
21078 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21079 dwarf_attr_name (die->attrs[i].name),
21080 dwarf_form_name (die->attrs[i].form));
d97bc12b 21081
c906108c
SS
21082 switch (die->attrs[i].form)
21083 {
c906108c 21084 case DW_FORM_addr:
3019eac3 21085 case DW_FORM_GNU_addr_index:
d97bc12b 21086 fprintf_unfiltered (f, "address: ");
5af949e3 21087 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
21088 break;
21089 case DW_FORM_block2:
21090 case DW_FORM_block4:
21091 case DW_FORM_block:
21092 case DW_FORM_block1:
56eb65bd
SP
21093 fprintf_unfiltered (f, "block: size %s",
21094 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 21095 break;
2dc7f7b3 21096 case DW_FORM_exprloc:
56eb65bd
SP
21097 fprintf_unfiltered (f, "expression: size %s",
21098 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 21099 break;
0224619f
JK
21100 case DW_FORM_data16:
21101 fprintf_unfiltered (f, "constant of 16 bytes");
21102 break;
4568ecf9
DE
21103 case DW_FORM_ref_addr:
21104 fprintf_unfiltered (f, "ref address: ");
21105 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21106 break;
36586728
TT
21107 case DW_FORM_GNU_ref_alt:
21108 fprintf_unfiltered (f, "alt ref address: ");
21109 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21110 break;
10b3939b
DJ
21111 case DW_FORM_ref1:
21112 case DW_FORM_ref2:
21113 case DW_FORM_ref4:
4568ecf9
DE
21114 case DW_FORM_ref8:
21115 case DW_FORM_ref_udata:
d97bc12b 21116 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 21117 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 21118 break;
c906108c
SS
21119 case DW_FORM_data1:
21120 case DW_FORM_data2:
21121 case DW_FORM_data4:
ce5d95e1 21122 case DW_FORM_data8:
c906108c
SS
21123 case DW_FORM_udata:
21124 case DW_FORM_sdata:
43bbcdc2
PH
21125 fprintf_unfiltered (f, "constant: %s",
21126 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 21127 break;
2dc7f7b3
TT
21128 case DW_FORM_sec_offset:
21129 fprintf_unfiltered (f, "section offset: %s",
21130 pulongest (DW_UNSND (&die->attrs[i])));
21131 break;
55f1336d 21132 case DW_FORM_ref_sig8:
ac9ec31b
DE
21133 fprintf_unfiltered (f, "signature: %s",
21134 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 21135 break;
c906108c 21136 case DW_FORM_string:
4bdf3d34 21137 case DW_FORM_strp:
43988095 21138 case DW_FORM_line_strp:
3019eac3 21139 case DW_FORM_GNU_str_index:
36586728 21140 case DW_FORM_GNU_strp_alt:
8285870a 21141 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 21142 DW_STRING (&die->attrs[i])
8285870a
JK
21143 ? DW_STRING (&die->attrs[i]) : "",
21144 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
21145 break;
21146 case DW_FORM_flag:
21147 if (DW_UNSND (&die->attrs[i]))
d97bc12b 21148 fprintf_unfiltered (f, "flag: TRUE");
c906108c 21149 else
d97bc12b 21150 fprintf_unfiltered (f, "flag: FALSE");
c906108c 21151 break;
2dc7f7b3
TT
21152 case DW_FORM_flag_present:
21153 fprintf_unfiltered (f, "flag: TRUE");
21154 break;
a8329558 21155 case DW_FORM_indirect:
0963b4bd
MS
21156 /* The reader will have reduced the indirect form to
21157 the "base form" so this form should not occur. */
3e43a32a
MS
21158 fprintf_unfiltered (f,
21159 "unexpected attribute form: DW_FORM_indirect");
a8329558 21160 break;
663c44ac
JK
21161 case DW_FORM_implicit_const:
21162 fprintf_unfiltered (f, "constant: %s",
21163 plongest (DW_SND (&die->attrs[i])));
21164 break;
c906108c 21165 default:
d97bc12b 21166 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 21167 die->attrs[i].form);
d97bc12b 21168 break;
c906108c 21169 }
d97bc12b 21170 fprintf_unfiltered (f, "\n");
c906108c
SS
21171 }
21172}
21173
f9aca02d 21174static void
d97bc12b 21175dump_die_for_error (struct die_info *die)
c906108c 21176{
d97bc12b
DE
21177 dump_die_shallow (gdb_stderr, 0, die);
21178}
21179
21180static void
21181dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21182{
21183 int indent = level * 4;
21184
21185 gdb_assert (die != NULL);
21186
21187 if (level >= max_level)
21188 return;
21189
21190 dump_die_shallow (f, indent, die);
21191
21192 if (die->child != NULL)
c906108c 21193 {
d97bc12b
DE
21194 print_spaces (indent, f);
21195 fprintf_unfiltered (f, " Children:");
21196 if (level + 1 < max_level)
21197 {
21198 fprintf_unfiltered (f, "\n");
21199 dump_die_1 (f, level + 1, max_level, die->child);
21200 }
21201 else
21202 {
3e43a32a
MS
21203 fprintf_unfiltered (f,
21204 " [not printed, max nesting level reached]\n");
d97bc12b
DE
21205 }
21206 }
21207
21208 if (die->sibling != NULL && level > 0)
21209 {
21210 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
21211 }
21212}
21213
d97bc12b
DE
21214/* This is called from the pdie macro in gdbinit.in.
21215 It's not static so gcc will keep a copy callable from gdb. */
21216
21217void
21218dump_die (struct die_info *die, int max_level)
21219{
21220 dump_die_1 (gdb_stdlog, 0, max_level, die);
21221}
21222
f9aca02d 21223static void
51545339 21224store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21225{
51545339 21226 void **slot;
c906108c 21227
9c541725
PA
21228 slot = htab_find_slot_with_hash (cu->die_hash, die,
21229 to_underlying (die->sect_off),
b64f50a1 21230 INSERT);
51545339
DJ
21231
21232 *slot = die;
c906108c
SS
21233}
21234
b64f50a1
JK
21235/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
21236 required kind. */
21237
21238static sect_offset
ff39bb5e 21239dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 21240{
7771576e 21241 if (attr_form_is_ref (attr))
9c541725 21242 return (sect_offset) DW_UNSND (attr);
93311388
DE
21243
21244 complaint (&symfile_complaints,
21245 _("unsupported die ref attribute form: '%s'"),
21246 dwarf_form_name (attr->form));
9c541725 21247 return {};
c906108c
SS
21248}
21249
43bbcdc2
PH
21250/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
21251 * the value held by the attribute is not constant. */
a02abb62 21252
43bbcdc2 21253static LONGEST
ff39bb5e 21254dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 21255{
663c44ac 21256 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
21257 return DW_SND (attr);
21258 else if (attr->form == DW_FORM_udata
21259 || attr->form == DW_FORM_data1
21260 || attr->form == DW_FORM_data2
21261 || attr->form == DW_FORM_data4
21262 || attr->form == DW_FORM_data8)
21263 return DW_UNSND (attr);
21264 else
21265 {
0224619f 21266 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
21267 complaint (&symfile_complaints,
21268 _("Attribute value is not a constant (%s)"),
a02abb62
JB
21269 dwarf_form_name (attr->form));
21270 return default_value;
21271 }
21272}
21273
348e048f
DE
21274/* Follow reference or signature attribute ATTR of SRC_DIE.
21275 On entry *REF_CU is the CU of SRC_DIE.
21276 On exit *REF_CU is the CU of the result. */
21277
21278static struct die_info *
ff39bb5e 21279follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
21280 struct dwarf2_cu **ref_cu)
21281{
21282 struct die_info *die;
21283
7771576e 21284 if (attr_form_is_ref (attr))
348e048f 21285 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 21286 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
21287 die = follow_die_sig (src_die, attr, ref_cu);
21288 else
21289 {
21290 dump_die_for_error (src_die);
21291 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 21292 objfile_name ((*ref_cu)->objfile));
348e048f
DE
21293 }
21294
21295 return die;
03dd20cc
DJ
21296}
21297
5c631832 21298/* Follow reference OFFSET.
673bfd45
DE
21299 On entry *REF_CU is the CU of the source die referencing OFFSET.
21300 On exit *REF_CU is the CU of the result.
21301 Returns NULL if OFFSET is invalid. */
f504f079 21302
f9aca02d 21303static struct die_info *
9c541725 21304follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 21305 struct dwarf2_cu **ref_cu)
c906108c 21306{
10b3939b 21307 struct die_info temp_die;
f2f0e013 21308 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 21309
348e048f
DE
21310 gdb_assert (cu->per_cu != NULL);
21311
98bfdba5
PA
21312 target_cu = cu;
21313
3019eac3 21314 if (cu->per_cu->is_debug_types)
348e048f
DE
21315 {
21316 /* .debug_types CUs cannot reference anything outside their CU.
21317 If they need to, they have to reference a signatured type via
55f1336d 21318 DW_FORM_ref_sig8. */
9c541725 21319 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 21320 return NULL;
348e048f 21321 }
36586728 21322 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 21323 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
21324 {
21325 struct dwarf2_per_cu_data *per_cu;
9a619af0 21326
9c541725 21327 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 21328 cu->objfile);
03dd20cc
DJ
21329
21330 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
21331 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21332 load_full_comp_unit (per_cu, cu->language);
03dd20cc 21333
10b3939b
DJ
21334 target_cu = per_cu->cu;
21335 }
98bfdba5
PA
21336 else if (cu->dies == NULL)
21337 {
21338 /* We're loading full DIEs during partial symbol reading. */
21339 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 21340 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 21341 }
c906108c 21342
f2f0e013 21343 *ref_cu = target_cu;
9c541725 21344 temp_die.sect_off = sect_off;
9a3c8263 21345 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
21346 &temp_die,
21347 to_underlying (sect_off));
5c631832 21348}
10b3939b 21349
5c631832
JK
21350/* Follow reference attribute ATTR of SRC_DIE.
21351 On entry *REF_CU is the CU of SRC_DIE.
21352 On exit *REF_CU is the CU of the result. */
21353
21354static struct die_info *
ff39bb5e 21355follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
21356 struct dwarf2_cu **ref_cu)
21357{
9c541725 21358 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
21359 struct dwarf2_cu *cu = *ref_cu;
21360 struct die_info *die;
21361
9c541725 21362 die = follow_die_offset (sect_off,
36586728
TT
21363 (attr->form == DW_FORM_GNU_ref_alt
21364 || cu->per_cu->is_dwz),
21365 ref_cu);
5c631832
JK
21366 if (!die)
21367 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
21368 "at 0x%x [in module %s]"),
9c541725 21369 to_underlying (sect_off), to_underlying (src_die->sect_off),
4262abfb 21370 objfile_name (cu->objfile));
348e048f 21371
5c631832
JK
21372 return die;
21373}
21374
9c541725 21375/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b
JK
21376 Returned value is intended for DW_OP_call*. Returned
21377 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
21378
21379struct dwarf2_locexpr_baton
9c541725 21380dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
21381 struct dwarf2_per_cu_data *per_cu,
21382 CORE_ADDR (*get_frame_pc) (void *baton),
21383 void *baton)
5c631832 21384{
918dd910 21385 struct dwarf2_cu *cu;
5c631832
JK
21386 struct die_info *die;
21387 struct attribute *attr;
21388 struct dwarf2_locexpr_baton retval;
21389
8cf6f0b1
TT
21390 dw2_setup (per_cu->objfile);
21391
918dd910
JK
21392 if (per_cu->cu == NULL)
21393 load_cu (per_cu);
21394 cu = per_cu->cu;
cc12ce38
DE
21395 if (cu == NULL)
21396 {
21397 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21398 Instead just throw an error, not much else we can do. */
21399 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 21400 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 21401 }
918dd910 21402
9c541725 21403 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832
JK
21404 if (!die)
21405 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 21406 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
21407
21408 attr = dwarf2_attr (die, DW_AT_location, cu);
21409 if (!attr)
21410 {
e103e986
JK
21411 /* DWARF: "If there is no such attribute, then there is no effect.".
21412 DATA is ignored if SIZE is 0. */
5c631832 21413
e103e986 21414 retval.data = NULL;
5c631832
JK
21415 retval.size = 0;
21416 }
8cf6f0b1
TT
21417 else if (attr_form_is_section_offset (attr))
21418 {
21419 struct dwarf2_loclist_baton loclist_baton;
21420 CORE_ADDR pc = (*get_frame_pc) (baton);
21421 size_t size;
21422
21423 fill_in_loclist_baton (cu, &loclist_baton, attr);
21424
21425 retval.data = dwarf2_find_location_expression (&loclist_baton,
21426 &size, pc);
21427 retval.size = size;
21428 }
5c631832
JK
21429 else
21430 {
21431 if (!attr_form_is_block (attr))
21432 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
21433 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9c541725 21434 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
21435
21436 retval.data = DW_BLOCK (attr)->data;
21437 retval.size = DW_BLOCK (attr)->size;
21438 }
21439 retval.per_cu = cu->per_cu;
918dd910 21440
918dd910
JK
21441 age_cached_comp_units ();
21442
5c631832 21443 return retval;
348e048f
DE
21444}
21445
8b9737bf
TT
21446/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
21447 offset. */
21448
21449struct dwarf2_locexpr_baton
21450dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21451 struct dwarf2_per_cu_data *per_cu,
21452 CORE_ADDR (*get_frame_pc) (void *baton),
21453 void *baton)
21454{
9c541725 21455 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 21456
9c541725 21457 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
21458}
21459
b6807d98
TT
21460/* Write a constant of a given type as target-ordered bytes into
21461 OBSTACK. */
21462
21463static const gdb_byte *
21464write_constant_as_bytes (struct obstack *obstack,
21465 enum bfd_endian byte_order,
21466 struct type *type,
21467 ULONGEST value,
21468 LONGEST *len)
21469{
21470 gdb_byte *result;
21471
21472 *len = TYPE_LENGTH (type);
224c3ddb 21473 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
21474 store_unsigned_integer (result, *len, byte_order, value);
21475
21476 return result;
21477}
21478
21479/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
21480 pointer to the constant bytes and set LEN to the length of the
21481 data. If memory is needed, allocate it on OBSTACK. If the DIE
21482 does not have a DW_AT_const_value, return NULL. */
21483
21484const gdb_byte *
9c541725 21485dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
21486 struct dwarf2_per_cu_data *per_cu,
21487 struct obstack *obstack,
21488 LONGEST *len)
21489{
21490 struct dwarf2_cu *cu;
21491 struct die_info *die;
21492 struct attribute *attr;
21493 const gdb_byte *result = NULL;
21494 struct type *type;
21495 LONGEST value;
21496 enum bfd_endian byte_order;
21497
21498 dw2_setup (per_cu->objfile);
21499
21500 if (per_cu->cu == NULL)
21501 load_cu (per_cu);
21502 cu = per_cu->cu;
cc12ce38
DE
21503 if (cu == NULL)
21504 {
21505 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21506 Instead just throw an error, not much else we can do. */
21507 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 21508 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 21509 }
b6807d98 21510
9c541725 21511 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98
TT
21512 if (!die)
21513 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 21514 to_underlying (sect_off), objfile_name (per_cu->objfile));
b6807d98
TT
21515
21516
21517 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21518 if (attr == NULL)
21519 return NULL;
21520
21521 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
21522 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21523
21524 switch (attr->form)
21525 {
21526 case DW_FORM_addr:
21527 case DW_FORM_GNU_addr_index:
21528 {
21529 gdb_byte *tem;
21530
21531 *len = cu->header.addr_size;
224c3ddb 21532 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
21533 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
21534 result = tem;
21535 }
21536 break;
21537 case DW_FORM_string:
21538 case DW_FORM_strp:
21539 case DW_FORM_GNU_str_index:
21540 case DW_FORM_GNU_strp_alt:
21541 /* DW_STRING is already allocated on the objfile obstack, point
21542 directly to it. */
21543 result = (const gdb_byte *) DW_STRING (attr);
21544 *len = strlen (DW_STRING (attr));
21545 break;
21546 case DW_FORM_block1:
21547 case DW_FORM_block2:
21548 case DW_FORM_block4:
21549 case DW_FORM_block:
21550 case DW_FORM_exprloc:
0224619f 21551 case DW_FORM_data16:
b6807d98
TT
21552 result = DW_BLOCK (attr)->data;
21553 *len = DW_BLOCK (attr)->size;
21554 break;
21555
21556 /* The DW_AT_const_value attributes are supposed to carry the
21557 symbol's value "represented as it would be on the target
21558 architecture." By the time we get here, it's already been
21559 converted to host endianness, so we just need to sign- or
21560 zero-extend it as appropriate. */
21561 case DW_FORM_data1:
21562 type = die_type (die, cu);
21563 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
21564 if (result == NULL)
21565 result = write_constant_as_bytes (obstack, byte_order,
21566 type, value, len);
21567 break;
21568 case DW_FORM_data2:
21569 type = die_type (die, cu);
21570 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
21571 if (result == NULL)
21572 result = write_constant_as_bytes (obstack, byte_order,
21573 type, value, len);
21574 break;
21575 case DW_FORM_data4:
21576 type = die_type (die, cu);
21577 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
21578 if (result == NULL)
21579 result = write_constant_as_bytes (obstack, byte_order,
21580 type, value, len);
21581 break;
21582 case DW_FORM_data8:
21583 type = die_type (die, cu);
21584 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
21585 if (result == NULL)
21586 result = write_constant_as_bytes (obstack, byte_order,
21587 type, value, len);
21588 break;
21589
21590 case DW_FORM_sdata:
663c44ac 21591 case DW_FORM_implicit_const:
b6807d98
TT
21592 type = die_type (die, cu);
21593 result = write_constant_as_bytes (obstack, byte_order,
21594 type, DW_SND (attr), len);
21595 break;
21596
21597 case DW_FORM_udata:
21598 type = die_type (die, cu);
21599 result = write_constant_as_bytes (obstack, byte_order,
21600 type, DW_UNSND (attr), len);
21601 break;
21602
21603 default:
21604 complaint (&symfile_complaints,
21605 _("unsupported const value attribute form: '%s'"),
21606 dwarf_form_name (attr->form));
21607 break;
21608 }
21609
21610 return result;
21611}
21612
7942e96e
AA
21613/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
21614 valid type for this die is found. */
21615
21616struct type *
9c541725 21617dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
21618 struct dwarf2_per_cu_data *per_cu)
21619{
21620 struct dwarf2_cu *cu;
21621 struct die_info *die;
21622
21623 dw2_setup (per_cu->objfile);
21624
21625 if (per_cu->cu == NULL)
21626 load_cu (per_cu);
21627 cu = per_cu->cu;
21628 if (!cu)
21629 return NULL;
21630
9c541725 21631 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
21632 if (!die)
21633 return NULL;
21634
21635 return die_type (die, cu);
21636}
21637
8a9b8146
TT
21638/* Return the type of the DIE at DIE_OFFSET in the CU named by
21639 PER_CU. */
21640
21641struct type *
b64f50a1 21642dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
21643 struct dwarf2_per_cu_data *per_cu)
21644{
8a9b8146 21645 dw2_setup (per_cu->objfile);
b64f50a1 21646
9c541725 21647 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 21648 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
21649}
21650
ac9ec31b 21651/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 21652 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
21653 On exit *REF_CU is the CU of the result.
21654 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
21655
21656static struct die_info *
ac9ec31b
DE
21657follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21658 struct dwarf2_cu **ref_cu)
348e048f 21659{
348e048f 21660 struct die_info temp_die;
348e048f
DE
21661 struct dwarf2_cu *sig_cu;
21662 struct die_info *die;
21663
ac9ec31b
DE
21664 /* While it might be nice to assert sig_type->type == NULL here,
21665 we can get here for DW_AT_imported_declaration where we need
21666 the DIE not the type. */
348e048f
DE
21667
21668 /* If necessary, add it to the queue and load its DIEs. */
21669
95554aad 21670 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 21671 read_signatured_type (sig_type);
348e048f 21672
348e048f 21673 sig_cu = sig_type->per_cu.cu;
69d751e3 21674 gdb_assert (sig_cu != NULL);
9c541725
PA
21675 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
21676 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 21677 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 21678 to_underlying (temp_die.sect_off));
348e048f
DE
21679 if (die)
21680 {
796a7ff8
DE
21681 /* For .gdb_index version 7 keep track of included TUs.
21682 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
21683 if (dwarf2_per_objfile->index_table != NULL
21684 && dwarf2_per_objfile->index_table->version <= 7)
21685 {
21686 VEC_safe_push (dwarf2_per_cu_ptr,
21687 (*ref_cu)->per_cu->imported_symtabs,
21688 sig_cu->per_cu);
21689 }
21690
348e048f
DE
21691 *ref_cu = sig_cu;
21692 return die;
21693 }
21694
ac9ec31b
DE
21695 return NULL;
21696}
21697
21698/* Follow signatured type referenced by ATTR in SRC_DIE.
21699 On entry *REF_CU is the CU of SRC_DIE.
21700 On exit *REF_CU is the CU of the result.
21701 The result is the DIE of the type.
21702 If the referenced type cannot be found an error is thrown. */
21703
21704static struct die_info *
ff39bb5e 21705follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
21706 struct dwarf2_cu **ref_cu)
21707{
21708 ULONGEST signature = DW_SIGNATURE (attr);
21709 struct signatured_type *sig_type;
21710 struct die_info *die;
21711
21712 gdb_assert (attr->form == DW_FORM_ref_sig8);
21713
a2ce51a0 21714 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
21715 /* sig_type will be NULL if the signatured type is missing from
21716 the debug info. */
21717 if (sig_type == NULL)
21718 {
21719 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
21720 " from DIE at 0x%x [in module %s]"),
9c541725 21721 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 21722 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
21723 }
21724
21725 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
21726 if (die == NULL)
21727 {
21728 dump_die_for_error (src_die);
21729 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
21730 " from DIE at 0x%x [in module %s]"),
9c541725 21731 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 21732 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
21733 }
21734
21735 return die;
21736}
21737
21738/* Get the type specified by SIGNATURE referenced in DIE/CU,
21739 reading in and processing the type unit if necessary. */
21740
21741static struct type *
21742get_signatured_type (struct die_info *die, ULONGEST signature,
21743 struct dwarf2_cu *cu)
21744{
21745 struct signatured_type *sig_type;
21746 struct dwarf2_cu *type_cu;
21747 struct die_info *type_die;
21748 struct type *type;
21749
a2ce51a0 21750 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
21751 /* sig_type will be NULL if the signatured type is missing from
21752 the debug info. */
21753 if (sig_type == NULL)
21754 {
21755 complaint (&symfile_complaints,
21756 _("Dwarf Error: Cannot find signatured DIE %s referenced"
21757 " from DIE at 0x%x [in module %s]"),
9c541725 21758 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21759 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21760 return build_error_marker_type (cu, die);
21761 }
21762
21763 /* If we already know the type we're done. */
21764 if (sig_type->type != NULL)
21765 return sig_type->type;
21766
21767 type_cu = cu;
21768 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
21769 if (type_die != NULL)
21770 {
21771 /* N.B. We need to call get_die_type to ensure only one type for this DIE
21772 is created. This is important, for example, because for c++ classes
21773 we need TYPE_NAME set which is only done by new_symbol. Blech. */
21774 type = read_type_die (type_die, type_cu);
21775 if (type == NULL)
21776 {
21777 complaint (&symfile_complaints,
21778 _("Dwarf Error: Cannot build signatured type %s"
21779 " referenced from DIE at 0x%x [in module %s]"),
9c541725 21780 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21781 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21782 type = build_error_marker_type (cu, die);
21783 }
21784 }
21785 else
21786 {
21787 complaint (&symfile_complaints,
21788 _("Dwarf Error: Problem reading signatured DIE %s referenced"
21789 " from DIE at 0x%x [in module %s]"),
9c541725 21790 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21791 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21792 type = build_error_marker_type (cu, die);
21793 }
21794 sig_type->type = type;
21795
21796 return type;
21797}
21798
21799/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
21800 reading in and processing the type unit if necessary. */
21801
21802static struct type *
ff39bb5e 21803get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 21804 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
21805{
21806 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 21807 if (attr_form_is_ref (attr))
ac9ec31b
DE
21808 {
21809 struct dwarf2_cu *type_cu = cu;
21810 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
21811
21812 return read_type_die (type_die, type_cu);
21813 }
21814 else if (attr->form == DW_FORM_ref_sig8)
21815 {
21816 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
21817 }
21818 else
21819 {
21820 complaint (&symfile_complaints,
21821 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21822 " at 0x%x [in module %s]"),
9c541725 21823 dwarf_form_name (attr->form), to_underlying (die->sect_off),
4262abfb 21824 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21825 return build_error_marker_type (cu, die);
21826 }
348e048f
DE
21827}
21828
e5fe5e75 21829/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
21830
21831static void
e5fe5e75 21832load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 21833{
52dc124a 21834 struct signatured_type *sig_type;
348e048f 21835
f4dc4d17
DE
21836 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21837 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21838
6721b2ec
DE
21839 /* We have the per_cu, but we need the signatured_type.
21840 Fortunately this is an easy translation. */
21841 gdb_assert (per_cu->is_debug_types);
21842 sig_type = (struct signatured_type *) per_cu;
348e048f 21843
6721b2ec 21844 gdb_assert (per_cu->cu == NULL);
348e048f 21845
52dc124a 21846 read_signatured_type (sig_type);
348e048f 21847
6721b2ec 21848 gdb_assert (per_cu->cu != NULL);
348e048f
DE
21849}
21850
dee91e82
DE
21851/* die_reader_func for read_signatured_type.
21852 This is identical to load_full_comp_unit_reader,
21853 but is kept separate for now. */
348e048f
DE
21854
21855static void
dee91e82 21856read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 21857 const gdb_byte *info_ptr,
dee91e82
DE
21858 struct die_info *comp_unit_die,
21859 int has_children,
21860 void *data)
348e048f 21861{
dee91e82 21862 struct dwarf2_cu *cu = reader->cu;
348e048f 21863
dee91e82
DE
21864 gdb_assert (cu->die_hash == NULL);
21865 cu->die_hash =
21866 htab_create_alloc_ex (cu->header.length / 12,
21867 die_hash,
21868 die_eq,
21869 NULL,
21870 &cu->comp_unit_obstack,
21871 hashtab_obstack_allocate,
21872 dummy_obstack_deallocate);
348e048f 21873
dee91e82
DE
21874 if (has_children)
21875 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21876 &info_ptr, comp_unit_die);
21877 cu->dies = comp_unit_die;
21878 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
21879
21880 /* We try not to read any attributes in this function, because not
9cdd5dbd 21881 all CUs needed for references have been loaded yet, and symbol
348e048f 21882 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
21883 or we won't be able to build types correctly.
21884 Similarly, if we do not read the producer, we can not apply
21885 producer-specific interpretation. */
95554aad 21886 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 21887}
348e048f 21888
3019eac3
DE
21889/* Read in a signatured type and build its CU and DIEs.
21890 If the type is a stub for the real type in a DWO file,
21891 read in the real type from the DWO file as well. */
dee91e82
DE
21892
21893static void
21894read_signatured_type (struct signatured_type *sig_type)
21895{
21896 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 21897
3019eac3 21898 gdb_assert (per_cu->is_debug_types);
dee91e82 21899 gdb_assert (per_cu->cu == NULL);
348e048f 21900
f4dc4d17
DE
21901 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21902 read_signatured_type_reader, NULL);
7ee85ab1 21903 sig_type->per_cu.tu_read = 1;
c906108c
SS
21904}
21905
c906108c
SS
21906/* Decode simple location descriptions.
21907 Given a pointer to a dwarf block that defines a location, compute
21908 the location and return the value.
21909
4cecd739
DJ
21910 NOTE drow/2003-11-18: This function is called in two situations
21911 now: for the address of static or global variables (partial symbols
21912 only) and for offsets into structures which are expected to be
21913 (more or less) constant. The partial symbol case should go away,
21914 and only the constant case should remain. That will let this
21915 function complain more accurately. A few special modes are allowed
21916 without complaint for global variables (for instance, global
21917 register values and thread-local values).
c906108c
SS
21918
21919 A location description containing no operations indicates that the
4cecd739 21920 object is optimized out. The return value is 0 for that case.
6b992462
DJ
21921 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21922 callers will only want a very basic result and this can become a
21ae7a4d
JK
21923 complaint.
21924
21925 Note that stack[0] is unused except as a default error return. */
c906108c
SS
21926
21927static CORE_ADDR
e7c27a73 21928decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 21929{
e7c27a73 21930 struct objfile *objfile = cu->objfile;
56eb65bd
SP
21931 size_t i;
21932 size_t size = blk->size;
d521ce57 21933 const gdb_byte *data = blk->data;
21ae7a4d
JK
21934 CORE_ADDR stack[64];
21935 int stacki;
21936 unsigned int bytes_read, unsnd;
21937 gdb_byte op;
c906108c 21938
21ae7a4d
JK
21939 i = 0;
21940 stacki = 0;
21941 stack[stacki] = 0;
21942 stack[++stacki] = 0;
21943
21944 while (i < size)
21945 {
21946 op = data[i++];
21947 switch (op)
21948 {
21949 case DW_OP_lit0:
21950 case DW_OP_lit1:
21951 case DW_OP_lit2:
21952 case DW_OP_lit3:
21953 case DW_OP_lit4:
21954 case DW_OP_lit5:
21955 case DW_OP_lit6:
21956 case DW_OP_lit7:
21957 case DW_OP_lit8:
21958 case DW_OP_lit9:
21959 case DW_OP_lit10:
21960 case DW_OP_lit11:
21961 case DW_OP_lit12:
21962 case DW_OP_lit13:
21963 case DW_OP_lit14:
21964 case DW_OP_lit15:
21965 case DW_OP_lit16:
21966 case DW_OP_lit17:
21967 case DW_OP_lit18:
21968 case DW_OP_lit19:
21969 case DW_OP_lit20:
21970 case DW_OP_lit21:
21971 case DW_OP_lit22:
21972 case DW_OP_lit23:
21973 case DW_OP_lit24:
21974 case DW_OP_lit25:
21975 case DW_OP_lit26:
21976 case DW_OP_lit27:
21977 case DW_OP_lit28:
21978 case DW_OP_lit29:
21979 case DW_OP_lit30:
21980 case DW_OP_lit31:
21981 stack[++stacki] = op - DW_OP_lit0;
21982 break;
f1bea926 21983
21ae7a4d
JK
21984 case DW_OP_reg0:
21985 case DW_OP_reg1:
21986 case DW_OP_reg2:
21987 case DW_OP_reg3:
21988 case DW_OP_reg4:
21989 case DW_OP_reg5:
21990 case DW_OP_reg6:
21991 case DW_OP_reg7:
21992 case DW_OP_reg8:
21993 case DW_OP_reg9:
21994 case DW_OP_reg10:
21995 case DW_OP_reg11:
21996 case DW_OP_reg12:
21997 case DW_OP_reg13:
21998 case DW_OP_reg14:
21999 case DW_OP_reg15:
22000 case DW_OP_reg16:
22001 case DW_OP_reg17:
22002 case DW_OP_reg18:
22003 case DW_OP_reg19:
22004 case DW_OP_reg20:
22005 case DW_OP_reg21:
22006 case DW_OP_reg22:
22007 case DW_OP_reg23:
22008 case DW_OP_reg24:
22009 case DW_OP_reg25:
22010 case DW_OP_reg26:
22011 case DW_OP_reg27:
22012 case DW_OP_reg28:
22013 case DW_OP_reg29:
22014 case DW_OP_reg30:
22015 case DW_OP_reg31:
22016 stack[++stacki] = op - DW_OP_reg0;
22017 if (i < size)
22018 dwarf2_complex_location_expr_complaint ();
22019 break;
c906108c 22020
21ae7a4d
JK
22021 case DW_OP_regx:
22022 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22023 i += bytes_read;
22024 stack[++stacki] = unsnd;
22025 if (i < size)
22026 dwarf2_complex_location_expr_complaint ();
22027 break;
c906108c 22028
21ae7a4d
JK
22029 case DW_OP_addr:
22030 stack[++stacki] = read_address (objfile->obfd, &data[i],
22031 cu, &bytes_read);
22032 i += bytes_read;
22033 break;
d53d4ac5 22034
21ae7a4d
JK
22035 case DW_OP_const1u:
22036 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22037 i += 1;
22038 break;
22039
22040 case DW_OP_const1s:
22041 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22042 i += 1;
22043 break;
22044
22045 case DW_OP_const2u:
22046 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22047 i += 2;
22048 break;
22049
22050 case DW_OP_const2s:
22051 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22052 i += 2;
22053 break;
d53d4ac5 22054
21ae7a4d
JK
22055 case DW_OP_const4u:
22056 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22057 i += 4;
22058 break;
22059
22060 case DW_OP_const4s:
22061 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22062 i += 4;
22063 break;
22064
585861ea
JK
22065 case DW_OP_const8u:
22066 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22067 i += 8;
22068 break;
22069
21ae7a4d
JK
22070 case DW_OP_constu:
22071 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22072 &bytes_read);
22073 i += bytes_read;
22074 break;
22075
22076 case DW_OP_consts:
22077 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22078 i += bytes_read;
22079 break;
22080
22081 case DW_OP_dup:
22082 stack[stacki + 1] = stack[stacki];
22083 stacki++;
22084 break;
22085
22086 case DW_OP_plus:
22087 stack[stacki - 1] += stack[stacki];
22088 stacki--;
22089 break;
22090
22091 case DW_OP_plus_uconst:
22092 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22093 &bytes_read);
22094 i += bytes_read;
22095 break;
22096
22097 case DW_OP_minus:
22098 stack[stacki - 1] -= stack[stacki];
22099 stacki--;
22100 break;
22101
22102 case DW_OP_deref:
22103 /* If we're not the last op, then we definitely can't encode
22104 this using GDB's address_class enum. This is valid for partial
22105 global symbols, although the variable's address will be bogus
22106 in the psymtab. */
22107 if (i < size)
22108 dwarf2_complex_location_expr_complaint ();
22109 break;
22110
22111 case DW_OP_GNU_push_tls_address:
4aa4e28b 22112 case DW_OP_form_tls_address:
21ae7a4d
JK
22113 /* The top of the stack has the offset from the beginning
22114 of the thread control block at which the variable is located. */
22115 /* Nothing should follow this operator, so the top of stack would
22116 be returned. */
22117 /* This is valid for partial global symbols, but the variable's
585861ea
JK
22118 address will be bogus in the psymtab. Make it always at least
22119 non-zero to not look as a variable garbage collected by linker
22120 which have DW_OP_addr 0. */
21ae7a4d
JK
22121 if (i < size)
22122 dwarf2_complex_location_expr_complaint ();
585861ea 22123 stack[stacki]++;
21ae7a4d
JK
22124 break;
22125
22126 case DW_OP_GNU_uninit:
22127 break;
22128
3019eac3 22129 case DW_OP_GNU_addr_index:
49f6c839 22130 case DW_OP_GNU_const_index:
3019eac3
DE
22131 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22132 &bytes_read);
22133 i += bytes_read;
22134 break;
22135
21ae7a4d
JK
22136 default:
22137 {
f39c6ffd 22138 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
22139
22140 if (name)
22141 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
22142 name);
22143 else
22144 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
22145 op);
22146 }
22147
22148 return (stack[stacki]);
d53d4ac5 22149 }
3c6e0cb3 22150
21ae7a4d
JK
22151 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22152 outside of the allocated space. Also enforce minimum>0. */
22153 if (stacki >= ARRAY_SIZE (stack) - 1)
22154 {
22155 complaint (&symfile_complaints,
22156 _("location description stack overflow"));
22157 return 0;
22158 }
22159
22160 if (stacki <= 0)
22161 {
22162 complaint (&symfile_complaints,
22163 _("location description stack underflow"));
22164 return 0;
22165 }
22166 }
22167 return (stack[stacki]);
c906108c
SS
22168}
22169
22170/* memory allocation interface */
22171
c906108c 22172static struct dwarf_block *
7b5a2f43 22173dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 22174{
8d749320 22175 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
22176}
22177
c906108c 22178static struct die_info *
b60c80d6 22179dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
22180{
22181 struct die_info *die;
b60c80d6
DJ
22182 size_t size = sizeof (struct die_info);
22183
22184 if (num_attrs > 1)
22185 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 22186
b60c80d6 22187 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
22188 memset (die, 0, sizeof (struct die_info));
22189 return (die);
22190}
2e276125
JB
22191
22192\f
22193/* Macro support. */
22194
233d95b5
JK
22195/* Return file name relative to the compilation directory of file number I in
22196 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 22197 responsible for freeing it. */
233d95b5 22198
2e276125 22199static char *
233d95b5 22200file_file_name (int file, struct line_header *lh)
2e276125 22201{
6a83a1e6
EZ
22202 /* Is the file number a valid index into the line header's file name
22203 table? Remember that file numbers start with one, not zero. */
fff8551c 22204 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 22205 {
8c43009f 22206 const file_entry &fe = lh->file_names[file - 1];
6e70227d 22207
8c43009f
PA
22208 if (!IS_ABSOLUTE_PATH (fe.name))
22209 {
22210 const char *dir = fe.include_dir (lh);
22211 if (dir != NULL)
22212 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
22213 }
22214 return xstrdup (fe.name);
6a83a1e6 22215 }
2e276125
JB
22216 else
22217 {
6a83a1e6
EZ
22218 /* The compiler produced a bogus file number. We can at least
22219 record the macro definitions made in the file, even if we
22220 won't be able to find the file by name. */
22221 char fake_name[80];
9a619af0 22222
8c042590
PM
22223 xsnprintf (fake_name, sizeof (fake_name),
22224 "<bad macro file number %d>", file);
2e276125 22225
6e70227d 22226 complaint (&symfile_complaints,
6a83a1e6
EZ
22227 _("bad file number in macro information (%d)"),
22228 file);
2e276125 22229
6a83a1e6 22230 return xstrdup (fake_name);
2e276125
JB
22231 }
22232}
22233
233d95b5
JK
22234/* Return the full name of file number I in *LH's file name table.
22235 Use COMP_DIR as the name of the current directory of the
22236 compilation. The result is allocated using xmalloc; the caller is
22237 responsible for freeing it. */
22238static char *
22239file_full_name (int file, struct line_header *lh, const char *comp_dir)
22240{
22241 /* Is the file number a valid index into the line header's file name
22242 table? Remember that file numbers start with one, not zero. */
fff8551c 22243 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
22244 {
22245 char *relative = file_file_name (file, lh);
22246
22247 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
22248 return relative;
b36cec19
PA
22249 return reconcat (relative, comp_dir, SLASH_STRING,
22250 relative, (char *) NULL);
233d95b5
JK
22251 }
22252 else
22253 return file_file_name (file, lh);
22254}
22255
2e276125
JB
22256
22257static struct macro_source_file *
22258macro_start_file (int file, int line,
22259 struct macro_source_file *current_file,
43f3e411 22260 struct line_header *lh)
2e276125 22261{
233d95b5
JK
22262 /* File name relative to the compilation directory of this source file. */
22263 char *file_name = file_file_name (file, lh);
2e276125 22264
2e276125 22265 if (! current_file)
abc9d0dc 22266 {
fc474241
DE
22267 /* Note: We don't create a macro table for this compilation unit
22268 at all until we actually get a filename. */
43f3e411 22269 struct macro_table *macro_table = get_macro_table ();
fc474241 22270
abc9d0dc
TT
22271 /* If we have no current file, then this must be the start_file
22272 directive for the compilation unit's main source file. */
fc474241
DE
22273 current_file = macro_set_main (macro_table, file_name);
22274 macro_define_special (macro_table);
abc9d0dc 22275 }
2e276125 22276 else
233d95b5 22277 current_file = macro_include (current_file, line, file_name);
2e276125 22278
233d95b5 22279 xfree (file_name);
6e70227d 22280
2e276125
JB
22281 return current_file;
22282}
22283
2e276125
JB
22284static const char *
22285consume_improper_spaces (const char *p, const char *body)
22286{
22287 if (*p == ' ')
22288 {
4d3c2250 22289 complaint (&symfile_complaints,
3e43a32a
MS
22290 _("macro definition contains spaces "
22291 "in formal argument list:\n`%s'"),
4d3c2250 22292 body);
2e276125
JB
22293
22294 while (*p == ' ')
22295 p++;
22296 }
22297
22298 return p;
22299}
22300
22301
22302static void
22303parse_macro_definition (struct macro_source_file *file, int line,
22304 const char *body)
22305{
22306 const char *p;
22307
22308 /* The body string takes one of two forms. For object-like macro
22309 definitions, it should be:
22310
22311 <macro name> " " <definition>
22312
22313 For function-like macro definitions, it should be:
22314
22315 <macro name> "() " <definition>
22316 or
22317 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
22318
22319 Spaces may appear only where explicitly indicated, and in the
22320 <definition>.
22321
22322 The Dwarf 2 spec says that an object-like macro's name is always
22323 followed by a space, but versions of GCC around March 2002 omit
6e70227d 22324 the space when the macro's definition is the empty string.
2e276125
JB
22325
22326 The Dwarf 2 spec says that there should be no spaces between the
22327 formal arguments in a function-like macro's formal argument list,
22328 but versions of GCC around March 2002 include spaces after the
22329 commas. */
22330
22331
22332 /* Find the extent of the macro name. The macro name is terminated
22333 by either a space or null character (for an object-like macro) or
22334 an opening paren (for a function-like macro). */
22335 for (p = body; *p; p++)
22336 if (*p == ' ' || *p == '(')
22337 break;
22338
22339 if (*p == ' ' || *p == '\0')
22340 {
22341 /* It's an object-like macro. */
22342 int name_len = p - body;
3f8a7804 22343 char *name = savestring (body, name_len);
2e276125
JB
22344 const char *replacement;
22345
22346 if (*p == ' ')
22347 replacement = body + name_len + 1;
22348 else
22349 {
4d3c2250 22350 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22351 replacement = body + name_len;
22352 }
6e70227d 22353
2e276125
JB
22354 macro_define_object (file, line, name, replacement);
22355
22356 xfree (name);
22357 }
22358 else if (*p == '(')
22359 {
22360 /* It's a function-like macro. */
3f8a7804 22361 char *name = savestring (body, p - body);
2e276125
JB
22362 int argc = 0;
22363 int argv_size = 1;
8d749320 22364 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
22365
22366 p++;
22367
22368 p = consume_improper_spaces (p, body);
22369
22370 /* Parse the formal argument list. */
22371 while (*p && *p != ')')
22372 {
22373 /* Find the extent of the current argument name. */
22374 const char *arg_start = p;
22375
22376 while (*p && *p != ',' && *p != ')' && *p != ' ')
22377 p++;
22378
22379 if (! *p || p == arg_start)
4d3c2250 22380 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22381 else
22382 {
22383 /* Make sure argv has room for the new argument. */
22384 if (argc >= argv_size)
22385 {
22386 argv_size *= 2;
224c3ddb 22387 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
22388 }
22389
3f8a7804 22390 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
22391 }
22392
22393 p = consume_improper_spaces (p, body);
22394
22395 /* Consume the comma, if present. */
22396 if (*p == ',')
22397 {
22398 p++;
22399
22400 p = consume_improper_spaces (p, body);
22401 }
22402 }
22403
22404 if (*p == ')')
22405 {
22406 p++;
22407
22408 if (*p == ' ')
22409 /* Perfectly formed definition, no complaints. */
22410 macro_define_function (file, line, name,
6e70227d 22411 argc, (const char **) argv,
2e276125
JB
22412 p + 1);
22413 else if (*p == '\0')
22414 {
22415 /* Complain, but do define it. */
4d3c2250 22416 dwarf2_macro_malformed_definition_complaint (body);
2e276125 22417 macro_define_function (file, line, name,
6e70227d 22418 argc, (const char **) argv,
2e276125
JB
22419 p);
22420 }
22421 else
22422 /* Just complain. */
4d3c2250 22423 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22424 }
22425 else
22426 /* Just complain. */
4d3c2250 22427 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22428
22429 xfree (name);
22430 {
22431 int i;
22432
22433 for (i = 0; i < argc; i++)
22434 xfree (argv[i]);
22435 }
22436 xfree (argv);
22437 }
22438 else
4d3c2250 22439 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
22440}
22441
cf2c3c16
TT
22442/* Skip some bytes from BYTES according to the form given in FORM.
22443 Returns the new pointer. */
2e276125 22444
d521ce57
TT
22445static const gdb_byte *
22446skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
22447 enum dwarf_form form,
22448 unsigned int offset_size,
22449 struct dwarf2_section_info *section)
2e276125 22450{
cf2c3c16 22451 unsigned int bytes_read;
2e276125 22452
cf2c3c16 22453 switch (form)
2e276125 22454 {
cf2c3c16
TT
22455 case DW_FORM_data1:
22456 case DW_FORM_flag:
22457 ++bytes;
22458 break;
22459
22460 case DW_FORM_data2:
22461 bytes += 2;
22462 break;
22463
22464 case DW_FORM_data4:
22465 bytes += 4;
22466 break;
22467
22468 case DW_FORM_data8:
22469 bytes += 8;
22470 break;
22471
0224619f
JK
22472 case DW_FORM_data16:
22473 bytes += 16;
22474 break;
22475
cf2c3c16
TT
22476 case DW_FORM_string:
22477 read_direct_string (abfd, bytes, &bytes_read);
22478 bytes += bytes_read;
22479 break;
22480
22481 case DW_FORM_sec_offset:
22482 case DW_FORM_strp:
36586728 22483 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
22484 bytes += offset_size;
22485 break;
22486
22487 case DW_FORM_block:
22488 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
22489 bytes += bytes_read;
22490 break;
22491
22492 case DW_FORM_block1:
22493 bytes += 1 + read_1_byte (abfd, bytes);
22494 break;
22495 case DW_FORM_block2:
22496 bytes += 2 + read_2_bytes (abfd, bytes);
22497 break;
22498 case DW_FORM_block4:
22499 bytes += 4 + read_4_bytes (abfd, bytes);
22500 break;
22501
22502 case DW_FORM_sdata:
22503 case DW_FORM_udata:
3019eac3
DE
22504 case DW_FORM_GNU_addr_index:
22505 case DW_FORM_GNU_str_index:
d521ce57 22506 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
22507 if (bytes == NULL)
22508 {
22509 dwarf2_section_buffer_overflow_complaint (section);
22510 return NULL;
22511 }
cf2c3c16
TT
22512 break;
22513
663c44ac
JK
22514 case DW_FORM_implicit_const:
22515 break;
22516
cf2c3c16
TT
22517 default:
22518 {
22519 complain:
22520 complaint (&symfile_complaints,
22521 _("invalid form 0x%x in `%s'"),
a32a8923 22522 form, get_section_name (section));
cf2c3c16
TT
22523 return NULL;
22524 }
2e276125
JB
22525 }
22526
cf2c3c16
TT
22527 return bytes;
22528}
757a13d0 22529
cf2c3c16
TT
22530/* A helper for dwarf_decode_macros that handles skipping an unknown
22531 opcode. Returns an updated pointer to the macro data buffer; or,
22532 on error, issues a complaint and returns NULL. */
757a13d0 22533
d521ce57 22534static const gdb_byte *
cf2c3c16 22535skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
22536 const gdb_byte **opcode_definitions,
22537 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
22538 bfd *abfd,
22539 unsigned int offset_size,
22540 struct dwarf2_section_info *section)
22541{
22542 unsigned int bytes_read, i;
22543 unsigned long arg;
d521ce57 22544 const gdb_byte *defn;
2e276125 22545
cf2c3c16 22546 if (opcode_definitions[opcode] == NULL)
2e276125 22547 {
cf2c3c16
TT
22548 complaint (&symfile_complaints,
22549 _("unrecognized DW_MACFINO opcode 0x%x"),
22550 opcode);
22551 return NULL;
22552 }
2e276125 22553
cf2c3c16
TT
22554 defn = opcode_definitions[opcode];
22555 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
22556 defn += bytes_read;
2e276125 22557
cf2c3c16
TT
22558 for (i = 0; i < arg; ++i)
22559 {
aead7601
SM
22560 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
22561 (enum dwarf_form) defn[i], offset_size,
f664829e 22562 section);
cf2c3c16
TT
22563 if (mac_ptr == NULL)
22564 {
22565 /* skip_form_bytes already issued the complaint. */
22566 return NULL;
22567 }
22568 }
757a13d0 22569
cf2c3c16
TT
22570 return mac_ptr;
22571}
757a13d0 22572
cf2c3c16
TT
22573/* A helper function which parses the header of a macro section.
22574 If the macro section is the extended (for now called "GNU") type,
22575 then this updates *OFFSET_SIZE. Returns a pointer to just after
22576 the header, or issues a complaint and returns NULL on error. */
757a13d0 22577
d521ce57
TT
22578static const gdb_byte *
22579dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 22580 bfd *abfd,
d521ce57 22581 const gdb_byte *mac_ptr,
cf2c3c16
TT
22582 unsigned int *offset_size,
22583 int section_is_gnu)
22584{
22585 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 22586
cf2c3c16
TT
22587 if (section_is_gnu)
22588 {
22589 unsigned int version, flags;
757a13d0 22590
cf2c3c16 22591 version = read_2_bytes (abfd, mac_ptr);
0af92d60 22592 if (version != 4 && version != 5)
cf2c3c16
TT
22593 {
22594 complaint (&symfile_complaints,
22595 _("unrecognized version `%d' in .debug_macro section"),
22596 version);
22597 return NULL;
22598 }
22599 mac_ptr += 2;
757a13d0 22600
cf2c3c16
TT
22601 flags = read_1_byte (abfd, mac_ptr);
22602 ++mac_ptr;
22603 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 22604
cf2c3c16
TT
22605 if ((flags & 2) != 0)
22606 /* We don't need the line table offset. */
22607 mac_ptr += *offset_size;
757a13d0 22608
cf2c3c16
TT
22609 /* Vendor opcode descriptions. */
22610 if ((flags & 4) != 0)
22611 {
22612 unsigned int i, count;
757a13d0 22613
cf2c3c16
TT
22614 count = read_1_byte (abfd, mac_ptr);
22615 ++mac_ptr;
22616 for (i = 0; i < count; ++i)
22617 {
22618 unsigned int opcode, bytes_read;
22619 unsigned long arg;
22620
22621 opcode = read_1_byte (abfd, mac_ptr);
22622 ++mac_ptr;
22623 opcode_definitions[opcode] = mac_ptr;
22624 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22625 mac_ptr += bytes_read;
22626 mac_ptr += arg;
22627 }
757a13d0 22628 }
cf2c3c16 22629 }
757a13d0 22630
cf2c3c16
TT
22631 return mac_ptr;
22632}
757a13d0 22633
cf2c3c16 22634/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 22635 including DW_MACRO_import. */
cf2c3c16
TT
22636
22637static void
d521ce57
TT
22638dwarf_decode_macro_bytes (bfd *abfd,
22639 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 22640 struct macro_source_file *current_file,
43f3e411 22641 struct line_header *lh,
cf2c3c16 22642 struct dwarf2_section_info *section,
36586728 22643 int section_is_gnu, int section_is_dwz,
cf2c3c16 22644 unsigned int offset_size,
8fc3fc34 22645 htab_t include_hash)
cf2c3c16 22646{
4d663531 22647 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
22648 enum dwarf_macro_record_type macinfo_type;
22649 int at_commandline;
d521ce57 22650 const gdb_byte *opcode_definitions[256];
757a13d0 22651
cf2c3c16
TT
22652 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22653 &offset_size, section_is_gnu);
22654 if (mac_ptr == NULL)
22655 {
22656 /* We already issued a complaint. */
22657 return;
22658 }
757a13d0
JK
22659
22660 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
22661 GDB is still reading the definitions from command line. First
22662 DW_MACINFO_start_file will need to be ignored as it was already executed
22663 to create CURRENT_FILE for the main source holding also the command line
22664 definitions. On first met DW_MACINFO_start_file this flag is reset to
22665 normally execute all the remaining DW_MACINFO_start_file macinfos. */
22666
22667 at_commandline = 1;
22668
22669 do
22670 {
22671 /* Do we at least have room for a macinfo type byte? */
22672 if (mac_ptr >= mac_end)
22673 {
f664829e 22674 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
22675 break;
22676 }
22677
aead7601 22678 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
22679 mac_ptr++;
22680
cf2c3c16
TT
22681 /* Note that we rely on the fact that the corresponding GNU and
22682 DWARF constants are the same. */
757a13d0
JK
22683 switch (macinfo_type)
22684 {
22685 /* A zero macinfo type indicates the end of the macro
22686 information. */
22687 case 0:
22688 break;
2e276125 22689
0af92d60
JK
22690 case DW_MACRO_define:
22691 case DW_MACRO_undef:
22692 case DW_MACRO_define_strp:
22693 case DW_MACRO_undef_strp:
22694 case DW_MACRO_define_sup:
22695 case DW_MACRO_undef_sup:
2e276125 22696 {
891d2f0b 22697 unsigned int bytes_read;
2e276125 22698 int line;
d521ce57 22699 const char *body;
cf2c3c16 22700 int is_define;
2e276125 22701
cf2c3c16
TT
22702 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22703 mac_ptr += bytes_read;
22704
0af92d60
JK
22705 if (macinfo_type == DW_MACRO_define
22706 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
22707 {
22708 body = read_direct_string (abfd, mac_ptr, &bytes_read);
22709 mac_ptr += bytes_read;
22710 }
22711 else
22712 {
22713 LONGEST str_offset;
22714
22715 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
22716 mac_ptr += offset_size;
2e276125 22717
0af92d60
JK
22718 if (macinfo_type == DW_MACRO_define_sup
22719 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 22720 || section_is_dwz)
36586728
TT
22721 {
22722 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22723
22724 body = read_indirect_string_from_dwz (dwz, str_offset);
22725 }
22726 else
22727 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
22728 }
22729
0af92d60
JK
22730 is_define = (macinfo_type == DW_MACRO_define
22731 || macinfo_type == DW_MACRO_define_strp
22732 || macinfo_type == DW_MACRO_define_sup);
2e276125 22733 if (! current_file)
757a13d0
JK
22734 {
22735 /* DWARF violation as no main source is present. */
22736 complaint (&symfile_complaints,
22737 _("debug info with no main source gives macro %s "
22738 "on line %d: %s"),
cf2c3c16
TT
22739 is_define ? _("definition") : _("undefinition"),
22740 line, body);
757a13d0
JK
22741 break;
22742 }
3e43a32a
MS
22743 if ((line == 0 && !at_commandline)
22744 || (line != 0 && at_commandline))
4d3c2250 22745 complaint (&symfile_complaints,
757a13d0
JK
22746 _("debug info gives %s macro %s with %s line %d: %s"),
22747 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 22748 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
22749 line == 0 ? _("zero") : _("non-zero"), line, body);
22750
cf2c3c16 22751 if (is_define)
757a13d0 22752 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
22753 else
22754 {
0af92d60
JK
22755 gdb_assert (macinfo_type == DW_MACRO_undef
22756 || macinfo_type == DW_MACRO_undef_strp
22757 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
22758 macro_undef (current_file, line, body);
22759 }
2e276125
JB
22760 }
22761 break;
22762
0af92d60 22763 case DW_MACRO_start_file:
2e276125 22764 {
891d2f0b 22765 unsigned int bytes_read;
2e276125
JB
22766 int line, file;
22767
22768 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22769 mac_ptr += bytes_read;
22770 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22771 mac_ptr += bytes_read;
22772
3e43a32a
MS
22773 if ((line == 0 && !at_commandline)
22774 || (line != 0 && at_commandline))
757a13d0
JK
22775 complaint (&symfile_complaints,
22776 _("debug info gives source %d included "
22777 "from %s at %s line %d"),
22778 file, at_commandline ? _("command-line") : _("file"),
22779 line == 0 ? _("zero") : _("non-zero"), line);
22780
22781 if (at_commandline)
22782 {
0af92d60 22783 /* This DW_MACRO_start_file was executed in the
cf2c3c16 22784 pass one. */
757a13d0
JK
22785 at_commandline = 0;
22786 }
22787 else
43f3e411 22788 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
22789 }
22790 break;
22791
0af92d60 22792 case DW_MACRO_end_file:
2e276125 22793 if (! current_file)
4d3c2250 22794 complaint (&symfile_complaints,
3e43a32a
MS
22795 _("macro debug info has an unmatched "
22796 "`close_file' directive"));
2e276125
JB
22797 else
22798 {
22799 current_file = current_file->included_by;
22800 if (! current_file)
22801 {
cf2c3c16 22802 enum dwarf_macro_record_type next_type;
2e276125
JB
22803
22804 /* GCC circa March 2002 doesn't produce the zero
22805 type byte marking the end of the compilation
22806 unit. Complain if it's not there, but exit no
22807 matter what. */
22808
22809 /* Do we at least have room for a macinfo type byte? */
22810 if (mac_ptr >= mac_end)
22811 {
f664829e 22812 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
22813 return;
22814 }
22815
22816 /* We don't increment mac_ptr here, so this is just
22817 a look-ahead. */
aead7601
SM
22818 next_type
22819 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22820 mac_ptr);
2e276125 22821 if (next_type != 0)
4d3c2250 22822 complaint (&symfile_complaints,
3e43a32a
MS
22823 _("no terminating 0-type entry for "
22824 "macros in `.debug_macinfo' section"));
2e276125
JB
22825
22826 return;
22827 }
22828 }
22829 break;
22830
0af92d60
JK
22831 case DW_MACRO_import:
22832 case DW_MACRO_import_sup:
cf2c3c16
TT
22833 {
22834 LONGEST offset;
8fc3fc34 22835 void **slot;
a036ba48
TT
22836 bfd *include_bfd = abfd;
22837 struct dwarf2_section_info *include_section = section;
d521ce57 22838 const gdb_byte *include_mac_end = mac_end;
a036ba48 22839 int is_dwz = section_is_dwz;
d521ce57 22840 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
22841
22842 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22843 mac_ptr += offset_size;
22844
0af92d60 22845 if (macinfo_type == DW_MACRO_import_sup)
a036ba48
TT
22846 {
22847 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22848
4d663531 22849 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 22850
a036ba48 22851 include_section = &dwz->macro;
a32a8923 22852 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
22853 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22854 is_dwz = 1;
22855 }
22856
22857 new_mac_ptr = include_section->buffer + offset;
22858 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22859
8fc3fc34
TT
22860 if (*slot != NULL)
22861 {
22862 /* This has actually happened; see
22863 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22864 complaint (&symfile_complaints,
0af92d60 22865 _("recursive DW_MACRO_import in "
8fc3fc34
TT
22866 ".debug_macro section"));
22867 }
22868 else
22869 {
d521ce57 22870 *slot = (void *) new_mac_ptr;
36586728 22871
a036ba48 22872 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 22873 include_mac_end, current_file, lh,
36586728 22874 section, section_is_gnu, is_dwz,
4d663531 22875 offset_size, include_hash);
8fc3fc34 22876
d521ce57 22877 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 22878 }
cf2c3c16
TT
22879 }
22880 break;
22881
2e276125 22882 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
22883 if (!section_is_gnu)
22884 {
22885 unsigned int bytes_read;
2e276125 22886
ac298888
TT
22887 /* This reads the constant, but since we don't recognize
22888 any vendor extensions, we ignore it. */
22889 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
22890 mac_ptr += bytes_read;
22891 read_direct_string (abfd, mac_ptr, &bytes_read);
22892 mac_ptr += bytes_read;
2e276125 22893
cf2c3c16
TT
22894 /* We don't recognize any vendor extensions. */
22895 break;
22896 }
22897 /* FALLTHROUGH */
22898
22899 default:
22900 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22901 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22902 section);
22903 if (mac_ptr == NULL)
22904 return;
22905 break;
2e276125 22906 }
757a13d0 22907 } while (macinfo_type != 0);
2e276125 22908}
8e19ed76 22909
cf2c3c16 22910static void
09262596 22911dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 22912 int section_is_gnu)
cf2c3c16 22913{
bb5ed363 22914 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
22915 struct line_header *lh = cu->line_header;
22916 bfd *abfd;
d521ce57 22917 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
22918 struct macro_source_file *current_file = 0;
22919 enum dwarf_macro_record_type macinfo_type;
22920 unsigned int offset_size = cu->header.offset_size;
d521ce57 22921 const gdb_byte *opcode_definitions[256];
8fc3fc34 22922 void **slot;
09262596
DE
22923 struct dwarf2_section_info *section;
22924 const char *section_name;
22925
22926 if (cu->dwo_unit != NULL)
22927 {
22928 if (section_is_gnu)
22929 {
22930 section = &cu->dwo_unit->dwo_file->sections.macro;
22931 section_name = ".debug_macro.dwo";
22932 }
22933 else
22934 {
22935 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22936 section_name = ".debug_macinfo.dwo";
22937 }
22938 }
22939 else
22940 {
22941 if (section_is_gnu)
22942 {
22943 section = &dwarf2_per_objfile->macro;
22944 section_name = ".debug_macro";
22945 }
22946 else
22947 {
22948 section = &dwarf2_per_objfile->macinfo;
22949 section_name = ".debug_macinfo";
22950 }
22951 }
cf2c3c16 22952
bb5ed363 22953 dwarf2_read_section (objfile, section);
cf2c3c16
TT
22954 if (section->buffer == NULL)
22955 {
fceca515 22956 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
22957 return;
22958 }
a32a8923 22959 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
22960
22961 /* First pass: Find the name of the base filename.
22962 This filename is needed in order to process all macros whose definition
22963 (or undefinition) comes from the command line. These macros are defined
22964 before the first DW_MACINFO_start_file entry, and yet still need to be
22965 associated to the base file.
22966
22967 To determine the base file name, we scan the macro definitions until we
22968 reach the first DW_MACINFO_start_file entry. We then initialize
22969 CURRENT_FILE accordingly so that any macro definition found before the
22970 first DW_MACINFO_start_file can still be associated to the base file. */
22971
22972 mac_ptr = section->buffer + offset;
22973 mac_end = section->buffer + section->size;
22974
22975 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22976 &offset_size, section_is_gnu);
22977 if (mac_ptr == NULL)
22978 {
22979 /* We already issued a complaint. */
22980 return;
22981 }
22982
22983 do
22984 {
22985 /* Do we at least have room for a macinfo type byte? */
22986 if (mac_ptr >= mac_end)
22987 {
22988 /* Complaint is printed during the second pass as GDB will probably
22989 stop the first pass earlier upon finding
22990 DW_MACINFO_start_file. */
22991 break;
22992 }
22993
aead7601 22994 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
22995 mac_ptr++;
22996
22997 /* Note that we rely on the fact that the corresponding GNU and
22998 DWARF constants are the same. */
22999 switch (macinfo_type)
23000 {
23001 /* A zero macinfo type indicates the end of the macro
23002 information. */
23003 case 0:
23004 break;
23005
0af92d60
JK
23006 case DW_MACRO_define:
23007 case DW_MACRO_undef:
cf2c3c16
TT
23008 /* Only skip the data by MAC_PTR. */
23009 {
23010 unsigned int bytes_read;
23011
23012 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23013 mac_ptr += bytes_read;
23014 read_direct_string (abfd, mac_ptr, &bytes_read);
23015 mac_ptr += bytes_read;
23016 }
23017 break;
23018
0af92d60 23019 case DW_MACRO_start_file:
cf2c3c16
TT
23020 {
23021 unsigned int bytes_read;
23022 int line, file;
23023
23024 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23025 mac_ptr += bytes_read;
23026 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23027 mac_ptr += bytes_read;
23028
43f3e411 23029 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
23030 }
23031 break;
23032
0af92d60 23033 case DW_MACRO_end_file:
cf2c3c16
TT
23034 /* No data to skip by MAC_PTR. */
23035 break;
23036
0af92d60
JK
23037 case DW_MACRO_define_strp:
23038 case DW_MACRO_undef_strp:
23039 case DW_MACRO_define_sup:
23040 case DW_MACRO_undef_sup:
cf2c3c16
TT
23041 {
23042 unsigned int bytes_read;
23043
23044 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23045 mac_ptr += bytes_read;
23046 mac_ptr += offset_size;
23047 }
23048 break;
23049
0af92d60
JK
23050 case DW_MACRO_import:
23051 case DW_MACRO_import_sup:
cf2c3c16 23052 /* Note that, according to the spec, a transparent include
0af92d60 23053 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
23054 skip this opcode. */
23055 mac_ptr += offset_size;
23056 break;
23057
23058 case DW_MACINFO_vendor_ext:
23059 /* Only skip the data by MAC_PTR. */
23060 if (!section_is_gnu)
23061 {
23062 unsigned int bytes_read;
23063
23064 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23065 mac_ptr += bytes_read;
23066 read_direct_string (abfd, mac_ptr, &bytes_read);
23067 mac_ptr += bytes_read;
23068 }
23069 /* FALLTHROUGH */
23070
23071 default:
23072 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 23073 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
23074 section);
23075 if (mac_ptr == NULL)
23076 return;
23077 break;
23078 }
23079 } while (macinfo_type != 0 && current_file == NULL);
23080
23081 /* Second pass: Process all entries.
23082
23083 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23084 command-line macro definitions/undefinitions. This flag is unset when we
23085 reach the first DW_MACINFO_start_file entry. */
23086
fc4007c9
TT
23087 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23088 htab_eq_pointer,
23089 NULL, xcalloc, xfree));
8fc3fc34 23090 mac_ptr = section->buffer + offset;
fc4007c9 23091 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 23092 *slot = (void *) mac_ptr;
8fc3fc34 23093 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 23094 current_file, lh, section,
fc4007c9
TT
23095 section_is_gnu, 0, offset_size,
23096 include_hash.get ());
cf2c3c16
TT
23097}
23098
8e19ed76 23099/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 23100 if so return true else false. */
380bca97 23101
8e19ed76 23102static int
6e5a29e1 23103attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
23104{
23105 return (attr == NULL ? 0 :
23106 attr->form == DW_FORM_block1
23107 || attr->form == DW_FORM_block2
23108 || attr->form == DW_FORM_block4
2dc7f7b3
TT
23109 || attr->form == DW_FORM_block
23110 || attr->form == DW_FORM_exprloc);
8e19ed76 23111}
4c2df51b 23112
c6a0999f
JB
23113/* Return non-zero if ATTR's value is a section offset --- classes
23114 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
23115 You may use DW_UNSND (attr) to retrieve such offsets.
23116
23117 Section 7.5.4, "Attribute Encodings", explains that no attribute
23118 may have a value that belongs to more than one of these classes; it
23119 would be ambiguous if we did, because we use the same forms for all
23120 of them. */
380bca97 23121
3690dd37 23122static int
6e5a29e1 23123attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
23124{
23125 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
23126 || attr->form == DW_FORM_data8
23127 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
23128}
23129
3690dd37
JB
23130/* Return non-zero if ATTR's value falls in the 'constant' class, or
23131 zero otherwise. When this function returns true, you can apply
23132 dwarf2_get_attr_constant_value to it.
23133
23134 However, note that for some attributes you must check
23135 attr_form_is_section_offset before using this test. DW_FORM_data4
23136 and DW_FORM_data8 are members of both the constant class, and of
23137 the classes that contain offsets into other debug sections
23138 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
23139 that, if an attribute's can be either a constant or one of the
23140 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
23141 taken as section offsets, not constants.
23142
23143 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
23144 cannot handle that. */
380bca97 23145
3690dd37 23146static int
6e5a29e1 23147attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
23148{
23149 switch (attr->form)
23150 {
23151 case DW_FORM_sdata:
23152 case DW_FORM_udata:
23153 case DW_FORM_data1:
23154 case DW_FORM_data2:
23155 case DW_FORM_data4:
23156 case DW_FORM_data8:
663c44ac 23157 case DW_FORM_implicit_const:
3690dd37
JB
23158 return 1;
23159 default:
23160 return 0;
23161 }
23162}
23163
7771576e
SA
23164
23165/* DW_ADDR is always stored already as sect_offset; despite for the forms
23166 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
23167
23168static int
6e5a29e1 23169attr_form_is_ref (const struct attribute *attr)
7771576e
SA
23170{
23171 switch (attr->form)
23172 {
23173 case DW_FORM_ref_addr:
23174 case DW_FORM_ref1:
23175 case DW_FORM_ref2:
23176 case DW_FORM_ref4:
23177 case DW_FORM_ref8:
23178 case DW_FORM_ref_udata:
23179 case DW_FORM_GNU_ref_alt:
23180 return 1;
23181 default:
23182 return 0;
23183 }
23184}
23185
3019eac3
DE
23186/* Return the .debug_loc section to use for CU.
23187 For DWO files use .debug_loc.dwo. */
23188
23189static struct dwarf2_section_info *
23190cu_debug_loc_section (struct dwarf2_cu *cu)
23191{
23192 if (cu->dwo_unit)
43988095
JK
23193 {
23194 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23195
23196 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23197 }
23198 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23199 : &dwarf2_per_objfile->loc);
3019eac3
DE
23200}
23201
8cf6f0b1
TT
23202/* A helper function that fills in a dwarf2_loclist_baton. */
23203
23204static void
23205fill_in_loclist_baton (struct dwarf2_cu *cu,
23206 struct dwarf2_loclist_baton *baton,
ff39bb5e 23207 const struct attribute *attr)
8cf6f0b1 23208{
3019eac3
DE
23209 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23210
23211 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
23212
23213 baton->per_cu = cu->per_cu;
23214 gdb_assert (baton->per_cu);
23215 /* We don't know how long the location list is, but make sure we
23216 don't run off the edge of the section. */
3019eac3
DE
23217 baton->size = section->size - DW_UNSND (attr);
23218 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 23219 baton->base_address = cu->base_address;
f664829e 23220 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23221}
23222
4c2df51b 23223static void
ff39bb5e 23224dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23225 struct dwarf2_cu *cu, int is_block)
4c2df51b 23226{
bb5ed363 23227 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23228 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23229
3690dd37 23230 if (attr_form_is_section_offset (attr)
3019eac3 23231 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23232 the section. If so, fall through to the complaint in the
23233 other branch. */
3019eac3 23234 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 23235 {
0d53c4c4 23236 struct dwarf2_loclist_baton *baton;
4c2df51b 23237
8d749320 23238 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23239
8cf6f0b1 23240 fill_in_loclist_baton (cu, baton, attr);
be391dca 23241
d00adf39 23242 if (cu->base_known == 0)
0d53c4c4 23243 complaint (&symfile_complaints,
3e43a32a
MS
23244 _("Location list used without "
23245 "specifying the CU base address."));
4c2df51b 23246
f1e6e072
TT
23247 SYMBOL_ACLASS_INDEX (sym) = (is_block
23248 ? dwarf2_loclist_block_index
23249 : dwarf2_loclist_index);
0d53c4c4
DJ
23250 SYMBOL_LOCATION_BATON (sym) = baton;
23251 }
23252 else
23253 {
23254 struct dwarf2_locexpr_baton *baton;
23255
8d749320 23256 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
23257 baton->per_cu = cu->per_cu;
23258 gdb_assert (baton->per_cu);
0d53c4c4
DJ
23259
23260 if (attr_form_is_block (attr))
23261 {
23262 /* Note that we're just copying the block's data pointer
23263 here, not the actual data. We're still pointing into the
6502dd73
DJ
23264 info_buffer for SYM's objfile; right now we never release
23265 that buffer, but when we do clean up properly this may
23266 need to change. */
0d53c4c4
DJ
23267 baton->size = DW_BLOCK (attr)->size;
23268 baton->data = DW_BLOCK (attr)->data;
23269 }
23270 else
23271 {
23272 dwarf2_invalid_attrib_class_complaint ("location description",
23273 SYMBOL_NATURAL_NAME (sym));
23274 baton->size = 0;
0d53c4c4 23275 }
6e70227d 23276
f1e6e072
TT
23277 SYMBOL_ACLASS_INDEX (sym) = (is_block
23278 ? dwarf2_locexpr_block_index
23279 : dwarf2_locexpr_index);
0d53c4c4
DJ
23280 SYMBOL_LOCATION_BATON (sym) = baton;
23281 }
4c2df51b 23282}
6502dd73 23283
9aa1f1e3
TT
23284/* Return the OBJFILE associated with the compilation unit CU. If CU
23285 came from a separate debuginfo file, then the master objfile is
23286 returned. */
ae0d2f24
UW
23287
23288struct objfile *
23289dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
23290{
9291a0cd 23291 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
23292
23293 /* Return the master objfile, so that we can report and look up the
23294 correct file containing this variable. */
23295 if (objfile->separate_debug_objfile_backlink)
23296 objfile = objfile->separate_debug_objfile_backlink;
23297
23298 return objfile;
23299}
23300
96408a79
SA
23301/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23302 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23303 CU_HEADERP first. */
23304
23305static const struct comp_unit_head *
23306per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23307 struct dwarf2_per_cu_data *per_cu)
23308{
d521ce57 23309 const gdb_byte *info_ptr;
96408a79
SA
23310
23311 if (per_cu->cu)
23312 return &per_cu->cu->header;
23313
9c541725 23314 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
23315
23316 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
23317 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23318 rcuh_kind::COMPILE);
96408a79
SA
23319
23320 return cu_headerp;
23321}
23322
ae0d2f24
UW
23323/* Return the address size given in the compilation unit header for CU. */
23324
98714339 23325int
ae0d2f24
UW
23326dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
23327{
96408a79
SA
23328 struct comp_unit_head cu_header_local;
23329 const struct comp_unit_head *cu_headerp;
c471e790 23330
96408a79
SA
23331 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23332
23333 return cu_headerp->addr_size;
ae0d2f24
UW
23334}
23335
9eae7c52
TT
23336/* Return the offset size given in the compilation unit header for CU. */
23337
23338int
23339dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
23340{
96408a79
SA
23341 struct comp_unit_head cu_header_local;
23342 const struct comp_unit_head *cu_headerp;
9c6c53f7 23343
96408a79
SA
23344 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23345
23346 return cu_headerp->offset_size;
23347}
23348
23349/* See its dwarf2loc.h declaration. */
23350
23351int
23352dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
23353{
23354 struct comp_unit_head cu_header_local;
23355 const struct comp_unit_head *cu_headerp;
23356
23357 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23358
23359 if (cu_headerp->version == 2)
23360 return cu_headerp->addr_size;
23361 else
23362 return cu_headerp->offset_size;
181cebd4
JK
23363}
23364
9aa1f1e3
TT
23365/* Return the text offset of the CU. The returned offset comes from
23366 this CU's objfile. If this objfile came from a separate debuginfo
23367 file, then the offset may be different from the corresponding
23368 offset in the parent objfile. */
23369
23370CORE_ADDR
23371dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
23372{
bb3fa9d0 23373 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
23374
23375 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23376}
23377
43988095
JK
23378/* Return DWARF version number of PER_CU. */
23379
23380short
23381dwarf2_version (struct dwarf2_per_cu_data *per_cu)
23382{
23383 return per_cu->dwarf_version;
23384}
23385
348e048f
DE
23386/* Locate the .debug_info compilation unit from CU's objfile which contains
23387 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
23388
23389static struct dwarf2_per_cu_data *
9c541725 23390dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 23391 unsigned int offset_in_dwz,
ae038cb0
DJ
23392 struct objfile *objfile)
23393{
23394 struct dwarf2_per_cu_data *this_cu;
23395 int low, high;
36586728 23396 const sect_offset *cu_off;
ae038cb0 23397
ae038cb0
DJ
23398 low = 0;
23399 high = dwarf2_per_objfile->n_comp_units - 1;
23400 while (high > low)
23401 {
36586728 23402 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23403 int mid = low + (high - low) / 2;
9a619af0 23404
36586728 23405 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 23406 cu_off = &mid_cu->sect_off;
36586728 23407 if (mid_cu->is_dwz > offset_in_dwz
9c541725 23408 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
23409 high = mid;
23410 else
23411 low = mid + 1;
23412 }
23413 gdb_assert (low == high);
36586728 23414 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
23415 cu_off = &this_cu->sect_off;
23416 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 23417 {
36586728 23418 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23419 error (_("Dwarf Error: could not find partial DIE containing "
9c541725
PA
23420 "offset 0x%x [in module %s]"),
23421 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
10b3939b 23422
9c541725
PA
23423 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23424 <= sect_off);
ae038cb0
DJ
23425 return dwarf2_per_objfile->all_comp_units[low-1];
23426 }
23427 else
23428 {
23429 this_cu = dwarf2_per_objfile->all_comp_units[low];
23430 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725
PA
23431 && sect_off >= this_cu->sect_off + this_cu->length)
23432 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
23433 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23434 return this_cu;
23435 }
23436}
23437
23745b47 23438/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 23439
9816fde3 23440static void
23745b47 23441init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 23442{
9816fde3 23443 memset (cu, 0, sizeof (*cu));
23745b47
DE
23444 per_cu->cu = cu;
23445 cu->per_cu = per_cu;
23446 cu->objfile = per_cu->objfile;
93311388 23447 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
23448}
23449
23450/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23451
23452static void
95554aad
TT
23453prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23454 enum language pretend_language)
9816fde3
JK
23455{
23456 struct attribute *attr;
23457
23458 /* Set the language we're debugging. */
23459 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23460 if (attr)
23461 set_cu_language (DW_UNSND (attr), cu);
23462 else
9cded63f 23463 {
95554aad 23464 cu->language = pretend_language;
9cded63f
TT
23465 cu->language_defn = language_def (cu->language);
23466 }
dee91e82 23467
7d45c7c3 23468 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23469}
23470
ae038cb0
DJ
23471/* Release one cached compilation unit, CU. We unlink it from the tree
23472 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
23473 the caller is responsible for that.
23474 NOTE: DATA is a void * because this function is also used as a
23475 cleanup routine. */
ae038cb0
DJ
23476
23477static void
68dc6402 23478free_heap_comp_unit (void *data)
ae038cb0 23479{
9a3c8263 23480 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 23481
23745b47
DE
23482 gdb_assert (cu->per_cu != NULL);
23483 cu->per_cu->cu = NULL;
ae038cb0
DJ
23484 cu->per_cu = NULL;
23485
23486 obstack_free (&cu->comp_unit_obstack, NULL);
23487
23488 xfree (cu);
23489}
23490
72bf9492 23491/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 23492 when we're finished with it. We can't free the pointer itself, but be
dee91e82 23493 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
23494
23495static void
23496free_stack_comp_unit (void *data)
23497{
9a3c8263 23498 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 23499
23745b47
DE
23500 gdb_assert (cu->per_cu != NULL);
23501 cu->per_cu->cu = NULL;
23502 cu->per_cu = NULL;
23503
72bf9492
DJ
23504 obstack_free (&cu->comp_unit_obstack, NULL);
23505 cu->partial_dies = NULL;
ae038cb0
DJ
23506}
23507
23508/* Free all cached compilation units. */
23509
23510static void
23511free_cached_comp_units (void *data)
23512{
330cdd98 23513 dwarf2_per_objfile->free_cached_comp_units ();
ae038cb0
DJ
23514}
23515
23516/* Increase the age counter on each cached compilation unit, and free
23517 any that are too old. */
23518
23519static void
23520age_cached_comp_units (void)
23521{
23522 struct dwarf2_per_cu_data *per_cu, **last_chain;
23523
23524 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23525 per_cu = dwarf2_per_objfile->read_in_chain;
23526 while (per_cu != NULL)
23527 {
23528 per_cu->cu->last_used ++;
b4f54984 23529 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23530 dwarf2_mark (per_cu->cu);
23531 per_cu = per_cu->cu->read_in_chain;
23532 }
23533
23534 per_cu = dwarf2_per_objfile->read_in_chain;
23535 last_chain = &dwarf2_per_objfile->read_in_chain;
23536 while (per_cu != NULL)
23537 {
23538 struct dwarf2_per_cu_data *next_cu;
23539
23540 next_cu = per_cu->cu->read_in_chain;
23541
23542 if (!per_cu->cu->mark)
23543 {
68dc6402 23544 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
23545 *last_chain = next_cu;
23546 }
23547 else
23548 last_chain = &per_cu->cu->read_in_chain;
23549
23550 per_cu = next_cu;
23551 }
23552}
23553
23554/* Remove a single compilation unit from the cache. */
23555
23556static void
dee91e82 23557free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
23558{
23559 struct dwarf2_per_cu_data *per_cu, **last_chain;
23560
23561 per_cu = dwarf2_per_objfile->read_in_chain;
23562 last_chain = &dwarf2_per_objfile->read_in_chain;
23563 while (per_cu != NULL)
23564 {
23565 struct dwarf2_per_cu_data *next_cu;
23566
23567 next_cu = per_cu->cu->read_in_chain;
23568
dee91e82 23569 if (per_cu == target_per_cu)
ae038cb0 23570 {
68dc6402 23571 free_heap_comp_unit (per_cu->cu);
dee91e82 23572 per_cu->cu = NULL;
ae038cb0
DJ
23573 *last_chain = next_cu;
23574 break;
23575 }
23576 else
23577 last_chain = &per_cu->cu->read_in_chain;
23578
23579 per_cu = next_cu;
23580 }
23581}
23582
fe3e1990
DJ
23583/* Release all extra memory associated with OBJFILE. */
23584
23585void
23586dwarf2_free_objfile (struct objfile *objfile)
23587{
9a3c8263
SM
23588 dwarf2_per_objfile
23589 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23590 dwarf2_objfile_data_key);
fe3e1990
DJ
23591
23592 if (dwarf2_per_objfile == NULL)
23593 return;
23594
330cdd98 23595 dwarf2_per_objfile->~dwarf2_per_objfile ();
fe3e1990
DJ
23596}
23597
dee91e82
DE
23598/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23599 We store these in a hash table separate from the DIEs, and preserve them
23600 when the DIEs are flushed out of cache.
23601
23602 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23603 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23604 or the type may come from a DWO file. Furthermore, while it's more logical
23605 to use per_cu->section+offset, with Fission the section with the data is in
23606 the DWO file but we don't know that section at the point we need it.
23607 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23608 because we can enter the lookup routine, get_die_type_at_offset, from
23609 outside this file, and thus won't necessarily have PER_CU->cu.
23610 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23611
dee91e82 23612struct dwarf2_per_cu_offset_and_type
1c379e20 23613{
dee91e82 23614 const struct dwarf2_per_cu_data *per_cu;
9c541725 23615 sect_offset sect_off;
1c379e20
DJ
23616 struct type *type;
23617};
23618
dee91e82 23619/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23620
23621static hashval_t
dee91e82 23622per_cu_offset_and_type_hash (const void *item)
1c379e20 23623{
9a3c8263
SM
23624 const struct dwarf2_per_cu_offset_and_type *ofs
23625 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23626
9c541725 23627 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23628}
23629
dee91e82 23630/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23631
23632static int
dee91e82 23633per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23634{
9a3c8263
SM
23635 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23636 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23637 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23638 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23639
dee91e82 23640 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23641 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23642}
23643
23644/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23645 table if necessary. For convenience, return TYPE.
23646
23647 The DIEs reading must have careful ordering to:
23648 * Not cause infite loops trying to read in DIEs as a prerequisite for
23649 reading current DIE.
23650 * Not trying to dereference contents of still incompletely read in types
23651 while reading in other DIEs.
23652 * Enable referencing still incompletely read in types just by a pointer to
23653 the type without accessing its fields.
23654
23655 Therefore caller should follow these rules:
23656 * Try to fetch any prerequisite types we may need to build this DIE type
23657 before building the type and calling set_die_type.
e71ec853 23658 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23659 possible before fetching more types to complete the current type.
23660 * Make the type as complete as possible before fetching more types. */
1c379e20 23661
f792889a 23662static struct type *
1c379e20
DJ
23663set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23664{
dee91e82 23665 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 23666 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
23667 struct attribute *attr;
23668 struct dynamic_prop prop;
1c379e20 23669
b4ba55a1
JB
23670 /* For Ada types, make sure that the gnat-specific data is always
23671 initialized (if not already set). There are a few types where
23672 we should not be doing so, because the type-specific area is
23673 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23674 where the type-specific area is used to store the floatformat).
23675 But this is not a problem, because the gnat-specific information
23676 is actually not needed for these types. */
23677 if (need_gnat_info (cu)
23678 && TYPE_CODE (type) != TYPE_CODE_FUNC
23679 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
23680 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23681 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23682 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
23683 && !HAVE_GNAT_AUX_INFO (type))
23684 INIT_GNAT_SPECIFIC (type);
23685
3f2f83dd
KB
23686 /* Read DW_AT_allocated and set in type. */
23687 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23688 if (attr_form_is_block (attr))
23689 {
23690 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23691 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
23692 }
23693 else if (attr != NULL)
23694 {
23695 complaint (&symfile_complaints,
9c541725
PA
23696 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
23697 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23698 to_underlying (die->sect_off));
3f2f83dd
KB
23699 }
23700
23701 /* Read DW_AT_associated and set in type. */
23702 attr = dwarf2_attr (die, DW_AT_associated, cu);
23703 if (attr_form_is_block (attr))
23704 {
23705 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23706 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
23707 }
23708 else if (attr != NULL)
23709 {
23710 complaint (&symfile_complaints,
9c541725
PA
23711 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
23712 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23713 to_underlying (die->sect_off));
3f2f83dd
KB
23714 }
23715
3cdcd0ce
JB
23716 /* Read DW_AT_data_location and set in type. */
23717 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23718 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 23719 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 23720
dee91e82 23721 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23722 {
dee91e82
DE
23723 dwarf2_per_objfile->die_type_hash =
23724 htab_create_alloc_ex (127,
23725 per_cu_offset_and_type_hash,
23726 per_cu_offset_and_type_eq,
23727 NULL,
23728 &objfile->objfile_obstack,
23729 hashtab_obstack_allocate,
23730 dummy_obstack_deallocate);
f792889a 23731 }
1c379e20 23732
dee91e82 23733 ofs.per_cu = cu->per_cu;
9c541725 23734 ofs.sect_off = die->sect_off;
1c379e20 23735 ofs.type = type;
dee91e82
DE
23736 slot = (struct dwarf2_per_cu_offset_and_type **)
23737 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
23738 if (*slot)
23739 complaint (&symfile_complaints,
23740 _("A problem internal to GDB: DIE 0x%x has type already set"),
9c541725 23741 to_underlying (die->sect_off));
8d749320
SM
23742 *slot = XOBNEW (&objfile->objfile_obstack,
23743 struct dwarf2_per_cu_offset_and_type);
1c379e20 23744 **slot = ofs;
f792889a 23745 return type;
1c379e20
DJ
23746}
23747
9c541725 23748/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23749 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23750
23751static struct type *
9c541725 23752get_die_type_at_offset (sect_offset sect_off,
673bfd45 23753 struct dwarf2_per_cu_data *per_cu)
1c379e20 23754{
dee91e82 23755 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23756
dee91e82 23757 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23758 return NULL;
1c379e20 23759
dee91e82 23760 ofs.per_cu = per_cu;
9c541725 23761 ofs.sect_off = sect_off;
9a3c8263
SM
23762 slot = ((struct dwarf2_per_cu_offset_and_type *)
23763 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
23764 if (slot)
23765 return slot->type;
23766 else
23767 return NULL;
23768}
23769
02142a6c 23770/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23771 or return NULL if DIE does not have a saved type. */
23772
23773static struct type *
23774get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23775{
9c541725 23776 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23777}
23778
10b3939b
DJ
23779/* Add a dependence relationship from CU to REF_PER_CU. */
23780
23781static void
23782dwarf2_add_dependence (struct dwarf2_cu *cu,
23783 struct dwarf2_per_cu_data *ref_per_cu)
23784{
23785 void **slot;
23786
23787 if (cu->dependencies == NULL)
23788 cu->dependencies
23789 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23790 NULL, &cu->comp_unit_obstack,
23791 hashtab_obstack_allocate,
23792 dummy_obstack_deallocate);
23793
23794 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23795 if (*slot == NULL)
23796 *slot = ref_per_cu;
23797}
1c379e20 23798
f504f079
DE
23799/* Subroutine of dwarf2_mark to pass to htab_traverse.
23800 Set the mark field in every compilation unit in the
ae038cb0
DJ
23801 cache that we must keep because we are keeping CU. */
23802
10b3939b
DJ
23803static int
23804dwarf2_mark_helper (void **slot, void *data)
23805{
23806 struct dwarf2_per_cu_data *per_cu;
23807
23808 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23809
23810 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23811 reading of the chain. As such dependencies remain valid it is not much
23812 useful to track and undo them during QUIT cleanups. */
23813 if (per_cu->cu == NULL)
23814 return 1;
23815
10b3939b
DJ
23816 if (per_cu->cu->mark)
23817 return 1;
23818 per_cu->cu->mark = 1;
23819
23820 if (per_cu->cu->dependencies != NULL)
23821 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23822
23823 return 1;
23824}
23825
f504f079
DE
23826/* Set the mark field in CU and in every other compilation unit in the
23827 cache that we must keep because we are keeping CU. */
23828
ae038cb0
DJ
23829static void
23830dwarf2_mark (struct dwarf2_cu *cu)
23831{
23832 if (cu->mark)
23833 return;
23834 cu->mark = 1;
10b3939b
DJ
23835 if (cu->dependencies != NULL)
23836 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23837}
23838
23839static void
23840dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23841{
23842 while (per_cu)
23843 {
23844 per_cu->cu->mark = 0;
23845 per_cu = per_cu->cu->read_in_chain;
23846 }
72bf9492
DJ
23847}
23848
72bf9492
DJ
23849/* Trivial hash function for partial_die_info: the hash value of a DIE
23850 is its offset in .debug_info for this objfile. */
23851
23852static hashval_t
23853partial_die_hash (const void *item)
23854{
9a3c8263
SM
23855 const struct partial_die_info *part_die
23856 = (const struct partial_die_info *) item;
9a619af0 23857
9c541725 23858 return to_underlying (part_die->sect_off);
72bf9492
DJ
23859}
23860
23861/* Trivial comparison function for partial_die_info structures: two DIEs
23862 are equal if they have the same offset. */
23863
23864static int
23865partial_die_eq (const void *item_lhs, const void *item_rhs)
23866{
9a3c8263
SM
23867 const struct partial_die_info *part_die_lhs
23868 = (const struct partial_die_info *) item_lhs;
23869 const struct partial_die_info *part_die_rhs
23870 = (const struct partial_die_info *) item_rhs;
9a619af0 23871
9c541725 23872 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23873}
23874
b4f54984
DE
23875static struct cmd_list_element *set_dwarf_cmdlist;
23876static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
23877
23878static void
981a3fb3 23879set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 23880{
b4f54984 23881 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 23882 gdb_stdout);
ae038cb0
DJ
23883}
23884
23885static void
981a3fb3 23886show_dwarf_cmd (const char *args, int from_tty)
6e70227d 23887{
b4f54984 23888 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
23889}
23890
4bf44c1c 23891/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
23892
23893static void
c1bd65d0 23894dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 23895{
9a3c8263 23896 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 23897 int ix;
8b70b953 23898
626f2d1c
TT
23899 /* Make sure we don't accidentally use dwarf2_per_objfile while
23900 cleaning up. */
23901 dwarf2_per_objfile = NULL;
23902
59b0c7c1
JB
23903 for (ix = 0; ix < data->n_comp_units; ++ix)
23904 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 23905
59b0c7c1 23906 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 23907 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
23908 data->all_type_units[ix]->per_cu.imported_symtabs);
23909 xfree (data->all_type_units);
95554aad 23910
8b70b953 23911 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
23912
23913 if (data->dwo_files)
23914 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
23915 if (data->dwp_file)
23916 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
23917
23918 if (data->dwz_file && data->dwz_file->dwz_bfd)
23919 gdb_bfd_unref (data->dwz_file->dwz_bfd);
3f563c84
PA
23920
23921 if (data->index_table != NULL)
23922 data->index_table->~mapped_index ();
9291a0cd
TT
23923}
23924
23925\f
ae2de4f8 23926/* The "save gdb-index" command. */
9291a0cd 23927
bc8f2430
JK
23928/* In-memory buffer to prepare data to be written later to a file. */
23929class data_buf
9291a0cd 23930{
bc8f2430 23931public:
bc8f2430
JK
23932 /* Copy DATA to the end of the buffer. */
23933 template<typename T>
23934 void append_data (const T &data)
23935 {
23936 std::copy (reinterpret_cast<const gdb_byte *> (&data),
23937 reinterpret_cast<const gdb_byte *> (&data + 1),
c2f134ac 23938 grow (sizeof (data)));
bc8f2430 23939 }
b89be57b 23940
c2f134ac
PA
23941 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
23942 terminating zero is appended too. */
bc8f2430
JK
23943 void append_cstr0 (const char *cstr)
23944 {
23945 const size_t size = strlen (cstr) + 1;
c2f134ac
PA
23946 std::copy (cstr, cstr + size, grow (size));
23947 }
23948
23949 /* Accept a host-format integer in VAL and append it to the buffer
23950 as a target-format integer which is LEN bytes long. */
23951 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
23952 {
23953 ::store_unsigned_integer (grow (len), len, byte_order, val);
bc8f2430 23954 }
9291a0cd 23955
bc8f2430
JK
23956 /* Return the size of the buffer. */
23957 size_t size () const
23958 {
23959 return m_vec.size ();
23960 }
23961
23962 /* Write the buffer to FILE. */
23963 void file_write (FILE *file) const
23964 {
a81e6d4d
PA
23965 if (::fwrite (m_vec.data (), 1, m_vec.size (), file) != m_vec.size ())
23966 error (_("couldn't write data to file"));
bc8f2430
JK
23967 }
23968
23969private:
c2f134ac
PA
23970 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
23971 the start of the new block. */
23972 gdb_byte *grow (size_t size)
23973 {
23974 m_vec.resize (m_vec.size () + size);
23975 return &*m_vec.end () - size;
23976 }
23977
d5722aa2 23978 gdb::byte_vector m_vec;
bc8f2430 23979};
9291a0cd
TT
23980
23981/* An entry in the symbol table. */
23982struct symtab_index_entry
23983{
23984 /* The name of the symbol. */
23985 const char *name;
23986 /* The offset of the name in the constant pool. */
23987 offset_type index_offset;
23988 /* A sorted vector of the indices of all the CUs that hold an object
23989 of this name. */
bc8f2430 23990 std::vector<offset_type> cu_indices;
9291a0cd
TT
23991};
23992
23993/* The symbol table. This is a power-of-2-sized hash table. */
23994struct mapped_symtab
23995{
bc8f2430
JK
23996 mapped_symtab ()
23997 {
23998 data.resize (1024);
23999 }
b89be57b 24000
bc8f2430 24001 offset_type n_elements = 0;
4b76cda9 24002 std::vector<symtab_index_entry> data;
bc8f2430 24003};
9291a0cd 24004
bc8f2430 24005/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
559a7a62
JK
24006 the slot.
24007
24008 Function is used only during write_hash_table so no index format backward
24009 compatibility is needed. */
b89be57b 24010
4b76cda9 24011static symtab_index_entry &
9291a0cd
TT
24012find_slot (struct mapped_symtab *symtab, const char *name)
24013{
559a7a62 24014 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd 24015
bc8f2430
JK
24016 index = hash & (symtab->data.size () - 1);
24017 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
9291a0cd
TT
24018
24019 for (;;)
24020 {
4b76cda9
PA
24021 if (symtab->data[index].name == NULL
24022 || strcmp (name, symtab->data[index].name) == 0)
bc8f2430
JK
24023 return symtab->data[index];
24024 index = (index + step) & (symtab->data.size () - 1);
9291a0cd
TT
24025 }
24026}
24027
24028/* Expand SYMTAB's hash table. */
b89be57b 24029
9291a0cd
TT
24030static void
24031hash_expand (struct mapped_symtab *symtab)
24032{
bc8f2430 24033 auto old_entries = std::move (symtab->data);
9291a0cd 24034
bc8f2430
JK
24035 symtab->data.clear ();
24036 symtab->data.resize (old_entries.size () * 2);
9291a0cd 24037
bc8f2430 24038 for (auto &it : old_entries)
4b76cda9 24039 if (it.name != NULL)
bc8f2430 24040 {
4b76cda9 24041 auto &ref = find_slot (symtab, it.name);
bc8f2430
JK
24042 ref = std::move (it);
24043 }
9291a0cd
TT
24044}
24045
156942c7
DE
24046/* Add an entry to SYMTAB. NAME is the name of the symbol.
24047 CU_INDEX is the index of the CU in which the symbol appears.
24048 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 24049
9291a0cd
TT
24050static void
24051add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 24052 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
24053 offset_type cu_index)
24054{
156942c7 24055 offset_type cu_index_and_attrs;
9291a0cd
TT
24056
24057 ++symtab->n_elements;
bc8f2430 24058 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
9291a0cd
TT
24059 hash_expand (symtab);
24060
4b76cda9
PA
24061 symtab_index_entry &slot = find_slot (symtab, name);
24062 if (slot.name == NULL)
9291a0cd 24063 {
4b76cda9 24064 slot.name = name;
156942c7 24065 /* index_offset is set later. */
9291a0cd 24066 }
156942c7
DE
24067
24068 cu_index_and_attrs = 0;
24069 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
24070 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
24071 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
24072
24073 /* We don't want to record an index value twice as we want to avoid the
24074 duplication.
24075 We process all global symbols and then all static symbols
24076 (which would allow us to avoid the duplication by only having to check
24077 the last entry pushed), but a symbol could have multiple kinds in one CU.
24078 To keep things simple we don't worry about the duplication here and
24079 sort and uniqufy the list after we've processed all symbols. */
4b76cda9 24080 slot.cu_indices.push_back (cu_index_and_attrs);
156942c7
DE
24081}
24082
24083/* Sort and remove duplicates of all symbols' cu_indices lists. */
24084
24085static void
24086uniquify_cu_indices (struct mapped_symtab *symtab)
24087{
4b76cda9 24088 for (auto &entry : symtab->data)
156942c7 24089 {
4b76cda9 24090 if (entry.name != NULL && !entry.cu_indices.empty ())
156942c7 24091 {
4b76cda9 24092 auto &cu_indices = entry.cu_indices;
6fd931f2
PA
24093 std::sort (cu_indices.begin (), cu_indices.end ());
24094 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
24095 cu_indices.erase (from, cu_indices.end ());
156942c7
DE
24096 }
24097 }
9291a0cd
TT
24098}
24099
bc8f2430
JK
24100/* A form of 'const char *' suitable for container keys. Only the
24101 pointer is stored. The strings themselves are compared, not the
24102 pointers. */
24103class c_str_view
9291a0cd 24104{
bc8f2430
JK
24105public:
24106 c_str_view (const char *cstr)
24107 : m_cstr (cstr)
24108 {}
9291a0cd 24109
bc8f2430
JK
24110 bool operator== (const c_str_view &other) const
24111 {
24112 return strcmp (m_cstr, other.m_cstr) == 0;
24113 }
9291a0cd 24114
bc8f2430
JK
24115private:
24116 friend class c_str_view_hasher;
24117 const char *const m_cstr;
24118};
9291a0cd 24119
bc8f2430
JK
24120/* A std::unordered_map::hasher for c_str_view that uses the right
24121 hash function for strings in a mapped index. */
24122class c_str_view_hasher
24123{
24124public:
24125 size_t operator () (const c_str_view &x) const
24126 {
24127 return mapped_index_string_hash (INT_MAX, x.m_cstr);
24128 }
24129};
b89be57b 24130
bc8f2430
JK
24131/* A std::unordered_map::hasher for std::vector<>. */
24132template<typename T>
24133class vector_hasher
9291a0cd 24134{
bc8f2430
JK
24135public:
24136 size_t operator () (const std::vector<T> &key) const
24137 {
24138 return iterative_hash (key.data (),
24139 sizeof (key.front ()) * key.size (), 0);
24140 }
24141};
9291a0cd 24142
bc8f2430
JK
24143/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
24144 constant pool entries going into the data buffer CPOOL. */
3876f04e 24145
bc8f2430
JK
24146static void
24147write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
24148{
24149 {
24150 /* Elements are sorted vectors of the indices of all the CUs that
24151 hold an object of this name. */
24152 std::unordered_map<std::vector<offset_type>, offset_type,
24153 vector_hasher<offset_type>>
24154 symbol_hash_table;
24155
24156 /* We add all the index vectors to the constant pool first, to
24157 ensure alignment is ok. */
4b76cda9 24158 for (symtab_index_entry &entry : symtab->data)
bc8f2430 24159 {
4b76cda9 24160 if (entry.name == NULL)
bc8f2430 24161 continue;
4b76cda9 24162 gdb_assert (entry.index_offset == 0);
70a1152b
PA
24163
24164 /* Finding before inserting is faster than always trying to
24165 insert, because inserting always allocates a node, does the
24166 lookup, and then destroys the new node if another node
24167 already had the same key. C++17 try_emplace will avoid
24168 this. */
24169 const auto found
4b76cda9 24170 = symbol_hash_table.find (entry.cu_indices);
70a1152b
PA
24171 if (found != symbol_hash_table.end ())
24172 {
4b76cda9 24173 entry.index_offset = found->second;
70a1152b
PA
24174 continue;
24175 }
24176
4b76cda9
PA
24177 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
24178 entry.index_offset = cpool.size ();
24179 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
24180 for (const auto index : entry.cu_indices)
24181 cpool.append_data (MAYBE_SWAP (index));
bc8f2430
JK
24182 }
24183 }
9291a0cd
TT
24184
24185 /* Now write out the hash table. */
bc8f2430 24186 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
4b76cda9 24187 for (const auto &entry : symtab->data)
9291a0cd
TT
24188 {
24189 offset_type str_off, vec_off;
24190
4b76cda9 24191 if (entry.name != NULL)
9291a0cd 24192 {
4b76cda9 24193 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
bc8f2430 24194 if (insertpair.second)
4b76cda9 24195 cpool.append_cstr0 (entry.name);
bc8f2430 24196 str_off = insertpair.first->second;
4b76cda9 24197 vec_off = entry.index_offset;
9291a0cd
TT
24198 }
24199 else
24200 {
24201 /* While 0 is a valid constant pool index, it is not valid
24202 to have 0 for both offsets. */
24203 str_off = 0;
24204 vec_off = 0;
24205 }
24206
bc8f2430
JK
24207 output.append_data (MAYBE_SWAP (str_off));
24208 output.append_data (MAYBE_SWAP (vec_off));
9291a0cd 24209 }
9291a0cd
TT
24210}
24211
bc8f2430 24212typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
0a5429f6
DE
24213
24214/* Helper struct for building the address table. */
24215struct addrmap_index_data
24216{
bc8f2430
JK
24217 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
24218 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
24219 {}
24220
0a5429f6 24221 struct objfile *objfile;
bc8f2430
JK
24222 data_buf &addr_vec;
24223 psym_index_map &cu_index_htab;
0a5429f6
DE
24224
24225 /* Non-zero if the previous_* fields are valid.
24226 We can't write an entry until we see the next entry (since it is only then
24227 that we know the end of the entry). */
24228 int previous_valid;
24229 /* Index of the CU in the table of all CUs in the index file. */
24230 unsigned int previous_cu_index;
0963b4bd 24231 /* Start address of the CU. */
0a5429f6
DE
24232 CORE_ADDR previous_cu_start;
24233};
24234
bc8f2430 24235/* Write an address entry to ADDR_VEC. */
b89be57b 24236
9291a0cd 24237static void
bc8f2430 24238add_address_entry (struct objfile *objfile, data_buf &addr_vec,
0a5429f6 24239 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 24240{
9291a0cd
TT
24241 CORE_ADDR baseaddr;
24242
24243 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24244
c2f134ac
PA
24245 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
24246 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
bc8f2430 24247 addr_vec.append_data (MAYBE_SWAP (cu_index));
0a5429f6
DE
24248}
24249
24250/* Worker function for traversing an addrmap to build the address table. */
24251
24252static int
24253add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
24254{
9a3c8263
SM
24255 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
24256 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
24257
24258 if (data->previous_valid)
bc8f2430 24259 add_address_entry (data->objfile, data->addr_vec,
0a5429f6
DE
24260 data->previous_cu_start, start_addr,
24261 data->previous_cu_index);
24262
24263 data->previous_cu_start = start_addr;
24264 if (pst != NULL)
24265 {
bc8f2430
JK
24266 const auto it = data->cu_index_htab.find (pst);
24267 gdb_assert (it != data->cu_index_htab.cend ());
24268 data->previous_cu_index = it->second;
0a5429f6
DE
24269 data->previous_valid = 1;
24270 }
24271 else
bc8f2430 24272 data->previous_valid = 0;
0a5429f6
DE
24273
24274 return 0;
24275}
24276
bc8f2430 24277/* Write OBJFILE's address map to ADDR_VEC.
0a5429f6
DE
24278 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
24279 in the index file. */
24280
24281static void
bc8f2430
JK
24282write_address_map (struct objfile *objfile, data_buf &addr_vec,
24283 psym_index_map &cu_index_htab)
0a5429f6 24284{
bc8f2430 24285 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
0a5429f6
DE
24286
24287 /* When writing the address table, we have to cope with the fact that
24288 the addrmap iterator only provides the start of a region; we have to
24289 wait until the next invocation to get the start of the next region. */
24290
24291 addrmap_index_data.objfile = objfile;
0a5429f6
DE
24292 addrmap_index_data.previous_valid = 0;
24293
24294 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
24295 &addrmap_index_data);
24296
24297 /* It's highly unlikely the last entry (end address = 0xff...ff)
24298 is valid, but we should still handle it.
24299 The end address is recorded as the start of the next region, but that
24300 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
24301 anyway. */
24302 if (addrmap_index_data.previous_valid)
bc8f2430 24303 add_address_entry (objfile, addr_vec,
0a5429f6
DE
24304 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
24305 addrmap_index_data.previous_cu_index);
9291a0cd
TT
24306}
24307
156942c7
DE
24308/* Return the symbol kind of PSYM. */
24309
24310static gdb_index_symbol_kind
24311symbol_kind (struct partial_symbol *psym)
24312{
24313 domain_enum domain = PSYMBOL_DOMAIN (psym);
24314 enum address_class aclass = PSYMBOL_CLASS (psym);
24315
24316 switch (domain)
24317 {
24318 case VAR_DOMAIN:
24319 switch (aclass)
24320 {
24321 case LOC_BLOCK:
24322 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
24323 case LOC_TYPEDEF:
24324 return GDB_INDEX_SYMBOL_KIND_TYPE;
24325 case LOC_COMPUTED:
24326 case LOC_CONST_BYTES:
24327 case LOC_OPTIMIZED_OUT:
24328 case LOC_STATIC:
24329 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
24330 case LOC_CONST:
24331 /* Note: It's currently impossible to recognize psyms as enum values
24332 short of reading the type info. For now punt. */
24333 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
24334 default:
24335 /* There are other LOC_FOO values that one might want to classify
24336 as variables, but dwarf2read.c doesn't currently use them. */
24337 return GDB_INDEX_SYMBOL_KIND_OTHER;
24338 }
24339 case STRUCT_DOMAIN:
24340 return GDB_INDEX_SYMBOL_KIND_TYPE;
24341 default:
24342 return GDB_INDEX_SYMBOL_KIND_OTHER;
24343 }
24344}
24345
9291a0cd 24346/* Add a list of partial symbols to SYMTAB. */
b89be57b 24347
9291a0cd
TT
24348static void
24349write_psymbols (struct mapped_symtab *symtab,
bc8f2430 24350 std::unordered_set<partial_symbol *> &psyms_seen,
9291a0cd
TT
24351 struct partial_symbol **psymp,
24352 int count,
987d643c
TT
24353 offset_type cu_index,
24354 int is_static)
9291a0cd
TT
24355{
24356 for (; count-- > 0; ++psymp)
24357 {
156942c7 24358 struct partial_symbol *psym = *psymp;
987d643c 24359
156942c7 24360 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 24361 error (_("Ada is not currently supported by the index"));
987d643c 24362
987d643c 24363 /* Only add a given psymbol once. */
bc8f2430 24364 if (psyms_seen.insert (psym).second)
987d643c 24365 {
156942c7
DE
24366 gdb_index_symbol_kind kind = symbol_kind (psym);
24367
156942c7
DE
24368 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
24369 is_static, kind, cu_index);
987d643c 24370 }
9291a0cd
TT
24371 }
24372}
24373
1fd400ff
TT
24374/* A helper struct used when iterating over debug_types. */
24375struct signatured_type_index_data
24376{
bc8f2430
JK
24377 signatured_type_index_data (data_buf &types_list_,
24378 std::unordered_set<partial_symbol *> &psyms_seen_)
24379 : types_list (types_list_), psyms_seen (psyms_seen_)
24380 {}
24381
1fd400ff
TT
24382 struct objfile *objfile;
24383 struct mapped_symtab *symtab;
bc8f2430
JK
24384 data_buf &types_list;
24385 std::unordered_set<partial_symbol *> &psyms_seen;
1fd400ff
TT
24386 int cu_index;
24387};
24388
24389/* A helper function that writes a single signatured_type to an
24390 obstack. */
b89be57b 24391
1fd400ff
TT
24392static int
24393write_one_signatured_type (void **slot, void *d)
24394{
9a3c8263
SM
24395 struct signatured_type_index_data *info
24396 = (struct signatured_type_index_data *) d;
1fd400ff 24397 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 24398 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
24399
24400 write_psymbols (info->symtab,
987d643c 24401 info->psyms_seen,
af5bf4ad 24402 &info->objfile->global_psymbols[psymtab->globals_offset],
987d643c
TT
24403 psymtab->n_global_syms, info->cu_index,
24404 0);
1fd400ff 24405 write_psymbols (info->symtab,
987d643c 24406 info->psyms_seen,
af5bf4ad 24407 &info->objfile->static_psymbols[psymtab->statics_offset],
987d643c
TT
24408 psymtab->n_static_syms, info->cu_index,
24409 1);
1fd400ff 24410
c2f134ac
PA
24411 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
24412 to_underlying (entry->per_cu.sect_off));
24413 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
24414 to_underlying (entry->type_offset_in_tu));
24415 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
1fd400ff
TT
24416
24417 ++info->cu_index;
24418
24419 return 1;
24420}
24421
e8f8bcb3
PA
24422/* Recurse into all "included" dependencies and count their symbols as
24423 if they appeared in this psymtab. */
24424
24425static void
24426recursively_count_psymbols (struct partial_symtab *psymtab,
24427 size_t &psyms_seen)
24428{
24429 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
24430 if (psymtab->dependencies[i]->user != NULL)
24431 recursively_count_psymbols (psymtab->dependencies[i],
24432 psyms_seen);
24433
24434 psyms_seen += psymtab->n_global_syms;
24435 psyms_seen += psymtab->n_static_syms;
24436}
24437
95554aad
TT
24438/* Recurse into all "included" dependencies and write their symbols as
24439 if they appeared in this psymtab. */
24440
24441static void
24442recursively_write_psymbols (struct objfile *objfile,
24443 struct partial_symtab *psymtab,
24444 struct mapped_symtab *symtab,
bc8f2430 24445 std::unordered_set<partial_symbol *> &psyms_seen,
95554aad
TT
24446 offset_type cu_index)
24447{
24448 int i;
24449
24450 for (i = 0; i < psymtab->number_of_dependencies; ++i)
24451 if (psymtab->dependencies[i]->user != NULL)
24452 recursively_write_psymbols (objfile, psymtab->dependencies[i],
24453 symtab, psyms_seen, cu_index);
24454
24455 write_psymbols (symtab,
24456 psyms_seen,
af5bf4ad 24457 &objfile->global_psymbols[psymtab->globals_offset],
95554aad
TT
24458 psymtab->n_global_syms, cu_index,
24459 0);
24460 write_psymbols (symtab,
24461 psyms_seen,
af5bf4ad 24462 &objfile->static_psymbols[psymtab->statics_offset],
95554aad
TT
24463 psymtab->n_static_syms, cu_index,
24464 1);
24465}
24466
9291a0cd 24467/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 24468
9291a0cd
TT
24469static void
24470write_psymtabs_to_index (struct objfile *objfile, const char *dir)
24471{
9291a0cd
TT
24472 if (dwarf2_per_objfile->using_index)
24473 error (_("Cannot use an index to create the index"));
24474
8b70b953
TT
24475 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
24476 error (_("Cannot make an index when the file has multiple .debug_types sections"));
24477
260b681b
DE
24478 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
24479 return;
24480
bc8f2430 24481 struct stat st;
4262abfb
JK
24482 if (stat (objfile_name (objfile), &st) < 0)
24483 perror_with_name (objfile_name (objfile));
9291a0cd 24484
bc8f2430
JK
24485 std::string filename (std::string (dir) + SLASH_STRING
24486 + lbasename (objfile_name (objfile)) + INDEX_SUFFIX);
9291a0cd 24487
d419f42d 24488 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
9291a0cd 24489 if (!out_file)
bc8f2430 24490 error (_("Can't open `%s' for writing"), filename.c_str ());
9291a0cd 24491
16b7a719
PA
24492 /* Order matters here; we want FILE to be closed before FILENAME is
24493 unlinked, because on MS-Windows one cannot delete a file that is
24494 still open. (Don't call anything here that might throw until
24495 file_closer is created.) */
bc8f2430 24496 gdb::unlinker unlink_file (filename.c_str ());
d419f42d 24497 gdb_file_up close_out_file (out_file);
9291a0cd 24498
bc8f2430
JK
24499 mapped_symtab symtab;
24500 data_buf cu_list;
987d643c 24501
0a5429f6
DE
24502 /* While we're scanning CU's create a table that maps a psymtab pointer
24503 (which is what addrmap records) to its index (which is what is recorded
24504 in the index file). This will later be needed to write the address
24505 table. */
bc8f2430
JK
24506 psym_index_map cu_index_htab;
24507 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
0a5429f6
DE
24508
24509 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
24510 work here. Also, the debug_types entries do not appear in
24511 all_comp_units, but only in their own hash table. */
e8f8bcb3
PA
24512
24513 /* The psyms_seen set is potentially going to be largish (~40k
24514 elements when indexing a -g3 build of GDB itself). Estimate the
24515 number of elements in order to avoid too many rehashes, which
24516 require rebuilding buckets and thus many trips to
24517 malloc/free. */
24518 size_t psyms_count = 0;
24519 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24520 {
24521 struct dwarf2_per_cu_data *per_cu
24522 = dwarf2_per_objfile->all_comp_units[i];
24523 struct partial_symtab *psymtab = per_cu->v.psymtab;
24524
24525 if (psymtab != NULL && psymtab->user == NULL)
24526 recursively_count_psymbols (psymtab, psyms_count);
24527 }
24528 /* Generating an index for gdb itself shows a ratio of
24529 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
24530 std::unordered_set<partial_symbol *> psyms_seen (psyms_count / 4);
bc8f2430 24531 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 24532 {
3e43a32a
MS
24533 struct dwarf2_per_cu_data *per_cu
24534 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 24535 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 24536
92fac807
JK
24537 /* CU of a shared file from 'dwz -m' may be unused by this main file.
24538 It may be referenced from a local scope but in such case it does not
24539 need to be present in .gdb_index. */
24540 if (psymtab == NULL)
24541 continue;
24542
95554aad 24543 if (psymtab->user == NULL)
bc8f2430
JK
24544 recursively_write_psymbols (objfile, psymtab, &symtab,
24545 psyms_seen, i);
9291a0cd 24546
bc8f2430
JK
24547 const auto insertpair = cu_index_htab.emplace (psymtab, i);
24548 gdb_assert (insertpair.second);
9291a0cd 24549
c2f134ac
PA
24550 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
24551 to_underlying (per_cu->sect_off));
24552 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
24553 }
24554
0a5429f6 24555 /* Dump the address map. */
bc8f2430
JK
24556 data_buf addr_vec;
24557 write_address_map (objfile, addr_vec, cu_index_htab);
0a5429f6 24558
1fd400ff 24559 /* Write out the .debug_type entries, if any. */
bc8f2430 24560 data_buf types_cu_list;
1fd400ff
TT
24561 if (dwarf2_per_objfile->signatured_types)
24562 {
bc8f2430
JK
24563 signatured_type_index_data sig_data (types_cu_list,
24564 psyms_seen);
1fd400ff
TT
24565
24566 sig_data.objfile = objfile;
bc8f2430 24567 sig_data.symtab = &symtab;
1fd400ff
TT
24568 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
24569 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
24570 write_one_signatured_type, &sig_data);
24571 }
24572
156942c7
DE
24573 /* Now that we've processed all symbols we can shrink their cu_indices
24574 lists. */
bc8f2430 24575 uniquify_cu_indices (&symtab);
156942c7 24576
bc8f2430
JK
24577 data_buf symtab_vec, constant_pool;
24578 write_hash_table (&symtab, symtab_vec, constant_pool);
9291a0cd 24579
bc8f2430
JK
24580 data_buf contents;
24581 const offset_type size_of_contents = 6 * sizeof (offset_type);
24582 offset_type total_len = size_of_contents;
9291a0cd
TT
24583
24584 /* The version number. */
bc8f2430 24585 contents.append_data (MAYBE_SWAP (8));
9291a0cd
TT
24586
24587 /* The offset of the CU list from the start of the file. */
bc8f2430
JK
24588 contents.append_data (MAYBE_SWAP (total_len));
24589 total_len += cu_list.size ();
9291a0cd 24590
1fd400ff 24591 /* The offset of the types CU list from the start of the file. */
bc8f2430
JK
24592 contents.append_data (MAYBE_SWAP (total_len));
24593 total_len += types_cu_list.size ();
1fd400ff 24594
9291a0cd 24595 /* The offset of the address table from the start of the file. */
bc8f2430
JK
24596 contents.append_data (MAYBE_SWAP (total_len));
24597 total_len += addr_vec.size ();
9291a0cd
TT
24598
24599 /* The offset of the symbol table from the start of the file. */
bc8f2430
JK
24600 contents.append_data (MAYBE_SWAP (total_len));
24601 total_len += symtab_vec.size ();
9291a0cd
TT
24602
24603 /* The offset of the constant pool from the start of the file. */
bc8f2430
JK
24604 contents.append_data (MAYBE_SWAP (total_len));
24605 total_len += constant_pool.size ();
9291a0cd 24606
bc8f2430 24607 gdb_assert (contents.size () == size_of_contents);
9291a0cd 24608
bc8f2430
JK
24609 contents.file_write (out_file);
24610 cu_list.file_write (out_file);
24611 types_cu_list.file_write (out_file);
24612 addr_vec.file_write (out_file);
24613 symtab_vec.file_write (out_file);
24614 constant_pool.file_write (out_file);
9291a0cd 24615
bef155c3
TT
24616 /* We want to keep the file. */
24617 unlink_file.keep ();
9291a0cd
TT
24618}
24619
90476074
TT
24620/* Implementation of the `save gdb-index' command.
24621
24622 Note that the file format used by this command is documented in the
24623 GDB manual. Any changes here must be documented there. */
11570e71 24624
9291a0cd 24625static void
8384c356 24626save_gdb_index_command (const char *arg, int from_tty)
9291a0cd
TT
24627{
24628 struct objfile *objfile;
24629
24630 if (!arg || !*arg)
96d19272 24631 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
24632
24633 ALL_OBJFILES (objfile)
24634 {
24635 struct stat st;
24636
24637 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 24638 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
24639 continue;
24640
9a3c8263
SM
24641 dwarf2_per_objfile
24642 = (struct dwarf2_per_objfile *) objfile_data (objfile,
24643 dwarf2_objfile_data_key);
9291a0cd
TT
24644 if (dwarf2_per_objfile)
24645 {
9291a0cd 24646
492d29ea 24647 TRY
9291a0cd
TT
24648 {
24649 write_psymtabs_to_index (objfile, arg);
24650 }
492d29ea
PA
24651 CATCH (except, RETURN_MASK_ERROR)
24652 {
24653 exception_fprintf (gdb_stderr, except,
24654 _("Error while writing index for `%s': "),
24655 objfile_name (objfile));
24656 }
24657 END_CATCH
9291a0cd
TT
24658 }
24659 }
dce234bc
PP
24660}
24661
9291a0cd
TT
24662\f
24663
b4f54984 24664int dwarf_always_disassemble;
9eae7c52
TT
24665
24666static void
b4f54984
DE
24667show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24668 struct cmd_list_element *c, const char *value)
9eae7c52 24669{
3e43a32a
MS
24670 fprintf_filtered (file,
24671 _("Whether to always disassemble "
24672 "DWARF expressions is %s.\n"),
9eae7c52
TT
24673 value);
24674}
24675
900e11f9
JK
24676static void
24677show_check_physname (struct ui_file *file, int from_tty,
24678 struct cmd_list_element *c, const char *value)
24679{
24680 fprintf_filtered (file,
24681 _("Whether to check \"physname\" is %s.\n"),
24682 value);
24683}
24684
6502dd73
DJ
24685void
24686_initialize_dwarf2_read (void)
24687{
96d19272
JK
24688 struct cmd_list_element *c;
24689
dce234bc 24690 dwarf2_objfile_data_key
c1bd65d0 24691 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 24692
b4f54984
DE
24693 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24694Set DWARF specific variables.\n\
24695Configure DWARF variables such as the cache size"),
24696 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
24697 0/*allow-unknown*/, &maintenance_set_cmdlist);
24698
b4f54984
DE
24699 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24700Show DWARF specific variables\n\
24701Show DWARF variables such as the cache size"),
24702 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
24703 0/*allow-unknown*/, &maintenance_show_cmdlist);
24704
24705 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
24706 &dwarf_max_cache_age, _("\
24707Set the upper bound on the age of cached DWARF compilation units."), _("\
24708Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
24709A higher limit means that cached compilation units will be stored\n\
24710in memory longer, and more total memory will be used. Zero disables\n\
24711caching, which can slow down startup."),
2c5b56ce 24712 NULL,
b4f54984
DE
24713 show_dwarf_max_cache_age,
24714 &set_dwarf_cmdlist,
24715 &show_dwarf_cmdlist);
d97bc12b 24716
9eae7c52 24717 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 24718 &dwarf_always_disassemble, _("\
9eae7c52
TT
24719Set whether `info address' always disassembles DWARF expressions."), _("\
24720Show whether `info address' always disassembles DWARF expressions."), _("\
24721When enabled, DWARF expressions are always printed in an assembly-like\n\
24722syntax. When disabled, expressions will be printed in a more\n\
24723conversational style, when possible."),
24724 NULL,
b4f54984
DE
24725 show_dwarf_always_disassemble,
24726 &set_dwarf_cmdlist,
24727 &show_dwarf_cmdlist);
24728
24729 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24730Set debugging of the DWARF reader."), _("\
24731Show debugging of the DWARF reader."), _("\
24732When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
24733reading and symtab expansion. A value of 1 (one) provides basic\n\
24734information. A value greater than 1 provides more verbose information."),
45cfd468
DE
24735 NULL,
24736 NULL,
24737 &setdebuglist, &showdebuglist);
24738
b4f54984
DE
24739 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24740Set debugging of the DWARF DIE reader."), _("\
24741Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
24742When enabled (non-zero), DIEs are dumped after they are read in.\n\
24743The value is the maximum depth to print."),
ccce17b0
YQ
24744 NULL,
24745 NULL,
24746 &setdebuglist, &showdebuglist);
9291a0cd 24747
27e0867f
DE
24748 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24749Set debugging of the dwarf line reader."), _("\
24750Show debugging of the dwarf line reader."), _("\
24751When enabled (non-zero), line number entries are dumped as they are read in.\n\
24752A value of 1 (one) provides basic information.\n\
24753A value greater than 1 provides more verbose information."),
24754 NULL,
24755 NULL,
24756 &setdebuglist, &showdebuglist);
24757
900e11f9
JK
24758 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24759Set cross-checking of \"physname\" code against demangler."), _("\
24760Show cross-checking of \"physname\" code against demangler."), _("\
24761When enabled, GDB's internal \"physname\" code is checked against\n\
24762the demangler."),
24763 NULL, show_check_physname,
24764 &setdebuglist, &showdebuglist);
24765
e615022a
DE
24766 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24767 no_class, &use_deprecated_index_sections, _("\
24768Set whether to use deprecated gdb_index sections."), _("\
24769Show whether to use deprecated gdb_index sections."), _("\
24770When enabled, deprecated .gdb_index sections are used anyway.\n\
24771Normally they are ignored either because of a missing feature or\n\
24772performance issue.\n\
24773Warning: This option must be enabled before gdb reads the file."),
24774 NULL,
24775 NULL,
24776 &setlist, &showlist);
24777
96d19272 24778 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 24779 _("\
fc1a9d6e 24780Save a gdb-index file.\n\
11570e71 24781Usage: save gdb-index DIRECTORY"),
96d19272
JK
24782 &save_cmdlist);
24783 set_cmd_completer (c, filename_completer);
f1e6e072
TT
24784
24785 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24786 &dwarf2_locexpr_funcs);
24787 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24788 &dwarf2_loclist_funcs);
24789
24790 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24791 &dwarf2_block_frame_base_locexpr_funcs);
24792 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24793 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24794
24795#if GDB_SELF_TEST
24796 selftests::register_test ("dw2_expand_symtabs_matching",
24797 selftests::dw2_expand_symtabs_matching::run_test);
24798#endif
6502dd73 24799}
This page took 4.13956 seconds and 4 git commands to generate.